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