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