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