Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame^] | 1 | /* $OpenBSD: ssh-agent.c,v 1.34 2000/08/31 22:09:34 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. |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 9 | * |
| 10 | * SSH2 implementation, |
| 11 | * Copyright (c) 2000 Markus Friedl. All rights reserved. |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 12 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 13 | |
| 14 | #include "includes.h" |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame^] | 15 | RCSID("$OpenBSD: ssh-agent.c,v 1.34 2000/08/31 22:09:34 markus Exp $"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 16 | |
| 17 | #include "ssh.h" |
| 18 | #include "rsa.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 19 | #include "buffer.h" |
| 20 | #include "bufaux.h" |
| 21 | #include "xmalloc.h" |
| 22 | #include "packet.h" |
| 23 | #include "getput.h" |
| 24 | #include "mpaux.h" |
| 25 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 26 | #include <openssl/evp.h> |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 27 | #include <openssl/md5.h> |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 28 | #include <openssl/dsa.h> |
| 29 | #include <openssl/rsa.h> |
| 30 | #include "key.h" |
| 31 | #include "authfd.h" |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 32 | #include "dsa.h" |
| 33 | #include "kex.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 34 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 35 | typedef struct { |
| 36 | int fd; |
| 37 | enum { |
| 38 | AUTH_UNUSED, AUTH_SOCKET, AUTH_CONNECTION |
| 39 | } type; |
| 40 | Buffer input; |
| 41 | Buffer output; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 42 | } SocketEntry; |
| 43 | |
| 44 | unsigned int sockets_alloc = 0; |
| 45 | SocketEntry *sockets = NULL; |
| 46 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 47 | typedef struct { |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 48 | Key *key; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 49 | char *comment; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 50 | } Identity; |
| 51 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 52 | typedef struct { |
| 53 | int nentries; |
| 54 | Identity *identities; |
| 55 | } Idtab; |
| 56 | |
| 57 | /* private key table, one per protocol version */ |
| 58 | Idtab idtable[3]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 59 | |
| 60 | int max_fd = 0; |
| 61 | |
| 62 | /* pid of shell == parent of agent */ |
Damien Miller | 166fca8 | 2000-04-20 07:42:21 +1000 | [diff] [blame] | 63 | pid_t parent_pid = -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 64 | |
| 65 | /* pathname and directory for AUTH_SOCKET */ |
| 66 | char socket_name[1024]; |
| 67 | char socket_dir[1024]; |
| 68 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 69 | #ifdef HAVE___PROGNAME |
| 70 | extern char *__progname; |
| 71 | #else /* HAVE___PROGNAME */ |
Damien Miller | 70fb671 | 2000-05-01 20:59:50 +1000 | [diff] [blame] | 72 | static const char *__progname = "ssh-agent"; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 73 | #endif /* HAVE___PROGNAME */ |
| 74 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 75 | void |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 76 | idtab_init(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 77 | { |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 78 | int i; |
| 79 | for (i = 0; i <=2; i++){ |
| 80 | idtable[i].identities = NULL; |
| 81 | idtable[i].nentries = 0; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /* return private key table for requested protocol version */ |
| 86 | Idtab * |
| 87 | idtab_lookup(int version) |
| 88 | { |
| 89 | if (version < 1 || version > 2) |
| 90 | fatal("internal error, bad protocol version %d", version); |
| 91 | return &idtable[version]; |
| 92 | } |
| 93 | |
| 94 | /* return matching private key for given public key */ |
| 95 | Key * |
| 96 | lookup_private_key(Key *key, int *idx, int version) |
| 97 | { |
| 98 | int i; |
| 99 | Idtab *tab = idtab_lookup(version); |
| 100 | for (i = 0; i < tab->nentries; i++) { |
| 101 | if (key_equal(key, tab->identities[i].key)) { |
| 102 | if (idx != NULL) |
| 103 | *idx = i; |
| 104 | return tab->identities[i].key; |
| 105 | } |
| 106 | } |
| 107 | return NULL; |
| 108 | } |
| 109 | |
| 110 | /* send list of supported public keys to 'client' */ |
| 111 | void |
| 112 | process_request_identities(SocketEntry *e, int version) |
| 113 | { |
| 114 | Idtab *tab = idtab_lookup(version); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 115 | Buffer msg; |
| 116 | int i; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 117 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 118 | buffer_init(&msg); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 119 | buffer_put_char(&msg, (version == 1) ? |
| 120 | SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER); |
| 121 | buffer_put_int(&msg, tab->nentries); |
| 122 | for (i = 0; i < tab->nentries; i++) { |
| 123 | Identity *id = &tab->identities[i]; |
| 124 | if (id->key->type == KEY_RSA) { |
| 125 | buffer_put_int(&msg, BN_num_bits(id->key->rsa->n)); |
| 126 | buffer_put_bignum(&msg, id->key->rsa->e); |
| 127 | buffer_put_bignum(&msg, id->key->rsa->n); |
| 128 | } else { |
| 129 | unsigned char *blob; |
| 130 | unsigned int blen; |
| 131 | dsa_make_key_blob(id->key, &blob, &blen); |
| 132 | buffer_put_string(&msg, blob, blen); |
| 133 | xfree(blob); |
| 134 | } |
| 135 | buffer_put_cstring(&msg, id->comment); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 136 | } |
| 137 | buffer_put_int(&e->output, buffer_len(&msg)); |
| 138 | buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg)); |
| 139 | buffer_free(&msg); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 140 | } |
| 141 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 142 | /* ssh1 only */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 143 | void |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 144 | process_authentication_challenge1(SocketEntry *e) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 145 | { |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 146 | Key *key, *private; |
| 147 | BIGNUM *challenge; |
| 148 | int i, len; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 149 | Buffer msg; |
| 150 | MD5_CTX md; |
| 151 | unsigned char buf[32], mdbuf[16], session_id[16]; |
| 152 | unsigned int response_type; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 153 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 154 | buffer_init(&msg); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 155 | key = key_new(KEY_RSA); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 156 | challenge = BN_new(); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 157 | |
| 158 | buffer_get_int(&e->input); /* ignored */ |
| 159 | buffer_get_bignum(&e->input, key->rsa->e); |
| 160 | buffer_get_bignum(&e->input, key->rsa->n); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 161 | buffer_get_bignum(&e->input, challenge); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 162 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 163 | /* Only protocol 1.1 is supported */ |
| 164 | if (buffer_len(&e->input) == 0) |
| 165 | goto failure; |
| 166 | buffer_get(&e->input, (char *) session_id, 16); |
| 167 | response_type = buffer_get_int(&e->input); |
| 168 | if (response_type != 1) |
| 169 | goto failure; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 170 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 171 | private = lookup_private_key(key, NULL, 1); |
| 172 | if (private != NULL) { |
| 173 | /* Decrypt the challenge using the private key. */ |
| 174 | rsa_private_decrypt(challenge, challenge, private->rsa); |
Damien Miller | fd7c911 | 1999-11-08 16:15:55 +1100 | [diff] [blame] | 175 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 176 | /* The response is MD5 of decrypted challenge plus session id. */ |
| 177 | len = BN_num_bytes(challenge); |
| 178 | if (len <= 0 || len > 32) { |
| 179 | log("process_authentication_challenge: bad challenge length %d", len); |
| 180 | goto failure; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 181 | } |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 182 | memset(buf, 0, 32); |
| 183 | BN_bn2bin(challenge, buf + 32 - len); |
| 184 | MD5_Init(&md); |
| 185 | MD5_Update(&md, buf, 32); |
| 186 | MD5_Update(&md, session_id, 16); |
| 187 | MD5_Final(mdbuf, &md); |
| 188 | |
| 189 | /* Send the response. */ |
| 190 | buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE); |
| 191 | for (i = 0; i < 16; i++) |
| 192 | buffer_put_char(&msg, mdbuf[i]); |
| 193 | goto send; |
| 194 | } |
| 195 | |
| 196 | failure: |
| 197 | /* Unknown identity or protocol error. Send failure. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 198 | buffer_put_char(&msg, SSH_AGENT_FAILURE); |
| 199 | send: |
| 200 | buffer_put_int(&e->output, buffer_len(&msg)); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 201 | buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg)); |
| 202 | key_free(key); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 203 | BN_clear_free(challenge); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 204 | buffer_free(&msg); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 205 | } |
| 206 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 207 | /* ssh2 only */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 208 | void |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 209 | process_sign_request2(SocketEntry *e) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 210 | { |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 211 | extern int datafellows; |
| 212 | Key *key, *private; |
| 213 | unsigned char *blob, *data, *signature = NULL; |
| 214 | unsigned int blen, dlen, slen = 0; |
| 215 | Buffer msg; |
| 216 | int ok = -1; |
| 217 | |
| 218 | datafellows = 0; |
| 219 | |
| 220 | blob = buffer_get_string(&e->input, &blen); |
| 221 | data = buffer_get_string(&e->input, &dlen); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame^] | 222 | buffer_get_int(&e->input); /* flags, unused */ |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 223 | |
| 224 | key = dsa_key_from_blob(blob, blen); |
| 225 | if (key != NULL) { |
| 226 | private = lookup_private_key(key, NULL, 2); |
| 227 | if (private != NULL) |
| 228 | ok = dsa_sign(private, &signature, &slen, data, dlen); |
| 229 | } |
| 230 | key_free(key); |
| 231 | buffer_init(&msg); |
| 232 | if (ok == 0) { |
| 233 | buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE); |
| 234 | buffer_put_string(&msg, signature, slen); |
| 235 | } else { |
| 236 | buffer_put_char(&msg, SSH_AGENT_FAILURE); |
| 237 | } |
| 238 | buffer_put_int(&e->output, buffer_len(&msg)); |
| 239 | buffer_append(&e->output, buffer_ptr(&msg), |
| 240 | buffer_len(&msg)); |
| 241 | buffer_free(&msg); |
| 242 | xfree(data); |
| 243 | xfree(blob); |
| 244 | if (signature != NULL) |
| 245 | xfree(signature); |
| 246 | } |
| 247 | |
| 248 | /* shared */ |
| 249 | void |
| 250 | process_remove_identity(SocketEntry *e, int version) |
| 251 | { |
| 252 | Key *key = NULL, *private; |
| 253 | unsigned char *blob; |
| 254 | unsigned int blen; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 255 | unsigned int bits; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 256 | int success = 0; |
Damien Miller | 7e8e820 | 1999-11-16 13:37:16 +1100 | [diff] [blame] | 257 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 258 | switch(version){ |
| 259 | case 1: |
| 260 | key = key_new(KEY_RSA); |
| 261 | bits = buffer_get_int(&e->input); |
| 262 | buffer_get_bignum(&e->input, key->rsa->e); |
| 263 | buffer_get_bignum(&e->input, key->rsa->n); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 264 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 265 | if (bits != key_size(key)) |
| 266 | log("Warning: identity keysize mismatch: actual %d, announced %d", |
| 267 | key_size(key), bits); |
| 268 | break; |
| 269 | case 2: |
| 270 | blob = buffer_get_string(&e->input, &blen); |
| 271 | key = dsa_key_from_blob(blob, blen); |
| 272 | xfree(blob); |
| 273 | break; |
| 274 | } |
| 275 | if (key != NULL) { |
| 276 | int idx; |
| 277 | private = lookup_private_key(key, &idx, version); |
| 278 | if (private != NULL) { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 279 | /* |
| 280 | * We have this key. Free the old key. Since we |
| 281 | * don\'t want to leave empty slots in the middle of |
| 282 | * the array, we actually free the key there and copy |
| 283 | * data from the last entry. |
| 284 | */ |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 285 | Idtab *tab = idtab_lookup(version); |
| 286 | key_free(tab->identities[idx].key); |
| 287 | xfree(tab->identities[idx].comment); |
| 288 | if (idx != tab->nentries) |
| 289 | tab->identities[idx] = tab->identities[tab->nentries]; |
| 290 | tab->nentries--; |
| 291 | success = 1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 292 | } |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 293 | key_free(key); |
| 294 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 295 | buffer_put_int(&e->output, 1); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 296 | buffer_put_char(&e->output, |
| 297 | success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 298 | } |
| 299 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 300 | void |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 301 | process_remove_all_identities(SocketEntry *e, int version) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 302 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 303 | unsigned int i; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 304 | Idtab *tab = idtab_lookup(version); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 305 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 306 | /* Loop over all identities and clear the keys. */ |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 307 | for (i = 0; i < tab->nentries; i++) { |
| 308 | key_free(tab->identities[i].key); |
| 309 | xfree(tab->identities[i].comment); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 310 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 311 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 312 | /* Mark that there are no identities. */ |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 313 | tab->nentries = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 314 | |
| 315 | /* Send success. */ |
| 316 | buffer_put_int(&e->output, 1); |
| 317 | buffer_put_char(&e->output, SSH_AGENT_SUCCESS); |
| 318 | return; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 319 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 320 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 321 | void |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 322 | process_add_identity(SocketEntry *e, int version) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 323 | { |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 324 | Key *k = NULL; |
| 325 | RSA *rsa; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 326 | BIGNUM *aux; |
| 327 | BN_CTX *ctx; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 328 | char *type; |
| 329 | char *comment; |
| 330 | int success = 0; |
| 331 | Idtab *tab = idtab_lookup(version); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 332 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 333 | switch (version) { |
| 334 | case 1: |
| 335 | k = key_new(KEY_RSA); |
| 336 | rsa = k->rsa; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 337 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 338 | /* allocate mem for private key */ |
| 339 | /* XXX rsa->n and rsa->e are already allocated */ |
| 340 | rsa->d = BN_new(); |
| 341 | rsa->iqmp = BN_new(); |
| 342 | rsa->q = BN_new(); |
| 343 | rsa->p = BN_new(); |
| 344 | rsa->dmq1 = BN_new(); |
| 345 | rsa->dmp1 = BN_new(); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 346 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 347 | buffer_get_int(&e->input); /* ignored */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 348 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 349 | buffer_get_bignum(&e->input, rsa->n); |
| 350 | buffer_get_bignum(&e->input, rsa->e); |
| 351 | buffer_get_bignum(&e->input, rsa->d); |
| 352 | buffer_get_bignum(&e->input, rsa->iqmp); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 353 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 354 | /* SSH and SSL have p and q swapped */ |
| 355 | buffer_get_bignum(&e->input, rsa->q); /* p */ |
| 356 | buffer_get_bignum(&e->input, rsa->p); /* q */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 357 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 358 | /* Generate additional parameters */ |
| 359 | aux = BN_new(); |
| 360 | ctx = BN_CTX_new(); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 361 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 362 | BN_sub(aux, rsa->q, BN_value_one()); |
| 363 | BN_mod(rsa->dmq1, rsa->d, aux, ctx); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 364 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 365 | BN_sub(aux, rsa->p, BN_value_one()); |
| 366 | BN_mod(rsa->dmp1, rsa->d, aux, ctx); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 367 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 368 | BN_clear_free(aux); |
| 369 | BN_CTX_free(ctx); |
| 370 | |
| 371 | break; |
| 372 | case 2: |
| 373 | type = buffer_get_string(&e->input, NULL); |
| 374 | if (strcmp(type, KEX_DSS)) { |
| 375 | buffer_clear(&e->input); |
| 376 | xfree(type); |
| 377 | goto send; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 378 | } |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 379 | xfree(type); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 380 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 381 | k = key_new(KEY_DSA); |
| 382 | |
| 383 | /* allocate mem for private key */ |
| 384 | k->dsa->priv_key = BN_new(); |
| 385 | |
| 386 | buffer_get_bignum2(&e->input, k->dsa->p); |
| 387 | buffer_get_bignum2(&e->input, k->dsa->q); |
| 388 | buffer_get_bignum2(&e->input, k->dsa->g); |
| 389 | buffer_get_bignum2(&e->input, k->dsa->pub_key); |
| 390 | buffer_get_bignum2(&e->input, k->dsa->priv_key); |
| 391 | |
| 392 | break; |
| 393 | } |
| 394 | |
| 395 | comment = buffer_get_string(&e->input, NULL); |
| 396 | if (k == NULL) { |
| 397 | xfree(comment); |
| 398 | goto send; |
| 399 | } |
| 400 | success = 1; |
| 401 | if (lookup_private_key(k, NULL, version) == NULL) { |
| 402 | if (tab->nentries == 0) |
| 403 | tab->identities = xmalloc(sizeof(Identity)); |
| 404 | else |
| 405 | tab->identities = xrealloc(tab->identities, |
| 406 | (tab->nentries + 1) * sizeof(Identity)); |
| 407 | tab->identities[tab->nentries].key = k; |
| 408 | tab->identities[tab->nentries].comment = comment; |
| 409 | /* Increment the number of identities. */ |
| 410 | tab->nentries++; |
| 411 | } else { |
| 412 | key_free(k); |
| 413 | xfree(comment); |
| 414 | } |
| 415 | send: |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 416 | buffer_put_int(&e->output, 1); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 417 | buffer_put_char(&e->output, |
| 418 | success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 419 | } |
| 420 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 421 | /* dispatch incoming messages */ |
| 422 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 423 | void |
| 424 | process_message(SocketEntry *e) |
| 425 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 426 | unsigned int msg_len; |
| 427 | unsigned int type; |
| 428 | unsigned char *cp; |
| 429 | if (buffer_len(&e->input) < 5) |
| 430 | return; /* Incomplete message. */ |
| 431 | cp = (unsigned char *) buffer_ptr(&e->input); |
| 432 | msg_len = GET_32BIT(cp); |
| 433 | if (msg_len > 256 * 1024) { |
| 434 | shutdown(e->fd, SHUT_RDWR); |
| 435 | close(e->fd); |
| 436 | e->type = AUTH_UNUSED; |
| 437 | return; |
| 438 | } |
| 439 | if (buffer_len(&e->input) < msg_len + 4) |
| 440 | return; |
| 441 | buffer_consume(&e->input, 4); |
| 442 | type = buffer_get_char(&e->input); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 443 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 444 | switch (type) { |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 445 | /* ssh1 */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 446 | case SSH_AGENTC_RSA_CHALLENGE: |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 447 | process_authentication_challenge1(e); |
| 448 | break; |
| 449 | case SSH_AGENTC_REQUEST_RSA_IDENTITIES: |
| 450 | process_request_identities(e, 1); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 451 | break; |
| 452 | case SSH_AGENTC_ADD_RSA_IDENTITY: |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 453 | process_add_identity(e, 1); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 454 | break; |
| 455 | case SSH_AGENTC_REMOVE_RSA_IDENTITY: |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 456 | process_remove_identity(e, 1); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 457 | break; |
| 458 | case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES: |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 459 | process_remove_all_identities(e, 1); |
| 460 | break; |
| 461 | /* ssh2 */ |
| 462 | case SSH2_AGENTC_SIGN_REQUEST: |
| 463 | process_sign_request2(e); |
| 464 | break; |
| 465 | case SSH2_AGENTC_REQUEST_IDENTITIES: |
| 466 | process_request_identities(e, 2); |
| 467 | break; |
| 468 | case SSH2_AGENTC_ADD_IDENTITY: |
| 469 | process_add_identity(e, 2); |
| 470 | break; |
| 471 | case SSH2_AGENTC_REMOVE_IDENTITY: |
| 472 | process_remove_identity(e, 2); |
| 473 | break; |
| 474 | case SSH2_AGENTC_REMOVE_ALL_IDENTITIES: |
| 475 | process_remove_all_identities(e, 2); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 476 | break; |
| 477 | default: |
| 478 | /* Unknown message. Respond with failure. */ |
| 479 | error("Unknown message %d", type); |
| 480 | buffer_clear(&e->input); |
| 481 | buffer_put_int(&e->output, 1); |
| 482 | buffer_put_char(&e->output, SSH_AGENT_FAILURE); |
| 483 | break; |
| 484 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | void |
| 488 | new_socket(int type, int fd) |
| 489 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 490 | unsigned int i, old_alloc; |
| 491 | if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) |
| 492 | error("fcntl O_NONBLOCK: %s", strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 493 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 494 | if (fd > max_fd) |
| 495 | max_fd = fd; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 496 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 497 | for (i = 0; i < sockets_alloc; i++) |
| 498 | if (sockets[i].type == AUTH_UNUSED) { |
| 499 | sockets[i].fd = fd; |
| 500 | sockets[i].type = type; |
| 501 | buffer_init(&sockets[i].input); |
| 502 | buffer_init(&sockets[i].output); |
| 503 | return; |
| 504 | } |
| 505 | old_alloc = sockets_alloc; |
| 506 | sockets_alloc += 10; |
| 507 | if (sockets) |
| 508 | sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0])); |
| 509 | else |
| 510 | sockets = xmalloc(sockets_alloc * sizeof(sockets[0])); |
| 511 | for (i = old_alloc; i < sockets_alloc; i++) |
| 512 | sockets[i].type = AUTH_UNUSED; |
| 513 | sockets[old_alloc].type = type; |
| 514 | sockets[old_alloc].fd = fd; |
| 515 | buffer_init(&sockets[old_alloc].input); |
| 516 | buffer_init(&sockets[old_alloc].output); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | void |
| 520 | prepare_select(fd_set *readset, fd_set *writeset) |
| 521 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 522 | unsigned int i; |
| 523 | for (i = 0; i < sockets_alloc; i++) |
| 524 | switch (sockets[i].type) { |
| 525 | case AUTH_SOCKET: |
| 526 | case AUTH_CONNECTION: |
| 527 | FD_SET(sockets[i].fd, readset); |
| 528 | if (buffer_len(&sockets[i].output) > 0) |
| 529 | FD_SET(sockets[i].fd, writeset); |
| 530 | break; |
| 531 | case AUTH_UNUSED: |
| 532 | break; |
| 533 | default: |
| 534 | fatal("Unknown socket type %d", sockets[i].type); |
| 535 | break; |
| 536 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 537 | } |
| 538 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 539 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 540 | after_select(fd_set *readset, fd_set *writeset) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 541 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 542 | unsigned int i; |
| 543 | int len, sock; |
Damien Miller | 7684ee1 | 2000-03-17 23:40:15 +1100 | [diff] [blame] | 544 | socklen_t slen; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 545 | char buf[1024]; |
| 546 | struct sockaddr_un sunaddr; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 547 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 548 | for (i = 0; i < sockets_alloc; i++) |
| 549 | switch (sockets[i].type) { |
| 550 | case AUTH_UNUSED: |
| 551 | break; |
| 552 | case AUTH_SOCKET: |
| 553 | if (FD_ISSET(sockets[i].fd, readset)) { |
Damien Miller | 7684ee1 | 2000-03-17 23:40:15 +1100 | [diff] [blame] | 554 | slen = sizeof(sunaddr); |
| 555 | sock = accept(sockets[i].fd, (struct sockaddr *) & sunaddr, &slen); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 556 | if (sock < 0) { |
| 557 | perror("accept from AUTH_SOCKET"); |
| 558 | break; |
| 559 | } |
| 560 | new_socket(AUTH_CONNECTION, sock); |
| 561 | } |
| 562 | break; |
| 563 | case AUTH_CONNECTION: |
| 564 | if (buffer_len(&sockets[i].output) > 0 && |
| 565 | FD_ISSET(sockets[i].fd, writeset)) { |
| 566 | len = write(sockets[i].fd, buffer_ptr(&sockets[i].output), |
| 567 | buffer_len(&sockets[i].output)); |
| 568 | if (len <= 0) { |
| 569 | shutdown(sockets[i].fd, SHUT_RDWR); |
| 570 | close(sockets[i].fd); |
| 571 | sockets[i].type = AUTH_UNUSED; |
Damien Miller | a552faf | 2000-04-21 15:55:20 +1000 | [diff] [blame] | 572 | buffer_free(&sockets[i].input); |
| 573 | buffer_free(&sockets[i].output); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 574 | break; |
| 575 | } |
| 576 | buffer_consume(&sockets[i].output, len); |
| 577 | } |
| 578 | if (FD_ISSET(sockets[i].fd, readset)) { |
| 579 | len = read(sockets[i].fd, buf, sizeof(buf)); |
| 580 | if (len <= 0) { |
| 581 | shutdown(sockets[i].fd, SHUT_RDWR); |
| 582 | close(sockets[i].fd); |
| 583 | sockets[i].type = AUTH_UNUSED; |
Damien Miller | a552faf | 2000-04-21 15:55:20 +1000 | [diff] [blame] | 584 | buffer_free(&sockets[i].input); |
| 585 | buffer_free(&sockets[i].output); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 586 | break; |
| 587 | } |
| 588 | buffer_append(&sockets[i].input, buf, len); |
| 589 | process_message(&sockets[i]); |
| 590 | } |
| 591 | break; |
| 592 | default: |
| 593 | fatal("Unknown type %d", sockets[i].type); |
| 594 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | void |
| 598 | check_parent_exists(int sig) |
| 599 | { |
Damien Miller | 166fca8 | 2000-04-20 07:42:21 +1000 | [diff] [blame] | 600 | if (parent_pid != -1 && kill(parent_pid, 0) < 0) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 601 | /* printf("Parent has died - Authentication agent exiting.\n"); */ |
| 602 | exit(1); |
| 603 | } |
| 604 | signal(SIGALRM, check_parent_exists); |
| 605 | alarm(10); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 606 | } |
| 607 | |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 608 | void |
| 609 | cleanup_socket(void) |
| 610 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 611 | remove(socket_name); |
| 612 | rmdir(socket_dir); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 613 | } |
| 614 | |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 615 | void |
| 616 | cleanup_exit(int i) |
| 617 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 618 | cleanup_socket(); |
| 619 | exit(i); |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | void |
| 623 | usage() |
| 624 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 625 | fprintf(stderr, "ssh-agent version %s\n", SSH_VERSION); |
| 626 | fprintf(stderr, "Usage: %s [-c | -s] [-k] [command {args...]]\n", |
| 627 | __progname); |
| 628 | exit(1); |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 629 | } |
| 630 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 631 | int |
| 632 | main(int ac, char **av) |
| 633 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 634 | fd_set readset, writeset; |
| 635 | int sock, c_flag = 0, k_flag = 0, s_flag = 0, ch; |
| 636 | struct sockaddr_un sunaddr; |
| 637 | pid_t pid; |
| 638 | char *shell, *format, *pidstr, pidstrbuf[1 + 3 * sizeof pid]; |
Damien Miller | 615f939 | 2000-05-17 22:53:33 +1000 | [diff] [blame] | 639 | extern int optind; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 640 | |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 641 | init_rng(); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 642 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 643 | /* check if RSA support exists */ |
| 644 | if (rsa_alive() == 0) { |
| 645 | fprintf(stderr, |
| 646 | "%s: no RSA support in libssl and libcrypto. See ssl(8).\n", |
| 647 | __progname); |
| 648 | exit(1); |
| 649 | } |
Damien Miller | d0cff3e | 2000-04-20 23:12:58 +1000 | [diff] [blame] | 650 | #ifdef __GNU_LIBRARY__ |
| 651 | while ((ch = getopt(ac, av, "+cks")) != -1) { |
| 652 | #else /* __GNU_LIBRARY__ */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 653 | while ((ch = getopt(ac, av, "cks")) != -1) { |
Damien Miller | d0cff3e | 2000-04-20 23:12:58 +1000 | [diff] [blame] | 654 | #endif /* __GNU_LIBRARY__ */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 655 | switch (ch) { |
| 656 | case 'c': |
| 657 | if (s_flag) |
| 658 | usage(); |
| 659 | c_flag++; |
| 660 | break; |
| 661 | case 'k': |
| 662 | k_flag++; |
| 663 | break; |
| 664 | case 's': |
| 665 | if (c_flag) |
| 666 | usage(); |
| 667 | s_flag++; |
| 668 | break; |
| 669 | default: |
| 670 | usage(); |
| 671 | } |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 672 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 673 | ac -= optind; |
| 674 | av += optind; |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 675 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 676 | if (ac > 0 && (c_flag || k_flag || s_flag)) |
| 677 | usage(); |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 678 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 679 | if (ac == 0 && !c_flag && !k_flag && !s_flag) { |
| 680 | shell = getenv("SHELL"); |
| 681 | if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0) |
| 682 | c_flag = 1; |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 683 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 684 | if (k_flag) { |
| 685 | pidstr = getenv(SSH_AGENTPID_ENV_NAME); |
| 686 | if (pidstr == NULL) { |
| 687 | fprintf(stderr, "%s not set, cannot kill agent\n", |
| 688 | SSH_AGENTPID_ENV_NAME); |
| 689 | exit(1); |
| 690 | } |
| 691 | pid = atoi(pidstr); |
| 692 | if (pid < 1) { /* XXX PID_MAX check too */ |
Damien Miller | 166fca8 | 2000-04-20 07:42:21 +1000 | [diff] [blame] | 693 | /* Yes, PID_MAX check please */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 694 | fprintf(stderr, "%s=\"%s\", which is not a good PID\n", |
| 695 | SSH_AGENTPID_ENV_NAME, pidstr); |
| 696 | exit(1); |
| 697 | } |
| 698 | if (kill(pid, SIGTERM) == -1) { |
| 699 | perror("kill"); |
| 700 | exit(1); |
| 701 | } |
| 702 | format = c_flag ? "unsetenv %s;\n" : "unset %s;\n"; |
| 703 | printf(format, SSH_AUTHSOCKET_ENV_NAME); |
| 704 | printf(format, SSH_AGENTPID_ENV_NAME); |
| 705 | printf("echo Agent pid %d killed;\n", pid); |
| 706 | exit(0); |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 707 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 708 | parent_pid = getpid(); |
| 709 | |
| 710 | /* Create private directory for agent socket */ |
| 711 | strlcpy(socket_dir, "/tmp/ssh-XXXXXXXX", sizeof socket_dir); |
| 712 | if (mkdtemp(socket_dir) == NULL) { |
| 713 | perror("mkdtemp: private socket dir"); |
| 714 | exit(1); |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 715 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 716 | snprintf(socket_name, sizeof socket_name, "%s/agent.%d", socket_dir, |
| 717 | parent_pid); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 718 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 719 | /* |
| 720 | * Create socket early so it will exist before command gets run from |
| 721 | * the parent. |
| 722 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 723 | sock = socket(AF_UNIX, SOCK_STREAM, 0); |
| 724 | if (sock < 0) { |
| 725 | perror("socket"); |
| 726 | cleanup_exit(1); |
Damien Miller | 792c511 | 1999-10-29 09:47:09 +1000 | [diff] [blame] | 727 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 728 | memset(&sunaddr, 0, sizeof(sunaddr)); |
| 729 | sunaddr.sun_family = AF_UNIX; |
| 730 | strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path)); |
| 731 | if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) { |
| 732 | perror("bind"); |
| 733 | cleanup_exit(1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 734 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 735 | if (listen(sock, 5) < 0) { |
| 736 | perror("listen"); |
| 737 | cleanup_exit(1); |
| 738 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 739 | /* |
| 740 | * Fork, and have the parent execute the command, if any, or present |
| 741 | * the socket data. The child continues as the authentication agent. |
| 742 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 743 | pid = fork(); |
| 744 | if (pid == -1) { |
| 745 | perror("fork"); |
| 746 | exit(1); |
| 747 | } |
| 748 | if (pid != 0) { /* Parent - execute the given command. */ |
| 749 | close(sock); |
| 750 | snprintf(pidstrbuf, sizeof pidstrbuf, "%d", pid); |
| 751 | if (ac == 0) { |
| 752 | format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n"; |
| 753 | printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name, |
| 754 | SSH_AUTHSOCKET_ENV_NAME); |
| 755 | printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf, |
| 756 | SSH_AGENTPID_ENV_NAME); |
| 757 | printf("echo Agent pid %d;\n", pid); |
| 758 | exit(0); |
| 759 | } |
| 760 | setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1); |
| 761 | setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1); |
| 762 | execvp(av[0], av); |
| 763 | perror(av[0]); |
| 764 | exit(1); |
| 765 | } |
| 766 | close(0); |
| 767 | close(1); |
| 768 | close(2); |
| 769 | |
| 770 | if (setsid() == -1) { |
| 771 | perror("setsid"); |
| 772 | cleanup_exit(1); |
| 773 | } |
| 774 | if (atexit(cleanup_socket) < 0) { |
| 775 | perror("atexit"); |
| 776 | cleanup_exit(1); |
| 777 | } |
| 778 | new_socket(AUTH_SOCKET, sock); |
| 779 | if (ac > 0) { |
| 780 | signal(SIGALRM, check_parent_exists); |
| 781 | alarm(10); |
| 782 | } |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 783 | idtab_init(); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 784 | signal(SIGINT, SIG_IGN); |
| 785 | signal(SIGPIPE, SIG_IGN); |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 786 | signal(SIGHUP, cleanup_exit); |
| 787 | signal(SIGTERM, cleanup_exit); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 788 | while (1) { |
| 789 | FD_ZERO(&readset); |
| 790 | FD_ZERO(&writeset); |
| 791 | prepare_select(&readset, &writeset); |
| 792 | if (select(max_fd + 1, &readset, &writeset, NULL, NULL) < 0) { |
| 793 | if (errno == EINTR) |
| 794 | continue; |
| 795 | exit(1); |
| 796 | } |
| 797 | after_select(&readset, &writeset); |
| 798 | } |
| 799 | /* NOTREACHED */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 800 | } |