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