Revert "source3/smbd"
[metze/samba/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    Copyright (C) Jeremy Allison 2007.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "system/passwd.h"
23 #include "smbd/smbd.h"
24 #include "smbd/globals.h"
25 #include "../librpc/gen_ndr/netlogon.h"
26 #include "auth.h"
27 #include "../libcli/security/security.h"
28
29 /* Fix up prototypes for OSX 10.4, where they're missing */
30 #ifndef HAVE_SETNETGRENT_PROTOTYPE
31 extern int setnetgrent(const char* netgroup);
32 #endif
33 #ifndef HAVE_GETNETGRENT_PROTOTYPE
34 extern int getnetgrent(char **host, char **user, char **domain);
35 #endif
36 #ifndef HAVE_ENDNETGRENT_PROTOTYPE
37 extern void endnetgrent(void);
38 #endif
39
40 enum server_allocated_state { SERVER_ALLOCATED_REQUIRED_YES,
41                                 SERVER_ALLOCATED_REQUIRED_NO,
42                                 SERVER_ALLOCATED_REQUIRED_ANY};
43
44 static user_struct *get_valid_user_struct_internal(
45                         struct smbd_server_connection *sconn,
46                         uint16 vuid,
47                         enum server_allocated_state server_allocated)
48 {
49         user_struct *usp;
50         int count=0;
51
52         if (vuid == UID_FIELD_INVALID)
53                 return NULL;
54
55         usp=sconn->users;
56         for (;usp;usp=usp->next,count++) {
57                 if (vuid == usp->vuid) {
58                         switch (server_allocated) {
59                                 case SERVER_ALLOCATED_REQUIRED_YES:
60                                         if (usp->session_info == NULL) {
61                                                 continue;
62                                         }
63                                         break;
64                                 case SERVER_ALLOCATED_REQUIRED_NO:
65                                         if (usp->session_info != NULL) {
66                                                 continue;
67                                         }
68                                 case SERVER_ALLOCATED_REQUIRED_ANY:
69                                         break;
70                         }
71                         if (count > 10) {
72                                 DLIST_PROMOTE(sconn->users, usp);
73                         }
74                         return usp;
75                 }
76         }
77
78         return NULL;
79 }
80
81 /****************************************************************************
82  Check if a uid has been validated, and return an pointer to the user_struct
83  if it has. NULL if not. vuid is biased by an offset. This allows us to
84  tell random client vuid's (normally zero) from valid vuids.
85 ****************************************************************************/
86
87 user_struct *get_valid_user_struct(struct smbd_server_connection *sconn,
88                                    uint16 vuid)
89 {
90         return get_valid_user_struct_internal(sconn, vuid,
91                         SERVER_ALLOCATED_REQUIRED_YES);
92 }
93
94 bool is_partial_auth_vuid(struct smbd_server_connection *sconn, uint16 vuid)
95 {
96         return (get_partial_auth_user_struct(sconn, vuid) != NULL);
97 }
98
99 /****************************************************************************
100  Get the user struct of a partial NTLMSSP login
101 ****************************************************************************/
102
103 user_struct *get_partial_auth_user_struct(struct smbd_server_connection *sconn,
104                                           uint16 vuid)
105 {
106         return get_valid_user_struct_internal(sconn, vuid,
107                         SERVER_ALLOCATED_REQUIRED_NO);
108 }
109
110 /****************************************************************************
111  Invalidate a uid.
112 ****************************************************************************/
113
114 void invalidate_vuid(struct smbd_server_connection *sconn, uint16 vuid)
115 {
116         user_struct *vuser = NULL;
117
118         vuser = get_valid_user_struct_internal(sconn, vuid,
119                         SERVER_ALLOCATED_REQUIRED_ANY);
120         if (vuser == NULL) {
121                 return;
122         }
123
124         session_yield(vuser);
125
126         if (vuser->gensec_security) {
127                 TALLOC_FREE(vuser->gensec_security);
128         }
129
130         DLIST_REMOVE(sconn->users, vuser);
131         SMB_ASSERT(sconn->num_users > 0);
132         sconn->num_users--;
133
134         /* clear the vuid from the 'cache' on each connection, and
135            from the vuid 'owner' of connections */
136         conn_clear_vuid_caches(sconn, vuid);
137
138         TALLOC_FREE(vuser);
139 }
140
141 /****************************************************************************
142  Invalidate all vuid entries for this process.
143 ****************************************************************************/
144
145 void invalidate_all_vuids(struct smbd_server_connection *sconn)
146 {
147         if (sconn->using_smb2) {
148                 return;
149         }
150
151         while (sconn->users != NULL) {
152                 invalidate_vuid(sconn, sconn->users->vuid);
153         }
154 }
155
156 static void increment_next_vuid(uint16_t *vuid)
157 {
158         *vuid += 1;
159
160         /* Check for vuid wrap. */
161         if (*vuid == UID_FIELD_INVALID) {
162                 *vuid = VUID_OFFSET;
163         }
164 }
165
166 /****************************************************
167  Create a new partial auth user struct.
168 *****************************************************/
169
170 int register_initial_vuid(struct smbd_server_connection *sconn)
171 {
172         user_struct *vuser;
173
174         /* Limit allowed vuids to 16bits - VUID_OFFSET. */
175         if (sconn->num_users >= 0xFFFF-VUID_OFFSET) {
176                 return UID_FIELD_INVALID;
177         }
178
179         if((vuser = talloc_zero(NULL, user_struct)) == NULL) {
180                 DEBUG(0,("register_initial_vuid: "
181                                 "Failed to talloc users struct!\n"));
182                 return UID_FIELD_INVALID;
183         }
184
185         /* Allocate a free vuid. Yes this is a linear search... */
186         while( get_valid_user_struct_internal(sconn,
187                         sconn->smb1.sessions.next_vuid,
188                         SERVER_ALLOCATED_REQUIRED_ANY) != NULL ) {
189                 increment_next_vuid(&sconn->smb1.sessions.next_vuid);
190         }
191
192         DEBUG(10,("register_initial_vuid: allocated vuid = %u\n",
193                 (unsigned int)sconn->smb1.sessions.next_vuid ));
194
195         vuser->vuid = sconn->smb1.sessions.next_vuid;
196
197         /*
198          * This happens in an unfinished NTLMSSP session setup. We
199          * need to allocate a vuid between the first and second calls
200          * to NTLMSSP.
201          */
202         increment_next_vuid(&sconn->smb1.sessions.next_vuid);
203
204         sconn->num_users++;
205         DLIST_ADD(sconn->users, vuser);
206
207         return vuser->vuid;
208 }
209
210 int register_homes_share(const char *username)
211 {
212         int result;
213         struct passwd *pwd;
214
215         result = lp_servicenumber(username);
216         if (result != -1) {
217                 DEBUG(3, ("Using static (or previously created) service for "
218                           "user '%s'; path = '%s'\n", username,
219                           lp_pathname(result)));
220                 return result;
221         }
222
223         pwd = Get_Pwnam_alloc(talloc_tos(), username);
224
225         if ((pwd == NULL) || (pwd->pw_dir[0] == '\0')) {
226                 DEBUG(3, ("No home directory defined for user '%s'\n",
227                           username));
228                 TALLOC_FREE(pwd);
229                 return -1;
230         }
231
232         DEBUG(3, ("Adding homes service for user '%s' using home directory: "
233                   "'%s'\n", username, pwd->pw_dir));
234
235         result = add_home_service(username, username, pwd->pw_dir);
236
237         TALLOC_FREE(pwd);
238         return result;
239 }
240
241 /**
242  *  register that a valid login has been performed, establish 'session'.
243  *  @param session_info The token returned from the authentication process.
244  *   (now 'owned' by register_existing_vuid)
245  *
246  *  @param session_key The User session key for the login session (now also
247  *  'owned' by register_existing_vuid)
248  *
249  *  @param respose_blob The NT challenge-response, if available.  (May be
250  *  freed after this call)
251  *
252  *  @param smb_name The untranslated name of the user
253  *
254  *  @return Newly allocated vuid, biased by an offset. (This allows us to
255  *   tell random client vuid's (normally zero) from valid vuids.)
256  *
257  */
258
259 int register_existing_vuid(struct smbd_server_connection *sconn,
260                         uint16 vuid,
261                         struct auth_session_info *session_info,
262                         DATA_BLOB response_blob)
263 {
264         user_struct *vuser;
265         bool guest = security_session_user_level(session_info, NULL) < SECURITY_USER;
266
267         vuser = get_partial_auth_user_struct(sconn, vuid);
268         if (!vuser) {
269                 goto fail;
270         }
271
272         /* Use this to keep tabs on all our info from the authentication */
273         vuser->session_info = talloc_move(vuser, &session_info);
274
275         /* Make clear that we require the optional unix_token and unix_info in the source3 code */
276         SMB_ASSERT(vuser->session_info->unix_token);
277         SMB_ASSERT(vuser->session_info->unix_info);
278
279         DEBUG(10,("register_existing_vuid: (%u,%u) %s %s %s guest=%d\n",
280                   (unsigned int)vuser->session_info->unix_token->uid,
281                   (unsigned int)vuser->session_info->unix_token->gid,
282                   vuser->session_info->unix_info->unix_name,
283                   vuser->session_info->unix_info->sanitized_username,
284                   vuser->session_info->info->domain_name,
285                   guest));
286
287         DEBUG(3, ("register_existing_vuid: User name: %s\t"
288                   "Real name: %s\n", vuser->session_info->unix_info->unix_name,
289                   vuser->session_info->info->full_name ?
290                   vuser->session_info->info->full_name : ""));
291
292         if (!vuser->session_info->security_token) {
293                 DEBUG(1, ("register_existing_vuid: session_info does not "
294                         "contain a user_token - cannot continue\n"));
295                 goto fail;
296         }
297
298         /* Make clear that we require the optional unix_token in the source3 code */
299         SMB_ASSERT(vuser->session_info->unix_token);
300
301         DEBUG(3,("register_existing_vuid: UNIX uid %d is UNIX user %s, "
302                 "and will be vuid %u\n", (int)vuser->session_info->unix_token->uid,
303                  vuser->session_info->unix_info->unix_name, vuser->vuid));
304
305         if (!session_claim(sconn, vuser)) {
306                 DEBUG(1, ("register_existing_vuid: Failed to claim session "
307                         "for vuid=%d\n",
308                         vuser->vuid));
309                 goto fail;
310         }
311
312         /* Register a home dir service for this user if
313         (a) This is not a guest connection,
314         (b) we have a home directory defined
315         (c) there s not an existing static share by that name
316         If a share exists by this name (autoloaded or not) reuse it . */
317
318         vuser->homes_snum = -1;
319
320
321         if (!guest) {
322                 vuser->homes_snum = register_homes_share(
323                         vuser->session_info->unix_info->unix_name);
324         }
325
326         if (srv_is_signing_negotiated(sconn) &&
327             !guest) {
328                 /* Try and turn on server signing on the first non-guest
329                  * sessionsetup. */
330                 srv_set_signing(sconn,
331                                 vuser->session_info->session_key,
332                                 response_blob);
333         }
334
335         /* fill in the current_user_info struct */
336         set_current_user_info(
337                 vuser->session_info->unix_info->sanitized_username,
338                 vuser->session_info->unix_info->unix_name,
339                 vuser->session_info->info->domain_name);
340
341         return vuser->vuid;
342
343   fail:
344
345         if (vuser) {
346                 invalidate_vuid(sconn, vuid);
347         }
348         return UID_FIELD_INVALID;
349 }