summaryrefslogtreecommitdiffstats
path: root/source/smbd/uid.c
blob: 6b05cfda2f4a7aaaf286201052b9816059451e79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/* 
   Unix SMB/Netbios implementation.
   Version 1.9.
   uid/user handling
   Copyright (C) Andrew Tridgell 1992-1998
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"

extern int DEBUGLEVEL;

extern struct current_user current_user;

/****************************************************************************
 Become the user of a connection number.
****************************************************************************/
BOOL become_user(connection_struct *conn, uint16 vuid)
{
	vuser_key key;
	key.pid = getpid();
	key.vuid = vuid;
	return become_userk(conn, &key);
}
/****************************************************************************
 Become the user of a connection number.
****************************************************************************/
BOOL become_userk(connection_struct *conn, const vuser_key *key)
{
	user_struct *vuser = NULL;
	int snum;
	gid_t gid = -1;
	uid_t uid = -1;
	char group_c;
	int ngroups = 0;
	gid_t *groups = NULL;

	if (!conn)
	{
		DEBUG(2,("Connection not open\n"));
		return(False);
	}

	/*
	 * We need a separate check in security=share mode due to vuid
	 * always being UID_FIELD_INVALID. If we don't do this then
	 * in share mode security we are *always* changing uid's between
	 * SMB's - this hurts performance - Badly.
	 */

	if((lp_security() == SEC_SHARE) && (current_user.conn == conn) &&
	   (current_user.uid == conn->uid))
	{
		DEBUG(4,("Skipping become_user - already user\n"));

		return(True);
	}

	vuser = get_valid_user_struct(key);

	if ((current_user.conn == conn) && 
		   (vuser != NULL) &&
		   (current_user.key.vuid == key->vuid) && 
		   (current_user.key.pid == key->pid) && 
		   (current_user.uid == vuser->uid))
	{
		DEBUG(4,("Skipping become_user - already user\n"));
		vuid_free_user_struct(vuser);
		safe_free(vuser);
		return(True);
	}

	unbecome_user();

	snum = SNUM(conn);

	if((vuser != NULL) && !check_vuser_ok(&conn->uid_cache, vuser, snum))
	{
		vuid_free_user_struct(vuser);
		safe_free(vuser);
		return False;
	}

	if (conn->force_user || 
	    lp_security() == SEC_SHARE ||
	    !(vuser) || (vuser->guest)) {
		uid = conn->uid;
		gid = conn->gid;
		groups = conn->groups;
		ngroups = conn->ngroups;
	} else {
		if (!vuser) {
			DEBUG(2,("Invalid vuid used %d\n",key->vuid));
			return(False);
		}
		uid = vuser->uid;
		gid = vuser->gid;
		ngroups = vuser->n_groups;
		groups  = vuser->groups;
	}

	/*
	 * See if we should force group for this service.
	 * If so this overrides any group set in the force
	 * user code.
	 */

	if((group_c = *lp_force_group(snum))) {
		if(group_c == '+') {

			/*
			 * Only force group if the user is a member of
			 * the service group. Check the group memberships for
			 * this user (we already have this) to
			 * see if we should force the group.
			 */

			int i;
			for (i = 0; i < current_user.ngroups; i++) {
				if (current_user.groups[i] == conn->gid) {
					gid = conn->gid;
					break;
				}
			}
		} else {
			gid = conn->gid;
		}
	}
	
	vuid_free_user_struct(vuser);
	safe_free(vuser);

	return become_unix_sec_ctx(key, conn, uid, gid, ngroups, groups);
}

/****************************************************************************
  unbecome the user of a connection number
****************************************************************************/
BOOL unbecome_user(void )
{
  if (!current_user.conn)
    return(False);

  return unbecome_to_initial_uid();
}