]>
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" | |
44a053a3 | 26 | RCSID("$OpenBSD: sshconnect2.c,v 1.104 2002/06/19 00:27:55 deraadt 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" | |
44a053a3 | 48 | #include "msg.h" |
49 | #include "pathnames.h" | |
3c0ef626 | 50 | |
5598e598 | 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; | |
1e608e42 | 84 | #ifdef GSSAPI |
85 | char *orig, *gss; | |
86 | int len; | |
87 | #endif | |
3c0ef626 | 88 | |
89 | xxx_host = host; | |
90 | xxx_hostaddr = hostaddr; | |
91 | ||
5598e598 | 92 | #ifdef GSSAPI |
1e608e42 | 93 | /* Add the GSSAPI mechanisms currently supported on this client to |
94 | * the key exchange algorithm proposal */ | |
95 | orig = myproposal[PROPOSAL_KEX_ALGS]; | |
96 | gss = ssh_gssapi_mechanisms(0,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); | |
5598e598 | 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] = | |
118 | myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib"; | |
119 | } else { | |
120 | myproposal[PROPOSAL_COMP_ALGS_CTOS] = | |
121 | myproposal[PROPOSAL_COMP_ALGS_STOC] = "none"; | |
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) | |
1e608e42 | 128 | myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = |
3c0ef626 | 129 | options.hostkeyalgorithms; |
130 | ||
1e608e42 | 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 | |
141 | ||
3c0ef626 | 142 | /* start key exchange */ |
143 | kex = kex_setup(myproposal); | |
144 | kex->client_version_string=client_version_string; | |
145 | kex->server_version_string=server_version_string; | |
146 | kex->verify_host_key=&verify_host_key_callback; | |
5598e598 | 147 | kex->host=host; |
148 | ||
149 | #ifdef GSSAPI | |
150 | kex->options.gss_deleg_creds=options.gss_deleg_creds; | |
151 | #endif | |
3c0ef626 | 152 | |
153 | xxx_kex = kex; | |
154 | ||
155 | dispatch_run(DISPATCH_BLOCK, &kex->done, kex); | |
156 | ||
157 | session_id2 = kex->session_id; | |
158 | session_id2_len = kex->session_id_len; | |
159 | ||
160 | #ifdef DEBUG_KEXDH | |
161 | /* send 1st encrypted/maced/compressed message */ | |
162 | packet_start(SSH2_MSG_IGNORE); | |
163 | packet_put_cstring("markus"); | |
164 | packet_send(); | |
165 | packet_write_wait(); | |
166 | #endif | |
167 | debug("done: ssh_kex2."); | |
168 | } | |
169 | ||
170 | /* | |
171 | * Authenticate user | |
172 | */ | |
173 | ||
174 | typedef struct Authctxt Authctxt; | |
175 | typedef struct Authmethod Authmethod; | |
176 | ||
177 | typedef int sign_cb_fn( | |
178 | Authctxt *authctxt, Key *key, | |
1e608e42 | 179 | u_char **sigp, u_int *lenp, u_char *data, u_int datalen); |
3c0ef626 | 180 | |
181 | struct Authctxt { | |
182 | const char *server_user; | |
183 | const char *local_user; | |
184 | const char *host; | |
185 | const char *service; | |
186 | Authmethod *method; | |
187 | int success; | |
188 | char *authlist; | |
189 | /* pubkey */ | |
190 | Key *last_key; | |
191 | sign_cb_fn *last_key_sign; | |
192 | int last_key_hint; | |
193 | AuthenticationConnection *agent; | |
194 | /* hostbased */ | |
44a053a3 | 195 | Sensitive *sensitive; |
3c0ef626 | 196 | /* kbd-interactive */ |
197 | int info_req_seen; | |
5598e598 | 198 | /* generic */ |
199 | void *methoddata; | |
3c0ef626 | 200 | }; |
201 | struct Authmethod { | |
202 | char *name; /* string to compare against server's list */ | |
203 | int (*userauth)(Authctxt *authctxt); | |
204 | int *enabled; /* flag in option struct that enables method */ | |
205 | int *batch_flag; /* flag in option struct that disables method */ | |
206 | }; | |
207 | ||
1e608e42 | 208 | void input_userauth_success(int, u_int32_t, void *); |
209 | void input_userauth_failure(int, u_int32_t, void *); | |
210 | void input_userauth_banner(int, u_int32_t, void *); | |
211 | void input_userauth_error(int, u_int32_t, void *); | |
212 | void input_userauth_info_req(int, u_int32_t, void *); | |
213 | void input_userauth_pk_ok(int, u_int32_t, void *); | |
510132b6 | 214 | void input_userauth_passwd_changereq(int, u_int32_t, void *); |
3c0ef626 | 215 | |
1e608e42 | 216 | int userauth_none(Authctxt *); |
217 | int userauth_pubkey(Authctxt *); | |
218 | int userauth_passwd(Authctxt *); | |
219 | int userauth_kbdint(Authctxt *); | |
220 | int userauth_hostbased(Authctxt *); | |
3c0ef626 | 221 | |
5598e598 | 222 | #ifdef GSSAPI |
1e608e42 | 223 | int userauth_external(Authctxt *authctxt); |
224 | int userauth_gssapi(Authctxt *authctxt); | |
225 | void input_gssapi_response(int type, u_int32_t plen, void *ctxt); | |
226 | void input_gssapi_token(int type, u_int32_t plen, void *ctxt); | |
227 | void input_gssapi_hash(int type, u_int32_t plen, void *ctxt); | |
5598e598 | 228 | #endif |
229 | ||
1e608e42 | 230 | void userauth(Authctxt *, char *); |
3c0ef626 | 231 | |
232 | static int sign_and_send_pubkey(Authctxt *, Key *, sign_cb_fn *); | |
233 | static void clear_auth_state(Authctxt *); | |
234 | ||
235 | static Authmethod *authmethod_get(char *authlist); | |
236 | static Authmethod *authmethod_lookup(const char *name); | |
237 | static char *authmethods_get(void); | |
238 | ||
239 | Authmethod authmethods[] = { | |
5598e598 | 240 | #ifdef GSSAPI |
241 | {"external-keyx", | |
242 | userauth_external, | |
243 | &options.gss_authentication, | |
244 | NULL}, | |
245 | {"gssapi", | |
246 | userauth_gssapi, | |
247 | &options.gss_authentication, | |
248 | NULL}, | |
249 | #endif | |
3c0ef626 | 250 | {"hostbased", |
251 | userauth_hostbased, | |
252 | &options.hostbased_authentication, | |
253 | NULL}, | |
254 | {"publickey", | |
255 | userauth_pubkey, | |
256 | &options.pubkey_authentication, | |
257 | NULL}, | |
258 | {"keyboard-interactive", | |
259 | userauth_kbdint, | |
260 | &options.kbd_interactive_authentication, | |
261 | &options.batch_mode}, | |
262 | {"password", | |
263 | userauth_passwd, | |
264 | &options.password_authentication, | |
265 | &options.batch_mode}, | |
266 | {"none", | |
267 | userauth_none, | |
268 | NULL, | |
269 | NULL}, | |
270 | {NULL, NULL, NULL, NULL} | |
271 | }; | |
272 | ||
273 | void | |
274 | ssh_userauth2(const char *local_user, const char *server_user, char *host, | |
44a053a3 | 275 | Sensitive *sensitive) |
3c0ef626 | 276 | { |
277 | Authctxt authctxt; | |
278 | int type; | |
3c0ef626 | 279 | |
280 | if (options.challenge_response_authentication) | |
281 | options.kbd_interactive_authentication = 1; | |
282 | ||
283 | debug("send SSH2_MSG_SERVICE_REQUEST"); | |
284 | packet_start(SSH2_MSG_SERVICE_REQUEST); | |
285 | packet_put_cstring("ssh-userauth"); | |
286 | packet_send(); | |
287 | packet_write_wait(); | |
1e608e42 | 288 | type = packet_read(); |
3c0ef626 | 289 | if (type != SSH2_MSG_SERVICE_ACCEPT) { |
290 | fatal("denied SSH2_MSG_SERVICE_ACCEPT: %d", type); | |
291 | } | |
292 | if (packet_remaining() > 0) { | |
1e608e42 | 293 | char *reply = packet_get_string(NULL); |
3c0ef626 | 294 | debug("service_accept: %s", reply); |
295 | xfree(reply); | |
296 | } else { | |
297 | debug("buggy server: service_accept w/o service"); | |
298 | } | |
1e608e42 | 299 | packet_check_eom(); |
3c0ef626 | 300 | debug("got SSH2_MSG_SERVICE_ACCEPT"); |
301 | ||
302 | if (options.preferred_authentications == NULL) | |
303 | options.preferred_authentications = authmethods_get(); | |
304 | ||
305 | /* setup authentication context */ | |
306 | memset(&authctxt, 0, sizeof(authctxt)); | |
307 | authctxt.agent = ssh_get_authentication_connection(); | |
308 | authctxt.server_user = server_user; | |
309 | authctxt.local_user = local_user; | |
310 | authctxt.host = host; | |
311 | authctxt.service = "ssh-connection"; /* service name */ | |
312 | authctxt.success = 0; | |
313 | authctxt.method = authmethod_lookup("none"); | |
314 | authctxt.authlist = NULL; | |
5598e598 | 315 | authctxt.methoddata = NULL; |
44a053a3 | 316 | authctxt.sensitive = sensitive; |
3c0ef626 | 317 | authctxt.info_req_seen = 0; |
318 | if (authctxt.method == NULL) | |
319 | fatal("ssh_userauth2: internal error: cannot send userauth none request"); | |
320 | ||
321 | /* initial userauth request */ | |
322 | userauth_none(&authctxt); | |
323 | ||
324 | dispatch_init(&input_userauth_error); | |
325 | dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success); | |
326 | dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure); | |
327 | dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner); | |
328 | dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */ | |
329 | ||
330 | if (authctxt.agent != NULL) | |
331 | ssh_close_authentication_connection(authctxt.agent); | |
332 | ||
333 | debug("ssh-userauth2 successful: method %s", authctxt.method->name); | |
334 | } | |
335 | void | |
336 | userauth(Authctxt *authctxt, char *authlist) | |
337 | { | |
5598e598 | 338 | if (authctxt->methoddata!=NULL) { |
339 | xfree(authctxt->methoddata); | |
340 | authctxt->methoddata=NULL; | |
341 | } | |
342 | ||
3c0ef626 | 343 | if (authlist == NULL) { |
344 | authlist = authctxt->authlist; | |
345 | } else { | |
346 | if (authctxt->authlist) | |
347 | xfree(authctxt->authlist); | |
348 | authctxt->authlist = authlist; | |
349 | } | |
350 | for (;;) { | |
351 | Authmethod *method = authmethod_get(authlist); | |
352 | if (method == NULL) | |
353 | fatal("Permission denied (%s).", authlist); | |
354 | authctxt->method = method; | |
355 | if (method->userauth(authctxt) != 0) { | |
356 | debug2("we sent a %s packet, wait for reply", method->name); | |
357 | break; | |
358 | } else { | |
359 | debug2("we did not send a packet, disable method"); | |
360 | method->enabled = NULL; | |
361 | } | |
362 | } | |
363 | } | |
364 | void | |
1e608e42 | 365 | input_userauth_error(int type, u_int32_t seq, void *ctxt) |
3c0ef626 | 366 | { |
367 | fatal("input_userauth_error: bad message during authentication: " | |
368 | "type %d", type); | |
369 | } | |
370 | void | |
1e608e42 | 371 | input_userauth_banner(int type, u_int32_t seq, void *ctxt) |
3c0ef626 | 372 | { |
373 | char *msg, *lang; | |
374 | debug3("input_userauth_banner"); | |
375 | msg = packet_get_string(NULL); | |
376 | lang = packet_get_string(NULL); | |
377 | fprintf(stderr, "%s", msg); | |
378 | xfree(msg); | |
379 | xfree(lang); | |
380 | } | |
381 | void | |
1e608e42 | 382 | input_userauth_success(int type, u_int32_t seq, void *ctxt) |
3c0ef626 | 383 | { |
384 | Authctxt *authctxt = ctxt; | |
385 | if (authctxt == NULL) | |
386 | fatal("input_userauth_success: no authentication context"); | |
387 | if (authctxt->authlist) | |
388 | xfree(authctxt->authlist); | |
5598e598 | 389 | if (authctxt->methoddata) |
390 | xfree(authctxt->methoddata); | |
3c0ef626 | 391 | clear_auth_state(authctxt); |
392 | authctxt->success = 1; /* break out */ | |
393 | } | |
394 | void | |
1e608e42 | 395 | input_userauth_failure(int type, u_int32_t seq, void *ctxt) |
3c0ef626 | 396 | { |
397 | Authctxt *authctxt = ctxt; | |
398 | char *authlist = NULL; | |
399 | int partial; | |
400 | ||
401 | if (authctxt == NULL) | |
402 | fatal("input_userauth_failure: no authentication context"); | |
403 | ||
404 | authlist = packet_get_string(NULL); | |
405 | partial = packet_get_char(); | |
1e608e42 | 406 | packet_check_eom(); |
3c0ef626 | 407 | |
408 | if (partial != 0) | |
409 | log("Authenticated with partial success."); | |
410 | debug("authentications that can continue: %s", authlist); | |
411 | ||
412 | clear_auth_state(authctxt); | |
413 | userauth(authctxt, authlist); | |
414 | } | |
415 | void | |
1e608e42 | 416 | input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt) |
3c0ef626 | 417 | { |
418 | Authctxt *authctxt = ctxt; | |
419 | Key *key = NULL; | |
420 | Buffer b; | |
1e608e42 | 421 | int pktype, sent = 0; |
422 | u_int alen, blen; | |
423 | char *pkalg, *fp; | |
424 | u_char *pkblob; | |
3c0ef626 | 425 | |
426 | if (authctxt == NULL) | |
427 | fatal("input_userauth_pk_ok: no authentication context"); | |
428 | if (datafellows & SSH_BUG_PKOK) { | |
429 | /* this is similar to SSH_BUG_PKAUTH */ | |
430 | debug2("input_userauth_pk_ok: SSH_BUG_PKOK"); | |
431 | pkblob = packet_get_string(&blen); | |
432 | buffer_init(&b); | |
433 | buffer_append(&b, pkblob, blen); | |
434 | pkalg = buffer_get_string(&b, &alen); | |
435 | buffer_free(&b); | |
436 | } else { | |
437 | pkalg = packet_get_string(&alen); | |
438 | pkblob = packet_get_string(&blen); | |
439 | } | |
1e608e42 | 440 | packet_check_eom(); |
3c0ef626 | 441 | |
442 | debug("input_userauth_pk_ok: pkalg %s blen %d lastkey %p hint %d", | |
443 | pkalg, blen, authctxt->last_key, authctxt->last_key_hint); | |
444 | ||
445 | do { | |
446 | if (authctxt->last_key == NULL || | |
447 | authctxt->last_key_sign == NULL) { | |
448 | debug("no last key or no sign cb"); | |
449 | break; | |
450 | } | |
1e608e42 | 451 | if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) { |
3c0ef626 | 452 | debug("unknown pkalg %s", pkalg); |
453 | break; | |
454 | } | |
455 | if ((key = key_from_blob(pkblob, blen)) == NULL) { | |
456 | debug("no key from blob. pkalg %s", pkalg); | |
457 | break; | |
458 | } | |
510132b6 | 459 | if (key->type != pktype) { |
1e608e42 | 460 | error("input_userauth_pk_ok: type mismatch " |
461 | "for decoded key (received %d, expected %d)", | |
44a053a3 | 462 | key->type, pktype); |
1e608e42 | 463 | break; |
464 | } | |
3c0ef626 | 465 | fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX); |
466 | debug2("input_userauth_pk_ok: fp %s", fp); | |
467 | xfree(fp); | |
468 | if (!key_equal(key, authctxt->last_key)) { | |
469 | debug("key != last_key"); | |
470 | break; | |
471 | } | |
472 | sent = sign_and_send_pubkey(authctxt, key, | |
473 | authctxt->last_key_sign); | |
1e608e42 | 474 | } while (0); |
3c0ef626 | 475 | |
476 | if (key != NULL) | |
477 | key_free(key); | |
478 | xfree(pkalg); | |
479 | xfree(pkblob); | |
480 | ||
481 | /* unregister */ | |
482 | clear_auth_state(authctxt); | |
483 | dispatch_set(SSH2_MSG_USERAUTH_PK_OK, NULL); | |
484 | ||
485 | /* try another method if we did not send a packet*/ | |
486 | if (sent == 0) | |
487 | userauth(authctxt, NULL); | |
488 | ||
489 | } | |
490 | ||
5598e598 | 491 | #ifdef GSSAPI |
492 | int | |
493 | userauth_gssapi(Authctxt *authctxt) | |
494 | { | |
495 | int i; | |
496 | Gssctxt *gssctxt; | |
497 | static int tries=0; | |
498 | ||
499 | /* For now, we only make one attempt at this. We could try offering | |
500 | * the server different GSSAPI OIDs until we get bored, I suppose. | |
501 | */ | |
502 | if (tries++>0) return 0; | |
503 | ||
1e608e42 | 504 | if (datafellows & SSH_OLD_GSSAPI) return 0; |
505 | ||
5598e598 | 506 | gssctxt=xmalloc(sizeof(Gssctxt)); |
507 | ||
508 | /* Initialise as much of our context as we can, so failures can be | |
509 | * trapped before sending any packets. | |
510 | */ | |
511 | ssh_gssapi_build_ctx(gssctxt); | |
512 | if (ssh_gssapi_import_name(gssctxt,authctxt->host)) { | |
513 | return(0); | |
514 | } | |
515 | authctxt->methoddata=(void *)gssctxt; | |
516 | ||
517 | packet_start(SSH2_MSG_USERAUTH_REQUEST); | |
2e9d2d36 | 518 | if(options.implicit && !(datafellows & SSH_BUG_GSS_EMPTYUSER)) { |
519 | packet_put_cstring(""); | |
520 | } else { | |
521 | packet_put_cstring(authctxt->server_user); | |
522 | } | |
5598e598 | 523 | packet_put_cstring(authctxt->service); |
524 | packet_put_cstring(authctxt->method->name); | |
525 | ||
526 | /* FIXME: This assumes that our current GSSAPI implementation | |
527 | * supports all of the mechanisms listed in supported_mechs. | |
528 | * This may not be the case - we should use something along | |
529 | * the lines of the code in gss_genr to remove the ones that | |
530 | * aren't supported */ | |
531 | packet_put_int(GSS_LAST_ENTRY); | |
532 | for (i=0;i<GSS_LAST_ENTRY;i++) { | |
533 | packet_put_string(supported_mechs[i].oid.elements, | |
534 | supported_mechs[i].oid.length); | |
535 | } | |
536 | packet_send(); | |
537 | packet_write_wait(); | |
538 | ||
539 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE,&input_gssapi_response); | |
540 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN,&input_gssapi_token); | |
541 | ||
542 | return 1; | |
543 | } | |
544 | ||
545 | void | |
1e608e42 | 546 | input_gssapi_response(int type, u_int32_t plen, void *ctxt) |
5598e598 | 547 | { |
548 | Authctxt *authctxt = ctxt; | |
549 | Gssctxt *gssctxt; | |
550 | OM_uint32 status,ms; | |
551 | int oidlen; | |
552 | char *oidv; | |
553 | gss_buffer_desc send_tok; | |
554 | ||
555 | if (authctxt == NULL) | |
556 | fatal("input_gssapi_response: no authentication context"); | |
557 | gssctxt = authctxt->methoddata; | |
558 | ||
559 | /* Setup our OID */ | |
560 | oidv=packet_get_string(&oidlen); | |
561 | ssh_gssapi_set_oid_data(gssctxt,oidv,oidlen); | |
562 | ||
1e608e42 | 563 | packet_check_eom(); |
5598e598 | 564 | |
565 | status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds, | |
566 | GSS_C_NO_BUFFER, &send_tok, | |
567 | NULL); | |
568 | if (GSS_ERROR(status)) { | |
569 | /* Start again with next method on list */ | |
570 | debug("Trying to start again"); | |
571 | userauth(authctxt,NULL); | |
572 | return; | |
573 | } | |
574 | ||
575 | /* We must have data to send */ | |
576 | packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN); | |
577 | packet_put_string(send_tok.value,send_tok.length); | |
578 | packet_send(); | |
579 | packet_write_wait(); | |
580 | gss_release_buffer(&ms, &send_tok); | |
581 | } | |
582 | ||
583 | void | |
1e608e42 | 584 | input_gssapi_token(int type, u_int32_t plen, void *ctxt) |
5598e598 | 585 | { |
586 | Authctxt *authctxt = ctxt; | |
587 | Gssctxt *gssctxt; | |
588 | gss_buffer_desc send_tok,recv_tok; | |
589 | OM_uint32 status; | |
590 | ||
591 | if (authctxt == NULL) | |
592 | fatal("input_gssapi_response: no authentication context"); | |
593 | gssctxt = authctxt->methoddata; | |
594 | ||
595 | recv_tok.value=packet_get_string(&recv_tok.length); | |
596 | ||
597 | status=ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds, | |
598 | &recv_tok, &send_tok, NULL); | |
599 | ||
1e608e42 | 600 | packet_check_eom(); |
5598e598 | 601 | |
602 | if (GSS_ERROR(status)) { | |
603 | /* Start again with the next method in the list */ | |
604 | userauth(authctxt,NULL); | |
605 | return; | |
606 | } | |
607 | ||
608 | if (send_tok.length>0) { | |
609 | packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN); | |
610 | packet_put_string(send_tok.value,send_tok.length); | |
611 | packet_send(); | |
612 | packet_write_wait(); | |
613 | } | |
614 | ||
615 | if (status == GSS_S_COMPLETE) { | |
616 | /* If that succeeded, send a exchange complete message */ | |
617 | packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE); | |
618 | packet_send(); | |
619 | packet_write_wait(); | |
620 | } | |
621 | } | |
622 | ||
623 | int | |
624 | userauth_external(Authctxt *authctxt) | |
625 | { | |
626 | static int attempt =0; | |
627 | ||
628 | if (attempt++ >= 1) | |
629 | return 0; | |
630 | ||
631 | debug2("userauth_external"); | |
632 | packet_start(SSH2_MSG_USERAUTH_REQUEST); | |
2e9d2d36 | 633 | if(options.implicit && !(datafellows & SSH_BUG_GSS_EMPTYUSER)) { |
634 | packet_put_cstring(""); | |
635 | } else { | |
636 | packet_put_cstring(authctxt->server_user); | |
637 | } | |
5598e598 | 638 | packet_put_cstring(authctxt->service); |
639 | packet_put_cstring(authctxt->method->name); | |
640 | packet_send(); | |
641 | packet_write_wait(); | |
642 | return 1; | |
643 | } | |
644 | #endif /* GSSAPI */ | |
645 | ||
3c0ef626 | 646 | int |
647 | userauth_none(Authctxt *authctxt) | |
648 | { | |
649 | /* initial userauth request */ | |
650 | packet_start(SSH2_MSG_USERAUTH_REQUEST); | |
651 | packet_put_cstring(authctxt->server_user); | |
652 | packet_put_cstring(authctxt->service); | |
653 | packet_put_cstring(authctxt->method->name); | |
654 | packet_send(); | |
655 | return 1; | |
5598e598 | 656 | |
3c0ef626 | 657 | } |
658 | ||
659 | int | |
660 | userauth_passwd(Authctxt *authctxt) | |
661 | { | |
662 | static int attempt = 0; | |
510132b6 | 663 | char prompt[150]; |
3c0ef626 | 664 | char *password; |
665 | ||
666 | if (attempt++ >= options.number_of_password_prompts) | |
667 | return 0; | |
668 | ||
1e608e42 | 669 | if (attempt != 1) |
3c0ef626 | 670 | error("Permission denied, please try again."); |
671 | ||
672 | snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ", | |
673 | authctxt->server_user, authctxt->host); | |
674 | password = read_passphrase(prompt, 0); | |
675 | packet_start(SSH2_MSG_USERAUTH_REQUEST); | |
676 | packet_put_cstring(authctxt->server_user); | |
677 | packet_put_cstring(authctxt->service); | |
678 | packet_put_cstring(authctxt->method->name); | |
679 | packet_put_char(0); | |
680 | packet_put_cstring(password); | |
681 | memset(password, 0, strlen(password)); | |
682 | xfree(password); | |
683 | packet_add_padding(64); | |
684 | packet_send(); | |
510132b6 | 685 | |
44a053a3 | 686 | dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, |
510132b6 | 687 | &input_userauth_passwd_changereq); |
688 | ||
3c0ef626 | 689 | return 1; |
690 | } | |
510132b6 | 691 | /* |
692 | * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST | |
693 | */ | |
694 | void | |
695 | input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt) | |
696 | { | |
697 | Authctxt *authctxt = ctxt; | |
698 | char *info, *lang, *password = NULL, *retype = NULL; | |
699 | char prompt[150]; | |
700 | ||
701 | debug2("input_userauth_passwd_changereq"); | |
702 | ||
703 | if (authctxt == NULL) | |
704 | fatal("input_userauth_passwd_changereq: " | |
705 | "no authentication context"); | |
706 | ||
707 | info = packet_get_string(NULL); | |
708 | lang = packet_get_string(NULL); | |
709 | if (strlen(info) > 0) | |
710 | log("%s", info); | |
711 | xfree(info); | |
712 | xfree(lang); | |
713 | packet_start(SSH2_MSG_USERAUTH_REQUEST); | |
714 | packet_put_cstring(authctxt->server_user); | |
715 | packet_put_cstring(authctxt->service); | |
716 | packet_put_cstring(authctxt->method->name); | |
717 | packet_put_char(1); /* additional info */ | |
44a053a3 | 718 | snprintf(prompt, sizeof(prompt), |
510132b6 | 719 | "Enter %.30s@%.128s's old password: ", |
720 | authctxt->server_user, authctxt->host); | |
721 | password = read_passphrase(prompt, 0); | |
722 | packet_put_cstring(password); | |
723 | memset(password, 0, strlen(password)); | |
724 | xfree(password); | |
725 | password = NULL; | |
726 | while (password == NULL) { | |
44a053a3 | 727 | snprintf(prompt, sizeof(prompt), |
510132b6 | 728 | "Enter %.30s@%.128s's new password: ", |
729 | authctxt->server_user, authctxt->host); | |
730 | password = read_passphrase(prompt, RP_ALLOW_EOF); | |
731 | if (password == NULL) { | |
732 | /* bail out */ | |
733 | return; | |
734 | } | |
44a053a3 | 735 | snprintf(prompt, sizeof(prompt), |
510132b6 | 736 | "Retype %.30s@%.128s's new password: ", |
737 | authctxt->server_user, authctxt->host); | |
738 | retype = read_passphrase(prompt, 0); | |
739 | if (strcmp(password, retype) != 0) { | |
740 | memset(password, 0, strlen(password)); | |
741 | xfree(password); | |
742 | log("Mismatch; try again, EOF to quit."); | |
743 | password = NULL; | |
744 | } | |
745 | memset(retype, 0, strlen(retype)); | |
746 | xfree(retype); | |
747 | } | |
748 | packet_put_cstring(password); | |
749 | memset(password, 0, strlen(password)); | |
750 | xfree(password); | |
751 | packet_add_padding(64); | |
752 | packet_send(); | |
44a053a3 | 753 | |
754 | dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, | |
510132b6 | 755 | &input_userauth_passwd_changereq); |
756 | } | |
3c0ef626 | 757 | |
758 | static void | |
759 | clear_auth_state(Authctxt *authctxt) | |
760 | { | |
761 | /* XXX clear authentication state */ | |
510132b6 | 762 | dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, NULL); |
763 | ||
3c0ef626 | 764 | if (authctxt->last_key != NULL && authctxt->last_key_hint == -1) { |
765 | debug3("clear_auth_state: key_free %p", authctxt->last_key); | |
766 | key_free(authctxt->last_key); | |
767 | } | |
768 | authctxt->last_key = NULL; | |
769 | authctxt->last_key_hint = -2; | |
770 | authctxt->last_key_sign = NULL; | |
771 | } | |
772 | ||
773 | static int | |
774 | sign_and_send_pubkey(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback) | |
775 | { | |
776 | Buffer b; | |
777 | u_char *blob, *signature; | |
1e608e42 | 778 | u_int bloblen, slen; |
3c0ef626 | 779 | int skip = 0; |
780 | int ret = -1; | |
781 | int have_sig = 1; | |
782 | ||
783 | debug3("sign_and_send_pubkey"); | |
784 | ||
785 | if (key_to_blob(k, &blob, &bloblen) == 0) { | |
786 | /* we cannot handle this key */ | |
787 | debug3("sign_and_send_pubkey: cannot handle key"); | |
788 | return 0; | |
789 | } | |
790 | /* data to be signed */ | |
791 | buffer_init(&b); | |
792 | if (datafellows & SSH_OLD_SESSIONID) { | |
793 | buffer_append(&b, session_id2, session_id2_len); | |
794 | skip = session_id2_len; | |
795 | } else { | |
796 | buffer_put_string(&b, session_id2, session_id2_len); | |
797 | skip = buffer_len(&b); | |
798 | } | |
799 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); | |
800 | buffer_put_cstring(&b, authctxt->server_user); | |
801 | buffer_put_cstring(&b, | |
802 | datafellows & SSH_BUG_PKSERVICE ? | |
803 | "ssh-userauth" : | |
804 | authctxt->service); | |
805 | if (datafellows & SSH_BUG_PKAUTH) { | |
806 | buffer_put_char(&b, have_sig); | |
807 | } else { | |
808 | buffer_put_cstring(&b, authctxt->method->name); | |
809 | buffer_put_char(&b, have_sig); | |
810 | buffer_put_cstring(&b, key_ssh_name(k)); | |
811 | } | |
812 | buffer_put_string(&b, blob, bloblen); | |
813 | ||
814 | /* generate signature */ | |
815 | ret = (*sign_callback)(authctxt, k, &signature, &slen, | |
816 | buffer_ptr(&b), buffer_len(&b)); | |
817 | if (ret == -1) { | |
818 | xfree(blob); | |
819 | buffer_free(&b); | |
820 | return 0; | |
821 | } | |
822 | #ifdef DEBUG_PK | |
823 | buffer_dump(&b); | |
824 | #endif | |
825 | if (datafellows & SSH_BUG_PKSERVICE) { | |
826 | buffer_clear(&b); | |
827 | buffer_append(&b, session_id2, session_id2_len); | |
828 | skip = session_id2_len; | |
829 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); | |
830 | buffer_put_cstring(&b, authctxt->server_user); | |
831 | buffer_put_cstring(&b, authctxt->service); | |
832 | buffer_put_cstring(&b, authctxt->method->name); | |
833 | buffer_put_char(&b, have_sig); | |
834 | if (!(datafellows & SSH_BUG_PKAUTH)) | |
835 | buffer_put_cstring(&b, key_ssh_name(k)); | |
836 | buffer_put_string(&b, blob, bloblen); | |
837 | } | |
838 | xfree(blob); | |
839 | ||
840 | /* append signature */ | |
841 | buffer_put_string(&b, signature, slen); | |
842 | xfree(signature); | |
843 | ||
844 | /* skip session id and packet type */ | |
845 | if (buffer_len(&b) < skip + 1) | |
846 | fatal("userauth_pubkey: internal error"); | |
847 | buffer_consume(&b, skip + 1); | |
848 | ||
849 | /* put remaining data from buffer into packet */ | |
850 | packet_start(SSH2_MSG_USERAUTH_REQUEST); | |
851 | packet_put_raw(buffer_ptr(&b), buffer_len(&b)); | |
852 | buffer_free(&b); | |
853 | packet_send(); | |
854 | ||
855 | return 1; | |
856 | } | |
857 | ||
858 | static int | |
859 | send_pubkey_test(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback, | |
860 | int hint) | |
861 | { | |
862 | u_char *blob; | |
1e608e42 | 863 | u_int bloblen, have_sig = 0; |
3c0ef626 | 864 | |
865 | debug3("send_pubkey_test"); | |
866 | ||
867 | if (key_to_blob(k, &blob, &bloblen) == 0) { | |
868 | /* we cannot handle this key */ | |
869 | debug3("send_pubkey_test: cannot handle key"); | |
870 | return 0; | |
871 | } | |
872 | /* register callback for USERAUTH_PK_OK message */ | |
873 | authctxt->last_key_sign = sign_callback; | |
874 | authctxt->last_key_hint = hint; | |
875 | authctxt->last_key = k; | |
876 | dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok); | |
877 | ||
878 | packet_start(SSH2_MSG_USERAUTH_REQUEST); | |
879 | packet_put_cstring(authctxt->server_user); | |
880 | packet_put_cstring(authctxt->service); | |
881 | packet_put_cstring(authctxt->method->name); | |
882 | packet_put_char(have_sig); | |
883 | if (!(datafellows & SSH_BUG_PKAUTH)) | |
884 | packet_put_cstring(key_ssh_name(k)); | |
885 | packet_put_string(blob, bloblen); | |
886 | xfree(blob); | |
887 | packet_send(); | |
888 | return 1; | |
889 | } | |
890 | ||
891 | static Key * | |
892 | load_identity_file(char *filename) | |
893 | { | |
894 | Key *private; | |
895 | char prompt[300], *passphrase; | |
896 | int quit, i; | |
897 | struct stat st; | |
898 | ||
899 | if (stat(filename, &st) < 0) { | |
900 | debug3("no such identity: %s", filename); | |
901 | return NULL; | |
902 | } | |
903 | private = key_load_private_type(KEY_UNSPEC, filename, "", NULL); | |
904 | if (private == NULL) { | |
905 | if (options.batch_mode) | |
906 | return NULL; | |
907 | snprintf(prompt, sizeof prompt, | |
1e608e42 | 908 | "Enter passphrase for key '%.100s': ", filename); |
3c0ef626 | 909 | for (i = 0; i < options.number_of_password_prompts; i++) { |
910 | passphrase = read_passphrase(prompt, 0); | |
911 | if (strcmp(passphrase, "") != 0) { | |
912 | private = key_load_private_type(KEY_UNSPEC, filename, | |
913 | passphrase, NULL); | |
914 | quit = 0; | |
915 | } else { | |
916 | debug2("no passphrase given, try next key"); | |
917 | quit = 1; | |
918 | } | |
919 | memset(passphrase, 0, strlen(passphrase)); | |
920 | xfree(passphrase); | |
921 | if (private != NULL || quit) | |
922 | break; | |
923 | debug2("bad passphrase given, try again..."); | |
924 | } | |
925 | } | |
926 | return private; | |
927 | } | |
928 | ||
929 | static int | |
1e608e42 | 930 | identity_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp, |
931 | u_char *data, u_int datalen) | |
3c0ef626 | 932 | { |
933 | Key *private; | |
934 | int idx, ret; | |
935 | ||
936 | idx = authctxt->last_key_hint; | |
937 | if (idx < 0) | |
938 | return -1; | |
939 | ||
940 | /* private key is stored in external hardware */ | |
1e608e42 | 941 | if (options.identity_keys[idx]->flags & KEY_FLAG_EXT) |
3c0ef626 | 942 | return key_sign(options.identity_keys[idx], sigp, lenp, data, datalen); |
943 | ||
944 | private = load_identity_file(options.identity_files[idx]); | |
945 | if (private == NULL) | |
946 | return -1; | |
947 | ret = key_sign(private, sigp, lenp, data, datalen); | |
948 | key_free(private); | |
949 | return ret; | |
950 | } | |
951 | ||
952 | static int | |
1e608e42 | 953 | agent_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp, |
954 | u_char *data, u_int datalen) | |
3c0ef626 | 955 | { |
956 | return ssh_agent_sign(authctxt->agent, key, sigp, lenp, data, datalen); | |
957 | } | |
958 | ||
959 | static int | |
1e608e42 | 960 | key_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp, |
961 | u_char *data, u_int datalen) | |
3c0ef626 | 962 | { |
963 | return key_sign(key, sigp, lenp, data, datalen); | |
964 | } | |
965 | ||
966 | static int | |
967 | userauth_pubkey_agent(Authctxt *authctxt) | |
968 | { | |
969 | static int called = 0; | |
970 | int ret = 0; | |
971 | char *comment; | |
972 | Key *k; | |
973 | ||
974 | if (called == 0) { | |
975 | if (ssh_get_num_identities(authctxt->agent, 2) == 0) | |
976 | debug2("userauth_pubkey_agent: no keys at all"); | |
977 | called = 1; | |
978 | } | |
979 | k = ssh_get_next_identity(authctxt->agent, &comment, 2); | |
980 | if (k == NULL) { | |
981 | debug2("userauth_pubkey_agent: no more keys"); | |
982 | } else { | |
983 | debug("userauth_pubkey_agent: testing agent key %s", comment); | |
984 | xfree(comment); | |
985 | ret = send_pubkey_test(authctxt, k, agent_sign_cb, -1); | |
986 | if (ret == 0) | |
987 | key_free(k); | |
988 | } | |
989 | if (ret == 0) | |
990 | debug2("userauth_pubkey_agent: no message sent"); | |
991 | return ret; | |
992 | } | |
993 | ||
994 | int | |
995 | userauth_pubkey(Authctxt *authctxt) | |
996 | { | |
997 | static int idx = 0; | |
998 | int sent = 0; | |
999 | Key *key; | |
1000 | char *filename; | |
1001 | ||
1002 | if (authctxt->agent != NULL) { | |
1003 | do { | |
1004 | sent = userauth_pubkey_agent(authctxt); | |
1e608e42 | 1005 | } while (!sent && authctxt->agent->howmany > 0); |
3c0ef626 | 1006 | } |
1007 | while (!sent && idx < options.num_identity_files) { | |
1008 | key = options.identity_keys[idx]; | |
1009 | filename = options.identity_files[idx]; | |
1010 | if (key == NULL) { | |
1011 | debug("try privkey: %s", filename); | |
1012 | key = load_identity_file(filename); | |
1013 | if (key != NULL) { | |
1014 | sent = sign_and_send_pubkey(authctxt, key, | |
1015 | key_sign_cb); | |
1016 | key_free(key); | |
1017 | } | |
1018 | } else if (key->type != KEY_RSA1) { | |
1019 | debug("try pubkey: %s", filename); | |
1020 | sent = send_pubkey_test(authctxt, key, | |
1021 | identity_sign_cb, idx); | |
1022 | } | |
1023 | idx++; | |
1024 | } | |
1025 | return sent; | |
1026 | } | |
1027 | ||
1028 | /* | |
1029 | * Send userauth request message specifying keyboard-interactive method. | |
1030 | */ | |
1031 | int | |
1032 | userauth_kbdint(Authctxt *authctxt) | |
1033 | { | |
1034 | static int attempt = 0; | |
1035 | ||
1036 | if (attempt++ >= options.number_of_password_prompts) | |
1037 | return 0; | |
1038 | /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */ | |
1039 | if (attempt > 1 && !authctxt->info_req_seen) { | |
1040 | debug3("userauth_kbdint: disable: no info_req_seen"); | |
1041 | dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL); | |
1042 | return 0; | |
1043 | } | |
1044 | ||
1045 | debug2("userauth_kbdint"); | |
1046 | packet_start(SSH2_MSG_USERAUTH_REQUEST); | |
1047 | packet_put_cstring(authctxt->server_user); | |
1048 | packet_put_cstring(authctxt->service); | |
1049 | packet_put_cstring(authctxt->method->name); | |
1050 | packet_put_cstring(""); /* lang */ | |
1051 | packet_put_cstring(options.kbd_interactive_devices ? | |
1052 | options.kbd_interactive_devices : ""); | |
1053 | packet_send(); | |
1054 | ||
1055 | dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req); | |
1056 | return 1; | |
1057 | } | |
1058 | ||
1059 | /* | |
1060 | * parse INFO_REQUEST, prompt user and send INFO_RESPONSE | |
1061 | */ | |
1062 | void | |
1e608e42 | 1063 | input_userauth_info_req(int type, u_int32_t seq, void *ctxt) |
3c0ef626 | 1064 | { |
1065 | Authctxt *authctxt = ctxt; | |
1066 | char *name, *inst, *lang, *prompt, *response; | |
1067 | u_int num_prompts, i; | |
1068 | int echo = 0; | |
1069 | ||
1070 | debug2("input_userauth_info_req"); | |
1071 | ||
1072 | if (authctxt == NULL) | |
1073 | fatal("input_userauth_info_req: no authentication context"); | |
1074 | ||
1075 | authctxt->info_req_seen = 1; | |
1076 | ||
1077 | name = packet_get_string(NULL); | |
1078 | inst = packet_get_string(NULL); | |
1079 | lang = packet_get_string(NULL); | |
1080 | if (strlen(name) > 0) | |
1081 | log("%s", name); | |
1082 | if (strlen(inst) > 0) | |
1083 | log("%s", inst); | |
1084 | xfree(name); | |
1085 | xfree(inst); | |
1086 | xfree(lang); | |
1087 | ||
1088 | num_prompts = packet_get_int(); | |
1089 | /* | |
1090 | * Begin to build info response packet based on prompts requested. | |
1091 | * We commit to providing the correct number of responses, so if | |
1092 | * further on we run into a problem that prevents this, we have to | |
1093 | * be sure and clean this up and send a correct error response. | |
1094 | */ | |
1095 | packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE); | |
1096 | packet_put_int(num_prompts); | |
1097 | ||
1098 | debug2("input_userauth_info_req: num_prompts %d", num_prompts); | |
1099 | for (i = 0; i < num_prompts; i++) { | |
1100 | prompt = packet_get_string(NULL); | |
1101 | echo = packet_get_char(); | |
1102 | ||
1103 | response = read_passphrase(prompt, echo ? RP_ECHO : 0); | |
1104 | ||
1105 | packet_put_cstring(response); | |
1106 | memset(response, 0, strlen(response)); | |
1107 | xfree(response); | |
1108 | xfree(prompt); | |
1109 | } | |
1e608e42 | 1110 | packet_check_eom(); /* done with parsing incoming message. */ |
3c0ef626 | 1111 | |
1112 | packet_add_padding(64); | |
1113 | packet_send(); | |
1114 | } | |
1115 | ||
44a053a3 | 1116 | static int |
1117 | ssh_keysign( | |
1118 | Key *key, | |
1119 | u_char **sigp, u_int *lenp, | |
1120 | u_char *data, u_int datalen) | |
1121 | { | |
1122 | Buffer b; | |
1123 | struct stat st; | |
1124 | pid_t pid; | |
1125 | int to[2], from[2], status, version = 2; | |
1126 | ||
1127 | debug("ssh_keysign called"); | |
1128 | ||
1129 | if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) { | |
1130 | error("ssh_keysign: no installed: %s", strerror(errno)); | |
1131 | return -1; | |
1132 | } | |
1133 | if (fflush(stdout) != 0) | |
1134 | error("ssh_keysign: fflush: %s", strerror(errno)); | |
1135 | if (pipe(to) < 0) { | |
1136 | error("ssh_keysign: pipe: %s", strerror(errno)); | |
1137 | return -1; | |
1138 | } | |
1139 | if (pipe(from) < 0) { | |
1140 | error("ssh_keysign: pipe: %s", strerror(errno)); | |
1141 | return -1; | |
1142 | } | |
1143 | if ((pid = fork()) < 0) { | |
1144 | error("ssh_keysign: fork: %s", strerror(errno)); | |
1145 | return -1; | |
1146 | } | |
1147 | if (pid == 0) { | |
1148 | seteuid(getuid()); | |
1149 | setuid(getuid()); | |
1150 | close(from[0]); | |
1151 | if (dup2(from[1], STDOUT_FILENO) < 0) | |
1152 | fatal("ssh_keysign: dup2: %s", strerror(errno)); | |
1153 | close(to[1]); | |
1154 | if (dup2(to[0], STDIN_FILENO) < 0) | |
1155 | fatal("ssh_keysign: dup2: %s", strerror(errno)); | |
1156 | close(from[1]); | |
1157 | close(to[0]); | |
1158 | execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0); | |
1159 | fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN, | |
1160 | strerror(errno)); | |
1161 | } | |
1162 | close(from[1]); | |
1163 | close(to[0]); | |
1164 | ||
1165 | buffer_init(&b); | |
1166 | buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */ | |
1167 | buffer_put_string(&b, data, datalen); | |
1168 | msg_send(to[1], version, &b); | |
1169 | ||
1170 | if (msg_recv(from[0], &b) < 0) { | |
1171 | error("ssh_keysign: no reply"); | |
1172 | buffer_clear(&b); | |
1173 | return -1; | |
1174 | } | |
1175 | close(from[0]); | |
1176 | close(to[1]); | |
1177 | ||
1178 | while (waitpid(pid, &status, 0) < 0) | |
1179 | if (errno != EINTR) | |
1180 | break; | |
1181 | ||
1182 | if (buffer_get_char(&b) != version) { | |
1183 | error("ssh_keysign: bad version"); | |
1184 | buffer_clear(&b); | |
1185 | return -1; | |
1186 | } | |
1187 | *sigp = buffer_get_string(&b, lenp); | |
1188 | buffer_clear(&b); | |
1189 | ||
1190 | return 0; | |
1191 | } | |
1192 | ||
3c0ef626 | 1193 | int |
1194 | userauth_hostbased(Authctxt *authctxt) | |
1195 | { | |
1196 | Key *private = NULL; | |
44a053a3 | 1197 | Sensitive *sensitive = authctxt->sensitive; |
3c0ef626 | 1198 | Buffer b; |
1199 | u_char *signature, *blob; | |
1200 | char *chost, *pkalg, *p; | |
1201 | const char *service; | |
1202 | u_int blen, slen; | |
1203 | int ok, i, len, found = 0; | |
1204 | ||
1205 | /* check for a useful key */ | |
44a053a3 | 1206 | for (i = 0; i < sensitive->nkeys; i++) { |
1207 | private = sensitive->keys[i]; | |
3c0ef626 | 1208 | if (private && private->type != KEY_RSA1) { |
1209 | found = 1; | |
1210 | /* we take and free the key */ | |
44a053a3 | 1211 | sensitive->keys[i] = NULL; |
3c0ef626 | 1212 | break; |
1213 | } | |
1214 | } | |
1215 | if (!found) { | |
1216 | debug("userauth_hostbased: no more client hostkeys"); | |
1217 | return 0; | |
1218 | } | |
1219 | if (key_to_blob(private, &blob, &blen) == 0) { | |
1220 | key_free(private); | |
1221 | return 0; | |
1222 | } | |
1223 | /* figure out a name for the client host */ | |
1224 | p = get_local_name(packet_get_connection_in()); | |
1225 | if (p == NULL) { | |
1226 | error("userauth_hostbased: cannot get local ipaddr/name"); | |
1227 | key_free(private); | |
1228 | return 0; | |
1229 | } | |
1230 | len = strlen(p) + 2; | |
1231 | chost = xmalloc(len); | |
1232 | strlcpy(chost, p, len); | |
1233 | strlcat(chost, ".", len); | |
1234 | debug2("userauth_hostbased: chost %s", chost); | |
1235 | ||
1236 | service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" : | |
1237 | authctxt->service; | |
1238 | pkalg = xstrdup(key_ssh_name(private)); | |
1239 | buffer_init(&b); | |
1240 | /* construct data */ | |
1241 | buffer_put_string(&b, session_id2, session_id2_len); | |
1242 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); | |
1243 | buffer_put_cstring(&b, authctxt->server_user); | |
1244 | buffer_put_cstring(&b, service); | |
1245 | buffer_put_cstring(&b, authctxt->method->name); | |
1246 | buffer_put_cstring(&b, pkalg); | |
1247 | buffer_put_string(&b, blob, blen); | |
1248 | buffer_put_cstring(&b, chost); | |
1249 | buffer_put_cstring(&b, authctxt->local_user); | |
1250 | #ifdef DEBUG_PK | |
1251 | buffer_dump(&b); | |
1252 | #endif | |
44a053a3 | 1253 | if (sensitive->external_keysign) |
1254 | ok = ssh_keysign(private, &signature, &slen, | |
1255 | buffer_ptr(&b), buffer_len(&b)); | |
1256 | else | |
1257 | ok = key_sign(private, &signature, &slen, | |
1258 | buffer_ptr(&b), buffer_len(&b)); | |
3c0ef626 | 1259 | key_free(private); |
1260 | buffer_free(&b); | |
1261 | if (ok != 0) { | |
1262 | error("key_sign failed"); | |
1263 | xfree(chost); | |
1264 | xfree(pkalg); | |
1265 | return 0; | |
1266 | } | |
1267 | packet_start(SSH2_MSG_USERAUTH_REQUEST); | |
1268 | packet_put_cstring(authctxt->server_user); | |
1269 | packet_put_cstring(authctxt->service); | |
1270 | packet_put_cstring(authctxt->method->name); | |
1271 | packet_put_cstring(pkalg); | |
1272 | packet_put_string(blob, blen); | |
1273 | packet_put_cstring(chost); | |
1274 | packet_put_cstring(authctxt->local_user); | |
1275 | packet_put_string(signature, slen); | |
1276 | memset(signature, 's', slen); | |
1277 | xfree(signature); | |
1278 | xfree(chost); | |
1279 | xfree(pkalg); | |
1280 | ||
1281 | packet_send(); | |
1282 | return 1; | |
1283 | } | |
1284 | ||
1285 | /* find auth method */ | |
1286 | ||
1287 | /* | |
1288 | * given auth method name, if configurable options permit this method fill | |
1289 | * in auth_ident field and return true, otherwise return false. | |
1290 | */ | |
1291 | static int | |
1292 | authmethod_is_enabled(Authmethod *method) | |
1293 | { | |
1294 | if (method == NULL) | |
1295 | return 0; | |
1296 | /* return false if options indicate this method is disabled */ | |
1297 | if (method->enabled == NULL || *method->enabled == 0) | |
1298 | return 0; | |
1299 | /* return false if batch mode is enabled but method needs interactive mode */ | |
1300 | if (method->batch_flag != NULL && *method->batch_flag != 0) | |
1301 | return 0; | |
1302 | return 1; | |
1303 | } | |
1304 | ||
1305 | static Authmethod * | |
1306 | authmethod_lookup(const char *name) | |
1307 | { | |
1308 | Authmethod *method = NULL; | |
1309 | if (name != NULL) | |
1310 | for (method = authmethods; method->name != NULL; method++) | |
1311 | if (strcmp(name, method->name) == 0) | |
1312 | return method; | |
1313 | debug2("Unrecognized authentication method name: %s", name ? name : "NULL"); | |
1314 | return NULL; | |
1315 | } | |
1316 | ||
1317 | /* XXX internal state */ | |
1318 | static Authmethod *current = NULL; | |
1319 | static char *supported = NULL; | |
1320 | static char *preferred = NULL; | |
1321 | /* | |
1322 | * Given the authentication method list sent by the server, return the | |
1323 | * next method we should try. If the server initially sends a nil list, | |
1324 | * use a built-in default list. | |
1325 | */ | |
1326 | static Authmethod * | |
1327 | authmethod_get(char *authlist) | |
1328 | { | |
1329 | ||
1330 | char *name = NULL; | |
1e608e42 | 1331 | u_int next; |
3c0ef626 | 1332 | |
1333 | /* Use a suitable default if we're passed a nil list. */ | |
1334 | if (authlist == NULL || strlen(authlist) == 0) | |
1335 | authlist = options.preferred_authentications; | |
1336 | ||
1337 | if (supported == NULL || strcmp(authlist, supported) != 0) { | |
1338 | debug3("start over, passed a different list %s", authlist); | |
1339 | if (supported != NULL) | |
1340 | xfree(supported); | |
1341 | supported = xstrdup(authlist); | |
1342 | preferred = options.preferred_authentications; | |
1343 | debug3("preferred %s", preferred); | |
1344 | current = NULL; | |
1345 | } else if (current != NULL && authmethod_is_enabled(current)) | |
1346 | return current; | |
1347 | ||
1348 | for (;;) { | |
1349 | if ((name = match_list(preferred, supported, &next)) == NULL) { | |
1350 | debug("no more auth methods to try"); | |
1351 | current = NULL; | |
1352 | return NULL; | |
1353 | } | |
1354 | preferred += next; | |
1355 | debug3("authmethod_lookup %s", name); | |
1356 | debug3("remaining preferred: %s", preferred); | |
1357 | if ((current = authmethod_lookup(name)) != NULL && | |
1358 | authmethod_is_enabled(current)) { | |
1359 | debug3("authmethod_is_enabled %s", name); | |
1360 | debug("next auth method to try is %s", name); | |
1361 | return current; | |
1362 | } | |
1363 | } | |
1364 | } | |
1365 | ||
3c0ef626 | 1366 | static char * |
1367 | authmethods_get(void) | |
1368 | { | |
1369 | Authmethod *method = NULL; | |
1e608e42 | 1370 | Buffer b; |
1371 | char *list; | |
3c0ef626 | 1372 | |
1e608e42 | 1373 | buffer_init(&b); |
3c0ef626 | 1374 | for (method = authmethods; method->name != NULL; method++) { |
1375 | if (authmethod_is_enabled(method)) { | |
1e608e42 | 1376 | if (buffer_len(&b) > 0) |
1377 | buffer_append(&b, ",", 1); | |
1378 | buffer_append(&b, method->name, strlen(method->name)); | |
3c0ef626 | 1379 | } |
1380 | } | |
1e608e42 | 1381 | buffer_append(&b, "\0", 1); |
1382 | list = xstrdup(buffer_ptr(&b)); | |
1383 | buffer_free(&b); | |
1384 | return list; | |
3c0ef626 | 1385 | } |