Annotation of src/usr.bin/ssh/sshconnect2.c, Revision 1.169
1.169 ! djm 1: /* $OpenBSD: sshconnect2.c,v 1.168 2008/10/03 23:56:28 deraadt Exp $ */
1.1 markus 2: /*
3: * Copyright (c) 2000 Markus Friedl. All rights reserved.
4: *
5: * Redistribution and use in source and binary forms, with or without
6: * modification, are permitted provided that the following conditions
7: * are met:
8: * 1. Redistributions of source code must retain the above copyright
9: * notice, this list of conditions and the following disclaimer.
10: * 2. Redistributions in binary form must reproduce the above copyright
11: * notice, this list of conditions and the following disclaimer in the
12: * documentation and/or other materials provided with the distribution.
13: *
14: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24: */
25:
1.145 stevesk 26: #include <sys/types.h>
1.160 deraadt 27: #include <sys/socket.h>
1.145 stevesk 28: #include <sys/wait.h>
1.144 stevesk 29: #include <sys/queue.h>
1.146 stevesk 30: #include <sys/stat.h>
1.156 stevesk 31:
32: #include <errno.h>
1.164 jolan 33: #include <netdb.h>
1.159 stevesk 34: #include <stdio.h>
1.158 stevesk 35: #include <string.h>
1.160 deraadt 36: #include <signal.h>
37: #include <pwd.h>
1.157 stevesk 38: #include <unistd.h>
1.166 djm 39: #include <vis.h>
1.1 markus 40:
1.160 deraadt 41: #include "xmalloc.h"
1.1 markus 42: #include "ssh.h"
1.37 markus 43: #include "ssh2.h"
1.1 markus 44: #include "buffer.h"
45: #include "packet.h"
46: #include "compat.h"
1.37 markus 47: #include "cipher.h"
1.160 deraadt 48: #include "key.h"
1.1 markus 49: #include "kex.h"
50: #include "myproposal.h"
51: #include "sshconnect.h"
52: #include "authfile.h"
1.57 provos 53: #include "dh.h"
1.17 markus 54: #include "authfd.h"
1.37 markus 55: #include "log.h"
56: #include "readconf.h"
1.137 djm 57: #include "misc.h"
1.53 markus 58: #include "match.h"
1.61 markus 59: #include "dispatch.h"
1.68 markus 60: #include "canohost.h"
1.100 markus 61: #include "msg.h"
62: #include "pathnames.h"
1.154 markus 63: #include "uidswap.h"
1.22 provos 64:
1.121 markus 65: #ifdef GSSAPI
66: #include "ssh-gss.h"
67: #endif
68:
1.1 markus 69: /* import */
70: extern char *client_version_string;
71: extern char *server_version_string;
72: extern Options options;
73:
74: /*
75: * SSH2 key exchange
76: */
77:
1.32 markus 78: u_char *session_id2 = NULL;
1.120 markus 79: u_int session_id2_len = 0;
1.1 markus 80:
1.61 markus 81: char *xxx_host;
82: struct sockaddr *xxx_hostaddr;
83:
1.63 markus 84: Kex *xxx_kex = NULL;
85:
1.76 itojun 86: static int
1.75 markus 87: verify_host_key_callback(Key *hostkey)
1.61 markus 88: {
1.75 markus 89: if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
1.83 markus 90: fatal("Host key verification failed.");
1.61 markus 91: return 0;
92: }
93:
1.1 markus 94: void
1.22 provos 95: ssh_kex2(char *host, struct sockaddr *hostaddr)
96: {
97: Kex *kex;
1.61 markus 98:
99: xxx_host = host;
100: xxx_hostaddr = hostaddr;
1.22 provos 101:
1.29 markus 102: if (options.ciphers == (char *)-1) {
1.116 itojun 103: logit("No valid ciphers for protocol version 2 given, using defaults.");
1.29 markus 104: options.ciphers = NULL;
1.24 markus 105: }
1.22 provos 106: if (options.ciphers != NULL) {
107: myproposal[PROPOSAL_ENC_ALGS_CTOS] =
108: myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
109: }
1.60 stevesk 110: myproposal[PROPOSAL_ENC_ALGS_CTOS] =
111: compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
112: myproposal[PROPOSAL_ENC_ALGS_STOC] =
113: compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
1.22 provos 114: if (options.compression) {
1.47 markus 115: myproposal[PROPOSAL_COMP_ALGS_CTOS] =
1.141 markus 116: myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib@openssh.com,zlib,none";
1.22 provos 117: } else {
1.47 markus 118: myproposal[PROPOSAL_COMP_ALGS_CTOS] =
1.141 markus 119: myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com,zlib";
1.47 markus 120: }
121: if (options.macs != NULL) {
122: myproposal[PROPOSAL_MAC_ALGS_CTOS] =
123: myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
1.22 provos 124: }
1.70 markus 125: if (options.hostkeyalgorithms != NULL)
1.88 deraadt 126: myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
1.70 markus 127: options.hostkeyalgorithms;
1.115 markus 128:
129: if (options.rekey_limit)
1.165 djm 130: packet_set_rekey_limit((u_int32_t)options.rekey_limit);
1.22 provos 131:
1.65 markus 132: /* start key exchange */
1.64 markus 133: kex = kex_setup(myproposal);
1.111 markus 134: kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
1.138 djm 135: kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
1.111 markus 136: kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
1.147 djm 137: kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
1.61 markus 138: kex->client_version_string=client_version_string;
139: kex->server_version_string=server_version_string;
1.75 markus 140: kex->verify_host_key=&verify_host_key_callback;
1.63 markus 141:
142: xxx_kex = kex;
1.22 provos 143:
1.66 markus 144: dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
1.62 markus 145:
146: session_id2 = kex->session_id;
147: session_id2_len = kex->session_id_len;
1.22 provos 148:
149: #ifdef DEBUG_KEXDH
150: /* send 1st encrypted/maced/compressed message */
151: packet_start(SSH2_MSG_IGNORE);
152: packet_put_cstring("markus");
153: packet_send();
154: packet_write_wait();
155: #endif
1.1 markus 156: }
1.11 markus 157:
1.1 markus 158: /*
159: * Authenticate user
160: */
1.20 markus 161:
162: typedef struct Authctxt Authctxt;
163: typedef struct Authmethod Authmethod;
1.117 markus 164: typedef struct identity Identity;
165: typedef struct idlist Idlist;
1.20 markus 166:
1.117 markus 167: struct identity {
168: TAILQ_ENTRY(identity) next;
169: AuthenticationConnection *ac; /* set if agent supports key */
170: Key *key; /* public/private key */
171: char *filename; /* comment for agent-only keys */
172: int tried;
173: int isprivate; /* key points to the private key */
174: };
175: TAILQ_HEAD(idlist, identity);
1.20 markus 176:
177: struct Authctxt {
178: const char *server_user;
1.68 markus 179: const char *local_user;
1.20 markus 180: const char *host;
181: const char *service;
1.23 markus 182: Authmethod *method;
1.20 markus 183: int success;
1.51 markus 184: char *authlist;
1.68 markus 185: /* pubkey */
1.117 markus 186: Idlist keys;
1.68 markus 187: AuthenticationConnection *agent;
188: /* hostbased */
1.100 markus 189: Sensitive *sensitive;
1.82 markus 190: /* kbd-interactive */
191: int info_req_seen;
1.121 markus 192: /* generic */
193: void *methoddata;
1.20 markus 194: };
195: struct Authmethod {
196: char *name; /* string to compare against server's list */
197: int (*userauth)(Authctxt *authctxt);
198: int *enabled; /* flag in option struct that enables method */
199: int *batch_flag; /* flag in option struct that disables method */
200: };
201:
1.92 markus 202: void input_userauth_success(int, u_int32_t, void *);
203: void input_userauth_failure(int, u_int32_t, void *);
204: void input_userauth_banner(int, u_int32_t, void *);
205: void input_userauth_error(int, u_int32_t, void *);
206: void input_userauth_info_req(int, u_int32_t, void *);
207: void input_userauth_pk_ok(int, u_int32_t, void *);
1.99 markus 208: void input_userauth_passwd_changereq(int, u_int32_t, void *);
1.86 itojun 209:
210: int userauth_none(Authctxt *);
211: int userauth_pubkey(Authctxt *);
212: int userauth_passwd(Authctxt *);
213: int userauth_kbdint(Authctxt *);
214: int userauth_hostbased(Authctxt *);
1.20 markus 215:
1.121 markus 216: #ifdef GSSAPI
217: int userauth_gssapi(Authctxt *authctxt);
218: void input_gssapi_response(int type, u_int32_t, void *);
219: void input_gssapi_token(int type, u_int32_t, void *);
220: void input_gssapi_hash(int type, u_int32_t, void *);
221: void input_gssapi_error(int, u_int32_t, void *);
222: void input_gssapi_errtok(int, u_int32_t, void *);
223: #endif
224:
1.86 itojun 225: void userauth(Authctxt *, char *);
1.51 markus 226:
1.117 markus 227: static int sign_and_send_pubkey(Authctxt *, Identity *);
228: static void pubkey_prepare(Authctxt *);
229: static void pubkey_cleanup(Authctxt *);
230: static Key *load_identity_file(char *);
1.76 itojun 231:
232: static Authmethod *authmethod_get(char *authlist);
233: static Authmethod *authmethod_lookup(const char *name);
234: static char *authmethods_get(void);
1.20 markus 235:
236: Authmethod authmethods[] = {
1.121 markus 237: #ifdef GSSAPI
1.132 markus 238: {"gssapi-with-mic",
1.121 markus 239: userauth_gssapi,
240: &options.gss_authentication,
241: NULL},
242: #endif
1.81 markus 243: {"hostbased",
244: userauth_hostbased,
245: &options.hostbased_authentication,
246: NULL},
1.20 markus 247: {"publickey",
248: userauth_pubkey,
1.28 markus 249: &options.pubkey_authentication,
1.20 markus 250: NULL},
1.81 markus 251: {"keyboard-interactive",
252: userauth_kbdint,
253: &options.kbd_interactive_authentication,
254: &options.batch_mode},
1.20 markus 255: {"password",
256: userauth_passwd,
257: &options.password_authentication,
1.23 markus 258: &options.batch_mode},
259: {"none",
260: userauth_none,
261: NULL,
262: NULL},
1.20 markus 263: {NULL, NULL, NULL, NULL}
264: };
265:
266: void
1.68 markus 267: ssh_userauth2(const char *local_user, const char *server_user, char *host,
1.100 markus 268: Sensitive *sensitive)
1.20 markus 269: {
270: Authctxt authctxt;
271: int type;
1.39 markus 272:
1.73 markus 273: if (options.challenge_response_authentication)
1.39 markus 274: options.kbd_interactive_authentication = 1;
1.20 markus 275:
276: packet_start(SSH2_MSG_SERVICE_REQUEST);
277: packet_put_cstring("ssh-userauth");
278: packet_send();
1.108 markus 279: debug("SSH2_MSG_SERVICE_REQUEST sent");
1.20 markus 280: packet_write_wait();
1.91 markus 281: type = packet_read();
1.108 markus 282: if (type != SSH2_MSG_SERVICE_ACCEPT)
283: fatal("Server denied authentication request: %d", type);
1.20 markus 284: if (packet_remaining() > 0) {
1.91 markus 285: char *reply = packet_get_string(NULL);
1.108 markus 286: debug2("service_accept: %s", reply);
1.20 markus 287: xfree(reply);
288: } else {
1.109 markus 289: debug2("buggy server: service_accept w/o service");
1.20 markus 290: }
1.90 markus 291: packet_check_eom();
1.108 markus 292: debug("SSH2_MSG_SERVICE_ACCEPT received");
1.20 markus 293:
1.53 markus 294: if (options.preferred_authentications == NULL)
295: options.preferred_authentications = authmethods_get();
296:
1.20 markus 297: /* setup authentication context */
1.82 markus 298: memset(&authctxt, 0, sizeof(authctxt));
1.117 markus 299: pubkey_prepare(&authctxt);
1.20 markus 300: authctxt.server_user = server_user;
1.68 markus 301: authctxt.local_user = local_user;
1.20 markus 302: authctxt.host = host;
303: authctxt.service = "ssh-connection"; /* service name */
304: authctxt.success = 0;
1.23 markus 305: authctxt.method = authmethod_lookup("none");
1.51 markus 306: authctxt.authlist = NULL;
1.121 markus 307: authctxt.methoddata = NULL;
1.100 markus 308: authctxt.sensitive = sensitive;
1.82 markus 309: authctxt.info_req_seen = 0;
1.23 markus 310: if (authctxt.method == NULL)
311: fatal("ssh_userauth2: internal error: cannot send userauth none request");
1.20 markus 312:
313: /* initial userauth request */
1.23 markus 314: userauth_none(&authctxt);
1.20 markus 315:
1.65 markus 316: dispatch_init(&input_userauth_error);
1.20 markus 317: dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
318: dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
1.35 markus 319: dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
1.20 markus 320: dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
321:
1.117 markus 322: pubkey_cleanup(&authctxt);
1.119 markus 323: dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
324:
1.109 markus 325: debug("Authentication succeeded (%s).", authctxt.method->name);
1.20 markus 326: }
1.119 markus 327:
1.20 markus 328: void
1.51 markus 329: userauth(Authctxt *authctxt, char *authlist)
330: {
1.121 markus 331: if (authctxt->methoddata) {
332: xfree(authctxt->methoddata);
333: authctxt->methoddata = NULL;
334: }
1.51 markus 335: if (authlist == NULL) {
336: authlist = authctxt->authlist;
337: } else {
338: if (authctxt->authlist)
339: xfree(authctxt->authlist);
340: authctxt->authlist = authlist;
341: }
342: for (;;) {
343: Authmethod *method = authmethod_get(authlist);
344: if (method == NULL)
345: fatal("Permission denied (%s).", authlist);
346: authctxt->method = method;
1.119 markus 347:
348: /* reset the per method handler */
349: dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN,
350: SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
351:
352: /* and try new method */
1.51 markus 353: if (method->userauth(authctxt) != 0) {
354: debug2("we sent a %s packet, wait for reply", method->name);
355: break;
356: } else {
357: debug2("we did not send a packet, disable method");
358: method->enabled = NULL;
359: }
360: }
361: }
1.105 deraadt 362:
1.169 ! djm 363: /* ARGSUSED */
1.51 markus 364: void
1.92 markus 365: input_userauth_error(int type, u_int32_t seq, void *ctxt)
1.20 markus 366: {
1.35 markus 367: fatal("input_userauth_error: bad message during authentication: "
1.140 djm 368: "type %d", type);
1.35 markus 369: }
1.105 deraadt 370:
1.169 ! djm 371: /* ARGSUSED */
1.35 markus 372: void
1.92 markus 373: input_userauth_banner(int type, u_int32_t seq, void *ctxt)
1.35 markus 374: {
1.166 djm 375: char *msg, *raw, *lang;
376: u_int len;
1.126 deraadt 377:
1.35 markus 378: debug3("input_userauth_banner");
1.166 djm 379: raw = packet_get_string(&len);
1.35 markus 380: lang = packet_get_string(NULL);
1.167 markus 381: if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO) {
1.166 djm 382: if (len > 65536)
383: len = 65536;
1.168 deraadt 384: msg = xmalloc(len * 4 + 1); /* max expansion from strnvis() */
385: strnvis(msg, raw, len * 4 + 1, VIS_SAFE|VIS_OCTAL);
1.125 dtucker 386: fprintf(stderr, "%s", msg);
1.166 djm 387: xfree(msg);
388: }
389: xfree(raw);
1.35 markus 390: xfree(lang);
1.20 markus 391: }
1.105 deraadt 392:
1.169 ! djm 393: /* ARGSUSED */
1.20 markus 394: void
1.92 markus 395: input_userauth_success(int type, u_int32_t seq, void *ctxt)
1.20 markus 396: {
397: Authctxt *authctxt = ctxt;
398: if (authctxt == NULL)
399: fatal("input_userauth_success: no authentication context");
1.126 deraadt 400: if (authctxt->authlist) {
1.51 markus 401: xfree(authctxt->authlist);
1.126 deraadt 402: authctxt->authlist = NULL;
403: }
404: if (authctxt->methoddata) {
1.121 markus 405: xfree(authctxt->methoddata);
1.126 deraadt 406: authctxt->methoddata = NULL;
407: }
1.20 markus 408: authctxt->success = 1; /* break out */
409: }
1.105 deraadt 410:
1.169 ! djm 411: /* ARGSUSED */
1.20 markus 412: void
1.92 markus 413: input_userauth_failure(int type, u_int32_t seq, void *ctxt)
1.20 markus 414: {
415: Authctxt *authctxt = ctxt;
416: char *authlist = NULL;
417: int partial;
418:
419: if (authctxt == NULL)
420: fatal("input_userauth_failure: no authentication context");
421:
1.23 markus 422: authlist = packet_get_string(NULL);
1.20 markus 423: partial = packet_get_char();
1.90 markus 424: packet_check_eom();
1.20 markus 425:
426: if (partial != 0)
1.116 itojun 427: logit("Authenticated with partial success.");
1.109 markus 428: debug("Authentications that can continue: %s", authlist);
1.20 markus 429:
1.51 markus 430: userauth(authctxt, authlist);
431: }
1.169 ! djm 432:
! 433: /* ARGSUSED */
1.51 markus 434: void
1.92 markus 435: input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
1.51 markus 436: {
437: Authctxt *authctxt = ctxt;
438: Key *key = NULL;
1.117 markus 439: Identity *id = NULL;
1.51 markus 440: Buffer b;
1.96 markus 441: int pktype, sent = 0;
442: u_int alen, blen;
443: char *pkalg, *fp;
444: u_char *pkblob;
1.51 markus 445:
446: if (authctxt == NULL)
447: fatal("input_userauth_pk_ok: no authentication context");
448: if (datafellows & SSH_BUG_PKOK) {
449: /* this is similar to SSH_BUG_PKAUTH */
450: debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
451: pkblob = packet_get_string(&blen);
452: buffer_init(&b);
453: buffer_append(&b, pkblob, blen);
454: pkalg = buffer_get_string(&b, &alen);
455: buffer_free(&b);
456: } else {
457: pkalg = packet_get_string(&alen);
458: pkblob = packet_get_string(&blen);
459: }
1.90 markus 460: packet_check_eom();
1.51 markus 461:
1.117 markus 462: debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
1.51 markus 463:
1.117 markus 464: if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
465: debug("unknown pkalg %s", pkalg);
466: goto done;
467: }
468: if ((key = key_from_blob(pkblob, blen)) == NULL) {
469: debug("no key from blob. pkalg %s", pkalg);
470: goto done;
471: }
472: if (key->type != pktype) {
473: error("input_userauth_pk_ok: type mismatch "
474: "for decoded key (received %d, expected %d)",
475: key->type, pktype);
476: goto done;
477: }
478: fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
479: debug2("input_userauth_pk_ok: fp %s", fp);
480: xfree(fp);
481:
1.127 markus 482: /*
483: * search keys in the reverse order, because last candidate has been
484: * moved to the end of the queue. this also avoids confusion by
485: * duplicate keys
486: */
1.136 henning 487: TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
1.117 markus 488: if (key_equal(key, id->key)) {
489: sent = sign_and_send_pubkey(authctxt, id);
1.51 markus 490: break;
491: }
1.117 markus 492: }
493: done:
1.51 markus 494: if (key != NULL)
495: key_free(key);
496: xfree(pkalg);
497: xfree(pkblob);
498:
1.106 deraadt 499: /* try another method if we did not send a packet */
1.51 markus 500: if (sent == 0)
501: userauth(authctxt, NULL);
1.20 markus 502: }
1.121 markus 503:
504: #ifdef GSSAPI
1.133 djm 505: int
1.121 markus 506: userauth_gssapi(Authctxt *authctxt)
507: {
508: Gssctxt *gssctxt = NULL;
1.128 avsm 509: static gss_OID_set gss_supported = NULL;
1.139 djm 510: static u_int mech = 0;
1.121 markus 511: OM_uint32 min;
512: int ok = 0;
513:
514: /* Try one GSSAPI method at a time, rather than sending them all at
515: * once. */
516:
1.128 avsm 517: if (gss_supported == NULL)
518: gss_indicate_mechs(&min, &gss_supported);
1.121 markus 519:
520: /* Check to see if the mechanism is usable before we offer it */
1.128 avsm 521: while (mech < gss_supported->count && !ok) {
1.121 markus 522: /* My DER encoding requires length<128 */
1.128 avsm 523: if (gss_supported->elements[mech].length < 128 &&
1.161 djm 524: ssh_gssapi_check_mechanism(&gssctxt,
525: &gss_supported->elements[mech], authctxt->host)) {
1.121 markus 526: ok = 1; /* Mechanism works */
527: } else {
528: mech++;
529: }
530: }
531:
1.161 djm 532: if (!ok)
1.139 djm 533: return 0;
1.121 markus 534:
535: authctxt->methoddata=(void *)gssctxt;
536:
537: packet_start(SSH2_MSG_USERAUTH_REQUEST);
538: packet_put_cstring(authctxt->server_user);
539: packet_put_cstring(authctxt->service);
540: packet_put_cstring(authctxt->method->name);
541:
542: packet_put_int(1);
543:
1.129 markus 544: packet_put_int((gss_supported->elements[mech].length) + 2);
545: packet_put_char(SSH_GSS_OIDTYPE);
546: packet_put_char(gss_supported->elements[mech].length);
547: packet_put_raw(gss_supported->elements[mech].elements,
548: gss_supported->elements[mech].length);
1.121 markus 549:
550: packet_send();
551:
552: dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
553: dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
554: dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
555: dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
556:
557: mech++; /* Move along to next candidate */
558:
559: return 1;
560: }
561:
1.130 markus 562: static OM_uint32
563: process_gssapi_token(void *ctxt, gss_buffer_t recv_tok)
564: {
565: Authctxt *authctxt = ctxt;
566: Gssctxt *gssctxt = authctxt->methoddata;
567: gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
1.142 djm 568: gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
569: gss_buffer_desc gssbuf;
1.132 markus 570: OM_uint32 status, ms, flags;
571: Buffer b;
1.133 djm 572:
1.130 markus 573: status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
1.132 markus 574: recv_tok, &send_tok, &flags);
1.130 markus 575:
576: if (send_tok.length > 0) {
577: if (GSS_ERROR(status))
578: packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
579: else
580: packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
1.133 djm 581:
1.130 markus 582: packet_put_string(send_tok.value, send_tok.length);
583: packet_send();
584: gss_release_buffer(&ms, &send_tok);
585: }
1.133 djm 586:
1.130 markus 587: if (status == GSS_S_COMPLETE) {
1.132 markus 588: /* send either complete or MIC, depending on mechanism */
589: if (!(flags & GSS_C_INTEG_FLAG)) {
590: packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
591: packet_send();
592: } else {
593: ssh_gssapi_buildmic(&b, authctxt->server_user,
594: authctxt->service, "gssapi-with-mic");
595:
596: gssbuf.value = buffer_ptr(&b);
597: gssbuf.length = buffer_len(&b);
1.133 djm 598:
1.132 markus 599: status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
1.133 djm 600:
1.132 markus 601: if (!GSS_ERROR(status)) {
602: packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC);
603: packet_put_string(mic.value, mic.length);
1.133 djm 604:
1.132 markus 605: packet_send();
606: }
1.133 djm 607:
1.132 markus 608: buffer_free(&b);
609: gss_release_buffer(&ms, &mic);
1.133 djm 610: }
1.130 markus 611: }
1.133 djm 612:
1.130 markus 613: return status;
614: }
615:
1.169 ! djm 616: /* ARGSUSED */
1.121 markus 617: void
618: input_gssapi_response(int type, u_int32_t plen, void *ctxt)
619: {
620: Authctxt *authctxt = ctxt;
621: Gssctxt *gssctxt;
622: int oidlen;
623: char *oidv;
624:
625: if (authctxt == NULL)
626: fatal("input_gssapi_response: no authentication context");
627: gssctxt = authctxt->methoddata;
628:
629: /* Setup our OID */
630: oidv = packet_get_string(&oidlen);
631:
1.129 markus 632: if (oidlen <= 2 ||
633: oidv[0] != SSH_GSS_OIDTYPE ||
634: oidv[1] != oidlen - 2) {
1.130 markus 635: xfree(oidv);
1.129 markus 636: debug("Badly encoded mechanism OID received");
637: userauth(authctxt, NULL);
638: return;
1.121 markus 639: }
1.129 markus 640:
641: if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
642: fatal("Server returned different OID than expected");
1.121 markus 643:
644: packet_check_eom();
645:
646: xfree(oidv);
647:
1.130 markus 648: if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) {
1.121 markus 649: /* Start again with next method on list */
650: debug("Trying to start again");
651: userauth(authctxt, NULL);
652: return;
653: }
654: }
655:
1.169 ! djm 656: /* ARGSUSED */
1.121 markus 657: void
658: input_gssapi_token(int type, u_int32_t plen, void *ctxt)
659: {
660: Authctxt *authctxt = ctxt;
661: gss_buffer_desc recv_tok;
1.130 markus 662: OM_uint32 status;
1.121 markus 663: u_int slen;
664:
665: if (authctxt == NULL)
666: fatal("input_gssapi_response: no authentication context");
667:
668: recv_tok.value = packet_get_string(&slen);
669: recv_tok.length = slen; /* safe typecast */
670:
671: packet_check_eom();
672:
1.130 markus 673: status = process_gssapi_token(ctxt, &recv_tok);
1.121 markus 674:
675: xfree(recv_tok.value);
676:
677: if (GSS_ERROR(status)) {
678: /* Start again with the next method in the list */
679: userauth(authctxt, NULL);
680: return;
681: }
682: }
683:
1.169 ! djm 684: /* ARGSUSED */
1.121 markus 685: void
686: input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
687: {
688: Authctxt *authctxt = ctxt;
689: Gssctxt *gssctxt;
690: gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
691: gss_buffer_desc recv_tok;
692: OM_uint32 status, ms;
1.123 deraadt 693: u_int len;
1.121 markus 694:
695: if (authctxt == NULL)
696: fatal("input_gssapi_response: no authentication context");
697: gssctxt = authctxt->methoddata;
698:
1.123 deraadt 699: recv_tok.value = packet_get_string(&len);
700: recv_tok.length = len;
1.121 markus 701:
702: packet_check_eom();
703:
704: /* Stick it into GSSAPI and see what it says */
705: status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
1.140 djm 706: &recv_tok, &send_tok, NULL);
1.121 markus 707:
708: xfree(recv_tok.value);
709: gss_release_buffer(&ms, &send_tok);
710:
711: /* Server will be returning a failed packet after this one */
712: }
713:
1.169 ! djm 714: /* ARGSUSED */
1.121 markus 715: void
716: input_gssapi_error(int type, u_int32_t plen, void *ctxt)
717: {
718: OM_uint32 maj, min;
719: char *msg;
720: char *lang;
721:
722: maj=packet_get_int();
723: min=packet_get_int();
724: msg=packet_get_string(NULL);
725: lang=packet_get_string(NULL);
726:
727: packet_check_eom();
728:
1.143 stevesk 729: debug("Server GSSAPI Error:\n%s", msg);
1.121 markus 730: xfree(msg);
731: xfree(lang);
732: }
733: #endif /* GSSAPI */
1.20 markus 734:
1.1 markus 735: int
1.23 markus 736: userauth_none(Authctxt *authctxt)
737: {
738: /* initial userauth request */
739: packet_start(SSH2_MSG_USERAUTH_REQUEST);
740: packet_put_cstring(authctxt->server_user);
741: packet_put_cstring(authctxt->service);
742: packet_put_cstring(authctxt->method->name);
743: packet_send();
744: return 1;
745: }
746:
747: int
1.20 markus 748: userauth_passwd(Authctxt *authctxt)
1.1 markus 749: {
1.6 markus 750: static int attempt = 0;
1.99 markus 751: char prompt[150];
1.1 markus 752: char *password;
1.6 markus 753:
1.13 todd 754: if (attempt++ >= options.number_of_password_prompts)
1.6 markus 755: return 0;
1.13 todd 756:
1.87 deraadt 757: if (attempt != 1)
1.13 todd 758: error("Permission denied, please try again.");
1.1 markus 759:
1.43 itojun 760: snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
1.20 markus 761: authctxt->server_user, authctxt->host);
1.1 markus 762: password = read_passphrase(prompt, 0);
763: packet_start(SSH2_MSG_USERAUTH_REQUEST);
1.20 markus 764: packet_put_cstring(authctxt->server_user);
765: packet_put_cstring(authctxt->service);
1.23 markus 766: packet_put_cstring(authctxt->method->name);
1.1 markus 767: packet_put_char(0);
1.49 markus 768: packet_put_cstring(password);
1.1 markus 769: memset(password, 0, strlen(password));
770: xfree(password);
1.85 markus 771: packet_add_padding(64);
1.1 markus 772: packet_send();
1.99 markus 773:
1.104 deraadt 774: dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
1.99 markus 775: &input_userauth_passwd_changereq);
776:
1.1 markus 777: return 1;
778: }
1.169 ! djm 779:
1.99 markus 780: /*
781: * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
782: */
1.169 ! djm 783: /* ARGSUSED */
1.99 markus 784: void
1.153 djm 785: input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
1.99 markus 786: {
787: Authctxt *authctxt = ctxt;
788: char *info, *lang, *password = NULL, *retype = NULL;
789: char prompt[150];
790:
791: debug2("input_userauth_passwd_changereq");
792:
793: if (authctxt == NULL)
794: fatal("input_userauth_passwd_changereq: "
795: "no authentication context");
796:
797: info = packet_get_string(NULL);
798: lang = packet_get_string(NULL);
799: if (strlen(info) > 0)
1.116 itojun 800: logit("%s", info);
1.99 markus 801: xfree(info);
802: xfree(lang);
803: packet_start(SSH2_MSG_USERAUTH_REQUEST);
804: packet_put_cstring(authctxt->server_user);
805: packet_put_cstring(authctxt->service);
806: packet_put_cstring(authctxt->method->name);
807: packet_put_char(1); /* additional info */
1.104 deraadt 808: snprintf(prompt, sizeof(prompt),
1.99 markus 809: "Enter %.30s@%.128s's old password: ",
810: authctxt->server_user, authctxt->host);
811: password = read_passphrase(prompt, 0);
812: packet_put_cstring(password);
813: memset(password, 0, strlen(password));
814: xfree(password);
815: password = NULL;
816: while (password == NULL) {
1.104 deraadt 817: snprintf(prompt, sizeof(prompt),
1.99 markus 818: "Enter %.30s@%.128s's new password: ",
819: authctxt->server_user, authctxt->host);
820: password = read_passphrase(prompt, RP_ALLOW_EOF);
821: if (password == NULL) {
822: /* bail out */
823: return;
824: }
1.104 deraadt 825: snprintf(prompt, sizeof(prompt),
1.99 markus 826: "Retype %.30s@%.128s's new password: ",
827: authctxt->server_user, authctxt->host);
828: retype = read_passphrase(prompt, 0);
829: if (strcmp(password, retype) != 0) {
830: memset(password, 0, strlen(password));
831: xfree(password);
1.116 itojun 832: logit("Mismatch; try again, EOF to quit.");
1.99 markus 833: password = NULL;
834: }
835: memset(retype, 0, strlen(retype));
836: xfree(retype);
837: }
838: packet_put_cstring(password);
839: memset(password, 0, strlen(password));
840: xfree(password);
841: packet_add_padding(64);
842: packet_send();
1.104 deraadt 843:
844: dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
1.99 markus 845: &input_userauth_passwd_changereq);
1.117 markus 846: }
847:
848: static int
849: identity_sign(Identity *id, u_char **sigp, u_int *lenp,
850: u_char *data, u_int datalen)
851: {
852: Key *prv;
853: int ret;
1.99 markus 854:
1.117 markus 855: /* the agent supports this key */
856: if (id->ac)
857: return (ssh_agent_sign(id->ac, id->key, sigp, lenp,
858: data, datalen));
859: /*
860: * we have already loaded the private key or
861: * the private key is stored in external hardware
862: */
863: if (id->isprivate || (id->key->flags & KEY_FLAG_EXT))
864: return (key_sign(id->key, sigp, lenp, data, datalen));
865: /* load the private key from the file */
866: if ((prv = load_identity_file(id->filename)) == NULL)
867: return (-1);
868: ret = key_sign(prv, sigp, lenp, data, datalen);
869: key_free(prv);
870: return (ret);
1.51 markus 871: }
872:
1.76 itojun 873: static int
1.117 markus 874: sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
1.1 markus 875: {
876: Buffer b;
1.32 markus 877: u_char *blob, *signature;
1.96 markus 878: u_int bloblen, slen;
1.120 markus 879: u_int skip = 0;
1.17 markus 880: int ret = -1;
1.23 markus 881: int have_sig = 1;
1.1 markus 882:
1.30 markus 883: debug3("sign_and_send_pubkey");
1.51 markus 884:
1.117 markus 885: if (key_to_blob(id->key, &blob, &bloblen) == 0) {
1.28 markus 886: /* we cannot handle this key */
1.30 markus 887: debug3("sign_and_send_pubkey: cannot handle key");
1.28 markus 888: return 0;
889: }
1.1 markus 890: /* data to be signed */
891: buffer_init(&b);
1.26 markus 892: if (datafellows & SSH_OLD_SESSIONID) {
893: buffer_append(&b, session_id2, session_id2_len);
1.41 stevesk 894: skip = session_id2_len;
1.26 markus 895: } else {
1.14 markus 896: buffer_put_string(&b, session_id2, session_id2_len);
897: skip = buffer_len(&b);
898: }
1.1 markus 899: buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1.20 markus 900: buffer_put_cstring(&b, authctxt->server_user);
1.10 markus 901: buffer_put_cstring(&b,
1.30 markus 902: datafellows & SSH_BUG_PKSERVICE ?
1.10 markus 903: "ssh-userauth" :
1.20 markus 904: authctxt->service);
1.30 markus 905: if (datafellows & SSH_BUG_PKAUTH) {
906: buffer_put_char(&b, have_sig);
907: } else {
908: buffer_put_cstring(&b, authctxt->method->name);
909: buffer_put_char(&b, have_sig);
1.117 markus 910: buffer_put_cstring(&b, key_ssh_name(id->key));
1.30 markus 911: }
1.1 markus 912: buffer_put_string(&b, blob, bloblen);
913:
914: /* generate signature */
1.117 markus 915: ret = identity_sign(id, &signature, &slen,
1.51 markus 916: buffer_ptr(&b), buffer_len(&b));
1.17 markus 917: if (ret == -1) {
918: xfree(blob);
919: buffer_free(&b);
920: return 0;
921: }
1.28 markus 922: #ifdef DEBUG_PK
1.1 markus 923: buffer_dump(&b);
924: #endif
1.30 markus 925: if (datafellows & SSH_BUG_PKSERVICE) {
1.10 markus 926: buffer_clear(&b);
927: buffer_append(&b, session_id2, session_id2_len);
1.51 markus 928: skip = session_id2_len;
1.10 markus 929: buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1.20 markus 930: buffer_put_cstring(&b, authctxt->server_user);
931: buffer_put_cstring(&b, authctxt->service);
1.23 markus 932: buffer_put_cstring(&b, authctxt->method->name);
933: buffer_put_char(&b, have_sig);
1.30 markus 934: if (!(datafellows & SSH_BUG_PKAUTH))
1.117 markus 935: buffer_put_cstring(&b, key_ssh_name(id->key));
1.10 markus 936: buffer_put_string(&b, blob, bloblen);
937: }
938: xfree(blob);
1.51 markus 939:
1.1 markus 940: /* append signature */
941: buffer_put_string(&b, signature, slen);
942: xfree(signature);
943:
944: /* skip session id and packet type */
1.14 markus 945: if (buffer_len(&b) < skip + 1)
1.20 markus 946: fatal("userauth_pubkey: internal error");
1.14 markus 947: buffer_consume(&b, skip + 1);
1.1 markus 948:
949: /* put remaining data from buffer into packet */
950: packet_start(SSH2_MSG_USERAUTH_REQUEST);
951: packet_put_raw(buffer_ptr(&b), buffer_len(&b));
952: buffer_free(&b);
953: packet_send();
1.17 markus 954:
955: return 1;
1.16 markus 956: }
957:
1.76 itojun 958: static int
1.117 markus 959: send_pubkey_test(Authctxt *authctxt, Identity *id)
1.20 markus 960: {
1.51 markus 961: u_char *blob;
1.97 markus 962: u_int bloblen, have_sig = 0;
1.20 markus 963:
1.51 markus 964: debug3("send_pubkey_test");
1.16 markus 965:
1.117 markus 966: if (key_to_blob(id->key, &blob, &bloblen) == 0) {
1.51 markus 967: /* we cannot handle this key */
968: debug3("send_pubkey_test: cannot handle key");
1.16 markus 969: return 0;
970: }
1.51 markus 971: /* register callback for USERAUTH_PK_OK message */
972: dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
973:
974: packet_start(SSH2_MSG_USERAUTH_REQUEST);
975: packet_put_cstring(authctxt->server_user);
976: packet_put_cstring(authctxt->service);
977: packet_put_cstring(authctxt->method->name);
978: packet_put_char(have_sig);
979: if (!(datafellows & SSH_BUG_PKAUTH))
1.117 markus 980: packet_put_cstring(key_ssh_name(id->key));
1.51 markus 981: packet_put_string(blob, bloblen);
982: xfree(blob);
983: packet_send();
984: return 1;
985: }
986:
1.76 itojun 987: static Key *
1.51 markus 988: load_identity_file(char *filename)
989: {
990: Key *private;
991: char prompt[300], *passphrase;
1.152 dtucker 992: int perm_ok, quit, i;
1.52 markus 993: struct stat st;
1.16 markus 994:
1.52 markus 995: if (stat(filename, &st) < 0) {
996: debug3("no such identity: %s", filename);
997: return NULL;
998: }
1.152 dtucker 999: private = key_load_private_type(KEY_UNSPEC, filename, "", NULL, &perm_ok);
1000: if (!perm_ok)
1001: return NULL;
1.56 markus 1002: if (private == NULL) {
1003: if (options.batch_mode)
1.51 markus 1004: return NULL;
1.16 markus 1005: snprintf(prompt, sizeof prompt,
1.88 deraadt 1006: "Enter passphrase for key '%.100s': ", filename);
1.20 markus 1007: for (i = 0; i < options.number_of_password_prompts; i++) {
1008: passphrase = read_passphrase(prompt, 0);
1009: if (strcmp(passphrase, "") != 0) {
1.152 dtucker 1010: private = key_load_private_type(KEY_UNSPEC,
1011: filename, passphrase, NULL, NULL);
1.51 markus 1012: quit = 0;
1.20 markus 1013: } else {
1014: debug2("no passphrase given, try next key");
1.51 markus 1015: quit = 1;
1.20 markus 1016: }
1017: memset(passphrase, 0, strlen(passphrase));
1018: xfree(passphrase);
1.56 markus 1019: if (private != NULL || quit)
1.20 markus 1020: break;
1021: debug2("bad passphrase given, try again...");
1.16 markus 1022: }
1023: }
1.51 markus 1024: return private;
1025: }
1026:
1.117 markus 1027: /*
1028: * try keys in the following order:
1029: * 1. agent keys that are found in the config file
1030: * 2. other agent keys
1031: * 3. keys that are only listed in the config file
1032: */
1033: static void
1034: pubkey_prepare(Authctxt *authctxt)
1.51 markus 1035: {
1.117 markus 1036: Identity *id;
1037: Idlist agent, files, *preferred;
1038: Key *key;
1039: AuthenticationConnection *ac;
1040: char *comment;
1041: int i, found;
1.51 markus 1042:
1.117 markus 1043: TAILQ_INIT(&agent); /* keys from the agent */
1044: TAILQ_INIT(&files); /* keys from the config file */
1045: preferred = &authctxt->keys;
1046: TAILQ_INIT(preferred); /* preferred order of keys */
1047:
1048: /* list of keys stored in the filesystem */
1049: for (i = 0; i < options.num_identity_files; i++) {
1050: key = options.identity_keys[i];
1051: if (key && key->type == KEY_RSA1)
1052: continue;
1053: options.identity_keys[i] = NULL;
1.150 djm 1054: id = xcalloc(1, sizeof(*id));
1.117 markus 1055: id->key = key;
1056: id->filename = xstrdup(options.identity_files[i]);
1057: TAILQ_INSERT_TAIL(&files, id, next);
1058: }
1059: /* list of keys supported by the agent */
1060: if ((ac = ssh_get_authentication_connection())) {
1061: for (key = ssh_get_first_identity(ac, &comment, 2);
1062: key != NULL;
1063: key = ssh_get_next_identity(ac, &comment, 2)) {
1064: found = 0;
1065: TAILQ_FOREACH(id, &files, next) {
1.133 djm 1066: /* agent keys from the config file are preferred */
1.117 markus 1067: if (key_equal(key, id->key)) {
1068: key_free(key);
1069: xfree(comment);
1070: TAILQ_REMOVE(&files, id, next);
1071: TAILQ_INSERT_TAIL(preferred, id, next);
1072: id->ac = ac;
1073: found = 1;
1074: break;
1075: }
1076: }
1.135 markus 1077: if (!found && !options.identities_only) {
1.150 djm 1078: id = xcalloc(1, sizeof(*id));
1.117 markus 1079: id->key = key;
1080: id->filename = comment;
1081: id->ac = ac;
1082: TAILQ_INSERT_TAIL(&agent, id, next);
1083: }
1084: }
1085: /* append remaining agent keys */
1086: for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1087: TAILQ_REMOVE(&agent, id, next);
1088: TAILQ_INSERT_TAIL(preferred, id, next);
1089: }
1090: authctxt->agent = ac;
1091: }
1092: /* append remaining keys from the config file */
1093: for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1094: TAILQ_REMOVE(&files, id, next);
1095: TAILQ_INSERT_TAIL(preferred, id, next);
1096: }
1097: TAILQ_FOREACH(id, preferred, next) {
1098: debug2("key: %s (%p)", id->filename, id->key);
1099: }
1.17 markus 1100: }
1101:
1.117 markus 1102: static void
1103: pubkey_cleanup(Authctxt *authctxt)
1.51 markus 1104: {
1.117 markus 1105: Identity *id;
1.51 markus 1106:
1.117 markus 1107: if (authctxt->agent != NULL)
1108: ssh_close_authentication_connection(authctxt->agent);
1109: for (id = TAILQ_FIRST(&authctxt->keys); id;
1110: id = TAILQ_FIRST(&authctxt->keys)) {
1111: TAILQ_REMOVE(&authctxt->keys, id, next);
1112: if (id->key)
1113: key_free(id->key);
1114: if (id->filename)
1115: xfree(id->filename);
1116: xfree(id);
1117: }
1.1 markus 1118: }
1119:
1.20 markus 1120: int
1121: userauth_pubkey(Authctxt *authctxt)
1122: {
1.117 markus 1123: Identity *id;
1.20 markus 1124: int sent = 0;
1125:
1.117 markus 1126: while ((id = TAILQ_FIRST(&authctxt->keys))) {
1127: if (id->tried++)
1128: return (0);
1.127 markus 1129: /* move key to the end of the queue */
1.117 markus 1130: TAILQ_REMOVE(&authctxt->keys, id, next);
1131: TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1132: /*
1133: * send a test message if we have the public key. for
1134: * encrypted keys we cannot do this and have to load the
1135: * private key instead
1136: */
1137: if (id->key && id->key->type != KEY_RSA1) {
1138: debug("Offering public key: %s", id->filename);
1139: sent = send_pubkey_test(authctxt, id);
1140: } else if (id->key == NULL) {
1141: debug("Trying private key: %s", id->filename);
1142: id->key = load_identity_file(id->filename);
1143: if (id->key != NULL) {
1144: id->isprivate = 1;
1145: sent = sign_and_send_pubkey(authctxt, id);
1146: key_free(id->key);
1147: id->key = NULL;
1.51 markus 1148: }
1149: }
1.117 markus 1150: if (sent)
1151: return (sent);
1.28 markus 1152: }
1.117 markus 1153: return (0);
1.20 markus 1154: }
1155:
1.23 markus 1156: /*
1157: * Send userauth request message specifying keyboard-interactive method.
1158: */
1159: int
1160: userauth_kbdint(Authctxt *authctxt)
1161: {
1162: static int attempt = 0;
1163:
1164: if (attempt++ >= options.number_of_password_prompts)
1165: return 0;
1.82 markus 1166: /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1167: if (attempt > 1 && !authctxt->info_req_seen) {
1168: debug3("userauth_kbdint: disable: no info_req_seen");
1169: dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1170: return 0;
1171: }
1.23 markus 1172:
1173: debug2("userauth_kbdint");
1174: packet_start(SSH2_MSG_USERAUTH_REQUEST);
1175: packet_put_cstring(authctxt->server_user);
1176: packet_put_cstring(authctxt->service);
1177: packet_put_cstring(authctxt->method->name);
1178: packet_put_cstring(""); /* lang */
1179: packet_put_cstring(options.kbd_interactive_devices ?
1180: options.kbd_interactive_devices : "");
1181: packet_send();
1182:
1183: dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1184: return 1;
1185: }
1186:
1187: /*
1.46 markus 1188: * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1.23 markus 1189: */
1190: void
1.92 markus 1191: input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
1.23 markus 1192: {
1193: Authctxt *authctxt = ctxt;
1.46 markus 1194: char *name, *inst, *lang, *prompt, *response;
1.32 markus 1195: u_int num_prompts, i;
1.23 markus 1196: int echo = 0;
1197:
1198: debug2("input_userauth_info_req");
1199:
1200: if (authctxt == NULL)
1201: fatal("input_userauth_info_req: no authentication context");
1.82 markus 1202:
1203: authctxt->info_req_seen = 1;
1.23 markus 1204:
1205: name = packet_get_string(NULL);
1206: inst = packet_get_string(NULL);
1207: lang = packet_get_string(NULL);
1208: if (strlen(name) > 0)
1.116 itojun 1209: logit("%s", name);
1.23 markus 1210: if (strlen(inst) > 0)
1.116 itojun 1211: logit("%s", inst);
1.46 markus 1212: xfree(name);
1.23 markus 1213: xfree(inst);
1.46 markus 1214: xfree(lang);
1.23 markus 1215:
1216: num_prompts = packet_get_int();
1217: /*
1218: * Begin to build info response packet based on prompts requested.
1219: * We commit to providing the correct number of responses, so if
1220: * further on we run into a problem that prevents this, we have to
1221: * be sure and clean this up and send a correct error response.
1222: */
1223: packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1224: packet_put_int(num_prompts);
1225:
1.73 markus 1226: debug2("input_userauth_info_req: num_prompts %d", num_prompts);
1.23 markus 1227: for (i = 0; i < num_prompts; i++) {
1228: prompt = packet_get_string(NULL);
1229: echo = packet_get_char();
1230:
1.77 markus 1231: response = read_passphrase(prompt, echo ? RP_ECHO : 0);
1.23 markus 1232:
1.49 markus 1233: packet_put_cstring(response);
1.23 markus 1234: memset(response, 0, strlen(response));
1235: xfree(response);
1236: xfree(prompt);
1237: }
1.90 markus 1238: packet_check_eom(); /* done with parsing incoming message. */
1.23 markus 1239:
1.85 markus 1240: packet_add_padding(64);
1.23 markus 1241: packet_send();
1.68 markus 1242: }
1243:
1.100 markus 1244: static int
1.105 deraadt 1245: ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
1.100 markus 1246: u_char *data, u_int datalen)
1247: {
1248: Buffer b;
1.101 markus 1249: struct stat st;
1.100 markus 1250: pid_t pid;
1.103 markus 1251: int to[2], from[2], status, version = 2;
1.100 markus 1252:
1.109 markus 1253: debug2("ssh_keysign called");
1.100 markus 1254:
1.101 markus 1255: if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
1256: error("ssh_keysign: no installed: %s", strerror(errno));
1257: return -1;
1258: }
1.100 markus 1259: if (fflush(stdout) != 0)
1260: error("ssh_keysign: fflush: %s", strerror(errno));
1261: if (pipe(to) < 0) {
1262: error("ssh_keysign: pipe: %s", strerror(errno));
1263: return -1;
1264: }
1265: if (pipe(from) < 0) {
1266: error("ssh_keysign: pipe: %s", strerror(errno));
1267: return -1;
1268: }
1269: if ((pid = fork()) < 0) {
1270: error("ssh_keysign: fork: %s", strerror(errno));
1271: return -1;
1272: }
1273: if (pid == 0) {
1.155 markus 1274: permanently_drop_suid(getuid());
1.100 markus 1275: close(from[0]);
1276: if (dup2(from[1], STDOUT_FILENO) < 0)
1277: fatal("ssh_keysign: dup2: %s", strerror(errno));
1278: close(to[1]);
1279: if (dup2(to[0], STDIN_FILENO) < 0)
1280: fatal("ssh_keysign: dup2: %s", strerror(errno));
1.103 markus 1281: close(from[1]);
1282: close(to[0]);
1.102 markus 1283: execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
1.100 markus 1284: fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1285: strerror(errno));
1286: }
1287: close(from[1]);
1288: close(to[0]);
1289:
1290: buffer_init(&b);
1.103 markus 1291: buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
1.100 markus 1292: buffer_put_string(&b, data, datalen);
1.131 djm 1293: if (ssh_msg_send(to[1], version, &b) == -1)
1294: fatal("ssh_keysign: couldn't send request");
1.100 markus 1295:
1.110 djm 1296: if (ssh_msg_recv(from[0], &b) < 0) {
1.101 markus 1297: error("ssh_keysign: no reply");
1.134 markus 1298: buffer_free(&b);
1.100 markus 1299: return -1;
1300: }
1.101 markus 1301: close(from[0]);
1302: close(to[1]);
1303:
1.103 markus 1304: while (waitpid(pid, &status, 0) < 0)
1305: if (errno != EINTR)
1306: break;
1.101 markus 1307:
1.100 markus 1308: if (buffer_get_char(&b) != version) {
1.101 markus 1309: error("ssh_keysign: bad version");
1.134 markus 1310: buffer_free(&b);
1.100 markus 1311: return -1;
1312: }
1313: *sigp = buffer_get_string(&b, lenp);
1.134 markus 1314: buffer_free(&b);
1.100 markus 1315:
1316: return 0;
1317: }
1318:
1.68 markus 1319: int
1320: userauth_hostbased(Authctxt *authctxt)
1321: {
1322: Key *private = NULL;
1.100 markus 1323: Sensitive *sensitive = authctxt->sensitive;
1.68 markus 1324: Buffer b;
1325: u_char *signature, *blob;
1.163 djm 1326: char *chost, *pkalg, *p, myname[NI_MAXHOST];
1.72 markus 1327: const char *service;
1.68 markus 1328: u_int blen, slen;
1.71 markus 1329: int ok, i, len, found = 0;
1.68 markus 1330:
1331: /* check for a useful key */
1.100 markus 1332: for (i = 0; i < sensitive->nkeys; i++) {
1333: private = sensitive->keys[i];
1.68 markus 1334: if (private && private->type != KEY_RSA1) {
1335: found = 1;
1336: /* we take and free the key */
1.100 markus 1337: sensitive->keys[i] = NULL;
1.68 markus 1338: break;
1339: }
1340: }
1341: if (!found) {
1.109 markus 1342: debug("No more client hostkeys for hostbased authentication.");
1.68 markus 1343: return 0;
1344: }
1345: if (key_to_blob(private, &blob, &blen) == 0) {
1346: key_free(private);
1347: return 0;
1348: }
1.84 markus 1349: /* figure out a name for the client host */
1.163 djm 1350: p = NULL;
1351: if (packet_connection_is_on_socket())
1352: p = get_local_name(packet_get_connection_in());
1353: if (p == NULL) {
1354: if (gethostname(myname, sizeof(myname)) == -1) {
1355: verbose("userauth_hostbased: gethostname: %s",
1356: strerror(errno));
1357: } else
1358: p = xstrdup(myname);
1359: }
1.84 markus 1360: if (p == NULL) {
1361: error("userauth_hostbased: cannot get local ipaddr/name");
1362: key_free(private);
1.148 djm 1363: xfree(blob);
1.84 markus 1364: return 0;
1365: }
1366: len = strlen(p) + 2;
1.150 djm 1367: xasprintf(&chost, "%s.", p);
1.84 markus 1368: debug2("userauth_hostbased: chost %s", chost);
1.112 markus 1369: xfree(p);
1.84 markus 1370:
1.72 markus 1371: service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1372: authctxt->service;
1.68 markus 1373: pkalg = xstrdup(key_ssh_name(private));
1374: buffer_init(&b);
1375: /* construct data */
1.72 markus 1376: buffer_put_string(&b, session_id2, session_id2_len);
1.68 markus 1377: buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1378: buffer_put_cstring(&b, authctxt->server_user);
1.72 markus 1379: buffer_put_cstring(&b, service);
1.68 markus 1380: buffer_put_cstring(&b, authctxt->method->name);
1381: buffer_put_cstring(&b, pkalg);
1382: buffer_put_string(&b, blob, blen);
1383: buffer_put_cstring(&b, chost);
1384: buffer_put_cstring(&b, authctxt->local_user);
1385: #ifdef DEBUG_PK
1386: buffer_dump(&b);
1387: #endif
1.100 markus 1388: if (sensitive->external_keysign)
1389: ok = ssh_keysign(private, &signature, &slen,
1390: buffer_ptr(&b), buffer_len(&b));
1391: else
1392: ok = key_sign(private, &signature, &slen,
1393: buffer_ptr(&b), buffer_len(&b));
1.68 markus 1394: key_free(private);
1395: buffer_free(&b);
1396: if (ok != 0) {
1397: error("key_sign failed");
1398: xfree(chost);
1399: xfree(pkalg);
1.148 djm 1400: xfree(blob);
1.68 markus 1401: return 0;
1402: }
1403: packet_start(SSH2_MSG_USERAUTH_REQUEST);
1404: packet_put_cstring(authctxt->server_user);
1405: packet_put_cstring(authctxt->service);
1406: packet_put_cstring(authctxt->method->name);
1407: packet_put_cstring(pkalg);
1408: packet_put_string(blob, blen);
1409: packet_put_cstring(chost);
1410: packet_put_cstring(authctxt->local_user);
1411: packet_put_string(signature, slen);
1412: memset(signature, 's', slen);
1413: xfree(signature);
1414: xfree(chost);
1415: xfree(pkalg);
1.148 djm 1416: xfree(blob);
1.68 markus 1417:
1418: packet_send();
1419: return 1;
1.23 markus 1420: }
1.20 markus 1421:
1422: /* find auth method */
1423:
1424: /*
1425: * given auth method name, if configurable options permit this method fill
1426: * in auth_ident field and return true, otherwise return false.
1427: */
1.76 itojun 1428: static int
1.20 markus 1429: authmethod_is_enabled(Authmethod *method)
1430: {
1431: if (method == NULL)
1432: return 0;
1433: /* return false if options indicate this method is disabled */
1434: if (method->enabled == NULL || *method->enabled == 0)
1435: return 0;
1436: /* return false if batch mode is enabled but method needs interactive mode */
1437: if (method->batch_flag != NULL && *method->batch_flag != 0)
1438: return 0;
1439: return 1;
1440: }
1441:
1.76 itojun 1442: static Authmethod *
1.20 markus 1443: authmethod_lookup(const char *name)
1.1 markus 1444: {
1.20 markus 1445: Authmethod *method = NULL;
1446: if (name != NULL)
1447: for (method = authmethods; method->name != NULL; method++)
1448: if (strcmp(name, method->name) == 0)
1449: return method;
1450: debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1451: return NULL;
1452: }
1.1 markus 1453:
1.53 markus 1454: /* XXX internal state */
1455: static Authmethod *current = NULL;
1456: static char *supported = NULL;
1457: static char *preferred = NULL;
1.105 deraadt 1458:
1.20 markus 1459: /*
1460: * Given the authentication method list sent by the server, return the
1461: * next method we should try. If the server initially sends a nil list,
1.67 markus 1462: * use a built-in default list.
1.41 stevesk 1463: */
1.76 itojun 1464: static Authmethod *
1.20 markus 1465: authmethod_get(char *authlist)
1466: {
1.53 markus 1467: char *name = NULL;
1.97 markus 1468: u_int next;
1.41 stevesk 1469:
1.20 markus 1470: /* Use a suitable default if we're passed a nil list. */
1471: if (authlist == NULL || strlen(authlist) == 0)
1.53 markus 1472: authlist = options.preferred_authentications;
1.20 markus 1473:
1.53 markus 1474: if (supported == NULL || strcmp(authlist, supported) != 0) {
1475: debug3("start over, passed a different list %s", authlist);
1476: if (supported != NULL)
1477: xfree(supported);
1478: supported = xstrdup(authlist);
1479: preferred = options.preferred_authentications;
1480: debug3("preferred %s", preferred);
1481: current = NULL;
1482: } else if (current != NULL && authmethod_is_enabled(current))
1483: return current;
1.20 markus 1484:
1.53 markus 1485: for (;;) {
1486: if ((name = match_list(preferred, supported, &next)) == NULL) {
1.109 markus 1487: debug("No more authentication methods to try.");
1.53 markus 1488: current = NULL;
1489: return NULL;
1490: }
1491: preferred += next;
1.23 markus 1492: debug3("authmethod_lookup %s", name);
1.53 markus 1493: debug3("remaining preferred: %s", preferred);
1494: if ((current = authmethod_lookup(name)) != NULL &&
1495: authmethod_is_enabled(current)) {
1.23 markus 1496: debug3("authmethod_is_enabled %s", name);
1.109 markus 1497: debug("Next authentication method: %s", name);
1.53 markus 1498: return current;
1.23 markus 1499: }
1.1 markus 1500: }
1.53 markus 1501: }
1.1 markus 1502:
1.79 stevesk 1503: static char *
1.53 markus 1504: authmethods_get(void)
1505: {
1506: Authmethod *method = NULL;
1.93 markus 1507: Buffer b;
1508: char *list;
1.27 provos 1509:
1.93 markus 1510: buffer_init(&b);
1.53 markus 1511: for (method = authmethods; method->name != NULL; method++) {
1512: if (authmethod_is_enabled(method)) {
1.93 markus 1513: if (buffer_len(&b) > 0)
1514: buffer_append(&b, ",", 1);
1515: buffer_append(&b, method->name, strlen(method->name));
1.53 markus 1516: }
1517: }
1.93 markus 1518: buffer_append(&b, "\0", 1);
1519: list = xstrdup(buffer_ptr(&b));
1520: buffer_free(&b);
1521: return list;
1.1 markus 1522: }