2 Unix SMB/CIFS implementation.
3 Password and authentication handling
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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 /* users from session setup */
24 static char *session_userlist
= NULL
;
25 static int len_session_userlist
= 0;
27 /* this holds info on user ids that are already validated for this VC */
28 static user_struct
*validated_users
;
29 static int next_vuid
= VUID_OFFSET
;
30 static int num_validated_vuids
;
32 extern userdom_struct current_user_info
;
35 /****************************************************************************
36 Check if a uid has been validated, and return an pointer to the user_struct
37 if it has. NULL if not. vuid is biased by an offset. This allows us to
38 tell random client vuid's (normally zero) from valid vuids.
39 ****************************************************************************/
41 user_struct
*get_valid_user_struct(uint16 vuid
)
46 if (vuid
== UID_FIELD_INVALID
)
49 for (usp
=validated_users
;usp
;usp
=usp
->next
,count
++) {
50 if (vuid
== usp
->vuid
&& usp
->server_info
) {
52 DLIST_PROMOTE(validated_users
, usp
);
61 /****************************************************************************
62 Get the user struct of a partial NTLMSSP login
63 ****************************************************************************/
65 user_struct
*get_partial_auth_user_struct(uint16 vuid
)
70 if (vuid
== UID_FIELD_INVALID
)
73 for (usp
=validated_users
;usp
;usp
=usp
->next
,count
++) {
74 if (vuid
== usp
->vuid
&& !usp
->server_info
) {
76 DLIST_PROMOTE(validated_users
, usp
);
85 /****************************************************************************
87 ****************************************************************************/
89 void invalidate_vuid(uint16 vuid
)
91 user_struct
*vuser
= get_valid_user_struct(vuid
);
96 SAFE_FREE(vuser
->homedir
);
97 SAFE_FREE(vuser
->unix_homedir
);
98 SAFE_FREE(vuser
->logon_script
);
100 session_yield(vuser
);
101 SAFE_FREE(vuser
->session_keystr
);
103 free_server_info(&vuser
->server_info
);
105 data_blob_free(&vuser
->session_key
);
107 DLIST_REMOVE(validated_users
, vuser
);
109 /* clear the vuid from the 'cache' on each connection, and
110 from the vuid 'owner' of connections */
111 conn_clear_vuid_cache(vuid
);
113 SAFE_FREE(vuser
->groups
);
114 delete_nt_token(&vuser
->nt_user_token
);
116 num_validated_vuids
--;
119 /****************************************************************************
120 Invalidate all vuid entries for this process.
121 ****************************************************************************/
123 void invalidate_all_vuids(void)
125 user_struct
*usp
, *next
=NULL
;
127 for (usp
=validated_users
;usp
;usp
=next
) {
130 invalidate_vuid(usp
->vuid
);
135 * register that a valid login has been performed, establish 'session'.
136 * @param server_info The token returned from the authentication process.
137 * (now 'owned' by register_vuid)
139 * @param session_key The User session key for the login session (now also 'owned' by register_vuid)
141 * @param respose_blob The NT challenge-response, if available. (May be freed after this call)
143 * @param smb_name The untranslated name of the user
145 * @return Newly allocated vuid, biased by an offset. (This allows us to
146 * tell random client vuid's (normally zero) from valid vuids.)
150 int register_vuid(auth_serversupplied_info
*server_info
, DATA_BLOB session_key
, DATA_BLOB response_blob
, const char *smb_name
)
152 user_struct
*vuser
= NULL
;
154 /* Ensure no vuid gets registered in share level security. */
155 if(lp_security() == SEC_SHARE
) {
156 data_blob_free(&session_key
);
157 return UID_FIELD_INVALID
;
160 /* Limit allowed vuids to 16bits - VUID_OFFSET. */
161 if (num_validated_vuids
>= 0xFFFF-VUID_OFFSET
) {
162 data_blob_free(&session_key
);
163 return UID_FIELD_INVALID
;
166 if((vuser
= SMB_MALLOC_P(user_struct
)) == NULL
) {
167 DEBUG(0,("Failed to malloc users struct!\n"));
168 data_blob_free(&session_key
);
169 return UID_FIELD_INVALID
;
174 /* Allocate a free vuid. Yes this is a linear search... :-) */
175 while( get_valid_user_struct(next_vuid
) != NULL
) {
177 /* Check for vuid wrap. */
178 if (next_vuid
== UID_FIELD_INVALID
)
179 next_vuid
= VUID_OFFSET
;
182 DEBUG(10,("register_vuid: allocated vuid = %u\n", (unsigned int)next_vuid
));
184 vuser
->vuid
= next_vuid
;
188 num_validated_vuids
++;
190 vuser
->server_info
= NULL
;
192 DLIST_ADD(validated_users
, vuser
);
197 /* the next functions should be done by a SID mapping system (SMS) as
198 * the new real sam db won't have reference to unix uids or gids
201 vuser
->uid
= server_info
->uid
;
202 vuser
->gid
= server_info
->gid
;
204 vuser
->n_groups
= server_info
->n_groups
;
205 if (vuser
->n_groups
) {
206 if (!(vuser
->groups
= (gid_t
*)memdup(server_info
->groups
, sizeof(gid_t
) * vuser
->n_groups
))) {
207 DEBUG(0,("register_vuid: failed to memdup vuser->groups\n"));
208 data_blob_free(&session_key
);
210 free_server_info(&server_info
);
211 return UID_FIELD_INVALID
;
215 vuser
->guest
= server_info
->guest
;
216 fstrcpy(vuser
->user
.unix_name
, server_info
->unix_name
);
218 /* This is a potentially untrusted username */
219 alpha_strcpy(vuser
->user
.smb_name
, smb_name
, ". _-$", sizeof(vuser
->user
.smb_name
));
221 fstrcpy(vuser
->user
.domain
, pdb_get_domain(server_info
->sam_account
));
222 fstrcpy(vuser
->user
.full_name
, pdb_get_fullname(server_info
->sam_account
));
225 /* Keep the homedir handy */
226 const char *homedir
= pdb_get_homedir(server_info
->sam_account
);
227 const char *logon_script
= pdb_get_logon_script(server_info
->sam_account
);
229 if (!IS_SAM_DEFAULT(server_info
->sam_account
, PDB_UNIXHOMEDIR
)) {
230 const char *unix_homedir
= pdb_get_unix_homedir(server_info
->sam_account
);
232 vuser
->unix_homedir
= smb_xstrdup(unix_homedir
);
235 struct passwd
*passwd
= getpwnam_alloc(vuser
->user
.unix_name
);
237 vuser
->unix_homedir
= smb_xstrdup(passwd
->pw_dir
);
238 passwd_free(&passwd
);
243 vuser
->homedir
= smb_xstrdup(homedir
);
246 vuser
->logon_script
= smb_xstrdup(logon_script
);
250 vuser
->session_key
= session_key
;
252 DEBUG(10,("register_vuid: (%u,%u) %s %s %s guest=%d\n",
253 (unsigned int)vuser
->uid
,
254 (unsigned int)vuser
->gid
,
255 vuser
->user
.unix_name
, vuser
->user
.smb_name
, vuser
->user
.domain
, vuser
->guest
));
257 DEBUG(3, ("User name: %s\tReal name: %s\n",vuser
->user
.unix_name
,vuser
->user
.full_name
));
259 if (server_info
->ptok
) {
260 vuser
->nt_user_token
= dup_nt_token(server_info
->ptok
);
262 DEBUG(1, ("server_info does not contain a user_token - cannot continue\n"));
263 free_server_info(&server_info
);
264 data_blob_free(&session_key
);
265 SAFE_FREE(vuser
->homedir
);
266 SAFE_FREE(vuser
->unix_homedir
);
267 SAFE_FREE(vuser
->logon_script
);
270 return UID_FIELD_INVALID
;
273 /* use this to keep tabs on all our info from the authentication */
274 vuser
->server_info
= server_info
;
276 DEBUG(3,("UNIX uid %d is UNIX user %s, and will be vuid %u\n",(int)vuser
->uid
,vuser
->user
.unix_name
, vuser
->vuid
));
279 num_validated_vuids
++;
281 DLIST_ADD(validated_users
, vuser
);
283 if (!session_claim(vuser
)) {
284 DEBUG(1,("Failed to claim session for vuid=%d\n", vuser
->vuid
));
285 invalidate_vuid(vuser
->vuid
);
289 /* Register a home dir service for this user iff
291 (a) This is not a guest connection,
292 (b) we have a home directory defined
293 (c) there s not an existing static share by that name
295 If a share exists by this name (autoloaded or not) reuse it . */
297 vuser
->homes_snum
= -1;
299 if ( (!vuser
->guest
) && vuser
->unix_homedir
&& *(vuser
->unix_homedir
))
301 int servicenumber
= lp_servicenumber(vuser
->user
.unix_name
);
303 if ( servicenumber
== -1 ) {
304 DEBUG(3, ("Adding homes service for user '%s' using home directory: '%s'\n",
305 vuser
->user
.unix_name
, vuser
->unix_homedir
));
306 vuser
->homes_snum
= add_home_service(vuser
->user
.unix_name
,
307 vuser
->user
.unix_name
, vuser
->unix_homedir
);
309 DEBUG(3, ("Using static (or previously created) service for user '%s'; path = '%s'\n",
310 vuser
->user
.unix_name
, lp_pathname(servicenumber
) ));
311 vuser
->homes_snum
= servicenumber
;
315 if (srv_is_signing_negotiated() && !vuser
->guest
&& !srv_signing_started()) {
316 /* Try and turn on server signing on the first non-guest sessionsetup. */
317 srv_set_signing(vuser
->session_key
, response_blob
);
320 /* fill in the current_user_info struct */
321 set_current_user_info( &vuser
->user
);
327 /****************************************************************************
328 Add a name to the session users list.
329 ****************************************************************************/
331 void add_session_user(const char *user
)
334 struct passwd
*passwd
;
336 if (!(passwd
= Get_Pwnam(user
)))
339 fstrcpy(suser
,passwd
->pw_name
);
344 if( session_userlist
&& in_list(suser
,session_userlist
,False
) )
347 if( !session_userlist
|| (strlen(suser
) + strlen(session_userlist
) + 2 >= len_session_userlist
) ) {
350 if (len_session_userlist
> 128 * PSTRING_LEN
) {
351 DEBUG(3,("add_session_user: session userlist already too large.\n"));
354 newlist
= (char *)SMB_REALLOC( session_userlist
, len_session_userlist
+ PSTRING_LEN
);
355 if( newlist
== NULL
) {
356 DEBUG(1,("Unable to resize session_userlist\n"));
359 if (!session_userlist
) {
362 session_userlist
= newlist
;
363 len_session_userlist
+= PSTRING_LEN
;
366 safe_strcat(session_userlist
," ",len_session_userlist
-1);
367 safe_strcat(session_userlist
,suser
,len_session_userlist
-1);
370 /****************************************************************************
371 Check if a username is valid.
372 ****************************************************************************/
374 BOOL
user_ok(const char *user
,int snum
, gid_t
*groups
, size_t n_groups
)
376 char **valid
, **invalid
;
379 valid
= invalid
= NULL
;
382 if (lp_invalid_users(snum
)) {
383 str_list_copy(&invalid
, lp_invalid_users(snum
));
384 if (invalid
&& str_list_substitute(invalid
, "%S", lp_servicename(snum
))) {
385 if ( invalid
&& str_list_sub_basic(invalid
, current_user_info
.smb_name
) ) {
386 ret
= !user_in_list(user
, (const char **)invalid
, groups
, n_groups
);
391 str_list_free (&invalid
);
393 if (ret
&& lp_valid_users(snum
)) {
394 str_list_copy(&valid
, lp_valid_users(snum
));
395 if ( valid
&& str_list_substitute(valid
, "%S", lp_servicename(snum
)) ) {
396 if ( valid
&& str_list_sub_basic(valid
, current_user_info
.smb_name
) ) {
397 ret
= user_in_list(user
, (const char **)valid
, groups
, n_groups
);
402 str_list_free (&valid
);
404 if (ret
&& lp_onlyuser(snum
)) {
405 char **user_list
= str_list_make (lp_username(snum
), NULL
);
406 if (user_list
&& str_list_substitute(user_list
, "%S", lp_servicename(snum
))) {
407 ret
= user_in_list(user
, (const char **)user_list
, groups
, n_groups
);
409 if (user_list
) str_list_free (&user_list
);
415 /****************************************************************************
416 Validate a group username entry. Return the username or NULL.
417 ****************************************************************************/
419 static char *validate_group(char *group
, DATA_BLOB password
,int snum
)
423 char *host
, *user
, *domain
;
425 while (getnetgrent(&host
, &user
, &domain
)) {
427 if (user_ok(user
, snum
, NULL
, 0) &&
428 password_ok(user
,password
)) {
442 while ((gptr
= (struct group
*)getgrent())) {
443 if (strequal(gptr
->gr_name
,group
))
448 * As user_ok can recurse doing a getgrent(), we must
449 * copy the member list into a pstring on the stack before
450 * use. Bug pointed out by leon@eatworms.swmed.edu.
456 size_t copied_len
= 0;
460 member
= member_list
;
462 for(i
= 0; gptr
->gr_mem
&& gptr
->gr_mem
[i
]; i
++) {
463 size_t member_len
= strlen(gptr
->gr_mem
[i
]) + 1;
464 if( copied_len
+ member_len
< sizeof(pstring
)) {
466 DEBUG(10,("validate_group: = gr_mem = %s\n", gptr
->gr_mem
[i
]));
468 safe_strcpy(member
, gptr
->gr_mem
[i
], sizeof(pstring
) - copied_len
- 1);
469 copied_len
+= member_len
;
470 member
+= copied_len
;
478 member
= member_list
;
481 fstrcpy(name
,member
);
482 if (user_ok(name
,snum
, NULL
, 0) &&
483 password_ok(name
,password
)) {
488 DEBUG(10,("validate_group = member = %s\n", member
));
490 member
+= strlen(member
) + 1;
501 /****************************************************************************
502 Check for authority to login to a service with a given username/password.
503 Note this is *NOT* used when logging on using sessionsetup_and_X.
504 ****************************************************************************/
506 BOOL
authorise_login(int snum
, fstring user
, DATA_BLOB password
,
511 #ifdef DEBUG_PASSWORD
512 DEBUG(100,("authorise_login: checking authorisation on user=%s pass=%s\n",
513 user
,password
.data
));
518 /* there are several possibilities:
519 1) login as the given user with given password
520 2) login as a previously registered username with the given password
521 3) login as a session list username with the given password
522 4) login as a previously validated user/password pair
523 5) login as the "user =" user with given password
524 6) login as the "user =" user with no password (guest connection)
525 7) login as guest user with no password
527 if the service is guest_only then steps 1 to 5 are skipped
530 /* now check the list of session users */
533 char *user_list
= NULL
;
535 if ( session_userlist
)
536 user_list
= SMB_STRDUP(session_userlist
);
538 user_list
= SMB_STRDUP("");
543 for (auser
=strtok(user_list
,LIST_SEP
); !ok
&& auser
;
544 auser
= strtok(NULL
,LIST_SEP
)) {
546 fstrcpy(user2
,auser
);
547 if (!user_ok(user2
,snum
, NULL
, 0))
550 if (password_ok(user2
,password
)) {
553 DEBUG(3,("authorise_login: ACCEPTED: session list username (%s) \
554 and given password ok\n", user
));
558 SAFE_FREE(user_list
);
561 /* check the user= fields and the given password */
562 if (!ok
&& lp_username(snum
)) {
565 pstrcpy(user_list
,lp_username(snum
));
567 pstring_sub(user_list
,"%S",lp_servicename(snum
));
569 for (auser
=strtok(user_list
,LIST_SEP
); auser
&& !ok
;
570 auser
= strtok(NULL
,LIST_SEP
)) {
572 auser
= validate_group(auser
+1,password
,snum
);
576 DEBUG(3,("authorise_login: ACCEPTED: group username \
577 and given password ok (%s)\n", user
));
581 fstrcpy(user2
,auser
);
582 if (user_ok(user2
,snum
, NULL
, 0) && password_ok(user2
,password
)) {
585 DEBUG(3,("authorise_login: ACCEPTED: user list username \
586 and given password ok (%s)\n", user
));
592 /* check for a normal guest connection */
593 if (!ok
&& GUEST_OK(snum
)) {
595 fstrcpy(guestname
,lp_guestaccount());
596 if (Get_Pwnam(guestname
)) {
597 fstrcpy(user
,guestname
);
599 DEBUG(3,("authorise_login: ACCEPTED: guest account and guest ok (%s)\n",
602 DEBUG(0,("authorise_login: Invalid guest account %s??\n",guestname
));
607 if (ok
&& !user_ok(user
, snum
, NULL
, 0)) {
608 DEBUG(0,("authorise_login: rejected invalid user %s\n",user
));