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/globals.h"
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
31 static bool do_group_checks(const char **name
, const char **pattern
)
33 if ((*name
)[0] == '@') {
39 if (((*name
)[0] == '+') && ((*name
)[1] == '&')) {
45 if ((*name
)[0] == '+') {
51 if (((*name
)[0] == '&') && ((*name
)[1] == '+')) {
57 if ((*name
)[0] == '&') {
66 static bool token_contains_name(TALLOC_CTX
*mem_ctx
,
69 const char *sharename
,
70 const struct nt_user_token
*token
,
75 enum lsa_SidType type
;
76 struct smbd_server_connection
*sconn
= smbd_server_conn
;
78 if (username
!= NULL
) {
79 name
= talloc_sub_basic(mem_ctx
, username
, domain
, name
);
81 if (sharename
!= NULL
) {
82 name
= talloc_string_sub(mem_ctx
, name
, "%S", sharename
);
86 /* This is too security sensitive, better panic than return a
87 * result that might be interpreted in a wrong way. */
88 smb_panic("substitutions failed");
91 /* check to see is we already have a SID */
93 if ( string_to_sid( &sid
, name
) ) {
94 DEBUG(5,("token_contains_name: Checking for SID [%s] in token\n", name
));
95 return nt_token_check_sid( &sid
, token
);
98 if (!do_group_checks(&name
, &prefix
)) {
99 if (!lookup_name_smbconf(mem_ctx
, name
, LOOKUP_NAME_ALL
,
100 NULL
, NULL
, &sid
, &type
)) {
101 DEBUG(5, ("lookup_name %s failed\n", name
));
104 if (type
!= SID_NAME_USER
) {
105 DEBUG(5, ("%s is a %s, expected a user\n",
106 name
, sid_type_lookup(type
)));
109 return nt_token_check_sid(&sid
, token
);
112 for (/* initialized above */ ; *prefix
!= '\0'; prefix
++) {
113 if (*prefix
== '+') {
114 if (!lookup_name_smbconf(mem_ctx
, name
,
115 LOOKUP_NAME_ALL
|LOOKUP_NAME_GROUP
,
116 NULL
, NULL
, &sid
, &type
)) {
117 DEBUG(5, ("lookup_name %s failed\n", name
));
120 if ((type
!= SID_NAME_DOM_GRP
) &&
121 (type
!= SID_NAME_ALIAS
) &&
122 (type
!= SID_NAME_WKN_GRP
)) {
123 DEBUG(5, ("%s is a %s, expected a group\n",
124 name
, sid_type_lookup(type
)));
127 if (nt_token_check_sid(&sid
, token
)) {
132 if (*prefix
== '&') {
133 if (user_in_netgroup(sconn
, username
, name
)) {
138 smb_panic("got invalid prefix from do_groups_check");
144 * Check whether a user is contained in the list provided.
146 * Please note that the user name and share names passed in here mainly for
147 * the substitution routines that expand the parameter values, the decision
148 * whether a user is in the list is done after a lookup_name on the expanded
149 * parameter value, solely based on comparing the SIDs in token.
151 * The other use is the netgroup check when using @group or &group.
154 bool token_contains_name_in_list(const char *username
,
156 const char *sharename
,
157 const struct nt_user_token
*token
,
166 if ( (mem_ctx
= talloc_new(NULL
)) == NULL
) {
167 smb_panic("talloc_new failed");
170 while (*list
!= NULL
) {
171 if (token_contains_name(mem_ctx
, username
, domain
, sharename
,
173 TALLOC_FREE(mem_ctx
);
179 TALLOC_FREE(mem_ctx
);
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 nt_user_token
*token
, int snum
)
199 if (lp_invalid_users(snum
) != NULL
) {
200 if (token_contains_name_in_list(username
, domain
,
201 lp_servicename(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(snum
), token
,
212 lp_valid_users(snum
))) {
213 DEBUG(10, ("User %s not in 'valid users'\n",
219 if (lp_onlyuser(snum
)) {
221 list
[0] = lp_username(snum
);
223 if ((list
[0] == NULL
) || (*list
[0] == '\0')) {
224 DEBUG(0, ("'only user = yes' and no 'username ='\n"));
227 if (!token_contains_name_in_list(NULL
, domain
,
228 lp_servicename(snum
),
230 DEBUG(10, ("%s != 'username'\n", username
));
235 DEBUG(10, ("user_ok_token: share %s is ok for unix user %s\n",
236 lp_servicename(snum
), username
));
242 * Check whether the user described by "token" is restricted to read-only
243 * access on share snum.
245 * This looks at "invalid users", "valid users" and "only user/username"
247 * Please note that the user name and share names passed in here mainly for
248 * the substitution routines that expand the parameter values, the decision
249 * whether a user is in the list is done after a lookup_name on the expanded
250 * parameter value, solely based on comparing the SIDs in token.
252 * The other use is the netgroup check when using @group or &group.
255 bool is_share_read_only_for_token(const char *username
,
257 const struct nt_user_token
*token
,
258 connection_struct
*conn
)
260 int snum
= SNUM(conn
);
261 bool result
= conn
->read_only
;
263 if (lp_readlist(snum
) != NULL
) {
264 if (token_contains_name_in_list(username
, domain
,
265 lp_servicename(snum
), token
,
266 lp_readlist(snum
))) {
271 if (lp_writelist(snum
) != NULL
) {
272 if (token_contains_name_in_list(username
, domain
,
273 lp_servicename(snum
), token
,
274 lp_writelist(snum
))) {
279 DEBUG(10,("is_share_read_only_for_user: share %s is %s for unix user "
280 "%s\n", lp_servicename(snum
),
281 result
? "read-only" : "read-write", username
));