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