Annotation of src/usr.bin/ssh/sshconnect2.c, Revision 1.291
1.291 ! djm 1: /* $OpenBSD: sshconnect2.c,v 1.290 2018/11/28 06:00:38 djm Exp $ */
1.1 markus 2: /*
3: * Copyright (c) 2000 Markus Friedl. All rights reserved.
1.170 djm 4: * Copyright (c) 2008 Damien Miller. All rights reserved.
1.1 markus 5: *
6: * Redistribution and use in source and binary forms, with or without
7: * modification, are permitted provided that the following conditions
8: * are met:
9: * 1. Redistributions of source code must retain the above copyright
10: * notice, this list of conditions and the following disclaimer.
11: * 2. Redistributions in binary form must reproduce the above copyright
12: * notice, this list of conditions and the following disclaimer in the
13: * documentation and/or other materials provided with the distribution.
14: *
15: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25: */
26:
1.145 stevesk 27: #include <sys/types.h>
1.160 deraadt 28: #include <sys/socket.h>
1.145 stevesk 29: #include <sys/wait.h>
1.144 stevesk 30: #include <sys/queue.h>
1.146 stevesk 31: #include <sys/stat.h>
1.156 stevesk 32:
33: #include <errno.h>
1.174 dtucker 34: #include <fcntl.h>
1.164 jolan 35: #include <netdb.h>
1.159 stevesk 36: #include <stdio.h>
1.158 stevesk 37: #include <string.h>
1.160 deraadt 38: #include <signal.h>
39: #include <pwd.h>
1.157 stevesk 40: #include <unistd.h>
1.166 djm 41: #include <vis.h>
1.1 markus 42:
1.160 deraadt 43: #include "xmalloc.h"
1.1 markus 44: #include "ssh.h"
1.37 markus 45: #include "ssh2.h"
1.278 markus 46: #include "sshbuf.h"
1.1 markus 47: #include "packet.h"
48: #include "compat.h"
1.37 markus 49: #include "cipher.h"
1.278 markus 50: #include "sshkey.h"
1.1 markus 51: #include "kex.h"
52: #include "myproposal.h"
53: #include "sshconnect.h"
54: #include "authfile.h"
1.57 provos 55: #include "dh.h"
1.17 markus 56: #include "authfd.h"
1.37 markus 57: #include "log.h"
1.210 millert 58: #include "misc.h"
1.37 markus 59: #include "readconf.h"
1.53 markus 60: #include "match.h"
1.61 markus 61: #include "dispatch.h"
1.68 markus 62: #include "canohost.h"
1.100 markus 63: #include "msg.h"
64: #include "pathnames.h"
1.154 markus 65: #include "uidswap.h"
1.186 djm 66: #include "hostfile.h"
1.214 djm 67: #include "ssherr.h"
1.246 djm 68: #include "utf8.h"
1.22 provos 69:
1.121 markus 70: #ifdef GSSAPI
71: #include "ssh-gss.h"
72: #endif
73:
1.1 markus 74: /* import */
75: extern char *client_version_string;
76: extern char *server_version_string;
77: extern Options options;
78:
79: /*
80: * SSH2 key exchange
81: */
82:
1.32 markus 83: u_char *session_id2 = NULL;
1.120 markus 84: u_int session_id2_len = 0;
1.1 markus 85:
1.61 markus 86: char *xxx_host;
87: struct sockaddr *xxx_hostaddr;
88:
1.76 itojun 89: static int
1.259 markus 90: verify_host_key_callback(struct sshkey *hostkey, struct ssh *ssh)
1.61 markus 91: {
1.75 markus 92: if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
1.83 markus 93: fatal("Host key verification failed.");
1.61 markus 94: return 0;
95: }
96:
1.186 djm 97: static char *
98: order_hostkeyalgs(char *host, struct sockaddr *hostaddr, u_short port)
99: {
100: char *oavail, *avail, *first, *last, *alg, *hostname, *ret;
101: size_t maxlen;
102: struct hostkeys *hostkeys;
103: int ktype;
1.188 djm 104: u_int i;
1.186 djm 105:
106: /* Find all hostkeys for this hostname */
107: get_hostfile_hostname_ipaddr(host, hostaddr, port, &hostname, NULL);
108: hostkeys = init_hostkeys();
1.188 djm 109: for (i = 0; i < options.num_user_hostfiles; i++)
110: load_hostkeys(hostkeys, hostname, options.user_hostfiles[i]);
111: for (i = 0; i < options.num_system_hostfiles; i++)
112: load_hostkeys(hostkeys, hostname, options.system_hostfiles[i]);
1.186 djm 113:
114: oavail = avail = xstrdup(KEX_DEFAULT_PK_ALG);
115: maxlen = strlen(avail) + 1;
116: first = xmalloc(maxlen);
117: last = xmalloc(maxlen);
118: *first = *last = '\0';
119:
120: #define ALG_APPEND(to, from) \
121: do { \
122: if (*to != '\0') \
123: strlcat(to, ",", maxlen); \
124: strlcat(to, from, maxlen); \
125: } while (0)
126:
127: while ((alg = strsep(&avail, ",")) && *alg != '\0') {
1.214 djm 128: if ((ktype = sshkey_type_from_name(alg)) == KEY_UNSPEC)
1.186 djm 129: fatal("%s: unknown alg %s", __func__, alg);
130: if (lookup_key_in_hostkeys_by_type(hostkeys,
1.214 djm 131: sshkey_type_plain(ktype), NULL))
1.186 djm 132: ALG_APPEND(first, alg);
133: else
134: ALG_APPEND(last, alg);
135: }
136: #undef ALG_APPEND
1.216 djm 137: xasprintf(&ret, "%s%s%s", first,
138: (*first == '\0' || *last == '\0') ? "" : ",", last);
1.186 djm 139: if (*first != '\0')
140: debug3("%s: prefer hostkeyalgs: %s", __func__, first);
141:
1.197 djm 142: free(first);
143: free(last);
144: free(hostname);
145: free(oavail);
1.186 djm 146: free_hostkeys(hostkeys);
147:
148: return ret;
149: }
150:
1.1 markus 151: void
1.291 ! djm 152: ssh_kex2(struct ssh *ssh, char *host, struct sockaddr *hostaddr, u_short port)
1.22 provos 153: {
1.205 markus 154: char *myproposal[PROPOSAL_MAX] = { KEX_CLIENT };
1.275 djm 155: char *s, *all_key;
1.221 markus 156: int r;
1.61 markus 157:
158: xxx_host = host;
159: xxx_hostaddr = hostaddr;
1.22 provos 160:
1.231 markus 161: if ((s = kex_names_cat(options.kex_algorithms, "ext-info-c")) == NULL)
162: fatal("%s: kex_names_cat", __func__);
1.284 djm 163: myproposal[PROPOSAL_KEX_ALGS] = compat_kex_proposal(s);
1.60 stevesk 164: myproposal[PROPOSAL_ENC_ALGS_CTOS] =
1.284 djm 165: compat_cipher_proposal(options.ciphers);
1.60 stevesk 166: myproposal[PROPOSAL_ENC_ALGS_STOC] =
1.284 djm 167: compat_cipher_proposal(options.ciphers);
1.245 dtucker 168: myproposal[PROPOSAL_COMP_ALGS_CTOS] =
169: myproposal[PROPOSAL_COMP_ALGS_STOC] = options.compression ?
1.277 sf 170: "zlib@openssh.com,zlib,none" : "none,zlib@openssh.com,zlib";
1.226 djm 171: myproposal[PROPOSAL_MAC_ALGS_CTOS] =
172: myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
173: if (options.hostkeyalgorithms != NULL) {
1.275 djm 174: all_key = sshkey_alg_list(0, 0, 1, ',');
175: if (kex_assemble_names(&options.hostkeyalgorithms,
176: KEX_DEFAULT_PK_ALG, all_key) != 0)
1.226 djm 177: fatal("%s: kex_assemble_namelist", __func__);
1.275 djm 178: free(all_key);
1.88 deraadt 179: myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
1.284 djm 180: compat_pkalg_proposal(options.hostkeyalgorithms);
1.226 djm 181: } else {
1.225 markus 182: /* Enforce default */
183: options.hostkeyalgorithms = xstrdup(KEX_DEFAULT_PK_ALG);
1.186 djm 184: /* Prefer algorithms that we already have keys for */
185: myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
1.200 djm 186: compat_pkalg_proposal(
1.284 djm 187: order_hostkeyalgs(host, hostaddr, port));
1.186 djm 188: }
1.115 markus 189:
1.196 dtucker 190: if (options.rekey_limit || options.rekey_interval)
1.254 dtucker 191: packet_set_rekey_limits(options.rekey_limit,
192: options.rekey_interval);
1.22 provos 193:
1.65 markus 194: /* start key exchange */
1.291 ! djm 195: if ((r = kex_setup(ssh, myproposal)) != 0)
1.221 markus 196: fatal("kex_setup: %s", ssh_err(r));
1.207 markus 197: #ifdef WITH_OPENSSL
1.291 ! djm 198: ssh->kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
! 199: ssh->kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
! 200: ssh->kex->kex[KEX_DH_GRP14_SHA256] = kexdh_client;
! 201: ssh->kex->kex[KEX_DH_GRP16_SHA512] = kexdh_client;
! 202: ssh->kex->kex[KEX_DH_GRP18_SHA512] = kexdh_client;
! 203: ssh->kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
! 204: ssh->kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
! 205: ssh->kex->kex[KEX_ECDH_SHA2] = kexecdh_client;
1.207 markus 206: #endif
1.291 ! djm 207: ssh->kex->kex[KEX_C25519_SHA256] = kexc25519_client;
! 208: ssh->kex->verify_host_key=&verify_host_key_callback;
1.22 provos 209:
1.291 ! djm 210: ssh_dispatch_run_fatal(ssh, DISPATCH_BLOCK, &ssh->kex->done);
1.173 andreas 211:
1.231 markus 212: /* remove ext-info from the KEX proposals for rekeying */
213: myproposal[PROPOSAL_KEX_ALGS] =
1.284 djm 214: compat_kex_proposal(options.kex_algorithms);
1.291 ! djm 215: if ((r = kex_prop2buf(ssh->kex->my, myproposal)) != 0)
1.231 markus 216: fatal("kex_prop2buf: %s", ssh_err(r));
1.62 markus 217:
1.291 ! djm 218: session_id2 = ssh->kex->session_id;
! 219: session_id2_len = ssh->kex->session_id_len;
1.22 provos 220:
221: #ifdef DEBUG_KEXDH
222: /* send 1st encrypted/maced/compressed message */
1.278 markus 223: if ((r = sshpkt_start(ssh, SSH2_MSG_IGNORE)) != 0 ||
224: (r = sshpkt_put_cstring(ssh, "markus")) != 0 ||
1.280 markus 225: (r = sshpkt_send(ssh)) != 0 ||
226: (r = ssh_packet_write_wait(ssh)) != 0)
1.278 markus 227: fatal("%s: %s", __func__, ssh_err(r));
1.22 provos 228: #endif
1.1 markus 229: }
1.11 markus 230:
1.1 markus 231: /*
232: * Authenticate user
233: */
1.20 markus 234:
1.214 djm 235: typedef struct cauthctxt Authctxt;
236: typedef struct cauthmethod Authmethod;
1.117 markus 237: typedef struct identity Identity;
238: typedef struct idlist Idlist;
1.20 markus 239:
1.117 markus 240: struct identity {
241: TAILQ_ENTRY(identity) next;
1.214 djm 242: int agent_fd; /* >=0 if agent supports key */
243: struct sshkey *key; /* public/private key */
1.117 markus 244: char *filename; /* comment for agent-only keys */
245: int tried;
246: int isprivate; /* key points to the private key */
1.191 dtucker 247: int userprovided;
1.117 markus 248: };
249: TAILQ_HEAD(idlist, identity);
1.20 markus 250:
1.214 djm 251: struct cauthctxt {
1.20 markus 252: const char *server_user;
1.68 markus 253: const char *local_user;
1.20 markus 254: const char *host;
255: const char *service;
1.214 djm 256: struct cauthmethod *method;
1.183 djm 257: sig_atomic_t success;
1.51 markus 258: char *authlist;
1.214 djm 259: int attempt;
1.68 markus 260: /* pubkey */
1.214 djm 261: struct idlist keys;
262: int agent_fd;
1.68 markus 263: /* hostbased */
1.100 markus 264: Sensitive *sensitive;
1.223 djm 265: char *oktypes, *ktypes;
266: const char *active_ktype;
1.82 markus 267: /* kbd-interactive */
268: int info_req_seen;
1.121 markus 269: /* generic */
270: void *methoddata;
1.20 markus 271: };
1.214 djm 272:
273: struct cauthmethod {
1.20 markus 274: char *name; /* string to compare against server's list */
275: int (*userauth)(Authctxt *authctxt);
1.170 djm 276: void (*cleanup)(Authctxt *authctxt);
1.20 markus 277: int *enabled; /* flag in option struct that enables method */
278: int *batch_flag; /* flag in option struct that disables method */
279: };
280:
1.261 markus 281: int input_userauth_service_accept(int, u_int32_t, struct ssh *);
282: int input_userauth_ext_info(int, u_int32_t, struct ssh *);
283: int input_userauth_success(int, u_int32_t, struct ssh *);
284: int input_userauth_success_unexpected(int, u_int32_t, struct ssh *);
285: int input_userauth_failure(int, u_int32_t, struct ssh *);
286: int input_userauth_banner(int, u_int32_t, struct ssh *);
287: int input_userauth_error(int, u_int32_t, struct ssh *);
288: int input_userauth_info_req(int, u_int32_t, struct ssh *);
289: int input_userauth_pk_ok(int, u_int32_t, struct ssh *);
290: int input_userauth_passwd_changereq(int, u_int32_t, struct ssh *);
1.86 itojun 291:
292: int userauth_none(Authctxt *);
293: int userauth_pubkey(Authctxt *);
294: int userauth_passwd(Authctxt *);
295: int userauth_kbdint(Authctxt *);
296: int userauth_hostbased(Authctxt *);
1.20 markus 297:
1.121 markus 298: #ifdef GSSAPI
299: int userauth_gssapi(Authctxt *authctxt);
1.261 markus 300: int input_gssapi_response(int type, u_int32_t, struct ssh *);
301: int input_gssapi_token(int type, u_int32_t, struct ssh *);
302: int input_gssapi_hash(int type, u_int32_t, struct ssh *);
303: int input_gssapi_error(int, u_int32_t, struct ssh *);
304: int input_gssapi_errtok(int, u_int32_t, struct ssh *);
1.121 markus 305: #endif
306:
1.86 itojun 307: void userauth(Authctxt *, char *);
1.51 markus 308:
1.272 djm 309: static int sign_and_send_pubkey(struct ssh *ssh, Authctxt *, Identity *);
1.117 markus 310: static void pubkey_prepare(Authctxt *);
311: static void pubkey_cleanup(Authctxt *);
1.251 djm 312: static void pubkey_reset(Authctxt *);
1.259 markus 313: static struct sshkey *load_identity_file(Identity *);
1.76 itojun 314:
315: static Authmethod *authmethod_get(char *authlist);
316: static Authmethod *authmethod_lookup(const char *name);
317: static char *authmethods_get(void);
1.20 markus 318:
319: Authmethod authmethods[] = {
1.121 markus 320: #ifdef GSSAPI
1.132 markus 321: {"gssapi-with-mic",
1.121 markus 322: userauth_gssapi,
1.170 djm 323: NULL,
1.121 markus 324: &options.gss_authentication,
325: NULL},
326: #endif
1.81 markus 327: {"hostbased",
328: userauth_hostbased,
1.170 djm 329: NULL,
1.81 markus 330: &options.hostbased_authentication,
331: NULL},
1.20 markus 332: {"publickey",
333: userauth_pubkey,
1.170 djm 334: NULL,
1.28 markus 335: &options.pubkey_authentication,
1.20 markus 336: NULL},
1.81 markus 337: {"keyboard-interactive",
338: userauth_kbdint,
1.170 djm 339: NULL,
1.81 markus 340: &options.kbd_interactive_authentication,
341: &options.batch_mode},
1.20 markus 342: {"password",
343: userauth_passwd,
1.170 djm 344: NULL,
1.20 markus 345: &options.password_authentication,
1.23 markus 346: &options.batch_mode},
347: {"none",
348: userauth_none,
349: NULL,
1.170 djm 350: NULL,
1.23 markus 351: NULL},
1.170 djm 352: {NULL, NULL, NULL, NULL, NULL}
1.20 markus 353: };
354:
355: void
1.291 ! djm 356: ssh_userauth2(struct ssh *ssh, const char *local_user,
! 357: const char *server_user, char *host, Sensitive *sensitive)
1.20 markus 358: {
359: Authctxt authctxt;
1.231 markus 360: int r;
1.39 markus 361:
1.73 markus 362: if (options.challenge_response_authentication)
1.39 markus 363: options.kbd_interactive_authentication = 1;
1.53 markus 364: if (options.preferred_authentications == NULL)
365: options.preferred_authentications = authmethods_get();
366:
1.20 markus 367: /* setup authentication context */
1.82 markus 368: memset(&authctxt, 0, sizeof(authctxt));
1.20 markus 369: authctxt.server_user = server_user;
1.68 markus 370: authctxt.local_user = local_user;
1.20 markus 371: authctxt.host = host;
372: authctxt.service = "ssh-connection"; /* service name */
373: authctxt.success = 0;
1.23 markus 374: authctxt.method = authmethod_lookup("none");
1.51 markus 375: authctxt.authlist = NULL;
1.121 markus 376: authctxt.methoddata = NULL;
1.100 markus 377: authctxt.sensitive = sensitive;
1.223 djm 378: authctxt.active_ktype = authctxt.oktypes = authctxt.ktypes = NULL;
1.82 markus 379: authctxt.info_req_seen = 0;
1.215 djm 380: authctxt.agent_fd = -1;
1.289 djm 381: pubkey_prepare(&authctxt);
1.291 ! djm 382: if (authctxt.method == NULL) {
! 383: fatal("%s: internal error: cannot send userauth none request",
! 384: __func__);
! 385: }
1.20 markus 386:
1.231 markus 387: if ((r = sshpkt_start(ssh, SSH2_MSG_SERVICE_REQUEST)) != 0 ||
388: (r = sshpkt_put_cstring(ssh, "ssh-userauth")) != 0 ||
389: (r = sshpkt_send(ssh)) != 0)
390: fatal("%s: %s", __func__, ssh_err(r));
391:
1.260 markus 392: ssh->authctxt = &authctxt;
1.231 markus 393: ssh_dispatch_init(ssh, &input_userauth_error);
394: ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, &input_userauth_ext_info);
395: ssh_dispatch_set(ssh, SSH2_MSG_SERVICE_ACCEPT, &input_userauth_service_accept);
1.263 markus 396: ssh_dispatch_run_fatal(ssh, DISPATCH_BLOCK, &authctxt.success); /* loop until success */
1.260 markus 397: ssh->authctxt = NULL;
1.20 markus 398:
1.117 markus 399: pubkey_cleanup(&authctxt);
1.231 markus 400: ssh_dispatch_range(ssh, SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
1.119 markus 401:
1.248 dtucker 402: if (!authctxt.success)
403: fatal("Authentication failed.");
1.109 markus 404: debug("Authentication succeeded (%s).", authctxt.method->name);
1.20 markus 405: }
1.119 markus 406:
1.231 markus 407: /* ARGSUSED */
408: int
1.261 markus 409: input_userauth_service_accept(int type, u_int32_t seq, struct ssh *ssh)
1.231 markus 410: {
1.260 markus 411: Authctxt *authctxt = ssh->authctxt;
1.231 markus 412: int r;
413:
414: if (ssh_packet_remaining(ssh) > 0) {
415: char *reply;
416:
417: if ((r = sshpkt_get_cstring(ssh, &reply, NULL)) != 0)
418: goto out;
419: debug2("service_accept: %s", reply);
420: free(reply);
421: } else {
422: debug2("buggy server: service_accept w/o service");
423: }
424: if ((r = sshpkt_get_end(ssh)) != 0)
425: goto out;
426: debug("SSH2_MSG_SERVICE_ACCEPT received");
427:
428: /* initial userauth request */
429: userauth_none(authctxt);
430:
431: ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, &input_userauth_error);
432: ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
433: ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
434: ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
435: r = 0;
436: out:
437: return r;
438: }
439:
440: /* ARGSUSED */
441: int
1.261 markus 442: input_userauth_ext_info(int type, u_int32_t seqnr, struct ssh *ssh)
1.231 markus 443: {
1.261 markus 444: return kex_input_ext_info(type, seqnr, ssh);
1.231 markus 445: }
446:
1.20 markus 447: void
1.51 markus 448: userauth(Authctxt *authctxt, char *authlist)
449: {
1.278 markus 450: struct ssh *ssh = active_state; /* XXX */
451:
1.170 djm 452: if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
453: authctxt->method->cleanup(authctxt);
454:
1.197 djm 455: free(authctxt->methoddata);
456: authctxt->methoddata = NULL;
1.51 markus 457: if (authlist == NULL) {
458: authlist = authctxt->authlist;
459: } else {
1.197 djm 460: free(authctxt->authlist);
1.51 markus 461: authctxt->authlist = authlist;
462: }
463: for (;;) {
464: Authmethod *method = authmethod_get(authlist);
465: if (method == NULL)
1.264 dtucker 466: fatal("%s@%s: Permission denied (%s).",
467: authctxt->server_user, authctxt->host, authlist);
1.51 markus 468: authctxt->method = method;
1.119 markus 469:
470: /* reset the per method handler */
1.278 markus 471: ssh_dispatch_range(ssh, SSH2_MSG_USERAUTH_PER_METHOD_MIN,
1.119 markus 472: SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
473:
474: /* and try new method */
1.51 markus 475: if (method->userauth(authctxt) != 0) {
476: debug2("we sent a %s packet, wait for reply", method->name);
477: break;
478: } else {
479: debug2("we did not send a packet, disable method");
480: method->enabled = NULL;
481: }
482: }
483: }
1.105 deraadt 484:
1.169 djm 485: /* ARGSUSED */
1.218 markus 486: int
1.261 markus 487: input_userauth_error(int type, u_int32_t seq, struct ssh *ssh)
1.20 markus 488: {
1.35 markus 489: fatal("input_userauth_error: bad message during authentication: "
1.140 djm 490: "type %d", type);
1.218 markus 491: return 0;
1.35 markus 492: }
1.105 deraadt 493:
1.169 djm 494: /* ARGSUSED */
1.218 markus 495: int
1.261 markus 496: input_userauth_banner(int type, u_int32_t seq, struct ssh *ssh)
1.35 markus 497: {
1.246 djm 498: char *msg, *lang;
1.166 djm 499: u_int len;
1.126 deraadt 500:
1.246 djm 501: debug3("%s", __func__);
502: msg = packet_get_string(&len);
1.35 markus 503: lang = packet_get_string(NULL);
1.246 djm 504: if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO)
505: fmprintf(stderr, "%s", msg);
506: free(msg);
1.197 djm 507: free(lang);
1.218 markus 508: return 0;
1.20 markus 509: }
1.105 deraadt 510:
1.169 djm 511: /* ARGSUSED */
1.218 markus 512: int
1.261 markus 513: input_userauth_success(int type, u_int32_t seq, struct ssh *ssh)
1.20 markus 514: {
1.260 markus 515: Authctxt *authctxt = ssh->authctxt;
1.172 djm 516:
1.20 markus 517: if (authctxt == NULL)
518: fatal("input_userauth_success: no authentication context");
1.197 djm 519: free(authctxt->authlist);
520: authctxt->authlist = NULL;
1.172 djm 521: if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
522: authctxt->method->cleanup(authctxt);
1.197 djm 523: free(authctxt->methoddata);
524: authctxt->methoddata = NULL;
1.20 markus 525: authctxt->success = 1; /* break out */
1.218 markus 526: return 0;
1.20 markus 527: }
1.105 deraadt 528:
1.218 markus 529: int
1.261 markus 530: input_userauth_success_unexpected(int type, u_int32_t seq, struct ssh *ssh)
1.172 djm 531: {
1.260 markus 532: Authctxt *authctxt = ssh->authctxt;
1.172 djm 533:
534: if (authctxt == NULL)
535: fatal("%s: no authentication context", __func__);
536:
537: fatal("Unexpected authentication success during %s.",
538: authctxt->method->name);
1.218 markus 539: return 0;
1.172 djm 540: }
541:
1.169 djm 542: /* ARGSUSED */
1.218 markus 543: int
1.261 markus 544: input_userauth_failure(int type, u_int32_t seq, struct ssh *ssh)
1.20 markus 545: {
1.260 markus 546: Authctxt *authctxt = ssh->authctxt;
1.20 markus 547: char *authlist = NULL;
1.278 markus 548: u_char partial;
549: int r;
1.20 markus 550:
551: if (authctxt == NULL)
552: fatal("input_userauth_failure: no authentication context");
553:
1.278 markus 554: if ((r = sshpkt_get_cstring(ssh, &authlist, NULL)) != 0 ||
555: (r = sshpkt_get_u8(ssh, &partial)) != 0 ||
556: (r = sshpkt_get_end(ssh)) != 0)
557: goto out;
1.20 markus 558:
1.193 markus 559: if (partial != 0) {
1.247 dtucker 560: verbose("Authenticated with partial success.");
1.193 markus 561: /* reset state */
1.251 djm 562: pubkey_reset(authctxt);
1.193 markus 563: }
1.109 markus 564: debug("Authentications that can continue: %s", authlist);
1.20 markus 565:
1.51 markus 566: userauth(authctxt, authlist);
1.278 markus 567: authlist = NULL;
568: out:
569: free(authlist);
1.218 markus 570: return 0;
1.51 markus 571: }
1.169 djm 572:
1.287 djm 573: /*
574: * Format an identity for logging including filename, key type, fingerprint
575: * and location (agent, etc.). Caller must free.
576: */
577: static char *
578: format_identity(Identity *id)
579: {
580: char *fp = NULL, *ret = NULL;
581:
582: if (id->key != NULL) {
583: fp = sshkey_fingerprint(id->key, options.fingerprint_hash,
584: SSH_FP_DEFAULT);
585: }
586: xasprintf(&ret, "%s %s%s%s%s%s%s",
587: id->filename,
588: id->key ? sshkey_type(id->key) : "", id->key ? " " : "",
589: fp ? fp : "",
590: id->userprovided ? " explicit" : "",
591: (id->key && (id->key->flags & SSHKEY_FLAG_EXT)) ? " token" : "",
592: id->agent_fd != -1 ? " agent" : "");
593: free(fp);
594: return ret;
595: }
596:
1.169 djm 597: /* ARGSUSED */
1.218 markus 598: int
1.261 markus 599: input_userauth_pk_ok(int type, u_int32_t seq, struct ssh *ssh)
1.51 markus 600: {
1.260 markus 601: Authctxt *authctxt = ssh->authctxt;
1.259 markus 602: struct sshkey *key = NULL;
1.117 markus 603: Identity *id = NULL;
1.287 djm 604: int pktype, found = 0, sent = 0;
1.278 markus 605: size_t blen;
1.287 djm 606: char *pkalg = NULL, *fp = NULL, *ident = NULL;
1.278 markus 607: u_char *pkblob = NULL;
608: int r;
1.51 markus 609:
610: if (authctxt == NULL)
611: fatal("input_userauth_pk_ok: no authentication context");
1.267 djm 612:
1.278 markus 613: if ((r = sshpkt_get_cstring(ssh, &pkalg, NULL)) != 0 ||
614: (r = sshpkt_get_string(ssh, &pkblob, &blen)) != 0 ||
615: (r = sshpkt_get_end(ssh)) != 0)
616: goto done;
1.51 markus 617:
1.278 markus 618: if ((pktype = sshkey_type_from_name(pkalg)) == KEY_UNSPEC) {
1.287 djm 619: debug("%s: server sent unknown pkalg %s", __func__, pkalg);
1.117 markus 620: goto done;
621: }
1.278 markus 622: if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) {
623: debug("no key from blob. pkalg %s: %s", pkalg, ssh_err(r));
1.117 markus 624: goto done;
625: }
626: if (key->type != pktype) {
627: error("input_userauth_pk_ok: type mismatch "
628: "for decoded key (received %d, expected %d)",
629: key->type, pktype);
630: goto done;
631: }
632:
1.127 markus 633: /*
634: * search keys in the reverse order, because last candidate has been
635: * moved to the end of the queue. this also avoids confusion by
636: * duplicate keys
637: */
1.136 henning 638: TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
1.278 markus 639: if (sshkey_equal(key, id->key)) {
1.287 djm 640: found = 1;
1.51 markus 641: break;
642: }
1.117 markus 643: }
1.287 djm 644: if (!found || id == NULL) {
645: fp = sshkey_fingerprint(key, options.fingerprint_hash,
646: SSH_FP_DEFAULT);
647: error("%s: server replied with unknown key: %s %s", __func__,
648: sshkey_type(key), fp == NULL ? "<ERROR>" : fp);
649: goto done;
650: }
651: ident = format_identity(id);
652: debug("Server accepts key: %s", ident);
653: sent = sign_and_send_pubkey(ssh, authctxt, id);
1.278 markus 654: r = 0;
655: done:
656: sshkey_free(key);
1.287 djm 657: free(ident);
658: free(fp);
1.197 djm 659: free(pkalg);
660: free(pkblob);
1.51 markus 661:
1.106 deraadt 662: /* try another method if we did not send a packet */
1.278 markus 663: if (r == 0 && sent == 0)
1.51 markus 664: userauth(authctxt, NULL);
1.278 markus 665: return r;
1.20 markus 666: }
1.121 markus 667:
668: #ifdef GSSAPI
1.133 djm 669: int
1.121 markus 670: userauth_gssapi(Authctxt *authctxt)
671: {
1.278 markus 672: struct ssh *ssh = active_state; /* XXX */
1.121 markus 673: Gssctxt *gssctxt = NULL;
1.128 avsm 674: static gss_OID_set gss_supported = NULL;
1.139 djm 675: static u_int mech = 0;
1.121 markus 676: OM_uint32 min;
1.278 markus 677: int r, ok = 0;
1.121 markus 678:
679: /* Try one GSSAPI method at a time, rather than sending them all at
680: * once. */
681:
1.128 avsm 682: if (gss_supported == NULL)
683: gss_indicate_mechs(&min, &gss_supported);
1.121 markus 684:
685: /* Check to see if the mechanism is usable before we offer it */
1.128 avsm 686: while (mech < gss_supported->count && !ok) {
1.121 markus 687: /* My DER encoding requires length<128 */
1.128 avsm 688: if (gss_supported->elements[mech].length < 128 &&
1.271 djm 689: ssh_gssapi_check_mechanism(&gssctxt,
1.161 djm 690: &gss_supported->elements[mech], authctxt->host)) {
1.121 markus 691: ok = 1; /* Mechanism works */
692: } else {
693: mech++;
694: }
695: }
696:
1.161 djm 697: if (!ok)
1.139 djm 698: return 0;
1.121 markus 699:
700: authctxt->methoddata=(void *)gssctxt;
701:
1.278 markus 702: if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
703: (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
704: (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
705: (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
706: (r = sshpkt_put_u32(ssh, 1)) != 0 ||
707: (r = sshpkt_put_u32(ssh,
708: (gss_supported->elements[mech].length) + 2)) != 0 ||
709: (r = sshpkt_put_u8(ssh, SSH_GSS_OIDTYPE)) != 0 ||
710: (r = sshpkt_put_u8(ssh,
711: gss_supported->elements[mech].length)) != 0 ||
712: (r = sshpkt_put(ssh,
713: gss_supported->elements[mech].elements,
714: gss_supported->elements[mech].length)) != 0 ||
715: (r = sshpkt_send(ssh)) != 0)
716: fatal("%s: %s", __func__, ssh_err(r));
717:
718: ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
719: ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
720: ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
721: ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
1.121 markus 722:
723: mech++; /* Move along to next candidate */
724:
725: return 1;
726: }
727:
1.130 markus 728: static OM_uint32
1.262 djm 729: process_gssapi_token(struct ssh *ssh, gss_buffer_t recv_tok)
1.130 markus 730: {
1.260 markus 731: Authctxt *authctxt = ssh->authctxt;
1.130 markus 732: Gssctxt *gssctxt = authctxt->methoddata;
733: gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
1.142 djm 734: gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
735: gss_buffer_desc gssbuf;
1.132 markus 736: OM_uint32 status, ms, flags;
1.278 markus 737: int r;
1.133 djm 738:
1.130 markus 739: status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
1.132 markus 740: recv_tok, &send_tok, &flags);
1.130 markus 741:
742: if (send_tok.length > 0) {
1.278 markus 743: u_char type = GSS_ERROR(status) ?
744: SSH2_MSG_USERAUTH_GSSAPI_ERRTOK :
745: SSH2_MSG_USERAUTH_GSSAPI_TOKEN;
746:
747: if ((r = sshpkt_start(ssh, type)) != 0 ||
748: (r = sshpkt_put_string(ssh, send_tok.value,
749: send_tok.length)) != 0 ||
750: (r = sshpkt_send(ssh)) != 0)
751: fatal("%s: %s", __func__, ssh_err(r));
1.133 djm 752:
1.130 markus 753: gss_release_buffer(&ms, &send_tok);
754: }
1.133 djm 755:
1.130 markus 756: if (status == GSS_S_COMPLETE) {
1.132 markus 757: /* send either complete or MIC, depending on mechanism */
758: if (!(flags & GSS_C_INTEG_FLAG)) {
1.278 markus 759: if ((r = sshpkt_start(ssh,
760: SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE)) != 0 ||
761: (r = sshpkt_send(ssh)) != 0)
762: fatal("%s: %s", __func__, ssh_err(r));
1.132 markus 763: } else {
1.278 markus 764: struct sshbuf *b;
765:
766: if ((b = sshbuf_new()) == NULL)
767: fatal("%s: sshbuf_new failed", __func__);
768: ssh_gssapi_buildmic(b, authctxt->server_user,
1.132 markus 769: authctxt->service, "gssapi-with-mic");
770:
1.278 markus 771: if ((gssbuf.value = sshbuf_mutable_ptr(b)) == NULL)
772: fatal("%s: sshbuf_mutable_ptr failed", __func__);
773: gssbuf.length = sshbuf_len(b);
1.133 djm 774:
1.132 markus 775: status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
1.133 djm 776:
1.132 markus 777: if (!GSS_ERROR(status)) {
1.278 markus 778: if ((r = sshpkt_start(ssh,
779: SSH2_MSG_USERAUTH_GSSAPI_MIC)) != 0 ||
780: (r = sshpkt_put_string(ssh, mic.value,
781: mic.length)) != 0 ||
782: (r = sshpkt_send(ssh)) != 0)
783: fatal("%s: %s", __func__, ssh_err(r));
1.132 markus 784: }
1.133 djm 785:
1.278 markus 786: sshbuf_free(b);
1.132 markus 787: gss_release_buffer(&ms, &mic);
1.133 djm 788: }
1.130 markus 789: }
1.133 djm 790:
1.130 markus 791: return status;
792: }
793:
1.169 djm 794: /* ARGSUSED */
1.220 djm 795: int
1.261 markus 796: input_gssapi_response(int type, u_int32_t plen, struct ssh *ssh)
1.121 markus 797: {
1.260 markus 798: Authctxt *authctxt = ssh->authctxt;
1.121 markus 799: Gssctxt *gssctxt;
1.278 markus 800: size_t oidlen;
801: u_char *oidv = NULL;
802: int r;
1.121 markus 803:
804: if (authctxt == NULL)
805: fatal("input_gssapi_response: no authentication context");
806: gssctxt = authctxt->methoddata;
807:
808: /* Setup our OID */
1.278 markus 809: if ((r = sshpkt_get_string(ssh, &oidv, &oidlen)) != 0)
810: goto done;
1.121 markus 811:
1.129 markus 812: if (oidlen <= 2 ||
813: oidv[0] != SSH_GSS_OIDTYPE ||
814: oidv[1] != oidlen - 2) {
815: debug("Badly encoded mechanism OID received");
816: userauth(authctxt, NULL);
1.278 markus 817: goto ok;
1.121 markus 818: }
1.129 markus 819:
820: if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
821: fatal("Server returned different OID than expected");
1.121 markus 822:
1.278 markus 823: if ((r = sshpkt_get_end(ssh)) != 0)
824: goto done;
1.121 markus 825:
1.262 djm 826: if (GSS_ERROR(process_gssapi_token(ssh, GSS_C_NO_BUFFER))) {
1.121 markus 827: /* Start again with next method on list */
828: debug("Trying to start again");
829: userauth(authctxt, NULL);
1.278 markus 830: goto ok;
1.121 markus 831: }
1.278 markus 832: ok:
833: r = 0;
834: done:
835: free(oidv);
836: return r;
1.121 markus 837: }
838:
1.169 djm 839: /* ARGSUSED */
1.220 djm 840: int
1.261 markus 841: input_gssapi_token(int type, u_int32_t plen, struct ssh *ssh)
1.121 markus 842: {
1.260 markus 843: Authctxt *authctxt = ssh->authctxt;
1.121 markus 844: gss_buffer_desc recv_tok;
1.278 markus 845: u_char *p = NULL;
846: size_t len;
1.130 markus 847: OM_uint32 status;
1.278 markus 848: int r;
1.121 markus 849:
850: if (authctxt == NULL)
851: fatal("input_gssapi_response: no authentication context");
852:
1.278 markus 853: if ((r = sshpkt_get_string(ssh, &p, &len)) != 0 ||
854: (r = sshpkt_get_end(ssh)) != 0)
855: goto out;
1.121 markus 856:
1.278 markus 857: recv_tok.value = p;
858: recv_tok.length = len;
1.262 djm 859: status = process_gssapi_token(ssh, &recv_tok);
1.121 markus 860:
1.278 markus 861: /* Start again with the next method in the list */
1.121 markus 862: if (GSS_ERROR(status)) {
863: userauth(authctxt, NULL);
1.278 markus 864: /* ok */
1.121 markus 865: }
1.278 markus 866: r = 0;
867: out:
868: free(p);
869: return r;
1.121 markus 870: }
871:
1.169 djm 872: /* ARGSUSED */
1.220 djm 873: int
1.261 markus 874: input_gssapi_errtok(int type, u_int32_t plen, struct ssh *ssh)
1.121 markus 875: {
1.260 markus 876: Authctxt *authctxt = ssh->authctxt;
1.121 markus 877: Gssctxt *gssctxt;
878: gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
879: gss_buffer_desc recv_tok;
1.194 djm 880: OM_uint32 ms;
1.278 markus 881: u_char *p = NULL;
882: size_t len;
883: int r;
1.121 markus 884:
885: if (authctxt == NULL)
886: fatal("input_gssapi_response: no authentication context");
887: gssctxt = authctxt->methoddata;
888:
1.278 markus 889: if ((r = sshpkt_get_string(ssh, &p, &len)) != 0 ||
890: (r = sshpkt_get_end(ssh)) != 0) {
891: free(p);
892: return r;
893: }
1.121 markus 894:
895: /* Stick it into GSSAPI and see what it says */
1.278 markus 896: recv_tok.value = p;
897: recv_tok.length = len;
1.194 djm 898: (void)ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
1.140 djm 899: &recv_tok, &send_tok, NULL);
1.278 markus 900: free(p);
1.121 markus 901: gss_release_buffer(&ms, &send_tok);
902:
903: /* Server will be returning a failed packet after this one */
1.220 djm 904: return 0;
1.121 markus 905: }
906:
1.169 djm 907: /* ARGSUSED */
1.220 djm 908: int
1.261 markus 909: input_gssapi_error(int type, u_int32_t plen, struct ssh *ssh)
1.121 markus 910: {
1.278 markus 911: char *msg = NULL;
912: char *lang = NULL;
913: int r;
1.121 markus 914:
1.278 markus 915: if ((r = sshpkt_get_u32(ssh, NULL)) != 0 || /* maj */
916: (r = sshpkt_get_u32(ssh, NULL)) != 0 || /* min */
917: (r = sshpkt_get_cstring(ssh, &msg, NULL)) != 0 ||
918: (r = sshpkt_get_cstring(ssh, &lang, NULL)) != 0)
919: goto out;
920: r = sshpkt_get_end(ssh);
1.143 stevesk 921: debug("Server GSSAPI Error:\n%s", msg);
1.278 markus 922: out:
1.197 djm 923: free(msg);
924: free(lang);
1.278 markus 925: return r;
1.121 markus 926: }
927: #endif /* GSSAPI */
1.20 markus 928:
1.1 markus 929: int
1.23 markus 930: userauth_none(Authctxt *authctxt)
931: {
1.278 markus 932: struct ssh *ssh = active_state; /* XXX */
933: int r;
934:
1.23 markus 935: /* initial userauth request */
1.278 markus 936: if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
937: (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
938: (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
939: (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
940: (r = sshpkt_send(ssh)) != 0)
941: fatal("%s: %s", __func__, ssh_err(r));
1.23 markus 942: return 1;
943: }
944:
945: int
1.20 markus 946: userauth_passwd(Authctxt *authctxt)
1.1 markus 947: {
1.278 markus 948: struct ssh *ssh = active_state; /* XXX */
1.6 markus 949: static int attempt = 0;
1.290 djm 950: char *password, *prompt = NULL;
1.175 dtucker 951: const char *host = options.host_key_alias ? options.host_key_alias :
952: authctxt->host;
1.278 markus 953: int r;
1.6 markus 954:
1.13 todd 955: if (attempt++ >= options.number_of_password_prompts)
1.6 markus 956: return 0;
1.13 todd 957:
1.87 deraadt 958: if (attempt != 1)
1.13 todd 959: error("Permission denied, please try again.");
1.1 markus 960:
1.290 djm 961: xasprintf(&prompt, "%s@%s's password: ", authctxt->server_user, host);
1.1 markus 962: password = read_passphrase(prompt, 0);
1.278 markus 963: if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
964: (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
965: (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
966: (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
967: (r = sshpkt_put_u8(ssh, 0)) != 0 ||
968: (r = sshpkt_put_cstring(ssh, password)) != 0 ||
969: (r = sshpkt_add_padding(ssh, 64)) != 0 ||
970: (r = sshpkt_send(ssh)) != 0)
971: fatal("%s: %s", __func__, ssh_err(r));
1.99 markus 972:
1.290 djm 973: free(prompt);
974: if (password != NULL)
1.278 markus 975: freezero(password, strlen(password));
976:
977: ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
1.99 markus 978: &input_userauth_passwd_changereq);
979:
1.1 markus 980: return 1;
981: }
1.169 djm 982:
1.99 markus 983: /*
984: * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
985: */
1.169 djm 986: /* ARGSUSED */
1.218 markus 987: int
1.261 markus 988: input_userauth_passwd_changereq(int type, u_int32_t seqnr, struct ssh *ssh)
1.99 markus 989: {
1.260 markus 990: Authctxt *authctxt = ssh->authctxt;
1.278 markus 991: char *info = NULL, *lang = NULL, *password = NULL, *retype = NULL;
1.266 dtucker 992: char prompt[256];
1.252 djm 993: const char *host;
1.278 markus 994: int r;
1.99 markus 995:
996: debug2("input_userauth_passwd_changereq");
997:
998: if (authctxt == NULL)
999: fatal("input_userauth_passwd_changereq: "
1000: "no authentication context");
1.252 djm 1001: host = options.host_key_alias ? options.host_key_alias : authctxt->host;
1.99 markus 1002:
1.278 markus 1003: if ((r = sshpkt_get_cstring(ssh, &info, NULL)) != 0 ||
1004: (r = sshpkt_get_cstring(ssh, &lang, NULL)) != 0)
1005: goto out;
1.99 markus 1006: if (strlen(info) > 0)
1.116 itojun 1007: logit("%s", info);
1.278 markus 1008: if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1009: (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
1010: (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
1011: (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
1012: (r = sshpkt_put_u8(ssh, 1)) != 0) /* additional info */
1013: goto out;
1014:
1.104 deraadt 1015: snprintf(prompt, sizeof(prompt),
1.99 markus 1016: "Enter %.30s@%.128s's old password: ",
1.175 dtucker 1017: authctxt->server_user, host);
1.99 markus 1018: password = read_passphrase(prompt, 0);
1.278 markus 1019: if ((r = sshpkt_put_cstring(ssh, password)) != 0)
1020: goto out;
1021:
1022: freezero(password, strlen(password));
1.99 markus 1023: password = NULL;
1024: while (password == NULL) {
1.104 deraadt 1025: snprintf(prompt, sizeof(prompt),
1.99 markus 1026: "Enter %.30s@%.128s's new password: ",
1.175 dtucker 1027: authctxt->server_user, host);
1.99 markus 1028: password = read_passphrase(prompt, RP_ALLOW_EOF);
1029: if (password == NULL) {
1030: /* bail out */
1.278 markus 1031: r = 0;
1032: goto out;
1.99 markus 1033: }
1.104 deraadt 1034: snprintf(prompt, sizeof(prompt),
1.99 markus 1035: "Retype %.30s@%.128s's new password: ",
1.175 dtucker 1036: authctxt->server_user, host);
1.99 markus 1037: retype = read_passphrase(prompt, 0);
1038: if (strcmp(password, retype) != 0) {
1.278 markus 1039: freezero(password, strlen(password));
1.116 itojun 1040: logit("Mismatch; try again, EOF to quit.");
1.99 markus 1041: password = NULL;
1042: }
1.278 markus 1043: freezero(retype, strlen(retype));
1.99 markus 1044: }
1.278 markus 1045: if ((r = sshpkt_put_cstring(ssh, password)) != 0 ||
1046: (r = sshpkt_add_padding(ssh, 64)) != 0 ||
1047: (r = sshpkt_send(ssh)) != 0)
1048: goto out;
1.104 deraadt 1049:
1.278 markus 1050: ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
1.99 markus 1051: &input_userauth_passwd_changereq);
1.278 markus 1052: r = 0;
1053: out:
1054: if (password)
1055: freezero(password, strlen(password));
1056: free(info);
1057: free(lang);
1058: return r;
1.117 markus 1059: }
1060:
1.272 djm 1061: /*
1062: * Select an algorithm for publickey signatures.
1063: * Returns algorithm (caller must free) or NULL if no mutual algorithm found.
1064: *
1065: * Call with ssh==NULL to ignore server-sig-algs extension list and
1066: * only attempt with the key's base signature type.
1067: */
1068: static char *
1069: key_sig_algorithm(struct ssh *ssh, const struct sshkey *key)
1.231 markus 1070: {
1.274 djm 1071: char *allowed, *oallowed, *cp, *tmp, *alg = NULL;
1.231 markus 1072:
1.272 djm 1073: /*
1074: * The signature algorithm will only differ from the key algorithm
1075: * for RSA keys/certs and when the server advertises support for
1076: * newer (SHA2) algorithms.
1077: */
1078: if (ssh == NULL || ssh->kex->server_sig_algs == NULL ||
1.288 djm 1079: (key->type != KEY_RSA && key->type != KEY_RSA_CERT) ||
1080: (key->type == KEY_RSA_CERT && (datafellows & SSH_BUG_SIGTYPE))) {
1.272 djm 1081: /* Filter base key signature alg against our configuration */
1.279 markus 1082: return match_list(sshkey_ssh_name(key),
1.272 djm 1083: options.pubkey_key_types, NULL);
1.231 markus 1084: }
1085:
1.272 djm 1086: /*
1087: * For RSA keys/certs, since these might have a different sig type:
1088: * find the first entry in PubkeyAcceptedKeyTypes of the right type
1089: * that also appears in the supported signature algorithms list from
1090: * the server.
1091: */
1092: oallowed = allowed = xstrdup(options.pubkey_key_types);
1093: while ((cp = strsep(&allowed, ",")) != NULL) {
1094: if (sshkey_type_from_name(cp) != key->type)
1095: continue;
1.274 djm 1096: tmp = match_list(sshkey_sigalg_by_name(cp), ssh->kex->server_sig_algs, NULL);
1097: if (tmp != NULL)
1098: alg = xstrdup(cp);
1099: free(tmp);
1.272 djm 1100: if (alg != NULL)
1101: break;
1.269 djm 1102: }
1.272 djm 1103: free(oallowed);
1104: return alg;
1.269 djm 1105: }
1106:
1.117 markus 1107: static int
1.214 djm 1108: identity_sign(struct identity *id, u_char **sigp, size_t *lenp,
1.272 djm 1109: const u_char *data, size_t datalen, u_int compat, const char *alg)
1.117 markus 1110: {
1.259 markus 1111: struct sshkey *prv;
1.269 djm 1112: int r;
1.99 markus 1113:
1.272 djm 1114: /* The agent supports this key. */
1.269 djm 1115: if (id->key != NULL && id->agent_fd != -1) {
1.272 djm 1116: return ssh_agent_sign(id->agent_fd, id->key, sigp, lenp,
1117: data, datalen, alg, compat);
1.269 djm 1118: }
1.214 djm 1119:
1.117 markus 1120: /*
1.272 djm 1121: * We have already loaded the private key or the private key is
1122: * stored in external hardware.
1.117 markus 1123: */
1.255 djm 1124: if (id->key != NULL &&
1.272 djm 1125: (id->isprivate || (id->key->flags & SSHKEY_FLAG_EXT))) {
1126: if ((r = sshkey_sign(id->key, sigp, lenp, data, datalen,
1127: alg, compat)) != 0)
1128: return r;
1129: /*
1130: * PKCS#11 tokens may not support all signature algorithms,
1131: * so check what we get back.
1132: */
1133: if ((r = sshkey_check_sigtype(*sigp, *lenp, alg)) != 0)
1134: return r;
1135: return 0;
1136: }
1.255 djm 1137:
1.272 djm 1138: /* Load the private key from the file. */
1.229 jcs 1139: if ((prv = load_identity_file(id)) == NULL)
1.239 djm 1140: return SSH_ERR_KEY_NOT_FOUND;
1.265 djm 1141: if (id->key != NULL && !sshkey_equal_public(prv, id->key)) {
1142: error("%s: private key %s contents do not match public",
1143: __func__, id->filename);
1144: return SSH_ERR_KEY_NOT_FOUND;
1145: }
1.272 djm 1146: r = sshkey_sign(prv, sigp, lenp, data, datalen, alg, compat);
1.214 djm 1147: sshkey_free(prv);
1.269 djm 1148: return r;
1.51 markus 1149: }
1150:
1.76 itojun 1151: static int
1.255 djm 1152: id_filename_matches(Identity *id, Identity *private_id)
1153: {
1154: const char *suffixes[] = { ".pub", "-cert.pub", NULL };
1155: size_t len = strlen(id->filename), plen = strlen(private_id->filename);
1156: size_t i, slen;
1157:
1158: if (strcmp(id->filename, private_id->filename) == 0)
1159: return 1;
1160: for (i = 0; suffixes[i]; i++) {
1161: slen = strlen(suffixes[i]);
1162: if (len > slen && plen == len - slen &&
1163: strcmp(id->filename + (len - slen), suffixes[i]) == 0 &&
1164: memcmp(id->filename, private_id->filename, plen) == 0)
1165: return 1;
1166: }
1167: return 0;
1168: }
1169:
1170: static int
1.272 djm 1171: sign_and_send_pubkey(struct ssh *ssh, Authctxt *authctxt, Identity *id)
1.1 markus 1172: {
1.272 djm 1173: struct sshbuf *b = NULL;
1174: Identity *private_id, *sign_id = NULL;
1175: u_char *signature = NULL;
1176: size_t slen = 0, skip = 0;
1177: int r, fallback_sigtype, sent = 0;
1178: char *alg = NULL, *fp = NULL;
1179: const char *loc = "";
1.1 markus 1180:
1.222 djm 1181: if ((fp = sshkey_fingerprint(id->key, options.fingerprint_hash,
1182: SSH_FP_DEFAULT)) == NULL)
1183: return 0;
1.51 markus 1184:
1.272 djm 1185: debug3("%s: %s %s", __func__, sshkey_type(id->key), fp);
1.1 markus 1186:
1.227 djm 1187: /*
1188: * If the key is an certificate, try to find a matching private key
1189: * and use it to complete the signature.
1.241 djm 1190: * If no such private key exists, fall back to trying the certificate
1191: * key itself in case it has a private half already loaded.
1.272 djm 1192: * This will try to set sign_id to the private key that will perform
1193: * the signature.
1.227 djm 1194: */
1.272 djm 1195: if (sshkey_is_cert(id->key)) {
1.227 djm 1196: TAILQ_FOREACH(private_id, &authctxt->keys, next) {
1197: if (sshkey_equal_public(id->key, private_id->key) &&
1198: id->key->type != private_id->key->type) {
1.272 djm 1199: sign_id = private_id;
1.227 djm 1200: break;
1201: }
1202: }
1.255 djm 1203: /*
1204: * Exact key matches are preferred, but also allow
1205: * filename matches for non-PKCS#11/agent keys that
1206: * didn't load public keys. This supports the case
1207: * of keeping just a private key file and public
1208: * certificate on disk.
1209: */
1.272 djm 1210: if (sign_id == NULL &&
1211: !id->isprivate && id->agent_fd == -1 &&
1.255 djm 1212: (id->key->flags & SSHKEY_FLAG_EXT) == 0) {
1213: TAILQ_FOREACH(private_id, &authctxt->keys, next) {
1214: if (private_id->key == NULL &&
1215: id_filename_matches(id, private_id)) {
1.272 djm 1216: sign_id = private_id;
1.255 djm 1217: break;
1218: }
1219: }
1220: }
1.272 djm 1221: if (sign_id != NULL) {
1.227 djm 1222: debug2("%s: using private key \"%s\"%s for "
1223: "certificate", __func__, id->filename,
1224: id->agent_fd != -1 ? " from agent" : "");
1225: } else {
1.240 djm 1226: debug("%s: no separate private key for certificate "
1.227 djm 1227: "\"%s\"", __func__, id->filename);
1228: }
1229: }
1230:
1.272 djm 1231: /*
1232: * If the above didn't select another identity to do the signing
1233: * then default to the one we started with.
1234: */
1235: if (sign_id == NULL)
1236: sign_id = id;
1237:
1238: /* assemble and sign data */
1239: for (fallback_sigtype = 0; fallback_sigtype <= 1; fallback_sigtype++) {
1240: free(alg);
1241: slen = 0;
1242: signature = NULL;
1243: if ((alg = key_sig_algorithm(fallback_sigtype ? NULL : ssh,
1244: id->key)) == NULL) {
1245: error("%s: no mutual signature supported", __func__);
1246: goto out;
1247: }
1248: debug3("%s: signing using %s", __func__, alg);
1249:
1250: sshbuf_free(b);
1251: if ((b = sshbuf_new()) == NULL)
1252: fatal("%s: sshbuf_new failed", __func__);
1253: if (datafellows & SSH_OLD_SESSIONID) {
1254: if ((r = sshbuf_put(b, session_id2,
1255: session_id2_len)) != 0) {
1256: fatal("%s: sshbuf_put: %s",
1257: __func__, ssh_err(r));
1258: }
1259: } else {
1260: if ((r = sshbuf_put_string(b, session_id2,
1261: session_id2_len)) != 0) {
1262: fatal("%s: sshbuf_put_string: %s",
1263: __func__, ssh_err(r));
1264: }
1265: }
1.278 markus 1266: skip = sshbuf_len(b);
1.272 djm 1267: if ((r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1268: (r = sshbuf_put_cstring(b, authctxt->server_user)) != 0 ||
1269: (r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||
1270: (r = sshbuf_put_cstring(b, authctxt->method->name)) != 0 ||
1271: (r = sshbuf_put_u8(b, 1)) != 0 ||
1272: (r = sshbuf_put_cstring(b, alg)) != 0 ||
1273: (r = sshkey_puts(id->key, b)) != 0) {
1274: fatal("%s: assemble signed data: %s",
1275: __func__, ssh_err(r));
1276: }
1277:
1278: /* generate signature */
1279: r = identity_sign(sign_id, &signature, &slen,
1280: sshbuf_ptr(b), sshbuf_len(b), datafellows, alg);
1281: if (r == 0)
1282: break;
1283: else if (r == SSH_ERR_KEY_NOT_FOUND)
1284: goto out; /* soft failure */
1285: else if (r == SSH_ERR_SIGN_ALG_UNSUPPORTED &&
1286: !fallback_sigtype) {
1287: if (sign_id->agent_fd != -1)
1288: loc = "agent ";
1289: else if ((sign_id->key->flags & SSHKEY_FLAG_EXT) != 0)
1290: loc = "token ";
1291: logit("%skey %s %s returned incorrect signature type",
1292: loc, sshkey_type(id->key), fp);
1293: continue;
1294: }
1295: error("%s: signing failed: %s", __func__, ssh_err(r));
1296: goto out;
1.17 markus 1297: }
1.272 djm 1298: if (slen == 0 || signature == NULL) /* shouldn't happen */
1299: fatal("%s: no signature", __func__);
1.51 markus 1300:
1.1 markus 1301: /* append signature */
1.272 djm 1302: if ((r = sshbuf_put_string(b, signature, slen)) != 0)
1303: fatal("%s: append signature: %s", __func__, ssh_err(r));
1.1 markus 1304:
1.272 djm 1305: #ifdef DEBUG_PK
1306: sshbuf_dump(b, stderr);
1307: #endif
1.1 markus 1308: /* skip session id and packet type */
1.272 djm 1309: if ((r = sshbuf_consume(b, skip + 1)) != 0)
1310: fatal("%s: consume: %s", __func__, ssh_err(r));
1.1 markus 1311:
1312: /* put remaining data from buffer into packet */
1.272 djm 1313: if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1314: (r = sshpkt_putb(ssh, b)) != 0 ||
1315: (r = sshpkt_send(ssh)) != 0)
1316: fatal("%s: enqueue request: %s", __func__, ssh_err(r));
1317:
1318: /* success */
1319: sent = 1;
1.17 markus 1320:
1.272 djm 1321: out:
1322: free(fp);
1323: free(alg);
1324: sshbuf_free(b);
1325: freezero(signature, slen);
1326: return sent;
1.16 markus 1327: }
1328:
1.76 itojun 1329: static int
1.272 djm 1330: send_pubkey_test(struct ssh *ssh, Authctxt *authctxt, Identity *id)
1.20 markus 1331: {
1.272 djm 1332: u_char *blob = NULL;
1333: char *alg = NULL;
1.278 markus 1334: size_t bloblen;
1335: u_int have_sig = 0;
1336: int sent = 0, r;
1.20 markus 1337:
1.272 djm 1338: if ((alg = key_sig_algorithm(ssh, id->key)) == NULL) {
1339: debug("%s: no mutual signature algorithm", __func__);
1340: goto out;
1341: }
1.16 markus 1342:
1.278 markus 1343: if ((r = sshkey_to_blob(id->key, &blob, &bloblen)) != 0) {
1.51 markus 1344: /* we cannot handle this key */
1.272 djm 1345: debug3("%s: cannot handle key", __func__);
1346: goto out;
1.16 markus 1347: }
1.51 markus 1348: /* register callback for USERAUTH_PK_OK message */
1.278 markus 1349: ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
1.51 markus 1350:
1.278 markus 1351: if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1352: (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
1353: (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
1354: (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
1355: (r = sshpkt_put_u8(ssh, have_sig)) != 0 ||
1356: (r = sshpkt_put_cstring(ssh, alg)) != 0 ||
1357: (r = sshpkt_put_string(ssh, blob, bloblen)) != 0 ||
1358: (r = sshpkt_send(ssh)) != 0)
1359: fatal("%s: %s", __func__, ssh_err(r));
1.272 djm 1360: sent = 1;
1.278 markus 1361:
1362: out:
1.272 djm 1363: free(alg);
1.197 djm 1364: free(blob);
1.272 djm 1365: return sent;
1.51 markus 1366: }
1367:
1.259 markus 1368: static struct sshkey *
1.229 jcs 1369: load_identity_file(Identity *id)
1.51 markus 1370: {
1.259 markus 1371: struct sshkey *private = NULL;
1.229 jcs 1372: char prompt[300], *passphrase, *comment;
1.215 djm 1373: int r, perm_ok = 0, quit = 0, i;
1.52 markus 1374: struct stat st;
1.16 markus 1375:
1.229 jcs 1376: if (stat(id->filename, &st) < 0) {
1377: (id->userprovided ? logit : debug3)("no such identity: %s: %s",
1378: id->filename, strerror(errno));
1.52 markus 1379: return NULL;
1380: }
1.214 djm 1381: snprintf(prompt, sizeof prompt,
1.229 jcs 1382: "Enter passphrase for key '%.100s': ", id->filename);
1.214 djm 1383: for (i = 0; i <= options.number_of_password_prompts; i++) {
1384: if (i == 0)
1385: passphrase = "";
1386: else {
1.20 markus 1387: passphrase = read_passphrase(prompt, 0);
1.214 djm 1388: if (*passphrase == '\0') {
1.20 markus 1389: debug2("no passphrase given, try next key");
1.214 djm 1390: free(passphrase);
1391: break;
1392: }
1393: }
1.229 jcs 1394: switch ((r = sshkey_load_private_type(KEY_UNSPEC, id->filename,
1395: passphrase, &private, &comment, &perm_ok))) {
1.214 djm 1396: case 0:
1397: break;
1398: case SSH_ERR_KEY_WRONG_PASSPHRASE:
1399: if (options.batch_mode) {
1400: quit = 1;
1401: break;
1402: }
1.215 djm 1403: if (i != 0)
1404: debug2("bad passphrase given, try again...");
1.214 djm 1405: break;
1406: case SSH_ERR_SYSTEM_ERROR:
1407: if (errno == ENOENT) {
1408: debug2("Load key \"%s\": %s",
1.229 jcs 1409: id->filename, ssh_err(r));
1.51 markus 1410: quit = 1;
1.214 djm 1411: break;
1.20 markus 1412: }
1.214 djm 1413: /* FALLTHROUGH */
1414: default:
1.229 jcs 1415: error("Load key \"%s\": %s", id->filename, ssh_err(r));
1.214 djm 1416: quit = 1;
1417: break;
1418: }
1.230 djm 1419: if (!quit && private != NULL && id->agent_fd == -1 &&
1.229 jcs 1420: !(id->key && id->isprivate))
1421: maybe_add_key_to_agent(id->filename, private, comment,
1422: passphrase);
1.278 markus 1423: if (i > 0)
1424: freezero(passphrase, strlen(passphrase));
1.232 mmcc 1425: free(comment);
1.214 djm 1426: if (private != NULL || quit)
1427: break;
1.16 markus 1428: }
1.51 markus 1429: return private;
1430: }
1431:
1.272 djm 1432: static int
1433: key_type_allowed_by_config(struct sshkey *key)
1434: {
1435: if (match_pattern_list(sshkey_ssh_name(key),
1436: options.pubkey_key_types, 0) == 1)
1437: return 1;
1438:
1439: /* RSA keys/certs might be allowed by alternate signature types */
1440: switch (key->type) {
1441: case KEY_RSA:
1442: if (match_pattern_list("rsa-sha2-512",
1443: options.pubkey_key_types, 0) == 1)
1444: return 1;
1445: if (match_pattern_list("rsa-sha2-256",
1446: options.pubkey_key_types, 0) == 1)
1447: return 1;
1448: break;
1449: case KEY_RSA_CERT:
1450: if (match_pattern_list("rsa-sha2-512-cert-v01@openssh.com",
1451: options.pubkey_key_types, 0) == 1)
1452: return 1;
1453: if (match_pattern_list("rsa-sha2-256-cert-v01@openssh.com",
1454: options.pubkey_key_types, 0) == 1)
1455: return 1;
1456: break;
1457: }
1458: return 0;
1459: }
1460:
1461:
1.117 markus 1462: /*
1463: * try keys in the following order:
1.227 djm 1464: * 1. certificates listed in the config file
1465: * 2. other input certificates
1466: * 3. agent keys that are found in the config file
1467: * 4. other agent keys
1468: * 5. keys that are only listed in the config file
1.117 markus 1469: */
1470: static void
1471: pubkey_prepare(Authctxt *authctxt)
1.51 markus 1472: {
1.214 djm 1473: struct identity *id, *id2, *tmp;
1474: struct idlist agent, files, *preferred;
1475: struct sshkey *key;
1.230 djm 1476: int agent_fd = -1, i, r, found;
1.214 djm 1477: size_t j;
1478: struct ssh_identitylist *idlist;
1.287 djm 1479: char *ident;
1.51 markus 1480:
1.117 markus 1481: TAILQ_INIT(&agent); /* keys from the agent */
1482: TAILQ_INIT(&files); /* keys from the config file */
1483: preferred = &authctxt->keys;
1484: TAILQ_INIT(preferred); /* preferred order of keys */
1485:
1.190 djm 1486: /* list of keys stored in the filesystem and PKCS#11 */
1.117 markus 1487: for (i = 0; i < options.num_identity_files; i++) {
1488: key = options.identity_keys[i];
1.180 djm 1489: if (key && key->cert && key->cert->type != SSH2_CERT_TYPE_USER)
1.117 markus 1490: continue;
1491: options.identity_keys[i] = NULL;
1.150 djm 1492: id = xcalloc(1, sizeof(*id));
1.230 djm 1493: id->agent_fd = -1;
1.117 markus 1494: id->key = key;
1495: id->filename = xstrdup(options.identity_files[i]);
1.192 dtucker 1496: id->userprovided = options.identity_file_userprovided[i];
1.117 markus 1497: TAILQ_INSERT_TAIL(&files, id, next);
1.190 djm 1498: }
1.227 djm 1499: /* list of certificates specified by user */
1500: for (i = 0; i < options.num_certificate_files; i++) {
1501: key = options.certificates[i];
1.278 markus 1502: if (!sshkey_is_cert(key) || key->cert == NULL ||
1.227 djm 1503: key->cert->type != SSH2_CERT_TYPE_USER)
1504: continue;
1505: id = xcalloc(1, sizeof(*id));
1.230 djm 1506: id->agent_fd = -1;
1.227 djm 1507: id->key = key;
1508: id->filename = xstrdup(options.certificate_files[i]);
1509: id->userprovided = options.certificate_file_userprovided[i];
1510: TAILQ_INSERT_TAIL(preferred, id, next);
1.117 markus 1511: }
1512: /* list of keys supported by the agent */
1.214 djm 1513: if ((r = ssh_get_authentication_socket(&agent_fd)) != 0) {
1514: if (r != SSH_ERR_AGENT_NOT_PRESENT)
1515: debug("%s: ssh_get_authentication_socket: %s",
1516: __func__, ssh_err(r));
1.258 naddy 1517: } else if ((r = ssh_fetch_identitylist(agent_fd, &idlist)) != 0) {
1.214 djm 1518: if (r != SSH_ERR_AGENT_NO_IDENTITIES)
1519: debug("%s: ssh_fetch_identitylist: %s",
1520: __func__, ssh_err(r));
1.237 markus 1521: close(agent_fd);
1.214 djm 1522: } else {
1523: for (j = 0; j < idlist->nkeys; j++) {
1.117 markus 1524: found = 0;
1525: TAILQ_FOREACH(id, &files, next) {
1.214 djm 1526: /*
1527: * agent keys from the config file are
1528: * preferred
1529: */
1530: if (sshkey_equal(idlist->keys[j], id->key)) {
1.117 markus 1531: TAILQ_REMOVE(&files, id, next);
1532: TAILQ_INSERT_TAIL(preferred, id, next);
1.214 djm 1533: id->agent_fd = agent_fd;
1.117 markus 1534: found = 1;
1535: break;
1536: }
1537: }
1.135 markus 1538: if (!found && !options.identities_only) {
1.150 djm 1539: id = xcalloc(1, sizeof(*id));
1.214 djm 1540: /* XXX "steals" key/comment from idlist */
1541: id->key = idlist->keys[j];
1542: id->filename = idlist->comments[j];
1543: idlist->keys[j] = NULL;
1544: idlist->comments[j] = NULL;
1545: id->agent_fd = agent_fd;
1.117 markus 1546: TAILQ_INSERT_TAIL(&agent, id, next);
1547: }
1548: }
1.214 djm 1549: ssh_free_identitylist(idlist);
1.117 markus 1550: /* append remaining agent keys */
1551: for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1552: TAILQ_REMOVE(&agent, id, next);
1553: TAILQ_INSERT_TAIL(preferred, id, next);
1554: }
1.214 djm 1555: authctxt->agent_fd = agent_fd;
1.244 djm 1556: }
1557: /* Prefer PKCS11 keys that are explicitly listed */
1558: TAILQ_FOREACH_SAFE(id, &files, next, tmp) {
1559: if (id->key == NULL || (id->key->flags & SSHKEY_FLAG_EXT) == 0)
1560: continue;
1561: found = 0;
1562: TAILQ_FOREACH(id2, &files, next) {
1563: if (id2->key == NULL ||
1564: (id2->key->flags & SSHKEY_FLAG_EXT) == 0)
1565: continue;
1566: if (sshkey_equal(id->key, id2->key)) {
1567: TAILQ_REMOVE(&files, id, next);
1568: TAILQ_INSERT_TAIL(preferred, id, next);
1569: found = 1;
1570: break;
1571: }
1572: }
1573: /* If IdentitiesOnly set and key not found then don't use it */
1574: if (!found && options.identities_only) {
1575: TAILQ_REMOVE(&files, id, next);
1.278 markus 1576: freezero(id, sizeof(*id));
1.244 djm 1577: }
1.117 markus 1578: }
1579: /* append remaining keys from the config file */
1580: for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1581: TAILQ_REMOVE(&files, id, next);
1582: TAILQ_INSERT_TAIL(preferred, id, next);
1583: }
1.228 djm 1584: /* finally, filter by PubkeyAcceptedKeyTypes */
1585: TAILQ_FOREACH_SAFE(id, preferred, next, id2) {
1.273 djm 1586: if (id->key != NULL && !key_type_allowed_by_config(id->key)) {
1.228 djm 1587: debug("Skipping %s key %s - "
1588: "not in PubkeyAcceptedKeyTypes",
1589: sshkey_ssh_name(id->key), id->filename);
1590: TAILQ_REMOVE(preferred, id, next);
1591: sshkey_free(id->key);
1592: free(id->filename);
1593: memset(id, 0, sizeof(*id));
1594: continue;
1595: }
1.117 markus 1596: }
1.287 djm 1597: /* List the keys we plan on using */
1598: TAILQ_FOREACH_SAFE(id, preferred, next, id2) {
1599: ident = format_identity(id);
1600: debug("Will attempt key: %s", ident);
1601: free(ident);
1602: }
1603: debug2("%s: done", __func__);
1.17 markus 1604: }
1605:
1.117 markus 1606: static void
1607: pubkey_cleanup(Authctxt *authctxt)
1.51 markus 1608: {
1.117 markus 1609: Identity *id;
1.51 markus 1610:
1.289 djm 1611: if (authctxt->agent_fd != -1) {
1.214 djm 1612: ssh_close_authentication_socket(authctxt->agent_fd);
1.289 djm 1613: authctxt->agent_fd = -1;
1614: }
1.117 markus 1615: for (id = TAILQ_FIRST(&authctxt->keys); id;
1616: id = TAILQ_FIRST(&authctxt->keys)) {
1617: TAILQ_REMOVE(&authctxt->keys, id, next);
1.235 mmcc 1618: sshkey_free(id->key);
1.197 djm 1619: free(id->filename);
1620: free(id);
1.117 markus 1621: }
1.1 markus 1622: }
1623:
1.251 djm 1624: static void
1625: pubkey_reset(Authctxt *authctxt)
1626: {
1627: Identity *id;
1628:
1629: TAILQ_FOREACH(id, &authctxt->keys, next)
1630: id->tried = 0;
1631: }
1632:
1.225 markus 1633: static int
1634: try_identity(Identity *id)
1635: {
1636: if (!id->key)
1637: return (0);
1.279 markus 1638: if (sshkey_type_plain(id->key->type) == KEY_RSA &&
1.225 markus 1639: (datafellows & SSH_BUG_RSASIGMD5) != 0) {
1640: debug("Skipped %s key %s for RSA/MD5 server",
1.279 markus 1641: sshkey_type(id->key), id->filename);
1.225 markus 1642: return (0);
1643: }
1.257 djm 1644: return 1;
1.225 markus 1645: }
1646:
1.20 markus 1647: int
1648: userauth_pubkey(Authctxt *authctxt)
1649: {
1.272 djm 1650: struct ssh *ssh = active_state; /* XXX */
1.117 markus 1651: Identity *id;
1.20 markus 1652: int sent = 0;
1.287 djm 1653: char *ident;
1.20 markus 1654:
1.117 markus 1655: while ((id = TAILQ_FIRST(&authctxt->keys))) {
1656: if (id->tried++)
1657: return (0);
1.127 markus 1658: /* move key to the end of the queue */
1.117 markus 1659: TAILQ_REMOVE(&authctxt->keys, id, next);
1660: TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1661: /*
1662: * send a test message if we have the public key. for
1663: * encrypted keys we cannot do this and have to load the
1664: * private key instead
1665: */
1.200 djm 1666: if (id->key != NULL) {
1.225 markus 1667: if (try_identity(id)) {
1.287 djm 1668: ident = format_identity(id);
1669: debug("Offering public key: %s", ident);
1670: free(ident);
1.272 djm 1671: sent = send_pubkey_test(ssh, authctxt, id);
1.200 djm 1672: }
1673: } else {
1.117 markus 1674: debug("Trying private key: %s", id->filename);
1.229 jcs 1675: id->key = load_identity_file(id);
1.117 markus 1676: if (id->key != NULL) {
1.225 markus 1677: if (try_identity(id)) {
1678: id->isprivate = 1;
1.272 djm 1679: sent = sign_and_send_pubkey(ssh,
1.200 djm 1680: authctxt, id);
1681: }
1.278 markus 1682: sshkey_free(id->key);
1.117 markus 1683: id->key = NULL;
1.251 djm 1684: id->isprivate = 0;
1.51 markus 1685: }
1686: }
1.117 markus 1687: if (sent)
1688: return (sent);
1.28 markus 1689: }
1.117 markus 1690: return (0);
1.20 markus 1691: }
1692:
1.23 markus 1693: /*
1694: * Send userauth request message specifying keyboard-interactive method.
1695: */
1696: int
1697: userauth_kbdint(Authctxt *authctxt)
1698: {
1.278 markus 1699: struct ssh *ssh = active_state; /* XXX */
1.23 markus 1700: static int attempt = 0;
1.278 markus 1701: int r;
1.23 markus 1702:
1703: if (attempt++ >= options.number_of_password_prompts)
1704: return 0;
1.82 markus 1705: /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1706: if (attempt > 1 && !authctxt->info_req_seen) {
1707: debug3("userauth_kbdint: disable: no info_req_seen");
1.278 markus 1708: ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1.82 markus 1709: return 0;
1710: }
1.23 markus 1711:
1712: debug2("userauth_kbdint");
1.278 markus 1713: if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1714: (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
1715: (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
1716: (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
1717: (r = sshpkt_put_cstring(ssh, "")) != 0 || /* lang */
1718: (r = sshpkt_put_cstring(ssh, options.kbd_interactive_devices ?
1719: options.kbd_interactive_devices : "")) != 0 ||
1720: (r = sshpkt_send(ssh)) != 0)
1721: fatal("%s: %s", __func__, ssh_err(r));
1.23 markus 1722:
1.278 markus 1723: ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1.23 markus 1724: return 1;
1725: }
1726:
1727: /*
1.46 markus 1728: * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1.23 markus 1729: */
1.218 markus 1730: int
1.261 markus 1731: input_userauth_info_req(int type, u_int32_t seq, struct ssh *ssh)
1.23 markus 1732: {
1.260 markus 1733: Authctxt *authctxt = ssh->authctxt;
1.278 markus 1734: char *name = NULL, *inst = NULL, *lang = NULL, *prompt = NULL;
1735: char *response = NULL;
1736: u_char echo = 0;
1.32 markus 1737: u_int num_prompts, i;
1.278 markus 1738: int r;
1.23 markus 1739:
1740: debug2("input_userauth_info_req");
1741:
1742: if (authctxt == NULL)
1743: fatal("input_userauth_info_req: no authentication context");
1.82 markus 1744:
1745: authctxt->info_req_seen = 1;
1.23 markus 1746:
1.278 markus 1747: if ((r = sshpkt_get_cstring(ssh, &name, NULL)) != 0 ||
1748: (r = sshpkt_get_cstring(ssh, &inst, NULL)) != 0 ||
1749: (r = sshpkt_get_cstring(ssh, &lang, NULL)) != 0)
1750: goto out;
1.23 markus 1751: if (strlen(name) > 0)
1.116 itojun 1752: logit("%s", name);
1.23 markus 1753: if (strlen(inst) > 0)
1.116 itojun 1754: logit("%s", inst);
1.23 markus 1755:
1.278 markus 1756: if ((r = sshpkt_get_u32(ssh, &num_prompts)) != 0)
1757: goto out;
1.23 markus 1758: /*
1759: * Begin to build info response packet based on prompts requested.
1760: * We commit to providing the correct number of responses, so if
1761: * further on we run into a problem that prevents this, we have to
1762: * be sure and clean this up and send a correct error response.
1763: */
1.278 markus 1764: if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_INFO_RESPONSE)) != 0 ||
1765: (r = sshpkt_put_u32(ssh, num_prompts)) != 0)
1766: goto out;
1.23 markus 1767:
1.73 markus 1768: debug2("input_userauth_info_req: num_prompts %d", num_prompts);
1.23 markus 1769: for (i = 0; i < num_prompts; i++) {
1.278 markus 1770: if ((r = sshpkt_get_cstring(ssh, &prompt, NULL)) != 0 ||
1771: (r = sshpkt_get_u8(ssh, &echo)) != 0)
1772: goto out;
1.77 markus 1773: response = read_passphrase(prompt, echo ? RP_ECHO : 0);
1.278 markus 1774: if ((r = sshpkt_put_cstring(ssh, response)) != 0)
1775: goto out;
1776: freezero(response, strlen(response));
1.197 djm 1777: free(prompt);
1.278 markus 1778: response = prompt = NULL;
1.23 markus 1779: }
1.278 markus 1780: /* done with parsing incoming message. */
1781: if ((r = sshpkt_get_end(ssh)) != 0 ||
1782: (r = sshpkt_add_padding(ssh, 64)) != 0)
1783: goto out;
1784: r = sshpkt_send(ssh);
1785: out:
1786: if (response)
1787: freezero(response, strlen(response));
1788: free(prompt);
1789: free(name);
1790: free(inst);
1791: free(lang);
1792: return r;
1.68 markus 1793: }
1794:
1.100 markus 1795: static int
1.223 djm 1796: ssh_keysign(struct sshkey *key, u_char **sigp, size_t *lenp,
1797: const u_char *data, size_t datalen)
1.100 markus 1798: {
1.223 djm 1799: struct sshbuf *b;
1.101 markus 1800: struct stat st;
1.100 markus 1801: pid_t pid;
1.223 djm 1802: int i, r, to[2], from[2], status, sock = packet_get_connection_in();
1803: u_char rversion = 0, version = 2;
1804: void (*osigchld)(int);
1.100 markus 1805:
1.223 djm 1806: *sigp = NULL;
1807: *lenp = 0;
1.100 markus 1808:
1.101 markus 1809: if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
1.223 djm 1810: error("%s: not installed: %s", __func__, strerror(errno));
1811: return -1;
1812: }
1813: if (fflush(stdout) != 0) {
1814: error("%s: fflush: %s", __func__, strerror(errno));
1.101 markus 1815: return -1;
1816: }
1.100 markus 1817: if (pipe(to) < 0) {
1.223 djm 1818: error("%s: pipe: %s", __func__, strerror(errno));
1.100 markus 1819: return -1;
1820: }
1821: if (pipe(from) < 0) {
1.223 djm 1822: error("%s: pipe: %s", __func__, strerror(errno));
1.100 markus 1823: return -1;
1824: }
1825: if ((pid = fork()) < 0) {
1.223 djm 1826: error("%s: fork: %s", __func__, strerror(errno));
1.100 markus 1827: return -1;
1828: }
1.223 djm 1829: osigchld = signal(SIGCHLD, SIG_DFL);
1.100 markus 1830: if (pid == 0) {
1.174 dtucker 1831: /* keep the socket on exec */
1.223 djm 1832: fcntl(sock, F_SETFD, 0);
1.100 markus 1833: close(from[0]);
1834: if (dup2(from[1], STDOUT_FILENO) < 0)
1.223 djm 1835: fatal("%s: dup2: %s", __func__, strerror(errno));
1.100 markus 1836: close(to[1]);
1837: if (dup2(to[0], STDIN_FILENO) < 0)
1.223 djm 1838: fatal("%s: dup2: %s", __func__, strerror(errno));
1.103 markus 1839: close(from[1]);
1840: close(to[0]);
1.223 djm 1841: /* Close everything but stdio and the socket */
1842: for (i = STDERR_FILENO + 1; i < sock; i++)
1843: close(i);
1844: closefrom(sock + 1);
1845: debug3("%s: [child] pid=%ld, exec %s",
1846: __func__, (long)getpid(), _PATH_SSH_KEY_SIGN);
1.233 mmcc 1847: execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *)NULL);
1.223 djm 1848: fatal("%s: exec(%s): %s", __func__, _PATH_SSH_KEY_SIGN,
1.100 markus 1849: strerror(errno));
1850: }
1851: close(from[1]);
1852: close(to[0]);
1853:
1.223 djm 1854: if ((b = sshbuf_new()) == NULL)
1855: fatal("%s: sshbuf_new failed", __func__);
1856: /* send # of sock, data to be signed */
1.253 djm 1857: if ((r = sshbuf_put_u32(b, sock)) != 0 ||
1.223 djm 1858: (r = sshbuf_put_string(b, data, datalen)) != 0)
1859: fatal("%s: buffer error: %s", __func__, ssh_err(r));
1860: if (ssh_msg_send(to[1], version, b) == -1)
1861: fatal("%s: couldn't send request", __func__);
1862: sshbuf_reset(b);
1863: r = ssh_msg_recv(from[0], b);
1.101 markus 1864: close(from[0]);
1865: close(to[1]);
1.223 djm 1866: if (r < 0) {
1867: error("%s: no reply", __func__);
1868: goto fail;
1869: }
1.101 markus 1870:
1.223 djm 1871: errno = 0;
1872: while (waitpid(pid, &status, 0) < 0) {
1873: if (errno != EINTR) {
1874: error("%s: waitpid %ld: %s",
1875: __func__, (long)pid, strerror(errno));
1876: goto fail;
1877: }
1878: }
1879: if (!WIFEXITED(status)) {
1880: error("%s: exited abnormally", __func__);
1881: goto fail;
1882: }
1883: if (WEXITSTATUS(status) != 0) {
1884: error("%s: exited with status %d",
1885: __func__, WEXITSTATUS(status));
1886: goto fail;
1887: }
1888: if ((r = sshbuf_get_u8(b, &rversion)) != 0) {
1889: error("%s: buffer error: %s", __func__, ssh_err(r));
1890: goto fail;
1891: }
1892: if (rversion != version) {
1893: error("%s: bad version", __func__);
1894: goto fail;
1895: }
1896: if ((r = sshbuf_get_string(b, sigp, lenp)) != 0) {
1897: error("%s: buffer error: %s", __func__, ssh_err(r));
1898: fail:
1899: signal(SIGCHLD, osigchld);
1900: sshbuf_free(b);
1.100 markus 1901: return -1;
1902: }
1.223 djm 1903: signal(SIGCHLD, osigchld);
1904: sshbuf_free(b);
1.100 markus 1905:
1906: return 0;
1907: }
1908:
1.68 markus 1909: int
1910: userauth_hostbased(Authctxt *authctxt)
1911: {
1.272 djm 1912: struct ssh *ssh = active_state; /* XXX */
1.223 djm 1913: struct sshkey *private = NULL;
1914: struct sshbuf *b = NULL;
1915: u_char *sig = NULL, *keyblob = NULL;
1916: char *fp = NULL, *chost = NULL, *lname = NULL;
1917: size_t siglen = 0, keylen = 0;
1918: int i, r, success = 0;
1919:
1920: if (authctxt->ktypes == NULL) {
1921: authctxt->oktypes = xstrdup(options.hostbased_key_types);
1922: authctxt->ktypes = authctxt->oktypes;
1923: }
1.213 djm 1924:
1.223 djm 1925: /*
1926: * Work through each listed type pattern in HostbasedKeyTypes,
1927: * trying each hostkey that matches the type in turn.
1928: */
1929: for (;;) {
1930: if (authctxt->active_ktype == NULL)
1931: authctxt->active_ktype = strsep(&authctxt->ktypes, ",");
1932: if (authctxt->active_ktype == NULL ||
1933: *authctxt->active_ktype == '\0')
1934: break;
1935: debug3("%s: trying key type %s", __func__,
1936: authctxt->active_ktype);
1.68 markus 1937:
1.223 djm 1938: /* check for a useful key */
1939: private = NULL;
1940: for (i = 0; i < authctxt->sensitive->nkeys; i++) {
1941: if (authctxt->sensitive->keys[i] == NULL ||
1942: authctxt->sensitive->keys[i]->type == KEY_UNSPEC)
1943: continue;
1944: if (match_pattern_list(
1945: sshkey_ssh_name(authctxt->sensitive->keys[i]),
1.224 djm 1946: authctxt->active_ktype, 0) != 1)
1.223 djm 1947: continue;
1.68 markus 1948: /* we take and free the key */
1.223 djm 1949: private = authctxt->sensitive->keys[i];
1950: authctxt->sensitive->keys[i] = NULL;
1.68 markus 1951: break;
1952: }
1.223 djm 1953: /* Found one */
1954: if (private != NULL)
1955: break;
1956: /* No more keys of this type; advance */
1957: authctxt->active_ktype = NULL;
1.68 markus 1958: }
1.223 djm 1959: if (private == NULL) {
1960: free(authctxt->oktypes);
1961: authctxt->oktypes = authctxt->ktypes = NULL;
1962: authctxt->active_ktype = NULL;
1.109 markus 1963: debug("No more client hostkeys for hostbased authentication.");
1.223 djm 1964: goto out;
1.68 markus 1965: }
1.211 djm 1966:
1.223 djm 1967: if ((fp = sshkey_fingerprint(private, options.fingerprint_hash,
1968: SSH_FP_DEFAULT)) == NULL) {
1969: error("%s: sshkey_fingerprint failed", __func__);
1970: goto out;
1.68 markus 1971: }
1.223 djm 1972: debug("%s: trying hostkey %s %s",
1973: __func__, sshkey_ssh_name(private), fp);
1.211 djm 1974:
1.84 markus 1975: /* figure out a name for the client host */
1.223 djm 1976: if ((lname = get_local_name(packet_get_connection_in())) == NULL) {
1977: error("%s: cannot get local ipaddr/name", __func__);
1978: goto out;
1.84 markus 1979: }
1.223 djm 1980:
1981: /* XXX sshbuf_put_stringf? */
1982: xasprintf(&chost, "%s.", lname);
1983: debug2("%s: chost %s", __func__, chost);
1.84 markus 1984:
1.68 markus 1985: /* construct data */
1.223 djm 1986: if ((b = sshbuf_new()) == NULL) {
1987: error("%s: sshbuf_new failed", __func__);
1988: goto out;
1989: }
1990: if ((r = sshkey_to_blob(private, &keyblob, &keylen)) != 0) {
1991: error("%s: sshkey_to_blob: %s", __func__, ssh_err(r));
1992: goto out;
1993: }
1994: if ((r = sshbuf_put_string(b, session_id2, session_id2_len)) != 0 ||
1995: (r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1996: (r = sshbuf_put_cstring(b, authctxt->server_user)) != 0 ||
1.267 djm 1997: (r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||
1.223 djm 1998: (r = sshbuf_put_cstring(b, authctxt->method->name)) != 0 ||
1.279 markus 1999: (r = sshbuf_put_cstring(b, sshkey_ssh_name(private))) != 0 ||
1.223 djm 2000: (r = sshbuf_put_string(b, keyblob, keylen)) != 0 ||
2001: (r = sshbuf_put_cstring(b, chost)) != 0 ||
2002: (r = sshbuf_put_cstring(b, authctxt->local_user)) != 0) {
2003: error("%s: buffer error: %s", __func__, ssh_err(r));
2004: goto out;
2005: }
2006:
1.68 markus 2007: #ifdef DEBUG_PK
1.223 djm 2008: sshbuf_dump(b, stderr);
1.68 markus 2009: #endif
1.281 dtucker 2010: r = ssh_keysign(private, &sig, &siglen,
2011: sshbuf_ptr(b), sshbuf_len(b));
1.223 djm 2012: if (r != 0) {
2013: error("sign using hostkey %s %s failed",
2014: sshkey_ssh_name(private), fp);
2015: goto out;
2016: }
2017: if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
2018: (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
2019: (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
2020: (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
1.279 markus 2021: (r = sshpkt_put_cstring(ssh, sshkey_ssh_name(private))) != 0 ||
1.223 djm 2022: (r = sshpkt_put_string(ssh, keyblob, keylen)) != 0 ||
2023: (r = sshpkt_put_cstring(ssh, chost)) != 0 ||
2024: (r = sshpkt_put_cstring(ssh, authctxt->local_user)) != 0 ||
2025: (r = sshpkt_put_string(ssh, sig, siglen)) != 0 ||
2026: (r = sshpkt_send(ssh)) != 0) {
2027: error("%s: packet error: %s", __func__, ssh_err(r));
2028: goto out;
2029: }
2030: success = 1;
2031:
2032: out:
1.278 markus 2033: if (sig != NULL)
2034: freezero(sig, siglen);
1.223 djm 2035: free(keyblob);
2036: free(lname);
2037: free(fp);
1.197 djm 2038: free(chost);
1.223 djm 2039: sshkey_free(private);
2040: sshbuf_free(b);
1.68 markus 2041:
1.223 djm 2042: return success;
1.23 markus 2043: }
1.170 djm 2044:
1.20 markus 2045: /* find auth method */
2046:
2047: /*
2048: * given auth method name, if configurable options permit this method fill
2049: * in auth_ident field and return true, otherwise return false.
2050: */
1.76 itojun 2051: static int
1.20 markus 2052: authmethod_is_enabled(Authmethod *method)
2053: {
2054: if (method == NULL)
2055: return 0;
2056: /* return false if options indicate this method is disabled */
2057: if (method->enabled == NULL || *method->enabled == 0)
2058: return 0;
2059: /* return false if batch mode is enabled but method needs interactive mode */
2060: if (method->batch_flag != NULL && *method->batch_flag != 0)
2061: return 0;
2062: return 1;
2063: }
2064:
1.76 itojun 2065: static Authmethod *
1.20 markus 2066: authmethod_lookup(const char *name)
1.1 markus 2067: {
1.20 markus 2068: Authmethod *method = NULL;
2069: if (name != NULL)
2070: for (method = authmethods; method->name != NULL; method++)
2071: if (strcmp(name, method->name) == 0)
2072: return method;
2073: debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
2074: return NULL;
2075: }
1.1 markus 2076:
1.53 markus 2077: /* XXX internal state */
2078: static Authmethod *current = NULL;
2079: static char *supported = NULL;
2080: static char *preferred = NULL;
1.105 deraadt 2081:
1.20 markus 2082: /*
2083: * Given the authentication method list sent by the server, return the
2084: * next method we should try. If the server initially sends a nil list,
1.67 markus 2085: * use a built-in default list.
1.41 stevesk 2086: */
1.76 itojun 2087: static Authmethod *
1.20 markus 2088: authmethod_get(char *authlist)
2089: {
1.53 markus 2090: char *name = NULL;
1.97 markus 2091: u_int next;
1.41 stevesk 2092:
1.20 markus 2093: /* Use a suitable default if we're passed a nil list. */
2094: if (authlist == NULL || strlen(authlist) == 0)
1.53 markus 2095: authlist = options.preferred_authentications;
1.20 markus 2096:
1.53 markus 2097: if (supported == NULL || strcmp(authlist, supported) != 0) {
2098: debug3("start over, passed a different list %s", authlist);
1.197 djm 2099: free(supported);
1.53 markus 2100: supported = xstrdup(authlist);
2101: preferred = options.preferred_authentications;
2102: debug3("preferred %s", preferred);
2103: current = NULL;
2104: } else if (current != NULL && authmethod_is_enabled(current))
2105: return current;
1.20 markus 2106:
1.53 markus 2107: for (;;) {
2108: if ((name = match_list(preferred, supported, &next)) == NULL) {
1.109 markus 2109: debug("No more authentication methods to try.");
1.53 markus 2110: current = NULL;
2111: return NULL;
2112: }
2113: preferred += next;
1.23 markus 2114: debug3("authmethod_lookup %s", name);
1.53 markus 2115: debug3("remaining preferred: %s", preferred);
2116: if ((current = authmethod_lookup(name)) != NULL &&
2117: authmethod_is_enabled(current)) {
1.23 markus 2118: debug3("authmethod_is_enabled %s", name);
1.109 markus 2119: debug("Next authentication method: %s", name);
1.197 djm 2120: free(name);
1.53 markus 2121: return current;
1.23 markus 2122: }
1.198 dtucker 2123: free(name);
1.1 markus 2124: }
1.53 markus 2125: }
1.1 markus 2126:
1.79 stevesk 2127: static char *
1.53 markus 2128: authmethods_get(void)
2129: {
2130: Authmethod *method = NULL;
1.278 markus 2131: struct sshbuf *b;
1.93 markus 2132: char *list;
1.278 markus 2133: int r;
1.27 provos 2134:
1.278 markus 2135: if ((b = sshbuf_new()) == NULL)
2136: fatal("%s: sshbuf_new failed", __func__);
1.53 markus 2137: for (method = authmethods; method->name != NULL; method++) {
2138: if (authmethod_is_enabled(method)) {
1.278 markus 2139: if ((r = sshbuf_putf(b, "%s%s",
2140: sshbuf_len(b) ? "," : "", method->name)) != 0)
2141: fatal("%s: buffer error: %s",
2142: __func__, ssh_err(r));
1.53 markus 2143: }
2144: }
1.278 markus 2145: if ((list = sshbuf_dup_string(b)) == NULL)
1.242 djm 2146: fatal("%s: sshbuf_dup_string failed", __func__);
1.278 markus 2147: sshbuf_free(b);
1.93 markus 2148: return list;
1.1 markus 2149: }