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/>.
22 #include "system/passwd.h"
23 #include "smbd/smbd.h"
24 #include "smbd/globals.h"
25 #include "../librpc/gen_ndr/netlogon.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);
33 #ifndef HAVE_GETNETGRENT_PROTOTYPE
34 extern int getnetgrent(char **host, char **user, char **domain);
36 #ifndef HAVE_ENDNETGRENT_PROTOTYPE
37 extern void endnetgrent(void);
40 enum server_allocated_state { SERVER_ALLOCATED_REQUIRED_YES,
41 SERVER_ALLOCATED_REQUIRED_NO,
42 SERVER_ALLOCATED_REQUIRED_ANY};
44 static user_struct *get_valid_user_struct_internal(
45 struct smbd_server_connection *sconn,
47 enum server_allocated_state server_allocated)
52 if (vuid == UID_FIELD_INVALID)
55 usp=sconn->smb1.sessions.validated_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) {
64 case SERVER_ALLOCATED_REQUIRED_NO:
65 if (usp->session_info != NULL) {
68 case SERVER_ALLOCATED_REQUIRED_ANY:
72 DLIST_PROMOTE(sconn->smb1.sessions.validated_users,
82 /****************************************************************************
83 Check if a uid has been validated, and return an pointer to the user_struct
84 if it has. NULL if not. vuid is biased by an offset. This allows us to
85 tell random client vuid's (normally zero) from valid vuids.
86 ****************************************************************************/
88 user_struct *get_valid_user_struct(struct smbd_server_connection *sconn,
91 return get_valid_user_struct_internal(sconn, vuid,
92 SERVER_ALLOCATED_REQUIRED_YES);
95 bool is_partial_auth_vuid(struct smbd_server_connection *sconn, uint16 vuid)
97 return (get_partial_auth_user_struct(sconn, vuid) != NULL);
100 /****************************************************************************
101 Get the user struct of a partial NTLMSSP login
102 ****************************************************************************/
104 user_struct *get_partial_auth_user_struct(struct smbd_server_connection *sconn,
107 return get_valid_user_struct_internal(sconn, vuid,
108 SERVER_ALLOCATED_REQUIRED_NO);
111 /****************************************************************************
113 ****************************************************************************/
115 void invalidate_vuid(struct smbd_server_connection *sconn, uint16 vuid)
117 user_struct *vuser = NULL;
119 vuser = get_valid_user_struct_internal(sconn, vuid,
120 SERVER_ALLOCATED_REQUIRED_ANY);
125 session_yield(vuser);
127 if (vuser->gensec_security) {
128 TALLOC_FREE(vuser->gensec_security);
131 DLIST_REMOVE(sconn->smb1.sessions.validated_users, vuser);
133 /* clear the vuid from the 'cache' on each connection, and
134 from the vuid 'owner' of connections */
135 conn_clear_vuid_caches(sconn, vuid);
138 sconn->smb1.sessions.num_validated_vuids--;
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) {
151 while (sconn->smb1.sessions.validated_users != NULL) {
152 invalidate_vuid(sconn,
153 sconn->smb1.sessions.validated_users->vuid);
157 static void increment_next_vuid(uint16_t *vuid)
161 /* Check for vuid wrap. */
162 if (*vuid == UID_FIELD_INVALID) {
167 /****************************************************
168 Create a new partial auth user struct.
169 *****************************************************/
171 int register_initial_vuid(struct smbd_server_connection *sconn)
175 /* Limit allowed vuids to 16bits - VUID_OFFSET. */
176 if (sconn->smb1.sessions.num_validated_vuids >= 0xFFFF-VUID_OFFSET) {
177 return UID_FIELD_INVALID;
180 if((vuser = talloc_zero(NULL, user_struct)) == NULL) {
181 DEBUG(0,("register_initial_vuid: "
182 "Failed to talloc users struct!\n"));
183 return UID_FIELD_INVALID;
186 /* Allocate a free vuid. Yes this is a linear search... */
187 while( get_valid_user_struct_internal(sconn,
188 sconn->smb1.sessions.next_vuid,
189 SERVER_ALLOCATED_REQUIRED_ANY) != NULL ) {
190 increment_next_vuid(&sconn->smb1.sessions.next_vuid);
193 DEBUG(10,("register_initial_vuid: allocated vuid = %u\n",
194 (unsigned int)sconn->smb1.sessions.next_vuid ));
196 vuser->vuid = sconn->smb1.sessions.next_vuid;
199 * This happens in an unfinished NTLMSSP session setup. We
200 * need to allocate a vuid between the first and second calls
203 increment_next_vuid(&sconn->smb1.sessions.next_vuid);
204 sconn->smb1.sessions.num_validated_vuids++;
206 DLIST_ADD(sconn->smb1.sessions.validated_users, vuser);
210 int register_homes_share(const char *username)
215 result = lp_servicenumber(username);
217 DEBUG(3, ("Using static (or previously created) service for "
218 "user '%s'; path = '%s'\n", username,
219 lp_pathname(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",
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);
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 int register_existing_vuid(struct smbd_server_connection *sconn,
261 struct auth_session_info *session_info,
262 DATA_BLOB response_blob)
265 bool guest = security_session_user_level(session_info, NULL) < SECURITY_USER;
267 vuser = get_partial_auth_user_struct(sconn, vuid);
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,
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));
291 if (!vuser->session_info->security_token) {
292 DEBUG(1, ("register_existing_vuid: session_info does not "
293 "contain a user_token - cannot continue\n"));
297 /* Make clear that we require the optional unix_token in the source3 code */
298 SMB_ASSERT(vuser->session_info->unix_token);
300 DEBUG(3,("register_existing_vuid: UNIX uid %d is UNIX user %s, "
301 "and will be vuid %u\n", (int)vuser->session_info->unix_token->uid,
302 vuser->session_info->unix_info->unix_name, vuser->vuid));
304 if (!session_claim(sconn, vuser)) {
305 DEBUG(1, ("register_existing_vuid: Failed to claim session "
311 /* Register a home dir service for this user if
312 (a) This is not a guest connection,
313 (b) we have a home directory defined
314 (c) there s not an existing static share by that name
315 If a share exists by this name (autoloaded or not) reuse it . */
317 vuser->homes_snum = -1;
321 vuser->homes_snum = register_homes_share(
322 vuser->session_info->unix_info->unix_name);
325 if (srv_is_signing_negotiated(sconn) &&
327 /* Try and turn on server signing on the first non-guest
329 srv_set_signing(sconn,
330 vuser->session_info->session_key,
334 /* fill in the current_user_info struct */
335 set_current_user_info(
336 vuser->session_info->unix_info->sanitized_username,
337 vuser->session_info->unix_info->unix_name,
338 vuser->session_info->info->domain_name);
345 invalidate_vuid(sconn, vuid);
347 return UID_FIELD_INVALID;