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