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