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 3 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, see <http://www.gnu.org/licenses/>.
22 /* users from session setup */
23 static char *session_userlist
= NULL
;
24 static int len_session_userlist
= 0;
25 /* workgroup from session setup. */
26 static char *session_workgroup
= NULL
;
28 /* this holds info on user ids that are already validated for this VC */
29 static user_struct
*validated_users
;
30 static int next_vuid
= VUID_OFFSET
;
31 static int num_validated_vuids
;
33 /****************************************************************************
34 Check if a uid has been validated, and return an pointer to the user_struct
35 if it has. NULL if not. vuid is biased by an offset. This allows us to
36 tell random client vuid's (normally zero) from valid vuids.
37 ****************************************************************************/
39 user_struct
*get_valid_user_struct(uint16 vuid
)
44 if (vuid
== UID_FIELD_INVALID
)
47 for (usp
=validated_users
;usp
;usp
=usp
->next
,count
++) {
48 if (vuid
== usp
->vuid
&& usp
->server_info
) {
50 DLIST_PROMOTE(validated_users
, usp
);
59 /****************************************************************************
60 Get the user struct of a partial NTLMSSP login
61 ****************************************************************************/
63 user_struct
*get_partial_auth_user_struct(uint16 vuid
)
68 if (vuid
== UID_FIELD_INVALID
)
71 for (usp
=validated_users
;usp
;usp
=usp
->next
,count
++) {
72 if (vuid
== usp
->vuid
&& !usp
->server_info
) {
74 DLIST_PROMOTE(validated_users
, usp
);
83 /****************************************************************************
85 ****************************************************************************/
87 void invalidate_vuid(uint16 vuid
)
89 user_struct
*vuser
= get_valid_user_struct(vuid
);
96 data_blob_free(&vuser
->session_key
);
98 DLIST_REMOVE(validated_users
, vuser
);
100 /* clear the vuid from the 'cache' on each connection, and
101 from the vuid 'owner' of connections */
102 conn_clear_vuid_cache(vuid
);
105 num_validated_vuids
--;
108 /****************************************************************************
109 Invalidate all vuid entries for this process.
110 ****************************************************************************/
112 void invalidate_all_vuids(void)
114 user_struct
*usp
, *next
=NULL
;
116 for (usp
=validated_users
;usp
;usp
=next
) {
119 invalidate_vuid(usp
->vuid
);
124 * register that a valid login has been performed, establish 'session'.
125 * @param server_info The token returned from the authentication process.
126 * (now 'owned' by register_vuid)
128 * @param session_key The User session key for the login session (now also
129 * 'owned' by register_vuid)
131 * @param respose_blob The NT challenge-response, if available. (May be
132 * freed after this call)
134 * @param smb_name The untranslated name of the user
136 * @return Newly allocated vuid, biased by an offset. (This allows us to
137 * tell random client vuid's (normally zero) from valid vuids.)
141 int register_vuid(auth_serversupplied_info
*server_info
,
142 DATA_BLOB session_key
, DATA_BLOB response_blob
,
143 const char *smb_name
)
147 /* Paranoia check. */
148 if(lp_security() == SEC_SHARE
) {
149 smb_panic("Tried to register uid in security=share");
152 /* Limit allowed vuids to 16bits - VUID_OFFSET. */
153 if (num_validated_vuids
>= 0xFFFF-VUID_OFFSET
) {
154 data_blob_free(&session_key
);
155 return UID_FIELD_INVALID
;
158 if((vuser
= TALLOC_ZERO_P(NULL
, user_struct
)) == NULL
) {
159 DEBUG(0,("Failed to talloc users struct!\n"));
160 data_blob_free(&session_key
);
161 return UID_FIELD_INVALID
;
164 /* Allocate a free vuid. Yes this is a linear search... :-) */
165 while( get_valid_user_struct(next_vuid
) != NULL
) {
167 /* Check for vuid wrap. */
168 if (next_vuid
== UID_FIELD_INVALID
)
169 next_vuid
= VUID_OFFSET
;
172 DEBUG(10,("register_vuid: allocated vuid = %u\n",
173 (unsigned int)next_vuid
));
175 vuser
->vuid
= next_vuid
;
179 * This happens in an unfinished NTLMSSP session setup. We
180 * need to allocate a vuid between the first and second calls
184 num_validated_vuids
++;
186 vuser
->server_info
= NULL
;
188 DLIST_ADD(validated_users
, vuser
);
193 /* use this to keep tabs on all our info from the authentication */
194 vuser
->server_info
= server_info
;
195 /* Ensure that the server_info will dissapear with the vuser it is now attached to */
196 talloc_steal(vuser
, vuser
->server_info
);
198 /* the next functions should be done by a SID mapping system (SMS) as
199 * the new real sam db won't have reference to unix uids or gids
202 vuser
->uid
= server_info
->uid
;
203 vuser
->gid
= server_info
->gid
;
205 vuser
->n_groups
= server_info
->n_groups
;
206 if (vuser
->n_groups
) {
207 if (!(vuser
->groups
= (gid_t
*)talloc_memdup(vuser
, server_info
->groups
,
210 DEBUG(0,("register_vuid: failed to talloc_memdup "
212 data_blob_free(&session_key
);
214 return UID_FIELD_INVALID
;
218 vuser
->guest
= server_info
->guest
;
219 fstrcpy(vuser
->user
.unix_name
, server_info
->unix_name
);
221 /* This is a potentially untrusted username */
222 alpha_strcpy(vuser
->user
.smb_name
, smb_name
, ". _-$",
223 sizeof(vuser
->user
.smb_name
));
225 fstrcpy(vuser
->user
.domain
, pdb_get_domain(server_info
->sam_account
));
226 fstrcpy(vuser
->user
.full_name
,
227 pdb_get_fullname(server_info
->sam_account
));
230 /* Keep the homedir handy */
231 const char *homedir
=
232 pdb_get_homedir(server_info
->sam_account
);
233 const char *logon_script
=
234 pdb_get_logon_script(server_info
->sam_account
);
236 if (!IS_SAM_DEFAULT(server_info
->sam_account
,
238 const char *unix_homedir
=
239 pdb_get_unix_homedir(server_info
->sam_account
);
241 vuser
->unix_homedir
= unix_homedir
;
244 struct passwd
*passwd
=
245 getpwnam_alloc(vuser
, vuser
->user
.unix_name
);
247 vuser
->unix_homedir
= passwd
->pw_dir
;
248 /* Ensure that the unix_homedir now
249 * belongs to vuser, so it goes away
250 * with it, not with passwd below: */
251 talloc_steal(vuser
, vuser
->unix_homedir
);
257 vuser
->homedir
= homedir
;
260 vuser
->logon_script
= logon_script
;
264 vuser
->session_key
= session_key
;
266 DEBUG(10,("register_vuid: (%u,%u) %s %s %s guest=%d\n",
267 (unsigned int)vuser
->uid
,
268 (unsigned int)vuser
->gid
,
269 vuser
->user
.unix_name
, vuser
->user
.smb_name
,
270 vuser
->user
.domain
, vuser
->guest
));
272 DEBUG(3, ("User name: %s\tReal name: %s\n", vuser
->user
.unix_name
,
273 vuser
->user
.full_name
));
275 if (server_info
->ptok
) {
276 vuser
->nt_user_token
= dup_nt_token(vuser
, server_info
->ptok
);
278 DEBUG(1, ("server_info does not contain a user_token - "
279 "cannot continue\n"));
281 data_blob_free(&session_key
);
282 return UID_FIELD_INVALID
;
285 DEBUG(3,("UNIX uid %d is UNIX user %s, and will be vuid %u\n",
286 (int)vuser
->uid
,vuser
->user
.unix_name
, vuser
->vuid
));
289 num_validated_vuids
++;
291 DLIST_ADD(validated_users
, vuser
);
293 if (!session_claim(vuser
)) {
294 DEBUG(1, ("Failed to claim session for vuid=%d\n",
296 invalidate_vuid(vuser
->vuid
);
297 return UID_FIELD_INVALID
;
300 /* Register a home dir service for this user iff
302 (a) This is not a guest connection,
303 (b) we have a home directory defined
304 (c) there s not an existing static share by that name
306 If a share exists by this name (autoloaded or not) reuse it . */
308 vuser
->homes_snum
= -1;
310 if ( (!vuser
->guest
) && vuser
->unix_homedir
&& *(vuser
->unix_homedir
))
312 int servicenumber
= lp_servicenumber(vuser
->user
.unix_name
);
314 if ( servicenumber
== -1 ) {
315 DEBUG(3, ("Adding homes service for user '%s' using "
316 "home directory: '%s'\n",
317 vuser
->user
.unix_name
, vuser
->unix_homedir
));
319 add_home_service(vuser
->user
.unix_name
,
320 vuser
->user
.unix_name
,
321 vuser
->unix_homedir
);
323 DEBUG(3, ("Using static (or previously created) "
324 "service for user '%s'; path = '%s'\n",
325 vuser
->user
.unix_name
,
326 lp_pathname(servicenumber
) ));
327 vuser
->homes_snum
= servicenumber
;
331 if (srv_is_signing_negotiated() && !vuser
->guest
&&
332 !srv_signing_started()) {
333 /* Try and turn on server signing on the first non-guest
335 srv_set_signing(vuser
->session_key
, response_blob
);
338 /* fill in the current_user_info struct */
339 set_current_user_info( &vuser
->user
);
345 /****************************************************************************
346 Add a name to the session users list.
347 ****************************************************************************/
349 void add_session_user(const char *user
)
352 struct passwd
*passwd
;
354 if (!(passwd
= Get_Pwnam(user
)))
357 fstrcpy(suser
,passwd
->pw_name
);
362 if( session_userlist
&& in_list(suser
,session_userlist
,False
) )
365 if( !session_userlist
||
366 (strlen(suser
) + strlen(session_userlist
) + 2 >=
367 len_session_userlist
) ) {
370 if (len_session_userlist
> 128 * PSTRING_LEN
) {
371 DEBUG(3,("add_session_user: session userlist already "
375 newlist
= (char *)SMB_REALLOC_KEEP_OLD_ON_ERROR(
377 len_session_userlist
+ PSTRING_LEN
);
378 if( newlist
== NULL
) {
379 DEBUG(1,("Unable to resize session_userlist\n"));
382 if (!session_userlist
) {
385 session_userlist
= newlist
;
386 len_session_userlist
+= PSTRING_LEN
;
389 safe_strcat(session_userlist
," ",len_session_userlist
-1);
390 safe_strcat(session_userlist
,suser
,len_session_userlist
-1);
393 /****************************************************************************
394 In security=share mode we need to store the client workgroup, as that's
395 what Vista uses for the NTLMv2 calculation.
396 ****************************************************************************/
398 void add_session_workgroup(const char *workgroup
)
400 if (session_workgroup
) {
401 SAFE_FREE(session_workgroup
);
403 session_workgroup
= smb_xstrdup(workgroup
);
406 /****************************************************************************
407 In security=share mode we need to return the client workgroup, as that's
408 what Vista uses for the NTLMv2 calculation.
409 ****************************************************************************/
411 const char *get_session_workgroup(void)
413 return session_workgroup
;
416 /****************************************************************************
417 Check if a user is in a netgroup user list. If at first we don't succeed,
419 ****************************************************************************/
421 BOOL
user_in_netgroup(const char *user
, const char *ngname
)
424 static char *mydomain
= NULL
;
425 fstring lowercase_user
;
427 if (mydomain
== NULL
)
428 yp_get_default_domain(&mydomain
);
430 if(mydomain
== NULL
) {
431 DEBUG(5,("Unable to get default yp domain, let's try without specifying it\n"));
434 DEBUG(5,("looking for user %s of domain %s in netgroup %s\n",
435 user
, mydomain
?mydomain
:"(ANY)", ngname
));
437 if (innetgr(ngname
, NULL
, user
, mydomain
)) {
438 DEBUG(5,("user_in_netgroup: Found\n"));
443 * Ok, innetgr is case sensitive. Try once more with lowercase
444 * just in case. Attempt to fix #703. JRA.
447 fstrcpy(lowercase_user
, user
);
448 strlower_m(lowercase_user
);
450 DEBUG(5,("looking for user %s of domain %s in netgroup %s\n",
451 lowercase_user
, mydomain
?mydomain
:"(ANY)", ngname
));
453 if (innetgr(ngname
, NULL
, lowercase_user
, mydomain
)) {
454 DEBUG(5,("user_in_netgroup: Found\n"));
458 #endif /* HAVE_NETGROUP */
462 /****************************************************************************
463 Check if a user is in a user list - can check combinations of UNIX
465 ****************************************************************************/
467 BOOL
user_in_list(const char *user
,const char **list
)
472 DEBUG(10,("user_in_list: checking user %s in list\n", user
));
476 DEBUG(10,("user_in_list: checking user |%s| against |%s|\n",
480 * Check raw username.
482 if (strequal(user
, *list
))
486 * Now check to see if any combination
487 * of UNIX and netgroups has been specified.
492 * Old behaviour. Check netgroup list
493 * followed by UNIX list.
495 if(user_in_netgroup(user
, *list
+1))
497 if(user_in_group(user
, *list
+1))
499 } else if (**list
== '+') {
501 if((*(*list
+1)) == '&') {
503 * Search UNIX list followed by netgroup.
505 if(user_in_group(user
, *list
+2))
507 if(user_in_netgroup(user
, *list
+2))
513 * Just search UNIX list.
516 if(user_in_group(user
, *list
+1))
520 } else if (**list
== '&') {
522 if(*(*list
+1) == '+') {
524 * Search netgroup list followed by UNIX list.
526 if(user_in_netgroup(user
, *list
+2))
528 if(user_in_group(user
, *list
+2))
532 * Just search netgroup list.
534 if(user_in_netgroup(user
, *list
+1))
544 /****************************************************************************
545 Check if a username is valid.
546 ****************************************************************************/
548 static BOOL
user_ok(const char *user
, int snum
)
550 char **valid
, **invalid
;
553 valid
= invalid
= NULL
;
556 if (lp_invalid_users(snum
)) {
557 str_list_copy(&invalid
, lp_invalid_users(snum
));
559 str_list_substitute(invalid
, "%S", lp_servicename(snum
))) {
561 /* This is used in sec=share only, so no current user
562 * around to pass to str_list_sub_basic() */
564 if ( invalid
&& str_list_sub_basic(invalid
, "", "") ) {
565 ret
= !user_in_list(user
,
566 (const char **)invalid
);
571 str_list_free (&invalid
);
573 if (ret
&& lp_valid_users(snum
)) {
574 str_list_copy(&valid
, lp_valid_users(snum
));
576 str_list_substitute(valid
, "%S", lp_servicename(snum
)) ) {
578 /* This is used in sec=share only, so no current user
579 * around to pass to str_list_sub_basic() */
581 if ( valid
&& str_list_sub_basic(valid
, "", "") ) {
582 ret
= user_in_list(user
, (const char **)valid
);
587 str_list_free (&valid
);
589 if (ret
&& lp_onlyuser(snum
)) {
590 char **user_list
= str_list_make (lp_username(snum
), NULL
);
592 str_list_substitute(user_list
, "%S",
593 lp_servicename(snum
))) {
594 ret
= user_in_list(user
, (const char **)user_list
);
596 if (user_list
) str_list_free (&user_list
);
602 /****************************************************************************
603 Validate a group username entry. Return the username or NULL.
604 ****************************************************************************/
606 static char *validate_group(char *group
, DATA_BLOB password
,int snum
)
610 char *host
, *user
, *domain
;
612 while (getnetgrent(&host
, &user
, &domain
)) {
614 if (user_ok(user
, snum
) &&
615 password_ok(user
,password
)) {
629 while ((gptr
= (struct group
*)getgrent())) {
630 if (strequal(gptr
->gr_name
,group
))
635 * As user_ok can recurse doing a getgrent(), we must
636 * copy the member list into a pstring on the stack before
637 * use. Bug pointed out by leon@eatworms.swmed.edu.
643 size_t copied_len
= 0;
647 member
= member_list
;
649 for(i
= 0; gptr
->gr_mem
&& gptr
->gr_mem
[i
]; i
++) {
650 size_t member_len
= strlen(gptr
->gr_mem
[i
])+1;
651 if(copied_len
+member_len
< sizeof(pstring
)) {
653 DEBUG(10,("validate_group: = gr_mem = "
654 "%s\n", gptr
->gr_mem
[i
]));
656 safe_strcpy(member
, gptr
->gr_mem
[i
],
659 copied_len
+= member_len
;
660 member
+= copied_len
;
668 member
= member_list
;
671 fstrcpy(name
,member
);
672 if (user_ok(name
,snum
) &&
673 password_ok(name
,password
)) {
678 DEBUG(10,("validate_group = member = %s\n",
681 member
+= strlen(member
) + 1;
692 /****************************************************************************
693 Check for authority to login to a service with a given username/password.
694 Note this is *NOT* used when logging on using sessionsetup_and_X.
695 ****************************************************************************/
697 BOOL
authorise_login(int snum
, fstring user
, DATA_BLOB password
,
702 #ifdef DEBUG_PASSWORD
703 DEBUG(100,("authorise_login: checking authorisation on "
704 "user=%s pass=%s\n", user
,password
.data
));
709 /* there are several possibilities:
710 1) login as the given user with given password
711 2) login as a previously registered username with the given
713 3) login as a session list username with the given password
714 4) login as a previously validated user/password pair
715 5) login as the "user =" user with given password
716 6) login as the "user =" user with no password
718 7) login as guest user with no password
720 if the service is guest_only then steps 1 to 5 are skipped
723 /* now check the list of session users */
726 char *user_list
= NULL
;
728 if ( session_userlist
)
729 user_list
= SMB_STRDUP(session_userlist
);
731 user_list
= SMB_STRDUP("");
736 for (auser
=strtok(user_list
,LIST_SEP
); !ok
&& auser
;
737 auser
= strtok(NULL
,LIST_SEP
)) {
739 fstrcpy(user2
,auser
);
740 if (!user_ok(user2
,snum
))
743 if (password_ok(user2
,password
)) {
746 DEBUG(3,("authorise_login: ACCEPTED: session "
747 "list username (%s) and given "
748 "password ok\n", user
));
752 SAFE_FREE(user_list
);
755 /* check the user= fields and the given password */
756 if (!ok
&& lp_username(snum
)) {
759 pstrcpy(user_list
,lp_username(snum
));
761 pstring_sub(user_list
,"%S",lp_servicename(snum
));
763 for (auser
=strtok(user_list
,LIST_SEP
); auser
&& !ok
;
764 auser
= strtok(NULL
,LIST_SEP
)) {
766 auser
= validate_group(auser
+1,password
,snum
);
770 DEBUG(3,("authorise_login: ACCEPTED: "
771 "group username and given "
772 "password ok (%s)\n", user
));
776 fstrcpy(user2
,auser
);
777 if (user_ok(user2
,snum
) &&
778 password_ok(user2
,password
)) {
781 DEBUG(3,("authorise_login: ACCEPTED: "
782 "user list username and "
783 "given password ok (%s)\n",
790 /* check for a normal guest connection */
791 if (!ok
&& GUEST_OK(snum
)) {
793 fstrcpy(guestname
,lp_guestaccount());
794 if (Get_Pwnam(guestname
)) {
795 fstrcpy(user
,guestname
);
797 DEBUG(3,("authorise_login: ACCEPTED: guest account "
798 "and guest ok (%s)\n", user
));
800 DEBUG(0,("authorise_login: Invalid guest account "
801 "%s??\n",guestname
));
806 if (ok
&& !user_ok(user
, snum
)) {
807 DEBUG(0,("authorise_login: rejected invalid user %s\n",user
));