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