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