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