(merge from 3.0)
[nivanova/samba-autobuild/.git] / source3 / auth / auth_domain.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Authenticate against a remote domain
4    Copyright (C) Andrew Tridgell 1992-1998
5    Copyright (C) Andrew Bartlett 2001
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 2 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, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23
24 #undef DBGC_CLASS
25 #define DBGC_CLASS DBGC_AUTH
26
27 extern BOOL global_machine_password_needs_changing;
28
29 /**
30  * Connect to a remote server for domain security authenticaion.
31  *
32  * @param cli the cli to return containing the active connection
33  * @param server either a machine name or text IP address to
34  *               connect to.
35  * @param trust_passwd the trust password to establish the
36  *                       credentials with.
37  *
38  **/
39
40 static NTSTATUS connect_to_domain_password_server(struct cli_state **cli, 
41                                                   const char *dc_name, struct in_addr dc_ip, 
42                                                   const char *setup_creds_as,
43                                                   uint16 sec_chan,
44                                                   const unsigned char *trust_passwd,
45                                                   BOOL *retry)
46 {
47         NTSTATUS result;
48
49         /* TODO: Send a SAMLOGON request to determine whether this is a valid
50            logonserver.  We can avoid a 30-second timeout if the DC is down
51            if the SAMLOGON request fails as it is only over UDP. */
52
53         /* we use a mutex to prevent two connections at once - when a 
54            Win2k PDC get two connections where one hasn't completed a 
55            session setup yet it will send a TCP reset to the first 
56            connection (tridge) */
57
58         /*
59          * With NT4.x DC's *all* authentication must be serialized to avoid
60          * ACCESS_DENIED errors if 2 auths are done from the same machine. JRA.
61          */
62
63         if (!grab_server_mutex(dc_name))
64                 return NT_STATUS_NO_LOGON_SERVERS;
65         
66         /* Attempt connection */
67         *retry = True;
68         result = cli_full_connection(cli, global_myname(), dc_name, &dc_ip, 0, 
69                 "IPC$", "IPC", "", "", "", 0, Undefined, retry);
70
71         if (!NT_STATUS_IS_OK(result)) {
72                 /* map to something more useful */
73                 if (NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL)) {
74                         result = NT_STATUS_NO_LOGON_SERVERS;
75                 }
76
77                 release_server_mutex();
78                 return result;
79         }
80
81         /*
82          * We now have an anonymous connection to IPC$ on the domain password server.
83          */
84
85         /*
86          * Even if the connect succeeds we need to setup the netlogon
87          * pipe here. We do this as we may just have changed the domain
88          * account password on the PDC and yet we may be talking to
89          * a BDC that doesn't have this replicated yet. In this case
90          * a successful connect to a DC needs to take the netlogon connect
91          * into account also. This patch from "Bjart Kvarme" <bjart.kvarme@usit.uio.no>.
92          */
93
94         if(cli_nt_session_open(*cli, PI_NETLOGON) == False) {
95                 DEBUG(0,("connect_to_domain_password_server: unable to open the domain client session to \
96 machine %s. Error was : %s.\n", dc_name, cli_errstr(*cli)));
97                 cli_nt_session_close(*cli);
98                 cli_ulogoff(*cli);
99                 cli_shutdown(*cli);
100                 release_server_mutex();
101                 return NT_STATUS_NO_LOGON_SERVERS;
102         }
103
104         fstr_sprintf((*cli)->mach_acct, "%s$", setup_creds_as);
105
106         if (!(*cli)->mach_acct) {
107                 release_server_mutex();
108                 return NT_STATUS_NO_MEMORY;
109         }
110
111         result = cli_nt_establish_netlogon(*cli, sec_chan, trust_passwd);
112
113         if (!NT_STATUS_IS_OK(result)) {
114                 DEBUG(0,("connect_to_domain_password_server: unable to setup the NETLOGON credentials to machine \
115 %s. Error was : %s.\n", dc_name, nt_errstr(result)));
116                 cli_nt_session_close(*cli);
117                 cli_ulogoff(*cli);
118                 cli_shutdown(*cli);
119                 release_server_mutex();
120                 return result;
121         }
122
123         /* We exit here with the mutex *locked*. JRA */
124
125         return NT_STATUS_OK;
126 }
127
128 /***********************************************************************
129  Do the same as security=server, but using NT Domain calls and a session
130  key from the machine password.  If the server parameter is specified
131  use it, otherwise figure out a server from the 'password server' param.
132 ************************************************************************/
133
134 static NTSTATUS domain_client_validate(TALLOC_CTX *mem_ctx,
135                                        const auth_usersupplied_info *user_info, 
136                                        const char *domain,
137                                        uchar chal[8],
138                                        auth_serversupplied_info **server_info, 
139                                        const char *dc_name, struct in_addr dc_ip,
140                                        const char *setup_creds_as,
141                                        uint16 sec_chan,
142                                        unsigned char trust_passwd[16],
143                                        time_t last_change_time)
144 {
145         NET_USER_INFO_3 info3;
146         struct cli_state *cli = NULL;
147         NTSTATUS nt_status = NT_STATUS_NO_LOGON_SERVERS;
148         int i;
149         BOOL retry = True;
150
151         /*
152          * At this point, smb_apasswd points to the lanman response to
153          * the challenge in local_challenge, and smb_ntpasswd points to
154          * the NT response to the challenge in local_challenge. Ship
155          * these over the secure channel to a domain controller and
156          * see if they were valid.
157          */
158
159         /* rety loop for robustness */
160         
161         for (i = 0; !NT_STATUS_IS_OK(nt_status) && retry && (i < 3); i++) {
162                 nt_status = connect_to_domain_password_server(&cli, dc_name, dc_ip, setup_creds_as,
163                         sec_chan, trust_passwd, &retry);
164         }
165
166         if ( !NT_STATUS_IS_OK(nt_status) ) {
167                 DEBUG(0,("domain_client_validate: Domain password server not available.\n"));
168                 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCESS_DENIED)) {
169                         return NT_STATUS_TRUSTED_RELATIONSHIP_FAILURE;
170                 }
171                 return nt_status;
172         }
173
174         ZERO_STRUCT(info3);
175
176         /*
177          * If this call succeeds, we now have lots of info about the user
178          * in the info3 structure.  
179          */
180
181         nt_status = cli_netlogon_sam_network_logon(cli, mem_ctx,
182                 NULL, user_info->smb_name.str, user_info->domain.str, 
183                 user_info->wksta_name.str, chal, user_info->lm_resp, 
184                 user_info->nt_resp, &info3);
185         
186         /* let go as soon as possible so we avoid any potential deadlocks
187            with winbind lookup up users or groups */
188            
189         release_server_mutex();
190
191         if (!NT_STATUS_IS_OK(nt_status)) {
192                 DEBUG(0,("domain_client_validate: unable to validate password "
193                          "for user %s in domain %s to Domain controller %s. "
194                          "Error was %s.\n", user_info->smb_name.str,
195                          user_info->domain.str, cli->srv_name_slash, 
196                          nt_errstr(nt_status)));
197
198                 /* map to something more useful */
199                 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_UNSUCCESSFUL)) {
200                         nt_status = NT_STATUS_NO_LOGON_SERVERS;
201                 }
202         } else {
203                 nt_status = make_server_info_info3(mem_ctx, user_info->internal_username.str, 
204                                                    user_info->smb_name.str, domain, server_info, &info3);
205                 netsamlogon_cache_store( mem_ctx, &info3 );
206         }
207
208 #if 0
209         /* 
210          * We don't actually need to do this - plus it fails currently with
211          * NT_STATUS_INVALID_INFO_CLASS - we need to know *exactly* what to
212          * send here. JRA.
213          */
214
215         if (NT_STATUS_IS_OK(status)) {
216                 if(cli_nt_logoff(&cli, &ctr) == False) {
217                         DEBUG(0,("domain_client_validate: unable to log off user %s in domain \
218 %s to Domain controller %s. Error was %s.\n", user, domain, dc_name, cli_errstr(&cli)));        
219                         nt_status = NT_STATUS_LOGON_FAILURE;
220                 }
221         }
222 #endif /* 0 */
223
224         /* Note - once the cli stream is shutdown the mem_ctx used
225            to allocate the other_sids and gids structures has been deleted - so
226            these pointers are no longer valid..... */
227
228         cli_nt_session_close(cli);
229         cli_ulogoff(cli);
230         cli_shutdown(cli);
231         return nt_status;
232 }
233
234 /****************************************************************************
235  Check for a valid username and password in security=domain mode.
236 ****************************************************************************/
237
238 static NTSTATUS check_ntdomain_security(const struct auth_context *auth_context,
239                                         void *my_private_data, 
240                                         TALLOC_CTX *mem_ctx,
241                                         const auth_usersupplied_info *user_info, 
242                                         auth_serversupplied_info **server_info)
243 {
244         NTSTATUS nt_status = NT_STATUS_LOGON_FAILURE;
245         unsigned char trust_passwd[16];
246         time_t last_change_time;
247         const char *domain = lp_workgroup();
248         uint32 sec_channel_type = 0;
249         fstring dc_name;
250         struct in_addr dc_ip;
251
252         if ( lp_server_role() != ROLE_DOMAIN_MEMBER ) {
253                 DEBUG(0,("check_ntdomain_security: Configuration error!  Cannot use "
254                         "ntdomain auth method when not a member of a domain.\n"));
255                 return NT_STATUS_NOT_IMPLEMENTED;
256         }
257
258         if (!user_info || !server_info || !auth_context) {
259                 DEBUG(1,("check_ntdomain_security: Critical variables not present.  Failing.\n"));
260                 return NT_STATUS_INVALID_PARAMETER;
261         }
262
263         /* 
264          * Check that the requested domain is not our own machine name.
265          * If it is, we should never check the PDC here, we use our own local
266          * password file.
267          */
268
269         if(strequal(get_global_sam_name(), user_info->domain.str)) {
270                 DEBUG(3,("check_ntdomain_security: Requested domain was for this machine.\n"));
271                 return NT_STATUS_NOT_IMPLEMENTED;
272         }
273
274         /*
275          * Get the machine account password for our primary domain
276          * No need to become_root() as secrets_init() is done at startup.
277          */
278
279         if (!secrets_fetch_trust_account_password(domain, trust_passwd, &last_change_time, &sec_channel_type))
280         {
281                 DEBUG(0, ("check_ntdomain_security: could not fetch trust account password for domain '%s'\n", domain));
282                 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
283         }
284
285         /* Test if machine password has expired and needs to be changed */
286         if (lp_machine_password_timeout()) {
287                 if (last_change_time > 0 && 
288                     time(NULL) > (last_change_time + 
289                                   lp_machine_password_timeout())) {
290                         global_machine_password_needs_changing = True;
291                 }
292         }
293
294         /* we need our DC to send the net_sam_logon() request to */
295
296         if ( !get_dc_name(domain, NULL, dc_name, &dc_ip) ) {
297                 DEBUG(5,("check_trustdomain_security: unable to locate a DC for domain %s\n",
298                         user_info->domain.str));
299                 return NT_STATUS_NO_LOGON_SERVERS;
300         }
301         
302         nt_status = domain_client_validate(mem_ctx, user_info, domain,
303                 (uchar *)auth_context->challenge.data, server_info, dc_name, dc_ip,
304                 global_myname(), sec_channel_type,trust_passwd, last_change_time);
305                 
306         return nt_status;
307 }
308
309 /* module initialisation */
310 static NTSTATUS auth_init_ntdomain(struct auth_context *auth_context, const char* param, auth_methods **auth_method) 
311 {
312         if (!make_auth_methods(auth_context, auth_method)) {
313                 return NT_STATUS_NO_MEMORY;
314         }
315
316         (*auth_method)->name = "ntdomain";
317         (*auth_method)->auth = check_ntdomain_security;
318         return NT_STATUS_OK;
319 }
320
321
322 /****************************************************************************
323  Check for a valid username and password in a trusted domain
324 ****************************************************************************/
325
326 static NTSTATUS check_trustdomain_security(const struct auth_context *auth_context,
327                                            void *my_private_data, 
328                                            TALLOC_CTX *mem_ctx,
329                                            const auth_usersupplied_info *user_info, 
330                                            auth_serversupplied_info **server_info)
331 {
332         NTSTATUS nt_status = NT_STATUS_LOGON_FAILURE;
333         unsigned char trust_md4_password[16];
334         char *trust_password;
335         time_t last_change_time;
336         DOM_SID sid;
337         fstring dc_name;
338         struct in_addr dc_ip;
339
340         if (!user_info || !server_info || !auth_context) {
341                 DEBUG(1,("check_trustdomain_security: Critical variables not present.  Failing.\n"));
342                 return NT_STATUS_INVALID_PARAMETER;
343         }
344
345         /* 
346          * Check that the requested domain is not our own machine name or domain name.
347          */
348
349         if( strequal(get_global_sam_name(), user_info->domain.str)) {
350                 DEBUG(3,("check_trustdomain_security: Requested domain [%s] was for this machine.\n",
351                         user_info->domain.str));
352                 return NT_STATUS_NOT_IMPLEMENTED;
353         }
354
355         /* No point is bothering if this is not a trusted domain.
356            This return makes "map to guest = bad user" work again.
357            The logic is that if we know nothing about the domain, that
358            user is known to us and does not exist */
359         
360         if ( !is_trusted_domain( user_info->domain.str ) )
361                 return NT_STATUS_NOT_IMPLEMENTED;
362
363         /*
364          * Get the trusted account password for the trusted domain
365          * No need to become_root() as secrets_init() is done at startup.
366          */
367
368         if (!secrets_fetch_trusted_domain_password(user_info->domain.str, &trust_password, &sid, &last_change_time))
369         {
370                 DEBUG(0, ("check_trustdomain_security: could not fetch trust account password for domain %s\n", user_info->domain.str));
371                 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
372         }
373
374 #ifdef DEBUG_PASSWORD
375         DEBUG(100, ("Trust password for domain %s is %s\n", user_info->domain.str, trust_password));
376 #endif
377         E_md4hash(trust_password, trust_md4_password);
378         SAFE_FREE(trust_password);
379
380 #if 0
381         /* Test if machine password is expired and need to be changed */
382         if (time(NULL) > last_change_time + lp_machine_password_timeout())
383         {
384                 global_machine_password_needs_changing = True;
385         }
386 #endif
387
388         /* use get_dc_name() for consistency even through we know that it will be 
389            a netbios name */
390            
391         if ( !get_dc_name(user_info->domain.str, NULL, dc_name, &dc_ip) ) {
392                 DEBUG(5,("check_trustdomain_security: unable to locate a DC for domain %s\n",
393                         user_info->domain.str));
394                 return NT_STATUS_NO_LOGON_SERVERS;
395         }
396         
397         nt_status = domain_client_validate(mem_ctx, user_info, user_info->domain.str,
398                 (uchar *)auth_context->challenge.data, server_info, dc_name, dc_ip,
399                 lp_workgroup(), SEC_CHAN_DOMAIN, trust_md4_password, last_change_time);
400
401         return nt_status;
402 }
403
404 /* module initialisation */
405 static NTSTATUS auth_init_trustdomain(struct auth_context *auth_context, const char* param, auth_methods **auth_method) 
406 {
407         if (!make_auth_methods(auth_context, auth_method)) {
408                 return NT_STATUS_NO_MEMORY;
409         }
410
411         (*auth_method)->name = "trustdomain";
412         (*auth_method)->auth = check_trustdomain_security;
413         return NT_STATUS_OK;
414 }
415
416 NTSTATUS auth_domain_init(void) 
417 {
418         smb_register_auth(AUTH_INTERFACE_VERSION, "trustdomain", auth_init_trustdomain);
419         smb_register_auth(AUTH_INTERFACE_VERSION, "ntdomain", auth_init_ntdomain);
420         return NT_STATUS_OK;
421 }