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