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