Annotation of src/usr.bin/ssh/sshconnect2.c, Revision 1.222
1.222 ! djm 1: /* $OpenBSD: sshconnect2.c,v 1.221 2015/01/20 20:16:21 markus 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.1 markus 46: #include "buffer.h"
47: #include "packet.h"
48: #include "compat.h"
1.37 markus 49: #include "cipher.h"
1.160 deraadt 50: #include "key.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.22 provos 68:
1.121 markus 69: #ifdef GSSAPI
70: #include "ssh-gss.h"
71: #endif
72:
1.1 markus 73: /* import */
74: extern char *client_version_string;
75: extern char *server_version_string;
76: extern Options options;
77:
78: /*
79: * SSH2 key exchange
80: */
81:
1.32 markus 82: u_char *session_id2 = NULL;
1.120 markus 83: u_int session_id2_len = 0;
1.1 markus 84:
1.61 markus 85: char *xxx_host;
86: struct sockaddr *xxx_hostaddr;
87:
1.76 itojun 88: static int
1.219 markus 89: verify_host_key_callback(Key *hostkey, struct ssh *ssh)
1.61 markus 90: {
1.75 markus 91: if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
1.83 markus 92: fatal("Host key verification failed.");
1.61 markus 93: return 0;
94: }
95:
1.186 djm 96: static char *
97: order_hostkeyalgs(char *host, struct sockaddr *hostaddr, u_short port)
98: {
99: char *oavail, *avail, *first, *last, *alg, *hostname, *ret;
100: size_t maxlen;
101: struct hostkeys *hostkeys;
102: int ktype;
1.188 djm 103: u_int i;
1.186 djm 104:
105: /* Find all hostkeys for this hostname */
106: get_hostfile_hostname_ipaddr(host, hostaddr, port, &hostname, NULL);
107: hostkeys = init_hostkeys();
1.188 djm 108: for (i = 0; i < options.num_user_hostfiles; i++)
109: load_hostkeys(hostkeys, hostname, options.user_hostfiles[i]);
110: for (i = 0; i < options.num_system_hostfiles; i++)
111: load_hostkeys(hostkeys, hostname, options.system_hostfiles[i]);
1.186 djm 112:
113: oavail = avail = xstrdup(KEX_DEFAULT_PK_ALG);
114: maxlen = strlen(avail) + 1;
115: first = xmalloc(maxlen);
116: last = xmalloc(maxlen);
117: *first = *last = '\0';
118:
119: #define ALG_APPEND(to, from) \
120: do { \
121: if (*to != '\0') \
122: strlcat(to, ",", maxlen); \
123: strlcat(to, from, maxlen); \
124: } while (0)
125:
126: while ((alg = strsep(&avail, ",")) && *alg != '\0') {
1.214 djm 127: if ((ktype = sshkey_type_from_name(alg)) == KEY_UNSPEC)
1.186 djm 128: fatal("%s: unknown alg %s", __func__, alg);
129: if (lookup_key_in_hostkeys_by_type(hostkeys,
1.214 djm 130: sshkey_type_plain(ktype), NULL))
1.186 djm 131: ALG_APPEND(first, alg);
132: else
133: ALG_APPEND(last, alg);
134: }
135: #undef ALG_APPEND
1.216 djm 136: xasprintf(&ret, "%s%s%s", first,
137: (*first == '\0' || *last == '\0') ? "" : ",", last);
1.186 djm 138: if (*first != '\0')
139: debug3("%s: prefer hostkeyalgs: %s", __func__, first);
140:
1.197 djm 141: free(first);
142: free(last);
143: free(hostname);
144: free(oavail);
1.186 djm 145: free_hostkeys(hostkeys);
146:
147: return ret;
148: }
149:
1.1 markus 150: void
1.186 djm 151: ssh_kex2(char *host, struct sockaddr *hostaddr, u_short port)
1.22 provos 152: {
1.205 markus 153: char *myproposal[PROPOSAL_MAX] = { KEX_CLIENT };
1.219 markus 154: struct kex *kex;
1.221 markus 155: int r;
1.61 markus 156:
157: xxx_host = host;
158: xxx_hostaddr = hostaddr;
1.22 provos 159:
1.29 markus 160: if (options.ciphers == (char *)-1) {
1.116 itojun 161: logit("No valid ciphers for protocol version 2 given, using defaults.");
1.29 markus 162: options.ciphers = NULL;
1.24 markus 163: }
1.22 provos 164: if (options.ciphers != NULL) {
165: myproposal[PROPOSAL_ENC_ALGS_CTOS] =
166: myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
167: }
1.60 stevesk 168: myproposal[PROPOSAL_ENC_ALGS_CTOS] =
169: compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
170: myproposal[PROPOSAL_ENC_ALGS_STOC] =
171: compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
1.22 provos 172: if (options.compression) {
1.47 markus 173: myproposal[PROPOSAL_COMP_ALGS_CTOS] =
1.141 markus 174: myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib@openssh.com,zlib,none";
1.22 provos 175: } else {
1.47 markus 176: myproposal[PROPOSAL_COMP_ALGS_CTOS] =
1.141 markus 177: myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com,zlib";
1.47 markus 178: }
179: if (options.macs != NULL) {
180: myproposal[PROPOSAL_MAC_ALGS_CTOS] =
181: myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
1.22 provos 182: }
1.70 markus 183: if (options.hostkeyalgorithms != NULL)
1.88 deraadt 184: myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
1.200 djm 185: compat_pkalg_proposal(options.hostkeyalgorithms);
1.186 djm 186: else {
187: /* Prefer algorithms that we already have keys for */
188: myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
1.200 djm 189: compat_pkalg_proposal(
190: order_hostkeyalgs(host, hostaddr, port));
1.186 djm 191: }
1.185 djm 192: if (options.kex_algorithms != NULL)
193: myproposal[PROPOSAL_KEX_ALGS] = options.kex_algorithms;
1.206 djm 194: myproposal[PROPOSAL_KEX_ALGS] = compat_kex_proposal(
195: myproposal[PROPOSAL_KEX_ALGS]);
1.115 markus 196:
1.196 dtucker 197: if (options.rekey_limit || options.rekey_interval)
198: packet_set_rekey_limits((u_int32_t)options.rekey_limit,
199: (time_t)options.rekey_interval);
1.22 provos 200:
1.65 markus 201: /* start key exchange */
1.221 markus 202: if ((r = kex_setup(active_state, myproposal)) != 0)
203: fatal("kex_setup: %s", ssh_err(r));
1.219 markus 204: kex = active_state->kex;
1.207 markus 205: #ifdef WITH_OPENSSL
1.111 markus 206: kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
1.138 djm 207: kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
1.111 markus 208: kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
1.147 djm 209: kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
1.184 djm 210: kex->kex[KEX_ECDH_SHA2] = kexecdh_client;
1.207 markus 211: #endif
1.199 markus 212: kex->kex[KEX_C25519_SHA256] = kexc25519_client;
1.61 markus 213: kex->client_version_string=client_version_string;
214: kex->server_version_string=server_version_string;
1.75 markus 215: kex->verify_host_key=&verify_host_key_callback;
1.22 provos 216:
1.219 markus 217: dispatch_run(DISPATCH_BLOCK, &kex->done, active_state);
1.173 andreas 218:
219: if (options.use_roaming && !kex->roaming) {
220: debug("Roaming not allowed by server");
221: options.use_roaming = 0;
222: }
1.62 markus 223:
224: session_id2 = kex->session_id;
225: session_id2_len = kex->session_id_len;
1.22 provos 226:
227: #ifdef DEBUG_KEXDH
228: /* send 1st encrypted/maced/compressed message */
229: packet_start(SSH2_MSG_IGNORE);
230: packet_put_cstring("markus");
231: packet_send();
232: packet_write_wait();
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.82 markus 270: /* kbd-interactive */
271: int info_req_seen;
1.121 markus 272: /* generic */
273: void *methoddata;
1.20 markus 274: };
1.214 djm 275:
276: struct cauthmethod {
1.20 markus 277: char *name; /* string to compare against server's list */
278: int (*userauth)(Authctxt *authctxt);
1.170 djm 279: void (*cleanup)(Authctxt *authctxt);
1.20 markus 280: int *enabled; /* flag in option struct that enables method */
281: int *batch_flag; /* flag in option struct that disables method */
282: };
283:
1.218 markus 284: int input_userauth_success(int, u_int32_t, void *);
285: int input_userauth_success_unexpected(int, u_int32_t, void *);
286: int input_userauth_failure(int, u_int32_t, void *);
287: int input_userauth_banner(int, u_int32_t, void *);
288: int input_userauth_error(int, u_int32_t, void *);
289: int input_userauth_info_req(int, u_int32_t, void *);
290: int input_userauth_pk_ok(int, u_int32_t, void *);
291: int input_userauth_passwd_changereq(int, u_int32_t, void *);
1.86 itojun 292:
293: int userauth_none(Authctxt *);
294: int userauth_pubkey(Authctxt *);
295: int userauth_passwd(Authctxt *);
296: int userauth_kbdint(Authctxt *);
297: int userauth_hostbased(Authctxt *);
1.20 markus 298:
1.121 markus 299: #ifdef GSSAPI
300: int userauth_gssapi(Authctxt *authctxt);
1.218 markus 301: int input_gssapi_response(int type, u_int32_t, void *);
302: int input_gssapi_token(int type, u_int32_t, void *);
303: int input_gssapi_hash(int type, u_int32_t, void *);
304: int input_gssapi_error(int, u_int32_t, void *);
305: int input_gssapi_errtok(int, u_int32_t, void *);
1.121 markus 306: #endif
307:
1.86 itojun 308: void userauth(Authctxt *, char *);
1.51 markus 309:
1.117 markus 310: static int sign_and_send_pubkey(Authctxt *, Identity *);
311: static void pubkey_prepare(Authctxt *);
312: static void pubkey_cleanup(Authctxt *);
1.191 dtucker 313: static Key *load_identity_file(char *, int);
1.76 itojun 314:
315: static Authmethod *authmethod_get(char *authlist);
316: static Authmethod *authmethod_lookup(const char *name);
317: static char *authmethods_get(void);
1.20 markus 318:
319: Authmethod authmethods[] = {
1.121 markus 320: #ifdef GSSAPI
1.132 markus 321: {"gssapi-with-mic",
1.121 markus 322: userauth_gssapi,
1.170 djm 323: NULL,
1.121 markus 324: &options.gss_authentication,
325: NULL},
326: #endif
1.81 markus 327: {"hostbased",
328: userauth_hostbased,
1.170 djm 329: NULL,
1.81 markus 330: &options.hostbased_authentication,
331: NULL},
1.20 markus 332: {"publickey",
333: userauth_pubkey,
1.170 djm 334: NULL,
1.28 markus 335: &options.pubkey_authentication,
1.20 markus 336: NULL},
1.81 markus 337: {"keyboard-interactive",
338: userauth_kbdint,
1.170 djm 339: NULL,
1.81 markus 340: &options.kbd_interactive_authentication,
341: &options.batch_mode},
1.20 markus 342: {"password",
343: userauth_passwd,
1.170 djm 344: NULL,
1.20 markus 345: &options.password_authentication,
1.23 markus 346: &options.batch_mode},
347: {"none",
348: userauth_none,
349: NULL,
1.170 djm 350: NULL,
1.23 markus 351: NULL},
1.170 djm 352: {NULL, NULL, NULL, NULL, NULL}
1.20 markus 353: };
354:
355: void
1.68 markus 356: ssh_userauth2(const char *local_user, const char *server_user, char *host,
1.100 markus 357: Sensitive *sensitive)
1.20 markus 358: {
359: Authctxt authctxt;
360: int type;
1.39 markus 361:
1.73 markus 362: if (options.challenge_response_authentication)
1.39 markus 363: options.kbd_interactive_authentication = 1;
1.20 markus 364:
365: packet_start(SSH2_MSG_SERVICE_REQUEST);
366: packet_put_cstring("ssh-userauth");
367: packet_send();
1.108 markus 368: debug("SSH2_MSG_SERVICE_REQUEST sent");
1.20 markus 369: packet_write_wait();
1.91 markus 370: type = packet_read();
1.108 markus 371: if (type != SSH2_MSG_SERVICE_ACCEPT)
372: fatal("Server denied authentication request: %d", type);
1.20 markus 373: if (packet_remaining() > 0) {
1.91 markus 374: char *reply = packet_get_string(NULL);
1.108 markus 375: debug2("service_accept: %s", reply);
1.197 djm 376: free(reply);
1.20 markus 377: } else {
1.109 markus 378: debug2("buggy server: service_accept w/o service");
1.20 markus 379: }
1.90 markus 380: packet_check_eom();
1.108 markus 381: debug("SSH2_MSG_SERVICE_ACCEPT received");
1.20 markus 382:
1.53 markus 383: if (options.preferred_authentications == NULL)
384: options.preferred_authentications = authmethods_get();
385:
1.20 markus 386: /* setup authentication context */
1.82 markus 387: memset(&authctxt, 0, sizeof(authctxt));
1.117 markus 388: pubkey_prepare(&authctxt);
1.20 markus 389: authctxt.server_user = server_user;
1.68 markus 390: authctxt.local_user = local_user;
1.20 markus 391: authctxt.host = host;
392: authctxt.service = "ssh-connection"; /* service name */
393: authctxt.success = 0;
1.23 markus 394: authctxt.method = authmethod_lookup("none");
1.51 markus 395: authctxt.authlist = NULL;
1.121 markus 396: authctxt.methoddata = NULL;
1.100 markus 397: authctxt.sensitive = sensitive;
1.82 markus 398: authctxt.info_req_seen = 0;
1.215 djm 399: authctxt.agent_fd = -1;
1.23 markus 400: if (authctxt.method == NULL)
401: fatal("ssh_userauth2: internal error: cannot send userauth none request");
1.20 markus 402:
403: /* initial userauth request */
1.23 markus 404: userauth_none(&authctxt);
1.20 markus 405:
1.65 markus 406: dispatch_init(&input_userauth_error);
1.20 markus 407: dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
408: dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
1.35 markus 409: dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
1.20 markus 410: dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
411:
1.117 markus 412: pubkey_cleanup(&authctxt);
1.119 markus 413: dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
414:
1.109 markus 415: debug("Authentication succeeded (%s).", authctxt.method->name);
1.20 markus 416: }
1.119 markus 417:
1.20 markus 418: void
1.51 markus 419: userauth(Authctxt *authctxt, char *authlist)
420: {
1.170 djm 421: if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
422: authctxt->method->cleanup(authctxt);
423:
1.197 djm 424: free(authctxt->methoddata);
425: authctxt->methoddata = NULL;
1.51 markus 426: if (authlist == NULL) {
427: authlist = authctxt->authlist;
428: } else {
1.197 djm 429: free(authctxt->authlist);
1.51 markus 430: authctxt->authlist = authlist;
431: }
432: for (;;) {
433: Authmethod *method = authmethod_get(authlist);
434: if (method == NULL)
435: fatal("Permission denied (%s).", authlist);
436: authctxt->method = method;
1.119 markus 437:
438: /* reset the per method handler */
439: dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN,
440: SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
441:
442: /* and try new method */
1.51 markus 443: if (method->userauth(authctxt) != 0) {
444: debug2("we sent a %s packet, wait for reply", method->name);
445: break;
446: } else {
447: debug2("we did not send a packet, disable method");
448: method->enabled = NULL;
449: }
450: }
451: }
1.105 deraadt 452:
1.169 djm 453: /* ARGSUSED */
1.218 markus 454: int
1.92 markus 455: input_userauth_error(int type, u_int32_t seq, void *ctxt)
1.20 markus 456: {
1.35 markus 457: fatal("input_userauth_error: bad message during authentication: "
1.140 djm 458: "type %d", type);
1.218 markus 459: return 0;
1.35 markus 460: }
1.105 deraadt 461:
1.169 djm 462: /* ARGSUSED */
1.218 markus 463: int
1.92 markus 464: input_userauth_banner(int type, u_int32_t seq, void *ctxt)
1.35 markus 465: {
1.166 djm 466: char *msg, *raw, *lang;
467: u_int len;
1.126 deraadt 468:
1.35 markus 469: debug3("input_userauth_banner");
1.166 djm 470: raw = packet_get_string(&len);
1.35 markus 471: lang = packet_get_string(NULL);
1.167 markus 472: if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO) {
1.166 djm 473: if (len > 65536)
474: len = 65536;
1.168 deraadt 475: msg = xmalloc(len * 4 + 1); /* max expansion from strnvis() */
1.177 dtucker 476: strnvis(msg, raw, len * 4 + 1, VIS_SAFE|VIS_OCTAL|VIS_NOSLASH);
1.125 dtucker 477: fprintf(stderr, "%s", msg);
1.197 djm 478: free(msg);
1.166 djm 479: }
1.197 djm 480: free(raw);
481: free(lang);
1.218 markus 482: return 0;
1.20 markus 483: }
1.105 deraadt 484:
1.169 djm 485: /* ARGSUSED */
1.218 markus 486: int
1.92 markus 487: input_userauth_success(int type, u_int32_t seq, void *ctxt)
1.20 markus 488: {
489: Authctxt *authctxt = ctxt;
1.172 djm 490:
1.20 markus 491: if (authctxt == NULL)
492: fatal("input_userauth_success: no authentication context");
1.197 djm 493: free(authctxt->authlist);
494: authctxt->authlist = NULL;
1.172 djm 495: if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
496: authctxt->method->cleanup(authctxt);
1.197 djm 497: free(authctxt->methoddata);
498: authctxt->methoddata = NULL;
1.20 markus 499: authctxt->success = 1; /* break out */
1.218 markus 500: return 0;
1.20 markus 501: }
1.105 deraadt 502:
1.218 markus 503: int
1.172 djm 504: input_userauth_success_unexpected(int type, u_int32_t seq, void *ctxt)
505: {
506: Authctxt *authctxt = ctxt;
507:
508: if (authctxt == NULL)
509: fatal("%s: no authentication context", __func__);
510:
511: fatal("Unexpected authentication success during %s.",
512: authctxt->method->name);
1.218 markus 513: return 0;
1.172 djm 514: }
515:
1.169 djm 516: /* ARGSUSED */
1.218 markus 517: int
1.92 markus 518: input_userauth_failure(int type, u_int32_t seq, void *ctxt)
1.20 markus 519: {
520: Authctxt *authctxt = ctxt;
521: char *authlist = NULL;
522: int partial;
523:
524: if (authctxt == NULL)
525: fatal("input_userauth_failure: no authentication context");
526:
1.23 markus 527: authlist = packet_get_string(NULL);
1.20 markus 528: partial = packet_get_char();
1.90 markus 529: packet_check_eom();
1.20 markus 530:
1.193 markus 531: if (partial != 0) {
1.116 itojun 532: logit("Authenticated with partial success.");
1.193 markus 533: /* reset state */
534: pubkey_cleanup(authctxt);
535: pubkey_prepare(authctxt);
536: }
1.109 markus 537: debug("Authentications that can continue: %s", authlist);
1.20 markus 538:
1.51 markus 539: userauth(authctxt, authlist);
1.218 markus 540: return 0;
1.51 markus 541: }
1.169 djm 542:
543: /* ARGSUSED */
1.218 markus 544: int
1.92 markus 545: input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
1.51 markus 546: {
547: Authctxt *authctxt = ctxt;
548: Key *key = NULL;
1.117 markus 549: Identity *id = NULL;
1.51 markus 550: Buffer b;
1.96 markus 551: int pktype, sent = 0;
552: u_int alen, blen;
553: char *pkalg, *fp;
554: u_char *pkblob;
1.51 markus 555:
556: if (authctxt == NULL)
557: fatal("input_userauth_pk_ok: no authentication context");
558: if (datafellows & SSH_BUG_PKOK) {
559: /* this is similar to SSH_BUG_PKAUTH */
560: debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
561: pkblob = packet_get_string(&blen);
562: buffer_init(&b);
563: buffer_append(&b, pkblob, blen);
564: pkalg = buffer_get_string(&b, &alen);
565: buffer_free(&b);
566: } else {
567: pkalg = packet_get_string(&alen);
568: pkblob = packet_get_string(&blen);
569: }
1.90 markus 570: packet_check_eom();
1.51 markus 571:
1.117 markus 572: debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
1.51 markus 573:
1.117 markus 574: if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
575: debug("unknown pkalg %s", pkalg);
576: goto done;
577: }
578: if ((key = key_from_blob(pkblob, blen)) == NULL) {
579: debug("no key from blob. pkalg %s", pkalg);
580: goto done;
581: }
582: if (key->type != pktype) {
583: error("input_userauth_pk_ok: type mismatch "
584: "for decoded key (received %d, expected %d)",
585: key->type, pktype);
586: goto done;
587: }
1.222 ! djm 588: if ((fp = sshkey_fingerprint(key, options.fingerprint_hash,
! 589: SSH_FP_DEFAULT)) == NULL)
! 590: goto done;
1.117 markus 591: debug2("input_userauth_pk_ok: fp %s", fp);
1.197 djm 592: free(fp);
1.117 markus 593:
1.127 markus 594: /*
595: * search keys in the reverse order, because last candidate has been
596: * moved to the end of the queue. this also avoids confusion by
597: * duplicate keys
598: */
1.136 henning 599: TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
1.117 markus 600: if (key_equal(key, id->key)) {
601: sent = sign_and_send_pubkey(authctxt, id);
1.51 markus 602: break;
603: }
1.117 markus 604: }
605: done:
1.51 markus 606: if (key != NULL)
607: key_free(key);
1.197 djm 608: free(pkalg);
609: free(pkblob);
1.51 markus 610:
1.106 deraadt 611: /* try another method if we did not send a packet */
1.51 markus 612: if (sent == 0)
613: userauth(authctxt, NULL);
1.218 markus 614: return 0;
1.20 markus 615: }
1.121 markus 616:
617: #ifdef GSSAPI
1.133 djm 618: int
1.121 markus 619: userauth_gssapi(Authctxt *authctxt)
620: {
621: Gssctxt *gssctxt = NULL;
1.128 avsm 622: static gss_OID_set gss_supported = NULL;
1.139 djm 623: static u_int mech = 0;
1.121 markus 624: OM_uint32 min;
625: int ok = 0;
626:
627: /* Try one GSSAPI method at a time, rather than sending them all at
628: * once. */
629:
1.128 avsm 630: if (gss_supported == NULL)
631: gss_indicate_mechs(&min, &gss_supported);
1.121 markus 632:
633: /* Check to see if the mechanism is usable before we offer it */
1.128 avsm 634: while (mech < gss_supported->count && !ok) {
1.121 markus 635: /* My DER encoding requires length<128 */
1.128 avsm 636: if (gss_supported->elements[mech].length < 128 &&
1.161 djm 637: ssh_gssapi_check_mechanism(&gssctxt,
638: &gss_supported->elements[mech], authctxt->host)) {
1.121 markus 639: ok = 1; /* Mechanism works */
640: } else {
641: mech++;
642: }
643: }
644:
1.161 djm 645: if (!ok)
1.139 djm 646: return 0;
1.121 markus 647:
648: authctxt->methoddata=(void *)gssctxt;
649:
650: packet_start(SSH2_MSG_USERAUTH_REQUEST);
651: packet_put_cstring(authctxt->server_user);
652: packet_put_cstring(authctxt->service);
653: packet_put_cstring(authctxt->method->name);
654:
655: packet_put_int(1);
656:
1.129 markus 657: packet_put_int((gss_supported->elements[mech].length) + 2);
658: packet_put_char(SSH_GSS_OIDTYPE);
659: packet_put_char(gss_supported->elements[mech].length);
660: packet_put_raw(gss_supported->elements[mech].elements,
661: gss_supported->elements[mech].length);
1.121 markus 662:
663: packet_send();
664:
665: dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
666: dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
667: dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
668: dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
669:
670: mech++; /* Move along to next candidate */
671:
672: return 1;
673: }
674:
1.130 markus 675: static OM_uint32
676: process_gssapi_token(void *ctxt, gss_buffer_t recv_tok)
677: {
678: Authctxt *authctxt = ctxt;
679: Gssctxt *gssctxt = authctxt->methoddata;
680: gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
1.142 djm 681: gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
682: gss_buffer_desc gssbuf;
1.132 markus 683: OM_uint32 status, ms, flags;
684: Buffer b;
1.133 djm 685:
1.130 markus 686: status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
1.132 markus 687: recv_tok, &send_tok, &flags);
1.130 markus 688:
689: if (send_tok.length > 0) {
690: if (GSS_ERROR(status))
691: packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
692: else
693: packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
1.133 djm 694:
1.130 markus 695: packet_put_string(send_tok.value, send_tok.length);
696: packet_send();
697: gss_release_buffer(&ms, &send_tok);
698: }
1.133 djm 699:
1.130 markus 700: if (status == GSS_S_COMPLETE) {
1.132 markus 701: /* send either complete or MIC, depending on mechanism */
702: if (!(flags & GSS_C_INTEG_FLAG)) {
703: packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
704: packet_send();
705: } else {
706: ssh_gssapi_buildmic(&b, authctxt->server_user,
707: authctxt->service, "gssapi-with-mic");
708:
709: gssbuf.value = buffer_ptr(&b);
710: gssbuf.length = buffer_len(&b);
1.133 djm 711:
1.132 markus 712: status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
1.133 djm 713:
1.132 markus 714: if (!GSS_ERROR(status)) {
715: packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC);
716: packet_put_string(mic.value, mic.length);
1.133 djm 717:
1.132 markus 718: packet_send();
719: }
1.133 djm 720:
1.132 markus 721: buffer_free(&b);
722: gss_release_buffer(&ms, &mic);
1.133 djm 723: }
1.130 markus 724: }
1.133 djm 725:
1.130 markus 726: return status;
727: }
728:
1.169 djm 729: /* ARGSUSED */
1.220 djm 730: int
1.121 markus 731: input_gssapi_response(int type, u_int32_t plen, void *ctxt)
732: {
733: Authctxt *authctxt = ctxt;
734: Gssctxt *gssctxt;
735: int oidlen;
736: char *oidv;
737:
738: if (authctxt == NULL)
739: fatal("input_gssapi_response: no authentication context");
740: gssctxt = authctxt->methoddata;
741:
742: /* Setup our OID */
743: oidv = packet_get_string(&oidlen);
744:
1.129 markus 745: if (oidlen <= 2 ||
746: oidv[0] != SSH_GSS_OIDTYPE ||
747: oidv[1] != oidlen - 2) {
1.197 djm 748: free(oidv);
1.129 markus 749: debug("Badly encoded mechanism OID received");
750: userauth(authctxt, NULL);
1.220 djm 751: return 0;
1.121 markus 752: }
1.129 markus 753:
754: if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
755: fatal("Server returned different OID than expected");
1.121 markus 756:
757: packet_check_eom();
758:
1.197 djm 759: free(oidv);
1.121 markus 760:
1.130 markus 761: if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) {
1.121 markus 762: /* Start again with next method on list */
763: debug("Trying to start again");
764: userauth(authctxt, NULL);
1.220 djm 765: return 0;
1.121 markus 766: }
1.220 djm 767: return 0;
1.121 markus 768: }
769:
1.169 djm 770: /* ARGSUSED */
1.220 djm 771: int
1.121 markus 772: input_gssapi_token(int type, u_int32_t plen, void *ctxt)
773: {
774: Authctxt *authctxt = ctxt;
775: gss_buffer_desc recv_tok;
1.130 markus 776: OM_uint32 status;
1.121 markus 777: u_int slen;
778:
779: if (authctxt == NULL)
780: fatal("input_gssapi_response: no authentication context");
781:
782: recv_tok.value = packet_get_string(&slen);
783: recv_tok.length = slen; /* safe typecast */
784:
785: packet_check_eom();
786:
1.130 markus 787: status = process_gssapi_token(ctxt, &recv_tok);
1.121 markus 788:
1.197 djm 789: free(recv_tok.value);
1.121 markus 790:
791: if (GSS_ERROR(status)) {
792: /* Start again with the next method in the list */
793: userauth(authctxt, NULL);
1.220 djm 794: return 0;
1.121 markus 795: }
1.220 djm 796: return 0;
1.121 markus 797: }
798:
1.169 djm 799: /* ARGSUSED */
1.220 djm 800: int
1.121 markus 801: input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
802: {
803: Authctxt *authctxt = ctxt;
804: Gssctxt *gssctxt;
805: gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
806: gss_buffer_desc recv_tok;
1.194 djm 807: OM_uint32 ms;
1.123 deraadt 808: u_int len;
1.121 markus 809:
810: if (authctxt == NULL)
811: fatal("input_gssapi_response: no authentication context");
812: gssctxt = authctxt->methoddata;
813:
1.123 deraadt 814: recv_tok.value = packet_get_string(&len);
815: recv_tok.length = len;
1.121 markus 816:
817: packet_check_eom();
818:
819: /* Stick it into GSSAPI and see what it says */
1.194 djm 820: (void)ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
1.140 djm 821: &recv_tok, &send_tok, NULL);
1.121 markus 822:
1.197 djm 823: free(recv_tok.value);
1.121 markus 824: gss_release_buffer(&ms, &send_tok);
825:
826: /* Server will be returning a failed packet after this one */
1.220 djm 827: return 0;
1.121 markus 828: }
829:
1.169 djm 830: /* ARGSUSED */
1.220 djm 831: int
1.121 markus 832: input_gssapi_error(int type, u_int32_t plen, void *ctxt)
833: {
834: char *msg;
835: char *lang;
836:
1.194 djm 837: /* maj */(void)packet_get_int();
838: /* min */(void)packet_get_int();
1.121 markus 839: msg=packet_get_string(NULL);
840: lang=packet_get_string(NULL);
841:
842: packet_check_eom();
843:
1.143 stevesk 844: debug("Server GSSAPI Error:\n%s", msg);
1.197 djm 845: free(msg);
846: free(lang);
1.220 djm 847: return 0;
1.121 markus 848: }
849: #endif /* GSSAPI */
1.20 markus 850:
1.1 markus 851: int
1.23 markus 852: userauth_none(Authctxt *authctxt)
853: {
854: /* initial userauth request */
855: packet_start(SSH2_MSG_USERAUTH_REQUEST);
856: packet_put_cstring(authctxt->server_user);
857: packet_put_cstring(authctxt->service);
858: packet_put_cstring(authctxt->method->name);
859: packet_send();
860: return 1;
861: }
862:
863: int
1.20 markus 864: userauth_passwd(Authctxt *authctxt)
1.1 markus 865: {
1.6 markus 866: static int attempt = 0;
1.99 markus 867: char prompt[150];
1.1 markus 868: char *password;
1.175 dtucker 869: const char *host = options.host_key_alias ? options.host_key_alias :
870: authctxt->host;
1.6 markus 871:
1.13 todd 872: if (attempt++ >= options.number_of_password_prompts)
1.6 markus 873: return 0;
1.13 todd 874:
1.87 deraadt 875: if (attempt != 1)
1.13 todd 876: error("Permission denied, please try again.");
1.1 markus 877:
1.43 itojun 878: snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
1.175 dtucker 879: authctxt->server_user, host);
1.1 markus 880: password = read_passphrase(prompt, 0);
881: packet_start(SSH2_MSG_USERAUTH_REQUEST);
1.20 markus 882: packet_put_cstring(authctxt->server_user);
883: packet_put_cstring(authctxt->service);
1.23 markus 884: packet_put_cstring(authctxt->method->name);
1.1 markus 885: packet_put_char(0);
1.49 markus 886: packet_put_cstring(password);
1.204 djm 887: explicit_bzero(password, strlen(password));
1.197 djm 888: free(password);
1.85 markus 889: packet_add_padding(64);
1.1 markus 890: packet_send();
1.99 markus 891:
1.104 deraadt 892: dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
1.99 markus 893: &input_userauth_passwd_changereq);
894:
1.1 markus 895: return 1;
896: }
1.169 djm 897:
1.99 markus 898: /*
899: * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
900: */
1.169 djm 901: /* ARGSUSED */
1.218 markus 902: int
1.153 djm 903: input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
1.99 markus 904: {
905: Authctxt *authctxt = ctxt;
906: char *info, *lang, *password = NULL, *retype = NULL;
907: char prompt[150];
1.175 dtucker 908: const char *host = options.host_key_alias ? options.host_key_alias :
909: authctxt->host;
1.99 markus 910:
911: debug2("input_userauth_passwd_changereq");
912:
913: if (authctxt == NULL)
914: fatal("input_userauth_passwd_changereq: "
915: "no authentication context");
916:
917: info = packet_get_string(NULL);
918: lang = packet_get_string(NULL);
919: if (strlen(info) > 0)
1.116 itojun 920: logit("%s", info);
1.197 djm 921: free(info);
922: free(lang);
1.99 markus 923: packet_start(SSH2_MSG_USERAUTH_REQUEST);
924: packet_put_cstring(authctxt->server_user);
925: packet_put_cstring(authctxt->service);
926: packet_put_cstring(authctxt->method->name);
927: packet_put_char(1); /* additional info */
1.104 deraadt 928: snprintf(prompt, sizeof(prompt),
1.99 markus 929: "Enter %.30s@%.128s's old password: ",
1.175 dtucker 930: authctxt->server_user, host);
1.99 markus 931: password = read_passphrase(prompt, 0);
932: packet_put_cstring(password);
1.204 djm 933: explicit_bzero(password, strlen(password));
1.197 djm 934: free(password);
1.99 markus 935: password = NULL;
936: while (password == NULL) {
1.104 deraadt 937: snprintf(prompt, sizeof(prompt),
1.99 markus 938: "Enter %.30s@%.128s's new password: ",
1.175 dtucker 939: authctxt->server_user, host);
1.99 markus 940: password = read_passphrase(prompt, RP_ALLOW_EOF);
941: if (password == NULL) {
942: /* bail out */
1.218 markus 943: return 0;
1.99 markus 944: }
1.104 deraadt 945: snprintf(prompt, sizeof(prompt),
1.99 markus 946: "Retype %.30s@%.128s's new password: ",
1.175 dtucker 947: authctxt->server_user, host);
1.99 markus 948: retype = read_passphrase(prompt, 0);
949: if (strcmp(password, retype) != 0) {
1.204 djm 950: explicit_bzero(password, strlen(password));
1.197 djm 951: free(password);
1.116 itojun 952: logit("Mismatch; try again, EOF to quit.");
1.99 markus 953: password = NULL;
954: }
1.204 djm 955: explicit_bzero(retype, strlen(retype));
1.197 djm 956: free(retype);
1.99 markus 957: }
958: packet_put_cstring(password);
1.204 djm 959: explicit_bzero(password, strlen(password));
1.197 djm 960: free(password);
1.99 markus 961: packet_add_padding(64);
962: packet_send();
1.104 deraadt 963:
964: dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
1.99 markus 965: &input_userauth_passwd_changereq);
1.218 markus 966: return 0;
1.117 markus 967: }
968:
969: static int
1.214 djm 970: identity_sign(struct identity *id, u_char **sigp, size_t *lenp,
971: const u_char *data, size_t datalen, u_int compat)
1.117 markus 972: {
973: Key *prv;
974: int ret;
1.99 markus 975:
1.117 markus 976: /* the agent supports this key */
1.214 djm 977: if (id->agent_fd)
978: return ssh_agent_sign(id->agent_fd, id->key, sigp, lenp,
979: data, datalen, compat);
980:
1.117 markus 981: /*
982: * we have already loaded the private key or
983: * the private key is stored in external hardware
984: */
1.209 djm 985: if (id->isprivate || (id->key->flags & SSHKEY_FLAG_EXT))
1.214 djm 986: return (sshkey_sign(id->key, sigp, lenp, data, datalen,
987: compat));
1.117 markus 988: /* load the private key from the file */
1.191 dtucker 989: if ((prv = load_identity_file(id->filename, id->userprovided)) == NULL)
1.214 djm 990: return (-1); /* XXX return decent error code */
991: ret = sshkey_sign(prv, sigp, lenp, data, datalen, compat);
992: sshkey_free(prv);
1.117 markus 993: return (ret);
1.51 markus 994: }
995:
1.76 itojun 996: static int
1.117 markus 997: sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
1.1 markus 998: {
999: Buffer b;
1.32 markus 1000: u_char *blob, *signature;
1.214 djm 1001: u_int bloblen;
1002: size_t slen;
1.120 markus 1003: u_int skip = 0;
1.17 markus 1004: int ret = -1;
1.23 markus 1005: int have_sig = 1;
1.182 djm 1006: char *fp;
1.1 markus 1007:
1.222 ! djm 1008: if ((fp = sshkey_fingerprint(id->key, options.fingerprint_hash,
! 1009: SSH_FP_DEFAULT)) == NULL)
! 1010: return 0;
1.182 djm 1011: debug3("sign_and_send_pubkey: %s %s", key_type(id->key), fp);
1.197 djm 1012: free(fp);
1.51 markus 1013:
1.117 markus 1014: if (key_to_blob(id->key, &blob, &bloblen) == 0) {
1.28 markus 1015: /* we cannot handle this key */
1.30 markus 1016: debug3("sign_and_send_pubkey: cannot handle key");
1.28 markus 1017: return 0;
1018: }
1.1 markus 1019: /* data to be signed */
1020: buffer_init(&b);
1.26 markus 1021: if (datafellows & SSH_OLD_SESSIONID) {
1022: buffer_append(&b, session_id2, session_id2_len);
1.41 stevesk 1023: skip = session_id2_len;
1.26 markus 1024: } else {
1.14 markus 1025: buffer_put_string(&b, session_id2, session_id2_len);
1026: skip = buffer_len(&b);
1027: }
1.1 markus 1028: buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1.20 markus 1029: buffer_put_cstring(&b, authctxt->server_user);
1.10 markus 1030: buffer_put_cstring(&b,
1.30 markus 1031: datafellows & SSH_BUG_PKSERVICE ?
1.10 markus 1032: "ssh-userauth" :
1.20 markus 1033: authctxt->service);
1.30 markus 1034: if (datafellows & SSH_BUG_PKAUTH) {
1035: buffer_put_char(&b, have_sig);
1036: } else {
1037: buffer_put_cstring(&b, authctxt->method->name);
1038: buffer_put_char(&b, have_sig);
1.117 markus 1039: buffer_put_cstring(&b, key_ssh_name(id->key));
1.30 markus 1040: }
1.1 markus 1041: buffer_put_string(&b, blob, bloblen);
1042:
1043: /* generate signature */
1.117 markus 1044: ret = identity_sign(id, &signature, &slen,
1.214 djm 1045: buffer_ptr(&b), buffer_len(&b), datafellows);
1046: if (ret != 0) {
1.197 djm 1047: free(blob);
1.17 markus 1048: buffer_free(&b);
1049: return 0;
1050: }
1.28 markus 1051: #ifdef DEBUG_PK
1.1 markus 1052: buffer_dump(&b);
1053: #endif
1.30 markus 1054: if (datafellows & SSH_BUG_PKSERVICE) {
1.10 markus 1055: buffer_clear(&b);
1056: buffer_append(&b, session_id2, session_id2_len);
1.51 markus 1057: skip = session_id2_len;
1.10 markus 1058: buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1.20 markus 1059: buffer_put_cstring(&b, authctxt->server_user);
1060: buffer_put_cstring(&b, authctxt->service);
1.23 markus 1061: buffer_put_cstring(&b, authctxt->method->name);
1062: buffer_put_char(&b, have_sig);
1.30 markus 1063: if (!(datafellows & SSH_BUG_PKAUTH))
1.117 markus 1064: buffer_put_cstring(&b, key_ssh_name(id->key));
1.10 markus 1065: buffer_put_string(&b, blob, bloblen);
1066: }
1.197 djm 1067: free(blob);
1.51 markus 1068:
1.1 markus 1069: /* append signature */
1070: buffer_put_string(&b, signature, slen);
1.197 djm 1071: free(signature);
1.1 markus 1072:
1073: /* skip session id and packet type */
1.14 markus 1074: if (buffer_len(&b) < skip + 1)
1.20 markus 1075: fatal("userauth_pubkey: internal error");
1.14 markus 1076: buffer_consume(&b, skip + 1);
1.1 markus 1077:
1078: /* put remaining data from buffer into packet */
1079: packet_start(SSH2_MSG_USERAUTH_REQUEST);
1080: packet_put_raw(buffer_ptr(&b), buffer_len(&b));
1081: buffer_free(&b);
1082: packet_send();
1.17 markus 1083:
1084: return 1;
1.16 markus 1085: }
1086:
1.76 itojun 1087: static int
1.117 markus 1088: send_pubkey_test(Authctxt *authctxt, Identity *id)
1.20 markus 1089: {
1.51 markus 1090: u_char *blob;
1.97 markus 1091: u_int bloblen, have_sig = 0;
1.20 markus 1092:
1.51 markus 1093: debug3("send_pubkey_test");
1.16 markus 1094:
1.117 markus 1095: if (key_to_blob(id->key, &blob, &bloblen) == 0) {
1.51 markus 1096: /* we cannot handle this key */
1097: debug3("send_pubkey_test: cannot handle key");
1.16 markus 1098: return 0;
1099: }
1.51 markus 1100: /* register callback for USERAUTH_PK_OK message */
1101: dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
1102:
1103: packet_start(SSH2_MSG_USERAUTH_REQUEST);
1104: packet_put_cstring(authctxt->server_user);
1105: packet_put_cstring(authctxt->service);
1106: packet_put_cstring(authctxt->method->name);
1107: packet_put_char(have_sig);
1108: if (!(datafellows & SSH_BUG_PKAUTH))
1.117 markus 1109: packet_put_cstring(key_ssh_name(id->key));
1.51 markus 1110: packet_put_string(blob, bloblen);
1.197 djm 1111: free(blob);
1.51 markus 1112: packet_send();
1113: return 1;
1114: }
1115:
1.76 itojun 1116: static Key *
1.191 dtucker 1117: load_identity_file(char *filename, int userprovided)
1.51 markus 1118: {
1119: Key *private;
1120: char prompt[300], *passphrase;
1.215 djm 1121: int r, perm_ok = 0, quit = 0, i;
1.52 markus 1122: struct stat st;
1.16 markus 1123:
1.52 markus 1124: if (stat(filename, &st) < 0) {
1.191 dtucker 1125: (userprovided ? logit : debug3)("no such identity: %s: %s",
1126: filename, strerror(errno));
1.52 markus 1127: return NULL;
1128: }
1.214 djm 1129: snprintf(prompt, sizeof prompt,
1130: "Enter passphrase for key '%.100s': ", filename);
1131: for (i = 0; i <= options.number_of_password_prompts; i++) {
1132: if (i == 0)
1133: passphrase = "";
1134: else {
1.20 markus 1135: passphrase = read_passphrase(prompt, 0);
1.214 djm 1136: if (*passphrase == '\0') {
1.20 markus 1137: debug2("no passphrase given, try next key");
1.214 djm 1138: free(passphrase);
1139: break;
1140: }
1141: }
1142: switch ((r = sshkey_load_private_type(KEY_UNSPEC, filename,
1143: passphrase, &private, NULL, &perm_ok))) {
1144: case 0:
1145: break;
1146: case SSH_ERR_KEY_WRONG_PASSPHRASE:
1147: if (options.batch_mode) {
1148: quit = 1;
1149: break;
1150: }
1.215 djm 1151: if (i != 0)
1152: debug2("bad passphrase given, try again...");
1.214 djm 1153: break;
1154: case SSH_ERR_SYSTEM_ERROR:
1155: if (errno == ENOENT) {
1156: debug2("Load key \"%s\": %s",
1157: filename, ssh_err(r));
1.51 markus 1158: quit = 1;
1.214 djm 1159: break;
1.20 markus 1160: }
1.214 djm 1161: /* FALLTHROUGH */
1162: default:
1163: error("Load key \"%s\": %s", filename, ssh_err(r));
1164: quit = 1;
1165: break;
1166: }
1167: if (i > 0) {
1.204 djm 1168: explicit_bzero(passphrase, strlen(passphrase));
1.197 djm 1169: free(passphrase);
1.16 markus 1170: }
1.214 djm 1171: if (private != NULL || quit)
1172: break;
1.16 markus 1173: }
1.51 markus 1174: return private;
1175: }
1176:
1.117 markus 1177: /*
1178: * try keys in the following order:
1179: * 1. agent keys that are found in the config file
1180: * 2. other agent keys
1181: * 3. keys that are only listed in the config file
1182: */
1183: static void
1184: pubkey_prepare(Authctxt *authctxt)
1.51 markus 1185: {
1.214 djm 1186: struct identity *id, *id2, *tmp;
1187: struct idlist agent, files, *preferred;
1188: struct sshkey *key;
1189: int agent_fd, i, r, found;
1190: size_t j;
1191: struct ssh_identitylist *idlist;
1.51 markus 1192:
1.117 markus 1193: TAILQ_INIT(&agent); /* keys from the agent */
1194: TAILQ_INIT(&files); /* keys from the config file */
1195: preferred = &authctxt->keys;
1196: TAILQ_INIT(preferred); /* preferred order of keys */
1197:
1.190 djm 1198: /* list of keys stored in the filesystem and PKCS#11 */
1.117 markus 1199: for (i = 0; i < options.num_identity_files; i++) {
1200: key = options.identity_keys[i];
1201: if (key && key->type == KEY_RSA1)
1.180 djm 1202: continue;
1203: if (key && key->cert && key->cert->type != SSH2_CERT_TYPE_USER)
1.117 markus 1204: continue;
1205: options.identity_keys[i] = NULL;
1.150 djm 1206: id = xcalloc(1, sizeof(*id));
1.117 markus 1207: id->key = key;
1208: id->filename = xstrdup(options.identity_files[i]);
1.192 dtucker 1209: id->userprovided = options.identity_file_userprovided[i];
1.117 markus 1210: TAILQ_INSERT_TAIL(&files, id, next);
1.190 djm 1211: }
1212: /* Prefer PKCS11 keys that are explicitly listed */
1213: TAILQ_FOREACH_SAFE(id, &files, next, tmp) {
1.209 djm 1214: if (id->key == NULL || (id->key->flags & SSHKEY_FLAG_EXT) == 0)
1.190 djm 1215: continue;
1216: found = 0;
1217: TAILQ_FOREACH(id2, &files, next) {
1218: if (id2->key == NULL ||
1.209 djm 1219: (id2->key->flags & SSHKEY_FLAG_EXT) == 0)
1.190 djm 1220: continue;
1.214 djm 1221: if (sshkey_equal(id->key, id2->key)) {
1.190 djm 1222: TAILQ_REMOVE(&files, id, next);
1223: TAILQ_INSERT_TAIL(preferred, id, next);
1224: found = 1;
1225: break;
1226: }
1227: }
1228: /* If IdentitiesOnly set and key not found then don't use it */
1229: if (!found && options.identities_only) {
1230: TAILQ_REMOVE(&files, id, next);
1.203 tedu 1231: explicit_bzero(id, sizeof(*id));
1.190 djm 1232: free(id);
1233: }
1.117 markus 1234: }
1235: /* list of keys supported by the agent */
1.214 djm 1236: if ((r = ssh_get_authentication_socket(&agent_fd)) != 0) {
1237: if (r != SSH_ERR_AGENT_NOT_PRESENT)
1238: debug("%s: ssh_get_authentication_socket: %s",
1239: __func__, ssh_err(r));
1240: } else if ((r = ssh_fetch_identitylist(agent_fd, 2, &idlist)) != 0) {
1241: if (r != SSH_ERR_AGENT_NO_IDENTITIES)
1242: debug("%s: ssh_fetch_identitylist: %s",
1243: __func__, ssh_err(r));
1244: } else {
1245: for (j = 0; j < idlist->nkeys; j++) {
1.117 markus 1246: found = 0;
1247: TAILQ_FOREACH(id, &files, next) {
1.214 djm 1248: /*
1249: * agent keys from the config file are
1250: * preferred
1251: */
1252: if (sshkey_equal(idlist->keys[j], id->key)) {
1.117 markus 1253: TAILQ_REMOVE(&files, id, next);
1254: TAILQ_INSERT_TAIL(preferred, id, next);
1.214 djm 1255: id->agent_fd = agent_fd;
1.117 markus 1256: found = 1;
1257: break;
1258: }
1259: }
1.135 markus 1260: if (!found && !options.identities_only) {
1.150 djm 1261: id = xcalloc(1, sizeof(*id));
1.214 djm 1262: /* XXX "steals" key/comment from idlist */
1263: id->key = idlist->keys[j];
1264: id->filename = idlist->comments[j];
1265: idlist->keys[j] = NULL;
1266: idlist->comments[j] = NULL;
1267: id->agent_fd = agent_fd;
1.117 markus 1268: TAILQ_INSERT_TAIL(&agent, id, next);
1269: }
1270: }
1.214 djm 1271: ssh_free_identitylist(idlist);
1.117 markus 1272: /* append remaining agent keys */
1273: for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1274: TAILQ_REMOVE(&agent, id, next);
1275: TAILQ_INSERT_TAIL(preferred, id, next);
1276: }
1.214 djm 1277: authctxt->agent_fd = agent_fd;
1.117 markus 1278: }
1279: /* append remaining keys from the config file */
1280: for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1281: TAILQ_REMOVE(&files, id, next);
1282: TAILQ_INSERT_TAIL(preferred, id, next);
1283: }
1284: TAILQ_FOREACH(id, preferred, next) {
1.191 dtucker 1285: debug2("key: %s (%p),%s", id->filename, id->key,
1286: id->userprovided ? " explicit" : "");
1.117 markus 1287: }
1.17 markus 1288: }
1289:
1.117 markus 1290: static void
1291: pubkey_cleanup(Authctxt *authctxt)
1.51 markus 1292: {
1.117 markus 1293: Identity *id;
1.51 markus 1294:
1.214 djm 1295: if (authctxt->agent_fd != -1)
1296: ssh_close_authentication_socket(authctxt->agent_fd);
1.117 markus 1297: for (id = TAILQ_FIRST(&authctxt->keys); id;
1298: id = TAILQ_FIRST(&authctxt->keys)) {
1299: TAILQ_REMOVE(&authctxt->keys, id, next);
1300: if (id->key)
1.214 djm 1301: sshkey_free(id->key);
1.197 djm 1302: free(id->filename);
1303: free(id);
1.117 markus 1304: }
1.1 markus 1305: }
1306:
1.20 markus 1307: int
1308: userauth_pubkey(Authctxt *authctxt)
1309: {
1.117 markus 1310: Identity *id;
1.20 markus 1311: int sent = 0;
1312:
1.117 markus 1313: while ((id = TAILQ_FIRST(&authctxt->keys))) {
1314: if (id->tried++)
1315: return (0);
1.127 markus 1316: /* move key to the end of the queue */
1.117 markus 1317: TAILQ_REMOVE(&authctxt->keys, id, next);
1318: TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1319: /*
1320: * send a test message if we have the public key. for
1321: * encrypted keys we cannot do this and have to load the
1322: * private key instead
1323: */
1.200 djm 1324: if (id->key != NULL) {
1325: if (key_type_plain(id->key->type) == KEY_RSA &&
1326: (datafellows & SSH_BUG_RSASIGMD5) != 0) {
1327: debug("Skipped %s key %s for RSA/MD5 server",
1328: key_type(id->key), id->filename);
1329: } else if (id->key->type != KEY_RSA1) {
1330: debug("Offering %s public key: %s",
1331: key_type(id->key), id->filename);
1332: sent = send_pubkey_test(authctxt, id);
1333: }
1334: } else {
1.117 markus 1335: debug("Trying private key: %s", id->filename);
1.191 dtucker 1336: id->key = load_identity_file(id->filename,
1337: id->userprovided);
1.117 markus 1338: if (id->key != NULL) {
1339: id->isprivate = 1;
1.200 djm 1340: if (key_type_plain(id->key->type) == KEY_RSA &&
1341: (datafellows & SSH_BUG_RSASIGMD5) != 0) {
1342: debug("Skipped %s key %s for RSA/MD5 "
1343: "server", key_type(id->key),
1344: id->filename);
1345: } else {
1346: sent = sign_and_send_pubkey(
1347: authctxt, id);
1348: }
1.117 markus 1349: key_free(id->key);
1350: id->key = NULL;
1.51 markus 1351: }
1352: }
1.117 markus 1353: if (sent)
1354: return (sent);
1.28 markus 1355: }
1.117 markus 1356: return (0);
1.20 markus 1357: }
1358:
1.23 markus 1359: /*
1360: * Send userauth request message specifying keyboard-interactive method.
1361: */
1362: int
1363: userauth_kbdint(Authctxt *authctxt)
1364: {
1365: static int attempt = 0;
1366:
1367: if (attempt++ >= options.number_of_password_prompts)
1368: return 0;
1.82 markus 1369: /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1370: if (attempt > 1 && !authctxt->info_req_seen) {
1371: debug3("userauth_kbdint: disable: no info_req_seen");
1372: dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1373: return 0;
1374: }
1.23 markus 1375:
1376: debug2("userauth_kbdint");
1377: packet_start(SSH2_MSG_USERAUTH_REQUEST);
1378: packet_put_cstring(authctxt->server_user);
1379: packet_put_cstring(authctxt->service);
1380: packet_put_cstring(authctxt->method->name);
1381: packet_put_cstring(""); /* lang */
1382: packet_put_cstring(options.kbd_interactive_devices ?
1383: options.kbd_interactive_devices : "");
1384: packet_send();
1385:
1386: dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1387: return 1;
1388: }
1389:
1390: /*
1.46 markus 1391: * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1.23 markus 1392: */
1.218 markus 1393: int
1.92 markus 1394: input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
1.23 markus 1395: {
1396: Authctxt *authctxt = ctxt;
1.46 markus 1397: char *name, *inst, *lang, *prompt, *response;
1.32 markus 1398: u_int num_prompts, i;
1.23 markus 1399: int echo = 0;
1400:
1401: debug2("input_userauth_info_req");
1402:
1403: if (authctxt == NULL)
1404: fatal("input_userauth_info_req: no authentication context");
1.82 markus 1405:
1406: authctxt->info_req_seen = 1;
1.23 markus 1407:
1408: name = packet_get_string(NULL);
1409: inst = packet_get_string(NULL);
1410: lang = packet_get_string(NULL);
1411: if (strlen(name) > 0)
1.116 itojun 1412: logit("%s", name);
1.23 markus 1413: if (strlen(inst) > 0)
1.116 itojun 1414: logit("%s", inst);
1.197 djm 1415: free(name);
1416: free(inst);
1417: free(lang);
1.23 markus 1418:
1419: num_prompts = packet_get_int();
1420: /*
1421: * Begin to build info response packet based on prompts requested.
1422: * We commit to providing the correct number of responses, so if
1423: * further on we run into a problem that prevents this, we have to
1424: * be sure and clean this up and send a correct error response.
1425: */
1426: packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1427: packet_put_int(num_prompts);
1428:
1.73 markus 1429: debug2("input_userauth_info_req: num_prompts %d", num_prompts);
1.23 markus 1430: for (i = 0; i < num_prompts; i++) {
1431: prompt = packet_get_string(NULL);
1432: echo = packet_get_char();
1433:
1.77 markus 1434: response = read_passphrase(prompt, echo ? RP_ECHO : 0);
1.23 markus 1435:
1.49 markus 1436: packet_put_cstring(response);
1.204 djm 1437: explicit_bzero(response, strlen(response));
1.197 djm 1438: free(response);
1439: free(prompt);
1.23 markus 1440: }
1.90 markus 1441: packet_check_eom(); /* done with parsing incoming message. */
1.23 markus 1442:
1.85 markus 1443: packet_add_padding(64);
1.23 markus 1444: packet_send();
1.218 markus 1445: return 0;
1.68 markus 1446: }
1447:
1.100 markus 1448: static int
1.105 deraadt 1449: ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
1.100 markus 1450: u_char *data, u_int datalen)
1451: {
1452: Buffer b;
1.101 markus 1453: struct stat st;
1.100 markus 1454: pid_t pid;
1.103 markus 1455: int to[2], from[2], status, version = 2;
1.100 markus 1456:
1.109 markus 1457: debug2("ssh_keysign called");
1.100 markus 1458:
1.101 markus 1459: if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
1.179 dtucker 1460: error("ssh_keysign: not installed: %s", strerror(errno));
1.101 markus 1461: return -1;
1462: }
1.100 markus 1463: if (fflush(stdout) != 0)
1464: error("ssh_keysign: fflush: %s", strerror(errno));
1465: if (pipe(to) < 0) {
1466: error("ssh_keysign: pipe: %s", strerror(errno));
1467: return -1;
1468: }
1469: if (pipe(from) < 0) {
1470: error("ssh_keysign: pipe: %s", strerror(errno));
1471: return -1;
1472: }
1473: if ((pid = fork()) < 0) {
1474: error("ssh_keysign: fork: %s", strerror(errno));
1475: return -1;
1476: }
1477: if (pid == 0) {
1.174 dtucker 1478: /* keep the socket on exec */
1479: fcntl(packet_get_connection_in(), F_SETFD, 0);
1.155 markus 1480: permanently_drop_suid(getuid());
1.100 markus 1481: close(from[0]);
1482: if (dup2(from[1], STDOUT_FILENO) < 0)
1483: fatal("ssh_keysign: dup2: %s", strerror(errno));
1484: close(to[1]);
1485: if (dup2(to[0], STDIN_FILENO) < 0)
1486: fatal("ssh_keysign: dup2: %s", strerror(errno));
1.103 markus 1487: close(from[1]);
1488: close(to[0]);
1.102 markus 1489: execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
1.100 markus 1490: fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1491: strerror(errno));
1492: }
1493: close(from[1]);
1494: close(to[0]);
1495:
1496: buffer_init(&b);
1.103 markus 1497: buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
1.100 markus 1498: buffer_put_string(&b, data, datalen);
1.131 djm 1499: if (ssh_msg_send(to[1], version, &b) == -1)
1500: fatal("ssh_keysign: couldn't send request");
1.100 markus 1501:
1.110 djm 1502: if (ssh_msg_recv(from[0], &b) < 0) {
1.101 markus 1503: error("ssh_keysign: no reply");
1.134 markus 1504: buffer_free(&b);
1.100 markus 1505: return -1;
1506: }
1.101 markus 1507: close(from[0]);
1508: close(to[1]);
1509:
1.103 markus 1510: while (waitpid(pid, &status, 0) < 0)
1511: if (errno != EINTR)
1512: break;
1.101 markus 1513:
1.100 markus 1514: if (buffer_get_char(&b) != version) {
1.101 markus 1515: error("ssh_keysign: bad version");
1.134 markus 1516: buffer_free(&b);
1.100 markus 1517: return -1;
1518: }
1519: *sigp = buffer_get_string(&b, lenp);
1.134 markus 1520: buffer_free(&b);
1.100 markus 1521:
1522: return 0;
1523: }
1524:
1.68 markus 1525: int
1526: userauth_hostbased(Authctxt *authctxt)
1527: {
1528: Key *private = NULL;
1.100 markus 1529: Sensitive *sensitive = authctxt->sensitive;
1.68 markus 1530: Buffer b;
1531: u_char *signature, *blob;
1.179 dtucker 1532: char *chost, *pkalg, *p;
1.72 markus 1533: const char *service;
1.68 markus 1534: u_int blen, slen;
1.176 dtucker 1535: int ok, i, found = 0;
1.213 djm 1536:
1537: /* XXX provide some way to allow user to specify key types attempted */
1.68 markus 1538:
1539: /* check for a useful key */
1.100 markus 1540: for (i = 0; i < sensitive->nkeys; i++) {
1541: private = sensitive->keys[i];
1.68 markus 1542: if (private && private->type != KEY_RSA1) {
1543: found = 1;
1544: /* we take and free the key */
1.100 markus 1545: sensitive->keys[i] = NULL;
1.68 markus 1546: break;
1547: }
1548: }
1549: if (!found) {
1.109 markus 1550: debug("No more client hostkeys for hostbased authentication.");
1.68 markus 1551: return 0;
1552: }
1.211 djm 1553:
1554: debug("%s: trying hostkey type %s", __func__, key_type(private));
1555:
1.68 markus 1556: if (key_to_blob(private, &blob, &blen) == 0) {
1557: key_free(private);
1558: return 0;
1559: }
1.211 djm 1560:
1.84 markus 1561: /* figure out a name for the client host */
1.179 dtucker 1562: p = get_local_name(packet_get_connection_in());
1.84 markus 1563: if (p == NULL) {
1564: error("userauth_hostbased: cannot get local ipaddr/name");
1565: key_free(private);
1.197 djm 1566: free(blob);
1.84 markus 1567: return 0;
1568: }
1.150 djm 1569: xasprintf(&chost, "%s.", p);
1.84 markus 1570: debug2("userauth_hostbased: chost %s", chost);
1.197 djm 1571: free(p);
1.84 markus 1572:
1.72 markus 1573: service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1574: authctxt->service;
1.68 markus 1575: pkalg = xstrdup(key_ssh_name(private));
1576: buffer_init(&b);
1577: /* construct data */
1.72 markus 1578: buffer_put_string(&b, session_id2, session_id2_len);
1.68 markus 1579: buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1580: buffer_put_cstring(&b, authctxt->server_user);
1.72 markus 1581: buffer_put_cstring(&b, service);
1.68 markus 1582: buffer_put_cstring(&b, authctxt->method->name);
1583: buffer_put_cstring(&b, pkalg);
1584: buffer_put_string(&b, blob, blen);
1585: buffer_put_cstring(&b, chost);
1586: buffer_put_cstring(&b, authctxt->local_user);
1587: #ifdef DEBUG_PK
1588: buffer_dump(&b);
1589: #endif
1.100 markus 1590: if (sensitive->external_keysign)
1591: ok = ssh_keysign(private, &signature, &slen,
1592: buffer_ptr(&b), buffer_len(&b));
1593: else
1594: ok = key_sign(private, &signature, &slen,
1595: buffer_ptr(&b), buffer_len(&b));
1.68 markus 1596: key_free(private);
1597: buffer_free(&b);
1598: if (ok != 0) {
1599: error("key_sign failed");
1.197 djm 1600: free(chost);
1601: free(pkalg);
1602: free(blob);
1.68 markus 1603: return 0;
1604: }
1605: packet_start(SSH2_MSG_USERAUTH_REQUEST);
1606: packet_put_cstring(authctxt->server_user);
1607: packet_put_cstring(authctxt->service);
1608: packet_put_cstring(authctxt->method->name);
1609: packet_put_cstring(pkalg);
1610: packet_put_string(blob, blen);
1611: packet_put_cstring(chost);
1612: packet_put_cstring(authctxt->local_user);
1613: packet_put_string(signature, slen);
1.204 djm 1614: explicit_bzero(signature, slen);
1.197 djm 1615: free(signature);
1616: free(chost);
1617: free(pkalg);
1618: free(blob);
1.68 markus 1619:
1620: packet_send();
1621: return 1;
1.23 markus 1622: }
1.170 djm 1623:
1.20 markus 1624: /* find auth method */
1625:
1626: /*
1627: * given auth method name, if configurable options permit this method fill
1628: * in auth_ident field and return true, otherwise return false.
1629: */
1.76 itojun 1630: static int
1.20 markus 1631: authmethod_is_enabled(Authmethod *method)
1632: {
1633: if (method == NULL)
1634: return 0;
1635: /* return false if options indicate this method is disabled */
1636: if (method->enabled == NULL || *method->enabled == 0)
1637: return 0;
1638: /* return false if batch mode is enabled but method needs interactive mode */
1639: if (method->batch_flag != NULL && *method->batch_flag != 0)
1640: return 0;
1641: return 1;
1642: }
1643:
1.76 itojun 1644: static Authmethod *
1.20 markus 1645: authmethod_lookup(const char *name)
1.1 markus 1646: {
1.20 markus 1647: Authmethod *method = NULL;
1648: if (name != NULL)
1649: for (method = authmethods; method->name != NULL; method++)
1650: if (strcmp(name, method->name) == 0)
1651: return method;
1652: debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1653: return NULL;
1654: }
1.1 markus 1655:
1.53 markus 1656: /* XXX internal state */
1657: static Authmethod *current = NULL;
1658: static char *supported = NULL;
1659: static char *preferred = NULL;
1.105 deraadt 1660:
1.20 markus 1661: /*
1662: * Given the authentication method list sent by the server, return the
1663: * next method we should try. If the server initially sends a nil list,
1.67 markus 1664: * use a built-in default list.
1.41 stevesk 1665: */
1.76 itojun 1666: static Authmethod *
1.20 markus 1667: authmethod_get(char *authlist)
1668: {
1.53 markus 1669: char *name = NULL;
1.97 markus 1670: u_int next;
1.41 stevesk 1671:
1.20 markus 1672: /* Use a suitable default if we're passed a nil list. */
1673: if (authlist == NULL || strlen(authlist) == 0)
1.53 markus 1674: authlist = options.preferred_authentications;
1.20 markus 1675:
1.53 markus 1676: if (supported == NULL || strcmp(authlist, supported) != 0) {
1677: debug3("start over, passed a different list %s", authlist);
1.197 djm 1678: free(supported);
1.53 markus 1679: supported = xstrdup(authlist);
1680: preferred = options.preferred_authentications;
1681: debug3("preferred %s", preferred);
1682: current = NULL;
1683: } else if (current != NULL && authmethod_is_enabled(current))
1684: return current;
1.20 markus 1685:
1.53 markus 1686: for (;;) {
1687: if ((name = match_list(preferred, supported, &next)) == NULL) {
1.109 markus 1688: debug("No more authentication methods to try.");
1.53 markus 1689: current = NULL;
1690: return NULL;
1691: }
1692: preferred += next;
1.23 markus 1693: debug3("authmethod_lookup %s", name);
1.53 markus 1694: debug3("remaining preferred: %s", preferred);
1695: if ((current = authmethod_lookup(name)) != NULL &&
1696: authmethod_is_enabled(current)) {
1.23 markus 1697: debug3("authmethod_is_enabled %s", name);
1.109 markus 1698: debug("Next authentication method: %s", name);
1.197 djm 1699: free(name);
1.53 markus 1700: return current;
1.23 markus 1701: }
1.198 dtucker 1702: free(name);
1.1 markus 1703: }
1.53 markus 1704: }
1.1 markus 1705:
1.79 stevesk 1706: static char *
1.53 markus 1707: authmethods_get(void)
1708: {
1709: Authmethod *method = NULL;
1.93 markus 1710: Buffer b;
1711: char *list;
1.27 provos 1712:
1.93 markus 1713: buffer_init(&b);
1.53 markus 1714: for (method = authmethods; method->name != NULL; method++) {
1715: if (authmethod_is_enabled(method)) {
1.93 markus 1716: if (buffer_len(&b) > 0)
1717: buffer_append(&b, ",", 1);
1718: buffer_append(&b, method->name, strlen(method->name));
1.53 markus 1719: }
1720: }
1.93 markus 1721: buffer_append(&b, "\0", 1);
1722: list = xstrdup(buffer_ptr(&b));
1723: buffer_free(&b);
1724: return list;
1.1 markus 1725: }
1.170 djm 1726: