2 * Copyright (c) 2000 Markus Friedl. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 RCSID("$OpenBSD: auth2.c,v 1.32 2001/01/21 19:05:44 markus Exp $");
33 #include <openssl/evp.h>
53 #include "pathnames.h"
56 #include "auth-options.h"
59 extern ServerOptions options;
60 extern u_char *session_id2;
61 extern int session_id2_len;
63 #ifdef WITH_AIXAUTHENTICATE
64 extern char *aixloginmsg;
67 extern int saved_argc;
68 extern char **saved_argv;
71 static Authctxt *x_authctxt = NULL;
74 typedef struct Authmethod Authmethod;
77 int (*userauth)(Authctxt *authctxt);
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);
88 Authmethod *authmethod_lookup(const char *name);
89 struct passwd *pwcopy(struct passwd *pw);
90 int user_key_allowed(struct passwd *pw, Key *key);
91 char *authmethods_get(void);
94 void userauth_banner(void);
95 int userauth_none(Authctxt *authctxt);
96 int userauth_passwd(Authctxt *authctxt);
97 int userauth_pubkey(Authctxt *authctxt);
98 int userauth_kbdint(Authctxt *authctxt);
100 Authmethod authmethods[] = {
106 &options.pubkey_authentication},
107 {"keyboard-interactive",
109 &options.kbd_interactive_authentication},
112 &options.password_authentication},
117 * loop until authctxt->success == TRUE
123 Authctxt *authctxt = authctxt_new();
125 x_authctxt = authctxt; /*XXX*/
128 /* If machine has AFS, set process authentication group. */
134 dispatch_init(&protocol_error);
135 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
136 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
137 do_authenticated2(authctxt);
141 protocol_error(int type, int plen, void *ctxt)
143 log("auth: protocol error: type %d plen %d", type, plen);
144 packet_start(SSH2_MSG_UNIMPLEMENTED);
151 input_service_request(int type, int plen, void *ctxt)
153 Authctxt *authctxt = ctxt;
156 char *service = packet_get_string(&len);
159 if (authctxt == NULL)
160 fatal("input_service_request: no authctxt");
162 if (strcmp(service, "ssh-userauth") == 0) {
163 if (!authctxt->success) {
165 /* now we can handle user-auth requests */
166 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
169 /* XXX all other service requests are denied */
172 packet_start(SSH2_MSG_SERVICE_ACCEPT);
173 packet_put_cstring(service);
177 debug("bad service request %s", service);
178 packet_disconnect("bad service request %s", service);
184 input_userauth_request(int type, int plen, void *ctxt)
186 Authctxt *authctxt = ctxt;
187 Authmethod *m = NULL;
188 char *user, *service, *method, *style = NULL;
189 int authenticated = 0;
191 if (authctxt == NULL)
192 fatal("input_userauth_request: no authctxt");
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);
198 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
200 if ((style = strchr(user, ':')) != NULL)
203 if (authctxt->attempt++ == 0) {
204 /* setup auth context */
205 struct passwd *pw = NULL;
206 setproctitle("%s", user);
208 if (pw && allowed_user(pw) && strcmp(service, "ssh-connection")==0) {
209 authctxt->pw = pwcopy(pw);
211 debug2("input_userauth_request: setting up authctxt for %s", user);
213 start_pam(pw->pw_name);
216 log("input_userauth_request: illegal user %s", user);
221 authctxt->user = xstrdup(user);
222 authctxt->service = xstrdup(service);
223 authctxt->style = style ? xstrdup(style) : NULL; /* currently unused */
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);
233 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, &protocol_error);
234 authctxt->postponed = 0;
236 /* try to authenticate user */
237 m = authmethod_lookup(method);
239 debug2("input_userauth_request: try method %s", method);
240 authenticated = m->userauth(authctxt);
242 if (!authctxt->valid && authenticated)
243 fatal("INTERNAL ERROR: authenticated invalid user %s",
246 /* Special handling for root */
247 if (authenticated && authctxt->pw->pw_uid == 0 && !auth_root_allowed())
251 if (authenticated && authctxt->user && !do_pam_account(authctxt->user, NULL))
255 /* Log before sending the reply */
256 auth_log(authctxt, authenticated, method, " ssh2");
258 if (!authctxt->postponed)
259 userauth_reply(authctxt, authenticated);
267 userauth_banner(void)
274 if (options.banner == NULL || (datafellows & SSH_BUG_BANNER))
276 if ((fd = open(options.banner, O_RDONLY)) < 0) {
277 error("userauth_banner: open %s failed: %s",
278 options.banner, strerror(errno));
281 if (fstat(fd, &st) < 0)
284 banner = xmalloc(len + 1);
285 if ((n = read(fd, banner, len)) < 0)
288 packet_start(SSH2_MSG_USERAUTH_BANNER);
289 packet_put_cstring(banner);
290 packet_put_cstring(""); /* language, unused */
292 debug("userauth_banner: sent");
301 userauth_reply(Authctxt *authctxt, int authenticated)
305 /* XXX todo: check if multiple auth methods are needed */
307 #ifdef WITH_AIXAUTHENTICATE
308 /* We don't have a pty yet, so just label the line as "ssh" */
309 if (loginsuccess(authctxt->user?authctxt->user:"NOUSER",
310 get_canonical_hostname(), "ssh", &aixloginmsg) < 0)
312 #endif /* WITH_AIXAUTHENTICATE */
313 /* turn off userauth */
314 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &protocol_error);
315 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
318 /* now we can break out */
319 authctxt->success = 1;
321 if (authctxt->failures++ > AUTH_FAIL_MAX)
322 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
323 methods = authmethods_get();
324 packet_start(SSH2_MSG_USERAUTH_FAILURE);
325 packet_put_cstring(methods);
326 packet_put_char(0); /* XXX partial success, unused */
334 userauth_none(Authctxt *authctxt)
336 /* disable method "none", only allowed one time */
337 Authmethod *m = authmethod_lookup("none");
343 if (authctxt->valid == 0)
347 if (check_nt_auth(1, authctxt->pw->pw_uid) == 0)
351 return auth_pam_password(authctxt->pw, "");
352 #elif defined(HAVE_OSF_SIA)
353 return (sia_validate_user(NULL, saved_argc, saved_argv,
354 get_canonical_hostname(), authctxt->user?authctxt->user:"NOUSER",
355 NULL, 0, NULL, "") == SIASUCCESS);
356 #else /* !HAVE_OSF_SIA && !USE_PAM */
357 return auth_password(authctxt->pw, "");
362 userauth_passwd(Authctxt *authctxt)
365 int authenticated = 0;
368 change = packet_get_char();
370 log("password change not supported");
371 password = packet_get_string(&len);
373 if (authctxt->valid &&
375 check_nt_auth(1, authctxt->pw->pw_uid) &&
378 auth_pam_password(authctxt->pw, password) == 1)
379 #elif defined(HAVE_OSF_SIA)
380 sia_validate_user(NULL, saved_argc, saved_argv,
381 get_canonical_hostname(), authctxt->user?authctxt->user:"NOUSER",
382 NULL, 0, NULL, password) == SIASUCCESS)
383 #else /* !USE_PAM && !HAVE_OSF_SIA */
384 auth_password(authctxt->pw, password) == 1)
387 memset(password, 0, len);
389 return authenticated;
393 userauth_kbdint(Authctxt *authctxt)
395 int authenticated = 0;
399 lang = packet_get_string(NULL);
400 devs = packet_get_string(NULL);
403 debug("keyboard-interactive language %s devs %s", lang, devs);
405 authenticated = auth2_challenge(authctxt, devs);
408 if (authenticated == 0)
409 authenticated = auth2_pam(authctxt);
414 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
417 return authenticated;
421 userauth_pubkey(Authctxt *authctxt)
425 char *pkalg, *pkblob, *sig;
426 u_int alen, blen, slen;
427 int have_sig, pktype;
428 int authenticated = 0;
430 if (!authctxt->valid) {
431 debug2("userauth_pubkey: disabled because of invalid user");
434 have_sig = packet_get_char();
435 if (datafellows & SSH_BUG_PKAUTH) {
436 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
437 /* no explicit pkalg given */
438 pkblob = packet_get_string(&blen);
440 buffer_append(&b, pkblob, blen);
441 /* so we have to extract the pkalg from the pkblob */
442 pkalg = buffer_get_string(&b, &alen);
445 pkalg = packet_get_string(&alen);
446 pkblob = packet_get_string(&blen);
448 pktype = key_type_from_name(pkalg);
449 if (pktype == KEY_UNSPEC) {
450 /* this is perfectly legal */
451 log("userauth_pubkey: unsupported public key algorithm: %s", pkalg);
456 key = key_from_blob(pkblob, blen);
459 sig = packet_get_string(&slen);
462 if (datafellows & SSH_OLD_SESSIONID) {
463 buffer_append(&b, session_id2, session_id2_len);
465 buffer_put_string(&b, session_id2, session_id2_len);
467 /* reconstruct packet */
468 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
469 buffer_put_cstring(&b, authctxt->user);
470 buffer_put_cstring(&b,
471 datafellows & SSH_BUG_PKSERVICE ?
474 if (datafellows & SSH_BUG_PKAUTH) {
475 buffer_put_char(&b, have_sig);
477 buffer_put_cstring(&b, "publickey");
478 buffer_put_char(&b, have_sig);
479 buffer_put_cstring(&b, key_ssh_name(key));
481 buffer_put_string(&b, pkblob, blen);
485 /* test for correct signature */
486 if (user_key_allowed(authctxt->pw, key) &&
487 key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
492 debug("test whether pkalg/pkblob are acceptable");
495 /* XXX fake reply and always send PK_OK ? */
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
503 if (user_key_allowed(authctxt->pw, key)) {
504 packet_start(SSH2_MSG_USERAUTH_PK_OK);
505 packet_put_string(pkalg, alen);
506 packet_put_string(pkblob, blen);
512 if (authenticated != 1)
513 auth_clear_options();
516 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
520 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
523 return authenticated;
526 /* get current user */
531 return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
537 authmethods_get(void)
539 Authmethod *method = NULL;
543 for (method = authmethods; method->name != NULL; method++) {
544 if (strcmp(method->name, "none") == 0)
546 if (method->enabled != NULL && *(method->enabled) != 0) {
548 size += strlen(DELIM);
549 size += strlen(method->name);
552 size++; /* trailing '\0' */
553 list = xmalloc(size);
556 for (method = authmethods; method->name != NULL; method++) {
557 if (strcmp(method->name, "none") == 0)
559 if (method->enabled != NULL && *(method->enabled) != 0) {
561 strlcat(list, DELIM, size);
562 strlcat(list, method->name, size);
569 authmethod_lookup(const char *name)
571 Authmethod *method = NULL;
573 for (method = authmethods; method->name != NULL; method++)
574 if (method->enabled != NULL &&
575 *(method->enabled) != 0 &&
576 strcmp(name, method->name) == 0)
578 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
582 /* return 1 if user allows given key */
584 user_key_allowed(struct passwd *pw, Key *key)
586 char line[8192], file[MAXPATHLEN];
596 /* Temporarily use the user's uid. */
597 temporarily_use_uid(pw->pw_uid);
599 /* The authorized keys. */
600 snprintf(file, sizeof file, "%.500s/%.100s", pw->pw_dir,
601 _PATH_SSH_USER_PERMITTED_KEYS2);
603 /* Fail quietly if file does not exist */
604 if (stat(file, &st) < 0) {
605 /* Restore the privileged uid. */
609 /* Open the file containing the authorized keys. */
610 f = fopen(file, "r");
612 /* Restore the privileged uid. */
616 if (options.strict_modes) {
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) {
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);
629 /* Check path to _PATH_SSH_USER_PERMITTED_KEYS */
631 static const char *check[] = {
632 "", _PATH_SSH_USER_DIR, NULL
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,
641 "%s authentication refused for %.100s: "
642 "bad ownership or modes for '%s'.",
643 key_type(key), pw->pw_name, line);
657 found = key_new(key->type);
659 while (fgets(line, sizeof(line), f)) {
660 char *cp, *options = NULL;
662 /* Skip leading whitespace, empty and comment lines. */
663 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
665 if (!*cp || *cp == '\n' || *cp == '#')
668 if (key_read(found, &cp) == -1) {
669 /* no key? check if there are options for this key */
671 debug2("user_key_allowed: check options: '%s'", cp);
673 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
674 if (*cp == '\\' && cp[1] == '"')
675 cp++; /* Skip both */
679 /* Skip remaining whitespace. */
680 for (; *cp == ' ' || *cp == '\t'; cp++)
682 if (key_read(found, &cp) == -1) {
683 debug2("user_key_allowed: advance: '%s'", cp);
684 /* still no key? advance to next line*/
688 if (key_equal(found, key) &&
689 auth_parse_options(pw, options, file, linenum) == 1) {
691 debug("matching key found: file %s, line %ld",