Revert "s3:smbd: set req->smb2req->compat_chain_fsp in file_fsp()"
[Samba/gebeck_regimport.git] / source3 / smbd / password.c
blobe8b48c464d85e8f42b332ca9cf3a1fd99096e597
1 /*
2 Unix SMB/CIFS implementation.
3 Password and authentication handling
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Jeremy Allison 2007.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "system/passwd.h"
23 #include "smbd/smbd.h"
24 #include "smbd/globals.h"
25 #include "../librpc/gen_ndr/netlogon.h"
26 #include "auth.h"
27 #include "../libcli/security/security.h"
29 /* Fix up prototypes for OSX 10.4, where they're missing */
30 #ifndef HAVE_SETNETGRENT_PROTOTYPE
31 extern int setnetgrent(const char* netgroup);
32 #endif
33 #ifndef HAVE_GETNETGRENT_PROTOTYPE
34 extern int getnetgrent(char **host, char **user, char **domain);
35 #endif
36 #ifndef HAVE_ENDNETGRENT_PROTOTYPE
37 extern void endnetgrent(void);
38 #endif
40 enum server_allocated_state { SERVER_ALLOCATED_REQUIRED_YES,
41 SERVER_ALLOCATED_REQUIRED_NO,
42 SERVER_ALLOCATED_REQUIRED_ANY};
44 static struct user_struct *get_valid_user_struct_internal(
45 struct smbd_server_connection *sconn,
46 uint64_t vuid,
47 enum server_allocated_state server_allocated)
49 struct user_struct *usp;
50 int count=0;
52 if (vuid == UID_FIELD_INVALID)
53 return NULL;
55 usp=sconn->users;
56 for (;usp;usp=usp->next,count++) {
57 if (vuid == usp->vuid) {
58 switch (server_allocated) {
59 case SERVER_ALLOCATED_REQUIRED_YES:
60 if (usp->session_info == NULL) {
61 continue;
63 break;
64 case SERVER_ALLOCATED_REQUIRED_NO:
65 if (usp->session_info != NULL) {
66 continue;
68 case SERVER_ALLOCATED_REQUIRED_ANY:
69 break;
71 if (count > 10) {
72 DLIST_PROMOTE(sconn->users, usp);
74 return usp;
78 return NULL;
81 /****************************************************************************
82 Check if a uid has been validated, and return an pointer to the user_struct
83 if it has. NULL if not. vuid is biased by an offset. This allows us to
84 tell random client vuid's (normally zero) from valid vuids.
85 ****************************************************************************/
87 struct user_struct *get_valid_user_struct(struct smbd_server_connection *sconn,
88 uint64_t vuid)
90 return get_valid_user_struct_internal(sconn, vuid,
91 SERVER_ALLOCATED_REQUIRED_YES);
94 bool is_partial_auth_vuid(struct smbd_server_connection *sconn, uint64_t vuid)
96 return (get_partial_auth_user_struct(sconn, vuid) != NULL);
99 /****************************************************************************
100 Get the user struct of a partial NTLMSSP login
101 ****************************************************************************/
103 struct user_struct *get_partial_auth_user_struct(struct smbd_server_connection *sconn,
104 uint64_t vuid)
106 return get_valid_user_struct_internal(sconn, vuid,
107 SERVER_ALLOCATED_REQUIRED_NO);
110 /****************************************************************************
111 Invalidate a uid.
112 ****************************************************************************/
114 void invalidate_vuid(struct smbd_server_connection *sconn, uint64_t vuid)
116 struct user_struct *vuser = NULL;
118 vuser = get_valid_user_struct_internal(sconn, vuid,
119 SERVER_ALLOCATED_REQUIRED_ANY);
120 if (vuser == NULL) {
121 return;
124 session_yield(vuser);
126 if (vuser->gensec_security) {
127 TALLOC_FREE(vuser->gensec_security);
130 DLIST_REMOVE(sconn->users, vuser);
131 SMB_ASSERT(sconn->num_users > 0);
132 sconn->num_users--;
134 /* clear the vuid from the 'cache' on each connection, and
135 from the vuid 'owner' of connections */
136 conn_clear_vuid_caches(sconn, vuid);
138 TALLOC_FREE(vuser);
141 /****************************************************************************
142 Invalidate all vuid entries for this process.
143 ****************************************************************************/
145 void invalidate_all_vuids(struct smbd_server_connection *sconn)
147 if (sconn->using_smb2) {
148 return;
151 while (sconn->users != NULL) {
152 invalidate_vuid(sconn, sconn->users->vuid);
156 static void increment_next_vuid(uint16_t *vuid)
158 *vuid += 1;
160 /* Check for vuid wrap. */
161 if (*vuid == UID_FIELD_INVALID) {
162 *vuid = VUID_OFFSET;
166 /****************************************************
167 Create a new partial auth user struct.
168 *****************************************************/
170 uint64_t register_initial_vuid(struct smbd_server_connection *sconn)
172 struct user_struct *vuser;
174 /* Limit allowed vuids to 16bits - VUID_OFFSET. */
175 if (sconn->num_users >= 0xFFFF-VUID_OFFSET) {
176 return UID_FIELD_INVALID;
179 if((vuser = talloc_zero(NULL, struct user_struct)) == NULL) {
180 DEBUG(0,("register_initial_vuid: "
181 "Failed to talloc users struct!\n"));
182 return UID_FIELD_INVALID;
185 /* Allocate a free vuid. Yes this is a linear search... */
186 while( get_valid_user_struct_internal(sconn,
187 sconn->smb1.sessions.next_vuid,
188 SERVER_ALLOCATED_REQUIRED_ANY) != NULL ) {
189 increment_next_vuid(&sconn->smb1.sessions.next_vuid);
192 DEBUG(10,("register_initial_vuid: allocated vuid = %u\n",
193 (unsigned int)sconn->smb1.sessions.next_vuid ));
195 vuser->vuid = sconn->smb1.sessions.next_vuid;
198 * This happens in an unfinished NTLMSSP session setup. We
199 * need to allocate a vuid between the first and second calls
200 * to NTLMSSP.
202 increment_next_vuid(&sconn->smb1.sessions.next_vuid);
204 sconn->num_users++;
205 DLIST_ADD(sconn->users, vuser);
207 return vuser->vuid;
210 int register_homes_share(const char *username)
212 int result;
213 struct passwd *pwd;
215 result = lp_servicenumber(username);
216 if (result != -1) {
217 DEBUG(3, ("Using static (or previously created) service for "
218 "user '%s'; path = '%s'\n", username,
219 lp_pathname(result)));
220 return result;
223 pwd = Get_Pwnam_alloc(talloc_tos(), username);
225 if ((pwd == NULL) || (pwd->pw_dir[0] == '\0')) {
226 DEBUG(3, ("No home directory defined for user '%s'\n",
227 username));
228 TALLOC_FREE(pwd);
229 return -1;
232 DEBUG(3, ("Adding homes service for user '%s' using home directory: "
233 "'%s'\n", username, pwd->pw_dir));
235 result = add_home_service(username, username, pwd->pw_dir);
237 TALLOC_FREE(pwd);
238 return result;
242 * register that a valid login has been performed, establish 'session'.
243 * @param session_info The token returned from the authentication process.
244 * (now 'owned' by register_existing_vuid)
246 * @param session_key The User session key for the login session (now also
247 * 'owned' by register_existing_vuid)
249 * @param respose_blob The NT challenge-response, if available. (May be
250 * freed after this call)
252 * @param smb_name The untranslated name of the user
254 * @return Newly allocated vuid, biased by an offset. (This allows us to
255 * tell random client vuid's (normally zero) from valid vuids.)
259 uint64_t register_existing_vuid(struct smbd_server_connection *sconn,
260 uint64_t vuid,
261 struct auth_session_info *session_info,
262 DATA_BLOB response_blob)
264 struct user_struct *vuser;
265 bool guest = security_session_user_level(session_info, NULL) < SECURITY_USER;
267 vuser = get_partial_auth_user_struct(sconn, vuid);
268 if (!vuser) {
269 goto fail;
272 /* Use this to keep tabs on all our info from the authentication */
273 vuser->session_info = talloc_move(vuser, &session_info);
275 /* Make clear that we require the optional unix_token and unix_info in the source3 code */
276 SMB_ASSERT(vuser->session_info->unix_token);
277 SMB_ASSERT(vuser->session_info->unix_info);
279 DEBUG(10,("register_existing_vuid: (%u,%u) %s %s %s guest=%d\n",
280 (unsigned int)vuser->session_info->unix_token->uid,
281 (unsigned int)vuser->session_info->unix_token->gid,
282 vuser->session_info->unix_info->unix_name,
283 vuser->session_info->unix_info->sanitized_username,
284 vuser->session_info->info->domain_name,
285 guest));
287 DEBUG(3, ("register_existing_vuid: User name: %s\t"
288 "Real name: %s\n", vuser->session_info->unix_info->unix_name,
289 vuser->session_info->info->full_name ?
290 vuser->session_info->info->full_name : ""));
292 if (!vuser->session_info->security_token) {
293 DEBUG(1, ("register_existing_vuid: session_info does not "
294 "contain a user_token - cannot continue\n"));
295 goto fail;
298 /* Make clear that we require the optional unix_token in the source3 code */
299 SMB_ASSERT(vuser->session_info->unix_token);
301 DEBUG(3,("register_existing_vuid: UNIX uid %d is UNIX user %s, "
302 "and will be vuid %llu\n", (int)vuser->session_info->unix_token->uid,
303 vuser->session_info->unix_info->unix_name,
304 (unsigned long long)vuser->vuid));
306 if (!session_claim(sconn, vuser)) {
307 DEBUG(1, ("register_existing_vuid: Failed to claim session "
308 "for vuid=%llu\n",
309 (unsigned long long)vuser->vuid));
310 goto fail;
313 /* Register a home dir service for this user if
314 (a) This is not a guest connection,
315 (b) we have a home directory defined
316 (c) there s not an existing static share by that name
317 If a share exists by this name (autoloaded or not) reuse it . */
319 vuser->homes_snum = -1;
322 if (!guest) {
323 vuser->homes_snum = register_homes_share(
324 vuser->session_info->unix_info->unix_name);
327 if (srv_is_signing_negotiated(sconn) &&
328 !guest) {
329 /* Try and turn on server signing on the first non-guest
330 * sessionsetup. */
331 srv_set_signing(sconn,
332 vuser->session_info->session_key,
333 response_blob);
336 /* fill in the current_user_info struct */
337 set_current_user_info(
338 vuser->session_info->unix_info->sanitized_username,
339 vuser->session_info->unix_info->unix_name,
340 vuser->session_info->info->domain_name);
342 return vuser->vuid;
344 fail:
346 if (vuser) {
347 invalidate_vuid(sconn, vuid);
349 return UID_FIELD_INVALID;