Annotation of src/usr.bin/ssh/ssh-agent.c, Revision 1.301
1.301 ! djm 1: /* $OpenBSD: ssh-agent.c,v 1.300 2023/07/19 13:56:33 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.238 djm 44: #include <sys/wait.h>
1.126 stevesk 45:
1.185 markus 46: #ifdef WITH_OPENSSL
1.146 stevesk 47: #include <openssl/evp.h>
1.185 markus 48: #endif
1.146 stevesk 49:
1.143 stevesk 50: #include <errno.h>
1.142 stevesk 51: #include <fcntl.h>
1.126 stevesk 52: #include <paths.h>
1.223 djm 53: #include <poll.h>
1.129 stevesk 54: #include <signal.h>
1.149 stevesk 55: #include <stdlib.h>
1.150 stevesk 56: #include <stdio.h>
1.146 stevesk 57: #include <string.h>
1.248 naddy 58: #include <stdarg.h>
1.196 deraadt 59: #include <limits.h>
1.145 stevesk 60: #include <time.h>
1.144 stevesk 61: #include <unistd.h>
1.203 dtucker 62: #include <util.h>
1.194 markus 63:
1.151 deraadt 64: #include "xmalloc.h"
1.1 deraadt 65: #include "ssh.h"
1.258 djm 66: #include "ssh2.h"
1.194 markus 67: #include "sshbuf.h"
68: #include "sshkey.h"
1.32 markus 69: #include "authfd.h"
1.47 markus 70: #include "log.h"
1.107 markus 71: #include "misc.h"
1.182 markus 72: #include "digest.h"
1.194 markus 73: #include "ssherr.h"
1.215 djm 74: #include "match.h"
1.238 djm 75: #include "msg.h"
76: #include "pathnames.h"
1.163 markus 77: #include "ssh-pkcs11.h"
1.251 djm 78: #include "sk-api.h"
1.281 djm 79: #include "myproposal.h"
1.59 markus 80:
1.262 djm 81: #ifndef DEFAULT_ALLOWED_PROVIDERS
82: # define DEFAULT_ALLOWED_PROVIDERS "/usr/lib*/*,/usr/local/lib*/*"
1.215 djm 83: #endif
84:
1.223 djm 85: /* Maximum accepted message length */
1.280 djm 86: #define AGENT_MAX_LEN (256*1024)
1.233 djm 87: /* Maximum bytes to read from client socket */
1.280 djm 88: #define AGENT_RBUF_LEN (4096)
89: /* Maximum number of recorded session IDs/hostkeys per connection */
90: #define AGENT_MAX_SESSION_IDS 16
91: /* Maximum size of session ID */
92: #define AGENT_MAX_SID_LEN 128
1.281 djm 93: /* Maximum number of destination constraints to accept on a key */
94: #define AGENT_MAX_DEST_CONSTRAINTS 1024
1.280 djm 95:
96: /* XXX store hostkey_sid in a refcounted tree */
1.223 djm 97:
1.73 stevesk 98: typedef enum {
1.275 djm 99: AUTH_UNUSED = 0,
100: AUTH_SOCKET = 1,
101: AUTH_CONNECTION = 2,
1.73 stevesk 102: } sock_type;
103:
1.280 djm 104: struct hostkey_sid {
105: struct sshkey *key;
106: struct sshbuf *sid;
107: int forwarded;
108: };
109:
1.274 djm 110: typedef struct socket_entry {
1.21 markus 111: int fd;
1.73 stevesk 112: sock_type type;
1.194 markus 113: struct sshbuf *input;
114: struct sshbuf *output;
115: struct sshbuf *request;
1.280 djm 116: size_t nsession_ids;
117: struct hostkey_sid *session_ids;
1.1 deraadt 118: } SocketEntry;
119:
1.45 markus 120: u_int sockets_alloc = 0;
1.1 deraadt 121: SocketEntry *sockets = NULL;
122:
1.78 provos 123: typedef struct identity {
124: TAILQ_ENTRY(identity) next;
1.194 markus 125: struct sshkey *key;
1.21 markus 126: char *comment;
1.163 markus 127: char *provider;
1.174 dtucker 128: time_t death;
1.107 markus 129: u_int confirm;
1.238 djm 130: char *sk_provider;
1.281 djm 131: struct dest_constraint *dest_constraints;
132: size_t ndest_constraints;
1.1 deraadt 133: } Identity;
134:
1.221 djm 135: struct idtable {
1.33 markus 136: int nentries;
1.78 provos 137: TAILQ_HEAD(idqueue, identity) idlist;
1.221 djm 138: };
1.33 markus 139:
1.221 djm 140: /* private key table */
141: struct idtable *idtab;
1.1 deraadt 142:
143: int max_fd = 0;
144:
1.11 markus 145: /* pid of shell == parent of agent */
1.29 deraadt 146: pid_t parent_pid = -1;
1.176 dtucker 147: time_t parent_alive_interval = 0;
1.10 markus 148:
1.187 djm 149: /* pid of process for which cleanup_socket is applicable */
150: pid_t cleanup_pid = 0;
151:
1.10 markus 152: /* pathname and directory for AUTH_SOCKET */
1.196 deraadt 153: char socket_name[PATH_MAX];
154: char socket_dir[PATH_MAX];
1.10 markus 155:
1.260 djm 156: /* Pattern-list of allowed PKCS#11/Security key paths */
157: static char *allowed_providers;
1.215 djm 158:
1.300 djm 159: /*
160: * Allows PKCS11 providers or SK keys that use non-internal providers to
161: * be added over a remote connection (identified by session-bind@openssh.com).
162: */
163: static int remote_add_provider;
164:
1.88 markus 165: /* locking */
1.203 dtucker 166: #define LOCK_SIZE 32
167: #define LOCK_SALT_SIZE 16
168: #define LOCK_ROUNDS 1
1.88 markus 169: int locked = 0;
1.213 djm 170: u_char lock_pwhash[LOCK_SIZE];
171: u_char lock_salt[LOCK_SALT_SIZE];
1.88 markus 172:
1.20 markus 173: extern char *__progname;
174:
1.174 dtucker 175: /* Default lifetime in seconds (0 == forever) */
1.268 dtucker 176: static int lifetime = 0;
1.106 marc 177:
1.192 djm 178: static int fingerprint_hash = SSH_FP_HASH_DEFAULT;
179:
1.258 djm 180: /* Refuse signing of non-SSH messages for web-origin FIDO keys */
181: static int restrict_websafe = 1;
182:
1.55 itojun 183: static void
1.101 stevesk 184: close_socket(SocketEntry *e)
185: {
1.280 djm 186: size_t i;
187:
1.101 stevesk 188: close(e->fd);
1.194 markus 189: sshbuf_free(e->input);
190: sshbuf_free(e->output);
191: sshbuf_free(e->request);
1.280 djm 192: for (i = 0; i < e->nsession_ids; i++) {
193: sshkey_free(e->session_ids[i].key);
194: sshbuf_free(e->session_ids[i].sid);
195: }
196: free(e->session_ids);
1.269 djm 197: memset(e, '\0', sizeof(*e));
198: e->fd = -1;
199: e->type = AUTH_UNUSED;
1.101 stevesk 200: }
201:
202: static void
1.33 markus 203: idtab_init(void)
1.1 deraadt 204: {
1.221 djm 205: idtab = xcalloc(1, sizeof(*idtab));
206: TAILQ_INIT(&idtab->idlist);
207: idtab->nentries = 0;
1.33 markus 208: }
209:
1.89 markus 210: static void
1.281 djm 211: free_dest_constraint_hop(struct dest_constraint_hop *dch)
212: {
213: u_int i;
214:
215: if (dch == NULL)
216: return;
217: free(dch->user);
218: free(dch->hostname);
219: for (i = 0; i < dch->nkeys; i++)
220: sshkey_free(dch->keys[i]);
221: free(dch->keys);
222: free(dch->key_is_ca);
223: }
224:
225: static void
226: free_dest_constraints(struct dest_constraint *dcs, size_t ndcs)
227: {
228: size_t i;
229:
230: for (i = 0; i < ndcs; i++) {
231: free_dest_constraint_hop(&dcs[i].from);
232: free_dest_constraint_hop(&dcs[i].to);
233: }
234: free(dcs);
235: }
236:
237: static void
1.301 ! djm 238: dup_dest_constraint_hop(const struct dest_constraint_hop *dch,
! 239: struct dest_constraint_hop *out)
! 240: {
! 241: u_int i;
! 242: int r;
! 243:
! 244: out->user = dch->user == NULL ? NULL : xstrdup(dch->user);
! 245: out->hostname = dch->hostname == NULL ? NULL : xstrdup(dch->hostname);
! 246: out->is_ca = dch->is_ca;
! 247: out->nkeys = dch->nkeys;
! 248: out->keys = out->nkeys == 0 ? NULL :
! 249: xcalloc(out->nkeys, sizeof(*out->keys));
! 250: out->key_is_ca = out->nkeys == 0 ? NULL :
! 251: xcalloc(out->nkeys, sizeof(*out->key_is_ca));
! 252: for (i = 0; i < dch->nkeys; i++) {
! 253: if (dch->keys[i] != NULL &&
! 254: (r = sshkey_from_private(dch->keys[i],
! 255: &(out->keys[i]))) != 0)
! 256: fatal_fr(r, "copy key");
! 257: out->key_is_ca[i] = dch->key_is_ca[i];
! 258: }
! 259: }
! 260:
! 261: static struct dest_constraint *
! 262: dup_dest_constraints(const struct dest_constraint *dcs, size_t ndcs)
! 263: {
! 264: size_t i;
! 265: struct dest_constraint *ret;
! 266:
! 267: if (ndcs == 0)
! 268: return NULL;
! 269: ret = xcalloc(ndcs, sizeof(*ret));
! 270: for (i = 0; i < ndcs; i++) {
! 271: dup_dest_constraint_hop(&dcs[i].from, &ret[i].from);
! 272: dup_dest_constraint_hop(&dcs[i].to, &ret[i].to);
! 273: }
! 274: return ret;
! 275: }
! 276:
! 277: #ifdef DEBUG_CONSTRAINTS
! 278: static void
! 279: dump_dest_constraint_hop(const struct dest_constraint_hop *dch)
! 280: {
! 281: u_int i;
! 282: char *fp;
! 283:
! 284: debug_f("user %s hostname %s is_ca %d nkeys %u",
! 285: dch->user == NULL ? "(null)" : dch->user,
! 286: dch->hostname == NULL ? "(null)" : dch->hostname,
! 287: dch->is_ca, dch->nkeys);
! 288: for (i = 0; i < dch->nkeys; i++) {
! 289: fp = NULL;
! 290: if (dch->keys[i] != NULL &&
! 291: (fp = sshkey_fingerprint(dch->keys[i],
! 292: SSH_FP_HASH_DEFAULT, SSH_FP_DEFAULT)) == NULL)
! 293: fatal_f("fingerprint failed");
! 294: debug_f("key %u/%u: %s%s%s key_is_ca %d", i, dch->nkeys,
! 295: dch->keys[i] == NULL ? "" : sshkey_ssh_name(dch->keys[i]),
! 296: dch->keys[i] == NULL ? "" : " ",
! 297: dch->keys[i] == NULL ? "none" : fp,
! 298: dch->key_is_ca[i]);
! 299: free(fp);
! 300: }
! 301: }
! 302: #endif /* DEBUG_CONSTRAINTS */
! 303:
! 304: static void
! 305: dump_dest_constraints(const char *context,
! 306: const struct dest_constraint *dcs, size_t ndcs)
! 307: {
! 308: #ifdef DEBUG_CONSTRAINTS
! 309: size_t i;
! 310:
! 311: debug_f("%s: %zu constraints", context, ndcs);
! 312: for (i = 0; i < ndcs; i++) {
! 313: debug_f("constraint %zu / %zu: from: ", i, ndcs);
! 314: dump_dest_constraint_hop(&dcs[i].from);
! 315: debug_f("constraint %zu / %zu: to: ", i, ndcs);
! 316: dump_dest_constraint_hop(&dcs[i].to);
! 317: }
! 318: debug_f("done for %s", context);
! 319: #endif /* DEBUG_CONSTRAINTS */
! 320: }
! 321:
! 322: static void
1.89 markus 323: free_identity(Identity *id)
324: {
1.194 markus 325: sshkey_free(id->key);
1.173 djm 326: free(id->provider);
327: free(id->comment);
1.238 djm 328: free(id->sk_provider);
1.281 djm 329: free_dest_constraints(id->dest_constraints, id->ndest_constraints);
1.173 djm 330: free(id);
1.89 markus 331: }
332:
1.281 djm 333: /*
334: * Match 'key' against the key/CA list in a destination constraint hop
335: * Returns 0 on success or -1 otherwise.
336: */
337: static int
338: match_key_hop(const char *tag, const struct sshkey *key,
339: const struct dest_constraint_hop *dch)
340: {
341: const char *reason = NULL;
1.286 dtucker 342: const char *hostname = dch->hostname ? dch->hostname : "(ORIGIN)";
1.281 djm 343: u_int i;
344: char *fp;
345:
346: if (key == NULL)
347: return -1;
348: /* XXX logspam */
349: if ((fp = sshkey_fingerprint(key, SSH_FP_HASH_DEFAULT,
350: SSH_FP_DEFAULT)) == NULL)
351: fatal_f("fingerprint failed");
352: debug3_f("%s: entering hostname %s, requested key %s %s, %u keys avail",
1.286 dtucker 353: tag, hostname, sshkey_type(key), fp, dch->nkeys);
1.281 djm 354: free(fp);
355: for (i = 0; i < dch->nkeys; i++) {
356: if (dch->keys[i] == NULL)
357: return -1;
358: /* XXX logspam */
359: if ((fp = sshkey_fingerprint(dch->keys[i], SSH_FP_HASH_DEFAULT,
360: SSH_FP_DEFAULT)) == NULL)
361: fatal_f("fingerprint failed");
362: debug3_f("%s: key %u: %s%s %s", tag, i,
363: dch->key_is_ca[i] ? "CA " : "",
364: sshkey_type(dch->keys[i]), fp);
365: free(fp);
366: if (!sshkey_is_cert(key)) {
367: /* plain key */
368: if (dch->key_is_ca[i] ||
369: !sshkey_equal(key, dch->keys[i]))
370: continue;
371: return 0;
372: }
373: /* certificate */
374: if (!dch->key_is_ca[i])
375: continue;
376: if (key->cert == NULL || key->cert->signature_key == NULL)
377: return -1; /* shouldn't happen */
378: if (!sshkey_equal(key->cert->signature_key, dch->keys[i]))
379: continue;
1.286 dtucker 380: if (sshkey_cert_check_host(key, hostname, 1,
1.281 djm 381: SSH_ALLOWED_CA_SIGALGS, &reason) != 0) {
382: debug_f("cert %s / hostname %s rejected: %s",
1.286 dtucker 383: key->cert->key_id, hostname, reason);
1.281 djm 384: continue;
385: }
386: return 0;
387: }
388: return -1;
389: }
390:
391: /* Check destination constraints on an identity against the hostkey/user */
392: static int
393: permitted_by_dest_constraints(const struct sshkey *fromkey,
394: const struct sshkey *tokey, Identity *id, const char *user,
395: const char **hostnamep)
396: {
397: size_t i;
398: struct dest_constraint *d;
399:
400: if (hostnamep != NULL)
401: *hostnamep = NULL;
402: for (i = 0; i < id->ndest_constraints; i++) {
403: d = id->dest_constraints + i;
404: /* XXX remove logspam */
405: debug2_f("constraint %zu %s%s%s (%u keys) > %s%s%s (%u keys)",
406: i, d->from.user ? d->from.user : "",
407: d->from.user ? "@" : "",
408: d->from.hostname ? d->from.hostname : "(ORIGIN)",
409: d->from.nkeys,
410: d->to.user ? d->to.user : "", d->to.user ? "@" : "",
411: d->to.hostname ? d->to.hostname : "(ANY)", d->to.nkeys);
412:
413: /* Match 'from' key */
414: if (fromkey == NULL) {
415: /* We are matching the first hop */
416: if (d->from.hostname != NULL || d->from.nkeys != 0)
417: continue;
418: } else if (match_key_hop("from", fromkey, &d->from) != 0)
419: continue;
420:
421: /* Match 'to' key */
422: if (tokey != NULL && match_key_hop("to", tokey, &d->to) != 0)
423: continue;
424:
425: /* Match user if specified */
426: if (d->to.user != NULL && user != NULL &&
427: !match_pattern(user, d->to.user))
428: continue;
429:
430: /* successfully matched this constraint */
431: if (hostnamep != NULL)
432: *hostnamep = d->to.hostname;
433: debug2_f("allowed for hostname %s",
434: d->to.hostname == NULL ? "*" : d->to.hostname);
435: return 0;
436: }
437: /* no match */
438: debug2_f("%s identity \"%s\" not permitted for this destination",
439: sshkey_type(id->key), id->comment);
440: return -1;
441: }
442:
443: /*
444: * Check whether hostkeys on a SocketEntry and the optionally specified user
445: * are permitted by the destination constraints on the Identity.
446: * Returns 0 on success or -1 otherwise.
447: */
448: static int
449: identity_permitted(Identity *id, SocketEntry *e, char *user,
450: const char **forward_hostnamep, const char **last_hostnamep)
451: {
452: size_t i;
453: const char **hp;
454: struct hostkey_sid *hks;
455: const struct sshkey *fromkey = NULL;
456: const char *test_user;
457: char *fp1, *fp2;
458:
459: /* XXX remove logspam */
460: debug3_f("entering: key %s comment \"%s\", %zu socket bindings, "
461: "%zu constraints", sshkey_type(id->key), id->comment,
462: e->nsession_ids, id->ndest_constraints);
463: if (id->ndest_constraints == 0)
464: return 0; /* unconstrained */
465: if (e->nsession_ids == 0)
466: return 0; /* local use */
467: /*
468: * Walk through the hops recorded by session_id and try to find a
469: * constraint that satisfies each.
470: */
471: for (i = 0; i < e->nsession_ids; i++) {
472: hks = e->session_ids + i;
473: if (hks->key == NULL)
474: fatal_f("internal error: no bound key");
475: /* XXX remove logspam */
476: fp1 = fp2 = NULL;
477: if (fromkey != NULL &&
478: (fp1 = sshkey_fingerprint(fromkey, SSH_FP_HASH_DEFAULT,
479: SSH_FP_DEFAULT)) == NULL)
480: fatal_f("fingerprint failed");
481: if ((fp2 = sshkey_fingerprint(hks->key, SSH_FP_HASH_DEFAULT,
482: SSH_FP_DEFAULT)) == NULL)
483: fatal_f("fingerprint failed");
484: debug3_f("socketentry fd=%d, entry %zu %s, "
485: "from hostkey %s %s to user %s hostkey %s %s",
486: e->fd, i, hks->forwarded ? "FORWARD" : "AUTH",
487: fromkey ? sshkey_type(fromkey) : "(ORIGIN)",
488: fromkey ? fp1 : "", user ? user : "(ANY)",
489: sshkey_type(hks->key), fp2);
490: free(fp1);
491: free(fp2);
492: /*
493: * Record the hostnames for the initial forwarding and
494: * the final destination.
495: */
496: hp = NULL;
497: if (i == e->nsession_ids - 1)
498: hp = last_hostnamep;
499: else if (i == 0)
500: hp = forward_hostnamep;
501: /* Special handling for final recorded binding */
502: test_user = NULL;
503: if (i == e->nsession_ids - 1) {
504: /* Can only check user at final hop */
505: test_user = user;
506: /*
507: * user is only presented for signature requests.
508: * If this is the case, make sure last binding is not
509: * for a forwarding.
510: */
511: if (hks->forwarded && user != NULL) {
512: error_f("tried to sign on forwarding hop");
513: return -1;
514: }
515: } else if (!hks->forwarded) {
516: error_f("tried to forward though signing bind");
517: return -1;
518: }
519: if (permitted_by_dest_constraints(fromkey, hks->key, id,
520: test_user, hp) != 0)
521: return -1;
522: fromkey = hks->key;
523: }
524: /*
525: * Another special case: if the last bound session ID was for a
526: * forwarding, and this function is not being called to check a sign
527: * request (i.e. no 'user' supplied), then only permit the key if
528: * there is a permission that would allow it to be used at another
529: * destination. This hides keys that are allowed to be used to
1.284 jsg 530: * authenticate *to* a host but not permitted for *use* beyond it.
1.281 djm 531: */
532: hks = &e->session_ids[e->nsession_ids - 1];
533: if (hks->forwarded && user == NULL &&
534: permitted_by_dest_constraints(hks->key, NULL, id,
535: NULL, NULL) != 0) {
536: debug3_f("key permitted at host but not after");
537: return -1;
538: }
539:
540: /* success */
541: return 0;
542: }
543:
1.33 markus 544: /* return matching private key for given public key */
1.78 provos 545: static Identity *
1.221 djm 546: lookup_identity(struct sshkey *key)
1.33 markus 547: {
1.78 provos 548: Identity *id;
549:
1.221 djm 550: TAILQ_FOREACH(id, &idtab->idlist, next) {
1.194 markus 551: if (sshkey_equal(key, id->key))
1.78 provos 552: return (id);
1.33 markus 553: }
1.78 provos 554: return (NULL);
555: }
556:
1.107 markus 557: /* Check confirmation of keysign request */
558: static int
1.270 djm 559: confirm_key(Identity *id, const char *extra)
1.107 markus 560: {
1.122 djm 561: char *p;
1.107 markus 562: int ret = -1;
563:
1.194 markus 564: p = sshkey_fingerprint(id->key, fingerprint_hash, SSH_FP_DEFAULT);
1.197 djm 565: if (p != NULL &&
1.270 djm 566: ask_permission("Allow use of key %s?\nKey fingerprint %s.%s%s",
567: id->comment, p,
568: extra == NULL ? "" : "\n", extra == NULL ? "" : extra))
1.122 djm 569: ret = 0;
1.173 djm 570: free(p);
1.122 djm 571:
1.107 markus 572: return (ret);
573: }
574:
1.194 markus 575: static void
576: send_status(SocketEntry *e, int success)
577: {
578: int r;
579:
580: if ((r = sshbuf_put_u32(e->output, 1)) != 0 ||
581: (r = sshbuf_put_u8(e->output, success ?
582: SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE)) != 0)
1.266 djm 583: fatal_fr(r, "compose");
1.194 markus 584: }
585:
1.33 markus 586: /* send list of supported public keys to 'client' */
1.55 itojun 587: static void
1.221 djm 588: process_request_identities(SocketEntry *e)
1.33 markus 589: {
1.96 deraadt 590: Identity *id;
1.281 djm 591: struct sshbuf *msg, *keys;
1.194 markus 592: int r;
1.301 ! djm 593: u_int i = 0, nentries = 0;
! 594: char *fp;
1.1 deraadt 595:
1.269 djm 596: debug2_f("entering");
597:
1.281 djm 598: if ((msg = sshbuf_new()) == NULL || (keys = sshbuf_new()) == NULL)
1.266 djm 599: fatal_f("sshbuf_new failed");
1.221 djm 600: TAILQ_FOREACH(id, &idtab->idlist, next) {
1.301 ! djm 601: if ((fp = sshkey_fingerprint(id->key, SSH_FP_HASH_DEFAULT,
! 602: SSH_FP_DEFAULT)) == NULL)
! 603: fatal_f("fingerprint failed");
! 604: debug_f("key %u / %u: %s %s", i++, idtab->nentries,
! 605: sshkey_ssh_name(id->key), fp);
! 606: dump_dest_constraints(__func__,
! 607: id->dest_constraints, id->ndest_constraints);
! 608: free(fp);
1.281 djm 609: /* identity not visible, don't include in response */
610: if (identity_permitted(id, e, NULL, NULL, NULL) != 0)
611: continue;
612: if ((r = sshkey_puts_opts(id->key, keys,
1.278 djm 613: SSHKEY_SERIALIZE_INFO)) != 0 ||
1.281 djm 614: (r = sshbuf_put_cstring(keys, id->comment)) != 0) {
1.266 djm 615: error_fr(r, "compose key/comment");
1.220 djm 616: continue;
1.33 markus 617: }
1.281 djm 618: nentries++;
1.21 markus 619: }
1.281 djm 620: debug2_f("replying with %u allowed of %u available keys",
621: nentries, idtab->nentries);
622: if ((r = sshbuf_put_u8(msg, SSH2_AGENT_IDENTITIES_ANSWER)) != 0 ||
623: (r = sshbuf_put_u32(msg, nentries)) != 0 ||
624: (r = sshbuf_putb(msg, keys)) != 0)
625: fatal_fr(r, "compose");
1.194 markus 626: if ((r = sshbuf_put_stringb(e->output, msg)) != 0)
1.266 djm 627: fatal_fr(r, "enqueue");
1.194 markus 628: sshbuf_free(msg);
1.281 djm 629: sshbuf_free(keys);
1.1 deraadt 630: }
631:
1.33 markus 632:
1.208 markus 633: static char *
634: agent_decode_alg(struct sshkey *key, u_int flags)
635: {
636: if (key->type == KEY_RSA) {
637: if (flags & SSH_AGENT_RSA_SHA2_256)
638: return "rsa-sha2-256";
639: else if (flags & SSH_AGENT_RSA_SHA2_512)
640: return "rsa-sha2-512";
1.235 djm 641: } else if (key->type == KEY_RSA_CERT) {
642: if (flags & SSH_AGENT_RSA_SHA2_256)
643: return "rsa-sha2-256-cert-v01@openssh.com";
644: else if (flags & SSH_AGENT_RSA_SHA2_512)
645: return "rsa-sha2-512-cert-v01@openssh.com";
1.208 markus 646: }
647: return NULL;
648: }
649:
1.258 djm 650: /*
1.270 djm 651: * Attempt to parse the contents of a buffer as a SSH publickey userauth
652: * request, checking its contents for consistency and matching the embedded
653: * key against the one that is being used for signing.
654: * Note: does not modify msg buffer.
1.282 djm 655: * Optionally extract the username, session ID and/or hostkey from the request.
1.258 djm 656: */
657: static int
1.270 djm 658: parse_userauth_request(struct sshbuf *msg, const struct sshkey *expected_key,
1.282 djm 659: char **userp, struct sshbuf **sess_idp, struct sshkey **hostkeyp)
1.258 djm 660: {
1.270 djm 661: struct sshbuf *b = NULL, *sess_id = NULL;
662: char *user = NULL, *service = NULL, *method = NULL, *pkalg = NULL;
1.258 djm 663: int r;
1.270 djm 664: u_char t, sig_follows;
1.282 djm 665: struct sshkey *mkey = NULL, *hostkey = NULL;
1.258 djm 666:
1.270 djm 667: if (userp != NULL)
668: *userp = NULL;
669: if (sess_idp != NULL)
670: *sess_idp = NULL;
1.282 djm 671: if (hostkeyp != NULL)
672: *hostkeyp = NULL;
1.270 djm 673: if ((b = sshbuf_fromb(msg)) == NULL)
674: fatal_f("sshbuf_fromb");
1.258 djm 675:
676: /* SSH userauth request */
1.270 djm 677: if ((r = sshbuf_froms(b, &sess_id)) != 0)
678: goto out;
679: if (sshbuf_len(sess_id) == 0) {
680: r = SSH_ERR_INVALID_FORMAT;
681: goto out;
682: }
683: if ((r = sshbuf_get_u8(b, &t)) != 0 || /* SSH2_MSG_USERAUTH_REQUEST */
684: (r = sshbuf_get_cstring(b, &user, NULL)) != 0 || /* server user */
685: (r = sshbuf_get_cstring(b, &service, NULL)) != 0 || /* service */
686: (r = sshbuf_get_cstring(b, &method, NULL)) != 0 || /* method */
687: (r = sshbuf_get_u8(b, &sig_follows)) != 0 || /* sig-follows */
688: (r = sshbuf_get_cstring(b, &pkalg, NULL)) != 0 || /* alg */
689: (r = sshkey_froms(b, &mkey)) != 0) /* key */
690: goto out;
691: if (t != SSH2_MSG_USERAUTH_REQUEST ||
692: sig_follows != 1 ||
693: strcmp(service, "ssh-connection") != 0 ||
694: !sshkey_equal(expected_key, mkey) ||
695: sshkey_type_from_name(pkalg) != expected_key->type) {
696: r = SSH_ERR_INVALID_FORMAT;
697: goto out;
698: }
1.282 djm 699: if (strcmp(method, "publickey-hostbound-v00@openssh.com") == 0) {
700: if ((r = sshkey_froms(b, &hostkey)) != 0)
701: goto out;
702: } else if (strcmp(method, "publickey") != 0) {
1.270 djm 703: r = SSH_ERR_INVALID_FORMAT;
704: goto out;
705: }
706: if (sshbuf_len(b) != 0) {
707: r = SSH_ERR_INVALID_FORMAT;
708: goto out;
709: }
710: /* success */
711: r = 0;
712: debug3_f("well formed userauth");
713: if (userp != NULL) {
714: *userp = user;
715: user = NULL;
716: }
717: if (sess_idp != NULL) {
718: *sess_idp = sess_id;
719: sess_id = NULL;
1.258 djm 720: }
1.282 djm 721: if (hostkeyp != NULL) {
722: *hostkeyp = hostkey;
723: hostkey = NULL;
724: }
1.270 djm 725: out:
726: sshbuf_free(b);
727: sshbuf_free(sess_id);
728: free(user);
729: free(service);
730: free(method);
731: free(pkalg);
1.258 djm 732: sshkey_free(mkey);
1.282 djm 733: sshkey_free(hostkey);
1.270 djm 734: return r;
735: }
736:
737: /*
738: * Attempt to parse the contents of a buffer as a SSHSIG signature request.
739: * Note: does not modify buffer.
740: */
741: static int
742: parse_sshsig_request(struct sshbuf *msg)
743: {
744: int r;
745: struct sshbuf *b;
746:
747: if ((b = sshbuf_fromb(msg)) == NULL)
748: fatal_f("sshbuf_fromb");
749:
750: if ((r = sshbuf_cmp(b, 0, "SSHSIG", 6)) != 0 ||
751: (r = sshbuf_consume(b, 6)) != 0 ||
752: (r = sshbuf_get_cstring(b, NULL, NULL)) != 0 || /* namespace */
753: (r = sshbuf_get_string_direct(b, NULL, NULL)) != 0 || /* reserved */
754: (r = sshbuf_get_cstring(b, NULL, NULL)) != 0 || /* hashalg */
755: (r = sshbuf_get_string_direct(b, NULL, NULL)) != 0) /* H(msg) */
756: goto out;
757: if (sshbuf_len(b) != 0) {
758: r = SSH_ERR_INVALID_FORMAT;
759: goto out;
760: }
761: /* success */
762: r = 0;
763: out:
1.258 djm 764: sshbuf_free(b);
1.270 djm 765: return r;
766: }
767:
768: /*
769: * This function inspects a message to be signed by a FIDO key that has a
770: * web-like application string (i.e. one that does not begin with "ssh:".
771: * It checks that the message is one of those expected for SSH operations
772: * (pubkey userauth, sshsig, CA key signing) to exclude signing challenges
773: * for the web.
774: */
775: static int
776: check_websafe_message_contents(struct sshkey *key, struct sshbuf *data)
777: {
1.282 djm 778: if (parse_userauth_request(data, key, NULL, NULL, NULL) == 0) {
1.270 djm 779: debug_f("signed data matches public key userauth request");
1.258 djm 780: return 1;
781: }
1.270 djm 782: if (parse_sshsig_request(data) == 0) {
783: debug_f("signed data matches SSHSIG signature request");
1.258 djm 784: return 1;
1.270 djm 785: }
1.258 djm 786:
1.270 djm 787: /* XXX check CA signature operation */
1.258 djm 788:
789: error("web-origin key attempting to sign non-SSH message");
790: return 0;
791: }
792:
1.280 djm 793: static int
794: buf_equal(const struct sshbuf *a, const struct sshbuf *b)
795: {
796: if (sshbuf_ptr(a) == NULL || sshbuf_ptr(b) == NULL)
797: return SSH_ERR_INVALID_ARGUMENT;
798: if (sshbuf_len(a) != sshbuf_len(b))
799: return SSH_ERR_INVALID_FORMAT;
800: if (timingsafe_bcmp(sshbuf_ptr(a), sshbuf_ptr(b), sshbuf_len(a)) != 0)
801: return SSH_ERR_INVALID_FORMAT;
802: return 0;
803: }
804:
1.33 markus 805: /* ssh2 only */
1.55 itojun 806: static void
1.33 markus 807: process_sign_request2(SocketEntry *e)
808: {
1.221 djm 809: u_char *signature = NULL;
1.281 djm 810: size_t slen = 0;
1.194 markus 811: u_int compat = 0, flags;
1.287 djm 812: int r, ok = -1, retried = 0;
813: char *fp = NULL, *pin = NULL, *prompt = NULL;
814: char *user = NULL, *sig_dest = NULL;
1.281 djm 815: const char *fwd_host = NULL, *dest_host = NULL;
1.280 djm 816: struct sshbuf *msg = NULL, *data = NULL, *sid = NULL;
1.283 djm 817: struct sshkey *key = NULL, *hostkey = NULL;
1.195 djm 818: struct identity *id;
1.251 djm 819: struct notifier_ctx *notifier = NULL;
1.194 markus 820:
1.270 djm 821: debug_f("entering");
822:
1.273 dtucker 823: if ((msg = sshbuf_new()) == NULL || (data = sshbuf_new()) == NULL)
1.266 djm 824: fatal_f("sshbuf_new failed");
1.221 djm 825: if ((r = sshkey_froms(e->request, &key)) != 0 ||
1.270 djm 826: (r = sshbuf_get_stringb(e->request, data)) != 0 ||
1.225 djm 827: (r = sshbuf_get_u32(e->request, &flags)) != 0) {
1.266 djm 828: error_fr(r, "parse");
1.225 djm 829: goto send;
830: }
831:
1.221 djm 832: if ((id = lookup_identity(key)) == NULL) {
1.266 djm 833: verbose_f("%s key not found", sshkey_type(key));
1.195 djm 834: goto send;
835: }
1.281 djm 836: if ((fp = sshkey_fingerprint(key, SSH_FP_HASH_DEFAULT,
837: SSH_FP_DEFAULT)) == NULL)
838: fatal_f("fingerprint failed");
839:
840: if (id->ndest_constraints != 0) {
841: if (e->nsession_ids == 0) {
842: logit_f("refusing use of destination-constrained key "
843: "to sign on unbound connection");
844: goto send;
845: }
1.283 djm 846: if (parse_userauth_request(data, key, &user, &sid,
847: &hostkey) != 0) {
1.281 djm 848: logit_f("refusing use of destination-constrained key "
849: "to sign an unidentified signature");
850: goto send;
851: }
852: /* XXX logspam */
853: debug_f("user=%s", user);
854: if (identity_permitted(id, e, user, &fwd_host, &dest_host) != 0)
855: goto send;
856: /* XXX display fwd_host/dest_host in askpass UI */
1.280 djm 857: /*
1.281 djm 858: * Ensure that the session ID is the most recent one
859: * registered on the socket - it should have been bound by
860: * ssh immediately before userauth.
1.280 djm 861: */
1.281 djm 862: if (buf_equal(sid,
863: e->session_ids[e->nsession_ids - 1].sid) != 0) {
864: error_f("unexpected session ID (%zu listed) on "
865: "signature request for target user %s with "
866: "key %s %s", e->nsession_ids, user,
867: sshkey_type(id->key), fp);
868: goto send;
1.280 djm 869: }
1.283 djm 870: /*
871: * Ensure that the hostkey embedded in the signature matches
872: * the one most recently bound to the socket. An exception is
873: * made for the initial forwarding hop.
874: */
875: if (e->nsession_ids > 1 && hostkey == NULL) {
876: error_f("refusing use of destination-constrained key: "
877: "no hostkey recorded in signature for forwarded "
878: "connection");
879: goto send;
880: }
881: if (hostkey != NULL && !sshkey_equal(hostkey,
882: e->session_ids[e->nsession_ids - 1].key)) {
883: error_f("refusing use of destination-constrained key: "
884: "mismatch between hostkey in request and most "
885: "recently bound session");
886: goto send;
887: }
1.281 djm 888: xasprintf(&sig_dest, "public key authentication request for "
889: "user \"%s\" to listed host", user);
1.280 djm 890: }
891: if (id->confirm && confirm_key(id, sig_dest) != 0) {
1.266 djm 892: verbose_f("user refused key");
1.195 djm 893: goto send;
894: }
1.258 djm 895: if (sshkey_is_sk(id->key)) {
1.292 djm 896: if (restrict_websafe &&
897: strncmp(id->key->sk_application, "ssh:", 4) != 0 &&
1.270 djm 898: !check_websafe_message_contents(key, data)) {
1.258 djm 899: /* error already logged */
900: goto send;
901: }
1.290 djm 902: if (id->key->sk_flags & SSH_SK_USER_PRESENCE_REQD) {
1.258 djm 903: notifier = notify_start(0,
1.280 djm 904: "Confirm user presence for key %s %s%s%s",
905: sshkey_type(id->key), fp,
906: sig_dest == NULL ? "" : "\n",
907: sig_dest == NULL ? "" : sig_dest);
1.258 djm 908: }
1.251 djm 909: }
1.287 djm 910: retry_pin:
1.251 djm 911: if ((r = sshkey_sign(id->key, &signature, &slen,
1.270 djm 912: sshbuf_ptr(data), sshbuf_len(data), agent_decode_alg(key, flags),
1.287 djm 913: id->sk_provider, pin, compat)) != 0) {
914: debug_fr(r, "sshkey_sign");
915: if (pin == NULL && !retried && sshkey_is_sk(id->key) &&
916: r == SSH_ERR_KEY_WRONG_PASSPHRASE) {
1.289 djm 917: notify_complete(notifier, NULL);
918: notifier = NULL;
1.287 djm 919: /* XXX include sig_dest */
920: xasprintf(&prompt, "Enter PIN%sfor %s key %s: ",
921: (id->key->sk_flags & SSH_SK_USER_PRESENCE_REQD) ?
922: " and confirm user presence " : " ",
923: sshkey_type(id->key), fp);
924: pin = read_passphrase(prompt, RP_USE_ASKPASS);
925: retried = 1;
926: goto retry_pin;
927: }
1.266 djm 928: error_fr(r, "sshkey_sign");
1.251 djm 929: goto send;
1.33 markus 930: }
1.195 djm 931: /* Success */
932: ok = 0;
1.299 djm 933: debug_f("good signature");
1.195 djm 934: send:
1.267 djm 935: notify_complete(notifier, "User presence confirmed");
1.270 djm 936:
1.33 markus 937: if (ok == 0) {
1.194 markus 938: if ((r = sshbuf_put_u8(msg, SSH2_AGENT_SIGN_RESPONSE)) != 0 ||
939: (r = sshbuf_put_string(msg, signature, slen)) != 0)
1.266 djm 940: fatal_fr(r, "compose");
1.194 markus 941: } else if ((r = sshbuf_put_u8(msg, SSH_AGENT_FAILURE)) != 0)
1.266 djm 942: fatal_fr(r, "compose failure");
1.194 markus 943:
944: if ((r = sshbuf_put_stringb(e->output, msg)) != 0)
1.266 djm 945: fatal_fr(r, "enqueue");
1.194 markus 946:
1.280 djm 947: sshbuf_free(sid);
1.270 djm 948: sshbuf_free(data);
1.194 markus 949: sshbuf_free(msg);
1.270 djm 950: sshkey_free(key);
1.283 djm 951: sshkey_free(hostkey);
1.270 djm 952: free(fp);
1.173 djm 953: free(signature);
1.280 djm 954: free(sig_dest);
955: free(user);
1.287 djm 956: free(prompt);
957: if (pin != NULL)
958: freezero(pin, strlen(pin));
1.1 deraadt 959: }
960:
1.33 markus 961: /* shared */
1.55 itojun 962: static void
1.221 djm 963: process_remove_identity(SocketEntry *e)
1.1 deraadt 964: {
1.194 markus 965: int r, success = 0;
966: struct sshkey *key = NULL;
1.221 djm 967: Identity *id;
1.21 markus 968:
1.269 djm 969: debug2_f("entering");
1.221 djm 970: if ((r = sshkey_froms(e->request, &key)) != 0) {
1.266 djm 971: error_fr(r, "parse key");
1.221 djm 972: goto done;
973: }
974: if ((id = lookup_identity(key)) == NULL) {
1.266 djm 975: debug_f("key not found");
1.221 djm 976: goto done;
977: }
1.281 djm 978: /* identity not visible, cannot be removed */
979: if (identity_permitted(id, e, NULL, NULL, NULL) != 0)
980: goto done; /* error already logged */
1.221 djm 981: /* We have this key, free it. */
982: if (idtab->nentries < 1)
1.266 djm 983: fatal_f("internal error: nentries %d", idtab->nentries);
1.221 djm 984: TAILQ_REMOVE(&idtab->idlist, id, next);
985: free_identity(id);
986: idtab->nentries--;
987: success = 1;
988: done:
1.276 djm 989: sshkey_free(key);
1.194 markus 990: send_status(e, success);
1.1 deraadt 991: }
992:
1.55 itojun 993: static void
1.221 djm 994: process_remove_all_identities(SocketEntry *e)
1.1 deraadt 995: {
1.78 provos 996: Identity *id;
1.21 markus 997:
1.269 djm 998: debug2_f("entering");
1.21 markus 999: /* Loop over all identities and clear the keys. */
1.221 djm 1000: for (id = TAILQ_FIRST(&idtab->idlist); id;
1001: id = TAILQ_FIRST(&idtab->idlist)) {
1002: TAILQ_REMOVE(&idtab->idlist, id, next);
1.78 provos 1003: free_identity(id);
1.21 markus 1004: }
1005:
1006: /* Mark that there are no identities. */
1.221 djm 1007: idtab->nentries = 0;
1.21 markus 1008:
1009: /* Send success. */
1.194 markus 1010: send_status(e, 1);
1.1 deraadt 1011: }
1012:
1.155 dtucker 1013: /* removes expired keys and returns number of seconds until the next expiry */
1.174 dtucker 1014: static time_t
1.89 markus 1015: reaper(void)
1016: {
1.175 dtucker 1017: time_t deadline = 0, now = monotime();
1.89 markus 1018: Identity *id, *nxt;
1019:
1.221 djm 1020: for (id = TAILQ_FIRST(&idtab->idlist); id; id = nxt) {
1021: nxt = TAILQ_NEXT(id, next);
1022: if (id->death == 0)
1023: continue;
1024: if (now >= id->death) {
1025: debug("expiring key '%s'", id->comment);
1026: TAILQ_REMOVE(&idtab->idlist, id, next);
1027: free_identity(id);
1028: idtab->nentries--;
1029: } else
1030: deadline = (deadline == 0) ? id->death :
1031: MINIMUM(deadline, id->death);
1.89 markus 1032: }
1.155 dtucker 1033: if (deadline == 0 || deadline <= now)
1034: return 0;
1035: else
1036: return (deadline - now);
1.89 markus 1037: }
1038:
1.271 djm 1039: static int
1.281 djm 1040: parse_dest_constraint_hop(struct sshbuf *b, struct dest_constraint_hop *dch)
1041: {
1042: u_char key_is_ca;
1043: size_t elen = 0;
1044: int r;
1045: struct sshkey *k = NULL;
1046: char *fp;
1047:
1048: memset(dch, '\0', sizeof(*dch));
1049: if ((r = sshbuf_get_cstring(b, &dch->user, NULL)) != 0 ||
1050: (r = sshbuf_get_cstring(b, &dch->hostname, NULL)) != 0 ||
1051: (r = sshbuf_get_string_direct(b, NULL, &elen)) != 0) {
1052: error_fr(r, "parse");
1053: goto out;
1054: }
1055: if (elen != 0) {
1056: error_f("unsupported extensions (len %zu)", elen);
1057: r = SSH_ERR_FEATURE_UNSUPPORTED;
1058: goto out;
1059: }
1060: if (*dch->hostname == '\0') {
1061: free(dch->hostname);
1062: dch->hostname = NULL;
1063: }
1064: if (*dch->user == '\0') {
1065: free(dch->user);
1066: dch->user = NULL;
1067: }
1068: while (sshbuf_len(b) != 0) {
1069: dch->keys = xrecallocarray(dch->keys, dch->nkeys,
1070: dch->nkeys + 1, sizeof(*dch->keys));
1071: dch->key_is_ca = xrecallocarray(dch->key_is_ca, dch->nkeys,
1072: dch->nkeys + 1, sizeof(*dch->key_is_ca));
1073: if ((r = sshkey_froms(b, &k)) != 0 ||
1074: (r = sshbuf_get_u8(b, &key_is_ca)) != 0)
1075: goto out;
1076: if ((fp = sshkey_fingerprint(k, SSH_FP_HASH_DEFAULT,
1077: SSH_FP_DEFAULT)) == NULL)
1078: fatal_f("fingerprint failed");
1079: debug3_f("%s%s%s: adding %skey %s %s",
1080: dch->user == NULL ? "" : dch->user,
1081: dch->user == NULL ? "" : "@",
1082: dch->hostname, key_is_ca ? "CA " : "", sshkey_type(k), fp);
1083: free(fp);
1084: dch->keys[dch->nkeys] = k;
1085: dch->key_is_ca[dch->nkeys] = key_is_ca != 0;
1086: dch->nkeys++;
1087: k = NULL; /* transferred */
1088: }
1089: /* success */
1090: r = 0;
1091: out:
1092: sshkey_free(k);
1093: return r;
1094: }
1095:
1096: static int
1097: parse_dest_constraint(struct sshbuf *m, struct dest_constraint *dc)
1098: {
1099: struct sshbuf *b = NULL, *frombuf = NULL, *tobuf = NULL;
1100: int r;
1101: size_t elen = 0;
1102:
1103: debug3_f("entering");
1104:
1105: memset(dc, '\0', sizeof(*dc));
1106: if ((r = sshbuf_froms(m, &b)) != 0 ||
1107: (r = sshbuf_froms(b, &frombuf)) != 0 ||
1108: (r = sshbuf_froms(b, &tobuf)) != 0 ||
1109: (r = sshbuf_get_string_direct(b, NULL, &elen)) != 0) {
1110: error_fr(r, "parse");
1111: goto out;
1112: }
1.297 jcs 1113: if ((r = parse_dest_constraint_hop(frombuf, &dc->from)) != 0 ||
1114: (r = parse_dest_constraint_hop(tobuf, &dc->to)) != 0)
1.281 djm 1115: goto out; /* already logged */
1116: if (elen != 0) {
1117: error_f("unsupported extensions (len %zu)", elen);
1118: r = SSH_ERR_FEATURE_UNSUPPORTED;
1119: goto out;
1120: }
1121: debug2_f("parsed %s (%u keys) > %s%s%s (%u keys)",
1122: dc->from.hostname ? dc->from.hostname : "(ORIGIN)", dc->from.nkeys,
1123: dc->to.user ? dc->to.user : "", dc->to.user ? "@" : "",
1124: dc->to.hostname ? dc->to.hostname : "(ANY)", dc->to.nkeys);
1125: /* check consistency */
1126: if ((dc->from.hostname == NULL) != (dc->from.nkeys == 0) ||
1127: dc->from.user != NULL) {
1128: error_f("inconsistent \"from\" specification");
1129: r = SSH_ERR_INVALID_FORMAT;
1130: goto out;
1131: }
1132: if (dc->to.hostname == NULL || dc->to.nkeys == 0) {
1133: error_f("incomplete \"to\" specification");
1134: r = SSH_ERR_INVALID_FORMAT;
1135: goto out;
1136: }
1137: /* success */
1138: r = 0;
1139: out:
1140: sshbuf_free(b);
1141: sshbuf_free(frombuf);
1142: sshbuf_free(tobuf);
1143: return r;
1144: }
1145:
1146: static int
1147: parse_key_constraint_extension(struct sshbuf *m, char **sk_providerp,
1148: struct dest_constraint **dcsp, size_t *ndcsp)
1.277 djm 1149: {
1150: char *ext_name = NULL;
1151: int r;
1.281 djm 1152: struct sshbuf *b = NULL;
1.277 djm 1153:
1154: if ((r = sshbuf_get_cstring(m, &ext_name, NULL)) != 0) {
1155: error_fr(r, "parse constraint extension");
1156: goto out;
1157: }
1158: debug_f("constraint ext %s", ext_name);
1159: if (strcmp(ext_name, "sk-provider@openssh.com") == 0) {
1160: if (sk_providerp == NULL) {
1161: error_f("%s not valid here", ext_name);
1162: r = SSH_ERR_INVALID_FORMAT;
1163: goto out;
1164: }
1165: if (*sk_providerp != NULL) {
1166: error_f("%s already set", ext_name);
1167: r = SSH_ERR_INVALID_FORMAT;
1168: goto out;
1169: }
1170: if ((r = sshbuf_get_cstring(m, sk_providerp, NULL)) != 0) {
1171: error_fr(r, "parse %s", ext_name);
1172: goto out;
1173: }
1.281 djm 1174: } else if (strcmp(ext_name,
1175: "restrict-destination-v00@openssh.com") == 0) {
1176: if (*dcsp != NULL) {
1177: error_f("%s already set", ext_name);
1178: goto out;
1179: }
1180: if ((r = sshbuf_froms(m, &b)) != 0) {
1181: error_fr(r, "parse %s outer", ext_name);
1182: goto out;
1183: }
1184: while (sshbuf_len(b) != 0) {
1185: if (*ndcsp >= AGENT_MAX_DEST_CONSTRAINTS) {
1186: error_f("too many %s constraints", ext_name);
1187: goto out;
1188: }
1189: *dcsp = xrecallocarray(*dcsp, *ndcsp, *ndcsp + 1,
1190: sizeof(**dcsp));
1191: if ((r = parse_dest_constraint(b,
1192: *dcsp + (*ndcsp)++)) != 0)
1193: goto out; /* error already logged */
1194: }
1.277 djm 1195: } else {
1196: error_f("unsupported constraint \"%s\"", ext_name);
1197: r = SSH_ERR_FEATURE_UNSUPPORTED;
1198: goto out;
1199: }
1200: /* success */
1201: r = 0;
1202: out:
1203: free(ext_name);
1.281 djm 1204: sshbuf_free(b);
1.277 djm 1205: return r;
1206: }
1207:
1208: static int
1.271 djm 1209: parse_key_constraints(struct sshbuf *m, struct sshkey *k, time_t *deathp,
1.281 djm 1210: u_int *secondsp, int *confirmp, char **sk_providerp,
1211: struct dest_constraint **dcsp, size_t *ndcsp)
1.1 deraadt 1212: {
1.194 markus 1213: u_char ctype;
1.271 djm 1214: int r;
1215: u_int seconds, maxsign = 0;
1.33 markus 1216:
1.271 djm 1217: while (sshbuf_len(m)) {
1218: if ((r = sshbuf_get_u8(m, &ctype)) != 0) {
1.266 djm 1219: error_fr(r, "parse constraint type");
1.277 djm 1220: goto out;
1.194 markus 1221: }
1222: switch (ctype) {
1.94 markus 1223: case SSH_AGENT_CONSTRAIN_LIFETIME:
1.271 djm 1224: if (*deathp != 0) {
1225: error_f("lifetime already set");
1.277 djm 1226: r = SSH_ERR_INVALID_FORMAT;
1227: goto out;
1.271 djm 1228: }
1229: if ((r = sshbuf_get_u32(m, &seconds)) != 0) {
1.266 djm 1230: error_fr(r, "parse lifetime constraint");
1.277 djm 1231: goto out;
1.194 markus 1232: }
1.271 djm 1233: *deathp = monotime() + seconds;
1234: *secondsp = seconds;
1.94 markus 1235: break;
1.107 markus 1236: case SSH_AGENT_CONSTRAIN_CONFIRM:
1.271 djm 1237: if (*confirmp != 0) {
1238: error_f("confirm already set");
1.277 djm 1239: r = SSH_ERR_INVALID_FORMAT;
1240: goto out;
1.271 djm 1241: }
1242: *confirmp = 1;
1.107 markus 1243: break;
1.228 markus 1244: case SSH_AGENT_CONSTRAIN_MAXSIGN:
1.271 djm 1245: if (k == NULL) {
1246: error_f("maxsign not valid here");
1.277 djm 1247: r = SSH_ERR_INVALID_FORMAT;
1248: goto out;
1.271 djm 1249: }
1250: if (maxsign != 0) {
1251: error_f("maxsign already set");
1.277 djm 1252: r = SSH_ERR_INVALID_FORMAT;
1253: goto out;
1.271 djm 1254: }
1255: if ((r = sshbuf_get_u32(m, &maxsign)) != 0) {
1.266 djm 1256: error_fr(r, "parse maxsign constraint");
1.277 djm 1257: goto out;
1.228 markus 1258: }
1259: if ((r = sshkey_enable_maxsign(k, maxsign)) != 0) {
1.266 djm 1260: error_fr(r, "enable maxsign");
1.277 djm 1261: goto out;
1.228 markus 1262: }
1263: break;
1.238 djm 1264: case SSH_AGENT_CONSTRAIN_EXTENSION:
1.277 djm 1265: if ((r = parse_key_constraint_extension(m,
1.281 djm 1266: sk_providerp, dcsp, ndcsp)) != 0)
1.277 djm 1267: goto out; /* error already logged */
1.238 djm 1268: break;
1.94 markus 1269: default:
1.266 djm 1270: error_f("Unknown constraint %d", ctype);
1.277 djm 1271: r = SSH_ERR_FEATURE_UNSUPPORTED;
1272: goto out;
1.94 markus 1273: }
1274: }
1.271 djm 1275: /* success */
1.277 djm 1276: r = 0;
1277: out:
1278: return r;
1.271 djm 1279: }
1280:
1281: static void
1282: process_add_identity(SocketEntry *e)
1283: {
1284: Identity *id;
1285: int success = 0, confirm = 0;
1.272 dtucker 1286: char *fp, *comment = NULL, *sk_provider = NULL;
1.271 djm 1287: char canonical_provider[PATH_MAX];
1288: time_t death = 0;
1289: u_int seconds = 0;
1.281 djm 1290: struct dest_constraint *dest_constraints = NULL;
1291: size_t ndest_constraints = 0;
1.271 djm 1292: struct sshkey *k = NULL;
1293: int r = SSH_ERR_INTERNAL_ERROR;
1294:
1295: debug2_f("entering");
1296: if ((r = sshkey_private_deserialize(e->request, &k)) != 0 ||
1297: k == NULL ||
1298: (r = sshbuf_get_cstring(e->request, &comment, NULL)) != 0) {
1299: error_fr(r, "parse");
1300: goto out;
1301: }
1302: if (parse_key_constraints(e->request, k, &death, &seconds, &confirm,
1.281 djm 1303: &sk_provider, &dest_constraints, &ndest_constraints) != 0) {
1.271 djm 1304: error_f("failed to parse constraints");
1305: sshbuf_reset(e->request);
1306: goto out;
1307: }
1.301 ! djm 1308: dump_dest_constraints(__func__, dest_constraints, ndest_constraints);
1.271 djm 1309:
1.238 djm 1310: if (sk_provider != NULL) {
1.240 markus 1311: if (!sshkey_is_sk(k)) {
1.255 naddy 1312: error("Cannot add provider: %s is not an "
1313: "authenticator-hosted key", sshkey_type(k));
1.271 djm 1314: goto out;
1.238 djm 1315: }
1.254 djm 1316: if (strcasecmp(sk_provider, "internal") == 0) {
1.266 djm 1317: debug_f("internal provider");
1.254 djm 1318: } else {
1.300 djm 1319: if (e->nsession_ids != 0 && !remote_add_provider) {
1320: verbose("failed add of SK provider \"%.100s\": "
1321: "remote addition of providers is disabled",
1322: sk_provider);
1323: goto out;
1324: }
1.254 djm 1325: if (realpath(sk_provider, canonical_provider) == NULL) {
1326: verbose("failed provider \"%.100s\": "
1327: "realpath: %s", sk_provider,
1328: strerror(errno));
1.271 djm 1329: goto out;
1.254 djm 1330: }
1.238 djm 1331: free(sk_provider);
1.254 djm 1332: sk_provider = xstrdup(canonical_provider);
1333: if (match_pattern_list(sk_provider,
1.260 djm 1334: allowed_providers, 0) != 1) {
1.254 djm 1335: error("Refusing add key: "
1.260 djm 1336: "provider %s not allowed", sk_provider);
1.271 djm 1337: goto out;
1.254 djm 1338: }
1.238 djm 1339: }
1.242 markus 1340: }
1341: if ((r = sshkey_shield_private(k)) != 0) {
1.266 djm 1342: error_fr(r, "shield private");
1.271 djm 1343: goto out;
1.238 djm 1344: }
1.106 marc 1345: if (lifetime && !death)
1.175 dtucker 1346: death = monotime() + lifetime;
1.221 djm 1347: if ((id = lookup_identity(k)) == NULL) {
1.163 markus 1348: id = xcalloc(1, sizeof(Identity));
1.221 djm 1349: TAILQ_INSERT_TAIL(&idtab->idlist, id, next);
1.33 markus 1350: /* Increment the number of identities. */
1.221 djm 1351: idtab->nentries++;
1.33 markus 1352: } else {
1.281 djm 1353: /* identity not visible, do not update */
1354: if (identity_permitted(id, e, NULL, NULL, NULL) != 0)
1355: goto out; /* error already logged */
1.228 markus 1356: /* key state might have been updated */
1357: sshkey_free(id->key);
1.173 djm 1358: free(id->comment);
1.238 djm 1359: free(id->sk_provider);
1.281 djm 1360: free_dest_constraints(id->dest_constraints,
1361: id->ndest_constraints);
1.33 markus 1362: }
1.271 djm 1363: /* success */
1.228 markus 1364: id->key = k;
1.157 canacar 1365: id->comment = comment;
1366: id->death = death;
1367: id->confirm = confirm;
1.238 djm 1368: id->sk_provider = sk_provider;
1.281 djm 1369: id->dest_constraints = dest_constraints;
1370: id->ndest_constraints = ndest_constraints;
1.238 djm 1371:
1372: if ((fp = sshkey_fingerprint(k, SSH_FP_HASH_DEFAULT,
1373: SSH_FP_DEFAULT)) == NULL)
1.266 djm 1374: fatal_f("sshkey_fingerprint failed");
1375: debug_f("add %s %s \"%.100s\" (life: %u) (confirm: %u) "
1.281 djm 1376: "(provider: %s) (destination constraints: %zu)",
1377: sshkey_ssh_name(k), fp, comment, seconds, confirm,
1378: sk_provider == NULL ? "none" : sk_provider, ndest_constraints);
1.238 djm 1379: free(fp);
1.271 djm 1380: /* transferred */
1381: k = NULL;
1382: comment = NULL;
1383: sk_provider = NULL;
1.281 djm 1384: dest_constraints = NULL;
1385: ndest_constraints = 0;
1.271 djm 1386: success = 1;
1387: out:
1388: free(sk_provider);
1389: free(comment);
1390: sshkey_free(k);
1.281 djm 1391: free_dest_constraints(dest_constraints, ndest_constraints);
1.194 markus 1392: send_status(e, success);
1.1 deraadt 1393: }
1394:
1.88 markus 1395: /* XXX todo: encrypt sensitive data with passphrase */
1396: static void
1397: process_lock_agent(SocketEntry *e, int lock)
1398: {
1.203 dtucker 1399: int r, success = 0, delay;
1.213 djm 1400: char *passwd;
1401: u_char passwdhash[LOCK_SIZE];
1.203 dtucker 1402: static u_int fail_count = 0;
1403: size_t pwlen;
1404:
1.269 djm 1405: debug2_f("entering");
1.226 djm 1406: /*
1407: * This is deliberately fatal: the user has requested that we lock,
1408: * but we can't parse their request properly. The only safe thing to
1409: * do is abort.
1410: */
1.203 dtucker 1411: if ((r = sshbuf_get_cstring(e->request, &passwd, &pwlen)) != 0)
1.266 djm 1412: fatal_fr(r, "parse");
1.203 dtucker 1413: if (pwlen == 0) {
1414: debug("empty password not supported");
1415: } else if (locked && !lock) {
1416: if (bcrypt_pbkdf(passwd, pwlen, lock_salt, sizeof(lock_salt),
1417: passwdhash, sizeof(passwdhash), LOCK_ROUNDS) < 0)
1418: fatal("bcrypt_pbkdf");
1.213 djm 1419: if (timingsafe_bcmp(passwdhash, lock_pwhash, LOCK_SIZE) == 0) {
1.203 dtucker 1420: debug("agent unlocked");
1421: locked = 0;
1422: fail_count = 0;
1.213 djm 1423: explicit_bzero(lock_pwhash, sizeof(lock_pwhash));
1.203 dtucker 1424: success = 1;
1425: } else {
1426: /* delay in 0.1s increments up to 10s */
1427: if (fail_count < 100)
1428: fail_count++;
1429: delay = 100000 * fail_count;
1430: debug("unlock failed, delaying %0.1lf seconds",
1431: (double)delay/1000000);
1432: usleep(delay);
1433: }
1434: explicit_bzero(passwdhash, sizeof(passwdhash));
1.88 markus 1435: } else if (!locked && lock) {
1.203 dtucker 1436: debug("agent locked");
1.88 markus 1437: locked = 1;
1.203 dtucker 1438: arc4random_buf(lock_salt, sizeof(lock_salt));
1439: if (bcrypt_pbkdf(passwd, pwlen, lock_salt, sizeof(lock_salt),
1.213 djm 1440: lock_pwhash, sizeof(lock_pwhash), LOCK_ROUNDS) < 0)
1.203 dtucker 1441: fatal("bcrypt_pbkdf");
1.88 markus 1442: success = 1;
1443: }
1.256 jsg 1444: freezero(passwd, pwlen);
1.194 markus 1445: send_status(e, success);
1.88 markus 1446: }
1447:
1448: static void
1.221 djm 1449: no_identities(SocketEntry *e)
1.88 markus 1450: {
1.194 markus 1451: struct sshbuf *msg;
1452: int r;
1.88 markus 1453:
1.194 markus 1454: if ((msg = sshbuf_new()) == NULL)
1.266 djm 1455: fatal_f("sshbuf_new failed");
1.221 djm 1456: if ((r = sshbuf_put_u8(msg, SSH2_AGENT_IDENTITIES_ANSWER)) != 0 ||
1.194 markus 1457: (r = sshbuf_put_u32(msg, 0)) != 0 ||
1458: (r = sshbuf_put_stringb(e->output, msg)) != 0)
1.266 djm 1459: fatal_fr(r, "compose");
1.194 markus 1460: sshbuf_free(msg);
1.88 markus 1461: }
1.59 markus 1462:
1.163 markus 1463: #ifdef ENABLE_PKCS11
1.59 markus 1464: static void
1.158 djm 1465: process_add_smartcard_key(SocketEntry *e)
1.59 markus 1466: {
1.226 djm 1467: char *provider = NULL, *pin = NULL, canonical_provider[PATH_MAX];
1.253 djm 1468: char **comments = NULL;
1.221 djm 1469: int r, i, count = 0, success = 0, confirm = 0;
1.271 djm 1470: u_int seconds = 0;
1.174 dtucker 1471: time_t death = 0;
1.194 markus 1472: struct sshkey **keys = NULL, *k;
1.84 markus 1473: Identity *id;
1.281 djm 1474: struct dest_constraint *dest_constraints = NULL;
1475: size_t ndest_constraints = 0;
1.75 deraadt 1476:
1.269 djm 1477: debug2_f("entering");
1.194 markus 1478: if ((r = sshbuf_get_cstring(e->request, &provider, NULL)) != 0 ||
1.226 djm 1479: (r = sshbuf_get_cstring(e->request, &pin, NULL)) != 0) {
1.266 djm 1480: error_fr(r, "parse");
1.226 djm 1481: goto send;
1482: }
1.271 djm 1483: if (parse_key_constraints(e->request, NULL, &death, &seconds, &confirm,
1.281 djm 1484: NULL, &dest_constraints, &ndest_constraints) != 0) {
1.271 djm 1485: error_f("failed to parse constraints");
1486: goto send;
1.110 djm 1487: }
1.301 ! djm 1488: dump_dest_constraints(__func__, dest_constraints, ndest_constraints);
1.300 djm 1489: if (e->nsession_ids != 0 && !remote_add_provider) {
1490: verbose("failed PKCS#11 add of \"%.100s\": remote addition of "
1491: "providers is disabled", provider);
1492: goto send;
1493: }
1.215 djm 1494: if (realpath(provider, canonical_provider) == NULL) {
1495: verbose("failed PKCS#11 add of \"%.100s\": realpath: %s",
1496: provider, strerror(errno));
1497: goto send;
1498: }
1.260 djm 1499: if (match_pattern_list(canonical_provider, allowed_providers, 0) != 1) {
1.215 djm 1500: verbose("refusing PKCS#11 add of \"%.100s\": "
1.260 djm 1501: "provider not allowed", canonical_provider);
1.215 djm 1502: goto send;
1503: }
1.266 djm 1504: debug_f("add %.100s", canonical_provider);
1.110 djm 1505: if (lifetime && !death)
1.175 dtucker 1506: death = monotime() + lifetime;
1.110 djm 1507:
1.253 djm 1508: count = pkcs11_add_provider(canonical_provider, pin, &keys, &comments);
1.163 markus 1509: for (i = 0; i < count; i++) {
1.84 markus 1510: k = keys[i];
1.221 djm 1511: if (lookup_identity(k) == NULL) {
1.163 markus 1512: id = xcalloc(1, sizeof(Identity));
1.84 markus 1513: id->key = k;
1.253 djm 1514: keys[i] = NULL; /* transferred */
1.215 djm 1515: id->provider = xstrdup(canonical_provider);
1.253 djm 1516: if (*comments[i] != '\0') {
1517: id->comment = comments[i];
1518: comments[i] = NULL; /* transferred */
1519: } else {
1520: id->comment = xstrdup(canonical_provider);
1521: }
1.110 djm 1522: id->death = death;
1523: id->confirm = confirm;
1.301 ! djm 1524: id->dest_constraints = dup_dest_constraints(
! 1525: dest_constraints, ndest_constraints);
1.281 djm 1526: id->ndest_constraints = ndest_constraints;
1.221 djm 1527: TAILQ_INSERT_TAIL(&idtab->idlist, id, next);
1528: idtab->nentries++;
1.84 markus 1529: success = 1;
1530: }
1.271 djm 1531: /* XXX update constraints for existing keys */
1.253 djm 1532: sshkey_free(keys[i]);
1533: free(comments[i]);
1.59 markus 1534: }
1535: send:
1.173 djm 1536: free(pin);
1537: free(provider);
1538: free(keys);
1.253 djm 1539: free(comments);
1.281 djm 1540: free_dest_constraints(dest_constraints, ndest_constraints);
1.194 markus 1541: send_status(e, success);
1.59 markus 1542: }
1543:
1544: static void
1545: process_remove_smartcard_key(SocketEntry *e)
1546: {
1.217 djm 1547: char *provider = NULL, *pin = NULL, canonical_provider[PATH_MAX];
1.221 djm 1548: int r, success = 0;
1.163 markus 1549: Identity *id, *nxt;
1.59 markus 1550:
1.269 djm 1551: debug2_f("entering");
1.194 markus 1552: if ((r = sshbuf_get_cstring(e->request, &provider, NULL)) != 0 ||
1.226 djm 1553: (r = sshbuf_get_cstring(e->request, &pin, NULL)) != 0) {
1.266 djm 1554: error_fr(r, "parse");
1.226 djm 1555: goto send;
1556: }
1.173 djm 1557: free(pin);
1.59 markus 1558:
1.217 djm 1559: if (realpath(provider, canonical_provider) == NULL) {
1560: verbose("failed PKCS#11 add of \"%.100s\": realpath: %s",
1561: provider, strerror(errno));
1562: goto send;
1563: }
1564:
1.266 djm 1565: debug_f("remove %.100s", canonical_provider);
1.221 djm 1566: for (id = TAILQ_FIRST(&idtab->idlist); id; id = nxt) {
1567: nxt = TAILQ_NEXT(id, next);
1568: /* Skip file--based keys */
1569: if (id->provider == NULL)
1570: continue;
1571: if (!strcmp(canonical_provider, id->provider)) {
1572: TAILQ_REMOVE(&idtab->idlist, id, next);
1573: free_identity(id);
1574: idtab->nentries--;
1.59 markus 1575: }
1576: }
1.217 djm 1577: if (pkcs11_del_provider(canonical_provider) == 0)
1.163 markus 1578: success = 1;
1579: else
1.266 djm 1580: error_f("pkcs11_del_provider failed");
1.218 deraadt 1581: send:
1.173 djm 1582: free(provider);
1.194 markus 1583: send_status(e, success);
1.59 markus 1584: }
1.163 markus 1585: #endif /* ENABLE_PKCS11 */
1.59 markus 1586:
1.280 djm 1587: static int
1588: process_ext_session_bind(SocketEntry *e)
1589: {
1590: int r, sid_match, key_match;
1591: struct sshkey *key = NULL;
1592: struct sshbuf *sid = NULL, *sig = NULL;
1593: char *fp = NULL;
1594: size_t i;
1.281 djm 1595: u_char fwd = 0;
1.280 djm 1596:
1597: debug2_f("entering");
1598: if ((r = sshkey_froms(e->request, &key)) != 0 ||
1599: (r = sshbuf_froms(e->request, &sid)) != 0 ||
1600: (r = sshbuf_froms(e->request, &sig)) != 0 ||
1601: (r = sshbuf_get_u8(e->request, &fwd)) != 0) {
1602: error_fr(r, "parse");
1603: goto out;
1604: }
1605: if ((fp = sshkey_fingerprint(key, SSH_FP_HASH_DEFAULT,
1606: SSH_FP_DEFAULT)) == NULL)
1607: fatal_f("fingerprint failed");
1608: /* check signature with hostkey on session ID */
1609: if ((r = sshkey_verify(key, sshbuf_ptr(sig), sshbuf_len(sig),
1610: sshbuf_ptr(sid), sshbuf_len(sid), NULL, 0, NULL)) != 0) {
1611: error_fr(r, "sshkey_verify for %s %s", sshkey_type(key), fp);
1612: goto out;
1613: }
1614: /* check whether sid/key already recorded */
1615: for (i = 0; i < e->nsession_ids; i++) {
1.281 djm 1616: if (!e->session_ids[i].forwarded) {
1617: error_f("attempt to bind session ID to socket "
1618: "previously bound for authentication attempt");
1619: r = -1;
1620: goto out;
1621: }
1.280 djm 1622: sid_match = buf_equal(sid, e->session_ids[i].sid) == 0;
1623: key_match = sshkey_equal(key, e->session_ids[i].key);
1624: if (sid_match && key_match) {
1625: debug_f("session ID already recorded for %s %s",
1626: sshkey_type(key), fp);
1627: r = 0;
1628: goto out;
1629: } else if (sid_match) {
1630: error_f("session ID recorded against different key "
1631: "for %s %s", sshkey_type(key), fp);
1632: r = -1;
1633: goto out;
1634: }
1635: /*
1636: * new sid with previously-seen key can happen, e.g. multiple
1637: * connections to the same host.
1638: */
1639: }
1640: /* record new key/sid */
1641: if (e->nsession_ids >= AGENT_MAX_SESSION_IDS) {
1642: error_f("too many session IDs recorded");
1643: goto out;
1644: }
1645: e->session_ids = xrecallocarray(e->session_ids, e->nsession_ids,
1646: e->nsession_ids + 1, sizeof(*e->session_ids));
1647: i = e->nsession_ids++;
1648: debug_f("recorded %s %s (slot %zu of %d)", sshkey_type(key), fp, i,
1649: AGENT_MAX_SESSION_IDS);
1650: e->session_ids[i].key = key;
1651: e->session_ids[i].forwarded = fwd != 0;
1652: key = NULL; /* transferred */
1653: /* can't transfer sid; it's refcounted and scoped to request's life */
1654: if ((e->session_ids[i].sid = sshbuf_new()) == NULL)
1655: fatal_f("sshbuf_new");
1656: if ((r = sshbuf_putb(e->session_ids[i].sid, sid)) != 0)
1657: fatal_fr(r, "sshbuf_putb session ID");
1658: /* success */
1659: r = 0;
1660: out:
1.288 djm 1661: free(fp);
1.280 djm 1662: sshkey_free(key);
1663: sshbuf_free(sid);
1664: sshbuf_free(sig);
1665: return r == 0 ? 1 : 0;
1666: }
1667:
1668: static void
1669: process_extension(SocketEntry *e)
1670: {
1671: int r, success = 0;
1672: char *name;
1673:
1674: debug2_f("entering");
1675: if ((r = sshbuf_get_cstring(e->request, &name, NULL)) != 0) {
1676: error_fr(r, "parse");
1677: goto send;
1678: }
1679: if (strcmp(name, "session-bind@openssh.com") == 0)
1680: success = process_ext_session_bind(e);
1681: else
1682: debug_f("unsupported extension \"%s\"", name);
1.285 djm 1683: free(name);
1.280 djm 1684: send:
1685: send_status(e, success);
1686: }
1.264 djm 1687: /*
1688: * dispatch incoming message.
1689: * returns 1 on success, 0 for incomplete messages or -1 on error.
1690: */
1.223 djm 1691: static int
1692: process_message(u_int socknum)
1.1 deraadt 1693: {
1.194 markus 1694: u_int msg_len;
1695: u_char type;
1696: const u_char *cp;
1697: int r;
1.223 djm 1698: SocketEntry *e;
1699:
1.266 djm 1700: if (socknum >= sockets_alloc)
1701: fatal_f("sock %u >= allocated %u", socknum, sockets_alloc);
1.223 djm 1702: e = &sockets[socknum];
1.89 markus 1703:
1.194 markus 1704: if (sshbuf_len(e->input) < 5)
1.223 djm 1705: return 0; /* Incomplete message header. */
1.194 markus 1706: cp = sshbuf_ptr(e->input);
1707: msg_len = PEEK_U32(cp);
1.223 djm 1708: if (msg_len > AGENT_MAX_LEN) {
1.266 djm 1709: debug_f("socket %u (fd=%d) message too long %u > %u",
1710: socknum, e->fd, msg_len, AGENT_MAX_LEN);
1.223 djm 1711: return -1;
1.21 markus 1712: }
1.194 markus 1713: if (sshbuf_len(e->input) < msg_len + 4)
1.223 djm 1714: return 0; /* Incomplete message body. */
1.87 markus 1715:
1716: /* move the current input to e->request */
1.194 markus 1717: sshbuf_reset(e->request);
1718: if ((r = sshbuf_get_stringb(e->input, e->request)) != 0 ||
1.223 djm 1719: (r = sshbuf_get_u8(e->request, &type)) != 0) {
1720: if (r == SSH_ERR_MESSAGE_INCOMPLETE ||
1721: r == SSH_ERR_STRING_TOO_LARGE) {
1.266 djm 1722: error_fr(r, "parse");
1.223 djm 1723: return -1;
1724: }
1.266 djm 1725: fatal_fr(r, "parse");
1.223 djm 1726: }
1727:
1.266 djm 1728: debug_f("socket %u (fd=%d) type %d", socknum, e->fd, type);
1.21 markus 1729:
1.230 djm 1730: /* check whether agent is locked */
1.88 markus 1731: if (locked && type != SSH_AGENTC_UNLOCK) {
1.194 markus 1732: sshbuf_reset(e->request);
1.88 markus 1733: switch (type) {
1734: case SSH2_AGENTC_REQUEST_IDENTITIES:
1735: /* send empty lists */
1.221 djm 1736: no_identities(e);
1.88 markus 1737: break;
1738: default:
1739: /* send a fail message for all other request types */
1.194 markus 1740: send_status(e, 0);
1.88 markus 1741: }
1.264 djm 1742: return 1;
1.88 markus 1743: }
1744:
1.21 markus 1745: switch (type) {
1.88 markus 1746: case SSH_AGENTC_LOCK:
1747: case SSH_AGENTC_UNLOCK:
1748: process_lock_agent(e, type == SSH_AGENTC_LOCK);
1749: break;
1.21 markus 1750: case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
1.221 djm 1751: process_remove_all_identities(e); /* safe for !WITH_SSH1 */
1.33 markus 1752: break;
1753: /* ssh2 */
1754: case SSH2_AGENTC_SIGN_REQUEST:
1755: process_sign_request2(e);
1756: break;
1757: case SSH2_AGENTC_REQUEST_IDENTITIES:
1.221 djm 1758: process_request_identities(e);
1.33 markus 1759: break;
1760: case SSH2_AGENTC_ADD_IDENTITY:
1.94 markus 1761: case SSH2_AGENTC_ADD_ID_CONSTRAINED:
1.221 djm 1762: process_add_identity(e);
1.33 markus 1763: break;
1764: case SSH2_AGENTC_REMOVE_IDENTITY:
1.221 djm 1765: process_remove_identity(e);
1.33 markus 1766: break;
1767: case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
1.221 djm 1768: process_remove_all_identities(e);
1.21 markus 1769: break;
1.163 markus 1770: #ifdef ENABLE_PKCS11
1.59 markus 1771: case SSH_AGENTC_ADD_SMARTCARD_KEY:
1.110 djm 1772: case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED:
1.59 markus 1773: process_add_smartcard_key(e);
1.75 deraadt 1774: break;
1.59 markus 1775: case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
1776: process_remove_smartcard_key(e);
1.75 deraadt 1777: break;
1.163 markus 1778: #endif /* ENABLE_PKCS11 */
1.280 djm 1779: case SSH_AGENTC_EXTENSION:
1780: process_extension(e);
1781: break;
1.21 markus 1782: default:
1783: /* Unknown message. Respond with failure. */
1784: error("Unknown message %d", type);
1.194 markus 1785: sshbuf_reset(e->request);
1786: send_status(e, 0);
1.21 markus 1787: break;
1788: }
1.264 djm 1789: return 1;
1.1 deraadt 1790: }
1791:
1.55 itojun 1792: static void
1.73 stevesk 1793: new_socket(sock_type type, int fd)
1.1 deraadt 1794: {
1.112 markus 1795: u_int i, old_alloc, new_alloc;
1.96 deraadt 1796:
1.269 djm 1797: debug_f("type = %s", type == AUTH_CONNECTION ? "CONNECTION" :
1798: (type == AUTH_SOCKET ? "SOCKET" : "UNKNOWN"));
1.119 djm 1799: set_nonblock(fd);
1.21 markus 1800:
1801: if (fd > max_fd)
1802: max_fd = fd;
1803:
1804: for (i = 0; i < sockets_alloc; i++)
1805: if (sockets[i].type == AUTH_UNUSED) {
1806: sockets[i].fd = fd;
1.266 djm 1807: if ((sockets[i].input = sshbuf_new()) == NULL ||
1808: (sockets[i].output = sshbuf_new()) == NULL ||
1809: (sockets[i].request = sshbuf_new()) == NULL)
1810: fatal_f("sshbuf_new failed");
1.112 markus 1811: sockets[i].type = type;
1.21 markus 1812: return;
1813: }
1814: old_alloc = sockets_alloc;
1.112 markus 1815: new_alloc = sockets_alloc + 10;
1.269 djm 1816: sockets = xrecallocarray(sockets, old_alloc, new_alloc,
1817: sizeof(sockets[0]));
1.112 markus 1818: for (i = old_alloc; i < new_alloc; i++)
1.21 markus 1819: sockets[i].type = AUTH_UNUSED;
1.112 markus 1820: sockets_alloc = new_alloc;
1.21 markus 1821: sockets[old_alloc].fd = fd;
1.266 djm 1822: if ((sockets[old_alloc].input = sshbuf_new()) == NULL ||
1823: (sockets[old_alloc].output = sshbuf_new()) == NULL ||
1824: (sockets[old_alloc].request = sshbuf_new()) == NULL)
1825: fatal_f("sshbuf_new failed");
1.112 markus 1826: sockets[old_alloc].type = type;
1.1 deraadt 1827: }
1828:
1.55 itojun 1829: static int
1.223 djm 1830: handle_socket_read(u_int socknum)
1.1 deraadt 1831: {
1.223 djm 1832: struct sockaddr_un sunaddr;
1833: socklen_t slen;
1834: uid_t euid;
1835: gid_t egid;
1836: int fd;
1837:
1838: slen = sizeof(sunaddr);
1839: fd = accept(sockets[socknum].fd, (struct sockaddr *)&sunaddr, &slen);
1.237 deraadt 1840: if (fd == -1) {
1.223 djm 1841: error("accept from AUTH_SOCKET: %s", strerror(errno));
1842: return -1;
1843: }
1.237 deraadt 1844: if (getpeereid(fd, &euid, &egid) == -1) {
1.223 djm 1845: error("getpeereid %d failed: %s", fd, strerror(errno));
1846: close(fd);
1847: return -1;
1848: }
1849: if ((euid != 0) && (getuid() != euid)) {
1850: error("uid mismatch: peer euid %u != uid %u",
1851: (u_int) euid, (u_int) getuid());
1852: close(fd);
1853: return -1;
1854: }
1855: new_socket(AUTH_CONNECTION, fd);
1856: return 0;
1857: }
1858:
1859: static int
1860: handle_conn_read(u_int socknum)
1861: {
1.233 djm 1862: char buf[AGENT_RBUF_LEN];
1.223 djm 1863: ssize_t len;
1864: int r;
1865:
1866: if ((len = read(sockets[socknum].fd, buf, sizeof(buf))) <= 0) {
1867: if (len == -1) {
1868: if (errno == EAGAIN || errno == EINTR)
1869: return 0;
1.266 djm 1870: error_f("read error on socket %u (fd %d): %s",
1871: socknum, sockets[socknum].fd, strerror(errno));
1.223 djm 1872: }
1873: return -1;
1874: }
1875: if ((r = sshbuf_put(sockets[socknum].input, buf, len)) != 0)
1.266 djm 1876: fatal_fr(r, "compose");
1.223 djm 1877: explicit_bzero(buf, sizeof(buf));
1.264 djm 1878: for (;;) {
1879: if ((r = process_message(socknum)) == -1)
1880: return -1;
1881: else if (r == 0)
1882: break;
1883: }
1.223 djm 1884: return 0;
1885: }
1886:
1887: static int
1888: handle_conn_write(u_int socknum)
1889: {
1890: ssize_t len;
1891: int r;
1892:
1893: if (sshbuf_len(sockets[socknum].output) == 0)
1894: return 0; /* shouldn't happen */
1895: if ((len = write(sockets[socknum].fd,
1896: sshbuf_ptr(sockets[socknum].output),
1897: sshbuf_len(sockets[socknum].output))) <= 0) {
1898: if (len == -1) {
1899: if (errno == EAGAIN || errno == EINTR)
1900: return 0;
1.266 djm 1901: error_f("read error on socket %u (fd %d): %s",
1902: socknum, sockets[socknum].fd, strerror(errno));
1.223 djm 1903: }
1904: return -1;
1905: }
1906: if ((r = sshbuf_consume(sockets[socknum].output, len)) != 0)
1.266 djm 1907: fatal_fr(r, "consume");
1.223 djm 1908: return 0;
1909: }
1910:
1911: static void
1.231 djm 1912: after_poll(struct pollfd *pfd, size_t npfd, u_int maxfds)
1.223 djm 1913: {
1914: size_t i;
1.231 djm 1915: u_int socknum, activefds = npfd;
1.223 djm 1916:
1917: for (i = 0; i < npfd; i++) {
1918: if (pfd[i].revents == 0)
1919: continue;
1920: /* Find sockets entry */
1921: for (socknum = 0; socknum < sockets_alloc; socknum++) {
1922: if (sockets[socknum].type != AUTH_SOCKET &&
1923: sockets[socknum].type != AUTH_CONNECTION)
1924: continue;
1925: if (pfd[i].fd == sockets[socknum].fd)
1926: break;
1927: }
1928: if (socknum >= sockets_alloc) {
1.266 djm 1929: error_f("no socket for fd %d", pfd[i].fd);
1.223 djm 1930: continue;
1931: }
1932: /* Process events */
1933: switch (sockets[socknum].type) {
1934: case AUTH_SOCKET:
1.231 djm 1935: if ((pfd[i].revents & (POLLIN|POLLERR)) == 0)
1936: break;
1937: if (npfd > maxfds) {
1938: debug3("out of fds (active %u >= limit %u); "
1939: "skipping accept", activefds, maxfds);
1940: break;
1941: }
1942: if (handle_socket_read(socknum) == 0)
1943: activefds++;
1.223 djm 1944: break;
1945: case AUTH_CONNECTION:
1.279 djm 1946: if ((pfd[i].revents & (POLLIN|POLLHUP|POLLERR)) != 0 &&
1947: handle_conn_read(socknum) != 0)
1.231 djm 1948: goto close_sock;
1949: if ((pfd[i].revents & (POLLOUT|POLLHUP)) != 0 &&
1950: handle_conn_write(socknum) != 0) {
1951: close_sock:
1952: if (activefds == 0)
1953: fatal("activefds == 0 at close_sock");
1.223 djm 1954: close_socket(&sockets[socknum]);
1.231 djm 1955: activefds--;
1.223 djm 1956: break;
1957: }
1958: break;
1959: default:
1960: break;
1961: }
1962: }
1963: }
1964:
1965: static int
1.231 djm 1966: prepare_poll(struct pollfd **pfdp, size_t *npfdp, int *timeoutp, u_int maxfds)
1.223 djm 1967: {
1968: struct pollfd *pfd = *pfdp;
1969: size_t i, j, npfd = 0;
1.174 dtucker 1970: time_t deadline;
1.233 djm 1971: int r;
1.46 markus 1972:
1.223 djm 1973: /* Count active sockets */
1.46 markus 1974: for (i = 0; i < sockets_alloc; i++) {
1.21 markus 1975: switch (sockets[i].type) {
1976: case AUTH_SOCKET:
1977: case AUTH_CONNECTION:
1.223 djm 1978: npfd++;
1.21 markus 1979: break;
1980: case AUTH_UNUSED:
1981: break;
1982: default:
1983: fatal("Unknown socket type %d", sockets[i].type);
1984: break;
1985: }
1.46 markus 1986: }
1.223 djm 1987: if (npfd != *npfdp &&
1988: (pfd = recallocarray(pfd, *npfdp, npfd, sizeof(*pfd))) == NULL)
1.266 djm 1989: fatal_f("recallocarray failed");
1.223 djm 1990: *pfdp = pfd;
1991: *npfdp = npfd;
1.46 markus 1992:
1.223 djm 1993: for (i = j = 0; i < sockets_alloc; i++) {
1.46 markus 1994: switch (sockets[i].type) {
1995: case AUTH_SOCKET:
1.231 djm 1996: if (npfd > maxfds) {
1997: debug3("out of fds (active %zu >= limit %u); "
1998: "skipping arming listener", npfd, maxfds);
1999: break;
2000: }
2001: pfd[j].fd = sockets[i].fd;
2002: pfd[j].revents = 0;
2003: pfd[j].events = POLLIN;
2004: j++;
2005: break;
1.46 markus 2006: case AUTH_CONNECTION:
1.223 djm 2007: pfd[j].fd = sockets[i].fd;
2008: pfd[j].revents = 0;
1.233 djm 2009: /*
2010: * Only prepare to read if we can handle a full-size
2011: * input read buffer and enqueue a max size reply..
2012: */
2013: if ((r = sshbuf_check_reserve(sockets[i].input,
2014: AGENT_RBUF_LEN)) == 0 &&
2015: (r = sshbuf_check_reserve(sockets[i].output,
1.278 djm 2016: AGENT_MAX_LEN)) == 0)
1.233 djm 2017: pfd[j].events = POLLIN;
1.266 djm 2018: else if (r != SSH_ERR_NO_BUFFER_SPACE)
2019: fatal_fr(r, "reserve");
1.194 markus 2020: if (sshbuf_len(sockets[i].output) > 0)
1.223 djm 2021: pfd[j].events |= POLLOUT;
2022: j++;
1.46 markus 2023: break;
2024: default:
2025: break;
2026: }
2027: }
1.155 dtucker 2028: deadline = reaper();
2029: if (parent_alive_interval != 0)
2030: deadline = (deadline == 0) ? parent_alive_interval :
1.214 deraadt 2031: MINIMUM(deadline, parent_alive_interval);
1.155 dtucker 2032: if (deadline == 0) {
1.224 djm 2033: *timeoutp = -1; /* INFTIM */
1.155 dtucker 2034: } else {
1.223 djm 2035: if (deadline > INT_MAX / 1000)
2036: *timeoutp = INT_MAX / 1000;
2037: else
2038: *timeoutp = deadline * 1000;
1.155 dtucker 2039: }
1.46 markus 2040: return (1);
1.21 markus 2041: }
2042:
1.55 itojun 2043: static void
1.113 markus 2044: cleanup_socket(void)
1.15 markus 2045: {
1.187 djm 2046: if (cleanup_pid != 0 && getpid() != cleanup_pid)
2047: return;
1.266 djm 2048: debug_f("cleanup");
1.48 deraadt 2049: if (socket_name[0])
2050: unlink(socket_name);
2051: if (socket_dir[0])
2052: rmdir(socket_dir);
1.10 markus 2053: }
2054:
1.114 markus 2055: void
1.15 markus 2056: cleanup_exit(int i)
2057: {
1.113 markus 2058: cleanup_socket();
2059: _exit(i);
1.15 markus 2060: }
2061:
1.55 itojun 2062: static void
1.48 deraadt 2063: cleanup_handler(int sig)
2064: {
1.113 markus 2065: cleanup_socket();
1.163 markus 2066: #ifdef ENABLE_PKCS11
2067: pkcs11_terminate();
2068: #endif
1.48 deraadt 2069: _exit(2);
1.113 markus 2070: }
2071:
1.68 markus 2072: static void
1.155 dtucker 2073: check_parent_exists(void)
1.68 markus 2074: {
1.172 dtucker 2075: /*
2076: * If our parent has exited then getppid() will return (pid_t)1,
2077: * so testing for that should be safe.
2078: */
2079: if (parent_pid != -1 && getppid() != parent_pid) {
1.68 markus 2080: /* printf("Parent has died - Authentication agent exiting.\n"); */
1.155 dtucker 2081: cleanup_socket();
2082: _exit(2);
1.68 markus 2083: }
1.48 deraadt 2084: }
2085:
1.55 itojun 2086: static void
1.50 itojun 2087: usage(void)
1.15 markus 2088: {
1.184 deraadt 2089: fprintf(stderr,
1.250 jmc 2090: "usage: ssh-agent [-c | -s] [-Dd] [-a bind_address] [-E fingerprint_hash]\n"
1.293 jmc 2091: " [-O option] [-P allowed_providers] [-t life]\n"
2092: " ssh-agent [-a bind_address] [-E fingerprint_hash] [-O option]\n"
2093: " [-P allowed_providers] [-t life] command [arg ...]\n"
1.250 jmc 2094: " ssh-agent [-c | -s] -k\n");
1.21 markus 2095: exit(1);
1.15 markus 2096: }
2097:
1.2 provos 2098: int
2099: main(int ac, char **av)
1.1 deraadt 2100: {
1.201 djm 2101: int c_flag = 0, d_flag = 0, D_flag = 0, k_flag = 0, s_flag = 0;
1.265 djm 2102: int sock, ch, result, saved_errno;
1.96 deraadt 2103: char *shell, *format, *pidstr, *agentsocket = NULL;
1.41 markus 2104: struct rlimit rlim;
1.96 deraadt 2105: extern int optind;
1.98 stevesk 2106: extern char *optarg;
1.21 markus 2107: pid_t pid;
1.96 deraadt 2108: char pidstrbuf[1 + 3 * sizeof pid];
1.161 tobias 2109: size_t len;
1.189 djm 2110: mode_t prev_mask;
1.224 djm 2111: int timeout = -1; /* INFTIM */
1.223 djm 2112: struct pollfd *pfd = NULL;
2113: size_t npfd = 0;
1.231 djm 2114: u_int maxfds;
1.123 djm 2115:
2116: /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
2117: sanitise_stdfd();
1.99 markus 2118:
2119: /* drop */
1.298 dtucker 2120: (void)setegid(getgid());
2121: (void)setgid(getgid());
1.53 markus 2122:
1.231 djm 2123: if (getrlimit(RLIMIT_NOFILE, &rlim) == -1)
2124: fatal("%s: getrlimit: %s", __progname, strerror(errno));
2125:
1.185 markus 2126: #ifdef WITH_OPENSSL
1.170 djm 2127: OpenSSL_add_all_algorithms();
1.185 markus 2128: #endif
1.21 markus 2129:
1.258 djm 2130: while ((ch = getopt(ac, av, "cDdksE:a:O:P:t:")) != -1) {
1.21 markus 2131: switch (ch) {
1.192 djm 2132: case 'E':
2133: fingerprint_hash = ssh_digest_alg_by_name(optarg);
2134: if (fingerprint_hash == -1)
2135: fatal("Invalid hash algorithm \"%s\"", optarg);
2136: break;
1.21 markus 2137: case 'c':
2138: if (s_flag)
2139: usage();
2140: c_flag++;
2141: break;
2142: case 'k':
2143: k_flag++;
1.258 djm 2144: break;
2145: case 'O':
2146: if (strcmp(optarg, "no-restrict-websafe") == 0)
1.300 djm 2147: restrict_websafe = 0;
2148: else if (strcmp(optarg, "allow-remote-pkcs11") == 0)
2149: remote_add_provider = 1;
1.258 djm 2150: else
2151: fatal("Unknown -O option");
1.21 markus 2152: break;
1.215 djm 2153: case 'P':
1.260 djm 2154: if (allowed_providers != NULL)
1.215 djm 2155: fatal("-P option already specified");
1.260 djm 2156: allowed_providers = xstrdup(optarg);
1.215 djm 2157: break;
1.21 markus 2158: case 's':
2159: if (c_flag)
2160: usage();
2161: s_flag++;
2162: break;
1.57 markus 2163: case 'd':
1.201 djm 2164: if (d_flag || D_flag)
1.57 markus 2165: usage();
2166: d_flag++;
2167: break;
1.201 djm 2168: case 'D':
2169: if (d_flag || D_flag)
2170: usage();
2171: D_flag++;
2172: break;
1.86 markus 2173: case 'a':
2174: agentsocket = optarg;
1.106 marc 2175: break;
2176: case 't':
2177: if ((lifetime = convtime(optarg)) == -1) {
2178: fprintf(stderr, "Invalid lifetime\n");
2179: usage();
2180: }
1.86 markus 2181: break;
1.21 markus 2182: default:
2183: usage();
2184: }
2185: }
2186: ac -= optind;
2187: av += optind;
2188:
1.201 djm 2189: if (ac > 0 && (c_flag || k_flag || s_flag || d_flag || D_flag))
1.21 markus 2190: usage();
2191:
1.260 djm 2192: if (allowed_providers == NULL)
1.262 djm 2193: allowed_providers = xstrdup(DEFAULT_ALLOWED_PROVIDERS);
1.215 djm 2194:
1.85 markus 2195: if (ac == 0 && !c_flag && !s_flag) {
1.21 markus 2196: shell = getenv("SHELL");
1.161 tobias 2197: if (shell != NULL && (len = strlen(shell)) > 2 &&
2198: strncmp(shell + len - 3, "csh", 3) == 0)
1.21 markus 2199: c_flag = 1;
2200: }
2201: if (k_flag) {
1.136 deraadt 2202: const char *errstr = NULL;
2203:
1.21 markus 2204: pidstr = getenv(SSH_AGENTPID_ENV_NAME);
2205: if (pidstr == NULL) {
2206: fprintf(stderr, "%s not set, cannot kill agent\n",
1.46 markus 2207: SSH_AGENTPID_ENV_NAME);
1.21 markus 2208: exit(1);
2209: }
1.136 deraadt 2210: pid = (int)strtonum(pidstr, 2, INT_MAX, &errstr);
2211: if (errstr) {
2212: fprintf(stderr,
2213: "%s=\"%s\", which is not a good PID: %s\n",
2214: SSH_AGENTPID_ENV_NAME, pidstr, errstr);
1.21 markus 2215: exit(1);
2216: }
2217: if (kill(pid, SIGTERM) == -1) {
2218: perror("kill");
2219: exit(1);
2220: }
2221: format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1.140 djm 2222: printf(format, SSH_AUTHSOCKET_ENV_NAME);
2223: printf(format, SSH_AGENTPID_ENV_NAME);
1.91 mpech 2224: printf("echo Agent pid %ld killed;\n", (long)pid);
1.21 markus 2225: exit(0);
2226: }
1.231 djm 2227:
2228: /*
2229: * Minimum file descriptors:
2230: * stdio (3) + listener (1) + syslog (1 maybe) + connection (1) +
2231: * a few spare for libc / stack protectors / sanitisers, etc.
2232: */
2233: #define SSH_AGENT_MIN_FDS (3+1+1+1+4)
2234: if (rlim.rlim_cur < SSH_AGENT_MIN_FDS)
1.232 djm 2235: fatal("%s: file descriptor rlimit %lld too low (minimum %u)",
1.231 djm 2236: __progname, (long long)rlim.rlim_cur, SSH_AGENT_MIN_FDS);
2237: maxfds = rlim.rlim_cur - SSH_AGENT_MIN_FDS;
2238:
1.21 markus 2239: parent_pid = getpid();
2240:
1.86 markus 2241: if (agentsocket == NULL) {
2242: /* Create private directory for agent socket */
1.171 djm 2243: mktemp_proto(socket_dir, sizeof(socket_dir));
1.86 markus 2244: if (mkdtemp(socket_dir) == NULL) {
2245: perror("mkdtemp: private socket dir");
2246: exit(1);
2247: }
1.91 mpech 2248: snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
2249: (long)parent_pid);
1.86 markus 2250: } else {
2251: /* Try to use specified agent socket */
2252: socket_dir[0] = '\0';
2253: strlcpy(socket_name, agentsocket, sizeof socket_name);
1.21 markus 2254: }
2255:
1.23 markus 2256: /*
2257: * Create socket early so it will exist before command gets run from
2258: * the parent.
2259: */
1.189 djm 2260: prev_mask = umask(0177);
1.188 millert 2261: sock = unix_listener(socket_name, SSH_LISTEN_BACKLOG, 0);
1.21 markus 2262: if (sock < 0) {
1.188 millert 2263: /* XXX - unix_listener() calls error() not perror() */
1.121 djm 2264: *socket_name = '\0'; /* Don't unlink any existing file */
1.21 markus 2265: cleanup_exit(1);
2266: }
1.189 djm 2267: umask(prev_mask);
1.46 markus 2268:
1.23 markus 2269: /*
2270: * Fork, and have the parent execute the command, if any, or present
2271: * the socket data. The child continues as the authentication agent.
2272: */
1.201 djm 2273: if (D_flag || d_flag) {
2274: log_init(__progname,
2275: d_flag ? SYSLOG_LEVEL_DEBUG3 : SYSLOG_LEVEL_INFO,
2276: SYSLOG_FACILITY_AUTH, 1);
1.57 markus 2277: format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
2278: printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
2279: SSH_AUTHSOCKET_ENV_NAME);
1.91 mpech 2280: printf("echo Agent pid %ld;\n", (long)parent_pid);
1.210 dtucker 2281: fflush(stdout);
1.57 markus 2282: goto skip;
2283: }
1.21 markus 2284: pid = fork();
2285: if (pid == -1) {
2286: perror("fork");
1.81 stevesk 2287: cleanup_exit(1);
1.21 markus 2288: }
2289: if (pid != 0) { /* Parent - execute the given command. */
2290: close(sock);
1.91 mpech 2291: snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
1.21 markus 2292: if (ac == 0) {
2293: format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
2294: printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1.46 markus 2295: SSH_AUTHSOCKET_ENV_NAME);
1.21 markus 2296: printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
1.46 markus 2297: SSH_AGENTPID_ENV_NAME);
1.91 mpech 2298: printf("echo Agent pid %ld;\n", (long)pid);
1.21 markus 2299: exit(0);
2300: }
1.36 deraadt 2301: if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
2302: setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
2303: perror("setenv");
2304: exit(1);
2305: }
1.21 markus 2306: execvp(av[0], av);
2307: perror(av[0]);
2308: exit(1);
2309: }
1.81 stevesk 2310: /* child */
2311: log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
1.67 stevesk 2312:
2313: if (setsid() == -1) {
1.81 stevesk 2314: error("setsid: %s", strerror(errno));
1.67 stevesk 2315: cleanup_exit(1);
2316: }
2317:
2318: (void)chdir("/");
1.265 djm 2319: if (stdfd_devnull(1, 1, 1) == -1)
1.266 djm 2320: error_f("stdfd_devnull failed");
1.21 markus 2321:
1.41 markus 2322: /* deny core dumps, since memory contains unencrypted private keys */
2323: rlim.rlim_cur = rlim.rlim_max = 0;
1.237 deraadt 2324: if (setrlimit(RLIMIT_CORE, &rlim) == -1) {
1.81 stevesk 2325: error("setrlimit RLIMIT_CORE: %s", strerror(errno));
1.21 markus 2326: cleanup_exit(1);
2327: }
1.57 markus 2328:
2329: skip:
1.187 djm 2330:
2331: cleanup_pid = getpid();
1.163 markus 2332:
2333: #ifdef ENABLE_PKCS11
2334: pkcs11_init(0);
2335: #endif
1.21 markus 2336: new_socket(AUTH_SOCKET, sock);
1.155 dtucker 2337: if (ac > 0)
2338: parent_alive_interval = 10;
1.33 markus 2339: idtab_init();
1.252 dtucker 2340: ssh_signal(SIGPIPE, SIG_IGN);
2341: ssh_signal(SIGINT, (d_flag | D_flag) ? cleanup_handler : SIG_IGN);
2342: ssh_signal(SIGHUP, cleanup_handler);
2343: ssh_signal(SIGTERM, cleanup_handler);
1.205 djm 2344:
1.215 djm 2345: if (pledge("stdio rpath cpath unix id proc exec", NULL) == -1)
1.205 djm 2346: fatal("%s: pledge: %s", __progname, strerror(errno));
1.66 markus 2347:
1.21 markus 2348: while (1) {
1.231 djm 2349: prepare_poll(&pfd, &npfd, &timeout, maxfds);
1.223 djm 2350: result = poll(pfd, npfd, timeout);
1.154 dtucker 2351: saved_errno = errno;
1.155 dtucker 2352: if (parent_alive_interval != 0)
2353: check_parent_exists();
2354: (void) reaper(); /* remove expired keys */
1.237 deraadt 2355: if (result == -1) {
1.154 dtucker 2356: if (saved_errno == EINTR)
1.21 markus 2357: continue;
1.223 djm 2358: fatal("poll: %s", strerror(saved_errno));
1.154 dtucker 2359: } else if (result > 0)
1.231 djm 2360: after_poll(pfd, npfd, maxfds);
1.15 markus 2361: }
1.21 markus 2362: /* NOTREACHED */
1.1 deraadt 2363: }