s3:cleanupd: use MSG_SMB_BRL_VALIDATE to signal cleanupd unclean process shutdown
[vlendec/samba-autobuild/.git] / source3 / smbd / share_access.c
1 /*
2    Unix SMB/CIFS implementation.
3    Check access based on valid users, read list and friends
4    Copyright (C) Volker Lendecke 2005
5
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.
10
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.
15
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/>.
18 */
19
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"
26
27 /*
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
33  */
34
35 static bool do_group_checks(const char **name, const char **pattern)
36 {
37         if ((*name)[0] == '@') {
38                 *pattern = "&+";
39                 *name += 1;
40                 return True;
41         }
42
43         if (((*name)[0] == '+') && ((*name)[1] == '&')) {
44                 *pattern = "+&";
45                 *name += 2;
46                 return True;
47         }
48
49         if ((*name)[0] == '+') {
50                 *pattern = "+";
51                 *name += 1;
52                 return True;
53         }
54
55         if (((*name)[0] == '&') && ((*name)[1] == '+')) {
56                 *pattern = "&+";
57                 *name += 2;
58                 return True;
59         }
60
61         if ((*name)[0] == '&') {
62                 *pattern = "&";
63                 *name += 1;
64                 return True;
65         }
66
67         return False;
68 }
69
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)
76 {
77         const char *prefix;
78         struct dom_sid sid;
79         enum lsa_SidType type;
80
81         if (username != NULL) {
82                 name = talloc_sub_basic(mem_ctx, username, domain, name);
83         }
84         if (sharename != NULL) {
85                 name = talloc_string_sub(mem_ctx, name, "%S", sharename);
86         }
87
88         if (name == NULL) {
89                 /* This is too security sensitive, better panic than return a
90                  * result that might be interpreted in a wrong way. */
91                 smb_panic("substitutions failed");
92         }
93
94         if ( string_to_sid( &sid, name ) ) {
95                 DEBUG(5,("token_contains_name: Checking for SID [%s] in token\n", name));
96                 return nt_token_check_sid( &sid, token );
97         }
98
99         if (!do_group_checks(&name, &prefix)) {
100                 if (!lookup_name_smbconf(mem_ctx, name, LOOKUP_NAME_ALL,
101                                  NULL, NULL, &sid, &type)) {
102                         DEBUG(5, ("lookup_name %s failed\n", name));
103                         return False;
104                 }
105                 if (type != SID_NAME_USER) {
106                         DEBUG(5, ("%s is a %s, expected a user\n",
107                                   name, sid_type_lookup(type)));
108                         return False;
109                 }
110                 return nt_token_check_sid(&sid, token);
111         }
112
113         for (/* initialized above */ ; *prefix != '\0'; prefix++) {
114                 if (*prefix == '+') {
115                         if (!lookup_name_smbconf(mem_ctx, name,
116                                          LOOKUP_NAME_ALL|LOOKUP_NAME_GROUP,
117                                          NULL, NULL, &sid, &type)) {
118                                 DEBUG(5, ("lookup_name %s failed\n", name));
119                                 return False;
120                         }
121                         if ((type != SID_NAME_DOM_GRP) &&
122                             (type != SID_NAME_ALIAS) &&
123                             (type != SID_NAME_WKN_GRP)) {
124                                 DEBUG(5, ("%s is a %s, expected a group\n",
125                                           name, sid_type_lookup(type)));
126                                 return False;
127                         }
128                         if (nt_token_check_sid(&sid, token)) {
129                                 return True;
130                         }
131                         continue;
132                 }
133                 if (*prefix == '&') {
134                         if (username) {
135                                 if (user_in_netgroup(mem_ctx, username, name)) {
136                                         return True;
137                                 }
138                         }
139                         continue;
140                 }
141                 smb_panic("got invalid prefix from do_groups_check");
142         }
143         return False;
144 }
145
146 /*
147  * Check whether a user is contained in the list provided.
148  *
149  * Please note that the user name and share names passed in here mainly for
150  * the substitution routines that expand the parameter values, the decision
151  * whether a user is in the list is done after a lookup_name on the expanded
152  * parameter value, solely based on comparing the SIDs in token.
153  *
154  * The other use is the netgroup check when using @group or &group.
155  */
156
157 bool token_contains_name_in_list(const char *username,
158                                  const char *domain,
159                                  const char *sharename,
160                                  const struct security_token *token,
161                                  const char **list)
162 {
163         if (list == NULL) {
164                 return False;
165         }
166         while (*list != NULL) {
167                 TALLOC_CTX *frame = talloc_stackframe();
168                 bool ret;
169
170                 ret = token_contains_name(frame, username, domain, sharename,
171                                           token, *list);
172                 TALLOC_FREE(frame);
173                 if (ret) {
174                         return true;
175                 }
176                 list += 1;
177         }
178         return False;
179 }
180
181 /*
182  * Check whether the user described by "token" has access to share snum.
183  *
184  * This looks at "invalid users" and "valid users".
185  *
186  * Please note that the user name and share names passed in here mainly for
187  * the substitution routines that expand the parameter values, the decision
188  * whether a user is in the list is done after a lookup_name on the expanded
189  * parameter value, solely based on comparing the SIDs in token.
190  *
191  * The other use is the netgroup check when using @group or &group.
192  */
193
194 bool user_ok_token(const char *username, const char *domain,
195                    const struct security_token *token, int snum)
196 {
197         if (lp_invalid_users(snum) != NULL) {
198                 if (token_contains_name_in_list(username, domain,
199                                                 lp_servicename(talloc_tos(), snum),
200                                                 token,
201                                                 lp_invalid_users(snum))) {
202                         DEBUG(10, ("User %s in 'invalid users'\n", username));
203                         return False;
204                 }
205         }
206
207         if (lp_valid_users(snum) != NULL) {
208                 if (!token_contains_name_in_list(username, domain,
209                                                  lp_servicename(talloc_tos(), snum),
210                                                  token,
211                                                  lp_valid_users(snum))) {
212                         DEBUG(10, ("User %s not in 'valid users'\n",
213                                    username));
214                         return False;
215                 }
216         }
217
218         DEBUG(10, ("user_ok_token: share %s is ok for unix user %s\n",
219                    lp_servicename(talloc_tos(), snum), username));
220
221         return True;
222 }
223
224 /*
225  * Check whether the user described by "token" is restricted to read-only
226  * access on share snum.
227  *
228  * This looks at "read list", "write list" and "read only".
229  *
230  * Please note that the user name and share names passed in here mainly for
231  * the substitution routines that expand the parameter values, the decision
232  * whether a user is in the list is done after a lookup_name on the expanded
233  * parameter value, solely based on comparing the SIDs in token.
234  *
235  * The other use is the netgroup check when using @group or &group.
236  */
237
238 bool is_share_read_only_for_token(const char *username,
239                                   const char *domain,
240                                   const struct security_token *token,
241                                   connection_struct *conn)
242 {
243         int snum = SNUM(conn);
244         bool result = conn->read_only;
245
246         if (lp_read_list(snum) != NULL) {
247                 if (token_contains_name_in_list(username, domain,
248                                                 lp_servicename(talloc_tos(), snum),
249                                                 token,
250                                                 lp_read_list(snum))) {
251                         result = True;
252                 }
253         }
254
255         if (lp_write_list(snum) != NULL) {
256                 if (token_contains_name_in_list(username, domain,
257                                                 lp_servicename(talloc_tos(), snum),
258                                                 token,
259                                                 lp_write_list(snum))) {
260                         result = False;
261                 }
262         }
263
264         DEBUG(10,("is_share_read_only_for_user: share %s is %s for unix user "
265                   "%s\n", lp_servicename(talloc_tos(), snum),
266                   result ? "read-only" : "read-write", username));
267
268         return result;
269 }