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