]>
Commit | Line | Data |
---|---|---|
1 | /* $OpenBSD: sshconnect2.c,v 1.162 2006/08/30 00:06:51 dtucker Exp $ */ | |
2 | /* | |
3 | * Copyright (c) 2000 Markus Friedl. All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * 2. Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
13 | * | |
14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |
15 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
16 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
17 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | |
18 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
19 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
20 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
21 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
23 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
24 | */ | |
25 | ||
26 | #include "includes.h" | |
27 | ||
28 | #include <sys/types.h> | |
29 | #include <sys/socket.h> | |
30 | #include <sys/wait.h> | |
31 | #include <sys/stat.h> | |
32 | ||
33 | #include <errno.h> | |
34 | #include <pwd.h> | |
35 | #include <signal.h> | |
36 | #include <stdarg.h> | |
37 | #include <stdio.h> | |
38 | #include <string.h> | |
39 | #include <unistd.h> | |
40 | ||
41 | #include "openbsd-compat/sys-queue.h" | |
42 | ||
43 | #include "xmalloc.h" | |
44 | #include "ssh.h" | |
45 | #include "ssh2.h" | |
46 | #include "buffer.h" | |
47 | #include "packet.h" | |
48 | #include "compat.h" | |
49 | #include "cipher.h" | |
50 | #include "key.h" | |
51 | #include "kex.h" | |
52 | #include "myproposal.h" | |
53 | #include "sshconnect.h" | |
54 | #include "authfile.h" | |
55 | #include "dh.h" | |
56 | #include "authfd.h" | |
57 | #include "log.h" | |
58 | #include "readconf.h" | |
59 | #include "misc.h" | |
60 | #include "match.h" | |
61 | #include "dispatch.h" | |
62 | #include "canohost.h" | |
63 | #include "msg.h" | |
64 | #include "pathnames.h" | |
65 | #include "uidswap.h" | |
66 | ||
67 | #ifdef GSSAPI | |
68 | #include "ssh-gss.h" | |
69 | #endif | |
70 | ||
71 | /* import */ | |
72 | extern char *client_version_string; | |
73 | extern char *server_version_string; | |
74 | extern Options options; | |
75 | extern Kex *xxx_kex; | |
76 | ||
77 | /* tty_flag is set in ssh.c. use this in ssh_userauth2 */ | |
78 | /* if it is set then prevent the switch to the null cipher */ | |
79 | ||
80 | extern int tty_flag; | |
81 | ||
82 | /* | |
83 | * SSH2 key exchange | |
84 | */ | |
85 | ||
86 | u_char *session_id2 = NULL; | |
87 | u_int session_id2_len = 0; | |
88 | ||
89 | char *xxx_host; | |
90 | struct sockaddr *xxx_hostaddr; | |
91 | ||
92 | Kex *xxx_kex = NULL; | |
93 | ||
94 | static int | |
95 | verify_host_key_callback(Key *hostkey) | |
96 | { | |
97 | if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1) | |
98 | fatal("Host key verification failed."); | |
99 | return 0; | |
100 | } | |
101 | ||
102 | void | |
103 | ssh_kex2(char *host, struct sockaddr *hostaddr) | |
104 | { | |
105 | Kex *kex; | |
106 | ||
107 | #ifdef GSSAPI | |
108 | char *orig = NULL, *gss = NULL; | |
109 | char *gss_host = NULL; | |
110 | #endif | |
111 | ||
112 | xxx_host = host; | |
113 | xxx_hostaddr = hostaddr; | |
114 | ||
115 | #ifdef GSSAPI | |
116 | if (options.gss_keyex) { | |
117 | /* Add the GSSAPI mechanisms currently supported on this | |
118 | * client to the key exchange algorithm proposal */ | |
119 | orig = myproposal[PROPOSAL_KEX_ALGS]; | |
120 | ||
121 | if (options.gss_trust_dns) | |
122 | gss_host = (char *)get_canonical_hostname(1); | |
123 | else | |
124 | gss_host = host; | |
125 | ||
126 | gss = ssh_gssapi_client_mechanisms(gss_host); | |
127 | if (gss) { | |
128 | debug("Offering GSSAPI proposal: %s", gss); | |
129 | xasprintf(&myproposal[PROPOSAL_KEX_ALGS], | |
130 | "%s,%s", gss, orig); | |
131 | } | |
132 | } | |
133 | #endif | |
134 | ||
135 | if (options.ciphers == (char *)-1) { | |
136 | logit("No valid ciphers for protocol version 2 given, using defaults."); | |
137 | options.ciphers = NULL; | |
138 | } | |
139 | if (options.ciphers != NULL) { | |
140 | myproposal[PROPOSAL_ENC_ALGS_CTOS] = | |
141 | myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers; | |
142 | } | |
143 | ||
144 | myproposal[PROPOSAL_ENC_ALGS_CTOS] = | |
145 | compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]); | |
146 | myproposal[PROPOSAL_ENC_ALGS_STOC] = | |
147 | compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]); | |
148 | if (options.compression) { | |
149 | myproposal[PROPOSAL_COMP_ALGS_CTOS] = | |
150 | myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib@openssh.com,zlib,none"; | |
151 | } else { | |
152 | myproposal[PROPOSAL_COMP_ALGS_CTOS] = | |
153 | myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com,zlib"; | |
154 | } | |
155 | if (options.macs != NULL) { | |
156 | myproposal[PROPOSAL_MAC_ALGS_CTOS] = | |
157 | myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs; | |
158 | } | |
159 | if (options.hostkeyalgorithms != NULL) | |
160 | myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = | |
161 | options.hostkeyalgorithms; | |
162 | ||
163 | #ifdef GSSAPI | |
164 | /* If we've got GSSAPI algorithms, then we also support the | |
165 | * 'null' hostkey, as a last resort */ | |
166 | if (options.gss_keyex && gss) { | |
167 | orig = myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS]; | |
168 | xasprintf(&myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS], | |
169 | "%s,null", orig); | |
170 | } | |
171 | #endif | |
172 | ||
173 | if (options.rekey_limit) | |
174 | packet_set_rekey_limit(options.rekey_limit); | |
175 | ||
176 | /* start key exchange */ | |
177 | kex = kex_setup(myproposal); | |
178 | kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client; | |
179 | kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client; | |
180 | kex->kex[KEX_DH_GEX_SHA1] = kexgex_client; | |
181 | kex->kex[KEX_DH_GEX_SHA256] = kexgex_client; | |
182 | #ifdef GSSAPI | |
183 | kex->kex[KEX_GSS_GRP1_SHA1] = kexgss_client; | |
184 | kex->kex[KEX_GSS_GRP14_SHA1] = kexgss_client; | |
185 | kex->kex[KEX_GSS_GEX_SHA1] = kexgss_client; | |
186 | #endif | |
187 | kex->client_version_string=client_version_string; | |
188 | kex->server_version_string=server_version_string; | |
189 | kex->verify_host_key=&verify_host_key_callback; | |
190 | ||
191 | #ifdef GSSAPI | |
192 | kex->gss_deleg_creds = options.gss_deleg_creds; | |
193 | kex->gss_trust_dns = options.gss_trust_dns; | |
194 | kex->gss_host = gss_host; | |
195 | #endif | |
196 | ||
197 | xxx_kex = kex; | |
198 | ||
199 | dispatch_run(DISPATCH_BLOCK, &kex->done, kex); | |
200 | ||
201 | session_id2 = kex->session_id; | |
202 | session_id2_len = kex->session_id_len; | |
203 | ||
204 | #ifdef DEBUG_KEXDH | |
205 | /* send 1st encrypted/maced/compressed message */ | |
206 | packet_start(SSH2_MSG_IGNORE); | |
207 | packet_put_cstring("markus"); | |
208 | packet_send(); | |
209 | packet_write_wait(); | |
210 | #endif | |
211 | } | |
212 | ||
213 | /* | |
214 | * Authenticate user | |
215 | */ | |
216 | ||
217 | typedef struct Authctxt Authctxt; | |
218 | typedef struct Authmethod Authmethod; | |
219 | typedef struct identity Identity; | |
220 | typedef struct idlist Idlist; | |
221 | ||
222 | struct identity { | |
223 | TAILQ_ENTRY(identity) next; | |
224 | AuthenticationConnection *ac; /* set if agent supports key */ | |
225 | Key *key; /* public/private key */ | |
226 | char *filename; /* comment for agent-only keys */ | |
227 | int tried; | |
228 | int isprivate; /* key points to the private key */ | |
229 | }; | |
230 | TAILQ_HEAD(idlist, identity); | |
231 | ||
232 | struct Authctxt { | |
233 | const char *server_user; | |
234 | const char *local_user; | |
235 | const char *host; | |
236 | const char *service; | |
237 | Authmethod *method; | |
238 | int success; | |
239 | char *authlist; | |
240 | /* pubkey */ | |
241 | Idlist keys; | |
242 | AuthenticationConnection *agent; | |
243 | /* hostbased */ | |
244 | Sensitive *sensitive; | |
245 | /* kbd-interactive */ | |
246 | int info_req_seen; | |
247 | /* generic */ | |
248 | void *methoddata; | |
249 | }; | |
250 | struct Authmethod { | |
251 | char *name; /* string to compare against server's list */ | |
252 | int (*userauth)(Authctxt *authctxt); | |
253 | int *enabled; /* flag in option struct that enables method */ | |
254 | int *batch_flag; /* flag in option struct that disables method */ | |
255 | }; | |
256 | ||
257 | void input_userauth_success(int, u_int32_t, void *); | |
258 | void input_userauth_failure(int, u_int32_t, void *); | |
259 | void input_userauth_banner(int, u_int32_t, void *); | |
260 | void input_userauth_error(int, u_int32_t, void *); | |
261 | void input_userauth_info_req(int, u_int32_t, void *); | |
262 | void input_userauth_pk_ok(int, u_int32_t, void *); | |
263 | void input_userauth_passwd_changereq(int, u_int32_t, void *); | |
264 | ||
265 | int userauth_none(Authctxt *); | |
266 | int userauth_pubkey(Authctxt *); | |
267 | int userauth_passwd(Authctxt *); | |
268 | int userauth_kbdint(Authctxt *); | |
269 | int userauth_hostbased(Authctxt *); | |
270 | int userauth_kerberos(Authctxt *); | |
271 | ||
272 | #ifdef GSSAPI | |
273 | int userauth_external(Authctxt *authctxt); | |
274 | int userauth_gssapi(Authctxt *authctxt); | |
275 | int userauth_gssapi_with_mic(Authctxt *authctxt); | |
276 | int userauth_gssapi_without_mic(Authctxt *authctxt); | |
277 | void input_gssapi_response(int type, u_int32_t, void *); | |
278 | void input_gssapi_token(int type, u_int32_t, void *); | |
279 | void input_gssapi_hash(int type, u_int32_t, void *); | |
280 | void input_gssapi_error(int, u_int32_t, void *); | |
281 | void input_gssapi_errtok(int, u_int32_t, void *); | |
282 | int userauth_gsskeyex(Authctxt *authctxt); | |
283 | #endif | |
284 | ||
285 | void userauth(Authctxt *, char *); | |
286 | ||
287 | static int sign_and_send_pubkey(Authctxt *, Identity *); | |
288 | static void pubkey_prepare(Authctxt *); | |
289 | static void pubkey_cleanup(Authctxt *); | |
290 | static Key *load_identity_file(char *); | |
291 | ||
292 | static Authmethod *authmethod_get(char *authlist); | |
293 | static Authmethod *authmethod_lookup(const char *name); | |
294 | static char *authmethods_get(void); | |
295 | ||
296 | Authmethod authmethods[] = { | |
297 | #ifdef GSSAPI | |
298 | {"gssapi-keyex", | |
299 | userauth_gsskeyex, | |
300 | &options.gss_authentication, | |
301 | NULL}, | |
302 | {"external-keyx", | |
303 | userauth_external, | |
304 | &options.gss_authentication, | |
305 | NULL}, | |
306 | {"gssapi-with-mic", | |
307 | userauth_gssapi, | |
308 | &options.gss_authentication, | |
309 | NULL}, | |
310 | {"gssapi", | |
311 | userauth_gssapi, | |
312 | &options.gss_authentication, | |
313 | NULL}, | |
314 | #endif | |
315 | {"hostbased", | |
316 | userauth_hostbased, | |
317 | &options.hostbased_authentication, | |
318 | NULL}, | |
319 | {"publickey", | |
320 | userauth_pubkey, | |
321 | &options.pubkey_authentication, | |
322 | NULL}, | |
323 | {"keyboard-interactive", | |
324 | userauth_kbdint, | |
325 | &options.kbd_interactive_authentication, | |
326 | &options.batch_mode}, | |
327 | {"password", | |
328 | userauth_passwd, | |
329 | &options.password_authentication, | |
330 | &options.batch_mode}, | |
331 | {"none", | |
332 | userauth_none, | |
333 | NULL, | |
334 | NULL}, | |
335 | {NULL, NULL, NULL, NULL} | |
336 | }; | |
337 | ||
338 | void | |
339 | ssh_userauth2(const char *local_user, const char *server_user, char *host, | |
340 | Sensitive *sensitive) | |
341 | { | |
342 | Authctxt authctxt; | |
343 | int type; | |
344 | ||
345 | if (options.challenge_response_authentication) | |
346 | options.kbd_interactive_authentication = 1; | |
347 | ||
348 | packet_start(SSH2_MSG_SERVICE_REQUEST); | |
349 | packet_put_cstring("ssh-userauth"); | |
350 | packet_send(); | |
351 | debug("SSH2_MSG_SERVICE_REQUEST sent"); | |
352 | packet_write_wait(); | |
353 | type = packet_read(); | |
354 | if (type != SSH2_MSG_SERVICE_ACCEPT) | |
355 | fatal("Server denied authentication request: %d", type); | |
356 | if (packet_remaining() > 0) { | |
357 | char *reply = packet_get_string(NULL); | |
358 | debug2("service_accept: %s", reply); | |
359 | xfree(reply); | |
360 | } else { | |
361 | debug2("buggy server: service_accept w/o service"); | |
362 | } | |
363 | packet_check_eom(); | |
364 | debug("SSH2_MSG_SERVICE_ACCEPT received"); | |
365 | ||
366 | if (options.preferred_authentications == NULL) | |
367 | options.preferred_authentications = authmethods_get(); | |
368 | ||
369 | /* setup authentication context */ | |
370 | memset(&authctxt, 0, sizeof(authctxt)); | |
371 | pubkey_prepare(&authctxt); | |
372 | authctxt.server_user = server_user; | |
373 | authctxt.local_user = local_user; | |
374 | authctxt.host = host; | |
375 | authctxt.service = "ssh-connection"; /* service name */ | |
376 | authctxt.success = 0; | |
377 | authctxt.method = authmethod_lookup("none"); | |
378 | authctxt.authlist = NULL; | |
379 | authctxt.methoddata = NULL; | |
380 | authctxt.sensitive = sensitive; | |
381 | authctxt.info_req_seen = 0; | |
382 | if (authctxt.method == NULL) | |
383 | fatal("ssh_userauth2: internal error: cannot send userauth none request"); | |
384 | ||
385 | /* initial userauth request */ | |
386 | userauth_none(&authctxt); | |
387 | ||
388 | dispatch_init(&input_userauth_error); | |
389 | dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success); | |
390 | dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure); | |
391 | dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner); | |
392 | dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */ | |
393 | ||
394 | pubkey_cleanup(&authctxt); | |
395 | dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL); | |
396 | ||
397 | /* if the user wants to use the none cipher do it */ | |
398 | /* post authentication and only if the right conditions are met */ | |
399 | /* both of the NONE commands must be true and there must be no */ | |
400 | /* tty allocated */ | |
401 | if ((options.none_switch == 1) && (options.none_enabled == 1)) | |
402 | { | |
403 | if (!tty_flag) /* no null on tty sessions */ | |
404 | { | |
405 | debug("Requesting none rekeying..."); | |
406 | myproposal[PROPOSAL_ENC_ALGS_STOC] = "none"; | |
407 | myproposal[PROPOSAL_ENC_ALGS_CTOS] = "none"; | |
408 | kex_prop2buf(&xxx_kex->my,myproposal); | |
409 | packet_request_rekeying(); | |
410 | fprintf(stderr, "WARNING: ENABLED NONE CIPHER\n"); | |
411 | } | |
412 | else | |
413 | { | |
414 | /* requested NONE cipher when in a tty */ | |
415 | debug("Cannot switch to NONE cipher with tty allocated"); | |
416 | fprintf(stderr, "NONE cipher switch disabled when a TTY is allocated\n"); | |
417 | } | |
418 | } | |
419 | debug("Authentication succeeded (%s).", authctxt.method->name); | |
420 | } | |
421 | ||
422 | void | |
423 | userauth(Authctxt *authctxt, char *authlist) | |
424 | { | |
425 | if (authctxt->methoddata) { | |
426 | xfree(authctxt->methoddata); | |
427 | authctxt->methoddata = NULL; | |
428 | } | |
429 | if (authlist == NULL) { | |
430 | authlist = authctxt->authlist; | |
431 | } else { | |
432 | if (authctxt->authlist) | |
433 | xfree(authctxt->authlist); | |
434 | authctxt->authlist = authlist; | |
435 | } | |
436 | for (;;) { | |
437 | Authmethod *method = authmethod_get(authlist); | |
438 | if (method == NULL) | |
439 | fatal("Permission denied (%s).", authlist); | |
440 | authctxt->method = method; | |
441 | ||
442 | /* reset the per method handler */ | |
443 | dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN, | |
444 | SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL); | |
445 | ||
446 | /* and try new method */ | |
447 | if (method->userauth(authctxt) != 0) { | |
448 | debug2("we sent a %s packet, wait for reply", method->name); | |
449 | break; | |
450 | } else { | |
451 | debug2("we did not send a packet, disable method"); | |
452 | method->enabled = NULL; | |
453 | } | |
454 | } | |
455 | } | |
456 | ||
457 | void | |
458 | input_userauth_error(int type, u_int32_t seq, void *ctxt) | |
459 | { | |
460 | fatal("input_userauth_error: bad message during authentication: " | |
461 | "type %d", type); | |
462 | } | |
463 | ||
464 | void | |
465 | input_userauth_banner(int type, u_int32_t seq, void *ctxt) | |
466 | { | |
467 | char *msg, *lang; | |
468 | ||
469 | debug3("input_userauth_banner"); | |
470 | msg = packet_get_string(NULL); | |
471 | lang = packet_get_string(NULL); | |
472 | if (options.log_level >= SYSLOG_LEVEL_INFO) | |
473 | fprintf(stderr, "%s", msg); | |
474 | xfree(msg); | |
475 | xfree(lang); | |
476 | } | |
477 | ||
478 | void | |
479 | input_userauth_success(int type, u_int32_t seq, void *ctxt) | |
480 | { | |
481 | Authctxt *authctxt = ctxt; | |
482 | if (authctxt == NULL) | |
483 | fatal("input_userauth_success: no authentication context"); | |
484 | if (authctxt->authlist) { | |
485 | xfree(authctxt->authlist); | |
486 | authctxt->authlist = NULL; | |
487 | } | |
488 | if (authctxt->methoddata) { | |
489 | xfree(authctxt->methoddata); | |
490 | authctxt->methoddata = NULL; | |
491 | } | |
492 | authctxt->success = 1; /* break out */ | |
493 | } | |
494 | ||
495 | void | |
496 | input_userauth_failure(int type, u_int32_t seq, void *ctxt) | |
497 | { | |
498 | Authctxt *authctxt = ctxt; | |
499 | char *authlist = NULL; | |
500 | int partial; | |
501 | ||
502 | if (authctxt == NULL) | |
503 | fatal("input_userauth_failure: no authentication context"); | |
504 | ||
505 | authlist = packet_get_string(NULL); | |
506 | partial = packet_get_char(); | |
507 | packet_check_eom(); | |
508 | ||
509 | if (partial != 0) | |
510 | logit("Authenticated with partial success."); | |
511 | debug("Authentications that can continue: %s", authlist); | |
512 | ||
513 | userauth(authctxt, authlist); | |
514 | } | |
515 | void | |
516 | input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt) | |
517 | { | |
518 | Authctxt *authctxt = ctxt; | |
519 | Key *key = NULL; | |
520 | Identity *id = NULL; | |
521 | Buffer b; | |
522 | int pktype, sent = 0; | |
523 | u_int alen, blen; | |
524 | char *pkalg, *fp; | |
525 | u_char *pkblob; | |
526 | ||
527 | if (authctxt == NULL) | |
528 | fatal("input_userauth_pk_ok: no authentication context"); | |
529 | if (datafellows & SSH_BUG_PKOK) { | |
530 | /* this is similar to SSH_BUG_PKAUTH */ | |
531 | debug2("input_userauth_pk_ok: SSH_BUG_PKOK"); | |
532 | pkblob = packet_get_string(&blen); | |
533 | buffer_init(&b); | |
534 | buffer_append(&b, pkblob, blen); | |
535 | pkalg = buffer_get_string(&b, &alen); | |
536 | buffer_free(&b); | |
537 | } else { | |
538 | pkalg = packet_get_string(&alen); | |
539 | pkblob = packet_get_string(&blen); | |
540 | } | |
541 | packet_check_eom(); | |
542 | ||
543 | debug("Server accepts key: pkalg %s blen %u", pkalg, blen); | |
544 | ||
545 | if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) { | |
546 | debug("unknown pkalg %s", pkalg); | |
547 | goto done; | |
548 | } | |
549 | if ((key = key_from_blob(pkblob, blen)) == NULL) { | |
550 | debug("no key from blob. pkalg %s", pkalg); | |
551 | goto done; | |
552 | } | |
553 | if (key->type != pktype) { | |
554 | error("input_userauth_pk_ok: type mismatch " | |
555 | "for decoded key (received %d, expected %d)", | |
556 | key->type, pktype); | |
557 | goto done; | |
558 | } | |
559 | fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX); | |
560 | debug2("input_userauth_pk_ok: fp %s", fp); | |
561 | xfree(fp); | |
562 | ||
563 | /* | |
564 | * search keys in the reverse order, because last candidate has been | |
565 | * moved to the end of the queue. this also avoids confusion by | |
566 | * duplicate keys | |
567 | */ | |
568 | TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) { | |
569 | if (key_equal(key, id->key)) { | |
570 | sent = sign_and_send_pubkey(authctxt, id); | |
571 | break; | |
572 | } | |
573 | } | |
574 | done: | |
575 | if (key != NULL) | |
576 | key_free(key); | |
577 | xfree(pkalg); | |
578 | xfree(pkblob); | |
579 | ||
580 | /* try another method if we did not send a packet */ | |
581 | if (sent == 0) | |
582 | userauth(authctxt, NULL); | |
583 | } | |
584 | ||
585 | #ifdef GSSAPI | |
586 | int | |
587 | userauth_gssapi(Authctxt *authctxt) | |
588 | { | |
589 | Gssctxt *gssctxt = NULL; | |
590 | static gss_OID_set gss_supported = NULL; | |
591 | static u_int mech = 0; | |
592 | OM_uint32 min; | |
593 | int ok = 0; | |
594 | char *gss_host = NULL; | |
595 | ||
596 | if (!options.gss_authentication) { | |
597 | verbose("GSSAPI authentication disabled."); | |
598 | return 0; | |
599 | } | |
600 | ||
601 | if (options.gss_trust_dns) | |
602 | gss_host = (char *)get_canonical_hostname(1); | |
603 | else | |
604 | gss_host = (char *)authctxt->host; | |
605 | ||
606 | /* Try one GSSAPI method at a time, rather than sending them all at | |
607 | * once. */ | |
608 | ||
609 | if (gss_supported == NULL) | |
610 | gss_indicate_mechs(&min, &gss_supported); | |
611 | ||
612 | /* Check to see if the mechanism is usable before we offer it */ | |
613 | while (mech < gss_supported->count && !ok) { | |
614 | /* My DER encoding requires length<128 */ | |
615 | if (gss_supported->elements[mech].length < 128 && | |
616 | ssh_gssapi_check_mechanism(&gssctxt, | |
617 | &gss_supported->elements[mech], gss_host)) { | |
618 | ok = 1; /* Mechanism works */ | |
619 | } else { | |
620 | mech++; | |
621 | } | |
622 | } | |
623 | ||
624 | if (!ok) | |
625 | return 0; | |
626 | ||
627 | authctxt->methoddata=(void *)gssctxt; | |
628 | ||
629 | packet_start(SSH2_MSG_USERAUTH_REQUEST); | |
630 | packet_put_cstring(authctxt->server_user); | |
631 | packet_put_cstring(authctxt->service); | |
632 | packet_put_cstring(authctxt->method->name); | |
633 | ||
634 | packet_put_int(1); | |
635 | ||
636 | packet_put_int((gss_supported->elements[mech].length) + 2); | |
637 | packet_put_char(SSH_GSS_OIDTYPE); | |
638 | packet_put_char(gss_supported->elements[mech].length); | |
639 | packet_put_raw(gss_supported->elements[mech].elements, | |
640 | gss_supported->elements[mech].length); | |
641 | ||
642 | packet_send(); | |
643 | ||
644 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response); | |
645 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token); | |
646 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error); | |
647 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok); | |
648 | ||
649 | mech++; /* Move along to next candidate */ | |
650 | ||
651 | return 1; | |
652 | } | |
653 | ||
654 | static OM_uint32 | |
655 | process_gssapi_token(void *ctxt, gss_buffer_t recv_tok) | |
656 | { | |
657 | Authctxt *authctxt = ctxt; | |
658 | Gssctxt *gssctxt = authctxt->methoddata; | |
659 | gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER; | |
660 | gss_buffer_desc mic = GSS_C_EMPTY_BUFFER; | |
661 | gss_buffer_desc gssbuf; | |
662 | OM_uint32 status, ms, flags; | |
663 | Buffer b; | |
664 | ||
665 | status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds, | |
666 | recv_tok, &send_tok, &flags); | |
667 | ||
668 | if (send_tok.length > 0) { | |
669 | if (GSS_ERROR(status)) | |
670 | packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK); | |
671 | else | |
672 | packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN); | |
673 | ||
674 | packet_put_string(send_tok.value, send_tok.length); | |
675 | packet_send(); | |
676 | gss_release_buffer(&ms, &send_tok); | |
677 | } | |
678 | ||
679 | if (status == GSS_S_COMPLETE) { | |
680 | /* send either complete or MIC, depending on mechanism */ | |
681 | if (strcmp(authctxt->method->name,"gssapi")==0 || | |
682 | (!(flags & GSS_C_INTEG_FLAG))) { | |
683 | packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE); | |
684 | packet_send(); | |
685 | } else { | |
686 | ssh_gssapi_buildmic(&b, authctxt->server_user, | |
687 | authctxt->service, "gssapi-with-mic"); | |
688 | ||
689 | gssbuf.value = buffer_ptr(&b); | |
690 | gssbuf.length = buffer_len(&b); | |
691 | ||
692 | status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic); | |
693 | ||
694 | if (!GSS_ERROR(status)) { | |
695 | packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC); | |
696 | packet_put_string(mic.value, mic.length); | |
697 | ||
698 | packet_send(); | |
699 | } | |
700 | ||
701 | buffer_free(&b); | |
702 | gss_release_buffer(&ms, &mic); | |
703 | } | |
704 | } | |
705 | ||
706 | return status; | |
707 | } | |
708 | ||
709 | void | |
710 | input_gssapi_response(int type, u_int32_t plen, void *ctxt) | |
711 | { | |
712 | Authctxt *authctxt = ctxt; | |
713 | Gssctxt *gssctxt; | |
714 | u_int oidlen; | |
715 | u_char *oidv; | |
716 | ||
717 | if (authctxt == NULL) | |
718 | fatal("input_gssapi_response: no authentication context"); | |
719 | gssctxt = authctxt->methoddata; | |
720 | ||
721 | /* Setup our OID */ | |
722 | oidv = packet_get_string(&oidlen); | |
723 | ||
724 | if (oidlen <= 2 || | |
725 | oidv[0] != SSH_GSS_OIDTYPE || | |
726 | oidv[1] != oidlen - 2) { | |
727 | xfree(oidv); | |
728 | debug("Badly encoded mechanism OID received"); | |
729 | userauth(authctxt, NULL); | |
730 | return; | |
731 | } | |
732 | ||
733 | if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2)) | |
734 | fatal("Server returned different OID than expected"); | |
735 | ||
736 | packet_check_eom(); | |
737 | ||
738 | xfree(oidv); | |
739 | ||
740 | if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) { | |
741 | /* Start again with next method on list */ | |
742 | debug("Trying to start again"); | |
743 | userauth(authctxt, NULL); | |
744 | return; | |
745 | } | |
746 | } | |
747 | ||
748 | void | |
749 | input_gssapi_token(int type, u_int32_t plen, void *ctxt) | |
750 | { | |
751 | Authctxt *authctxt = ctxt; | |
752 | gss_buffer_desc recv_tok; | |
753 | OM_uint32 status; | |
754 | u_int slen; | |
755 | ||
756 | if (authctxt == NULL) | |
757 | fatal("input_gssapi_response: no authentication context"); | |
758 | ||
759 | recv_tok.value = packet_get_string(&slen); | |
760 | recv_tok.length = slen; /* safe typecast */ | |
761 | ||
762 | packet_check_eom(); | |
763 | ||
764 | status = process_gssapi_token(ctxt, &recv_tok); | |
765 | ||
766 | xfree(recv_tok.value); | |
767 | ||
768 | if (GSS_ERROR(status)) { | |
769 | /* Start again with the next method in the list */ | |
770 | userauth(authctxt, NULL); | |
771 | return; | |
772 | } | |
773 | } | |
774 | ||
775 | void | |
776 | input_gssapi_errtok(int type, u_int32_t plen, void *ctxt) | |
777 | { | |
778 | Authctxt *authctxt = ctxt; | |
779 | Gssctxt *gssctxt; | |
780 | gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER; | |
781 | gss_buffer_desc recv_tok; | |
782 | OM_uint32 status, ms; | |
783 | u_int len; | |
784 | ||
785 | if (authctxt == NULL) | |
786 | fatal("input_gssapi_response: no authentication context"); | |
787 | gssctxt = authctxt->methoddata; | |
788 | ||
789 | recv_tok.value = packet_get_string(&len); | |
790 | recv_tok.length = len; | |
791 | ||
792 | packet_check_eom(); | |
793 | ||
794 | /* Stick it into GSSAPI and see what it says */ | |
795 | status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds, | |
796 | &recv_tok, &send_tok, NULL); | |
797 | ||
798 | xfree(recv_tok.value); | |
799 | gss_release_buffer(&ms, &send_tok); | |
800 | ||
801 | /* Server will be returning a failed packet after this one */ | |
802 | } | |
803 | ||
804 | void | |
805 | input_gssapi_error(int type, u_int32_t plen, void *ctxt) | |
806 | { | |
807 | OM_uint32 maj, min; | |
808 | char *msg; | |
809 | char *lang; | |
810 | ||
811 | maj=packet_get_int(); | |
812 | min=packet_get_int(); | |
813 | msg=packet_get_string(NULL); | |
814 | lang=packet_get_string(NULL); | |
815 | ||
816 | packet_check_eom(); | |
817 | ||
818 | debug("Server GSSAPI Error:\n%s", msg); | |
819 | xfree(msg); | |
820 | xfree(lang); | |
821 | } | |
822 | ||
823 | #ifdef GSI | |
824 | extern | |
825 | const gss_OID_desc * const gss_mech_globus_gssapi_openssl; | |
826 | #define is_gsi_oid(oid) \ | |
827 | (oid->length == gss_mech_globus_gssapi_openssl->length && \ | |
828 | (memcmp(oid->elements, gss_mech_globus_gssapi_openssl->elements, \ | |
829 | oid->length) == 0)) | |
830 | #endif | |
831 | ||
832 | int | |
833 | userauth_external(Authctxt *authctxt) | |
834 | { | |
835 | static int attempt = 0; | |
836 | ||
837 | if (attempt++ >= 1) | |
838 | return 0; | |
839 | ||
840 | /* The client MUST NOT try this method if initial key exchange | |
841 | was not performed using a GSSAPI-based key exchange | |
842 | method. */ | |
843 | if (gss_kex_context == NULL) { | |
844 | debug2("gsskex not performed, skipping external-keyx"); | |
845 | return 0; | |
846 | } | |
847 | ||
848 | debug2("userauth_external"); | |
849 | packet_start(SSH2_MSG_USERAUTH_REQUEST); | |
850 | #ifdef GSI | |
851 | if (options.implicit && is_gsi_oid(gss_kex_context->oid)) { | |
852 | packet_put_cstring(""); | |
853 | } else { | |
854 | #endif | |
855 | packet_put_cstring(authctxt->server_user); | |
856 | #ifdef GSI | |
857 | } | |
858 | #endif | |
859 | packet_put_cstring(authctxt->service); | |
860 | packet_put_cstring(authctxt->method->name); | |
861 | packet_send(); | |
862 | packet_write_wait(); | |
863 | return 1; | |
864 | } | |
865 | int | |
866 | userauth_gsskeyex(Authctxt *authctxt) | |
867 | { | |
868 | Buffer b; | |
869 | gss_buffer_desc gssbuf; | |
870 | gss_buffer_desc mic = GSS_C_EMPTY_BUFFER; | |
871 | OM_uint32 ms; | |
872 | ||
873 | static int attempt = 0; | |
874 | if (attempt++ >= 1) | |
875 | return (0); | |
876 | ||
877 | if (gss_kex_context == NULL) { | |
878 | debug("No valid Key exchange context"); | |
879 | return (0); | |
880 | } | |
881 | ||
882 | #ifdef GSI | |
883 | if (options.implicit && is_gsi_oid(gss_kex_context->oid)) { | |
884 | ssh_gssapi_buildmic(&b, "", authctxt->service, "gssapi-keyex"); | |
885 | } else { | |
886 | #endif | |
887 | ssh_gssapi_buildmic(&b, authctxt->server_user, authctxt->service, | |
888 | "gssapi-keyex"); | |
889 | #ifdef GSI | |
890 | } | |
891 | #endif | |
892 | ||
893 | gssbuf.value = buffer_ptr(&b); | |
894 | gssbuf.length = buffer_len(&b); | |
895 | ||
896 | if (GSS_ERROR(ssh_gssapi_sign(gss_kex_context, &gssbuf, &mic))) { | |
897 | buffer_free(&b); | |
898 | return (0); | |
899 | } | |
900 | ||
901 | packet_start(SSH2_MSG_USERAUTH_REQUEST); | |
902 | #ifdef GSI | |
903 | if (options.implicit && is_gsi_oid(gss_kex_context->oid)) { | |
904 | packet_put_cstring(""); | |
905 | } else { | |
906 | #endif | |
907 | packet_put_cstring(authctxt->server_user); | |
908 | #ifdef GSI | |
909 | } | |
910 | #endif | |
911 | packet_put_cstring(authctxt->service); | |
912 | packet_put_cstring(authctxt->method->name); | |
913 | packet_put_string(mic.value, mic.length); | |
914 | packet_send(); | |
915 | ||
916 | buffer_free(&b); | |
917 | gss_release_buffer(&ms, &mic); | |
918 | ||
919 | return (1); | |
920 | } | |
921 | ||
922 | #endif /* GSSAPI */ | |
923 | ||
924 | int | |
925 | userauth_none(Authctxt *authctxt) | |
926 | { | |
927 | /* initial userauth request */ | |
928 | packet_start(SSH2_MSG_USERAUTH_REQUEST); | |
929 | packet_put_cstring(authctxt->server_user); | |
930 | packet_put_cstring(authctxt->service); | |
931 | packet_put_cstring(authctxt->method->name); | |
932 | packet_send(); | |
933 | return 1; | |
934 | } | |
935 | ||
936 | int | |
937 | userauth_passwd(Authctxt *authctxt) | |
938 | { | |
939 | static int attempt = 0; | |
940 | char prompt[150]; | |
941 | char *password; | |
942 | ||
943 | if (attempt++ >= options.number_of_password_prompts) | |
944 | return 0; | |
945 | ||
946 | if (attempt != 1) | |
947 | error("Permission denied, please try again."); | |
948 | ||
949 | snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ", | |
950 | authctxt->server_user, authctxt->host); | |
951 | password = read_passphrase(prompt, 0); | |
952 | packet_start(SSH2_MSG_USERAUTH_REQUEST); | |
953 | packet_put_cstring(authctxt->server_user); | |
954 | packet_put_cstring(authctxt->service); | |
955 | packet_put_cstring(authctxt->method->name); | |
956 | packet_put_char(0); | |
957 | packet_put_cstring(password); | |
958 | memset(password, 0, strlen(password)); | |
959 | xfree(password); | |
960 | packet_add_padding(64); | |
961 | packet_send(); | |
962 | ||
963 | dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, | |
964 | &input_userauth_passwd_changereq); | |
965 | ||
966 | return 1; | |
967 | } | |
968 | /* | |
969 | * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST | |
970 | */ | |
971 | void | |
972 | input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt) | |
973 | { | |
974 | Authctxt *authctxt = ctxt; | |
975 | char *info, *lang, *password = NULL, *retype = NULL; | |
976 | char prompt[150]; | |
977 | ||
978 | debug2("input_userauth_passwd_changereq"); | |
979 | ||
980 | if (authctxt == NULL) | |
981 | fatal("input_userauth_passwd_changereq: " | |
982 | "no authentication context"); | |
983 | ||
984 | info = packet_get_string(NULL); | |
985 | lang = packet_get_string(NULL); | |
986 | if (strlen(info) > 0) | |
987 | logit("%s", info); | |
988 | xfree(info); | |
989 | xfree(lang); | |
990 | packet_start(SSH2_MSG_USERAUTH_REQUEST); | |
991 | packet_put_cstring(authctxt->server_user); | |
992 | packet_put_cstring(authctxt->service); | |
993 | packet_put_cstring(authctxt->method->name); | |
994 | packet_put_char(1); /* additional info */ | |
995 | snprintf(prompt, sizeof(prompt), | |
996 | "Enter %.30s@%.128s's old password: ", | |
997 | authctxt->server_user, authctxt->host); | |
998 | password = read_passphrase(prompt, 0); | |
999 | packet_put_cstring(password); | |
1000 | memset(password, 0, strlen(password)); | |
1001 | xfree(password); | |
1002 | password = NULL; | |
1003 | while (password == NULL) { | |
1004 | snprintf(prompt, sizeof(prompt), | |
1005 | "Enter %.30s@%.128s's new password: ", | |
1006 | authctxt->server_user, authctxt->host); | |
1007 | password = read_passphrase(prompt, RP_ALLOW_EOF); | |
1008 | if (password == NULL) { | |
1009 | /* bail out */ | |
1010 | return; | |
1011 | } | |
1012 | snprintf(prompt, sizeof(prompt), | |
1013 | "Retype %.30s@%.128s's new password: ", | |
1014 | authctxt->server_user, authctxt->host); | |
1015 | retype = read_passphrase(prompt, 0); | |
1016 | if (strcmp(password, retype) != 0) { | |
1017 | memset(password, 0, strlen(password)); | |
1018 | xfree(password); | |
1019 | logit("Mismatch; try again, EOF to quit."); | |
1020 | password = NULL; | |
1021 | } | |
1022 | memset(retype, 0, strlen(retype)); | |
1023 | xfree(retype); | |
1024 | } | |
1025 | packet_put_cstring(password); | |
1026 | memset(password, 0, strlen(password)); | |
1027 | xfree(password); | |
1028 | packet_add_padding(64); | |
1029 | packet_send(); | |
1030 | ||
1031 | dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, | |
1032 | &input_userauth_passwd_changereq); | |
1033 | } | |
1034 | ||
1035 | static int | |
1036 | identity_sign(Identity *id, u_char **sigp, u_int *lenp, | |
1037 | u_char *data, u_int datalen) | |
1038 | { | |
1039 | Key *prv; | |
1040 | int ret; | |
1041 | ||
1042 | /* the agent supports this key */ | |
1043 | if (id->ac) | |
1044 | return (ssh_agent_sign(id->ac, id->key, sigp, lenp, | |
1045 | data, datalen)); | |
1046 | /* | |
1047 | * we have already loaded the private key or | |
1048 | * the private key is stored in external hardware | |
1049 | */ | |
1050 | if (id->isprivate || (id->key->flags & KEY_FLAG_EXT)) | |
1051 | return (key_sign(id->key, sigp, lenp, data, datalen)); | |
1052 | /* load the private key from the file */ | |
1053 | if ((prv = load_identity_file(id->filename)) == NULL) | |
1054 | return (-1); | |
1055 | ret = key_sign(prv, sigp, lenp, data, datalen); | |
1056 | key_free(prv); | |
1057 | return (ret); | |
1058 | } | |
1059 | ||
1060 | static int | |
1061 | sign_and_send_pubkey(Authctxt *authctxt, Identity *id) | |
1062 | { | |
1063 | Buffer b; | |
1064 | u_char *blob, *signature; | |
1065 | u_int bloblen, slen; | |
1066 | u_int skip = 0; | |
1067 | int ret = -1; | |
1068 | int have_sig = 1; | |
1069 | ||
1070 | debug3("sign_and_send_pubkey"); | |
1071 | ||
1072 | if (key_to_blob(id->key, &blob, &bloblen) == 0) { | |
1073 | /* we cannot handle this key */ | |
1074 | debug3("sign_and_send_pubkey: cannot handle key"); | |
1075 | return 0; | |
1076 | } | |
1077 | /* data to be signed */ | |
1078 | buffer_init(&b); | |
1079 | if (datafellows & SSH_OLD_SESSIONID) { | |
1080 | buffer_append(&b, session_id2, session_id2_len); | |
1081 | skip = session_id2_len; | |
1082 | } else { | |
1083 | buffer_put_string(&b, session_id2, session_id2_len); | |
1084 | skip = buffer_len(&b); | |
1085 | } | |
1086 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); | |
1087 | buffer_put_cstring(&b, authctxt->server_user); | |
1088 | buffer_put_cstring(&b, | |
1089 | datafellows & SSH_BUG_PKSERVICE ? | |
1090 | "ssh-userauth" : | |
1091 | authctxt->service); | |
1092 | if (datafellows & SSH_BUG_PKAUTH) { | |
1093 | buffer_put_char(&b, have_sig); | |
1094 | } else { | |
1095 | buffer_put_cstring(&b, authctxt->method->name); | |
1096 | buffer_put_char(&b, have_sig); | |
1097 | buffer_put_cstring(&b, key_ssh_name(id->key)); | |
1098 | } | |
1099 | buffer_put_string(&b, blob, bloblen); | |
1100 | ||
1101 | /* generate signature */ | |
1102 | ret = identity_sign(id, &signature, &slen, | |
1103 | buffer_ptr(&b), buffer_len(&b)); | |
1104 | if (ret == -1) { | |
1105 | xfree(blob); | |
1106 | buffer_free(&b); | |
1107 | return 0; | |
1108 | } | |
1109 | #ifdef DEBUG_PK | |
1110 | buffer_dump(&b); | |
1111 | #endif | |
1112 | if (datafellows & SSH_BUG_PKSERVICE) { | |
1113 | buffer_clear(&b); | |
1114 | buffer_append(&b, session_id2, session_id2_len); | |
1115 | skip = session_id2_len; | |
1116 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); | |
1117 | buffer_put_cstring(&b, authctxt->server_user); | |
1118 | buffer_put_cstring(&b, authctxt->service); | |
1119 | buffer_put_cstring(&b, authctxt->method->name); | |
1120 | buffer_put_char(&b, have_sig); | |
1121 | if (!(datafellows & SSH_BUG_PKAUTH)) | |
1122 | buffer_put_cstring(&b, key_ssh_name(id->key)); | |
1123 | buffer_put_string(&b, blob, bloblen); | |
1124 | } | |
1125 | xfree(blob); | |
1126 | ||
1127 | /* append signature */ | |
1128 | buffer_put_string(&b, signature, slen); | |
1129 | xfree(signature); | |
1130 | ||
1131 | /* skip session id and packet type */ | |
1132 | if (buffer_len(&b) < skip + 1) | |
1133 | fatal("userauth_pubkey: internal error"); | |
1134 | buffer_consume(&b, skip + 1); | |
1135 | ||
1136 | /* put remaining data from buffer into packet */ | |
1137 | packet_start(SSH2_MSG_USERAUTH_REQUEST); | |
1138 | packet_put_raw(buffer_ptr(&b), buffer_len(&b)); | |
1139 | buffer_free(&b); | |
1140 | packet_send(); | |
1141 | ||
1142 | return 1; | |
1143 | } | |
1144 | ||
1145 | static int | |
1146 | send_pubkey_test(Authctxt *authctxt, Identity *id) | |
1147 | { | |
1148 | u_char *blob; | |
1149 | u_int bloblen, have_sig = 0; | |
1150 | ||
1151 | debug3("send_pubkey_test"); | |
1152 | ||
1153 | if (key_to_blob(id->key, &blob, &bloblen) == 0) { | |
1154 | /* we cannot handle this key */ | |
1155 | debug3("send_pubkey_test: cannot handle key"); | |
1156 | return 0; | |
1157 | } | |
1158 | /* register callback for USERAUTH_PK_OK message */ | |
1159 | dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok); | |
1160 | ||
1161 | packet_start(SSH2_MSG_USERAUTH_REQUEST); | |
1162 | packet_put_cstring(authctxt->server_user); | |
1163 | packet_put_cstring(authctxt->service); | |
1164 | packet_put_cstring(authctxt->method->name); | |
1165 | packet_put_char(have_sig); | |
1166 | if (!(datafellows & SSH_BUG_PKAUTH)) | |
1167 | packet_put_cstring(key_ssh_name(id->key)); | |
1168 | packet_put_string(blob, bloblen); | |
1169 | xfree(blob); | |
1170 | packet_send(); | |
1171 | return 1; | |
1172 | } | |
1173 | ||
1174 | static Key * | |
1175 | load_identity_file(char *filename) | |
1176 | { | |
1177 | Key *private; | |
1178 | char prompt[300], *passphrase; | |
1179 | int perm_ok, quit, i; | |
1180 | struct stat st; | |
1181 | ||
1182 | if (stat(filename, &st) < 0) { | |
1183 | debug3("no such identity: %s", filename); | |
1184 | return NULL; | |
1185 | } | |
1186 | private = key_load_private_type(KEY_UNSPEC, filename, "", NULL, &perm_ok); | |
1187 | if (!perm_ok) | |
1188 | return NULL; | |
1189 | if (private == NULL) { | |
1190 | if (options.batch_mode) | |
1191 | return NULL; | |
1192 | snprintf(prompt, sizeof prompt, | |
1193 | "Enter passphrase for key '%.100s': ", filename); | |
1194 | for (i = 0; i < options.number_of_password_prompts; i++) { | |
1195 | passphrase = read_passphrase(prompt, 0); | |
1196 | if (strcmp(passphrase, "") != 0) { | |
1197 | private = key_load_private_type(KEY_UNSPEC, | |
1198 | filename, passphrase, NULL, NULL); | |
1199 | quit = 0; | |
1200 | } else { | |
1201 | debug2("no passphrase given, try next key"); | |
1202 | quit = 1; | |
1203 | } | |
1204 | memset(passphrase, 0, strlen(passphrase)); | |
1205 | xfree(passphrase); | |
1206 | if (private != NULL || quit) | |
1207 | break; | |
1208 | debug2("bad passphrase given, try again..."); | |
1209 | } | |
1210 | } | |
1211 | return private; | |
1212 | } | |
1213 | ||
1214 | /* | |
1215 | * try keys in the following order: | |
1216 | * 1. agent keys that are found in the config file | |
1217 | * 2. other agent keys | |
1218 | * 3. keys that are only listed in the config file | |
1219 | */ | |
1220 | static void | |
1221 | pubkey_prepare(Authctxt *authctxt) | |
1222 | { | |
1223 | Identity *id; | |
1224 | Idlist agent, files, *preferred; | |
1225 | Key *key; | |
1226 | AuthenticationConnection *ac; | |
1227 | char *comment; | |
1228 | int i, found; | |
1229 | ||
1230 | TAILQ_INIT(&agent); /* keys from the agent */ | |
1231 | TAILQ_INIT(&files); /* keys from the config file */ | |
1232 | preferred = &authctxt->keys; | |
1233 | TAILQ_INIT(preferred); /* preferred order of keys */ | |
1234 | ||
1235 | /* list of keys stored in the filesystem */ | |
1236 | for (i = 0; i < options.num_identity_files; i++) { | |
1237 | key = options.identity_keys[i]; | |
1238 | if (key && key->type == KEY_RSA1) | |
1239 | continue; | |
1240 | options.identity_keys[i] = NULL; | |
1241 | id = xcalloc(1, sizeof(*id)); | |
1242 | id->key = key; | |
1243 | id->filename = xstrdup(options.identity_files[i]); | |
1244 | TAILQ_INSERT_TAIL(&files, id, next); | |
1245 | } | |
1246 | /* list of keys supported by the agent */ | |
1247 | if ((ac = ssh_get_authentication_connection())) { | |
1248 | for (key = ssh_get_first_identity(ac, &comment, 2); | |
1249 | key != NULL; | |
1250 | key = ssh_get_next_identity(ac, &comment, 2)) { | |
1251 | found = 0; | |
1252 | TAILQ_FOREACH(id, &files, next) { | |
1253 | /* agent keys from the config file are preferred */ | |
1254 | if (key_equal(key, id->key)) { | |
1255 | key_free(key); | |
1256 | xfree(comment); | |
1257 | TAILQ_REMOVE(&files, id, next); | |
1258 | TAILQ_INSERT_TAIL(preferred, id, next); | |
1259 | id->ac = ac; | |
1260 | found = 1; | |
1261 | break; | |
1262 | } | |
1263 | } | |
1264 | if (!found && !options.identities_only) { | |
1265 | id = xcalloc(1, sizeof(*id)); | |
1266 | id->key = key; | |
1267 | id->filename = comment; | |
1268 | id->ac = ac; | |
1269 | TAILQ_INSERT_TAIL(&agent, id, next); | |
1270 | } | |
1271 | } | |
1272 | /* append remaining agent keys */ | |
1273 | for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) { | |
1274 | TAILQ_REMOVE(&agent, id, next); | |
1275 | TAILQ_INSERT_TAIL(preferred, id, next); | |
1276 | } | |
1277 | authctxt->agent = ac; | |
1278 | } | |
1279 | /* append remaining keys from the config file */ | |
1280 | for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) { | |
1281 | TAILQ_REMOVE(&files, id, next); | |
1282 | TAILQ_INSERT_TAIL(preferred, id, next); | |
1283 | } | |
1284 | TAILQ_FOREACH(id, preferred, next) { | |
1285 | debug2("key: %s (%p)", id->filename, id->key); | |
1286 | } | |
1287 | } | |
1288 | ||
1289 | static void | |
1290 | pubkey_cleanup(Authctxt *authctxt) | |
1291 | { | |
1292 | Identity *id; | |
1293 | ||
1294 | if (authctxt->agent != NULL) | |
1295 | ssh_close_authentication_connection(authctxt->agent); | |
1296 | for (id = TAILQ_FIRST(&authctxt->keys); id; | |
1297 | id = TAILQ_FIRST(&authctxt->keys)) { | |
1298 | TAILQ_REMOVE(&authctxt->keys, id, next); | |
1299 | if (id->key) | |
1300 | key_free(id->key); | |
1301 | if (id->filename) | |
1302 | xfree(id->filename); | |
1303 | xfree(id); | |
1304 | } | |
1305 | } | |
1306 | ||
1307 | int | |
1308 | userauth_pubkey(Authctxt *authctxt) | |
1309 | { | |
1310 | Identity *id; | |
1311 | int sent = 0; | |
1312 | ||
1313 | while ((id = TAILQ_FIRST(&authctxt->keys))) { | |
1314 | if (id->tried++) | |
1315 | return (0); | |
1316 | /* move key to the end of the queue */ | |
1317 | TAILQ_REMOVE(&authctxt->keys, id, next); | |
1318 | TAILQ_INSERT_TAIL(&authctxt->keys, id, next); | |
1319 | /* | |
1320 | * send a test message if we have the public key. for | |
1321 | * encrypted keys we cannot do this and have to load the | |
1322 | * private key instead | |
1323 | */ | |
1324 | if (id->key && id->key->type != KEY_RSA1) { | |
1325 | debug("Offering public key: %s", id->filename); | |
1326 | sent = send_pubkey_test(authctxt, id); | |
1327 | } else if (id->key == NULL) { | |
1328 | debug("Trying private key: %s", id->filename); | |
1329 | id->key = load_identity_file(id->filename); | |
1330 | if (id->key != NULL) { | |
1331 | id->isprivate = 1; | |
1332 | sent = sign_and_send_pubkey(authctxt, id); | |
1333 | key_free(id->key); | |
1334 | id->key = NULL; | |
1335 | } | |
1336 | } | |
1337 | if (sent) | |
1338 | return (sent); | |
1339 | } | |
1340 | return (0); | |
1341 | } | |
1342 | ||
1343 | /* | |
1344 | * Send userauth request message specifying keyboard-interactive method. | |
1345 | */ | |
1346 | int | |
1347 | userauth_kbdint(Authctxt *authctxt) | |
1348 | { | |
1349 | static int attempt = 0; | |
1350 | ||
1351 | if (attempt++ >= options.number_of_password_prompts) | |
1352 | return 0; | |
1353 | /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */ | |
1354 | if (attempt > 1 && !authctxt->info_req_seen) { | |
1355 | debug3("userauth_kbdint: disable: no info_req_seen"); | |
1356 | dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL); | |
1357 | return 0; | |
1358 | } | |
1359 | ||
1360 | debug2("userauth_kbdint"); | |
1361 | packet_start(SSH2_MSG_USERAUTH_REQUEST); | |
1362 | packet_put_cstring(authctxt->server_user); | |
1363 | packet_put_cstring(authctxt->service); | |
1364 | packet_put_cstring(authctxt->method->name); | |
1365 | packet_put_cstring(""); /* lang */ | |
1366 | packet_put_cstring(options.kbd_interactive_devices ? | |
1367 | options.kbd_interactive_devices : ""); | |
1368 | packet_send(); | |
1369 | ||
1370 | dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req); | |
1371 | return 1; | |
1372 | } | |
1373 | ||
1374 | /* | |
1375 | * parse INFO_REQUEST, prompt user and send INFO_RESPONSE | |
1376 | */ | |
1377 | void | |
1378 | input_userauth_info_req(int type, u_int32_t seq, void *ctxt) | |
1379 | { | |
1380 | Authctxt *authctxt = ctxt; | |
1381 | char *name, *inst, *lang, *prompt, *response; | |
1382 | u_int num_prompts, i; | |
1383 | int echo = 0; | |
1384 | ||
1385 | debug2("input_userauth_info_req"); | |
1386 | ||
1387 | if (authctxt == NULL) | |
1388 | fatal("input_userauth_info_req: no authentication context"); | |
1389 | ||
1390 | authctxt->info_req_seen = 1; | |
1391 | ||
1392 | name = packet_get_string(NULL); | |
1393 | inst = packet_get_string(NULL); | |
1394 | lang = packet_get_string(NULL); | |
1395 | if (strlen(name) > 0) | |
1396 | logit("%s", name); | |
1397 | if (strlen(inst) > 0) | |
1398 | logit("%s", inst); | |
1399 | xfree(name); | |
1400 | xfree(inst); | |
1401 | xfree(lang); | |
1402 | ||
1403 | num_prompts = packet_get_int(); | |
1404 | /* | |
1405 | * Begin to build info response packet based on prompts requested. | |
1406 | * We commit to providing the correct number of responses, so if | |
1407 | * further on we run into a problem that prevents this, we have to | |
1408 | * be sure and clean this up and send a correct error response. | |
1409 | */ | |
1410 | packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE); | |
1411 | packet_put_int(num_prompts); | |
1412 | ||
1413 | debug2("input_userauth_info_req: num_prompts %d", num_prompts); | |
1414 | for (i = 0; i < num_prompts; i++) { | |
1415 | prompt = packet_get_string(NULL); | |
1416 | echo = packet_get_char(); | |
1417 | ||
1418 | response = read_passphrase(prompt, echo ? RP_ECHO : 0); | |
1419 | ||
1420 | packet_put_cstring(response); | |
1421 | memset(response, 0, strlen(response)); | |
1422 | xfree(response); | |
1423 | xfree(prompt); | |
1424 | } | |
1425 | packet_check_eom(); /* done with parsing incoming message. */ | |
1426 | ||
1427 | packet_add_padding(64); | |
1428 | packet_send(); | |
1429 | } | |
1430 | ||
1431 | static int | |
1432 | ssh_keysign(Key *key, u_char **sigp, u_int *lenp, | |
1433 | u_char *data, u_int datalen) | |
1434 | { | |
1435 | Buffer b; | |
1436 | struct stat st; | |
1437 | pid_t pid; | |
1438 | int to[2], from[2], status, version = 2; | |
1439 | ||
1440 | debug2("ssh_keysign called"); | |
1441 | ||
1442 | if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) { | |
1443 | error("ssh_keysign: no installed: %s", strerror(errno)); | |
1444 | return -1; | |
1445 | } | |
1446 | if (fflush(stdout) != 0) | |
1447 | error("ssh_keysign: fflush: %s", strerror(errno)); | |
1448 | if (pipe(to) < 0) { | |
1449 | error("ssh_keysign: pipe: %s", strerror(errno)); | |
1450 | return -1; | |
1451 | } | |
1452 | if (pipe(from) < 0) { | |
1453 | error("ssh_keysign: pipe: %s", strerror(errno)); | |
1454 | return -1; | |
1455 | } | |
1456 | if ((pid = fork()) < 0) { | |
1457 | error("ssh_keysign: fork: %s", strerror(errno)); | |
1458 | return -1; | |
1459 | } | |
1460 | if (pid == 0) { | |
1461 | permanently_drop_suid(getuid()); | |
1462 | close(from[0]); | |
1463 | if (dup2(from[1], STDOUT_FILENO) < 0) | |
1464 | fatal("ssh_keysign: dup2: %s", strerror(errno)); | |
1465 | close(to[1]); | |
1466 | if (dup2(to[0], STDIN_FILENO) < 0) | |
1467 | fatal("ssh_keysign: dup2: %s", strerror(errno)); | |
1468 | close(from[1]); | |
1469 | close(to[0]); | |
1470 | execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0); | |
1471 | fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN, | |
1472 | strerror(errno)); | |
1473 | } | |
1474 | close(from[1]); | |
1475 | close(to[0]); | |
1476 | ||
1477 | buffer_init(&b); | |
1478 | buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */ | |
1479 | buffer_put_string(&b, data, datalen); | |
1480 | if (ssh_msg_send(to[1], version, &b) == -1) | |
1481 | fatal("ssh_keysign: couldn't send request"); | |
1482 | ||
1483 | if (ssh_msg_recv(from[0], &b) < 0) { | |
1484 | error("ssh_keysign: no reply"); | |
1485 | buffer_free(&b); | |
1486 | return -1; | |
1487 | } | |
1488 | close(from[0]); | |
1489 | close(to[1]); | |
1490 | ||
1491 | while (waitpid(pid, &status, 0) < 0) | |
1492 | if (errno != EINTR) | |
1493 | break; | |
1494 | ||
1495 | if (buffer_get_char(&b) != version) { | |
1496 | error("ssh_keysign: bad version"); | |
1497 | buffer_free(&b); | |
1498 | return -1; | |
1499 | } | |
1500 | *sigp = buffer_get_string(&b, lenp); | |
1501 | buffer_free(&b); | |
1502 | ||
1503 | return 0; | |
1504 | } | |
1505 | ||
1506 | int | |
1507 | userauth_hostbased(Authctxt *authctxt) | |
1508 | { | |
1509 | Key *private = NULL; | |
1510 | Sensitive *sensitive = authctxt->sensitive; | |
1511 | Buffer b; | |
1512 | u_char *signature, *blob; | |
1513 | char *chost, *pkalg, *p; | |
1514 | const char *service; | |
1515 | u_int blen, slen; | |
1516 | int ok, i, len, found = 0; | |
1517 | ||
1518 | /* check for a useful key */ | |
1519 | for (i = 0; i < sensitive->nkeys; i++) { | |
1520 | private = sensitive->keys[i]; | |
1521 | if (private && private->type != KEY_RSA1) { | |
1522 | found = 1; | |
1523 | /* we take and free the key */ | |
1524 | sensitive->keys[i] = NULL; | |
1525 | break; | |
1526 | } | |
1527 | } | |
1528 | if (!found) { | |
1529 | debug("No more client hostkeys for hostbased authentication."); | |
1530 | return 0; | |
1531 | } | |
1532 | if (key_to_blob(private, &blob, &blen) == 0) { | |
1533 | key_free(private); | |
1534 | return 0; | |
1535 | } | |
1536 | /* figure out a name for the client host */ | |
1537 | p = get_local_name(packet_get_connection_in()); | |
1538 | if (p == NULL) { | |
1539 | error("userauth_hostbased: cannot get local ipaddr/name"); | |
1540 | key_free(private); | |
1541 | xfree(blob); | |
1542 | return 0; | |
1543 | } | |
1544 | len = strlen(p) + 2; | |
1545 | xasprintf(&chost, "%s.", p); | |
1546 | debug2("userauth_hostbased: chost %s", chost); | |
1547 | xfree(p); | |
1548 | ||
1549 | service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" : | |
1550 | authctxt->service; | |
1551 | pkalg = xstrdup(key_ssh_name(private)); | |
1552 | buffer_init(&b); | |
1553 | /* construct data */ | |
1554 | buffer_put_string(&b, session_id2, session_id2_len); | |
1555 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); | |
1556 | buffer_put_cstring(&b, authctxt->server_user); | |
1557 | buffer_put_cstring(&b, service); | |
1558 | buffer_put_cstring(&b, authctxt->method->name); | |
1559 | buffer_put_cstring(&b, pkalg); | |
1560 | buffer_put_string(&b, blob, blen); | |
1561 | buffer_put_cstring(&b, chost); | |
1562 | buffer_put_cstring(&b, authctxt->local_user); | |
1563 | #ifdef DEBUG_PK | |
1564 | buffer_dump(&b); | |
1565 | #endif | |
1566 | if (sensitive->external_keysign) | |
1567 | ok = ssh_keysign(private, &signature, &slen, | |
1568 | buffer_ptr(&b), buffer_len(&b)); | |
1569 | else | |
1570 | ok = key_sign(private, &signature, &slen, | |
1571 | buffer_ptr(&b), buffer_len(&b)); | |
1572 | key_free(private); | |
1573 | buffer_free(&b); | |
1574 | if (ok != 0) { | |
1575 | error("key_sign failed"); | |
1576 | xfree(chost); | |
1577 | xfree(pkalg); | |
1578 | xfree(blob); | |
1579 | return 0; | |
1580 | } | |
1581 | packet_start(SSH2_MSG_USERAUTH_REQUEST); | |
1582 | packet_put_cstring(authctxt->server_user); | |
1583 | packet_put_cstring(authctxt->service); | |
1584 | packet_put_cstring(authctxt->method->name); | |
1585 | packet_put_cstring(pkalg); | |
1586 | packet_put_string(blob, blen); | |
1587 | packet_put_cstring(chost); | |
1588 | packet_put_cstring(authctxt->local_user); | |
1589 | packet_put_string(signature, slen); | |
1590 | memset(signature, 's', slen); | |
1591 | xfree(signature); | |
1592 | xfree(chost); | |
1593 | xfree(pkalg); | |
1594 | xfree(blob); | |
1595 | ||
1596 | packet_send(); | |
1597 | return 1; | |
1598 | } | |
1599 | ||
1600 | /* find auth method */ | |
1601 | ||
1602 | /* | |
1603 | * given auth method name, if configurable options permit this method fill | |
1604 | * in auth_ident field and return true, otherwise return false. | |
1605 | */ | |
1606 | static int | |
1607 | authmethod_is_enabled(Authmethod *method) | |
1608 | { | |
1609 | if (method == NULL) | |
1610 | return 0; | |
1611 | /* return false if options indicate this method is disabled */ | |
1612 | if (method->enabled == NULL || *method->enabled == 0) | |
1613 | return 0; | |
1614 | /* return false if batch mode is enabled but method needs interactive mode */ | |
1615 | if (method->batch_flag != NULL && *method->batch_flag != 0) | |
1616 | return 0; | |
1617 | return 1; | |
1618 | } | |
1619 | ||
1620 | static Authmethod * | |
1621 | authmethod_lookup(const char *name) | |
1622 | { | |
1623 | Authmethod *method = NULL; | |
1624 | if (name != NULL) | |
1625 | for (method = authmethods; method->name != NULL; method++) | |
1626 | if (strcmp(name, method->name) == 0) | |
1627 | return method; | |
1628 | debug2("Unrecognized authentication method name: %s", name ? name : "NULL"); | |
1629 | return NULL; | |
1630 | } | |
1631 | ||
1632 | /* XXX internal state */ | |
1633 | static Authmethod *current = NULL; | |
1634 | static char *supported = NULL; | |
1635 | static char *preferred = NULL; | |
1636 | ||
1637 | /* | |
1638 | * Given the authentication method list sent by the server, return the | |
1639 | * next method we should try. If the server initially sends a nil list, | |
1640 | * use a built-in default list. | |
1641 | */ | |
1642 | static Authmethod * | |
1643 | authmethod_get(char *authlist) | |
1644 | { | |
1645 | char *name = NULL; | |
1646 | u_int next; | |
1647 | ||
1648 | /* Use a suitable default if we're passed a nil list. */ | |
1649 | if (authlist == NULL || strlen(authlist) == 0) | |
1650 | authlist = options.preferred_authentications; | |
1651 | ||
1652 | if (supported == NULL || strcmp(authlist, supported) != 0) { | |
1653 | debug3("start over, passed a different list %s", authlist); | |
1654 | if (supported != NULL) | |
1655 | xfree(supported); | |
1656 | supported = xstrdup(authlist); | |
1657 | preferred = options.preferred_authentications; | |
1658 | debug3("preferred %s", preferred); | |
1659 | current = NULL; | |
1660 | } else if (current != NULL && authmethod_is_enabled(current)) | |
1661 | return current; | |
1662 | ||
1663 | for (;;) { | |
1664 | if ((name = match_list(preferred, supported, &next)) == NULL) { | |
1665 | debug("No more authentication methods to try."); | |
1666 | current = NULL; | |
1667 | return NULL; | |
1668 | } | |
1669 | preferred += next; | |
1670 | debug3("authmethod_lookup %s", name); | |
1671 | debug3("remaining preferred: %s", preferred); | |
1672 | if ((current = authmethod_lookup(name)) != NULL && | |
1673 | authmethod_is_enabled(current)) { | |
1674 | debug3("authmethod_is_enabled %s", name); | |
1675 | debug("Next authentication method: %s", name); | |
1676 | return current; | |
1677 | } | |
1678 | } | |
1679 | } | |
1680 | ||
1681 | static char * | |
1682 | authmethods_get(void) | |
1683 | { | |
1684 | Authmethod *method = NULL; | |
1685 | Buffer b; | |
1686 | char *list; | |
1687 | ||
1688 | buffer_init(&b); | |
1689 | for (method = authmethods; method->name != NULL; method++) { | |
1690 | if (authmethod_is_enabled(method)) { | |
1691 | if (buffer_len(&b) > 0) | |
1692 | buffer_append(&b, ",", 1); | |
1693 | buffer_append(&b, method->name, strlen(method->name)); | |
1694 | } | |
1695 | } | |
1696 | buffer_append(&b, "\0", 1); | |
1697 | list = xstrdup(buffer_ptr(&b)); | |
1698 | buffer_free(&b); | |
1699 | return list; | |
1700 | } |