]> andersk Git - openssh.git/blob - sshconnect2.c
4ed39a23ee641eab3990a1748ff334c54708b62f
[openssh.git] / sshconnect2.c
1 /*
2  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  */
24
25 #include "includes.h"
26 RCSID("$OpenBSD: sshconnect2.c,v 1.61 2001/04/03 19:53:29 markus Exp $");
27
28 #include <openssl/bn.h>
29 #include <openssl/md5.h>
30 #include <openssl/dh.h>
31 #include <openssl/hmac.h>
32
33 #include "ssh.h"
34 #include "ssh2.h"
35 #include "xmalloc.h"
36 #include "rsa.h"
37 #include "buffer.h"
38 #include "packet.h"
39 #include "uidswap.h"
40 #include "compat.h"
41 #include "bufaux.h"
42 #include "cipher.h"
43 #include "kex.h"
44 #include "myproposal.h"
45 #include "key.h"
46 #include "sshconnect.h"
47 #include "authfile.h"
48 #include "cli.h"
49 #include "dh.h"
50 #include "authfd.h"
51 #include "log.h"
52 #include "readconf.h"
53 #include "readpass.h"
54 #include "match.h"
55 #include "dispatch.h"
56
57 /* import */
58 extern char *client_version_string;
59 extern char *server_version_string;
60 extern Options options;
61
62 /*
63  * SSH2 key exchange
64  */
65
66 u_char *session_id2 = NULL;
67 int session_id2_len = 0;
68
69 char *xxx_host;
70 struct sockaddr *xxx_hostaddr;
71
72 int
73 check_host_key_callback(Key *hostkey)
74 {
75         check_host_key(xxx_host, xxx_hostaddr, hostkey,
76             options.user_hostfile2, options.system_hostfile2);
77         return 0;
78 }
79
80 void
81 ssh_kex2(char *host, struct sockaddr *hostaddr)
82 {
83         Kex *kex;
84
85         xxx_host = host;
86         xxx_hostaddr = hostaddr;
87
88         if (options.ciphers == (char *)-1) {
89                 log("No valid ciphers for protocol version 2 given, using defaults.");
90                 options.ciphers = NULL;
91         }
92         if (options.ciphers != NULL) {
93                 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
94                 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
95         }
96         myproposal[PROPOSAL_ENC_ALGS_CTOS] =
97             compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
98         myproposal[PROPOSAL_ENC_ALGS_STOC] =
99             compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
100         if (options.compression) {
101                 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
102                 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib";
103         } else {
104                 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
105                 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none";
106         }
107         if (options.macs != NULL) {
108                 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
109                 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
110         }
111
112         kex = kex_start(myproposal);
113         kex->client_version_string=client_version_string;
114         kex->server_version_string=server_version_string;
115         kex->check_host_key=&check_host_key_callback;
116
117         /* start key exchange */
118         dispatch_run(DISPATCH_BLOCK, &kex->newkeys, kex);
119
120 #ifdef DEBUG_KEXDH
121         /* send 1st encrypted/maced/compressed message */
122         packet_start(SSH2_MSG_IGNORE);
123         packet_put_cstring("markus");
124         packet_send();
125         packet_write_wait();
126 #endif
127         debug("done: ssh_kex2.");
128 }
129
130 /*
131  * Authenticate user
132  */
133
134 typedef struct Authctxt Authctxt;
135 typedef struct Authmethod Authmethod;
136
137 typedef int sign_cb_fn(
138     Authctxt *authctxt, Key *key,
139     u_char **sigp, int *lenp, u_char *data, int datalen);
140
141 struct Authctxt {
142         const char *server_user;
143         const char *host;
144         const char *service;
145         AuthenticationConnection *agent;
146         Authmethod *method;
147         int success;
148         char *authlist;
149         Key *last_key;
150         sign_cb_fn *last_key_sign;
151         int last_key_hint;
152 };
153 struct Authmethod {
154         char    *name;          /* string to compare against server's list */
155         int     (*userauth)(Authctxt *authctxt);
156         int     *enabled;       /* flag in option struct that enables method */
157         int     *batch_flag;    /* flag in option struct that disables method */
158 };
159
160 void    input_userauth_success(int type, int plen, void *ctxt);
161 void    input_userauth_failure(int type, int plen, void *ctxt);
162 void    input_userauth_banner(int type, int plen, void *ctxt);
163 void    input_userauth_error(int type, int plen, void *ctxt);
164 void    input_userauth_info_req(int type, int plen, void *ctxt);
165 void    input_userauth_pk_ok(int type, int plen, void *ctxt);
166
167 int     userauth_none(Authctxt *authctxt);
168 int     userauth_pubkey(Authctxt *authctxt);
169 int     userauth_passwd(Authctxt *authctxt);
170 int     userauth_kbdint(Authctxt *authctxt);
171
172 void    userauth(Authctxt *authctxt, char *authlist);
173
174 int
175 sign_and_send_pubkey(Authctxt *authctxt, Key *k,
176     sign_cb_fn *sign_callback);
177 void    clear_auth_state(Authctxt *authctxt);
178
179 Authmethod *authmethod_get(char *authlist);
180 Authmethod *authmethod_lookup(const char *name);
181 char *authmethods_get(void);
182
183 Authmethod authmethods[] = {
184         {"publickey",
185                 userauth_pubkey,
186                 &options.pubkey_authentication,
187                 NULL},
188         {"password",
189                 userauth_passwd,
190                 &options.password_authentication,
191                 &options.batch_mode},
192         {"keyboard-interactive",
193                 userauth_kbdint,
194                 &options.kbd_interactive_authentication,
195                 &options.batch_mode},
196         {"none",
197                 userauth_none,
198                 NULL,
199                 NULL},
200         {NULL, NULL, NULL, NULL}
201 };
202
203 void
204 ssh_userauth2(const char *server_user, char *host)
205 {
206         Authctxt authctxt;
207         int type;
208         int plen;
209         int i;
210
211         if (options.challenge_reponse_authentication)
212                 options.kbd_interactive_authentication = 1;
213
214         debug("send SSH2_MSG_SERVICE_REQUEST");
215         packet_start(SSH2_MSG_SERVICE_REQUEST);
216         packet_put_cstring("ssh-userauth");
217         packet_send();
218         packet_write_wait();
219         type = packet_read(&plen);
220         if (type != SSH2_MSG_SERVICE_ACCEPT) {
221                 fatal("denied SSH2_MSG_SERVICE_ACCEPT: %d", type);
222         }
223         if (packet_remaining() > 0) {
224                 char *reply = packet_get_string(&plen);
225                 debug("service_accept: %s", reply);
226                 xfree(reply);
227         } else {
228                 debug("buggy server: service_accept w/o service");
229         }
230         packet_done();
231         debug("got SSH2_MSG_SERVICE_ACCEPT");
232
233         if (options.preferred_authentications == NULL)
234                 options.preferred_authentications = authmethods_get();
235
236         /* setup authentication context */
237         authctxt.agent = ssh_get_authentication_connection();
238         authctxt.server_user = server_user;
239         authctxt.host = host;
240         authctxt.service = "ssh-connection";            /* service name */
241         authctxt.success = 0;
242         authctxt.method = authmethod_lookup("none");
243         authctxt.authlist = NULL;
244         if (authctxt.method == NULL)
245                 fatal("ssh_userauth2: internal error: cannot send userauth none request");
246
247         /* initial userauth request */
248         userauth_none(&authctxt);
249
250         //dispatch_init(&input_userauth_error);
251         for (i = 50; i <= 254; i++) {
252                 dispatch_set(i, &input_userauth_error);
253         }
254         dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
255         dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
256         dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
257         dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt);     /* loop until success */
258
259         if (authctxt.agent != NULL)
260                 ssh_close_authentication_connection(authctxt.agent);
261
262         debug("ssh-userauth2 successful: method %s", authctxt.method->name);
263 }
264 void
265 userauth(Authctxt *authctxt, char *authlist)
266 {
267         if (authlist == NULL) {
268                 authlist = authctxt->authlist;
269         } else {
270                 if (authctxt->authlist)
271                         xfree(authctxt->authlist);
272                 authctxt->authlist = authlist;
273         }
274         for (;;) {
275                 Authmethod *method = authmethod_get(authlist);
276                 if (method == NULL)
277                         fatal("Permission denied (%s).", authlist);
278                 authctxt->method = method;
279                 if (method->userauth(authctxt) != 0) {
280                         debug2("we sent a %s packet, wait for reply", method->name);
281                         break;
282                 } else {
283                         debug2("we did not send a packet, disable method");
284                         method->enabled = NULL;
285                 }
286         }
287 }
288 void
289 input_userauth_error(int type, int plen, void *ctxt)
290 {
291         fatal("input_userauth_error: bad message during authentication: "
292            "type %d", type);
293 }
294 void
295 input_userauth_banner(int type, int plen, void *ctxt)
296 {
297         char *msg, *lang;
298         debug3("input_userauth_banner");
299         msg = packet_get_string(NULL);
300         lang = packet_get_string(NULL);
301         fprintf(stderr, "%s", msg);
302         xfree(msg);
303         xfree(lang);
304 }
305 void
306 input_userauth_success(int type, int plen, void *ctxt)
307 {
308         Authctxt *authctxt = ctxt;
309         if (authctxt == NULL)
310                 fatal("input_userauth_success: no authentication context");
311         if (authctxt->authlist)
312                 xfree(authctxt->authlist);
313         clear_auth_state(authctxt);
314         authctxt->success = 1;                  /* break out */
315 }
316 void
317 input_userauth_failure(int type, int plen, void *ctxt)
318 {
319         Authctxt *authctxt = ctxt;
320         char *authlist = NULL;
321         int partial;
322
323         if (authctxt == NULL)
324                 fatal("input_userauth_failure: no authentication context");
325
326         authlist = packet_get_string(NULL);
327         partial = packet_get_char();
328         packet_done();
329
330         if (partial != 0)
331                 log("Authenticated with partial success.");
332         debug("authentications that can continue: %s", authlist);
333
334         clear_auth_state(authctxt);
335         userauth(authctxt, authlist);
336 }
337 void
338 input_userauth_pk_ok(int type, int plen, void *ctxt)
339 {
340         Authctxt *authctxt = ctxt;
341         Key *key = NULL;
342         Buffer b;
343         int alen, blen, pktype, sent = 0;
344         char *pkalg, *pkblob, *fp;
345
346         if (authctxt == NULL)
347                 fatal("input_userauth_pk_ok: no authentication context");
348         if (datafellows & SSH_BUG_PKOK) {
349                 /* this is similar to SSH_BUG_PKAUTH */
350                 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
351                 pkblob = packet_get_string(&blen);
352                 buffer_init(&b);
353                 buffer_append(&b, pkblob, blen);
354                 pkalg = buffer_get_string(&b, &alen);
355                 buffer_free(&b);
356         } else {
357                 pkalg = packet_get_string(&alen);
358                 pkblob = packet_get_string(&blen);
359         }
360         packet_done();
361
362         debug("input_userauth_pk_ok: pkalg %s blen %d lastkey %p hint %d",
363             pkalg, blen, authctxt->last_key, authctxt->last_key_hint);
364
365         do {
366                 if (authctxt->last_key == NULL ||
367                     authctxt->last_key_sign == NULL) {
368                         debug("no last key or no sign cb");
369                         break;
370                 }
371                 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
372                         debug("unknown pkalg %s", pkalg);
373                         break;
374                 }
375                 if ((key = key_from_blob(pkblob, blen)) == NULL) {
376                         debug("no key from blob. pkalg %s", pkalg);
377                         break;
378                 }
379                 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
380                 debug2("input_userauth_pk_ok: fp %s", fp);
381                 xfree(fp);
382                 if (!key_equal(key, authctxt->last_key)) {
383                         debug("key != last_key");
384                         break;
385                 }
386                 sent = sign_and_send_pubkey(authctxt, key,
387                    authctxt->last_key_sign);
388         } while(0);
389
390         if (key != NULL)
391                 key_free(key);
392         xfree(pkalg);
393         xfree(pkblob);
394
395         /* unregister */
396         clear_auth_state(authctxt);
397         dispatch_set(SSH2_MSG_USERAUTH_PK_OK, NULL);
398
399         /* try another method if we did not send a packet*/
400         if (sent == 0)
401                 userauth(authctxt, NULL);
402
403 }
404
405 int
406 userauth_none(Authctxt *authctxt)
407 {
408         /* initial userauth request */
409         packet_start(SSH2_MSG_USERAUTH_REQUEST);
410         packet_put_cstring(authctxt->server_user);
411         packet_put_cstring(authctxt->service);
412         packet_put_cstring(authctxt->method->name);
413         packet_send();
414         return 1;
415 }
416
417 int
418 userauth_passwd(Authctxt *authctxt)
419 {
420         static int attempt = 0;
421         char prompt[80];
422         char *password;
423
424         if (attempt++ >= options.number_of_password_prompts)
425                 return 0;
426
427         if(attempt != 1)
428                 error("Permission denied, please try again.");
429
430         snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
431             authctxt->server_user, authctxt->host);
432         password = read_passphrase(prompt, 0);
433         packet_start(SSH2_MSG_USERAUTH_REQUEST);
434         packet_put_cstring(authctxt->server_user);
435         packet_put_cstring(authctxt->service);
436         packet_put_cstring(authctxt->method->name);
437         packet_put_char(0);
438         packet_put_cstring(password);
439         memset(password, 0, strlen(password));
440         xfree(password);
441         packet_inject_ignore(64);
442         packet_send();
443         return 1;
444 }
445
446 void
447 clear_auth_state(Authctxt *authctxt)
448 {
449         /* XXX clear authentication state */
450         if (authctxt->last_key != NULL && authctxt->last_key_hint == -1) {
451                 debug3("clear_auth_state: key_free %p", authctxt->last_key);
452                 key_free(authctxt->last_key);
453         }
454         authctxt->last_key = NULL;
455         authctxt->last_key_hint = -2;
456         authctxt->last_key_sign = NULL;
457 }
458
459 int
460 sign_and_send_pubkey(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback)
461 {
462         Buffer b;
463         u_char *blob, *signature;
464         int bloblen, slen;
465         int skip = 0;
466         int ret = -1;
467         int have_sig = 1;
468
469         debug3("sign_and_send_pubkey");
470
471         if (key_to_blob(k, &blob, &bloblen) == 0) {
472                 /* we cannot handle this key */
473                 debug3("sign_and_send_pubkey: cannot handle key");
474                 return 0;
475         }
476         /* data to be signed */
477         buffer_init(&b);
478         if (datafellows & SSH_OLD_SESSIONID) {
479                 buffer_append(&b, session_id2, session_id2_len);
480                 skip = session_id2_len;
481         } else {
482                 buffer_put_string(&b, session_id2, session_id2_len);
483                 skip = buffer_len(&b);
484         }
485         buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
486         buffer_put_cstring(&b, authctxt->server_user);
487         buffer_put_cstring(&b,
488             datafellows & SSH_BUG_PKSERVICE ?
489             "ssh-userauth" :
490             authctxt->service);
491         if (datafellows & SSH_BUG_PKAUTH) {
492                 buffer_put_char(&b, have_sig);
493         } else {
494                 buffer_put_cstring(&b, authctxt->method->name);
495                 buffer_put_char(&b, have_sig);
496                 buffer_put_cstring(&b, key_ssh_name(k));
497         }
498         buffer_put_string(&b, blob, bloblen);
499
500         /* generate signature */
501         ret = (*sign_callback)(authctxt, k, &signature, &slen,
502             buffer_ptr(&b), buffer_len(&b));
503         if (ret == -1) {
504                 xfree(blob);
505                 buffer_free(&b);
506                 return 0;
507         }
508 #ifdef DEBUG_PK
509         buffer_dump(&b);
510 #endif
511         if (datafellows & SSH_BUG_PKSERVICE) {
512                 buffer_clear(&b);
513                 buffer_append(&b, session_id2, session_id2_len);
514                 skip = session_id2_len;
515                 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
516                 buffer_put_cstring(&b, authctxt->server_user);
517                 buffer_put_cstring(&b, authctxt->service);
518                 buffer_put_cstring(&b, authctxt->method->name);
519                 buffer_put_char(&b, have_sig);
520                 if (!(datafellows & SSH_BUG_PKAUTH))
521                         buffer_put_cstring(&b, key_ssh_name(k));
522                 buffer_put_string(&b, blob, bloblen);
523         }
524         xfree(blob);
525
526         /* append signature */
527         buffer_put_string(&b, signature, slen);
528         xfree(signature);
529
530         /* skip session id and packet type */
531         if (buffer_len(&b) < skip + 1)
532                 fatal("userauth_pubkey: internal error");
533         buffer_consume(&b, skip + 1);
534
535         /* put remaining data from buffer into packet */
536         packet_start(SSH2_MSG_USERAUTH_REQUEST);
537         packet_put_raw(buffer_ptr(&b), buffer_len(&b));
538         buffer_free(&b);
539         packet_send();
540
541         return 1;
542 }
543
544 int
545 send_pubkey_test(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback,
546     int hint)
547 {
548         u_char *blob;
549         int bloblen, have_sig = 0;
550
551         debug3("send_pubkey_test");
552
553         if (key_to_blob(k, &blob, &bloblen) == 0) {
554                 /* we cannot handle this key */
555                 debug3("send_pubkey_test: cannot handle key");
556                 return 0;
557         }
558         /* register callback for USERAUTH_PK_OK message */
559         authctxt->last_key_sign = sign_callback;
560         authctxt->last_key_hint = hint;
561         authctxt->last_key = k;
562         dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
563
564         packet_start(SSH2_MSG_USERAUTH_REQUEST);
565         packet_put_cstring(authctxt->server_user);
566         packet_put_cstring(authctxt->service);
567         packet_put_cstring(authctxt->method->name);
568         packet_put_char(have_sig);
569         if (!(datafellows & SSH_BUG_PKAUTH))
570                 packet_put_cstring(key_ssh_name(k));
571         packet_put_string(blob, bloblen);
572         xfree(blob);
573         packet_send();
574         return 1;
575 }
576
577 Key *
578 load_identity_file(char *filename)
579 {
580         Key *private;
581         char prompt[300], *passphrase;
582         int quit, i;
583         struct stat st;
584
585         if (stat(filename, &st) < 0) {
586                 debug3("no such identity: %s", filename);
587                 return NULL;
588         }
589         private = key_load_private_type(KEY_UNSPEC, filename, "", NULL);
590         if (private == NULL) {
591                 if (options.batch_mode)
592                         return NULL;
593                 snprintf(prompt, sizeof prompt,
594                      "Enter passphrase for key '%.100s': ", filename);
595                 for (i = 0; i < options.number_of_password_prompts; i++) {
596                         passphrase = read_passphrase(prompt, 0);
597                         if (strcmp(passphrase, "") != 0) {
598                                 private = key_load_private_type(KEY_UNSPEC, filename,
599                                     passphrase, NULL);
600                                 quit = 0;
601                         } else {
602                                 debug2("no passphrase given, try next key");
603                                 quit = 1;
604                         }
605                         memset(passphrase, 0, strlen(passphrase));
606                         xfree(passphrase);
607                         if (private != NULL || quit)
608                                 break;
609                         debug2("bad passphrase given, try again...");
610                 }
611         }
612         return private;
613 }
614
615 int
616 identity_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
617     u_char *data, int datalen)
618 {
619         Key *private;
620         int idx, ret;
621
622         idx = authctxt->last_key_hint;
623         if (idx < 0)
624                 return -1;
625         private = load_identity_file(options.identity_files[idx]);
626         if (private == NULL)
627                 return -1;
628         ret = key_sign(private, sigp, lenp, data, datalen);
629         key_free(private);
630         return ret;
631 }
632
633 int agent_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
634     u_char *data, int datalen)
635 {
636         return ssh_agent_sign(authctxt->agent, key, sigp, lenp, data, datalen);
637 }
638
639 int key_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
640     u_char *data, int datalen)
641 {
642         return key_sign(key, sigp, lenp, data, datalen);
643 }
644
645 int
646 userauth_pubkey_agent(Authctxt *authctxt)
647 {
648         static int called = 0;
649         int ret = 0;
650         char *comment;
651         Key *k;
652
653         if (called == 0) {
654                 if (ssh_get_num_identities(authctxt->agent, 2) == 0)
655                         debug2("userauth_pubkey_agent: no keys at all");
656                 called = 1;
657         }
658         k = ssh_get_next_identity(authctxt->agent, &comment, 2);
659         if (k == NULL) {
660                 debug2("userauth_pubkey_agent: no more keys");
661         } else {
662                 debug("userauth_pubkey_agent: testing agent key %s", comment);
663                 xfree(comment);
664                 ret = send_pubkey_test(authctxt, k, agent_sign_cb, -1);
665                 if (ret == 0)
666                         key_free(k);
667         }
668         if (ret == 0)
669                 debug2("userauth_pubkey_agent: no message sent");
670         return ret;
671 }
672
673 int
674 userauth_pubkey(Authctxt *authctxt)
675 {
676         static int idx = 0;
677         int sent = 0;
678         Key *key;
679         char *filename;
680
681         if (authctxt->agent != NULL) {
682                 do {
683                         sent = userauth_pubkey_agent(authctxt);
684                 } while(!sent && authctxt->agent->howmany > 0);
685         }
686         while (!sent && idx < options.num_identity_files) {
687                 key = options.identity_keys[idx];
688                 filename = options.identity_files[idx];
689                 if (key == NULL) {
690                         debug("try privkey: %s", filename);
691                         key = load_identity_file(filename);
692                         if (key != NULL) {
693                                 sent = sign_and_send_pubkey(authctxt, key,
694                                     key_sign_cb);
695                                 key_free(key);
696                         }
697                 } else if (key->type != KEY_RSA1) {
698                         debug("try pubkey: %s", filename);
699                         sent = send_pubkey_test(authctxt, key,
700                             identity_sign_cb, idx);
701                 }
702                 idx++;
703         }
704         return sent;
705 }
706
707 /*
708  * Send userauth request message specifying keyboard-interactive method.
709  */
710 int
711 userauth_kbdint(Authctxt *authctxt)
712 {
713         static int attempt = 0;
714
715         if (attempt++ >= options.number_of_password_prompts)
716                 return 0;
717
718         debug2("userauth_kbdint");
719         packet_start(SSH2_MSG_USERAUTH_REQUEST);
720         packet_put_cstring(authctxt->server_user);
721         packet_put_cstring(authctxt->service);
722         packet_put_cstring(authctxt->method->name);
723         packet_put_cstring("");                                 /* lang */
724         packet_put_cstring(options.kbd_interactive_devices ?
725             options.kbd_interactive_devices : "");
726         packet_send();
727
728         dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
729         return 1;
730 }
731
732 /*
733  * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
734  */
735 void
736 input_userauth_info_req(int type, int plen, void *ctxt)
737 {
738         Authctxt *authctxt = ctxt;
739         char *name, *inst, *lang, *prompt, *response;
740         u_int num_prompts, i;
741         int echo = 0;
742
743         debug2("input_userauth_info_req");
744
745         if (authctxt == NULL)
746                 fatal("input_userauth_info_req: no authentication context");
747
748         name = packet_get_string(NULL);
749         inst = packet_get_string(NULL);
750         lang = packet_get_string(NULL);
751         if (strlen(name) > 0)
752                 cli_mesg(name);
753         if (strlen(inst) > 0)
754                 cli_mesg(inst);
755         xfree(name);
756         xfree(inst);
757         xfree(lang);
758
759         num_prompts = packet_get_int();
760         /*
761          * Begin to build info response packet based on prompts requested.
762          * We commit to providing the correct number of responses, so if
763          * further on we run into a problem that prevents this, we have to
764          * be sure and clean this up and send a correct error response.
765          */
766         packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
767         packet_put_int(num_prompts);
768
769         for (i = 0; i < num_prompts; i++) {
770                 prompt = packet_get_string(NULL);
771                 echo = packet_get_char();
772
773                 response = cli_prompt(prompt, echo);
774
775                 packet_put_cstring(response);
776                 memset(response, 0, strlen(response));
777                 xfree(response);
778                 xfree(prompt);
779         }
780         packet_done(); /* done with parsing incoming message. */
781
782         packet_inject_ignore(64);
783         packet_send();
784 }
785
786 /* find auth method */
787
788 /*
789  * given auth method name, if configurable options permit this method fill
790  * in auth_ident field and return true, otherwise return false.
791  */
792 int
793 authmethod_is_enabled(Authmethod *method)
794 {
795         if (method == NULL)
796                 return 0;
797         /* return false if options indicate this method is disabled */
798         if  (method->enabled == NULL || *method->enabled == 0)
799                 return 0;
800         /* return false if batch mode is enabled but method needs interactive mode */
801         if  (method->batch_flag != NULL && *method->batch_flag != 0)
802                 return 0;
803         return 1;
804 }
805
806 Authmethod *
807 authmethod_lookup(const char *name)
808 {
809         Authmethod *method = NULL;
810         if (name != NULL)
811                 for (method = authmethods; method->name != NULL; method++)
812                         if (strcmp(name, method->name) == 0)
813                                 return method;
814         debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
815         return NULL;
816 }
817
818 /* XXX internal state */
819 static Authmethod *current = NULL;
820 static char *supported = NULL;
821 static char *preferred = NULL;
822 /*
823  * Given the authentication method list sent by the server, return the
824  * next method we should try.  If the server initially sends a nil list,
825  * use a built-in default list. 
826  */
827 Authmethod *
828 authmethod_get(char *authlist)
829 {
830
831         char *name = NULL;
832         int next;
833
834         /* Use a suitable default if we're passed a nil list.  */
835         if (authlist == NULL || strlen(authlist) == 0)
836                 authlist = options.preferred_authentications;
837
838         if (supported == NULL || strcmp(authlist, supported) != 0) {
839                 debug3("start over, passed a different list %s", authlist);
840                 if (supported != NULL)
841                         xfree(supported);
842                 supported = xstrdup(authlist);
843                 preferred = options.preferred_authentications;
844                 debug3("preferred %s", preferred);
845                 current = NULL;
846         } else if (current != NULL && authmethod_is_enabled(current))
847                 return current;
848
849         for (;;) {
850                 if ((name = match_list(preferred, supported, &next)) == NULL) {
851                         debug("no more auth methods to try");
852                         current = NULL;
853                         return NULL;
854                 }
855                 preferred += next;
856                 debug3("authmethod_lookup %s", name);
857                 debug3("remaining preferred: %s", preferred);
858                 if ((current = authmethod_lookup(name)) != NULL &&
859                     authmethod_is_enabled(current)) {
860                         debug3("authmethod_is_enabled %s", name);
861                         debug("next auth method to try is %s", name);
862                         return current;
863                 }
864         }
865 }
866
867
868 #define DELIM   ","
869 char *
870 authmethods_get(void)
871 {
872         Authmethod *method = NULL;
873         char buf[1024];
874
875         buf[0] = '\0';
876         for (method = authmethods; method->name != NULL; method++) {
877                 if (authmethod_is_enabled(method)) {
878                         if (buf[0] != '\0')
879                                 strlcat(buf, DELIM, sizeof buf);
880                         strlcat(buf, method->name, sizeof buf);
881                 }
882         }
883         return xstrdup(buf);
884 }
This page took 0.290343 seconds and 3 git commands to generate.