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