]> andersk Git - openssh.git/blob - sshconnect2.c
- markus@cvs.openbsd.org 2002/01/25 22:07:40
[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.94 2002/01/25 21:00:24 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 /* import */
50 extern char *client_version_string;
51 extern char *server_version_string;
52 extern Options options;
53
54 /*
55  * SSH2 key exchange
56  */
57
58 u_char *session_id2 = NULL;
59 int session_id2_len = 0;
60
61 char *xxx_host;
62 struct sockaddr *xxx_hostaddr;
63
64 Kex *xxx_kex = NULL;
65
66 static int
67 verify_host_key_callback(Key *hostkey)
68 {
69         if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
70                 fatal("Host key verification failed.");
71         return 0;
72 }
73
74 void
75 ssh_kex2(char *host, struct sockaddr *hostaddr)
76 {
77         Kex *kex;
78
79         xxx_host = host;
80         xxx_hostaddr = hostaddr;
81
82         if (options.ciphers == (char *)-1) {
83                 log("No valid ciphers for protocol version 2 given, using defaults.");
84                 options.ciphers = NULL;
85         }
86         if (options.ciphers != NULL) {
87                 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
88                 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
89         }
90         myproposal[PROPOSAL_ENC_ALGS_CTOS] =
91             compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
92         myproposal[PROPOSAL_ENC_ALGS_STOC] =
93             compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
94         if (options.compression) {
95                 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
96                 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib";
97         } else {
98                 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
99                 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none";
100         }
101         if (options.macs != NULL) {
102                 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
103                 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
104         }
105         if (options.hostkeyalgorithms != NULL)
106                 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
107                     options.hostkeyalgorithms;
108
109         /* start key exchange */
110         kex = kex_setup(myproposal);
111         kex->client_version_string=client_version_string;
112         kex->server_version_string=server_version_string;
113         kex->verify_host_key=&verify_host_key_callback;
114
115         xxx_kex = kex;
116
117         dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
118
119         session_id2 = kex->session_id;
120         session_id2_len = kex->session_id_len;
121
122 #ifdef DEBUG_KEXDH
123         /* send 1st encrypted/maced/compressed message */
124         packet_start(SSH2_MSG_IGNORE);
125         packet_put_cstring("markus");
126         packet_send();
127         packet_write_wait();
128 #endif
129         debug("done: ssh_kex2.");
130 }
131
132 /*
133  * Authenticate user
134  */
135
136 typedef struct Authctxt Authctxt;
137 typedef struct Authmethod Authmethod;
138
139 typedef int sign_cb_fn(
140     Authctxt *authctxt, Key *key,
141     u_char **sigp, int *lenp, u_char *data, int datalen);
142
143 struct Authctxt {
144         const char *server_user;
145         const char *local_user;
146         const char *host;
147         const char *service;
148         Authmethod *method;
149         int success;
150         char *authlist;
151         /* pubkey */
152         Key *last_key;
153         sign_cb_fn *last_key_sign;
154         int last_key_hint;
155         AuthenticationConnection *agent;
156         /* hostbased */
157         Key **keys;
158         int nkeys;
159         /* kbd-interactive */
160         int info_req_seen;
161 };
162 struct Authmethod {
163         char    *name;          /* string to compare against server's list */
164         int     (*userauth)(Authctxt *authctxt);
165         int     *enabled;       /* flag in option struct that enables method */
166         int     *batch_flag;    /* flag in option struct that disables method */
167 };
168
169 void    input_userauth_success(int, u_int32_t, void *);
170 void    input_userauth_failure(int, u_int32_t, void *);
171 void    input_userauth_banner(int, u_int32_t, void *);
172 void    input_userauth_error(int, u_int32_t, void *);
173 void    input_userauth_info_req(int, u_int32_t, void *);
174 void    input_userauth_pk_ok(int, u_int32_t, void *);
175
176 int     userauth_none(Authctxt *);
177 int     userauth_pubkey(Authctxt *);
178 int     userauth_passwd(Authctxt *);
179 int     userauth_kbdint(Authctxt *);
180 int     userauth_hostbased(Authctxt *);
181
182 void    userauth(Authctxt *, char *);
183
184 static int sign_and_send_pubkey(Authctxt *, Key *, sign_cb_fn *);
185 static void clear_auth_state(Authctxt *);
186
187 static Authmethod *authmethod_get(char *authlist);
188 static Authmethod *authmethod_lookup(const char *name);
189 static char *authmethods_get(void);
190
191 Authmethod authmethods[] = {
192         {"hostbased",
193                 userauth_hostbased,
194                 &options.hostbased_authentication,
195                 NULL},
196         {"publickey",
197                 userauth_pubkey,
198                 &options.pubkey_authentication,
199                 NULL},
200         {"keyboard-interactive",
201                 userauth_kbdint,
202                 &options.kbd_interactive_authentication,
203                 &options.batch_mode},
204         {"password",
205                 userauth_passwd,
206                 &options.password_authentication,
207                 &options.batch_mode},
208         {"none",
209                 userauth_none,
210                 NULL,
211                 NULL},
212         {NULL, NULL, NULL, NULL}
213 };
214
215 void
216 ssh_userauth2(const char *local_user, const char *server_user, char *host,
217     Key **keys, int nkeys)
218 {
219         Authctxt authctxt;
220         int type;
221
222         if (options.challenge_response_authentication)
223                 options.kbd_interactive_authentication = 1;
224
225         debug("send SSH2_MSG_SERVICE_REQUEST");
226         packet_start(SSH2_MSG_SERVICE_REQUEST);
227         packet_put_cstring("ssh-userauth");
228         packet_send();
229         packet_write_wait();
230         type = packet_read();
231         if (type != SSH2_MSG_SERVICE_ACCEPT) {
232                 fatal("denied SSH2_MSG_SERVICE_ACCEPT: %d", type);
233         }
234         if (packet_remaining() > 0) {
235                 char *reply = packet_get_string(NULL);
236                 debug("service_accept: %s", reply);
237                 xfree(reply);
238         } else {
239                 debug("buggy server: service_accept w/o service");
240         }
241         packet_check_eom();
242         debug("got SSH2_MSG_SERVICE_ACCEPT");
243
244         if (options.preferred_authentications == NULL)
245                 options.preferred_authentications = authmethods_get();
246
247         /* setup authentication context */
248         memset(&authctxt, 0, sizeof(authctxt));
249         authctxt.agent = ssh_get_authentication_connection();
250         authctxt.server_user = server_user;
251         authctxt.local_user = local_user;
252         authctxt.host = host;
253         authctxt.service = "ssh-connection";            /* service name */
254         authctxt.success = 0;
255         authctxt.method = authmethod_lookup("none");
256         authctxt.authlist = NULL;
257         authctxt.keys = keys;
258         authctxt.nkeys = nkeys;
259         authctxt.info_req_seen = 0;
260         if (authctxt.method == NULL)
261                 fatal("ssh_userauth2: internal error: cannot send userauth none request");
262
263         /* initial userauth request */
264         userauth_none(&authctxt);
265
266         dispatch_init(&input_userauth_error);
267         dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
268         dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
269         dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
270         dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt);     /* loop until success */
271
272         if (authctxt.agent != NULL)
273                 ssh_close_authentication_connection(authctxt.agent);
274
275         debug("ssh-userauth2 successful: method %s", authctxt.method->name);
276 }
277 void
278 userauth(Authctxt *authctxt, char *authlist)
279 {
280         if (authlist == NULL) {
281                 authlist = authctxt->authlist;
282         } else {
283                 if (authctxt->authlist)
284                         xfree(authctxt->authlist);
285                 authctxt->authlist = authlist;
286         }
287         for (;;) {
288                 Authmethod *method = authmethod_get(authlist);
289                 if (method == NULL)
290                         fatal("Permission denied (%s).", authlist);
291                 authctxt->method = method;
292                 if (method->userauth(authctxt) != 0) {
293                         debug2("we sent a %s packet, wait for reply", method->name);
294                         break;
295                 } else {
296                         debug2("we did not send a packet, disable method");
297                         method->enabled = NULL;
298                 }
299         }
300 }
301 void
302 input_userauth_error(int type, u_int32_t seq, void *ctxt)
303 {
304         fatal("input_userauth_error: bad message during authentication: "
305            "type %d", type);
306 }
307 void
308 input_userauth_banner(int type, u_int32_t seq, void *ctxt)
309 {
310         char *msg, *lang;
311         debug3("input_userauth_banner");
312         msg = packet_get_string(NULL);
313         lang = packet_get_string(NULL);
314         fprintf(stderr, "%s", msg);
315         xfree(msg);
316         xfree(lang);
317 }
318 void
319 input_userauth_success(int type, u_int32_t seq, void *ctxt)
320 {
321         Authctxt *authctxt = ctxt;
322         if (authctxt == NULL)
323                 fatal("input_userauth_success: no authentication context");
324         if (authctxt->authlist)
325                 xfree(authctxt->authlist);
326         clear_auth_state(authctxt);
327         authctxt->success = 1;                  /* break out */
328 }
329 void
330 input_userauth_failure(int type, u_int32_t seq, void *ctxt)
331 {
332         Authctxt *authctxt = ctxt;
333         char *authlist = NULL;
334         int partial;
335
336         if (authctxt == NULL)
337                 fatal("input_userauth_failure: no authentication context");
338
339         authlist = packet_get_string(NULL);
340         partial = packet_get_char();
341         packet_check_eom();
342
343         if (partial != 0)
344                 log("Authenticated with partial success.");
345         debug("authentications that can continue: %s", authlist);
346
347         clear_auth_state(authctxt);
348         userauth(authctxt, authlist);
349 }
350 void
351 input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
352 {
353         Authctxt *authctxt = ctxt;
354         Key *key = NULL;
355         Buffer b;
356         int alen, blen, sent = 0;
357         char *pkalg, *pkblob, *fp;
358
359         if (authctxt == NULL)
360                 fatal("input_userauth_pk_ok: no authentication context");
361         if (datafellows & SSH_BUG_PKOK) {
362                 /* this is similar to SSH_BUG_PKAUTH */
363                 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
364                 pkblob = packet_get_string(&blen);
365                 buffer_init(&b);
366                 buffer_append(&b, pkblob, blen);
367                 pkalg = buffer_get_string(&b, &alen);
368                 buffer_free(&b);
369         } else {
370                 pkalg = packet_get_string(&alen);
371                 pkblob = packet_get_string(&blen);
372         }
373         packet_check_eom();
374
375         debug("input_userauth_pk_ok: pkalg %s blen %d lastkey %p hint %d",
376             pkalg, blen, authctxt->last_key, authctxt->last_key_hint);
377
378         do {
379                 if (authctxt->last_key == NULL ||
380                     authctxt->last_key_sign == NULL) {
381                         debug("no last key or no sign cb");
382                         break;
383                 }
384                 if (key_type_from_name(pkalg) == KEY_UNSPEC) {
385                         debug("unknown pkalg %s", pkalg);
386                         break;
387                 }
388                 if ((key = key_from_blob(pkblob, blen)) == NULL) {
389                         debug("no key from blob. pkalg %s", pkalg);
390                         break;
391                 }
392                 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
393                 debug2("input_userauth_pk_ok: fp %s", fp);
394                 xfree(fp);
395                 if (!key_equal(key, authctxt->last_key)) {
396                         debug("key != last_key");
397                         break;
398                 }
399                 sent = sign_and_send_pubkey(authctxt, key,
400                    authctxt->last_key_sign);
401         } while (0);
402
403         if (key != NULL)
404                 key_free(key);
405         xfree(pkalg);
406         xfree(pkblob);
407
408         /* unregister */
409         clear_auth_state(authctxt);
410         dispatch_set(SSH2_MSG_USERAUTH_PK_OK, NULL);
411
412         /* try another method if we did not send a packet*/
413         if (sent == 0)
414                 userauth(authctxt, NULL);
415
416 }
417
418 int
419 userauth_none(Authctxt *authctxt)
420 {
421         /* initial userauth request */
422         packet_start(SSH2_MSG_USERAUTH_REQUEST);
423         packet_put_cstring(authctxt->server_user);
424         packet_put_cstring(authctxt->service);
425         packet_put_cstring(authctxt->method->name);
426         packet_send();
427         return 1;
428 }
429
430 int
431 userauth_passwd(Authctxt *authctxt)
432 {
433         static int attempt = 0;
434         char prompt[80];
435         char *password;
436
437         if (attempt++ >= options.number_of_password_prompts)
438                 return 0;
439
440         if (attempt != 1)
441                 error("Permission denied, please try again.");
442
443         snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
444             authctxt->server_user, authctxt->host);
445         password = read_passphrase(prompt, 0);
446         packet_start(SSH2_MSG_USERAUTH_REQUEST);
447         packet_put_cstring(authctxt->server_user);
448         packet_put_cstring(authctxt->service);
449         packet_put_cstring(authctxt->method->name);
450         packet_put_char(0);
451         packet_put_cstring(password);
452         memset(password, 0, strlen(password));
453         xfree(password);
454         packet_add_padding(64);
455         packet_send();
456         return 1;
457 }
458
459 static void
460 clear_auth_state(Authctxt *authctxt)
461 {
462         /* XXX clear authentication state */
463         if (authctxt->last_key != NULL && authctxt->last_key_hint == -1) {
464                 debug3("clear_auth_state: key_free %p", authctxt->last_key);
465                 key_free(authctxt->last_key);
466         }
467         authctxt->last_key = NULL;
468         authctxt->last_key_hint = -2;
469         authctxt->last_key_sign = NULL;
470 }
471
472 static int
473 sign_and_send_pubkey(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback)
474 {
475         Buffer b;
476         u_char *blob, *signature;
477         int bloblen, slen;
478         int skip = 0;
479         int ret = -1;
480         int have_sig = 1;
481
482         debug3("sign_and_send_pubkey");
483
484         if (key_to_blob(k, &blob, &bloblen) == 0) {
485                 /* we cannot handle this key */
486                 debug3("sign_and_send_pubkey: cannot handle key");
487                 return 0;
488         }
489         /* data to be signed */
490         buffer_init(&b);
491         if (datafellows & SSH_OLD_SESSIONID) {
492                 buffer_append(&b, session_id2, session_id2_len);
493                 skip = session_id2_len;
494         } else {
495                 buffer_put_string(&b, session_id2, session_id2_len);
496                 skip = buffer_len(&b);
497         }
498         buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
499         buffer_put_cstring(&b, authctxt->server_user);
500         buffer_put_cstring(&b,
501             datafellows & SSH_BUG_PKSERVICE ?
502             "ssh-userauth" :
503             authctxt->service);
504         if (datafellows & SSH_BUG_PKAUTH) {
505                 buffer_put_char(&b, have_sig);
506         } else {
507                 buffer_put_cstring(&b, authctxt->method->name);
508                 buffer_put_char(&b, have_sig);
509                 buffer_put_cstring(&b, key_ssh_name(k));
510         }
511         buffer_put_string(&b, blob, bloblen);
512
513         /* generate signature */
514         ret = (*sign_callback)(authctxt, k, &signature, &slen,
515             buffer_ptr(&b), buffer_len(&b));
516         if (ret == -1) {
517                 xfree(blob);
518                 buffer_free(&b);
519                 return 0;
520         }
521 #ifdef DEBUG_PK
522         buffer_dump(&b);
523 #endif
524         if (datafellows & SSH_BUG_PKSERVICE) {
525                 buffer_clear(&b);
526                 buffer_append(&b, session_id2, session_id2_len);
527                 skip = session_id2_len;
528                 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
529                 buffer_put_cstring(&b, authctxt->server_user);
530                 buffer_put_cstring(&b, authctxt->service);
531                 buffer_put_cstring(&b, authctxt->method->name);
532                 buffer_put_char(&b, have_sig);
533                 if (!(datafellows & SSH_BUG_PKAUTH))
534                         buffer_put_cstring(&b, key_ssh_name(k));
535                 buffer_put_string(&b, blob, bloblen);
536         }
537         xfree(blob);
538
539         /* append signature */
540         buffer_put_string(&b, signature, slen);
541         xfree(signature);
542
543         /* skip session id and packet type */
544         if (buffer_len(&b) < skip + 1)
545                 fatal("userauth_pubkey: internal error");
546         buffer_consume(&b, skip + 1);
547
548         /* put remaining data from buffer into packet */
549         packet_start(SSH2_MSG_USERAUTH_REQUEST);
550         packet_put_raw(buffer_ptr(&b), buffer_len(&b));
551         buffer_free(&b);
552         packet_send();
553
554         return 1;
555 }
556
557 static int
558 send_pubkey_test(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback,
559     int hint)
560 {
561         u_char *blob;
562         int bloblen, have_sig = 0;
563
564         debug3("send_pubkey_test");
565
566         if (key_to_blob(k, &blob, &bloblen) == 0) {
567                 /* we cannot handle this key */
568                 debug3("send_pubkey_test: cannot handle key");
569                 return 0;
570         }
571         /* register callback for USERAUTH_PK_OK message */
572         authctxt->last_key_sign = sign_callback;
573         authctxt->last_key_hint = hint;
574         authctxt->last_key = k;
575         dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
576
577         packet_start(SSH2_MSG_USERAUTH_REQUEST);
578         packet_put_cstring(authctxt->server_user);
579         packet_put_cstring(authctxt->service);
580         packet_put_cstring(authctxt->method->name);
581         packet_put_char(have_sig);
582         if (!(datafellows & SSH_BUG_PKAUTH))
583                 packet_put_cstring(key_ssh_name(k));
584         packet_put_string(blob, bloblen);
585         xfree(blob);
586         packet_send();
587         return 1;
588 }
589
590 static Key *
591 load_identity_file(char *filename)
592 {
593         Key *private;
594         char prompt[300], *passphrase;
595         int quit, i;
596         struct stat st;
597
598         if (stat(filename, &st) < 0) {
599                 debug3("no such identity: %s", filename);
600                 return NULL;
601         }
602         private = key_load_private_type(KEY_UNSPEC, filename, "", NULL);
603         if (private == NULL) {
604                 if (options.batch_mode)
605                         return NULL;
606                 snprintf(prompt, sizeof prompt,
607                     "Enter passphrase for key '%.100s': ", filename);
608                 for (i = 0; i < options.number_of_password_prompts; i++) {
609                         passphrase = read_passphrase(prompt, 0);
610                         if (strcmp(passphrase, "") != 0) {
611                                 private = key_load_private_type(KEY_UNSPEC, filename,
612                                     passphrase, NULL);
613                                 quit = 0;
614                         } else {
615                                 debug2("no passphrase given, try next key");
616                                 quit = 1;
617                         }
618                         memset(passphrase, 0, strlen(passphrase));
619                         xfree(passphrase);
620                         if (private != NULL || quit)
621                                 break;
622                         debug2("bad passphrase given, try again...");
623                 }
624         }
625         return private;
626 }
627
628 static int
629 identity_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
630     u_char *data, int datalen)
631 {
632         Key *private;
633         int idx, ret;
634
635         idx = authctxt->last_key_hint;
636         if (idx < 0)
637                 return -1;
638
639         /* private key is stored in external hardware */
640         if (options.identity_keys[idx]->flags & KEY_FLAG_EXT)
641                 return key_sign(options.identity_keys[idx], sigp, lenp, data, datalen);
642
643         private = load_identity_file(options.identity_files[idx]);
644         if (private == NULL)
645                 return -1;
646         ret = key_sign(private, sigp, lenp, data, datalen);
647         key_free(private);
648         return ret;
649 }
650
651 static int
652 agent_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
653     u_char *data, int datalen)
654 {
655         return ssh_agent_sign(authctxt->agent, key, sigp, lenp, data, datalen);
656 }
657
658 static int
659 key_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
660     u_char *data, int datalen)
661 {
662         return key_sign(key, sigp, lenp, data, datalen);
663 }
664
665 static int
666 userauth_pubkey_agent(Authctxt *authctxt)
667 {
668         static int called = 0;
669         int ret = 0;
670         char *comment;
671         Key *k;
672
673         if (called == 0) {
674                 if (ssh_get_num_identities(authctxt->agent, 2) == 0)
675                         debug2("userauth_pubkey_agent: no keys at all");
676                 called = 1;
677         }
678         k = ssh_get_next_identity(authctxt->agent, &comment, 2);
679         if (k == NULL) {
680                 debug2("userauth_pubkey_agent: no more keys");
681         } else {
682                 debug("userauth_pubkey_agent: testing agent key %s", comment);
683                 xfree(comment);
684                 ret = send_pubkey_test(authctxt, k, agent_sign_cb, -1);
685                 if (ret == 0)
686                         key_free(k);
687         }
688         if (ret == 0)
689                 debug2("userauth_pubkey_agent: no message sent");
690         return ret;
691 }
692
693 int
694 userauth_pubkey(Authctxt *authctxt)
695 {
696         static int idx = 0;
697         int sent = 0;
698         Key *key;
699         char *filename;
700
701         if (authctxt->agent != NULL) {
702                 do {
703                         sent = userauth_pubkey_agent(authctxt);
704                 } while (!sent && authctxt->agent->howmany > 0);
705         }
706         while (!sent && idx < options.num_identity_files) {
707                 key = options.identity_keys[idx];
708                 filename = options.identity_files[idx];
709                 if (key == NULL) {
710                         debug("try privkey: %s", filename);
711                         key = load_identity_file(filename);
712                         if (key != NULL) {
713                                 sent = sign_and_send_pubkey(authctxt, key,
714                                     key_sign_cb);
715                                 key_free(key);
716                         }
717                 } else if (key->type != KEY_RSA1) {
718                         debug("try pubkey: %s", filename);
719                         sent = send_pubkey_test(authctxt, key,
720                             identity_sign_cb, idx);
721                 }
722                 idx++;
723         }
724         return sent;
725 }
726
727 /*
728  * Send userauth request message specifying keyboard-interactive method.
729  */
730 int
731 userauth_kbdint(Authctxt *authctxt)
732 {
733         static int attempt = 0;
734
735         if (attempt++ >= options.number_of_password_prompts)
736                 return 0;
737         /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
738         if (attempt > 1 && !authctxt->info_req_seen) {
739                 debug3("userauth_kbdint: disable: no info_req_seen");
740                 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
741                 return 0;
742         }
743
744         debug2("userauth_kbdint");
745         packet_start(SSH2_MSG_USERAUTH_REQUEST);
746         packet_put_cstring(authctxt->server_user);
747         packet_put_cstring(authctxt->service);
748         packet_put_cstring(authctxt->method->name);
749         packet_put_cstring("");                                 /* lang */
750         packet_put_cstring(options.kbd_interactive_devices ?
751             options.kbd_interactive_devices : "");
752         packet_send();
753
754         dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
755         return 1;
756 }
757
758 /*
759  * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
760  */
761 void
762 input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
763 {
764         Authctxt *authctxt = ctxt;
765         char *name, *inst, *lang, *prompt, *response;
766         u_int num_prompts, i;
767         int echo = 0;
768
769         debug2("input_userauth_info_req");
770
771         if (authctxt == NULL)
772                 fatal("input_userauth_info_req: no authentication context");
773
774         authctxt->info_req_seen = 1;
775
776         name = packet_get_string(NULL);
777         inst = packet_get_string(NULL);
778         lang = packet_get_string(NULL);
779         if (strlen(name) > 0)
780                 log("%s", name);
781         if (strlen(inst) > 0)
782                 log("%s", inst);
783         xfree(name);
784         xfree(inst);
785         xfree(lang);
786
787         num_prompts = packet_get_int();
788         /*
789          * Begin to build info response packet based on prompts requested.
790          * We commit to providing the correct number of responses, so if
791          * further on we run into a problem that prevents this, we have to
792          * be sure and clean this up and send a correct error response.
793          */
794         packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
795         packet_put_int(num_prompts);
796
797         debug2("input_userauth_info_req: num_prompts %d", num_prompts);
798         for (i = 0; i < num_prompts; i++) {
799                 prompt = packet_get_string(NULL);
800                 echo = packet_get_char();
801
802                 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
803
804                 packet_put_cstring(response);
805                 memset(response, 0, strlen(response));
806                 xfree(response);
807                 xfree(prompt);
808         }
809         packet_check_eom(); /* done with parsing incoming message. */
810
811         packet_add_padding(64);
812         packet_send();
813 }
814
815 /*
816  * this will be move to an external program (ssh-keysign) ASAP. ssh-keysign
817  * will be setuid-root and the sbit can be removed from /usr/bin/ssh.
818  */
819 int
820 userauth_hostbased(Authctxt *authctxt)
821 {
822         Key *private = NULL;
823         Buffer b;
824         u_char *signature, *blob;
825         char *chost, *pkalg, *p;
826         const char *service;
827         u_int blen, slen;
828         int ok, i, len, found = 0;
829
830         /* check for a useful key */
831         for (i = 0; i < authctxt->nkeys; i++) {
832                 private = authctxt->keys[i];
833                 if (private && private->type != KEY_RSA1) {
834                         found = 1;
835                         /* we take and free the key */
836                         authctxt->keys[i] = NULL;
837                         break;
838                 }
839         }
840         if (!found) {
841                 debug("userauth_hostbased: no more client hostkeys");
842                 return 0;
843         }
844         if (key_to_blob(private, &blob, &blen) == 0) {
845                 key_free(private);
846                 return 0;
847         }
848         /* figure out a name for the client host */
849         p = get_local_name(packet_get_connection_in());
850         if (p == NULL) {
851                 error("userauth_hostbased: cannot get local ipaddr/name");
852                 key_free(private);
853                 return 0;
854         }
855         len = strlen(p) + 2;
856         chost = xmalloc(len);
857         strlcpy(chost, p, len);
858         strlcat(chost, ".", len);
859         debug2("userauth_hostbased: chost %s", chost);
860
861         service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
862             authctxt->service;
863         pkalg = xstrdup(key_ssh_name(private));
864         buffer_init(&b);
865         /* construct data */
866         buffer_put_string(&b, session_id2, session_id2_len);
867         buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
868         buffer_put_cstring(&b, authctxt->server_user);
869         buffer_put_cstring(&b, service);
870         buffer_put_cstring(&b, authctxt->method->name);
871         buffer_put_cstring(&b, pkalg);
872         buffer_put_string(&b, blob, blen);
873         buffer_put_cstring(&b, chost);
874         buffer_put_cstring(&b, authctxt->local_user);
875 #ifdef DEBUG_PK
876         buffer_dump(&b);
877 #endif
878         ok = key_sign(private, &signature, &slen, buffer_ptr(&b), buffer_len(&b));
879         key_free(private);
880         buffer_free(&b);
881         if (ok != 0) {
882                 error("key_sign failed");
883                 xfree(chost);
884                 xfree(pkalg);
885                 return 0;
886         }
887         packet_start(SSH2_MSG_USERAUTH_REQUEST);
888         packet_put_cstring(authctxt->server_user);
889         packet_put_cstring(authctxt->service);
890         packet_put_cstring(authctxt->method->name);
891         packet_put_cstring(pkalg);
892         packet_put_string(blob, blen);
893         packet_put_cstring(chost);
894         packet_put_cstring(authctxt->local_user);
895         packet_put_string(signature, slen);
896         memset(signature, 's', slen);
897         xfree(signature);
898         xfree(chost);
899         xfree(pkalg);
900
901         packet_send();
902         return 1;
903 }
904
905 /* find auth method */
906
907 /*
908  * given auth method name, if configurable options permit this method fill
909  * in auth_ident field and return true, otherwise return false.
910  */
911 static int
912 authmethod_is_enabled(Authmethod *method)
913 {
914         if (method == NULL)
915                 return 0;
916         /* return false if options indicate this method is disabled */
917         if  (method->enabled == NULL || *method->enabled == 0)
918                 return 0;
919         /* return false if batch mode is enabled but method needs interactive mode */
920         if  (method->batch_flag != NULL && *method->batch_flag != 0)
921                 return 0;
922         return 1;
923 }
924
925 static Authmethod *
926 authmethod_lookup(const char *name)
927 {
928         Authmethod *method = NULL;
929         if (name != NULL)
930                 for (method = authmethods; method->name != NULL; method++)
931                         if (strcmp(name, method->name) == 0)
932                                 return method;
933         debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
934         return NULL;
935 }
936
937 /* XXX internal state */
938 static Authmethod *current = NULL;
939 static char *supported = NULL;
940 static char *preferred = NULL;
941 /*
942  * Given the authentication method list sent by the server, return the
943  * next method we should try.  If the server initially sends a nil list,
944  * use a built-in default list.
945  */
946 static Authmethod *
947 authmethod_get(char *authlist)
948 {
949
950         char *name = NULL;
951         int next;
952
953         /* Use a suitable default if we're passed a nil list.  */
954         if (authlist == NULL || strlen(authlist) == 0)
955                 authlist = options.preferred_authentications;
956
957         if (supported == NULL || strcmp(authlist, supported) != 0) {
958                 debug3("start over, passed a different list %s", authlist);
959                 if (supported != NULL)
960                         xfree(supported);
961                 supported = xstrdup(authlist);
962                 preferred = options.preferred_authentications;
963                 debug3("preferred %s", preferred);
964                 current = NULL;
965         } else if (current != NULL && authmethod_is_enabled(current))
966                 return current;
967
968         for (;;) {
969                 if ((name = match_list(preferred, supported, &next)) == NULL) {
970                         debug("no more auth methods to try");
971                         current = NULL;
972                         return NULL;
973                 }
974                 preferred += next;
975                 debug3("authmethod_lookup %s", name);
976                 debug3("remaining preferred: %s", preferred);
977                 if ((current = authmethod_lookup(name)) != NULL &&
978                     authmethod_is_enabled(current)) {
979                         debug3("authmethod_is_enabled %s", name);
980                         debug("next auth method to try is %s", name);
981                         return current;
982                 }
983         }
984 }
985
986 static char *
987 authmethods_get(void)
988 {
989         Authmethod *method = NULL;
990         Buffer b;
991         char *list;
992
993         buffer_init(&b);
994         for (method = authmethods; method->name != NULL; method++) {
995                 if (authmethod_is_enabled(method)) {
996                         if (buffer_len(&b) > 0)
997                                 buffer_append(&b, ",", 1);
998                         buffer_append(&b, method->name, strlen(method->name));
999                 }
1000         }
1001         buffer_append(&b, "\0", 1);
1002         list = xstrdup(buffer_ptr(&b));
1003         buffer_free(&b);
1004         return list;
1005 }
This page took 0.123708 seconds and 5 git commands to generate.