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