Annotation of src/usr.bin/ssh/auth.c, Revision 1.20
1.1 markus 1: /*
1.19 deraadt 2: * Copyright (c) 2000 Markus Friedl. All rights reserved.
1.9 deraadt 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.
1.1 markus 23: */
24:
25: #include "includes.h"
1.20 ! markus 26: RCSID("$OpenBSD: auth.c,v 1.19 2001/03/02 18:54:31 deraadt Exp $");
1.1 markus 27:
28: #include "xmalloc.h"
1.13 markus 29: #include "match.h"
1.14 markus 30: #include "groupaccess.h"
31: #include "log.h"
1.1 markus 32: #include "servconf.h"
1.2 markus 33: #include "auth.h"
1.13 markus 34: #include "auth-options.h"
1.14 markus 35: #include "canohost.h"
1.2 markus 36:
1.1 markus 37: /* import */
38: extern ServerOptions options;
39:
40: /*
1.12 markus 41: * Check if the user is allowed to log in via ssh. If user is listed
42: * in DenyUsers or one of user's groups is listed in DenyGroups, false
43: * will be returned. If AllowUsers isn't empty and user isn't listed
44: * there, or if AllowGroups isn't empty and one of user's groups isn't
45: * listed there, false will be returned.
1.1 markus 46: * If the user's shell is not executable, false will be returned.
1.4 markus 47: * Otherwise true is returned.
1.1 markus 48: */
1.5 markus 49: int
1.1 markus 50: allowed_user(struct passwd * pw)
51: {
52: struct stat st;
1.20 ! markus 53: char *shell, *cp;
1.1 markus 54: int i;
55:
56: /* Shouldn't be called if pw is NULL, but better safe than sorry... */
1.12 markus 57: if (!pw || !pw->pw_name)
1.1 markus 58: return 0;
59:
1.7 deraadt 60: /*
61: * Get the shell from the password data. An empty shell field is
62: * legal, and means /bin/sh.
63: */
64: shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
1.20 ! markus 65:
! 66: /* disallow anyone who does not have a standard shell */
! 67: setusershell();
! 68: while ((cp = getusershell()) != NULL)
! 69: if (strcmp(cp, shell) == 0)
! 70: break;
! 71: endusershell();
! 72: if (cp == NULL)
! 73: return 0;
1.7 deraadt 74:
1.1 markus 75: /* deny if shell does not exists or is not executable */
1.7 deraadt 76: if (stat(shell, &st) != 0)
1.1 markus 77: return 0;
78: if (!((st.st_mode & S_IFREG) && (st.st_mode & (S_IXOTH|S_IXUSR|S_IXGRP))))
79: return 0;
80:
81: /* Return false if user is listed in DenyUsers */
82: if (options.num_deny_users > 0) {
83: for (i = 0; i < options.num_deny_users; i++)
84: if (match_pattern(pw->pw_name, options.deny_users[i]))
85: return 0;
86: }
87: /* Return false if AllowUsers isn't empty and user isn't listed there */
88: if (options.num_allow_users > 0) {
89: for (i = 0; i < options.num_allow_users; i++)
90: if (match_pattern(pw->pw_name, options.allow_users[i]))
91: break;
92: /* i < options.num_allow_users iff we break for loop */
93: if (i >= options.num_allow_users)
94: return 0;
95: }
96: if (options.num_deny_groups > 0 || options.num_allow_groups > 0) {
1.12 markus 97: /* Get the user's group access list (primary and supplementary) */
98: if (ga_init(pw->pw_name, pw->pw_gid) == 0)
1.1 markus 99: return 0;
100:
1.12 markus 101: /* Return false if one of user's groups is listed in DenyGroups */
102: if (options.num_deny_groups > 0)
103: if (ga_match(options.deny_groups,
104: options.num_deny_groups)) {
105: ga_free();
1.1 markus 106: return 0;
1.12 markus 107: }
1.1 markus 108: /*
1.12 markus 109: * Return false if AllowGroups isn't empty and one of user's groups
1.1 markus 110: * isn't listed there
111: */
1.12 markus 112: if (options.num_allow_groups > 0)
113: if (!ga_match(options.allow_groups,
114: options.num_allow_groups)) {
115: ga_free();
1.1 markus 116: return 0;
1.12 markus 117: }
118: ga_free();
1.1 markus 119: }
120: /* We found no reason not to let this user try to log on... */
121: return 1;
1.13 markus 122: }
123:
124: Authctxt *
125: authctxt_new(void)
126: {
1.16 stevesk 127: Authctxt *authctxt = xmalloc(sizeof(*authctxt));
128: memset(authctxt, 0, sizeof(*authctxt));
129: return authctxt;
1.13 markus 130: }
131:
132: void
133: auth_log(Authctxt *authctxt, int authenticated, char *method, char *info)
134: {
135: void (*authlog) (const char *fmt,...) = verbose;
136: char *authmsg;
137:
138: /* Raise logging level */
139: if (authenticated == 1 ||
140: !authctxt->valid ||
141: authctxt->failures >= AUTH_FAIL_LOG ||
142: strcmp(method, "password") == 0)
143: authlog = log;
144:
145: if (authctxt->postponed)
146: authmsg = "Postponed";
147: else
148: authmsg = authenticated ? "Accepted" : "Failed";
149:
150: authlog("%s %s for %s%.100s from %.200s port %d%s",
151: authmsg,
152: method,
153: authctxt->valid ? "" : "illegal user ",
154: authctxt->valid && authctxt->pw->pw_uid == 0 ? "ROOT" : authctxt->user,
155: get_remote_ipaddr(),
156: get_remote_port(),
157: info);
158: }
159:
160: /*
1.17 markus 161: * Check whether root logins are disallowed.
1.13 markus 162: */
163: int
1.17 markus 164: auth_root_allowed(char *method)
1.13 markus 165: {
1.17 markus 166: switch (options.permit_root_login) {
167: case PERMIT_YES:
1.13 markus 168: return 1;
1.17 markus 169: break;
170: case PERMIT_NO_PASSWD:
171: if (strcmp(method, "password") != 0)
172: return 1;
173: break;
174: case PERMIT_FORCED_ONLY:
175: if (forced_command) {
176: log("Root login accepted for forced command.");
177: return 1;
178: }
179: break;
1.13 markus 180: }
1.17 markus 181: log("ROOT LOGIN REFUSED FROM %.200s", get_remote_ipaddr());
182: return 0;
1.1 markus 183: }