]> andersk Git - openssh.git/blame - sshconnect2.c
- deraadt@cvs.openbsd.org 2001/06/23 02:33:05
[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"
3398dda9 26RCSID("$OpenBSD: sshconnect2.c,v 1.74 2001/05/19 16:32:16 markus Exp $");
a306f2dd 27
28#include <openssl/bn.h>
a306f2dd 29#include <openssl/md5.h>
30#include <openssl/dh.h>
31#include <openssl/hmac.h>
32
33#include "ssh.h"
42f11eb2 34#include "ssh2.h"
a306f2dd 35#include "xmalloc.h"
36#include "rsa.h"
37#include "buffer.h"
38#include "packet.h"
a306f2dd 39#include "uidswap.h"
40#include "compat.h"
a306f2dd 41#include "bufaux.h"
42f11eb2 42#include "cipher.h"
a306f2dd 43#include "kex.h"
44#include "myproposal.h"
45#include "key.h"
a306f2dd 46#include "sshconnect.h"
47#include "authfile.h"
94ec8c6b 48#include "cli.h"
db1cd2f3 49#include "dh.h"
2e73a022 50#include "authfd.h"
42f11eb2 51#include "log.h"
52#include "readconf.h"
53#include "readpass.h"
cab80f75 54#include "match.h"
a5c9ffdb 55#include "dispatch.h"
8002af61 56#include "canohost.h"
94ec8c6b 57
a306f2dd 58/* import */
59extern char *client_version_string;
60extern char *server_version_string;
61extern Options options;
62
63/*
64 * SSH2 key exchange
65 */
66
1e3b8b07 67u_char *session_id2 = NULL;
a306f2dd 68int session_id2_len = 0;
69
a5c9ffdb 70char *xxx_host;
71struct sockaddr *xxx_hostaddr;
72
e092ce67 73Kex *xxx_kex = NULL;
74
a5c9ffdb 75int
76check_host_key_callback(Key *hostkey)
77{
78 check_host_key(xxx_host, xxx_hostaddr, hostkey,
79 options.user_hostfile2, options.system_hostfile2);
80 return 0;
81}
82
a306f2dd 83void
94ec8c6b 84ssh_kex2(char *host, struct sockaddr *hostaddr)
85{
94ec8c6b 86 Kex *kex;
a5c9ffdb 87
88 xxx_host = host;
89 xxx_hostaddr = hostaddr;
94ec8c6b 90
c523303b 91 if (options.ciphers == (char *)-1) {
92 log("No valid ciphers for protocol version 2 given, using defaults.");
93 options.ciphers = NULL;
94ec8c6b 94 }
95 if (options.ciphers != NULL) {
96 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
97 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
98 }
1ad64a93 99 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
100 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
101 myproposal[PROPOSAL_ENC_ALGS_STOC] =
102 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
94ec8c6b 103 if (options.compression) {
b2552997 104 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
94ec8c6b 105 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib";
106 } else {
b2552997 107 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
94ec8c6b 108 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none";
109 }
b2552997 110 if (options.macs != NULL) {
111 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
112 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
113 }
e961a8f9 114 if (options.hostkeyalgorithms != NULL)
115 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
116 options.hostkeyalgorithms;
94ec8c6b 117
7a37c112 118 /* start key exchange */
d8ee838b 119 kex = kex_setup(myproposal);
a5c9ffdb 120 kex->client_version_string=client_version_string;
121 kex->server_version_string=server_version_string;
122 kex->check_host_key=&check_host_key_callback;
94ec8c6b 123
e092ce67 124 xxx_kex = kex;
125
c422989b 126 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
94ec8c6b 127
d1ac6175 128 session_id2 = kex->session_id;
129 session_id2_len = kex->session_id_len;
130
94ec8c6b 131#ifdef DEBUG_KEXDH
132 /* send 1st encrypted/maced/compressed message */
133 packet_start(SSH2_MSG_IGNORE);
134 packet_put_cstring("markus");
135 packet_send();
136 packet_write_wait();
137#endif
a5c9ffdb 138 debug("done: ssh_kex2.");
a306f2dd 139}
71276795 140
a306f2dd 141/*
142 * Authenticate user
143 */
188adeb2 144
145typedef struct Authctxt Authctxt;
146typedef struct Authmethod Authmethod;
147
148typedef int sign_cb_fn(
149 Authctxt *authctxt, Key *key,
1e3b8b07 150 u_char **sigp, int *lenp, u_char *data, int datalen);
188adeb2 151
152struct Authctxt {
153 const char *server_user;
8002af61 154 const char *local_user;
188adeb2 155 const char *host;
156 const char *service;
188adeb2 157 Authmethod *method;
94ec8c6b 158 int success;
fee56204 159 char *authlist;
8002af61 160 /* pubkey */
fee56204 161 Key *last_key;
162 sign_cb_fn *last_key_sign;
163 int last_key_hint;
8002af61 164 AuthenticationConnection *agent;
165 /* hostbased */
166 Key **keys;
167 int nkeys;
188adeb2 168};
169struct Authmethod {
170 char *name; /* string to compare against server's list */
171 int (*userauth)(Authctxt *authctxt);
172 int *enabled; /* flag in option struct that enables method */
173 int *batch_flag; /* flag in option struct that disables method */
174};
175
176void input_userauth_success(int type, int plen, void *ctxt);
177void input_userauth_failure(int type, int plen, void *ctxt);
9616313f 178void input_userauth_banner(int type, int plen, void *ctxt);
188adeb2 179void input_userauth_error(int type, int plen, void *ctxt);
94ec8c6b 180void input_userauth_info_req(int type, int plen, void *ctxt);
fee56204 181void input_userauth_pk_ok(int type, int plen, void *ctxt);
94ec8c6b 182
183int userauth_none(Authctxt *authctxt);
188adeb2 184int userauth_pubkey(Authctxt *authctxt);
185int userauth_passwd(Authctxt *authctxt);
94ec8c6b 186int userauth_kbdint(Authctxt *authctxt);
8002af61 187int userauth_hostbased(Authctxt *authctxt);
188adeb2 188
fee56204 189void userauth(Authctxt *authctxt, char *authlist);
190
191int
192sign_and_send_pubkey(Authctxt *authctxt, Key *k,
193 sign_cb_fn *sign_callback);
194void clear_auth_state(Authctxt *authctxt);
195
94ec8c6b 196Authmethod *authmethod_get(char *authlist);
197Authmethod *authmethod_lookup(const char *name);
cab80f75 198char *authmethods_get(void);
188adeb2 199
200Authmethod authmethods[] = {
201 {"publickey",
202 userauth_pubkey,
fa08c86b 203 &options.pubkey_authentication,
188adeb2 204 NULL},
3398dda9 205 {"hostbased",
206 userauth_hostbased,
207 &options.hostbased_authentication,
208 NULL},
188adeb2 209 {"password",
210 userauth_passwd,
211 &options.password_authentication,
212 &options.batch_mode},
94ec8c6b 213 {"keyboard-interactive",
214 userauth_kbdint,
215 &options.kbd_interactive_authentication,
216 &options.batch_mode},
217 {"none",
218 userauth_none,
219 NULL,
220 NULL},
188adeb2 221 {NULL, NULL, NULL, NULL}
222};
223
224void
8002af61 225ssh_userauth2(const char *local_user, const char *server_user, char *host,
226 Key **keys, int nkeys)
188adeb2 227{
228 Authctxt authctxt;
229 int type;
230 int plen;
231
5ba55ada 232 if (options.challenge_response_authentication)
d464095c 233 options.kbd_interactive_authentication = 1;
234
188adeb2 235 debug("send SSH2_MSG_SERVICE_REQUEST");
236 packet_start(SSH2_MSG_SERVICE_REQUEST);
237 packet_put_cstring("ssh-userauth");
238 packet_send();
239 packet_write_wait();
240 type = packet_read(&plen);
241 if (type != SSH2_MSG_SERVICE_ACCEPT) {
242 fatal("denied SSH2_MSG_SERVICE_ACCEPT: %d", type);
243 }
244 if (packet_remaining() > 0) {
245 char *reply = packet_get_string(&plen);
246 debug("service_accept: %s", reply);
247 xfree(reply);
188adeb2 248 } else {
249 debug("buggy server: service_accept w/o service");
250 }
251 packet_done();
252 debug("got SSH2_MSG_SERVICE_ACCEPT");
253
cab80f75 254 if (options.preferred_authentications == NULL)
255 options.preferred_authentications = authmethods_get();
256
188adeb2 257 /* setup authentication context */
258 authctxt.agent = ssh_get_authentication_connection();
259 authctxt.server_user = server_user;
8002af61 260 authctxt.local_user = local_user;
188adeb2 261 authctxt.host = host;
262 authctxt.service = "ssh-connection"; /* service name */
263 authctxt.success = 0;
94ec8c6b 264 authctxt.method = authmethod_lookup("none");
fee56204 265 authctxt.authlist = NULL;
8002af61 266 authctxt.keys = keys;
267 authctxt.nkeys = nkeys;
94ec8c6b 268 if (authctxt.method == NULL)
269 fatal("ssh_userauth2: internal error: cannot send userauth none request");
188adeb2 270
271 /* initial userauth request */
94ec8c6b 272 userauth_none(&authctxt);
188adeb2 273
7a37c112 274 dispatch_init(&input_userauth_error);
188adeb2 275 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
276 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
9616313f 277 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
188adeb2 278 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
279
280 if (authctxt.agent != NULL)
281 ssh_close_authentication_connection(authctxt.agent);
282
8abcdba4 283 debug("ssh-userauth2 successful: method %s", authctxt.method->name);
188adeb2 284}
285void
fee56204 286userauth(Authctxt *authctxt, char *authlist)
287{
288 if (authlist == NULL) {
289 authlist = authctxt->authlist;
290 } else {
291 if (authctxt->authlist)
292 xfree(authctxt->authlist);
293 authctxt->authlist = authlist;
294 }
295 for (;;) {
296 Authmethod *method = authmethod_get(authlist);
297 if (method == NULL)
298 fatal("Permission denied (%s).", authlist);
299 authctxt->method = method;
300 if (method->userauth(authctxt) != 0) {
301 debug2("we sent a %s packet, wait for reply", method->name);
302 break;
303 } else {
304 debug2("we did not send a packet, disable method");
305 method->enabled = NULL;
306 }
307 }
308}
309void
188adeb2 310input_userauth_error(int type, int plen, void *ctxt)
311{
9616313f 312 fatal("input_userauth_error: bad message during authentication: "
313 "type %d", type);
314}
315void
316input_userauth_banner(int type, int plen, void *ctxt)
317{
318 char *msg, *lang;
319 debug3("input_userauth_banner");
320 msg = packet_get_string(NULL);
321 lang = packet_get_string(NULL);
322 fprintf(stderr, "%s", msg);
323 xfree(msg);
324 xfree(lang);
188adeb2 325}
326void
327input_userauth_success(int type, int plen, void *ctxt)
328{
329 Authctxt *authctxt = ctxt;
330 if (authctxt == NULL)
331 fatal("input_userauth_success: no authentication context");
fee56204 332 if (authctxt->authlist)
333 xfree(authctxt->authlist);
334 clear_auth_state(authctxt);
188adeb2 335 authctxt->success = 1; /* break out */
336}
337void
338input_userauth_failure(int type, int plen, void *ctxt)
339{
188adeb2 340 Authctxt *authctxt = ctxt;
341 char *authlist = NULL;
342 int partial;
188adeb2 343
344 if (authctxt == NULL)
345 fatal("input_userauth_failure: no authentication context");
346
94ec8c6b 347 authlist = packet_get_string(NULL);
188adeb2 348 partial = packet_get_char();
349 packet_done();
350
351 if (partial != 0)
f72e01a5 352 log("Authenticated with partial success.");
188adeb2 353 debug("authentications that can continue: %s", authlist);
354
fee56204 355 clear_auth_state(authctxt);
356 userauth(authctxt, authlist);
357}
358void
359input_userauth_pk_ok(int type, int plen, void *ctxt)
360{
361 Authctxt *authctxt = ctxt;
362 Key *key = NULL;
363 Buffer b;
ec1f12d3 364 int alen, blen, sent = 0;
22138a36 365 char *pkalg, *pkblob, *fp;
fee56204 366
367 if (authctxt == NULL)
368 fatal("input_userauth_pk_ok: no authentication context");
369 if (datafellows & SSH_BUG_PKOK) {
370 /* this is similar to SSH_BUG_PKAUTH */
371 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
372 pkblob = packet_get_string(&blen);
373 buffer_init(&b);
374 buffer_append(&b, pkblob, blen);
375 pkalg = buffer_get_string(&b, &alen);
376 buffer_free(&b);
377 } else {
378 pkalg = packet_get_string(&alen);
379 pkblob = packet_get_string(&blen);
380 }
381 packet_done();
382
383 debug("input_userauth_pk_ok: pkalg %s blen %d lastkey %p hint %d",
384 pkalg, blen, authctxt->last_key, authctxt->last_key_hint);
385
386 do {
387 if (authctxt->last_key == NULL ||
388 authctxt->last_key_sign == NULL) {
389 debug("no last key or no sign cb");
188adeb2 390 break;
188adeb2 391 }
ec1f12d3 392 if (key_type_from_name(pkalg) == KEY_UNSPEC) {
fee56204 393 debug("unknown pkalg %s", pkalg);
394 break;
395 }
396 if ((key = key_from_blob(pkblob, blen)) == NULL) {
397 debug("no key from blob. pkalg %s", pkalg);
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);
409 } while(0);
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
fa649821 448 if(attempt != 1)
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);
a6215e53 462 packet_inject_ignore(64);
a306f2dd 463 packet_send();
a306f2dd 464 return 1;
465}
466
fee56204 467void
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
2e73a022 480int
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;
a306f2dd 485 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
565int
fee56204 566send_pubkey_test(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback,
567 int hint)
4c8722d9 568{
fee56204 569 u_char *blob;
570 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
598Key *
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,
fa08c86b 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
636int
637identity_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
638 u_char *data, int datalen)
639{
640 Key *private;
641 int idx, ret;
642
643 idx = authctxt->last_key_hint;
644 if (idx < 0)
645 return -1;
646 private = load_identity_file(options.identity_files[idx]);
647 if (private == NULL)
648 return -1;
649 ret = key_sign(private, sigp, lenp, data, datalen);
650 key_free(private);
2e73a022 651 return ret;
652}
653
1e3b8b07 654int agent_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
655 u_char *data, int datalen)
2e73a022 656{
188adeb2 657 return ssh_agent_sign(authctxt->agent, key, sigp, lenp, data, datalen);
2e73a022 658}
659
fee56204 660int key_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
661 u_char *data, int datalen)
662{
cd332296 663 return key_sign(key, sigp, lenp, data, datalen);
fee56204 664}
665
2e73a022 666int
188adeb2 667userauth_pubkey_agent(Authctxt *authctxt)
2e73a022 668{
669 static int called = 0;
fa08c86b 670 int ret = 0;
2e73a022 671 char *comment;
672 Key *k;
2e73a022 673
674 if (called == 0) {
fa08c86b 675 if (ssh_get_num_identities(authctxt->agent, 2) == 0)
676 debug2("userauth_pubkey_agent: no keys at all");
188adeb2 677 called = 1;
2e73a022 678 }
fa08c86b 679 k = ssh_get_next_identity(authctxt->agent, &comment, 2);
188adeb2 680 if (k == NULL) {
fa08c86b 681 debug2("userauth_pubkey_agent: no more keys");
682 } else {
fee56204 683 debug("userauth_pubkey_agent: testing agent key %s", comment);
fa08c86b 684 xfree(comment);
fee56204 685 ret = send_pubkey_test(authctxt, k, agent_sign_cb, -1);
686 if (ret == 0)
687 key_free(k);
188adeb2 688 }
fa08c86b 689 if (ret == 0)
690 debug2("userauth_pubkey_agent: no message sent");
2e73a022 691 return ret;
a306f2dd 692}
693
188adeb2 694int
695userauth_pubkey(Authctxt *authctxt)
a306f2dd 696{
188adeb2 697 static int idx = 0;
698 int sent = 0;
fee56204 699 Key *key;
700 char *filename;
188adeb2 701
fa08c86b 702 if (authctxt->agent != NULL) {
703 do {
704 sent = userauth_pubkey_agent(authctxt);
705 } while(!sent && authctxt->agent->howmany > 0);
706 }
707 while (!sent && idx < options.num_identity_files) {
fee56204 708 key = options.identity_keys[idx];
709 filename = options.identity_files[idx];
710 if (key == NULL) {
711 debug("try privkey: %s", filename);
712 key = load_identity_file(filename);
713 if (key != NULL) {
714 sent = sign_and_send_pubkey(authctxt, key,
715 key_sign_cb);
716 key_free(key);
717 }
718 } else if (key->type != KEY_RSA1) {
719 debug("try pubkey: %s", filename);
720 sent = send_pubkey_test(authctxt, key,
721 identity_sign_cb, idx);
722 }
fa08c86b 723 idx++;
724 }
188adeb2 725 return sent;
726}
a306f2dd 727
94ec8c6b 728/*
729 * Send userauth request message specifying keyboard-interactive method.
730 */
731int
732userauth_kbdint(Authctxt *authctxt)
733{
734 static int attempt = 0;
735
736 if (attempt++ >= options.number_of_password_prompts)
737 return 0;
738
739 debug2("userauth_kbdint");
740 packet_start(SSH2_MSG_USERAUTH_REQUEST);
741 packet_put_cstring(authctxt->server_user);
742 packet_put_cstring(authctxt->service);
743 packet_put_cstring(authctxt->method->name);
744 packet_put_cstring(""); /* lang */
745 packet_put_cstring(options.kbd_interactive_devices ?
746 options.kbd_interactive_devices : "");
747 packet_send();
94ec8c6b 748
749 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
750 return 1;
751}
752
753/*
f72e01a5 754 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
94ec8c6b 755 */
756void
757input_userauth_info_req(int type, int plen, void *ctxt)
758{
759 Authctxt *authctxt = ctxt;
f72e01a5 760 char *name, *inst, *lang, *prompt, *response;
1e3b8b07 761 u_int num_prompts, i;
94ec8c6b 762 int echo = 0;
763
764 debug2("input_userauth_info_req");
765
766 if (authctxt == NULL)
767 fatal("input_userauth_info_req: no authentication context");
768
769 name = packet_get_string(NULL);
770 inst = packet_get_string(NULL);
771 lang = packet_get_string(NULL);
94ec8c6b 772 if (strlen(name) > 0)
773 cli_mesg(name);
94ec8c6b 774 if (strlen(inst) > 0)
775 cli_mesg(inst);
f72e01a5 776 xfree(name);
94ec8c6b 777 xfree(inst);
f72e01a5 778 xfree(lang);
94ec8c6b 779
780 num_prompts = packet_get_int();
781 /*
782 * Begin to build info response packet based on prompts requested.
783 * We commit to providing the correct number of responses, so if
784 * further on we run into a problem that prevents this, we have to
785 * be sure and clean this up and send a correct error response.
786 */
787 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
788 packet_put_int(num_prompts);
789
5ba55ada 790 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
94ec8c6b 791 for (i = 0; i < num_prompts; i++) {
792 prompt = packet_get_string(NULL);
793 echo = packet_get_char();
794
795 response = cli_prompt(prompt, echo);
796
a6215e53 797 packet_put_cstring(response);
94ec8c6b 798 memset(response, 0, strlen(response));
799 xfree(response);
800 xfree(prompt);
801 }
802 packet_done(); /* done with parsing incoming message. */
803
a6215e53 804 packet_inject_ignore(64);
94ec8c6b 805 packet_send();
94ec8c6b 806}
a306f2dd 807
8002af61 808/*
809 * this will be move to an external program (ssh-keysign) ASAP. ssh-keysign
810 * will be setuid-root and the sbit can be removed from /usr/bin/ssh.
811 */
812int
813userauth_hostbased(Authctxt *authctxt)
814{
815 Key *private = NULL;
816 Buffer b;
817 u_char *signature, *blob;
818 char *chost, *pkalg, *p;
8dddf799 819 const char *service;
8002af61 820 u_int blen, slen;
f98c3421 821 int ok, i, len, found = 0;
8002af61 822
823 p = get_local_name(packet_get_connection_in());
824 if (p == NULL) {
825 error("userauth_hostbased: cannot get local ipaddr/name");
826 return 0;
827 }
f98c3421 828 len = strlen(p) + 2;
829 chost = xmalloc(len);
830 strlcpy(chost, p, len);
831 strlcat(chost, ".", len);
8002af61 832 debug2("userauth_hostbased: chost %s", chost);
833 /* check for a useful key */
834 for (i = 0; i < authctxt->nkeys; i++) {
835 private = authctxt->keys[i];
836 if (private && private->type != KEY_RSA1) {
837 found = 1;
838 /* we take and free the key */
839 authctxt->keys[i] = NULL;
840 break;
841 }
842 }
843 if (!found) {
844 xfree(chost);
845 return 0;
846 }
847 if (key_to_blob(private, &blob, &blen) == 0) {
848 key_free(private);
849 xfree(chost);
850 return 0;
851 }
8dddf799 852 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
853 authctxt->service;
8002af61 854 pkalg = xstrdup(key_ssh_name(private));
855 buffer_init(&b);
8002af61 856 /* construct data */
8dddf799 857 buffer_put_string(&b, session_id2, session_id2_len);
8002af61 858 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
859 buffer_put_cstring(&b, authctxt->server_user);
8dddf799 860 buffer_put_cstring(&b, service);
8002af61 861 buffer_put_cstring(&b, authctxt->method->name);
862 buffer_put_cstring(&b, pkalg);
863 buffer_put_string(&b, blob, blen);
864 buffer_put_cstring(&b, chost);
865 buffer_put_cstring(&b, authctxt->local_user);
866#ifdef DEBUG_PK
867 buffer_dump(&b);
868#endif
869 debug2("xxx: chost %s", chost);
870 ok = key_sign(private, &signature, &slen, buffer_ptr(&b), buffer_len(&b));
871 key_free(private);
872 buffer_free(&b);
873 if (ok != 0) {
874 error("key_sign failed");
875 xfree(chost);
876 xfree(pkalg);
877 return 0;
878 }
879 packet_start(SSH2_MSG_USERAUTH_REQUEST);
880 packet_put_cstring(authctxt->server_user);
881 packet_put_cstring(authctxt->service);
882 packet_put_cstring(authctxt->method->name);
883 packet_put_cstring(pkalg);
884 packet_put_string(blob, blen);
885 packet_put_cstring(chost);
886 packet_put_cstring(authctxt->local_user);
887 packet_put_string(signature, slen);
888 memset(signature, 's', slen);
889 xfree(signature);
890 xfree(chost);
891 xfree(pkalg);
892
893 packet_send();
894 return 1;
895}
896
188adeb2 897/* find auth method */
898
188adeb2 899/*
900 * given auth method name, if configurable options permit this method fill
901 * in auth_ident field and return true, otherwise return false.
902 */
903int
904authmethod_is_enabled(Authmethod *method)
905{
906 if (method == NULL)
907 return 0;
908 /* return false if options indicate this method is disabled */
909 if (method->enabled == NULL || *method->enabled == 0)
910 return 0;
911 /* return false if batch mode is enabled but method needs interactive mode */
912 if (method->batch_flag != NULL && *method->batch_flag != 0)
913 return 0;
914 return 1;
915}
a306f2dd 916
188adeb2 917Authmethod *
918authmethod_lookup(const char *name)
919{
920 Authmethod *method = NULL;
921 if (name != NULL)
922 for (method = authmethods; method->name != NULL; method++)
923 if (strcmp(name, method->name) == 0)
924 return method;
925 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
926 return NULL;
927}
928
cab80f75 929/* XXX internal state */
930static Authmethod *current = NULL;
931static char *supported = NULL;
932static char *preferred = NULL;
188adeb2 933/*
934 * Given the authentication method list sent by the server, return the
935 * next method we should try. If the server initially sends a nil list,
cd332296 936 * use a built-in default list.
2b87da3b 937 */
188adeb2 938Authmethod *
939authmethod_get(char *authlist)
940{
cab80f75 941
942 char *name = NULL;
943 int next;
2b87da3b 944
188adeb2 945 /* Use a suitable default if we're passed a nil list. */
946 if (authlist == NULL || strlen(authlist) == 0)
cab80f75 947 authlist = options.preferred_authentications;
948
949 if (supported == NULL || strcmp(authlist, supported) != 0) {
950 debug3("start over, passed a different list %s", authlist);
951 if (supported != NULL)
952 xfree(supported);
953 supported = xstrdup(authlist);
954 preferred = options.preferred_authentications;
955 debug3("preferred %s", preferred);
956 current = NULL;
957 } else if (current != NULL && authmethod_is_enabled(current))
958 return current;
188adeb2 959
cab80f75 960 for (;;) {
961 if ((name = match_list(preferred, supported, &next)) == NULL) {
962 debug("no more auth methods to try");
963 current = NULL;
964 return NULL;
965 }
966 preferred += next;
94ec8c6b 967 debug3("authmethod_lookup %s", name);
cab80f75 968 debug3("remaining preferred: %s", preferred);
969 if ((current = authmethod_lookup(name)) != NULL &&
970 authmethod_is_enabled(current)) {
94ec8c6b 971 debug3("authmethod_is_enabled %s", name);
cab80f75 972 debug("next auth method to try is %s", name);
973 return current;
94ec8c6b 974 }
a306f2dd 975 }
cab80f75 976}
a22aff1f 977
a22aff1f 978
cab80f75 979#define DELIM ","
980char *
981authmethods_get(void)
982{
983 Authmethod *method = NULL;
984 char buf[1024];
985
986 buf[0] = '\0';
987 for (method = authmethods; method->name != NULL; method++) {
988 if (authmethod_is_enabled(method)) {
989 if (buf[0] != '\0')
990 strlcat(buf, DELIM, sizeof buf);
991 strlcat(buf, method->name, sizeof buf);
992 }
993 }
994 return xstrdup(buf);
a306f2dd 995}
This page took 2.463489 seconds and 5 git commands to generate.