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 enum server_allocated_state { SERVER_ALLOCATED_REQUIRED_YES,
34 SERVER_ALLOCATED_REQUIRED_NO,
35 SERVER_ALLOCATED_REQUIRED_ANY};
37 static user_struct *get_valid_user_struct_internal(uint16 vuid,
38 enum server_allocated_state server_allocated)
43 if (vuid == UID_FIELD_INVALID)
46 for (usp=validated_users;usp;usp=usp->next,count++) {
47 if (vuid == usp->vuid) {
48 switch (server_allocated) {
49 case SERVER_ALLOCATED_REQUIRED_YES:
50 if (usp->server_info == NULL) {
54 case SERVER_ALLOCATED_REQUIRED_NO:
55 if (usp->server_info != NULL) {
58 case SERVER_ALLOCATED_REQUIRED_ANY:
62 DLIST_PROMOTE(validated_users, usp);
71 /****************************************************************************
72 Check if a uid has been validated, and return an pointer to the user_struct
73 if it has. NULL if not. vuid is biased by an offset. This allows us to
74 tell random client vuid's (normally zero) from valid vuids.
75 ****************************************************************************/
77 user_struct *get_valid_user_struct(uint16 vuid)
79 return get_valid_user_struct_internal(vuid, SERVER_ALLOCATED_REQUIRED_YES);
82 BOOL is_partial_auth_vuid(uint16 vuid)
84 if (vuid == UID_FIELD_INVALID) {
87 return get_valid_user_struct_internal(vuid,
88 SERVER_ALLOCATED_REQUIRED_NO) ? True : False;
91 /****************************************************************************
92 Get the user struct of a partial NTLMSSP login
93 ****************************************************************************/
95 user_struct *get_partial_auth_user_struct(uint16 vuid)
97 return get_valid_user_struct_internal(vuid,
98 SERVER_ALLOCATED_REQUIRED_NO);
101 /****************************************************************************
103 ****************************************************************************/
105 void invalidate_vuid(uint16 vuid)
107 user_struct *vuser = NULL;
109 if (vuid == UID_FIELD_INVALID) {
113 vuser = get_valid_user_struct_internal(vuid,
114 SERVER_ALLOCATED_REQUIRED_ANY);
119 session_yield(vuser);
121 data_blob_free(&vuser->session_key);
123 DLIST_REMOVE(validated_users, vuser);
125 /* clear the vuid from the 'cache' on each connection, and
126 from the vuid 'owner' of connections */
127 conn_clear_vuid_cache(vuid);
130 num_validated_vuids--;
133 /****************************************************************************
134 Invalidate all vuid entries for this process.
135 ****************************************************************************/
137 void invalidate_all_vuids(void)
139 user_struct *usp, *next=NULL;
141 for (usp=validated_users;usp;usp=next) {
143 invalidate_vuid(usp->vuid);
147 /****************************************************
148 Create a new partial auth user struct.
149 *****************************************************/
151 int register_initial_vuid(void)
155 /* Paranoia check. */
156 if(lp_security() == SEC_SHARE) {
157 smb_panic("register_initial_vuid: "
158 "Tried to register uid in security=share");
161 /* Limit allowed vuids to 16bits - VUID_OFFSET. */
162 if (num_validated_vuids >= 0xFFFF-VUID_OFFSET) {
163 return UID_FIELD_INVALID;
166 if((vuser = talloc_zero(NULL, user_struct)) == NULL) {
167 DEBUG(0,("register_initial_vuid: "
168 "Failed to talloc users struct!\n"));
169 return UID_FIELD_INVALID;
172 /* Allocate a free vuid. Yes this is a linear search... */
173 while( get_valid_user_struct_internal(next_vuid,
174 SERVER_ALLOCATED_REQUIRED_ANY) != NULL ) {
176 /* Check for vuid wrap. */
177 if (next_vuid == UID_FIELD_INVALID) {
178 next_vuid = VUID_OFFSET;
182 DEBUG(10,("register_initial_vuid: allocated vuid = %u\n",
183 (unsigned int)next_vuid ));
185 vuser->vuid = next_vuid;
188 * This happens in an unfinished NTLMSSP session setup. We
189 * need to allocate a vuid between the first and second calls
193 num_validated_vuids++;
195 DLIST_ADD(validated_users, vuser);
200 * register that a valid login has been performed, establish 'session'.
201 * @param server_info The token returned from the authentication process.
202 * (now 'owned' by register_existing_vuid)
204 * @param session_key The User session key for the login session (now also
205 * 'owned' by register_existing_vuid)
207 * @param respose_blob The NT challenge-response, if available. (May be
208 * freed after this call)
210 * @param smb_name The untranslated name of the user
212 * @return Newly allocated vuid, biased by an offset. (This allows us to
213 * tell random client vuid's (normally zero) from valid vuids.)
217 int register_existing_vuid(uint16 vuid,
218 auth_serversupplied_info *server_info,
219 DATA_BLOB session_key,
220 DATA_BLOB response_blob,
221 const char *smb_name)
223 user_struct *vuser = get_partial_auth_user_struct(vuid);
228 /* Use this to keep tabs on all our info from the authentication */
229 vuser->server_info = server_info;
231 /* Ensure that the server_info will disappear with
232 * the vuser it is now attached to */
234 talloc_steal(vuser, vuser->server_info);
236 /* the next functions should be done by a SID mapping system (SMS) as
237 * the new real sam db won't have reference to unix uids or gids
240 vuser->uid = server_info->uid;
241 vuser->gid = server_info->gid;
243 vuser->n_groups = server_info->n_groups;
244 if (vuser->n_groups) {
245 if (!(vuser->groups = (gid_t *)talloc_memdup(vuser,
247 sizeof(gid_t)*vuser->n_groups))) {
248 DEBUG(0,("register_existing_vuid: "
249 "failed to talloc_memdup vuser->groups\n"));
254 vuser->guest = server_info->guest;
255 fstrcpy(vuser->user.unix_name, server_info->unix_name);
257 /* This is a potentially untrusted username */
258 alpha_strcpy(vuser->user.smb_name, smb_name, ". _-$",
259 sizeof(vuser->user.smb_name));
261 fstrcpy(vuser->user.domain, pdb_get_domain(server_info->sam_account));
262 fstrcpy(vuser->user.full_name,
263 pdb_get_fullname(server_info->sam_account));
266 /* Keep the homedir handy */
267 const char *homedir =
268 pdb_get_homedir(server_info->sam_account);
269 const char *logon_script =
270 pdb_get_logon_script(server_info->sam_account);
272 if (!IS_SAM_DEFAULT(server_info->sam_account,
274 const char *unix_homedir =
275 pdb_get_unix_homedir(server_info->sam_account);
277 vuser->unix_homedir = unix_homedir;
280 struct passwd *passwd =
281 getpwnam_alloc(vuser, vuser->user.unix_name);
283 vuser->unix_homedir = passwd->pw_dir;
284 /* Ensure that the unix_homedir now
285 * belongs to vuser, so it goes away
286 * with it, not with passwd below: */
287 talloc_steal(vuser, vuser->unix_homedir);
293 vuser->homedir = homedir;
296 vuser->logon_script = logon_script;
299 vuser->session_key = session_key;
301 DEBUG(10,("register_existing_vuid: (%u,%u) %s %s %s guest=%d\n",
302 (unsigned int)vuser->uid,
303 (unsigned int)vuser->gid,
304 vuser->user.unix_name, vuser->user.smb_name,
305 vuser->user.domain, vuser->guest ));
307 DEBUG(3, ("register_existing_vuid: User name: %s\t"
308 "Real name: %s\n", vuser->user.unix_name,
309 vuser->user.full_name));
311 if (server_info->ptok) {
312 vuser->nt_user_token = dup_nt_token(vuser, server_info->ptok);
314 DEBUG(1, ("register_existing_vuid: server_info does not "
315 "contain a user_token - cannot continue\n"));
319 DEBUG(3,("register_existing_vuid: UNIX uid %d is UNIX user %s, "
320 "and will be vuid %u\n",
321 (int)vuser->uid,vuser->user.unix_name, vuser->vuid));
324 num_validated_vuids++;
326 if (!session_claim(vuser)) {
327 DEBUG(1, ("register_existing_vuid: Failed to claim session "
333 /* Register a home dir service for this user if
334 (a) This is not a guest connection,
335 (b) we have a home directory defined
336 (c) there s not an existing static share by that name
337 If a share exists by this name (autoloaded or not) reuse it . */
339 vuser->homes_snum = -1;
340 if ( (!vuser->guest) && vuser->unix_homedir && *(vuser->unix_homedir)) {
341 int servicenumber = lp_servicenumber(vuser->user.unix_name);
342 if ( servicenumber == -1 ) {
343 DEBUG(3, ("Adding homes service for user '%s' using "
344 "home directory: '%s'\n",
345 vuser->user.unix_name, vuser->unix_homedir));
347 add_home_service(vuser->user.unix_name,
348 vuser->user.unix_name,
349 vuser->unix_homedir);
351 DEBUG(3, ("Using static (or previously created) "
352 "service for user '%s'; path = '%s'\n",
353 vuser->user.unix_name,
354 lp_pathname(servicenumber) ));
355 vuser->homes_snum = servicenumber;
359 if (srv_is_signing_negotiated() && !vuser->guest &&
360 !srv_signing_started()) {
361 /* Try and turn on server signing on the first non-guest
363 srv_set_signing(vuser->session_key, response_blob);
366 /* fill in the current_user_info struct */
367 set_current_user_info( &vuser->user );
373 invalidate_vuid(vuid);
375 return UID_FIELD_INVALID;
378 /****************************************************************************
379 Add a name to the session users list.
380 ****************************************************************************/
382 void add_session_user(const char *user)
385 struct passwd *passwd;
387 if (!(passwd = Get_Pwnam(user)))
390 fstrcpy(suser,passwd->pw_name);
395 if( session_userlist && in_list(suser,session_userlist,False) )
398 if( !session_userlist ||
399 (strlen(suser) + strlen(session_userlist) + 2 >=
400 len_session_userlist) ) {
403 if (len_session_userlist > 128 * PSTRING_LEN) {
404 DEBUG(3,("add_session_user: session userlist already "
408 newlist = (char *)SMB_REALLOC_KEEP_OLD_ON_ERROR(
410 len_session_userlist + PSTRING_LEN );
411 if( newlist == NULL ) {
412 DEBUG(1,("Unable to resize session_userlist\n"));
415 if (!session_userlist) {
418 session_userlist = newlist;
419 len_session_userlist += PSTRING_LEN;
422 safe_strcat(session_userlist," ",len_session_userlist-1);
423 safe_strcat(session_userlist,suser,len_session_userlist-1);
426 /****************************************************************************
427 In security=share mode we need to store the client workgroup, as that's
428 what Vista uses for the NTLMv2 calculation.
429 ****************************************************************************/
431 void add_session_workgroup(const char *workgroup)
433 if (session_workgroup) {
434 SAFE_FREE(session_workgroup);
436 session_workgroup = smb_xstrdup(workgroup);
439 /****************************************************************************
440 In security=share mode we need to return the client workgroup, as that's
441 what Vista uses for the NTLMv2 calculation.
442 ****************************************************************************/
444 const char *get_session_workgroup(void)
446 return session_workgroup;
449 /****************************************************************************
450 Check if a user is in a netgroup user list. If at first we don't succeed,
452 ****************************************************************************/
454 BOOL user_in_netgroup(const char *user, const char *ngname)
457 static char *mydomain = NULL;
458 fstring lowercase_user;
460 if (mydomain == NULL)
461 yp_get_default_domain(&mydomain);
463 if(mydomain == NULL) {
464 DEBUG(5,("Unable to get default yp domain, let's try without specifying it\n"));
467 DEBUG(5,("looking for user %s of domain %s in netgroup %s\n",
468 user, mydomain?mydomain:"(ANY)", ngname));
470 if (innetgr(ngname, NULL, user, mydomain)) {
471 DEBUG(5,("user_in_netgroup: Found\n"));
476 * Ok, innetgr is case sensitive. Try once more with lowercase
477 * just in case. Attempt to fix #703. JRA.
480 fstrcpy(lowercase_user, user);
481 strlower_m(lowercase_user);
483 DEBUG(5,("looking for user %s of domain %s in netgroup %s\n",
484 lowercase_user, mydomain?mydomain:"(ANY)", ngname));
486 if (innetgr(ngname, NULL, lowercase_user, mydomain)) {
487 DEBUG(5,("user_in_netgroup: Found\n"));
491 #endif /* HAVE_NETGROUP */
495 /****************************************************************************
496 Check if a user is in a user list - can check combinations of UNIX
498 ****************************************************************************/
500 BOOL user_in_list(const char *user,const char **list)
505 DEBUG(10,("user_in_list: checking user %s in list\n", user));
509 DEBUG(10,("user_in_list: checking user |%s| against |%s|\n",
513 * Check raw username.
515 if (strequal(user, *list))
519 * Now check to see if any combination
520 * of UNIX and netgroups has been specified.
525 * Old behaviour. Check netgroup list
526 * followed by UNIX list.
528 if(user_in_netgroup(user, *list +1))
530 if(user_in_group(user, *list +1))
532 } else if (**list == '+') {
534 if((*(*list +1)) == '&') {
536 * Search UNIX list followed by netgroup.
538 if(user_in_group(user, *list +2))
540 if(user_in_netgroup(user, *list +2))
546 * Just search UNIX list.
549 if(user_in_group(user, *list +1))
553 } else if (**list == '&') {
555 if(*(*list +1) == '+') {
557 * Search netgroup list followed by UNIX list.
559 if(user_in_netgroup(user, *list +2))
561 if(user_in_group(user, *list +2))
565 * Just search netgroup list.
567 if(user_in_netgroup(user, *list +1))
577 /****************************************************************************
578 Check if a username is valid.
579 ****************************************************************************/
581 static BOOL user_ok(const char *user, int snum)
583 char **valid, **invalid;
586 valid = invalid = NULL;
589 if (lp_invalid_users(snum)) {
590 str_list_copy(&invalid, lp_invalid_users(snum));
592 str_list_substitute(invalid, "%S", lp_servicename(snum))) {
594 /* This is used in sec=share only, so no current user
595 * around to pass to str_list_sub_basic() */
597 if ( invalid && str_list_sub_basic(invalid, "", "") ) {
598 ret = !user_in_list(user,
599 (const char **)invalid);
604 str_list_free (&invalid);
606 if (ret && lp_valid_users(snum)) {
607 str_list_copy(&valid, lp_valid_users(snum));
609 str_list_substitute(valid, "%S", lp_servicename(snum)) ) {
611 /* This is used in sec=share only, so no current user
612 * around to pass to str_list_sub_basic() */
614 if ( valid && str_list_sub_basic(valid, "", "") ) {
615 ret = user_in_list(user, (const char **)valid);
620 str_list_free (&valid);
622 if (ret && lp_onlyuser(snum)) {
623 char **user_list = str_list_make (lp_username(snum), NULL);
625 str_list_substitute(user_list, "%S",
626 lp_servicename(snum))) {
627 ret = user_in_list(user, (const char **)user_list);
629 if (user_list) str_list_free (&user_list);
635 /****************************************************************************
636 Validate a group username entry. Return the username or NULL.
637 ****************************************************************************/
639 static char *validate_group(char *group, DATA_BLOB password,int snum)
643 char *host, *user, *domain;
645 while (getnetgrent(&host, &user, &domain)) {
647 if (user_ok(user, snum) &&
648 password_ok(user,password)) {
662 while ((gptr = (struct group *)getgrent())) {
663 if (strequal(gptr->gr_name,group))
668 * As user_ok can recurse doing a getgrent(), we must
669 * copy the member list into a pstring on the stack before
670 * use. Bug pointed out by leon@eatworms.swmed.edu.
676 size_t copied_len = 0;
680 member = member_list;
682 for(i = 0; gptr->gr_mem && gptr->gr_mem[i]; i++) {
683 size_t member_len = strlen(gptr->gr_mem[i])+1;
684 if(copied_len+member_len < sizeof(pstring)) {
686 DEBUG(10,("validate_group: = gr_mem = "
687 "%s\n", gptr->gr_mem[i]));
689 safe_strcpy(member, gptr->gr_mem[i],
692 copied_len += member_len;
693 member += copied_len;
701 member = member_list;
704 fstrcpy(name,member);
705 if (user_ok(name,snum) &&
706 password_ok(name,password)) {
711 DEBUG(10,("validate_group = member = %s\n",
714 member += strlen(member) + 1;
725 /****************************************************************************
726 Check for authority to login to a service with a given username/password.
727 Note this is *NOT* used when logging on using sessionsetup_and_X.
728 ****************************************************************************/
730 BOOL authorise_login(int snum, fstring user, DATA_BLOB password,
735 #ifdef DEBUG_PASSWORD
736 DEBUG(100,("authorise_login: checking authorisation on "
737 "user=%s pass=%s\n", user,password.data));
742 /* there are several possibilities:
743 1) login as the given user with given password
744 2) login as a previously registered username with the given
746 3) login as a session list username with the given password
747 4) login as a previously validated user/password pair
748 5) login as the "user =" user with given password
749 6) login as the "user =" user with no password
751 7) login as guest user with no password
753 if the service is guest_only then steps 1 to 5 are skipped
756 /* now check the list of session users */
759 char *user_list = NULL;
761 if ( session_userlist )
762 user_list = SMB_STRDUP(session_userlist);
764 user_list = SMB_STRDUP("");
769 for (auser=strtok(user_list,LIST_SEP); !ok && auser;
770 auser = strtok(NULL,LIST_SEP)) {
772 fstrcpy(user2,auser);
773 if (!user_ok(user2,snum))
776 if (password_ok(user2,password)) {
779 DEBUG(3,("authorise_login: ACCEPTED: session "
780 "list username (%s) and given "
781 "password ok\n", user));
785 SAFE_FREE(user_list);
788 /* check the user= fields and the given password */
789 if (!ok && lp_username(snum)) {
792 pstrcpy(user_list,lp_username(snum));
794 pstring_sub(user_list,"%S",lp_servicename(snum));
796 for (auser=strtok(user_list,LIST_SEP); auser && !ok;
797 auser = strtok(NULL,LIST_SEP)) {
799 auser = validate_group(auser+1,password,snum);
803 DEBUG(3,("authorise_login: ACCEPTED: "
804 "group username and given "
805 "password ok (%s)\n", user));
809 fstrcpy(user2,auser);
810 if (user_ok(user2,snum) &&
811 password_ok(user2,password)) {
814 DEBUG(3,("authorise_login: ACCEPTED: "
815 "user list username and "
816 "given password ok (%s)\n",
823 /* check for a normal guest connection */
824 if (!ok && GUEST_OK(snum)) {
826 fstrcpy(guestname,lp_guestaccount());
827 if (Get_Pwnam(guestname)) {
828 fstrcpy(user,guestname);
830 DEBUG(3,("authorise_login: ACCEPTED: guest account "
831 "and guest ok (%s)\n", user));
833 DEBUG(0,("authorise_login: Invalid guest account "
834 "%s??\n",guestname));
839 if (ok && !user_ok(user, snum)) {
840 DEBUG(0,("authorise_login: rejected invalid user %s\n",user));