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