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