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