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