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