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