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