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