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