s3-auth: Remove security=share (depricated since 3.6).
[gd/samba-autobuild/.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->smb1.sessions.validated_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->smb1.sessions.validated_users,
73                                               usp);
74                         }
75                         return usp;
76                 }
77         }
78
79         return NULL;
80 }
81
82 /****************************************************************************
83  Check if a uid has been validated, and return an pointer to the user_struct
84  if it has. NULL if not. vuid is biased by an offset. This allows us to
85  tell random client vuid's (normally zero) from valid vuids.
86 ****************************************************************************/
87
88 user_struct *get_valid_user_struct(struct smbd_server_connection *sconn,
89                                    uint16 vuid)
90 {
91         return get_valid_user_struct_internal(sconn, vuid,
92                         SERVER_ALLOCATED_REQUIRED_YES);
93 }
94
95 bool is_partial_auth_vuid(struct smbd_server_connection *sconn, uint16 vuid)
96 {
97         return (get_partial_auth_user_struct(sconn, vuid) != NULL);
98 }
99
100 /****************************************************************************
101  Get the user struct of a partial NTLMSSP login
102 ****************************************************************************/
103
104 user_struct *get_partial_auth_user_struct(struct smbd_server_connection *sconn,
105                                           uint16 vuid)
106 {
107         return get_valid_user_struct_internal(sconn, vuid,
108                         SERVER_ALLOCATED_REQUIRED_NO);
109 }
110
111 /****************************************************************************
112  Invalidate a uid.
113 ****************************************************************************/
114
115 void invalidate_vuid(struct smbd_server_connection *sconn, uint16 vuid)
116 {
117         user_struct *vuser = NULL;
118
119         vuser = get_valid_user_struct_internal(sconn, vuid,
120                         SERVER_ALLOCATED_REQUIRED_ANY);
121         if (vuser == NULL) {
122                 return;
123         }
124
125         session_yield(vuser);
126
127         if (vuser->gensec_security) {
128                 TALLOC_FREE(vuser->gensec_security);
129         }
130
131         DLIST_REMOVE(sconn->smb1.sessions.validated_users, vuser);
132
133         /* clear the vuid from the 'cache' on each connection, and
134            from the vuid 'owner' of connections */
135         conn_clear_vuid_caches(sconn, vuid);
136
137         TALLOC_FREE(vuser);
138         sconn->smb1.sessions.num_validated_vuids--;
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->smb1.sessions.validated_users != NULL) {
152                 invalidate_vuid(sconn,
153                                 sconn->smb1.sessions.validated_users->vuid);
154         }
155 }
156
157 static void increment_next_vuid(uint16_t *vuid)
158 {
159         *vuid += 1;
160
161         /* Check for vuid wrap. */
162         if (*vuid == UID_FIELD_INVALID) {
163                 *vuid = VUID_OFFSET;
164         }
165 }
166
167 /****************************************************
168  Create a new partial auth user struct.
169 *****************************************************/
170
171 int register_initial_vuid(struct smbd_server_connection *sconn)
172 {
173         user_struct *vuser;
174
175         /* Limit allowed vuids to 16bits - VUID_OFFSET. */
176         if (sconn->smb1.sessions.num_validated_vuids >= 0xFFFF-VUID_OFFSET) {
177                 return UID_FIELD_INVALID;
178         }
179
180         if((vuser = talloc_zero(NULL, user_struct)) == NULL) {
181                 DEBUG(0,("register_initial_vuid: "
182                                 "Failed to talloc users struct!\n"));
183                 return UID_FIELD_INVALID;
184         }
185
186         /* Allocate a free vuid. Yes this is a linear search... */
187         while( get_valid_user_struct_internal(sconn,
188                         sconn->smb1.sessions.next_vuid,
189                         SERVER_ALLOCATED_REQUIRED_ANY) != NULL ) {
190                 increment_next_vuid(&sconn->smb1.sessions.next_vuid);
191         }
192
193         DEBUG(10,("register_initial_vuid: allocated vuid = %u\n",
194                 (unsigned int)sconn->smb1.sessions.next_vuid ));
195
196         vuser->vuid = sconn->smb1.sessions.next_vuid;
197
198         /*
199          * This happens in an unfinished NTLMSSP session setup. We
200          * need to allocate a vuid between the first and second calls
201          * to NTLMSSP.
202          */
203         increment_next_vuid(&sconn->smb1.sessions.next_vuid);
204         sconn->smb1.sessions.num_validated_vuids++;
205
206         DLIST_ADD(sconn->smb1.sessions.validated_users, vuser);
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
291         if (!vuser->session_info->security_token) {
292                 DEBUG(1, ("register_existing_vuid: session_info does not "
293                         "contain a user_token - cannot continue\n"));
294                 goto fail;
295         }
296
297         /* Make clear that we require the optional unix_token in the source3 code */
298         SMB_ASSERT(vuser->session_info->unix_token);
299
300         DEBUG(3,("register_existing_vuid: UNIX uid %d is UNIX user %s, "
301                 "and will be vuid %u\n", (int)vuser->session_info->unix_token->uid,
302                  vuser->session_info->unix_info->unix_name, vuser->vuid));
303
304         if (!session_claim(sconn, vuser)) {
305                 DEBUG(1, ("register_existing_vuid: Failed to claim session "
306                         "for vuid=%d\n",
307                         vuser->vuid));
308                 goto fail;
309         }
310
311         /* Register a home dir service for this user if
312         (a) This is not a guest connection,
313         (b) we have a home directory defined
314         (c) there s not an existing static share by that name
315         If a share exists by this name (autoloaded or not) reuse it . */
316
317         vuser->homes_snum = -1;
318
319
320         if (!guest) {
321                 vuser->homes_snum = register_homes_share(
322                         vuser->session_info->unix_info->unix_name);
323         }
324
325         if (srv_is_signing_negotiated(sconn) &&
326             !guest) {
327                 /* Try and turn on server signing on the first non-guest
328                  * sessionsetup. */
329                 srv_set_signing(sconn,
330                                 vuser->session_info->session_key,
331                                 response_blob);
332         }
333
334         /* fill in the current_user_info struct */
335         set_current_user_info(
336                 vuser->session_info->unix_info->sanitized_username,
337                 vuser->session_info->unix_info->unix_name,
338                 vuser->session_info->info->domain_name);
339
340         return vuser->vuid;
341
342   fail:
343
344         if (vuser) {
345                 invalidate_vuid(sconn, vuid);
346         }
347         return UID_FIELD_INVALID;
348 }