tests/krb5: Clarify checksum type assertion message
[Samba.git] / source3 / smbd / share_access.c
blob57754a0f7663b9e19195af5509177b68d060a932
1 /*
2 Unix SMB/CIFS implementation.
3 Check access based on valid users, read list and friends
4 Copyright (C) Volker Lendecke 2005
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "smbd/smbd.h"
22 #include "smbd/globals.h"
23 #include "../libcli/security/security.h"
24 #include "passdb/lookup_sid.h"
25 #include "auth.h"
28 * No prefix means direct username
29 * @name means netgroup first, then unix group
30 * &name means netgroup
31 * +name means unix group
32 * + and & may be combined
35 static bool do_group_checks(const char **name, const char **pattern)
37 if ((*name)[0] == '@') {
38 *pattern = "&+";
39 *name += 1;
40 return True;
43 if (((*name)[0] == '+') && ((*name)[1] == '&')) {
44 *pattern = "+&";
45 *name += 2;
46 return True;
49 if ((*name)[0] == '+') {
50 *pattern = "+";
51 *name += 1;
52 return True;
55 if (((*name)[0] == '&') && ((*name)[1] == '+')) {
56 *pattern = "&+";
57 *name += 2;
58 return True;
61 if ((*name)[0] == '&') {
62 *pattern = "&";
63 *name += 1;
64 return True;
67 return False;
70 static bool token_contains_name(TALLOC_CTX *mem_ctx,
71 const char *username,
72 const char *domain,
73 const char *sharename,
74 const struct security_token *token,
75 const char *name)
77 const char *prefix;
78 struct dom_sid sid;
79 enum lsa_SidType type;
81 if (username != NULL) {
82 size_t domain_len = strlen(domain);
84 /* Check if username starts with domain name */
85 if (domain_len > 0) {
86 const char *sep = lp_winbind_separator();
87 int cmp = strncasecmp_m(username, domain, domain_len);
88 if (cmp == 0 && sep[0] == username[domain_len]) {
89 /* Move after the winbind separator */
90 domain_len += 1;
91 } else {
92 domain_len = 0;
95 name = talloc_sub_basic(mem_ctx,
96 username + domain_len,
97 domain,
98 name);
100 if (sharename != NULL) {
101 name = talloc_string_sub(mem_ctx, name, "%S", sharename);
104 if (name == NULL) {
105 /* This is too security sensitive, better panic than return a
106 * result that might be interpreted in a wrong way. */
107 smb_panic("substitutions failed");
110 if ( string_to_sid( &sid, name ) ) {
111 DEBUG(5,("token_contains_name: Checking for SID [%s] in token\n", name));
112 return nt_token_check_sid( &sid, token );
115 if (!do_group_checks(&name, &prefix)) {
116 if (!lookup_name_smbconf(mem_ctx, name, LOOKUP_NAME_ALL,
117 NULL, NULL, &sid, &type)) {
118 DEBUG(5, ("lookup_name %s failed\n", name));
119 return False;
121 if (type != SID_NAME_USER) {
122 DEBUG(5, ("%s is a %s, expected a user\n",
123 name, sid_type_lookup(type)));
124 return False;
126 return nt_token_check_sid(&sid, token);
129 for (/* initialized above */ ; *prefix != '\0'; prefix++) {
130 if (*prefix == '+') {
131 if (!lookup_name_smbconf(mem_ctx, name,
132 LOOKUP_NAME_ALL|LOOKUP_NAME_GROUP,
133 NULL, NULL, &sid, &type)) {
134 DEBUG(5, ("lookup_name %s failed\n", name));
135 return False;
137 if ((type != SID_NAME_DOM_GRP) &&
138 (type != SID_NAME_ALIAS) &&
139 (type != SID_NAME_WKN_GRP)) {
140 DEBUG(5, ("%s is a %s, expected a group\n",
141 name, sid_type_lookup(type)));
142 return False;
144 if (nt_token_check_sid(&sid, token)) {
145 return True;
147 continue;
149 if (*prefix == '&') {
150 if (username) {
151 if (user_in_netgroup(mem_ctx, username, name)) {
152 return True;
155 continue;
157 smb_panic("got invalid prefix from do_groups_check");
159 return False;
163 * Check whether a user is contained in the list provided.
165 * Please note that the user name and share names passed in here mainly for
166 * the substitution routines that expand the parameter values, the decision
167 * whether a user is in the list is done after a lookup_name on the expanded
168 * parameter value, solely based on comparing the SIDs in token.
170 * The other use is the netgroup check when using @group or &group.
173 bool token_contains_name_in_list(const char *username,
174 const char *domain,
175 const char *sharename,
176 const struct security_token *token,
177 const char **list)
179 if (list == NULL) {
180 return False;
182 while (*list != NULL) {
183 TALLOC_CTX *frame = talloc_stackframe();
184 bool ret;
186 ret = token_contains_name(frame, username, domain, sharename,
187 token, *list);
188 TALLOC_FREE(frame);
189 if (ret) {
190 return true;
192 list += 1;
194 return False;
198 * Check whether the user described by "token" has access to share snum.
200 * This looks at "invalid users" and "valid users".
202 * Please note that the user name and share names passed in here mainly for
203 * the substitution routines that expand the parameter values, the decision
204 * whether a user is in the list is done after a lookup_name on the expanded
205 * parameter value, solely based on comparing the SIDs in token.
207 * The other use is the netgroup check when using @group or &group.
210 bool user_ok_token(const char *username, const char *domain,
211 const struct security_token *token, int snum)
213 const struct loadparm_substitution *lp_sub =
214 loadparm_s3_global_substitution();
216 if (lp_invalid_users(snum) != NULL) {
217 if (token_contains_name_in_list(username, domain,
218 lp_servicename(talloc_tos(), lp_sub, snum),
219 token,
220 lp_invalid_users(snum))) {
221 DEBUG(10, ("User %s in 'invalid users'\n", username));
222 return False;
226 if (lp_valid_users(snum) != NULL) {
227 if (!token_contains_name_in_list(username, domain,
228 lp_servicename(talloc_tos(), lp_sub, snum),
229 token,
230 lp_valid_users(snum))) {
231 DEBUG(10, ("User %s not in 'valid users'\n",
232 username));
233 return False;
237 DEBUG(10, ("user_ok_token: share %s is ok for unix user %s\n",
238 lp_servicename(talloc_tos(), lp_sub, snum), username));
240 return True;
244 * Check whether the user described by "token" is restricted to read-only
245 * access on share snum.
247 * This looks at "read list", "write list" and "read only".
249 * Please note that the user name and share names passed in here mainly for
250 * the substitution routines that expand the parameter values, the decision
251 * whether a user is in the list is done after a lookup_name on the expanded
252 * parameter value, solely based on comparing the SIDs in token.
254 * The other use is the netgroup check when using @group or &group.
257 bool is_share_read_only_for_token(const char *username,
258 const char *domain,
259 const struct security_token *token,
260 connection_struct *conn)
262 const struct loadparm_substitution *lp_sub =
263 loadparm_s3_global_substitution();
264 int snum = SNUM(conn);
265 bool result = conn->read_only;
267 if (lp_read_list(snum) != NULL) {
268 if (token_contains_name_in_list(username, domain,
269 lp_servicename(talloc_tos(), lp_sub, snum),
270 token,
271 lp_read_list(snum))) {
272 result = True;
276 if (lp_write_list(snum) != NULL) {
277 if (token_contains_name_in_list(username, domain,
278 lp_servicename(talloc_tos(), lp_sub, snum),
279 token,
280 lp_write_list(snum))) {
281 result = False;
285 DEBUG(10,("is_share_read_only_for_user: share %s is %s for unix user "
286 "%s\n", lp_servicename(talloc_tos(), lp_sub, snum),
287 result ? "read-only" : "read-write", username));
289 return result;