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