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