Annotation of src/usr.bin/ssh/sshconnect2.c, Revision 1.77
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.77 ! markus 26: RCSID("$OpenBSD: sshconnect2.c,v 1.76 2001/06/23 15:12:21 itojun Exp $");
1.1 markus 27:
28: #include <openssl/bn.h>
29: #include <openssl/md5.h>
30: #include <openssl/dh.h>
31: #include <openssl/hmac.h>
32:
33: #include "ssh.h"
1.37 markus 34: #include "ssh2.h"
1.1 markus 35: #include "xmalloc.h"
36: #include "rsa.h"
37: #include "buffer.h"
38: #include "packet.h"
39: #include "uidswap.h"
40: #include "compat.h"
41: #include "bufaux.h"
1.37 markus 42: #include "cipher.h"
1.1 markus 43: #include "kex.h"
44: #include "myproposal.h"
45: #include "key.h"
46: #include "sshconnect.h"
47: #include "authfile.h"
1.57 provos 48: #include "dh.h"
1.17 markus 49: #include "authfd.h"
1.37 markus 50: #include "log.h"
51: #include "readconf.h"
52: #include "readpass.h"
1.53 markus 53: #include "match.h"
1.61 markus 54: #include "dispatch.h"
1.68 markus 55: #include "canohost.h"
1.22 provos 56:
1.1 markus 57: /* import */
58: extern char *client_version_string;
59: extern char *server_version_string;
60: extern Options options;
61:
62: /*
63: * SSH2 key exchange
64: */
65:
1.32 markus 66: u_char *session_id2 = NULL;
1.1 markus 67: int session_id2_len = 0;
68:
1.61 markus 69: char *xxx_host;
70: struct sockaddr *xxx_hostaddr;
71:
1.63 markus 72: Kex *xxx_kex = NULL;
73:
1.76 itojun 74: static int
1.75 markus 75: verify_host_key_callback(Key *hostkey)
1.61 markus 76: {
1.75 markus 77: if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
78: fatal("verify_host_key failed");
1.61 markus 79: return 0;
80: }
81:
1.1 markus 82: void
1.22 provos 83: ssh_kex2(char *host, struct sockaddr *hostaddr)
84: {
85: Kex *kex;
1.61 markus 86:
87: xxx_host = host;
88: xxx_hostaddr = hostaddr;
1.22 provos 89:
1.29 markus 90: if (options.ciphers == (char *)-1) {
91: log("No valid ciphers for protocol version 2 given, using defaults.");
92: options.ciphers = NULL;
1.24 markus 93: }
1.22 provos 94: if (options.ciphers != NULL) {
95: myproposal[PROPOSAL_ENC_ALGS_CTOS] =
96: myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
97: }
1.60 stevesk 98: myproposal[PROPOSAL_ENC_ALGS_CTOS] =
99: compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
100: myproposal[PROPOSAL_ENC_ALGS_STOC] =
101: compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
1.22 provos 102: if (options.compression) {
1.47 markus 103: myproposal[PROPOSAL_COMP_ALGS_CTOS] =
1.22 provos 104: myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib";
105: } else {
1.47 markus 106: myproposal[PROPOSAL_COMP_ALGS_CTOS] =
1.22 provos 107: myproposal[PROPOSAL_COMP_ALGS_STOC] = "none";
1.47 markus 108: }
109: if (options.macs != NULL) {
110: myproposal[PROPOSAL_MAC_ALGS_CTOS] =
111: myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
1.22 provos 112: }
1.70 markus 113: if (options.hostkeyalgorithms != NULL)
114: myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
115: options.hostkeyalgorithms;
1.22 provos 116:
1.65 markus 117: /* start key exchange */
1.64 markus 118: kex = kex_setup(myproposal);
1.61 markus 119: kex->client_version_string=client_version_string;
120: kex->server_version_string=server_version_string;
1.75 markus 121: kex->verify_host_key=&verify_host_key_callback;
1.63 markus 122:
123: xxx_kex = kex;
1.22 provos 124:
1.66 markus 125: dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
1.62 markus 126:
127: session_id2 = kex->session_id;
128: session_id2_len = kex->session_id_len;
1.22 provos 129:
130: #ifdef DEBUG_KEXDH
131: /* send 1st encrypted/maced/compressed message */
132: packet_start(SSH2_MSG_IGNORE);
133: packet_put_cstring("markus");
134: packet_send();
135: packet_write_wait();
136: #endif
1.61 markus 137: debug("done: ssh_kex2.");
1.1 markus 138: }
1.11 markus 139:
1.1 markus 140: /*
141: * Authenticate user
142: */
1.20 markus 143:
144: typedef struct Authctxt Authctxt;
145: typedef struct Authmethod Authmethod;
146:
147: typedef int sign_cb_fn(
148: Authctxt *authctxt, Key *key,
1.32 markus 149: u_char **sigp, int *lenp, u_char *data, int datalen);
1.20 markus 150:
151: struct Authctxt {
152: const char *server_user;
1.68 markus 153: const char *local_user;
1.20 markus 154: const char *host;
155: const char *service;
1.23 markus 156: Authmethod *method;
1.20 markus 157: int success;
1.51 markus 158: char *authlist;
1.68 markus 159: /* pubkey */
1.51 markus 160: Key *last_key;
161: sign_cb_fn *last_key_sign;
162: int last_key_hint;
1.68 markus 163: AuthenticationConnection *agent;
164: /* hostbased */
165: Key **keys;
166: int nkeys;
1.20 markus 167: };
168: struct Authmethod {
169: char *name; /* string to compare against server's list */
170: int (*userauth)(Authctxt *authctxt);
171: int *enabled; /* flag in option struct that enables method */
172: int *batch_flag; /* flag in option struct that disables method */
173: };
174:
175: void input_userauth_success(int type, int plen, void *ctxt);
176: void input_userauth_failure(int type, int plen, void *ctxt);
1.35 markus 177: void input_userauth_banner(int type, int plen, void *ctxt);
1.20 markus 178: void input_userauth_error(int type, int plen, void *ctxt);
1.23 markus 179: void input_userauth_info_req(int type, int plen, void *ctxt);
1.51 markus 180: void input_userauth_pk_ok(int type, int plen, void *ctxt);
1.23 markus 181:
182: int userauth_none(Authctxt *authctxt);
1.20 markus 183: int userauth_pubkey(Authctxt *authctxt);
184: int userauth_passwd(Authctxt *authctxt);
1.23 markus 185: int userauth_kbdint(Authctxt *authctxt);
1.68 markus 186: int userauth_hostbased(Authctxt *authctxt);
1.20 markus 187:
1.51 markus 188: void userauth(Authctxt *authctxt, char *authlist);
189:
1.76 itojun 190: static int sign_and_send_pubkey(Authctxt *, Key *, sign_cb_fn *);
191: static void clear_auth_state(Authctxt *);
192:
193: static Authmethod *authmethod_get(char *authlist);
194: static Authmethod *authmethod_lookup(const char *name);
195: static char *authmethods_get(void);
1.20 markus 196:
197: Authmethod authmethods[] = {
198: {"publickey",
199: userauth_pubkey,
1.28 markus 200: &options.pubkey_authentication,
1.20 markus 201: NULL},
1.74 markus 202: {"hostbased",
203: userauth_hostbased,
204: &options.hostbased_authentication,
205: NULL},
1.20 markus 206: {"password",
207: userauth_passwd,
208: &options.password_authentication,
209: &options.batch_mode},
1.23 markus 210: {"keyboard-interactive",
211: userauth_kbdint,
212: &options.kbd_interactive_authentication,
213: &options.batch_mode},
214: {"none",
215: userauth_none,
216: NULL,
217: NULL},
1.20 markus 218: {NULL, NULL, NULL, NULL}
219: };
220:
221: void
1.68 markus 222: ssh_userauth2(const char *local_user, const char *server_user, char *host,
223: Key **keys, int nkeys)
1.20 markus 224: {
225: Authctxt authctxt;
226: int type;
227: int plen;
1.39 markus 228:
1.73 markus 229: if (options.challenge_response_authentication)
1.39 markus 230: options.kbd_interactive_authentication = 1;
1.20 markus 231:
232: debug("send SSH2_MSG_SERVICE_REQUEST");
233: packet_start(SSH2_MSG_SERVICE_REQUEST);
234: packet_put_cstring("ssh-userauth");
235: packet_send();
236: packet_write_wait();
237: type = packet_read(&plen);
238: if (type != SSH2_MSG_SERVICE_ACCEPT) {
239: fatal("denied SSH2_MSG_SERVICE_ACCEPT: %d", type);
240: }
241: if (packet_remaining() > 0) {
242: char *reply = packet_get_string(&plen);
243: debug("service_accept: %s", reply);
244: xfree(reply);
245: } else {
246: debug("buggy server: service_accept w/o service");
247: }
248: packet_done();
249: debug("got SSH2_MSG_SERVICE_ACCEPT");
250:
1.53 markus 251: if (options.preferred_authentications == NULL)
252: options.preferred_authentications = authmethods_get();
253:
1.20 markus 254: /* setup authentication context */
255: authctxt.agent = ssh_get_authentication_connection();
256: authctxt.server_user = server_user;
1.68 markus 257: authctxt.local_user = local_user;
1.20 markus 258: authctxt.host = host;
259: authctxt.service = "ssh-connection"; /* service name */
260: authctxt.success = 0;
1.23 markus 261: authctxt.method = authmethod_lookup("none");
1.51 markus 262: authctxt.authlist = NULL;
1.68 markus 263: authctxt.keys = keys;
264: authctxt.nkeys = nkeys;
1.23 markus 265: if (authctxt.method == NULL)
266: fatal("ssh_userauth2: internal error: cannot send userauth none request");
1.20 markus 267:
268: /* initial userauth request */
1.23 markus 269: userauth_none(&authctxt);
1.20 markus 270:
1.65 markus 271: dispatch_init(&input_userauth_error);
1.20 markus 272: dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
273: dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
1.35 markus 274: dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
1.20 markus 275: dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
276:
277: if (authctxt.agent != NULL)
278: ssh_close_authentication_connection(authctxt.agent);
279:
1.34 markus 280: debug("ssh-userauth2 successful: method %s", authctxt.method->name);
1.20 markus 281: }
282: void
1.51 markus 283: userauth(Authctxt *authctxt, char *authlist)
284: {
285: if (authlist == NULL) {
286: authlist = authctxt->authlist;
287: } else {
288: if (authctxt->authlist)
289: xfree(authctxt->authlist);
290: authctxt->authlist = authlist;
291: }
292: for (;;) {
293: Authmethod *method = authmethod_get(authlist);
294: if (method == NULL)
295: fatal("Permission denied (%s).", authlist);
296: authctxt->method = method;
297: if (method->userauth(authctxt) != 0) {
298: debug2("we sent a %s packet, wait for reply", method->name);
299: break;
300: } else {
301: debug2("we did not send a packet, disable method");
302: method->enabled = NULL;
303: }
304: }
305: }
306: void
1.20 markus 307: input_userauth_error(int type, int plen, void *ctxt)
308: {
1.35 markus 309: fatal("input_userauth_error: bad message during authentication: "
310: "type %d", type);
311: }
312: void
313: input_userauth_banner(int type, int plen, void *ctxt)
314: {
315: char *msg, *lang;
316: debug3("input_userauth_banner");
317: msg = packet_get_string(NULL);
318: lang = packet_get_string(NULL);
319: fprintf(stderr, "%s", msg);
320: xfree(msg);
321: xfree(lang);
1.20 markus 322: }
323: void
324: input_userauth_success(int type, int plen, void *ctxt)
325: {
326: Authctxt *authctxt = ctxt;
327: if (authctxt == NULL)
328: fatal("input_userauth_success: no authentication context");
1.51 markus 329: if (authctxt->authlist)
330: xfree(authctxt->authlist);
331: clear_auth_state(authctxt);
1.20 markus 332: authctxt->success = 1; /* break out */
333: }
334: void
335: input_userauth_failure(int type, int plen, void *ctxt)
336: {
337: Authctxt *authctxt = ctxt;
338: char *authlist = NULL;
339: int partial;
340:
341: if (authctxt == NULL)
342: fatal("input_userauth_failure: no authentication context");
343:
1.23 markus 344: authlist = packet_get_string(NULL);
1.20 markus 345: partial = packet_get_char();
346: packet_done();
347:
348: if (partial != 0)
1.45 markus 349: log("Authenticated with partial success.");
1.20 markus 350: debug("authentications that can continue: %s", authlist);
351:
1.51 markus 352: clear_auth_state(authctxt);
353: userauth(authctxt, authlist);
354: }
355: void
356: input_userauth_pk_ok(int type, int plen, void *ctxt)
357: {
358: Authctxt *authctxt = ctxt;
359: Key *key = NULL;
360: Buffer b;
1.69 markus 361: int alen, blen, sent = 0;
1.54 markus 362: char *pkalg, *pkblob, *fp;
1.51 markus 363:
364: if (authctxt == NULL)
365: fatal("input_userauth_pk_ok: no authentication context");
366: if (datafellows & SSH_BUG_PKOK) {
367: /* this is similar to SSH_BUG_PKAUTH */
368: debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
369: pkblob = packet_get_string(&blen);
370: buffer_init(&b);
371: buffer_append(&b, pkblob, blen);
372: pkalg = buffer_get_string(&b, &alen);
373: buffer_free(&b);
374: } else {
375: pkalg = packet_get_string(&alen);
376: pkblob = packet_get_string(&blen);
377: }
378: packet_done();
379:
380: debug("input_userauth_pk_ok: pkalg %s blen %d lastkey %p hint %d",
381: pkalg, blen, authctxt->last_key, authctxt->last_key_hint);
382:
383: do {
384: if (authctxt->last_key == NULL ||
385: authctxt->last_key_sign == NULL) {
386: debug("no last key or no sign cb");
387: break;
388: }
1.69 markus 389: if (key_type_from_name(pkalg) == KEY_UNSPEC) {
1.51 markus 390: debug("unknown pkalg %s", pkalg);
391: break;
392: }
393: if ((key = key_from_blob(pkblob, blen)) == NULL) {
394: debug("no key from blob. pkalg %s", pkalg);
395: break;
396: }
1.54 markus 397: fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
398: debug2("input_userauth_pk_ok: fp %s", fp);
399: xfree(fp);
1.51 markus 400: if (!key_equal(key, authctxt->last_key)) {
401: debug("key != last_key");
1.20 markus 402: break;
403: }
1.51 markus 404: sent = sign_and_send_pubkey(authctxt, key,
405: authctxt->last_key_sign);
406: } while(0);
407:
408: if (key != NULL)
409: key_free(key);
410: xfree(pkalg);
411: xfree(pkblob);
412:
413: /* unregister */
414: clear_auth_state(authctxt);
415: dispatch_set(SSH2_MSG_USERAUTH_PK_OK, NULL);
416:
417: /* try another method if we did not send a packet*/
418: if (sent == 0)
419: userauth(authctxt, NULL);
420:
1.20 markus 421: }
422:
1.1 markus 423: int
1.23 markus 424: userauth_none(Authctxt *authctxt)
425: {
426: /* initial userauth request */
427: packet_start(SSH2_MSG_USERAUTH_REQUEST);
428: packet_put_cstring(authctxt->server_user);
429: packet_put_cstring(authctxt->service);
430: packet_put_cstring(authctxt->method->name);
431: packet_send();
432: return 1;
433: }
434:
435: int
1.20 markus 436: userauth_passwd(Authctxt *authctxt)
1.1 markus 437: {
1.6 markus 438: static int attempt = 0;
1.1 markus 439: char prompt[80];
440: char *password;
1.6 markus 441:
1.13 todd 442: if (attempt++ >= options.number_of_password_prompts)
1.6 markus 443: return 0;
1.13 todd 444:
445: if(attempt != 1)
446: error("Permission denied, please try again.");
1.1 markus 447:
1.43 itojun 448: snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
1.20 markus 449: authctxt->server_user, authctxt->host);
1.1 markus 450: password = read_passphrase(prompt, 0);
451: packet_start(SSH2_MSG_USERAUTH_REQUEST);
1.20 markus 452: packet_put_cstring(authctxt->server_user);
453: packet_put_cstring(authctxt->service);
1.23 markus 454: packet_put_cstring(authctxt->method->name);
1.1 markus 455: packet_put_char(0);
1.49 markus 456: packet_put_cstring(password);
1.1 markus 457: memset(password, 0, strlen(password));
458: xfree(password);
1.49 markus 459: packet_inject_ignore(64);
1.1 markus 460: packet_send();
461: return 1;
462: }
463:
1.51 markus 464: void
465: clear_auth_state(Authctxt *authctxt)
466: {
467: /* XXX clear authentication state */
468: if (authctxt->last_key != NULL && authctxt->last_key_hint == -1) {
469: debug3("clear_auth_state: key_free %p", authctxt->last_key);
470: key_free(authctxt->last_key);
471: }
472: authctxt->last_key = NULL;
473: authctxt->last_key_hint = -2;
474: authctxt->last_key_sign = NULL;
475: }
476:
1.76 itojun 477: static int
1.20 markus 478: sign_and_send_pubkey(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback)
1.1 markus 479: {
480: Buffer b;
1.32 markus 481: u_char *blob, *signature;
1.1 markus 482: int bloblen, slen;
1.14 markus 483: int skip = 0;
1.17 markus 484: int ret = -1;
1.23 markus 485: int have_sig = 1;
1.1 markus 486:
1.30 markus 487: debug3("sign_and_send_pubkey");
1.51 markus 488:
1.28 markus 489: if (key_to_blob(k, &blob, &bloblen) == 0) {
490: /* we cannot handle this key */
1.30 markus 491: debug3("sign_and_send_pubkey: cannot handle key");
1.28 markus 492: return 0;
493: }
1.1 markus 494: /* data to be signed */
495: buffer_init(&b);
1.26 markus 496: if (datafellows & SSH_OLD_SESSIONID) {
497: buffer_append(&b, session_id2, session_id2_len);
1.41 stevesk 498: skip = session_id2_len;
1.26 markus 499: } else {
1.14 markus 500: buffer_put_string(&b, session_id2, session_id2_len);
501: skip = buffer_len(&b);
502: }
1.1 markus 503: buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1.20 markus 504: buffer_put_cstring(&b, authctxt->server_user);
1.10 markus 505: buffer_put_cstring(&b,
1.30 markus 506: datafellows & SSH_BUG_PKSERVICE ?
1.10 markus 507: "ssh-userauth" :
1.20 markus 508: authctxt->service);
1.30 markus 509: if (datafellows & SSH_BUG_PKAUTH) {
510: buffer_put_char(&b, have_sig);
511: } else {
512: buffer_put_cstring(&b, authctxt->method->name);
513: buffer_put_char(&b, have_sig);
1.41 stevesk 514: buffer_put_cstring(&b, key_ssh_name(k));
1.30 markus 515: }
1.1 markus 516: buffer_put_string(&b, blob, bloblen);
517:
518: /* generate signature */
1.51 markus 519: ret = (*sign_callback)(authctxt, k, &signature, &slen,
520: buffer_ptr(&b), buffer_len(&b));
1.17 markus 521: if (ret == -1) {
522: xfree(blob);
523: buffer_free(&b);
524: return 0;
525: }
1.28 markus 526: #ifdef DEBUG_PK
1.1 markus 527: buffer_dump(&b);
528: #endif
1.30 markus 529: if (datafellows & SSH_BUG_PKSERVICE) {
1.10 markus 530: buffer_clear(&b);
531: buffer_append(&b, session_id2, session_id2_len);
1.51 markus 532: skip = session_id2_len;
1.10 markus 533: buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1.20 markus 534: buffer_put_cstring(&b, authctxt->server_user);
535: buffer_put_cstring(&b, authctxt->service);
1.23 markus 536: buffer_put_cstring(&b, authctxt->method->name);
537: buffer_put_char(&b, have_sig);
1.30 markus 538: if (!(datafellows & SSH_BUG_PKAUTH))
1.41 stevesk 539: buffer_put_cstring(&b, key_ssh_name(k));
1.10 markus 540: buffer_put_string(&b, blob, bloblen);
541: }
542: xfree(blob);
1.51 markus 543:
1.1 markus 544: /* append signature */
545: buffer_put_string(&b, signature, slen);
546: xfree(signature);
547:
548: /* skip session id and packet type */
1.14 markus 549: if (buffer_len(&b) < skip + 1)
1.20 markus 550: fatal("userauth_pubkey: internal error");
1.14 markus 551: buffer_consume(&b, skip + 1);
1.1 markus 552:
553: /* put remaining data from buffer into packet */
554: packet_start(SSH2_MSG_USERAUTH_REQUEST);
555: packet_put_raw(buffer_ptr(&b), buffer_len(&b));
556: buffer_free(&b);
557: packet_send();
1.17 markus 558:
559: return 1;
1.16 markus 560: }
561:
1.76 itojun 562: static int
1.51 markus 563: send_pubkey_test(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback,
564: int hint)
1.20 markus 565: {
1.51 markus 566: u_char *blob;
567: int bloblen, have_sig = 0;
1.20 markus 568:
1.51 markus 569: debug3("send_pubkey_test");
1.16 markus 570:
1.51 markus 571: if (key_to_blob(k, &blob, &bloblen) == 0) {
572: /* we cannot handle this key */
573: debug3("send_pubkey_test: cannot handle key");
1.16 markus 574: return 0;
575: }
1.51 markus 576: /* register callback for USERAUTH_PK_OK message */
577: authctxt->last_key_sign = sign_callback;
578: authctxt->last_key_hint = hint;
579: authctxt->last_key = k;
580: dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
581:
582: packet_start(SSH2_MSG_USERAUTH_REQUEST);
583: packet_put_cstring(authctxt->server_user);
584: packet_put_cstring(authctxt->service);
585: packet_put_cstring(authctxt->method->name);
586: packet_put_char(have_sig);
587: if (!(datafellows & SSH_BUG_PKAUTH))
588: packet_put_cstring(key_ssh_name(k));
589: packet_put_string(blob, bloblen);
590: xfree(blob);
591: packet_send();
592: return 1;
593: }
594:
1.76 itojun 595: static Key *
1.51 markus 596: load_identity_file(char *filename)
597: {
598: Key *private;
599: char prompt[300], *passphrase;
1.56 markus 600: int quit, i;
1.52 markus 601: struct stat st;
1.16 markus 602:
1.52 markus 603: if (stat(filename, &st) < 0) {
604: debug3("no such identity: %s", filename);
605: return NULL;
606: }
1.56 markus 607: private = key_load_private_type(KEY_UNSPEC, filename, "", NULL);
608: if (private == NULL) {
609: if (options.batch_mode)
1.51 markus 610: return NULL;
1.16 markus 611: snprintf(prompt, sizeof prompt,
1.28 markus 612: "Enter passphrase for key '%.100s': ", filename);
1.20 markus 613: for (i = 0; i < options.number_of_password_prompts; i++) {
614: passphrase = read_passphrase(prompt, 0);
615: if (strcmp(passphrase, "") != 0) {
1.56 markus 616: private = key_load_private_type(KEY_UNSPEC, filename,
617: passphrase, NULL);
1.51 markus 618: quit = 0;
1.20 markus 619: } else {
620: debug2("no passphrase given, try next key");
1.51 markus 621: quit = 1;
1.20 markus 622: }
623: memset(passphrase, 0, strlen(passphrase));
624: xfree(passphrase);
1.56 markus 625: if (private != NULL || quit)
1.20 markus 626: break;
627: debug2("bad passphrase given, try again...");
1.16 markus 628: }
629: }
1.51 markus 630: return private;
631: }
632:
1.76 itojun 633: static int
1.51 markus 634: identity_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
635: u_char *data, int datalen)
636: {
637: Key *private;
638: int idx, ret;
639:
640: idx = authctxt->last_key_hint;
641: if (idx < 0)
642: return -1;
643: private = load_identity_file(options.identity_files[idx]);
644: if (private == NULL)
645: return -1;
646: ret = key_sign(private, sigp, lenp, data, datalen);
647: key_free(private);
1.17 markus 648: return ret;
649: }
650:
1.76 itojun 651: static int
652: agent_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
1.32 markus 653: u_char *data, int datalen)
1.17 markus 654: {
1.20 markus 655: return ssh_agent_sign(authctxt->agent, key, sigp, lenp, data, datalen);
1.17 markus 656: }
657:
1.76 itojun 658: static int
659: key_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
1.51 markus 660: u_char *data, int datalen)
661: {
1.67 markus 662: return key_sign(key, sigp, lenp, data, datalen);
1.51 markus 663: }
664:
1.76 itojun 665: static int
1.20 markus 666: userauth_pubkey_agent(Authctxt *authctxt)
1.17 markus 667: {
668: static int called = 0;
1.28 markus 669: int ret = 0;
1.17 markus 670: char *comment;
671: Key *k;
672:
673: if (called == 0) {
1.28 markus 674: if (ssh_get_num_identities(authctxt->agent, 2) == 0)
675: debug2("userauth_pubkey_agent: no keys at all");
1.20 markus 676: called = 1;
1.17 markus 677: }
1.28 markus 678: k = ssh_get_next_identity(authctxt->agent, &comment, 2);
1.20 markus 679: if (k == NULL) {
1.28 markus 680: debug2("userauth_pubkey_agent: no more keys");
681: } else {
1.51 markus 682: debug("userauth_pubkey_agent: testing agent key %s", comment);
1.28 markus 683: xfree(comment);
1.51 markus 684: ret = send_pubkey_test(authctxt, k, agent_sign_cb, -1);
685: if (ret == 0)
686: key_free(k);
1.20 markus 687: }
1.28 markus 688: if (ret == 0)
689: debug2("userauth_pubkey_agent: no message sent");
1.17 markus 690: return ret;
1.1 markus 691: }
692:
1.20 markus 693: int
694: userauth_pubkey(Authctxt *authctxt)
695: {
696: static int idx = 0;
697: int sent = 0;
1.51 markus 698: Key *key;
699: char *filename;
1.20 markus 700:
1.28 markus 701: if (authctxt->agent != NULL) {
702: do {
703: sent = userauth_pubkey_agent(authctxt);
704: } while(!sent && authctxt->agent->howmany > 0);
705: }
706: while (!sent && idx < options.num_identity_files) {
1.51 markus 707: key = options.identity_keys[idx];
708: filename = options.identity_files[idx];
709: if (key == NULL) {
710: debug("try privkey: %s", filename);
711: key = load_identity_file(filename);
712: if (key != NULL) {
713: sent = sign_and_send_pubkey(authctxt, key,
714: key_sign_cb);
715: key_free(key);
716: }
717: } else if (key->type != KEY_RSA1) {
718: debug("try pubkey: %s", filename);
719: sent = send_pubkey_test(authctxt, key,
720: identity_sign_cb, idx);
721: }
1.28 markus 722: idx++;
723: }
1.20 markus 724: return sent;
725: }
726:
1.23 markus 727: /*
728: * Send userauth request message specifying keyboard-interactive method.
729: */
730: int
731: userauth_kbdint(Authctxt *authctxt)
732: {
733: static int attempt = 0;
734:
735: if (attempt++ >= options.number_of_password_prompts)
736: return 0;
737:
738: debug2("userauth_kbdint");
739: packet_start(SSH2_MSG_USERAUTH_REQUEST);
740: packet_put_cstring(authctxt->server_user);
741: packet_put_cstring(authctxt->service);
742: packet_put_cstring(authctxt->method->name);
743: packet_put_cstring(""); /* lang */
744: packet_put_cstring(options.kbd_interactive_devices ?
745: options.kbd_interactive_devices : "");
746: packet_send();
747:
748: dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
749: return 1;
750: }
751:
752: /*
1.46 markus 753: * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1.23 markus 754: */
755: void
756: input_userauth_info_req(int type, int plen, void *ctxt)
757: {
758: Authctxt *authctxt = ctxt;
1.46 markus 759: char *name, *inst, *lang, *prompt, *response;
1.32 markus 760: u_int num_prompts, i;
1.23 markus 761: int echo = 0;
762:
763: debug2("input_userauth_info_req");
764:
765: if (authctxt == NULL)
766: fatal("input_userauth_info_req: no authentication context");
767:
768: name = packet_get_string(NULL);
769: inst = packet_get_string(NULL);
770: lang = packet_get_string(NULL);
771: if (strlen(name) > 0)
1.77 ! markus 772: log(name);
1.23 markus 773: if (strlen(inst) > 0)
1.77 ! markus 774: log(inst);
1.46 markus 775: xfree(name);
1.23 markus 776: xfree(inst);
1.46 markus 777: xfree(lang);
1.23 markus 778:
779: num_prompts = packet_get_int();
780: /*
781: * Begin to build info response packet based on prompts requested.
782: * We commit to providing the correct number of responses, so if
783: * further on we run into a problem that prevents this, we have to
784: * be sure and clean this up and send a correct error response.
785: */
786: packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
787: packet_put_int(num_prompts);
788:
1.73 markus 789: debug2("input_userauth_info_req: num_prompts %d", num_prompts);
1.23 markus 790: for (i = 0; i < num_prompts; i++) {
791: prompt = packet_get_string(NULL);
792: echo = packet_get_char();
793:
1.77 ! markus 794: response = read_passphrase(prompt, echo ? RP_ECHO : 0);
1.23 markus 795:
1.49 markus 796: packet_put_cstring(response);
1.23 markus 797: memset(response, 0, strlen(response));
798: xfree(response);
799: xfree(prompt);
800: }
801: packet_done(); /* done with parsing incoming message. */
802:
1.49 markus 803: packet_inject_ignore(64);
1.23 markus 804: packet_send();
1.68 markus 805: }
806:
807: /*
808: * this will be move to an external program (ssh-keysign) ASAP. ssh-keysign
809: * will be setuid-root and the sbit can be removed from /usr/bin/ssh.
810: */
811: int
812: userauth_hostbased(Authctxt *authctxt)
813: {
814: Key *private = NULL;
815: Buffer b;
816: u_char *signature, *blob;
817: char *chost, *pkalg, *p;
1.72 markus 818: const char *service;
1.68 markus 819: u_int blen, slen;
1.71 markus 820: int ok, i, len, found = 0;
1.68 markus 821:
822: p = get_local_name(packet_get_connection_in());
823: if (p == NULL) {
824: error("userauth_hostbased: cannot get local ipaddr/name");
825: return 0;
826: }
1.71 markus 827: len = strlen(p) + 2;
828: chost = xmalloc(len);
829: strlcpy(chost, p, len);
830: strlcat(chost, ".", len);
1.68 markus 831: debug2("userauth_hostbased: chost %s", chost);
832: /* check for a useful key */
833: for (i = 0; i < authctxt->nkeys; i++) {
834: private = authctxt->keys[i];
835: if (private && private->type != KEY_RSA1) {
836: found = 1;
837: /* we take and free the key */
838: authctxt->keys[i] = NULL;
839: break;
840: }
841: }
842: if (!found) {
843: xfree(chost);
844: return 0;
845: }
846: if (key_to_blob(private, &blob, &blen) == 0) {
847: key_free(private);
848: xfree(chost);
849: return 0;
850: }
1.72 markus 851: service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
852: authctxt->service;
1.68 markus 853: pkalg = xstrdup(key_ssh_name(private));
854: buffer_init(&b);
855: /* construct data */
1.72 markus 856: buffer_put_string(&b, session_id2, session_id2_len);
1.68 markus 857: buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
858: buffer_put_cstring(&b, authctxt->server_user);
1.72 markus 859: buffer_put_cstring(&b, service);
1.68 markus 860: buffer_put_cstring(&b, authctxt->method->name);
861: buffer_put_cstring(&b, pkalg);
862: buffer_put_string(&b, blob, blen);
863: buffer_put_cstring(&b, chost);
864: buffer_put_cstring(&b, authctxt->local_user);
865: #ifdef DEBUG_PK
866: buffer_dump(&b);
867: #endif
868: debug2("xxx: chost %s", chost);
869: ok = key_sign(private, &signature, &slen, buffer_ptr(&b), buffer_len(&b));
870: key_free(private);
871: buffer_free(&b);
872: if (ok != 0) {
873: error("key_sign failed");
874: xfree(chost);
875: xfree(pkalg);
876: return 0;
877: }
878: packet_start(SSH2_MSG_USERAUTH_REQUEST);
879: packet_put_cstring(authctxt->server_user);
880: packet_put_cstring(authctxt->service);
881: packet_put_cstring(authctxt->method->name);
882: packet_put_cstring(pkalg);
883: packet_put_string(blob, blen);
884: packet_put_cstring(chost);
885: packet_put_cstring(authctxt->local_user);
886: packet_put_string(signature, slen);
887: memset(signature, 's', slen);
888: xfree(signature);
889: xfree(chost);
890: xfree(pkalg);
891:
892: packet_send();
893: return 1;
1.23 markus 894: }
1.20 markus 895:
896: /* find auth method */
897:
898: /*
899: * given auth method name, if configurable options permit this method fill
900: * in auth_ident field and return true, otherwise return false.
901: */
1.76 itojun 902: static int
1.20 markus 903: authmethod_is_enabled(Authmethod *method)
904: {
905: if (method == NULL)
906: return 0;
907: /* return false if options indicate this method is disabled */
908: if (method->enabled == NULL || *method->enabled == 0)
909: return 0;
910: /* return false if batch mode is enabled but method needs interactive mode */
911: if (method->batch_flag != NULL && *method->batch_flag != 0)
912: return 0;
913: return 1;
914: }
915:
1.76 itojun 916: static Authmethod *
1.20 markus 917: authmethod_lookup(const char *name)
1.1 markus 918: {
1.20 markus 919: Authmethod *method = NULL;
920: if (name != NULL)
921: for (method = authmethods; method->name != NULL; method++)
922: if (strcmp(name, method->name) == 0)
923: return method;
924: debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
925: return NULL;
926: }
1.1 markus 927:
1.53 markus 928: /* XXX internal state */
929: static Authmethod *current = NULL;
930: static char *supported = NULL;
931: static char *preferred = NULL;
1.20 markus 932: /*
933: * Given the authentication method list sent by the server, return the
934: * next method we should try. If the server initially sends a nil list,
1.67 markus 935: * use a built-in default list.
1.41 stevesk 936: */
1.76 itojun 937: static Authmethod *
1.20 markus 938: authmethod_get(char *authlist)
939: {
1.53 markus 940:
941: char *name = NULL;
942: int next;
1.41 stevesk 943:
1.20 markus 944: /* Use a suitable default if we're passed a nil list. */
945: if (authlist == NULL || strlen(authlist) == 0)
1.53 markus 946: authlist = options.preferred_authentications;
1.20 markus 947:
1.53 markus 948: if (supported == NULL || strcmp(authlist, supported) != 0) {
949: debug3("start over, passed a different list %s", authlist);
950: if (supported != NULL)
951: xfree(supported);
952: supported = xstrdup(authlist);
953: preferred = options.preferred_authentications;
954: debug3("preferred %s", preferred);
955: current = NULL;
956: } else if (current != NULL && authmethod_is_enabled(current))
957: return current;
1.20 markus 958:
1.53 markus 959: for (;;) {
960: if ((name = match_list(preferred, supported, &next)) == NULL) {
961: debug("no more auth methods to try");
962: current = NULL;
963: return NULL;
964: }
965: preferred += next;
1.23 markus 966: debug3("authmethod_lookup %s", name);
1.53 markus 967: debug3("remaining preferred: %s", preferred);
968: if ((current = authmethod_lookup(name)) != NULL &&
969: authmethod_is_enabled(current)) {
1.23 markus 970: debug3("authmethod_is_enabled %s", name);
1.53 markus 971: debug("next auth method to try is %s", name);
972: return current;
1.23 markus 973: }
1.1 markus 974: }
1.53 markus 975: }
1.1 markus 976:
1.27 provos 977:
1.53 markus 978: #define DELIM ","
979: char *
980: authmethods_get(void)
981: {
982: Authmethod *method = NULL;
983: char buf[1024];
1.27 provos 984:
1.53 markus 985: buf[0] = '\0';
986: for (method = authmethods; method->name != NULL; method++) {
987: if (authmethod_is_enabled(method)) {
988: if (buf[0] != '\0')
989: strlcat(buf, DELIM, sizeof buf);
990: strlcat(buf, method->name, sizeof buf);
991: }
992: }
993: return xstrdup(buf);
1.1 markus 994: }