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