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.25 2001/01/08 22:29:05 markus Exp $");
33 #include <openssl/dsa.h>
34 #include <openssl/rsa.h>
35 #include <openssl/evp.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);
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);
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);
101 Authmethod authmethods[] = {
107 &options.pubkey_authentication},
108 {"keyboard-interactive",
110 &options.kbd_interactive_authentication},
113 &options.password_authentication},
118 * loop until authctxt->success == TRUE
124 Authctxt *authctxt = xmalloc(sizeof(*authctxt));
125 memset(authctxt, 'a', sizeof(*authctxt));
127 authctxt->attempt = 0;
128 authctxt->failures = 0;
129 authctxt->success = 0;
130 x_authctxt = authctxt; /*XXX*/
133 /* turn off kerberos, not supported by SSH2 */
134 options.kerberos_authentication = 0;
136 dispatch_init(&protocol_error);
137 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
138 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
143 protocol_error(int type, int plen, void *ctxt)
145 log("auth: protocol error: type %d plen %d", type, plen);
146 packet_start(SSH2_MSG_UNIMPLEMENTED);
153 input_service_request(int type, int plen, void *ctxt)
155 Authctxt *authctxt = ctxt;
158 char *service = packet_get_string(&len);
161 if (authctxt == NULL)
162 fatal("input_service_request: no authctxt");
164 if (strcmp(service, "ssh-userauth") == 0) {
165 if (!authctxt->success) {
167 /* now we can handle user-auth requests */
168 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
171 /* XXX all other service requests are denied */
174 packet_start(SSH2_MSG_SERVICE_ACCEPT);
175 packet_put_cstring(service);
179 debug("bad service request %s", service);
180 packet_disconnect("bad service request %s", service);
186 input_userauth_request(int type, int plen, void *ctxt)
188 Authctxt *authctxt = ctxt;
189 Authmethod *m = NULL;
190 char *user, *service, *method;
191 int authenticated = 0;
193 if (authctxt == NULL)
194 fatal("input_userauth_request: no authctxt");
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);
202 if (authctxt->attempt++ == 0) {
203 /* setup auth context */
204 struct passwd *pw = NULL;
205 setproctitle("%s", user);
207 if (pw && allowed_user(pw) && strcmp(service, "ssh-connection")==0) {
208 authctxt->pw = pwcopy(pw);
210 debug2("input_userauth_request: setting up authctxt for %s", user);
215 log("input_userauth_request: illegal user %s", user);
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);
228 m = authmethod_lookup(method);
230 debug2("input_userauth_request: try method %s", method);
231 authenticated = m->userauth(authctxt);
233 debug2("input_userauth_request: unsupported method %s", method);
235 if (!authctxt->valid && authenticated == 1) {
236 log("input_userauth_request: INTERNAL ERROR: authenticated invalid user %s service %s", user, method);
240 /* Special handling for root */
241 if (authenticated == 1 &&
242 authctxt->valid && authctxt->pw->pw_uid == 0 && !options.permit_root_login) {
244 log("ROOT LOGIN REFUSED FROM %.200s", get_canonical_hostname());
248 if (authenticated && authctxt->user && !do_pam_account(authctxt->user, NULL))
252 /* Log before sending the reply */
253 userauth_log(authctxt, authenticated, method);
254 userauth_reply(authctxt, authenticated);
262 userauth_banner(void)
269 if (options.banner == NULL || (datafellows & SSH_BUG_BANNER))
271 if ((fd = open(options.banner, O_RDONLY)) < 0) {
272 error("userauth_banner: open %s failed: %s",
273 options.banner, strerror(errno));
276 if (fstat(fd, &st) < 0)
279 banner = xmalloc(len + 1);
280 if ((n = read(fd, banner, len)) < 0)
283 packet_start(SSH2_MSG_USERAUTH_BANNER);
284 packet_put_cstring(banner);
285 packet_put_cstring(""); /* language, unused */
287 debug("userauth_banner: sent");
296 userauth_log(Authctxt *authctxt, int authenticated, char *method)
298 void (*authlog) (const char *fmt,...) = verbose;
299 char *user = NULL, *authmsg = NULL;
301 /* Raise logging level */
302 if (authenticated == 1 ||
304 authctxt->failures >= AUTH_FAIL_LOG ||
305 strcmp(method, "password") == 0)
308 if (authenticated == 1) {
309 authmsg = "Accepted";
310 } else if (authenticated == 0) {
313 authmsg = "Postponed";
316 if (authctxt->valid) {
317 user = authctxt->pw->pw_uid == 0 ? "ROOT" : authctxt->user;
322 authlog("%s %s for %.200s from %.200s port %d ssh2",
331 userauth_reply(Authctxt *authctxt, int authenticated)
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)
341 #endif /* WITH_AIXAUTHENTICATE */
342 /* turn off userauth */
343 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &protocol_error);
344 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
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 */
360 /* do nothing, we did already send a reply */
365 userauth_none(Authctxt *authctxt)
367 /* disable method "none", only allowed one time */
368 Authmethod *m = authmethod_lookup("none");
374 if (authctxt->valid == 0)
378 if (check_nt_auth(1, authctxt->pw->pw_uid) == 0)
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, "");
393 userauth_passwd(Authctxt *authctxt)
396 int authenticated = 0;
399 change = packet_get_char();
401 log("password change not supported");
402 password = packet_get_string(&len);
404 if (authctxt->valid &&
406 check_nt_auth(1, authctxt->pw->pw_uid) &&
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)
418 memset(password, 0, len);
420 return authenticated;
424 userauth_kbdint(Authctxt *authctxt)
426 int authenticated = 0;
430 lang = packet_get_string(NULL);
431 devs = packet_get_string(NULL);
434 debug("keyboard-interactive language %s devs %s", lang, devs);
436 if (authenticated == 0)
437 authenticated = auth2_pam(authctxt);
440 /* XXX hardcoded, we should look at devs */
441 if (authenticated == 0)
442 if (options.skey_authentication != 0)
443 authenticated = auth2_skey(authctxt);
448 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
451 return authenticated;
455 userauth_pubkey(Authctxt *authctxt)
459 char *pkalg, *pkblob, *sig;
460 u_int alen, blen, slen;
461 int have_sig, pktype;
462 int authenticated = 0;
464 if (!authctxt->valid) {
465 debug2("userauth_pubkey: disabled because of invalid user");
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);
474 buffer_append(&b, pkblob, blen);
475 /* so we have to extract the pkalg from the pkblob */
476 pkalg = buffer_get_string(&b, &alen);
479 pkalg = packet_get_string(&alen);
480 pkblob = packet_get_string(&blen);
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);
490 key = key_from_blob(pkblob, blen);
493 sig = packet_get_string(&slen);
496 if (datafellows & SSH_OLD_SESSIONID) {
497 buffer_append(&b, session_id2, session_id2_len);
499 buffer_put_string(&b, session_id2, session_id2_len);
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 ?
508 if (datafellows & SSH_BUG_PKAUTH) {
509 buffer_put_char(&b, have_sig);
511 buffer_put_cstring(&b, "publickey");
512 buffer_put_char(&b, have_sig);
513 buffer_put_cstring(&b, key_ssh_name(key));
515 buffer_put_string(&b, pkblob, blen);
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)
526 debug("test whether pkalg/pkblob are acceptable");
529 /* XXX fake reply and always send PK_OK ? */
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
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);
546 if (authenticated != 1)
547 auth_clear_options();
550 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
554 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
557 return authenticated;
560 /* get current user */
565 return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
571 authmethods_get(void)
573 Authmethod *method = NULL;
577 for (method = authmethods; method->name != NULL; method++) {
578 if (strcmp(method->name, "none") == 0)
580 if (method->enabled != NULL && *(method->enabled) != 0) {
582 size += strlen(DELIM);
583 size += strlen(method->name);
586 size++; /* trailing '\0' */
587 list = xmalloc(size);
590 for (method = authmethods; method->name != NULL; method++) {
591 if (strcmp(method->name, "none") == 0)
593 if (method->enabled != NULL && *(method->enabled) != 0) {
595 strlcat(list, DELIM, size);
596 strlcat(list, method->name, size);
603 authmethod_lookup(const char *name)
605 Authmethod *method = NULL;
607 for (method = authmethods; method->name != NULL; method++)
608 if (method->enabled != NULL &&
609 *(method->enabled) != 0 &&
610 strcmp(name, method->name) == 0)
612 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
616 /* return 1 if user allows given key */
618 user_key_allowed(struct passwd *pw, Key *key)
620 char line[8192], file[1024];
630 /* Temporarily use the user's uid. */
631 temporarily_use_uid(pw->pw_uid);
633 /* The authorized keys. */
634 snprintf(file, sizeof file, "%.500s/%.100s", pw->pw_dir,
635 SSH_USER_PERMITTED_KEYS2);
637 /* Fail quietly if file does not exist */
638 if (stat(file, &st) < 0) {
639 /* Restore the privileged uid. */
643 /* Open the file containing the authorized keys. */
644 f = fopen(file, "r");
646 /* Restore the privileged uid. */
650 if (options.strict_modes) {
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);
663 /* Check path to SSH_USER_PERMITTED_KEYS */
665 static const char *check[] = {
666 "", SSH_USER_DIR, NULL
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);
691 found = key_new(key->type);
693 while (fgets(line, sizeof(line), f)) {
694 char *cp, *options = NULL;
696 /* Skip leading whitespace, empty and comment lines. */
697 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
699 if (!*cp || *cp == '\n' || *cp == '#')
702 if (key_read(found, &cp) == -1) {
703 /* no key? check if there are options for this key */
705 debug2("user_key_allowed: check options: '%s'", cp);
707 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
708 if (*cp == '\\' && cp[1] == '"')
709 cp++; /* Skip both */
713 /* Skip remaining whitespace. */
714 for (; *cp == ' ' || *cp == '\t'; cp++)
716 if (key_read(found, &cp) == -1) {
717 debug2("user_key_allowed: advance: '%s'", cp);
718 /* still no key? advance to next line*/
722 if (key_equal(found, key) &&
723 auth_parse_options(pw, options, linenum) == 1) {
725 debug("matching key found: file %s, line %ld",
737 pwcopy(struct passwd *pw)
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);
748 copy->pw_dir = xstrdup(pw->pw_dir);
749 copy->pw_shell = xstrdup(pw->pw_shell);