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