Ok here it is my latest work on privileges
[Samba.git] / source / smbd / uid.c
blobd43bf301e8855719550b20fbec8d5856c55c73cf
1 /*
2 Unix SMB/CIFS implementation.
3 uid/user handling
4 Copyright (C) Andrew Tridgell 1992-1998
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.
21 #include "includes.h"
23 /* what user is current? */
24 extern struct current_user current_user;
26 /****************************************************************************
27 Become the guest user without changing the security context stack.
28 ****************************************************************************/
30 BOOL change_to_guest(void)
32 static struct passwd *pass=NULL;
34 if (!pass) {
35 /* Don't need to free() this as its stored in a static */
36 pass = getpwnam_alloc(lp_guestaccount());
37 if (!pass)
38 return(False);
41 #ifdef AIX
42 /* MWW: From AIX FAQ patch to WU-ftpd: call initgroups before
43 setting IDs */
44 initgroups(pass->pw_name, pass->pw_gid);
45 #endif
47 set_sec_ctx(pass->pw_uid, pass->pw_gid, 0, NULL, NULL, NULL);
49 current_user.conn = NULL;
50 current_user.vuid = UID_FIELD_INVALID;
52 passwd_free(&pass);
54 return True;
57 /****************************************************************************
58 Readonly share for this user ?
59 ****************************************************************************/
61 static BOOL is_share_read_only_for_user(connection_struct *conn, user_struct *vuser)
63 char **list;
64 const char *service = lp_servicename(conn->service);
65 BOOL read_only_ret = lp_readonly(conn->service);
67 if (!service)
68 return read_only_ret;
70 str_list_copy(&list, lp_readlist(conn->service));
71 if (list) {
72 if (!str_list_sub_basic(list, vuser->user.smb_name) ) {
73 DEBUG(0, ("is_share_read_only_for_user: ERROR: read list substitution failed\n"));
75 if (!str_list_substitute(list, "%S", service)) {
76 DEBUG(0, ("is_share_read_only_for_user: ERROR: read list service substitution failed\n"));
78 if (user_in_list(vuser->user.unix_name, (const char **)list, vuser->groups, vuser->n_groups)) {
79 read_only_ret = True;
81 str_list_free(&list);
84 str_list_copy(&list, lp_writelist(conn->service));
85 if (list) {
86 if (!str_list_sub_basic(list, vuser->user.smb_name) ) {
87 DEBUG(0, ("is_share_read_only_for_user: ERROR: write list substitution failed\n"));
89 if (!str_list_substitute(list, "%S", service)) {
90 DEBUG(0, ("is_share_read_only_for_user: ERROR: write list service substitution failed\n"));
92 if (user_in_list(vuser->user.unix_name, (const char **)list, vuser->groups, vuser->n_groups)) {
93 read_only_ret = False;
95 str_list_free(&list);
98 DEBUG(10,("is_share_read_only_for_user: share %s is %s for unix user %s\n",
99 service, read_only_ret ? "read-only" : "read-write", vuser->user.unix_name ));
101 return read_only_ret;
104 /*******************************************************************
105 Check if a username is OK.
106 ********************************************************************/
108 static BOOL check_user_ok(connection_struct *conn, user_struct *vuser,int snum)
110 unsigned int i;
111 struct vuid_cache_entry *ent = NULL;
112 BOOL readonly_share;
114 for (i=0;i<conn->vuid_cache.entries && i< VUID_CACHE_SIZE;i++) {
115 if (conn->vuid_cache.array[i].vuid == vuser->vuid) {
116 ent = &conn->vuid_cache.array[i];
117 conn->read_only = ent->read_only;
118 conn->admin_user = ent->admin_user;
119 return(True);
123 if (!user_ok(vuser->user.unix_name,snum, vuser->groups, vuser->n_groups))
124 return(False);
126 readonly_share = is_share_read_only_for_user(conn, vuser);
128 if (!share_access_check(conn, snum, vuser, readonly_share ? FILE_READ_DATA : FILE_WRITE_DATA)) {
129 return False;
132 i = conn->vuid_cache.entries % VUID_CACHE_SIZE;
133 if (conn->vuid_cache.entries < VUID_CACHE_SIZE)
134 conn->vuid_cache.entries++;
136 ent = &conn->vuid_cache.array[i];
137 ent->vuid = vuser->vuid;
138 ent->read_only = readonly_share;
140 if (user_in_list(vuser->user.unix_name ,lp_admin_users(conn->service), vuser->groups, vuser->n_groups)) {
141 ent->admin_user = True;
142 } else {
143 ent->admin_user = False;
146 conn->read_only = ent->read_only;
147 conn->admin_user = ent->admin_user;
149 return(True);
152 /****************************************************************************
153 Become the user of a connection number without changing the security context
154 stack, but modify the currnet_user entries.
155 ****************************************************************************/
157 BOOL change_to_user(connection_struct *conn, uint16 vuid)
159 user_struct *vuser = get_valid_user_struct(vuid);
160 int snum;
161 gid_t gid;
162 uid_t uid;
163 char group_c;
164 BOOL must_free_token_priv = False;
165 NT_USER_TOKEN *token = NULL;
166 PRIVILEGE_SET *privs = NULL;
168 if (!conn) {
169 DEBUG(2,("change_to_user: Connection not open\n"));
170 return(False);
174 * We need a separate check in security=share mode due to vuid
175 * always being UID_FIELD_INVALID. If we don't do this then
176 * in share mode security we are *always* changing uid's between
177 * SMB's - this hurts performance - Badly.
180 if((lp_security() == SEC_SHARE) && (current_user.conn == conn) &&
181 (current_user.uid == conn->uid)) {
182 DEBUG(4,("change_to_user: Skipping user change - already user\n"));
183 return(True);
184 } else if ((current_user.conn == conn) &&
185 (vuser != 0) && (current_user.vuid == vuid) &&
186 (current_user.uid == vuser->uid)) {
187 DEBUG(4,("change_to_user: Skipping user change - already user\n"));
188 return(True);
191 snum = SNUM(conn);
193 if (conn->force_user) /* security = share sets this too */ {
194 uid = conn->uid;
195 gid = conn->gid;
196 current_user.groups = conn->groups;
197 current_user.ngroups = conn->ngroups;
198 token = conn->nt_user_token;
199 privs = conn->privs;
200 } else if ((vuser) && check_user_ok(conn, vuser, snum)) {
201 uid = conn->admin_user ? 0 : vuser->uid;
202 gid = vuser->gid;
203 current_user.ngroups = vuser->n_groups;
204 current_user.groups = vuser->groups;
205 token = vuser->nt_user_token;
206 privs = vuser->privs;
207 } else {
208 DEBUG(2,("change_to_user: Invalid vuid used %d or vuid not permitted access to share.\n",vuid));
209 return False;
213 * See if we should force group for this service.
214 * If so this overrides any group set in the force
215 * user code.
218 if((group_c = *lp_force_group(snum))) {
219 BOOL is_guest = False;
221 if(group_c == '+') {
224 * Only force group if the user is a member of
225 * the service group. Check the group memberships for
226 * this user (we already have this) to
227 * see if we should force the group.
230 int i;
231 for (i = 0; i < current_user.ngroups; i++) {
232 if (current_user.groups[i] == conn->gid) {
233 gid = conn->gid;
234 break;
237 } else {
238 gid = conn->gid;
242 * We've changed the group list in the token - we must
243 * re-create it.
246 if (vuser && vuser->guest)
247 is_guest = True;
249 token = create_nt_token(uid, gid, current_user.ngroups, current_user.groups, is_guest);
250 if (!token) {
251 DEBUG(1, ("change_to_user: create_nt_token failed!\n"));
252 return False;
254 pdb_get_privilege_set(token, privs);
255 must_free_token_priv = True;
258 set_sec_ctx(uid, gid, current_user.ngroups, current_user.groups, token, privs);
261 * Free the new token (as set_sec_ctx copies it).
264 if (must_free_token_priv) {
265 delete_nt_token(&token);
266 destroy_privilege(&privs);
269 current_user.conn = conn;
270 current_user.vuid = vuid;
272 DEBUG(5,("change_to_user uid=(%d,%d) gid=(%d,%d)\n",
273 (int)getuid(),(int)geteuid(),(int)getgid(),(int)getegid()));
275 return(True);
278 /****************************************************************************
279 Go back to being root without changing the security context stack,
280 but modify the current_user entries.
281 ****************************************************************************/
283 BOOL change_to_root_user(void)
285 set_root_sec_ctx();
287 DEBUG(5,("change_to_root_user: now uid=(%d,%d) gid=(%d,%d)\n",
288 (int)getuid(),(int)geteuid(),(int)getgid(),(int)getegid()));
290 current_user.conn = NULL;
291 current_user.vuid = UID_FIELD_INVALID;
293 return(True);
296 /****************************************************************************
297 Become the user of an authenticated connected named pipe.
298 When this is called we are currently running as the connection
299 user. Doesn't modify current_user.
300 ****************************************************************************/
302 BOOL become_authenticated_pipe_user(pipes_struct *p)
304 if (!push_sec_ctx())
305 return False;
307 set_sec_ctx(p->pipe_user.uid, p->pipe_user.gid,
308 p->pipe_user.ngroups, p->pipe_user.groups, p->pipe_user.nt_user_token, p->pipe_user.privs);
310 return True;
313 /****************************************************************************
314 Unbecome the user of an authenticated connected named pipe.
315 When this is called we are running as the authenticated pipe
316 user and need to go back to being the connection user. Doesn't modify
317 current_user.
318 ****************************************************************************/
320 BOOL unbecome_authenticated_pipe_user(void)
322 return pop_sec_ctx();
325 /****************************************************************************
326 Utility functions used by become_xxx/unbecome_xxx.
327 ****************************************************************************/
329 struct conn_ctx {
330 connection_struct *conn;
331 uint16 vuid;
334 /* A stack of current_user connection contexts. */
336 static struct conn_ctx conn_ctx_stack[MAX_SEC_CTX_DEPTH];
337 static int conn_ctx_stack_ndx;
339 static void push_conn_ctx(void)
341 struct conn_ctx *ctx_p;
343 /* Check we don't overflow our stack */
345 if (conn_ctx_stack_ndx == MAX_SEC_CTX_DEPTH) {
346 DEBUG(0, ("Connection context stack overflow!\n"));
347 smb_panic("Connection context stack overflow!\n");
350 /* Store previous user context */
351 ctx_p = &conn_ctx_stack[conn_ctx_stack_ndx];
353 ctx_p->conn = current_user.conn;
354 ctx_p->vuid = current_user.vuid;
356 DEBUG(3, ("push_conn_ctx(%u) : conn_ctx_stack_ndx = %d\n",
357 (unsigned int)ctx_p->vuid, conn_ctx_stack_ndx ));
359 conn_ctx_stack_ndx++;
362 static void pop_conn_ctx(void)
364 struct conn_ctx *ctx_p;
366 /* Check for stack underflow. */
368 if (conn_ctx_stack_ndx == 0) {
369 DEBUG(0, ("Connection context stack underflow!\n"));
370 smb_panic("Connection context stack underflow!\n");
373 conn_ctx_stack_ndx--;
374 ctx_p = &conn_ctx_stack[conn_ctx_stack_ndx];
376 current_user.conn = ctx_p->conn;
377 current_user.vuid = ctx_p->vuid;
379 ctx_p->conn = NULL;
380 ctx_p->vuid = UID_FIELD_INVALID;
383 /****************************************************************************
384 Temporarily become a root user. Must match with unbecome_root(). Saves and
385 restores the connection context.
386 ****************************************************************************/
388 void become_root(void)
390 push_sec_ctx();
391 push_conn_ctx();
392 set_root_sec_ctx();
395 /* Unbecome the root user */
397 void unbecome_root(void)
399 pop_sec_ctx();
400 pop_conn_ctx();
403 /****************************************************************************
404 Push the current security context then force a change via change_to_user().
405 Saves and restores the connection context.
406 ****************************************************************************/
408 BOOL become_user(connection_struct *conn, uint16 vuid)
410 if (!push_sec_ctx())
411 return False;
413 push_conn_ctx();
415 if (!change_to_user(conn, vuid)) {
416 pop_sec_ctx();
417 pop_conn_ctx();
418 return False;
421 return True;
424 BOOL unbecome_user(void)
426 pop_sec_ctx();
427 pop_conn_ctx();
428 return True;