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