Annotation of src/usr.bin/ssh/ssh-agent.c, Revision 1.196
1.196 ! deraadt 1: /* $OpenBSD: ssh-agent.c,v 1.195 2015/01/14 19:33:41 djm Exp $ */
1.1 deraadt 2: /*
1.22 deraadt 3: * Author: Tatu Ylonen <ylo@cs.hut.fi>
4: * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5: * All rights reserved
6: * The authentication agent program.
1.33 markus 7: *
1.35 deraadt 8: * As far as I am concerned, the code I have written for this software
9: * can be used freely for any purpose. Any derived versions of this
10: * software must be clearly marked as such, and if the derived work is
11: * incompatible with the protocol description in the RFC file, it must be
12: * called by a name other than "ssh" or "Secure Shell".
13: *
1.56 markus 14: * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
1.35 deraadt 15: *
16: * Redistribution and use in source and binary forms, with or without
17: * modification, are permitted provided that the following conditions
18: * are met:
19: * 1. Redistributions of source code must retain the above copyright
20: * notice, this list of conditions and the following disclaimer.
21: * 2. Redistributions in binary form must reproduce the above copyright
22: * notice, this list of conditions and the following disclaimer in the
23: * documentation and/or other materials provided with the distribution.
24: *
25: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.22 deraadt 35: */
1.1 deraadt 36:
1.196 ! deraadt 37: #include <sys/param.h> /* MIN MAX */
1.151 deraadt 38: #include <sys/types.h>
1.153 djm 39: #include <sys/time.h>
1.78 provos 40: #include <sys/queue.h>
1.127 stevesk 41: #include <sys/resource.h>
1.141 stevesk 42: #include <sys/socket.h>
1.189 djm 43: #include <sys/stat.h>
1.128 stevesk 44: #include <sys/un.h>
1.126 stevesk 45:
1.185 markus 46: #ifdef WITH_OPENSSL
1.146 stevesk 47: #include <openssl/evp.h>
1.185 markus 48: #endif
1.146 stevesk 49:
1.143 stevesk 50: #include <errno.h>
1.142 stevesk 51: #include <fcntl.h>
1.126 stevesk 52: #include <paths.h>
1.129 stevesk 53: #include <signal.h>
1.149 stevesk 54: #include <stdlib.h>
1.150 stevesk 55: #include <stdio.h>
1.146 stevesk 56: #include <string.h>
1.196 ! deraadt 57: #include <limits.h>
1.145 stevesk 58: #include <time.h>
1.144 stevesk 59: #include <unistd.h>
1.1 deraadt 60:
1.194 markus 61: #include "key.h" /* XXX for typedef */
62: #include "buffer.h" /* XXX for typedef */
63:
1.151 deraadt 64: #include "xmalloc.h"
1.1 deraadt 65: #include "ssh.h"
66: #include "rsa.h"
1.194 markus 67: #include "sshbuf.h"
68: #include "sshkey.h"
1.32 markus 69: #include "authfd.h"
1.37 markus 70: #include "compat.h"
1.47 markus 71: #include "log.h"
1.107 markus 72: #include "misc.h"
1.182 markus 73: #include "digest.h"
1.194 markus 74: #include "ssherr.h"
1.7 deraadt 75:
1.163 markus 76: #ifdef ENABLE_PKCS11
77: #include "ssh-pkcs11.h"
1.71 jakob 78: #endif
1.59 markus 79:
1.73 stevesk 80: typedef enum {
81: AUTH_UNUSED,
82: AUTH_SOCKET,
83: AUTH_CONNECTION
84: } sock_type;
85:
1.21 markus 86: typedef struct {
87: int fd;
1.73 stevesk 88: sock_type type;
1.194 markus 89: struct sshbuf *input;
90: struct sshbuf *output;
91: struct sshbuf *request;
1.1 deraadt 92: } SocketEntry;
93:
1.45 markus 94: u_int sockets_alloc = 0;
1.1 deraadt 95: SocketEntry *sockets = NULL;
96:
1.78 provos 97: typedef struct identity {
98: TAILQ_ENTRY(identity) next;
1.194 markus 99: struct sshkey *key;
1.21 markus 100: char *comment;
1.163 markus 101: char *provider;
1.174 dtucker 102: time_t death;
1.107 markus 103: u_int confirm;
1.1 deraadt 104: } Identity;
105:
1.33 markus 106: typedef struct {
107: int nentries;
1.78 provos 108: TAILQ_HEAD(idqueue, identity) idlist;
1.33 markus 109: } Idtab;
110:
111: /* private key table, one per protocol version */
112: Idtab idtable[3];
1.1 deraadt 113:
114: int max_fd = 0;
115:
1.11 markus 116: /* pid of shell == parent of agent */
1.29 deraadt 117: pid_t parent_pid = -1;
1.176 dtucker 118: time_t parent_alive_interval = 0;
1.10 markus 119:
1.187 djm 120: /* pid of process for which cleanup_socket is applicable */
121: pid_t cleanup_pid = 0;
122:
1.10 markus 123: /* pathname and directory for AUTH_SOCKET */
1.196 ! deraadt 124: char socket_name[PATH_MAX];
! 125: char socket_dir[PATH_MAX];
1.10 markus 126:
1.88 markus 127: /* locking */
128: int locked = 0;
129: char *lock_passwd = NULL;
130:
1.20 markus 131: extern char *__progname;
132:
1.174 dtucker 133: /* Default lifetime in seconds (0 == forever) */
134: static long lifetime = 0;
1.106 marc 135:
1.192 djm 136: static int fingerprint_hash = SSH_FP_HASH_DEFAULT;
137:
1.55 itojun 138: static void
1.101 stevesk 139: close_socket(SocketEntry *e)
140: {
141: close(e->fd);
142: e->fd = -1;
143: e->type = AUTH_UNUSED;
1.194 markus 144: sshbuf_free(e->input);
145: sshbuf_free(e->output);
146: sshbuf_free(e->request);
1.101 stevesk 147: }
148:
149: static void
1.33 markus 150: idtab_init(void)
1.1 deraadt 151: {
1.33 markus 152: int i;
1.96 deraadt 153:
1.74 deraadt 154: for (i = 0; i <=2; i++) {
1.78 provos 155: TAILQ_INIT(&idtable[i].idlist);
1.33 markus 156: idtable[i].nentries = 0;
157: }
158: }
159:
160: /* return private key table for requested protocol version */
1.55 itojun 161: static Idtab *
1.33 markus 162: idtab_lookup(int version)
163: {
164: if (version < 1 || version > 2)
165: fatal("internal error, bad protocol version %d", version);
166: return &idtable[version];
167: }
168:
1.89 markus 169: static void
170: free_identity(Identity *id)
171: {
1.194 markus 172: sshkey_free(id->key);
1.173 djm 173: free(id->provider);
174: free(id->comment);
175: free(id);
1.89 markus 176: }
177:
1.33 markus 178: /* return matching private key for given public key */
1.78 provos 179: static Identity *
1.194 markus 180: lookup_identity(struct sshkey *key, int version)
1.33 markus 181: {
1.78 provos 182: Identity *id;
183:
1.33 markus 184: Idtab *tab = idtab_lookup(version);
1.78 provos 185: TAILQ_FOREACH(id, &tab->idlist, next) {
1.194 markus 186: if (sshkey_equal(key, id->key))
1.78 provos 187: return (id);
1.33 markus 188: }
1.78 provos 189: return (NULL);
190: }
191:
1.107 markus 192: /* Check confirmation of keysign request */
193: static int
194: confirm_key(Identity *id)
195: {
1.122 djm 196: char *p;
1.107 markus 197: int ret = -1;
198:
1.194 markus 199: p = sshkey_fingerprint(id->key, fingerprint_hash, SSH_FP_DEFAULT);
1.122 djm 200: if (ask_permission("Allow use of key %s?\nKey fingerprint %s.",
201: id->comment, p))
202: ret = 0;
1.173 djm 203: free(p);
1.122 djm 204:
1.107 markus 205: return (ret);
206: }
207:
1.194 markus 208: static void
209: send_status(SocketEntry *e, int success)
210: {
211: int r;
212:
213: if ((r = sshbuf_put_u32(e->output, 1)) != 0 ||
214: (r = sshbuf_put_u8(e->output, success ?
215: SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE)) != 0)
216: fatal("%s: buffer error: %s", __func__, ssh_err(r));
217: }
218:
1.33 markus 219: /* send list of supported public keys to 'client' */
1.55 itojun 220: static void
1.33 markus 221: process_request_identities(SocketEntry *e, int version)
222: {
223: Idtab *tab = idtab_lookup(version);
1.96 deraadt 224: Identity *id;
1.194 markus 225: struct sshbuf *msg;
226: int r;
1.1 deraadt 227:
1.194 markus 228: if ((msg = sshbuf_new()) == NULL)
229: fatal("%s: sshbuf_new failed", __func__);
230: if ((r = sshbuf_put_u8(msg, (version == 1) ?
231: SSH_AGENT_RSA_IDENTITIES_ANSWER :
232: SSH2_AGENT_IDENTITIES_ANSWER)) != 0 ||
233: (r = sshbuf_put_u32(msg, tab->nentries)) != 0)
234: fatal("%s: buffer error: %s", __func__, ssh_err(r));
1.78 provos 235: TAILQ_FOREACH(id, &tab->idlist, next) {
1.39 markus 236: if (id->key->type == KEY_RSA1) {
1.185 markus 237: #ifdef WITH_SSH1
1.194 markus 238: if ((r = sshbuf_put_u32(msg,
239: BN_num_bits(id->key->rsa->n))) != 0 ||
240: (r = sshbuf_put_bignum1(msg,
241: id->key->rsa->e)) != 0 ||
242: (r = sshbuf_put_bignum1(msg,
243: id->key->rsa->n)) != 0)
244: fatal("%s: buffer error: %s",
245: __func__, ssh_err(r));
1.185 markus 246: #endif
1.33 markus 247: } else {
1.45 markus 248: u_char *blob;
1.194 markus 249: size_t blen;
250:
251: if ((r = sshkey_to_blob(id->key, &blob, &blen)) != 0) {
252: error("%s: sshkey_to_blob: %s", __func__,
253: ssh_err(r));
254: continue;
255: }
256: if ((r = sshbuf_put_string(msg, blob, blen)) != 0)
257: fatal("%s: buffer error: %s",
258: __func__, ssh_err(r));
1.173 djm 259: free(blob);
1.33 markus 260: }
1.194 markus 261: if ((r = sshbuf_put_cstring(msg, id->comment)) != 0)
262: fatal("%s: buffer error: %s", __func__, ssh_err(r));
1.21 markus 263: }
1.194 markus 264: if ((r = sshbuf_put_stringb(e->output, msg)) != 0)
265: fatal("%s: buffer error: %s", __func__, ssh_err(r));
266: sshbuf_free(msg);
1.1 deraadt 267: }
268:
1.185 markus 269: #ifdef WITH_SSH1
1.33 markus 270: /* ssh1 only */
1.55 itojun 271: static void
1.33 markus 272: process_authentication_challenge1(SocketEntry *e)
1.1 deraadt 273: {
1.96 deraadt 274: u_char buf[32], mdbuf[16], session_id[16];
275: u_int response_type;
276: BIGNUM *challenge;
1.78 provos 277: Identity *id;
1.194 markus 278: int r, len;
279: struct sshbuf *msg;
1.182 markus 280: struct ssh_digest_ctx *md;
1.194 markus 281: struct sshkey *key;
1.21 markus 282:
1.194 markus 283: if ((msg = sshbuf_new()) == NULL)
284: fatal("%s: sshbuf_new failed", __func__);
285: if ((key = sshkey_new(KEY_RSA1)) == NULL)
286: fatal("%s: sshkey_new failed", __func__);
1.76 markus 287: if ((challenge = BN_new()) == NULL)
1.194 markus 288: fatal("%s: BN_new failed", __func__);
1.33 markus 289:
1.194 markus 290: if ((r = sshbuf_get_u32(e->request, NULL)) != 0 || /* ignored */
291: (r = sshbuf_get_bignum1(e->request, key->rsa->e)) != 0 ||
292: (r = sshbuf_get_bignum1(e->request, key->rsa->n)) != 0 ||
293: (r = sshbuf_get_bignum1(e->request, challenge)))
294: fatal("%s: buffer error: %s", __func__, ssh_err(r));
1.21 markus 295:
1.33 markus 296: /* Only protocol 1.1 is supported */
1.194 markus 297: if (sshbuf_len(e->request) == 0)
1.33 markus 298: goto failure;
1.194 markus 299: if ((r = sshbuf_get(e->request, session_id, sizeof(session_id))) != 0 ||
300: (r = sshbuf_get_u32(e->request, &response_type)) != 0)
301: fatal("%s: buffer error: %s", __func__, ssh_err(r));
1.33 markus 302: if (response_type != 1)
303: goto failure;
304:
1.78 provos 305: id = lookup_identity(key, 1);
1.107 markus 306: if (id != NULL && (!id->confirm || confirm_key(id) == 0)) {
1.194 markus 307: struct sshkey *private = id->key;
1.33 markus 308: /* Decrypt the challenge using the private key. */
1.194 markus 309: if ((r = rsa_private_decrypt(challenge, challenge,
310: private->rsa) != 0)) {
311: fatal("%s: rsa_public_encrypt: %s", __func__,
312: ssh_err(r));
313: goto failure; /* XXX ? */
314: }
1.33 markus 315:
1.194 markus 316: /* The response is MD5 of decrypted challenge plus session id */
1.33 markus 317: len = BN_num_bytes(challenge);
318: if (len <= 0 || len > 32) {
1.194 markus 319: logit("%s: bad challenge length %d", __func__, len);
1.33 markus 320: goto failure;
321: }
322: memset(buf, 0, 32);
323: BN_bn2bin(challenge, buf + 32 - len);
1.182 markus 324: if ((md = ssh_digest_start(SSH_DIGEST_MD5)) == NULL ||
325: ssh_digest_update(md, buf, 32) < 0 ||
326: ssh_digest_update(md, session_id, 16) < 0 ||
327: ssh_digest_final(md, mdbuf, sizeof(mdbuf)) < 0)
328: fatal("%s: md5 failed", __func__);
329: ssh_digest_free(md);
1.33 markus 330:
331: /* Send the response. */
1.194 markus 332: if ((r = sshbuf_put_u8(msg, SSH_AGENT_RSA_RESPONSE)) != 0 ||
333: (r = sshbuf_put(msg, mdbuf, sizeof(mdbuf))) != 0)
334: fatal("%s: buffer error: %s", __func__, ssh_err(r));
1.33 markus 335: goto send;
336: }
1.21 markus 337:
1.194 markus 338: failure:
1.33 markus 339: /* Unknown identity or protocol error. Send failure. */
1.194 markus 340: if ((r = sshbuf_put_u8(msg, SSH_AGENT_FAILURE)) != 0)
341: fatal("%s: buffer error: %s", __func__, ssh_err(r));
342: send:
343: if ((r = sshbuf_put_stringb(e->output, msg)) != 0)
344: fatal("%s: buffer error: %s", __func__, ssh_err(r));
345: sshkey_free(key);
1.33 markus 346: BN_clear_free(challenge);
1.194 markus 347: sshbuf_free(msg);
1.33 markus 348: }
1.185 markus 349: #endif
1.33 markus 350:
351: /* ssh2 only */
1.55 itojun 352: static void
1.33 markus 353: process_sign_request2(SocketEntry *e)
354: {
1.45 markus 355: u_char *blob, *data, *signature = NULL;
1.194 markus 356: size_t blen, dlen, slen = 0;
357: u_int compat = 0, flags;
358: int r, ok = -1;
359: struct sshbuf *msg;
360: struct sshkey *key;
1.195 djm 361: struct identity *id;
1.194 markus 362:
1.195 djm 363: if ((msg = sshbuf_new()) == NULL)
364: fatal("%s: sshbuf_new failed", __func__);
1.194 markus 365: if ((r = sshbuf_get_string(e->request, &blob, &blen)) != 0 ||
366: (r = sshbuf_get_string(e->request, &data, &dlen)) != 0 ||
367: (r = sshbuf_get_u32(e->request, &flags)) != 0)
368: fatal("%s: buffer error: %s", __func__, ssh_err(r));
1.37 markus 369: if (flags & SSH_AGENT_OLD_SIGNATURE)
1.194 markus 370: compat = SSH_BUG_SIGBLOB;
1.195 djm 371: if ((r = sshkey_from_blob(blob, blen, &key)) != 0) {
1.194 markus 372: error("%s: cannot parse key blob: %s", __func__, ssh_err(ok));
1.195 djm 373: goto send;
374: }
375: if ((id = lookup_identity(key, 2)) == NULL) {
376: verbose("%s: %s key not found", __func__, sshkey_type(key));
377: goto send;
378: }
379: if (id->confirm && confirm_key(id) != 0) {
380: verbose("%s: user refused key", __func__);
381: goto send;
382: }
383: if ((r = sshkey_sign(id->key, &signature, &slen,
384: data, dlen, compat)) != 0) {
385: error("%s: sshkey_sign: %s", __func__, ssh_err(ok));
386: goto send;
1.33 markus 387: }
1.195 djm 388: /* Success */
389: ok = 0;
390: send:
391: sshkey_free(key);
1.33 markus 392: if (ok == 0) {
1.194 markus 393: if ((r = sshbuf_put_u8(msg, SSH2_AGENT_SIGN_RESPONSE)) != 0 ||
394: (r = sshbuf_put_string(msg, signature, slen)) != 0)
395: fatal("%s: buffer error: %s", __func__, ssh_err(r));
396: } else if ((r = sshbuf_put_u8(msg, SSH_AGENT_FAILURE)) != 0)
397: fatal("%s: buffer error: %s", __func__, ssh_err(r));
398:
399: if ((r = sshbuf_put_stringb(e->output, msg)) != 0)
400: fatal("%s: buffer error: %s", __func__, ssh_err(r));
401:
402: sshbuf_free(msg);
1.173 djm 403: free(data);
404: free(blob);
405: free(signature);
1.1 deraadt 406: }
407:
1.33 markus 408: /* shared */
1.55 itojun 409: static void
1.33 markus 410: process_remove_identity(SocketEntry *e, int version)
1.1 deraadt 411: {
1.194 markus 412: size_t blen;
413: int r, success = 0;
414: struct sshkey *key = NULL;
1.45 markus 415: u_char *blob;
1.186 djm 416: #ifdef WITH_SSH1
417: u_int bits;
418: #endif /* WITH_SSH1 */
1.21 markus 419:
1.74 deraadt 420: switch (version) {
1.186 djm 421: #ifdef WITH_SSH1
1.33 markus 422: case 1:
1.194 markus 423: if ((key = sshkey_new(KEY_RSA1)) == NULL) {
424: error("%s: sshkey_new failed", __func__);
425: return;
426: }
427: if ((r = sshbuf_get_u32(e->request, &bits)) != 0 ||
428: (r = sshbuf_get_bignum1(e->request, key->rsa->e)) != 0 ||
429: (r = sshbuf_get_bignum1(e->request, key->rsa->n)) != 0)
430: fatal("%s: buffer error: %s", __func__, ssh_err(r));
431:
432: if (bits != sshkey_size(key))
433: logit("Warning: identity keysize mismatch: "
434: "actual %u, announced %u",
435: sshkey_size(key), bits);
1.33 markus 436: break;
1.186 djm 437: #endif /* WITH_SSH1 */
1.33 markus 438: case 2:
1.194 markus 439: if ((r = sshbuf_get_string(e->request, &blob, &blen)) != 0)
440: fatal("%s: buffer error: %s", __func__, ssh_err(r));
441: if ((r = sshkey_from_blob(blob, blen, &key)) != 0)
442: error("%s: sshkey_from_blob failed: %s",
443: __func__, ssh_err(r));
1.173 djm 444: free(blob);
1.33 markus 445: break;
446: }
447: if (key != NULL) {
1.78 provos 448: Identity *id = lookup_identity(key, version);
449: if (id != NULL) {
1.23 markus 450: /*
451: * We have this key. Free the old key. Since we
1.124 djm 452: * don't want to leave empty slots in the middle of
1.40 markus 453: * the array, we actually free the key there and move
454: * all the entries between the empty slot and the end
455: * of the array.
1.23 markus 456: */
1.33 markus 457: Idtab *tab = idtab_lookup(version);
1.38 markus 458: if (tab->nentries < 1)
459: fatal("process_remove_identity: "
460: "internal error: tab->nentries %d",
461: tab->nentries);
1.78 provos 462: TAILQ_REMOVE(&tab->idlist, id, next);
463: free_identity(id);
1.33 markus 464: tab->nentries--;
465: success = 1;
1.21 markus 466: }
1.194 markus 467: sshkey_free(key);
1.33 markus 468: }
1.194 markus 469: send_status(e, success);
1.1 deraadt 470: }
471:
1.55 itojun 472: static void
1.33 markus 473: process_remove_all_identities(SocketEntry *e, int version)
1.1 deraadt 474: {
1.33 markus 475: Idtab *tab = idtab_lookup(version);
1.78 provos 476: Identity *id;
1.21 markus 477:
478: /* Loop over all identities and clear the keys. */
1.78 provos 479: for (id = TAILQ_FIRST(&tab->idlist); id;
480: id = TAILQ_FIRST(&tab->idlist)) {
481: TAILQ_REMOVE(&tab->idlist, id, next);
482: free_identity(id);
1.21 markus 483: }
484:
485: /* Mark that there are no identities. */
1.33 markus 486: tab->nentries = 0;
1.21 markus 487:
488: /* Send success. */
1.194 markus 489: send_status(e, 1);
1.1 deraadt 490: }
491:
1.155 dtucker 492: /* removes expired keys and returns number of seconds until the next expiry */
1.174 dtucker 493: static time_t
1.89 markus 494: reaper(void)
495: {
1.175 dtucker 496: time_t deadline = 0, now = monotime();
1.89 markus 497: Identity *id, *nxt;
498: int version;
1.96 deraadt 499: Idtab *tab;
1.89 markus 500:
501: for (version = 1; version < 3; version++) {
502: tab = idtab_lookup(version);
503: for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
504: nxt = TAILQ_NEXT(id, next);
1.155 dtucker 505: if (id->death == 0)
506: continue;
507: if (now >= id->death) {
1.154 dtucker 508: debug("expiring key '%s'", id->comment);
1.89 markus 509: TAILQ_REMOVE(&tab->idlist, id, next);
510: free_identity(id);
511: tab->nentries--;
1.155 dtucker 512: } else
513: deadline = (deadline == 0) ? id->death :
514: MIN(deadline, id->death);
1.89 markus 515: }
516: }
1.155 dtucker 517: if (deadline == 0 || deadline <= now)
518: return 0;
519: else
520: return (deadline - now);
1.89 markus 521: }
522:
1.194 markus 523: /*
524: * XXX this and the corresponding serialisation function probably belongs
525: * in key.c
526: */
527: static int
528: agent_decode_rsa1(struct sshbuf *m, struct sshkey **kp)
529: {
530: struct sshkey *k = NULL;
531: int r = SSH_ERR_INTERNAL_ERROR;
532:
533: *kp = NULL;
534: if ((k = sshkey_new_private(KEY_RSA1)) == NULL)
535: return SSH_ERR_ALLOC_FAIL;
536:
537: if ((r = sshbuf_get_u32(m, NULL)) != 0 || /* ignored */
538: (r = sshbuf_get_bignum1(m, k->rsa->n)) != 0 ||
539: (r = sshbuf_get_bignum1(m, k->rsa->e)) != 0 ||
540: (r = sshbuf_get_bignum1(m, k->rsa->d)) != 0 ||
541: (r = sshbuf_get_bignum1(m, k->rsa->iqmp)) != 0 ||
542: /* SSH1 and SSL have p and q swapped */
543: (r = sshbuf_get_bignum1(m, k->rsa->q)) != 0 || /* p */
544: (r = sshbuf_get_bignum1(m, k->rsa->p)) != 0) /* q */
545: goto out;
546:
547: /* Generate additional parameters */
548: if ((r = rsa_generate_additional_parameters(k->rsa)) != 0)
549: goto out;
550: /* enable blinding */
551: if (RSA_blinding_on(k->rsa, NULL) != 1) {
552: r = SSH_ERR_LIBCRYPTO_ERROR;
553: goto out;
554: }
555:
556: r = 0; /* success */
557: out:
558: if (r == 0)
559: *kp = k;
560: else
561: sshkey_free(k);
562: return r;
563: }
564:
1.89 markus 565: static void
1.33 markus 566: process_add_identity(SocketEntry *e, int version)
1.1 deraadt 567: {
1.96 deraadt 568: Idtab *tab = idtab_lookup(version);
1.157 canacar 569: Identity *id;
1.194 markus 570: int success = 0, confirm = 0;
571: u_int seconds;
572: char *comment = NULL;
1.174 dtucker 573: time_t death = 0;
1.194 markus 574: struct sshkey *k = NULL;
575: u_char ctype;
576: int r = SSH_ERR_INTERNAL_ERROR;
1.33 markus 577:
578: switch (version) {
1.186 djm 579: #ifdef WITH_SSH1
1.33 markus 580: case 1:
1.194 markus 581: r = agent_decode_rsa1(e->request, &k);
1.33 markus 582: break;
1.186 djm 583: #endif /* WITH_SSH1 */
1.33 markus 584: case 2:
1.194 markus 585: r = sshkey_private_deserialize(e->request, &k);
1.33 markus 586: break;
587: }
1.194 markus 588: if (r != 0 || k == NULL ||
589: (r = sshbuf_get_cstring(e->request, &comment, NULL)) != 0) {
590: error("%s: decode private key: %s", __func__, ssh_err(r));
591: goto err;
592: }
1.186 djm 593:
1.194 markus 594: while (sshbuf_len(e->request)) {
595: if ((r = sshbuf_get_u8(e->request, &ctype)) != 0) {
596: error("%s: buffer error: %s", __func__, ssh_err(r));
597: goto err;
598: }
599: switch (ctype) {
1.94 markus 600: case SSH_AGENT_CONSTRAIN_LIFETIME:
1.194 markus 601: if ((r = sshbuf_get_u32(e->request, &seconds)) != 0) {
602: error("%s: bad lifetime constraint: %s",
603: __func__, ssh_err(r));
604: goto err;
605: }
606: death = monotime() + seconds;
1.94 markus 607: break;
1.107 markus 608: case SSH_AGENT_CONSTRAIN_CONFIRM:
609: confirm = 1;
610: break;
1.94 markus 611: default:
1.194 markus 612: error("%s: Unknown constraint %d", __func__, ctype);
613: err:
614: sshbuf_reset(e->request);
1.173 djm 615: free(comment);
1.194 markus 616: sshkey_free(k);
1.158 djm 617: goto send;
1.94 markus 618: }
619: }
1.194 markus 620:
1.158 djm 621: success = 1;
1.106 marc 622: if (lifetime && !death)
1.175 dtucker 623: death = monotime() + lifetime;
1.157 canacar 624: if ((id = lookup_identity(k, version)) == NULL) {
1.163 markus 625: id = xcalloc(1, sizeof(Identity));
1.78 provos 626: id->key = k;
627: TAILQ_INSERT_TAIL(&tab->idlist, id, next);
1.33 markus 628: /* Increment the number of identities. */
629: tab->nentries++;
630: } else {
1.194 markus 631: sshkey_free(k);
1.173 djm 632: free(id->comment);
1.33 markus 633: }
1.157 canacar 634: id->comment = comment;
635: id->death = death;
636: id->confirm = confirm;
1.33 markus 637: send:
1.194 markus 638: send_status(e, success);
1.1 deraadt 639: }
640:
1.88 markus 641: /* XXX todo: encrypt sensitive data with passphrase */
642: static void
643: process_lock_agent(SocketEntry *e, int lock)
644: {
1.194 markus 645: int r, success = 0;
1.88 markus 646: char *passwd;
647:
1.194 markus 648: if ((r = sshbuf_get_cstring(e->request, &passwd, NULL)) != 0)
649: fatal("%s: buffer error: %s", __func__, ssh_err(r));
1.88 markus 650: if (locked && !lock && strcmp(passwd, lock_passwd) == 0) {
651: locked = 0;
1.183 djm 652: explicit_bzero(lock_passwd, strlen(lock_passwd));
1.173 djm 653: free(lock_passwd);
1.88 markus 654: lock_passwd = NULL;
655: success = 1;
656: } else if (!locked && lock) {
657: locked = 1;
658: lock_passwd = xstrdup(passwd);
659: success = 1;
660: }
1.183 djm 661: explicit_bzero(passwd, strlen(passwd));
1.173 djm 662: free(passwd);
1.194 markus 663: send_status(e, success);
1.88 markus 664: }
665:
666: static void
667: no_identities(SocketEntry *e, u_int type)
668: {
1.194 markus 669: struct sshbuf *msg;
670: int r;
1.88 markus 671:
1.194 markus 672: if ((msg = sshbuf_new()) == NULL)
673: fatal("%s: sshbuf_new failed", __func__);
674: if ((r = sshbuf_put_u8(msg,
1.88 markus 675: (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
1.194 markus 676: SSH_AGENT_RSA_IDENTITIES_ANSWER :
677: SSH2_AGENT_IDENTITIES_ANSWER)) != 0 ||
678: (r = sshbuf_put_u32(msg, 0)) != 0 ||
679: (r = sshbuf_put_stringb(e->output, msg)) != 0)
680: fatal("%s: buffer error: %s", __func__, ssh_err(r));
681: sshbuf_free(msg);
1.88 markus 682: }
1.59 markus 683:
1.163 markus 684: #ifdef ENABLE_PKCS11
1.59 markus 685: static void
1.158 djm 686: process_add_smartcard_key(SocketEntry *e)
1.59 markus 687: {
1.163 markus 688: char *provider = NULL, *pin;
1.194 markus 689: int r, i, version, count = 0, success = 0, confirm = 0;
690: u_int seconds;
1.174 dtucker 691: time_t death = 0;
1.194 markus 692: u_char type;
693: struct sshkey **keys = NULL, *k;
1.84 markus 694: Identity *id;
1.59 markus 695: Idtab *tab;
1.75 deraadt 696:
1.194 markus 697: if ((r = sshbuf_get_cstring(e->request, &provider, NULL)) != 0 ||
698: (r = sshbuf_get_cstring(e->request, &pin, NULL)) != 0)
699: fatal("%s: buffer error: %s", __func__, ssh_err(r));
700:
701: while (sshbuf_len(e->request)) {
702: if ((r = sshbuf_get_u8(e->request, &type)) != 0)
703: fatal("%s: buffer error: %s", __func__, ssh_err(r));
704: switch (type) {
1.110 djm 705: case SSH_AGENT_CONSTRAIN_LIFETIME:
1.194 markus 706: if ((r = sshbuf_get_u32(e->request, &seconds)) != 0)
707: fatal("%s: buffer error: %s",
708: __func__, ssh_err(r));
709: death = monotime() + seconds;
1.110 djm 710: break;
711: case SSH_AGENT_CONSTRAIN_CONFIRM:
712: confirm = 1;
713: break;
714: default:
1.158 djm 715: error("process_add_smartcard_key: "
716: "Unknown constraint type %d", type);
717: goto send;
1.110 djm 718: }
719: }
720: if (lifetime && !death)
1.175 dtucker 721: death = monotime() + lifetime;
1.110 djm 722:
1.163 markus 723: count = pkcs11_add_provider(provider, pin, &keys);
724: for (i = 0; i < count; i++) {
1.84 markus 725: k = keys[i];
726: version = k->type == KEY_RSA1 ? 1 : 2;
727: tab = idtab_lookup(version);
728: if (lookup_identity(k, version) == NULL) {
1.163 markus 729: id = xcalloc(1, sizeof(Identity));
1.84 markus 730: id->key = k;
1.163 markus 731: id->provider = xstrdup(provider);
732: id->comment = xstrdup(provider); /* XXX */
1.110 djm 733: id->death = death;
734: id->confirm = confirm;
1.84 markus 735: TAILQ_INSERT_TAIL(&tab->idlist, id, next);
736: tab->nentries++;
737: success = 1;
738: } else {
1.194 markus 739: sshkey_free(k);
1.84 markus 740: }
741: keys[i] = NULL;
1.59 markus 742: }
743: send:
1.173 djm 744: free(pin);
745: free(provider);
746: free(keys);
1.194 markus 747: send_status(e, success);
1.59 markus 748: }
749:
750: static void
751: process_remove_smartcard_key(SocketEntry *e)
752: {
1.163 markus 753: char *provider = NULL, *pin = NULL;
1.194 markus 754: int r, version, success = 0;
1.163 markus 755: Identity *id, *nxt;
1.84 markus 756: Idtab *tab;
1.59 markus 757:
1.194 markus 758: if ((r = sshbuf_get_cstring(e->request, &provider, NULL)) != 0 ||
759: (r = sshbuf_get_cstring(e->request, &pin, NULL)) != 0)
760: fatal("%s: buffer error: %s", __func__, ssh_err(r));
1.173 djm 761: free(pin);
1.59 markus 762:
1.163 markus 763: for (version = 1; version < 3; version++) {
764: tab = idtab_lookup(version);
765: for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
766: nxt = TAILQ_NEXT(id, next);
1.181 djm 767: /* Skip file--based keys */
768: if (id->provider == NULL)
769: continue;
1.163 markus 770: if (!strcmp(provider, id->provider)) {
771: TAILQ_REMOVE(&tab->idlist, id, next);
772: free_identity(id);
773: tab->nentries--;
774: }
1.59 markus 775: }
776: }
1.163 markus 777: if (pkcs11_del_provider(provider) == 0)
778: success = 1;
779: else
780: error("process_remove_smartcard_key:"
781: " pkcs11_del_provider failed");
1.173 djm 782: free(provider);
1.194 markus 783: send_status(e, success);
1.59 markus 784: }
1.163 markus 785: #endif /* ENABLE_PKCS11 */
1.59 markus 786:
1.33 markus 787: /* dispatch incoming messages */
788:
1.55 itojun 789: static void
1.2 provos 790: process_message(SocketEntry *e)
1.1 deraadt 791: {
1.194 markus 792: u_int msg_len;
793: u_char type;
794: const u_char *cp;
795: int r;
1.89 markus 796:
1.194 markus 797: if (sshbuf_len(e->input) < 5)
1.21 markus 798: return; /* Incomplete message. */
1.194 markus 799: cp = sshbuf_ptr(e->input);
800: msg_len = PEEK_U32(cp);
1.21 markus 801: if (msg_len > 256 * 1024) {
1.101 stevesk 802: close_socket(e);
1.21 markus 803: return;
804: }
1.194 markus 805: if (sshbuf_len(e->input) < msg_len + 4)
1.21 markus 806: return;
1.87 markus 807:
808: /* move the current input to e->request */
1.194 markus 809: sshbuf_reset(e->request);
810: if ((r = sshbuf_get_stringb(e->input, e->request)) != 0 ||
811: (r = sshbuf_get_u8(e->request, &type)) != 0)
812: fatal("%s: buffer error: %s", __func__, ssh_err(r));
1.21 markus 813:
1.88 markus 814: /* check wheter agent is locked */
815: if (locked && type != SSH_AGENTC_UNLOCK) {
1.194 markus 816: sshbuf_reset(e->request);
1.88 markus 817: switch (type) {
818: case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
819: case SSH2_AGENTC_REQUEST_IDENTITIES:
820: /* send empty lists */
821: no_identities(e, type);
822: break;
823: default:
824: /* send a fail message for all other request types */
1.194 markus 825: send_status(e, 0);
1.88 markus 826: }
827: return;
828: }
829:
1.59 markus 830: debug("type %d", type);
1.21 markus 831: switch (type) {
1.88 markus 832: case SSH_AGENTC_LOCK:
833: case SSH_AGENTC_UNLOCK:
834: process_lock_agent(e, type == SSH_AGENTC_LOCK);
835: break;
1.185 markus 836: #ifdef WITH_SSH1
1.33 markus 837: /* ssh1 */
838: case SSH_AGENTC_RSA_CHALLENGE:
839: process_authentication_challenge1(e);
840: break;
1.21 markus 841: case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
1.33 markus 842: process_request_identities(e, 1);
1.21 markus 843: break;
844: case SSH_AGENTC_ADD_RSA_IDENTITY:
1.94 markus 845: case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED:
1.33 markus 846: process_add_identity(e, 1);
1.21 markus 847: break;
848: case SSH_AGENTC_REMOVE_RSA_IDENTITY:
1.33 markus 849: process_remove_identity(e, 1);
1.21 markus 850: break;
851: case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
1.33 markus 852: process_remove_all_identities(e, 1);
853: break;
1.185 markus 854: #endif
1.33 markus 855: /* ssh2 */
856: case SSH2_AGENTC_SIGN_REQUEST:
857: process_sign_request2(e);
858: break;
859: case SSH2_AGENTC_REQUEST_IDENTITIES:
860: process_request_identities(e, 2);
861: break;
862: case SSH2_AGENTC_ADD_IDENTITY:
1.94 markus 863: case SSH2_AGENTC_ADD_ID_CONSTRAINED:
1.33 markus 864: process_add_identity(e, 2);
865: break;
866: case SSH2_AGENTC_REMOVE_IDENTITY:
867: process_remove_identity(e, 2);
868: break;
869: case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
870: process_remove_all_identities(e, 2);
1.21 markus 871: break;
1.163 markus 872: #ifdef ENABLE_PKCS11
1.59 markus 873: case SSH_AGENTC_ADD_SMARTCARD_KEY:
1.110 djm 874: case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED:
1.59 markus 875: process_add_smartcard_key(e);
1.75 deraadt 876: break;
1.59 markus 877: case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
878: process_remove_smartcard_key(e);
1.75 deraadt 879: break;
1.163 markus 880: #endif /* ENABLE_PKCS11 */
1.21 markus 881: default:
882: /* Unknown message. Respond with failure. */
883: error("Unknown message %d", type);
1.194 markus 884: sshbuf_reset(e->request);
885: send_status(e, 0);
1.21 markus 886: break;
887: }
1.1 deraadt 888: }
889:
1.55 itojun 890: static void
1.73 stevesk 891: new_socket(sock_type type, int fd)
1.1 deraadt 892: {
1.112 markus 893: u_int i, old_alloc, new_alloc;
1.96 deraadt 894:
1.119 djm 895: set_nonblock(fd);
1.21 markus 896:
897: if (fd > max_fd)
898: max_fd = fd;
899:
900: for (i = 0; i < sockets_alloc; i++)
901: if (sockets[i].type == AUTH_UNUSED) {
902: sockets[i].fd = fd;
1.194 markus 903: if ((sockets[i].input = sshbuf_new()) == NULL)
904: fatal("%s: sshbuf_new failed", __func__);
905: if ((sockets[i].output = sshbuf_new()) == NULL)
906: fatal("%s: sshbuf_new failed", __func__);
907: if ((sockets[i].request = sshbuf_new()) == NULL)
908: fatal("%s: sshbuf_new failed", __func__);
1.112 markus 909: sockets[i].type = type;
1.21 markus 910: return;
911: }
912: old_alloc = sockets_alloc;
1.112 markus 913: new_alloc = sockets_alloc + 10;
1.133 djm 914: sockets = xrealloc(sockets, new_alloc, sizeof(sockets[0]));
1.112 markus 915: for (i = old_alloc; i < new_alloc; i++)
1.21 markus 916: sockets[i].type = AUTH_UNUSED;
1.112 markus 917: sockets_alloc = new_alloc;
1.21 markus 918: sockets[old_alloc].fd = fd;
1.194 markus 919: if ((sockets[old_alloc].input = sshbuf_new()) == NULL)
920: fatal("%s: sshbuf_new failed", __func__);
921: if ((sockets[old_alloc].output = sshbuf_new()) == NULL)
922: fatal("%s: sshbuf_new failed", __func__);
923: if ((sockets[old_alloc].request = sshbuf_new()) == NULL)
924: fatal("%s: sshbuf_new failed", __func__);
1.112 markus 925: sockets[old_alloc].type = type;
1.1 deraadt 926: }
927:
1.55 itojun 928: static int
1.155 dtucker 929: prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, u_int *nallocp,
930: struct timeval **tvpp)
1.1 deraadt 931: {
1.174 dtucker 932: u_int i, sz;
1.46 markus 933: int n = 0;
1.155 dtucker 934: static struct timeval tv;
1.174 dtucker 935: time_t deadline;
1.46 markus 936:
937: for (i = 0; i < sockets_alloc; i++) {
1.21 markus 938: switch (sockets[i].type) {
939: case AUTH_SOCKET:
940: case AUTH_CONNECTION:
1.46 markus 941: n = MAX(n, sockets[i].fd);
1.21 markus 942: break;
943: case AUTH_UNUSED:
944: break;
945: default:
946: fatal("Unknown socket type %d", sockets[i].type);
947: break;
948: }
1.46 markus 949: }
950:
951: sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
1.66 markus 952: if (*fdrp == NULL || sz > *nallocp) {
1.173 djm 953: free(*fdrp);
954: free(*fdwp);
1.46 markus 955: *fdrp = xmalloc(sz);
956: *fdwp = xmalloc(sz);
1.66 markus 957: *nallocp = sz;
1.46 markus 958: }
1.66 markus 959: if (n < *fdl)
960: debug("XXX shrink: %d < %d", n, *fdl);
961: *fdl = n;
1.46 markus 962: memset(*fdrp, 0, sz);
963: memset(*fdwp, 0, sz);
964:
965: for (i = 0; i < sockets_alloc; i++) {
966: switch (sockets[i].type) {
967: case AUTH_SOCKET:
968: case AUTH_CONNECTION:
969: FD_SET(sockets[i].fd, *fdrp);
1.194 markus 970: if (sshbuf_len(sockets[i].output) > 0)
1.46 markus 971: FD_SET(sockets[i].fd, *fdwp);
972: break;
973: default:
974: break;
975: }
976: }
1.155 dtucker 977: deadline = reaper();
978: if (parent_alive_interval != 0)
979: deadline = (deadline == 0) ? parent_alive_interval :
980: MIN(deadline, parent_alive_interval);
981: if (deadline == 0) {
982: *tvpp = NULL;
983: } else {
984: tv.tv_sec = deadline;
985: tv.tv_usec = 0;
986: *tvpp = &tv;
987: }
1.46 markus 988: return (1);
1.21 markus 989: }
990:
1.55 itojun 991: static void
1.21 markus 992: after_select(fd_set *readset, fd_set *writeset)
993: {
1.96 deraadt 994: struct sockaddr_un sunaddr;
1.26 markus 995: socklen_t slen;
1.21 markus 996: char buf[1024];
1.194 markus 997: int len, sock, r;
1.162 djm 998: u_int i, orig_alloc;
1.103 markus 999: uid_t euid;
1000: gid_t egid;
1.21 markus 1001:
1.162 djm 1002: for (i = 0, orig_alloc = sockets_alloc; i < orig_alloc; i++)
1.21 markus 1003: switch (sockets[i].type) {
1004: case AUTH_UNUSED:
1005: break;
1006: case AUTH_SOCKET:
1007: if (FD_ISSET(sockets[i].fd, readset)) {
1.26 markus 1008: slen = sizeof(sunaddr);
1.46 markus 1009: sock = accept(sockets[i].fd,
1.131 deraadt 1010: (struct sockaddr *)&sunaddr, &slen);
1.21 markus 1011: if (sock < 0) {
1.81 stevesk 1012: error("accept from AUTH_SOCKET: %s",
1013: strerror(errno));
1.103 markus 1014: break;
1015: }
1016: if (getpeereid(sock, &euid, &egid) < 0) {
1017: error("getpeereid %d failed: %s",
1018: sock, strerror(errno));
1019: close(sock);
1020: break;
1021: }
1.105 markus 1022: if ((euid != 0) && (getuid() != euid)) {
1.103 markus 1023: error("uid mismatch: "
1.104 stevesk 1024: "peer euid %u != uid %u",
1025: (u_int) euid, (u_int) getuid());
1.103 markus 1026: close(sock);
1.21 markus 1027: break;
1028: }
1029: new_socket(AUTH_CONNECTION, sock);
1030: }
1031: break;
1032: case AUTH_CONNECTION:
1.194 markus 1033: if (sshbuf_len(sockets[i].output) > 0 &&
1.21 markus 1034: FD_ISSET(sockets[i].fd, writeset)) {
1.162 djm 1035: len = write(sockets[i].fd,
1.194 markus 1036: sshbuf_ptr(sockets[i].output),
1037: sshbuf_len(sockets[i].output));
1.162 djm 1038: if (len == -1 && (errno == EAGAIN ||
1039: errno == EINTR))
1040: continue;
1.21 markus 1041: if (len <= 0) {
1.101 stevesk 1042: close_socket(&sockets[i]);
1.21 markus 1043: break;
1044: }
1.194 markus 1045: if ((r = sshbuf_consume(sockets[i].output,
1046: len)) != 0)
1047: fatal("%s: buffer error: %s",
1048: __func__, ssh_err(r));
1.21 markus 1049: }
1050: if (FD_ISSET(sockets[i].fd, readset)) {
1.162 djm 1051: len = read(sockets[i].fd, buf, sizeof(buf));
1052: if (len == -1 && (errno == EAGAIN ||
1053: errno == EINTR))
1054: continue;
1.21 markus 1055: if (len <= 0) {
1.101 stevesk 1056: close_socket(&sockets[i]);
1.21 markus 1057: break;
1058: }
1.194 markus 1059: if ((r = sshbuf_put(sockets[i].input,
1060: buf, len)) != 0)
1061: fatal("%s: buffer error: %s",
1062: __func__, ssh_err(r));
1.190 dtucker 1063: explicit_bzero(buf, sizeof(buf));
1.21 markus 1064: process_message(&sockets[i]);
1065: }
1066: break;
1067: default:
1068: fatal("Unknown type %d", sockets[i].type);
1069: }
1.1 deraadt 1070: }
1071:
1.55 itojun 1072: static void
1.113 markus 1073: cleanup_socket(void)
1.15 markus 1074: {
1.187 djm 1075: if (cleanup_pid != 0 && getpid() != cleanup_pid)
1076: return;
1077: debug("%s: cleanup", __func__);
1.48 deraadt 1078: if (socket_name[0])
1079: unlink(socket_name);
1080: if (socket_dir[0])
1081: rmdir(socket_dir);
1.10 markus 1082: }
1083:
1.114 markus 1084: void
1.15 markus 1085: cleanup_exit(int i)
1086: {
1.113 markus 1087: cleanup_socket();
1088: _exit(i);
1.15 markus 1089: }
1090:
1.135 deraadt 1091: /*ARGSUSED*/
1.55 itojun 1092: static void
1.48 deraadt 1093: cleanup_handler(int sig)
1094: {
1.113 markus 1095: cleanup_socket();
1.163 markus 1096: #ifdef ENABLE_PKCS11
1097: pkcs11_terminate();
1098: #endif
1.48 deraadt 1099: _exit(2);
1.113 markus 1100: }
1101:
1.68 markus 1102: static void
1.155 dtucker 1103: check_parent_exists(void)
1.68 markus 1104: {
1.172 dtucker 1105: /*
1106: * If our parent has exited then getppid() will return (pid_t)1,
1107: * so testing for that should be safe.
1108: */
1109: if (parent_pid != -1 && getppid() != parent_pid) {
1.68 markus 1110: /* printf("Parent has died - Authentication agent exiting.\n"); */
1.155 dtucker 1111: cleanup_socket();
1112: _exit(2);
1.68 markus 1113: }
1.48 deraadt 1114: }
1115:
1.55 itojun 1116: static void
1.50 itojun 1117: usage(void)
1.15 markus 1118: {
1.184 deraadt 1119: fprintf(stderr,
1.193 jmc 1120: "usage: ssh-agent [-c | -s] [-d] [-a bind_address] [-E fingerprint_hash]\n"
1121: " [-t life] [command [arg ...]]\n"
1.184 deraadt 1122: " ssh-agent [-c | -s] -k\n");
1.21 markus 1123: exit(1);
1.15 markus 1124: }
1125:
1.2 provos 1126: int
1127: main(int ac, char **av)
1.1 deraadt 1128: {
1.107 markus 1129: int c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0;
1.154 dtucker 1130: int sock, fd, ch, result, saved_errno;
1.120 avsm 1131: u_int nalloc;
1.96 deraadt 1132: char *shell, *format, *pidstr, *agentsocket = NULL;
1133: fd_set *readsetp = NULL, *writesetp = NULL;
1.41 markus 1134: struct rlimit rlim;
1.96 deraadt 1135: extern int optind;
1.98 stevesk 1136: extern char *optarg;
1.21 markus 1137: pid_t pid;
1.96 deraadt 1138: char pidstrbuf[1 + 3 * sizeof pid];
1.155 dtucker 1139: struct timeval *tvp = NULL;
1.161 tobias 1140: size_t len;
1.189 djm 1141: mode_t prev_mask;
1.123 djm 1142:
1143: /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1144: sanitise_stdfd();
1.99 markus 1145:
1146: /* drop */
1147: setegid(getgid());
1148: setgid(getgid());
1.53 markus 1149:
1.185 markus 1150: #ifdef WITH_OPENSSL
1.170 djm 1151: OpenSSL_add_all_algorithms();
1.185 markus 1152: #endif
1.21 markus 1153:
1.192 djm 1154: while ((ch = getopt(ac, av, "cdksE:a:t:")) != -1) {
1.21 markus 1155: switch (ch) {
1.192 djm 1156: case 'E':
1157: fingerprint_hash = ssh_digest_alg_by_name(optarg);
1158: if (fingerprint_hash == -1)
1159: fatal("Invalid hash algorithm \"%s\"", optarg);
1160: break;
1.21 markus 1161: case 'c':
1162: if (s_flag)
1163: usage();
1164: c_flag++;
1165: break;
1166: case 'k':
1167: k_flag++;
1168: break;
1169: case 's':
1170: if (c_flag)
1171: usage();
1172: s_flag++;
1173: break;
1.57 markus 1174: case 'd':
1175: if (d_flag)
1176: usage();
1177: d_flag++;
1178: break;
1.86 markus 1179: case 'a':
1180: agentsocket = optarg;
1.106 marc 1181: break;
1182: case 't':
1183: if ((lifetime = convtime(optarg)) == -1) {
1184: fprintf(stderr, "Invalid lifetime\n");
1185: usage();
1186: }
1.86 markus 1187: break;
1.21 markus 1188: default:
1189: usage();
1190: }
1191: }
1192: ac -= optind;
1193: av += optind;
1194:
1.57 markus 1195: if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
1.21 markus 1196: usage();
1197:
1.85 markus 1198: if (ac == 0 && !c_flag && !s_flag) {
1.21 markus 1199: shell = getenv("SHELL");
1.161 tobias 1200: if (shell != NULL && (len = strlen(shell)) > 2 &&
1201: strncmp(shell + len - 3, "csh", 3) == 0)
1.21 markus 1202: c_flag = 1;
1203: }
1204: if (k_flag) {
1.136 deraadt 1205: const char *errstr = NULL;
1206:
1.21 markus 1207: pidstr = getenv(SSH_AGENTPID_ENV_NAME);
1208: if (pidstr == NULL) {
1209: fprintf(stderr, "%s not set, cannot kill agent\n",
1.46 markus 1210: SSH_AGENTPID_ENV_NAME);
1.21 markus 1211: exit(1);
1212: }
1.136 deraadt 1213: pid = (int)strtonum(pidstr, 2, INT_MAX, &errstr);
1214: if (errstr) {
1215: fprintf(stderr,
1216: "%s=\"%s\", which is not a good PID: %s\n",
1217: SSH_AGENTPID_ENV_NAME, pidstr, errstr);
1.21 markus 1218: exit(1);
1219: }
1220: if (kill(pid, SIGTERM) == -1) {
1221: perror("kill");
1222: exit(1);
1223: }
1224: format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1.140 djm 1225: printf(format, SSH_AUTHSOCKET_ENV_NAME);
1226: printf(format, SSH_AGENTPID_ENV_NAME);
1.91 mpech 1227: printf("echo Agent pid %ld killed;\n", (long)pid);
1.21 markus 1228: exit(0);
1229: }
1230: parent_pid = getpid();
1231:
1.86 markus 1232: if (agentsocket == NULL) {
1233: /* Create private directory for agent socket */
1.171 djm 1234: mktemp_proto(socket_dir, sizeof(socket_dir));
1.86 markus 1235: if (mkdtemp(socket_dir) == NULL) {
1236: perror("mkdtemp: private socket dir");
1237: exit(1);
1238: }
1.91 mpech 1239: snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1240: (long)parent_pid);
1.86 markus 1241: } else {
1242: /* Try to use specified agent socket */
1243: socket_dir[0] = '\0';
1244: strlcpy(socket_name, agentsocket, sizeof socket_name);
1.21 markus 1245: }
1246:
1.23 markus 1247: /*
1248: * Create socket early so it will exist before command gets run from
1249: * the parent.
1250: */
1.189 djm 1251: prev_mask = umask(0177);
1.188 millert 1252: sock = unix_listener(socket_name, SSH_LISTEN_BACKLOG, 0);
1.21 markus 1253: if (sock < 0) {
1.188 millert 1254: /* XXX - unix_listener() calls error() not perror() */
1.121 djm 1255: *socket_name = '\0'; /* Don't unlink any existing file */
1.21 markus 1256: cleanup_exit(1);
1257: }
1.189 djm 1258: umask(prev_mask);
1.46 markus 1259:
1.23 markus 1260: /*
1261: * Fork, and have the parent execute the command, if any, or present
1262: * the socket data. The child continues as the authentication agent.
1263: */
1.57 markus 1264: if (d_flag) {
1265: log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
1266: format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1267: printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1268: SSH_AUTHSOCKET_ENV_NAME);
1.91 mpech 1269: printf("echo Agent pid %ld;\n", (long)parent_pid);
1.57 markus 1270: goto skip;
1271: }
1.21 markus 1272: pid = fork();
1273: if (pid == -1) {
1274: perror("fork");
1.81 stevesk 1275: cleanup_exit(1);
1.21 markus 1276: }
1277: if (pid != 0) { /* Parent - execute the given command. */
1278: close(sock);
1.91 mpech 1279: snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
1.21 markus 1280: if (ac == 0) {
1281: format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1282: printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1.46 markus 1283: SSH_AUTHSOCKET_ENV_NAME);
1.21 markus 1284: printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
1.46 markus 1285: SSH_AGENTPID_ENV_NAME);
1.91 mpech 1286: printf("echo Agent pid %ld;\n", (long)pid);
1.21 markus 1287: exit(0);
1288: }
1.36 deraadt 1289: if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1290: setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1291: perror("setenv");
1292: exit(1);
1293: }
1.21 markus 1294: execvp(av[0], av);
1295: perror(av[0]);
1296: exit(1);
1297: }
1.81 stevesk 1298: /* child */
1299: log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
1.67 stevesk 1300:
1301: if (setsid() == -1) {
1.81 stevesk 1302: error("setsid: %s", strerror(errno));
1.67 stevesk 1303: cleanup_exit(1);
1304: }
1305:
1306: (void)chdir("/");
1.107 markus 1307: if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1308: /* XXX might close listen socket */
1309: (void)dup2(fd, STDIN_FILENO);
1310: (void)dup2(fd, STDOUT_FILENO);
1311: (void)dup2(fd, STDERR_FILENO);
1312: if (fd > 2)
1313: close(fd);
1314: }
1.21 markus 1315:
1.41 markus 1316: /* deny core dumps, since memory contains unencrypted private keys */
1317: rlim.rlim_cur = rlim.rlim_max = 0;
1318: if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
1.81 stevesk 1319: error("setrlimit RLIMIT_CORE: %s", strerror(errno));
1.21 markus 1320: cleanup_exit(1);
1321: }
1.57 markus 1322:
1323: skip:
1.187 djm 1324:
1325: cleanup_pid = getpid();
1.163 markus 1326:
1327: #ifdef ENABLE_PKCS11
1328: pkcs11_init(0);
1329: #endif
1.21 markus 1330: new_socket(AUTH_SOCKET, sock);
1.155 dtucker 1331: if (ac > 0)
1332: parent_alive_interval = 10;
1.33 markus 1333: idtab_init();
1.61 markus 1334: signal(SIGPIPE, SIG_IGN);
1.177 djm 1335: signal(SIGINT, d_flag ? cleanup_handler : SIG_IGN);
1.48 deraadt 1336: signal(SIGHUP, cleanup_handler);
1337: signal(SIGTERM, cleanup_handler);
1.66 markus 1338: nalloc = 0;
1339:
1.21 markus 1340: while (1) {
1.155 dtucker 1341: prepare_select(&readsetp, &writesetp, &max_fd, &nalloc, &tvp);
1342: result = select(max_fd + 1, readsetp, writesetp, NULL, tvp);
1.154 dtucker 1343: saved_errno = errno;
1.155 dtucker 1344: if (parent_alive_interval != 0)
1345: check_parent_exists();
1346: (void) reaper(); /* remove expired keys */
1.154 dtucker 1347: if (result < 0) {
1348: if (saved_errno == EINTR)
1.21 markus 1349: continue;
1.154 dtucker 1350: fatal("select: %s", strerror(saved_errno));
1351: } else if (result > 0)
1352: after_select(readsetp, writesetp);
1.15 markus 1353: }
1.21 markus 1354: /* NOTREACHED */
1.1 deraadt 1355: }