r14075: * add support for long variable names in smb.conf in the form of %(....)
[amitay/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         /* check to see is we already have a SID */
90
91         if ( string_to_sid( &sid, name ) ) {
92                 DEBUG(5,("token_contains_name: Checking for SID [%s] in token\n", name));
93                 return nt_token_check_sid( &sid, token );
94         }
95
96         if (!do_group_checks(&name, &prefix)) {
97                 if (!lookup_name(mem_ctx, name, LOOKUP_NAME_ALL,
98                                  NULL, NULL, &sid, &type)) {
99                         DEBUG(5, ("lookup_name %s failed\n", name));
100                         return False;
101                 }
102                 if (type != SID_NAME_USER) {
103                         DEBUG(5, ("%s is a %s, expected a user\n",
104                                   name, sid_type_lookup(type)));
105                         return False;
106                 }
107                 return nt_token_check_sid(&sid, token);
108         }
109
110         for (/* initialized above */ ; *prefix != '\0'; prefix++) {
111                 if (*prefix == '+') {
112                         if (!lookup_name(mem_ctx, name,
113                                          LOOKUP_NAME_ALL|LOOKUP_NAME_GROUP,
114                                          NULL, NULL, &sid, &type)) {
115                                 DEBUG(5, ("lookup_name %s failed\n", name));
116                                 return False;
117                         }
118                         if ((type != SID_NAME_DOM_GRP) &&
119                             (type != SID_NAME_ALIAS) &&
120                             (type != SID_NAME_WKN_GRP)) {
121                                 DEBUG(5, ("%s is a %s, expected a group\n",
122                                           name, sid_type_lookup(type)));
123                                 return False;
124                         }
125                         if (nt_token_check_sid(&sid, token)) {
126                                 return True;
127                         }
128                         continue;
129                 }
130                 if (*prefix == '&') {
131                         if (user_in_netgroup(username, name)) {
132                                 return True;
133                         }
134                         continue;
135                 }
136                 smb_panic("got invalid prefix from do_groups_check\n");
137         }
138         return False;
139 }
140
141 /*
142  * Check whether a user is contained in the list provided.
143  *
144  * Please note that the user name and share names passed in here mainly for
145  * the substitution routines that expand the parameter values, the decision
146  * whether a user is in the list is done after a lookup_name on the expanded
147  * parameter value, solely based on comparing the SIDs in token.
148  *
149  * The other use is the netgroup check when using @group or &group.
150  */
151
152 BOOL token_contains_name_in_list(const char *username,
153                                  const char *sharename,
154                                  const struct nt_user_token *token,
155                                  const char **list)
156 {
157         TALLOC_CTX *mem_ctx;
158
159         if (list == NULL) {
160                 return False;
161         }
162
163         if ( (mem_ctx = talloc_new(NULL)) == NULL ) {
164                 smb_panic("talloc_new failed\n");
165         }
166
167         while (*list != NULL) {
168                 if (token_contains_name(mem_ctx, username, sharename,token, *list)) {
169                         TALLOC_FREE(mem_ctx);
170                         return True;
171                 }
172                 list += 1;
173         }
174
175         TALLOC_FREE(mem_ctx);
176         return False;
177 }
178
179 /*
180  * Check whether the user described by "token" has access to share snum.
181  *
182  * This looks at "invalid users", "valid users" and "only user/username"
183  *
184  * Please note that the user name and share names passed in here mainly for
185  * the substitution routines that expand the parameter values, the decision
186  * whether a user is in the list is done after a lookup_name on the expanded
187  * parameter value, solely based on comparing the SIDs in token.
188  *
189  * The other use is the netgroup check when using @group or &group.
190  */
191
192 BOOL user_ok_token(const char *username, struct nt_user_token *token, int snum)
193 {
194         if (lp_invalid_users(snum) != NULL) {
195                 if (token_contains_name_in_list(username, lp_servicename(snum),
196                                                 token,
197                                                 lp_invalid_users(snum))) {
198                         DEBUG(10, ("User %s in 'invalid users'\n", username));
199                         return False;
200                 }
201         }
202
203         if (lp_valid_users(snum) != NULL) {
204                 if (!token_contains_name_in_list(username,
205                                                  lp_servicename(snum), token,
206                                                  lp_valid_users(snum))) {
207                         DEBUG(10, ("User %s no in 'valid users'\n", username));
208                         return False;
209                 }
210         }
211
212         if (lp_onlyuser(snum)) {
213                 const char *list[2];
214                 list[0] = lp_username(snum);
215                 list[1] = NULL;
216                 if (!token_contains_name_in_list(NULL, lp_servicename(snum),
217                                                  token, list)) {
218                         DEBUG(10, ("%s != 'username'\n", username));
219                         return False;
220                 }
221         }
222
223         DEBUG(10, ("user_ok_token: share %s is ok for unix user %s\n",
224                    lp_servicename(snum), username));
225
226         return True;
227 }
228
229 /*
230  * Check whether the user described by "token" is restricted to read-only
231  * access on share snum.
232  *
233  * This looks at "invalid users", "valid users" and "only user/username"
234  *
235  * Please note that the user name and share names passed in here mainly for
236  * the substitution routines that expand the parameter values, the decision
237  * whether a user is in the list is done after a lookup_name on the expanded
238  * parameter value, solely based on comparing the SIDs in token.
239  *
240  * The other use is the netgroup check when using @group or &group.
241  */
242
243 BOOL is_share_read_only_for_token(const char *username,
244                                   struct nt_user_token *token, int snum)
245 {
246         BOOL result = lp_readonly(snum);
247
248         if (lp_readlist(snum) != NULL) {
249                 if (token_contains_name_in_list(username,
250                                                 lp_servicename(snum), token,
251                                                 lp_readlist(snum))) {
252                         result = True;
253                 }
254         }
255
256         if (lp_writelist(snum) != NULL) {
257                 if (token_contains_name_in_list(username,
258                                                 lp_servicename(snum), token,
259                                                 lp_writelist(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(snum),
266                   result ? "read-only" : "read-write", username));
267
268         return result;
269 }