Annotation of src/usr.bin/ssh/sshconnect2.c, Revision 1.130
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.130 ! markus 26: RCSID("$OpenBSD: sshconnect2.c,v 1.129 2003/11/02 11:01:03 markus 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;
1.128 avsm 481: static gss_OID_set gss_supported = NULL;
1.121 markus 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:
1.128 avsm 489: if (gss_supported == NULL)
490: gss_indicate_mechs(&min, &gss_supported);
1.121 markus 491:
492: /* Check to see if the mechanism is usable before we offer it */
1.128 avsm 493: while (mech < gss_supported->count && !ok) {
1.121 markus 494: if (gssctxt)
495: ssh_gssapi_delete_ctx(&gssctxt);
496: ssh_gssapi_build_ctx(&gssctxt);
1.128 avsm 497: ssh_gssapi_set_oid(gssctxt, &gss_supported->elements[mech]);
1.121 markus 498:
499: /* My DER encoding requires length<128 */
1.128 avsm 500: if (gss_supported->elements[mech].length < 128 &&
1.121 markus 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:
1.129 markus 520: packet_put_int((gss_supported->elements[mech].length) + 2);
521: packet_put_char(SSH_GSS_OIDTYPE);
522: packet_put_char(gss_supported->elements[mech].length);
523: packet_put_raw(gss_supported->elements[mech].elements,
524: gss_supported->elements[mech].length);
1.121 markus 525:
526: packet_send();
527:
528: dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
529: dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
530: dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
531: dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
532:
533: mech++; /* Move along to next candidate */
534:
535: return 1;
536: }
537:
1.130 ! markus 538: static OM_uint32
! 539: process_gssapi_token(void *ctxt, gss_buffer_t recv_tok)
! 540: {
! 541: Authctxt *authctxt = ctxt;
! 542: Gssctxt *gssctxt = authctxt->methoddata;
! 543: gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
! 544: OM_uint32 status, ms;
! 545:
! 546: status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
! 547: recv_tok, &send_tok, NULL);
! 548:
! 549: if (send_tok.length > 0) {
! 550: if (GSS_ERROR(status))
! 551: packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
! 552: else
! 553: packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
! 554:
! 555: packet_put_string(send_tok.value, send_tok.length);
! 556: packet_send();
! 557: gss_release_buffer(&ms, &send_tok);
! 558: }
! 559:
! 560: if (status == GSS_S_COMPLETE) {
! 561: /* If that succeeded, send a exchange complete message */
! 562: packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
! 563: packet_send();
! 564: }
! 565:
! 566: return status;
! 567: }
! 568:
1.121 markus 569: void
570: input_gssapi_response(int type, u_int32_t plen, void *ctxt)
571: {
572: Authctxt *authctxt = ctxt;
573: Gssctxt *gssctxt;
574: int oidlen;
575: char *oidv;
576:
577: if (authctxt == NULL)
578: fatal("input_gssapi_response: no authentication context");
579: gssctxt = authctxt->methoddata;
580:
581: /* Setup our OID */
582: oidv = packet_get_string(&oidlen);
583:
1.129 markus 584: if (oidlen <= 2 ||
585: oidv[0] != SSH_GSS_OIDTYPE ||
586: oidv[1] != oidlen - 2) {
1.130 ! markus 587: xfree(oidv);
1.129 markus 588: debug("Badly encoded mechanism OID received");
589: userauth(authctxt, NULL);
590: return;
1.121 markus 591: }
1.129 markus 592:
593: if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
594: fatal("Server returned different OID than expected");
1.121 markus 595:
596: packet_check_eom();
597:
598: xfree(oidv);
599:
1.130 ! markus 600: if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) {
1.121 markus 601: /* Start again with next method on list */
602: debug("Trying to start again");
603: userauth(authctxt, NULL);
604: return;
605: }
606: }
607:
608: void
609: input_gssapi_token(int type, u_int32_t plen, void *ctxt)
610: {
611: Authctxt *authctxt = ctxt;
612: gss_buffer_desc recv_tok;
1.130 ! markus 613: OM_uint32 status;
1.121 markus 614: u_int slen;
615:
616: if (authctxt == NULL)
617: fatal("input_gssapi_response: no authentication context");
618:
619: recv_tok.value = packet_get_string(&slen);
620: recv_tok.length = slen; /* safe typecast */
621:
622: packet_check_eom();
623:
1.130 ! markus 624: status = process_gssapi_token(ctxt, &recv_tok);
1.121 markus 625:
626: xfree(recv_tok.value);
627:
628: if (GSS_ERROR(status)) {
629: /* Start again with the next method in the list */
630: userauth(authctxt, NULL);
631: return;
632: }
633: }
634:
635: void
636: input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
637: {
638: Authctxt *authctxt = ctxt;
639: Gssctxt *gssctxt;
640: gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
641: gss_buffer_desc recv_tok;
642: OM_uint32 status, ms;
1.123 deraadt 643: u_int len;
1.121 markus 644:
645: if (authctxt == NULL)
646: fatal("input_gssapi_response: no authentication context");
647: gssctxt = authctxt->methoddata;
648:
1.123 deraadt 649: recv_tok.value = packet_get_string(&len);
650: recv_tok.length = len;
1.121 markus 651:
652: packet_check_eom();
653:
654: /* Stick it into GSSAPI and see what it says */
655: status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
656: &recv_tok, &send_tok, NULL);
657:
658: xfree(recv_tok.value);
659: gss_release_buffer(&ms, &send_tok);
660:
661: /* Server will be returning a failed packet after this one */
662: }
663:
664: void
665: input_gssapi_error(int type, u_int32_t plen, void *ctxt)
666: {
667: OM_uint32 maj, min;
668: char *msg;
669: char *lang;
670:
671: maj=packet_get_int();
672: min=packet_get_int();
673: msg=packet_get_string(NULL);
674: lang=packet_get_string(NULL);
675:
676: packet_check_eom();
677:
678: debug("Server GSSAPI Error:\n%s\n", msg);
679: xfree(msg);
680: xfree(lang);
681: }
682: #endif /* GSSAPI */
1.20 markus 683:
1.1 markus 684: int
1.23 markus 685: userauth_none(Authctxt *authctxt)
686: {
687: /* initial userauth request */
688: packet_start(SSH2_MSG_USERAUTH_REQUEST);
689: packet_put_cstring(authctxt->server_user);
690: packet_put_cstring(authctxt->service);
691: packet_put_cstring(authctxt->method->name);
692: packet_send();
693: return 1;
694: }
695:
696: int
1.20 markus 697: userauth_passwd(Authctxt *authctxt)
1.1 markus 698: {
1.6 markus 699: static int attempt = 0;
1.99 markus 700: char prompt[150];
1.1 markus 701: char *password;
1.6 markus 702:
1.13 todd 703: if (attempt++ >= options.number_of_password_prompts)
1.6 markus 704: return 0;
1.13 todd 705:
1.87 deraadt 706: if (attempt != 1)
1.13 todd 707: error("Permission denied, please try again.");
1.1 markus 708:
1.43 itojun 709: snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
1.20 markus 710: authctxt->server_user, authctxt->host);
1.1 markus 711: password = read_passphrase(prompt, 0);
712: packet_start(SSH2_MSG_USERAUTH_REQUEST);
1.20 markus 713: packet_put_cstring(authctxt->server_user);
714: packet_put_cstring(authctxt->service);
1.23 markus 715: packet_put_cstring(authctxt->method->name);
1.1 markus 716: packet_put_char(0);
1.49 markus 717: packet_put_cstring(password);
1.1 markus 718: memset(password, 0, strlen(password));
719: xfree(password);
1.85 markus 720: packet_add_padding(64);
1.1 markus 721: packet_send();
1.99 markus 722:
1.104 deraadt 723: dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
1.99 markus 724: &input_userauth_passwd_changereq);
725:
1.1 markus 726: return 1;
727: }
1.99 markus 728: /*
729: * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
730: */
731: void
732: input_userauth_passwd_changereq(int type, uint32_t seqnr, void *ctxt)
733: {
734: Authctxt *authctxt = ctxt;
735: char *info, *lang, *password = NULL, *retype = NULL;
736: char prompt[150];
737:
738: debug2("input_userauth_passwd_changereq");
739:
740: if (authctxt == NULL)
741: fatal("input_userauth_passwd_changereq: "
742: "no authentication context");
743:
744: info = packet_get_string(NULL);
745: lang = packet_get_string(NULL);
746: if (strlen(info) > 0)
1.116 itojun 747: logit("%s", info);
1.99 markus 748: xfree(info);
749: xfree(lang);
750: packet_start(SSH2_MSG_USERAUTH_REQUEST);
751: packet_put_cstring(authctxt->server_user);
752: packet_put_cstring(authctxt->service);
753: packet_put_cstring(authctxt->method->name);
754: packet_put_char(1); /* additional info */
1.104 deraadt 755: snprintf(prompt, sizeof(prompt),
1.99 markus 756: "Enter %.30s@%.128s's old password: ",
757: authctxt->server_user, authctxt->host);
758: password = read_passphrase(prompt, 0);
759: packet_put_cstring(password);
760: memset(password, 0, strlen(password));
761: xfree(password);
762: password = NULL;
763: while (password == NULL) {
1.104 deraadt 764: snprintf(prompt, sizeof(prompt),
1.99 markus 765: "Enter %.30s@%.128s's new password: ",
766: authctxt->server_user, authctxt->host);
767: password = read_passphrase(prompt, RP_ALLOW_EOF);
768: if (password == NULL) {
769: /* bail out */
770: return;
771: }
1.104 deraadt 772: snprintf(prompt, sizeof(prompt),
1.99 markus 773: "Retype %.30s@%.128s's new password: ",
774: authctxt->server_user, authctxt->host);
775: retype = read_passphrase(prompt, 0);
776: if (strcmp(password, retype) != 0) {
777: memset(password, 0, strlen(password));
778: xfree(password);
1.116 itojun 779: logit("Mismatch; try again, EOF to quit.");
1.99 markus 780: password = NULL;
781: }
782: memset(retype, 0, strlen(retype));
783: xfree(retype);
784: }
785: packet_put_cstring(password);
786: memset(password, 0, strlen(password));
787: xfree(password);
788: packet_add_padding(64);
789: packet_send();
1.104 deraadt 790:
791: dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
1.99 markus 792: &input_userauth_passwd_changereq);
1.117 markus 793: }
794:
795: static int
796: identity_sign(Identity *id, u_char **sigp, u_int *lenp,
797: u_char *data, u_int datalen)
798: {
799: Key *prv;
800: int ret;
1.99 markus 801:
1.117 markus 802: /* the agent supports this key */
803: if (id->ac)
804: return (ssh_agent_sign(id->ac, id->key, sigp, lenp,
805: data, datalen));
806: /*
807: * we have already loaded the private key or
808: * the private key is stored in external hardware
809: */
810: if (id->isprivate || (id->key->flags & KEY_FLAG_EXT))
811: return (key_sign(id->key, sigp, lenp, data, datalen));
812: /* load the private key from the file */
813: if ((prv = load_identity_file(id->filename)) == NULL)
814: return (-1);
815: ret = key_sign(prv, sigp, lenp, data, datalen);
816: key_free(prv);
817: return (ret);
1.51 markus 818: }
819:
1.76 itojun 820: static int
1.117 markus 821: sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
1.1 markus 822: {
823: Buffer b;
1.32 markus 824: u_char *blob, *signature;
1.96 markus 825: u_int bloblen, slen;
1.120 markus 826: u_int skip = 0;
1.17 markus 827: int ret = -1;
1.23 markus 828: int have_sig = 1;
1.1 markus 829:
1.30 markus 830: debug3("sign_and_send_pubkey");
1.51 markus 831:
1.117 markus 832: if (key_to_blob(id->key, &blob, &bloblen) == 0) {
1.28 markus 833: /* we cannot handle this key */
1.30 markus 834: debug3("sign_and_send_pubkey: cannot handle key");
1.28 markus 835: return 0;
836: }
1.1 markus 837: /* data to be signed */
838: buffer_init(&b);
1.26 markus 839: if (datafellows & SSH_OLD_SESSIONID) {
840: buffer_append(&b, session_id2, session_id2_len);
1.41 stevesk 841: skip = session_id2_len;
1.26 markus 842: } else {
1.14 markus 843: buffer_put_string(&b, session_id2, session_id2_len);
844: skip = buffer_len(&b);
845: }
1.1 markus 846: buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1.20 markus 847: buffer_put_cstring(&b, authctxt->server_user);
1.10 markus 848: buffer_put_cstring(&b,
1.30 markus 849: datafellows & SSH_BUG_PKSERVICE ?
1.10 markus 850: "ssh-userauth" :
1.20 markus 851: authctxt->service);
1.30 markus 852: if (datafellows & SSH_BUG_PKAUTH) {
853: buffer_put_char(&b, have_sig);
854: } else {
855: buffer_put_cstring(&b, authctxt->method->name);
856: buffer_put_char(&b, have_sig);
1.117 markus 857: buffer_put_cstring(&b, key_ssh_name(id->key));
1.30 markus 858: }
1.1 markus 859: buffer_put_string(&b, blob, bloblen);
860:
861: /* generate signature */
1.117 markus 862: ret = identity_sign(id, &signature, &slen,
1.51 markus 863: buffer_ptr(&b), buffer_len(&b));
1.17 markus 864: if (ret == -1) {
865: xfree(blob);
866: buffer_free(&b);
867: return 0;
868: }
1.28 markus 869: #ifdef DEBUG_PK
1.1 markus 870: buffer_dump(&b);
871: #endif
1.30 markus 872: if (datafellows & SSH_BUG_PKSERVICE) {
1.10 markus 873: buffer_clear(&b);
874: buffer_append(&b, session_id2, session_id2_len);
1.51 markus 875: skip = session_id2_len;
1.10 markus 876: buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1.20 markus 877: buffer_put_cstring(&b, authctxt->server_user);
878: buffer_put_cstring(&b, authctxt->service);
1.23 markus 879: buffer_put_cstring(&b, authctxt->method->name);
880: buffer_put_char(&b, have_sig);
1.30 markus 881: if (!(datafellows & SSH_BUG_PKAUTH))
1.117 markus 882: buffer_put_cstring(&b, key_ssh_name(id->key));
1.10 markus 883: buffer_put_string(&b, blob, bloblen);
884: }
885: xfree(blob);
1.51 markus 886:
1.1 markus 887: /* append signature */
888: buffer_put_string(&b, signature, slen);
889: xfree(signature);
890:
891: /* skip session id and packet type */
1.14 markus 892: if (buffer_len(&b) < skip + 1)
1.20 markus 893: fatal("userauth_pubkey: internal error");
1.14 markus 894: buffer_consume(&b, skip + 1);
1.1 markus 895:
896: /* put remaining data from buffer into packet */
897: packet_start(SSH2_MSG_USERAUTH_REQUEST);
898: packet_put_raw(buffer_ptr(&b), buffer_len(&b));
899: buffer_free(&b);
900: packet_send();
1.17 markus 901:
902: return 1;
1.16 markus 903: }
904:
1.76 itojun 905: static int
1.117 markus 906: send_pubkey_test(Authctxt *authctxt, Identity *id)
1.20 markus 907: {
1.51 markus 908: u_char *blob;
1.97 markus 909: u_int bloblen, have_sig = 0;
1.20 markus 910:
1.51 markus 911: debug3("send_pubkey_test");
1.16 markus 912:
1.117 markus 913: if (key_to_blob(id->key, &blob, &bloblen) == 0) {
1.51 markus 914: /* we cannot handle this key */
915: debug3("send_pubkey_test: cannot handle key");
1.16 markus 916: return 0;
917: }
1.51 markus 918: /* register callback for USERAUTH_PK_OK message */
919: dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
920:
921: packet_start(SSH2_MSG_USERAUTH_REQUEST);
922: packet_put_cstring(authctxt->server_user);
923: packet_put_cstring(authctxt->service);
924: packet_put_cstring(authctxt->method->name);
925: packet_put_char(have_sig);
926: if (!(datafellows & SSH_BUG_PKAUTH))
1.117 markus 927: packet_put_cstring(key_ssh_name(id->key));
1.51 markus 928: packet_put_string(blob, bloblen);
929: xfree(blob);
930: packet_send();
931: return 1;
932: }
933:
1.76 itojun 934: static Key *
1.51 markus 935: load_identity_file(char *filename)
936: {
937: Key *private;
938: char prompt[300], *passphrase;
1.56 markus 939: int quit, i;
1.52 markus 940: struct stat st;
1.16 markus 941:
1.52 markus 942: if (stat(filename, &st) < 0) {
943: debug3("no such identity: %s", filename);
944: return NULL;
945: }
1.56 markus 946: private = key_load_private_type(KEY_UNSPEC, filename, "", NULL);
947: if (private == NULL) {
948: if (options.batch_mode)
1.51 markus 949: return NULL;
1.16 markus 950: snprintf(prompt, sizeof prompt,
1.88 deraadt 951: "Enter passphrase for key '%.100s': ", filename);
1.20 markus 952: for (i = 0; i < options.number_of_password_prompts; i++) {
953: passphrase = read_passphrase(prompt, 0);
954: if (strcmp(passphrase, "") != 0) {
1.56 markus 955: private = key_load_private_type(KEY_UNSPEC, filename,
956: passphrase, NULL);
1.51 markus 957: quit = 0;
1.20 markus 958: } else {
959: debug2("no passphrase given, try next key");
1.51 markus 960: quit = 1;
1.20 markus 961: }
962: memset(passphrase, 0, strlen(passphrase));
963: xfree(passphrase);
1.56 markus 964: if (private != NULL || quit)
1.20 markus 965: break;
966: debug2("bad passphrase given, try again...");
1.16 markus 967: }
968: }
1.51 markus 969: return private;
970: }
971:
1.117 markus 972: /*
973: * try keys in the following order:
974: * 1. agent keys that are found in the config file
975: * 2. other agent keys
976: * 3. keys that are only listed in the config file
977: */
978: static void
979: pubkey_prepare(Authctxt *authctxt)
1.51 markus 980: {
1.117 markus 981: Identity *id;
982: Idlist agent, files, *preferred;
983: Key *key;
984: AuthenticationConnection *ac;
985: char *comment;
986: int i, found;
1.51 markus 987:
1.117 markus 988: TAILQ_INIT(&agent); /* keys from the agent */
989: TAILQ_INIT(&files); /* keys from the config file */
990: preferred = &authctxt->keys;
991: TAILQ_INIT(preferred); /* preferred order of keys */
992:
993: /* list of keys stored in the filesystem */
994: for (i = 0; i < options.num_identity_files; i++) {
995: key = options.identity_keys[i];
996: if (key && key->type == KEY_RSA1)
997: continue;
998: options.identity_keys[i] = NULL;
999: id = xmalloc(sizeof(*id));
1000: memset(id, 0, sizeof(*id));
1001: id->key = key;
1002: id->filename = xstrdup(options.identity_files[i]);
1003: TAILQ_INSERT_TAIL(&files, id, next);
1004: }
1005: /* list of keys supported by the agent */
1006: if ((ac = ssh_get_authentication_connection())) {
1007: for (key = ssh_get_first_identity(ac, &comment, 2);
1008: key != NULL;
1009: key = ssh_get_next_identity(ac, &comment, 2)) {
1010: found = 0;
1011: TAILQ_FOREACH(id, &files, next) {
1012: /* agent keys from the config file are preferred */
1013: if (key_equal(key, id->key)) {
1014: key_free(key);
1015: xfree(comment);
1016: TAILQ_REMOVE(&files, id, next);
1017: TAILQ_INSERT_TAIL(preferred, id, next);
1018: id->ac = ac;
1019: found = 1;
1020: break;
1021: }
1022: }
1023: if (!found) {
1024: id = xmalloc(sizeof(*id));
1025: memset(id, 0, sizeof(*id));
1026: id->key = key;
1027: id->filename = comment;
1028: id->ac = ac;
1029: TAILQ_INSERT_TAIL(&agent, id, next);
1030: }
1031: }
1032: /* append remaining agent keys */
1033: for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1034: TAILQ_REMOVE(&agent, id, next);
1035: TAILQ_INSERT_TAIL(preferred, id, next);
1036: }
1037: authctxt->agent = ac;
1038: }
1039: /* append remaining keys from the config file */
1040: for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1041: TAILQ_REMOVE(&files, id, next);
1042: TAILQ_INSERT_TAIL(preferred, id, next);
1043: }
1044: TAILQ_FOREACH(id, preferred, next) {
1045: debug2("key: %s (%p)", id->filename, id->key);
1046: }
1.17 markus 1047: }
1048:
1.117 markus 1049: static void
1050: pubkey_cleanup(Authctxt *authctxt)
1.51 markus 1051: {
1.117 markus 1052: Identity *id;
1.51 markus 1053:
1.117 markus 1054: if (authctxt->agent != NULL)
1055: ssh_close_authentication_connection(authctxt->agent);
1056: for (id = TAILQ_FIRST(&authctxt->keys); id;
1057: id = TAILQ_FIRST(&authctxt->keys)) {
1058: TAILQ_REMOVE(&authctxt->keys, id, next);
1059: if (id->key)
1060: key_free(id->key);
1061: if (id->filename)
1062: xfree(id->filename);
1063: xfree(id);
1064: }
1.1 markus 1065: }
1066:
1.20 markus 1067: int
1068: userauth_pubkey(Authctxt *authctxt)
1069: {
1.117 markus 1070: Identity *id;
1.20 markus 1071: int sent = 0;
1072:
1.117 markus 1073: while ((id = TAILQ_FIRST(&authctxt->keys))) {
1074: if (id->tried++)
1075: return (0);
1.127 markus 1076: /* move key to the end of the queue */
1.117 markus 1077: TAILQ_REMOVE(&authctxt->keys, id, next);
1078: TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1079: /*
1080: * send a test message if we have the public key. for
1081: * encrypted keys we cannot do this and have to load the
1082: * private key instead
1083: */
1084: if (id->key && id->key->type != KEY_RSA1) {
1085: debug("Offering public key: %s", id->filename);
1086: sent = send_pubkey_test(authctxt, id);
1087: } else if (id->key == NULL) {
1088: debug("Trying private key: %s", id->filename);
1089: id->key = load_identity_file(id->filename);
1090: if (id->key != NULL) {
1091: id->isprivate = 1;
1092: sent = sign_and_send_pubkey(authctxt, id);
1093: key_free(id->key);
1094: id->key = NULL;
1.51 markus 1095: }
1096: }
1.117 markus 1097: if (sent)
1098: return (sent);
1.28 markus 1099: }
1.117 markus 1100: return (0);
1.20 markus 1101: }
1102:
1.23 markus 1103: /*
1104: * Send userauth request message specifying keyboard-interactive method.
1105: */
1106: int
1107: userauth_kbdint(Authctxt *authctxt)
1108: {
1109: static int attempt = 0;
1110:
1111: if (attempt++ >= options.number_of_password_prompts)
1112: return 0;
1.82 markus 1113: /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1114: if (attempt > 1 && !authctxt->info_req_seen) {
1115: debug3("userauth_kbdint: disable: no info_req_seen");
1116: dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1117: return 0;
1118: }
1.23 markus 1119:
1120: debug2("userauth_kbdint");
1121: packet_start(SSH2_MSG_USERAUTH_REQUEST);
1122: packet_put_cstring(authctxt->server_user);
1123: packet_put_cstring(authctxt->service);
1124: packet_put_cstring(authctxt->method->name);
1125: packet_put_cstring(""); /* lang */
1126: packet_put_cstring(options.kbd_interactive_devices ?
1127: options.kbd_interactive_devices : "");
1128: packet_send();
1129:
1130: dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1131: return 1;
1132: }
1133:
1134: /*
1.46 markus 1135: * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1.23 markus 1136: */
1137: void
1.92 markus 1138: input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
1.23 markus 1139: {
1140: Authctxt *authctxt = ctxt;
1.46 markus 1141: char *name, *inst, *lang, *prompt, *response;
1.32 markus 1142: u_int num_prompts, i;
1.23 markus 1143: int echo = 0;
1144:
1145: debug2("input_userauth_info_req");
1146:
1147: if (authctxt == NULL)
1148: fatal("input_userauth_info_req: no authentication context");
1.82 markus 1149:
1150: authctxt->info_req_seen = 1;
1.23 markus 1151:
1152: name = packet_get_string(NULL);
1153: inst = packet_get_string(NULL);
1154: lang = packet_get_string(NULL);
1155: if (strlen(name) > 0)
1.116 itojun 1156: logit("%s", name);
1.23 markus 1157: if (strlen(inst) > 0)
1.116 itojun 1158: logit("%s", inst);
1.46 markus 1159: xfree(name);
1.23 markus 1160: xfree(inst);
1.46 markus 1161: xfree(lang);
1.23 markus 1162:
1163: num_prompts = packet_get_int();
1164: /*
1165: * Begin to build info response packet based on prompts requested.
1166: * We commit to providing the correct number of responses, so if
1167: * further on we run into a problem that prevents this, we have to
1168: * be sure and clean this up and send a correct error response.
1169: */
1170: packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1171: packet_put_int(num_prompts);
1172:
1.73 markus 1173: debug2("input_userauth_info_req: num_prompts %d", num_prompts);
1.23 markus 1174: for (i = 0; i < num_prompts; i++) {
1175: prompt = packet_get_string(NULL);
1176: echo = packet_get_char();
1177:
1.77 markus 1178: response = read_passphrase(prompt, echo ? RP_ECHO : 0);
1.23 markus 1179:
1.49 markus 1180: packet_put_cstring(response);
1.23 markus 1181: memset(response, 0, strlen(response));
1182: xfree(response);
1183: xfree(prompt);
1184: }
1.90 markus 1185: packet_check_eom(); /* done with parsing incoming message. */
1.23 markus 1186:
1.85 markus 1187: packet_add_padding(64);
1.23 markus 1188: packet_send();
1.68 markus 1189: }
1190:
1.100 markus 1191: static int
1.105 deraadt 1192: ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
1.100 markus 1193: u_char *data, u_int datalen)
1194: {
1195: Buffer b;
1.101 markus 1196: struct stat st;
1.100 markus 1197: pid_t pid;
1.103 markus 1198: int to[2], from[2], status, version = 2;
1.100 markus 1199:
1.109 markus 1200: debug2("ssh_keysign called");
1.100 markus 1201:
1.101 markus 1202: if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
1203: error("ssh_keysign: no installed: %s", strerror(errno));
1204: return -1;
1205: }
1.100 markus 1206: if (fflush(stdout) != 0)
1207: error("ssh_keysign: fflush: %s", strerror(errno));
1208: if (pipe(to) < 0) {
1209: error("ssh_keysign: pipe: %s", strerror(errno));
1210: return -1;
1211: }
1212: if (pipe(from) < 0) {
1213: error("ssh_keysign: pipe: %s", strerror(errno));
1214: return -1;
1215: }
1216: if ((pid = fork()) < 0) {
1217: error("ssh_keysign: fork: %s", strerror(errno));
1218: return -1;
1219: }
1220: if (pid == 0) {
1221: seteuid(getuid());
1222: setuid(getuid());
1223: close(from[0]);
1224: if (dup2(from[1], STDOUT_FILENO) < 0)
1225: fatal("ssh_keysign: dup2: %s", strerror(errno));
1226: close(to[1]);
1227: if (dup2(to[0], STDIN_FILENO) < 0)
1228: fatal("ssh_keysign: dup2: %s", strerror(errno));
1.103 markus 1229: close(from[1]);
1230: close(to[0]);
1.102 markus 1231: execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
1.100 markus 1232: fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1233: strerror(errno));
1234: }
1235: close(from[1]);
1236: close(to[0]);
1237:
1238: buffer_init(&b);
1.103 markus 1239: buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
1.100 markus 1240: buffer_put_string(&b, data, datalen);
1.110 djm 1241: ssh_msg_send(to[1], version, &b);
1.100 markus 1242:
1.110 djm 1243: if (ssh_msg_recv(from[0], &b) < 0) {
1.101 markus 1244: error("ssh_keysign: no reply");
1.100 markus 1245: buffer_clear(&b);
1246: return -1;
1247: }
1.101 markus 1248: close(from[0]);
1249: close(to[1]);
1250:
1.103 markus 1251: while (waitpid(pid, &status, 0) < 0)
1252: if (errno != EINTR)
1253: break;
1.101 markus 1254:
1.100 markus 1255: if (buffer_get_char(&b) != version) {
1.101 markus 1256: error("ssh_keysign: bad version");
1.100 markus 1257: buffer_clear(&b);
1258: return -1;
1259: }
1260: *sigp = buffer_get_string(&b, lenp);
1261: buffer_clear(&b);
1262:
1263: return 0;
1264: }
1265:
1.68 markus 1266: int
1267: userauth_hostbased(Authctxt *authctxt)
1268: {
1269: Key *private = NULL;
1.100 markus 1270: Sensitive *sensitive = authctxt->sensitive;
1.68 markus 1271: Buffer b;
1272: u_char *signature, *blob;
1273: char *chost, *pkalg, *p;
1.72 markus 1274: const char *service;
1.68 markus 1275: u_int blen, slen;
1.71 markus 1276: int ok, i, len, found = 0;
1.68 markus 1277:
1278: /* check for a useful key */
1.100 markus 1279: for (i = 0; i < sensitive->nkeys; i++) {
1280: private = sensitive->keys[i];
1.68 markus 1281: if (private && private->type != KEY_RSA1) {
1282: found = 1;
1283: /* we take and free the key */
1.100 markus 1284: sensitive->keys[i] = NULL;
1.68 markus 1285: break;
1286: }
1287: }
1288: if (!found) {
1.109 markus 1289: debug("No more client hostkeys for hostbased authentication.");
1.68 markus 1290: return 0;
1291: }
1292: if (key_to_blob(private, &blob, &blen) == 0) {
1293: key_free(private);
1294: return 0;
1295: }
1.84 markus 1296: /* figure out a name for the client host */
1297: p = get_local_name(packet_get_connection_in());
1298: if (p == NULL) {
1299: error("userauth_hostbased: cannot get local ipaddr/name");
1300: key_free(private);
1301: return 0;
1302: }
1303: len = strlen(p) + 2;
1304: chost = xmalloc(len);
1305: strlcpy(chost, p, len);
1306: strlcat(chost, ".", len);
1307: debug2("userauth_hostbased: chost %s", chost);
1.112 markus 1308: xfree(p);
1.84 markus 1309:
1.72 markus 1310: service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1311: authctxt->service;
1.68 markus 1312: pkalg = xstrdup(key_ssh_name(private));
1313: buffer_init(&b);
1314: /* construct data */
1.72 markus 1315: buffer_put_string(&b, session_id2, session_id2_len);
1.68 markus 1316: buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1317: buffer_put_cstring(&b, authctxt->server_user);
1.72 markus 1318: buffer_put_cstring(&b, service);
1.68 markus 1319: buffer_put_cstring(&b, authctxt->method->name);
1320: buffer_put_cstring(&b, pkalg);
1321: buffer_put_string(&b, blob, blen);
1322: buffer_put_cstring(&b, chost);
1323: buffer_put_cstring(&b, authctxt->local_user);
1324: #ifdef DEBUG_PK
1325: buffer_dump(&b);
1326: #endif
1.100 markus 1327: if (sensitive->external_keysign)
1328: ok = ssh_keysign(private, &signature, &slen,
1329: buffer_ptr(&b), buffer_len(&b));
1330: else
1331: ok = key_sign(private, &signature, &slen,
1332: buffer_ptr(&b), buffer_len(&b));
1.68 markus 1333: key_free(private);
1334: buffer_free(&b);
1335: if (ok != 0) {
1336: error("key_sign failed");
1337: xfree(chost);
1338: xfree(pkalg);
1339: return 0;
1340: }
1341: packet_start(SSH2_MSG_USERAUTH_REQUEST);
1342: packet_put_cstring(authctxt->server_user);
1343: packet_put_cstring(authctxt->service);
1344: packet_put_cstring(authctxt->method->name);
1345: packet_put_cstring(pkalg);
1346: packet_put_string(blob, blen);
1347: packet_put_cstring(chost);
1348: packet_put_cstring(authctxt->local_user);
1349: packet_put_string(signature, slen);
1350: memset(signature, 's', slen);
1351: xfree(signature);
1352: xfree(chost);
1353: xfree(pkalg);
1354:
1355: packet_send();
1356: return 1;
1.23 markus 1357: }
1.20 markus 1358:
1359: /* find auth method */
1360:
1361: /*
1362: * given auth method name, if configurable options permit this method fill
1363: * in auth_ident field and return true, otherwise return false.
1364: */
1.76 itojun 1365: static int
1.20 markus 1366: authmethod_is_enabled(Authmethod *method)
1367: {
1368: if (method == NULL)
1369: return 0;
1370: /* return false if options indicate this method is disabled */
1371: if (method->enabled == NULL || *method->enabled == 0)
1372: return 0;
1373: /* return false if batch mode is enabled but method needs interactive mode */
1374: if (method->batch_flag != NULL && *method->batch_flag != 0)
1375: return 0;
1376: return 1;
1377: }
1378:
1.76 itojun 1379: static Authmethod *
1.20 markus 1380: authmethod_lookup(const char *name)
1.1 markus 1381: {
1.20 markus 1382: Authmethod *method = NULL;
1383: if (name != NULL)
1384: for (method = authmethods; method->name != NULL; method++)
1385: if (strcmp(name, method->name) == 0)
1386: return method;
1387: debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1388: return NULL;
1389: }
1.1 markus 1390:
1.53 markus 1391: /* XXX internal state */
1392: static Authmethod *current = NULL;
1393: static char *supported = NULL;
1394: static char *preferred = NULL;
1.105 deraadt 1395:
1.20 markus 1396: /*
1397: * Given the authentication method list sent by the server, return the
1398: * next method we should try. If the server initially sends a nil list,
1.67 markus 1399: * use a built-in default list.
1.41 stevesk 1400: */
1.76 itojun 1401: static Authmethod *
1.20 markus 1402: authmethod_get(char *authlist)
1403: {
1.53 markus 1404: char *name = NULL;
1.97 markus 1405: u_int next;
1.41 stevesk 1406:
1.20 markus 1407: /* Use a suitable default if we're passed a nil list. */
1408: if (authlist == NULL || strlen(authlist) == 0)
1.53 markus 1409: authlist = options.preferred_authentications;
1.20 markus 1410:
1.53 markus 1411: if (supported == NULL || strcmp(authlist, supported) != 0) {
1412: debug3("start over, passed a different list %s", authlist);
1413: if (supported != NULL)
1414: xfree(supported);
1415: supported = xstrdup(authlist);
1416: preferred = options.preferred_authentications;
1417: debug3("preferred %s", preferred);
1418: current = NULL;
1419: } else if (current != NULL && authmethod_is_enabled(current))
1420: return current;
1.20 markus 1421:
1.53 markus 1422: for (;;) {
1423: if ((name = match_list(preferred, supported, &next)) == NULL) {
1.109 markus 1424: debug("No more authentication methods to try.");
1.53 markus 1425: current = NULL;
1426: return NULL;
1427: }
1428: preferred += next;
1.23 markus 1429: debug3("authmethod_lookup %s", name);
1.53 markus 1430: debug3("remaining preferred: %s", preferred);
1431: if ((current = authmethod_lookup(name)) != NULL &&
1432: authmethod_is_enabled(current)) {
1.23 markus 1433: debug3("authmethod_is_enabled %s", name);
1.109 markus 1434: debug("Next authentication method: %s", name);
1.53 markus 1435: return current;
1.23 markus 1436: }
1.1 markus 1437: }
1.53 markus 1438: }
1.1 markus 1439:
1.79 stevesk 1440: static char *
1.53 markus 1441: authmethods_get(void)
1442: {
1443: Authmethod *method = NULL;
1.93 markus 1444: Buffer b;
1445: char *list;
1.27 provos 1446:
1.93 markus 1447: buffer_init(&b);
1.53 markus 1448: for (method = authmethods; method->name != NULL; method++) {
1449: if (authmethod_is_enabled(method)) {
1.93 markus 1450: if (buffer_len(&b) > 0)
1451: buffer_append(&b, ",", 1);
1452: buffer_append(&b, method->name, strlen(method->name));
1.53 markus 1453: }
1454: }
1.93 markus 1455: buffer_append(&b, "\0", 1);
1456: list = xstrdup(buffer_ptr(&b));
1457: buffer_free(&b);
1458: return list;
1.1 markus 1459: }