]> andersk Git - gssapi-openssh.git/blame - openssh/sshconnect2.c
port to OpenSSH 3.1p1
[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"
1e608e42 26RCSID("$OpenBSD: sshconnect2.c,v 1.97 2002/02/25 16:33:27 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"
48
5598e598 49#ifdef GSSAPI
50#include "ssh-gss.h"
51#endif
52
3c0ef626 53/* import */
54extern char *client_version_string;
55extern char *server_version_string;
56extern Options options;
57
58/*
59 * SSH2 key exchange
60 */
61
62u_char *session_id2 = NULL;
63int session_id2_len = 0;
64
65char *xxx_host;
66struct sockaddr *xxx_hostaddr;
67
68Kex *xxx_kex = NULL;
69
70static int
71verify_host_key_callback(Key *hostkey)
72{
73 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
74 fatal("Host key verification failed.");
75 return 0;
76}
77
78void
79ssh_kex2(char *host, struct sockaddr *hostaddr)
80{
81 Kex *kex;
1e608e42 82#ifdef GSSAPI
83 char *orig, *gss;
84 int len;
85#endif
3c0ef626 86
87 xxx_host = host;
88 xxx_hostaddr = hostaddr;
89
5598e598 90#ifdef GSSAPI
1e608e42 91 /* Add the GSSAPI mechanisms currently supported on this client to
92 * the key exchange algorithm proposal */
93 orig = myproposal[PROPOSAL_KEX_ALGS];
94 gss = ssh_gssapi_mechanisms(0,host);
95 if (gss) {
96 len = strlen(orig)+strlen(gss)+2;
97 myproposal[PROPOSAL_KEX_ALGS]=xmalloc(len);
98 snprintf(myproposal[PROPOSAL_KEX_ALGS],len,"%s,%s",gss,orig);
5598e598 99 }
100#endif
101
3c0ef626 102 if (options.ciphers == (char *)-1) {
103 log("No valid ciphers for protocol version 2 given, using defaults.");
104 options.ciphers = NULL;
105 }
106 if (options.ciphers != NULL) {
107 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
108 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
109 }
110 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
111 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
112 myproposal[PROPOSAL_ENC_ALGS_STOC] =
113 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
114 if (options.compression) {
115 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
116 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib";
117 } else {
118 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
119 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none";
120 }
121 if (options.macs != NULL) {
122 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
123 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
124 }
125 if (options.hostkeyalgorithms != NULL)
1e608e42 126 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
3c0ef626 127 options.hostkeyalgorithms;
128
1e608e42 129#ifdef GSSAPI
130 /* If we've got GSSAPI algorithms, then we also support the
131 * 'null' hostkey, as a last resort */
132 if (gss) {
133 orig=myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS];
134 len = strlen(orig)+sizeof(",null");
135 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS]=xmalloc(len);
136 snprintf(myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS],len,"%s,null",orig);
137 }
138#endif
139
3c0ef626 140 /* start key exchange */
141 kex = kex_setup(myproposal);
142 kex->client_version_string=client_version_string;
143 kex->server_version_string=server_version_string;
144 kex->verify_host_key=&verify_host_key_callback;
5598e598 145 kex->host=host;
146
147#ifdef GSSAPI
148 kex->options.gss_deleg_creds=options.gss_deleg_creds;
149#endif
3c0ef626 150
151 xxx_kex = kex;
152
153 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
154
155 session_id2 = kex->session_id;
156 session_id2_len = kex->session_id_len;
157
158#ifdef DEBUG_KEXDH
159 /* send 1st encrypted/maced/compressed message */
160 packet_start(SSH2_MSG_IGNORE);
161 packet_put_cstring("markus");
162 packet_send();
163 packet_write_wait();
164#endif
165 debug("done: ssh_kex2.");
166}
167
168/*
169 * Authenticate user
170 */
171
172typedef struct Authctxt Authctxt;
173typedef struct Authmethod Authmethod;
174
175typedef int sign_cb_fn(
176 Authctxt *authctxt, Key *key,
1e608e42 177 u_char **sigp, u_int *lenp, u_char *data, u_int datalen);
3c0ef626 178
179struct Authctxt {
180 const char *server_user;
181 const char *local_user;
182 const char *host;
183 const char *service;
184 Authmethod *method;
185 int success;
186 char *authlist;
187 /* pubkey */
188 Key *last_key;
189 sign_cb_fn *last_key_sign;
190 int last_key_hint;
191 AuthenticationConnection *agent;
192 /* hostbased */
193 Key **keys;
194 int nkeys;
195 /* kbd-interactive */
196 int info_req_seen;
5598e598 197 /* generic */
198 void *methoddata;
3c0ef626 199};
200struct Authmethod {
201 char *name; /* string to compare against server's list */
202 int (*userauth)(Authctxt *authctxt);
203 int *enabled; /* flag in option struct that enables method */
204 int *batch_flag; /* flag in option struct that disables method */
205};
206
1e608e42 207void input_userauth_success(int, u_int32_t, void *);
208void input_userauth_failure(int, u_int32_t, void *);
209void input_userauth_banner(int, u_int32_t, void *);
210void input_userauth_error(int, u_int32_t, void *);
211void input_userauth_info_req(int, u_int32_t, void *);
212void input_userauth_pk_ok(int, u_int32_t, void *);
3c0ef626 213
1e608e42 214int userauth_none(Authctxt *);
215int userauth_pubkey(Authctxt *);
216int userauth_passwd(Authctxt *);
217int userauth_kbdint(Authctxt *);
218int userauth_hostbased(Authctxt *);
3c0ef626 219
5598e598 220#ifdef GSSAPI
1e608e42 221int userauth_external(Authctxt *authctxt);
222int userauth_gssapi(Authctxt *authctxt);
223void input_gssapi_response(int type, u_int32_t plen, void *ctxt);
224void input_gssapi_token(int type, u_int32_t plen, void *ctxt);
225void input_gssapi_hash(int type, u_int32_t plen, void *ctxt);
5598e598 226#endif
227
1e608e42 228void userauth(Authctxt *, char *);
3c0ef626 229
230static int sign_and_send_pubkey(Authctxt *, Key *, sign_cb_fn *);
231static void clear_auth_state(Authctxt *);
232
233static Authmethod *authmethod_get(char *authlist);
234static Authmethod *authmethod_lookup(const char *name);
235static char *authmethods_get(void);
236
237Authmethod authmethods[] = {
5598e598 238#ifdef GSSAPI
239 {"external-keyx",
240 userauth_external,
241 &options.gss_authentication,
242 NULL},
243 {"gssapi",
244 userauth_gssapi,
245 &options.gss_authentication,
246 NULL},
247#endif
3c0ef626 248 {"hostbased",
249 userauth_hostbased,
250 &options.hostbased_authentication,
251 NULL},
252 {"publickey",
253 userauth_pubkey,
254 &options.pubkey_authentication,
255 NULL},
256 {"keyboard-interactive",
257 userauth_kbdint,
258 &options.kbd_interactive_authentication,
259 &options.batch_mode},
260 {"password",
261 userauth_passwd,
262 &options.password_authentication,
263 &options.batch_mode},
264 {"none",
265 userauth_none,
266 NULL,
267 NULL},
268 {NULL, NULL, NULL, NULL}
269};
270
271void
272ssh_userauth2(const char *local_user, const char *server_user, char *host,
273 Key **keys, int nkeys)
274{
275 Authctxt authctxt;
276 int type;
3c0ef626 277
278 if (options.challenge_response_authentication)
279 options.kbd_interactive_authentication = 1;
280
281 debug("send SSH2_MSG_SERVICE_REQUEST");
282 packet_start(SSH2_MSG_SERVICE_REQUEST);
283 packet_put_cstring("ssh-userauth");
284 packet_send();
285 packet_write_wait();
1e608e42 286 type = packet_read();
3c0ef626 287 if (type != SSH2_MSG_SERVICE_ACCEPT) {
288 fatal("denied SSH2_MSG_SERVICE_ACCEPT: %d", type);
289 }
290 if (packet_remaining() > 0) {
1e608e42 291 char *reply = packet_get_string(NULL);
3c0ef626 292 debug("service_accept: %s", reply);
293 xfree(reply);
294 } else {
295 debug("buggy server: service_accept w/o service");
296 }
1e608e42 297 packet_check_eom();
3c0ef626 298 debug("got SSH2_MSG_SERVICE_ACCEPT");
299
300 if (options.preferred_authentications == NULL)
301 options.preferred_authentications = authmethods_get();
302
303 /* setup authentication context */
304 memset(&authctxt, 0, sizeof(authctxt));
305 authctxt.agent = ssh_get_authentication_connection();
306 authctxt.server_user = server_user;
307 authctxt.local_user = local_user;
308 authctxt.host = host;
309 authctxt.service = "ssh-connection"; /* service name */
310 authctxt.success = 0;
311 authctxt.method = authmethod_lookup("none");
312 authctxt.authlist = NULL;
5598e598 313 authctxt.methoddata = NULL;
3c0ef626 314 authctxt.keys = keys;
315 authctxt.nkeys = nkeys;
316 authctxt.info_req_seen = 0;
317 if (authctxt.method == NULL)
318 fatal("ssh_userauth2: internal error: cannot send userauth none request");
319
320 /* initial userauth request */
321 userauth_none(&authctxt);
322
323 dispatch_init(&input_userauth_error);
324 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
325 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
326 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
327 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
328
329 if (authctxt.agent != NULL)
330 ssh_close_authentication_connection(authctxt.agent);
331
332 debug("ssh-userauth2 successful: method %s", authctxt.method->name);
333}
334void
335userauth(Authctxt *authctxt, char *authlist)
336{
5598e598 337 if (authctxt->methoddata!=NULL) {
338 xfree(authctxt->methoddata);
339 authctxt->methoddata=NULL;
340 }
341
3c0ef626 342 if (authlist == NULL) {
343 authlist = authctxt->authlist;
344 } else {
345 if (authctxt->authlist)
346 xfree(authctxt->authlist);
347 authctxt->authlist = authlist;
348 }
349 for (;;) {
350 Authmethod *method = authmethod_get(authlist);
351 if (method == NULL)
352 fatal("Permission denied (%s).", authlist);
353 authctxt->method = method;
354 if (method->userauth(authctxt) != 0) {
355 debug2("we sent a %s packet, wait for reply", method->name);
356 break;
357 } else {
358 debug2("we did not send a packet, disable method");
359 method->enabled = NULL;
360 }
361 }
362}
363void
1e608e42 364input_userauth_error(int type, u_int32_t seq, void *ctxt)
3c0ef626 365{
366 fatal("input_userauth_error: bad message during authentication: "
367 "type %d", type);
368}
369void
1e608e42 370input_userauth_banner(int type, u_int32_t seq, void *ctxt)
3c0ef626 371{
372 char *msg, *lang;
373 debug3("input_userauth_banner");
374 msg = packet_get_string(NULL);
375 lang = packet_get_string(NULL);
376 fprintf(stderr, "%s", msg);
377 xfree(msg);
378 xfree(lang);
379}
380void
1e608e42 381input_userauth_success(int type, u_int32_t seq, void *ctxt)
3c0ef626 382{
383 Authctxt *authctxt = ctxt;
384 if (authctxt == NULL)
385 fatal("input_userauth_success: no authentication context");
386 if (authctxt->authlist)
387 xfree(authctxt->authlist);
5598e598 388 if (authctxt->methoddata)
389 xfree(authctxt->methoddata);
3c0ef626 390 clear_auth_state(authctxt);
391 authctxt->success = 1; /* break out */
392}
393void
1e608e42 394input_userauth_failure(int type, u_int32_t seq, void *ctxt)
3c0ef626 395{
396 Authctxt *authctxt = ctxt;
397 char *authlist = NULL;
398 int partial;
399
400 if (authctxt == NULL)
401 fatal("input_userauth_failure: no authentication context");
402
403 authlist = packet_get_string(NULL);
404 partial = packet_get_char();
1e608e42 405 packet_check_eom();
3c0ef626 406
407 if (partial != 0)
408 log("Authenticated with partial success.");
409 debug("authentications that can continue: %s", authlist);
410
411 clear_auth_state(authctxt);
412 userauth(authctxt, authlist);
413}
414void
1e608e42 415input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
3c0ef626 416{
417 Authctxt *authctxt = ctxt;
418 Key *key = NULL;
419 Buffer b;
1e608e42 420 int pktype, sent = 0;
421 u_int alen, blen;
422 char *pkalg, *fp;
423 u_char *pkblob;
3c0ef626 424
425 if (authctxt == NULL)
426 fatal("input_userauth_pk_ok: no authentication context");
427 if (datafellows & SSH_BUG_PKOK) {
428 /* this is similar to SSH_BUG_PKAUTH */
429 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
430 pkblob = packet_get_string(&blen);
431 buffer_init(&b);
432 buffer_append(&b, pkblob, blen);
433 pkalg = buffer_get_string(&b, &alen);
434 buffer_free(&b);
435 } else {
436 pkalg = packet_get_string(&alen);
437 pkblob = packet_get_string(&blen);
438 }
1e608e42 439 packet_check_eom();
3c0ef626 440
441 debug("input_userauth_pk_ok: pkalg %s blen %d lastkey %p hint %d",
442 pkalg, blen, authctxt->last_key, authctxt->last_key_hint);
443
444 do {
445 if (authctxt->last_key == NULL ||
446 authctxt->last_key_sign == NULL) {
447 debug("no last key or no sign cb");
448 break;
449 }
1e608e42 450 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
3c0ef626 451 debug("unknown pkalg %s", pkalg);
452 break;
453 }
454 if ((key = key_from_blob(pkblob, blen)) == NULL) {
455 debug("no key from blob. pkalg %s", pkalg);
456 break;
457 }
1e608e42 458 if (key->type != pktype) {
459 error("input_userauth_pk_ok: type mismatch "
460 "for decoded key (received %d, expected %d)",
461 key->type, pktype);
462 break;
463 }
3c0ef626 464 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
465 debug2("input_userauth_pk_ok: fp %s", fp);
466 xfree(fp);
467 if (!key_equal(key, authctxt->last_key)) {
468 debug("key != last_key");
469 break;
470 }
471 sent = sign_and_send_pubkey(authctxt, key,
472 authctxt->last_key_sign);
1e608e42 473 } while (0);
3c0ef626 474
475 if (key != NULL)
476 key_free(key);
477 xfree(pkalg);
478 xfree(pkblob);
479
480 /* unregister */
481 clear_auth_state(authctxt);
482 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, NULL);
483
484 /* try another method if we did not send a packet*/
485 if (sent == 0)
486 userauth(authctxt, NULL);
487
488}
489
5598e598 490#ifdef GSSAPI
491int
492userauth_gssapi(Authctxt *authctxt)
493{
494 int i;
495 Gssctxt *gssctxt;
496 static int tries=0;
497
498 /* For now, we only make one attempt at this. We could try offering
499 * the server different GSSAPI OIDs until we get bored, I suppose.
500 */
501 if (tries++>0) return 0;
502
1e608e42 503 if (datafellows & SSH_OLD_GSSAPI) return 0;
504
5598e598 505 gssctxt=xmalloc(sizeof(Gssctxt));
506
507 /* Initialise as much of our context as we can, so failures can be
508 * trapped before sending any packets.
509 */
510 ssh_gssapi_build_ctx(gssctxt);
511 if (ssh_gssapi_import_name(gssctxt,authctxt->host)) {
512 return(0);
513 }
514 authctxt->methoddata=(void *)gssctxt;
515
516 packet_start(SSH2_MSG_USERAUTH_REQUEST);
517 packet_put_cstring(authctxt->server_user);
518 packet_put_cstring(authctxt->service);
519 packet_put_cstring(authctxt->method->name);
520
521 /* FIXME: This assumes that our current GSSAPI implementation
522 * supports all of the mechanisms listed in supported_mechs.
523 * This may not be the case - we should use something along
524 * the lines of the code in gss_genr to remove the ones that
525 * aren't supported */
526 packet_put_int(GSS_LAST_ENTRY);
527 for (i=0;i<GSS_LAST_ENTRY;i++) {
528 packet_put_string(supported_mechs[i].oid.elements,
529 supported_mechs[i].oid.length);
530 }
531 packet_send();
532 packet_write_wait();
533
534 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE,&input_gssapi_response);
535 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN,&input_gssapi_token);
536
537 return 1;
538}
539
540void
1e608e42 541input_gssapi_response(int type, u_int32_t plen, void *ctxt)
5598e598 542{
543 Authctxt *authctxt = ctxt;
544 Gssctxt *gssctxt;
545 OM_uint32 status,ms;
546 int oidlen;
547 char *oidv;
548 gss_buffer_desc send_tok;
549
550 if (authctxt == NULL)
551 fatal("input_gssapi_response: no authentication context");
552 gssctxt = authctxt->methoddata;
553
554 /* Setup our OID */
555 oidv=packet_get_string(&oidlen);
556 ssh_gssapi_set_oid_data(gssctxt,oidv,oidlen);
557
1e608e42 558 packet_check_eom();
5598e598 559
560 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
561 GSS_C_NO_BUFFER, &send_tok,
562 NULL);
563 if (GSS_ERROR(status)) {
564 /* Start again with next method on list */
565 debug("Trying to start again");
566 userauth(authctxt,NULL);
567 return;
568 }
569
570 /* We must have data to send */
571 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
572 packet_put_string(send_tok.value,send_tok.length);
573 packet_send();
574 packet_write_wait();
575 gss_release_buffer(&ms, &send_tok);
576}
577
578void
1e608e42 579input_gssapi_token(int type, u_int32_t plen, void *ctxt)
5598e598 580{
581 Authctxt *authctxt = ctxt;
582 Gssctxt *gssctxt;
583 gss_buffer_desc send_tok,recv_tok;
584 OM_uint32 status;
585
586 if (authctxt == NULL)
587 fatal("input_gssapi_response: no authentication context");
588 gssctxt = authctxt->methoddata;
589
590 recv_tok.value=packet_get_string(&recv_tok.length);
591
592 status=ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
593 &recv_tok, &send_tok, NULL);
594
1e608e42 595 packet_check_eom();
5598e598 596
597 if (GSS_ERROR(status)) {
598 /* Start again with the next method in the list */
599 userauth(authctxt,NULL);
600 return;
601 }
602
603 if (send_tok.length>0) {
604 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
605 packet_put_string(send_tok.value,send_tok.length);
606 packet_send();
607 packet_write_wait();
608 }
609
610 if (status == GSS_S_COMPLETE) {
611 /* If that succeeded, send a exchange complete message */
612 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
613 packet_send();
614 packet_write_wait();
615 }
616}
617
618int
619userauth_external(Authctxt *authctxt)
620{
621 static int attempt =0;
622
623 if (attempt++ >= 1)
624 return 0;
625
626 debug2("userauth_external");
627 packet_start(SSH2_MSG_USERAUTH_REQUEST);
628 packet_put_cstring(authctxt->server_user);
629 packet_put_cstring(authctxt->service);
630 packet_put_cstring(authctxt->method->name);
631 packet_send();
632 packet_write_wait();
633 return 1;
634}
635#endif /* GSSAPI */
636
3c0ef626 637int
638userauth_none(Authctxt *authctxt)
639{
640 /* initial userauth request */
641 packet_start(SSH2_MSG_USERAUTH_REQUEST);
642 packet_put_cstring(authctxt->server_user);
643 packet_put_cstring(authctxt->service);
644 packet_put_cstring(authctxt->method->name);
645 packet_send();
646 return 1;
5598e598 647
3c0ef626 648}
649
650int
651userauth_passwd(Authctxt *authctxt)
652{
653 static int attempt = 0;
654 char prompt[80];
655 char *password;
656
657 if (attempt++ >= options.number_of_password_prompts)
658 return 0;
659
1e608e42 660 if (attempt != 1)
3c0ef626 661 error("Permission denied, please try again.");
662
663 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
664 authctxt->server_user, authctxt->host);
665 password = read_passphrase(prompt, 0);
666 packet_start(SSH2_MSG_USERAUTH_REQUEST);
667 packet_put_cstring(authctxt->server_user);
668 packet_put_cstring(authctxt->service);
669 packet_put_cstring(authctxt->method->name);
670 packet_put_char(0);
671 packet_put_cstring(password);
672 memset(password, 0, strlen(password));
673 xfree(password);
674 packet_add_padding(64);
675 packet_send();
676 return 1;
677}
678
679static void
680clear_auth_state(Authctxt *authctxt)
681{
682 /* XXX clear authentication state */
683 if (authctxt->last_key != NULL && authctxt->last_key_hint == -1) {
684 debug3("clear_auth_state: key_free %p", authctxt->last_key);
685 key_free(authctxt->last_key);
686 }
687 authctxt->last_key = NULL;
688 authctxt->last_key_hint = -2;
689 authctxt->last_key_sign = NULL;
690}
691
692static int
693sign_and_send_pubkey(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback)
694{
695 Buffer b;
696 u_char *blob, *signature;
1e608e42 697 u_int bloblen, slen;
3c0ef626 698 int skip = 0;
699 int ret = -1;
700 int have_sig = 1;
701
702 debug3("sign_and_send_pubkey");
703
704 if (key_to_blob(k, &blob, &bloblen) == 0) {
705 /* we cannot handle this key */
706 debug3("sign_and_send_pubkey: cannot handle key");
707 return 0;
708 }
709 /* data to be signed */
710 buffer_init(&b);
711 if (datafellows & SSH_OLD_SESSIONID) {
712 buffer_append(&b, session_id2, session_id2_len);
713 skip = session_id2_len;
714 } else {
715 buffer_put_string(&b, session_id2, session_id2_len);
716 skip = buffer_len(&b);
717 }
718 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
719 buffer_put_cstring(&b, authctxt->server_user);
720 buffer_put_cstring(&b,
721 datafellows & SSH_BUG_PKSERVICE ?
722 "ssh-userauth" :
723 authctxt->service);
724 if (datafellows & SSH_BUG_PKAUTH) {
725 buffer_put_char(&b, have_sig);
726 } else {
727 buffer_put_cstring(&b, authctxt->method->name);
728 buffer_put_char(&b, have_sig);
729 buffer_put_cstring(&b, key_ssh_name(k));
730 }
731 buffer_put_string(&b, blob, bloblen);
732
733 /* generate signature */
734 ret = (*sign_callback)(authctxt, k, &signature, &slen,
735 buffer_ptr(&b), buffer_len(&b));
736 if (ret == -1) {
737 xfree(blob);
738 buffer_free(&b);
739 return 0;
740 }
741#ifdef DEBUG_PK
742 buffer_dump(&b);
743#endif
744 if (datafellows & SSH_BUG_PKSERVICE) {
745 buffer_clear(&b);
746 buffer_append(&b, session_id2, session_id2_len);
747 skip = session_id2_len;
748 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
749 buffer_put_cstring(&b, authctxt->server_user);
750 buffer_put_cstring(&b, authctxt->service);
751 buffer_put_cstring(&b, authctxt->method->name);
752 buffer_put_char(&b, have_sig);
753 if (!(datafellows & SSH_BUG_PKAUTH))
754 buffer_put_cstring(&b, key_ssh_name(k));
755 buffer_put_string(&b, blob, bloblen);
756 }
757 xfree(blob);
758
759 /* append signature */
760 buffer_put_string(&b, signature, slen);
761 xfree(signature);
762
763 /* skip session id and packet type */
764 if (buffer_len(&b) < skip + 1)
765 fatal("userauth_pubkey: internal error");
766 buffer_consume(&b, skip + 1);
767
768 /* put remaining data from buffer into packet */
769 packet_start(SSH2_MSG_USERAUTH_REQUEST);
770 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
771 buffer_free(&b);
772 packet_send();
773
774 return 1;
775}
776
777static int
778send_pubkey_test(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback,
779 int hint)
780{
781 u_char *blob;
1e608e42 782 u_int bloblen, have_sig = 0;
3c0ef626 783
784 debug3("send_pubkey_test");
785
786 if (key_to_blob(k, &blob, &bloblen) == 0) {
787 /* we cannot handle this key */
788 debug3("send_pubkey_test: cannot handle key");
789 return 0;
790 }
791 /* register callback for USERAUTH_PK_OK message */
792 authctxt->last_key_sign = sign_callback;
793 authctxt->last_key_hint = hint;
794 authctxt->last_key = k;
795 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
796
797 packet_start(SSH2_MSG_USERAUTH_REQUEST);
798 packet_put_cstring(authctxt->server_user);
799 packet_put_cstring(authctxt->service);
800 packet_put_cstring(authctxt->method->name);
801 packet_put_char(have_sig);
802 if (!(datafellows & SSH_BUG_PKAUTH))
803 packet_put_cstring(key_ssh_name(k));
804 packet_put_string(blob, bloblen);
805 xfree(blob);
806 packet_send();
807 return 1;
808}
809
810static Key *
811load_identity_file(char *filename)
812{
813 Key *private;
814 char prompt[300], *passphrase;
815 int quit, i;
816 struct stat st;
817
818 if (stat(filename, &st) < 0) {
819 debug3("no such identity: %s", filename);
820 return NULL;
821 }
822 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL);
823 if (private == NULL) {
824 if (options.batch_mode)
825 return NULL;
826 snprintf(prompt, sizeof prompt,
1e608e42 827 "Enter passphrase for key '%.100s': ", filename);
3c0ef626 828 for (i = 0; i < options.number_of_password_prompts; i++) {
829 passphrase = read_passphrase(prompt, 0);
830 if (strcmp(passphrase, "") != 0) {
831 private = key_load_private_type(KEY_UNSPEC, filename,
832 passphrase, NULL);
833 quit = 0;
834 } else {
835 debug2("no passphrase given, try next key");
836 quit = 1;
837 }
838 memset(passphrase, 0, strlen(passphrase));
839 xfree(passphrase);
840 if (private != NULL || quit)
841 break;
842 debug2("bad passphrase given, try again...");
843 }
844 }
845 return private;
846}
847
848static int
1e608e42 849identity_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
850 u_char *data, u_int datalen)
3c0ef626 851{
852 Key *private;
853 int idx, ret;
854
855 idx = authctxt->last_key_hint;
856 if (idx < 0)
857 return -1;
858
859 /* private key is stored in external hardware */
1e608e42 860 if (options.identity_keys[idx]->flags & KEY_FLAG_EXT)
3c0ef626 861 return key_sign(options.identity_keys[idx], sigp, lenp, data, datalen);
862
863 private = load_identity_file(options.identity_files[idx]);
864 if (private == NULL)
865 return -1;
866 ret = key_sign(private, sigp, lenp, data, datalen);
867 key_free(private);
868 return ret;
869}
870
871static int
1e608e42 872agent_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
873 u_char *data, u_int datalen)
3c0ef626 874{
875 return ssh_agent_sign(authctxt->agent, key, sigp, lenp, data, datalen);
876}
877
878static int
1e608e42 879key_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
880 u_char *data, u_int datalen)
3c0ef626 881{
882 return key_sign(key, sigp, lenp, data, datalen);
883}
884
885static int
886userauth_pubkey_agent(Authctxt *authctxt)
887{
888 static int called = 0;
889 int ret = 0;
890 char *comment;
891 Key *k;
892
893 if (called == 0) {
894 if (ssh_get_num_identities(authctxt->agent, 2) == 0)
895 debug2("userauth_pubkey_agent: no keys at all");
896 called = 1;
897 }
898 k = ssh_get_next_identity(authctxt->agent, &comment, 2);
899 if (k == NULL) {
900 debug2("userauth_pubkey_agent: no more keys");
901 } else {
902 debug("userauth_pubkey_agent: testing agent key %s", comment);
903 xfree(comment);
904 ret = send_pubkey_test(authctxt, k, agent_sign_cb, -1);
905 if (ret == 0)
906 key_free(k);
907 }
908 if (ret == 0)
909 debug2("userauth_pubkey_agent: no message sent");
910 return ret;
911}
912
913int
914userauth_pubkey(Authctxt *authctxt)
915{
916 static int idx = 0;
917 int sent = 0;
918 Key *key;
919 char *filename;
920
921 if (authctxt->agent != NULL) {
922 do {
923 sent = userauth_pubkey_agent(authctxt);
1e608e42 924 } while (!sent && authctxt->agent->howmany > 0);
3c0ef626 925 }
926 while (!sent && idx < options.num_identity_files) {
927 key = options.identity_keys[idx];
928 filename = options.identity_files[idx];
929 if (key == NULL) {
930 debug("try privkey: %s", filename);
931 key = load_identity_file(filename);
932 if (key != NULL) {
933 sent = sign_and_send_pubkey(authctxt, key,
934 key_sign_cb);
935 key_free(key);
936 }
937 } else if (key->type != KEY_RSA1) {
938 debug("try pubkey: %s", filename);
939 sent = send_pubkey_test(authctxt, key,
940 identity_sign_cb, idx);
941 }
942 idx++;
943 }
944 return sent;
945}
946
947/*
948 * Send userauth request message specifying keyboard-interactive method.
949 */
950int
951userauth_kbdint(Authctxt *authctxt)
952{
953 static int attempt = 0;
954
955 if (attempt++ >= options.number_of_password_prompts)
956 return 0;
957 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
958 if (attempt > 1 && !authctxt->info_req_seen) {
959 debug3("userauth_kbdint: disable: no info_req_seen");
960 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
961 return 0;
962 }
963
964 debug2("userauth_kbdint");
965 packet_start(SSH2_MSG_USERAUTH_REQUEST);
966 packet_put_cstring(authctxt->server_user);
967 packet_put_cstring(authctxt->service);
968 packet_put_cstring(authctxt->method->name);
969 packet_put_cstring(""); /* lang */
970 packet_put_cstring(options.kbd_interactive_devices ?
971 options.kbd_interactive_devices : "");
972 packet_send();
973
974 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
975 return 1;
976}
977
978/*
979 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
980 */
981void
1e608e42 982input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
3c0ef626 983{
984 Authctxt *authctxt = ctxt;
985 char *name, *inst, *lang, *prompt, *response;
986 u_int num_prompts, i;
987 int echo = 0;
988
989 debug2("input_userauth_info_req");
990
991 if (authctxt == NULL)
992 fatal("input_userauth_info_req: no authentication context");
993
994 authctxt->info_req_seen = 1;
995
996 name = packet_get_string(NULL);
997 inst = packet_get_string(NULL);
998 lang = packet_get_string(NULL);
999 if (strlen(name) > 0)
1000 log("%s", name);
1001 if (strlen(inst) > 0)
1002 log("%s", inst);
1003 xfree(name);
1004 xfree(inst);
1005 xfree(lang);
1006
1007 num_prompts = packet_get_int();
1008 /*
1009 * Begin to build info response packet based on prompts requested.
1010 * We commit to providing the correct number of responses, so if
1011 * further on we run into a problem that prevents this, we have to
1012 * be sure and clean this up and send a correct error response.
1013 */
1014 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1015 packet_put_int(num_prompts);
1016
1017 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
1018 for (i = 0; i < num_prompts; i++) {
1019 prompt = packet_get_string(NULL);
1020 echo = packet_get_char();
1021
1022 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
1023
1024 packet_put_cstring(response);
1025 memset(response, 0, strlen(response));
1026 xfree(response);
1027 xfree(prompt);
1028 }
1e608e42 1029 packet_check_eom(); /* done with parsing incoming message. */
3c0ef626 1030
1031 packet_add_padding(64);
1032 packet_send();
1033}
1034
1035/*
1036 * this will be move to an external program (ssh-keysign) ASAP. ssh-keysign
1037 * will be setuid-root and the sbit can be removed from /usr/bin/ssh.
1038 */
1039int
1040userauth_hostbased(Authctxt *authctxt)
1041{
1042 Key *private = NULL;
1043 Buffer b;
1044 u_char *signature, *blob;
1045 char *chost, *pkalg, *p;
1046 const char *service;
1047 u_int blen, slen;
1048 int ok, i, len, found = 0;
1049
1050 /* check for a useful key */
1051 for (i = 0; i < authctxt->nkeys; i++) {
1052 private = authctxt->keys[i];
1053 if (private && private->type != KEY_RSA1) {
1054 found = 1;
1055 /* we take and free the key */
1056 authctxt->keys[i] = NULL;
1057 break;
1058 }
1059 }
1060 if (!found) {
1061 debug("userauth_hostbased: no more client hostkeys");
1062 return 0;
1063 }
1064 if (key_to_blob(private, &blob, &blen) == 0) {
1065 key_free(private);
1066 return 0;
1067 }
1068 /* figure out a name for the client host */
1069 p = get_local_name(packet_get_connection_in());
1070 if (p == NULL) {
1071 error("userauth_hostbased: cannot get local ipaddr/name");
1072 key_free(private);
1073 return 0;
1074 }
1075 len = strlen(p) + 2;
1076 chost = xmalloc(len);
1077 strlcpy(chost, p, len);
1078 strlcat(chost, ".", len);
1079 debug2("userauth_hostbased: chost %s", chost);
1080
1081 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1082 authctxt->service;
1083 pkalg = xstrdup(key_ssh_name(private));
1084 buffer_init(&b);
1085 /* construct data */
1086 buffer_put_string(&b, session_id2, session_id2_len);
1087 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1088 buffer_put_cstring(&b, authctxt->server_user);
1089 buffer_put_cstring(&b, service);
1090 buffer_put_cstring(&b, authctxt->method->name);
1091 buffer_put_cstring(&b, pkalg);
1092 buffer_put_string(&b, blob, blen);
1093 buffer_put_cstring(&b, chost);
1094 buffer_put_cstring(&b, authctxt->local_user);
1095#ifdef DEBUG_PK
1096 buffer_dump(&b);
1097#endif
1098 ok = key_sign(private, &signature, &slen, buffer_ptr(&b), buffer_len(&b));
1099 key_free(private);
1100 buffer_free(&b);
1101 if (ok != 0) {
1102 error("key_sign failed");
1103 xfree(chost);
1104 xfree(pkalg);
1105 return 0;
1106 }
1107 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1108 packet_put_cstring(authctxt->server_user);
1109 packet_put_cstring(authctxt->service);
1110 packet_put_cstring(authctxt->method->name);
1111 packet_put_cstring(pkalg);
1112 packet_put_string(blob, blen);
1113 packet_put_cstring(chost);
1114 packet_put_cstring(authctxt->local_user);
1115 packet_put_string(signature, slen);
1116 memset(signature, 's', slen);
1117 xfree(signature);
1118 xfree(chost);
1119 xfree(pkalg);
1120
1121 packet_send();
1122 return 1;
1123}
1124
1125/* find auth method */
1126
1127/*
1128 * given auth method name, if configurable options permit this method fill
1129 * in auth_ident field and return true, otherwise return false.
1130 */
1131static int
1132authmethod_is_enabled(Authmethod *method)
1133{
1134 if (method == NULL)
1135 return 0;
1136 /* return false if options indicate this method is disabled */
1137 if (method->enabled == NULL || *method->enabled == 0)
1138 return 0;
1139 /* return false if batch mode is enabled but method needs interactive mode */
1140 if (method->batch_flag != NULL && *method->batch_flag != 0)
1141 return 0;
1142 return 1;
1143}
1144
1145static Authmethod *
1146authmethod_lookup(const char *name)
1147{
1148 Authmethod *method = NULL;
1149 if (name != NULL)
1150 for (method = authmethods; method->name != NULL; method++)
1151 if (strcmp(name, method->name) == 0)
1152 return method;
1153 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1154 return NULL;
1155}
1156
1157/* XXX internal state */
1158static Authmethod *current = NULL;
1159static char *supported = NULL;
1160static char *preferred = NULL;
1161/*
1162 * Given the authentication method list sent by the server, return the
1163 * next method we should try. If the server initially sends a nil list,
1164 * use a built-in default list.
1165 */
1166static Authmethod *
1167authmethod_get(char *authlist)
1168{
1169
1170 char *name = NULL;
1e608e42 1171 u_int next;
3c0ef626 1172
1173 /* Use a suitable default if we're passed a nil list. */
1174 if (authlist == NULL || strlen(authlist) == 0)
1175 authlist = options.preferred_authentications;
1176
1177 if (supported == NULL || strcmp(authlist, supported) != 0) {
1178 debug3("start over, passed a different list %s", authlist);
1179 if (supported != NULL)
1180 xfree(supported);
1181 supported = xstrdup(authlist);
1182 preferred = options.preferred_authentications;
1183 debug3("preferred %s", preferred);
1184 current = NULL;
1185 } else if (current != NULL && authmethod_is_enabled(current))
1186 return current;
1187
1188 for (;;) {
1189 if ((name = match_list(preferred, supported, &next)) == NULL) {
1190 debug("no more auth methods to try");
1191 current = NULL;
1192 return NULL;
1193 }
1194 preferred += next;
1195 debug3("authmethod_lookup %s", name);
1196 debug3("remaining preferred: %s", preferred);
1197 if ((current = authmethod_lookup(name)) != NULL &&
1198 authmethod_is_enabled(current)) {
1199 debug3("authmethod_is_enabled %s", name);
1200 debug("next auth method to try is %s", name);
1201 return current;
1202 }
1203 }
1204}
1205
3c0ef626 1206static char *
1207authmethods_get(void)
1208{
1209 Authmethod *method = NULL;
1e608e42 1210 Buffer b;
1211 char *list;
3c0ef626 1212
1e608e42 1213 buffer_init(&b);
3c0ef626 1214 for (method = authmethods; method->name != NULL; method++) {
1215 if (authmethod_is_enabled(method)) {
1e608e42 1216 if (buffer_len(&b) > 0)
1217 buffer_append(&b, ",", 1);
1218 buffer_append(&b, method->name, strlen(method->name));
3c0ef626 1219 }
1220 }
1e608e42 1221 buffer_append(&b, "\0", 1);
1222 list = xstrdup(buffer_ptr(&b));
1223 buffer_free(&b);
1224 return list;
3c0ef626 1225}
This page took 0.695195 seconds and 5 git commands to generate.