]>
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" | |
3e2f2431 | 26 | RCSID("$OpenBSD: sshconnect2.c,v 1.112 2003/03/05 22:33:43 markus Exp $"); |
a306f2dd | 27 | |
28 | #include "ssh.h" | |
42f11eb2 | 29 | #include "ssh2.h" |
a306f2dd | 30 | #include "xmalloc.h" |
a306f2dd | 31 | #include "buffer.h" |
32 | #include "packet.h" | |
a306f2dd | 33 | #include "compat.h" |
a306f2dd | 34 | #include "bufaux.h" |
42f11eb2 | 35 | #include "cipher.h" |
a306f2dd | 36 | #include "kex.h" |
37 | #include "myproposal.h" | |
a306f2dd | 38 | #include "sshconnect.h" |
39 | #include "authfile.h" | |
db1cd2f3 | 40 | #include "dh.h" |
2e73a022 | 41 | #include "authfd.h" |
42f11eb2 | 42 | #include "log.h" |
43 | #include "readconf.h" | |
44 | #include "readpass.h" | |
cab80f75 | 45 | #include "match.h" |
a5c9ffdb | 46 | #include "dispatch.h" |
8002af61 | 47 | #include "canohost.h" |
39c00dc2 | 48 | #include "msg.h" |
49 | #include "pathnames.h" | |
94ec8c6b | 50 | |
a306f2dd | 51 | /* import */ |
52 | extern char *client_version_string; | |
53 | extern char *server_version_string; | |
54 | extern Options options; | |
55 | ||
56 | /* | |
57 | * SSH2 key exchange | |
58 | */ | |
59 | ||
1e3b8b07 | 60 | u_char *session_id2 = NULL; |
a306f2dd | 61 | int session_id2_len = 0; |
62 | ||
a5c9ffdb | 63 | char *xxx_host; |
64 | struct sockaddr *xxx_hostaddr; | |
65 | ||
e092ce67 | 66 | Kex *xxx_kex = NULL; |
67 | ||
396c147e | 68 | static int |
f49bc4f7 | 69 | verify_host_key_callback(Key *hostkey) |
a5c9ffdb | 70 | { |
f49bc4f7 | 71 | if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1) |
01e9ef57 | 72 | fatal("Host key verification failed."); |
a5c9ffdb | 73 | return 0; |
74 | } | |
75 | ||
a306f2dd | 76 | void |
94ec8c6b | 77 | ssh_kex2(char *host, struct sockaddr *hostaddr) |
78 | { | |
94ec8c6b | 79 | Kex *kex; |
a5c9ffdb | 80 | |
81 | xxx_host = host; | |
82 | xxx_hostaddr = hostaddr; | |
94ec8c6b | 83 | |
c523303b | 84 | if (options.ciphers == (char *)-1) { |
85 | log("No valid ciphers for protocol version 2 given, using defaults."); | |
86 | options.ciphers = NULL; | |
94ec8c6b | 87 | } |
88 | if (options.ciphers != NULL) { | |
89 | myproposal[PROPOSAL_ENC_ALGS_CTOS] = | |
90 | myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers; | |
91 | } | |
1ad64a93 | 92 | myproposal[PROPOSAL_ENC_ALGS_CTOS] = |
93 | compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]); | |
94 | myproposal[PROPOSAL_ENC_ALGS_STOC] = | |
95 | compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]); | |
94ec8c6b | 96 | if (options.compression) { |
b2552997 | 97 | myproposal[PROPOSAL_COMP_ALGS_CTOS] = |
713f6cd9 | 98 | myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib,none"; |
94ec8c6b | 99 | } else { |
b2552997 | 100 | myproposal[PROPOSAL_COMP_ALGS_CTOS] = |
713f6cd9 | 101 | myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib"; |
94ec8c6b | 102 | } |
b2552997 | 103 | if (options.macs != NULL) { |
104 | myproposal[PROPOSAL_MAC_ALGS_CTOS] = | |
105 | myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs; | |
106 | } | |
e961a8f9 | 107 | if (options.hostkeyalgorithms != NULL) |
184eed6a | 108 | myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = |
e961a8f9 | 109 | options.hostkeyalgorithms; |
94ec8c6b | 110 | |
7a37c112 | 111 | /* start key exchange */ |
d8ee838b | 112 | kex = kex_setup(myproposal); |
98a58eda | 113 | kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client; |
114 | kex->kex[KEX_DH_GEX_SHA1] = kexgex_client; | |
a5c9ffdb | 115 | kex->client_version_string=client_version_string; |
116 | kex->server_version_string=server_version_string; | |
f49bc4f7 | 117 | kex->verify_host_key=&verify_host_key_callback; |
94ec8c6b | 118 | |
e092ce67 | 119 | xxx_kex = kex; |
120 | ||
c422989b | 121 | dispatch_run(DISPATCH_BLOCK, &kex->done, kex); |
94ec8c6b | 122 | |
d1ac6175 | 123 | session_id2 = kex->session_id; |
124 | session_id2_len = kex->session_id_len; | |
125 | ||
94ec8c6b | 126 | #ifdef DEBUG_KEXDH |
127 | /* send 1st encrypted/maced/compressed message */ | |
128 | packet_start(SSH2_MSG_IGNORE); | |
129 | packet_put_cstring("markus"); | |
130 | packet_send(); | |
131 | packet_write_wait(); | |
132 | #endif | |
a306f2dd | 133 | } |
71276795 | 134 | |
a306f2dd | 135 | /* |
136 | * Authenticate user | |
137 | */ | |
188adeb2 | 138 | |
139 | typedef struct Authctxt Authctxt; | |
140 | typedef struct Authmethod Authmethod; | |
141 | ||
142 | typedef int sign_cb_fn( | |
143 | Authctxt *authctxt, Key *key, | |
c66f9d0e | 144 | u_char **sigp, u_int *lenp, u_char *data, u_int datalen); |
188adeb2 | 145 | |
146 | struct Authctxt { | |
147 | const char *server_user; | |
8002af61 | 148 | const char *local_user; |
188adeb2 | 149 | const char *host; |
150 | const char *service; | |
188adeb2 | 151 | Authmethod *method; |
94ec8c6b | 152 | int success; |
fee56204 | 153 | char *authlist; |
8002af61 | 154 | /* pubkey */ |
fee56204 | 155 | Key *last_key; |
156 | sign_cb_fn *last_key_sign; | |
157 | int last_key_hint; | |
8002af61 | 158 | AuthenticationConnection *agent; |
159 | /* hostbased */ | |
39c00dc2 | 160 | Sensitive *sensitive; |
6b759005 | 161 | /* kbd-interactive */ |
162 | int info_req_seen; | |
188adeb2 | 163 | }; |
164 | struct Authmethod { | |
165 | char *name; /* string to compare against server's list */ | |
166 | int (*userauth)(Authctxt *authctxt); | |
167 | int *enabled; /* flag in option struct that enables method */ | |
168 | int *batch_flag; /* flag in option struct that disables method */ | |
169 | }; | |
170 | ||
7819b5c3 | 171 | void input_userauth_success(int, u_int32_t, void *); |
172 | void input_userauth_failure(int, u_int32_t, void *); | |
173 | void input_userauth_banner(int, u_int32_t, void *); | |
174 | void input_userauth_error(int, u_int32_t, void *); | |
175 | void input_userauth_info_req(int, u_int32_t, void *); | |
176 | void input_userauth_pk_ok(int, u_int32_t, void *); | |
67ec167d | 177 | void input_userauth_passwd_changereq(int, u_int32_t, void *); |
94ec8c6b | 178 | |
d5bb9418 | 179 | int userauth_none(Authctxt *); |
180 | int userauth_pubkey(Authctxt *); | |
181 | int userauth_passwd(Authctxt *); | |
182 | int userauth_kbdint(Authctxt *); | |
183 | int userauth_hostbased(Authctxt *); | |
188adeb2 | 184 | |
d5bb9418 | 185 | void userauth(Authctxt *, char *); |
fee56204 | 186 | |
396c147e | 187 | static int sign_and_send_pubkey(Authctxt *, Key *, sign_cb_fn *); |
188 | static void clear_auth_state(Authctxt *); | |
fee56204 | 189 | |
396c147e | 190 | static Authmethod *authmethod_get(char *authlist); |
191 | static Authmethod *authmethod_lookup(const char *name); | |
192 | static char *authmethods_get(void); | |
188adeb2 | 193 | |
194 | Authmethod authmethods[] = { | |
3398dda9 | 195 | {"hostbased", |
196 | userauth_hostbased, | |
197 | &options.hostbased_authentication, | |
198 | NULL}, | |
9f37c0af | 199 | {"publickey", |
200 | userauth_pubkey, | |
201 | &options.pubkey_authentication, | |
202 | NULL}, | |
94ec8c6b | 203 | {"keyboard-interactive", |
204 | userauth_kbdint, | |
205 | &options.kbd_interactive_authentication, | |
206 | &options.batch_mode}, | |
9f37c0af | 207 | {"password", |
208 | userauth_passwd, | |
209 | &options.password_authentication, | |
210 | &options.batch_mode}, | |
94ec8c6b | 211 | {"none", |
212 | userauth_none, | |
213 | NULL, | |
214 | NULL}, | |
188adeb2 | 215 | {NULL, NULL, NULL, NULL} |
216 | }; | |
217 | ||
218 | void | |
8002af61 | 219 | ssh_userauth2(const char *local_user, const char *server_user, char *host, |
39c00dc2 | 220 | Sensitive *sensitive) |
188adeb2 | 221 | { |
222 | Authctxt authctxt; | |
223 | int type; | |
188adeb2 | 224 | |
5ba55ada | 225 | if (options.challenge_response_authentication) |
d464095c | 226 | options.kbd_interactive_authentication = 1; |
227 | ||
188adeb2 | 228 | packet_start(SSH2_MSG_SERVICE_REQUEST); |
229 | packet_put_cstring("ssh-userauth"); | |
230 | packet_send(); | |
a77673cc | 231 | debug("SSH2_MSG_SERVICE_REQUEST sent"); |
188adeb2 | 232 | packet_write_wait(); |
54a5250f | 233 | type = packet_read(); |
a77673cc | 234 | if (type != SSH2_MSG_SERVICE_ACCEPT) |
235 | fatal("Server denied authentication request: %d", type); | |
188adeb2 | 236 | if (packet_remaining() > 0) { |
54a5250f | 237 | char *reply = packet_get_string(NULL); |
a77673cc | 238 | debug2("service_accept: %s", reply); |
188adeb2 | 239 | xfree(reply); |
188adeb2 | 240 | } else { |
6226a8f8 | 241 | debug2("buggy server: service_accept w/o service"); |
188adeb2 | 242 | } |
95500969 | 243 | packet_check_eom(); |
a77673cc | 244 | debug("SSH2_MSG_SERVICE_ACCEPT received"); |
188adeb2 | 245 | |
cab80f75 | 246 | if (options.preferred_authentications == NULL) |
247 | options.preferred_authentications = authmethods_get(); | |
248 | ||
188adeb2 | 249 | /* setup authentication context */ |
6b759005 | 250 | memset(&authctxt, 0, sizeof(authctxt)); |
188adeb2 | 251 | authctxt.agent = ssh_get_authentication_connection(); |
252 | authctxt.server_user = server_user; | |
8002af61 | 253 | authctxt.local_user = local_user; |
188adeb2 | 254 | authctxt.host = host; |
255 | authctxt.service = "ssh-connection"; /* service name */ | |
256 | authctxt.success = 0; | |
94ec8c6b | 257 | authctxt.method = authmethod_lookup("none"); |
fee56204 | 258 | authctxt.authlist = NULL; |
39c00dc2 | 259 | authctxt.sensitive = sensitive; |
6b759005 | 260 | authctxt.info_req_seen = 0; |
94ec8c6b | 261 | if (authctxt.method == NULL) |
262 | fatal("ssh_userauth2: internal error: cannot send userauth none request"); | |
188adeb2 | 263 | |
264 | /* initial userauth request */ | |
94ec8c6b | 265 | userauth_none(&authctxt); |
188adeb2 | 266 | |
7a37c112 | 267 | dispatch_init(&input_userauth_error); |
188adeb2 | 268 | dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success); |
269 | dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure); | |
9616313f | 270 | dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner); |
188adeb2 | 271 | dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */ |
272 | ||
273 | if (authctxt.agent != NULL) | |
274 | ssh_close_authentication_connection(authctxt.agent); | |
275 | ||
6226a8f8 | 276 | debug("Authentication succeeded (%s).", authctxt.method->name); |
188adeb2 | 277 | } |
278 | void | |
fee56204 | 279 | userauth(Authctxt *authctxt, char *authlist) |
280 | { | |
281 | if (authlist == NULL) { | |
282 | authlist = authctxt->authlist; | |
283 | } else { | |
284 | if (authctxt->authlist) | |
285 | xfree(authctxt->authlist); | |
286 | authctxt->authlist = authlist; | |
287 | } | |
288 | for (;;) { | |
289 | Authmethod *method = authmethod_get(authlist); | |
290 | if (method == NULL) | |
291 | fatal("Permission denied (%s).", authlist); | |
292 | authctxt->method = method; | |
293 | if (method->userauth(authctxt) != 0) { | |
294 | debug2("we sent a %s packet, wait for reply", method->name); | |
295 | break; | |
296 | } else { | |
297 | debug2("we did not send a packet, disable method"); | |
298 | method->enabled = NULL; | |
299 | } | |
300 | } | |
301 | } | |
d6133f43 | 302 | |
fee56204 | 303 | void |
7819b5c3 | 304 | input_userauth_error(int type, u_int32_t seq, void *ctxt) |
188adeb2 | 305 | { |
9616313f | 306 | fatal("input_userauth_error: bad message during authentication: " |
307 | "type %d", type); | |
308 | } | |
d6133f43 | 309 | |
9616313f | 310 | void |
7819b5c3 | 311 | input_userauth_banner(int type, u_int32_t seq, void *ctxt) |
9616313f | 312 | { |
313 | char *msg, *lang; | |
314 | debug3("input_userauth_banner"); | |
315 | msg = packet_get_string(NULL); | |
316 | lang = packet_get_string(NULL); | |
317 | fprintf(stderr, "%s", msg); | |
318 | xfree(msg); | |
319 | xfree(lang); | |
188adeb2 | 320 | } |
d6133f43 | 321 | |
188adeb2 | 322 | void |
7819b5c3 | 323 | input_userauth_success(int type, u_int32_t seq, void *ctxt) |
188adeb2 | 324 | { |
325 | Authctxt *authctxt = ctxt; | |
326 | if (authctxt == NULL) | |
327 | fatal("input_userauth_success: no authentication context"); | |
fee56204 | 328 | if (authctxt->authlist) |
329 | xfree(authctxt->authlist); | |
330 | clear_auth_state(authctxt); | |
188adeb2 | 331 | authctxt->success = 1; /* break out */ |
332 | } | |
d6133f43 | 333 | |
188adeb2 | 334 | void |
7819b5c3 | 335 | input_userauth_failure(int type, u_int32_t seq, void *ctxt) |
188adeb2 | 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(); |
95500969 | 346 | packet_check_eom(); |
188adeb2 | 347 | |
348 | if (partial != 0) | |
f72e01a5 | 349 | log("Authenticated with partial success."); |
6226a8f8 | 350 | debug("Authentications that can continue: %s", authlist); |
188adeb2 | 351 | |
fee56204 | 352 | clear_auth_state(authctxt); |
353 | userauth(authctxt, authlist); | |
354 | } | |
355 | void | |
7819b5c3 | 356 | input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt) |
fee56204 | 357 | { |
358 | Authctxt *authctxt = ctxt; | |
359 | Key *key = NULL; | |
360 | Buffer b; | |
c66f9d0e | 361 | int pktype, sent = 0; |
362 | u_int alen, blen; | |
363 | char *pkalg, *fp; | |
364 | u_char *pkblob; | |
fee56204 | 365 | |
366 | if (authctxt == NULL) | |
367 | fatal("input_userauth_pk_ok: no authentication context"); | |
368 | if (datafellows & SSH_BUG_PKOK) { | |
369 | /* this is similar to SSH_BUG_PKAUTH */ | |
370 | debug2("input_userauth_pk_ok: SSH_BUG_PKOK"); | |
371 | pkblob = packet_get_string(&blen); | |
372 | buffer_init(&b); | |
373 | buffer_append(&b, pkblob, blen); | |
374 | pkalg = buffer_get_string(&b, &alen); | |
375 | buffer_free(&b); | |
376 | } else { | |
377 | pkalg = packet_get_string(&alen); | |
378 | pkblob = packet_get_string(&blen); | |
379 | } | |
95500969 | 380 | packet_check_eom(); |
fee56204 | 381 | |
6226a8f8 | 382 | debug("Server accepts key: pkalg %s blen %u lastkey %p hint %d", |
fee56204 | 383 | pkalg, blen, authctxt->last_key, authctxt->last_key_hint); |
384 | ||
385 | do { | |
386 | if (authctxt->last_key == NULL || | |
387 | authctxt->last_key_sign == NULL) { | |
388 | debug("no last key or no sign cb"); | |
188adeb2 | 389 | break; |
188adeb2 | 390 | } |
0dbdc37c | 391 | if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) { |
fee56204 | 392 | debug("unknown pkalg %s", pkalg); |
393 | break; | |
394 | } | |
395 | if ((key = key_from_blob(pkblob, blen)) == NULL) { | |
396 | debug("no key from blob. pkalg %s", pkalg); | |
397 | break; | |
398 | } | |
762715ce | 399 | if (key->type != pktype) { |
0dbdc37c | 400 | error("input_userauth_pk_ok: type mismatch " |
401 | "for decoded key (received %d, expected %d)", | |
7203d6bb | 402 | key->type, pktype); |
0dbdc37c | 403 | break; |
404 | } | |
22138a36 | 405 | fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX); |
406 | debug2("input_userauth_pk_ok: fp %s", fp); | |
407 | xfree(fp); | |
fee56204 | 408 | if (!key_equal(key, authctxt->last_key)) { |
409 | debug("key != last_key"); | |
410 | break; | |
411 | } | |
412 | sent = sign_and_send_pubkey(authctxt, key, | |
413 | authctxt->last_key_sign); | |
6aacefa7 | 414 | } while (0); |
fee56204 | 415 | |
416 | if (key != NULL) | |
417 | key_free(key); | |
418 | xfree(pkalg); | |
419 | xfree(pkblob); | |
420 | ||
421 | /* unregister */ | |
422 | clear_auth_state(authctxt); | |
423 | dispatch_set(SSH2_MSG_USERAUTH_PK_OK, NULL); | |
424 | ||
343288b8 | 425 | /* try another method if we did not send a packet */ |
fee56204 | 426 | if (sent == 0) |
427 | userauth(authctxt, NULL); | |
428 | ||
188adeb2 | 429 | } |
430 | ||
94ec8c6b | 431 | int |
432 | userauth_none(Authctxt *authctxt) | |
433 | { | |
434 | /* initial userauth request */ | |
435 | packet_start(SSH2_MSG_USERAUTH_REQUEST); | |
436 | packet_put_cstring(authctxt->server_user); | |
437 | packet_put_cstring(authctxt->service); | |
438 | packet_put_cstring(authctxt->method->name); | |
439 | packet_send(); | |
94ec8c6b | 440 | return 1; |
441 | } | |
442 | ||
a306f2dd | 443 | int |
188adeb2 | 444 | userauth_passwd(Authctxt *authctxt) |
a306f2dd | 445 | { |
1d1ffb87 | 446 | static int attempt = 0; |
67ec167d | 447 | char prompt[150]; |
a306f2dd | 448 | char *password; |
449 | ||
fa649821 | 450 | if (attempt++ >= options.number_of_password_prompts) |
1d1ffb87 | 451 | return 0; |
452 | ||
6aacefa7 | 453 | if (attempt != 1) |
fa649821 | 454 | error("Permission denied, please try again."); |
455 | ||
f72e01a5 | 456 | snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ", |
188adeb2 | 457 | authctxt->server_user, authctxt->host); |
a306f2dd | 458 | password = read_passphrase(prompt, 0); |
459 | packet_start(SSH2_MSG_USERAUTH_REQUEST); | |
188adeb2 | 460 | packet_put_cstring(authctxt->server_user); |
461 | packet_put_cstring(authctxt->service); | |
94ec8c6b | 462 | packet_put_cstring(authctxt->method->name); |
a306f2dd | 463 | packet_put_char(0); |
a6215e53 | 464 | packet_put_cstring(password); |
a306f2dd | 465 | memset(password, 0, strlen(password)); |
466 | xfree(password); | |
b4b701be | 467 | packet_add_padding(64); |
a306f2dd | 468 | packet_send(); |
67ec167d | 469 | |
7203d6bb | 470 | dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, |
67ec167d | 471 | &input_userauth_passwd_changereq); |
472 | ||
a306f2dd | 473 | return 1; |
474 | } | |
67ec167d | 475 | /* |
476 | * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST | |
477 | */ | |
478 | void | |
ef077e37 | 479 | input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt) |
67ec167d | 480 | { |
481 | Authctxt *authctxt = ctxt; | |
482 | char *info, *lang, *password = NULL, *retype = NULL; | |
483 | char prompt[150]; | |
484 | ||
485 | debug2("input_userauth_passwd_changereq"); | |
486 | ||
487 | if (authctxt == NULL) | |
488 | fatal("input_userauth_passwd_changereq: " | |
489 | "no authentication context"); | |
490 | ||
491 | info = packet_get_string(NULL); | |
492 | lang = packet_get_string(NULL); | |
493 | if (strlen(info) > 0) | |
494 | log("%s", info); | |
495 | xfree(info); | |
496 | xfree(lang); | |
497 | packet_start(SSH2_MSG_USERAUTH_REQUEST); | |
498 | packet_put_cstring(authctxt->server_user); | |
499 | packet_put_cstring(authctxt->service); | |
500 | packet_put_cstring(authctxt->method->name); | |
501 | packet_put_char(1); /* additional info */ | |
7203d6bb | 502 | snprintf(prompt, sizeof(prompt), |
67ec167d | 503 | "Enter %.30s@%.128s's old password: ", |
504 | authctxt->server_user, authctxt->host); | |
505 | password = read_passphrase(prompt, 0); | |
506 | packet_put_cstring(password); | |
507 | memset(password, 0, strlen(password)); | |
508 | xfree(password); | |
509 | password = NULL; | |
510 | while (password == NULL) { | |
7203d6bb | 511 | snprintf(prompt, sizeof(prompt), |
67ec167d | 512 | "Enter %.30s@%.128s's new password: ", |
513 | authctxt->server_user, authctxt->host); | |
514 | password = read_passphrase(prompt, RP_ALLOW_EOF); | |
515 | if (password == NULL) { | |
516 | /* bail out */ | |
517 | return; | |
518 | } | |
7203d6bb | 519 | snprintf(prompt, sizeof(prompt), |
67ec167d | 520 | "Retype %.30s@%.128s's new password: ", |
521 | authctxt->server_user, authctxt->host); | |
522 | retype = read_passphrase(prompt, 0); | |
523 | if (strcmp(password, retype) != 0) { | |
524 | memset(password, 0, strlen(password)); | |
525 | xfree(password); | |
526 | log("Mismatch; try again, EOF to quit."); | |
527 | password = NULL; | |
528 | } | |
529 | memset(retype, 0, strlen(retype)); | |
530 | xfree(retype); | |
531 | } | |
532 | packet_put_cstring(password); | |
533 | memset(password, 0, strlen(password)); | |
534 | xfree(password); | |
535 | packet_add_padding(64); | |
536 | packet_send(); | |
7203d6bb | 537 | |
538 | dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, | |
67ec167d | 539 | &input_userauth_passwd_changereq); |
540 | } | |
a306f2dd | 541 | |
ffb215be | 542 | static void |
fee56204 | 543 | clear_auth_state(Authctxt *authctxt) |
544 | { | |
545 | /* XXX clear authentication state */ | |
67ec167d | 546 | dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, NULL); |
547 | ||
fee56204 | 548 | if (authctxt->last_key != NULL && authctxt->last_key_hint == -1) { |
549 | debug3("clear_auth_state: key_free %p", authctxt->last_key); | |
550 | key_free(authctxt->last_key); | |
551 | } | |
552 | authctxt->last_key = NULL; | |
553 | authctxt->last_key_hint = -2; | |
554 | authctxt->last_key_sign = NULL; | |
555 | } | |
556 | ||
396c147e | 557 | static int |
188adeb2 | 558 | sign_and_send_pubkey(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback) |
a306f2dd | 559 | { |
560 | Buffer b; | |
1e3b8b07 | 561 | u_char *blob, *signature; |
c66f9d0e | 562 | u_int bloblen, slen; |
74fc9186 | 563 | int skip = 0; |
2e73a022 | 564 | int ret = -1; |
94ec8c6b | 565 | int have_sig = 1; |
a306f2dd | 566 | |
cbc5abf9 | 567 | debug3("sign_and_send_pubkey"); |
fee56204 | 568 | |
fa08c86b | 569 | if (key_to_blob(k, &blob, &bloblen) == 0) { |
570 | /* we cannot handle this key */ | |
cbc5abf9 | 571 | debug3("sign_and_send_pubkey: cannot handle key"); |
fa08c86b | 572 | return 0; |
573 | } | |
a306f2dd | 574 | /* data to be signed */ |
575 | buffer_init(&b); | |
33de75a3 | 576 | if (datafellows & SSH_OLD_SESSIONID) { |
74fc9186 | 577 | buffer_append(&b, session_id2, session_id2_len); |
2b87da3b | 578 | skip = session_id2_len; |
33de75a3 | 579 | } else { |
580 | buffer_put_string(&b, session_id2, session_id2_len); | |
581 | skip = buffer_len(&b); | |
74fc9186 | 582 | } |
a306f2dd | 583 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); |
188adeb2 | 584 | buffer_put_cstring(&b, authctxt->server_user); |
d0c832f3 | 585 | buffer_put_cstring(&b, |
cbc5abf9 | 586 | datafellows & SSH_BUG_PKSERVICE ? |
d0c832f3 | 587 | "ssh-userauth" : |
188adeb2 | 588 | authctxt->service); |
cbc5abf9 | 589 | if (datafellows & SSH_BUG_PKAUTH) { |
590 | buffer_put_char(&b, have_sig); | |
591 | } else { | |
592 | buffer_put_cstring(&b, authctxt->method->name); | |
593 | buffer_put_char(&b, have_sig); | |
2b87da3b | 594 | buffer_put_cstring(&b, key_ssh_name(k)); |
cbc5abf9 | 595 | } |
a306f2dd | 596 | buffer_put_string(&b, blob, bloblen); |
a306f2dd | 597 | |
598 | /* generate signature */ | |
fee56204 | 599 | ret = (*sign_callback)(authctxt, k, &signature, &slen, |
600 | buffer_ptr(&b), buffer_len(&b)); | |
2e73a022 | 601 | if (ret == -1) { |
602 | xfree(blob); | |
603 | buffer_free(&b); | |
604 | return 0; | |
605 | } | |
fa08c86b | 606 | #ifdef DEBUG_PK |
a306f2dd | 607 | buffer_dump(&b); |
608 | #endif | |
cbc5abf9 | 609 | if (datafellows & SSH_BUG_PKSERVICE) { |
d0c832f3 | 610 | buffer_clear(&b); |
611 | buffer_append(&b, session_id2, session_id2_len); | |
fee56204 | 612 | skip = session_id2_len; |
d0c832f3 | 613 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); |
188adeb2 | 614 | buffer_put_cstring(&b, authctxt->server_user); |
615 | buffer_put_cstring(&b, authctxt->service); | |
94ec8c6b | 616 | buffer_put_cstring(&b, authctxt->method->name); |
617 | buffer_put_char(&b, have_sig); | |
cbc5abf9 | 618 | if (!(datafellows & SSH_BUG_PKAUTH)) |
2b87da3b | 619 | buffer_put_cstring(&b, key_ssh_name(k)); |
d0c832f3 | 620 | buffer_put_string(&b, blob, bloblen); |
621 | } | |
622 | xfree(blob); | |
fee56204 | 623 | |
a306f2dd | 624 | /* append signature */ |
625 | buffer_put_string(&b, signature, slen); | |
626 | xfree(signature); | |
627 | ||
628 | /* skip session id and packet type */ | |
74fc9186 | 629 | if (buffer_len(&b) < skip + 1) |
188adeb2 | 630 | fatal("userauth_pubkey: internal error"); |
74fc9186 | 631 | buffer_consume(&b, skip + 1); |
a306f2dd | 632 | |
633 | /* put remaining data from buffer into packet */ | |
634 | packet_start(SSH2_MSG_USERAUTH_REQUEST); | |
635 | packet_put_raw(buffer_ptr(&b), buffer_len(&b)); | |
636 | buffer_free(&b); | |
a306f2dd | 637 | packet_send(); |
2e73a022 | 638 | |
639 | return 1; | |
4c8722d9 | 640 | } |
641 | ||
396c147e | 642 | static int |
fee56204 | 643 | send_pubkey_test(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback, |
644 | int hint) | |
4c8722d9 | 645 | { |
fee56204 | 646 | u_char *blob; |
21b30f38 | 647 | u_int bloblen, have_sig = 0; |
4c8722d9 | 648 | |
fee56204 | 649 | debug3("send_pubkey_test"); |
650 | ||
651 | if (key_to_blob(k, &blob, &bloblen) == 0) { | |
652 | /* we cannot handle this key */ | |
653 | debug3("send_pubkey_test: cannot handle key"); | |
4c8722d9 | 654 | return 0; |
655 | } | |
fee56204 | 656 | /* register callback for USERAUTH_PK_OK message */ |
657 | authctxt->last_key_sign = sign_callback; | |
658 | authctxt->last_key_hint = hint; | |
659 | authctxt->last_key = k; | |
660 | dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok); | |
4c8722d9 | 661 | |
fee56204 | 662 | packet_start(SSH2_MSG_USERAUTH_REQUEST); |
663 | packet_put_cstring(authctxt->server_user); | |
664 | packet_put_cstring(authctxt->service); | |
665 | packet_put_cstring(authctxt->method->name); | |
666 | packet_put_char(have_sig); | |
667 | if (!(datafellows & SSH_BUG_PKAUTH)) | |
668 | packet_put_cstring(key_ssh_name(k)); | |
669 | packet_put_string(blob, bloblen); | |
670 | xfree(blob); | |
671 | packet_send(); | |
672 | return 1; | |
673 | } | |
674 | ||
396c147e | 675 | static Key * |
fee56204 | 676 | load_identity_file(char *filename) |
677 | { | |
678 | Key *private; | |
679 | char prompt[300], *passphrase; | |
aeaa3d9e | 680 | int quit, i; |
d156519a | 681 | struct stat st; |
fee56204 | 682 | |
d156519a | 683 | if (stat(filename, &st) < 0) { |
684 | debug3("no such identity: %s", filename); | |
685 | return NULL; | |
686 | } | |
aeaa3d9e | 687 | private = key_load_private_type(KEY_UNSPEC, filename, "", NULL); |
688 | if (private == NULL) { | |
689 | if (options.batch_mode) | |
fee56204 | 690 | return NULL; |
4c8722d9 | 691 | snprintf(prompt, sizeof prompt, |
184eed6a | 692 | "Enter passphrase for key '%.100s': ", filename); |
188adeb2 | 693 | for (i = 0; i < options.number_of_password_prompts; i++) { |
694 | passphrase = read_passphrase(prompt, 0); | |
695 | if (strcmp(passphrase, "") != 0) { | |
aeaa3d9e | 696 | private = key_load_private_type(KEY_UNSPEC, filename, |
697 | passphrase, NULL); | |
fee56204 | 698 | quit = 0; |
188adeb2 | 699 | } else { |
700 | debug2("no passphrase given, try next key"); | |
fee56204 | 701 | quit = 1; |
188adeb2 | 702 | } |
703 | memset(passphrase, 0, strlen(passphrase)); | |
704 | xfree(passphrase); | |
aeaa3d9e | 705 | if (private != NULL || quit) |
188adeb2 | 706 | break; |
707 | debug2("bad passphrase given, try again..."); | |
708 | } | |
4c8722d9 | 709 | } |
fee56204 | 710 | return private; |
711 | } | |
712 | ||
396c147e | 713 | static int |
c66f9d0e | 714 | identity_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp, |
715 | u_char *data, u_int datalen) | |
fee56204 | 716 | { |
717 | Key *private; | |
718 | int idx, ret; | |
719 | ||
720 | idx = authctxt->last_key_hint; | |
721 | if (idx < 0) | |
722 | return -1; | |
383546c4 | 723 | |
724 | /* private key is stored in external hardware */ | |
184eed6a | 725 | if (options.identity_keys[idx]->flags & KEY_FLAG_EXT) |
383546c4 | 726 | return key_sign(options.identity_keys[idx], sigp, lenp, data, datalen); |
727 | ||
fee56204 | 728 | private = load_identity_file(options.identity_files[idx]); |
729 | if (private == NULL) | |
730 | return -1; | |
731 | ret = key_sign(private, sigp, lenp, data, datalen); | |
732 | key_free(private); | |
2e73a022 | 733 | return ret; |
734 | } | |
735 | ||
396c147e | 736 | static int |
c66f9d0e | 737 | agent_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp, |
738 | u_char *data, u_int datalen) | |
2e73a022 | 739 | { |
188adeb2 | 740 | return ssh_agent_sign(authctxt->agent, key, sigp, lenp, data, datalen); |
2e73a022 | 741 | } |
742 | ||
396c147e | 743 | static int |
c66f9d0e | 744 | key_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp, |
745 | u_char *data, u_int datalen) | |
fee56204 | 746 | { |
cd332296 | 747 | return key_sign(key, sigp, lenp, data, datalen); |
fee56204 | 748 | } |
749 | ||
396c147e | 750 | static int |
188adeb2 | 751 | userauth_pubkey_agent(Authctxt *authctxt) |
2e73a022 | 752 | { |
753 | static int called = 0; | |
fa08c86b | 754 | int ret = 0; |
2e73a022 | 755 | char *comment; |
756 | Key *k; | |
2e73a022 | 757 | |
758 | if (called == 0) { | |
fa08c86b | 759 | if (ssh_get_num_identities(authctxt->agent, 2) == 0) |
760 | debug2("userauth_pubkey_agent: no keys at all"); | |
188adeb2 | 761 | called = 1; |
2e73a022 | 762 | } |
fa08c86b | 763 | k = ssh_get_next_identity(authctxt->agent, &comment, 2); |
188adeb2 | 764 | if (k == NULL) { |
fa08c86b | 765 | debug2("userauth_pubkey_agent: no more keys"); |
766 | } else { | |
6226a8f8 | 767 | debug("Offering agent key: %s", comment); |
fa08c86b | 768 | xfree(comment); |
fee56204 | 769 | ret = send_pubkey_test(authctxt, k, agent_sign_cb, -1); |
770 | if (ret == 0) | |
771 | key_free(k); | |
188adeb2 | 772 | } |
fa08c86b | 773 | if (ret == 0) |
774 | debug2("userauth_pubkey_agent: no message sent"); | |
2e73a022 | 775 | return ret; |
a306f2dd | 776 | } |
777 | ||
188adeb2 | 778 | int |
779 | userauth_pubkey(Authctxt *authctxt) | |
a306f2dd | 780 | { |
188adeb2 | 781 | static int idx = 0; |
782 | int sent = 0; | |
fee56204 | 783 | Key *key; |
784 | char *filename; | |
188adeb2 | 785 | |
fa08c86b | 786 | if (authctxt->agent != NULL) { |
787 | do { | |
788 | sent = userauth_pubkey_agent(authctxt); | |
6aacefa7 | 789 | } while (!sent && authctxt->agent->howmany > 0); |
fa08c86b | 790 | } |
791 | while (!sent && idx < options.num_identity_files) { | |
fee56204 | 792 | key = options.identity_keys[idx]; |
793 | filename = options.identity_files[idx]; | |
794 | if (key == NULL) { | |
6226a8f8 | 795 | debug("Trying private key: %s", filename); |
fee56204 | 796 | key = load_identity_file(filename); |
797 | if (key != NULL) { | |
798 | sent = sign_and_send_pubkey(authctxt, key, | |
799 | key_sign_cb); | |
800 | key_free(key); | |
801 | } | |
802 | } else if (key->type != KEY_RSA1) { | |
6226a8f8 | 803 | debug("Offering public key: %s", filename); |
fee56204 | 804 | sent = send_pubkey_test(authctxt, key, |
805 | identity_sign_cb, idx); | |
806 | } | |
fa08c86b | 807 | idx++; |
808 | } | |
188adeb2 | 809 | return sent; |
810 | } | |
a306f2dd | 811 | |
94ec8c6b | 812 | /* |
813 | * Send userauth request message specifying keyboard-interactive method. | |
814 | */ | |
815 | int | |
816 | userauth_kbdint(Authctxt *authctxt) | |
817 | { | |
818 | static int attempt = 0; | |
819 | ||
820 | if (attempt++ >= options.number_of_password_prompts) | |
821 | return 0; | |
6b759005 | 822 | /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */ |
823 | if (attempt > 1 && !authctxt->info_req_seen) { | |
824 | debug3("userauth_kbdint: disable: no info_req_seen"); | |
825 | dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL); | |
826 | return 0; | |
827 | } | |
94ec8c6b | 828 | |
829 | debug2("userauth_kbdint"); | |
830 | packet_start(SSH2_MSG_USERAUTH_REQUEST); | |
831 | packet_put_cstring(authctxt->server_user); | |
832 | packet_put_cstring(authctxt->service); | |
833 | packet_put_cstring(authctxt->method->name); | |
834 | packet_put_cstring(""); /* lang */ | |
835 | packet_put_cstring(options.kbd_interactive_devices ? | |
836 | options.kbd_interactive_devices : ""); | |
837 | packet_send(); | |
94ec8c6b | 838 | |
839 | dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req); | |
840 | return 1; | |
841 | } | |
842 | ||
843 | /* | |
f72e01a5 | 844 | * parse INFO_REQUEST, prompt user and send INFO_RESPONSE |
94ec8c6b | 845 | */ |
846 | void | |
7819b5c3 | 847 | input_userauth_info_req(int type, u_int32_t seq, void *ctxt) |
94ec8c6b | 848 | { |
849 | Authctxt *authctxt = ctxt; | |
f72e01a5 | 850 | char *name, *inst, *lang, *prompt, *response; |
1e3b8b07 | 851 | u_int num_prompts, i; |
94ec8c6b | 852 | int echo = 0; |
853 | ||
854 | debug2("input_userauth_info_req"); | |
855 | ||
856 | if (authctxt == NULL) | |
857 | fatal("input_userauth_info_req: no authentication context"); | |
858 | ||
6b759005 | 859 | authctxt->info_req_seen = 1; |
860 | ||
94ec8c6b | 861 | name = packet_get_string(NULL); |
862 | inst = packet_get_string(NULL); | |
863 | lang = packet_get_string(NULL); | |
94ec8c6b | 864 | if (strlen(name) > 0) |
80097c54 | 865 | log("%s", name); |
94ec8c6b | 866 | if (strlen(inst) > 0) |
80097c54 | 867 | log("%s", inst); |
f72e01a5 | 868 | xfree(name); |
94ec8c6b | 869 | xfree(inst); |
f72e01a5 | 870 | xfree(lang); |
94ec8c6b | 871 | |
872 | num_prompts = packet_get_int(); | |
873 | /* | |
874 | * Begin to build info response packet based on prompts requested. | |
875 | * We commit to providing the correct number of responses, so if | |
876 | * further on we run into a problem that prevents this, we have to | |
877 | * be sure and clean this up and send a correct error response. | |
878 | */ | |
879 | packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE); | |
880 | packet_put_int(num_prompts); | |
881 | ||
5ba55ada | 882 | debug2("input_userauth_info_req: num_prompts %d", num_prompts); |
94ec8c6b | 883 | for (i = 0; i < num_prompts; i++) { |
884 | prompt = packet_get_string(NULL); | |
885 | echo = packet_get_char(); | |
886 | ||
1843a425 | 887 | response = read_passphrase(prompt, echo ? RP_ECHO : 0); |
94ec8c6b | 888 | |
a6215e53 | 889 | packet_put_cstring(response); |
94ec8c6b | 890 | memset(response, 0, strlen(response)); |
891 | xfree(response); | |
892 | xfree(prompt); | |
893 | } | |
95500969 | 894 | packet_check_eom(); /* done with parsing incoming message. */ |
94ec8c6b | 895 | |
b4b701be | 896 | packet_add_padding(64); |
94ec8c6b | 897 | packet_send(); |
94ec8c6b | 898 | } |
a306f2dd | 899 | |
39c00dc2 | 900 | static int |
d6133f43 | 901 | ssh_keysign(Key *key, u_char **sigp, u_int *lenp, |
39c00dc2 | 902 | u_char *data, u_int datalen) |
903 | { | |
904 | Buffer b; | |
7091a26b | 905 | struct stat st; |
39c00dc2 | 906 | pid_t pid; |
d4826734 | 907 | int to[2], from[2], status, version = 2; |
39c00dc2 | 908 | |
6226a8f8 | 909 | debug2("ssh_keysign called"); |
39c00dc2 | 910 | |
7091a26b | 911 | if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) { |
912 | error("ssh_keysign: no installed: %s", strerror(errno)); | |
913 | return -1; | |
914 | } | |
39c00dc2 | 915 | if (fflush(stdout) != 0) |
916 | error("ssh_keysign: fflush: %s", strerror(errno)); | |
917 | if (pipe(to) < 0) { | |
918 | error("ssh_keysign: pipe: %s", strerror(errno)); | |
919 | return -1; | |
920 | } | |
921 | if (pipe(from) < 0) { | |
922 | error("ssh_keysign: pipe: %s", strerror(errno)); | |
923 | return -1; | |
924 | } | |
925 | if ((pid = fork()) < 0) { | |
926 | error("ssh_keysign: fork: %s", strerror(errno)); | |
927 | return -1; | |
928 | } | |
929 | if (pid == 0) { | |
930 | seteuid(getuid()); | |
931 | setuid(getuid()); | |
932 | close(from[0]); | |
933 | if (dup2(from[1], STDOUT_FILENO) < 0) | |
934 | fatal("ssh_keysign: dup2: %s", strerror(errno)); | |
935 | close(to[1]); | |
936 | if (dup2(to[0], STDIN_FILENO) < 0) | |
937 | fatal("ssh_keysign: dup2: %s", strerror(errno)); | |
d4826734 | 938 | close(from[1]); |
939 | close(to[0]); | |
a3f69458 | 940 | execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0); |
39c00dc2 | 941 | fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN, |
942 | strerror(errno)); | |
943 | } | |
944 | close(from[1]); | |
945 | close(to[0]); | |
946 | ||
947 | buffer_init(&b); | |
d4826734 | 948 | buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */ |
39c00dc2 | 949 | buffer_put_string(&b, data, datalen); |
e987fdbe | 950 | ssh_msg_send(to[1], version, &b); |
39c00dc2 | 951 | |
e987fdbe | 952 | if (ssh_msg_recv(from[0], &b) < 0) { |
7091a26b | 953 | error("ssh_keysign: no reply"); |
39c00dc2 | 954 | buffer_clear(&b); |
955 | return -1; | |
956 | } | |
39c00dc2 | 957 | close(from[0]); |
958 | close(to[1]); | |
959 | ||
d4826734 | 960 | while (waitpid(pid, &status, 0) < 0) |
961 | if (errno != EINTR) | |
962 | break; | |
39c00dc2 | 963 | |
7091a26b | 964 | if (buffer_get_char(&b) != version) { |
965 | error("ssh_keysign: bad version"); | |
966 | buffer_clear(&b); | |
967 | return -1; | |
968 | } | |
969 | *sigp = buffer_get_string(&b, lenp); | |
970 | buffer_clear(&b); | |
971 | ||
39c00dc2 | 972 | return 0; |
973 | } | |
974 | ||
8002af61 | 975 | int |
976 | userauth_hostbased(Authctxt *authctxt) | |
977 | { | |
978 | Key *private = NULL; | |
39c00dc2 | 979 | Sensitive *sensitive = authctxt->sensitive; |
8002af61 | 980 | Buffer b; |
981 | u_char *signature, *blob; | |
982 | char *chost, *pkalg, *p; | |
8dddf799 | 983 | const char *service; |
8002af61 | 984 | u_int blen, slen; |
f98c3421 | 985 | int ok, i, len, found = 0; |
8002af61 | 986 | |
8002af61 | 987 | /* check for a useful key */ |
39c00dc2 | 988 | for (i = 0; i < sensitive->nkeys; i++) { |
989 | private = sensitive->keys[i]; | |
8002af61 | 990 | if (private && private->type != KEY_RSA1) { |
991 | found = 1; | |
992 | /* we take and free the key */ | |
39c00dc2 | 993 | sensitive->keys[i] = NULL; |
8002af61 | 994 | break; |
995 | } | |
996 | } | |
997 | if (!found) { | |
6226a8f8 | 998 | debug("No more client hostkeys for hostbased authentication."); |
8002af61 | 999 | return 0; |
1000 | } | |
1001 | if (key_to_blob(private, &blob, &blen) == 0) { | |
1002 | key_free(private); | |
8002af61 | 1003 | return 0; |
1004 | } | |
e8d59b4d | 1005 | /* figure out a name for the client host */ |
1006 | p = get_local_name(packet_get_connection_in()); | |
1007 | if (p == NULL) { | |
1008 | error("userauth_hostbased: cannot get local ipaddr/name"); | |
1009 | key_free(private); | |
1010 | return 0; | |
1011 | } | |
1012 | len = strlen(p) + 2; | |
1013 | chost = xmalloc(len); | |
1014 | strlcpy(chost, p, len); | |
1015 | strlcat(chost, ".", len); | |
1016 | debug2("userauth_hostbased: chost %s", chost); | |
3e2f2431 | 1017 | xfree(p); |
e8d59b4d | 1018 | |
8dddf799 | 1019 | service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" : |
1020 | authctxt->service; | |
8002af61 | 1021 | pkalg = xstrdup(key_ssh_name(private)); |
1022 | buffer_init(&b); | |
8002af61 | 1023 | /* construct data */ |
8dddf799 | 1024 | buffer_put_string(&b, session_id2, session_id2_len); |
8002af61 | 1025 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); |
1026 | buffer_put_cstring(&b, authctxt->server_user); | |
8dddf799 | 1027 | buffer_put_cstring(&b, service); |
8002af61 | 1028 | buffer_put_cstring(&b, authctxt->method->name); |
1029 | buffer_put_cstring(&b, pkalg); | |
1030 | buffer_put_string(&b, blob, blen); | |
1031 | buffer_put_cstring(&b, chost); | |
1032 | buffer_put_cstring(&b, authctxt->local_user); | |
1033 | #ifdef DEBUG_PK | |
1034 | buffer_dump(&b); | |
1035 | #endif | |
39c00dc2 | 1036 | if (sensitive->external_keysign) |
1037 | ok = ssh_keysign(private, &signature, &slen, | |
1038 | buffer_ptr(&b), buffer_len(&b)); | |
1039 | else | |
1040 | ok = key_sign(private, &signature, &slen, | |
1041 | buffer_ptr(&b), buffer_len(&b)); | |
8002af61 | 1042 | key_free(private); |
1043 | buffer_free(&b); | |
1044 | if (ok != 0) { | |
1045 | error("key_sign failed"); | |
1046 | xfree(chost); | |
1047 | xfree(pkalg); | |
1048 | return 0; | |
1049 | } | |
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(pkalg); | |
1055 | packet_put_string(blob, blen); | |
1056 | packet_put_cstring(chost); | |
1057 | packet_put_cstring(authctxt->local_user); | |
1058 | packet_put_string(signature, slen); | |
1059 | memset(signature, 's', slen); | |
1060 | xfree(signature); | |
1061 | xfree(chost); | |
1062 | xfree(pkalg); | |
1063 | ||
1064 | packet_send(); | |
1065 | return 1; | |
1066 | } | |
1067 | ||
188adeb2 | 1068 | /* find auth method */ |
1069 | ||
188adeb2 | 1070 | /* |
1071 | * given auth method name, if configurable options permit this method fill | |
1072 | * in auth_ident field and return true, otherwise return false. | |
1073 | */ | |
396c147e | 1074 | static int |
188adeb2 | 1075 | authmethod_is_enabled(Authmethod *method) |
1076 | { | |
1077 | if (method == NULL) | |
1078 | return 0; | |
1079 | /* return false if options indicate this method is disabled */ | |
1080 | if (method->enabled == NULL || *method->enabled == 0) | |
1081 | return 0; | |
1082 | /* return false if batch mode is enabled but method needs interactive mode */ | |
1083 | if (method->batch_flag != NULL && *method->batch_flag != 0) | |
1084 | return 0; | |
1085 | return 1; | |
1086 | } | |
a306f2dd | 1087 | |
396c147e | 1088 | static Authmethod * |
188adeb2 | 1089 | authmethod_lookup(const char *name) |
1090 | { | |
1091 | Authmethod *method = NULL; | |
1092 | if (name != NULL) | |
1093 | for (method = authmethods; method->name != NULL; method++) | |
1094 | if (strcmp(name, method->name) == 0) | |
1095 | return method; | |
1096 | debug2("Unrecognized authentication method name: %s", name ? name : "NULL"); | |
1097 | return NULL; | |
1098 | } | |
1099 | ||
cab80f75 | 1100 | /* XXX internal state */ |
1101 | static Authmethod *current = NULL; | |
1102 | static char *supported = NULL; | |
1103 | static char *preferred = NULL; | |
d6133f43 | 1104 | |
188adeb2 | 1105 | /* |
1106 | * Given the authentication method list sent by the server, return the | |
1107 | * next method we should try. If the server initially sends a nil list, | |
cd332296 | 1108 | * use a built-in default list. |
2b87da3b | 1109 | */ |
396c147e | 1110 | static Authmethod * |
188adeb2 | 1111 | authmethod_get(char *authlist) |
1112 | { | |
cab80f75 | 1113 | char *name = NULL; |
21b30f38 | 1114 | u_int next; |
2b87da3b | 1115 | |
188adeb2 | 1116 | /* Use a suitable default if we're passed a nil list. */ |
1117 | if (authlist == NULL || strlen(authlist) == 0) | |
cab80f75 | 1118 | authlist = options.preferred_authentications; |
1119 | ||
1120 | if (supported == NULL || strcmp(authlist, supported) != 0) { | |
1121 | debug3("start over, passed a different list %s", authlist); | |
1122 | if (supported != NULL) | |
1123 | xfree(supported); | |
1124 | supported = xstrdup(authlist); | |
1125 | preferred = options.preferred_authentications; | |
1126 | debug3("preferred %s", preferred); | |
1127 | current = NULL; | |
1128 | } else if (current != NULL && authmethod_is_enabled(current)) | |
1129 | return current; | |
188adeb2 | 1130 | |
cab80f75 | 1131 | for (;;) { |
1132 | if ((name = match_list(preferred, supported, &next)) == NULL) { | |
6226a8f8 | 1133 | debug("No more authentication methods to try."); |
cab80f75 | 1134 | current = NULL; |
1135 | return NULL; | |
1136 | } | |
1137 | preferred += next; | |
94ec8c6b | 1138 | debug3("authmethod_lookup %s", name); |
cab80f75 | 1139 | debug3("remaining preferred: %s", preferred); |
1140 | if ((current = authmethod_lookup(name)) != NULL && | |
1141 | authmethod_is_enabled(current)) { | |
94ec8c6b | 1142 | debug3("authmethod_is_enabled %s", name); |
6226a8f8 | 1143 | debug("Next authentication method: %s", name); |
cab80f75 | 1144 | return current; |
94ec8c6b | 1145 | } |
a306f2dd | 1146 | } |
cab80f75 | 1147 | } |
a22aff1f | 1148 | |
ffb215be | 1149 | static char * |
cab80f75 | 1150 | authmethods_get(void) |
1151 | { | |
1152 | Authmethod *method = NULL; | |
3469eac4 | 1153 | Buffer b; |
1154 | char *list; | |
cab80f75 | 1155 | |
3469eac4 | 1156 | buffer_init(&b); |
cab80f75 | 1157 | for (method = authmethods; method->name != NULL; method++) { |
1158 | if (authmethod_is_enabled(method)) { | |
3469eac4 | 1159 | if (buffer_len(&b) > 0) |
1160 | buffer_append(&b, ",", 1); | |
1161 | buffer_append(&b, method->name, strlen(method->name)); | |
cab80f75 | 1162 | } |
1163 | } | |
3469eac4 | 1164 | buffer_append(&b, "\0", 1); |
1165 | list = xstrdup(buffer_ptr(&b)); | |
1166 | buffer_free(&b); | |
1167 | return list; | |
a306f2dd | 1168 | } |