Fix two memleaks
[Samba.git] / source / smbd / password.c
blob254d3cf1486dfa5d21e09c77aaac788674042f53
1 /*
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.
21 #include "includes.h"
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 /****************************************************************************
35 Check if a uid has been validated, and return an pointer to the user_struct
36 if it has. NULL if not. vuid is biased by an offset. This allows us to
37 tell random client vuid's (normally zero) from valid vuids.
38 ****************************************************************************/
40 user_struct *get_valid_user_struct(uint16 vuid)
42 user_struct *usp;
43 int count=0;
45 if (vuid == UID_FIELD_INVALID)
46 return NULL;
48 for (usp=validated_users;usp;usp=usp->next,count++) {
49 if (vuid == usp->vuid && usp->server_info) {
50 if (count > 10) {
51 DLIST_PROMOTE(validated_users, usp);
53 return usp;
57 return NULL;
60 /****************************************************************************
61 Get the user struct of a partial NTLMSSP login
62 ****************************************************************************/
64 user_struct *get_partial_auth_user_struct(uint16 vuid)
66 user_struct *usp;
67 int count=0;
69 if (vuid == UID_FIELD_INVALID)
70 return NULL;
72 for (usp=validated_users;usp;usp=usp->next,count++) {
73 if (vuid == usp->vuid && !usp->server_info) {
74 if (count > 10) {
75 DLIST_PROMOTE(validated_users, usp);
77 return usp;
81 return NULL;
84 /****************************************************************************
85 Invalidate a uid.
86 ****************************************************************************/
88 void invalidate_vuid(uint16 vuid)
90 user_struct *vuser = get_valid_user_struct(vuid);
92 if (vuser == NULL)
93 return;
95 SAFE_FREE(vuser->homedir);
96 SAFE_FREE(vuser->unix_homedir);
97 SAFE_FREE(vuser->logon_script);
99 session_yield(vuser);
100 SAFE_FREE(vuser->session_keystr);
102 TALLOC_FREE(vuser->server_info);
104 data_blob_free(&vuser->session_key);
106 DLIST_REMOVE(validated_users, vuser);
108 /* clear the vuid from the 'cache' on each connection, and
109 from the vuid 'owner' of connections */
110 conn_clear_vuid_cache(vuid);
112 SAFE_FREE(vuser->groups);
113 TALLOC_FREE(vuser->nt_user_token);
115 SAFE_FREE(vuser);
116 num_validated_vuids--;
119 void invalidate_intermediate_vuid(uint16 vuid)
121 user_struct *vuser = get_partial_auth_user_struct(vuid);
123 if (vuser == NULL)
124 return;
126 DLIST_REMOVE(validated_users, vuser);
128 SAFE_FREE(vuser);
129 num_validated_vuids--;
132 /****************************************************************************
133 Invalidate all vuid entries for this process.
134 ****************************************************************************/
136 void invalidate_all_vuids(void)
138 user_struct *usp, *next=NULL;
140 for (usp=validated_users;usp;usp=next) {
141 next = usp->next;
143 invalidate_vuid(usp->vuid);
148 * register that a valid login has been performed, establish 'session'.
149 * @param server_info The token returned from the authentication process.
150 * (now 'owned' by register_vuid)
152 * @param session_key The User session key for the login session (now also
153 * 'owned' by register_vuid)
155 * @param respose_blob The NT challenge-response, if available. (May be
156 * freed after this call)
158 * @param smb_name The untranslated name of the user
160 * @return Newly allocated vuid, biased by an offset. (This allows us to
161 * tell random client vuid's (normally zero) from valid vuids.)
165 int register_vuid(auth_serversupplied_info *server_info,
166 DATA_BLOB session_key, DATA_BLOB response_blob,
167 const char *smb_name)
169 user_struct *vuser = NULL;
171 /* Paranoia check. */
172 if(lp_security() == SEC_SHARE) {
173 smb_panic("Tried to register uid in security=share\n");
176 /* Limit allowed vuids to 16bits - VUID_OFFSET. */
177 if (num_validated_vuids >= 0xFFFF-VUID_OFFSET) {
178 data_blob_free(&session_key);
179 TALLOC_FREE(server_info);
180 return UID_FIELD_INVALID;
183 if((vuser = SMB_MALLOC_P(user_struct)) == NULL) {
184 DEBUG(0,("Failed to malloc users struct!\n"));
185 data_blob_free(&session_key);
186 TALLOC_FREE(server_info);
187 return UID_FIELD_INVALID;
190 ZERO_STRUCTP(vuser);
192 /* Allocate a free vuid. Yes this is a linear search... :-) */
193 while( (get_valid_user_struct(next_vuid) != NULL)
194 || (get_partial_auth_user_struct(next_vuid) != NULL) ) {
195 next_vuid++;
196 /* Check for vuid wrap. */
197 if (next_vuid == UID_FIELD_INVALID)
198 next_vuid = VUID_OFFSET;
201 DEBUG(10,("register_vuid: allocated vuid = %u\n",
202 (unsigned int)next_vuid ));
204 vuser->vuid = next_vuid;
206 if (!server_info) {
208 * This happens in an unfinished NTLMSSP session setup. We
209 * need to allocate a vuid between the first and second calls
210 * to NTLMSSP.
212 next_vuid++;
213 num_validated_vuids++;
215 vuser->server_info = NULL;
217 DLIST_ADD(validated_users, vuser);
219 return vuser->vuid;
222 /* the next functions should be done by a SID mapping system (SMS) as
223 * the new real sam db won't have reference to unix uids or gids
226 vuser->uid = server_info->uid;
227 vuser->gid = server_info->gid;
229 vuser->n_groups = server_info->n_groups;
230 if (vuser->n_groups) {
231 if (!(vuser->groups = (gid_t *)memdup(server_info->groups,
232 sizeof(gid_t) *
233 vuser->n_groups))) {
234 DEBUG(0,("register_vuid: failed to memdup "
235 "vuser->groups\n"));
236 data_blob_free(&session_key);
237 free(vuser);
238 TALLOC_FREE(server_info);
239 return UID_FIELD_INVALID;
243 vuser->guest = server_info->guest;
244 fstrcpy(vuser->user.unix_name, server_info->unix_name);
246 /* This is a potentially untrusted username */
247 alpha_strcpy(vuser->user.smb_name, smb_name, ". _-$",
248 sizeof(vuser->user.smb_name));
250 fstrcpy(vuser->user.domain, pdb_get_domain(server_info->sam_account));
251 fstrcpy(vuser->user.full_name,
252 pdb_get_fullname(server_info->sam_account));
255 /* Keep the homedir handy */
256 const char *homedir =
257 pdb_get_homedir(server_info->sam_account);
258 const char *logon_script =
259 pdb_get_logon_script(server_info->sam_account);
261 if (!IS_SAM_DEFAULT(server_info->sam_account,
262 PDB_UNIXHOMEDIR)) {
263 const char *unix_homedir =
264 pdb_get_unix_homedir(server_info->sam_account);
265 if (unix_homedir) {
266 vuser->unix_homedir =
267 smb_xstrdup(unix_homedir);
269 } else {
270 struct passwd *passwd =
271 getpwnam_alloc(NULL, vuser->user.unix_name);
272 if (passwd) {
273 vuser->unix_homedir =
274 smb_xstrdup(passwd->pw_dir);
275 TALLOC_FREE(passwd);
279 if (homedir) {
280 vuser->homedir = smb_xstrdup(homedir);
282 if (logon_script) {
283 vuser->logon_script = smb_xstrdup(logon_script);
287 vuser->session_key = session_key;
289 DEBUG(10,("register_vuid: (%u,%u) %s %s %s guest=%d\n",
290 (unsigned int)vuser->uid,
291 (unsigned int)vuser->gid,
292 vuser->user.unix_name, vuser->user.smb_name,
293 vuser->user.domain, vuser->guest ));
295 DEBUG(3, ("User name: %s\tReal name: %s\n", vuser->user.unix_name,
296 vuser->user.full_name));
298 if (server_info->ptok) {
299 vuser->nt_user_token = dup_nt_token(NULL, server_info->ptok);
300 } else {
301 DEBUG(1, ("server_info does not contain a user_token - "
302 "cannot continue\n"));
303 TALLOC_FREE(server_info);
304 data_blob_free(&session_key);
305 SAFE_FREE(vuser->homedir);
306 SAFE_FREE(vuser->unix_homedir);
307 SAFE_FREE(vuser->logon_script);
309 SAFE_FREE(vuser);
310 return UID_FIELD_INVALID;
313 /* use this to keep tabs on all our info from the authentication */
314 vuser->server_info = server_info;
316 DEBUG(3,("UNIX uid %d is UNIX user %s, and will be vuid %u\n",
317 (int)vuser->uid,vuser->user.unix_name, vuser->vuid));
319 next_vuid++;
320 num_validated_vuids++;
322 DLIST_ADD(validated_users, vuser);
324 if (!session_claim(vuser)) {
325 DEBUG(1, ("Failed to claim session for vuid=%d\n",
326 vuser->vuid));
327 invalidate_vuid(vuser->vuid);
328 return UID_FIELD_INVALID;
331 /* Register a home dir service for this user iff
333 (a) This is not a guest connection,
334 (b) we have a home directory defined
335 (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;
341 if ( (!vuser->guest) && vuser->unix_homedir && *(vuser->unix_homedir))
343 int servicenumber = lp_servicenumber(vuser->user.unix_name);
345 if ( servicenumber == -1 ) {
346 DEBUG(3, ("Adding homes service for user '%s' using "
347 "home directory: '%s'\n",
348 vuser->user.unix_name, vuser->unix_homedir));
349 vuser->homes_snum =
350 add_home_service(vuser->user.unix_name,
351 vuser->user.unix_name,
352 vuser->unix_homedir);
353 } else {
354 DEBUG(3, ("Using static (or previously created) "
355 "service for user '%s'; path = '%s'\n",
356 vuser->user.unix_name,
357 lp_pathname(servicenumber) ));
358 vuser->homes_snum = servicenumber;
362 if (srv_is_signing_negotiated() && !vuser->guest &&
363 !srv_signing_started()) {
364 /* Try and turn on server signing on the first non-guest
365 * sessionsetup. */
366 srv_set_signing(vuser->session_key, response_blob);
369 /* fill in the current_user_info struct */
370 set_current_user_info( &vuser->user );
373 return vuser->vuid;
376 /****************************************************************************
377 Add a name to the session users list.
378 ****************************************************************************/
380 void add_session_user(const char *user)
382 fstring suser;
383 struct passwd *passwd;
385 if (!(passwd = Get_Pwnam(user)))
386 return;
388 fstrcpy(suser,passwd->pw_name);
390 if(!*suser)
391 return;
393 if( session_userlist && in_list(suser,session_userlist,False) )
394 return;
396 if( !session_userlist ||
397 (strlen(suser) + strlen(session_userlist) + 2 >=
398 len_session_userlist) ) {
399 char *newlist;
401 if (len_session_userlist > 128 * PSTRING_LEN) {
402 DEBUG(3,("add_session_user: session userlist already "
403 "too large.\n"));
404 return;
406 newlist = (char *)SMB_REALLOC_KEEP_OLD_ON_ERROR(
407 session_userlist,
408 len_session_userlist + PSTRING_LEN );
409 if( newlist == NULL ) {
410 DEBUG(1,("Unable to resize session_userlist\n"));
411 return;
413 if (!session_userlist) {
414 *newlist = '\0';
416 session_userlist = newlist;
417 len_session_userlist += PSTRING_LEN;
420 safe_strcat(session_userlist," ",len_session_userlist-1);
421 safe_strcat(session_userlist,suser,len_session_userlist-1);
424 /****************************************************************************
425 In security=share mode we need to store the client workgroup, as that's
426 what Vista uses for the NTLMv2 calculation.
427 ****************************************************************************/
429 void add_session_workgroup(const char *workgroup)
431 if (session_workgroup) {
432 SAFE_FREE(session_workgroup);
434 session_workgroup = smb_xstrdup(workgroup);
437 /****************************************************************************
438 In security=share mode we need to return the client workgroup, as that's
439 what Vista uses for the NTLMv2 calculation.
440 ****************************************************************************/
442 const char *get_session_workgroup(void)
444 return session_workgroup;
447 /****************************************************************************
448 Check if a user is in a netgroup user list. If at first we don't succeed,
449 try lower case.
450 ****************************************************************************/
452 BOOL user_in_netgroup(const char *user, const char *ngname)
454 #ifdef HAVE_NETGROUP
455 static char *mydomain = NULL;
456 fstring lowercase_user;
458 if (mydomain == NULL)
459 yp_get_default_domain(&mydomain);
461 if(mydomain == NULL) {
462 DEBUG(5,("Unable to get default yp domain, let's try without specifying it\n"));
465 DEBUG(5,("looking for user %s of domain %s in netgroup %s\n",
466 user, mydomain?mydomain:"(ANY)", ngname));
468 if (innetgr(ngname, NULL, user, mydomain)) {
469 DEBUG(5,("user_in_netgroup: Found\n"));
470 return (True);
471 } else {
474 * Ok, innetgr is case sensitive. Try once more with lowercase
475 * just in case. Attempt to fix #703. JRA.
478 fstrcpy(lowercase_user, user);
479 strlower_m(lowercase_user);
481 DEBUG(5,("looking for user %s of domain %s in netgroup %s\n",
482 lowercase_user, mydomain?mydomain:"(ANY)", ngname));
484 if (innetgr(ngname, NULL, lowercase_user, mydomain)) {
485 DEBUG(5,("user_in_netgroup: Found\n"));
486 return (True);
489 #endif /* HAVE_NETGROUP */
490 return False;
493 /****************************************************************************
494 Check if a user is in a user list - can check combinations of UNIX
495 and netgroup lists.
496 ****************************************************************************/
498 BOOL user_in_list(const char *user,const char **list)
500 if (!list || !*list)
501 return False;
503 DEBUG(10,("user_in_list: checking user %s in list\n", user));
505 while (*list) {
507 DEBUG(10,("user_in_list: checking user |%s| against |%s|\n",
508 user, *list));
511 * Check raw username.
513 if (strequal(user, *list))
514 return(True);
517 * Now check to see if any combination
518 * of UNIX and netgroups has been specified.
521 if(**list == '@') {
523 * Old behaviour. Check netgroup list
524 * followed by UNIX list.
526 if(user_in_netgroup(user, *list +1))
527 return True;
528 if(user_in_group(user, *list +1))
529 return True;
530 } else if (**list == '+') {
532 if((*(*list +1)) == '&') {
534 * Search UNIX list followed by netgroup.
536 if(user_in_group(user, *list +2))
537 return True;
538 if(user_in_netgroup(user, *list +2))
539 return True;
541 } else {
544 * Just search UNIX list.
547 if(user_in_group(user, *list +1))
548 return True;
551 } else if (**list == '&') {
553 if(*(*list +1) == '+') {
555 * Search netgroup list followed by UNIX list.
557 if(user_in_netgroup(user, *list +2))
558 return True;
559 if(user_in_group(user, *list +2))
560 return True;
561 } else {
563 * Just search netgroup list.
565 if(user_in_netgroup(user, *list +1))
566 return True;
570 list++;
572 return(False);
575 /****************************************************************************
576 Check if a username is valid.
577 ****************************************************************************/
579 static BOOL user_ok(const char *user, int snum)
581 char **valid, **invalid;
582 BOOL ret;
584 valid = invalid = NULL;
585 ret = True;
587 if (lp_invalid_users(snum)) {
588 str_list_copy(&invalid, lp_invalid_users(snum));
589 if (invalid &&
590 str_list_substitute(invalid, "%S", lp_servicename(snum))) {
592 /* This is used in sec=share only, so no current user
593 * around to pass to str_list_sub_basic() */
595 if ( invalid && str_list_sub_basic(invalid, "", "") ) {
596 ret = !user_in_list(user,
597 (const char **)invalid);
601 if (invalid)
602 str_list_free (&invalid);
604 if (ret && lp_valid_users(snum)) {
605 str_list_copy(&valid, lp_valid_users(snum));
606 if ( valid &&
607 str_list_substitute(valid, "%S", lp_servicename(snum)) ) {
609 /* This is used in sec=share only, so no current user
610 * around to pass to str_list_sub_basic() */
612 if ( valid && str_list_sub_basic(valid, "", "") ) {
613 ret = user_in_list(user, (const char **)valid);
617 if (valid)
618 str_list_free (&valid);
620 if (ret && lp_onlyuser(snum)) {
621 char **user_list = str_list_make (lp_username(snum), NULL);
622 if (user_list &&
623 str_list_substitute(user_list, "%S",
624 lp_servicename(snum))) {
625 ret = user_in_list(user, (const char **)user_list);
627 if (user_list) str_list_free (&user_list);
630 return(ret);
633 /****************************************************************************
634 Validate a group username entry. Return the username or NULL.
635 ****************************************************************************/
637 static char *validate_group(char *group, DATA_BLOB password,int snum)
639 #ifdef HAVE_NETGROUP
641 char *host, *user, *domain;
642 setnetgrent(group);
643 while (getnetgrent(&host, &user, &domain)) {
644 if (user) {
645 if (user_ok(user, snum) &&
646 password_ok(user,password)) {
647 endnetgrent();
648 return(user);
652 endnetgrent();
654 #endif
656 #ifdef HAVE_GETGRENT
658 struct group *gptr;
659 setgrent();
660 while ((gptr = (struct group *)getgrent())) {
661 if (strequal(gptr->gr_name,group))
662 break;
666 * As user_ok can recurse doing a getgrent(), we must
667 * copy the member list into a pstring on the stack before
668 * use. Bug pointed out by leon@eatworms.swmed.edu.
671 if (gptr) {
672 pstring member_list;
673 char *member;
674 size_t copied_len = 0;
675 int i;
677 *member_list = '\0';
678 member = member_list;
680 for(i = 0; gptr->gr_mem && gptr->gr_mem[i]; i++) {
681 size_t member_len = strlen(gptr->gr_mem[i])+1;
682 if(copied_len+member_len < sizeof(pstring)) {
684 DEBUG(10,("validate_group: = gr_mem = "
685 "%s\n", gptr->gr_mem[i]));
687 safe_strcpy(member, gptr->gr_mem[i],
688 sizeof(pstring) -
689 copied_len - 1);
690 copied_len += member_len;
691 member += copied_len;
692 } else {
693 *member = '\0';
697 endgrent();
699 member = member_list;
700 while (*member) {
701 static fstring name;
702 fstrcpy(name,member);
703 if (user_ok(name,snum) &&
704 password_ok(name,password)) {
705 endgrent();
706 return(&name[0]);
709 DEBUG(10,("validate_group = member = %s\n",
710 member));
712 member += strlen(member) + 1;
714 } else {
715 endgrent();
716 return NULL;
719 #endif
720 return(NULL);
723 /****************************************************************************
724 Check for authority to login to a service with a given username/password.
725 Note this is *NOT* used when logging on using sessionsetup_and_X.
726 ****************************************************************************/
728 BOOL authorise_login(int snum, fstring user, DATA_BLOB password,
729 BOOL *guest)
731 BOOL ok = False;
733 #ifdef DEBUG_PASSWORD
734 DEBUG(100,("authorise_login: checking authorisation on "
735 "user=%s pass=%s\n", user,password.data));
736 #endif
738 *guest = False;
740 /* there are several possibilities:
741 1) login as the given user with given password
742 2) login as a previously registered username with the given
743 password
744 3) login as a session list username with the given password
745 4) login as a previously validated user/password pair
746 5) login as the "user =" user with given password
747 6) login as the "user =" user with no password
748 (guest connection)
749 7) login as guest user with no password
751 if the service is guest_only then steps 1 to 5 are skipped
754 /* now check the list of session users */
755 if (!ok) {
756 char *auser;
757 char *user_list = NULL;
759 if ( session_userlist )
760 user_list = SMB_STRDUP(session_userlist);
761 else
762 user_list = SMB_STRDUP("");
764 if (!user_list)
765 return(False);
767 for (auser=strtok(user_list,LIST_SEP); !ok && auser;
768 auser = strtok(NULL,LIST_SEP)) {
769 fstring user2;
770 fstrcpy(user2,auser);
771 if (!user_ok(user2,snum))
772 continue;
774 if (password_ok(user2,password)) {
775 ok = True;
776 fstrcpy(user,user2);
777 DEBUG(3,("authorise_login: ACCEPTED: session "
778 "list username (%s) and given "
779 "password ok\n", user));
783 SAFE_FREE(user_list);
786 /* check the user= fields and the given password */
787 if (!ok && lp_username(snum)) {
788 char *auser;
789 pstring user_list;
790 pstrcpy(user_list,lp_username(snum));
792 pstring_sub(user_list,"%S",lp_servicename(snum));
794 for (auser=strtok(user_list,LIST_SEP); auser && !ok;
795 auser = strtok(NULL,LIST_SEP)) {
796 if (*auser == '@') {
797 auser = validate_group(auser+1,password,snum);
798 if (auser) {
799 ok = True;
800 fstrcpy(user,auser);
801 DEBUG(3,("authorise_login: ACCEPTED: "
802 "group username and given "
803 "password ok (%s)\n", user));
805 } else {
806 fstring user2;
807 fstrcpy(user2,auser);
808 if (user_ok(user2,snum) &&
809 password_ok(user2,password)) {
810 ok = True;
811 fstrcpy(user,user2);
812 DEBUG(3,("authorise_login: ACCEPTED: "
813 "user list username and "
814 "given password ok (%s)\n",
815 user));
821 /* check for a normal guest connection */
822 if (!ok && GUEST_OK(snum)) {
823 fstring guestname;
824 fstrcpy(guestname,lp_guestaccount());
825 if (Get_Pwnam(guestname)) {
826 fstrcpy(user,guestname);
827 ok = True;
828 DEBUG(3,("authorise_login: ACCEPTED: guest account "
829 "and guest ok (%s)\n", user));
830 } else {
831 DEBUG(0,("authorise_login: Invalid guest account "
832 "%s??\n",guestname));
834 *guest = True;
837 if (ok && !user_ok(user, snum)) {
838 DEBUG(0,("authorise_login: rejected invalid user %s\n",user));
839 ok = False;
842 return(ok);