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