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