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