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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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 extern userdom_struct current_user_info;
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,
70 const char *sharename,
71 const struct nt_user_token *token,
76 enum lsa_SidType type;
78 if (username != NULL) {
79 name = talloc_sub_basic(mem_ctx, username,
80 current_user_info.domain, name);
82 if (sharename != NULL) {
83 name = talloc_string_sub(mem_ctx, name, "%S", sharename);
87 /* This is too security sensitive, better panic than return a
88 * result that might be interpreted in a wrong way. */
89 smb_panic("substitutions failed");
92 /* check to see is we already have a SID */
94 if ( string_to_sid( &sid, name ) ) {
95 DEBUG(5,("token_contains_name: Checking for SID [%s] in token\n", name));
96 return nt_token_check_sid( &sid, token );
99 if (!do_group_checks(&name, &prefix)) {
100 if (!lookup_name_smbconf(mem_ctx, name, LOOKUP_NAME_ALL,
101 NULL, NULL, &sid, &type)) {
102 DEBUG(5, ("lookup_name %s failed\n", name));
105 if (type != SID_NAME_USER) {
106 DEBUG(5, ("%s is a %s, expected a user\n",
107 name, sid_type_lookup(type)));
110 return nt_token_check_sid(&sid, token);
113 for (/* initialized above */ ; *prefix != '\0'; prefix++) {
114 if (*prefix == '+') {
115 if (!lookup_name_smbconf(mem_ctx, name,
116 LOOKUP_NAME_ALL|LOOKUP_NAME_GROUP,
117 NULL, NULL, &sid, &type)) {
118 DEBUG(5, ("lookup_name %s failed\n", name));
121 if ((type != SID_NAME_DOM_GRP) &&
122 (type != SID_NAME_ALIAS) &&
123 (type != SID_NAME_WKN_GRP)) {
124 DEBUG(5, ("%s is a %s, expected a group\n",
125 name, sid_type_lookup(type)));
128 if (nt_token_check_sid(&sid, token)) {
133 if (*prefix == '&') {
134 if (user_in_netgroup(username, name)) {
139 smb_panic("got invalid prefix from do_groups_check");
145 * Check whether a user is contained in the list provided.
147 * Please note that the user name and share names passed in here mainly for
148 * the substitution routines that expand the parameter values, the decision
149 * whether a user is in the list is done after a lookup_name on the expanded
150 * parameter value, solely based on comparing the SIDs in token.
152 * The other use is the netgroup check when using @group or &group.
155 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, sharename,token, *list)) {
172 TALLOC_FREE(mem_ctx);
178 TALLOC_FREE(mem_ctx);
183 * Check whether the user described by "token" has access to share snum.
185 * This looks at "invalid users", "valid users" and "only user/username"
187 * Please note that the user name and share names passed in here mainly for
188 * the substitution routines that expand the parameter values, the decision
189 * whether a user is in the list is done after a lookup_name on the expanded
190 * parameter value, solely based on comparing the SIDs in token.
192 * The other use is the netgroup check when using @group or &group.
195 BOOL user_ok_token(const char *username, struct nt_user_token *token, int snum)
197 if (lp_invalid_users(snum) != NULL) {
198 if (token_contains_name_in_list(username, lp_servicename(snum),
200 lp_invalid_users(snum))) {
201 DEBUG(10, ("User %s in 'invalid users'\n", username));
206 if (lp_valid_users(snum) != NULL) {
207 if (!token_contains_name_in_list(username,
208 lp_servicename(snum), token,
209 lp_valid_users(snum))) {
210 DEBUG(10, ("User %s not in 'valid users'\n",
216 if (lp_onlyuser(snum)) {
218 list[0] = lp_username(snum);
220 if ((list[0] == NULL) || (*list[0] == '\0')) {
221 DEBUG(0, ("'only user = yes' and no 'username ='\n"));
224 if (!token_contains_name_in_list(NULL, lp_servicename(snum),
226 DEBUG(10, ("%s != 'username'\n", username));
231 DEBUG(10, ("user_ok_token: share %s is ok for unix user %s\n",
232 lp_servicename(snum), username));
238 * Check whether the user described by "token" is restricted to read-only
239 * access on share snum.
241 * This looks at "invalid users", "valid users" and "only user/username"
243 * Please note that the user name and share names passed in here mainly for
244 * the substitution routines that expand the parameter values, the decision
245 * whether a user is in the list is done after a lookup_name on the expanded
246 * parameter value, solely based on comparing the SIDs in token.
248 * The other use is the netgroup check when using @group or &group.
251 BOOL is_share_read_only_for_token(const char *username,
252 struct nt_user_token *token, int snum)
254 BOOL result = lp_readonly(snum);
256 if (lp_readlist(snum) != NULL) {
257 if (token_contains_name_in_list(username,
258 lp_servicename(snum), token,
259 lp_readlist(snum))) {
264 if (lp_writelist(snum) != NULL) {
265 if (token_contains_name_in_list(username,
266 lp_servicename(snum), token,
267 lp_writelist(snum))) {
272 DEBUG(10,("is_share_read_only_for_user: share %s is %s for unix user "
273 "%s\n", lp_servicename(snum),
274 result ? "read-only" : "read-write", username));