]>
Commit | Line | Data |
---|---|---|
3c0ef626 | 1 | /* |
2 | * Copyright (c) 2000 Markus Friedl. All rights reserved. | |
3 | * | |
4 | * Redistribution and use in source and binary forms, with or without | |
5 | * modification, are permitted provided that the following conditions | |
6 | * are met: | |
7 | * 1. Redistributions of source code must retain the above copyright | |
8 | * notice, this list of conditions and the following disclaimer. | |
9 | * 2. Redistributions in binary form must reproduce the above copyright | |
10 | * notice, this list of conditions and the following disclaimer in the | |
11 | * documentation and/or other materials provided with the distribution. | |
12 | * | |
13 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |
14 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
15 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
16 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | |
17 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
18 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
19 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
20 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
21 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
22 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
23 | */ | |
24 | ||
25 | #include "includes.h" | |
2ce0bfe4 | 26 | RCSID("$OpenBSD: sshconnect2.c,v 1.142 2005/08/30 22:08:05 djm Exp $"); |
7cac2b65 | 27 | |
28 | #include "openbsd-compat/sys-queue.h" | |
3c0ef626 | 29 | |
30 | #include "ssh.h" | |
31 | #include "ssh2.h" | |
32 | #include "xmalloc.h" | |
3c0ef626 | 33 | #include "buffer.h" |
34 | #include "packet.h" | |
3c0ef626 | 35 | #include "compat.h" |
36 | #include "bufaux.h" | |
37 | #include "cipher.h" | |
38 | #include "kex.h" | |
39 | #include "myproposal.h" | |
3c0ef626 | 40 | #include "sshconnect.h" |
41 | #include "authfile.h" | |
42 | #include "dh.h" | |
43 | #include "authfd.h" | |
44 | #include "log.h" | |
45 | #include "readconf.h" | |
7e82606e | 46 | #include "misc.h" |
3c0ef626 | 47 | #include "match.h" |
48 | #include "dispatch.h" | |
49 | #include "canohost.h" | |
44a053a3 | 50 | #include "msg.h" |
51 | #include "pathnames.h" | |
3c0ef626 | 52 | |
5598e598 | 53 | #ifdef GSSAPI |
54 | #include "ssh-gss.h" | |
55 | #endif | |
56 | ||
3c0ef626 | 57 | /* import */ |
58 | extern char *client_version_string; | |
59 | extern char *server_version_string; | |
60 | extern Options options; | |
61 | ||
62 | /* | |
63 | * SSH2 key exchange | |
64 | */ | |
65 | ||
66 | u_char *session_id2 = NULL; | |
7cac2b65 | 67 | u_int session_id2_len = 0; |
3c0ef626 | 68 | |
69 | char *xxx_host; | |
70 | struct sockaddr *xxx_hostaddr; | |
71 | ||
72 | Kex *xxx_kex = NULL; | |
73 | ||
74 | static int | |
75 | verify_host_key_callback(Key *hostkey) | |
76 | { | |
77 | if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1) | |
78 | fatal("Host key verification failed."); | |
79 | return 0; | |
80 | } | |
81 | ||
82 | void | |
83 | ssh_kex2(char *host, struct sockaddr *hostaddr) | |
84 | { | |
85 | Kex *kex; | |
1e608e42 | 86 | #ifdef GSSAPI |
06816bdd | 87 | char *orig=NULL, *gss=NULL; |
1e608e42 | 88 | int len; |
89 | #endif | |
3c0ef626 | 90 | |
91 | xxx_host = host; | |
92 | xxx_hostaddr = hostaddr; | |
93 | ||
5598e598 | 94 | #ifdef GSSAPI |
7a056ed1 | 95 | if (options.gss_keyex) { |
1e608e42 | 96 | /* Add the GSSAPI mechanisms currently supported on this client to |
97 | * the key exchange algorithm proposal */ | |
98 | orig = myproposal[PROPOSAL_KEX_ALGS]; | |
2ae11cdc | 99 | gss = ssh_gssapi_client_mechanisms((char *)get_canonical_hostname(1)); |
1e608e42 | 100 | if (gss) { |
101 | len = strlen(orig)+strlen(gss)+2; | |
102 | myproposal[PROPOSAL_KEX_ALGS]=xmalloc(len); | |
103 | snprintf(myproposal[PROPOSAL_KEX_ALGS],len,"%s,%s",gss,orig); | |
5598e598 | 104 | } |
7a056ed1 | 105 | } |
5598e598 | 106 | #endif |
107 | ||
3c0ef626 | 108 | if (options.ciphers == (char *)-1) { |
7cac2b65 | 109 | logit("No valid ciphers for protocol version 2 given, using defaults."); |
3c0ef626 | 110 | options.ciphers = NULL; |
111 | } | |
112 | if (options.ciphers != NULL) { | |
113 | myproposal[PROPOSAL_ENC_ALGS_CTOS] = | |
114 | myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers; | |
115 | } | |
116 | myproposal[PROPOSAL_ENC_ALGS_CTOS] = | |
117 | compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]); | |
118 | myproposal[PROPOSAL_ENC_ALGS_STOC] = | |
119 | compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]); | |
120 | if (options.compression) { | |
121 | myproposal[PROPOSAL_COMP_ALGS_CTOS] = | |
2ce0bfe4 | 122 | myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib@openssh.com,zlib,none"; |
3c0ef626 | 123 | } else { |
124 | myproposal[PROPOSAL_COMP_ALGS_CTOS] = | |
2ce0bfe4 | 125 | myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com,zlib"; |
3c0ef626 | 126 | } |
127 | if (options.macs != NULL) { | |
128 | myproposal[PROPOSAL_MAC_ALGS_CTOS] = | |
129 | myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs; | |
130 | } | |
131 | if (options.hostkeyalgorithms != NULL) | |
1e608e42 | 132 | myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = |
3c0ef626 | 133 | options.hostkeyalgorithms; |
134 | ||
1e608e42 | 135 | #ifdef GSSAPI |
136 | /* If we've got GSSAPI algorithms, then we also support the | |
137 | * 'null' hostkey, as a last resort */ | |
7a056ed1 | 138 | if (options.gss_keyex && gss) { |
23987cb8 | 139 | orig=myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS]; |
140 | len = strlen(orig)+sizeof(",null"); | |
141 | myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS]=xmalloc(len); | |
142 | snprintf(myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS],len,"%s,null",orig); | |
1e608e42 | 143 | } |
144 | #endif | |
7cac2b65 | 145 | |
146 | if (options.rekey_limit) | |
147 | packet_set_rekey_limit(options.rekey_limit); | |
148 | ||
3c0ef626 | 149 | /* start key exchange */ |
150 | kex = kex_setup(myproposal); | |
bfe49944 | 151 | kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client; |
7e82606e | 152 | kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client; |
bfe49944 | 153 | kex->kex[KEX_DH_GEX_SHA1] = kexgex_client; |
154 | #ifdef GSSAPI | |
155 | kex->kex[KEX_GSS_GRP1_SHA1] = kexgss_client; | |
156 | #endif | |
3c0ef626 | 157 | kex->client_version_string=client_version_string; |
158 | kex->server_version_string=server_version_string; | |
159 | kex->verify_host_key=&verify_host_key_callback; | |
5598e598 | 160 | #ifdef GSSAPI |
161 | kex->options.gss_deleg_creds=options.gss_deleg_creds; | |
162 | #endif | |
7cac2b65 | 163 | |
3c0ef626 | 164 | xxx_kex = kex; |
165 | ||
166 | dispatch_run(DISPATCH_BLOCK, &kex->done, kex); | |
167 | ||
168 | session_id2 = kex->session_id; | |
169 | session_id2_len = kex->session_id_len; | |
170 | ||
171 | #ifdef DEBUG_KEXDH | |
172 | /* send 1st encrypted/maced/compressed message */ | |
173 | packet_start(SSH2_MSG_IGNORE); | |
174 | packet_put_cstring("markus"); | |
175 | packet_send(); | |
176 | packet_write_wait(); | |
177 | #endif | |
3c0ef626 | 178 | } |
179 | ||
180 | /* | |
181 | * Authenticate user | |
182 | */ | |
183 | ||
184 | typedef struct Authctxt Authctxt; | |
185 | typedef struct Authmethod Authmethod; | |
7cac2b65 | 186 | typedef struct identity Identity; |
187 | typedef struct idlist Idlist; | |
188 | ||
189 | struct identity { | |
190 | TAILQ_ENTRY(identity) next; | |
191 | AuthenticationConnection *ac; /* set if agent supports key */ | |
192 | Key *key; /* public/private key */ | |
193 | char *filename; /* comment for agent-only keys */ | |
194 | int tried; | |
195 | int isprivate; /* key points to the private key */ | |
196 | }; | |
197 | TAILQ_HEAD(idlist, identity); | |
3c0ef626 | 198 | |
199 | struct Authctxt { | |
200 | const char *server_user; | |
201 | const char *local_user; | |
202 | const char *host; | |
203 | const char *service; | |
204 | Authmethod *method; | |
205 | int success; | |
206 | char *authlist; | |
207 | /* pubkey */ | |
7cac2b65 | 208 | Idlist keys; |
3c0ef626 | 209 | AuthenticationConnection *agent; |
210 | /* hostbased */ | |
44a053a3 | 211 | Sensitive *sensitive; |
3c0ef626 | 212 | /* kbd-interactive */ |
213 | int info_req_seen; | |
5598e598 | 214 | /* generic */ |
215 | void *methoddata; | |
3c0ef626 | 216 | }; |
217 | struct Authmethod { | |
218 | char *name; /* string to compare against server's list */ | |
219 | int (*userauth)(Authctxt *authctxt); | |
220 | int *enabled; /* flag in option struct that enables method */ | |
221 | int *batch_flag; /* flag in option struct that disables method */ | |
222 | }; | |
223 | ||
1e608e42 | 224 | void input_userauth_success(int, u_int32_t, void *); |
225 | void input_userauth_failure(int, u_int32_t, void *); | |
226 | void input_userauth_banner(int, u_int32_t, void *); | |
227 | void input_userauth_error(int, u_int32_t, void *); | |
228 | void input_userauth_info_req(int, u_int32_t, void *); | |
229 | void input_userauth_pk_ok(int, u_int32_t, void *); | |
510132b6 | 230 | void input_userauth_passwd_changereq(int, u_int32_t, void *); |
3c0ef626 | 231 | |
1e608e42 | 232 | int userauth_none(Authctxt *); |
233 | int userauth_pubkey(Authctxt *); | |
234 | int userauth_passwd(Authctxt *); | |
235 | int userauth_kbdint(Authctxt *); | |
236 | int userauth_hostbased(Authctxt *); | |
7cac2b65 | 237 | int userauth_kerberos(Authctxt *); |
238 | ||
239 | #ifdef GSSAPI | |
240 | int userauth_gssapi(Authctxt *authctxt); | |
241 | void input_gssapi_response(int type, u_int32_t, void *); | |
242 | void input_gssapi_token(int type, u_int32_t, void *); | |
243 | void input_gssapi_hash(int type, u_int32_t, void *); | |
244 | void input_gssapi_error(int, u_int32_t, void *); | |
245 | void input_gssapi_errtok(int, u_int32_t, void *); | |
246 | #endif | |
3c0ef626 | 247 | |
5598e598 | 248 | #ifdef GSSAPI |
1e608e42 | 249 | int userauth_external(Authctxt *authctxt); |
250 | int userauth_gssapi(Authctxt *authctxt); | |
540d72c3 | 251 | int userauth_gssapi_with_mic(Authctxt *authctxt); |
252 | int userauth_gssapi_without_mic(Authctxt *authctxt); | |
e23e524c | 253 | void input_gssapi_response(int type, u_int32_t, void *); |
254 | void input_gssapi_token(int type, u_int32_t, void *); | |
255 | void input_gssapi_hash(int type, u_int32_t, void *); | |
23987cb8 | 256 | void input_gssapi_error(int, u_int32_t, void *); |
e23e524c | 257 | void input_gssapi_errtok(int, u_int32_t, void *); |
5598e598 | 258 | #endif |
259 | ||
1e608e42 | 260 | void userauth(Authctxt *, char *); |
3c0ef626 | 261 | |
7cac2b65 | 262 | static int sign_and_send_pubkey(Authctxt *, Identity *); |
263 | static void pubkey_prepare(Authctxt *); | |
264 | static void pubkey_cleanup(Authctxt *); | |
265 | static Key *load_identity_file(char *); | |
3c0ef626 | 266 | |
267 | static Authmethod *authmethod_get(char *authlist); | |
268 | static Authmethod *authmethod_lookup(const char *name); | |
269 | static char *authmethods_get(void); | |
270 | ||
271 | Authmethod authmethods[] = { | |
5598e598 | 272 | #ifdef GSSAPI |
273 | {"external-keyx", | |
274 | userauth_external, | |
275 | &options.gss_authentication, | |
276 | NULL}, | |
540d72c3 | 277 | {"gssapi-with-mic", |
51b9ca75 | 278 | userauth_gssapi, |
540d72c3 | 279 | &options.gss_authentication, |
280 | NULL}, | |
5598e598 | 281 | {"gssapi", |
51b9ca75 | 282 | userauth_gssapi, |
5598e598 | 283 | &options.gss_authentication, |
284 | NULL}, | |
285 | #endif | |
3c0ef626 | 286 | {"hostbased", |
287 | userauth_hostbased, | |
288 | &options.hostbased_authentication, | |
289 | NULL}, | |
290 | {"publickey", | |
291 | userauth_pubkey, | |
292 | &options.pubkey_authentication, | |
293 | NULL}, | |
294 | {"keyboard-interactive", | |
295 | userauth_kbdint, | |
296 | &options.kbd_interactive_authentication, | |
297 | &options.batch_mode}, | |
298 | {"password", | |
299 | userauth_passwd, | |
300 | &options.password_authentication, | |
301 | &options.batch_mode}, | |
302 | {"none", | |
303 | userauth_none, | |
304 | NULL, | |
305 | NULL}, | |
306 | {NULL, NULL, NULL, NULL} | |
307 | }; | |
308 | ||
309 | void | |
310 | ssh_userauth2(const char *local_user, const char *server_user, char *host, | |
44a053a3 | 311 | Sensitive *sensitive) |
3c0ef626 | 312 | { |
313 | Authctxt authctxt; | |
314 | int type; | |
3c0ef626 | 315 | |
316 | if (options.challenge_response_authentication) | |
317 | options.kbd_interactive_authentication = 1; | |
318 | ||
3c0ef626 | 319 | packet_start(SSH2_MSG_SERVICE_REQUEST); |
320 | packet_put_cstring("ssh-userauth"); | |
321 | packet_send(); | |
bfe49944 | 322 | debug("SSH2_MSG_SERVICE_REQUEST sent"); |
3c0ef626 | 323 | packet_write_wait(); |
1e608e42 | 324 | type = packet_read(); |
bfe49944 | 325 | if (type != SSH2_MSG_SERVICE_ACCEPT) |
326 | fatal("Server denied authentication request: %d", type); | |
3c0ef626 | 327 | if (packet_remaining() > 0) { |
1e608e42 | 328 | char *reply = packet_get_string(NULL); |
bfe49944 | 329 | debug2("service_accept: %s", reply); |
3c0ef626 | 330 | xfree(reply); |
331 | } else { | |
bfe49944 | 332 | debug2("buggy server: service_accept w/o service"); |
3c0ef626 | 333 | } |
1e608e42 | 334 | packet_check_eom(); |
bfe49944 | 335 | debug("SSH2_MSG_SERVICE_ACCEPT received"); |
3c0ef626 | 336 | |
337 | if (options.preferred_authentications == NULL) | |
338 | options.preferred_authentications = authmethods_get(); | |
339 | ||
340 | /* setup authentication context */ | |
341 | memset(&authctxt, 0, sizeof(authctxt)); | |
7cac2b65 | 342 | pubkey_prepare(&authctxt); |
3c0ef626 | 343 | authctxt.server_user = server_user; |
344 | authctxt.local_user = local_user; | |
345 | authctxt.host = host; | |
346 | authctxt.service = "ssh-connection"; /* service name */ | |
347 | authctxt.success = 0; | |
348 | authctxt.method = authmethod_lookup("none"); | |
349 | authctxt.authlist = NULL; | |
5598e598 | 350 | authctxt.methoddata = NULL; |
44a053a3 | 351 | authctxt.sensitive = sensitive; |
3c0ef626 | 352 | authctxt.info_req_seen = 0; |
353 | if (authctxt.method == NULL) | |
354 | fatal("ssh_userauth2: internal error: cannot send userauth none request"); | |
355 | ||
356 | /* initial userauth request */ | |
357 | userauth_none(&authctxt); | |
358 | ||
359 | dispatch_init(&input_userauth_error); | |
360 | dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success); | |
361 | dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure); | |
362 | dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner); | |
363 | dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */ | |
364 | ||
7cac2b65 | 365 | pubkey_cleanup(&authctxt); |
366 | dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL); | |
3c0ef626 | 367 | |
bfe49944 | 368 | debug("Authentication succeeded (%s).", authctxt.method->name); |
3c0ef626 | 369 | } |
7cac2b65 | 370 | |
3c0ef626 | 371 | void |
372 | userauth(Authctxt *authctxt, char *authlist) | |
373 | { | |
7cac2b65 | 374 | if (authctxt->methoddata) { |
5598e598 | 375 | xfree(authctxt->methoddata); |
7cac2b65 | 376 | authctxt->methoddata = NULL; |
5598e598 | 377 | } |
3c0ef626 | 378 | if (authlist == NULL) { |
379 | authlist = authctxt->authlist; | |
380 | } else { | |
381 | if (authctxt->authlist) | |
382 | xfree(authctxt->authlist); | |
383 | authctxt->authlist = authlist; | |
384 | } | |
385 | for (;;) { | |
386 | Authmethod *method = authmethod_get(authlist); | |
387 | if (method == NULL) | |
388 | fatal("Permission denied (%s).", authlist); | |
389 | authctxt->method = method; | |
7cac2b65 | 390 | |
391 | /* reset the per method handler */ | |
392 | dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN, | |
393 | SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL); | |
394 | ||
395 | /* and try new method */ | |
3c0ef626 | 396 | if (method->userauth(authctxt) != 0) { |
397 | debug2("we sent a %s packet, wait for reply", method->name); | |
398 | break; | |
399 | } else { | |
400 | debug2("we did not send a packet, disable method"); | |
401 | method->enabled = NULL; | |
402 | } | |
403 | } | |
404 | } | |
276b07a3 | 405 | |
3c0ef626 | 406 | void |
1e608e42 | 407 | input_userauth_error(int type, u_int32_t seq, void *ctxt) |
3c0ef626 | 408 | { |
409 | fatal("input_userauth_error: bad message during authentication: " | |
2ce0bfe4 | 410 | "type %d", type); |
3c0ef626 | 411 | } |
276b07a3 | 412 | |
3c0ef626 | 413 | void |
1e608e42 | 414 | input_userauth_banner(int type, u_int32_t seq, void *ctxt) |
3c0ef626 | 415 | { |
416 | char *msg, *lang; | |
540d72c3 | 417 | |
3c0ef626 | 418 | debug3("input_userauth_banner"); |
419 | msg = packet_get_string(NULL); | |
420 | lang = packet_get_string(NULL); | |
540d72c3 | 421 | if (options.log_level > SYSLOG_LEVEL_QUIET) |
422 | fprintf(stderr, "%s", msg); | |
3c0ef626 | 423 | xfree(msg); |
424 | xfree(lang); | |
425 | } | |
276b07a3 | 426 | |
3c0ef626 | 427 | void |
1e608e42 | 428 | input_userauth_success(int type, u_int32_t seq, void *ctxt) |
3c0ef626 | 429 | { |
430 | Authctxt *authctxt = ctxt; | |
431 | if (authctxt == NULL) | |
432 | fatal("input_userauth_success: no authentication context"); | |
540d72c3 | 433 | if (authctxt->authlist) { |
3c0ef626 | 434 | xfree(authctxt->authlist); |
540d72c3 | 435 | authctxt->authlist = NULL; |
436 | } | |
437 | if (authctxt->methoddata) { | |
5598e598 | 438 | xfree(authctxt->methoddata); |
540d72c3 | 439 | authctxt->methoddata = NULL; |
440 | } | |
3c0ef626 | 441 | authctxt->success = 1; /* break out */ |
442 | } | |
276b07a3 | 443 | |
3c0ef626 | 444 | void |
1e608e42 | 445 | input_userauth_failure(int type, u_int32_t seq, void *ctxt) |
3c0ef626 | 446 | { |
447 | Authctxt *authctxt = ctxt; | |
448 | char *authlist = NULL; | |
449 | int partial; | |
450 | ||
451 | if (authctxt == NULL) | |
452 | fatal("input_userauth_failure: no authentication context"); | |
453 | ||
454 | authlist = packet_get_string(NULL); | |
455 | partial = packet_get_char(); | |
1e608e42 | 456 | packet_check_eom(); |
3c0ef626 | 457 | |
458 | if (partial != 0) | |
7cac2b65 | 459 | logit("Authenticated with partial success."); |
bfe49944 | 460 | debug("Authentications that can continue: %s", authlist); |
3c0ef626 | 461 | |
3c0ef626 | 462 | userauth(authctxt, authlist); |
463 | } | |
464 | void | |
1e608e42 | 465 | input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt) |
3c0ef626 | 466 | { |
467 | Authctxt *authctxt = ctxt; | |
468 | Key *key = NULL; | |
7cac2b65 | 469 | Identity *id = NULL; |
3c0ef626 | 470 | Buffer b; |
1e608e42 | 471 | int pktype, sent = 0; |
472 | u_int alen, blen; | |
473 | char *pkalg, *fp; | |
474 | u_char *pkblob; | |
3c0ef626 | 475 | |
476 | if (authctxt == NULL) | |
477 | fatal("input_userauth_pk_ok: no authentication context"); | |
478 | if (datafellows & SSH_BUG_PKOK) { | |
479 | /* this is similar to SSH_BUG_PKAUTH */ | |
480 | debug2("input_userauth_pk_ok: SSH_BUG_PKOK"); | |
481 | pkblob = packet_get_string(&blen); | |
482 | buffer_init(&b); | |
483 | buffer_append(&b, pkblob, blen); | |
484 | pkalg = buffer_get_string(&b, &alen); | |
485 | buffer_free(&b); | |
486 | } else { | |
487 | pkalg = packet_get_string(&alen); | |
488 | pkblob = packet_get_string(&blen); | |
489 | } | |
1e608e42 | 490 | packet_check_eom(); |
3c0ef626 | 491 | |
7cac2b65 | 492 | debug("Server accepts key: pkalg %s blen %u", pkalg, blen); |
3c0ef626 | 493 | |
7cac2b65 | 494 | if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) { |
495 | debug("unknown pkalg %s", pkalg); | |
496 | goto done; | |
497 | } | |
498 | if ((key = key_from_blob(pkblob, blen)) == NULL) { | |
499 | debug("no key from blob. pkalg %s", pkalg); | |
500 | goto done; | |
501 | } | |
502 | if (key->type != pktype) { | |
503 | error("input_userauth_pk_ok: type mismatch " | |
504 | "for decoded key (received %d, expected %d)", | |
505 | key->type, pktype); | |
506 | goto done; | |
507 | } | |
508 | fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX); | |
509 | debug2("input_userauth_pk_ok: fp %s", fp); | |
510 | xfree(fp); | |
511 | ||
540d72c3 | 512 | /* |
513 | * search keys in the reverse order, because last candidate has been | |
514 | * moved to the end of the queue. this also avoids confusion by | |
515 | * duplicate keys | |
516 | */ | |
7e82606e | 517 | TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) { |
7cac2b65 | 518 | if (key_equal(key, id->key)) { |
519 | sent = sign_and_send_pubkey(authctxt, id); | |
3c0ef626 | 520 | break; |
521 | } | |
7cac2b65 | 522 | } |
523 | done: | |
3c0ef626 | 524 | if (key != NULL) |
525 | key_free(key); | |
526 | xfree(pkalg); | |
527 | xfree(pkblob); | |
528 | ||
d03f4262 | 529 | /* try another method if we did not send a packet */ |
3c0ef626 | 530 | if (sent == 0) |
531 | userauth(authctxt, NULL); | |
3c0ef626 | 532 | } |
533 | ||
5598e598 | 534 | #ifdef GSSAPI |
540d72c3 | 535 | int |
5598e598 | 536 | userauth_gssapi(Authctxt *authctxt) |
537 | { | |
23987cb8 | 538 | Gssctxt *gssctxt = NULL; |
540d72c3 | 539 | static gss_OID_set gss_supported = NULL; |
2ce0bfe4 | 540 | static u_int mech = 0; |
23987cb8 | 541 | OM_uint32 min; |
7cac2b65 | 542 | int ok = 0; |
23987cb8 | 543 | |
35094e47 | 544 | if (!options.gss_authentication) { |
545 | verbose("GSSAPI authentication disabled."); | |
546 | return 0; | |
547 | } | |
548 | ||
7cac2b65 | 549 | /* Try one GSSAPI method at a time, rather than sending them all at |
550 | * once. */ | |
5598e598 | 551 | |
540d72c3 | 552 | if (gss_supported == NULL) |
553 | gss_indicate_mechs(&min, &gss_supported); | |
b59afbfe | 554 | |
7cac2b65 | 555 | /* Check to see if the mechanism is usable before we offer it */ |
540d72c3 | 556 | while (mech < gss_supported->count && !ok) { |
7cac2b65 | 557 | if (gssctxt) |
558 | ssh_gssapi_delete_ctx(&gssctxt); | |
23987cb8 | 559 | ssh_gssapi_build_ctx(&gssctxt); |
540d72c3 | 560 | ssh_gssapi_set_oid(gssctxt, &gss_supported->elements[mech]); |
23987cb8 | 561 | |
7cac2b65 | 562 | /* My DER encoding requires length<128 */ |
540d72c3 | 563 | if (gss_supported->elements[mech].length < 128 && |
f1c93965 | 564 | ssh_gssapi_check_mechanism(&gss_supported->elements[mech], |
565 | get_canonical_hostname(1)) && | |
23987cb8 | 566 | !GSS_ERROR(ssh_gssapi_import_name(gssctxt, |
c2a5fa61 | 567 | get_canonical_hostname(1)))) { |
7cac2b65 | 568 | ok = 1; /* Mechanism works */ |
23987cb8 | 569 | } else { |
570 | mech++; | |
571 | } | |
5598e598 | 572 | } |
7cac2b65 | 573 | |
2ce0bfe4 | 574 | if (!ok) |
575 | return 0; | |
7cac2b65 | 576 | |
5598e598 | 577 | authctxt->methoddata=(void *)gssctxt; |
7cac2b65 | 578 | |
5598e598 | 579 | packet_start(SSH2_MSG_USERAUTH_REQUEST); |
23987cb8 | 580 | packet_put_cstring(authctxt->server_user); |
5598e598 | 581 | packet_put_cstring(authctxt->service); |
7cac2b65 | 582 | packet_put_cstring(authctxt->method->name); |
583 | ||
23987cb8 | 584 | packet_put_int(1); |
5598e598 | 585 | |
dfddba3d | 586 | packet_put_int((gss_supported->elements[mech].length) + 2); |
587 | packet_put_char(SSH_GSS_OIDTYPE); | |
588 | packet_put_char(gss_supported->elements[mech].length); | |
589 | packet_put_raw(gss_supported->elements[mech].elements, | |
590 | gss_supported->elements[mech].length); | |
91a31bec | 591 | |
7cac2b65 | 592 | packet_send(); |
593 | ||
594 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response); | |
595 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token); | |
596 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error); | |
597 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok); | |
5598e598 | 598 | |
23987cb8 | 599 | mech++; /* Move along to next candidate */ |
91a31bec | 600 | |
7cac2b65 | 601 | return 1; |
5598e598 | 602 | } |
603 | ||
540d72c3 | 604 | static OM_uint32 |
605 | process_gssapi_token(void *ctxt, gss_buffer_t recv_tok) | |
606 | { | |
607 | Authctxt *authctxt = ctxt; | |
608 | Gssctxt *gssctxt = authctxt->methoddata; | |
609 | gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER; | |
2ce0bfe4 | 610 | gss_buffer_desc mic = GSS_C_EMPTY_BUFFER; |
611 | gss_buffer_desc gssbuf; | |
540d72c3 | 612 | OM_uint32 status, ms, flags; |
613 | Buffer b; | |
614 | ||
615 | status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds, | |
616 | recv_tok, &send_tok, &flags); | |
617 | ||
618 | if (send_tok.length > 0) { | |
619 | if (GSS_ERROR(status)) | |
620 | packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK); | |
621 | else | |
622 | packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN); | |
623 | ||
624 | packet_put_string(send_tok.value, send_tok.length); | |
625 | packet_send(); | |
626 | gss_release_buffer(&ms, &send_tok); | |
627 | } | |
628 | ||
629 | if (status == GSS_S_COMPLETE) { | |
630 | /* send either complete or MIC, depending on mechanism */ | |
51b9ca75 | 631 | if (strcmp(authctxt->method->name,"gssapi")==0 || |
632 | (!(flags & GSS_C_INTEG_FLAG))) { | |
540d72c3 | 633 | packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE); |
634 | packet_send(); | |
635 | } else { | |
636 | ssh_gssapi_buildmic(&b, authctxt->server_user, | |
637 | authctxt->service, "gssapi-with-mic"); | |
638 | ||
639 | gssbuf.value = buffer_ptr(&b); | |
640 | gssbuf.length = buffer_len(&b); | |
641 | ||
642 | status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic); | |
643 | ||
644 | if (!GSS_ERROR(status)) { | |
645 | packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC); | |
646 | packet_put_string(mic.value, mic.length); | |
647 | ||
648 | packet_send(); | |
649 | } | |
650 | ||
651 | buffer_free(&b); | |
652 | gss_release_buffer(&ms, &mic); | |
653 | } | |
654 | } | |
655 | ||
656 | return status; | |
657 | } | |
658 | ||
5598e598 | 659 | void |
7cac2b65 | 660 | input_gssapi_response(int type, u_int32_t plen, void *ctxt) |
5598e598 | 661 | { |
662 | Authctxt *authctxt = ctxt; | |
663 | Gssctxt *gssctxt; | |
5598e598 | 664 | int oidlen; |
665 | char *oidv; | |
7cac2b65 | 666 | |
5598e598 | 667 | if (authctxt == NULL) |
668 | fatal("input_gssapi_response: no authentication context"); | |
669 | gssctxt = authctxt->methoddata; | |
7cac2b65 | 670 | |
5598e598 | 671 | /* Setup our OID */ |
7cac2b65 | 672 | oidv = packet_get_string(&oidlen); |
673 | ||
dfddba3d | 674 | if (oidlen <= 2 || |
675 | oidv[0] != SSH_GSS_OIDTYPE || | |
676 | oidv[1] != oidlen - 2) { | |
677 | xfree(oidv); | |
678 | debug("Badly encoded mechanism OID received"); | |
679 | userauth(authctxt, NULL); | |
680 | return; | |
23987cb8 | 681 | } |
7cac2b65 | 682 | |
dfddba3d | 683 | if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2)) |
684 | fatal("Server returned different OID than expected"); | |
685 | ||
1e608e42 | 686 | packet_check_eom(); |
7cac2b65 | 687 | |
688 | xfree(oidv); | |
689 | ||
540d72c3 | 690 | if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) { |
5598e598 | 691 | /* Start again with next method on list */ |
692 | debug("Trying to start again"); | |
7cac2b65 | 693 | userauth(authctxt, NULL); |
5598e598 | 694 | return; |
695 | } | |
5598e598 | 696 | } |
697 | ||
698 | void | |
1e608e42 | 699 | input_gssapi_token(int type, u_int32_t plen, void *ctxt) |
5598e598 | 700 | { |
701 | Authctxt *authctxt = ctxt; | |
7cac2b65 | 702 | gss_buffer_desc recv_tok; |
540d72c3 | 703 | OM_uint32 status; |
007914b3 | 704 | u_int slen; |
7cac2b65 | 705 | |
5598e598 | 706 | if (authctxt == NULL) |
7cac2b65 | 707 | fatal("input_gssapi_response: no authentication context"); |
5598e598 | 708 | |
7cac2b65 | 709 | recv_tok.value = packet_get_string(&slen); |
710 | recv_tok.length = slen; /* safe typecast */ | |
5598e598 | 711 | |
1e608e42 | 712 | packet_check_eom(); |
7cac2b65 | 713 | |
540d72c3 | 714 | status = process_gssapi_token(ctxt, &recv_tok); |
7cac2b65 | 715 | |
716 | xfree(recv_tok.value); | |
717 | ||
5598e598 | 718 | if (GSS_ERROR(status)) { |
719 | /* Start again with the next method in the list */ | |
7cac2b65 | 720 | userauth(authctxt, NULL); |
5598e598 | 721 | return; |
722 | } | |
5598e598 | 723 | } |
724 | ||
e23e524c | 725 | void |
726 | input_gssapi_errtok(int type, u_int32_t plen, void *ctxt) | |
727 | { | |
728 | Authctxt *authctxt = ctxt; | |
729 | Gssctxt *gssctxt; | |
7cac2b65 | 730 | gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER; |
731 | gss_buffer_desc recv_tok; | |
732 | OM_uint32 status, ms; | |
733 | u_int len; | |
734 | ||
e23e524c | 735 | if (authctxt == NULL) |
736 | fatal("input_gssapi_response: no authentication context"); | |
737 | gssctxt = authctxt->methoddata; | |
e23e524c | 738 | |
7cac2b65 | 739 | recv_tok.value = packet_get_string(&len); |
740 | recv_tok.length = len; | |
e23e524c | 741 | |
742 | packet_check_eom(); | |
7cac2b65 | 743 | |
744 | /* Stick it into GSSAPI and see what it says */ | |
745 | status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds, | |
2ce0bfe4 | 746 | &recv_tok, &send_tok, NULL); |
7cac2b65 | 747 | |
748 | xfree(recv_tok.value); | |
749 | gss_release_buffer(&ms, &send_tok); | |
750 | ||
751 | /* Server will be returning a failed packet after this one */ | |
e23e524c | 752 | } |
753 | ||
23987cb8 | 754 | void |
755 | input_gssapi_error(int type, u_int32_t plen, void *ctxt) | |
756 | { | |
7cac2b65 | 757 | OM_uint32 maj, min; |
23987cb8 | 758 | char *msg; |
759 | char *lang; | |
7cac2b65 | 760 | |
23987cb8 | 761 | maj=packet_get_int(); |
762 | min=packet_get_int(); | |
763 | msg=packet_get_string(NULL); | |
764 | lang=packet_get_string(NULL); | |
765 | ||
766 | packet_check_eom(); | |
7cac2b65 | 767 | |
768 | debug("Server GSSAPI Error:\n%s\n", msg); | |
23987cb8 | 769 | xfree(msg); |
770 | xfree(lang); | |
771 | } | |
772 | ||
5598e598 | 773 | int |
774 | userauth_external(Authctxt *authctxt) | |
775 | { | |
7cac2b65 | 776 | static int attempt = 0; |
5598e598 | 777 | |
778 | if (attempt++ >= 1) | |
779 | return 0; | |
780 | ||
41cbd9f8 | 781 | /* The client MUST NOT try this method if initial key exchange |
782 | was not performed using a GSSAPI-based key exchange | |
783 | method. */ | |
784 | if (xxx_kex->kex_type != KEX_GSS_GRP1_SHA1) { | |
785 | debug2("gsskex not performed, skipping external-keyx"); | |
786 | return 0; | |
787 | } | |
788 | ||
5598e598 | 789 | debug2("userauth_external"); |
790 | packet_start(SSH2_MSG_USERAUTH_REQUEST); | |
1baa1dac | 791 | #ifdef GSI |
dfddba3d | 792 | if(options.implicit) { |
2e9d2d36 | 793 | packet_put_cstring(""); |
794 | } else { | |
1baa1dac | 795 | #endif |
2e9d2d36 | 796 | packet_put_cstring(authctxt->server_user); |
1baa1dac | 797 | #ifdef GSI |
2e9d2d36 | 798 | } |
1baa1dac | 799 | #endif |
5598e598 | 800 | packet_put_cstring(authctxt->service); |
801 | packet_put_cstring(authctxt->method->name); | |
802 | packet_send(); | |
803 | packet_write_wait(); | |
804 | return 1; | |
805 | } | |
806 | #endif /* GSSAPI */ | |
807 | ||
3c0ef626 | 808 | int |
809 | userauth_none(Authctxt *authctxt) | |
810 | { | |
811 | /* initial userauth request */ | |
812 | packet_start(SSH2_MSG_USERAUTH_REQUEST); | |
813 | packet_put_cstring(authctxt->server_user); | |
814 | packet_put_cstring(authctxt->service); | |
815 | packet_put_cstring(authctxt->method->name); | |
816 | packet_send(); | |
817 | return 1; | |
818 | } | |
819 | ||
820 | int | |
821 | userauth_passwd(Authctxt *authctxt) | |
822 | { | |
823 | static int attempt = 0; | |
510132b6 | 824 | char prompt[150]; |
3c0ef626 | 825 | char *password; |
826 | ||
827 | if (attempt++ >= options.number_of_password_prompts) | |
828 | return 0; | |
829 | ||
1e608e42 | 830 | if (attempt != 1) |
3c0ef626 | 831 | error("Permission denied, please try again."); |
832 | ||
833 | snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ", | |
834 | authctxt->server_user, authctxt->host); | |
835 | password = read_passphrase(prompt, 0); | |
836 | packet_start(SSH2_MSG_USERAUTH_REQUEST); | |
837 | packet_put_cstring(authctxt->server_user); | |
838 | packet_put_cstring(authctxt->service); | |
839 | packet_put_cstring(authctxt->method->name); | |
840 | packet_put_char(0); | |
841 | packet_put_cstring(password); | |
842 | memset(password, 0, strlen(password)); | |
843 | xfree(password); | |
844 | packet_add_padding(64); | |
845 | packet_send(); | |
510132b6 | 846 | |
44a053a3 | 847 | dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, |
510132b6 | 848 | &input_userauth_passwd_changereq); |
849 | ||
3c0ef626 | 850 | return 1; |
851 | } | |
510132b6 | 852 | /* |
853 | * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST | |
854 | */ | |
855 | void | |
856 | input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt) | |
857 | { | |
858 | Authctxt *authctxt = ctxt; | |
859 | char *info, *lang, *password = NULL, *retype = NULL; | |
860 | char prompt[150]; | |
861 | ||
862 | debug2("input_userauth_passwd_changereq"); | |
863 | ||
864 | if (authctxt == NULL) | |
865 | fatal("input_userauth_passwd_changereq: " | |
866 | "no authentication context"); | |
867 | ||
868 | info = packet_get_string(NULL); | |
869 | lang = packet_get_string(NULL); | |
870 | if (strlen(info) > 0) | |
7cac2b65 | 871 | logit("%s", info); |
510132b6 | 872 | xfree(info); |
873 | xfree(lang); | |
874 | packet_start(SSH2_MSG_USERAUTH_REQUEST); | |
875 | packet_put_cstring(authctxt->server_user); | |
876 | packet_put_cstring(authctxt->service); | |
877 | packet_put_cstring(authctxt->method->name); | |
878 | packet_put_char(1); /* additional info */ | |
44a053a3 | 879 | snprintf(prompt, sizeof(prompt), |
510132b6 | 880 | "Enter %.30s@%.128s's old password: ", |
881 | authctxt->server_user, authctxt->host); | |
882 | password = read_passphrase(prompt, 0); | |
883 | packet_put_cstring(password); | |
884 | memset(password, 0, strlen(password)); | |
885 | xfree(password); | |
886 | password = NULL; | |
887 | while (password == NULL) { | |
44a053a3 | 888 | snprintf(prompt, sizeof(prompt), |
510132b6 | 889 | "Enter %.30s@%.128s's new password: ", |
890 | authctxt->server_user, authctxt->host); | |
891 | password = read_passphrase(prompt, RP_ALLOW_EOF); | |
892 | if (password == NULL) { | |
893 | /* bail out */ | |
894 | return; | |
895 | } | |
44a053a3 | 896 | snprintf(prompt, sizeof(prompt), |
510132b6 | 897 | "Retype %.30s@%.128s's new password: ", |
898 | authctxt->server_user, authctxt->host); | |
899 | retype = read_passphrase(prompt, 0); | |
900 | if (strcmp(password, retype) != 0) { | |
901 | memset(password, 0, strlen(password)); | |
902 | xfree(password); | |
7cac2b65 | 903 | logit("Mismatch; try again, EOF to quit."); |
510132b6 | 904 | password = NULL; |
905 | } | |
906 | memset(retype, 0, strlen(retype)); | |
907 | xfree(retype); | |
908 | } | |
909 | packet_put_cstring(password); | |
910 | memset(password, 0, strlen(password)); | |
911 | xfree(password); | |
912 | packet_add_padding(64); | |
913 | packet_send(); | |
44a053a3 | 914 | |
915 | dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, | |
510132b6 | 916 | &input_userauth_passwd_changereq); |
917 | } | |
3c0ef626 | 918 | |
7cac2b65 | 919 | static int |
920 | identity_sign(Identity *id, u_char **sigp, u_int *lenp, | |
921 | u_char *data, u_int datalen) | |
3c0ef626 | 922 | { |
7cac2b65 | 923 | Key *prv; |
924 | int ret; | |
925 | ||
926 | /* the agent supports this key */ | |
927 | if (id->ac) | |
928 | return (ssh_agent_sign(id->ac, id->key, sigp, lenp, | |
929 | data, datalen)); | |
930 | /* | |
931 | * we have already loaded the private key or | |
932 | * the private key is stored in external hardware | |
933 | */ | |
934 | if (id->isprivate || (id->key->flags & KEY_FLAG_EXT)) | |
935 | return (key_sign(id->key, sigp, lenp, data, datalen)); | |
936 | /* load the private key from the file */ | |
937 | if ((prv = load_identity_file(id->filename)) == NULL) | |
938 | return (-1); | |
939 | ret = key_sign(prv, sigp, lenp, data, datalen); | |
940 | key_free(prv); | |
941 | return (ret); | |
3c0ef626 | 942 | } |
943 | ||
944 | static int | |
7cac2b65 | 945 | sign_and_send_pubkey(Authctxt *authctxt, Identity *id) |
3c0ef626 | 946 | { |
947 | Buffer b; | |
948 | u_char *blob, *signature; | |
1e608e42 | 949 | u_int bloblen, slen; |
7cac2b65 | 950 | u_int skip = 0; |
3c0ef626 | 951 | int ret = -1; |
952 | int have_sig = 1; | |
953 | ||
954 | debug3("sign_and_send_pubkey"); | |
955 | ||
7cac2b65 | 956 | if (key_to_blob(id->key, &blob, &bloblen) == 0) { |
3c0ef626 | 957 | /* we cannot handle this key */ |
958 | debug3("sign_and_send_pubkey: cannot handle key"); | |
959 | return 0; | |
960 | } | |
961 | /* data to be signed */ | |
962 | buffer_init(&b); | |
963 | if (datafellows & SSH_OLD_SESSIONID) { | |
964 | buffer_append(&b, session_id2, session_id2_len); | |
965 | skip = session_id2_len; | |
966 | } else { | |
967 | buffer_put_string(&b, session_id2, session_id2_len); | |
968 | skip = buffer_len(&b); | |
969 | } | |
970 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); | |
971 | buffer_put_cstring(&b, authctxt->server_user); | |
972 | buffer_put_cstring(&b, | |
973 | datafellows & SSH_BUG_PKSERVICE ? | |
974 | "ssh-userauth" : | |
975 | authctxt->service); | |
976 | if (datafellows & SSH_BUG_PKAUTH) { | |
977 | buffer_put_char(&b, have_sig); | |
978 | } else { | |
979 | buffer_put_cstring(&b, authctxt->method->name); | |
980 | buffer_put_char(&b, have_sig); | |
7cac2b65 | 981 | buffer_put_cstring(&b, key_ssh_name(id->key)); |
3c0ef626 | 982 | } |
983 | buffer_put_string(&b, blob, bloblen); | |
984 | ||
985 | /* generate signature */ | |
7cac2b65 | 986 | ret = identity_sign(id, &signature, &slen, |
3c0ef626 | 987 | buffer_ptr(&b), buffer_len(&b)); |
988 | if (ret == -1) { | |
989 | xfree(blob); | |
990 | buffer_free(&b); | |
991 | return 0; | |
992 | } | |
993 | #ifdef DEBUG_PK | |
994 | buffer_dump(&b); | |
995 | #endif | |
996 | if (datafellows & SSH_BUG_PKSERVICE) { | |
997 | buffer_clear(&b); | |
998 | buffer_append(&b, session_id2, session_id2_len); | |
999 | skip = session_id2_len; | |
1000 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); | |
1001 | buffer_put_cstring(&b, authctxt->server_user); | |
1002 | buffer_put_cstring(&b, authctxt->service); | |
1003 | buffer_put_cstring(&b, authctxt->method->name); | |
1004 | buffer_put_char(&b, have_sig); | |
1005 | if (!(datafellows & SSH_BUG_PKAUTH)) | |
7cac2b65 | 1006 | buffer_put_cstring(&b, key_ssh_name(id->key)); |
3c0ef626 | 1007 | buffer_put_string(&b, blob, bloblen); |
1008 | } | |
1009 | xfree(blob); | |
1010 | ||
1011 | /* append signature */ | |
1012 | buffer_put_string(&b, signature, slen); | |
1013 | xfree(signature); | |
1014 | ||
1015 | /* skip session id and packet type */ | |
1016 | if (buffer_len(&b) < skip + 1) | |
1017 | fatal("userauth_pubkey: internal error"); | |
1018 | buffer_consume(&b, skip + 1); | |
1019 | ||
1020 | /* put remaining data from buffer into packet */ | |
1021 | packet_start(SSH2_MSG_USERAUTH_REQUEST); | |
1022 | packet_put_raw(buffer_ptr(&b), buffer_len(&b)); | |
1023 | buffer_free(&b); | |
1024 | packet_send(); | |
1025 | ||
1026 | return 1; | |
1027 | } | |
1028 | ||
1029 | static int | |
7cac2b65 | 1030 | send_pubkey_test(Authctxt *authctxt, Identity *id) |
3c0ef626 | 1031 | { |
1032 | u_char *blob; | |
1e608e42 | 1033 | u_int bloblen, have_sig = 0; |
3c0ef626 | 1034 | |
1035 | debug3("send_pubkey_test"); | |
1036 | ||
7cac2b65 | 1037 | if (key_to_blob(id->key, &blob, &bloblen) == 0) { |
3c0ef626 | 1038 | /* we cannot handle this key */ |
1039 | debug3("send_pubkey_test: cannot handle key"); | |
1040 | return 0; | |
1041 | } | |
1042 | /* register callback for USERAUTH_PK_OK message */ | |
3c0ef626 | 1043 | dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok); |
1044 | ||
1045 | packet_start(SSH2_MSG_USERAUTH_REQUEST); | |
1046 | packet_put_cstring(authctxt->server_user); | |
1047 | packet_put_cstring(authctxt->service); | |
1048 | packet_put_cstring(authctxt->method->name); | |
1049 | packet_put_char(have_sig); | |
1050 | if (!(datafellows & SSH_BUG_PKAUTH)) | |
7cac2b65 | 1051 | packet_put_cstring(key_ssh_name(id->key)); |
3c0ef626 | 1052 | packet_put_string(blob, bloblen); |
1053 | xfree(blob); | |
1054 | packet_send(); | |
1055 | return 1; | |
1056 | } | |
1057 | ||
1058 | static Key * | |
1059 | load_identity_file(char *filename) | |
1060 | { | |
1061 | Key *private; | |
1062 | char prompt[300], *passphrase; | |
1063 | int quit, i; | |
1064 | struct stat st; | |
1065 | ||
1066 | if (stat(filename, &st) < 0) { | |
1067 | debug3("no such identity: %s", filename); | |
1068 | return NULL; | |
1069 | } | |
1070 | private = key_load_private_type(KEY_UNSPEC, filename, "", NULL); | |
1071 | if (private == NULL) { | |
1072 | if (options.batch_mode) | |
1073 | return NULL; | |
1074 | snprintf(prompt, sizeof prompt, | |
1e608e42 | 1075 | "Enter passphrase for key '%.100s': ", filename); |
3c0ef626 | 1076 | for (i = 0; i < options.number_of_password_prompts; i++) { |
1077 | passphrase = read_passphrase(prompt, 0); | |
1078 | if (strcmp(passphrase, "") != 0) { | |
1079 | private = key_load_private_type(KEY_UNSPEC, filename, | |
1080 | passphrase, NULL); | |
1081 | quit = 0; | |
1082 | } else { | |
1083 | debug2("no passphrase given, try next key"); | |
1084 | quit = 1; | |
1085 | } | |
1086 | memset(passphrase, 0, strlen(passphrase)); | |
1087 | xfree(passphrase); | |
1088 | if (private != NULL || quit) | |
1089 | break; | |
1090 | debug2("bad passphrase given, try again..."); | |
1091 | } | |
1092 | } | |
1093 | return private; | |
1094 | } | |
1095 | ||
7cac2b65 | 1096 | /* |
1097 | * try keys in the following order: | |
1098 | * 1. agent keys that are found in the config file | |
1099 | * 2. other agent keys | |
1100 | * 3. keys that are only listed in the config file | |
1101 | */ | |
1102 | static void | |
1103 | pubkey_prepare(Authctxt *authctxt) | |
3c0ef626 | 1104 | { |
7cac2b65 | 1105 | Identity *id; |
1106 | Idlist agent, files, *preferred; | |
1107 | Key *key; | |
1108 | AuthenticationConnection *ac; | |
1109 | char *comment; | |
1110 | int i, found; | |
1111 | ||
1112 | TAILQ_INIT(&agent); /* keys from the agent */ | |
1113 | TAILQ_INIT(&files); /* keys from the config file */ | |
1114 | preferred = &authctxt->keys; | |
1115 | TAILQ_INIT(preferred); /* preferred order of keys */ | |
1116 | ||
1117 | /* list of keys stored in the filesystem */ | |
1118 | for (i = 0; i < options.num_identity_files; i++) { | |
1119 | key = options.identity_keys[i]; | |
1120 | if (key && key->type == KEY_RSA1) | |
1121 | continue; | |
1122 | options.identity_keys[i] = NULL; | |
1123 | id = xmalloc(sizeof(*id)); | |
1124 | memset(id, 0, sizeof(*id)); | |
1125 | id->key = key; | |
1126 | id->filename = xstrdup(options.identity_files[i]); | |
1127 | TAILQ_INSERT_TAIL(&files, id, next); | |
1128 | } | |
1129 | /* list of keys supported by the agent */ | |
1130 | if ((ac = ssh_get_authentication_connection())) { | |
1131 | for (key = ssh_get_first_identity(ac, &comment, 2); | |
1132 | key != NULL; | |
1133 | key = ssh_get_next_identity(ac, &comment, 2)) { | |
1134 | found = 0; | |
1135 | TAILQ_FOREACH(id, &files, next) { | |
540d72c3 | 1136 | /* agent keys from the config file are preferred */ |
7cac2b65 | 1137 | if (key_equal(key, id->key)) { |
1138 | key_free(key); | |
1139 | xfree(comment); | |
1140 | TAILQ_REMOVE(&files, id, next); | |
1141 | TAILQ_INSERT_TAIL(preferred, id, next); | |
1142 | id->ac = ac; | |
1143 | found = 1; | |
1144 | break; | |
1145 | } | |
1146 | } | |
12a403af | 1147 | if (!found && !options.identities_only) { |
7cac2b65 | 1148 | id = xmalloc(sizeof(*id)); |
1149 | memset(id, 0, sizeof(*id)); | |
1150 | id->key = key; | |
1151 | id->filename = comment; | |
1152 | id->ac = ac; | |
1153 | TAILQ_INSERT_TAIL(&agent, id, next); | |
1154 | } | |
1155 | } | |
1156 | /* append remaining agent keys */ | |
1157 | for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) { | |
1158 | TAILQ_REMOVE(&agent, id, next); | |
1159 | TAILQ_INSERT_TAIL(preferred, id, next); | |
1160 | } | |
1161 | authctxt->agent = ac; | |
1162 | } | |
1163 | /* append remaining keys from the config file */ | |
1164 | for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) { | |
1165 | TAILQ_REMOVE(&files, id, next); | |
1166 | TAILQ_INSERT_TAIL(preferred, id, next); | |
1167 | } | |
1168 | TAILQ_FOREACH(id, preferred, next) { | |
1169 | debug2("key: %s (%p)", id->filename, id->key); | |
1170 | } | |
3c0ef626 | 1171 | } |
1172 | ||
7cac2b65 | 1173 | static void |
1174 | pubkey_cleanup(Authctxt *authctxt) | |
3c0ef626 | 1175 | { |
7cac2b65 | 1176 | Identity *id; |
1177 | ||
1178 | if (authctxt->agent != NULL) | |
1179 | ssh_close_authentication_connection(authctxt->agent); | |
1180 | for (id = TAILQ_FIRST(&authctxt->keys); id; | |
1181 | id = TAILQ_FIRST(&authctxt->keys)) { | |
1182 | TAILQ_REMOVE(&authctxt->keys, id, next); | |
1183 | if (id->key) | |
1184 | key_free(id->key); | |
1185 | if (id->filename) | |
1186 | xfree(id->filename); | |
1187 | xfree(id); | |
3c0ef626 | 1188 | } |
3c0ef626 | 1189 | } |
1190 | ||
1191 | int | |
1192 | userauth_pubkey(Authctxt *authctxt) | |
1193 | { | |
7cac2b65 | 1194 | Identity *id; |
3c0ef626 | 1195 | int sent = 0; |
3c0ef626 | 1196 | |
7cac2b65 | 1197 | while ((id = TAILQ_FIRST(&authctxt->keys))) { |
1198 | if (id->tried++) | |
1199 | return (0); | |
540d72c3 | 1200 | /* move key to the end of the queue */ |
7cac2b65 | 1201 | TAILQ_REMOVE(&authctxt->keys, id, next); |
1202 | TAILQ_INSERT_TAIL(&authctxt->keys, id, next); | |
1203 | /* | |
1204 | * send a test message if we have the public key. for | |
1205 | * encrypted keys we cannot do this and have to load the | |
1206 | * private key instead | |
1207 | */ | |
1208 | if (id->key && id->key->type != KEY_RSA1) { | |
1209 | debug("Offering public key: %s", id->filename); | |
1210 | sent = send_pubkey_test(authctxt, id); | |
1211 | } else if (id->key == NULL) { | |
1212 | debug("Trying private key: %s", id->filename); | |
1213 | id->key = load_identity_file(id->filename); | |
1214 | if (id->key != NULL) { | |
1215 | id->isprivate = 1; | |
1216 | sent = sign_and_send_pubkey(authctxt, id); | |
1217 | key_free(id->key); | |
1218 | id->key = NULL; | |
3c0ef626 | 1219 | } |
3c0ef626 | 1220 | } |
7cac2b65 | 1221 | if (sent) |
1222 | return (sent); | |
3c0ef626 | 1223 | } |
7cac2b65 | 1224 | return (0); |
3c0ef626 | 1225 | } |
1226 | ||
1227 | /* | |
1228 | * Send userauth request message specifying keyboard-interactive method. | |
1229 | */ | |
1230 | int | |
1231 | userauth_kbdint(Authctxt *authctxt) | |
1232 | { | |
1233 | static int attempt = 0; | |
1234 | ||
1235 | if (attempt++ >= options.number_of_password_prompts) | |
1236 | return 0; | |
1237 | /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */ | |
1238 | if (attempt > 1 && !authctxt->info_req_seen) { | |
1239 | debug3("userauth_kbdint: disable: no info_req_seen"); | |
1240 | dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL); | |
1241 | return 0; | |
1242 | } | |
1243 | ||
1244 | debug2("userauth_kbdint"); | |
1245 | packet_start(SSH2_MSG_USERAUTH_REQUEST); | |
1246 | packet_put_cstring(authctxt->server_user); | |
1247 | packet_put_cstring(authctxt->service); | |
1248 | packet_put_cstring(authctxt->method->name); | |
1249 | packet_put_cstring(""); /* lang */ | |
1250 | packet_put_cstring(options.kbd_interactive_devices ? | |
1251 | options.kbd_interactive_devices : ""); | |
1252 | packet_send(); | |
1253 | ||
1254 | dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req); | |
1255 | return 1; | |
1256 | } | |
1257 | ||
1258 | /* | |
1259 | * parse INFO_REQUEST, prompt user and send INFO_RESPONSE | |
1260 | */ | |
1261 | void | |
1e608e42 | 1262 | input_userauth_info_req(int type, u_int32_t seq, void *ctxt) |
3c0ef626 | 1263 | { |
1264 | Authctxt *authctxt = ctxt; | |
1265 | char *name, *inst, *lang, *prompt, *response; | |
1266 | u_int num_prompts, i; | |
1267 | int echo = 0; | |
1268 | ||
1269 | debug2("input_userauth_info_req"); | |
1270 | ||
1271 | if (authctxt == NULL) | |
1272 | fatal("input_userauth_info_req: no authentication context"); | |
1273 | ||
1274 | authctxt->info_req_seen = 1; | |
1275 | ||
1276 | name = packet_get_string(NULL); | |
1277 | inst = packet_get_string(NULL); | |
1278 | lang = packet_get_string(NULL); | |
1279 | if (strlen(name) > 0) | |
7cac2b65 | 1280 | logit("%s", name); |
3c0ef626 | 1281 | if (strlen(inst) > 0) |
7cac2b65 | 1282 | logit("%s", inst); |
3c0ef626 | 1283 | xfree(name); |
1284 | xfree(inst); | |
1285 | xfree(lang); | |
1286 | ||
1287 | num_prompts = packet_get_int(); | |
1288 | /* | |
1289 | * Begin to build info response packet based on prompts requested. | |
1290 | * We commit to providing the correct number of responses, so if | |
1291 | * further on we run into a problem that prevents this, we have to | |
1292 | * be sure and clean this up and send a correct error response. | |
1293 | */ | |
1294 | packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE); | |
1295 | packet_put_int(num_prompts); | |
1296 | ||
1297 | debug2("input_userauth_info_req: num_prompts %d", num_prompts); | |
1298 | for (i = 0; i < num_prompts; i++) { | |
1299 | prompt = packet_get_string(NULL); | |
1300 | echo = packet_get_char(); | |
1301 | ||
1302 | response = read_passphrase(prompt, echo ? RP_ECHO : 0); | |
1303 | ||
1304 | packet_put_cstring(response); | |
1305 | memset(response, 0, strlen(response)); | |
1306 | xfree(response); | |
1307 | xfree(prompt); | |
1308 | } | |
1e608e42 | 1309 | packet_check_eom(); /* done with parsing incoming message. */ |
3c0ef626 | 1310 | |
1311 | packet_add_padding(64); | |
1312 | packet_send(); | |
1313 | } | |
1314 | ||
44a053a3 | 1315 | static int |
276b07a3 | 1316 | ssh_keysign(Key *key, u_char **sigp, u_int *lenp, |
44a053a3 | 1317 | u_char *data, u_int datalen) |
1318 | { | |
1319 | Buffer b; | |
1320 | struct stat st; | |
1321 | pid_t pid; | |
1322 | int to[2], from[2], status, version = 2; | |
1323 | ||
bfe49944 | 1324 | debug2("ssh_keysign called"); |
44a053a3 | 1325 | |
1326 | if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) { | |
1327 | error("ssh_keysign: no installed: %s", strerror(errno)); | |
1328 | return -1; | |
1329 | } | |
1330 | if (fflush(stdout) != 0) | |
1331 | error("ssh_keysign: fflush: %s", strerror(errno)); | |
1332 | if (pipe(to) < 0) { | |
1333 | error("ssh_keysign: pipe: %s", strerror(errno)); | |
1334 | return -1; | |
1335 | } | |
1336 | if (pipe(from) < 0) { | |
1337 | error("ssh_keysign: pipe: %s", strerror(errno)); | |
1338 | return -1; | |
1339 | } | |
1340 | if ((pid = fork()) < 0) { | |
1341 | error("ssh_keysign: fork: %s", strerror(errno)); | |
1342 | return -1; | |
1343 | } | |
1344 | if (pid == 0) { | |
1345 | seteuid(getuid()); | |
1346 | setuid(getuid()); | |
1347 | close(from[0]); | |
1348 | if (dup2(from[1], STDOUT_FILENO) < 0) | |
1349 | fatal("ssh_keysign: dup2: %s", strerror(errno)); | |
1350 | close(to[1]); | |
1351 | if (dup2(to[0], STDIN_FILENO) < 0) | |
1352 | fatal("ssh_keysign: dup2: %s", strerror(errno)); | |
1353 | close(from[1]); | |
1354 | close(to[0]); | |
1355 | execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0); | |
1356 | fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN, | |
1357 | strerror(errno)); | |
1358 | } | |
1359 | close(from[1]); | |
1360 | close(to[0]); | |
1361 | ||
1362 | buffer_init(&b); | |
1363 | buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */ | |
1364 | buffer_put_string(&b, data, datalen); | |
540d72c3 | 1365 | if (ssh_msg_send(to[1], version, &b) == -1) |
1366 | fatal("ssh_keysign: couldn't send request"); | |
44a053a3 | 1367 | |
d03f4262 | 1368 | if (ssh_msg_recv(from[0], &b) < 0) { |
44a053a3 | 1369 | error("ssh_keysign: no reply"); |
540d72c3 | 1370 | buffer_free(&b); |
44a053a3 | 1371 | return -1; |
1372 | } | |
1373 | close(from[0]); | |
1374 | close(to[1]); | |
1375 | ||
1376 | while (waitpid(pid, &status, 0) < 0) | |
1377 | if (errno != EINTR) | |
1378 | break; | |
1379 | ||
1380 | if (buffer_get_char(&b) != version) { | |
1381 | error("ssh_keysign: bad version"); | |
540d72c3 | 1382 | buffer_free(&b); |
44a053a3 | 1383 | return -1; |
1384 | } | |
1385 | *sigp = buffer_get_string(&b, lenp); | |
540d72c3 | 1386 | buffer_free(&b); |
44a053a3 | 1387 | |
1388 | return 0; | |
1389 | } | |
1390 | ||
3c0ef626 | 1391 | int |
1392 | userauth_hostbased(Authctxt *authctxt) | |
1393 | { | |
1394 | Key *private = NULL; | |
44a053a3 | 1395 | Sensitive *sensitive = authctxt->sensitive; |
3c0ef626 | 1396 | Buffer b; |
1397 | u_char *signature, *blob; | |
1398 | char *chost, *pkalg, *p; | |
1399 | const char *service; | |
1400 | u_int blen, slen; | |
1401 | int ok, i, len, found = 0; | |
1402 | ||
1403 | /* check for a useful key */ | |
44a053a3 | 1404 | for (i = 0; i < sensitive->nkeys; i++) { |
1405 | private = sensitive->keys[i]; | |
3c0ef626 | 1406 | if (private && private->type != KEY_RSA1) { |
1407 | found = 1; | |
1408 | /* we take and free the key */ | |
44a053a3 | 1409 | sensitive->keys[i] = NULL; |
3c0ef626 | 1410 | break; |
1411 | } | |
1412 | } | |
1413 | if (!found) { | |
bfe49944 | 1414 | debug("No more client hostkeys for hostbased authentication."); |
3c0ef626 | 1415 | return 0; |
1416 | } | |
1417 | if (key_to_blob(private, &blob, &blen) == 0) { | |
1418 | key_free(private); | |
1419 | return 0; | |
1420 | } | |
1421 | /* figure out a name for the client host */ | |
1422 | p = get_local_name(packet_get_connection_in()); | |
1423 | if (p == NULL) { | |
1424 | error("userauth_hostbased: cannot get local ipaddr/name"); | |
1425 | key_free(private); | |
1426 | return 0; | |
1427 | } | |
1428 | len = strlen(p) + 2; | |
1429 | chost = xmalloc(len); | |
1430 | strlcpy(chost, p, len); | |
1431 | strlcat(chost, ".", len); | |
1432 | debug2("userauth_hostbased: chost %s", chost); | |
bfe49944 | 1433 | xfree(p); |
3c0ef626 | 1434 | |
1435 | service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" : | |
1436 | authctxt->service; | |
1437 | pkalg = xstrdup(key_ssh_name(private)); | |
1438 | buffer_init(&b); | |
1439 | /* construct data */ | |
1440 | buffer_put_string(&b, session_id2, session_id2_len); | |
1441 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); | |
1442 | buffer_put_cstring(&b, authctxt->server_user); | |
1443 | buffer_put_cstring(&b, service); | |
1444 | buffer_put_cstring(&b, authctxt->method->name); | |
1445 | buffer_put_cstring(&b, pkalg); | |
1446 | buffer_put_string(&b, blob, blen); | |
1447 | buffer_put_cstring(&b, chost); | |
1448 | buffer_put_cstring(&b, authctxt->local_user); | |
1449 | #ifdef DEBUG_PK | |
1450 | buffer_dump(&b); | |
1451 | #endif | |
44a053a3 | 1452 | if (sensitive->external_keysign) |
1453 | ok = ssh_keysign(private, &signature, &slen, | |
1454 | buffer_ptr(&b), buffer_len(&b)); | |
1455 | else | |
1456 | ok = key_sign(private, &signature, &slen, | |
1457 | buffer_ptr(&b), buffer_len(&b)); | |
3c0ef626 | 1458 | key_free(private); |
1459 | buffer_free(&b); | |
1460 | if (ok != 0) { | |
1461 | error("key_sign failed"); | |
1462 | xfree(chost); | |
1463 | xfree(pkalg); | |
1464 | return 0; | |
1465 | } | |
1466 | packet_start(SSH2_MSG_USERAUTH_REQUEST); | |
1467 | packet_put_cstring(authctxt->server_user); | |
1468 | packet_put_cstring(authctxt->service); | |
1469 | packet_put_cstring(authctxt->method->name); | |
1470 | packet_put_cstring(pkalg); | |
1471 | packet_put_string(blob, blen); | |
1472 | packet_put_cstring(chost); | |
1473 | packet_put_cstring(authctxt->local_user); | |
1474 | packet_put_string(signature, slen); | |
1475 | memset(signature, 's', slen); | |
1476 | xfree(signature); | |
1477 | xfree(chost); | |
1478 | xfree(pkalg); | |
1479 | ||
1480 | packet_send(); | |
1481 | return 1; | |
1482 | } | |
1483 | ||
1484 | /* find auth method */ | |
1485 | ||
1486 | /* | |
1487 | * given auth method name, if configurable options permit this method fill | |
1488 | * in auth_ident field and return true, otherwise return false. | |
1489 | */ | |
1490 | static int | |
1491 | authmethod_is_enabled(Authmethod *method) | |
1492 | { | |
1493 | if (method == NULL) | |
1494 | return 0; | |
1495 | /* return false if options indicate this method is disabled */ | |
1496 | if (method->enabled == NULL || *method->enabled == 0) | |
1497 | return 0; | |
1498 | /* return false if batch mode is enabled but method needs interactive mode */ | |
1499 | if (method->batch_flag != NULL && *method->batch_flag != 0) | |
1500 | return 0; | |
1501 | return 1; | |
1502 | } | |
1503 | ||
1504 | static Authmethod * | |
1505 | authmethod_lookup(const char *name) | |
1506 | { | |
1507 | Authmethod *method = NULL; | |
1508 | if (name != NULL) | |
1509 | for (method = authmethods; method->name != NULL; method++) | |
1510 | if (strcmp(name, method->name) == 0) | |
1511 | return method; | |
1512 | debug2("Unrecognized authentication method name: %s", name ? name : "NULL"); | |
1513 | return NULL; | |
1514 | } | |
1515 | ||
1516 | /* XXX internal state */ | |
1517 | static Authmethod *current = NULL; | |
1518 | static char *supported = NULL; | |
1519 | static char *preferred = NULL; | |
276b07a3 | 1520 | |
3c0ef626 | 1521 | /* |
1522 | * Given the authentication method list sent by the server, return the | |
1523 | * next method we should try. If the server initially sends a nil list, | |
1524 | * use a built-in default list. | |
1525 | */ | |
1526 | static Authmethod * | |
1527 | authmethod_get(char *authlist) | |
1528 | { | |
3c0ef626 | 1529 | char *name = NULL; |
1e608e42 | 1530 | u_int next; |
3c0ef626 | 1531 | |
1532 | /* Use a suitable default if we're passed a nil list. */ | |
1533 | if (authlist == NULL || strlen(authlist) == 0) | |
1534 | authlist = options.preferred_authentications; | |
1535 | ||
1536 | if (supported == NULL || strcmp(authlist, supported) != 0) { | |
1537 | debug3("start over, passed a different list %s", authlist); | |
1538 | if (supported != NULL) | |
1539 | xfree(supported); | |
1540 | supported = xstrdup(authlist); | |
1541 | preferred = options.preferred_authentications; | |
1542 | debug3("preferred %s", preferred); | |
1543 | current = NULL; | |
1544 | } else if (current != NULL && authmethod_is_enabled(current)) | |
1545 | return current; | |
1546 | ||
1547 | for (;;) { | |
1548 | if ((name = match_list(preferred, supported, &next)) == NULL) { | |
bfe49944 | 1549 | debug("No more authentication methods to try."); |
3c0ef626 | 1550 | current = NULL; |
1551 | return NULL; | |
1552 | } | |
1553 | preferred += next; | |
1554 | debug3("authmethod_lookup %s", name); | |
1555 | debug3("remaining preferred: %s", preferred); | |
1556 | if ((current = authmethod_lookup(name)) != NULL && | |
1557 | authmethod_is_enabled(current)) { | |
1558 | debug3("authmethod_is_enabled %s", name); | |
bfe49944 | 1559 | debug("Next authentication method: %s", name); |
3c0ef626 | 1560 | return current; |
1561 | } | |
1562 | } | |
1563 | } | |
1564 | ||
3c0ef626 | 1565 | static char * |
1566 | authmethods_get(void) | |
1567 | { | |
1568 | Authmethod *method = NULL; | |
1e608e42 | 1569 | Buffer b; |
1570 | char *list; | |
3c0ef626 | 1571 | |
1e608e42 | 1572 | buffer_init(&b); |
3c0ef626 | 1573 | for (method = authmethods; method->name != NULL; method++) { |
1574 | if (authmethod_is_enabled(method)) { | |
1e608e42 | 1575 | if (buffer_len(&b) > 0) |
1576 | buffer_append(&b, ",", 1); | |
1577 | buffer_append(&b, method->name, strlen(method->name)); | |
3c0ef626 | 1578 | } |
1579 | } | |
1e608e42 | 1580 | buffer_append(&b, "\0", 1); |
1581 | list = xstrdup(buffer_ptr(&b)); | |
1582 | buffer_free(&b); | |
1583 | return list; | |
3c0ef626 | 1584 | } |