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