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