Removed 'extern int DEBUGLEVEL' as it is now in the smb.h header.
[samba.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)
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 ((token->user_sids = (DOM_SID *)malloc( num_sids*sizeof(DOM_SID))) == NULL) {
141                 SAFE_FREE(token);
142                 return NULL;
143         }
144
145         psids = token->user_sids;
146
147         /*
148          * Note - user SID *MUST* be first in token !
149          * se_access_check depends on this.
150          */
151
152         uid_to_sid( &psids[psid_ndx++], uid);
153
154         /*
155          * Primary group SID is second in token. Convention.
156          */
157
158         gid_to_sid( &psids[psid_ndx++], gid);
159
160         /* Now add the group SIDs. */
161
162         for (i = 0; i < ngroups; i++) {
163                 if (groups[i] != gid) {
164                         gid_to_sid( &psids[psid_ndx++], groups[i]);
165                 }
166         }
167
168         /*
169          * Finally add the "standard" SIDs.
170          * The only difference between guest and "anonymous" (which we
171          * don't really support) is the addition of Authenticated_Users.
172          */
173
174         sid_copy( &psids[psid_ndx++], &global_sid_World);
175         sid_copy( &psids[psid_ndx++], &global_sid_Network);
176
177         if (is_guest)
178                 sid_copy( &psids[psid_ndx++], &global_sid_Builtin_Guests);
179         else
180                 sid_copy( &psids[psid_ndx++], &global_sid_Authenticated_Users);
181
182         token->num_sids = psid_ndx;
183
184         /* Dump list of sids in token */
185
186         for (i = 0; i < token->num_sids; i++) {
187                 DEBUG(5, ("user token sid %s\n", 
188                           sid_to_string(sid_str, &token->user_sids[i])));
189         }
190
191         return token;
192 }
193
194 /****************************************************************************
195 register a uid/name pair as being valid and that a valid password
196 has been given. vuid is biased by an offset. This allows us to
197 tell random client vuid's (normally zero) from valid vuids.
198 ****************************************************************************/
199
200 int register_vuid(uid_t uid,gid_t gid, char *unix_name, char *requested_name, 
201                   char *domain,BOOL guest, char *full_name)
202 {
203         user_struct *vuser = NULL;
204
205         /* Ensure no vuid gets registered in share level security. */
206         if(lp_security() == SEC_SHARE)
207                 return UID_FIELD_INVALID;
208
209         /* Limit allowed vuids to 16bits - VUID_OFFSET. */
210         if (num_validated_vuids >= 0xFFFF-VUID_OFFSET)
211                 return UID_FIELD_INVALID;
212
213         if((vuser = (user_struct *)malloc( sizeof(user_struct) )) == NULL) {
214                 DEBUG(0,("Failed to malloc users struct!\n"));
215                 return UID_FIELD_INVALID;
216         }
217
218         ZERO_STRUCTP(vuser);
219
220         DEBUG(10,("register_vuid: (%u,%u) %s %s %s guest=%d\n", (unsigned int)uid, (unsigned int)gid,
221                                 unix_name, requested_name, domain, guest ));
222
223         /* Allocate a free vuid. Yes this is a linear search... :-) */
224         while( get_valid_user_struct(next_vuid) != NULL ) {
225                 next_vuid++;
226                 /* Check for vuid wrap. */
227                 if (next_vuid == UID_FIELD_INVALID)
228                         next_vuid = VUID_OFFSET;
229         }
230
231         DEBUG(10,("register_vuid: allocated vuid = %u\n", (unsigned int)next_vuid ));
232
233         vuser->vuid = next_vuid;
234         vuser->uid = uid;
235         vuser->gid = gid;
236         vuser->guest = guest;
237         fstrcpy(vuser->user.unix_name,unix_name);
238         fstrcpy(vuser->user.smb_name,requested_name);
239         fstrcpy(vuser->user.domain,domain);
240         fstrcpy(vuser->user.full_name, full_name);
241         DEBUG(3, ("User name: %s\tReal name: %s\n",vuser->user.unix_name,vuser->user.full_name));       
242
243         vuser->n_groups = 0;
244         vuser->groups  = NULL;
245
246         /* Find all the groups this uid is in and store them. 
247                 Used by become_user() */
248         initialise_groups(vuser->user.unix_name, vuser->uid, vuser->gid);
249         get_current_groups( &vuser->n_groups, &vuser->groups);
250
251         /* Create an NT_USER_TOKEN struct for this user. */
252         vuser->nt_user_token = create_nt_token(vuser->uid, vuser->gid, vuser->n_groups, vuser->groups, guest);
253
254         DEBUG(3,("uid %d registered to name %s\n",(int)vuser->uid,vuser->user.unix_name));
255
256         next_vuid++;
257         num_validated_vuids++;
258
259         DLIST_ADD(validated_users, vuser);
260
261         if (!session_claim(vuser->vuid)) {
262                 DEBUG(1,("Failed to claim session for vuid=%d\n", vuser->vuid));
263                 invalidate_vuid(vuser->vuid);
264                 return -1;
265         }
266
267         return vuser->vuid;
268 }
269
270
271 /****************************************************************************
272 add a name to the session users list
273 ****************************************************************************/
274 void add_session_user(char *user)
275 {
276   fstring suser;
277   StrnCpy(suser,user,sizeof(suser)-1);
278
279   if (!Get_Pwnam(suser,True)) return;
280
281   if (suser && *suser && !in_list(suser,session_users,False))
282     {
283       if (strlen(suser) + strlen(session_users) + 2 >= sizeof(pstring))
284         DEBUG(1,("Too many session users??\n"));
285       else
286         {
287           pstrcat(session_users," ");
288           pstrcat(session_users,suser);
289         }
290     }
291 }
292
293
294 /****************************************************************************
295 check if a username is valid
296 ****************************************************************************/
297 BOOL user_ok(char *user,int snum)
298 {
299         char **valid, **invalid;
300         BOOL ret;
301
302         valid = invalid = NULL;
303         ret = True;
304
305         if (lp_invalid_users(snum)) {
306                 lp_list_copy(&invalid, lp_invalid_users(snum));
307                 if (invalid && lp_list_substitute(invalid, "%S", lp_servicename(snum))) {
308                         ret = !user_in_list(user, invalid);
309                 }
310         }
311         if (invalid) lp_list_free (&invalid);
312
313         if (ret && lp_valid_users(snum)) {
314                 lp_list_copy(&valid, lp_valid_users(snum));
315                 if (valid && lp_list_substitute(valid, "%S", lp_servicename(snum))) {
316                         ret = user_in_list(user,valid);
317                 }
318         }
319         if (valid) lp_list_free (&valid);
320
321         if (ret && lp_onlyuser(snum)) {
322                 char **user_list = lp_list_make (lp_username(snum));
323                 if (user_list && lp_list_substitute(user_list, "%S", lp_servicename(snum))) {
324                         ret = user_in_list(user, user_list);
325                 }
326                 if (user_list) lp_list_free (&user_list);
327         }
328
329         return(ret);
330 }
331
332 /****************************************************************************
333 validate a group username entry. Return the username or NULL
334 ****************************************************************************/
335 static char *validate_group(char *group,char *password,int pwlen,int snum)
336 {
337 #ifdef HAVE_NETGROUP
338         {
339                 char *host, *user, *domain;
340                 setnetgrent(group);
341                 while (getnetgrent(&host, &user, &domain)) {
342                         if (user) {
343                                 if (user_ok(user, snum) && 
344                                     password_ok(user,password,pwlen)) {
345                                         endnetgrent();
346                                         return(user);
347                                 }
348                         }
349                 }
350                 endnetgrent();
351         }
352 #endif
353   
354 #ifdef HAVE_GETGRENT
355         {
356                 struct group *gptr;
357                 setgrent();
358                 while ((gptr = (struct group *)getgrent())) {
359                         if (strequal(gptr->gr_name,group))
360                                 break;
361                 }
362
363                 /*
364                  * As user_ok can recurse doing a getgrent(), we must
365                  * copy the member list into a pstring on the stack before
366                  * use. Bug pointed out by leon@eatworms.swmed.edu.
367                  */
368
369                 if (gptr) {
370                         pstring member_list;
371                         char *member;
372                         size_t copied_len = 0;
373                         int i;
374
375                         *member_list = '\0';
376                         member = member_list;
377
378                         for(i = 0; gptr->gr_mem && gptr->gr_mem[i]; i++) {
379                                 size_t member_len = strlen(gptr->gr_mem[i]) + 1;
380                                 if( copied_len + member_len < sizeof(pstring)) { 
381
382                                         DEBUG(10,("validate_group: = gr_mem = %s\n", gptr->gr_mem[i]));
383
384                                         safe_strcpy(member, gptr->gr_mem[i], sizeof(pstring) - copied_len - 1);
385                                         copied_len += member_len;
386                                         member += copied_len;
387                                 } else {
388                                         *member = '\0';
389                                 }
390                         }
391
392                         endgrent();
393
394                         member = member_list;
395                         while (*member) {
396                                 static fstring name;
397                                 fstrcpy(name,member);
398                                 if (user_ok(name,snum) &&
399                                     password_ok(name,password,pwlen)) {
400                                         endgrent();
401                                         return(&name[0]);
402                                 }
403
404                                 DEBUG(10,("validate_group = member = %s\n", member));
405
406                                 member += strlen(member) + 1;
407                         }
408                 } else {
409                         endgrent();
410                         return NULL;
411                 }
412         }
413 #endif
414         return(NULL);
415 }
416
417 /****************************************************************************
418  Check for authority to login to a service with a given username/password.
419  Note this is *NOT* used when logging on using sessionsetup_and_X.
420 ****************************************************************************/
421
422 BOOL authorise_login(int snum,char *user,char *password, int pwlen, 
423                      BOOL *guest,BOOL *force,uint16 vuid)
424 {
425         BOOL ok = False;
426         user_struct *vuser = get_valid_user_struct(vuid);
427
428 #if DEBUG_PASSWORD
429         DEBUG(100,("authorise_login: checking authorisation on user=%s pass=%s\n",
430                         user,password));
431 #endif
432
433         *guest = False;
434   
435         if (GUEST_ONLY(snum))
436                 *force = True;
437
438         if (!GUEST_ONLY(snum) && (lp_security() > SEC_SHARE)) {
439
440                 /*
441                  * We should just use the given vuid from a sessionsetup_and_X.
442                  */
443
444                 if (!vuser) {
445                         DEBUG(1,("authorise_login: refusing user %s with no session setup\n",
446                                         user));
447                         return False;
448                 }
449
450                 if (!vuser->guest && user_ok(vuser->user.unix_name,snum)) {
451                         fstrcpy(user,vuser->user.unix_name);
452                         *guest = False;
453                         DEBUG(3,("authorise_login: ACCEPTED: validated uid ok as non-guest \
454 (user=%s)\n", user));
455                         return True;
456                 }
457         }
458  
459         /* there are several possibilities:
460                 1) login as the given user with given password
461                 2) login as a previously registered username with the given password
462                 3) login as a session list username with the given password
463                 4) login as a previously validated user/password pair
464                 5) login as the "user =" user with given password
465                 6) login as the "user =" user with no password (guest connection)
466                 7) login as guest user with no password
467
468                 if the service is guest_only then steps 1 to 5 are skipped
469         */
470
471         if (!(GUEST_ONLY(snum) && GUEST_OK(snum))) {
472                 /* check for a previously registered guest username */
473                 if (!ok && (vuser != 0) && vuser->guest) {        
474                         if (user_ok(vuser->user.unix_name,snum) &&
475                                         password_ok(vuser->user.unix_name, password, pwlen)) {
476                                 fstrcpy(user, vuser->user.unix_name);
477                                 vuser->guest = False;
478                                 DEBUG(3,("authorise_login: ACCEPTED: given password with registered user %s\n", user));
479                                 ok = True;
480                         }
481                 }
482
483                 /* now check the list of session users */
484                 if (!ok) {
485                         char *auser;
486                         char *user_list = strdup(session_users);
487                         if (!user_list)
488                                 return(False);
489
490                         for (auser=strtok(user_list,LIST_SEP); !ok && auser;
491                                                                         auser = strtok(NULL,LIST_SEP)) {
492                                 fstring user2;
493                                 fstrcpy(user2,auser);
494                                 if (!user_ok(user2,snum))
495                                         continue;
496                   
497                                 if (password_ok(user2,password, pwlen)) {
498                                         ok = True;
499                                         fstrcpy(user,user2);
500                                         DEBUG(3,("authorise_login: ACCEPTED: session list username (%s) \
501 and given password ok\n", user));
502                                 }
503                         }
504
505                         SAFE_FREE(user_list);
506                 }
507
508                 /* check for a previously validated username/password pair */
509                 if (!ok && (lp_security() > SEC_SHARE) && (vuser != 0) && !vuser->guest &&
510                                                         user_ok(vuser->user.unix_name,snum)) {
511                         fstrcpy(user,vuser->user.unix_name);
512                         *guest = False;
513                         DEBUG(3,("authorise_login: ACCEPTED: validated uid (%s) as non-guest\n",
514                                 user));
515                         ok = True;
516                 }
517
518                 /* check the user= fields and the given password */
519                 if (!ok && lp_username(snum)) {
520                         char *auser;
521                         pstring user_list;
522                         StrnCpy(user_list,lp_username(snum),sizeof(pstring));
523
524                         pstring_sub(user_list,"%S",lp_servicename(snum));
525           
526                         for (auser=strtok(user_list,LIST_SEP); auser && !ok;
527                                                                                         auser = strtok(NULL,LIST_SEP)) {
528                                 if (*auser == '@') {
529                                         auser = validate_group(auser+1,password,pwlen,snum);
530                                         if (auser) {
531                                                 ok = True;
532                                                 fstrcpy(user,auser);
533                                                 DEBUG(3,("authorise_login: ACCEPTED: group username \
534 and given password ok (%s)\n", user));
535                                         }
536                                 } else {
537                                         fstring user2;
538                                         fstrcpy(user2,auser);
539                                         if (user_ok(user2,snum) && password_ok(user2,password,pwlen)) {
540                                                 ok = True;
541                                                 fstrcpy(user,user2);
542                                                 DEBUG(3,("authorise_login: ACCEPTED: user list username \
543 and given password ok (%s)\n", user));
544                                         }
545                                 }
546                         }
547                 }
548         } /* not guest only */
549
550         /* check for a normal guest connection */
551         if (!ok && GUEST_OK(snum)) {
552                 fstring guestname;
553                 StrnCpy(guestname,lp_guestaccount(snum),sizeof(guestname)-1);
554                 if (Get_Pwnam(guestname,True)) {
555                         fstrcpy(user,guestname);
556                         ok = True;
557                         DEBUG(3,("authorise_login: ACCEPTED: guest account and guest ok (%s)\n",
558                                         user));
559                 } else {
560                         DEBUG(0,("authorise_login: Invalid guest account %s??\n",guestname));
561                 }
562                 *guest = True;
563         }
564
565         if (ok && !user_ok(user,snum)) {
566                 DEBUG(0,("authorise_login: rejected invalid user %s\n",user));
567                 ok = False;
568         }
569
570         return(ok);
571 }