Annotation of src/usr.bin/ssh/ssh-agent.c, Revision 1.199
1.199 ! djm 1: /* $OpenBSD: ssh-agent.c,v 1.198 2015/03/03 21:21:13 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.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: */
1.198 djm 528: #ifdef WITH_SSH1
1.194 markus 529: static int
530: agent_decode_rsa1(struct sshbuf *m, struct sshkey **kp)
531: {
532: struct sshkey *k = NULL;
533: int r = SSH_ERR_INTERNAL_ERROR;
534:
535: *kp = NULL;
536: if ((k = sshkey_new_private(KEY_RSA1)) == NULL)
537: return SSH_ERR_ALLOC_FAIL;
538:
539: if ((r = sshbuf_get_u32(m, NULL)) != 0 || /* ignored */
540: (r = sshbuf_get_bignum1(m, k->rsa->n)) != 0 ||
541: (r = sshbuf_get_bignum1(m, k->rsa->e)) != 0 ||
542: (r = sshbuf_get_bignum1(m, k->rsa->d)) != 0 ||
543: (r = sshbuf_get_bignum1(m, k->rsa->iqmp)) != 0 ||
544: /* SSH1 and SSL have p and q swapped */
545: (r = sshbuf_get_bignum1(m, k->rsa->q)) != 0 || /* p */
546: (r = sshbuf_get_bignum1(m, k->rsa->p)) != 0) /* q */
547: goto out;
548:
549: /* Generate additional parameters */
550: if ((r = rsa_generate_additional_parameters(k->rsa)) != 0)
551: goto out;
552: /* enable blinding */
553: if (RSA_blinding_on(k->rsa, NULL) != 1) {
554: r = SSH_ERR_LIBCRYPTO_ERROR;
555: goto out;
556: }
557:
558: r = 0; /* success */
559: out:
560: if (r == 0)
561: *kp = k;
562: else
563: sshkey_free(k);
564: return r;
565: }
1.198 djm 566: #endif /* WITH_SSH1 */
1.194 markus 567:
1.89 markus 568: static void
1.33 markus 569: process_add_identity(SocketEntry *e, int version)
1.1 deraadt 570: {
1.96 deraadt 571: Idtab *tab = idtab_lookup(version);
1.157 canacar 572: Identity *id;
1.194 markus 573: int success = 0, confirm = 0;
574: u_int seconds;
575: char *comment = NULL;
1.174 dtucker 576: time_t death = 0;
1.194 markus 577: struct sshkey *k = NULL;
578: u_char ctype;
579: int r = SSH_ERR_INTERNAL_ERROR;
1.33 markus 580:
581: switch (version) {
1.186 djm 582: #ifdef WITH_SSH1
1.33 markus 583: case 1:
1.194 markus 584: r = agent_decode_rsa1(e->request, &k);
1.33 markus 585: break;
1.186 djm 586: #endif /* WITH_SSH1 */
1.33 markus 587: case 2:
1.194 markus 588: r = sshkey_private_deserialize(e->request, &k);
1.33 markus 589: break;
590: }
1.194 markus 591: if (r != 0 || k == NULL ||
592: (r = sshbuf_get_cstring(e->request, &comment, NULL)) != 0) {
593: error("%s: decode private key: %s", __func__, ssh_err(r));
594: goto err;
595: }
1.186 djm 596:
1.194 markus 597: while (sshbuf_len(e->request)) {
598: if ((r = sshbuf_get_u8(e->request, &ctype)) != 0) {
599: error("%s: buffer error: %s", __func__, ssh_err(r));
600: goto err;
601: }
602: switch (ctype) {
1.94 markus 603: case SSH_AGENT_CONSTRAIN_LIFETIME:
1.194 markus 604: if ((r = sshbuf_get_u32(e->request, &seconds)) != 0) {
605: error("%s: bad lifetime constraint: %s",
606: __func__, ssh_err(r));
607: goto err;
608: }
609: death = monotime() + seconds;
1.94 markus 610: break;
1.107 markus 611: case SSH_AGENT_CONSTRAIN_CONFIRM:
612: confirm = 1;
613: break;
1.94 markus 614: default:
1.194 markus 615: error("%s: Unknown constraint %d", __func__, ctype);
616: err:
617: sshbuf_reset(e->request);
1.173 djm 618: free(comment);
1.194 markus 619: sshkey_free(k);
1.158 djm 620: goto send;
1.94 markus 621: }
622: }
1.194 markus 623:
1.158 djm 624: success = 1;
1.106 marc 625: if (lifetime && !death)
1.175 dtucker 626: death = monotime() + lifetime;
1.157 canacar 627: if ((id = lookup_identity(k, version)) == NULL) {
1.163 markus 628: id = xcalloc(1, sizeof(Identity));
1.78 provos 629: id->key = k;
630: TAILQ_INSERT_TAIL(&tab->idlist, id, next);
1.33 markus 631: /* Increment the number of identities. */
632: tab->nentries++;
633: } else {
1.194 markus 634: sshkey_free(k);
1.173 djm 635: free(id->comment);
1.33 markus 636: }
1.157 canacar 637: id->comment = comment;
638: id->death = death;
639: id->confirm = confirm;
1.33 markus 640: send:
1.194 markus 641: send_status(e, success);
1.1 deraadt 642: }
643:
1.88 markus 644: /* XXX todo: encrypt sensitive data with passphrase */
645: static void
646: process_lock_agent(SocketEntry *e, int lock)
647: {
1.194 markus 648: int r, success = 0;
1.88 markus 649: char *passwd;
650:
1.194 markus 651: if ((r = sshbuf_get_cstring(e->request, &passwd, NULL)) != 0)
652: fatal("%s: buffer error: %s", __func__, ssh_err(r));
1.88 markus 653: if (locked && !lock && strcmp(passwd, lock_passwd) == 0) {
654: locked = 0;
1.183 djm 655: explicit_bzero(lock_passwd, strlen(lock_passwd));
1.173 djm 656: free(lock_passwd);
1.88 markus 657: lock_passwd = NULL;
658: success = 1;
659: } else if (!locked && lock) {
660: locked = 1;
661: lock_passwd = xstrdup(passwd);
662: success = 1;
663: }
1.183 djm 664: explicit_bzero(passwd, strlen(passwd));
1.173 djm 665: free(passwd);
1.194 markus 666: send_status(e, success);
1.88 markus 667: }
668:
669: static void
670: no_identities(SocketEntry *e, u_int type)
671: {
1.194 markus 672: struct sshbuf *msg;
673: int r;
1.88 markus 674:
1.194 markus 675: if ((msg = sshbuf_new()) == NULL)
676: fatal("%s: sshbuf_new failed", __func__);
677: if ((r = sshbuf_put_u8(msg,
1.88 markus 678: (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
1.194 markus 679: SSH_AGENT_RSA_IDENTITIES_ANSWER :
680: SSH2_AGENT_IDENTITIES_ANSWER)) != 0 ||
681: (r = sshbuf_put_u32(msg, 0)) != 0 ||
682: (r = sshbuf_put_stringb(e->output, msg)) != 0)
683: fatal("%s: buffer error: %s", __func__, ssh_err(r));
684: sshbuf_free(msg);
1.88 markus 685: }
1.59 markus 686:
1.163 markus 687: #ifdef ENABLE_PKCS11
1.59 markus 688: static void
1.158 djm 689: process_add_smartcard_key(SocketEntry *e)
1.59 markus 690: {
1.163 markus 691: char *provider = NULL, *pin;
1.194 markus 692: int r, i, version, count = 0, success = 0, confirm = 0;
693: u_int seconds;
1.174 dtucker 694: time_t death = 0;
1.194 markus 695: u_char type;
696: struct sshkey **keys = NULL, *k;
1.84 markus 697: Identity *id;
1.59 markus 698: Idtab *tab;
1.75 deraadt 699:
1.194 markus 700: if ((r = sshbuf_get_cstring(e->request, &provider, NULL)) != 0 ||
701: (r = sshbuf_get_cstring(e->request, &pin, NULL)) != 0)
702: fatal("%s: buffer error: %s", __func__, ssh_err(r));
703:
704: while (sshbuf_len(e->request)) {
705: if ((r = sshbuf_get_u8(e->request, &type)) != 0)
706: fatal("%s: buffer error: %s", __func__, ssh_err(r));
707: switch (type) {
1.110 djm 708: case SSH_AGENT_CONSTRAIN_LIFETIME:
1.194 markus 709: if ((r = sshbuf_get_u32(e->request, &seconds)) != 0)
710: fatal("%s: buffer error: %s",
711: __func__, ssh_err(r));
712: death = monotime() + seconds;
1.110 djm 713: break;
714: case SSH_AGENT_CONSTRAIN_CONFIRM:
715: confirm = 1;
716: break;
717: default:
1.158 djm 718: error("process_add_smartcard_key: "
719: "Unknown constraint type %d", type);
720: goto send;
1.110 djm 721: }
722: }
723: if (lifetime && !death)
1.175 dtucker 724: death = monotime() + lifetime;
1.110 djm 725:
1.163 markus 726: count = pkcs11_add_provider(provider, pin, &keys);
727: for (i = 0; i < count; i++) {
1.84 markus 728: k = keys[i];
729: version = k->type == KEY_RSA1 ? 1 : 2;
730: tab = idtab_lookup(version);
731: if (lookup_identity(k, version) == NULL) {
1.163 markus 732: id = xcalloc(1, sizeof(Identity));
1.84 markus 733: id->key = k;
1.163 markus 734: id->provider = xstrdup(provider);
735: id->comment = xstrdup(provider); /* XXX */
1.110 djm 736: id->death = death;
737: id->confirm = confirm;
1.84 markus 738: TAILQ_INSERT_TAIL(&tab->idlist, id, next);
739: tab->nentries++;
740: success = 1;
741: } else {
1.194 markus 742: sshkey_free(k);
1.84 markus 743: }
744: keys[i] = NULL;
1.59 markus 745: }
746: send:
1.173 djm 747: free(pin);
748: free(provider);
749: free(keys);
1.194 markus 750: send_status(e, success);
1.59 markus 751: }
752:
753: static void
754: process_remove_smartcard_key(SocketEntry *e)
755: {
1.163 markus 756: char *provider = NULL, *pin = NULL;
1.194 markus 757: int r, version, success = 0;
1.163 markus 758: Identity *id, *nxt;
1.84 markus 759: Idtab *tab;
1.59 markus 760:
1.194 markus 761: if ((r = sshbuf_get_cstring(e->request, &provider, NULL)) != 0 ||
762: (r = sshbuf_get_cstring(e->request, &pin, NULL)) != 0)
763: fatal("%s: buffer error: %s", __func__, ssh_err(r));
1.173 djm 764: free(pin);
1.59 markus 765:
1.163 markus 766: for (version = 1; version < 3; version++) {
767: tab = idtab_lookup(version);
768: for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
769: nxt = TAILQ_NEXT(id, next);
1.181 djm 770: /* Skip file--based keys */
771: if (id->provider == NULL)
772: continue;
1.163 markus 773: if (!strcmp(provider, id->provider)) {
774: TAILQ_REMOVE(&tab->idlist, id, next);
775: free_identity(id);
776: tab->nentries--;
777: }
1.59 markus 778: }
779: }
1.163 markus 780: if (pkcs11_del_provider(provider) == 0)
781: success = 1;
782: else
783: error("process_remove_smartcard_key:"
784: " pkcs11_del_provider failed");
1.173 djm 785: free(provider);
1.194 markus 786: send_status(e, success);
1.59 markus 787: }
1.163 markus 788: #endif /* ENABLE_PKCS11 */
1.59 markus 789:
1.33 markus 790: /* dispatch incoming messages */
791:
1.55 itojun 792: static void
1.2 provos 793: process_message(SocketEntry *e)
1.1 deraadt 794: {
1.194 markus 795: u_int msg_len;
796: u_char type;
797: const u_char *cp;
798: int r;
1.89 markus 799:
1.194 markus 800: if (sshbuf_len(e->input) < 5)
1.21 markus 801: return; /* Incomplete message. */
1.194 markus 802: cp = sshbuf_ptr(e->input);
803: msg_len = PEEK_U32(cp);
1.21 markus 804: if (msg_len > 256 * 1024) {
1.101 stevesk 805: close_socket(e);
1.21 markus 806: return;
807: }
1.194 markus 808: if (sshbuf_len(e->input) < msg_len + 4)
1.21 markus 809: return;
1.87 markus 810:
811: /* move the current input to e->request */
1.194 markus 812: sshbuf_reset(e->request);
813: if ((r = sshbuf_get_stringb(e->input, e->request)) != 0 ||
814: (r = sshbuf_get_u8(e->request, &type)) != 0)
815: fatal("%s: buffer error: %s", __func__, ssh_err(r));
1.21 markus 816:
1.88 markus 817: /* check wheter agent is locked */
818: if (locked && type != SSH_AGENTC_UNLOCK) {
1.194 markus 819: sshbuf_reset(e->request);
1.88 markus 820: switch (type) {
821: case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
822: case SSH2_AGENTC_REQUEST_IDENTITIES:
823: /* send empty lists */
824: no_identities(e, type);
825: break;
826: default:
827: /* send a fail message for all other request types */
1.194 markus 828: send_status(e, 0);
1.88 markus 829: }
830: return;
831: }
832:
1.59 markus 833: debug("type %d", type);
1.21 markus 834: switch (type) {
1.88 markus 835: case SSH_AGENTC_LOCK:
836: case SSH_AGENTC_UNLOCK:
837: process_lock_agent(e, type == SSH_AGENTC_LOCK);
838: break;
1.185 markus 839: #ifdef WITH_SSH1
1.33 markus 840: /* ssh1 */
841: case SSH_AGENTC_RSA_CHALLENGE:
842: process_authentication_challenge1(e);
843: break;
1.21 markus 844: case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
1.33 markus 845: process_request_identities(e, 1);
1.21 markus 846: break;
847: case SSH_AGENTC_ADD_RSA_IDENTITY:
1.94 markus 848: case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED:
1.33 markus 849: process_add_identity(e, 1);
1.21 markus 850: break;
851: case SSH_AGENTC_REMOVE_RSA_IDENTITY:
1.33 markus 852: process_remove_identity(e, 1);
1.21 markus 853: break;
1.199 ! djm 854: #endif
1.21 markus 855: case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
1.199 ! djm 856: process_remove_all_identities(e, 1); /* safe for !WITH_SSH1 */
1.33 markus 857: break;
858: /* ssh2 */
859: case SSH2_AGENTC_SIGN_REQUEST:
860: process_sign_request2(e);
861: break;
862: case SSH2_AGENTC_REQUEST_IDENTITIES:
863: process_request_identities(e, 2);
864: break;
865: case SSH2_AGENTC_ADD_IDENTITY:
1.94 markus 866: case SSH2_AGENTC_ADD_ID_CONSTRAINED:
1.33 markus 867: process_add_identity(e, 2);
868: break;
869: case SSH2_AGENTC_REMOVE_IDENTITY:
870: process_remove_identity(e, 2);
871: break;
872: case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
873: process_remove_all_identities(e, 2);
1.21 markus 874: break;
1.163 markus 875: #ifdef ENABLE_PKCS11
1.59 markus 876: case SSH_AGENTC_ADD_SMARTCARD_KEY:
1.110 djm 877: case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED:
1.59 markus 878: process_add_smartcard_key(e);
1.75 deraadt 879: break;
1.59 markus 880: case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
881: process_remove_smartcard_key(e);
1.75 deraadt 882: break;
1.163 markus 883: #endif /* ENABLE_PKCS11 */
1.21 markus 884: default:
885: /* Unknown message. Respond with failure. */
886: error("Unknown message %d", type);
1.194 markus 887: sshbuf_reset(e->request);
888: send_status(e, 0);
1.21 markus 889: break;
890: }
1.1 deraadt 891: }
892:
1.55 itojun 893: static void
1.73 stevesk 894: new_socket(sock_type type, int fd)
1.1 deraadt 895: {
1.112 markus 896: u_int i, old_alloc, new_alloc;
1.96 deraadt 897:
1.119 djm 898: set_nonblock(fd);
1.21 markus 899:
900: if (fd > max_fd)
901: max_fd = fd;
902:
903: for (i = 0; i < sockets_alloc; i++)
904: if (sockets[i].type == AUTH_UNUSED) {
905: sockets[i].fd = fd;
1.194 markus 906: if ((sockets[i].input = sshbuf_new()) == NULL)
907: fatal("%s: sshbuf_new failed", __func__);
908: if ((sockets[i].output = sshbuf_new()) == NULL)
909: fatal("%s: sshbuf_new failed", __func__);
910: if ((sockets[i].request = sshbuf_new()) == NULL)
911: fatal("%s: sshbuf_new failed", __func__);
1.112 markus 912: sockets[i].type = type;
1.21 markus 913: return;
914: }
915: old_alloc = sockets_alloc;
1.112 markus 916: new_alloc = sockets_alloc + 10;
1.133 djm 917: sockets = xrealloc(sockets, new_alloc, sizeof(sockets[0]));
1.112 markus 918: for (i = old_alloc; i < new_alloc; i++)
1.21 markus 919: sockets[i].type = AUTH_UNUSED;
1.112 markus 920: sockets_alloc = new_alloc;
1.21 markus 921: sockets[old_alloc].fd = fd;
1.194 markus 922: if ((sockets[old_alloc].input = sshbuf_new()) == NULL)
923: fatal("%s: sshbuf_new failed", __func__);
924: if ((sockets[old_alloc].output = sshbuf_new()) == NULL)
925: fatal("%s: sshbuf_new failed", __func__);
926: if ((sockets[old_alloc].request = sshbuf_new()) == NULL)
927: fatal("%s: sshbuf_new failed", __func__);
1.112 markus 928: sockets[old_alloc].type = type;
1.1 deraadt 929: }
930:
1.55 itojun 931: static int
1.155 dtucker 932: prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, u_int *nallocp,
933: struct timeval **tvpp)
1.1 deraadt 934: {
1.174 dtucker 935: u_int i, sz;
1.46 markus 936: int n = 0;
1.155 dtucker 937: static struct timeval tv;
1.174 dtucker 938: time_t deadline;
1.46 markus 939:
940: for (i = 0; i < sockets_alloc; i++) {
1.21 markus 941: switch (sockets[i].type) {
942: case AUTH_SOCKET:
943: case AUTH_CONNECTION:
1.46 markus 944: n = MAX(n, sockets[i].fd);
1.21 markus 945: break;
946: case AUTH_UNUSED:
947: break;
948: default:
949: fatal("Unknown socket type %d", sockets[i].type);
950: break;
951: }
1.46 markus 952: }
953:
954: sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
1.66 markus 955: if (*fdrp == NULL || sz > *nallocp) {
1.173 djm 956: free(*fdrp);
957: free(*fdwp);
1.46 markus 958: *fdrp = xmalloc(sz);
959: *fdwp = xmalloc(sz);
1.66 markus 960: *nallocp = sz;
1.46 markus 961: }
1.66 markus 962: if (n < *fdl)
963: debug("XXX shrink: %d < %d", n, *fdl);
964: *fdl = n;
1.46 markus 965: memset(*fdrp, 0, sz);
966: memset(*fdwp, 0, sz);
967:
968: for (i = 0; i < sockets_alloc; i++) {
969: switch (sockets[i].type) {
970: case AUTH_SOCKET:
971: case AUTH_CONNECTION:
972: FD_SET(sockets[i].fd, *fdrp);
1.194 markus 973: if (sshbuf_len(sockets[i].output) > 0)
1.46 markus 974: FD_SET(sockets[i].fd, *fdwp);
975: break;
976: default:
977: break;
978: }
979: }
1.155 dtucker 980: deadline = reaper();
981: if (parent_alive_interval != 0)
982: deadline = (deadline == 0) ? parent_alive_interval :
983: MIN(deadline, parent_alive_interval);
984: if (deadline == 0) {
985: *tvpp = NULL;
986: } else {
987: tv.tv_sec = deadline;
988: tv.tv_usec = 0;
989: *tvpp = &tv;
990: }
1.46 markus 991: return (1);
1.21 markus 992: }
993:
1.55 itojun 994: static void
1.21 markus 995: after_select(fd_set *readset, fd_set *writeset)
996: {
1.96 deraadt 997: struct sockaddr_un sunaddr;
1.26 markus 998: socklen_t slen;
1.21 markus 999: char buf[1024];
1.194 markus 1000: int len, sock, r;
1.162 djm 1001: u_int i, orig_alloc;
1.103 markus 1002: uid_t euid;
1003: gid_t egid;
1.21 markus 1004:
1.162 djm 1005: for (i = 0, orig_alloc = sockets_alloc; i < orig_alloc; i++)
1.21 markus 1006: switch (sockets[i].type) {
1007: case AUTH_UNUSED:
1008: break;
1009: case AUTH_SOCKET:
1010: if (FD_ISSET(sockets[i].fd, readset)) {
1.26 markus 1011: slen = sizeof(sunaddr);
1.46 markus 1012: sock = accept(sockets[i].fd,
1.131 deraadt 1013: (struct sockaddr *)&sunaddr, &slen);
1.21 markus 1014: if (sock < 0) {
1.81 stevesk 1015: error("accept from AUTH_SOCKET: %s",
1016: strerror(errno));
1.103 markus 1017: break;
1018: }
1019: if (getpeereid(sock, &euid, &egid) < 0) {
1020: error("getpeereid %d failed: %s",
1021: sock, strerror(errno));
1022: close(sock);
1023: break;
1024: }
1.105 markus 1025: if ((euid != 0) && (getuid() != euid)) {
1.103 markus 1026: error("uid mismatch: "
1.104 stevesk 1027: "peer euid %u != uid %u",
1028: (u_int) euid, (u_int) getuid());
1.103 markus 1029: close(sock);
1.21 markus 1030: break;
1031: }
1032: new_socket(AUTH_CONNECTION, sock);
1033: }
1034: break;
1035: case AUTH_CONNECTION:
1.194 markus 1036: if (sshbuf_len(sockets[i].output) > 0 &&
1.21 markus 1037: FD_ISSET(sockets[i].fd, writeset)) {
1.162 djm 1038: len = write(sockets[i].fd,
1.194 markus 1039: sshbuf_ptr(sockets[i].output),
1040: sshbuf_len(sockets[i].output));
1.162 djm 1041: if (len == -1 && (errno == EAGAIN ||
1042: errno == EINTR))
1043: continue;
1.21 markus 1044: if (len <= 0) {
1.101 stevesk 1045: close_socket(&sockets[i]);
1.21 markus 1046: break;
1047: }
1.194 markus 1048: if ((r = sshbuf_consume(sockets[i].output,
1049: len)) != 0)
1050: fatal("%s: buffer error: %s",
1051: __func__, ssh_err(r));
1.21 markus 1052: }
1053: if (FD_ISSET(sockets[i].fd, readset)) {
1.162 djm 1054: len = read(sockets[i].fd, buf, sizeof(buf));
1055: if (len == -1 && (errno == EAGAIN ||
1056: errno == EINTR))
1057: continue;
1.21 markus 1058: if (len <= 0) {
1.101 stevesk 1059: close_socket(&sockets[i]);
1.21 markus 1060: break;
1061: }
1.194 markus 1062: if ((r = sshbuf_put(sockets[i].input,
1063: buf, len)) != 0)
1064: fatal("%s: buffer error: %s",
1065: __func__, ssh_err(r));
1.190 dtucker 1066: explicit_bzero(buf, sizeof(buf));
1.21 markus 1067: process_message(&sockets[i]);
1068: }
1069: break;
1070: default:
1071: fatal("Unknown type %d", sockets[i].type);
1072: }
1.1 deraadt 1073: }
1074:
1.55 itojun 1075: static void
1.113 markus 1076: cleanup_socket(void)
1.15 markus 1077: {
1.187 djm 1078: if (cleanup_pid != 0 && getpid() != cleanup_pid)
1079: return;
1080: debug("%s: cleanup", __func__);
1.48 deraadt 1081: if (socket_name[0])
1082: unlink(socket_name);
1083: if (socket_dir[0])
1084: rmdir(socket_dir);
1.10 markus 1085: }
1086:
1.114 markus 1087: void
1.15 markus 1088: cleanup_exit(int i)
1089: {
1.113 markus 1090: cleanup_socket();
1091: _exit(i);
1.15 markus 1092: }
1093:
1.135 deraadt 1094: /*ARGSUSED*/
1.55 itojun 1095: static void
1.48 deraadt 1096: cleanup_handler(int sig)
1097: {
1.113 markus 1098: cleanup_socket();
1.163 markus 1099: #ifdef ENABLE_PKCS11
1100: pkcs11_terminate();
1101: #endif
1.48 deraadt 1102: _exit(2);
1.113 markus 1103: }
1104:
1.68 markus 1105: static void
1.155 dtucker 1106: check_parent_exists(void)
1.68 markus 1107: {
1.172 dtucker 1108: /*
1109: * If our parent has exited then getppid() will return (pid_t)1,
1110: * so testing for that should be safe.
1111: */
1112: if (parent_pid != -1 && getppid() != parent_pid) {
1.68 markus 1113: /* printf("Parent has died - Authentication agent exiting.\n"); */
1.155 dtucker 1114: cleanup_socket();
1115: _exit(2);
1.68 markus 1116: }
1.48 deraadt 1117: }
1118:
1.55 itojun 1119: static void
1.50 itojun 1120: usage(void)
1.15 markus 1121: {
1.184 deraadt 1122: fprintf(stderr,
1.193 jmc 1123: "usage: ssh-agent [-c | -s] [-d] [-a bind_address] [-E fingerprint_hash]\n"
1124: " [-t life] [command [arg ...]]\n"
1.184 deraadt 1125: " ssh-agent [-c | -s] -k\n");
1.21 markus 1126: exit(1);
1.15 markus 1127: }
1128:
1.2 provos 1129: int
1130: main(int ac, char **av)
1.1 deraadt 1131: {
1.107 markus 1132: int c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0;
1.154 dtucker 1133: int sock, fd, ch, result, saved_errno;
1.120 avsm 1134: u_int nalloc;
1.96 deraadt 1135: char *shell, *format, *pidstr, *agentsocket = NULL;
1136: fd_set *readsetp = NULL, *writesetp = NULL;
1.41 markus 1137: struct rlimit rlim;
1.96 deraadt 1138: extern int optind;
1.98 stevesk 1139: extern char *optarg;
1.21 markus 1140: pid_t pid;
1.96 deraadt 1141: char pidstrbuf[1 + 3 * sizeof pid];
1.155 dtucker 1142: struct timeval *tvp = NULL;
1.161 tobias 1143: size_t len;
1.189 djm 1144: mode_t prev_mask;
1.123 djm 1145:
1146: /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1147: sanitise_stdfd();
1.99 markus 1148:
1149: /* drop */
1150: setegid(getgid());
1151: setgid(getgid());
1.53 markus 1152:
1.185 markus 1153: #ifdef WITH_OPENSSL
1.170 djm 1154: OpenSSL_add_all_algorithms();
1.185 markus 1155: #endif
1.21 markus 1156:
1.192 djm 1157: while ((ch = getopt(ac, av, "cdksE:a:t:")) != -1) {
1.21 markus 1158: switch (ch) {
1.192 djm 1159: case 'E':
1160: fingerprint_hash = ssh_digest_alg_by_name(optarg);
1161: if (fingerprint_hash == -1)
1162: fatal("Invalid hash algorithm \"%s\"", optarg);
1163: break;
1.21 markus 1164: case 'c':
1165: if (s_flag)
1166: usage();
1167: c_flag++;
1168: break;
1169: case 'k':
1170: k_flag++;
1171: break;
1172: case 's':
1173: if (c_flag)
1174: usage();
1175: s_flag++;
1176: break;
1.57 markus 1177: case 'd':
1178: if (d_flag)
1179: usage();
1180: d_flag++;
1181: break;
1.86 markus 1182: case 'a':
1183: agentsocket = optarg;
1.106 marc 1184: break;
1185: case 't':
1186: if ((lifetime = convtime(optarg)) == -1) {
1187: fprintf(stderr, "Invalid lifetime\n");
1188: usage();
1189: }
1.86 markus 1190: break;
1.21 markus 1191: default:
1192: usage();
1193: }
1194: }
1195: ac -= optind;
1196: av += optind;
1197:
1.57 markus 1198: if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
1.21 markus 1199: usage();
1200:
1.85 markus 1201: if (ac == 0 && !c_flag && !s_flag) {
1.21 markus 1202: shell = getenv("SHELL");
1.161 tobias 1203: if (shell != NULL && (len = strlen(shell)) > 2 &&
1204: strncmp(shell + len - 3, "csh", 3) == 0)
1.21 markus 1205: c_flag = 1;
1206: }
1207: if (k_flag) {
1.136 deraadt 1208: const char *errstr = NULL;
1209:
1.21 markus 1210: pidstr = getenv(SSH_AGENTPID_ENV_NAME);
1211: if (pidstr == NULL) {
1212: fprintf(stderr, "%s not set, cannot kill agent\n",
1.46 markus 1213: SSH_AGENTPID_ENV_NAME);
1.21 markus 1214: exit(1);
1215: }
1.136 deraadt 1216: pid = (int)strtonum(pidstr, 2, INT_MAX, &errstr);
1217: if (errstr) {
1218: fprintf(stderr,
1219: "%s=\"%s\", which is not a good PID: %s\n",
1220: SSH_AGENTPID_ENV_NAME, pidstr, errstr);
1.21 markus 1221: exit(1);
1222: }
1223: if (kill(pid, SIGTERM) == -1) {
1224: perror("kill");
1225: exit(1);
1226: }
1227: format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1.140 djm 1228: printf(format, SSH_AUTHSOCKET_ENV_NAME);
1229: printf(format, SSH_AGENTPID_ENV_NAME);
1.91 mpech 1230: printf("echo Agent pid %ld killed;\n", (long)pid);
1.21 markus 1231: exit(0);
1232: }
1233: parent_pid = getpid();
1234:
1.86 markus 1235: if (agentsocket == NULL) {
1236: /* Create private directory for agent socket */
1.171 djm 1237: mktemp_proto(socket_dir, sizeof(socket_dir));
1.86 markus 1238: if (mkdtemp(socket_dir) == NULL) {
1239: perror("mkdtemp: private socket dir");
1240: exit(1);
1241: }
1.91 mpech 1242: snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1243: (long)parent_pid);
1.86 markus 1244: } else {
1245: /* Try to use specified agent socket */
1246: socket_dir[0] = '\0';
1247: strlcpy(socket_name, agentsocket, sizeof socket_name);
1.21 markus 1248: }
1249:
1.23 markus 1250: /*
1251: * Create socket early so it will exist before command gets run from
1252: * the parent.
1253: */
1.189 djm 1254: prev_mask = umask(0177);
1.188 millert 1255: sock = unix_listener(socket_name, SSH_LISTEN_BACKLOG, 0);
1.21 markus 1256: if (sock < 0) {
1.188 millert 1257: /* XXX - unix_listener() calls error() not perror() */
1.121 djm 1258: *socket_name = '\0'; /* Don't unlink any existing file */
1.21 markus 1259: cleanup_exit(1);
1260: }
1.189 djm 1261: umask(prev_mask);
1.46 markus 1262:
1.23 markus 1263: /*
1264: * Fork, and have the parent execute the command, if any, or present
1265: * the socket data. The child continues as the authentication agent.
1266: */
1.57 markus 1267: if (d_flag) {
1268: log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
1269: format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1270: printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1271: SSH_AUTHSOCKET_ENV_NAME);
1.91 mpech 1272: printf("echo Agent pid %ld;\n", (long)parent_pid);
1.57 markus 1273: goto skip;
1274: }
1.21 markus 1275: pid = fork();
1276: if (pid == -1) {
1277: perror("fork");
1.81 stevesk 1278: cleanup_exit(1);
1.21 markus 1279: }
1280: if (pid != 0) { /* Parent - execute the given command. */
1281: close(sock);
1.91 mpech 1282: snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
1.21 markus 1283: if (ac == 0) {
1284: format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1285: printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1.46 markus 1286: SSH_AUTHSOCKET_ENV_NAME);
1.21 markus 1287: printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
1.46 markus 1288: SSH_AGENTPID_ENV_NAME);
1.91 mpech 1289: printf("echo Agent pid %ld;\n", (long)pid);
1.21 markus 1290: exit(0);
1291: }
1.36 deraadt 1292: if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1293: setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1294: perror("setenv");
1295: exit(1);
1296: }
1.21 markus 1297: execvp(av[0], av);
1298: perror(av[0]);
1299: exit(1);
1300: }
1.81 stevesk 1301: /* child */
1302: log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
1.67 stevesk 1303:
1304: if (setsid() == -1) {
1.81 stevesk 1305: error("setsid: %s", strerror(errno));
1.67 stevesk 1306: cleanup_exit(1);
1307: }
1308:
1309: (void)chdir("/");
1.107 markus 1310: if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1311: /* XXX might close listen socket */
1312: (void)dup2(fd, STDIN_FILENO);
1313: (void)dup2(fd, STDOUT_FILENO);
1314: (void)dup2(fd, STDERR_FILENO);
1315: if (fd > 2)
1316: close(fd);
1317: }
1.21 markus 1318:
1.41 markus 1319: /* deny core dumps, since memory contains unencrypted private keys */
1320: rlim.rlim_cur = rlim.rlim_max = 0;
1321: if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
1.81 stevesk 1322: error("setrlimit RLIMIT_CORE: %s", strerror(errno));
1.21 markus 1323: cleanup_exit(1);
1324: }
1.57 markus 1325:
1326: skip:
1.187 djm 1327:
1328: cleanup_pid = getpid();
1.163 markus 1329:
1330: #ifdef ENABLE_PKCS11
1331: pkcs11_init(0);
1332: #endif
1.21 markus 1333: new_socket(AUTH_SOCKET, sock);
1.155 dtucker 1334: if (ac > 0)
1335: parent_alive_interval = 10;
1.33 markus 1336: idtab_init();
1.61 markus 1337: signal(SIGPIPE, SIG_IGN);
1.177 djm 1338: signal(SIGINT, d_flag ? cleanup_handler : SIG_IGN);
1.48 deraadt 1339: signal(SIGHUP, cleanup_handler);
1340: signal(SIGTERM, cleanup_handler);
1.66 markus 1341: nalloc = 0;
1342:
1.21 markus 1343: while (1) {
1.155 dtucker 1344: prepare_select(&readsetp, &writesetp, &max_fd, &nalloc, &tvp);
1345: result = select(max_fd + 1, readsetp, writesetp, NULL, tvp);
1.154 dtucker 1346: saved_errno = errno;
1.155 dtucker 1347: if (parent_alive_interval != 0)
1348: check_parent_exists();
1349: (void) reaper(); /* remove expired keys */
1.154 dtucker 1350: if (result < 0) {
1351: if (saved_errno == EINTR)
1.21 markus 1352: continue;
1.154 dtucker 1353: fatal("select: %s", strerror(saved_errno));
1354: } else if (result > 0)
1355: after_select(readsetp, writesetp);
1.15 markus 1356: }
1.21 markus 1357: /* NOTREACHED */
1.1 deraadt 1358: }