Annotation of src/usr.bin/ssh/ssh-agent.c, Revision 1.233
1.233 ! djm 1: /* $OpenBSD: ssh-agent.c,v 1.232 2018/11/09 02:57:58 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.151 deraadt 37: #include <sys/types.h>
1.153 djm 38: #include <sys/time.h>
1.78 provos 39: #include <sys/queue.h>
1.127 stevesk 40: #include <sys/resource.h>
1.141 stevesk 41: #include <sys/socket.h>
1.189 djm 42: #include <sys/stat.h>
1.128 stevesk 43: #include <sys/un.h>
1.126 stevesk 44:
1.185 markus 45: #ifdef WITH_OPENSSL
1.146 stevesk 46: #include <openssl/evp.h>
1.185 markus 47: #endif
1.146 stevesk 48:
1.143 stevesk 49: #include <errno.h>
1.142 stevesk 50: #include <fcntl.h>
1.126 stevesk 51: #include <paths.h>
1.223 djm 52: #include <poll.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.203 dtucker 60: #include <util.h>
1.194 markus 61:
1.151 deraadt 62: #include "xmalloc.h"
1.1 deraadt 63: #include "ssh.h"
1.194 markus 64: #include "sshbuf.h"
65: #include "sshkey.h"
1.32 markus 66: #include "authfd.h"
1.37 markus 67: #include "compat.h"
1.47 markus 68: #include "log.h"
1.107 markus 69: #include "misc.h"
1.182 markus 70: #include "digest.h"
1.194 markus 71: #include "ssherr.h"
1.215 djm 72: #include "match.h"
1.7 deraadt 73:
1.163 markus 74: #ifdef ENABLE_PKCS11
75: #include "ssh-pkcs11.h"
1.71 jakob 76: #endif
1.59 markus 77:
1.215 djm 78: #ifndef DEFAULT_PKCS11_WHITELIST
1.216 djm 79: # define DEFAULT_PKCS11_WHITELIST "/usr/lib*/*,/usr/local/lib*/*"
1.215 djm 80: #endif
81:
1.223 djm 82: /* Maximum accepted message length */
83: #define AGENT_MAX_LEN (256*1024)
1.233 ! djm 84: /* Maximum bytes to read from client socket */
! 85: #define AGENT_RBUF_LEN (4096)
1.223 djm 86:
1.73 stevesk 87: typedef enum {
88: AUTH_UNUSED,
89: AUTH_SOCKET,
90: AUTH_CONNECTION
91: } sock_type;
92:
1.21 markus 93: typedef struct {
94: int fd;
1.73 stevesk 95: sock_type type;
1.194 markus 96: struct sshbuf *input;
97: struct sshbuf *output;
98: struct sshbuf *request;
1.1 deraadt 99: } SocketEntry;
100:
1.45 markus 101: u_int sockets_alloc = 0;
1.1 deraadt 102: SocketEntry *sockets = NULL;
103:
1.78 provos 104: typedef struct identity {
105: TAILQ_ENTRY(identity) next;
1.194 markus 106: struct sshkey *key;
1.21 markus 107: char *comment;
1.163 markus 108: char *provider;
1.174 dtucker 109: time_t death;
1.107 markus 110: u_int confirm;
1.1 deraadt 111: } Identity;
112:
1.221 djm 113: struct idtable {
1.33 markus 114: int nentries;
1.78 provos 115: TAILQ_HEAD(idqueue, identity) idlist;
1.221 djm 116: };
1.33 markus 117:
1.221 djm 118: /* private key table */
119: struct idtable *idtab;
1.1 deraadt 120:
121: int max_fd = 0;
122:
1.11 markus 123: /* pid of shell == parent of agent */
1.29 deraadt 124: pid_t parent_pid = -1;
1.176 dtucker 125: time_t parent_alive_interval = 0;
1.10 markus 126:
1.187 djm 127: /* pid of process for which cleanup_socket is applicable */
128: pid_t cleanup_pid = 0;
129:
1.10 markus 130: /* pathname and directory for AUTH_SOCKET */
1.196 deraadt 131: char socket_name[PATH_MAX];
132: char socket_dir[PATH_MAX];
1.10 markus 133:
1.215 djm 134: /* PKCS#11 path whitelist */
135: static char *pkcs11_whitelist;
136:
1.88 markus 137: /* locking */
1.203 dtucker 138: #define LOCK_SIZE 32
139: #define LOCK_SALT_SIZE 16
140: #define LOCK_ROUNDS 1
1.88 markus 141: int locked = 0;
1.213 djm 142: u_char lock_pwhash[LOCK_SIZE];
143: u_char lock_salt[LOCK_SALT_SIZE];
1.88 markus 144:
1.20 markus 145: extern char *__progname;
146:
1.174 dtucker 147: /* Default lifetime in seconds (0 == forever) */
148: static long lifetime = 0;
1.106 marc 149:
1.192 djm 150: static int fingerprint_hash = SSH_FP_HASH_DEFAULT;
151:
1.55 itojun 152: static void
1.101 stevesk 153: close_socket(SocketEntry *e)
154: {
155: close(e->fd);
156: e->fd = -1;
157: e->type = AUTH_UNUSED;
1.194 markus 158: sshbuf_free(e->input);
159: sshbuf_free(e->output);
160: sshbuf_free(e->request);
1.101 stevesk 161: }
162:
163: static void
1.33 markus 164: idtab_init(void)
1.1 deraadt 165: {
1.221 djm 166: idtab = xcalloc(1, sizeof(*idtab));
167: TAILQ_INIT(&idtab->idlist);
168: idtab->nentries = 0;
1.33 markus 169: }
170:
1.89 markus 171: static void
172: free_identity(Identity *id)
173: {
1.194 markus 174: sshkey_free(id->key);
1.173 djm 175: free(id->provider);
176: free(id->comment);
177: free(id);
1.89 markus 178: }
179:
1.33 markus 180: /* return matching private key for given public key */
1.78 provos 181: static Identity *
1.221 djm 182: lookup_identity(struct sshkey *key)
1.33 markus 183: {
1.78 provos 184: Identity *id;
185:
1.221 djm 186: TAILQ_FOREACH(id, &idtab->idlist, next) {
1.194 markus 187: if (sshkey_equal(key, id->key))
1.78 provos 188: return (id);
1.33 markus 189: }
1.78 provos 190: return (NULL);
191: }
192:
1.107 markus 193: /* Check confirmation of keysign request */
194: static int
195: confirm_key(Identity *id)
196: {
1.122 djm 197: char *p;
1.107 markus 198: int ret = -1;
199:
1.194 markus 200: p = sshkey_fingerprint(id->key, fingerprint_hash, SSH_FP_DEFAULT);
1.197 djm 201: if (p != NULL &&
202: ask_permission("Allow use of key %s?\nKey fingerprint %s.",
1.122 djm 203: id->comment, p))
204: ret = 0;
1.173 djm 205: free(p);
1.122 djm 206:
1.107 markus 207: return (ret);
208: }
209:
1.194 markus 210: static void
211: send_status(SocketEntry *e, int success)
212: {
213: int r;
214:
215: if ((r = sshbuf_put_u32(e->output, 1)) != 0 ||
216: (r = sshbuf_put_u8(e->output, success ?
217: SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE)) != 0)
218: fatal("%s: buffer error: %s", __func__, ssh_err(r));
219: }
220:
1.33 markus 221: /* send list of supported public keys to 'client' */
1.55 itojun 222: static void
1.221 djm 223: process_request_identities(SocketEntry *e)
1.33 markus 224: {
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__);
1.221 djm 231: if ((r = sshbuf_put_u8(msg, SSH2_AGENT_IDENTITIES_ANSWER)) != 0 ||
232: (r = sshbuf_put_u32(msg, idtab->nentries)) != 0)
233: fatal("%s: buffer error: %s", __func__, ssh_err(r));
234: TAILQ_FOREACH(id, &idtab->idlist, next) {
1.228 markus 235: if ((r = sshkey_puts_opts(id->key, msg, SSHKEY_SERIALIZE_INFO))
236: != 0 ||
1.221 djm 237: (r = sshbuf_put_cstring(msg, id->comment)) != 0) {
238: error("%s: put key/comment: %s", __func__,
1.220 djm 239: ssh_err(r));
240: continue;
1.33 markus 241: }
1.21 markus 242: }
1.194 markus 243: if ((r = sshbuf_put_stringb(e->output, msg)) != 0)
244: fatal("%s: buffer error: %s", __func__, ssh_err(r));
245: sshbuf_free(msg);
1.1 deraadt 246: }
247:
1.33 markus 248:
1.208 markus 249: static char *
250: agent_decode_alg(struct sshkey *key, u_int flags)
251: {
252: if (key->type == KEY_RSA) {
253: if (flags & SSH_AGENT_RSA_SHA2_256)
254: return "rsa-sha2-256";
255: else if (flags & SSH_AGENT_RSA_SHA2_512)
256: return "rsa-sha2-512";
257: }
258: return NULL;
259: }
260:
1.33 markus 261: /* ssh2 only */
1.55 itojun 262: static void
1.33 markus 263: process_sign_request2(SocketEntry *e)
264: {
1.221 djm 265: const u_char *data;
266: u_char *signature = NULL;
267: size_t dlen, slen = 0;
1.194 markus 268: u_int compat = 0, flags;
269: int r, ok = -1;
270: struct sshbuf *msg;
1.221 djm 271: struct sshkey *key = NULL;
1.195 djm 272: struct identity *id;
1.194 markus 273:
1.195 djm 274: if ((msg = sshbuf_new()) == NULL)
275: fatal("%s: sshbuf_new failed", __func__);
1.221 djm 276: if ((r = sshkey_froms(e->request, &key)) != 0 ||
277: (r = sshbuf_get_string_direct(e->request, &data, &dlen)) != 0 ||
1.225 djm 278: (r = sshbuf_get_u32(e->request, &flags)) != 0) {
279: error("%s: couldn't parse request: %s", __func__, ssh_err(r));
280: goto send;
281: }
282:
1.221 djm 283: if ((id = lookup_identity(key)) == NULL) {
1.195 djm 284: verbose("%s: %s key not found", __func__, sshkey_type(key));
285: goto send;
286: }
287: if (id->confirm && confirm_key(id) != 0) {
288: verbose("%s: user refused key", __func__);
289: goto send;
290: }
291: if ((r = sshkey_sign(id->key, &signature, &slen,
1.208 markus 292: data, dlen, agent_decode_alg(key, flags), compat)) != 0) {
1.209 djm 293: error("%s: sshkey_sign: %s", __func__, ssh_err(r));
1.195 djm 294: goto send;
1.33 markus 295: }
1.195 djm 296: /* Success */
297: ok = 0;
298: send:
299: sshkey_free(key);
1.33 markus 300: if (ok == 0) {
1.194 markus 301: if ((r = sshbuf_put_u8(msg, SSH2_AGENT_SIGN_RESPONSE)) != 0 ||
302: (r = sshbuf_put_string(msg, signature, slen)) != 0)
303: fatal("%s: buffer error: %s", __func__, ssh_err(r));
304: } else if ((r = sshbuf_put_u8(msg, SSH_AGENT_FAILURE)) != 0)
305: fatal("%s: buffer error: %s", __func__, ssh_err(r));
306:
307: if ((r = sshbuf_put_stringb(e->output, msg)) != 0)
308: fatal("%s: buffer error: %s", __func__, ssh_err(r));
309:
310: sshbuf_free(msg);
1.173 djm 311: free(signature);
1.1 deraadt 312: }
313:
1.33 markus 314: /* shared */
1.55 itojun 315: static void
1.221 djm 316: process_remove_identity(SocketEntry *e)
1.1 deraadt 317: {
1.194 markus 318: int r, success = 0;
319: struct sshkey *key = NULL;
1.221 djm 320: Identity *id;
1.21 markus 321:
1.221 djm 322: if ((r = sshkey_froms(e->request, &key)) != 0) {
323: error("%s: get key: %s", __func__, ssh_err(r));
324: goto done;
325: }
326: if ((id = lookup_identity(key)) == NULL) {
327: debug("%s: key not found", __func__);
328: goto done;
329: }
330: /* We have this key, free it. */
331: if (idtab->nentries < 1)
332: fatal("%s: internal error: nentries %d",
333: __func__, idtab->nentries);
334: TAILQ_REMOVE(&idtab->idlist, id, next);
335: free_identity(id);
336: idtab->nentries--;
337: sshkey_free(key);
338: success = 1;
339: done:
1.194 markus 340: send_status(e, success);
1.1 deraadt 341: }
342:
1.55 itojun 343: static void
1.221 djm 344: process_remove_all_identities(SocketEntry *e)
1.1 deraadt 345: {
1.78 provos 346: Identity *id;
1.21 markus 347:
348: /* Loop over all identities and clear the keys. */
1.221 djm 349: for (id = TAILQ_FIRST(&idtab->idlist); id;
350: id = TAILQ_FIRST(&idtab->idlist)) {
351: TAILQ_REMOVE(&idtab->idlist, id, next);
1.78 provos 352: free_identity(id);
1.21 markus 353: }
354:
355: /* Mark that there are no identities. */
1.221 djm 356: idtab->nentries = 0;
1.21 markus 357:
358: /* Send success. */
1.194 markus 359: send_status(e, 1);
1.1 deraadt 360: }
361:
1.155 dtucker 362: /* removes expired keys and returns number of seconds until the next expiry */
1.174 dtucker 363: static time_t
1.89 markus 364: reaper(void)
365: {
1.175 dtucker 366: time_t deadline = 0, now = monotime();
1.89 markus 367: Identity *id, *nxt;
368:
1.221 djm 369: for (id = TAILQ_FIRST(&idtab->idlist); id; id = nxt) {
370: nxt = TAILQ_NEXT(id, next);
371: if (id->death == 0)
372: continue;
373: if (now >= id->death) {
374: debug("expiring key '%s'", id->comment);
375: TAILQ_REMOVE(&idtab->idlist, id, next);
376: free_identity(id);
377: idtab->nentries--;
378: } else
379: deadline = (deadline == 0) ? id->death :
380: MINIMUM(deadline, id->death);
1.89 markus 381: }
1.155 dtucker 382: if (deadline == 0 || deadline <= now)
383: return 0;
384: else
385: return (deadline - now);
1.89 markus 386: }
387:
388: static void
1.221 djm 389: process_add_identity(SocketEntry *e)
1.1 deraadt 390: {
1.157 canacar 391: Identity *id;
1.194 markus 392: int success = 0, confirm = 0;
1.228 markus 393: u_int seconds, maxsign;
1.194 markus 394: char *comment = NULL;
1.174 dtucker 395: time_t death = 0;
1.194 markus 396: struct sshkey *k = NULL;
397: u_char ctype;
398: int r = SSH_ERR_INTERNAL_ERROR;
1.33 markus 399:
1.221 djm 400: if ((r = sshkey_private_deserialize(e->request, &k)) != 0 ||
401: k == NULL ||
1.194 markus 402: (r = sshbuf_get_cstring(e->request, &comment, NULL)) != 0) {
403: error("%s: decode private key: %s", __func__, ssh_err(r));
404: goto err;
405: }
1.186 djm 406:
1.194 markus 407: while (sshbuf_len(e->request)) {
408: if ((r = sshbuf_get_u8(e->request, &ctype)) != 0) {
409: error("%s: buffer error: %s", __func__, ssh_err(r));
410: goto err;
411: }
412: switch (ctype) {
1.94 markus 413: case SSH_AGENT_CONSTRAIN_LIFETIME:
1.194 markus 414: if ((r = sshbuf_get_u32(e->request, &seconds)) != 0) {
415: error("%s: bad lifetime constraint: %s",
416: __func__, ssh_err(r));
417: goto err;
418: }
419: death = monotime() + seconds;
1.94 markus 420: break;
1.107 markus 421: case SSH_AGENT_CONSTRAIN_CONFIRM:
422: confirm = 1;
423: break;
1.228 markus 424: case SSH_AGENT_CONSTRAIN_MAXSIGN:
425: if ((r = sshbuf_get_u32(e->request, &maxsign)) != 0) {
426: error("%s: bad maxsign constraint: %s",
427: __func__, ssh_err(r));
428: goto err;
429: }
430: if ((r = sshkey_enable_maxsign(k, maxsign)) != 0) {
431: error("%s: cannot enable maxsign: %s",
432: __func__, ssh_err(r));
433: goto err;
434: }
435: break;
1.94 markus 436: default:
1.194 markus 437: error("%s: Unknown constraint %d", __func__, ctype);
438: err:
439: sshbuf_reset(e->request);
1.173 djm 440: free(comment);
1.194 markus 441: sshkey_free(k);
1.158 djm 442: goto send;
1.94 markus 443: }
444: }
1.194 markus 445:
1.158 djm 446: success = 1;
1.106 marc 447: if (lifetime && !death)
1.175 dtucker 448: death = monotime() + lifetime;
1.221 djm 449: if ((id = lookup_identity(k)) == NULL) {
1.163 markus 450: id = xcalloc(1, sizeof(Identity));
1.221 djm 451: TAILQ_INSERT_TAIL(&idtab->idlist, id, next);
1.33 markus 452: /* Increment the number of identities. */
1.221 djm 453: idtab->nentries++;
1.33 markus 454: } else {
1.228 markus 455: /* key state might have been updated */
456: sshkey_free(id->key);
1.173 djm 457: free(id->comment);
1.33 markus 458: }
1.228 markus 459: id->key = k;
1.157 canacar 460: id->comment = comment;
461: id->death = death;
462: id->confirm = confirm;
1.33 markus 463: send:
1.194 markus 464: send_status(e, success);
1.1 deraadt 465: }
466:
1.88 markus 467: /* XXX todo: encrypt sensitive data with passphrase */
468: static void
469: process_lock_agent(SocketEntry *e, int lock)
470: {
1.203 dtucker 471: int r, success = 0, delay;
1.213 djm 472: char *passwd;
473: u_char passwdhash[LOCK_SIZE];
1.203 dtucker 474: static u_int fail_count = 0;
475: size_t pwlen;
476:
1.226 djm 477: /*
478: * This is deliberately fatal: the user has requested that we lock,
479: * but we can't parse their request properly. The only safe thing to
480: * do is abort.
481: */
1.203 dtucker 482: if ((r = sshbuf_get_cstring(e->request, &passwd, &pwlen)) != 0)
483: fatal("%s: buffer error: %s", __func__, ssh_err(r));
484: if (pwlen == 0) {
485: debug("empty password not supported");
486: } else if (locked && !lock) {
487: if (bcrypt_pbkdf(passwd, pwlen, lock_salt, sizeof(lock_salt),
488: passwdhash, sizeof(passwdhash), LOCK_ROUNDS) < 0)
489: fatal("bcrypt_pbkdf");
1.213 djm 490: if (timingsafe_bcmp(passwdhash, lock_pwhash, LOCK_SIZE) == 0) {
1.203 dtucker 491: debug("agent unlocked");
492: locked = 0;
493: fail_count = 0;
1.213 djm 494: explicit_bzero(lock_pwhash, sizeof(lock_pwhash));
1.203 dtucker 495: success = 1;
496: } else {
497: /* delay in 0.1s increments up to 10s */
498: if (fail_count < 100)
499: fail_count++;
500: delay = 100000 * fail_count;
501: debug("unlock failed, delaying %0.1lf seconds",
502: (double)delay/1000000);
503: usleep(delay);
504: }
505: explicit_bzero(passwdhash, sizeof(passwdhash));
1.88 markus 506: } else if (!locked && lock) {
1.203 dtucker 507: debug("agent locked");
1.88 markus 508: locked = 1;
1.203 dtucker 509: arc4random_buf(lock_salt, sizeof(lock_salt));
510: if (bcrypt_pbkdf(passwd, pwlen, lock_salt, sizeof(lock_salt),
1.213 djm 511: lock_pwhash, sizeof(lock_pwhash), LOCK_ROUNDS) < 0)
1.203 dtucker 512: fatal("bcrypt_pbkdf");
1.88 markus 513: success = 1;
514: }
1.203 dtucker 515: explicit_bzero(passwd, pwlen);
1.173 djm 516: free(passwd);
1.194 markus 517: send_status(e, success);
1.88 markus 518: }
519:
520: static void
1.221 djm 521: no_identities(SocketEntry *e)
1.88 markus 522: {
1.194 markus 523: struct sshbuf *msg;
524: int r;
1.88 markus 525:
1.194 markus 526: if ((msg = sshbuf_new()) == NULL)
527: fatal("%s: sshbuf_new failed", __func__);
1.221 djm 528: if ((r = sshbuf_put_u8(msg, SSH2_AGENT_IDENTITIES_ANSWER)) != 0 ||
1.194 markus 529: (r = sshbuf_put_u32(msg, 0)) != 0 ||
530: (r = sshbuf_put_stringb(e->output, msg)) != 0)
531: fatal("%s: buffer error: %s", __func__, ssh_err(r));
532: sshbuf_free(msg);
1.88 markus 533: }
1.59 markus 534:
1.163 markus 535: #ifdef ENABLE_PKCS11
1.59 markus 536: static void
1.158 djm 537: process_add_smartcard_key(SocketEntry *e)
1.59 markus 538: {
1.226 djm 539: char *provider = NULL, *pin = NULL, canonical_provider[PATH_MAX];
1.221 djm 540: int r, i, count = 0, success = 0, confirm = 0;
1.194 markus 541: u_int seconds;
1.174 dtucker 542: time_t death = 0;
1.194 markus 543: u_char type;
544: struct sshkey **keys = NULL, *k;
1.84 markus 545: Identity *id;
1.75 deraadt 546:
1.194 markus 547: if ((r = sshbuf_get_cstring(e->request, &provider, NULL)) != 0 ||
1.226 djm 548: (r = sshbuf_get_cstring(e->request, &pin, NULL)) != 0) {
549: error("%s: buffer error: %s", __func__, ssh_err(r));
550: goto send;
551: }
1.194 markus 552:
553: while (sshbuf_len(e->request)) {
1.226 djm 554: if ((r = sshbuf_get_u8(e->request, &type)) != 0) {
555: error("%s: buffer error: %s", __func__, ssh_err(r));
556: goto send;
557: }
1.194 markus 558: switch (type) {
1.110 djm 559: case SSH_AGENT_CONSTRAIN_LIFETIME:
1.226 djm 560: if ((r = sshbuf_get_u32(e->request, &seconds)) != 0) {
561: error("%s: buffer error: %s",
1.194 markus 562: __func__, ssh_err(r));
1.226 djm 563: goto send;
564: }
1.194 markus 565: death = monotime() + seconds;
1.110 djm 566: break;
567: case SSH_AGENT_CONSTRAIN_CONFIRM:
568: confirm = 1;
569: break;
570: default:
1.221 djm 571: error("%s: Unknown constraint type %d", __func__, type);
1.158 djm 572: goto send;
1.110 djm 573: }
574: }
1.215 djm 575: if (realpath(provider, canonical_provider) == NULL) {
576: verbose("failed PKCS#11 add of \"%.100s\": realpath: %s",
577: provider, strerror(errno));
578: goto send;
579: }
580: if (match_pattern_list(canonical_provider, pkcs11_whitelist, 0) != 1) {
581: verbose("refusing PKCS#11 add of \"%.100s\": "
582: "provider not whitelisted", canonical_provider);
583: goto send;
584: }
585: debug("%s: add %.100s", __func__, canonical_provider);
1.110 djm 586: if (lifetime && !death)
1.175 dtucker 587: death = monotime() + lifetime;
1.110 djm 588:
1.215 djm 589: count = pkcs11_add_provider(canonical_provider, pin, &keys);
1.163 markus 590: for (i = 0; i < count; i++) {
1.84 markus 591: k = keys[i];
1.221 djm 592: if (lookup_identity(k) == NULL) {
1.163 markus 593: id = xcalloc(1, sizeof(Identity));
1.84 markus 594: id->key = k;
1.215 djm 595: id->provider = xstrdup(canonical_provider);
596: id->comment = xstrdup(canonical_provider); /* XXX */
1.110 djm 597: id->death = death;
598: id->confirm = confirm;
1.221 djm 599: TAILQ_INSERT_TAIL(&idtab->idlist, id, next);
600: idtab->nentries++;
1.84 markus 601: success = 1;
602: } else {
1.194 markus 603: sshkey_free(k);
1.84 markus 604: }
605: keys[i] = NULL;
1.59 markus 606: }
607: send:
1.173 djm 608: free(pin);
609: free(provider);
610: free(keys);
1.194 markus 611: send_status(e, success);
1.59 markus 612: }
613:
614: static void
615: process_remove_smartcard_key(SocketEntry *e)
616: {
1.217 djm 617: char *provider = NULL, *pin = NULL, canonical_provider[PATH_MAX];
1.221 djm 618: int r, success = 0;
1.163 markus 619: Identity *id, *nxt;
1.59 markus 620:
1.194 markus 621: if ((r = sshbuf_get_cstring(e->request, &provider, NULL)) != 0 ||
1.226 djm 622: (r = sshbuf_get_cstring(e->request, &pin, NULL)) != 0) {
623: error("%s: buffer error: %s", __func__, ssh_err(r));
624: goto send;
625: }
1.173 djm 626: free(pin);
1.59 markus 627:
1.217 djm 628: if (realpath(provider, canonical_provider) == NULL) {
629: verbose("failed PKCS#11 add of \"%.100s\": realpath: %s",
630: provider, strerror(errno));
631: goto send;
632: }
633:
634: debug("%s: remove %.100s", __func__, canonical_provider);
1.221 djm 635: for (id = TAILQ_FIRST(&idtab->idlist); id; id = nxt) {
636: nxt = TAILQ_NEXT(id, next);
637: /* Skip file--based keys */
638: if (id->provider == NULL)
639: continue;
640: if (!strcmp(canonical_provider, id->provider)) {
641: TAILQ_REMOVE(&idtab->idlist, id, next);
642: free_identity(id);
643: idtab->nentries--;
1.59 markus 644: }
645: }
1.217 djm 646: if (pkcs11_del_provider(canonical_provider) == 0)
1.163 markus 647: success = 1;
648: else
1.221 djm 649: error("%s: pkcs11_del_provider failed", __func__);
1.218 deraadt 650: send:
1.173 djm 651: free(provider);
1.194 markus 652: send_status(e, success);
1.59 markus 653: }
1.163 markus 654: #endif /* ENABLE_PKCS11 */
1.59 markus 655:
1.33 markus 656: /* dispatch incoming messages */
657:
1.223 djm 658: static int
659: process_message(u_int socknum)
1.1 deraadt 660: {
1.194 markus 661: u_int msg_len;
662: u_char type;
663: const u_char *cp;
664: int r;
1.223 djm 665: SocketEntry *e;
666:
667: if (socknum >= sockets_alloc) {
668: fatal("%s: socket number %u >= allocated %u",
669: __func__, socknum, sockets_alloc);
670: }
671: e = &sockets[socknum];
1.89 markus 672:
1.194 markus 673: if (sshbuf_len(e->input) < 5)
1.223 djm 674: return 0; /* Incomplete message header. */
1.194 markus 675: cp = sshbuf_ptr(e->input);
676: msg_len = PEEK_U32(cp);
1.223 djm 677: if (msg_len > AGENT_MAX_LEN) {
678: debug("%s: socket %u (fd=%d) message too long %u > %u",
679: __func__, socknum, e->fd, msg_len, AGENT_MAX_LEN);
680: return -1;
1.21 markus 681: }
1.194 markus 682: if (sshbuf_len(e->input) < msg_len + 4)
1.223 djm 683: return 0; /* Incomplete message body. */
1.87 markus 684:
685: /* move the current input to e->request */
1.194 markus 686: sshbuf_reset(e->request);
687: if ((r = sshbuf_get_stringb(e->input, e->request)) != 0 ||
1.223 djm 688: (r = sshbuf_get_u8(e->request, &type)) != 0) {
689: if (r == SSH_ERR_MESSAGE_INCOMPLETE ||
690: r == SSH_ERR_STRING_TOO_LARGE) {
691: debug("%s: buffer error: %s", __func__, ssh_err(r));
692: return -1;
693: }
1.194 markus 694: fatal("%s: buffer error: %s", __func__, ssh_err(r));
1.223 djm 695: }
696:
697: debug("%s: socket %u (fd=%d) type %d", __func__, socknum, e->fd, type);
1.21 markus 698:
1.230 djm 699: /* check whether agent is locked */
1.88 markus 700: if (locked && type != SSH_AGENTC_UNLOCK) {
1.194 markus 701: sshbuf_reset(e->request);
1.88 markus 702: switch (type) {
703: case SSH2_AGENTC_REQUEST_IDENTITIES:
704: /* send empty lists */
1.221 djm 705: no_identities(e);
1.88 markus 706: break;
707: default:
708: /* send a fail message for all other request types */
1.194 markus 709: send_status(e, 0);
1.88 markus 710: }
1.223 djm 711: return 0;
1.88 markus 712: }
713:
1.21 markus 714: switch (type) {
1.88 markus 715: case SSH_AGENTC_LOCK:
716: case SSH_AGENTC_UNLOCK:
717: process_lock_agent(e, type == SSH_AGENTC_LOCK);
718: break;
1.21 markus 719: case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
1.221 djm 720: process_remove_all_identities(e); /* safe for !WITH_SSH1 */
1.33 markus 721: break;
722: /* ssh2 */
723: case SSH2_AGENTC_SIGN_REQUEST:
724: process_sign_request2(e);
725: break;
726: case SSH2_AGENTC_REQUEST_IDENTITIES:
1.221 djm 727: process_request_identities(e);
1.33 markus 728: break;
729: case SSH2_AGENTC_ADD_IDENTITY:
1.94 markus 730: case SSH2_AGENTC_ADD_ID_CONSTRAINED:
1.221 djm 731: process_add_identity(e);
1.33 markus 732: break;
733: case SSH2_AGENTC_REMOVE_IDENTITY:
1.221 djm 734: process_remove_identity(e);
1.33 markus 735: break;
736: case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
1.221 djm 737: process_remove_all_identities(e);
1.21 markus 738: break;
1.163 markus 739: #ifdef ENABLE_PKCS11
1.59 markus 740: case SSH_AGENTC_ADD_SMARTCARD_KEY:
1.110 djm 741: case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED:
1.59 markus 742: process_add_smartcard_key(e);
1.75 deraadt 743: break;
1.59 markus 744: case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
745: process_remove_smartcard_key(e);
1.75 deraadt 746: break;
1.163 markus 747: #endif /* ENABLE_PKCS11 */
1.21 markus 748: default:
749: /* Unknown message. Respond with failure. */
750: error("Unknown message %d", type);
1.194 markus 751: sshbuf_reset(e->request);
752: send_status(e, 0);
1.21 markus 753: break;
754: }
1.223 djm 755: return 0;
1.1 deraadt 756: }
757:
1.55 itojun 758: static void
1.73 stevesk 759: new_socket(sock_type type, int fd)
1.1 deraadt 760: {
1.112 markus 761: u_int i, old_alloc, new_alloc;
1.96 deraadt 762:
1.119 djm 763: set_nonblock(fd);
1.21 markus 764:
765: if (fd > max_fd)
766: max_fd = fd;
767:
768: for (i = 0; i < sockets_alloc; i++)
769: if (sockets[i].type == AUTH_UNUSED) {
770: sockets[i].fd = fd;
1.194 markus 771: if ((sockets[i].input = sshbuf_new()) == NULL)
772: fatal("%s: sshbuf_new failed", __func__);
773: if ((sockets[i].output = sshbuf_new()) == NULL)
774: fatal("%s: sshbuf_new failed", __func__);
775: if ((sockets[i].request = sshbuf_new()) == NULL)
776: fatal("%s: sshbuf_new failed", __func__);
1.112 markus 777: sockets[i].type = type;
1.21 markus 778: return;
779: }
780: old_alloc = sockets_alloc;
1.112 markus 781: new_alloc = sockets_alloc + 10;
1.200 deraadt 782: sockets = xreallocarray(sockets, new_alloc, sizeof(sockets[0]));
1.112 markus 783: for (i = old_alloc; i < new_alloc; i++)
1.21 markus 784: sockets[i].type = AUTH_UNUSED;
1.112 markus 785: sockets_alloc = new_alloc;
1.21 markus 786: sockets[old_alloc].fd = fd;
1.194 markus 787: if ((sockets[old_alloc].input = sshbuf_new()) == NULL)
788: fatal("%s: sshbuf_new failed", __func__);
789: if ((sockets[old_alloc].output = sshbuf_new()) == NULL)
790: fatal("%s: sshbuf_new failed", __func__);
791: if ((sockets[old_alloc].request = sshbuf_new()) == NULL)
792: fatal("%s: sshbuf_new failed", __func__);
1.112 markus 793: sockets[old_alloc].type = type;
1.1 deraadt 794: }
795:
1.55 itojun 796: static int
1.223 djm 797: handle_socket_read(u_int socknum)
1.1 deraadt 798: {
1.223 djm 799: struct sockaddr_un sunaddr;
800: socklen_t slen;
801: uid_t euid;
802: gid_t egid;
803: int fd;
804:
805: slen = sizeof(sunaddr);
806: fd = accept(sockets[socknum].fd, (struct sockaddr *)&sunaddr, &slen);
807: if (fd < 0) {
808: error("accept from AUTH_SOCKET: %s", strerror(errno));
809: return -1;
810: }
811: if (getpeereid(fd, &euid, &egid) < 0) {
812: error("getpeereid %d failed: %s", fd, strerror(errno));
813: close(fd);
814: return -1;
815: }
816: if ((euid != 0) && (getuid() != euid)) {
817: error("uid mismatch: peer euid %u != uid %u",
818: (u_int) euid, (u_int) getuid());
819: close(fd);
820: return -1;
821: }
822: new_socket(AUTH_CONNECTION, fd);
823: return 0;
824: }
825:
826: static int
827: handle_conn_read(u_int socknum)
828: {
1.233 ! djm 829: char buf[AGENT_RBUF_LEN];
1.223 djm 830: ssize_t len;
831: int r;
832:
833: if ((len = read(sockets[socknum].fd, buf, sizeof(buf))) <= 0) {
834: if (len == -1) {
835: if (errno == EAGAIN || errno == EINTR)
836: return 0;
837: error("%s: read error on socket %u (fd %d): %s",
838: __func__, socknum, sockets[socknum].fd,
839: strerror(errno));
840: }
841: return -1;
842: }
843: if ((r = sshbuf_put(sockets[socknum].input, buf, len)) != 0)
844: fatal("%s: buffer error: %s", __func__, ssh_err(r));
845: explicit_bzero(buf, sizeof(buf));
846: process_message(socknum);
847: return 0;
848: }
849:
850: static int
851: handle_conn_write(u_int socknum)
852: {
853: ssize_t len;
854: int r;
855:
856: if (sshbuf_len(sockets[socknum].output) == 0)
857: return 0; /* shouldn't happen */
858: if ((len = write(sockets[socknum].fd,
859: sshbuf_ptr(sockets[socknum].output),
860: sshbuf_len(sockets[socknum].output))) <= 0) {
861: if (len == -1) {
862: if (errno == EAGAIN || errno == EINTR)
863: return 0;
864: error("%s: read error on socket %u (fd %d): %s",
865: __func__, socknum, sockets[socknum].fd,
866: strerror(errno));
867: }
868: return -1;
869: }
870: if ((r = sshbuf_consume(sockets[socknum].output, len)) != 0)
871: fatal("%s: buffer error: %s", __func__, ssh_err(r));
872: return 0;
873: }
874:
875: static void
1.231 djm 876: after_poll(struct pollfd *pfd, size_t npfd, u_int maxfds)
1.223 djm 877: {
878: size_t i;
1.231 djm 879: u_int socknum, activefds = npfd;
1.223 djm 880:
881: for (i = 0; i < npfd; i++) {
882: if (pfd[i].revents == 0)
883: continue;
884: /* Find sockets entry */
885: for (socknum = 0; socknum < sockets_alloc; socknum++) {
886: if (sockets[socknum].type != AUTH_SOCKET &&
887: sockets[socknum].type != AUTH_CONNECTION)
888: continue;
889: if (pfd[i].fd == sockets[socknum].fd)
890: break;
891: }
892: if (socknum >= sockets_alloc) {
893: error("%s: no socket for fd %d", __func__, pfd[i].fd);
894: continue;
895: }
896: /* Process events */
897: switch (sockets[socknum].type) {
898: case AUTH_SOCKET:
1.231 djm 899: if ((pfd[i].revents & (POLLIN|POLLERR)) == 0)
900: break;
901: if (npfd > maxfds) {
902: debug3("out of fds (active %u >= limit %u); "
903: "skipping accept", activefds, maxfds);
904: break;
905: }
906: if (handle_socket_read(socknum) == 0)
907: activefds++;
1.223 djm 908: break;
909: case AUTH_CONNECTION:
910: if ((pfd[i].revents & (POLLIN|POLLERR)) != 0 &&
911: handle_conn_read(socknum) != 0) {
1.231 djm 912: goto close_sock;
913: }
914: if ((pfd[i].revents & (POLLOUT|POLLHUP)) != 0 &&
915: handle_conn_write(socknum) != 0) {
916: close_sock:
917: if (activefds == 0)
918: fatal("activefds == 0 at close_sock");
1.223 djm 919: close_socket(&sockets[socknum]);
1.231 djm 920: activefds--;
1.223 djm 921: break;
922: }
923: break;
924: default:
925: break;
926: }
927: }
928: }
929:
930: static int
1.231 djm 931: prepare_poll(struct pollfd **pfdp, size_t *npfdp, int *timeoutp, u_int maxfds)
1.223 djm 932: {
933: struct pollfd *pfd = *pfdp;
934: size_t i, j, npfd = 0;
1.174 dtucker 935: time_t deadline;
1.233 ! djm 936: int r;
1.46 markus 937:
1.223 djm 938: /* Count active sockets */
1.46 markus 939: for (i = 0; i < sockets_alloc; i++) {
1.21 markus 940: switch (sockets[i].type) {
941: case AUTH_SOCKET:
942: case AUTH_CONNECTION:
1.223 djm 943: npfd++;
1.21 markus 944: break;
945: case AUTH_UNUSED:
946: break;
947: default:
948: fatal("Unknown socket type %d", sockets[i].type);
949: break;
950: }
1.46 markus 951: }
1.223 djm 952: if (npfd != *npfdp &&
953: (pfd = recallocarray(pfd, *npfdp, npfd, sizeof(*pfd))) == NULL)
954: fatal("%s: recallocarray failed", __func__);
955: *pfdp = pfd;
956: *npfdp = npfd;
1.46 markus 957:
1.223 djm 958: for (i = j = 0; i < sockets_alloc; i++) {
1.46 markus 959: switch (sockets[i].type) {
960: case AUTH_SOCKET:
1.231 djm 961: if (npfd > maxfds) {
962: debug3("out of fds (active %zu >= limit %u); "
963: "skipping arming listener", npfd, maxfds);
964: break;
965: }
966: pfd[j].fd = sockets[i].fd;
967: pfd[j].revents = 0;
968: pfd[j].events = POLLIN;
969: j++;
970: break;
1.46 markus 971: case AUTH_CONNECTION:
1.223 djm 972: pfd[j].fd = sockets[i].fd;
973: pfd[j].revents = 0;
1.233 ! djm 974: /*
! 975: * Only prepare to read if we can handle a full-size
! 976: * input read buffer and enqueue a max size reply..
! 977: */
! 978: if ((r = sshbuf_check_reserve(sockets[i].input,
! 979: AGENT_RBUF_LEN)) == 0 &&
! 980: (r = sshbuf_check_reserve(sockets[i].output,
! 981: AGENT_MAX_LEN)) == 0)
! 982: pfd[j].events = POLLIN;
! 983: else if (r != SSH_ERR_NO_BUFFER_SPACE) {
! 984: fatal("%s: buffer error: %s",
! 985: __func__, ssh_err(r));
! 986: }
1.194 markus 987: if (sshbuf_len(sockets[i].output) > 0)
1.223 djm 988: pfd[j].events |= POLLOUT;
989: j++;
1.46 markus 990: break;
991: default:
992: break;
993: }
994: }
1.155 dtucker 995: deadline = reaper();
996: if (parent_alive_interval != 0)
997: deadline = (deadline == 0) ? parent_alive_interval :
1.214 deraadt 998: MINIMUM(deadline, parent_alive_interval);
1.155 dtucker 999: if (deadline == 0) {
1.224 djm 1000: *timeoutp = -1; /* INFTIM */
1.155 dtucker 1001: } else {
1.223 djm 1002: if (deadline > INT_MAX / 1000)
1003: *timeoutp = INT_MAX / 1000;
1004: else
1005: *timeoutp = deadline * 1000;
1.155 dtucker 1006: }
1.46 markus 1007: return (1);
1.21 markus 1008: }
1009:
1.55 itojun 1010: static void
1.113 markus 1011: cleanup_socket(void)
1.15 markus 1012: {
1.187 djm 1013: if (cleanup_pid != 0 && getpid() != cleanup_pid)
1014: return;
1015: debug("%s: cleanup", __func__);
1.48 deraadt 1016: if (socket_name[0])
1017: unlink(socket_name);
1018: if (socket_dir[0])
1019: rmdir(socket_dir);
1.10 markus 1020: }
1021:
1.114 markus 1022: void
1.15 markus 1023: cleanup_exit(int i)
1024: {
1.113 markus 1025: cleanup_socket();
1026: _exit(i);
1.15 markus 1027: }
1028:
1.135 deraadt 1029: /*ARGSUSED*/
1.55 itojun 1030: static void
1.48 deraadt 1031: cleanup_handler(int sig)
1032: {
1.113 markus 1033: cleanup_socket();
1.163 markus 1034: #ifdef ENABLE_PKCS11
1035: pkcs11_terminate();
1036: #endif
1.48 deraadt 1037: _exit(2);
1.113 markus 1038: }
1039:
1.68 markus 1040: static void
1.155 dtucker 1041: check_parent_exists(void)
1.68 markus 1042: {
1.172 dtucker 1043: /*
1044: * If our parent has exited then getppid() will return (pid_t)1,
1045: * so testing for that should be safe.
1046: */
1047: if (parent_pid != -1 && getppid() != parent_pid) {
1.68 markus 1048: /* printf("Parent has died - Authentication agent exiting.\n"); */
1.155 dtucker 1049: cleanup_socket();
1050: _exit(2);
1.68 markus 1051: }
1.48 deraadt 1052: }
1053:
1.55 itojun 1054: static void
1.50 itojun 1055: usage(void)
1.15 markus 1056: {
1.184 deraadt 1057: fprintf(stderr,
1.202 jmc 1058: "usage: ssh-agent [-c | -s] [-Dd] [-a bind_address] [-E fingerprint_hash]\n"
1.215 djm 1059: " [-P pkcs11_whitelist] [-t life] [command [arg ...]]\n"
1.184 deraadt 1060: " ssh-agent [-c | -s] -k\n");
1.21 markus 1061: exit(1);
1.15 markus 1062: }
1063:
1.2 provos 1064: int
1065: main(int ac, char **av)
1.1 deraadt 1066: {
1.201 djm 1067: int c_flag = 0, d_flag = 0, D_flag = 0, k_flag = 0, s_flag = 0;
1.154 dtucker 1068: int sock, fd, ch, result, saved_errno;
1.96 deraadt 1069: char *shell, *format, *pidstr, *agentsocket = NULL;
1.41 markus 1070: struct rlimit rlim;
1.96 deraadt 1071: extern int optind;
1.98 stevesk 1072: extern char *optarg;
1.21 markus 1073: pid_t pid;
1.96 deraadt 1074: char pidstrbuf[1 + 3 * sizeof pid];
1.161 tobias 1075: size_t len;
1.189 djm 1076: mode_t prev_mask;
1.224 djm 1077: int timeout = -1; /* INFTIM */
1.223 djm 1078: struct pollfd *pfd = NULL;
1079: size_t npfd = 0;
1.231 djm 1080: u_int maxfds;
1.123 djm 1081:
1.212 dtucker 1082: ssh_malloc_init(); /* must be called before any mallocs */
1.123 djm 1083: /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1084: sanitise_stdfd();
1.99 markus 1085:
1086: /* drop */
1087: setegid(getgid());
1088: setgid(getgid());
1.53 markus 1089:
1.231 djm 1090: if (getrlimit(RLIMIT_NOFILE, &rlim) == -1)
1091: fatal("%s: getrlimit: %s", __progname, strerror(errno));
1092:
1.185 markus 1093: #ifdef WITH_OPENSSL
1.170 djm 1094: OpenSSL_add_all_algorithms();
1.185 markus 1095: #endif
1.21 markus 1096:
1.215 djm 1097: while ((ch = getopt(ac, av, "cDdksE:a:P:t:")) != -1) {
1.21 markus 1098: switch (ch) {
1.192 djm 1099: case 'E':
1100: fingerprint_hash = ssh_digest_alg_by_name(optarg);
1101: if (fingerprint_hash == -1)
1102: fatal("Invalid hash algorithm \"%s\"", optarg);
1103: break;
1.21 markus 1104: case 'c':
1105: if (s_flag)
1106: usage();
1107: c_flag++;
1108: break;
1109: case 'k':
1110: k_flag++;
1111: break;
1.215 djm 1112: case 'P':
1113: if (pkcs11_whitelist != NULL)
1114: fatal("-P option already specified");
1115: pkcs11_whitelist = xstrdup(optarg);
1116: break;
1.21 markus 1117: case 's':
1118: if (c_flag)
1119: usage();
1120: s_flag++;
1121: break;
1.57 markus 1122: case 'd':
1.201 djm 1123: if (d_flag || D_flag)
1.57 markus 1124: usage();
1125: d_flag++;
1126: break;
1.201 djm 1127: case 'D':
1128: if (d_flag || D_flag)
1129: usage();
1130: D_flag++;
1131: break;
1.86 markus 1132: case 'a':
1133: agentsocket = optarg;
1.106 marc 1134: break;
1135: case 't':
1136: if ((lifetime = convtime(optarg)) == -1) {
1137: fprintf(stderr, "Invalid lifetime\n");
1138: usage();
1139: }
1.86 markus 1140: break;
1.21 markus 1141: default:
1142: usage();
1143: }
1144: }
1145: ac -= optind;
1146: av += optind;
1147:
1.201 djm 1148: if (ac > 0 && (c_flag || k_flag || s_flag || d_flag || D_flag))
1.21 markus 1149: usage();
1150:
1.215 djm 1151: if (pkcs11_whitelist == NULL)
1152: pkcs11_whitelist = xstrdup(DEFAULT_PKCS11_WHITELIST);
1153:
1.85 markus 1154: if (ac == 0 && !c_flag && !s_flag) {
1.21 markus 1155: shell = getenv("SHELL");
1.161 tobias 1156: if (shell != NULL && (len = strlen(shell)) > 2 &&
1157: strncmp(shell + len - 3, "csh", 3) == 0)
1.21 markus 1158: c_flag = 1;
1159: }
1160: if (k_flag) {
1.136 deraadt 1161: const char *errstr = NULL;
1162:
1.21 markus 1163: pidstr = getenv(SSH_AGENTPID_ENV_NAME);
1164: if (pidstr == NULL) {
1165: fprintf(stderr, "%s not set, cannot kill agent\n",
1.46 markus 1166: SSH_AGENTPID_ENV_NAME);
1.21 markus 1167: exit(1);
1168: }
1.136 deraadt 1169: pid = (int)strtonum(pidstr, 2, INT_MAX, &errstr);
1170: if (errstr) {
1171: fprintf(stderr,
1172: "%s=\"%s\", which is not a good PID: %s\n",
1173: SSH_AGENTPID_ENV_NAME, pidstr, errstr);
1.21 markus 1174: exit(1);
1175: }
1176: if (kill(pid, SIGTERM) == -1) {
1177: perror("kill");
1178: exit(1);
1179: }
1180: format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1.140 djm 1181: printf(format, SSH_AUTHSOCKET_ENV_NAME);
1182: printf(format, SSH_AGENTPID_ENV_NAME);
1.91 mpech 1183: printf("echo Agent pid %ld killed;\n", (long)pid);
1.21 markus 1184: exit(0);
1185: }
1.231 djm 1186:
1187: /*
1188: * Minimum file descriptors:
1189: * stdio (3) + listener (1) + syslog (1 maybe) + connection (1) +
1190: * a few spare for libc / stack protectors / sanitisers, etc.
1191: */
1192: #define SSH_AGENT_MIN_FDS (3+1+1+1+4)
1193: if (rlim.rlim_cur < SSH_AGENT_MIN_FDS)
1.232 djm 1194: fatal("%s: file descriptor rlimit %lld too low (minimum %u)",
1.231 djm 1195: __progname, (long long)rlim.rlim_cur, SSH_AGENT_MIN_FDS);
1196: maxfds = rlim.rlim_cur - SSH_AGENT_MIN_FDS;
1197:
1.21 markus 1198: parent_pid = getpid();
1199:
1.86 markus 1200: if (agentsocket == NULL) {
1201: /* Create private directory for agent socket */
1.171 djm 1202: mktemp_proto(socket_dir, sizeof(socket_dir));
1.86 markus 1203: if (mkdtemp(socket_dir) == NULL) {
1204: perror("mkdtemp: private socket dir");
1205: exit(1);
1206: }
1.91 mpech 1207: snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1208: (long)parent_pid);
1.86 markus 1209: } else {
1210: /* Try to use specified agent socket */
1211: socket_dir[0] = '\0';
1212: strlcpy(socket_name, agentsocket, sizeof socket_name);
1.21 markus 1213: }
1214:
1.23 markus 1215: /*
1216: * Create socket early so it will exist before command gets run from
1217: * the parent.
1218: */
1.189 djm 1219: prev_mask = umask(0177);
1.188 millert 1220: sock = unix_listener(socket_name, SSH_LISTEN_BACKLOG, 0);
1.21 markus 1221: if (sock < 0) {
1.188 millert 1222: /* XXX - unix_listener() calls error() not perror() */
1.121 djm 1223: *socket_name = '\0'; /* Don't unlink any existing file */
1.21 markus 1224: cleanup_exit(1);
1225: }
1.189 djm 1226: umask(prev_mask);
1.46 markus 1227:
1.23 markus 1228: /*
1229: * Fork, and have the parent execute the command, if any, or present
1230: * the socket data. The child continues as the authentication agent.
1231: */
1.201 djm 1232: if (D_flag || d_flag) {
1233: log_init(__progname,
1234: d_flag ? SYSLOG_LEVEL_DEBUG3 : SYSLOG_LEVEL_INFO,
1235: SYSLOG_FACILITY_AUTH, 1);
1.57 markus 1236: format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1237: printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1238: SSH_AUTHSOCKET_ENV_NAME);
1.91 mpech 1239: printf("echo Agent pid %ld;\n", (long)parent_pid);
1.210 dtucker 1240: fflush(stdout);
1.57 markus 1241: goto skip;
1242: }
1.21 markus 1243: pid = fork();
1244: if (pid == -1) {
1245: perror("fork");
1.81 stevesk 1246: cleanup_exit(1);
1.21 markus 1247: }
1248: if (pid != 0) { /* Parent - execute the given command. */
1249: close(sock);
1.91 mpech 1250: snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
1.21 markus 1251: if (ac == 0) {
1252: format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1253: printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1.46 markus 1254: SSH_AUTHSOCKET_ENV_NAME);
1.21 markus 1255: printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
1.46 markus 1256: SSH_AGENTPID_ENV_NAME);
1.91 mpech 1257: printf("echo Agent pid %ld;\n", (long)pid);
1.21 markus 1258: exit(0);
1259: }
1.36 deraadt 1260: if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1261: setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1262: perror("setenv");
1263: exit(1);
1264: }
1.21 markus 1265: execvp(av[0], av);
1266: perror(av[0]);
1267: exit(1);
1268: }
1.81 stevesk 1269: /* child */
1270: log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
1.67 stevesk 1271:
1272: if (setsid() == -1) {
1.81 stevesk 1273: error("setsid: %s", strerror(errno));
1.67 stevesk 1274: cleanup_exit(1);
1275: }
1276:
1277: (void)chdir("/");
1.107 markus 1278: if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1279: /* XXX might close listen socket */
1280: (void)dup2(fd, STDIN_FILENO);
1281: (void)dup2(fd, STDOUT_FILENO);
1282: (void)dup2(fd, STDERR_FILENO);
1283: if (fd > 2)
1284: close(fd);
1285: }
1.21 markus 1286:
1.41 markus 1287: /* deny core dumps, since memory contains unencrypted private keys */
1288: rlim.rlim_cur = rlim.rlim_max = 0;
1289: if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
1.81 stevesk 1290: error("setrlimit RLIMIT_CORE: %s", strerror(errno));
1.21 markus 1291: cleanup_exit(1);
1292: }
1.57 markus 1293:
1294: skip:
1.187 djm 1295:
1296: cleanup_pid = getpid();
1.163 markus 1297:
1298: #ifdef ENABLE_PKCS11
1299: pkcs11_init(0);
1300: #endif
1.21 markus 1301: new_socket(AUTH_SOCKET, sock);
1.155 dtucker 1302: if (ac > 0)
1303: parent_alive_interval = 10;
1.33 markus 1304: idtab_init();
1.61 markus 1305: signal(SIGPIPE, SIG_IGN);
1.201 djm 1306: signal(SIGINT, (d_flag | D_flag) ? cleanup_handler : SIG_IGN);
1.48 deraadt 1307: signal(SIGHUP, cleanup_handler);
1308: signal(SIGTERM, cleanup_handler);
1.205 djm 1309:
1.215 djm 1310: if (pledge("stdio rpath cpath unix id proc exec", NULL) == -1)
1.205 djm 1311: fatal("%s: pledge: %s", __progname, strerror(errno));
1.66 markus 1312:
1.21 markus 1313: while (1) {
1.231 djm 1314: prepare_poll(&pfd, &npfd, &timeout, maxfds);
1.223 djm 1315: result = poll(pfd, npfd, timeout);
1.154 dtucker 1316: saved_errno = errno;
1.155 dtucker 1317: if (parent_alive_interval != 0)
1318: check_parent_exists();
1319: (void) reaper(); /* remove expired keys */
1.154 dtucker 1320: if (result < 0) {
1321: if (saved_errno == EINTR)
1.21 markus 1322: continue;
1.223 djm 1323: fatal("poll: %s", strerror(saved_errno));
1.154 dtucker 1324: } else if (result > 0)
1.231 djm 1325: after_poll(pfd, npfd, maxfds);
1.15 markus 1326: }
1.21 markus 1327: /* NOTREACHED */
1.1 deraadt 1328: }