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