]>
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 | */ | |
bcbf86ec | 24 | |
a306f2dd | 25 | #include "includes.h" |
5f1f36b5 | 26 | RCSID("$OpenBSD: auth2.c,v 1.89 2002/03/19 14:27:39 markus Exp $"); |
94ec8c6b | 27 | |
a306f2dd | 28 | #include <openssl/evp.h> |
29 | ||
42f11eb2 | 30 | #include "ssh2.h" |
a306f2dd | 31 | #include "xmalloc.h" |
32 | #include "rsa.h" | |
1729c161 | 33 | #include "sshpty.h" |
a306f2dd | 34 | #include "packet.h" |
35 | #include "buffer.h" | |
42f11eb2 | 36 | #include "log.h" |
a306f2dd | 37 | #include "servconf.h" |
38 | #include "compat.h" | |
a5efa1bb | 39 | #include "channels.h" |
a306f2dd | 40 | #include "bufaux.h" |
a306f2dd | 41 | #include "auth.h" |
42 | #include "session.h" | |
43 | #include "dispatch.h" | |
a306f2dd | 44 | #include "key.h" |
b0e305c9 | 45 | #include "cipher.h" |
a306f2dd | 46 | #include "kex.h" |
42f11eb2 | 47 | #include "pathnames.h" |
a306f2dd | 48 | #include "uidswap.h" |
38c295d6 | 49 | #include "auth-options.h" |
8002af61 | 50 | #include "hostfile.h" |
51 | #include "canohost.h" | |
5ba55ada | 52 | #include "match.h" |
1853d1ef | 53 | #include "monitor_wrap.h" |
a306f2dd | 54 | |
55 | /* import */ | |
56 | extern ServerOptions options; | |
1e3b8b07 | 57 | extern u_char *session_id2; |
a306f2dd | 58 | extern int session_id2_len; |
59 | ||
1853d1ef | 60 | Authctxt *x_authctxt = NULL; |
94ec8c6b | 61 | static int one = 1; |
62 | ||
63 | typedef struct Authmethod Authmethod; | |
64 | struct Authmethod { | |
65 | char *name; | |
66 | int (*userauth)(Authctxt *authctxt); | |
67 | int *enabled; | |
68 | }; | |
69 | ||
a306f2dd | 70 | /* protocol */ |
71 | ||
7819b5c3 | 72 | static void input_service_request(int, u_int32_t, void *); |
73 | static void input_userauth_request(int, u_int32_t, void *); | |
a306f2dd | 74 | |
a306f2dd | 75 | /* helper */ |
396c147e | 76 | static Authmethod *authmethod_lookup(const char *); |
ffb215be | 77 | static char *authmethods_get(void); |
1853d1ef | 78 | int user_key_allowed(struct passwd *, Key *); |
79 | int hostbased_key_allowed(struct passwd *, const char *, char *, Key *); | |
a306f2dd | 80 | |
94ec8c6b | 81 | /* auth */ |
396c147e | 82 | static void userauth_banner(void); |
83 | static int userauth_none(Authctxt *); | |
84 | static int userauth_passwd(Authctxt *); | |
85 | static int userauth_pubkey(Authctxt *); | |
86 | static int userauth_hostbased(Authctxt *); | |
87 | static int userauth_kbdint(Authctxt *); | |
94ec8c6b | 88 | |
89 | Authmethod authmethods[] = { | |
90 | {"none", | |
91 | userauth_none, | |
92 | &one}, | |
93 | {"publickey", | |
94 | userauth_pubkey, | |
fa08c86b | 95 | &options.pubkey_authentication}, |
94ec8c6b | 96 | {"password", |
97 | userauth_passwd, | |
98 | &options.password_authentication}, | |
c845316f | 99 | {"keyboard-interactive", |
100 | userauth_kbdint, | |
101 | &options.kbd_interactive_authentication}, | |
8002af61 | 102 | {"hostbased", |
103 | userauth_hostbased, | |
104 | &options.hostbased_authentication}, | |
94ec8c6b | 105 | {NULL, NULL, NULL} |
a306f2dd | 106 | }; |
a306f2dd | 107 | |
108 | /* | |
94ec8c6b | 109 | * loop until authctxt->success == TRUE |
a306f2dd | 110 | */ |
111 | ||
1b34c1b3 | 112 | Authctxt * |
d5bb9418 | 113 | do_authentication2(void) |
a306f2dd | 114 | { |
59c97189 | 115 | Authctxt *authctxt = authctxt_new(); |
116 | ||
94ec8c6b | 117 | x_authctxt = authctxt; /*XXX*/ |
118 | ||
aa8003d6 | 119 | /* challenge-response is implemented via keyboard interactive */ |
5ba55ada | 120 | if (options.challenge_response_authentication) |
d464095c | 121 | options.kbd_interactive_authentication = 1; |
10f72868 | 122 | if (options.pam_authentication_via_kbd_int) |
123 | options.kbd_interactive_authentication = 1; | |
d464095c | 124 | |
6367063f | 125 | dispatch_init(&dispatch_protocol_error); |
a306f2dd | 126 | dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request); |
94ec8c6b | 127 | dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt); |
1b34c1b3 | 128 | |
129 | return (authctxt); | |
a306f2dd | 130 | } |
131 | ||
396c147e | 132 | static void |
7819b5c3 | 133 | input_service_request(int type, u_int32_t seq, void *ctxt) |
a306f2dd | 134 | { |
94ec8c6b | 135 | Authctxt *authctxt = ctxt; |
1e3b8b07 | 136 | u_int len; |
a306f2dd | 137 | int accept = 0; |
138 | char *service = packet_get_string(&len); | |
95500969 | 139 | packet_check_eom(); |
a306f2dd | 140 | |
94ec8c6b | 141 | if (authctxt == NULL) |
142 | fatal("input_service_request: no authctxt"); | |
143 | ||
a306f2dd | 144 | if (strcmp(service, "ssh-userauth") == 0) { |
94ec8c6b | 145 | if (!authctxt->success) { |
a306f2dd | 146 | accept = 1; |
147 | /* now we can handle user-auth requests */ | |
148 | dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request); | |
149 | } | |
150 | } | |
151 | /* XXX all other service requests are denied */ | |
152 | ||
153 | if (accept) { | |
154 | packet_start(SSH2_MSG_SERVICE_ACCEPT); | |
155 | packet_put_cstring(service); | |
156 | packet_send(); | |
157 | packet_write_wait(); | |
158 | } else { | |
159 | debug("bad service request %s", service); | |
160 | packet_disconnect("bad service request %s", service); | |
161 | } | |
162 | xfree(service); | |
163 | } | |
164 | ||
396c147e | 165 | static void |
7819b5c3 | 166 | input_userauth_request(int type, u_int32_t seq, void *ctxt) |
a306f2dd | 167 | { |
94ec8c6b | 168 | Authctxt *authctxt = ctxt; |
169 | Authmethod *m = NULL; | |
59c97189 | 170 | char *user, *service, *method, *style = NULL; |
a306f2dd | 171 | int authenticated = 0; |
a306f2dd | 172 | |
94ec8c6b | 173 | if (authctxt == NULL) |
174 | fatal("input_userauth_request: no authctxt"); | |
a306f2dd | 175 | |
94ec8c6b | 176 | user = packet_get_string(NULL); |
177 | service = packet_get_string(NULL); | |
178 | method = packet_get_string(NULL); | |
179 | debug("userauth-request for user %s service %s method %s", user, service, method); | |
8abcdba4 | 180 | debug("attempt %d failures %d", authctxt->attempt, authctxt->failures); |
94ec8c6b | 181 | |
59c97189 | 182 | if ((style = strchr(user, ':')) != NULL) |
183 | *style++ = 0; | |
184 | ||
2b87da3b | 185 | if (authctxt->attempt++ == 0) { |
63284fbb | 186 | /* setup auth context */ |
f7ed12f1 | 187 | authctxt->pw = PRIVSEP(getpwnamallow(user)); |
188 | if (authctxt->pw && strcmp(service, "ssh-connection")==0) { | |
94ec8c6b | 189 | authctxt->valid = 1; |
190 | debug2("input_userauth_request: setting up authctxt for %s", user); | |
191 | #ifdef USE_PAM | |
ad200abb | 192 | PRIVSEP(start_pam(authctxt->pw->pw_name)); |
94ec8c6b | 193 | #endif |
194 | } else { | |
195 | log("input_userauth_request: illegal user %s", user); | |
04fc7a67 | 196 | #ifdef USE_PAM |
ad200abb | 197 | PRIVSEP(start_pam("NOUSER")); |
04fc7a67 | 198 | #endif |
94ec8c6b | 199 | } |
5f1f36b5 | 200 | setproctitle("%s%s", authctxt->pw ? user : "unknown", |
1853d1ef | 201 | use_privsep ? " [net]" : ""); |
94ec8c6b | 202 | authctxt->user = xstrdup(user); |
203 | authctxt->service = xstrdup(service); | |
2a6a054e | 204 | authctxt->style = style ? xstrdup(style) : NULL; |
1853d1ef | 205 | if (use_privsep) |
206 | mm_inform_authserv(service, style); | |
2a6a054e | 207 | } else if (strcmp(user, authctxt->user) != 0 || |
208 | strcmp(service, authctxt->service) != 0) { | |
209 | packet_disconnect("Change of username or service not allowed: " | |
210 | "(%s,%s) -> (%s,%s)", | |
211 | authctxt->user, authctxt->service, user, service); | |
a306f2dd | 212 | } |
59c97189 | 213 | /* reset state */ |
2f293d43 | 214 | auth2_challenge_stop(authctxt); |
59c97189 | 215 | authctxt->postponed = 0; |
9cd45ea4 | 216 | |
59c97189 | 217 | /* try to authenticate user */ |
94ec8c6b | 218 | m = authmethod_lookup(method); |
219 | if (m != NULL) { | |
220 | debug2("input_userauth_request: try method %s", method); | |
221 | authenticated = m->userauth(authctxt); | |
9cd45ea4 | 222 | } |
d9cd3575 | 223 | userauth_finish(authctxt, authenticated, method); |
224 | ||
225 | xfree(service); | |
226 | xfree(user); | |
227 | xfree(method); | |
228 | } | |
229 | ||
230 | void | |
231 | userauth_finish(Authctxt *authctxt, int authenticated, char *method) | |
232 | { | |
4e81159c | 233 | char *methods; |
234 | ||
59c97189 | 235 | if (!authctxt->valid && authenticated) |
236 | fatal("INTERNAL ERROR: authenticated invalid user %s", | |
237 | authctxt->user); | |
9cd45ea4 | 238 | |
94ec8c6b | 239 | /* Special handling for root */ |
15853e93 | 240 | if (authenticated && authctxt->pw->pw_uid == 0 && |
241 | !auth_root_allowed(method)) | |
a306f2dd | 242 | authenticated = 0; |
a306f2dd | 243 | |
244 | #ifdef USE_PAM | |
ad200abb | 245 | if (!use_privsep && authenticated && authctxt->user && |
246 | !do_pam_account(authctxt->user, NULL)) | |
9cd45ea4 | 247 | authenticated = 0; |
a306f2dd | 248 | #endif /* USE_PAM */ |
249 | ||
94ec8c6b | 250 | /* Log before sending the reply */ |
59c97189 | 251 | auth_log(authctxt, authenticated, method, " ssh2"); |
252 | ||
4e81159c | 253 | if (authctxt->postponed) |
254 | return; | |
255 | ||
256 | /* XXX todo: check if multiple auth methods are needed */ | |
257 | if (authenticated == 1) { | |
258 | /* turn off userauth */ | |
6367063f | 259 | dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore); |
4e81159c | 260 | packet_start(SSH2_MSG_USERAUTH_SUCCESS); |
261 | packet_send(); | |
262 | packet_write_wait(); | |
263 | /* now we can break out */ | |
264 | authctxt->success = 1; | |
265 | } else { | |
19e810f6 | 266 | if (authctxt->failures++ > AUTH_FAIL_MAX) { |
267 | #ifdef WITH_AIXAUTHENTICATE | |
268 | loginfailed(authctxt->user, | |
983784a1 | 269 | get_canonical_hostname(options.verify_reverse_mapping), |
19e810f6 | 270 | "ssh"); |
271 | #endif /* WITH_AIXAUTHENTICATE */ | |
4e81159c | 272 | packet_disconnect(AUTH_FAIL_MSG, authctxt->user); |
19e810f6 | 273 | } |
4e81159c | 274 | methods = authmethods_get(); |
275 | packet_start(SSH2_MSG_USERAUTH_FAILURE); | |
276 | packet_put_cstring(methods); | |
277 | packet_put_char(0); /* XXX partial success, unused */ | |
278 | packet_send(); | |
279 | packet_write_wait(); | |
280 | xfree(methods); | |
281 | } | |
94ec8c6b | 282 | } |
283 | ||
396c147e | 284 | static void |
eea39c02 | 285 | userauth_banner(void) |
286 | { | |
287 | struct stat st; | |
288 | char *banner = NULL; | |
289 | off_t len, n; | |
290 | int fd; | |
291 | ||
292 | if (options.banner == NULL || (datafellows & SSH_BUG_BANNER)) | |
293 | return; | |
0715ec6c | 294 | if ((fd = open(options.banner, O_RDONLY)) < 0) |
eea39c02 | 295 | return; |
eea39c02 | 296 | if (fstat(fd, &st) < 0) |
297 | goto done; | |
298 | len = st.st_size; | |
299 | banner = xmalloc(len + 1); | |
300 | if ((n = read(fd, banner, len)) < 0) | |
301 | goto done; | |
302 | banner[n] = '\0'; | |
303 | packet_start(SSH2_MSG_USERAUTH_BANNER); | |
304 | packet_put_cstring(banner); | |
305 | packet_put_cstring(""); /* language, unused */ | |
306 | packet_send(); | |
307 | debug("userauth_banner: sent"); | |
308 | done: | |
309 | if (banner) | |
310 | xfree(banner); | |
311 | close(fd); | |
312 | return; | |
313 | } | |
94ec8c6b | 314 | |
396c147e | 315 | static int |
94ec8c6b | 316 | userauth_none(Authctxt *authctxt) |
a306f2dd | 317 | { |
94ec8c6b | 318 | /* disable method "none", only allowed one time */ |
319 | Authmethod *m = authmethod_lookup("none"); | |
320 | if (m != NULL) | |
321 | m->enabled = NULL; | |
95500969 | 322 | packet_check_eom(); |
2b87da3b | 323 | userauth_banner(); |
4d33e531 | 324 | |
94ec8c6b | 325 | if (authctxt->valid == 0) |
326 | return(0); | |
63284fbb | 327 | |
328 | #ifdef HAVE_CYGWIN | |
329 | if (check_nt_auth(1, authctxt->pw) == 0) | |
330 | return(0); | |
94ec8c6b | 331 | #endif |
1853d1ef | 332 | return PRIVSEP(auth_password(authctxt, "")); |
a306f2dd | 333 | } |
94ec8c6b | 334 | |
396c147e | 335 | static int |
94ec8c6b | 336 | userauth_passwd(Authctxt *authctxt) |
a306f2dd | 337 | { |
338 | char *password; | |
339 | int authenticated = 0; | |
340 | int change; | |
1e3b8b07 | 341 | u_int len; |
a306f2dd | 342 | change = packet_get_char(); |
343 | if (change) | |
344 | log("password change not supported"); | |
345 | password = packet_get_string(&len); | |
95500969 | 346 | packet_check_eom(); |
63284fbb | 347 | if (authctxt->valid && |
348 | #ifdef HAVE_CYGWIN | |
349 | check_nt_auth(1, authctxt->pw) && | |
94ec8c6b | 350 | #endif |
1853d1ef | 351 | PRIVSEP(auth_password(authctxt, password)) == 1) |
63284fbb | 352 | authenticated = 1; |
a306f2dd | 353 | memset(password, 0, len); |
354 | xfree(password); | |
355 | return authenticated; | |
356 | } | |
94ec8c6b | 357 | |
396c147e | 358 | static int |
94ec8c6b | 359 | userauth_kbdint(Authctxt *authctxt) |
360 | { | |
361 | int authenticated = 0; | |
5ba55ada | 362 | char *lang, *devs; |
94ec8c6b | 363 | |
364 | lang = packet_get_string(NULL); | |
365 | devs = packet_get_string(NULL); | |
95500969 | 366 | packet_check_eom(); |
94ec8c6b | 367 | |
5ba55ada | 368 | debug("keyboard-interactive devs %s", devs); |
59c97189 | 369 | |
5ba55ada | 370 | if (options.challenge_response_authentication) |
d464095c | 371 | authenticated = auth2_challenge(authctxt, devs); |
59c97189 | 372 | |
8c9fe09e | 373 | #ifdef USE_PAM |
10f72868 | 374 | if (authenticated == 0 && options.pam_authentication_via_kbd_int) |
8c9fe09e | 375 | authenticated = auth2_pam(authctxt); |
94ec8c6b | 376 | #endif |
94ec8c6b | 377 | xfree(devs); |
5ba55ada | 378 | xfree(lang); |
94ec8c6b | 379 | #ifdef HAVE_CYGWIN |
e9571a2c | 380 | if (check_nt_auth(0, authctxt->pw) == 0) |
94ec8c6b | 381 | return(0); |
382 | #endif | |
383 | return authenticated; | |
384 | } | |
385 | ||
396c147e | 386 | static int |
94ec8c6b | 387 | userauth_pubkey(Authctxt *authctxt) |
a306f2dd | 388 | { |
389 | Buffer b; | |
67fa09f5 | 390 | Key *key = NULL; |
c66f9d0e | 391 | char *pkalg; |
392 | u_char *pkblob, *sig; | |
1e3b8b07 | 393 | u_int alen, blen, slen; |
fa08c86b | 394 | int have_sig, pktype; |
a306f2dd | 395 | int authenticated = 0; |
396 | ||
94ec8c6b | 397 | if (!authctxt->valid) { |
398 | debug2("userauth_pubkey: disabled because of invalid user"); | |
a306f2dd | 399 | return 0; |
400 | } | |
401 | have_sig = packet_get_char(); | |
cbc5abf9 | 402 | if (datafellows & SSH_BUG_PKAUTH) { |
403 | debug2("userauth_pubkey: SSH_BUG_PKAUTH"); | |
404 | /* no explicit pkalg given */ | |
405 | pkblob = packet_get_string(&blen); | |
406 | buffer_init(&b); | |
407 | buffer_append(&b, pkblob, blen); | |
408 | /* so we have to extract the pkalg from the pkblob */ | |
409 | pkalg = buffer_get_string(&b, &alen); | |
410 | buffer_free(&b); | |
411 | } else { | |
412 | pkalg = packet_get_string(&alen); | |
413 | pkblob = packet_get_string(&blen); | |
414 | } | |
fa08c86b | 415 | pktype = key_type_from_name(pkalg); |
416 | if (pktype == KEY_UNSPEC) { | |
cbc5abf9 | 417 | /* this is perfectly legal */ |
67fa09f5 | 418 | log("userauth_pubkey: unsupported public key algorithm: %s", |
419 | pkalg); | |
420 | goto done; | |
a306f2dd | 421 | } |
fa08c86b | 422 | key = key_from_blob(pkblob, blen); |
67fa09f5 | 423 | if (key == NULL) { |
424 | error("userauth_pubkey: cannot decode key: %s", pkalg); | |
425 | goto done; | |
426 | } | |
427 | if (key->type != pktype) { | |
428 | error("userauth_pubkey: type mismatch for decoded key " | |
429 | "(received %d, expected %d)", key->type, pktype); | |
430 | goto done; | |
431 | } | |
432 | if (have_sig) { | |
433 | sig = packet_get_string(&slen); | |
434 | packet_check_eom(); | |
435 | buffer_init(&b); | |
436 | if (datafellows & SSH_OLD_SESSIONID) { | |
437 | buffer_append(&b, session_id2, session_id2_len); | |
438 | } else { | |
439 | buffer_put_string(&b, session_id2, session_id2_len); | |
440 | } | |
441 | /* reconstruct packet */ | |
442 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); | |
443 | buffer_put_cstring(&b, authctxt->user); | |
444 | buffer_put_cstring(&b, | |
445 | datafellows & SSH_BUG_PKSERVICE ? | |
446 | "ssh-userauth" : | |
447 | authctxt->service); | |
448 | if (datafellows & SSH_BUG_PKAUTH) { | |
449 | buffer_put_char(&b, have_sig); | |
450 | } else { | |
451 | buffer_put_cstring(&b, "publickey"); | |
452 | buffer_put_char(&b, have_sig); | |
453 | buffer_put_cstring(&b, pkalg); | |
454 | } | |
455 | buffer_put_string(&b, pkblob, blen); | |
fa08c86b | 456 | #ifdef DEBUG_PK |
67fa09f5 | 457 | buffer_dump(&b); |
a306f2dd | 458 | #endif |
67fa09f5 | 459 | /* test for correct signature */ |
1853d1ef | 460 | authenticated = 0; |
461 | if (PRIVSEP(user_key_allowed(authctxt->pw, key)) && | |
462 | PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b), | |
463 | buffer_len(&b))) == 1) | |
63284fbb | 464 | authenticated = 1; |
67fa09f5 | 465 | buffer_clear(&b); |
466 | xfree(sig); | |
467 | } else { | |
468 | debug("test whether pkalg/pkblob are acceptable"); | |
469 | packet_check_eom(); | |
470 | ||
471 | /* XXX fake reply and always send PK_OK ? */ | |
472 | /* | |
473 | * XXX this allows testing whether a user is allowed | |
474 | * to login: if you happen to have a valid pubkey this | |
475 | * message is sent. the message is NEVER sent at all | |
476 | * if a user is not allowed to login. is this an | |
477 | * issue? -markus | |
478 | */ | |
1853d1ef | 479 | if (PRIVSEP(user_key_allowed(authctxt->pw, key))) { |
67fa09f5 | 480 | packet_start(SSH2_MSG_USERAUTH_PK_OK); |
481 | packet_put_string(pkalg, alen); | |
482 | packet_put_string(pkblob, blen); | |
483 | packet_send(); | |
484 | packet_write_wait(); | |
485 | authctxt->postponed = 1; | |
a306f2dd | 486 | } |
a306f2dd | 487 | } |
67fa09f5 | 488 | if (authenticated != 1) |
489 | auth_clear_options(); | |
490 | done: | |
fa08c86b | 491 | debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg); |
67fa09f5 | 492 | if (key != NULL) |
493 | key_free(key); | |
a306f2dd | 494 | xfree(pkalg); |
495 | xfree(pkblob); | |
94ec8c6b | 496 | #ifdef HAVE_CYGWIN |
e9571a2c | 497 | if (check_nt_auth(0, authctxt->pw) == 0) |
94ec8c6b | 498 | return(0); |
499 | #endif | |
a306f2dd | 500 | return authenticated; |
501 | } | |
502 | ||
396c147e | 503 | static int |
8002af61 | 504 | userauth_hostbased(Authctxt *authctxt) |
505 | { | |
506 | Buffer b; | |
67fa09f5 | 507 | Key *key = NULL; |
c66f9d0e | 508 | char *pkalg, *cuser, *chost, *service; |
509 | u_char *pkblob, *sig; | |
8002af61 | 510 | u_int alen, blen, slen; |
511 | int pktype; | |
512 | int authenticated = 0; | |
513 | ||
514 | if (!authctxt->valid) { | |
515 | debug2("userauth_hostbased: disabled because of invalid user"); | |
516 | return 0; | |
517 | } | |
518 | pkalg = packet_get_string(&alen); | |
519 | pkblob = packet_get_string(&blen); | |
520 | chost = packet_get_string(NULL); | |
521 | cuser = packet_get_string(NULL); | |
522 | sig = packet_get_string(&slen); | |
523 | ||
524 | debug("userauth_hostbased: cuser %s chost %s pkalg %s slen %d", | |
525 | cuser, chost, pkalg, slen); | |
526 | #ifdef DEBUG_PK | |
527 | debug("signature:"); | |
528 | buffer_init(&b); | |
529 | buffer_append(&b, sig, slen); | |
530 | buffer_dump(&b); | |
531 | buffer_free(&b); | |
532 | #endif | |
533 | pktype = key_type_from_name(pkalg); | |
534 | if (pktype == KEY_UNSPEC) { | |
535 | /* this is perfectly legal */ | |
536 | log("userauth_hostbased: unsupported " | |
537 | "public key algorithm: %s", pkalg); | |
538 | goto done; | |
539 | } | |
540 | key = key_from_blob(pkblob, blen); | |
541 | if (key == NULL) { | |
67fa09f5 | 542 | error("userauth_hostbased: cannot decode key: %s", pkalg); |
543 | goto done; | |
544 | } | |
545 | if (key->type != pktype) { | |
546 | error("userauth_hostbased: type mismatch for decoded key " | |
547 | "(received %d, expected %d)", key->type, pktype); | |
8002af61 | 548 | goto done; |
549 | } | |
8dddf799 | 550 | service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" : |
551 | authctxt->service; | |
8002af61 | 552 | buffer_init(&b); |
8dddf799 | 553 | buffer_put_string(&b, session_id2, session_id2_len); |
8002af61 | 554 | /* reconstruct packet */ |
555 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); | |
556 | buffer_put_cstring(&b, authctxt->user); | |
8dddf799 | 557 | buffer_put_cstring(&b, service); |
8002af61 | 558 | buffer_put_cstring(&b, "hostbased"); |
559 | buffer_put_string(&b, pkalg, alen); | |
560 | buffer_put_string(&b, pkblob, blen); | |
561 | buffer_put_cstring(&b, chost); | |
562 | buffer_put_cstring(&b, cuser); | |
563 | #ifdef DEBUG_PK | |
564 | buffer_dump(&b); | |
565 | #endif | |
566 | /* test for allowed key and correct signature */ | |
1853d1ef | 567 | authenticated = 0; |
568 | if (PRIVSEP(hostbased_key_allowed(authctxt->pw, cuser, chost, key)) && | |
569 | PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b), | |
570 | buffer_len(&b))) == 1) | |
63284fbb | 571 | authenticated = 1; |
8002af61 | 572 | |
573 | buffer_clear(&b); | |
8002af61 | 574 | done: |
575 | debug2("userauth_hostbased: authenticated %d", authenticated); | |
67fa09f5 | 576 | if (key != NULL) |
577 | key_free(key); | |
8002af61 | 578 | xfree(pkalg); |
579 | xfree(pkblob); | |
580 | xfree(cuser); | |
581 | xfree(chost); | |
582 | xfree(sig); | |
583 | return authenticated; | |
584 | } | |
585 | ||
94ec8c6b | 586 | /* get current user */ |
a306f2dd | 587 | |
588 | struct passwd* | |
589 | auth_get_user(void) | |
590 | { | |
94ec8c6b | 591 | return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL; |
a306f2dd | 592 | } |
593 | ||
94ec8c6b | 594 | #define DELIM "," |
595 | ||
ffb215be | 596 | static char * |
94ec8c6b | 597 | authmethods_get(void) |
a306f2dd | 598 | { |
94ec8c6b | 599 | Authmethod *method = NULL; |
3469eac4 | 600 | Buffer b; |
94ec8c6b | 601 | char *list; |
602 | ||
3469eac4 | 603 | buffer_init(&b); |
94ec8c6b | 604 | for (method = authmethods; method->name != NULL; method++) { |
605 | if (strcmp(method->name, "none") == 0) | |
606 | continue; | |
607 | if (method->enabled != NULL && *(method->enabled) != 0) { | |
3469eac4 | 608 | if (buffer_len(&b) > 0) |
609 | buffer_append(&b, ",", 1); | |
610 | buffer_append(&b, method->name, strlen(method->name)); | |
a306f2dd | 611 | } |
612 | } | |
3469eac4 | 613 | buffer_append(&b, "\0", 1); |
614 | list = xstrdup(buffer_ptr(&b)); | |
615 | buffer_free(&b); | |
94ec8c6b | 616 | return list; |
617 | } | |
618 | ||
396c147e | 619 | static Authmethod * |
94ec8c6b | 620 | authmethod_lookup(const char *name) |
621 | { | |
622 | Authmethod *method = NULL; | |
623 | if (name != NULL) | |
624 | for (method = authmethods; method->name != NULL; method++) | |
625 | if (method->enabled != NULL && | |
626 | *(method->enabled) != 0 && | |
627 | strcmp(name, method->name) == 0) | |
628 | return method; | |
629 | debug2("Unrecognized authentication method name: %s", name ? name : "NULL"); | |
630 | return NULL; | |
a306f2dd | 631 | } |
632 | ||
633 | /* return 1 if user allows given key */ | |
396c147e | 634 | static int |
96a7b0cc | 635 | user_key_allowed2(struct passwd *pw, Key *key, char *file) |
a306f2dd | 636 | { |
96a7b0cc | 637 | char line[8192]; |
a306f2dd | 638 | int found_key = 0; |
a306f2dd | 639 | FILE *f; |
1e3b8b07 | 640 | u_long linenum = 0; |
a306f2dd | 641 | struct stat st; |
642 | Key *found; | |
306feb91 | 643 | char *fp; |
a306f2dd | 644 | |
94ec8c6b | 645 | if (pw == NULL) |
646 | return 0; | |
647 | ||
a306f2dd | 648 | /* Temporarily use the user's uid. */ |
63bd8c36 | 649 | temporarily_use_uid(pw); |
a306f2dd | 650 | |
c8445989 | 651 | debug("trying public key file %s", file); |
a306f2dd | 652 | |
653 | /* Fail quietly if file does not exist */ | |
654 | if (stat(file, &st) < 0) { | |
655 | /* Restore the privileged uid. */ | |
656 | restore_uid(); | |
657 | return 0; | |
658 | } | |
659 | /* Open the file containing the authorized keys. */ | |
660 | f = fopen(file, "r"); | |
661 | if (!f) { | |
662 | /* Restore the privileged uid. */ | |
663 | restore_uid(); | |
664 | return 0; | |
665 | } | |
c8445989 | 666 | if (options.strict_modes && |
0c9664c2 | 667 | secure_filename(f, file, pw, line, sizeof(line)) != 0) { |
c8445989 | 668 | fclose(f); |
669 | log("Authentication refused: %s", line); | |
670 | restore_uid(); | |
671 | return 0; | |
a306f2dd | 672 | } |
c8445989 | 673 | |
a306f2dd | 674 | found_key = 0; |
de273eef | 675 | found = key_new(key->type); |
a306f2dd | 676 | |
677 | while (fgets(line, sizeof(line), f)) { | |
38c295d6 | 678 | char *cp, *options = NULL; |
a306f2dd | 679 | linenum++; |
680 | /* Skip leading whitespace, empty and comment lines. */ | |
681 | for (cp = line; *cp == ' ' || *cp == '\t'; cp++) | |
682 | ; | |
683 | if (!*cp || *cp == '\n' || *cp == '#') | |
684 | continue; | |
38c295d6 | 685 | |
ddcfed57 | 686 | if (key_read(found, &cp) != 1) { |
38c295d6 | 687 | /* no key? check if there are options for this key */ |
688 | int quoted = 0; | |
fa08c86b | 689 | debug2("user_key_allowed: check options: '%s'", cp); |
38c295d6 | 690 | options = cp; |
691 | for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) { | |
692 | if (*cp == '\\' && cp[1] == '"') | |
693 | cp++; /* Skip both */ | |
694 | else if (*cp == '"') | |
695 | quoted = !quoted; | |
696 | } | |
697 | /* Skip remaining whitespace. */ | |
698 | for (; *cp == ' ' || *cp == '\t'; cp++) | |
699 | ; | |
ddcfed57 | 700 | if (key_read(found, &cp) != 1) { |
fa08c86b | 701 | debug2("user_key_allowed: advance: '%s'", cp); |
38c295d6 | 702 | /* still no key? advance to next line*/ |
703 | continue; | |
704 | } | |
705 | } | |
706 | if (key_equal(found, key) && | |
42f11eb2 | 707 | auth_parse_options(pw, options, file, linenum) == 1) { |
a306f2dd | 708 | found_key = 1; |
1bdee08c | 709 | debug("matching key found: file %s, line %lu", |
a306f2dd | 710 | file, linenum); |
306feb91 | 711 | fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX); |
712 | verbose("Found matching %s key: %s", | |
184eed6a | 713 | key_type(found), fp); |
306feb91 | 714 | xfree(fp); |
a306f2dd | 715 | break; |
716 | } | |
717 | } | |
718 | restore_uid(); | |
719 | fclose(f); | |
720 | key_free(found); | |
539af7f5 | 721 | if (!found_key) |
722 | debug2("key not found"); | |
a306f2dd | 723 | return found_key; |
724 | } | |
8002af61 | 725 | |
96a7b0cc | 726 | /* check whether given key is in .ssh/authorized_keys* */ |
1853d1ef | 727 | int |
96a7b0cc | 728 | user_key_allowed(struct passwd *pw, Key *key) |
729 | { | |
730 | int success; | |
731 | char *file; | |
732 | ||
733 | file = authorized_keys_file(pw); | |
734 | success = user_key_allowed2(pw, key, file); | |
735 | xfree(file); | |
736 | if (success) | |
737 | return success; | |
738 | ||
739 | /* try suffix "2" for backward compat, too */ | |
740 | file = authorized_keys_file2(pw); | |
741 | success = user_key_allowed2(pw, key, file); | |
742 | xfree(file); | |
743 | return success; | |
744 | } | |
745 | ||
8002af61 | 746 | /* return 1 if given hostkey is allowed */ |
1853d1ef | 747 | int |
57a5edd8 | 748 | hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost, |
8002af61 | 749 | Key *key) |
750 | { | |
8002af61 | 751 | const char *resolvedname, *ipaddr, *lookup; |
17a3011c | 752 | HostStatus host_status; |
753 | int len; | |
8002af61 | 754 | |
bf4c5edc | 755 | resolvedname = get_canonical_hostname(options.verify_reverse_mapping); |
8002af61 | 756 | ipaddr = get_remote_ipaddr(); |
757 | ||
f98c3421 | 758 | debug2("userauth_hostbased: chost %s resolvedname %s ipaddr %s", |
759 | chost, resolvedname, ipaddr); | |
8002af61 | 760 | |
761 | if (options.hostbased_uses_name_from_packet_only) { | |
762 | if (auth_rhosts2(pw, cuser, chost, chost) == 0) | |
763 | return 0; | |
764 | lookup = chost; | |
765 | } else { | |
f98c3421 | 766 | if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') { |
767 | debug2("stripping trailing dot from chost %s", chost); | |
768 | chost[len - 1] = '\0'; | |
769 | } | |
8002af61 | 770 | if (strcasecmp(resolvedname, chost) != 0) |
771 | log("userauth_hostbased mismatch: " | |
772 | "client sends %s, but we resolve %s to %s", | |
773 | chost, ipaddr, resolvedname); | |
774 | if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0) | |
775 | return 0; | |
776 | lookup = resolvedname; | |
777 | } | |
778 | debug2("userauth_hostbased: access allowed by auth_rhosts2"); | |
779 | ||
d0c8ca5c | 780 | host_status = check_key_in_hostfiles(pw, key, lookup, |
781 | _PATH_SSH_SYSTEM_HOSTFILE, | |
73473230 | 782 | options.ignore_user_known_hosts ? NULL : _PATH_SSH_USER_HOSTFILE); |
d0c8ca5c | 783 | |
784 | /* backward compat if no key has been found. */ | |
785 | if (host_status == HOST_NEW) | |
786 | host_status = check_key_in_hostfiles(pw, key, lookup, | |
787 | _PATH_SSH_SYSTEM_HOSTFILE2, | |
73473230 | 788 | options.ignore_user_known_hosts ? NULL : |
789 | _PATH_SSH_USER_HOSTFILE2); | |
8002af61 | 790 | |
8002af61 | 791 | return (host_status == HOST_OK); |
792 | } |