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