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