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