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