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