Added NT_USER_TOKEN into server_info to fix extra groups problem.
[kamenim/samba-autobuild/.git] / source3 / smbd / password.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    Password and authentication handling
5    Copyright (C) Andrew Tridgell 1992-1998
6    
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 2 of the License, or
10    (at your option) any later version.
11    
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.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23
24 extern struct in_addr ipzero;
25
26 /* users from session setup */
27 static pstring session_users="";
28
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;
33
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 ****************************************************************************/
39 user_struct *get_valid_user_struct(uint16 vuid)
40 {
41         user_struct *usp;
42         int count=0;
43
44         if (vuid == UID_FIELD_INVALID)
45                 return NULL;
46
47         for (usp=validated_users;usp;usp=usp->next,count++) {
48                 if (vuid == usp->vuid) {
49                         if (count > 10) {
50                                 DLIST_PROMOTE(validated_users, usp);
51                         }
52                         return usp;
53                 }
54         }
55
56         return NULL;
57 }
58
59 /****************************************************************************
60 invalidate a uid
61 ****************************************************************************/
62 void invalidate_vuid(uint16 vuid)
63 {
64         user_struct *vuser = get_valid_user_struct(vuid);
65
66         if (vuser == NULL)
67                 return;
68
69         session_yield(vuid);
70
71         DLIST_REMOVE(validated_users, vuser);
72
73         SAFE_FREE(vuser->groups);
74         delete_nt_token(&vuser->nt_user_token);
75         SAFE_FREE(vuser);
76         num_validated_vuids--;
77 }
78
79 /****************************************************************************
80 invalidate all vuid entries for this process
81 ****************************************************************************/
82 void invalidate_all_vuids(void)
83 {
84         user_struct *usp, *next=NULL;
85
86         for (usp=validated_users;usp;usp=next) {
87                 next = usp->next;
88                 
89                 invalidate_vuid(usp->vuid);
90         }
91 }
92
93 /****************************************************************************
94 return a validated username
95 ****************************************************************************/
96 char *validated_username(uint16 vuid)
97 {
98         user_struct *vuser = get_valid_user_struct(vuid);
99         if (vuser == NULL)
100                 return 0;
101         return(vuser->user.unix_name);
102 }
103
104 /****************************************************************************
105 return a validated domain
106 ****************************************************************************/
107 char *validated_domain(uint16 vuid)
108 {
109         user_struct *vuser = get_valid_user_struct(vuid);
110         if (vuser == NULL)
111                 return 0;
112         return(vuser->user.domain);
113 }
114
115
116 /****************************************************************************
117  Create the SID list for this user.
118 ****************************************************************************/
119
120 NT_USER_TOKEN *create_nt_token(uid_t uid, gid_t gid, int ngroups, gid_t *groups, BOOL is_guest, NT_USER_TOKEN *sup_tok)
121 {
122         extern DOM_SID global_sid_World;
123         extern DOM_SID global_sid_Network;
124         extern DOM_SID global_sid_Builtin_Guests;
125         extern DOM_SID global_sid_Authenticated_Users;
126         NT_USER_TOKEN *token;
127         DOM_SID *psids;
128         int i, psid_ndx = 0;
129         size_t num_sids = 0;
130         fstring sid_str;
131
132         if ((token = (NT_USER_TOKEN *)malloc( sizeof(NT_USER_TOKEN) ) ) == NULL)
133                 return NULL;
134
135         ZERO_STRUCTP(token);
136
137         /* We always have uid/gid plus World and Network and Authenticated Users or Guest SIDs. */
138         num_sids = 5 + ngroups;
139
140         if (sup_tok && sup_tok->num_sids)
141                 num_sids += sup_tok->num_sids;
142
143         if ((token->user_sids = (DOM_SID *)malloc( num_sids*sizeof(DOM_SID))) == NULL) {
144                 SAFE_FREE(token);
145                 return NULL;
146         }
147
148         psids = token->user_sids;
149
150         /*
151          * Note - user SID *MUST* be first in token !
152          * se_access_check depends on this.
153          */
154
155         uid_to_sid( &psids[PRIMARY_USER_SID_INDEX], uid);
156         psid_ndx++;
157
158         /*
159          * Primary group SID is second in token. Convention.
160          */
161
162         gid_to_sid( &psids[PRIMARY_GROUP_SID_INDEX], gid);
163         psid_ndx++;
164
165         /* Now add the group SIDs. */
166
167         for (i = 0; i < ngroups; i++) {
168                 if (groups[i] != gid) {
169                         gid_to_sid( &psids[psid_ndx++], groups[i]);
170                 }
171         }
172
173         /* Now add the additional SIDs from the supplimentary token. */
174         for (i = 0; i < sup_tok->num_sids; i++)
175                 sid_copy( &psids[psid_ndx++], &sup_tok->user_sids[i] );
176
177         /*
178          * Finally add the "standard" SIDs.
179          * The only difference between guest and "anonymous" (which we
180          * don't really support) is the addition of Authenticated_Users.
181          */
182
183         sid_copy( &psids[psid_ndx++], &global_sid_World);
184         sid_copy( &psids[psid_ndx++], &global_sid_Network);
185
186         if (is_guest)
187                 sid_copy( &psids[psid_ndx++], &global_sid_Builtin_Guests);
188         else
189                 sid_copy( &psids[psid_ndx++], &global_sid_Authenticated_Users);
190
191         token->num_sids = psid_ndx;
192
193         /* Dump list of sids in token */
194
195         for (i = 0; i < token->num_sids; i++) {
196                 DEBUG(5, ("user token sid %s\n", 
197                           sid_to_string(sid_str, &token->user_sids[i])));
198         }
199
200         return token;
201 }
202
203 /****************************************************************************
204 register a uid/name pair as being valid and that a valid password
205 has been given. vuid is biased by an offset. This allows us to
206 tell random client vuid's (normally zero) from valid vuids.
207 ****************************************************************************/
208
209 int register_vuid(auth_serversupplied_info *server_info, char *smb_name, BOOL guest)
210 {
211         user_struct *vuser = NULL;
212         uid_t *puid;
213         gid_t *pgid;
214
215         /* Ensure no vuid gets registered in share level security. */
216         if(lp_security() == SEC_SHARE)
217                 return UID_FIELD_INVALID;
218
219         /* Limit allowed vuids to 16bits - VUID_OFFSET. */
220         if (num_validated_vuids >= 0xFFFF-VUID_OFFSET)
221                 return UID_FIELD_INVALID;
222
223         if((vuser = (user_struct *)malloc( sizeof(user_struct) )) == NULL) {
224                 DEBUG(0,("Failed to malloc users struct!\n"));
225                 return UID_FIELD_INVALID;
226         }
227
228         ZERO_STRUCTP(vuser);
229
230         puid = pdb_get_uid(server_info->sam_account);
231         pgid = pdb_get_gid(server_info->sam_account);
232
233         if (!puid || !pgid) {
234                 DEBUG(0,("Attempted session setup with invalid user.  No uid/gid in SAM_ACCOUNT\n"));
235                 free(vuser);
236                 return UID_FIELD_INVALID;
237         }
238
239         /* Allocate a free vuid. Yes this is a linear search... :-) */
240         while( get_valid_user_struct(next_vuid) != NULL ) {
241                 next_vuid++;
242                 /* Check for vuid wrap. */
243                 if (next_vuid == UID_FIELD_INVALID)
244                         next_vuid = VUID_OFFSET;
245         }
246
247         DEBUG(10,("register_vuid: allocated vuid = %u\n", (unsigned int)next_vuid ));
248
249         vuser->vuid = next_vuid;
250         vuser->uid = *puid;
251         vuser->gid = *pgid;
252         vuser->guest = guest;
253         fstrcpy(vuser->user.unix_name, pdb_get_username(server_info->sam_account));
254         fstrcpy(vuser->user.smb_name, smb_name);
255         fstrcpy(vuser->user.domain, pdb_get_domain(server_info->sam_account));
256         fstrcpy(vuser->user.full_name, pdb_get_fullname(server_info->sam_account));
257
258         DEBUG(10,("register_vuid: (%u,%u) %s %s %s guest=%d\n", 
259                   (unsigned int)vuser->uid, 
260                   (unsigned int)vuser->gid,
261                   vuser->user.unix_name, vuser->user.smb_name, vuser->user.domain, guest ));
262
263         DEBUG(3, ("User name: %s\tReal name: %s\n",vuser->user.unix_name,vuser->user.full_name));       
264
265         vuser->n_groups = 0;
266         vuser->groups  = NULL;
267
268         /* Find all the groups this uid is in and store them. 
269                 Used by change_to_user() */
270         initialise_groups(vuser->user.unix_name, vuser->uid, vuser->gid);
271         get_current_groups( &vuser->n_groups, &vuser->groups);
272
273         if (server_info->ptok)
274                 add_supplementary_nt_login_groups(&vuser->n_groups, &vuser->groups, &server_info->ptok);
275
276         /* Create an NT_USER_TOKEN struct for this user. */
277         vuser->nt_user_token = create_nt_token(vuser->uid, vuser->gid, vuser->n_groups, vuser->groups, guest, server_info->ptok);
278
279         DEBUG(3,("uid %d registered to name %s\n",(int)vuser->uid,vuser->user.unix_name));
280
281         next_vuid++;
282         num_validated_vuids++;
283
284         DLIST_ADD(validated_users, vuser);
285
286         if (!session_claim(vuser->vuid)) {
287                 DEBUG(1,("Failed to claim session for vuid=%d\n", vuser->vuid));
288                 invalidate_vuid(vuser->vuid);
289                 return -1;
290         }
291
292         return vuser->vuid;
293 }
294
295
296 /****************************************************************************
297 add a name to the session users list
298 ****************************************************************************/
299 void add_session_user(char *user)
300 {
301   fstring suser;
302   StrnCpy(suser,user,sizeof(suser)-1);
303
304   if (!Get_Pwnam_Modify(suser)) return;
305
306   if (suser && *suser && !in_list(suser,session_users,False))
307     {
308       if (strlen(suser) + strlen(session_users) + 2 >= sizeof(pstring))
309         DEBUG(1,("Too many session users??\n"));
310       else
311         {
312           pstrcat(session_users," ");
313           pstrcat(session_users,suser);
314         }
315     }
316 }
317
318
319 /****************************************************************************
320 check if a username is valid
321 ****************************************************************************/
322 BOOL user_ok(char *user,int snum)
323 {
324         char **valid, **invalid;
325         BOOL ret;
326
327         valid = invalid = NULL;
328         ret = True;
329
330         if (lp_invalid_users(snum)) {
331                 lp_list_copy(&invalid, lp_invalid_users(snum));
332                 if (invalid && lp_list_substitute(invalid, "%S", lp_servicename(snum))) {
333                         ret = !user_in_list(user, invalid);
334                 }
335         }
336         if (invalid) lp_list_free (&invalid);
337
338         if (ret && lp_valid_users(snum)) {
339                 lp_list_copy(&valid, lp_valid_users(snum));
340                 if (valid && lp_list_substitute(valid, "%S", lp_servicename(snum))) {
341                         ret = user_in_list(user,valid);
342                 }
343         }
344         if (valid) lp_list_free (&valid);
345
346         if (ret && lp_onlyuser(snum)) {
347                 char **user_list = lp_list_make (lp_username(snum));
348                 if (user_list && lp_list_substitute(user_list, "%S", lp_servicename(snum))) {
349                         ret = user_in_list(user, user_list);
350                 }
351                 if (user_list) lp_list_free (&user_list);
352         }
353
354         return(ret);
355 }
356
357 /****************************************************************************
358 validate a group username entry. Return the username or NULL
359 ****************************************************************************/
360 static char *validate_group(char *group, DATA_BLOB password,int snum)
361 {
362 #ifdef HAVE_NETGROUP
363         {
364                 char *host, *user, *domain;
365                 setnetgrent(group);
366                 while (getnetgrent(&host, &user, &domain)) {
367                         if (user) {
368                                 if (user_ok(user, snum) && 
369                                     password_ok(user,password)) {
370                                         endnetgrent();
371                                         return(user);
372                                 }
373                         }
374                 }
375                 endnetgrent();
376         }
377 #endif
378   
379 #ifdef HAVE_GETGRENT
380         {
381                 struct group *gptr;
382                 setgrent();
383                 while ((gptr = (struct group *)getgrent())) {
384                         if (strequal(gptr->gr_name,group))
385                                 break;
386                 }
387
388                 /*
389                  * As user_ok can recurse doing a getgrent(), we must
390                  * copy the member list into a pstring on the stack before
391                  * use. Bug pointed out by leon@eatworms.swmed.edu.
392                  */
393
394                 if (gptr) {
395                         pstring member_list;
396                         char *member;
397                         size_t copied_len = 0;
398                         int i;
399
400                         *member_list = '\0';
401                         member = member_list;
402
403                         for(i = 0; gptr->gr_mem && gptr->gr_mem[i]; i++) {
404                                 size_t member_len = strlen(gptr->gr_mem[i]) + 1;
405                                 if( copied_len + member_len < sizeof(pstring)) { 
406
407                                         DEBUG(10,("validate_group: = gr_mem = %s\n", gptr->gr_mem[i]));
408
409                                         safe_strcpy(member, gptr->gr_mem[i], sizeof(pstring) - copied_len - 1);
410                                         copied_len += member_len;
411                                         member += copied_len;
412                                 } else {
413                                         *member = '\0';
414                                 }
415                         }
416
417                         endgrent();
418
419                         member = member_list;
420                         while (*member) {
421                                 static fstring name;
422                                 fstrcpy(name,member);
423                                 if (user_ok(name,snum) &&
424                                     password_ok(name,password)) {
425                                         endgrent();
426                                         return(&name[0]);
427                                 }
428
429                                 DEBUG(10,("validate_group = member = %s\n", member));
430
431                                 member += strlen(member) + 1;
432                         }
433                 } else {
434                         endgrent();
435                         return NULL;
436                 }
437         }
438 #endif
439         return(NULL);
440 }
441
442 /****************************************************************************
443  Check for authority to login to a service with a given username/password.
444  Note this is *NOT* used when logging on using sessionsetup_and_X.
445 ****************************************************************************/
446
447 BOOL authorise_login(int snum,char *user, DATA_BLOB password, 
448                      BOOL *guest,BOOL *force,uint16 vuid)
449 {
450         BOOL ok = False;
451         user_struct *vuser = get_valid_user_struct(vuid);
452
453 #if DEBUG_PASSWORD
454         DEBUG(100,("authorise_login: checking authorisation on user=%s pass=%s\n",
455                         user,password.data));
456 #endif
457
458         *guest = False;
459   
460         if (GUEST_ONLY(snum))
461                 *force = True;
462
463         if (!GUEST_ONLY(snum) && (lp_security() > SEC_SHARE)) {
464
465                 /*
466                  * We should just use the given vuid from a sessionsetup_and_X.
467                  */
468
469                 if (!vuser) {
470                         DEBUG(1,("authorise_login: refusing user %s with no session setup\n",
471                                         user));
472                         return False;
473                 }
474
475                 if (!vuser->guest && user_ok(vuser->user.unix_name,snum)) {
476                         fstrcpy(user,vuser->user.unix_name);
477                         *guest = False;
478                         DEBUG(3,("authorise_login: ACCEPTED: validated uid ok as non-guest \
479 (user=%s)\n", user));
480                         return True;
481                 }
482         }
483  
484         /* there are several possibilities:
485                 1) login as the given user with given password
486                 2) login as a previously registered username with the given password
487                 3) login as a session list username with the given password
488                 4) login as a previously validated user/password pair
489                 5) login as the "user =" user with given password
490                 6) login as the "user =" user with no password (guest connection)
491                 7) login as guest user with no password
492
493                 if the service is guest_only then steps 1 to 5 are skipped
494         */
495
496         if (!(GUEST_ONLY(snum) && GUEST_OK(snum))) {
497                 /* check for a previously registered guest username */
498                 if (!ok && (vuser != 0) && vuser->guest) {        
499                         if (user_ok(vuser->user.unix_name,snum) &&
500                                         password_ok(vuser->user.unix_name, password)) {
501                                 fstrcpy(user, vuser->user.unix_name);
502                                 vuser->guest = False;
503                                 DEBUG(3,("authorise_login: ACCEPTED: given password with registered user %s\n", user));
504                                 ok = True;
505                         }
506                 }
507
508                 /* now check the list of session users */
509                 if (!ok) {
510                         char *auser;
511                         char *user_list = strdup(session_users);
512                         if (!user_list)
513                                 return(False);
514
515                         for (auser=strtok(user_list,LIST_SEP); !ok && auser;
516                                                                         auser = strtok(NULL,LIST_SEP)) {
517                                 fstring user2;
518                                 fstrcpy(user2,auser);
519                                 if (!user_ok(user2,snum))
520                                         continue;
521                   
522                                 if (password_ok(user2,password)) {
523                                         ok = True;
524                                         fstrcpy(user,user2);
525                                         DEBUG(3,("authorise_login: ACCEPTED: session list username (%s) \
526 and given password ok\n", user));
527                                 }
528                         }
529
530                         SAFE_FREE(user_list);
531                 }
532
533                 /* check for a previously validated username/password pair */
534                 if (!ok && (lp_security() > SEC_SHARE) && (vuser != 0) && !vuser->guest &&
535                                                         user_ok(vuser->user.unix_name,snum)) {
536                         fstrcpy(user,vuser->user.unix_name);
537                         *guest = False;
538                         DEBUG(3,("authorise_login: ACCEPTED: validated uid (%s) as non-guest\n",
539                                 user));
540                         ok = True;
541                 }
542
543                 /* check the user= fields and the given password */
544                 if (!ok && lp_username(snum)) {
545                         char *auser;
546                         pstring user_list;
547                         StrnCpy(user_list,lp_username(snum),sizeof(pstring));
548
549                         pstring_sub(user_list,"%S",lp_servicename(snum));
550           
551                         for (auser=strtok(user_list,LIST_SEP); auser && !ok;
552                                                                                         auser = strtok(NULL,LIST_SEP)) {
553                                 if (*auser == '@') {
554                                         auser = validate_group(auser+1,password,snum);
555                                         if (auser) {
556                                                 ok = True;
557                                                 fstrcpy(user,auser);
558                                                 DEBUG(3,("authorise_login: ACCEPTED: group username \
559 and given password ok (%s)\n", user));
560                                         }
561                                 } else {
562                                         fstring user2;
563                                         fstrcpy(user2,auser);
564                                         if (user_ok(user2,snum) && password_ok(user2,password)) {
565                                                 ok = True;
566                                                 fstrcpy(user,user2);
567                                                 DEBUG(3,("authorise_login: ACCEPTED: user list username \
568 and given password ok (%s)\n", user));
569                                         }
570                                 }
571                         }
572                 }
573         } /* not guest only */
574
575         /* check for a normal guest connection */
576         if (!ok && GUEST_OK(snum)) {
577                 fstring guestname;
578                 StrnCpy(guestname,lp_guestaccount(snum),sizeof(guestname)-1);
579                 if (Get_Pwnam(guestname)) {
580                         fstrcpy(user,guestname);
581                         ok = True;
582                         DEBUG(3,("authorise_login: ACCEPTED: guest account and guest ok (%s)\n",
583                                         user));
584                 } else {
585                         DEBUG(0,("authorise_login: Invalid guest account %s??\n",guestname));
586                 }
587                 *guest = True;
588         }
589
590         if (ok && !user_ok(user,snum)) {
591                 DEBUG(0,("authorise_login: rejected invalid user %s\n",user));
592                 ok = False;
593         }
594
595         return(ok);
596 }