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/>.
23 /* users from session setup */
24 static char *session_userlist = NULL;
25 static int len_session_userlist = 0;
26 /* workgroup from session setup. */
27 static char *session_workgroup = NULL;
29 /* this holds info on user ids that are already validated for this VC */
30 static user_struct *validated_users;
31 static int next_vuid = VUID_OFFSET;
32 static int num_validated_vuids;
34 enum server_allocated_state { SERVER_ALLOCATED_REQUIRED_YES,
35 SERVER_ALLOCATED_REQUIRED_NO,
36 SERVER_ALLOCATED_REQUIRED_ANY};
38 static user_struct *get_valid_user_struct_internal(uint16 vuid,
39 enum server_allocated_state server_allocated)
44 if (vuid == UID_FIELD_INVALID)
47 for (usp=validated_users;usp;usp=usp->next,count++) {
48 if (vuid == usp->vuid) {
49 switch (server_allocated) {
50 case SERVER_ALLOCATED_REQUIRED_YES:
51 if (usp->server_info == NULL) {
55 case SERVER_ALLOCATED_REQUIRED_NO:
56 if (usp->server_info != NULL) {
59 case SERVER_ALLOCATED_REQUIRED_ANY:
63 DLIST_PROMOTE(validated_users, usp);
72 /****************************************************************************
73 Check if a uid has been validated, and return an pointer to the user_struct
74 if it has. NULL if not. vuid is biased by an offset. This allows us to
75 tell random client vuid's (normally zero) from valid vuids.
76 ****************************************************************************/
78 user_struct *get_valid_user_struct(uint16 vuid)
80 return get_valid_user_struct_internal(vuid,
81 SERVER_ALLOCATED_REQUIRED_YES);
84 bool is_partial_auth_vuid(uint16 vuid)
86 if (vuid == UID_FIELD_INVALID) {
89 return get_valid_user_struct_internal(vuid,
90 SERVER_ALLOCATED_REQUIRED_NO) ? True : False;
93 /****************************************************************************
94 Get the user struct of a partial NTLMSSP login
95 ****************************************************************************/
97 user_struct *get_partial_auth_user_struct(uint16 vuid)
99 return get_valid_user_struct_internal(vuid,
100 SERVER_ALLOCATED_REQUIRED_NO);
103 /****************************************************************************
105 ****************************************************************************/
107 void invalidate_vuid(uint16 vuid)
109 user_struct *vuser = NULL;
111 if (vuid == UID_FIELD_INVALID) {
115 vuser = get_valid_user_struct_internal(vuid,
116 SERVER_ALLOCATED_REQUIRED_ANY);
121 session_yield(vuser);
123 data_blob_free(&vuser->session_key);
125 DLIST_REMOVE(validated_users, vuser);
127 /* clear the vuid from the 'cache' on each connection, and
128 from the vuid 'owner' of connections */
129 conn_clear_vuid_cache(vuid);
132 num_validated_vuids--;
135 /****************************************************************************
136 Invalidate all vuid entries for this process.
137 ****************************************************************************/
139 void invalidate_all_vuids(void)
141 user_struct *usp, *next=NULL;
143 for (usp=validated_users;usp;usp=next) {
145 invalidate_vuid(usp->vuid);
149 /****************************************************
150 Create a new partial auth user struct.
151 *****************************************************/
153 int register_initial_vuid(void)
157 /* Paranoia check. */
158 if(lp_security() == SEC_SHARE) {
159 smb_panic("register_initial_vuid: "
160 "Tried to register uid in security=share");
163 /* Limit allowed vuids to 16bits - VUID_OFFSET. */
164 if (num_validated_vuids >= 0xFFFF-VUID_OFFSET) {
165 return UID_FIELD_INVALID;
168 if((vuser = talloc_zero(NULL, user_struct)) == NULL) {
169 DEBUG(0,("register_initial_vuid: "
170 "Failed to talloc users struct!\n"));
171 return UID_FIELD_INVALID;
174 /* Allocate a free vuid. Yes this is a linear search... */
175 while( get_valid_user_struct_internal(next_vuid,
176 SERVER_ALLOCATED_REQUIRED_ANY) != NULL ) {
178 /* Check for vuid wrap. */
179 if (next_vuid == UID_FIELD_INVALID) {
180 next_vuid = VUID_OFFSET;
184 DEBUG(10,("register_initial_vuid: allocated vuid = %u\n",
185 (unsigned int)next_vuid ));
187 vuser->vuid = next_vuid;
190 * This happens in an unfinished NTLMSSP session setup. We
191 * need to allocate a vuid between the first and second calls
195 num_validated_vuids++;
197 DLIST_ADD(validated_users, vuser);
202 * register that a valid login has been performed, establish 'session'.
203 * @param server_info The token returned from the authentication process.
204 * (now 'owned' by register_existing_vuid)
206 * @param session_key The User session key for the login session (now also
207 * 'owned' by register_existing_vuid)
209 * @param respose_blob The NT challenge-response, if available. (May be
210 * freed after this call)
212 * @param smb_name The untranslated name of the user
214 * @return Newly allocated vuid, biased by an offset. (This allows us to
215 * tell random client vuid's (normally zero) from valid vuids.)
219 int register_existing_vuid(uint16 vuid,
220 auth_serversupplied_info *server_info,
221 DATA_BLOB session_key,
222 DATA_BLOB response_blob,
223 const char *smb_name)
225 user_struct *vuser = get_partial_auth_user_struct(vuid);
230 /* Use this to keep tabs on all our info from the authentication */
231 vuser->server_info = server_info;
233 /* Ensure that the server_info will disappear with
234 * the vuser it is now attached to */
236 talloc_steal(vuser, vuser->server_info);
238 /* the next functions should be done by a SID mapping system (SMS) as
239 * the new real sam db won't have reference to unix uids or gids
242 vuser->uid = server_info->uid;
243 vuser->gid = server_info->gid;
245 vuser->n_groups = server_info->n_groups;
246 if (vuser->n_groups) {
247 if (!(vuser->groups = (gid_t *)talloc_memdup(vuser,
249 sizeof(gid_t)*vuser->n_groups))) {
250 DEBUG(0,("register_existing_vuid: "
251 "failed to talloc_memdup vuser->groups\n"));
256 vuser->guest = server_info->guest;
257 fstrcpy(vuser->user.unix_name, server_info->unix_name);
259 /* This is a potentially untrusted username */
260 alpha_strcpy(vuser->user.smb_name, smb_name, ". _-$",
261 sizeof(vuser->user.smb_name));
263 fstrcpy(vuser->user.domain, pdb_get_domain(server_info->sam_account));
264 fstrcpy(vuser->user.full_name,
265 pdb_get_fullname(server_info->sam_account));
268 /* Keep the homedir handy */
269 const char *homedir =
270 pdb_get_homedir(server_info->sam_account);
271 const char *logon_script =
272 pdb_get_logon_script(server_info->sam_account);
274 if (!IS_SAM_DEFAULT(server_info->sam_account,
276 const char *unix_homedir =
277 pdb_get_unix_homedir(server_info->sam_account);
279 vuser->unix_homedir = unix_homedir;
282 struct passwd *passwd =
283 getpwnam_alloc(vuser, vuser->user.unix_name);
285 vuser->unix_homedir = passwd->pw_dir;
286 /* Ensure that the unix_homedir now
287 * belongs to vuser, so it goes away
288 * with it, not with passwd below: */
289 talloc_steal(vuser, vuser->unix_homedir);
295 vuser->homedir = homedir;
298 vuser->logon_script = logon_script;
301 vuser->session_key = session_key;
303 DEBUG(10,("register_existing_vuid: (%u,%u) %s %s %s guest=%d\n",
304 (unsigned int)vuser->uid,
305 (unsigned int)vuser->gid,
306 vuser->user.unix_name, vuser->user.smb_name,
307 vuser->user.domain, vuser->guest ));
309 DEBUG(3, ("register_existing_vuid: User name: %s\t"
310 "Real name: %s\n", vuser->user.unix_name,
311 vuser->user.full_name));
313 if (server_info->ptok) {
314 vuser->nt_user_token = dup_nt_token(vuser, server_info->ptok);
316 DEBUG(1, ("register_existing_vuid: server_info does not "
317 "contain a user_token - cannot continue\n"));
321 DEBUG(3,("register_existing_vuid: UNIX uid %d is UNIX user %s, "
322 "and will be vuid %u\n",
323 (int)vuser->uid,vuser->user.unix_name, vuser->vuid));
326 num_validated_vuids++;
328 if (!session_claim(vuser)) {
329 DEBUG(1, ("register_existing_vuid: Failed to claim session "
335 /* Register a home dir service for this user if
336 (a) This is not a guest connection,
337 (b) we have a home directory defined
338 (c) there s not an existing static share by that name
339 If a share exists by this name (autoloaded or not) reuse it . */
341 vuser->homes_snum = -1;
342 if ( (!vuser->guest) && vuser->unix_homedir && *(vuser->unix_homedir)) {
343 int servicenumber = lp_servicenumber(vuser->user.unix_name);
344 if ( servicenumber == -1 ) {
345 DEBUG(3, ("Adding homes service for user '%s' using "
346 "home directory: '%s'\n",
347 vuser->user.unix_name, vuser->unix_homedir));
349 add_home_service(vuser->user.unix_name,
350 vuser->user.unix_name,
351 vuser->unix_homedir);
353 DEBUG(3, ("Using static (or previously created) "
354 "service for user '%s'; path = '%s'\n",
355 vuser->user.unix_name,
356 lp_pathname(servicenumber) ));
357 vuser->homes_snum = servicenumber;
361 if (srv_is_signing_negotiated() && !vuser->guest &&
362 !srv_signing_started()) {
363 /* Try and turn on server signing on the first non-guest
365 srv_set_signing(vuser->session_key, response_blob);
368 /* fill in the current_user_info struct */
369 set_current_user_info( &vuser->user );
375 invalidate_vuid(vuid);
377 return UID_FIELD_INVALID;
380 /****************************************************************************
381 Add a name to the session users list.
382 ****************************************************************************/
384 void add_session_user(const char *user)
387 struct passwd *passwd;
389 if (!(passwd = Get_Pwnam(user)))
392 fstrcpy(suser,passwd->pw_name);
397 if( session_userlist && in_list(suser,session_userlist,False) )
400 if( !session_userlist ||
401 (strlen(suser) + strlen(session_userlist) + 2 >=
402 len_session_userlist) ) {
405 if (len_session_userlist > 128 * PSTRING_LEN) {
406 DEBUG(3,("add_session_user: session userlist already "
410 newlist = (char *)SMB_REALLOC_KEEP_OLD_ON_ERROR(
412 len_session_userlist + PSTRING_LEN );
413 if( newlist == NULL ) {
414 DEBUG(1,("Unable to resize session_userlist\n"));
417 if (!session_userlist) {
420 session_userlist = newlist;
421 len_session_userlist += PSTRING_LEN;
424 safe_strcat(session_userlist," ",len_session_userlist-1);
425 safe_strcat(session_userlist,suser,len_session_userlist-1);
428 /****************************************************************************
429 In security=share mode we need to store the client workgroup, as that's
430 what Vista uses for the NTLMv2 calculation.
431 ****************************************************************************/
433 void add_session_workgroup(const char *workgroup)
435 if (session_workgroup) {
436 SAFE_FREE(session_workgroup);
438 session_workgroup = smb_xstrdup(workgroup);
441 /****************************************************************************
442 In security=share mode we need to return the client workgroup, as that's
443 what Vista uses for the NTLMv2 calculation.
444 ****************************************************************************/
446 const char *get_session_workgroup(void)
448 return session_workgroup;
451 /****************************************************************************
452 Check if a user is in a netgroup user list. If at first we don't succeed,
454 ****************************************************************************/
456 bool user_in_netgroup(const char *user, const char *ngname)
459 static char *mydomain = NULL;
460 fstring lowercase_user;
462 if (mydomain == NULL)
463 yp_get_default_domain(&mydomain);
465 if(mydomain == NULL) {
466 DEBUG(5,("Unable to get default yp domain, "
467 "let's try without specifying it\n"));
470 DEBUG(5,("looking for user %s of domain %s in netgroup %s\n",
471 user, mydomain?mydomain:"(ANY)", ngname));
473 if (innetgr(ngname, NULL, user, mydomain)) {
474 DEBUG(5,("user_in_netgroup: Found\n"));
479 * Ok, innetgr is case sensitive. Try once more with lowercase
480 * just in case. Attempt to fix #703. JRA.
483 fstrcpy(lowercase_user, user);
484 strlower_m(lowercase_user);
486 DEBUG(5,("looking for user %s of domain %s in netgroup %s\n",
487 lowercase_user, mydomain?mydomain:"(ANY)", ngname));
489 if (innetgr(ngname, NULL, lowercase_user, mydomain)) {
490 DEBUG(5,("user_in_netgroup: Found\n"));
494 #endif /* HAVE_NETGROUP */
498 /****************************************************************************
499 Check if a user is in a user list - can check combinations of UNIX
501 ****************************************************************************/
503 bool user_in_list(const char *user,const char **list)
508 DEBUG(10,("user_in_list: checking user %s in list\n", user));
512 DEBUG(10,("user_in_list: checking user |%s| against |%s|\n",
516 * Check raw username.
518 if (strequal(user, *list))
522 * Now check to see if any combination
523 * of UNIX and netgroups has been specified.
528 * Old behaviour. Check netgroup list
529 * followed by UNIX list.
531 if(user_in_netgroup(user, *list +1))
533 if(user_in_group(user, *list +1))
535 } else if (**list == '+') {
537 if((*(*list +1)) == '&') {
539 * Search UNIX list followed by netgroup.
541 if(user_in_group(user, *list +2))
543 if(user_in_netgroup(user, *list +2))
549 * Just search UNIX list.
552 if(user_in_group(user, *list +1))
556 } else if (**list == '&') {
558 if(*(*list +1) == '+') {
560 * Search netgroup list followed by UNIX list.
562 if(user_in_netgroup(user, *list +2))
564 if(user_in_group(user, *list +2))
568 * Just search netgroup list.
570 if(user_in_netgroup(user, *list +1))
580 /****************************************************************************
581 Check if a username is valid.
582 ****************************************************************************/
584 static bool user_ok(const char *user, int snum)
586 char **valid, **invalid;
589 valid = invalid = NULL;
592 if (lp_invalid_users(snum)) {
593 str_list_copy(&invalid, lp_invalid_users(snum));
595 str_list_substitute(invalid, "%S", lp_servicename(snum))) {
597 /* This is used in sec=share only, so no current user
598 * around to pass to str_list_sub_basic() */
600 if ( invalid && str_list_sub_basic(invalid, "", "") ) {
601 ret = !user_in_list(user,
602 (const char **)invalid);
607 str_list_free (&invalid);
609 if (ret && lp_valid_users(snum)) {
610 str_list_copy(&valid, lp_valid_users(snum));
612 str_list_substitute(valid, "%S", lp_servicename(snum)) ) {
614 /* This is used in sec=share only, so no current user
615 * around to pass to str_list_sub_basic() */
617 if ( valid && str_list_sub_basic(valid, "", "") ) {
618 ret = user_in_list(user, (const char **)valid);
623 str_list_free (&valid);
625 if (ret && lp_onlyuser(snum)) {
626 char **user_list = str_list_make (lp_username(snum), NULL);
628 str_list_substitute(user_list, "%S",
629 lp_servicename(snum))) {
630 ret = user_in_list(user, (const char **)user_list);
632 if (user_list) str_list_free (&user_list);
638 /****************************************************************************
639 Validate a group username entry. Return the username or NULL.
640 ****************************************************************************/
642 static char *validate_group(char *group, DATA_BLOB password,int snum)
646 char *host, *user, *domain;
648 while (getnetgrent(&host, &user, &domain)) {
650 if (user_ok(user, snum) &&
651 password_ok(user,password)) {
665 while ((gptr = (struct group *)getgrent())) {
666 if (strequal(gptr->gr_name,group))
671 * As user_ok can recurse doing a getgrent(), we must
672 * copy the member list onto the heap before
673 * use. Bug pointed out by leon@eatworms.swmed.edu.
677 char *member_list = NULL;
682 for(i = 0; gptr->gr_mem && gptr->gr_mem[i]; i++) {
683 list_len += strlen(gptr->gr_mem[i])+1;
687 member_list = SMB_MALLOC(list_len);
694 member = member_list;
696 for(i = 0; gptr->gr_mem && gptr->gr_mem[i]; i++) {
697 size_t member_len = strlen(gptr->gr_mem[i])+1;
699 DEBUG(10,("validate_group: = gr_mem = "
700 "%s\n", gptr->gr_mem[i]));
702 safe_strcpy(member, gptr->gr_mem[i],
703 list_len - (member-member_list));
704 member += member_len;
709 member = member_list;
711 if (user_ok(member,snum) &&
712 password_ok(member,password)) {
713 char *name = talloc_strdup(talloc_tos(),
715 SAFE_FREE(member_list);
719 DEBUG(10,("validate_group = member = %s\n",
722 member += strlen(member) + 1;
725 SAFE_FREE(member_list);
735 /****************************************************************************
736 Check for authority to login to a service with a given username/password.
737 Note this is *NOT* used when logging on using sessionsetup_and_X.
738 ****************************************************************************/
740 bool authorise_login(int snum, fstring user, DATA_BLOB password,
745 #ifdef DEBUG_PASSWORD
746 DEBUG(100,("authorise_login: checking authorisation on "
747 "user=%s pass=%s\n", user,password.data));
752 /* there are several possibilities:
753 1) login as the given user with given password
754 2) login as a previously registered username with the given
756 3) login as a session list username with the given password
757 4) login as a previously validated user/password pair
758 5) login as the "user =" user with given password
759 6) login as the "user =" user with no password
761 7) login as guest user with no password
763 if the service is guest_only then steps 1 to 5 are skipped
766 /* now check the list of session users */
769 char *user_list = NULL;
771 if ( session_userlist )
772 user_list = SMB_STRDUP(session_userlist);
774 user_list = SMB_STRDUP("");
779 for (auser=strtok(user_list,LIST_SEP); !ok && auser;
780 auser = strtok(NULL,LIST_SEP)) {
782 fstrcpy(user2,auser);
783 if (!user_ok(user2,snum))
786 if (password_ok(user2,password)) {
789 DEBUG(3,("authorise_login: ACCEPTED: session "
790 "list username (%s) and given "
791 "password ok\n", user));
795 SAFE_FREE(user_list);
798 /* check the user= fields and the given password */
799 if (!ok && lp_username(snum)) {
800 TALLOC_CTX *ctx = talloc_tos();
802 char *user_list = talloc_strdup(ctx, lp_username(snum));
808 user_list = talloc_string_sub(ctx,
811 lp_servicename(snum));
817 for (auser=strtok(user_list,LIST_SEP); auser && !ok;
818 auser = strtok(NULL,LIST_SEP)) {
820 auser = validate_group(auser+1,password,snum);
824 DEBUG(3,("authorise_login: ACCEPTED: "
825 "group username and given "
826 "password ok (%s)\n", user));
830 fstrcpy(user2,auser);
831 if (user_ok(user2,snum) &&
832 password_ok(user2,password)) {
835 DEBUG(3,("authorise_login: ACCEPTED: "
836 "user list username and "
837 "given password ok (%s)\n",
846 /* check for a normal guest connection */
847 if (!ok && GUEST_OK(snum)) {
849 fstrcpy(guestname,lp_guestaccount());
850 if (Get_Pwnam(guestname)) {
851 fstrcpy(user,guestname);
853 DEBUG(3,("authorise_login: ACCEPTED: guest account "
854 "and guest ok (%s)\n", user));
856 DEBUG(0,("authorise_login: Invalid guest account "
857 "%s??\n",guestname));
862 if (ok && !user_ok(user, snum)) {
863 DEBUG(0,("authorise_login: rejected invalid user %s\n",user));