]> andersk Git - openssh.git/blame - sshconnect2.c
- dtucker@cvs.openbsd.org 2003/10/07 01:47:27
[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"
19e633e7 26RCSID("$OpenBSD: sshconnect2.c,v 1.125 2003/10/07 01:47:27 dtucker Exp $");
a306f2dd 27
751092f9 28#include "openbsd-compat/sys-queue.h"
29
a306f2dd 30#include "ssh.h"
42f11eb2 31#include "ssh2.h"
a306f2dd 32#include "xmalloc.h"
a306f2dd 33#include "buffer.h"
34#include "packet.h"
a306f2dd 35#include "compat.h"
a306f2dd 36#include "bufaux.h"
42f11eb2 37#include "cipher.h"
a306f2dd 38#include "kex.h"
39#include "myproposal.h"
a306f2dd 40#include "sshconnect.h"
41#include "authfile.h"
db1cd2f3 42#include "dh.h"
2e73a022 43#include "authfd.h"
42f11eb2 44#include "log.h"
45#include "readconf.h"
46#include "readpass.h"
cab80f75 47#include "match.h"
a5c9ffdb 48#include "dispatch.h"
8002af61 49#include "canohost.h"
39c00dc2 50#include "msg.h"
51#include "pathnames.h"
94ec8c6b 52
7364bd04 53#ifdef GSSAPI
54#include "ssh-gss.h"
55#endif
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;
a27002e5 67u_int session_id2_len = 0;
a306f2dd 68
a5c9ffdb 69char *xxx_host;
70struct sockaddr *xxx_hostaddr;
71
e092ce67 72Kex *xxx_kex = NULL;
73
396c147e 74static int
f49bc4f7 75verify_host_key_callback(Key *hostkey)
a5c9ffdb 76{
f49bc4f7 77 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
01e9ef57 78 fatal("Host key verification failed.");
a5c9ffdb 79 return 0;
80}
81
a306f2dd 82void
94ec8c6b 83ssh_kex2(char *host, struct sockaddr *hostaddr)
84{
94ec8c6b 85 Kex *kex;
a5c9ffdb 86
87 xxx_host = host;
88 xxx_hostaddr = hostaddr;
94ec8c6b 89
c523303b 90 if (options.ciphers == (char *)-1) {
bbe88b6d 91 logit("No valid ciphers for protocol version 2 given, using defaults.");
c523303b 92 options.ciphers = NULL;
94ec8c6b 93 }
94 if (options.ciphers != NULL) {
95 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
96 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
97 }
1ad64a93 98 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
99 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
100 myproposal[PROPOSAL_ENC_ALGS_STOC] =
101 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
94ec8c6b 102 if (options.compression) {
b2552997 103 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
713f6cd9 104 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib,none";
94ec8c6b 105 } else {
b2552997 106 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
713f6cd9 107 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib";
94ec8c6b 108 }
b2552997 109 if (options.macs != NULL) {
110 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
111 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
112 }
e961a8f9 113 if (options.hostkeyalgorithms != NULL)
184eed6a 114 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
e961a8f9 115 options.hostkeyalgorithms;
94ec8c6b 116
ffd7b36b 117 if (options.rekey_limit)
118 packet_set_rekey_limit(options.rekey_limit);
119
7a37c112 120 /* start key exchange */
d8ee838b 121 kex = kex_setup(myproposal);
98a58eda 122 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
123 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
a5c9ffdb 124 kex->client_version_string=client_version_string;
125 kex->server_version_string=server_version_string;
f49bc4f7 126 kex->verify_host_key=&verify_host_key_callback;
94ec8c6b 127
e092ce67 128 xxx_kex = kex;
129
c422989b 130 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
94ec8c6b 131
d1ac6175 132 session_id2 = kex->session_id;
133 session_id2_len = kex->session_id_len;
134
94ec8c6b 135#ifdef DEBUG_KEXDH
136 /* send 1st encrypted/maced/compressed message */
137 packet_start(SSH2_MSG_IGNORE);
138 packet_put_cstring("markus");
139 packet_send();
140 packet_write_wait();
141#endif
a306f2dd 142}
71276795 143
a306f2dd 144/*
145 * Authenticate user
146 */
188adeb2 147
148typedef struct Authctxt Authctxt;
149typedef struct Authmethod Authmethod;
43348518 150typedef struct identity Identity;
151typedef struct idlist Idlist;
152
153struct identity {
154 TAILQ_ENTRY(identity) next;
155 AuthenticationConnection *ac; /* set if agent supports key */
156 Key *key; /* public/private key */
157 char *filename; /* comment for agent-only keys */
158 int tried;
159 int isprivate; /* key points to the private key */
160};
161TAILQ_HEAD(idlist, identity);
188adeb2 162
163struct Authctxt {
164 const char *server_user;
8002af61 165 const char *local_user;
188adeb2 166 const char *host;
167 const char *service;
188adeb2 168 Authmethod *method;
94ec8c6b 169 int success;
fee56204 170 char *authlist;
8002af61 171 /* pubkey */
43348518 172 Idlist keys;
8002af61 173 AuthenticationConnection *agent;
174 /* hostbased */
39c00dc2 175 Sensitive *sensitive;
6b759005 176 /* kbd-interactive */
177 int info_req_seen;
7364bd04 178 /* generic */
179 void *methoddata;
188adeb2 180};
181struct Authmethod {
182 char *name; /* string to compare against server's list */
183 int (*userauth)(Authctxt *authctxt);
184 int *enabled; /* flag in option struct that enables method */
185 int *batch_flag; /* flag in option struct that disables method */
186};
187
7819b5c3 188void input_userauth_success(int, u_int32_t, void *);
189void input_userauth_failure(int, u_int32_t, void *);
190void input_userauth_banner(int, u_int32_t, void *);
191void input_userauth_error(int, u_int32_t, void *);
192void input_userauth_info_req(int, u_int32_t, void *);
193void input_userauth_pk_ok(int, u_int32_t, void *);
67ec167d 194void input_userauth_passwd_changereq(int, u_int32_t, void *);
94ec8c6b 195
d5bb9418 196int userauth_none(Authctxt *);
197int userauth_pubkey(Authctxt *);
198int userauth_passwd(Authctxt *);
199int userauth_kbdint(Authctxt *);
200int userauth_hostbased(Authctxt *);
802e01b8 201int userauth_kerberos(Authctxt *);
188adeb2 202
7364bd04 203#ifdef GSSAPI
204int userauth_gssapi(Authctxt *authctxt);
205void input_gssapi_response(int type, u_int32_t, void *);
206void input_gssapi_token(int type, u_int32_t, void *);
207void input_gssapi_hash(int type, u_int32_t, void *);
208void input_gssapi_error(int, u_int32_t, void *);
209void input_gssapi_errtok(int, u_int32_t, void *);
210#endif
211
d5bb9418 212void userauth(Authctxt *, char *);
fee56204 213
43348518 214static int sign_and_send_pubkey(Authctxt *, Identity *);
43348518 215static void pubkey_prepare(Authctxt *);
216static void pubkey_cleanup(Authctxt *);
217static Key *load_identity_file(char *);
fee56204 218
396c147e 219static Authmethod *authmethod_get(char *authlist);
220static Authmethod *authmethod_lookup(const char *name);
221static char *authmethods_get(void);
188adeb2 222
223Authmethod authmethods[] = {
7364bd04 224#ifdef GSSAPI
225 {"gssapi",
226 userauth_gssapi,
227 &options.gss_authentication,
228 NULL},
229#endif
3398dda9 230 {"hostbased",
231 userauth_hostbased,
232 &options.hostbased_authentication,
233 NULL},
9f37c0af 234 {"publickey",
235 userauth_pubkey,
236 &options.pubkey_authentication,
237 NULL},
94ec8c6b 238 {"keyboard-interactive",
239 userauth_kbdint,
240 &options.kbd_interactive_authentication,
241 &options.batch_mode},
9f37c0af 242 {"password",
243 userauth_passwd,
244 &options.password_authentication,
245 &options.batch_mode},
94ec8c6b 246 {"none",
247 userauth_none,
248 NULL,
249 NULL},
188adeb2 250 {NULL, NULL, NULL, NULL}
251};
252
253void
8002af61 254ssh_userauth2(const char *local_user, const char *server_user, char *host,
39c00dc2 255 Sensitive *sensitive)
188adeb2 256{
257 Authctxt authctxt;
258 int type;
188adeb2 259
5ba55ada 260 if (options.challenge_response_authentication)
d464095c 261 options.kbd_interactive_authentication = 1;
262
188adeb2 263 packet_start(SSH2_MSG_SERVICE_REQUEST);
264 packet_put_cstring("ssh-userauth");
265 packet_send();
a77673cc 266 debug("SSH2_MSG_SERVICE_REQUEST sent");
188adeb2 267 packet_write_wait();
54a5250f 268 type = packet_read();
a77673cc 269 if (type != SSH2_MSG_SERVICE_ACCEPT)
270 fatal("Server denied authentication request: %d", type);
188adeb2 271 if (packet_remaining() > 0) {
54a5250f 272 char *reply = packet_get_string(NULL);
a77673cc 273 debug2("service_accept: %s", reply);
188adeb2 274 xfree(reply);
188adeb2 275 } else {
6226a8f8 276 debug2("buggy server: service_accept w/o service");
188adeb2 277 }
95500969 278 packet_check_eom();
a77673cc 279 debug("SSH2_MSG_SERVICE_ACCEPT received");
188adeb2 280
cab80f75 281 if (options.preferred_authentications == NULL)
282 options.preferred_authentications = authmethods_get();
283
188adeb2 284 /* setup authentication context */
6b759005 285 memset(&authctxt, 0, sizeof(authctxt));
43348518 286 pubkey_prepare(&authctxt);
188adeb2 287 authctxt.server_user = server_user;
8002af61 288 authctxt.local_user = local_user;
188adeb2 289 authctxt.host = host;
290 authctxt.service = "ssh-connection"; /* service name */
291 authctxt.success = 0;
94ec8c6b 292 authctxt.method = authmethod_lookup("none");
fee56204 293 authctxt.authlist = NULL;
7364bd04 294 authctxt.methoddata = NULL;
39c00dc2 295 authctxt.sensitive = sensitive;
6b759005 296 authctxt.info_req_seen = 0;
94ec8c6b 297 if (authctxt.method == NULL)
298 fatal("ssh_userauth2: internal error: cannot send userauth none request");
188adeb2 299
300 /* initial userauth request */
94ec8c6b 301 userauth_none(&authctxt);
188adeb2 302
7a37c112 303 dispatch_init(&input_userauth_error);
188adeb2 304 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
305 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
9616313f 306 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
188adeb2 307 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
308
43348518 309 pubkey_cleanup(&authctxt);
b8c2031b 310 dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
311
6226a8f8 312 debug("Authentication succeeded (%s).", authctxt.method->name);
188adeb2 313}
b8c2031b 314
188adeb2 315void
fee56204 316userauth(Authctxt *authctxt, char *authlist)
317{
7364bd04 318 if (authctxt->methoddata) {
319 xfree(authctxt->methoddata);
320 authctxt->methoddata = NULL;
321 }
fee56204 322 if (authlist == NULL) {
323 authlist = authctxt->authlist;
324 } else {
325 if (authctxt->authlist)
326 xfree(authctxt->authlist);
327 authctxt->authlist = authlist;
328 }
329 for (;;) {
330 Authmethod *method = authmethod_get(authlist);
331 if (method == NULL)
332 fatal("Permission denied (%s).", authlist);
333 authctxt->method = method;
b8c2031b 334
335 /* reset the per method handler */
336 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN,
337 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
338
339 /* and try new method */
fee56204 340 if (method->userauth(authctxt) != 0) {
341 debug2("we sent a %s packet, wait for reply", method->name);
342 break;
343 } else {
344 debug2("we did not send a packet, disable method");
345 method->enabled = NULL;
346 }
347 }
348}
d6133f43 349
fee56204 350void
7819b5c3 351input_userauth_error(int type, u_int32_t seq, void *ctxt)
188adeb2 352{
9616313f 353 fatal("input_userauth_error: bad message during authentication: "
354 "type %d", type);
355}
d6133f43 356
9616313f 357void
7819b5c3 358input_userauth_banner(int type, u_int32_t seq, void *ctxt)
9616313f 359{
360 char *msg, *lang;
361 debug3("input_userauth_banner");
362 msg = packet_get_string(NULL);
363 lang = packet_get_string(NULL);
19e633e7 364 if (options.log_level > SYSLOG_LEVEL_QUIET)
365 fprintf(stderr, "%s", msg);
9616313f 366 xfree(msg);
367 xfree(lang);
188adeb2 368}
d6133f43 369
188adeb2 370void
7819b5c3 371input_userauth_success(int type, u_int32_t seq, void *ctxt)
188adeb2 372{
373 Authctxt *authctxt = ctxt;
374 if (authctxt == NULL)
375 fatal("input_userauth_success: no authentication context");
fee56204 376 if (authctxt->authlist)
377 xfree(authctxt->authlist);
7364bd04 378 if (authctxt->methoddata)
379 xfree(authctxt->methoddata);
188adeb2 380 authctxt->success = 1; /* break out */
381}
d6133f43 382
188adeb2 383void
7819b5c3 384input_userauth_failure(int type, u_int32_t seq, void *ctxt)
188adeb2 385{
188adeb2 386 Authctxt *authctxt = ctxt;
387 char *authlist = NULL;
388 int partial;
188adeb2 389
390 if (authctxt == NULL)
391 fatal("input_userauth_failure: no authentication context");
392
94ec8c6b 393 authlist = packet_get_string(NULL);
188adeb2 394 partial = packet_get_char();
95500969 395 packet_check_eom();
188adeb2 396
397 if (partial != 0)
bbe88b6d 398 logit("Authenticated with partial success.");
6226a8f8 399 debug("Authentications that can continue: %s", authlist);
188adeb2 400
fee56204 401 userauth(authctxt, authlist);
402}
403void
7819b5c3 404input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
fee56204 405{
406 Authctxt *authctxt = ctxt;
407 Key *key = NULL;
43348518 408 Identity *id = NULL;
fee56204 409 Buffer b;
c66f9d0e 410 int pktype, sent = 0;
411 u_int alen, blen;
412 char *pkalg, *fp;
413 u_char *pkblob;
fee56204 414
415 if (authctxt == NULL)
416 fatal("input_userauth_pk_ok: no authentication context");
417 if (datafellows & SSH_BUG_PKOK) {
418 /* this is similar to SSH_BUG_PKAUTH */
419 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
420 pkblob = packet_get_string(&blen);
421 buffer_init(&b);
422 buffer_append(&b, pkblob, blen);
423 pkalg = buffer_get_string(&b, &alen);
424 buffer_free(&b);
425 } else {
426 pkalg = packet_get_string(&alen);
427 pkblob = packet_get_string(&blen);
428 }
95500969 429 packet_check_eom();
fee56204 430
43348518 431 debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
fee56204 432
43348518 433 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
434 debug("unknown pkalg %s", pkalg);
435 goto done;
436 }
437 if ((key = key_from_blob(pkblob, blen)) == NULL) {
438 debug("no key from blob. pkalg %s", pkalg);
439 goto done;
440 }
441 if (key->type != pktype) {
442 error("input_userauth_pk_ok: type mismatch "
443 "for decoded key (received %d, expected %d)",
444 key->type, pktype);
445 goto done;
446 }
447 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
448 debug2("input_userauth_pk_ok: fp %s", fp);
449 xfree(fp);
450
451 TAILQ_FOREACH(id, &authctxt->keys, next) {
452 if (key_equal(key, id->key)) {
453 sent = sign_and_send_pubkey(authctxt, id);
fee56204 454 break;
455 }
43348518 456 }
457done:
fee56204 458 if (key != NULL)
459 key_free(key);
460 xfree(pkalg);
461 xfree(pkblob);
462
343288b8 463 /* try another method if we did not send a packet */
fee56204 464 if (sent == 0)
465 userauth(authctxt, NULL);
188adeb2 466}
467
7364bd04 468#ifdef GSSAPI
469int
470userauth_gssapi(Authctxt *authctxt)
471{
472 Gssctxt *gssctxt = NULL;
473 static gss_OID_set supported = NULL;
474 static int mech = 0;
475 OM_uint32 min;
476 int ok = 0;
477
478 /* Try one GSSAPI method at a time, rather than sending them all at
479 * once. */
480
481 if (supported == NULL)
482 gss_indicate_mechs(&min, &supported);
483
484 /* Check to see if the mechanism is usable before we offer it */
485 while (mech<supported->count && !ok) {
486 if (gssctxt)
487 ssh_gssapi_delete_ctx(&gssctxt);
488 ssh_gssapi_build_ctx(&gssctxt);
489 ssh_gssapi_set_oid(gssctxt, &supported->elements[mech]);
490
491 /* My DER encoding requires length<128 */
492 if (supported->elements[mech].length < 128 &&
493 !GSS_ERROR(ssh_gssapi_import_name(gssctxt,
494 authctxt->host))) {
495 ok = 1; /* Mechanism works */
496 } else {
497 mech++;
498 }
499 }
500
501 if (!ok) return 0;
502
503 authctxt->methoddata=(void *)gssctxt;
504
505 packet_start(SSH2_MSG_USERAUTH_REQUEST);
506 packet_put_cstring(authctxt->server_user);
507 packet_put_cstring(authctxt->service);
508 packet_put_cstring(authctxt->method->name);
509
510 packet_put_int(1);
511
512 /* Some servers encode the OID incorrectly (as we used to) */
513 if (datafellows & SSH_BUG_GSSAPI_BER) {
514 packet_put_string(supported->elements[mech].elements,
515 supported->elements[mech].length);
516 } else {
517 packet_put_int((supported->elements[mech].length)+2);
518 packet_put_char(SSH_GSS_OIDTYPE);
519 packet_put_char(supported->elements[mech].length);
520 packet_put_raw(supported->elements[mech].elements,
521 supported->elements[mech].length);
522 }
523
524 packet_send();
525
526 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
527 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
528 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
529 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
530
531 mech++; /* Move along to next candidate */
532
533 return 1;
534}
535
536void
537input_gssapi_response(int type, u_int32_t plen, void *ctxt)
538{
539 Authctxt *authctxt = ctxt;
540 Gssctxt *gssctxt;
541 OM_uint32 status, ms;
542 int oidlen;
543 char *oidv;
544 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
545
546 if (authctxt == NULL)
547 fatal("input_gssapi_response: no authentication context");
548 gssctxt = authctxt->methoddata;
549
550 /* Setup our OID */
551 oidv = packet_get_string(&oidlen);
552
553 if (datafellows & SSH_BUG_GSSAPI_BER) {
554 if (!ssh_gssapi_check_oid(gssctxt, oidv, oidlen))
555 fatal("Server returned different OID than expected");
556 } else {
557 if(oidv[0] != SSH_GSS_OIDTYPE || oidv[1] != oidlen-2) {
558 debug("Badly encoded mechanism OID received");
559 userauth(authctxt, NULL);
560 xfree(oidv);
561 return;
562 }
563 if (!ssh_gssapi_check_oid(gssctxt, oidv+2, oidlen-2))
564 fatal("Server returned different OID than expected");
565 }
566
567 packet_check_eom();
568
569 xfree(oidv);
570
571 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
572 GSS_C_NO_BUFFER, &send_tok, NULL);
573 if (GSS_ERROR(status)) {
574 if (send_tok.length > 0) {
575 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
576 packet_put_string(send_tok.value, send_tok.length);
577 packet_send();
578 gss_release_buffer(&ms, &send_tok);
579 }
580 /* Start again with next method on list */
581 debug("Trying to start again");
582 userauth(authctxt, NULL);
583 return;
584 }
585
586 /* We must have data to send */
587 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
588 packet_put_string(send_tok.value, send_tok.length);
589 packet_send();
590 gss_release_buffer(&ms, &send_tok);
591}
592
593void
594input_gssapi_token(int type, u_int32_t plen, void *ctxt)
595{
596 Authctxt *authctxt = ctxt;
597 Gssctxt *gssctxt;
598 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
599 gss_buffer_desc recv_tok;
600 OM_uint32 status, ms;
601 u_int slen;
602
603 if (authctxt == NULL)
604 fatal("input_gssapi_response: no authentication context");
605 gssctxt = authctxt->methoddata;
606
607 recv_tok.value = packet_get_string(&slen);
608 recv_tok.length = slen; /* safe typecast */
609
610 packet_check_eom();
611
612 status=ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
613 &recv_tok, &send_tok, NULL);
614
615 xfree(recv_tok.value);
616
617 if (GSS_ERROR(status)) {
618 if (send_tok.length > 0) {
619 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
620 packet_put_string(send_tok.value, send_tok.length);
621 packet_send();
622 gss_release_buffer(&ms, &send_tok);
623 }
624 /* Start again with the next method in the list */
625 userauth(authctxt, NULL);
626 return;
627 }
628
629 if (send_tok.length > 0) {
630 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
631 packet_put_string(send_tok.value, send_tok.length);
632 packet_send();
633 gss_release_buffer(&ms, &send_tok);
634 }
635
636 if (status == GSS_S_COMPLETE) {
637 /* If that succeeded, send a exchange complete message */
638 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
639 packet_send();
640 }
641}
642
643void
644input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
645{
646 Authctxt *authctxt = ctxt;
647 Gssctxt *gssctxt;
648 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
649 gss_buffer_desc recv_tok;
650 OM_uint32 status, ms;
f99e1ca4 651 u_int len;
7364bd04 652
653 if (authctxt == NULL)
654 fatal("input_gssapi_response: no authentication context");
655 gssctxt = authctxt->methoddata;
656
f99e1ca4 657 recv_tok.value = packet_get_string(&len);
658 recv_tok.length = len;
7364bd04 659
660 packet_check_eom();
661
662 /* Stick it into GSSAPI and see what it says */
663 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
664 &recv_tok, &send_tok, NULL);
665
666 xfree(recv_tok.value);
667 gss_release_buffer(&ms, &send_tok);
668
669 /* Server will be returning a failed packet after this one */
670}
671
672void
673input_gssapi_error(int type, u_int32_t plen, void *ctxt)
674{
675 OM_uint32 maj, min;
676 char *msg;
677 char *lang;
678
679 maj=packet_get_int();
680 min=packet_get_int();
681 msg=packet_get_string(NULL);
682 lang=packet_get_string(NULL);
683
684 packet_check_eom();
685
686 debug("Server GSSAPI Error:\n%s\n", msg);
687 xfree(msg);
688 xfree(lang);
689}
690#endif /* GSSAPI */
691
94ec8c6b 692int
693userauth_none(Authctxt *authctxt)
694{
695 /* initial userauth request */
696 packet_start(SSH2_MSG_USERAUTH_REQUEST);
697 packet_put_cstring(authctxt->server_user);
698 packet_put_cstring(authctxt->service);
699 packet_put_cstring(authctxt->method->name);
700 packet_send();
94ec8c6b 701 return 1;
702}
703
a306f2dd 704int
188adeb2 705userauth_passwd(Authctxt *authctxt)
a306f2dd 706{
1d1ffb87 707 static int attempt = 0;
67ec167d 708 char prompt[150];
a306f2dd 709 char *password;
710
fa649821 711 if (attempt++ >= options.number_of_password_prompts)
1d1ffb87 712 return 0;
713
6aacefa7 714 if (attempt != 1)
fa649821 715 error("Permission denied, please try again.");
716
f72e01a5 717 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
188adeb2 718 authctxt->server_user, authctxt->host);
a306f2dd 719 password = read_passphrase(prompt, 0);
720 packet_start(SSH2_MSG_USERAUTH_REQUEST);
188adeb2 721 packet_put_cstring(authctxt->server_user);
722 packet_put_cstring(authctxt->service);
94ec8c6b 723 packet_put_cstring(authctxt->method->name);
a306f2dd 724 packet_put_char(0);
a6215e53 725 packet_put_cstring(password);
a306f2dd 726 memset(password, 0, strlen(password));
727 xfree(password);
b4b701be 728 packet_add_padding(64);
a306f2dd 729 packet_send();
67ec167d 730
7203d6bb 731 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
67ec167d 732 &input_userauth_passwd_changereq);
733
a306f2dd 734 return 1;
735}
67ec167d 736/*
737 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
738 */
739void
ef077e37 740input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
67ec167d 741{
742 Authctxt *authctxt = ctxt;
743 char *info, *lang, *password = NULL, *retype = NULL;
744 char prompt[150];
745
746 debug2("input_userauth_passwd_changereq");
747
748 if (authctxt == NULL)
749 fatal("input_userauth_passwd_changereq: "
750 "no authentication context");
751
752 info = packet_get_string(NULL);
753 lang = packet_get_string(NULL);
754 if (strlen(info) > 0)
bbe88b6d 755 logit("%s", info);
67ec167d 756 xfree(info);
757 xfree(lang);
758 packet_start(SSH2_MSG_USERAUTH_REQUEST);
759 packet_put_cstring(authctxt->server_user);
760 packet_put_cstring(authctxt->service);
761 packet_put_cstring(authctxt->method->name);
762 packet_put_char(1); /* additional info */
7203d6bb 763 snprintf(prompt, sizeof(prompt),
67ec167d 764 "Enter %.30s@%.128s's old password: ",
765 authctxt->server_user, authctxt->host);
766 password = read_passphrase(prompt, 0);
767 packet_put_cstring(password);
768 memset(password, 0, strlen(password));
769 xfree(password);
770 password = NULL;
771 while (password == NULL) {
7203d6bb 772 snprintf(prompt, sizeof(prompt),
67ec167d 773 "Enter %.30s@%.128s's new password: ",
774 authctxt->server_user, authctxt->host);
775 password = read_passphrase(prompt, RP_ALLOW_EOF);
776 if (password == NULL) {
777 /* bail out */
778 return;
779 }
7203d6bb 780 snprintf(prompt, sizeof(prompt),
67ec167d 781 "Retype %.30s@%.128s's new password: ",
782 authctxt->server_user, authctxt->host);
783 retype = read_passphrase(prompt, 0);
784 if (strcmp(password, retype) != 0) {
785 memset(password, 0, strlen(password));
786 xfree(password);
bbe88b6d 787 logit("Mismatch; try again, EOF to quit.");
67ec167d 788 password = NULL;
789 }
790 memset(retype, 0, strlen(retype));
791 xfree(retype);
792 }
793 packet_put_cstring(password);
794 memset(password, 0, strlen(password));
795 xfree(password);
796 packet_add_padding(64);
797 packet_send();
7203d6bb 798
799 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
67ec167d 800 &input_userauth_passwd_changereq);
801}
a306f2dd 802
43348518 803static int
804identity_sign(Identity *id, u_char **sigp, u_int *lenp,
805 u_char *data, u_int datalen)
806{
807 Key *prv;
808 int ret;
809
810 /* the agent supports this key */
811 if (id->ac)
812 return (ssh_agent_sign(id->ac, id->key, sigp, lenp,
813 data, datalen));
814 /*
815 * we have already loaded the private key or
816 * the private key is stored in external hardware
817 */
818 if (id->isprivate || (id->key->flags & KEY_FLAG_EXT))
819 return (key_sign(id->key, sigp, lenp, data, datalen));
820 /* load the private key from the file */
821 if ((prv = load_identity_file(id->filename)) == NULL)
822 return (-1);
823 ret = key_sign(prv, sigp, lenp, data, datalen);
824 key_free(prv);
825 return (ret);
fee56204 826}
827
396c147e 828static int
43348518 829sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
a306f2dd 830{
831 Buffer b;
1e3b8b07 832 u_char *blob, *signature;
c66f9d0e 833 u_int bloblen, slen;
a27002e5 834 u_int skip = 0;
2e73a022 835 int ret = -1;
94ec8c6b 836 int have_sig = 1;
a306f2dd 837
cbc5abf9 838 debug3("sign_and_send_pubkey");
fee56204 839
43348518 840 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
fa08c86b 841 /* we cannot handle this key */
cbc5abf9 842 debug3("sign_and_send_pubkey: cannot handle key");
fa08c86b 843 return 0;
844 }
a306f2dd 845 /* data to be signed */
846 buffer_init(&b);
33de75a3 847 if (datafellows & SSH_OLD_SESSIONID) {
74fc9186 848 buffer_append(&b, session_id2, session_id2_len);
2b87da3b 849 skip = session_id2_len;
33de75a3 850 } else {
851 buffer_put_string(&b, session_id2, session_id2_len);
852 skip = buffer_len(&b);
74fc9186 853 }
a306f2dd 854 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
188adeb2 855 buffer_put_cstring(&b, authctxt->server_user);
d0c832f3 856 buffer_put_cstring(&b,
cbc5abf9 857 datafellows & SSH_BUG_PKSERVICE ?
d0c832f3 858 "ssh-userauth" :
188adeb2 859 authctxt->service);
cbc5abf9 860 if (datafellows & SSH_BUG_PKAUTH) {
861 buffer_put_char(&b, have_sig);
862 } else {
863 buffer_put_cstring(&b, authctxt->method->name);
864 buffer_put_char(&b, have_sig);
43348518 865 buffer_put_cstring(&b, key_ssh_name(id->key));
cbc5abf9 866 }
a306f2dd 867 buffer_put_string(&b, blob, bloblen);
a306f2dd 868
869 /* generate signature */
43348518 870 ret = identity_sign(id, &signature, &slen,
fee56204 871 buffer_ptr(&b), buffer_len(&b));
2e73a022 872 if (ret == -1) {
873 xfree(blob);
874 buffer_free(&b);
875 return 0;
876 }
fa08c86b 877#ifdef DEBUG_PK
a306f2dd 878 buffer_dump(&b);
879#endif
cbc5abf9 880 if (datafellows & SSH_BUG_PKSERVICE) {
d0c832f3 881 buffer_clear(&b);
882 buffer_append(&b, session_id2, session_id2_len);
fee56204 883 skip = session_id2_len;
d0c832f3 884 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
188adeb2 885 buffer_put_cstring(&b, authctxt->server_user);
886 buffer_put_cstring(&b, authctxt->service);
94ec8c6b 887 buffer_put_cstring(&b, authctxt->method->name);
888 buffer_put_char(&b, have_sig);
cbc5abf9 889 if (!(datafellows & SSH_BUG_PKAUTH))
43348518 890 buffer_put_cstring(&b, key_ssh_name(id->key));
d0c832f3 891 buffer_put_string(&b, blob, bloblen);
892 }
893 xfree(blob);
fee56204 894
a306f2dd 895 /* append signature */
896 buffer_put_string(&b, signature, slen);
897 xfree(signature);
898
899 /* skip session id and packet type */
74fc9186 900 if (buffer_len(&b) < skip + 1)
188adeb2 901 fatal("userauth_pubkey: internal error");
74fc9186 902 buffer_consume(&b, skip + 1);
a306f2dd 903
904 /* put remaining data from buffer into packet */
905 packet_start(SSH2_MSG_USERAUTH_REQUEST);
906 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
907 buffer_free(&b);
a306f2dd 908 packet_send();
2e73a022 909
910 return 1;
4c8722d9 911}
912
396c147e 913static int
43348518 914send_pubkey_test(Authctxt *authctxt, Identity *id)
4c8722d9 915{
fee56204 916 u_char *blob;
21b30f38 917 u_int bloblen, have_sig = 0;
4c8722d9 918
fee56204 919 debug3("send_pubkey_test");
920
43348518 921 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
fee56204 922 /* we cannot handle this key */
923 debug3("send_pubkey_test: cannot handle key");
4c8722d9 924 return 0;
925 }
fee56204 926 /* register callback for USERAUTH_PK_OK message */
fee56204 927 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
4c8722d9 928
fee56204 929 packet_start(SSH2_MSG_USERAUTH_REQUEST);
930 packet_put_cstring(authctxt->server_user);
931 packet_put_cstring(authctxt->service);
932 packet_put_cstring(authctxt->method->name);
933 packet_put_char(have_sig);
934 if (!(datafellows & SSH_BUG_PKAUTH))
43348518 935 packet_put_cstring(key_ssh_name(id->key));
fee56204 936 packet_put_string(blob, bloblen);
937 xfree(blob);
938 packet_send();
939 return 1;
940}
941
396c147e 942static Key *
fee56204 943load_identity_file(char *filename)
944{
945 Key *private;
946 char prompt[300], *passphrase;
aeaa3d9e 947 int quit, i;
d156519a 948 struct stat st;
fee56204 949
d156519a 950 if (stat(filename, &st) < 0) {
951 debug3("no such identity: %s", filename);
952 return NULL;
953 }
aeaa3d9e 954 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL);
955 if (private == NULL) {
956 if (options.batch_mode)
fee56204 957 return NULL;
4c8722d9 958 snprintf(prompt, sizeof prompt,
184eed6a 959 "Enter passphrase for key '%.100s': ", filename);
188adeb2 960 for (i = 0; i < options.number_of_password_prompts; i++) {
961 passphrase = read_passphrase(prompt, 0);
962 if (strcmp(passphrase, "") != 0) {
aeaa3d9e 963 private = key_load_private_type(KEY_UNSPEC, filename,
964 passphrase, NULL);
fee56204 965 quit = 0;
188adeb2 966 } else {
967 debug2("no passphrase given, try next key");
fee56204 968 quit = 1;
188adeb2 969 }
970 memset(passphrase, 0, strlen(passphrase));
971 xfree(passphrase);
aeaa3d9e 972 if (private != NULL || quit)
188adeb2 973 break;
974 debug2("bad passphrase given, try again...");
975 }
4c8722d9 976 }
fee56204 977 return private;
978}
979
43348518 980/*
981 * try keys in the following order:
982 * 1. agent keys that are found in the config file
983 * 2. other agent keys
984 * 3. keys that are only listed in the config file
985 */
986static void
987pubkey_prepare(Authctxt *authctxt)
fee56204 988{
43348518 989 Identity *id;
990 Idlist agent, files, *preferred;
991 Key *key;
992 AuthenticationConnection *ac;
993 char *comment;
994 int i, found;
995
996 TAILQ_INIT(&agent); /* keys from the agent */
997 TAILQ_INIT(&files); /* keys from the config file */
998 preferred = &authctxt->keys;
999 TAILQ_INIT(preferred); /* preferred order of keys */
1000
1001 /* list of keys stored in the filesystem */
1002 for (i = 0; i < options.num_identity_files; i++) {
1003 key = options.identity_keys[i];
1004 if (key && key->type == KEY_RSA1)
1005 continue;
1006 options.identity_keys[i] = NULL;
1007 id = xmalloc(sizeof(*id));
1008 memset(id, 0, sizeof(*id));
1009 id->key = key;
1010 id->filename = xstrdup(options.identity_files[i]);
1011 TAILQ_INSERT_TAIL(&files, id, next);
1012 }
1013 /* list of keys supported by the agent */
1014 if ((ac = ssh_get_authentication_connection())) {
1015 for (key = ssh_get_first_identity(ac, &comment, 2);
1016 key != NULL;
1017 key = ssh_get_next_identity(ac, &comment, 2)) {
1018 found = 0;
1019 TAILQ_FOREACH(id, &files, next) {
1020 /* agent keys from the config file are preferred */
1021 if (key_equal(key, id->key)) {
1022 key_free(key);
1023 xfree(comment);
1024 TAILQ_REMOVE(&files, id, next);
1025 TAILQ_INSERT_TAIL(preferred, id, next);
1026 id->ac = ac;
1027 found = 1;
1028 break;
1029 }
1030 }
1031 if (!found) {
1032 id = xmalloc(sizeof(*id));
1033 memset(id, 0, sizeof(*id));
1034 id->key = key;
1035 id->filename = comment;
1036 id->ac = ac;
1037 TAILQ_INSERT_TAIL(&agent, id, next);
1038 }
1039 }
1040 /* append remaining agent keys */
1041 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1042 TAILQ_REMOVE(&agent, id, next);
1043 TAILQ_INSERT_TAIL(preferred, id, next);
1044 }
1045 authctxt->agent = ac;
1046 }
1047 /* append remaining keys from the config file */
1048 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1049 TAILQ_REMOVE(&files, id, next);
1050 TAILQ_INSERT_TAIL(preferred, id, next);
1051 }
1052 TAILQ_FOREACH(id, preferred, next) {
1053 debug2("key: %s (%p)", id->filename, id->key);
1054 }
fee56204 1055}
1056
43348518 1057static void
1058pubkey_cleanup(Authctxt *authctxt)
2e73a022 1059{
43348518 1060 Identity *id;
1061
1062 if (authctxt->agent != NULL)
1063 ssh_close_authentication_connection(authctxt->agent);
1064 for (id = TAILQ_FIRST(&authctxt->keys); id;
1065 id = TAILQ_FIRST(&authctxt->keys)) {
1066 TAILQ_REMOVE(&authctxt->keys, id, next);
1067 if (id->key)
1068 key_free(id->key);
1069 if (id->filename)
1070 xfree(id->filename);
1071 xfree(id);
2e73a022 1072 }
a306f2dd 1073}
1074
188adeb2 1075int
1076userauth_pubkey(Authctxt *authctxt)
a306f2dd 1077{
43348518 1078 Identity *id;
188adeb2 1079 int sent = 0;
1080
43348518 1081 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1082 if (id->tried++)
1083 return (0);
1084 TAILQ_REMOVE(&authctxt->keys, id, next);
1085 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1086 /*
1087 * send a test message if we have the public key. for
1088 * encrypted keys we cannot do this and have to load the
1089 * private key instead
1090 */
1091 if (id->key && id->key->type != KEY_RSA1) {
1092 debug("Offering public key: %s", id->filename);
1093 sent = send_pubkey_test(authctxt, id);
1094 } else if (id->key == NULL) {
1095 debug("Trying private key: %s", id->filename);
1096 id->key = load_identity_file(id->filename);
1097 if (id->key != NULL) {
1098 id->isprivate = 1;
1099 sent = sign_and_send_pubkey(authctxt, id);
1100 key_free(id->key);
1101 id->key = NULL;
fee56204 1102 }
fee56204 1103 }
43348518 1104 if (sent)
1105 return (sent);
fa08c86b 1106 }
43348518 1107 return (0);
188adeb2 1108}
a306f2dd 1109
94ec8c6b 1110/*
1111 * Send userauth request message specifying keyboard-interactive method.
1112 */
1113int
1114userauth_kbdint(Authctxt *authctxt)
1115{
1116 static int attempt = 0;
1117
1118 if (attempt++ >= options.number_of_password_prompts)
1119 return 0;
6b759005 1120 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1121 if (attempt > 1 && !authctxt->info_req_seen) {
1122 debug3("userauth_kbdint: disable: no info_req_seen");
1123 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1124 return 0;
1125 }
94ec8c6b 1126
1127 debug2("userauth_kbdint");
1128 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1129 packet_put_cstring(authctxt->server_user);
1130 packet_put_cstring(authctxt->service);
1131 packet_put_cstring(authctxt->method->name);
1132 packet_put_cstring(""); /* lang */
1133 packet_put_cstring(options.kbd_interactive_devices ?
1134 options.kbd_interactive_devices : "");
1135 packet_send();
94ec8c6b 1136
1137 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1138 return 1;
1139}
1140
1141/*
f72e01a5 1142 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
94ec8c6b 1143 */
1144void
7819b5c3 1145input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
94ec8c6b 1146{
1147 Authctxt *authctxt = ctxt;
f72e01a5 1148 char *name, *inst, *lang, *prompt, *response;
1e3b8b07 1149 u_int num_prompts, i;
94ec8c6b 1150 int echo = 0;
1151
1152 debug2("input_userauth_info_req");
1153
1154 if (authctxt == NULL)
1155 fatal("input_userauth_info_req: no authentication context");
1156
6b759005 1157 authctxt->info_req_seen = 1;
1158
94ec8c6b 1159 name = packet_get_string(NULL);
1160 inst = packet_get_string(NULL);
1161 lang = packet_get_string(NULL);
94ec8c6b 1162 if (strlen(name) > 0)
bbe88b6d 1163 logit("%s", name);
94ec8c6b 1164 if (strlen(inst) > 0)
bbe88b6d 1165 logit("%s", inst);
f72e01a5 1166 xfree(name);
94ec8c6b 1167 xfree(inst);
f72e01a5 1168 xfree(lang);
94ec8c6b 1169
1170 num_prompts = packet_get_int();
1171 /*
1172 * Begin to build info response packet based on prompts requested.
1173 * We commit to providing the correct number of responses, so if
1174 * further on we run into a problem that prevents this, we have to
1175 * be sure and clean this up and send a correct error response.
1176 */
1177 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1178 packet_put_int(num_prompts);
1179
5ba55ada 1180 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
94ec8c6b 1181 for (i = 0; i < num_prompts; i++) {
1182 prompt = packet_get_string(NULL);
1183 echo = packet_get_char();
1184
1843a425 1185 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
94ec8c6b 1186
a6215e53 1187 packet_put_cstring(response);
94ec8c6b 1188 memset(response, 0, strlen(response));
1189 xfree(response);
1190 xfree(prompt);
1191 }
95500969 1192 packet_check_eom(); /* done with parsing incoming message. */
94ec8c6b 1193
b4b701be 1194 packet_add_padding(64);
94ec8c6b 1195 packet_send();
94ec8c6b 1196}
a306f2dd 1197
39c00dc2 1198static int
d6133f43 1199ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
39c00dc2 1200 u_char *data, u_int datalen)
1201{
1202 Buffer b;
7091a26b 1203 struct stat st;
39c00dc2 1204 pid_t pid;
d4826734 1205 int to[2], from[2], status, version = 2;
39c00dc2 1206
6226a8f8 1207 debug2("ssh_keysign called");
39c00dc2 1208
7091a26b 1209 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
1210 error("ssh_keysign: no installed: %s", strerror(errno));
1211 return -1;
1212 }
39c00dc2 1213 if (fflush(stdout) != 0)
1214 error("ssh_keysign: fflush: %s", strerror(errno));
1215 if (pipe(to) < 0) {
1216 error("ssh_keysign: pipe: %s", strerror(errno));
1217 return -1;
1218 }
1219 if (pipe(from) < 0) {
1220 error("ssh_keysign: pipe: %s", strerror(errno));
1221 return -1;
1222 }
1223 if ((pid = fork()) < 0) {
1224 error("ssh_keysign: fork: %s", strerror(errno));
1225 return -1;
1226 }
1227 if (pid == 0) {
1228 seteuid(getuid());
1229 setuid(getuid());
1230 close(from[0]);
1231 if (dup2(from[1], STDOUT_FILENO) < 0)
1232 fatal("ssh_keysign: dup2: %s", strerror(errno));
1233 close(to[1]);
1234 if (dup2(to[0], STDIN_FILENO) < 0)
1235 fatal("ssh_keysign: dup2: %s", strerror(errno));
d4826734 1236 close(from[1]);
1237 close(to[0]);
a3f69458 1238 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
39c00dc2 1239 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1240 strerror(errno));
1241 }
1242 close(from[1]);
1243 close(to[0]);
1244
1245 buffer_init(&b);
d4826734 1246 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
39c00dc2 1247 buffer_put_string(&b, data, datalen);
e987fdbe 1248 ssh_msg_send(to[1], version, &b);
39c00dc2 1249
e987fdbe 1250 if (ssh_msg_recv(from[0], &b) < 0) {
7091a26b 1251 error("ssh_keysign: no reply");
39c00dc2 1252 buffer_clear(&b);
1253 return -1;
1254 }
39c00dc2 1255 close(from[0]);
1256 close(to[1]);
1257
d4826734 1258 while (waitpid(pid, &status, 0) < 0)
1259 if (errno != EINTR)
1260 break;
39c00dc2 1261
7091a26b 1262 if (buffer_get_char(&b) != version) {
1263 error("ssh_keysign: bad version");
1264 buffer_clear(&b);
1265 return -1;
1266 }
1267 *sigp = buffer_get_string(&b, lenp);
1268 buffer_clear(&b);
1269
39c00dc2 1270 return 0;
1271}
1272
8002af61 1273int
1274userauth_hostbased(Authctxt *authctxt)
1275{
1276 Key *private = NULL;
39c00dc2 1277 Sensitive *sensitive = authctxt->sensitive;
8002af61 1278 Buffer b;
1279 u_char *signature, *blob;
1280 char *chost, *pkalg, *p;
8dddf799 1281 const char *service;
8002af61 1282 u_int blen, slen;
f98c3421 1283 int ok, i, len, found = 0;
8002af61 1284
8002af61 1285 /* check for a useful key */
39c00dc2 1286 for (i = 0; i < sensitive->nkeys; i++) {
1287 private = sensitive->keys[i];
8002af61 1288 if (private && private->type != KEY_RSA1) {
1289 found = 1;
1290 /* we take and free the key */
39c00dc2 1291 sensitive->keys[i] = NULL;
8002af61 1292 break;
1293 }
1294 }
1295 if (!found) {
6226a8f8 1296 debug("No more client hostkeys for hostbased authentication.");
8002af61 1297 return 0;
1298 }
1299 if (key_to_blob(private, &blob, &blen) == 0) {
1300 key_free(private);
8002af61 1301 return 0;
1302 }
e8d59b4d 1303 /* figure out a name for the client host */
1304 p = get_local_name(packet_get_connection_in());
1305 if (p == NULL) {
1306 error("userauth_hostbased: cannot get local ipaddr/name");
1307 key_free(private);
1308 return 0;
1309 }
1310 len = strlen(p) + 2;
1311 chost = xmalloc(len);
1312 strlcpy(chost, p, len);
1313 strlcat(chost, ".", len);
1314 debug2("userauth_hostbased: chost %s", chost);
3e2f2431 1315 xfree(p);
e8d59b4d 1316
8dddf799 1317 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1318 authctxt->service;
8002af61 1319 pkalg = xstrdup(key_ssh_name(private));
1320 buffer_init(&b);
8002af61 1321 /* construct data */
8dddf799 1322 buffer_put_string(&b, session_id2, session_id2_len);
8002af61 1323 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1324 buffer_put_cstring(&b, authctxt->server_user);
8dddf799 1325 buffer_put_cstring(&b, service);
8002af61 1326 buffer_put_cstring(&b, authctxt->method->name);
1327 buffer_put_cstring(&b, pkalg);
1328 buffer_put_string(&b, blob, blen);
1329 buffer_put_cstring(&b, chost);
1330 buffer_put_cstring(&b, authctxt->local_user);
1331#ifdef DEBUG_PK
1332 buffer_dump(&b);
1333#endif
39c00dc2 1334 if (sensitive->external_keysign)
1335 ok = ssh_keysign(private, &signature, &slen,
1336 buffer_ptr(&b), buffer_len(&b));
1337 else
1338 ok = key_sign(private, &signature, &slen,
1339 buffer_ptr(&b), buffer_len(&b));
8002af61 1340 key_free(private);
1341 buffer_free(&b);
1342 if (ok != 0) {
1343 error("key_sign failed");
1344 xfree(chost);
1345 xfree(pkalg);
1346 return 0;
1347 }
1348 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1349 packet_put_cstring(authctxt->server_user);
1350 packet_put_cstring(authctxt->service);
1351 packet_put_cstring(authctxt->method->name);
1352 packet_put_cstring(pkalg);
1353 packet_put_string(blob, blen);
1354 packet_put_cstring(chost);
1355 packet_put_cstring(authctxt->local_user);
1356 packet_put_string(signature, slen);
1357 memset(signature, 's', slen);
1358 xfree(signature);
1359 xfree(chost);
1360 xfree(pkalg);
1361
1362 packet_send();
1363 return 1;
1364}
1365
188adeb2 1366/* find auth method */
1367
188adeb2 1368/*
1369 * given auth method name, if configurable options permit this method fill
1370 * in auth_ident field and return true, otherwise return false.
1371 */
396c147e 1372static int
188adeb2 1373authmethod_is_enabled(Authmethod *method)
1374{
1375 if (method == NULL)
1376 return 0;
1377 /* return false if options indicate this method is disabled */
1378 if (method->enabled == NULL || *method->enabled == 0)
1379 return 0;
1380 /* return false if batch mode is enabled but method needs interactive mode */
1381 if (method->batch_flag != NULL && *method->batch_flag != 0)
1382 return 0;
1383 return 1;
1384}
a306f2dd 1385
396c147e 1386static Authmethod *
188adeb2 1387authmethod_lookup(const char *name)
1388{
1389 Authmethod *method = NULL;
1390 if (name != NULL)
1391 for (method = authmethods; method->name != NULL; method++)
1392 if (strcmp(name, method->name) == 0)
1393 return method;
1394 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1395 return NULL;
1396}
1397
cab80f75 1398/* XXX internal state */
1399static Authmethod *current = NULL;
1400static char *supported = NULL;
1401static char *preferred = NULL;
d6133f43 1402
188adeb2 1403/*
1404 * Given the authentication method list sent by the server, return the
1405 * next method we should try. If the server initially sends a nil list,
cd332296 1406 * use a built-in default list.
2b87da3b 1407 */
396c147e 1408static Authmethod *
188adeb2 1409authmethod_get(char *authlist)
1410{
cab80f75 1411 char *name = NULL;
21b30f38 1412 u_int next;
2b87da3b 1413
188adeb2 1414 /* Use a suitable default if we're passed a nil list. */
1415 if (authlist == NULL || strlen(authlist) == 0)
cab80f75 1416 authlist = options.preferred_authentications;
1417
1418 if (supported == NULL || strcmp(authlist, supported) != 0) {
1419 debug3("start over, passed a different list %s", authlist);
1420 if (supported != NULL)
1421 xfree(supported);
1422 supported = xstrdup(authlist);
1423 preferred = options.preferred_authentications;
1424 debug3("preferred %s", preferred);
1425 current = NULL;
1426 } else if (current != NULL && authmethod_is_enabled(current))
1427 return current;
188adeb2 1428
cab80f75 1429 for (;;) {
1430 if ((name = match_list(preferred, supported, &next)) == NULL) {
6226a8f8 1431 debug("No more authentication methods to try.");
cab80f75 1432 current = NULL;
1433 return NULL;
1434 }
1435 preferred += next;
94ec8c6b 1436 debug3("authmethod_lookup %s", name);
cab80f75 1437 debug3("remaining preferred: %s", preferred);
1438 if ((current = authmethod_lookup(name)) != NULL &&
1439 authmethod_is_enabled(current)) {
94ec8c6b 1440 debug3("authmethod_is_enabled %s", name);
6226a8f8 1441 debug("Next authentication method: %s", name);
cab80f75 1442 return current;
94ec8c6b 1443 }
a306f2dd 1444 }
cab80f75 1445}
a22aff1f 1446
ffb215be 1447static char *
cab80f75 1448authmethods_get(void)
1449{
1450 Authmethod *method = NULL;
3469eac4 1451 Buffer b;
1452 char *list;
cab80f75 1453
3469eac4 1454 buffer_init(&b);
cab80f75 1455 for (method = authmethods; method->name != NULL; method++) {
1456 if (authmethod_is_enabled(method)) {
3469eac4 1457 if (buffer_len(&b) > 0)
1458 buffer_append(&b, ",", 1);
1459 buffer_append(&b, method->name, strlen(method->name));
cab80f75 1460 }
1461 }
3469eac4 1462 buffer_append(&b, "\0", 1);
1463 list = xstrdup(buffer_ptr(&b));
1464 buffer_free(&b);
1465 return list;
a306f2dd 1466}
This page took 0.951023 seconds and 5 git commands to generate.