r13571: Replace all calls to talloc_free() with thye TALLOC_FREE()
[kai/samba.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 2 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, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.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 *sharename,
69                                 const struct nt_user_token *token,
70                                 const char *name)
71 {
72         const char *prefix;
73         DOM_SID sid;
74         enum SID_NAME_USE type;
75
76         if (username != NULL) {
77                 name = talloc_sub_basic(mem_ctx, username, name);
78         }
79         if (sharename != NULL) {
80                 name = talloc_string_sub(mem_ctx, name, "%S", sharename);
81         }
82
83         if (name == NULL) {
84                 /* This is too security sensitive, better panic than return a
85                  * result that might be interpreted in a wrong way. */
86                 smb_panic("substitutions failed\n");
87         }
88
89         if (!do_group_checks(&name, &prefix)) {
90                 if (!lookup_name(mem_ctx, name, LOOKUP_NAME_ALL,
91                                  NULL, NULL, &sid, &type)) {
92                         DEBUG(5, ("lookup_name %s failed\n", name));
93                         return False;
94                 }
95                 if (type != SID_NAME_USER) {
96                         DEBUG(5, ("%s is a %s, expected a user\n",
97                                   name, sid_type_lookup(type)));
98                         return False;
99                 }
100                 return nt_token_check_sid(&sid, token);
101         }
102
103         for (/* initialized above */ ; *prefix != '\0'; prefix++) {
104                 if (*prefix == '+') {
105                         if (!lookup_name(mem_ctx, name,
106                                          LOOKUP_NAME_ALL|LOOKUP_NAME_GROUP,
107                                          NULL, NULL, &sid, &type)) {
108                                 DEBUG(5, ("lookup_name %s failed\n", name));
109                                 return False;
110                         }
111                         if ((type != SID_NAME_DOM_GRP) &&
112                             (type != SID_NAME_ALIAS) &&
113                             (type != SID_NAME_WKN_GRP)) {
114                                 DEBUG(5, ("%s is a %s, expected a group\n",
115                                           name, sid_type_lookup(type)));
116                                 return False;
117                         }
118                         if (nt_token_check_sid(&sid, token)) {
119                                 return True;
120                         }
121                         continue;
122                 }
123                 if (*prefix == '&') {
124                         if (user_in_netgroup(username, name)) {
125                                 return True;
126                         }
127                         continue;
128                 }
129                 smb_panic("got invalid prefix from do_groups_check\n");
130         }
131         return False;
132 }
133
134 /*
135  * Check whether a user is contained in the list provided.
136  *
137  * Please note that the user name and share names passed in here mainly for
138  * the substitution routines that expand the parameter values, the decision
139  * whether a user is in the list is done after a lookup_name on the expanded
140  * parameter value, solely based on comparing the SIDs in token.
141  *
142  * The other use is the netgroup check when using @group or &group.
143  */
144
145 BOOL token_contains_name_in_list(const char *username,
146                                  const char *sharename,
147                                  const struct nt_user_token *token,
148                                  const char **list)
149 {
150         TALLOC_CTX *mem_ctx;
151
152         if (list == NULL) {
153                 return False;
154         }
155
156         mem_ctx = talloc_new(NULL);
157         if (mem_ctx == NULL) {
158                 smb_panic("talloc_new failed\n");
159         }
160
161         while (*list != NULL) {
162                 if (token_contains_name(mem_ctx, username, sharename,
163                                         token, *list)) {
164                         TALLOC_FREE(mem_ctx);
165                         return True;
166                 }
167                 list += 1;
168         }
169
170         TALLOC_FREE(mem_ctx);
171         return False;
172 }
173
174 /*
175  * Check whether the user described by "token" has access to share snum.
176  *
177  * This looks at "invalid users", "valid users" and "only user/username"
178  *
179  * Please note that the user name and share names passed in here mainly for
180  * the substitution routines that expand the parameter values, the decision
181  * whether a user is in the list is done after a lookup_name on the expanded
182  * parameter value, solely based on comparing the SIDs in token.
183  *
184  * The other use is the netgroup check when using @group or &group.
185  */
186
187 BOOL user_ok_token(const char *username, struct nt_user_token *token, int snum)
188 {
189         if (lp_invalid_users(snum) != NULL) {
190                 if (token_contains_name_in_list(username, lp_servicename(snum),
191                                                 token,
192                                                 lp_invalid_users(snum))) {
193                         DEBUG(10, ("User %s in 'invalid users'\n", username));
194                         return False;
195                 }
196         }
197
198         if (lp_valid_users(snum) != NULL) {
199                 if (!token_contains_name_in_list(username,
200                                                  lp_servicename(snum), token,
201                                                  lp_valid_users(snum))) {
202                         DEBUG(10, ("User %s no in 'valid users'\n", username));
203                         return False;
204                 }
205         }
206
207         if (lp_onlyuser(snum)) {
208                 const char *list[2];
209                 list[0] = lp_username(snum);
210                 list[1] = NULL;
211                 if (!token_contains_name_in_list(NULL, lp_servicename(snum),
212                                                  token, list)) {
213                         DEBUG(10, ("%s != 'username'\n", 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(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 "invalid users", "valid users" and "only user/username"
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                                   struct nt_user_token *token, int snum)
240 {
241         BOOL result = lp_readonly(snum);
242
243         if (lp_readlist(snum) != NULL) {
244                 if (token_contains_name_in_list(username,
245                                                 lp_servicename(snum), token,
246                                                 lp_readlist(snum))) {
247                         result = True;
248                 }
249         }
250
251         if (lp_writelist(snum) != NULL) {
252                 if (token_contains_name_in_list(username,
253                                                 lp_servicename(snum), token,
254                                                 lp_writelist(snum))) {
255                         result = False;
256                 }
257         }
258
259         DEBUG(10,("is_share_read_only_for_user: share %s is %s for unix user "
260                   "%s\n", lp_servicename(snum),
261                   result ? "read-only" : "read-write", username));
262
263         return result;
264 }