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