This commit was manufactured by cvs2svn to create branch 'SAMBA_3_0'.
[ira/wip.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         SAFE_FREE(vuser->unix_homedir);
68         SAFE_FREE(vuser->logon_script);
69         
70         session_yield(vuser);
71         SAFE_FREE(vuser->session_keystr);
72
73         free_server_info(&vuser->server_info);
74
75         DLIST_REMOVE(validated_users, vuser);
76
77         /* clear the vuid from the 'cache' on each connection, and
78            from the vuid 'owner' of connections */
79         conn_clear_vuid_cache(vuid);
80
81         SAFE_FREE(vuser->groups);
82         delete_nt_token(&vuser->nt_user_token);
83         SAFE_FREE(vuser);
84         num_validated_vuids--;
85 }
86
87 /****************************************************************************
88 invalidate all vuid entries for this process
89 ****************************************************************************/
90 void invalidate_all_vuids(void)
91 {
92         user_struct *usp, *next=NULL;
93
94         for (usp=validated_users;usp;usp=next) {
95                 next = usp->next;
96                 
97                 invalidate_vuid(usp->vuid);
98         }
99 }
100
101 /**
102  *  register that a valid login has been performed, establish 'session'.
103  *  @param server_info The token returned from the authentication process. 
104  *   (now 'owned' by register_vuid)
105  *
106  *  @return Newly allocated vuid, biased by an offset. (This allows us to
107  *   tell random client vuid's (normally zero) from valid vuids.)
108  *
109  */
110
111 int register_vuid(auth_serversupplied_info *server_info, const char *smb_name)
112 {
113         user_struct *vuser = NULL;
114
115         /* Ensure no vuid gets registered in share level security. */
116         if(lp_security() == SEC_SHARE)
117                 return UID_FIELD_INVALID;
118
119         /* Limit allowed vuids to 16bits - VUID_OFFSET. */
120         if (num_validated_vuids >= 0xFFFF-VUID_OFFSET)
121                 return UID_FIELD_INVALID;
122
123         if((vuser = (user_struct *)malloc( sizeof(user_struct) )) == NULL) {
124                 DEBUG(0,("Failed to malloc users struct!\n"));
125                 return UID_FIELD_INVALID;
126         }
127
128         ZERO_STRUCTP(vuser);
129
130         /* Allocate a free vuid. Yes this is a linear search... :-) */
131         while( get_valid_user_struct(next_vuid) != NULL ) {
132                 next_vuid++;
133                 /* Check for vuid wrap. */
134                 if (next_vuid == UID_FIELD_INVALID)
135                         next_vuid = VUID_OFFSET;
136         }
137
138         DEBUG(10,("register_vuid: allocated vuid = %u\n", (unsigned int)next_vuid ));
139
140         vuser->vuid = next_vuid;
141
142         /* the next functions should be done by a SID mapping system (SMS) as
143          * the new real sam db won't have reference to unix uids or gids
144          */
145         if (!IS_SAM_UNIX_USER(server_info->sam_account)) {
146                 DEBUG(0,("Attempted session setup with invalid user.  No uid/gid in SAM_ACCOUNT\n"));
147                 free(vuser);
148                 free_server_info(&server_info);
149                 return UID_FIELD_INVALID;
150         }
151         
152         vuser->uid = pdb_get_uid(server_info->sam_account);
153         vuser->gid = pdb_get_gid(server_info->sam_account);
154         
155         vuser->n_groups = server_info->n_groups;
156         if (vuser->n_groups) {
157                 if (!(vuser->groups = memdup(server_info->groups, sizeof(gid_t) * vuser->n_groups))) {
158                         DEBUG(0,("register_vuid: failed to memdup vuser->groups\n"));
159                         free(vuser);
160                         free_server_info(&server_info);
161                         return UID_FIELD_INVALID;
162                 }
163         }
164
165         vuser->guest = server_info->guest;
166         fstrcpy(vuser->user.unix_name, pdb_get_username(server_info->sam_account)); 
167
168         /* This is a potentially untrusted username */
169         alpha_strcpy(vuser->user.smb_name, smb_name, ". _-$", sizeof(vuser->user.smb_name));
170
171         fstrcpy(vuser->user.domain, pdb_get_domain(server_info->sam_account));
172         fstrcpy(vuser->user.full_name, pdb_get_fullname(server_info->sam_account));
173
174         {
175                 /* Keep the homedir handy */
176                 const char *homedir = pdb_get_homedir(server_info->sam_account);
177                 const char *unix_homedir = pdb_get_unix_homedir(server_info->sam_account);
178                 const char *logon_script = pdb_get_logon_script(server_info->sam_account);
179                 if (homedir) {
180                         vuser->homedir = smb_xstrdup(homedir);
181                 }
182
183                 if (unix_homedir) {
184                         vuser->unix_homedir = smb_xstrdup(unix_homedir);
185                 }
186
187                 if (logon_script) {
188                         vuser->logon_script = smb_xstrdup(logon_script);
189                 }
190         }
191
192         memcpy(vuser->session_key, server_info->session_key, sizeof(vuser->session_key));
193
194         DEBUG(10,("register_vuid: (%u,%u) %s %s %s guest=%d\n", 
195                   (unsigned int)vuser->uid, 
196                   (unsigned int)vuser->gid,
197                   vuser->user.unix_name, vuser->user.smb_name, vuser->user.domain, vuser->guest ));
198
199         DEBUG(3, ("User name: %s\tReal name: %s\n",vuser->user.unix_name,vuser->user.full_name));       
200
201         if (server_info->ptok) {
202                 vuser->nt_user_token = dup_nt_token(server_info->ptok);
203         } else {
204                 DEBUG(1, ("server_info does not contain a user_token - cannot continue\n"));
205                 free_server_info(&server_info);
206                 SAFE_FREE(vuser->homedir);
207                 SAFE_FREE(vuser->unix_homedir);
208                 SAFE_FREE(vuser->logon_script);
209
210                 SAFE_FREE(vuser);
211                 return UID_FIELD_INVALID;
212         }
213
214         /* use this to keep tabs on all our info from the authentication */
215         vuser->server_info = server_info;
216
217         DEBUG(3,("UNIX uid %d is UNIX user %s, and will be vuid %u\n",(int)vuser->uid,vuser->user.unix_name, vuser->vuid));
218
219         next_vuid++;
220         num_validated_vuids++;
221
222         DLIST_ADD(validated_users, vuser);
223
224         if (!session_claim(vuser)) {
225                 DEBUG(1,("Failed to claim session for vuid=%d\n", vuser->vuid));
226                 invalidate_vuid(vuser->vuid);
227                 return -1;
228         }
229
230         /* Register a home dir service for this user */
231         if ((!vuser->guest) && vuser->unix_homedir && *(vuser->unix_homedir)) {
232                 DEBUG(3, ("Adding/updating homes service for user '%s' using home direcotry: '%s'\n", 
233                           vuser->user.unix_name, vuser->unix_homedir));
234                 vuser->homes_snum = add_home_service(vuser->user.unix_name, vuser->user.unix_name, vuser->unix_homedir);          
235         } else {
236                 vuser->homes_snum = -1;
237         }
238         
239         return vuser->vuid;
240 }
241
242
243 /****************************************************************************
244 add a name to the session users list
245 ****************************************************************************/
246 void add_session_user(const char *user)
247 {
248   fstring suser;
249   struct passwd *passwd;
250
251   if (!(passwd = Get_Pwnam(user))) return;
252
253   fstrcpy(suser,passwd->pw_name);
254
255   if (suser && *suser && !in_list(suser,session_users,False))
256     {
257       if (strlen(suser) + strlen(session_users) + 2 >= sizeof(pstring))
258         DEBUG(1,("Too many session users??\n"));
259       else
260         {
261           pstrcat(session_users," ");
262           pstrcat(session_users,suser);
263         }
264     }
265 }
266
267
268 /****************************************************************************
269 check if a username is valid
270 ****************************************************************************/
271 BOOL user_ok(const char *user,int snum, gid_t *groups, size_t n_groups)
272 {
273         char **valid, **invalid;
274         BOOL ret;
275
276         valid = invalid = NULL;
277         ret = True;
278
279         if (lp_invalid_users(snum)) {
280                 str_list_copy(&invalid, lp_invalid_users(snum));
281                 if (invalid && str_list_substitute(invalid, "%S", lp_servicename(snum))) {
282                         ret = !user_in_list(user, (const char **)invalid, groups, n_groups);
283                 }
284         }
285         if (invalid)
286                 str_list_free (&invalid);
287
288         if (ret && lp_valid_users(snum)) {
289                 str_list_copy(&valid, lp_valid_users(snum));
290                 if (valid && str_list_substitute(valid, "%S", lp_servicename(snum))) {
291                         ret = user_in_list(user, (const char **)valid, groups, n_groups);
292                 }
293         }
294         if (valid)
295                 str_list_free (&valid);
296
297         if (ret && lp_onlyuser(snum)) {
298                 char **user_list = str_list_make (lp_username(snum), NULL);
299                 if (user_list && str_list_substitute(user_list, "%S", lp_servicename(snum))) {
300                         ret = user_in_list(user, (const char **)user_list, groups, n_groups);
301                 }
302                 if (user_list) str_list_free (&user_list);
303         }
304
305         return(ret);
306 }
307
308 /****************************************************************************
309 validate a group username entry. Return the username or NULL
310 ****************************************************************************/
311 static char *validate_group(char *group, DATA_BLOB password,int snum)
312 {
313 #ifdef HAVE_NETGROUP
314         {
315                 char *host, *user, *domain;
316                 setnetgrent(group);
317                 while (getnetgrent(&host, &user, &domain)) {
318                         if (user) {
319                                 if (user_ok(user, snum, NULL, 0) && 
320                                     password_ok(user,password)) {
321                                         endnetgrent();
322                                         return(user);
323                                 }
324                         }
325                 }
326                 endnetgrent();
327         }
328 #endif
329   
330 #ifdef HAVE_GETGRENT
331         {
332                 struct group *gptr;
333                 setgrent();
334                 while ((gptr = (struct group *)getgrent())) {
335                         if (strequal(gptr->gr_name,group))
336                                 break;
337                 }
338
339                 /*
340                  * As user_ok can recurse doing a getgrent(), we must
341                  * copy the member list into a pstring on the stack before
342                  * use. Bug pointed out by leon@eatworms.swmed.edu.
343                  */
344
345                 if (gptr) {
346                         pstring member_list;
347                         char *member;
348                         size_t copied_len = 0;
349                         int i;
350
351                         *member_list = '\0';
352                         member = member_list;
353
354                         for(i = 0; gptr->gr_mem && gptr->gr_mem[i]; i++) {
355                                 size_t member_len = strlen(gptr->gr_mem[i]) + 1;
356                                 if( copied_len + member_len < sizeof(pstring)) { 
357
358                                         DEBUG(10,("validate_group: = gr_mem = %s\n", gptr->gr_mem[i]));
359
360                                         safe_strcpy(member, gptr->gr_mem[i], sizeof(pstring) - copied_len - 1);
361                                         copied_len += member_len;
362                                         member += copied_len;
363                                 } else {
364                                         *member = '\0';
365                                 }
366                         }
367
368                         endgrent();
369
370                         member = member_list;
371                         while (*member) {
372                                 static fstring name;
373                                 fstrcpy(name,member);
374                                 if (user_ok(name,snum, NULL, 0) &&
375                                     password_ok(name,password)) {
376                                         endgrent();
377                                         return(&name[0]);
378                                 }
379
380                                 DEBUG(10,("validate_group = member = %s\n", member));
381
382                                 member += strlen(member) + 1;
383                         }
384                 } else {
385                         endgrent();
386                         return NULL;
387                 }
388         }
389 #endif
390         return(NULL);
391 }
392
393 /****************************************************************************
394  Check for authority to login to a service with a given username/password.
395  Note this is *NOT* used when logging on using sessionsetup_and_X.
396 ****************************************************************************/
397
398 BOOL authorise_login(int snum, fstring user, DATA_BLOB password, 
399                      BOOL *guest)
400 {
401         BOOL ok = False;
402         
403 #if DEBUG_PASSWORD
404         DEBUG(100,("authorise_login: checking authorisation on user=%s pass=%s\n",
405                    user,password.data));
406 #endif
407
408         *guest = False;
409   
410         /* there are several possibilities:
411                 1) login as the given user with given password
412                 2) login as a previously registered username with the given password
413                 3) login as a session list username with the given password
414                 4) login as a previously validated user/password pair
415                 5) login as the "user =" user with given password
416                 6) login as the "user =" user with no password (guest connection)
417                 7) login as guest user with no password
418
419                 if the service is guest_only then steps 1 to 5 are skipped
420         */
421
422         /* now check the list of session users */
423         if (!ok) {
424                 char *auser;
425                 char *user_list = strdup(session_users);
426                 if (!user_list)
427                         return(False);
428                 
429                 for (auser=strtok(user_list,LIST_SEP); !ok && auser;
430                      auser = strtok(NULL,LIST_SEP)) {
431                         fstring user2;
432                         fstrcpy(user2,auser);
433                         if (!user_ok(user2,snum, NULL, 0))
434                                 continue;
435                         
436                         if (password_ok(user2,password)) {
437                                 ok = True;
438                                 fstrcpy(user,user2);
439                                 DEBUG(3,("authorise_login: ACCEPTED: session list username (%s) \
440 and given password ok\n", user));
441                         }
442                 }
443                 
444                 SAFE_FREE(user_list);
445         }
446         
447         /* check the user= fields and the given password */
448         if (!ok && lp_username(snum)) {
449                 char *auser;
450                 pstring user_list;
451                 pstrcpy(user_list,lp_username(snum));
452                 
453                 pstring_sub(user_list,"%S",lp_servicename(snum));
454                 
455                 for (auser=strtok(user_list,LIST_SEP); auser && !ok;
456                      auser = strtok(NULL,LIST_SEP)) {
457                         if (*auser == '@') {
458                                 auser = validate_group(auser+1,password,snum);
459                                 if (auser) {
460                                         ok = True;
461                                         fstrcpy(user,auser);
462                                         DEBUG(3,("authorise_login: ACCEPTED: group username \
463 and given password ok (%s)\n", user));
464                                 }
465                         } else {
466                                 fstring user2;
467                                 fstrcpy(user2,auser);
468                                 if (user_ok(user2,snum, NULL, 0) && password_ok(user2,password)) {
469                                         ok = True;
470                                         fstrcpy(user,user2);
471                                         DEBUG(3,("authorise_login: ACCEPTED: user list username \
472 and given password ok (%s)\n", user));
473                                 }
474                         }
475                 }
476         }
477
478         /* check for a normal guest connection */
479         if (!ok && GUEST_OK(snum)) {
480                 fstring guestname;
481                 fstrcpy(guestname,lp_guestaccount());
482                 if (Get_Pwnam(guestname)) {
483                         fstrcpy(user,guestname);
484                         ok = True;
485                         DEBUG(3,("authorise_login: ACCEPTED: guest account and guest ok (%s)\n",
486                                         user));
487                 } else {
488                         DEBUG(0,("authorise_login: Invalid guest account %s??\n",guestname));
489                 }
490                 *guest = True;
491         }
492
493         if (ok && !user_ok(user, snum, NULL, 0)) {
494                 DEBUG(0,("authorise_login: rejected invalid user %s\n",user));
495                 ok = False;
496         }
497
498         return(ok);
499 }