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