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