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