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"
28 * We dropped NIS support in 2021, but need to keep configs working.
30 * TODO FIXME: Remove me in future
33 static bool do_group_checks(const char **name
, const char **pattern
)
35 if ((*name
)[0] == '@') {
41 if (((*name
)[0] == '+') && ((*name
)[1] == '&')) {
47 if ((*name
)[0] == '+') {
53 if (((*name
)[0] == '&') && ((*name
)[1] == '+')) {
59 if ((*name
)[0] == '&') {
68 static bool token_contains_name(TALLOC_CTX
*mem_ctx
,
71 const char *sharename
,
72 const struct security_token
*token
,
77 enum lsa_SidType type
;
79 if (username
!= NULL
) {
80 size_t domain_len
= domain
!= NULL
? strlen(domain
) : 0;
82 /* Check if username starts with domain name */
84 const char *sep
= lp_winbind_separator();
85 int cmp
= strncasecmp_m(username
, domain
, domain_len
);
86 if (cmp
== 0 && sep
[0] == username
[domain_len
]) {
87 /* Move after the winbind separator */
93 name
= talloc_sub_basic(mem_ctx
,
94 username
+ domain_len
,
98 if (sharename
!= NULL
) {
99 name
= talloc_string_sub(mem_ctx
, name
, "%S", sharename
);
103 /* This is too security sensitive, better panic than return a
104 * result that might be interpreted in a wrong way. */
105 smb_panic("substitutions failed");
108 if ( string_to_sid( &sid
, name
) ) {
109 DEBUG(5,("token_contains_name: Checking for SID [%s] in token\n", name
));
110 return nt_token_check_sid( &sid
, token
);
113 if (!do_group_checks(&name
, &prefix
)) {
114 if (!lookup_name_smbconf(mem_ctx
, name
, LOOKUP_NAME_ALL
,
115 NULL
, NULL
, &sid
, &type
)) {
116 DEBUG(5, ("lookup_name %s failed\n", name
));
119 if (type
!= SID_NAME_USER
) {
120 DEBUG(5, ("%s is a %s, expected a user\n",
121 name
, sid_type_lookup(type
)));
124 return nt_token_check_sid(&sid
, token
);
127 for (/* initialized above */ ; *prefix
!= '\0'; prefix
++) {
128 if (*prefix
== '+') {
129 if (!lookup_name_smbconf(mem_ctx
, name
,
130 LOOKUP_NAME_ALL
|LOOKUP_NAME_GROUP
,
131 NULL
, NULL
, &sid
, &type
)) {
132 DEBUG(5, ("lookup_name %s failed\n", name
));
135 if ((type
!= SID_NAME_DOM_GRP
) &&
136 (type
!= SID_NAME_ALIAS
) &&
137 (type
!= SID_NAME_WKN_GRP
)) {
138 DEBUG(5, ("%s is a %s, expected a group\n",
139 name
, sid_type_lookup(type
)));
142 if (nt_token_check_sid(&sid
, token
)) {
147 if (*prefix
== '&') {
150 smb_panic("got invalid prefix from do_groups_check");
156 * Check whether a user is contained in the list provided.
158 * Please note that the user name and share names passed in here mainly for
159 * the substitution routines that expand the parameter values, the decision
160 * whether a user is in the list is done after a lookup_name on the expanded
161 * parameter value, solely based on comparing the SIDs in token.
163 * The other use is the netgroup check when using @group or &group.
166 bool token_contains_name_in_list(const char *username
,
168 const char *sharename
,
169 const struct security_token
*token
,
175 while (*list
!= NULL
) {
176 TALLOC_CTX
*frame
= talloc_stackframe();
179 ret
= token_contains_name(frame
, username
, domain
, sharename
,
191 * Check whether the user described by "token" has access to share snum.
193 * This looks at "invalid users" and "valid users".
195 * Please note that the user name and share names passed in here mainly for
196 * the substitution routines that expand the parameter values, the decision
197 * whether a user is in the list is done after a lookup_name on the expanded
198 * parameter value, solely based on comparing the SIDs in token.
200 * The other use is the netgroup check when using @group or &group.
203 bool user_ok_token(const char *username
, const char *domain
,
204 const struct security_token
*token
, int snum
)
206 const struct loadparm_substitution
*lp_sub
=
207 loadparm_s3_global_substitution();
209 if (lp_invalid_users(snum
) != NULL
) {
210 if (token_contains_name_in_list(username
, domain
,
211 lp_servicename(talloc_tos(), lp_sub
, snum
),
213 lp_invalid_users(snum
))) {
214 DEBUG(10, ("User %s in 'invalid users'\n", username
));
219 if (lp_valid_users(snum
) != NULL
) {
220 if (!token_contains_name_in_list(username
, domain
,
221 lp_servicename(talloc_tos(), lp_sub
, snum
),
223 lp_valid_users(snum
))) {
224 DEBUG(10, ("User %s not in 'valid users'\n",
230 DEBUG(10, ("user_ok_token: share %s is ok for unix user %s\n",
231 lp_servicename(talloc_tos(), lp_sub
, snum
), username
));
237 * Check whether the user described by "token" is restricted to read-only
238 * access on share snum.
240 * This looks at "read list", "write list" and "read only".
242 * Please note that the user name and share names passed in here mainly for
243 * the substitution routines that expand the parameter values, the decision
244 * whether a user is in the list is done after a lookup_name on the expanded
245 * parameter value, solely based on comparing the SIDs in token.
247 * The other use is the netgroup check when using @group or &group.
250 bool is_share_read_only_for_token(const char *username
,
252 const struct security_token
*token
,
253 connection_struct
*conn
)
255 const struct loadparm_substitution
*lp_sub
=
256 loadparm_s3_global_substitution();
257 int snum
= SNUM(conn
);
258 bool result
= conn
->read_only
;
260 if (lp_read_list(snum
) != NULL
) {
261 if (token_contains_name_in_list(username
, domain
,
262 lp_servicename(talloc_tos(), lp_sub
, snum
),
264 lp_read_list(snum
))) {
269 if (lp_write_list(snum
) != NULL
) {
270 if (token_contains_name_in_list(username
, domain
,
271 lp_servicename(talloc_tos(), lp_sub
, snum
),
273 lp_write_list(snum
))) {
278 DEBUG(10,("is_share_read_only_for_user: share %s is %s for unix user "
279 "%s\n", lp_servicename(talloc_tos(), lp_sub
, snum
),
280 result
? "read-only" : "read-write", username
));