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