]>
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" |
59c97189 | 26 | RCSID("$OpenBSD: auth2.c,v 1.28 2001/01/18 17:00:00 markus Exp $"); |
94ec8c6b | 27 | |
28 | #ifdef HAVE_OSF_SIA | |
29 | # include <sia.h> | |
30 | # include <siad.h> | |
31 | #endif | |
a306f2dd | 32 | |
33 | #include <openssl/dsa.h> | |
34 | #include <openssl/rsa.h> | |
35 | #include <openssl/evp.h> | |
36 | ||
37 | #include "xmalloc.h" | |
38 | #include "rsa.h" | |
39 | #include "ssh.h" | |
40 | #include "pty.h" | |
41 | #include "packet.h" | |
42 | #include "buffer.h" | |
a306f2dd | 43 | #include "servconf.h" |
44 | #include "compat.h" | |
45 | #include "channels.h" | |
46 | #include "bufaux.h" | |
47 | #include "ssh2.h" | |
48 | #include "auth.h" | |
49 | #include "session.h" | |
50 | #include "dispatch.h" | |
51 | #include "auth.h" | |
52 | #include "key.h" | |
53 | #include "kex.h" | |
54 | ||
a306f2dd | 55 | #include "uidswap.h" |
38c295d6 | 56 | #include "auth-options.h" |
a306f2dd | 57 | |
58 | /* import */ | |
59 | extern ServerOptions options; | |
1e3b8b07 | 60 | extern u_char *session_id2; |
a306f2dd | 61 | extern int session_id2_len; |
62 | ||
94ec8c6b | 63 | #ifdef WITH_AIXAUTHENTICATE |
64 | extern char *aixloginmsg; | |
65 | #endif | |
66 | #ifdef HAVE_OSF_SIA | |
67 | extern int saved_argc; | |
68 | extern char **saved_argv; | |
69 | #endif | |
70 | ||
71 | static Authctxt *x_authctxt = NULL; | |
72 | static int one = 1; | |
73 | ||
74 | typedef struct Authmethod Authmethod; | |
75 | struct Authmethod { | |
76 | char *name; | |
77 | int (*userauth)(Authctxt *authctxt); | |
78 | int *enabled; | |
79 | }; | |
80 | ||
a306f2dd | 81 | /* protocol */ |
82 | ||
188adeb2 | 83 | void input_service_request(int type, int plen, void *ctxt); |
84 | void input_userauth_request(int type, int plen, void *ctxt); | |
85 | void protocol_error(int type, int plen, void *ctxt); | |
a306f2dd | 86 | |
a306f2dd | 87 | /* helper */ |
94ec8c6b | 88 | Authmethod *authmethod_lookup(const char *name); |
89 | struct passwd *pwcopy(struct passwd *pw); | |
fa08c86b | 90 | int user_key_allowed(struct passwd *pw, Key *key); |
94ec8c6b | 91 | char *authmethods_get(void); |
a306f2dd | 92 | |
94ec8c6b | 93 | /* auth */ |
eea39c02 | 94 | void userauth_banner(void); |
94ec8c6b | 95 | int userauth_none(Authctxt *authctxt); |
96 | int userauth_passwd(Authctxt *authctxt); | |
97 | int userauth_pubkey(Authctxt *authctxt); | |
98 | int userauth_kbdint(Authctxt *authctxt); | |
99 | ||
100 | Authmethod authmethods[] = { | |
101 | {"none", | |
102 | userauth_none, | |
103 | &one}, | |
104 | {"publickey", | |
105 | userauth_pubkey, | |
fa08c86b | 106 | &options.pubkey_authentication}, |
94ec8c6b | 107 | {"keyboard-interactive", |
108 | userauth_kbdint, | |
109 | &options.kbd_interactive_authentication}, | |
110 | {"password", | |
111 | userauth_passwd, | |
112 | &options.password_authentication}, | |
113 | {NULL, NULL, NULL} | |
a306f2dd | 114 | }; |
a306f2dd | 115 | |
116 | /* | |
94ec8c6b | 117 | * loop until authctxt->success == TRUE |
a306f2dd | 118 | */ |
119 | ||
120 | void | |
121 | do_authentication2() | |
122 | { | |
59c97189 | 123 | Authctxt *authctxt = authctxt_new(); |
124 | ||
94ec8c6b | 125 | x_authctxt = authctxt; /*XXX*/ |
126 | ||
59c97189 | 127 | #ifdef AFS |
128 | /* If machine has AFS, set process authentication group. */ | |
129 | if (k_hasafs()) { | |
130 | k_setpag(); | |
131 | k_unlog(); | |
132 | } | |
8cb940db | 133 | #endif |
a306f2dd | 134 | dispatch_init(&protocol_error); |
135 | dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request); | |
94ec8c6b | 136 | dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt); |
59c97189 | 137 | do_authenticated2(authctxt); |
a306f2dd | 138 | } |
139 | ||
140 | void | |
188adeb2 | 141 | protocol_error(int type, int plen, void *ctxt) |
a306f2dd | 142 | { |
143 | log("auth: protocol error: type %d plen %d", type, plen); | |
144 | packet_start(SSH2_MSG_UNIMPLEMENTED); | |
145 | packet_put_int(0); | |
146 | packet_send(); | |
147 | packet_write_wait(); | |
148 | } | |
149 | ||
150 | void | |
188adeb2 | 151 | input_service_request(int type, int plen, void *ctxt) |
a306f2dd | 152 | { |
94ec8c6b | 153 | Authctxt *authctxt = ctxt; |
1e3b8b07 | 154 | u_int len; |
a306f2dd | 155 | int accept = 0; |
156 | char *service = packet_get_string(&len); | |
157 | packet_done(); | |
158 | ||
94ec8c6b | 159 | if (authctxt == NULL) |
160 | fatal("input_service_request: no authctxt"); | |
161 | ||
a306f2dd | 162 | if (strcmp(service, "ssh-userauth") == 0) { |
94ec8c6b | 163 | if (!authctxt->success) { |
a306f2dd | 164 | accept = 1; |
165 | /* now we can handle user-auth requests */ | |
166 | dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request); | |
167 | } | |
168 | } | |
169 | /* XXX all other service requests are denied */ | |
170 | ||
171 | if (accept) { | |
172 | packet_start(SSH2_MSG_SERVICE_ACCEPT); | |
173 | packet_put_cstring(service); | |
174 | packet_send(); | |
175 | packet_write_wait(); | |
176 | } else { | |
177 | debug("bad service request %s", service); | |
178 | packet_disconnect("bad service request %s", service); | |
179 | } | |
180 | xfree(service); | |
181 | } | |
182 | ||
183 | void | |
188adeb2 | 184 | input_userauth_request(int type, int plen, void *ctxt) |
a306f2dd | 185 | { |
94ec8c6b | 186 | Authctxt *authctxt = ctxt; |
187 | Authmethod *m = NULL; | |
59c97189 | 188 | char *user, *service, *method, *style = NULL; |
a306f2dd | 189 | int authenticated = 0; |
a306f2dd | 190 | |
94ec8c6b | 191 | if (authctxt == NULL) |
192 | fatal("input_userauth_request: no authctxt"); | |
a306f2dd | 193 | |
94ec8c6b | 194 | user = packet_get_string(NULL); |
195 | service = packet_get_string(NULL); | |
196 | method = packet_get_string(NULL); | |
197 | debug("userauth-request for user %s service %s method %s", user, service, method); | |
8abcdba4 | 198 | debug("attempt %d failures %d", authctxt->attempt, authctxt->failures); |
94ec8c6b | 199 | |
59c97189 | 200 | if ((style = strchr(user, ':')) != NULL) |
201 | *style++ = 0; | |
202 | ||
8abcdba4 | 203 | if (authctxt->attempt++ == 0) { |
94ec8c6b | 204 | /* setup auth context */ |
205 | struct passwd *pw = NULL; | |
206 | setproctitle("%s", user); | |
207 | pw = getpwnam(user); | |
208 | if (pw && allowed_user(pw) && strcmp(service, "ssh-connection")==0) { | |
209 | authctxt->pw = pwcopy(pw); | |
210 | authctxt->valid = 1; | |
211 | debug2("input_userauth_request: setting up authctxt for %s", user); | |
212 | #ifdef USE_PAM | |
04fc7a67 | 213 | start_pam(pw->pw_name); |
94ec8c6b | 214 | #endif |
215 | } else { | |
216 | log("input_userauth_request: illegal user %s", user); | |
04fc7a67 | 217 | #ifdef USE_PAM |
218 | start_pam("NOUSER"); | |
219 | #endif | |
94ec8c6b | 220 | } |
221 | authctxt->user = xstrdup(user); | |
222 | authctxt->service = xstrdup(service); | |
59c97189 | 223 | authctxt->style = style ? xstrdup(style) : NULL; /* currently unused */ |
94ec8c6b | 224 | } else if (authctxt->valid) { |
225 | if (strcmp(user, authctxt->user) != 0 || | |
226 | strcmp(service, authctxt->service) != 0) { | |
227 | log("input_userauth_request: missmatch: (%s,%s)!=(%s,%s)", | |
228 | user, service, authctxt->user, authctxt->service); | |
229 | authctxt->valid = 0; | |
a306f2dd | 230 | } |
231 | } | |
59c97189 | 232 | /* reset state */ |
233 | dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, &protocol_error); | |
234 | authctxt->postponed = 0; | |
9cd45ea4 | 235 | |
59c97189 | 236 | /* try to authenticate user */ |
94ec8c6b | 237 | m = authmethod_lookup(method); |
238 | if (m != NULL) { | |
239 | debug2("input_userauth_request: try method %s", method); | |
240 | authenticated = m->userauth(authctxt); | |
9cd45ea4 | 241 | } |
59c97189 | 242 | if (!authctxt->valid && authenticated) |
243 | fatal("INTERNAL ERROR: authenticated invalid user %s", | |
244 | authctxt->user); | |
9cd45ea4 | 245 | |
94ec8c6b | 246 | /* Special handling for root */ |
59c97189 | 247 | if (authenticated && authctxt->pw->pw_uid == 0 && !auth_root_allowed()) |
a306f2dd | 248 | authenticated = 0; |
a306f2dd | 249 | |
250 | #ifdef USE_PAM | |
606ea390 | 251 | if (authenticated && authctxt->user && !do_pam_account(authctxt->user, NULL)) |
9cd45ea4 | 252 | authenticated = 0; |
a306f2dd | 253 | #endif /* USE_PAM */ |
254 | ||
94ec8c6b | 255 | /* Log before sending the reply */ |
59c97189 | 256 | auth_log(authctxt, authenticated, method, " ssh2"); |
257 | ||
258 | if (!authctxt->postponed) | |
259 | userauth_reply(authctxt, authenticated); | |
94ec8c6b | 260 | |
261 | xfree(service); | |
262 | xfree(user); | |
263 | xfree(method); | |
264 | } | |
265 | ||
eea39c02 | 266 | void |
267 | userauth_banner(void) | |
268 | { | |
269 | struct stat st; | |
270 | char *banner = NULL; | |
271 | off_t len, n; | |
272 | int fd; | |
273 | ||
274 | if (options.banner == NULL || (datafellows & SSH_BUG_BANNER)) | |
275 | return; | |
276 | if ((fd = open(options.banner, O_RDONLY)) < 0) { | |
277 | error("userauth_banner: open %s failed: %s", | |
278 | options.banner, strerror(errno)); | |
279 | return; | |
280 | } | |
281 | if (fstat(fd, &st) < 0) | |
282 | goto done; | |
283 | len = st.st_size; | |
284 | banner = xmalloc(len + 1); | |
285 | if ((n = read(fd, banner, len)) < 0) | |
286 | goto done; | |
287 | banner[n] = '\0'; | |
288 | packet_start(SSH2_MSG_USERAUTH_BANNER); | |
289 | packet_put_cstring(banner); | |
290 | packet_put_cstring(""); /* language, unused */ | |
291 | packet_send(); | |
292 | debug("userauth_banner: sent"); | |
293 | done: | |
294 | if (banner) | |
295 | xfree(banner); | |
296 | close(fd); | |
297 | return; | |
298 | } | |
94ec8c6b | 299 | |
94ec8c6b | 300 | void |
301 | userauth_reply(Authctxt *authctxt, int authenticated) | |
302 | { | |
8abcdba4 | 303 | char *methods; |
59c97189 | 304 | |
1d1ffb87 | 305 | /* XXX todo: check if multiple auth methods are needed */ |
59c97189 | 306 | if (authenticated) { |
c1ef8333 | 307 | #ifdef WITH_AIXAUTHENTICATE |
308 | /* We don't have a pty yet, so just label the line as "ssh" */ | |
606ea390 | 309 | if (loginsuccess(authctxt->user?authctxt->user:"NOUSER", |
310 | get_canonical_hostname(), "ssh", &aixloginmsg) < 0) | |
c1ef8333 | 311 | aixloginmsg = NULL; |
312 | #endif /* WITH_AIXAUTHENTICATE */ | |
a306f2dd | 313 | /* turn off userauth */ |
314 | dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &protocol_error); | |
315 | packet_start(SSH2_MSG_USERAUTH_SUCCESS); | |
316 | packet_send(); | |
317 | packet_write_wait(); | |
318 | /* now we can break out */ | |
94ec8c6b | 319 | authctxt->success = 1; |
59c97189 | 320 | } else { |
321 | if (authctxt->failures++ > AUTH_FAIL_MAX) | |
322 | packet_disconnect(AUTH_FAIL_MSG, authctxt->user); | |
8abcdba4 | 323 | methods = authmethods_get(); |
a306f2dd | 324 | packet_start(SSH2_MSG_USERAUTH_FAILURE); |
94ec8c6b | 325 | packet_put_cstring(methods); |
326 | packet_put_char(0); /* XXX partial success, unused */ | |
a306f2dd | 327 | packet_send(); |
328 | packet_write_wait(); | |
94ec8c6b | 329 | xfree(methods); |
a306f2dd | 330 | } |
a306f2dd | 331 | } |
332 | ||
333 | int | |
94ec8c6b | 334 | userauth_none(Authctxt *authctxt) |
a306f2dd | 335 | { |
94ec8c6b | 336 | /* disable method "none", only allowed one time */ |
337 | Authmethod *m = authmethod_lookup("none"); | |
338 | if (m != NULL) | |
339 | m->enabled = NULL; | |
a306f2dd | 340 | packet_done(); |
eea39c02 | 341 | userauth_banner(); |
4d33e531 | 342 | |
94ec8c6b | 343 | if (authctxt->valid == 0) |
344 | return(0); | |
345 | ||
346 | #ifdef HAVE_CYGWIN | |
347 | if (check_nt_auth(1, authctxt->pw->pw_uid) == 0) | |
348 | return(0); | |
349 | #endif | |
a306f2dd | 350 | #ifdef USE_PAM |
94ec8c6b | 351 | return auth_pam_password(authctxt->pw, ""); |
4d33e531 | 352 | #elif defined(HAVE_OSF_SIA) |
94ec8c6b | 353 | return (sia_validate_user(NULL, saved_argc, saved_argv, |
606ea390 | 354 | get_canonical_hostname(), authctxt->user?authctxt->user:"NOUSER", |
355 | NULL, 0, NULL, "") == SIASUCCESS); | |
4d33e531 | 356 | #else /* !HAVE_OSF_SIA && !USE_PAM */ |
94ec8c6b | 357 | return auth_password(authctxt->pw, ""); |
a306f2dd | 358 | #endif /* USE_PAM */ |
359 | } | |
94ec8c6b | 360 | |
a306f2dd | 361 | int |
94ec8c6b | 362 | userauth_passwd(Authctxt *authctxt) |
a306f2dd | 363 | { |
364 | char *password; | |
365 | int authenticated = 0; | |
366 | int change; | |
1e3b8b07 | 367 | u_int len; |
a306f2dd | 368 | change = packet_get_char(); |
369 | if (change) | |
370 | log("password change not supported"); | |
371 | password = packet_get_string(&len); | |
372 | packet_done(); | |
94ec8c6b | 373 | if (authctxt->valid && |
374 | #ifdef HAVE_CYGWIN | |
375 | check_nt_auth(1, authctxt->pw->pw_uid) && | |
376 | #endif | |
a306f2dd | 377 | #ifdef USE_PAM |
94ec8c6b | 378 | auth_pam_password(authctxt->pw, password) == 1) |
4d33e531 | 379 | #elif defined(HAVE_OSF_SIA) |
380 | sia_validate_user(NULL, saved_argc, saved_argv, | |
606ea390 | 381 | get_canonical_hostname(), authctxt->user?authctxt->user:"NOUSER", |
382 | NULL, 0, NULL, password) == SIASUCCESS) | |
4d33e531 | 383 | #else /* !USE_PAM && !HAVE_OSF_SIA */ |
94ec8c6b | 384 | auth_password(authctxt->pw, password) == 1) |
a306f2dd | 385 | #endif /* USE_PAM */ |
386 | authenticated = 1; | |
387 | memset(password, 0, len); | |
388 | xfree(password); | |
389 | return authenticated; | |
390 | } | |
94ec8c6b | 391 | |
392 | int | |
393 | userauth_kbdint(Authctxt *authctxt) | |
394 | { | |
395 | int authenticated = 0; | |
396 | char *lang = NULL; | |
397 | char *devs = NULL; | |
398 | ||
399 | lang = packet_get_string(NULL); | |
400 | devs = packet_get_string(NULL); | |
401 | packet_done(); | |
402 | ||
403 | debug("keyboard-interactive language %s devs %s", lang, devs); | |
59c97189 | 404 | |
405 | authenticated = auth2_challenge(authctxt, devs); | |
406 | ||
8c9fe09e | 407 | #ifdef USE_PAM |
408 | if (authenticated == 0) | |
409 | authenticated = auth2_pam(authctxt); | |
94ec8c6b | 410 | #endif |
411 | xfree(lang); | |
412 | xfree(devs); | |
413 | #ifdef HAVE_CYGWIN | |
414 | if (check_nt_auth(0, authctxt->pw->pw_uid) == 0) | |
415 | return(0); | |
416 | #endif | |
417 | return authenticated; | |
418 | } | |
419 | ||
a306f2dd | 420 | int |
94ec8c6b | 421 | userauth_pubkey(Authctxt *authctxt) |
a306f2dd | 422 | { |
423 | Buffer b; | |
424 | Key *key; | |
425 | char *pkalg, *pkblob, *sig; | |
1e3b8b07 | 426 | u_int alen, blen, slen; |
fa08c86b | 427 | int have_sig, pktype; |
a306f2dd | 428 | int authenticated = 0; |
429 | ||
94ec8c6b | 430 | if (!authctxt->valid) { |
431 | debug2("userauth_pubkey: disabled because of invalid user"); | |
a306f2dd | 432 | return 0; |
433 | } | |
434 | have_sig = packet_get_char(); | |
cbc5abf9 | 435 | if (datafellows & SSH_BUG_PKAUTH) { |
436 | debug2("userauth_pubkey: SSH_BUG_PKAUTH"); | |
437 | /* no explicit pkalg given */ | |
438 | pkblob = packet_get_string(&blen); | |
439 | buffer_init(&b); | |
440 | buffer_append(&b, pkblob, blen); | |
441 | /* so we have to extract the pkalg from the pkblob */ | |
442 | pkalg = buffer_get_string(&b, &alen); | |
443 | buffer_free(&b); | |
444 | } else { | |
445 | pkalg = packet_get_string(&alen); | |
446 | pkblob = packet_get_string(&blen); | |
447 | } | |
fa08c86b | 448 | pktype = key_type_from_name(pkalg); |
449 | if (pktype == KEY_UNSPEC) { | |
cbc5abf9 | 450 | /* this is perfectly legal */ |
451 | log("userauth_pubkey: unsupported public key algorithm: %s", pkalg); | |
94ec8c6b | 452 | xfree(pkalg); |
cbc5abf9 | 453 | xfree(pkblob); |
a306f2dd | 454 | return 0; |
455 | } | |
fa08c86b | 456 | key = key_from_blob(pkblob, blen); |
a306f2dd | 457 | if (key != NULL) { |
458 | if (have_sig) { | |
459 | sig = packet_get_string(&slen); | |
460 | packet_done(); | |
461 | buffer_init(&b); | |
33de75a3 | 462 | if (datafellows & SSH_OLD_SESSIONID) { |
74fc9186 | 463 | buffer_append(&b, session_id2, session_id2_len); |
33de75a3 | 464 | } else { |
465 | buffer_put_string(&b, session_id2, session_id2_len); | |
74fc9186 | 466 | } |
38c295d6 | 467 | /* reconstruct packet */ |
a306f2dd | 468 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); |
94ec8c6b | 469 | buffer_put_cstring(&b, authctxt->user); |
38c295d6 | 470 | buffer_put_cstring(&b, |
cbc5abf9 | 471 | datafellows & SSH_BUG_PKSERVICE ? |
38c295d6 | 472 | "ssh-userauth" : |
94ec8c6b | 473 | authctxt->service); |
cbc5abf9 | 474 | if (datafellows & SSH_BUG_PKAUTH) { |
475 | buffer_put_char(&b, have_sig); | |
476 | } else { | |
477 | buffer_put_cstring(&b, "publickey"); | |
478 | buffer_put_char(&b, have_sig); | |
479 | buffer_put_cstring(&b, key_ssh_name(key)); | |
480 | } | |
38c295d6 | 481 | buffer_put_string(&b, pkblob, blen); |
fa08c86b | 482 | #ifdef DEBUG_PK |
a306f2dd | 483 | buffer_dump(&b); |
484 | #endif | |
485 | /* test for correct signature */ | |
fa08c86b | 486 | if (user_key_allowed(authctxt->pw, key) && |
487 | key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1) | |
a306f2dd | 488 | authenticated = 1; |
489 | buffer_clear(&b); | |
490 | xfree(sig); | |
491 | } else { | |
94ec8c6b | 492 | debug("test whether pkalg/pkblob are acceptable"); |
a306f2dd | 493 | packet_done(); |
94ec8c6b | 494 | |
a306f2dd | 495 | /* XXX fake reply and always send PK_OK ? */ |
1d1ffb87 | 496 | /* |
497 | * XXX this allows testing whether a user is allowed | |
498 | * to login: if you happen to have a valid pubkey this | |
499 | * message is sent. the message is NEVER sent at all | |
500 | * if a user is not allowed to login. is this an | |
501 | * issue? -markus | |
502 | */ | |
fa08c86b | 503 | if (user_key_allowed(authctxt->pw, key)) { |
a306f2dd | 504 | packet_start(SSH2_MSG_USERAUTH_PK_OK); |
505 | packet_put_string(pkalg, alen); | |
506 | packet_put_string(pkblob, blen); | |
507 | packet_send(); | |
508 | packet_write_wait(); | |
509 | authenticated = -1; | |
510 | } | |
511 | } | |
94ec8c6b | 512 | if (authenticated != 1) |
513 | auth_clear_options(); | |
a306f2dd | 514 | key_free(key); |
515 | } | |
fa08c86b | 516 | debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg); |
a306f2dd | 517 | xfree(pkalg); |
518 | xfree(pkblob); | |
94ec8c6b | 519 | #ifdef HAVE_CYGWIN |
520 | if (check_nt_auth(0, authctxt->pw->pw_uid) == 0) | |
521 | return(0); | |
522 | #endif | |
a306f2dd | 523 | return authenticated; |
524 | } | |
525 | ||
94ec8c6b | 526 | /* get current user */ |
a306f2dd | 527 | |
528 | struct passwd* | |
529 | auth_get_user(void) | |
530 | { | |
94ec8c6b | 531 | return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL; |
a306f2dd | 532 | } |
533 | ||
94ec8c6b | 534 | #define DELIM "," |
535 | ||
536 | char * | |
537 | authmethods_get(void) | |
a306f2dd | 538 | { |
94ec8c6b | 539 | Authmethod *method = NULL; |
1e3b8b07 | 540 | u_int size = 0; |
94ec8c6b | 541 | char *list; |
542 | ||
543 | for (method = authmethods; method->name != NULL; method++) { | |
544 | if (strcmp(method->name, "none") == 0) | |
545 | continue; | |
546 | if (method->enabled != NULL && *(method->enabled) != 0) { | |
547 | if (size != 0) | |
548 | size += strlen(DELIM); | |
549 | size += strlen(method->name); | |
a306f2dd | 550 | } |
94ec8c6b | 551 | } |
552 | size++; /* trailing '\0' */ | |
553 | list = xmalloc(size); | |
554 | list[0] = '\0'; | |
555 | ||
556 | for (method = authmethods; method->name != NULL; method++) { | |
557 | if (strcmp(method->name, "none") == 0) | |
558 | continue; | |
559 | if (method->enabled != NULL && *(method->enabled) != 0) { | |
560 | if (list[0] != '\0') | |
561 | strlcat(list, DELIM, size); | |
562 | strlcat(list, method->name, size); | |
a306f2dd | 563 | } |
564 | } | |
94ec8c6b | 565 | return list; |
566 | } | |
567 | ||
568 | Authmethod * | |
569 | authmethod_lookup(const char *name) | |
570 | { | |
571 | Authmethod *method = NULL; | |
572 | if (name != NULL) | |
573 | for (method = authmethods; method->name != NULL; method++) | |
574 | if (method->enabled != NULL && | |
575 | *(method->enabled) != 0 && | |
576 | strcmp(name, method->name) == 0) | |
577 | return method; | |
578 | debug2("Unrecognized authentication method name: %s", name ? name : "NULL"); | |
579 | return NULL; | |
a306f2dd | 580 | } |
581 | ||
582 | /* return 1 if user allows given key */ | |
583 | int | |
fa08c86b | 584 | user_key_allowed(struct passwd *pw, Key *key) |
a306f2dd | 585 | { |
586 | char line[8192], file[1024]; | |
587 | int found_key = 0; | |
a306f2dd | 588 | FILE *f; |
1e3b8b07 | 589 | u_long linenum = 0; |
a306f2dd | 590 | struct stat st; |
591 | Key *found; | |
592 | ||
94ec8c6b | 593 | if (pw == NULL) |
594 | return 0; | |
595 | ||
a306f2dd | 596 | /* Temporarily use the user's uid. */ |
597 | temporarily_use_uid(pw->pw_uid); | |
598 | ||
599 | /* The authorized keys. */ | |
600 | snprintf(file, sizeof file, "%.500s/%.100s", pw->pw_dir, | |
601 | SSH_USER_PERMITTED_KEYS2); | |
602 | ||
603 | /* Fail quietly if file does not exist */ | |
604 | if (stat(file, &st) < 0) { | |
605 | /* Restore the privileged uid. */ | |
606 | restore_uid(); | |
607 | return 0; | |
608 | } | |
609 | /* Open the file containing the authorized keys. */ | |
610 | f = fopen(file, "r"); | |
611 | if (!f) { | |
612 | /* Restore the privileged uid. */ | |
613 | restore_uid(); | |
614 | return 0; | |
615 | } | |
616 | if (options.strict_modes) { | |
617 | int fail = 0; | |
618 | char buf[1024]; | |
619 | /* Check open file in order to avoid open/stat races */ | |
620 | if (fstat(fileno(f), &st) < 0 || | |
621 | (st.st_uid != 0 && st.st_uid != pw->pw_uid) || | |
622 | (st.st_mode & 022) != 0) { | |
de273eef | 623 | snprintf(buf, sizeof buf, |
624 | "%s authentication refused for %.100s: " | |
625 | "bad ownership or modes for '%s'.", | |
626 | key_type(key), pw->pw_name, file); | |
a306f2dd | 627 | fail = 1; |
628 | } else { | |
629 | /* Check path to SSH_USER_PERMITTED_KEYS */ | |
630 | int i; | |
631 | static const char *check[] = { | |
632 | "", SSH_USER_DIR, NULL | |
633 | }; | |
634 | for (i = 0; check[i]; i++) { | |
635 | snprintf(line, sizeof line, "%.500s/%.100s", | |
636 | pw->pw_dir, check[i]); | |
637 | if (stat(line, &st) < 0 || | |
638 | (st.st_uid != 0 && st.st_uid != pw->pw_uid) || | |
639 | (st.st_mode & 022) != 0) { | |
640 | snprintf(buf, sizeof buf, | |
de273eef | 641 | "%s authentication refused for %.100s: " |
a306f2dd | 642 | "bad ownership or modes for '%s'.", |
de273eef | 643 | key_type(key), pw->pw_name, line); |
a306f2dd | 644 | fail = 1; |
645 | break; | |
646 | } | |
647 | } | |
648 | } | |
649 | if (fail) { | |
a306f2dd | 650 | fclose(f); |
089fbbd2 | 651 | log("%s",buf); |
a306f2dd | 652 | restore_uid(); |
653 | return 0; | |
654 | } | |
655 | } | |
656 | found_key = 0; | |
de273eef | 657 | found = key_new(key->type); |
a306f2dd | 658 | |
659 | while (fgets(line, sizeof(line), f)) { | |
38c295d6 | 660 | char *cp, *options = NULL; |
a306f2dd | 661 | linenum++; |
662 | /* Skip leading whitespace, empty and comment lines. */ | |
663 | for (cp = line; *cp == ' ' || *cp == '\t'; cp++) | |
664 | ; | |
665 | if (!*cp || *cp == '\n' || *cp == '#') | |
666 | continue; | |
38c295d6 | 667 | |
fa08c86b | 668 | if (key_read(found, &cp) == -1) { |
38c295d6 | 669 | /* no key? check if there are options for this key */ |
670 | int quoted = 0; | |
fa08c86b | 671 | debug2("user_key_allowed: check options: '%s'", cp); |
38c295d6 | 672 | options = cp; |
673 | for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) { | |
674 | if (*cp == '\\' && cp[1] == '"') | |
675 | cp++; /* Skip both */ | |
676 | else if (*cp == '"') | |
677 | quoted = !quoted; | |
678 | } | |
679 | /* Skip remaining whitespace. */ | |
680 | for (; *cp == ' ' || *cp == '\t'; cp++) | |
681 | ; | |
fa08c86b | 682 | if (key_read(found, &cp) == -1) { |
683 | debug2("user_key_allowed: advance: '%s'", cp); | |
38c295d6 | 684 | /* still no key? advance to next line*/ |
685 | continue; | |
686 | } | |
687 | } | |
688 | if (key_equal(found, key) && | |
689 | auth_parse_options(pw, options, linenum) == 1) { | |
a306f2dd | 690 | found_key = 1; |
691 | debug("matching key found: file %s, line %ld", | |
692 | file, linenum); | |
693 | break; | |
694 | } | |
695 | } | |
696 | restore_uid(); | |
697 | fclose(f); | |
698 | key_free(found); | |
699 | return found_key; | |
700 | } |