Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1 | /* $OpenBSD: ssh-agent.c,v 1.28 2000/04/14 10:30:33 markus Exp $ */ |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 2 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
| 5 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 6 | * All rights reserved |
| 7 | * Created: Wed Mar 29 03:46:59 1995 ylo |
| 8 | * The authentication agent program. |
| 9 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 10 | |
| 11 | #include "includes.h" |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 12 | RCSID("$OpenBSD: ssh-agent.c,v 1.28 2000/04/14 10:30:33 markus Exp $"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 13 | |
| 14 | #include "ssh.h" |
| 15 | #include "rsa.h" |
| 16 | #include "authfd.h" |
| 17 | #include "buffer.h" |
| 18 | #include "bufaux.h" |
| 19 | #include "xmalloc.h" |
| 20 | #include "packet.h" |
| 21 | #include "getput.h" |
| 22 | #include "mpaux.h" |
| 23 | |
| 24 | #include <openssl/md5.h> |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 25 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 26 | typedef struct { |
| 27 | int fd; |
| 28 | enum { |
| 29 | AUTH_UNUSED, AUTH_SOCKET, AUTH_CONNECTION |
| 30 | } type; |
| 31 | Buffer input; |
| 32 | Buffer output; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 33 | } SocketEntry; |
| 34 | |
| 35 | unsigned int sockets_alloc = 0; |
| 36 | SocketEntry *sockets = NULL; |
| 37 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 38 | typedef struct { |
| 39 | RSA *key; |
| 40 | char *comment; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 41 | } Identity; |
| 42 | |
| 43 | unsigned int num_identities = 0; |
| 44 | Identity *identities = NULL; |
| 45 | |
| 46 | int max_fd = 0; |
| 47 | |
| 48 | /* pid of shell == parent of agent */ |
| 49 | int parent_pid = -1; |
| 50 | |
| 51 | /* pathname and directory for AUTH_SOCKET */ |
| 52 | char socket_name[1024]; |
| 53 | char socket_dir[1024]; |
| 54 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 55 | #ifdef HAVE___PROGNAME |
| 56 | extern char *__progname; |
| 57 | #else /* HAVE___PROGNAME */ |
| 58 | const char *__progname = "ssh-agent"; |
| 59 | #endif /* HAVE___PROGNAME */ |
| 60 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 61 | void |
| 62 | process_request_identity(SocketEntry *e) |
| 63 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 64 | Buffer msg; |
| 65 | int i; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 66 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 67 | buffer_init(&msg); |
| 68 | buffer_put_char(&msg, SSH_AGENT_RSA_IDENTITIES_ANSWER); |
| 69 | buffer_put_int(&msg, num_identities); |
| 70 | for (i = 0; i < num_identities; i++) { |
| 71 | buffer_put_int(&msg, BN_num_bits(identities[i].key->n)); |
| 72 | buffer_put_bignum(&msg, identities[i].key->e); |
| 73 | buffer_put_bignum(&msg, identities[i].key->n); |
| 74 | buffer_put_string(&msg, identities[i].comment, |
| 75 | strlen(identities[i].comment)); |
| 76 | } |
| 77 | buffer_put_int(&e->output, buffer_len(&msg)); |
| 78 | buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg)); |
| 79 | buffer_free(&msg); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | void |
| 83 | process_authentication_challenge(SocketEntry *e) |
| 84 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 85 | int i, pub_bits, len; |
| 86 | BIGNUM *pub_e, *pub_n, *challenge; |
| 87 | Buffer msg; |
| 88 | MD5_CTX md; |
| 89 | unsigned char buf[32], mdbuf[16], session_id[16]; |
| 90 | unsigned int response_type; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 91 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 92 | buffer_init(&msg); |
| 93 | pub_e = BN_new(); |
| 94 | pub_n = BN_new(); |
| 95 | challenge = BN_new(); |
| 96 | pub_bits = buffer_get_int(&e->input); |
| 97 | buffer_get_bignum(&e->input, pub_e); |
| 98 | buffer_get_bignum(&e->input, pub_n); |
| 99 | buffer_get_bignum(&e->input, challenge); |
| 100 | if (buffer_len(&e->input) == 0) { |
| 101 | /* Compatibility code for old servers. */ |
| 102 | memset(session_id, 0, 16); |
| 103 | response_type = 0; |
| 104 | } else { |
| 105 | /* New code. */ |
| 106 | buffer_get(&e->input, (char *) session_id, 16); |
| 107 | response_type = buffer_get_int(&e->input); |
| 108 | } |
| 109 | for (i = 0; i < num_identities; i++) |
| 110 | if (pub_bits == BN_num_bits(identities[i].key->n) && |
| 111 | BN_cmp(pub_e, identities[i].key->e) == 0 && |
| 112 | BN_cmp(pub_n, identities[i].key->n) == 0) { |
| 113 | /* Decrypt the challenge using the private key. */ |
| 114 | rsa_private_decrypt(challenge, challenge, identities[i].key); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 115 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 116 | /* Compute the desired response. */ |
| 117 | switch (response_type) { |
| 118 | case 0:/* As of protocol 1.0 */ |
| 119 | /* This response type is no longer supported. */ |
| 120 | log("Compatibility with ssh protocol 1.0 no longer supported."); |
| 121 | buffer_put_char(&msg, SSH_AGENT_FAILURE); |
| 122 | goto send; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 123 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 124 | case 1:/* As of protocol 1.1 */ |
| 125 | /* The response is MD5 of decrypted challenge plus session id. */ |
| 126 | len = BN_num_bytes(challenge); |
Damien Miller | fd7c911 | 1999-11-08 16:15:55 +1100 | [diff] [blame] | 127 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 128 | if (len <= 0 || len > 32) { |
| 129 | fatal("process_authentication_challenge: " |
| 130 | "bad challenge length %d", len); |
| 131 | } |
| 132 | memset(buf, 0, 32); |
| 133 | BN_bn2bin(challenge, buf + 32 - len); |
| 134 | MD5_Init(&md); |
| 135 | MD5_Update(&md, buf, 32); |
| 136 | MD5_Update(&md, session_id, 16); |
| 137 | MD5_Final(mdbuf, &md); |
| 138 | break; |
Damien Miller | fd7c911 | 1999-11-08 16:15:55 +1100 | [diff] [blame] | 139 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 140 | default: |
| 141 | fatal("process_authentication_challenge: bad response_type %d", |
| 142 | response_type); |
| 143 | break; |
| 144 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 145 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 146 | /* Send the response. */ |
| 147 | buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE); |
| 148 | for (i = 0; i < 16; i++) |
| 149 | buffer_put_char(&msg, mdbuf[i]); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 150 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 151 | goto send; |
| 152 | } |
| 153 | /* Unknown identity. Send failure. */ |
| 154 | buffer_put_char(&msg, SSH_AGENT_FAILURE); |
| 155 | send: |
| 156 | buffer_put_int(&e->output, buffer_len(&msg)); |
| 157 | buffer_append(&e->output, buffer_ptr(&msg), |
| 158 | buffer_len(&msg)); |
| 159 | buffer_free(&msg); |
| 160 | BN_clear_free(pub_e); |
| 161 | BN_clear_free(pub_n); |
| 162 | BN_clear_free(challenge); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | void |
| 166 | process_remove_identity(SocketEntry *e) |
| 167 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 168 | unsigned int bits; |
| 169 | unsigned int i; |
| 170 | BIGNUM *dummy, *n; |
Damien Miller | 7e8e820 | 1999-11-16 13:37:16 +1100 | [diff] [blame] | 171 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 172 | dummy = BN_new(); |
| 173 | n = BN_new(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 174 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 175 | /* Get the key from the packet. */ |
| 176 | bits = buffer_get_int(&e->input); |
| 177 | buffer_get_bignum(&e->input, dummy); |
| 178 | buffer_get_bignum(&e->input, n); |
| 179 | |
| 180 | if (bits != BN_num_bits(n)) |
Damien Miller | 5e7c10e | 1999-12-16 13:18:04 +1100 | [diff] [blame] | 181 | error("Warning: identity keysize mismatch: actual %d, announced %d", |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 182 | BN_num_bits(n), bits); |
| 183 | |
| 184 | /* Check if we have the key. */ |
| 185 | for (i = 0; i < num_identities; i++) |
| 186 | if (BN_cmp(identities[i].key->n, n) == 0) { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 187 | /* |
| 188 | * We have this key. Free the old key. Since we |
| 189 | * don\'t want to leave empty slots in the middle of |
| 190 | * the array, we actually free the key there and copy |
| 191 | * data from the last entry. |
| 192 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 193 | RSA_free(identities[i].key); |
| 194 | xfree(identities[i].comment); |
| 195 | if (i < num_identities - 1) |
| 196 | identities[i] = identities[num_identities - 1]; |
| 197 | num_identities--; |
| 198 | BN_clear_free(dummy); |
| 199 | BN_clear_free(n); |
| 200 | |
| 201 | /* Send success. */ |
| 202 | buffer_put_int(&e->output, 1); |
| 203 | buffer_put_char(&e->output, SSH_AGENT_SUCCESS); |
| 204 | return; |
| 205 | } |
| 206 | /* We did not have the key. */ |
| 207 | BN_clear(dummy); |
| 208 | BN_clear(n); |
| 209 | |
| 210 | /* Send failure. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 211 | buffer_put_int(&e->output, 1); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 212 | buffer_put_char(&e->output, SSH_AGENT_FAILURE); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 213 | } |
| 214 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 215 | /* |
| 216 | * Removes all identities from the agent. |
| 217 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 218 | void |
| 219 | process_remove_all_identities(SocketEntry *e) |
| 220 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 221 | unsigned int i; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 222 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 223 | /* Loop over all identities and clear the keys. */ |
| 224 | for (i = 0; i < num_identities; i++) { |
| 225 | RSA_free(identities[i].key); |
| 226 | xfree(identities[i].comment); |
| 227 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 228 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 229 | /* Mark that there are no identities. */ |
| 230 | num_identities = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 231 | |
| 232 | /* Send success. */ |
| 233 | buffer_put_int(&e->output, 1); |
| 234 | buffer_put_char(&e->output, SSH_AGENT_SUCCESS); |
| 235 | return; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 236 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 237 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 238 | /* |
| 239 | * Adds an identity to the agent. |
| 240 | */ |
| 241 | void |
| 242 | process_add_identity(SocketEntry *e) |
| 243 | { |
| 244 | RSA *k; |
| 245 | int i; |
| 246 | BIGNUM *aux; |
| 247 | BN_CTX *ctx; |
| 248 | |
| 249 | if (num_identities == 0) |
| 250 | identities = xmalloc(sizeof(Identity)); |
| 251 | else |
| 252 | identities = xrealloc(identities, (num_identities + 1) * sizeof(Identity)); |
| 253 | |
| 254 | identities[num_identities].key = RSA_new(); |
| 255 | k = identities[num_identities].key; |
| 256 | buffer_get_int(&e->input); /* bits */ |
| 257 | k->n = BN_new(); |
| 258 | buffer_get_bignum(&e->input, k->n); |
| 259 | k->e = BN_new(); |
| 260 | buffer_get_bignum(&e->input, k->e); |
| 261 | k->d = BN_new(); |
| 262 | buffer_get_bignum(&e->input, k->d); |
| 263 | k->iqmp = BN_new(); |
| 264 | buffer_get_bignum(&e->input, k->iqmp); |
| 265 | /* SSH and SSL have p and q swapped */ |
| 266 | k->q = BN_new(); |
| 267 | buffer_get_bignum(&e->input, k->q); /* p */ |
| 268 | k->p = BN_new(); |
| 269 | buffer_get_bignum(&e->input, k->p); /* q */ |
| 270 | |
| 271 | /* Generate additional parameters */ |
| 272 | aux = BN_new(); |
| 273 | ctx = BN_CTX_new(); |
| 274 | |
| 275 | BN_sub(aux, k->q, BN_value_one()); |
| 276 | k->dmq1 = BN_new(); |
| 277 | BN_mod(k->dmq1, k->d, aux, ctx); |
| 278 | |
| 279 | BN_sub(aux, k->p, BN_value_one()); |
| 280 | k->dmp1 = BN_new(); |
| 281 | BN_mod(k->dmp1, k->d, aux, ctx); |
| 282 | |
| 283 | BN_clear_free(aux); |
| 284 | BN_CTX_free(ctx); |
| 285 | |
| 286 | identities[num_identities].comment = buffer_get_string(&e->input, NULL); |
| 287 | |
| 288 | /* Check if we already have the key. */ |
| 289 | for (i = 0; i < num_identities; i++) |
| 290 | if (BN_cmp(identities[i].key->n, k->n) == 0) { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 291 | /* |
| 292 | * We already have this key. Clear and free the new |
| 293 | * data and return success. |
| 294 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 295 | RSA_free(k); |
| 296 | xfree(identities[num_identities].comment); |
| 297 | |
| 298 | /* Send success. */ |
| 299 | buffer_put_int(&e->output, 1); |
| 300 | buffer_put_char(&e->output, SSH_AGENT_SUCCESS); |
| 301 | return; |
| 302 | } |
| 303 | /* Increment the number of identities. */ |
| 304 | num_identities++; |
| 305 | |
| 306 | /* Send a success message. */ |
| 307 | buffer_put_int(&e->output, 1); |
| 308 | buffer_put_char(&e->output, SSH_AGENT_SUCCESS); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | void |
| 312 | process_message(SocketEntry *e) |
| 313 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 314 | unsigned int msg_len; |
| 315 | unsigned int type; |
| 316 | unsigned char *cp; |
| 317 | if (buffer_len(&e->input) < 5) |
| 318 | return; /* Incomplete message. */ |
| 319 | cp = (unsigned char *) buffer_ptr(&e->input); |
| 320 | msg_len = GET_32BIT(cp); |
| 321 | if (msg_len > 256 * 1024) { |
| 322 | shutdown(e->fd, SHUT_RDWR); |
| 323 | close(e->fd); |
| 324 | e->type = AUTH_UNUSED; |
| 325 | return; |
| 326 | } |
| 327 | if (buffer_len(&e->input) < msg_len + 4) |
| 328 | return; |
| 329 | buffer_consume(&e->input, 4); |
| 330 | type = buffer_get_char(&e->input); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 331 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 332 | switch (type) { |
| 333 | case SSH_AGENTC_REQUEST_RSA_IDENTITIES: |
| 334 | process_request_identity(e); |
| 335 | break; |
| 336 | case SSH_AGENTC_RSA_CHALLENGE: |
| 337 | process_authentication_challenge(e); |
| 338 | break; |
| 339 | case SSH_AGENTC_ADD_RSA_IDENTITY: |
| 340 | process_add_identity(e); |
| 341 | break; |
| 342 | case SSH_AGENTC_REMOVE_RSA_IDENTITY: |
| 343 | process_remove_identity(e); |
| 344 | break; |
| 345 | case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES: |
| 346 | process_remove_all_identities(e); |
| 347 | break; |
| 348 | default: |
| 349 | /* Unknown message. Respond with failure. */ |
| 350 | error("Unknown message %d", type); |
| 351 | buffer_clear(&e->input); |
| 352 | buffer_put_int(&e->output, 1); |
| 353 | buffer_put_char(&e->output, SSH_AGENT_FAILURE); |
| 354 | break; |
| 355 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | void |
| 359 | new_socket(int type, int fd) |
| 360 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 361 | unsigned int i, old_alloc; |
| 362 | if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) |
| 363 | error("fcntl O_NONBLOCK: %s", strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 364 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 365 | if (fd > max_fd) |
| 366 | max_fd = fd; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 367 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 368 | for (i = 0; i < sockets_alloc; i++) |
| 369 | if (sockets[i].type == AUTH_UNUSED) { |
| 370 | sockets[i].fd = fd; |
| 371 | sockets[i].type = type; |
| 372 | buffer_init(&sockets[i].input); |
| 373 | buffer_init(&sockets[i].output); |
| 374 | return; |
| 375 | } |
| 376 | old_alloc = sockets_alloc; |
| 377 | sockets_alloc += 10; |
| 378 | if (sockets) |
| 379 | sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0])); |
| 380 | else |
| 381 | sockets = xmalloc(sockets_alloc * sizeof(sockets[0])); |
| 382 | for (i = old_alloc; i < sockets_alloc; i++) |
| 383 | sockets[i].type = AUTH_UNUSED; |
| 384 | sockets[old_alloc].type = type; |
| 385 | sockets[old_alloc].fd = fd; |
| 386 | buffer_init(&sockets[old_alloc].input); |
| 387 | buffer_init(&sockets[old_alloc].output); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | void |
| 391 | prepare_select(fd_set *readset, fd_set *writeset) |
| 392 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 393 | unsigned int i; |
| 394 | for (i = 0; i < sockets_alloc; i++) |
| 395 | switch (sockets[i].type) { |
| 396 | case AUTH_SOCKET: |
| 397 | case AUTH_CONNECTION: |
| 398 | FD_SET(sockets[i].fd, readset); |
| 399 | if (buffer_len(&sockets[i].output) > 0) |
| 400 | FD_SET(sockets[i].fd, writeset); |
| 401 | break; |
| 402 | case AUTH_UNUSED: |
| 403 | break; |
| 404 | default: |
| 405 | fatal("Unknown socket type %d", sockets[i].type); |
| 406 | break; |
| 407 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 408 | } |
| 409 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 410 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 411 | after_select(fd_set *readset, fd_set *writeset) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 412 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 413 | unsigned int i; |
| 414 | int len, sock; |
Damien Miller | 7684ee1 | 2000-03-17 23:40:15 +1100 | [diff] [blame] | 415 | socklen_t slen; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 416 | char buf[1024]; |
| 417 | struct sockaddr_un sunaddr; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 418 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 419 | for (i = 0; i < sockets_alloc; i++) |
| 420 | switch (sockets[i].type) { |
| 421 | case AUTH_UNUSED: |
| 422 | break; |
| 423 | case AUTH_SOCKET: |
| 424 | if (FD_ISSET(sockets[i].fd, readset)) { |
Damien Miller | 7684ee1 | 2000-03-17 23:40:15 +1100 | [diff] [blame] | 425 | slen = sizeof(sunaddr); |
| 426 | sock = accept(sockets[i].fd, (struct sockaddr *) & sunaddr, &slen); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 427 | if (sock < 0) { |
| 428 | perror("accept from AUTH_SOCKET"); |
| 429 | break; |
| 430 | } |
| 431 | new_socket(AUTH_CONNECTION, sock); |
| 432 | } |
| 433 | break; |
| 434 | case AUTH_CONNECTION: |
| 435 | if (buffer_len(&sockets[i].output) > 0 && |
| 436 | FD_ISSET(sockets[i].fd, writeset)) { |
| 437 | len = write(sockets[i].fd, buffer_ptr(&sockets[i].output), |
| 438 | buffer_len(&sockets[i].output)); |
| 439 | if (len <= 0) { |
| 440 | shutdown(sockets[i].fd, SHUT_RDWR); |
| 441 | close(sockets[i].fd); |
| 442 | sockets[i].type = AUTH_UNUSED; |
| 443 | break; |
| 444 | } |
| 445 | buffer_consume(&sockets[i].output, len); |
| 446 | } |
| 447 | if (FD_ISSET(sockets[i].fd, readset)) { |
| 448 | len = read(sockets[i].fd, buf, sizeof(buf)); |
| 449 | if (len <= 0) { |
| 450 | shutdown(sockets[i].fd, SHUT_RDWR); |
| 451 | close(sockets[i].fd); |
| 452 | sockets[i].type = AUTH_UNUSED; |
| 453 | break; |
| 454 | } |
| 455 | buffer_append(&sockets[i].input, buf, len); |
| 456 | process_message(&sockets[i]); |
| 457 | } |
| 458 | break; |
| 459 | default: |
| 460 | fatal("Unknown type %d", sockets[i].type); |
| 461 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | void |
| 465 | check_parent_exists(int sig) |
| 466 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 467 | if (kill(parent_pid, 0) < 0) { |
| 468 | /* printf("Parent has died - Authentication agent exiting.\n"); */ |
| 469 | exit(1); |
| 470 | } |
| 471 | signal(SIGALRM, check_parent_exists); |
| 472 | alarm(10); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 473 | } |
| 474 | |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 475 | void |
| 476 | cleanup_socket(void) |
| 477 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 478 | remove(socket_name); |
| 479 | rmdir(socket_dir); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 480 | } |
| 481 | |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 482 | void |
| 483 | cleanup_exit(int i) |
| 484 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 485 | cleanup_socket(); |
| 486 | exit(i); |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | void |
| 490 | usage() |
| 491 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 492 | fprintf(stderr, "ssh-agent version %s\n", SSH_VERSION); |
| 493 | fprintf(stderr, "Usage: %s [-c | -s] [-k] [command {args...]]\n", |
| 494 | __progname); |
| 495 | exit(1); |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 496 | } |
| 497 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 498 | int |
| 499 | main(int ac, char **av) |
| 500 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 501 | fd_set readset, writeset; |
| 502 | int sock, c_flag = 0, k_flag = 0, s_flag = 0, ch; |
| 503 | struct sockaddr_un sunaddr; |
| 504 | pid_t pid; |
| 505 | char *shell, *format, *pidstr, pidstrbuf[1 + 3 * sizeof pid]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 506 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 507 | /* check if RSA support exists */ |
| 508 | if (rsa_alive() == 0) { |
| 509 | fprintf(stderr, |
| 510 | "%s: no RSA support in libssl and libcrypto. See ssl(8).\n", |
| 511 | __progname); |
| 512 | exit(1); |
| 513 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 514 | while ((ch = getopt(ac, av, "cks")) != -1) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 515 | switch (ch) { |
| 516 | case 'c': |
| 517 | if (s_flag) |
| 518 | usage(); |
| 519 | c_flag++; |
| 520 | break; |
| 521 | case 'k': |
| 522 | k_flag++; |
| 523 | break; |
| 524 | case 's': |
| 525 | if (c_flag) |
| 526 | usage(); |
| 527 | s_flag++; |
| 528 | break; |
| 529 | default: |
| 530 | usage(); |
| 531 | } |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 532 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 533 | ac -= optind; |
| 534 | av += optind; |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 535 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 536 | if (ac > 0 && (c_flag || k_flag || s_flag)) |
| 537 | usage(); |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 538 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 539 | if (ac == 0 && !c_flag && !k_flag && !s_flag) { |
| 540 | shell = getenv("SHELL"); |
| 541 | if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0) |
| 542 | c_flag = 1; |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 543 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 544 | if (k_flag) { |
| 545 | pidstr = getenv(SSH_AGENTPID_ENV_NAME); |
| 546 | if (pidstr == NULL) { |
| 547 | fprintf(stderr, "%s not set, cannot kill agent\n", |
| 548 | SSH_AGENTPID_ENV_NAME); |
| 549 | exit(1); |
| 550 | } |
| 551 | pid = atoi(pidstr); |
| 552 | if (pid < 1) { /* XXX PID_MAX check too */ |
| 553 | fprintf(stderr, "%s=\"%s\", which is not a good PID\n", |
| 554 | SSH_AGENTPID_ENV_NAME, pidstr); |
| 555 | exit(1); |
| 556 | } |
| 557 | if (kill(pid, SIGTERM) == -1) { |
| 558 | perror("kill"); |
| 559 | exit(1); |
| 560 | } |
| 561 | format = c_flag ? "unsetenv %s;\n" : "unset %s;\n"; |
| 562 | printf(format, SSH_AUTHSOCKET_ENV_NAME); |
| 563 | printf(format, SSH_AGENTPID_ENV_NAME); |
| 564 | printf("echo Agent pid %d killed;\n", pid); |
| 565 | exit(0); |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 566 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 567 | parent_pid = getpid(); |
| 568 | |
| 569 | /* Create private directory for agent socket */ |
| 570 | strlcpy(socket_dir, "/tmp/ssh-XXXXXXXX", sizeof socket_dir); |
| 571 | if (mkdtemp(socket_dir) == NULL) { |
| 572 | perror("mkdtemp: private socket dir"); |
| 573 | exit(1); |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 574 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 575 | snprintf(socket_name, sizeof socket_name, "%s/agent.%d", socket_dir, |
| 576 | parent_pid); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 577 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 578 | /* |
| 579 | * Create socket early so it will exist before command gets run from |
| 580 | * the parent. |
| 581 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 582 | sock = socket(AF_UNIX, SOCK_STREAM, 0); |
| 583 | if (sock < 0) { |
| 584 | perror("socket"); |
| 585 | cleanup_exit(1); |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 586 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 587 | memset(&sunaddr, 0, sizeof(sunaddr)); |
| 588 | sunaddr.sun_family = AF_UNIX; |
| 589 | strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path)); |
| 590 | if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) { |
| 591 | perror("bind"); |
| 592 | cleanup_exit(1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 593 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 594 | if (listen(sock, 5) < 0) { |
| 595 | perror("listen"); |
| 596 | cleanup_exit(1); |
| 597 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 598 | /* |
| 599 | * Fork, and have the parent execute the command, if any, or present |
| 600 | * the socket data. The child continues as the authentication agent. |
| 601 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 602 | pid = fork(); |
| 603 | if (pid == -1) { |
| 604 | perror("fork"); |
| 605 | exit(1); |
| 606 | } |
| 607 | if (pid != 0) { /* Parent - execute the given command. */ |
| 608 | close(sock); |
| 609 | snprintf(pidstrbuf, sizeof pidstrbuf, "%d", pid); |
| 610 | if (ac == 0) { |
| 611 | format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n"; |
| 612 | printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name, |
| 613 | SSH_AUTHSOCKET_ENV_NAME); |
| 614 | printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf, |
| 615 | SSH_AGENTPID_ENV_NAME); |
| 616 | printf("echo Agent pid %d;\n", pid); |
| 617 | exit(0); |
| 618 | } |
| 619 | setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1); |
| 620 | setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1); |
| 621 | execvp(av[0], av); |
| 622 | perror(av[0]); |
| 623 | exit(1); |
| 624 | } |
| 625 | close(0); |
| 626 | close(1); |
| 627 | close(2); |
| 628 | |
| 629 | if (setsid() == -1) { |
| 630 | perror("setsid"); |
| 631 | cleanup_exit(1); |
| 632 | } |
| 633 | if (atexit(cleanup_socket) < 0) { |
| 634 | perror("atexit"); |
| 635 | cleanup_exit(1); |
| 636 | } |
| 637 | new_socket(AUTH_SOCKET, sock); |
| 638 | if (ac > 0) { |
| 639 | signal(SIGALRM, check_parent_exists); |
| 640 | alarm(10); |
| 641 | } |
| 642 | signal(SIGINT, SIG_IGN); |
| 643 | signal(SIGPIPE, SIG_IGN); |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 644 | signal(SIGHUP, cleanup_exit); |
| 645 | signal(SIGTERM, cleanup_exit); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 646 | while (1) { |
| 647 | FD_ZERO(&readset); |
| 648 | FD_ZERO(&writeset); |
| 649 | prepare_select(&readset, &writeset); |
| 650 | if (select(max_fd + 1, &readset, &writeset, NULL, NULL) < 0) { |
| 651 | if (errno == EINTR) |
| 652 | continue; |
| 653 | exit(1); |
| 654 | } |
| 655 | after_select(&readset, &writeset); |
| 656 | } |
| 657 | /* NOTREACHED */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 658 | } |