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