r622: removed some unused functions to make smbd compile again after
[gd/samba-autobuild/.git] / source4 / smb_server / 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
24 /****************************************************************************
25 check if a uid has been validated, and return an pointer to the user_struct
26 if it has. NULL if not. vuid is biased by an offset. This allows us to
27 tell random client vuid's (normally zero) from valid vuids.
28 ****************************************************************************/
29 struct user_struct *get_valid_user_struct(struct server_context *smb, uint16 vuid)
30 {
31         user_struct *usp;
32         int count=0;
33
34         if (vuid == UID_FIELD_INVALID)
35                 return NULL;
36
37         for (usp=smb->users.validated_users;usp;usp=usp->next,count++) {
38                 if (vuid == usp->vuid) {
39                         if (count > 10) {
40                                 DLIST_PROMOTE(smb->users.validated_users, usp);
41                         }
42                         return usp;
43                 }
44         }
45
46         return NULL;
47 }
48
49 /****************************************************************************
50 invalidate a uid
51 ****************************************************************************/
52 void invalidate_vuid(struct server_context *smb, uint16 vuid)
53 {
54         user_struct *vuser = get_valid_user_struct(smb, vuid);
55
56         if (vuser == NULL)
57                 return;
58         
59         data_blob_free(&vuser->session_key);
60
61         session_yield(vuser);
62
63         free_server_info(&vuser->server_info);
64
65         DLIST_REMOVE(smb->users.validated_users, vuser);
66
67         /* clear the vuid from the 'cache' on each connection, and
68            from the vuid 'owner' of connections */
69         /* REWRITE: conn_clear_vuid_cache(smb, vuid); */
70
71         delete_nt_token(&vuser->nt_user_token);
72         SAFE_FREE(vuser);
73         smb->users.num_validated_vuids--;
74 }
75
76 /****************************************************************************
77 invalidate all vuid entries for this process
78 ****************************************************************************/
79 void invalidate_all_vuids(struct server_context *smb)
80 {
81         user_struct *usp, *next=NULL;
82
83         for (usp=smb->users.validated_users;usp;usp=next) {
84                 next = usp->next;
85                 
86                 invalidate_vuid(smb, usp->vuid);
87         }
88 }
89
90 /**
91  *  register that a valid login has been performed, establish 'session'.
92  *  @param server_info The token returned from the authentication process. 
93  *   (now 'owned' by register_vuid)
94  *
95  *  @param session_key The User session key for the login session (now also 'owned' by register_vuid)
96  *
97  *  @param smb_name The untranslated name of the user
98  *
99  *  @return Newly allocated vuid, biased by an offset. (This allows us to
100  *   tell random client vuid's (normally zero) from valid vuids.)
101  *
102  */
103
104 int register_vuid(struct server_context *smb,
105                   struct auth_serversupplied_info *server_info, 
106                   DATA_BLOB *session_key,
107                   const char *smb_name)
108 {
109         user_struct *vuser = NULL;
110
111         /* Ensure no vuid gets registered in share level security. */
112         if(lp_security() == SEC_SHARE)
113                 return UID_FIELD_INVALID;
114
115         /* Limit allowed vuids to 16bits - VUID_OFFSET. */
116         if (smb->users.num_validated_vuids >= 0xFFFF-VUID_OFFSET)
117                 return UID_FIELD_INVALID;
118
119         if((vuser = (user_struct *)malloc( sizeof(user_struct) )) == NULL) {
120                 DEBUG(0,("Failed to malloc users struct!\n"));
121                 return UID_FIELD_INVALID;
122         }
123
124         ZERO_STRUCTP(vuser);
125
126         /* Allocate a free vuid. Yes this is a linear search... :-) */
127         while (get_valid_user_struct(smb, smb->users.next_vuid) != NULL ) {
128                 smb->users.next_vuid++;
129                 /* Check for vuid wrap. */
130                 if (smb->users.next_vuid == UID_FIELD_INVALID)
131                         smb->users.next_vuid = VUID_OFFSET;
132         }
133
134         DEBUG(10,("register_vuid: allocated vuid = %u\n", 
135                   (unsigned int)smb->users.next_vuid));
136
137         vuser->vuid = smb->users.next_vuid;
138
139         vuser->guest = server_info->guest;
140
141         vuser->session_key = *session_key;
142
143         DEBUG(10,("register_vuid: guest=%d\n", vuser->guest ));
144
145         if (server_info->ptok) {
146                 vuser->nt_user_token = dup_nt_token(server_info->ptok);
147         } else {
148                 DEBUG(1, ("server_info does not contain a user_token - cannot continue\n"));
149                 free_server_info(&server_info);
150
151                 SAFE_FREE(vuser);
152                 return UID_FIELD_INVALID;
153         }
154
155         /* use this to keep tabs on all our info from the authentication */
156         vuser->server_info = server_info;
157
158         smb->users.next_vuid++;
159         smb->users.num_validated_vuids++;
160
161         DLIST_ADD(smb->users.validated_users, vuser);
162
163         if (!session_claim(smb, vuser)) {
164                 DEBUG(1,("Failed to claim session for vuid=%d\n", vuser->vuid));
165                 invalidate_vuid(smb, vuser->vuid);
166                 return -1;
167         }
168
169         return vuser->vuid;
170 }
171
172
173 /****************************************************************************
174 add a name to the session users list
175 ****************************************************************************/
176 void add_session_user(struct server_context *smb, const char *user)
177 {
178         char *suser;
179         struct passwd *passwd;
180
181         if (!(passwd = Get_Pwnam(user))) return;
182
183         suser = strdup(passwd->pw_name);
184         if (!suser) {
185                 return;
186         }
187
188         if (suser && *suser && !in_list(suser,smb->users.session_users,False)) {
189                 char *p;
190                 if (!smb->users.session_users) {
191                         asprintf(&p, "%s", suser);
192                 } else {
193                         asprintf(&p, "%s %s", smb->users.session_users, suser);
194                 }
195                 SAFE_FREE(smb->users.session_users);
196                 smb->users.session_users = p;
197         }
198
199         free(suser);
200 }
201
202
203 /****************************************************************************
204 check if a username is valid
205 ****************************************************************************/
206 BOOL user_ok(const char *user,int snum, gid_t *groups, size_t n_groups)
207 {
208         char **valid, **invalid;
209         BOOL ret;
210
211         valid = invalid = NULL;
212         ret = True;
213
214         if (lp_invalid_users(snum)) {
215                 str_list_copy(&invalid, lp_invalid_users(snum));
216                 if (invalid && str_list_substitute(invalid, "%S", lp_servicename(snum))) {
217                         ret = !user_in_list(user, (const char **)invalid, groups, n_groups);
218                 }
219         }
220         if (invalid)
221                 str_list_free (&invalid);
222
223         if (ret && lp_valid_users(snum)) {
224                 str_list_copy(&valid, lp_valid_users(snum));
225                 if (valid && str_list_substitute(valid, "%S", lp_servicename(snum))) {
226                         ret = user_in_list(user, (const char **)valid, groups, n_groups);
227                 }
228         }
229         if (valid)
230                 str_list_free (&valid);
231
232         if (ret && lp_onlyuser(snum)) {
233                 char **user_list = str_list_make (lp_username(snum), NULL);
234                 if (user_list && str_list_substitute(user_list, "%S", lp_servicename(snum))) {
235                         ret = user_in_list(user, (const char **)user_list, groups, n_groups);
236                 }
237                 if (user_list) str_list_free (&user_list);
238         }
239
240         return(ret);
241 }