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