Cope with old CIFSFS clients that use SMBunlink to remove
[ira/wip.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/globals.h"
22
23 /*
24  * No prefix means direct username
25  * @name means netgroup first, then unix group
26  * &name means netgroup
27  * +name means unix group
28  * + and & may be combined
29  */
30
31 static bool do_group_checks(const char **name, const char **pattern)
32 {
33         if ((*name)[0] == '@') {
34                 *pattern = "&+";
35                 *name += 1;
36                 return True;
37         }
38
39         if (((*name)[0] == '+') && ((*name)[1] == '&')) {
40                 *pattern = "+&";
41                 *name += 2;
42                 return True;
43         }
44
45         if ((*name)[0] == '+') {
46                 *pattern = "+";
47                 *name += 1;
48                 return True;
49         }
50
51         if (((*name)[0] == '&') && ((*name)[1] == '+')) {
52                 *pattern = "&+";
53                 *name += 2;
54                 return True;
55         }
56
57         if ((*name)[0] == '&') {
58                 *pattern = "&";
59                 *name += 1;
60                 return True;
61         }
62
63         return False;
64 }
65
66 static bool token_contains_name(TALLOC_CTX *mem_ctx,
67                                 const char *username,
68                                 const char *domain,
69                                 const char *sharename,
70                                 const struct nt_user_token *token,
71                                 const char *name)
72 {
73         const char *prefix;
74         DOM_SID sid;
75         enum lsa_SidType type;
76         struct smbd_server_connection *sconn = smbd_server_conn;
77
78         if (username != NULL) {
79                 name = talloc_sub_basic(mem_ctx, username, domain, name);
80         }
81         if (sharename != NULL) {
82                 name = talloc_string_sub(mem_ctx, name, "%S", sharename);
83         }
84
85         if (name == NULL) {
86                 /* This is too security sensitive, better panic than return a
87                  * result that might be interpreted in a wrong way. */
88                 smb_panic("substitutions failed");
89         }
90         
91         /* check to see is we already have a SID */
92
93         if ( string_to_sid( &sid, name ) ) {
94                 DEBUG(5,("token_contains_name: Checking for SID [%s] in token\n", name));
95                 return nt_token_check_sid( &sid, token );
96         }
97
98         if (!do_group_checks(&name, &prefix)) {
99                 if (!lookup_name_smbconf(mem_ctx, name, LOOKUP_NAME_ALL,
100                                  NULL, NULL, &sid, &type)) {
101                         DEBUG(5, ("lookup_name %s failed\n", name));
102                         return False;
103                 }
104                 if (type != SID_NAME_USER) {
105                         DEBUG(5, ("%s is a %s, expected a user\n",
106                                   name, sid_type_lookup(type)));
107                         return False;
108                 }
109                 return nt_token_check_sid(&sid, token);
110         }
111
112         for (/* initialized above */ ; *prefix != '\0'; prefix++) {
113                 if (*prefix == '+') {
114                         if (!lookup_name_smbconf(mem_ctx, name,
115                                          LOOKUP_NAME_ALL|LOOKUP_NAME_GROUP,
116                                          NULL, NULL, &sid, &type)) {
117                                 DEBUG(5, ("lookup_name %s failed\n", name));
118                                 return False;
119                         }
120                         if ((type != SID_NAME_DOM_GRP) &&
121                             (type != SID_NAME_ALIAS) &&
122                             (type != SID_NAME_WKN_GRP)) {
123                                 DEBUG(5, ("%s is a %s, expected a group\n",
124                                           name, sid_type_lookup(type)));
125                                 return False;
126                         }
127                         if (nt_token_check_sid(&sid, token)) {
128                                 return True;
129                         }
130                         continue;
131                 }
132                 if (*prefix == '&') {
133                         if (username) {
134                                 if (user_in_netgroup(sconn, username, name)) {
135                                         return True;
136                                 }
137                         }
138                         continue;
139                 }
140                 smb_panic("got invalid prefix from do_groups_check");
141         }
142         return False;
143 }
144
145 /*
146  * Check whether a user is contained in the list provided.
147  *
148  * Please note that the user name and share names passed in here mainly for
149  * the substitution routines that expand the parameter values, the decision
150  * whether a user is in the list is done after a lookup_name on the expanded
151  * parameter value, solely based on comparing the SIDs in token.
152  *
153  * The other use is the netgroup check when using @group or &group.
154  */
155
156 bool token_contains_name_in_list(const char *username,
157                                  const char *domain,
158                                  const char *sharename,
159                                  const struct nt_user_token *token,
160                                  const char **list)
161 {
162         TALLOC_CTX *mem_ctx;
163
164         if (list == NULL) {
165                 return False;
166         }
167
168         if ( (mem_ctx = talloc_new(NULL)) == NULL ) {
169                 smb_panic("talloc_new failed");
170         }
171
172         while (*list != NULL) {
173                 if (token_contains_name(mem_ctx, username, domain, sharename,
174                                         token, *list)) {
175                         TALLOC_FREE(mem_ctx);
176                         return True;
177                 }
178                 list += 1;
179         }
180
181         TALLOC_FREE(mem_ctx);
182         return False;
183 }
184
185 /*
186  * Check whether the user described by "token" has access to share snum.
187  *
188  * This looks at "invalid users", "valid users" and "only user/username"
189  *
190  * Please note that the user name and share names passed in here mainly for
191  * the substitution routines that expand the parameter values, the decision
192  * whether a user is in the list is done after a lookup_name on the expanded
193  * parameter value, solely based on comparing the SIDs in token.
194  *
195  * The other use is the netgroup check when using @group or &group.
196  */
197
198 bool user_ok_token(const char *username, const char *domain,
199                    const struct nt_user_token *token, int snum)
200 {
201         if (lp_invalid_users(snum) != NULL) {
202                 if (token_contains_name_in_list(username, domain,
203                                                 lp_servicename(snum),
204                                                 token,
205                                                 lp_invalid_users(snum))) {
206                         DEBUG(10, ("User %s in 'invalid users'\n", username));
207                         return False;
208                 }
209         }
210
211         if (lp_valid_users(snum) != NULL) {
212                 if (!token_contains_name_in_list(username, domain,
213                                                  lp_servicename(snum), token,
214                                                  lp_valid_users(snum))) {
215                         DEBUG(10, ("User %s not in 'valid users'\n",
216                                    username));
217                         return False;
218                 }
219         }
220
221         if (lp_onlyuser(snum)) {
222                 const char *list[2];
223                 list[0] = lp_username(snum);
224                 list[1] = NULL;
225                 if ((list[0] == NULL) || (*list[0] == '\0')) {
226                         DEBUG(0, ("'only user = yes' and no 'username ='\n"));
227                         return False;
228                 }
229                 if (!token_contains_name_in_list(NULL, domain,
230                                                  lp_servicename(snum),
231                                                  token, list)) {
232                         DEBUG(10, ("%s != 'username'\n", username));
233                         return False;
234                 }
235         }
236
237         DEBUG(10, ("user_ok_token: share %s is ok for unix user %s\n",
238                    lp_servicename(snum), username));
239
240         return True;
241 }
242
243 /*
244  * Check whether the user described by "token" is restricted to read-only
245  * access on share snum.
246  *
247  * This looks at "invalid users", "valid users" and "only user/username"
248  *
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.
253  *
254  * The other use is the netgroup check when using @group or &group.
255  */
256
257 bool is_share_read_only_for_token(const char *username,
258                                   const char *domain,
259                                   const struct nt_user_token *token,
260                                   connection_struct *conn)
261 {
262         int snum = SNUM(conn);
263         bool result = conn->read_only;
264
265         if (lp_readlist(snum) != NULL) {
266                 if (token_contains_name_in_list(username, domain,
267                                                 lp_servicename(snum), token,
268                                                 lp_readlist(snum))) {
269                         result = True;
270                 }
271         }
272
273         if (lp_writelist(snum) != NULL) {
274                 if (token_contains_name_in_list(username, domain,
275                                                 lp_servicename(snum), token,
276                                                 lp_writelist(snum))) {
277                         result = False;
278                 }
279         }
280
281         DEBUG(10,("is_share_read_only_for_user: share %s is %s for unix user "
282                   "%s\n", lp_servicename(snum),
283                   result ? "read-only" : "read-write", username));
284
285         return result;
286 }