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