2 Unix SMB/CIFS implementation.
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 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"
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)
34 pass
= getpwnam_alloc(talloc_autofree_context(), lp_guestaccount());
40 /* MWW: From AIX FAQ patch to WU-ftpd: call initgroups before
42 initgroups(pass
->pw_name
, pass
->pw_gid
);
45 set_sec_ctx(pass
->pw_uid
, pass
->pw_gid
, 0, NULL
, NULL
);
47 current_user
.conn
= NULL
;
48 current_user
.vuid
= UID_FIELD_INVALID
;
55 /****************************************************************************
56 talloc free the conn->server_info if not used in the vuid cache.
57 ****************************************************************************/
59 static void free_conn_server_info_if_unused(connection_struct
*conn
)
63 for (i
= 0; i
< VUID_CACHE_SIZE
; i
++) {
64 struct vuid_cache_entry
*ent
;
65 ent
= &conn
->vuid_cache
.array
[i
];
66 if (ent
->vuid
!= UID_FIELD_INVALID
&&
67 conn
->server_info
== ent
->server_info
) {
71 /* Not used, safe to free. */
72 TALLOC_FREE(conn
->server_info
);
75 /*******************************************************************
76 Check if a username is OK.
78 This sets up conn->server_info with a copy related to this vuser that
79 later code can then mess with.
80 ********************************************************************/
82 static bool check_user_ok(connection_struct
*conn
,
84 const struct auth_serversupplied_info
*server_info
,
87 bool valid_vuid
= (vuid
!= UID_FIELD_INVALID
);
93 struct vuid_cache_entry
*ent
;
95 for (i
=0; i
<VUID_CACHE_SIZE
; i
++) {
96 ent
= &conn
->vuid_cache
.array
[i
];
97 if (ent
->vuid
== vuid
) {
98 free_conn_server_info_if_unused(conn
);
99 conn
->server_info
= ent
->server_info
;
100 conn
->read_only
= ent
->read_only
;
106 if (!user_ok_token(server_info
->unix_name
,
107 server_info
->info3
->base
.domain
.string
,
108 server_info
->ptok
, snum
))
111 readonly_share
= is_share_read_only_for_token(
112 server_info
->unix_name
,
113 server_info
->info3
->base
.domain
.string
,
117 if (!readonly_share
&&
118 !share_access_check(server_info
->ptok
, lp_servicename(snum
),
120 /* smb.conf allows r/w, but the security descriptor denies
121 * write. Fall back to looking at readonly. */
122 readonly_share
= True
;
123 DEBUG(5,("falling back to read-only access-evaluation due to "
124 "security descriptor\n"));
127 if (!share_access_check(server_info
->ptok
, lp_servicename(snum
),
129 FILE_READ_DATA
: FILE_WRITE_DATA
)) {
133 admin_user
= token_contains_name_in_list(
134 server_info
->unix_name
,
135 server_info
->info3
->base
.domain
.string
,
136 NULL
, server_info
->ptok
, lp_admin_users(snum
));
139 struct vuid_cache_entry
*ent
=
140 &conn
->vuid_cache
.array
[conn
->vuid_cache
.next_entry
];
142 conn
->vuid_cache
.next_entry
=
143 (conn
->vuid_cache
.next_entry
+ 1) % VUID_CACHE_SIZE
;
145 TALLOC_FREE(ent
->server_info
);
148 * If force_user was set, all server_info's are based on the same
149 * username-based faked one.
152 ent
->server_info
= copy_serverinfo(
153 conn
, conn
->force_user
? conn
->server_info
: server_info
);
155 if (ent
->server_info
== NULL
) {
156 ent
->vuid
= UID_FIELD_INVALID
;
161 ent
->read_only
= readonly_share
;
162 free_conn_server_info_if_unused(conn
);
163 conn
->server_info
= ent
->server_info
;
166 conn
->read_only
= readonly_share
;
168 DEBUG(2,("check_user_ok: user %s is an admin user. "
169 "Setting uid as %d\n",
170 conn
->server_info
->unix_name
,
171 sec_initial_uid() ));
172 conn
->server_info
->utok
.uid
= sec_initial_uid();
178 /****************************************************************************
179 Clear a vuid out of the connection's vuid cache
180 This is only called on SMBulogoff.
181 ****************************************************************************/
183 void conn_clear_vuid_cache(connection_struct
*conn
, uint16_t vuid
)
187 for (i
=0; i
<VUID_CACHE_SIZE
; i
++) {
188 struct vuid_cache_entry
*ent
;
190 ent
= &conn
->vuid_cache
.array
[i
];
192 if (ent
->vuid
== vuid
) {
193 ent
->vuid
= UID_FIELD_INVALID
;
195 * We need to keep conn->server_info around
196 * if it's equal to ent->server_info as a SMBulogoff
197 * is often followed by a SMBtdis (with an invalid
198 * vuid). The debug code (or regular code in
199 * vfs_full_audit) wants to refer to the
200 * conn->server_info pointer to print debug
201 * statements. Theoretically this is a bug,
202 * as once the vuid is gone the server_info
203 * on the conn struct isn't valid any more,
204 * but there's enough code that assumes
205 * conn->server_info is never null that
206 * it's easier to hold onto the old pointer
207 * until we get a new sessionsetupX.
208 * As everything is hung off the
209 * conn pointer as a talloc context we're not
210 * leaking memory here. See bug #6315. JRA.
212 if (conn
->server_info
== ent
->server_info
) {
213 ent
->server_info
= NULL
;
215 TALLOC_FREE(ent
->server_info
);
217 ent
->read_only
= False
;
222 /****************************************************************************
223 Become the user of a connection number without changing the security context
224 stack, but modify the current_user entries.
225 ****************************************************************************/
227 bool change_to_user(connection_struct
*conn
, uint16 vuid
)
229 const struct auth_serversupplied_info
*server_info
= NULL
;
230 user_struct
*vuser
= get_valid_user_struct(conn
->sconn
, vuid
);
236 gid_t
*group_list
= NULL
;
239 DEBUG(2,("change_to_user: Connection not open\n"));
244 * We need a separate check in security=share mode due to vuid
245 * always being UID_FIELD_INVALID. If we don't do this then
246 * in share mode security we are *always* changing uid's between
247 * SMB's - this hurts performance - Badly.
250 if((lp_security() == SEC_SHARE
) && (current_user
.conn
== conn
) &&
251 (current_user
.ut
.uid
== conn
->server_info
->utok
.uid
)) {
252 DEBUG(4,("change_to_user: Skipping user change - already "
255 } else if ((current_user
.conn
== conn
) &&
256 (vuser
!= NULL
) && (current_user
.vuid
== vuid
) &&
257 (current_user
.ut
.uid
== vuser
->server_info
->utok
.uid
)) {
258 DEBUG(4,("change_to_user: Skipping user change - already "
265 server_info
= vuser
? vuser
->server_info
: conn
->server_info
;
268 /* Invalid vuid sent - even with security = share. */
269 DEBUG(2,("change_to_user: Invalid vuid %d used on "
270 "share %s.\n",vuid
, lp_servicename(snum
) ));
274 if (!check_user_ok(conn
, vuid
, server_info
, snum
)) {
275 DEBUG(2,("change_to_user: SMB user %s (unix user %s, vuid %d) "
276 "not permitted access to share %s.\n",
277 server_info
->sanitized_username
,
278 server_info
->unix_name
, vuid
,
279 lp_servicename(snum
)));
283 /* security = share sets force_user. */
284 if (!conn
->force_user
&& !vuser
) {
285 DEBUG(2,("change_to_user: Invalid vuid used %d in accessing "
286 "share %s.\n",vuid
, lp_servicename(snum
) ));
291 * conn->server_info is now correctly set up with a copy we can mess
292 * with for force_group etc.
295 uid
= conn
->server_info
->utok
.uid
;
296 gid
= conn
->server_info
->utok
.gid
;
297 num_groups
= conn
->server_info
->utok
.ngroups
;
298 group_list
= conn
->server_info
->utok
.groups
;
301 * See if we should force group for this service.
302 * If so this overrides any group set in the force
306 if((group_c
= *lp_force_group(snum
))) {
308 SMB_ASSERT(conn
->force_group_gid
!= (gid_t
)-1);
313 * Only force group if the user is a member of
314 * the service group. Check the group memberships for
315 * this user (we already have this) to
316 * see if we should force the group.
320 for (i
= 0; i
< num_groups
; i
++) {
322 == conn
->force_group_gid
) {
323 conn
->server_info
->utok
.gid
=
324 conn
->force_group_gid
;
325 gid
= conn
->force_group_gid
;
326 gid_to_sid(&conn
->server_info
->ptok
327 ->user_sids
[1], gid
);
332 conn
->server_info
->utok
.gid
= conn
->force_group_gid
;
333 gid
= conn
->force_group_gid
;
334 gid_to_sid(&conn
->server_info
->ptok
->user_sids
[1],
339 /* Now set current_user since we will immediately also call
342 current_user
.ut
.ngroups
= num_groups
;
343 current_user
.ut
.groups
= group_list
;
345 set_sec_ctx(uid
, gid
, current_user
.ut
.ngroups
, current_user
.ut
.groups
,
346 conn
->server_info
->ptok
);
348 current_user
.conn
= conn
;
349 current_user
.vuid
= vuid
;
351 DEBUG(5,("change_to_user uid=(%d,%d) gid=(%d,%d)\n",
352 (int)getuid(),(int)geteuid(),(int)getgid(),(int)getegid()));
357 /****************************************************************************
358 Go back to being root without changing the security context stack,
359 but modify the current_user entries.
360 ****************************************************************************/
362 bool change_to_root_user(void)
366 DEBUG(5,("change_to_root_user: now uid=(%d,%d) gid=(%d,%d)\n",
367 (int)getuid(),(int)geteuid(),(int)getgid(),(int)getegid()));
369 current_user
.conn
= NULL
;
370 current_user
.vuid
= UID_FIELD_INVALID
;
375 /****************************************************************************
376 Become the user of an authenticated connected named pipe.
377 When this is called we are currently running as the connection
378 user. Doesn't modify current_user.
379 ****************************************************************************/
381 bool become_authenticated_pipe_user(pipes_struct
*p
)
386 set_sec_ctx(p
->server_info
->utok
.uid
, p
->server_info
->utok
.gid
,
387 p
->server_info
->utok
.ngroups
, p
->server_info
->utok
.groups
,
388 p
->server_info
->ptok
);
393 /****************************************************************************
394 Unbecome the user of an authenticated connected named pipe.
395 When this is called we are running as the authenticated pipe
396 user and need to go back to being the connection user. Doesn't modify
398 ****************************************************************************/
400 bool unbecome_authenticated_pipe_user(void)
402 return pop_sec_ctx();
405 /****************************************************************************
406 Utility functions used by become_xxx/unbecome_xxx.
407 ****************************************************************************/
409 static void push_conn_ctx(void)
411 struct conn_ctx
*ctx_p
;
413 /* Check we don't overflow our stack */
415 if (conn_ctx_stack_ndx
== MAX_SEC_CTX_DEPTH
) {
416 DEBUG(0, ("Connection context stack overflow!\n"));
417 smb_panic("Connection context stack overflow!\n");
420 /* Store previous user context */
421 ctx_p
= &conn_ctx_stack
[conn_ctx_stack_ndx
];
423 ctx_p
->conn
= current_user
.conn
;
424 ctx_p
->vuid
= current_user
.vuid
;
426 DEBUG(3, ("push_conn_ctx(%u) : conn_ctx_stack_ndx = %d\n",
427 (unsigned int)ctx_p
->vuid
, conn_ctx_stack_ndx
));
429 conn_ctx_stack_ndx
++;
432 static void pop_conn_ctx(void)
434 struct conn_ctx
*ctx_p
;
436 /* Check for stack underflow. */
438 if (conn_ctx_stack_ndx
== 0) {
439 DEBUG(0, ("Connection context stack underflow!\n"));
440 smb_panic("Connection context stack underflow!\n");
443 conn_ctx_stack_ndx
--;
444 ctx_p
= &conn_ctx_stack
[conn_ctx_stack_ndx
];
446 current_user
.conn
= ctx_p
->conn
;
447 current_user
.vuid
= ctx_p
->vuid
;
450 ctx_p
->vuid
= UID_FIELD_INVALID
;
453 /****************************************************************************
454 Temporarily become a root user. Must match with unbecome_root(). Saves and
455 restores the connection context.
456 ****************************************************************************/
458 void become_root(void)
461 * no good way to handle push_sec_ctx() failing without changing
462 * the prototype of become_root()
464 if (!push_sec_ctx()) {
465 smb_panic("become_root: push_sec_ctx failed");
471 /* Unbecome the root user */
473 void unbecome_root(void)
479 /****************************************************************************
480 Push the current security context then force a change via change_to_user().
481 Saves and restores the connection context.
482 ****************************************************************************/
484 bool become_user(connection_struct
*conn
, uint16 vuid
)
491 if (!change_to_user(conn
, vuid
)) {
500 bool unbecome_user(void)
507 /****************************************************************************
508 Return the current user we are running effectively as on this connection.
509 I'd like to make this return conn->server_info->utok.uid, but become_root()
510 doesn't alter this value.
511 ****************************************************************************/
513 uid_t
get_current_uid(connection_struct
*conn
)
515 return current_user
.ut
.uid
;
518 /****************************************************************************
519 Return the current group we are running effectively as on this connection.
520 I'd like to make this return conn->server_info->utok.gid, but become_root()
521 doesn't alter this value.
522 ****************************************************************************/
524 gid_t
get_current_gid(connection_struct
*conn
)
526 return current_user
.ut
.gid
;
529 /****************************************************************************
530 Return the UNIX token we are running effectively as on this connection.
531 I'd like to make this return &conn->server_info->utok, but become_root()
532 doesn't alter this value.
533 ****************************************************************************/
535 const UNIX_USER_TOKEN
*get_current_utok(connection_struct
*conn
)
537 return ¤t_user
.ut
;
540 const NT_USER_TOKEN
*get_current_nttok(connection_struct
*conn
)
542 return current_user
.nt_user_token
;
545 uint16_t get_current_vuid(connection_struct
*conn
)
547 return current_user
.vuid
;