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