Annotation of src/usr.bin/ssh/auth.c, Revision 1.21
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.21 ! markus 53: char *shell;
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;
65:
1.1 markus 66: /* deny if shell does not exists or is not executable */
1.7 deraadt 67: if (stat(shell, &st) != 0)
1.1 markus 68: return 0;
69: if (!((st.st_mode & S_IFREG) && (st.st_mode & (S_IXOTH|S_IXUSR|S_IXGRP))))
70: return 0;
71:
72: /* Return false if user is listed in DenyUsers */
73: if (options.num_deny_users > 0) {
74: for (i = 0; i < options.num_deny_users; i++)
75: if (match_pattern(pw->pw_name, options.deny_users[i]))
76: return 0;
77: }
78: /* Return false if AllowUsers isn't empty and user isn't listed there */
79: if (options.num_allow_users > 0) {
80: for (i = 0; i < options.num_allow_users; i++)
81: if (match_pattern(pw->pw_name, options.allow_users[i]))
82: break;
83: /* i < options.num_allow_users iff we break for loop */
84: if (i >= options.num_allow_users)
85: return 0;
86: }
87: if (options.num_deny_groups > 0 || options.num_allow_groups > 0) {
1.12 markus 88: /* Get the user's group access list (primary and supplementary) */
89: if (ga_init(pw->pw_name, pw->pw_gid) == 0)
1.1 markus 90: return 0;
91:
1.12 markus 92: /* Return false if one of user's groups is listed in DenyGroups */
93: if (options.num_deny_groups > 0)
94: if (ga_match(options.deny_groups,
95: options.num_deny_groups)) {
96: ga_free();
1.1 markus 97: return 0;
1.12 markus 98: }
1.1 markus 99: /*
1.12 markus 100: * Return false if AllowGroups isn't empty and one of user's groups
1.1 markus 101: * isn't listed there
102: */
1.12 markus 103: if (options.num_allow_groups > 0)
104: if (!ga_match(options.allow_groups,
105: options.num_allow_groups)) {
106: ga_free();
1.1 markus 107: return 0;
1.12 markus 108: }
109: ga_free();
1.1 markus 110: }
111: /* We found no reason not to let this user try to log on... */
112: return 1;
1.13 markus 113: }
114:
115: Authctxt *
116: authctxt_new(void)
117: {
1.16 stevesk 118: Authctxt *authctxt = xmalloc(sizeof(*authctxt));
119: memset(authctxt, 0, sizeof(*authctxt));
120: return authctxt;
1.13 markus 121: }
122:
123: void
124: auth_log(Authctxt *authctxt, int authenticated, char *method, char *info)
125: {
126: void (*authlog) (const char *fmt,...) = verbose;
127: char *authmsg;
128:
129: /* Raise logging level */
130: if (authenticated == 1 ||
131: !authctxt->valid ||
132: authctxt->failures >= AUTH_FAIL_LOG ||
133: strcmp(method, "password") == 0)
134: authlog = log;
135:
136: if (authctxt->postponed)
137: authmsg = "Postponed";
138: else
139: authmsg = authenticated ? "Accepted" : "Failed";
140:
141: authlog("%s %s for %s%.100s from %.200s port %d%s",
142: authmsg,
143: method,
144: authctxt->valid ? "" : "illegal user ",
145: authctxt->valid && authctxt->pw->pw_uid == 0 ? "ROOT" : authctxt->user,
146: get_remote_ipaddr(),
147: get_remote_port(),
148: info);
149: }
150:
151: /*
1.17 markus 152: * Check whether root logins are disallowed.
1.13 markus 153: */
154: int
1.17 markus 155: auth_root_allowed(char *method)
1.13 markus 156: {
1.17 markus 157: switch (options.permit_root_login) {
158: case PERMIT_YES:
1.13 markus 159: return 1;
1.17 markus 160: break;
161: case PERMIT_NO_PASSWD:
162: if (strcmp(method, "password") != 0)
163: return 1;
164: break;
165: case PERMIT_FORCED_ONLY:
166: if (forced_command) {
167: log("Root login accepted for forced command.");
168: return 1;
169: }
170: break;
1.13 markus 171: }
1.17 markus 172: log("ROOT LOGIN REFUSED FROM %.200s", get_remote_ipaddr());
173: return 0;
1.1 markus 174: }