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