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