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