(merge from 3.0)
[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 */
254         if ((!vuser->guest) && vuser->unix_homedir && *(vuser->unix_homedir)) {
255                 DEBUG(3, ("Adding/updating homes service for user '%s' using home directory: '%s'\n", 
256                           vuser->user.unix_name, vuser->unix_homedir));
257                 vuser->homes_snum = add_home_service(vuser->user.unix_name, vuser->user.unix_name, vuser->unix_homedir);          
258         } else {
259                 vuser->homes_snum = -1;
260         }
261         
262         if (lp_server_signing() && !vuser->guest && !srv_is_signing_active()) {
263                 /* Try and turn on server signing on the first non-guest sessionsetup. */
264                 srv_set_signing(vuser->session_key, response_blob);
265         }
266
267         return vuser->vuid;
268 }
269
270 /****************************************************************************
271  Add a name to the session users list.
272 ****************************************************************************/
273
274 void add_session_user(const char *user)
275 {
276         fstring suser;
277         struct passwd *passwd;
278
279         if (!(passwd = Get_Pwnam(user)))
280                 return;
281
282         fstrcpy(suser,passwd->pw_name);
283
284         if (suser && *suser && !in_list(suser,session_users,False)) {
285                 if (strlen(suser) + strlen(session_users) + 2 >= sizeof(pstring)) {
286                         DEBUG(1,("Too many session users??\n"));
287                 } else {
288                         pstrcat(session_users," ");
289                         pstrcat(session_users,suser);
290                 }
291         }
292 }
293
294 /****************************************************************************
295  Check if a username is valid.
296 ****************************************************************************/
297
298 BOOL user_ok(const char *user,int snum, gid_t *groups, size_t n_groups)
299 {
300         char **valid, **invalid;
301         BOOL ret;
302
303         valid = invalid = NULL;
304         ret = True;
305
306         if (lp_invalid_users(snum)) {
307                 str_list_copy(&invalid, lp_invalid_users(snum));
308                 if (invalid && str_list_substitute(invalid, "%S", lp_servicename(snum))) {
309                         if ( invalid && str_list_sub_basic(invalid, current_user_info.smb_name) ) {
310                                 ret = !user_in_list(user, (const char **)invalid, groups, n_groups);
311                         }
312                 }
313         }
314         if (invalid)
315                 str_list_free (&invalid);
316
317         if (ret && lp_valid_users(snum)) {
318                 str_list_copy(&valid, lp_valid_users(snum));
319                 if ( valid && str_list_substitute(valid, "%S", lp_servicename(snum)) ) {
320                         if ( valid && str_list_sub_basic(valid, current_user_info.smb_name) ) {
321                                 ret = user_in_list(user, (const char **)valid, groups, n_groups);
322                         }
323                 }
324         }
325         if (valid)
326                 str_list_free (&valid);
327
328         if (ret && lp_onlyuser(snum)) {
329                 char **user_list = str_list_make (lp_username(snum), NULL);
330                 if (user_list && str_list_substitute(user_list, "%S", lp_servicename(snum))) {
331                         ret = user_in_list(user, (const char **)user_list, groups, n_groups);
332                 }
333                 if (user_list) str_list_free (&user_list);
334         }
335
336         return(ret);
337 }
338
339 /****************************************************************************
340  Validate a group username entry. Return the username or NULL.
341 ****************************************************************************/
342
343 static char *validate_group(char *group, DATA_BLOB password,int snum)
344 {
345 #ifdef HAVE_NETGROUP
346         {
347                 char *host, *user, *domain;
348                 setnetgrent(group);
349                 while (getnetgrent(&host, &user, &domain)) {
350                         if (user) {
351                                 if (user_ok(user, snum, NULL, 0) && 
352                                     password_ok(user,password)) {
353                                         endnetgrent();
354                                         return(user);
355                                 }
356                         }
357                 }
358                 endnetgrent();
359         }
360 #endif
361   
362 #ifdef HAVE_GETGRENT
363         {
364                 struct group *gptr;
365                 setgrent();
366                 while ((gptr = (struct group *)getgrent())) {
367                         if (strequal(gptr->gr_name,group))
368                                 break;
369                 }
370
371                 /*
372                  * As user_ok can recurse doing a getgrent(), we must
373                  * copy the member list into a pstring on the stack before
374                  * use. Bug pointed out by leon@eatworms.swmed.edu.
375                  */
376
377                 if (gptr) {
378                         pstring member_list;
379                         char *member;
380                         size_t copied_len = 0;
381                         int i;
382
383                         *member_list = '\0';
384                         member = member_list;
385
386                         for(i = 0; gptr->gr_mem && gptr->gr_mem[i]; i++) {
387                                 size_t member_len = strlen(gptr->gr_mem[i]) + 1;
388                                 if( copied_len + member_len < sizeof(pstring)) { 
389
390                                         DEBUG(10,("validate_group: = gr_mem = %s\n", gptr->gr_mem[i]));
391
392                                         safe_strcpy(member, gptr->gr_mem[i], sizeof(pstring) - copied_len - 1);
393                                         copied_len += member_len;
394                                         member += copied_len;
395                                 } else {
396                                         *member = '\0';
397                                 }
398                         }
399
400                         endgrent();
401
402                         member = member_list;
403                         while (*member) {
404                                 static fstring name;
405                                 fstrcpy(name,member);
406                                 if (user_ok(name,snum, NULL, 0) &&
407                                     password_ok(name,password)) {
408                                         endgrent();
409                                         return(&name[0]);
410                                 }
411
412                                 DEBUG(10,("validate_group = member = %s\n", member));
413
414                                 member += strlen(member) + 1;
415                         }
416                 } else {
417                         endgrent();
418                         return NULL;
419                 }
420         }
421 #endif
422         return(NULL);
423 }
424
425 /****************************************************************************
426  Check for authority to login to a service with a given username/password.
427  Note this is *NOT* used when logging on using sessionsetup_and_X.
428 ****************************************************************************/
429
430 BOOL authorise_login(int snum, fstring user, DATA_BLOB password, 
431                      BOOL *guest)
432 {
433         BOOL ok = False;
434         
435 #if DEBUG_PASSWORD
436         DEBUG(100,("authorise_login: checking authorisation on user=%s pass=%s\n",
437                    user,password.data));
438 #endif
439
440         *guest = False;
441   
442         /* there are several possibilities:
443                 1) login as the given user with given password
444                 2) login as a previously registered username with the given password
445                 3) login as a session list username with the given password
446                 4) login as a previously validated user/password pair
447                 5) login as the "user =" user with given password
448                 6) login as the "user =" user with no password (guest connection)
449                 7) login as guest user with no password
450
451                 if the service is guest_only then steps 1 to 5 are skipped
452         */
453
454         /* now check the list of session users */
455         if (!ok) {
456                 char *auser;
457                 char *user_list = strdup(session_users);
458                 if (!user_list)
459                         return(False);
460                 
461                 for (auser=strtok(user_list,LIST_SEP); !ok && auser;
462                      auser = strtok(NULL,LIST_SEP)) {
463                         fstring user2;
464                         fstrcpy(user2,auser);
465                         if (!user_ok(user2,snum, NULL, 0))
466                                 continue;
467                         
468                         if (password_ok(user2,password)) {
469                                 ok = True;
470                                 fstrcpy(user,user2);
471                                 DEBUG(3,("authorise_login: ACCEPTED: session list username (%s) \
472 and given password ok\n", user));
473                         }
474                 }
475                 
476                 SAFE_FREE(user_list);
477         }
478         
479         /* check the user= fields and the given password */
480         if (!ok && lp_username(snum)) {
481                 char *auser;
482                 pstring user_list;
483                 pstrcpy(user_list,lp_username(snum));
484                 
485                 pstring_sub(user_list,"%S",lp_servicename(snum));
486                 
487                 for (auser=strtok(user_list,LIST_SEP); auser && !ok;
488                      auser = strtok(NULL,LIST_SEP)) {
489                         if (*auser == '@') {
490                                 auser = validate_group(auser+1,password,snum);
491                                 if (auser) {
492                                         ok = True;
493                                         fstrcpy(user,auser);
494                                         DEBUG(3,("authorise_login: ACCEPTED: group username \
495 and given password ok (%s)\n", user));
496                                 }
497                         } else {
498                                 fstring user2;
499                                 fstrcpy(user2,auser);
500                                 if (user_ok(user2,snum, NULL, 0) && password_ok(user2,password)) {
501                                         ok = True;
502                                         fstrcpy(user,user2);
503                                         DEBUG(3,("authorise_login: ACCEPTED: user list username \
504 and given password ok (%s)\n", user));
505                                 }
506                         }
507                 }
508         }
509
510         /* check for a normal guest connection */
511         if (!ok && GUEST_OK(snum)) {
512                 fstring guestname;
513                 fstrcpy(guestname,lp_guestaccount());
514                 if (Get_Pwnam(guestname)) {
515                         fstrcpy(user,guestname);
516                         ok = True;
517                         DEBUG(3,("authorise_login: ACCEPTED: guest account and guest ok (%s)\n",
518                                         user));
519                 } else {
520                         DEBUG(0,("authorise_login: Invalid guest account %s??\n",guestname));
521                 }
522                 *guest = True;
523         }
524
525         if (ok && !user_ok(user, snum, NULL, 0)) {
526                 DEBUG(0,("authorise_login: rejected invalid user %s\n",user));
527                 ok = False;
528         }
529
530         return(ok);
531 }