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