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 * No prefix means direct username
29 * @name means netgroup first, then unix group
30 * &name means netgroup
31 * +name means unix group
32 * + and & may be combined
35 static bool do_group_checks(const char **name
, const char **pattern
)
37 if ((*name
)[0] == '@') {
43 if (((*name
)[0] == '+') && ((*name
)[1] == '&')) {
49 if ((*name
)[0] == '+') {
55 if (((*name
)[0] == '&') && ((*name
)[1] == '+')) {
61 if ((*name
)[0] == '&') {
70 static bool token_contains_name(TALLOC_CTX
*mem_ctx
,
73 const char *sharename
,
74 const struct security_token
*token
,
79 enum lsa_SidType type
;
81 if (username
!= NULL
) {
82 name
= talloc_sub_basic(mem_ctx
, username
, domain
, name
);
84 if (sharename
!= NULL
) {
85 name
= talloc_string_sub(mem_ctx
, name
, "%S", sharename
);
89 /* This is too security sensitive, better panic than return a
90 * result that might be interpreted in a wrong way. */
91 smb_panic("substitutions failed");
94 /* check to see is we already have a SID */
96 if ( string_to_sid( &sid
, name
) ) {
97 DEBUG(5,("token_contains_name: Checking for SID [%s] in token\n", name
));
98 return nt_token_check_sid( &sid
, token
);
101 if (!do_group_checks(&name
, &prefix
)) {
102 if (!lookup_name_smbconf(mem_ctx
, name
, LOOKUP_NAME_ALL
,
103 NULL
, NULL
, &sid
, &type
)) {
104 DEBUG(5, ("lookup_name %s failed\n", name
));
107 if (type
!= SID_NAME_USER
) {
108 DEBUG(5, ("%s is a %s, expected a user\n",
109 name
, sid_type_lookup(type
)));
112 return nt_token_check_sid(&sid
, token
);
115 for (/* initialized above */ ; *prefix
!= '\0'; prefix
++) {
116 if (*prefix
== '+') {
117 if (!lookup_name_smbconf(mem_ctx
, name
,
118 LOOKUP_NAME_ALL
|LOOKUP_NAME_GROUP
,
119 NULL
, NULL
, &sid
, &type
)) {
120 DEBUG(5, ("lookup_name %s failed\n", name
));
123 if ((type
!= SID_NAME_DOM_GRP
) &&
124 (type
!= SID_NAME_ALIAS
) &&
125 (type
!= SID_NAME_WKN_GRP
)) {
126 DEBUG(5, ("%s is a %s, expected a group\n",
127 name
, sid_type_lookup(type
)));
130 if (nt_token_check_sid(&sid
, token
)) {
135 if (*prefix
== '&') {
137 if (user_in_netgroup(mem_ctx
, username
, name
)) {
143 smb_panic("got invalid prefix from do_groups_check");
149 * Check whether a user is contained in the list provided.
151 * Please note that the user name and share names passed in here mainly for
152 * the substitution routines that expand the parameter values, the decision
153 * whether a user is in the list is done after a lookup_name on the expanded
154 * parameter value, solely based on comparing the SIDs in token.
156 * The other use is the netgroup check when using @group or &group.
159 bool token_contains_name_in_list(const char *username
,
161 const char *sharename
,
162 const struct security_token
*token
,
168 while (*list
!= NULL
) {
169 TALLOC_CTX
*frame
= talloc_stackframe();
172 ret
= token_contains_name(frame
, username
, domain
, sharename
,
184 * Check whether the user described by "token" has access to share snum.
186 * This looks at "invalid users", "valid users" and "only user/username"
188 * Please note that the user name and share names passed in here mainly for
189 * the substitution routines that expand the parameter values, the decision
190 * whether a user is in the list is done after a lookup_name on the expanded
191 * parameter value, solely based on comparing the SIDs in token.
193 * The other use is the netgroup check when using @group or &group.
196 bool user_ok_token(const char *username
, const char *domain
,
197 const struct security_token
*token
, int snum
)
199 if (lp_invalid_users(snum
) != NULL
) {
200 if (token_contains_name_in_list(username
, domain
,
201 lp_servicename(talloc_tos(), snum
),
203 lp_invalid_users(snum
))) {
204 DEBUG(10, ("User %s in 'invalid users'\n", username
));
209 if (lp_valid_users(snum
) != NULL
) {
210 if (!token_contains_name_in_list(username
, domain
,
211 lp_servicename(talloc_tos(), snum
),
213 lp_valid_users(snum
))) {
214 DEBUG(10, ("User %s not in 'valid users'\n",
220 if (lp_onlyuser(snum
)) {
222 list
[0] = lp_username(talloc_tos(), snum
);
224 if ((list
[0] == NULL
) || (*list
[0] == '\0')) {
225 DEBUG(0, ("'only user = yes' and no 'username ='\n"));
228 if (!token_contains_name_in_list(NULL
, domain
,
229 lp_servicename(talloc_tos(), snum
),
231 DEBUG(10, ("%s != 'username'\n", username
));
236 DEBUG(10, ("user_ok_token: share %s is ok for unix user %s\n",
237 lp_servicename(talloc_tos(), snum
), username
));
243 * Check whether the user described by "token" is restricted to read-only
244 * access on share snum.
246 * This looks at "invalid users", "valid users" and "only user/username"
248 * Please note that the user name and share names passed in here mainly for
249 * the substitution routines that expand the parameter values, the decision
250 * whether a user is in the list is done after a lookup_name on the expanded
251 * parameter value, solely based on comparing the SIDs in token.
253 * The other use is the netgroup check when using @group or &group.
256 bool is_share_read_only_for_token(const char *username
,
258 const struct security_token
*token
,
259 connection_struct
*conn
)
261 int snum
= SNUM(conn
);
262 bool result
= conn
->read_only
;
264 if (lp_readlist(snum
) != NULL
) {
265 if (token_contains_name_in_list(username
, domain
,
266 lp_servicename(talloc_tos(), snum
),
268 lp_readlist(snum
))) {
273 if (lp_writelist(snum
) != NULL
) {
274 if (token_contains_name_in_list(username
, domain
,
275 lp_servicename(talloc_tos(), snum
),
277 lp_writelist(snum
))) {
282 DEBUG(10,("is_share_read_only_for_user: share %s is %s for unix user "
283 "%s\n", lp_servicename(talloc_tos(), snum
),
284 result
? "read-only" : "read-write", username
));