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/>.
21 #include "smbd/smbd.h"
22 #include "smbd/globals.h"
23 #include "../libcli/security/security.h"
24 #include "passdb/lookup_sid.h"
26 #include "source3/lib/substitute.h"
29 * No prefix means direct username
30 * @name means netgroup first, then unix group
31 * &name means netgroup
32 * +name means unix group
33 * + and & may be combined
36 static bool do_group_checks(const char **name
, const char **pattern
)
38 if ((*name
)[0] == '@') {
44 if (((*name
)[0] == '+') && ((*name
)[1] == '&')) {
50 if ((*name
)[0] == '+') {
56 if (((*name
)[0] == '&') && ((*name
)[1] == '+')) {
62 if ((*name
)[0] == '&') {
71 static bool token_contains_name(TALLOC_CTX
*mem_ctx
,
74 const char *sharename
,
75 const struct security_token
*token
,
80 enum lsa_SidType type
;
82 if (username
!= NULL
) {
83 size_t domain_len
= domain
!= NULL
? strlen(domain
) : 0;
85 /* Check if username starts with domain name */
87 const char *sep
= lp_winbind_separator();
88 int cmp
= strncasecmp_m(username
, domain
, domain_len
);
89 if (cmp
== 0 && sep
[0] == username
[domain_len
]) {
90 /* Move after the winbind separator */
96 name
= talloc_sub_basic(mem_ctx
,
97 username
+ domain_len
,
101 if (sharename
!= NULL
) {
102 name
= talloc_string_sub(mem_ctx
, name
, "%S", sharename
);
106 /* This is too security sensitive, better panic than return a
107 * result that might be interpreted in a wrong way. */
108 smb_panic("substitutions failed");
111 if ( string_to_sid( &sid
, name
) ) {
112 DEBUG(5,("token_contains_name: Checking for SID [%s] in token\n", name
));
113 return nt_token_check_sid( &sid
, token
);
116 if (!do_group_checks(&name
, &prefix
)) {
117 if (!lookup_name_smbconf(mem_ctx
, name
, LOOKUP_NAME_ALL
,
118 NULL
, NULL
, &sid
, &type
)) {
119 DEBUG(5, ("lookup_name %s failed\n", name
));
122 if (type
!= SID_NAME_USER
) {
123 DEBUG(5, ("%s is a %s, expected a user\n",
124 name
, sid_type_lookup(type
)));
127 return nt_token_check_sid(&sid
, token
);
130 for (/* initialized above */ ; *prefix
!= '\0'; prefix
++) {
131 if (*prefix
== '+') {
132 if (!lookup_name_smbconf(mem_ctx
, name
,
133 LOOKUP_NAME_ALL
|LOOKUP_NAME_GROUP
,
134 NULL
, NULL
, &sid
, &type
)) {
135 DEBUG(5, ("lookup_name %s failed\n", name
));
138 if ((type
!= SID_NAME_DOM_GRP
) &&
139 (type
!= SID_NAME_ALIAS
) &&
140 (type
!= SID_NAME_WKN_GRP
)) {
141 DEBUG(5, ("%s is a %s, expected a group\n",
142 name
, sid_type_lookup(type
)));
145 if (nt_token_check_sid(&sid
, token
)) {
150 if (*prefix
== '&') {
152 if (user_in_netgroup(mem_ctx
, username
, name
)) {
158 smb_panic("got invalid prefix from do_groups_check");
164 * Check whether a user is contained in the list provided.
166 * Please note that the user name and share names passed in here mainly for
167 * the substitution routines that expand the parameter values, the decision
168 * whether a user is in the list is done after a lookup_name on the expanded
169 * parameter value, solely based on comparing the SIDs in token.
171 * The other use is the netgroup check when using @group or &group.
174 bool token_contains_name_in_list(const char *username
,
176 const char *sharename
,
177 const struct security_token
*token
,
183 while (*list
!= NULL
) {
184 TALLOC_CTX
*frame
= talloc_stackframe();
187 ret
= token_contains_name(frame
, username
, domain
, sharename
,
199 * Check whether the user described by "token" has access to share snum.
201 * This looks at "invalid users" and "valid users".
203 * Please note that the user name and share names passed in here mainly for
204 * the substitution routines that expand the parameter values, the decision
205 * whether a user is in the list is done after a lookup_name on the expanded
206 * parameter value, solely based on comparing the SIDs in token.
208 * The other use is the netgroup check when using @group or &group.
211 bool user_ok_token(const char *username
, const char *domain
,
212 const struct security_token
*token
, int snum
)
214 const struct loadparm_substitution
*lp_sub
=
215 loadparm_s3_global_substitution();
217 if (lp_invalid_users(snum
) != NULL
) {
218 if (token_contains_name_in_list(username
, domain
,
219 lp_servicename(talloc_tos(), lp_sub
, snum
),
221 lp_invalid_users(snum
))) {
222 DEBUG(10, ("User %s in 'invalid users'\n", username
));
227 if (lp_valid_users(snum
) != NULL
) {
228 if (!token_contains_name_in_list(username
, domain
,
229 lp_servicename(talloc_tos(), lp_sub
, snum
),
231 lp_valid_users(snum
))) {
232 DEBUG(10, ("User %s not in 'valid users'\n",
238 DEBUG(10, ("user_ok_token: share %s is ok for unix user %s\n",
239 lp_servicename(talloc_tos(), lp_sub
, snum
), username
));
245 * Check whether the user described by "token" is restricted to read-only
246 * access on share snum.
248 * This looks at "read list", "write list" and "read only".
250 * Please note that the user name and share names passed in here mainly for
251 * the substitution routines that expand the parameter values, the decision
252 * whether a user is in the list is done after a lookup_name on the expanded
253 * parameter value, solely based on comparing the SIDs in token.
255 * The other use is the netgroup check when using @group or &group.
258 bool is_share_read_only_for_token(const char *username
,
260 const struct security_token
*token
,
261 connection_struct
*conn
)
263 const struct loadparm_substitution
*lp_sub
=
264 loadparm_s3_global_substitution();
265 int snum
= SNUM(conn
);
266 bool result
= conn
->read_only
;
268 if (lp_read_list(snum
) != NULL
) {
269 if (token_contains_name_in_list(username
, domain
,
270 lp_servicename(talloc_tos(), lp_sub
, snum
),
272 lp_read_list(snum
))) {
277 if (lp_write_list(snum
) != NULL
) {
278 if (token_contains_name_in_list(username
, domain
,
279 lp_servicename(talloc_tos(), lp_sub
, snum
),
281 lp_write_list(snum
))) {
286 DEBUG(10,("is_share_read_only_for_user: share %s is %s for unix user "
287 "%s\n", lp_servicename(talloc_tos(), lp_sub
, snum
),
288 result
? "read-only" : "read-write", username
));