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
)
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
->users
, usp
);
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 user_struct
*get_valid_user_struct(struct smbd_server_connection
*sconn
,
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
, uint16 vuid
)
96 return (get_partial_auth_user_struct(sconn
, vuid
) != NULL
);
99 /****************************************************************************
100 Get the user struct of a partial NTLMSSP login
101 ****************************************************************************/
103 user_struct
*get_partial_auth_user_struct(struct smbd_server_connection
*sconn
,
106 return get_valid_user_struct_internal(sconn
, vuid
,
107 SERVER_ALLOCATED_REQUIRED_NO
);
110 /****************************************************************************
112 ****************************************************************************/
114 void invalidate_vuid(struct smbd_server_connection
*sconn
, uint16 vuid
)
116 user_struct
*vuser
= NULL
;
118 vuser
= get_valid_user_struct_internal(sconn
, vuid
,
119 SERVER_ALLOCATED_REQUIRED_ANY
);
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);
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
);
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
->users
!= NULL
) {
152 invalidate_vuid(sconn
, sconn
->users
->vuid
);
156 static void increment_next_vuid(uint16_t *vuid
)
160 /* Check for vuid wrap. */
161 if (*vuid
== UID_FIELD_INVALID
) {
166 /****************************************************
167 Create a new partial auth user struct.
168 *****************************************************/
170 int register_initial_vuid(struct smbd_server_connection
*sconn
)
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
, 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
202 increment_next_vuid(&sconn
->smb1
.sessions
.next_vuid
);
205 DLIST_ADD(sconn
->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
;