]>
Commit | Line | Data |
---|---|---|
a306f2dd | 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. | |
a306f2dd | 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" | |
8002af61 | 26 | RCSID("$OpenBSD: sshconnect2.c,v 1.68 2001/04/12 19:15:25 markus Exp $"); |
a306f2dd | 27 | |
28 | #include <openssl/bn.h> | |
a306f2dd | 29 | #include <openssl/md5.h> |
30 | #include <openssl/dh.h> | |
31 | #include <openssl/hmac.h> | |
32 | ||
33 | #include "ssh.h" | |
42f11eb2 | 34 | #include "ssh2.h" |
a306f2dd | 35 | #include "xmalloc.h" |
36 | #include "rsa.h" | |
37 | #include "buffer.h" | |
38 | #include "packet.h" | |
a306f2dd | 39 | #include "uidswap.h" |
40 | #include "compat.h" | |
a306f2dd | 41 | #include "bufaux.h" |
42f11eb2 | 42 | #include "cipher.h" |
a306f2dd | 43 | #include "kex.h" |
44 | #include "myproposal.h" | |
45 | #include "key.h" | |
a306f2dd | 46 | #include "sshconnect.h" |
47 | #include "authfile.h" | |
94ec8c6b | 48 | #include "cli.h" |
db1cd2f3 | 49 | #include "dh.h" |
2e73a022 | 50 | #include "authfd.h" |
42f11eb2 | 51 | #include "log.h" |
52 | #include "readconf.h" | |
53 | #include "readpass.h" | |
cab80f75 | 54 | #include "match.h" |
a5c9ffdb | 55 | #include "dispatch.h" |
8002af61 | 56 | #include "canohost.h" |
94ec8c6b | 57 | |
a306f2dd | 58 | /* import */ |
59 | extern char *client_version_string; | |
60 | extern char *server_version_string; | |
61 | extern Options options; | |
62 | ||
63 | /* | |
64 | * SSH2 key exchange | |
65 | */ | |
66 | ||
1e3b8b07 | 67 | u_char *session_id2 = NULL; |
a306f2dd | 68 | int session_id2_len = 0; |
69 | ||
a5c9ffdb | 70 | char *xxx_host; |
71 | struct sockaddr *xxx_hostaddr; | |
72 | ||
e092ce67 | 73 | Kex *xxx_kex = NULL; |
74 | ||
a5c9ffdb | 75 | int |
76 | check_host_key_callback(Key *hostkey) | |
77 | { | |
78 | check_host_key(xxx_host, xxx_hostaddr, hostkey, | |
79 | options.user_hostfile2, options.system_hostfile2); | |
80 | return 0; | |
81 | } | |
82 | ||
a306f2dd | 83 | void |
94ec8c6b | 84 | ssh_kex2(char *host, struct sockaddr *hostaddr) |
85 | { | |
94ec8c6b | 86 | Kex *kex; |
a5c9ffdb | 87 | |
88 | xxx_host = host; | |
89 | xxx_hostaddr = hostaddr; | |
94ec8c6b | 90 | |
c523303b | 91 | if (options.ciphers == (char *)-1) { |
92 | log("No valid ciphers for protocol version 2 given, using defaults."); | |
93 | options.ciphers = NULL; | |
94ec8c6b | 94 | } |
95 | if (options.ciphers != NULL) { | |
96 | myproposal[PROPOSAL_ENC_ALGS_CTOS] = | |
97 | myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers; | |
98 | } | |
1ad64a93 | 99 | myproposal[PROPOSAL_ENC_ALGS_CTOS] = |
100 | compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]); | |
101 | myproposal[PROPOSAL_ENC_ALGS_STOC] = | |
102 | compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]); | |
94ec8c6b | 103 | if (options.compression) { |
b2552997 | 104 | myproposal[PROPOSAL_COMP_ALGS_CTOS] = |
94ec8c6b | 105 | myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib"; |
106 | } else { | |
b2552997 | 107 | myproposal[PROPOSAL_COMP_ALGS_CTOS] = |
94ec8c6b | 108 | myproposal[PROPOSAL_COMP_ALGS_STOC] = "none"; |
109 | } | |
b2552997 | 110 | if (options.macs != NULL) { |
111 | myproposal[PROPOSAL_MAC_ALGS_CTOS] = | |
112 | myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs; | |
113 | } | |
94ec8c6b | 114 | |
7a37c112 | 115 | /* start key exchange */ |
d8ee838b | 116 | kex = kex_setup(myproposal); |
a5c9ffdb | 117 | kex->client_version_string=client_version_string; |
118 | kex->server_version_string=server_version_string; | |
119 | kex->check_host_key=&check_host_key_callback; | |
94ec8c6b | 120 | |
e092ce67 | 121 | xxx_kex = kex; |
122 | ||
c422989b | 123 | dispatch_run(DISPATCH_BLOCK, &kex->done, kex); |
94ec8c6b | 124 | |
d1ac6175 | 125 | session_id2 = kex->session_id; |
126 | session_id2_len = kex->session_id_len; | |
127 | ||
94ec8c6b | 128 | #ifdef DEBUG_KEXDH |
129 | /* send 1st encrypted/maced/compressed message */ | |
130 | packet_start(SSH2_MSG_IGNORE); | |
131 | packet_put_cstring("markus"); | |
132 | packet_send(); | |
133 | packet_write_wait(); | |
134 | #endif | |
a5c9ffdb | 135 | debug("done: ssh_kex2."); |
a306f2dd | 136 | } |
71276795 | 137 | |
a306f2dd | 138 | /* |
139 | * Authenticate user | |
140 | */ | |
188adeb2 | 141 | |
142 | typedef struct Authctxt Authctxt; | |
143 | typedef struct Authmethod Authmethod; | |
144 | ||
145 | typedef int sign_cb_fn( | |
146 | Authctxt *authctxt, Key *key, | |
1e3b8b07 | 147 | u_char **sigp, int *lenp, u_char *data, int datalen); |
188adeb2 | 148 | |
149 | struct Authctxt { | |
150 | const char *server_user; | |
8002af61 | 151 | const char *local_user; |
188adeb2 | 152 | const char *host; |
153 | const char *service; | |
188adeb2 | 154 | Authmethod *method; |
94ec8c6b | 155 | int success; |
fee56204 | 156 | char *authlist; |
8002af61 | 157 | /* pubkey */ |
fee56204 | 158 | Key *last_key; |
159 | sign_cb_fn *last_key_sign; | |
160 | int last_key_hint; | |
8002af61 | 161 | AuthenticationConnection *agent; |
162 | /* hostbased */ | |
163 | Key **keys; | |
164 | int nkeys; | |
188adeb2 | 165 | }; |
166 | struct Authmethod { | |
167 | char *name; /* string to compare against server's list */ | |
168 | int (*userauth)(Authctxt *authctxt); | |
169 | int *enabled; /* flag in option struct that enables method */ | |
170 | int *batch_flag; /* flag in option struct that disables method */ | |
171 | }; | |
172 | ||
173 | void input_userauth_success(int type, int plen, void *ctxt); | |
174 | void input_userauth_failure(int type, int plen, void *ctxt); | |
9616313f | 175 | void input_userauth_banner(int type, int plen, void *ctxt); |
188adeb2 | 176 | void input_userauth_error(int type, int plen, void *ctxt); |
94ec8c6b | 177 | void input_userauth_info_req(int type, int plen, void *ctxt); |
fee56204 | 178 | void input_userauth_pk_ok(int type, int plen, void *ctxt); |
94ec8c6b | 179 | |
180 | int userauth_none(Authctxt *authctxt); | |
188adeb2 | 181 | int userauth_pubkey(Authctxt *authctxt); |
182 | int userauth_passwd(Authctxt *authctxt); | |
94ec8c6b | 183 | int userauth_kbdint(Authctxt *authctxt); |
8002af61 | 184 | int userauth_hostbased(Authctxt *authctxt); |
188adeb2 | 185 | |
fee56204 | 186 | void userauth(Authctxt *authctxt, char *authlist); |
187 | ||
188 | int | |
189 | sign_and_send_pubkey(Authctxt *authctxt, Key *k, | |
190 | sign_cb_fn *sign_callback); | |
191 | void clear_auth_state(Authctxt *authctxt); | |
192 | ||
94ec8c6b | 193 | Authmethod *authmethod_get(char *authlist); |
194 | Authmethod *authmethod_lookup(const char *name); | |
cab80f75 | 195 | char *authmethods_get(void); |
188adeb2 | 196 | |
197 | Authmethod authmethods[] = { | |
198 | {"publickey", | |
199 | userauth_pubkey, | |
fa08c86b | 200 | &options.pubkey_authentication, |
188adeb2 | 201 | NULL}, |
202 | {"password", | |
203 | userauth_passwd, | |
204 | &options.password_authentication, | |
205 | &options.batch_mode}, | |
94ec8c6b | 206 | {"keyboard-interactive", |
207 | userauth_kbdint, | |
208 | &options.kbd_interactive_authentication, | |
209 | &options.batch_mode}, | |
8002af61 | 210 | {"hostbased", |
211 | userauth_hostbased, | |
212 | &options.hostbased_authentication, | |
213 | NULL}, | |
94ec8c6b | 214 | {"none", |
215 | userauth_none, | |
216 | NULL, | |
217 | NULL}, | |
188adeb2 | 218 | {NULL, NULL, NULL, NULL} |
219 | }; | |
220 | ||
221 | void | |
8002af61 | 222 | ssh_userauth2(const char *local_user, const char *server_user, char *host, |
223 | Key **keys, int nkeys) | |
188adeb2 | 224 | { |
225 | Authctxt authctxt; | |
226 | int type; | |
227 | int plen; | |
228 | ||
d464095c | 229 | if (options.challenge_reponse_authentication) |
230 | options.kbd_interactive_authentication = 1; | |
231 | ||
188adeb2 | 232 | debug("send SSH2_MSG_SERVICE_REQUEST"); |
233 | packet_start(SSH2_MSG_SERVICE_REQUEST); | |
234 | packet_put_cstring("ssh-userauth"); | |
235 | packet_send(); | |
236 | packet_write_wait(); | |
237 | type = packet_read(&plen); | |
238 | if (type != SSH2_MSG_SERVICE_ACCEPT) { | |
239 | fatal("denied SSH2_MSG_SERVICE_ACCEPT: %d", type); | |
240 | } | |
241 | if (packet_remaining() > 0) { | |
242 | char *reply = packet_get_string(&plen); | |
243 | debug("service_accept: %s", reply); | |
244 | xfree(reply); | |
188adeb2 | 245 | } else { |
246 | debug("buggy server: service_accept w/o service"); | |
247 | } | |
248 | packet_done(); | |
249 | debug("got SSH2_MSG_SERVICE_ACCEPT"); | |
250 | ||
cab80f75 | 251 | if (options.preferred_authentications == NULL) |
252 | options.preferred_authentications = authmethods_get(); | |
253 | ||
188adeb2 | 254 | /* setup authentication context */ |
255 | authctxt.agent = ssh_get_authentication_connection(); | |
256 | authctxt.server_user = server_user; | |
8002af61 | 257 | authctxt.local_user = local_user; |
188adeb2 | 258 | authctxt.host = host; |
259 | authctxt.service = "ssh-connection"; /* service name */ | |
260 | authctxt.success = 0; | |
94ec8c6b | 261 | authctxt.method = authmethod_lookup("none"); |
fee56204 | 262 | authctxt.authlist = NULL; |
8002af61 | 263 | authctxt.keys = keys; |
264 | authctxt.nkeys = nkeys; | |
94ec8c6b | 265 | if (authctxt.method == NULL) |
266 | fatal("ssh_userauth2: internal error: cannot send userauth none request"); | |
188adeb2 | 267 | |
268 | /* initial userauth request */ | |
94ec8c6b | 269 | userauth_none(&authctxt); |
188adeb2 | 270 | |
7a37c112 | 271 | dispatch_init(&input_userauth_error); |
188adeb2 | 272 | dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success); |
273 | dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure); | |
9616313f | 274 | dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner); |
188adeb2 | 275 | dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */ |
276 | ||
277 | if (authctxt.agent != NULL) | |
278 | ssh_close_authentication_connection(authctxt.agent); | |
279 | ||
8abcdba4 | 280 | debug("ssh-userauth2 successful: method %s", authctxt.method->name); |
188adeb2 | 281 | } |
282 | void | |
fee56204 | 283 | userauth(Authctxt *authctxt, char *authlist) |
284 | { | |
285 | if (authlist == NULL) { | |
286 | authlist = authctxt->authlist; | |
287 | } else { | |
288 | if (authctxt->authlist) | |
289 | xfree(authctxt->authlist); | |
290 | authctxt->authlist = authlist; | |
291 | } | |
292 | for (;;) { | |
293 | Authmethod *method = authmethod_get(authlist); | |
294 | if (method == NULL) | |
295 | fatal("Permission denied (%s).", authlist); | |
296 | authctxt->method = method; | |
297 | if (method->userauth(authctxt) != 0) { | |
298 | debug2("we sent a %s packet, wait for reply", method->name); | |
299 | break; | |
300 | } else { | |
301 | debug2("we did not send a packet, disable method"); | |
302 | method->enabled = NULL; | |
303 | } | |
304 | } | |
305 | } | |
306 | void | |
188adeb2 | 307 | input_userauth_error(int type, int plen, void *ctxt) |
308 | { | |
9616313f | 309 | fatal("input_userauth_error: bad message during authentication: " |
310 | "type %d", type); | |
311 | } | |
312 | void | |
313 | input_userauth_banner(int type, int plen, void *ctxt) | |
314 | { | |
315 | char *msg, *lang; | |
316 | debug3("input_userauth_banner"); | |
317 | msg = packet_get_string(NULL); | |
318 | lang = packet_get_string(NULL); | |
319 | fprintf(stderr, "%s", msg); | |
320 | xfree(msg); | |
321 | xfree(lang); | |
188adeb2 | 322 | } |
323 | void | |
324 | input_userauth_success(int type, int plen, void *ctxt) | |
325 | { | |
326 | Authctxt *authctxt = ctxt; | |
327 | if (authctxt == NULL) | |
328 | fatal("input_userauth_success: no authentication context"); | |
fee56204 | 329 | if (authctxt->authlist) |
330 | xfree(authctxt->authlist); | |
331 | clear_auth_state(authctxt); | |
188adeb2 | 332 | authctxt->success = 1; /* break out */ |
333 | } | |
334 | void | |
335 | input_userauth_failure(int type, int plen, void *ctxt) | |
336 | { | |
188adeb2 | 337 | Authctxt *authctxt = ctxt; |
338 | char *authlist = NULL; | |
339 | int partial; | |
188adeb2 | 340 | |
341 | if (authctxt == NULL) | |
342 | fatal("input_userauth_failure: no authentication context"); | |
343 | ||
94ec8c6b | 344 | authlist = packet_get_string(NULL); |
188adeb2 | 345 | partial = packet_get_char(); |
346 | packet_done(); | |
347 | ||
348 | if (partial != 0) | |
f72e01a5 | 349 | log("Authenticated with partial success."); |
188adeb2 | 350 | debug("authentications that can continue: %s", authlist); |
351 | ||
fee56204 | 352 | clear_auth_state(authctxt); |
353 | userauth(authctxt, authlist); | |
354 | } | |
355 | void | |
356 | input_userauth_pk_ok(int type, int plen, void *ctxt) | |
357 | { | |
358 | Authctxt *authctxt = ctxt; | |
359 | Key *key = NULL; | |
360 | Buffer b; | |
361 | int alen, blen, pktype, sent = 0; | |
22138a36 | 362 | char *pkalg, *pkblob, *fp; |
fee56204 | 363 | |
364 | if (authctxt == NULL) | |
365 | fatal("input_userauth_pk_ok: no authentication context"); | |
366 | if (datafellows & SSH_BUG_PKOK) { | |
367 | /* this is similar to SSH_BUG_PKAUTH */ | |
368 | debug2("input_userauth_pk_ok: SSH_BUG_PKOK"); | |
369 | pkblob = packet_get_string(&blen); | |
370 | buffer_init(&b); | |
371 | buffer_append(&b, pkblob, blen); | |
372 | pkalg = buffer_get_string(&b, &alen); | |
373 | buffer_free(&b); | |
374 | } else { | |
375 | pkalg = packet_get_string(&alen); | |
376 | pkblob = packet_get_string(&blen); | |
377 | } | |
378 | packet_done(); | |
379 | ||
380 | debug("input_userauth_pk_ok: pkalg %s blen %d lastkey %p hint %d", | |
381 | pkalg, blen, authctxt->last_key, authctxt->last_key_hint); | |
382 | ||
383 | do { | |
384 | if (authctxt->last_key == NULL || | |
385 | authctxt->last_key_sign == NULL) { | |
386 | debug("no last key or no sign cb"); | |
188adeb2 | 387 | break; |
188adeb2 | 388 | } |
fee56204 | 389 | if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) { |
390 | debug("unknown pkalg %s", pkalg); | |
391 | break; | |
392 | } | |
393 | if ((key = key_from_blob(pkblob, blen)) == NULL) { | |
394 | debug("no key from blob. pkalg %s", pkalg); | |
395 | break; | |
396 | } | |
22138a36 | 397 | fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX); |
398 | debug2("input_userauth_pk_ok: fp %s", fp); | |
399 | xfree(fp); | |
fee56204 | 400 | if (!key_equal(key, authctxt->last_key)) { |
401 | debug("key != last_key"); | |
402 | break; | |
403 | } | |
404 | sent = sign_and_send_pubkey(authctxt, key, | |
405 | authctxt->last_key_sign); | |
406 | } while(0); | |
407 | ||
408 | if (key != NULL) | |
409 | key_free(key); | |
410 | xfree(pkalg); | |
411 | xfree(pkblob); | |
412 | ||
413 | /* unregister */ | |
414 | clear_auth_state(authctxt); | |
415 | dispatch_set(SSH2_MSG_USERAUTH_PK_OK, NULL); | |
416 | ||
417 | /* try another method if we did not send a packet*/ | |
418 | if (sent == 0) | |
419 | userauth(authctxt, NULL); | |
420 | ||
188adeb2 | 421 | } |
422 | ||
94ec8c6b | 423 | int |
424 | userauth_none(Authctxt *authctxt) | |
425 | { | |
426 | /* initial userauth request */ | |
427 | packet_start(SSH2_MSG_USERAUTH_REQUEST); | |
428 | packet_put_cstring(authctxt->server_user); | |
429 | packet_put_cstring(authctxt->service); | |
430 | packet_put_cstring(authctxt->method->name); | |
431 | packet_send(); | |
94ec8c6b | 432 | return 1; |
433 | } | |
434 | ||
a306f2dd | 435 | int |
188adeb2 | 436 | userauth_passwd(Authctxt *authctxt) |
a306f2dd | 437 | { |
1d1ffb87 | 438 | static int attempt = 0; |
a306f2dd | 439 | char prompt[80]; |
440 | char *password; | |
441 | ||
fa649821 | 442 | if (attempt++ >= options.number_of_password_prompts) |
1d1ffb87 | 443 | return 0; |
444 | ||
fa649821 | 445 | if(attempt != 1) |
446 | error("Permission denied, please try again."); | |
447 | ||
f72e01a5 | 448 | snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ", |
188adeb2 | 449 | authctxt->server_user, authctxt->host); |
a306f2dd | 450 | password = read_passphrase(prompt, 0); |
451 | packet_start(SSH2_MSG_USERAUTH_REQUEST); | |
188adeb2 | 452 | packet_put_cstring(authctxt->server_user); |
453 | packet_put_cstring(authctxt->service); | |
94ec8c6b | 454 | packet_put_cstring(authctxt->method->name); |
a306f2dd | 455 | packet_put_char(0); |
a6215e53 | 456 | packet_put_cstring(password); |
a306f2dd | 457 | memset(password, 0, strlen(password)); |
458 | xfree(password); | |
a6215e53 | 459 | packet_inject_ignore(64); |
a306f2dd | 460 | packet_send(); |
a306f2dd | 461 | return 1; |
462 | } | |
463 | ||
fee56204 | 464 | void |
465 | clear_auth_state(Authctxt *authctxt) | |
466 | { | |
467 | /* XXX clear authentication state */ | |
468 | if (authctxt->last_key != NULL && authctxt->last_key_hint == -1) { | |
469 | debug3("clear_auth_state: key_free %p", authctxt->last_key); | |
470 | key_free(authctxt->last_key); | |
471 | } | |
472 | authctxt->last_key = NULL; | |
473 | authctxt->last_key_hint = -2; | |
474 | authctxt->last_key_sign = NULL; | |
475 | } | |
476 | ||
2e73a022 | 477 | int |
188adeb2 | 478 | sign_and_send_pubkey(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback) |
a306f2dd | 479 | { |
480 | Buffer b; | |
1e3b8b07 | 481 | u_char *blob, *signature; |
a306f2dd | 482 | int bloblen, slen; |
74fc9186 | 483 | int skip = 0; |
2e73a022 | 484 | int ret = -1; |
94ec8c6b | 485 | int have_sig = 1; |
a306f2dd | 486 | |
cbc5abf9 | 487 | debug3("sign_and_send_pubkey"); |
fee56204 | 488 | |
fa08c86b | 489 | if (key_to_blob(k, &blob, &bloblen) == 0) { |
490 | /* we cannot handle this key */ | |
cbc5abf9 | 491 | debug3("sign_and_send_pubkey: cannot handle key"); |
fa08c86b | 492 | return 0; |
493 | } | |
a306f2dd | 494 | /* data to be signed */ |
495 | buffer_init(&b); | |
33de75a3 | 496 | if (datafellows & SSH_OLD_SESSIONID) { |
74fc9186 | 497 | buffer_append(&b, session_id2, session_id2_len); |
2b87da3b | 498 | skip = session_id2_len; |
33de75a3 | 499 | } else { |
500 | buffer_put_string(&b, session_id2, session_id2_len); | |
501 | skip = buffer_len(&b); | |
74fc9186 | 502 | } |
a306f2dd | 503 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); |
188adeb2 | 504 | buffer_put_cstring(&b, authctxt->server_user); |
d0c832f3 | 505 | buffer_put_cstring(&b, |
cbc5abf9 | 506 | datafellows & SSH_BUG_PKSERVICE ? |
d0c832f3 | 507 | "ssh-userauth" : |
188adeb2 | 508 | authctxt->service); |
cbc5abf9 | 509 | if (datafellows & SSH_BUG_PKAUTH) { |
510 | buffer_put_char(&b, have_sig); | |
511 | } else { | |
512 | buffer_put_cstring(&b, authctxt->method->name); | |
513 | buffer_put_char(&b, have_sig); | |
2b87da3b | 514 | buffer_put_cstring(&b, key_ssh_name(k)); |
cbc5abf9 | 515 | } |
a306f2dd | 516 | buffer_put_string(&b, blob, bloblen); |
a306f2dd | 517 | |
518 | /* generate signature */ | |
fee56204 | 519 | ret = (*sign_callback)(authctxt, k, &signature, &slen, |
520 | buffer_ptr(&b), buffer_len(&b)); | |
2e73a022 | 521 | if (ret == -1) { |
522 | xfree(blob); | |
523 | buffer_free(&b); | |
524 | return 0; | |
525 | } | |
fa08c86b | 526 | #ifdef DEBUG_PK |
a306f2dd | 527 | buffer_dump(&b); |
528 | #endif | |
cbc5abf9 | 529 | if (datafellows & SSH_BUG_PKSERVICE) { |
d0c832f3 | 530 | buffer_clear(&b); |
531 | buffer_append(&b, session_id2, session_id2_len); | |
fee56204 | 532 | skip = session_id2_len; |
d0c832f3 | 533 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); |
188adeb2 | 534 | buffer_put_cstring(&b, authctxt->server_user); |
535 | buffer_put_cstring(&b, authctxt->service); | |
94ec8c6b | 536 | buffer_put_cstring(&b, authctxt->method->name); |
537 | buffer_put_char(&b, have_sig); | |
cbc5abf9 | 538 | if (!(datafellows & SSH_BUG_PKAUTH)) |
2b87da3b | 539 | buffer_put_cstring(&b, key_ssh_name(k)); |
d0c832f3 | 540 | buffer_put_string(&b, blob, bloblen); |
541 | } | |
542 | xfree(blob); | |
fee56204 | 543 | |
a306f2dd | 544 | /* append signature */ |
545 | buffer_put_string(&b, signature, slen); | |
546 | xfree(signature); | |
547 | ||
548 | /* skip session id and packet type */ | |
74fc9186 | 549 | if (buffer_len(&b) < skip + 1) |
188adeb2 | 550 | fatal("userauth_pubkey: internal error"); |
74fc9186 | 551 | buffer_consume(&b, skip + 1); |
a306f2dd | 552 | |
553 | /* put remaining data from buffer into packet */ | |
554 | packet_start(SSH2_MSG_USERAUTH_REQUEST); | |
555 | packet_put_raw(buffer_ptr(&b), buffer_len(&b)); | |
556 | buffer_free(&b); | |
a306f2dd | 557 | packet_send(); |
2e73a022 | 558 | |
559 | return 1; | |
4c8722d9 | 560 | } |
561 | ||
562 | int | |
fee56204 | 563 | send_pubkey_test(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback, |
564 | int hint) | |
4c8722d9 | 565 | { |
fee56204 | 566 | u_char *blob; |
567 | int bloblen, have_sig = 0; | |
4c8722d9 | 568 | |
fee56204 | 569 | debug3("send_pubkey_test"); |
570 | ||
571 | if (key_to_blob(k, &blob, &bloblen) == 0) { | |
572 | /* we cannot handle this key */ | |
573 | debug3("send_pubkey_test: cannot handle key"); | |
4c8722d9 | 574 | return 0; |
575 | } | |
fee56204 | 576 | /* register callback for USERAUTH_PK_OK message */ |
577 | authctxt->last_key_sign = sign_callback; | |
578 | authctxt->last_key_hint = hint; | |
579 | authctxt->last_key = k; | |
580 | dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok); | |
4c8722d9 | 581 | |
fee56204 | 582 | packet_start(SSH2_MSG_USERAUTH_REQUEST); |
583 | packet_put_cstring(authctxt->server_user); | |
584 | packet_put_cstring(authctxt->service); | |
585 | packet_put_cstring(authctxt->method->name); | |
586 | packet_put_char(have_sig); | |
587 | if (!(datafellows & SSH_BUG_PKAUTH)) | |
588 | packet_put_cstring(key_ssh_name(k)); | |
589 | packet_put_string(blob, bloblen); | |
590 | xfree(blob); | |
591 | packet_send(); | |
592 | return 1; | |
593 | } | |
594 | ||
595 | Key * | |
596 | load_identity_file(char *filename) | |
597 | { | |
598 | Key *private; | |
599 | char prompt[300], *passphrase; | |
aeaa3d9e | 600 | int quit, i; |
d156519a | 601 | struct stat st; |
fee56204 | 602 | |
d156519a | 603 | if (stat(filename, &st) < 0) { |
604 | debug3("no such identity: %s", filename); | |
605 | return NULL; | |
606 | } | |
aeaa3d9e | 607 | private = key_load_private_type(KEY_UNSPEC, filename, "", NULL); |
608 | if (private == NULL) { | |
609 | if (options.batch_mode) | |
fee56204 | 610 | return NULL; |
4c8722d9 | 611 | snprintf(prompt, sizeof prompt, |
fa08c86b | 612 | "Enter passphrase for key '%.100s': ", filename); |
188adeb2 | 613 | for (i = 0; i < options.number_of_password_prompts; i++) { |
614 | passphrase = read_passphrase(prompt, 0); | |
615 | if (strcmp(passphrase, "") != 0) { | |
aeaa3d9e | 616 | private = key_load_private_type(KEY_UNSPEC, filename, |
617 | passphrase, NULL); | |
fee56204 | 618 | quit = 0; |
188adeb2 | 619 | } else { |
620 | debug2("no passphrase given, try next key"); | |
fee56204 | 621 | quit = 1; |
188adeb2 | 622 | } |
623 | memset(passphrase, 0, strlen(passphrase)); | |
624 | xfree(passphrase); | |
aeaa3d9e | 625 | if (private != NULL || quit) |
188adeb2 | 626 | break; |
627 | debug2("bad passphrase given, try again..."); | |
628 | } | |
4c8722d9 | 629 | } |
fee56204 | 630 | return private; |
631 | } | |
632 | ||
633 | int | |
634 | identity_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp, | |
635 | u_char *data, int datalen) | |
636 | { | |
637 | Key *private; | |
638 | int idx, ret; | |
639 | ||
640 | idx = authctxt->last_key_hint; | |
641 | if (idx < 0) | |
642 | return -1; | |
643 | private = load_identity_file(options.identity_files[idx]); | |
644 | if (private == NULL) | |
645 | return -1; | |
646 | ret = key_sign(private, sigp, lenp, data, datalen); | |
647 | key_free(private); | |
2e73a022 | 648 | return ret; |
649 | } | |
650 | ||
1e3b8b07 | 651 | int agent_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp, |
652 | u_char *data, int datalen) | |
2e73a022 | 653 | { |
188adeb2 | 654 | return ssh_agent_sign(authctxt->agent, key, sigp, lenp, data, datalen); |
2e73a022 | 655 | } |
656 | ||
fee56204 | 657 | int key_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp, |
658 | u_char *data, int datalen) | |
659 | { | |
cd332296 | 660 | return key_sign(key, sigp, lenp, data, datalen); |
fee56204 | 661 | } |
662 | ||
2e73a022 | 663 | int |
188adeb2 | 664 | userauth_pubkey_agent(Authctxt *authctxt) |
2e73a022 | 665 | { |
666 | static int called = 0; | |
fa08c86b | 667 | int ret = 0; |
2e73a022 | 668 | char *comment; |
669 | Key *k; | |
2e73a022 | 670 | |
671 | if (called == 0) { | |
fa08c86b | 672 | if (ssh_get_num_identities(authctxt->agent, 2) == 0) |
673 | debug2("userauth_pubkey_agent: no keys at all"); | |
188adeb2 | 674 | called = 1; |
2e73a022 | 675 | } |
fa08c86b | 676 | k = ssh_get_next_identity(authctxt->agent, &comment, 2); |
188adeb2 | 677 | if (k == NULL) { |
fa08c86b | 678 | debug2("userauth_pubkey_agent: no more keys"); |
679 | } else { | |
fee56204 | 680 | debug("userauth_pubkey_agent: testing agent key %s", comment); |
fa08c86b | 681 | xfree(comment); |
fee56204 | 682 | ret = send_pubkey_test(authctxt, k, agent_sign_cb, -1); |
683 | if (ret == 0) | |
684 | key_free(k); | |
188adeb2 | 685 | } |
fa08c86b | 686 | if (ret == 0) |
687 | debug2("userauth_pubkey_agent: no message sent"); | |
2e73a022 | 688 | return ret; |
a306f2dd | 689 | } |
690 | ||
188adeb2 | 691 | int |
692 | userauth_pubkey(Authctxt *authctxt) | |
a306f2dd | 693 | { |
188adeb2 | 694 | static int idx = 0; |
695 | int sent = 0; | |
fee56204 | 696 | Key *key; |
697 | char *filename; | |
188adeb2 | 698 | |
fa08c86b | 699 | if (authctxt->agent != NULL) { |
700 | do { | |
701 | sent = userauth_pubkey_agent(authctxt); | |
702 | } while(!sent && authctxt->agent->howmany > 0); | |
703 | } | |
704 | while (!sent && idx < options.num_identity_files) { | |
fee56204 | 705 | key = options.identity_keys[idx]; |
706 | filename = options.identity_files[idx]; | |
707 | if (key == NULL) { | |
708 | debug("try privkey: %s", filename); | |
709 | key = load_identity_file(filename); | |
710 | if (key != NULL) { | |
711 | sent = sign_and_send_pubkey(authctxt, key, | |
712 | key_sign_cb); | |
713 | key_free(key); | |
714 | } | |
715 | } else if (key->type != KEY_RSA1) { | |
716 | debug("try pubkey: %s", filename); | |
717 | sent = send_pubkey_test(authctxt, key, | |
718 | identity_sign_cb, idx); | |
719 | } | |
fa08c86b | 720 | idx++; |
721 | } | |
188adeb2 | 722 | return sent; |
723 | } | |
a306f2dd | 724 | |
94ec8c6b | 725 | /* |
726 | * Send userauth request message specifying keyboard-interactive method. | |
727 | */ | |
728 | int | |
729 | userauth_kbdint(Authctxt *authctxt) | |
730 | { | |
731 | static int attempt = 0; | |
732 | ||
733 | if (attempt++ >= options.number_of_password_prompts) | |
734 | return 0; | |
735 | ||
736 | debug2("userauth_kbdint"); | |
737 | packet_start(SSH2_MSG_USERAUTH_REQUEST); | |
738 | packet_put_cstring(authctxt->server_user); | |
739 | packet_put_cstring(authctxt->service); | |
740 | packet_put_cstring(authctxt->method->name); | |
741 | packet_put_cstring(""); /* lang */ | |
742 | packet_put_cstring(options.kbd_interactive_devices ? | |
743 | options.kbd_interactive_devices : ""); | |
744 | packet_send(); | |
94ec8c6b | 745 | |
746 | dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req); | |
747 | return 1; | |
748 | } | |
749 | ||
750 | /* | |
f72e01a5 | 751 | * parse INFO_REQUEST, prompt user and send INFO_RESPONSE |
94ec8c6b | 752 | */ |
753 | void | |
754 | input_userauth_info_req(int type, int plen, void *ctxt) | |
755 | { | |
756 | Authctxt *authctxt = ctxt; | |
f72e01a5 | 757 | char *name, *inst, *lang, *prompt, *response; |
1e3b8b07 | 758 | u_int num_prompts, i; |
94ec8c6b | 759 | int echo = 0; |
760 | ||
761 | debug2("input_userauth_info_req"); | |
762 | ||
763 | if (authctxt == NULL) | |
764 | fatal("input_userauth_info_req: no authentication context"); | |
765 | ||
766 | name = packet_get_string(NULL); | |
767 | inst = packet_get_string(NULL); | |
768 | lang = packet_get_string(NULL); | |
94ec8c6b | 769 | if (strlen(name) > 0) |
770 | cli_mesg(name); | |
94ec8c6b | 771 | if (strlen(inst) > 0) |
772 | cli_mesg(inst); | |
f72e01a5 | 773 | xfree(name); |
94ec8c6b | 774 | xfree(inst); |
f72e01a5 | 775 | xfree(lang); |
94ec8c6b | 776 | |
777 | num_prompts = packet_get_int(); | |
778 | /* | |
779 | * Begin to build info response packet based on prompts requested. | |
780 | * We commit to providing the correct number of responses, so if | |
781 | * further on we run into a problem that prevents this, we have to | |
782 | * be sure and clean this up and send a correct error response. | |
783 | */ | |
784 | packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE); | |
785 | packet_put_int(num_prompts); | |
786 | ||
787 | for (i = 0; i < num_prompts; i++) { | |
788 | prompt = packet_get_string(NULL); | |
789 | echo = packet_get_char(); | |
790 | ||
791 | response = cli_prompt(prompt, echo); | |
792 | ||
a6215e53 | 793 | packet_put_cstring(response); |
94ec8c6b | 794 | memset(response, 0, strlen(response)); |
795 | xfree(response); | |
796 | xfree(prompt); | |
797 | } | |
798 | packet_done(); /* done with parsing incoming message. */ | |
799 | ||
a6215e53 | 800 | packet_inject_ignore(64); |
94ec8c6b | 801 | packet_send(); |
94ec8c6b | 802 | } |
a306f2dd | 803 | |
8002af61 | 804 | /* |
805 | * this will be move to an external program (ssh-keysign) ASAP. ssh-keysign | |
806 | * will be setuid-root and the sbit can be removed from /usr/bin/ssh. | |
807 | */ | |
808 | int | |
809 | userauth_hostbased(Authctxt *authctxt) | |
810 | { | |
811 | Key *private = NULL; | |
812 | Buffer b; | |
813 | u_char *signature, *blob; | |
814 | char *chost, *pkalg, *p; | |
815 | u_int blen, slen; | |
816 | int ok, i, found = 0; | |
817 | ||
818 | p = get_local_name(packet_get_connection_in()); | |
819 | if (p == NULL) { | |
820 | error("userauth_hostbased: cannot get local ipaddr/name"); | |
821 | return 0; | |
822 | } | |
823 | chost = xstrdup(p); | |
824 | debug2("userauth_hostbased: chost %s", chost); | |
825 | /* check for a useful key */ | |
826 | for (i = 0; i < authctxt->nkeys; i++) { | |
827 | private = authctxt->keys[i]; | |
828 | if (private && private->type != KEY_RSA1) { | |
829 | found = 1; | |
830 | /* we take and free the key */ | |
831 | authctxt->keys[i] = NULL; | |
832 | break; | |
833 | } | |
834 | } | |
835 | if (!found) { | |
836 | xfree(chost); | |
837 | return 0; | |
838 | } | |
839 | if (key_to_blob(private, &blob, &blen) == 0) { | |
840 | key_free(private); | |
841 | xfree(chost); | |
842 | return 0; | |
843 | } | |
844 | pkalg = xstrdup(key_ssh_name(private)); | |
845 | buffer_init(&b); | |
846 | if (datafellows & SSH_OLD_SESSIONID) { | |
847 | buffer_append(&b, session_id2, session_id2_len); | |
848 | } else { | |
849 | buffer_put_string(&b, session_id2, session_id2_len); | |
850 | } | |
851 | /* construct data */ | |
852 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); | |
853 | buffer_put_cstring(&b, authctxt->server_user); | |
854 | buffer_put_cstring(&b, | |
855 | datafellows & SSH_BUG_HBSERVICE ? | |
856 | "ssh-userauth" : | |
857 | authctxt->service); | |
858 | buffer_put_cstring(&b, authctxt->method->name); | |
859 | buffer_put_cstring(&b, pkalg); | |
860 | buffer_put_string(&b, blob, blen); | |
861 | buffer_put_cstring(&b, chost); | |
862 | buffer_put_cstring(&b, authctxt->local_user); | |
863 | #ifdef DEBUG_PK | |
864 | buffer_dump(&b); | |
865 | #endif | |
866 | debug2("xxx: chost %s", chost); | |
867 | ok = key_sign(private, &signature, &slen, buffer_ptr(&b), buffer_len(&b)); | |
868 | key_free(private); | |
869 | buffer_free(&b); | |
870 | if (ok != 0) { | |
871 | error("key_sign failed"); | |
872 | xfree(chost); | |
873 | xfree(pkalg); | |
874 | return 0; | |
875 | } | |
876 | packet_start(SSH2_MSG_USERAUTH_REQUEST); | |
877 | packet_put_cstring(authctxt->server_user); | |
878 | packet_put_cstring(authctxt->service); | |
879 | packet_put_cstring(authctxt->method->name); | |
880 | packet_put_cstring(pkalg); | |
881 | packet_put_string(blob, blen); | |
882 | packet_put_cstring(chost); | |
883 | packet_put_cstring(authctxt->local_user); | |
884 | packet_put_string(signature, slen); | |
885 | memset(signature, 's', slen); | |
886 | xfree(signature); | |
887 | xfree(chost); | |
888 | xfree(pkalg); | |
889 | ||
890 | packet_send(); | |
891 | return 1; | |
892 | } | |
893 | ||
188adeb2 | 894 | /* find auth method */ |
895 | ||
188adeb2 | 896 | /* |
897 | * given auth method name, if configurable options permit this method fill | |
898 | * in auth_ident field and return true, otherwise return false. | |
899 | */ | |
900 | int | |
901 | authmethod_is_enabled(Authmethod *method) | |
902 | { | |
903 | if (method == NULL) | |
904 | return 0; | |
905 | /* return false if options indicate this method is disabled */ | |
906 | if (method->enabled == NULL || *method->enabled == 0) | |
907 | return 0; | |
908 | /* return false if batch mode is enabled but method needs interactive mode */ | |
909 | if (method->batch_flag != NULL && *method->batch_flag != 0) | |
910 | return 0; | |
911 | return 1; | |
912 | } | |
a306f2dd | 913 | |
188adeb2 | 914 | Authmethod * |
915 | authmethod_lookup(const char *name) | |
916 | { | |
917 | Authmethod *method = NULL; | |
918 | if (name != NULL) | |
919 | for (method = authmethods; method->name != NULL; method++) | |
920 | if (strcmp(name, method->name) == 0) | |
921 | return method; | |
922 | debug2("Unrecognized authentication method name: %s", name ? name : "NULL"); | |
923 | return NULL; | |
924 | } | |
925 | ||
cab80f75 | 926 | /* XXX internal state */ |
927 | static Authmethod *current = NULL; | |
928 | static char *supported = NULL; | |
929 | static char *preferred = NULL; | |
188adeb2 | 930 | /* |
931 | * Given the authentication method list sent by the server, return the | |
932 | * next method we should try. If the server initially sends a nil list, | |
cd332296 | 933 | * use a built-in default list. |
2b87da3b | 934 | */ |
188adeb2 | 935 | Authmethod * |
936 | authmethod_get(char *authlist) | |
937 | { | |
cab80f75 | 938 | |
939 | char *name = NULL; | |
940 | int next; | |
2b87da3b | 941 | |
188adeb2 | 942 | /* Use a suitable default if we're passed a nil list. */ |
943 | if (authlist == NULL || strlen(authlist) == 0) | |
cab80f75 | 944 | authlist = options.preferred_authentications; |
945 | ||
946 | if (supported == NULL || strcmp(authlist, supported) != 0) { | |
947 | debug3("start over, passed a different list %s", authlist); | |
948 | if (supported != NULL) | |
949 | xfree(supported); | |
950 | supported = xstrdup(authlist); | |
951 | preferred = options.preferred_authentications; | |
952 | debug3("preferred %s", preferred); | |
953 | current = NULL; | |
954 | } else if (current != NULL && authmethod_is_enabled(current)) | |
955 | return current; | |
188adeb2 | 956 | |
cab80f75 | 957 | for (;;) { |
958 | if ((name = match_list(preferred, supported, &next)) == NULL) { | |
959 | debug("no more auth methods to try"); | |
960 | current = NULL; | |
961 | return NULL; | |
962 | } | |
963 | preferred += next; | |
94ec8c6b | 964 | debug3("authmethod_lookup %s", name); |
cab80f75 | 965 | debug3("remaining preferred: %s", preferred); |
966 | if ((current = authmethod_lookup(name)) != NULL && | |
967 | authmethod_is_enabled(current)) { | |
94ec8c6b | 968 | debug3("authmethod_is_enabled %s", name); |
cab80f75 | 969 | debug("next auth method to try is %s", name); |
970 | return current; | |
94ec8c6b | 971 | } |
a306f2dd | 972 | } |
cab80f75 | 973 | } |
a22aff1f | 974 | |
a22aff1f | 975 | |
cab80f75 | 976 | #define DELIM "," |
977 | char * | |
978 | authmethods_get(void) | |
979 | { | |
980 | Authmethod *method = NULL; | |
981 | char buf[1024]; | |
982 | ||
983 | buf[0] = '\0'; | |
984 | for (method = authmethods; method->name != NULL; method++) { | |
985 | if (authmethod_is_enabled(method)) { | |
986 | if (buf[0] != '\0') | |
987 | strlcat(buf, DELIM, sizeof buf); | |
988 | strlcat(buf, method->name, sizeof buf); | |
989 | } | |
990 | } | |
991 | return xstrdup(buf); | |
a306f2dd | 992 | } |