s3-passdb: use passdb headers where needed.
[ira/wip.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 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 "../libcli/auth/libcli_auth.h"
23 #include "../librpc/gen_ndr/ndr_netlogon.h"
24 #include "rpc_client/cli_pipe.h"
25 #include "rpc_client/cli_netlogon.h"
26 #include "secrets.h"
27 #include "passdb.h"
28
29 #undef DBGC_CLASS
30 #define DBGC_CLASS DBGC_AUTH
31
32 extern bool global_machine_password_needs_changing;
33 static struct named_mutex *mutex;
34
35 /*
36  * Change machine password (called from main loop
37  * idle timeout. Must be done as root.
38  */
39
40 void attempt_machine_password_change(void)
41 {
42         unsigned char trust_passwd_hash[16];
43         time_t lct;
44         void *lock;
45
46         if (!global_machine_password_needs_changing) {
47                 return;
48         }
49
50         if (lp_security() != SEC_DOMAIN) {
51                 return;
52         }
53
54         /*
55          * We're in domain level security, and the code that
56          * read the machine password flagged that the machine
57          * password needs changing.
58          */
59
60         /*
61          * First, open the machine password file with an exclusive lock.
62          */
63
64         lock = secrets_get_trust_account_lock(NULL, lp_workgroup());
65
66         if (lock == NULL) {
67                 DEBUG(0,("attempt_machine_password_change: unable to lock "
68                         "the machine account password for machine %s in "
69                         "domain %s.\n",
70                         global_myname(), lp_workgroup() ));
71                 return;
72         }
73
74         if(!secrets_fetch_trust_account_password(lp_workgroup(),
75                         trust_passwd_hash, &lct, NULL)) {
76                 DEBUG(0,("attempt_machine_password_change: unable to read the "
77                         "machine account password for %s in domain %s.\n",
78                         global_myname(), lp_workgroup()));
79                 TALLOC_FREE(lock);
80                 return;
81         }
82
83         /*
84          * Make sure someone else hasn't already done this.
85          */
86
87         if(time(NULL) < lct + lp_machine_password_timeout()) {
88                 global_machine_password_needs_changing = false;
89                 TALLOC_FREE(lock);
90                 return;
91         }
92
93         /* always just contact the PDC here */
94
95         change_trust_account_password( lp_workgroup(), NULL);
96         global_machine_password_needs_changing = false;
97         TALLOC_FREE(lock);
98 }
99
100 /**
101  * Connect to a remote server for (inter)domain security authenticaion.
102  *
103  * @param cli the cli to return containing the active connection
104  * @param server either a machine name or text IP address to
105  *               connect to.
106  * @param setup_creds_as domain account to setup credentials as
107  * @param sec_chan a switch value to distinguish between domain
108  *                 member and interdomain authentication
109  * @param trust_passwd the trust password to establish the
110  *                     credentials with.
111  *
112  **/
113
114 static NTSTATUS connect_to_domain_password_server(struct cli_state **cli,
115                                                 const char *domain,
116                                                 const char *dc_name,
117                                                 struct sockaddr_storage *dc_ss, 
118                                                 struct rpc_pipe_client **pipe_ret)
119 {
120         NTSTATUS result;
121         struct rpc_pipe_client *netlogon_pipe = NULL;
122
123         *cli = NULL;
124
125         *pipe_ret = NULL;
126
127         /* TODO: Send a SAMLOGON request to determine whether this is a valid
128            logonserver.  We can avoid a 30-second timeout if the DC is down
129            if the SAMLOGON request fails as it is only over UDP. */
130
131         /* we use a mutex to prevent two connections at once - when a 
132            Win2k PDC get two connections where one hasn't completed a 
133            session setup yet it will send a TCP reset to the first 
134            connection (tridge) */
135
136         /*
137          * With NT4.x DC's *all* authentication must be serialized to avoid
138          * ACCESS_DENIED errors if 2 auths are done from the same machine. JRA.
139          */
140
141         mutex = grab_named_mutex(NULL, dc_name, 10);
142         if (mutex == NULL) {
143                 return NT_STATUS_NO_LOGON_SERVERS;
144         }
145
146         /* Attempt connection */
147         result = cli_full_connection(cli, global_myname(), dc_name, dc_ss, 0, 
148                 "IPC$", "IPC", "", "", "", 0, Undefined);
149
150         if (!NT_STATUS_IS_OK(result)) {
151                 /* map to something more useful */
152                 if (NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL)) {
153                         result = NT_STATUS_NO_LOGON_SERVERS;
154                 }
155
156                 if (*cli) {
157                         cli_shutdown(*cli);
158                         *cli = NULL;
159                 }
160
161                 TALLOC_FREE(mutex);
162                 return result;
163         }
164
165         /*
166          * We now have an anonymous connection to IPC$ on the domain password server.
167          */
168
169         /*
170          * Even if the connect succeeds we need to setup the netlogon
171          * pipe here. We do this as we may just have changed the domain
172          * account password on the PDC and yet we may be talking to
173          * a BDC that doesn't have this replicated yet. In this case
174          * a successful connect to a DC needs to take the netlogon connect
175          * into account also. This patch from "Bjart Kvarme" <bjart.kvarme@usit.uio.no>.
176          */
177
178         /* open the netlogon pipe. */
179         if (lp_client_schannel()) {
180                 /* We also setup the creds chain in the open_schannel call. */
181                 result = cli_rpc_pipe_open_schannel(
182                         *cli, &ndr_table_netlogon.syntax_id, NCACN_NP,
183                         DCERPC_AUTH_LEVEL_PRIVACY, domain, &netlogon_pipe);
184         } else {
185                 result = cli_rpc_pipe_open_noauth(
186                         *cli, &ndr_table_netlogon.syntax_id, &netlogon_pipe);
187         }
188
189         if (!NT_STATUS_IS_OK(result)) {
190                 DEBUG(0,("connect_to_domain_password_server: unable to open the domain client session to \
191 machine %s. Error was : %s.\n", dc_name, nt_errstr(result)));
192                 cli_shutdown(*cli);
193                 *cli = NULL;
194                 TALLOC_FREE(mutex);
195                 return result;
196         }
197
198         if (!lp_client_schannel()) {
199                 /* We need to set up a creds chain on an unauthenticated netlogon pipe. */
200                 uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
201                 enum netr_SchannelType sec_chan_type = 0;
202                 unsigned char machine_pwd[16];
203                 const char *account_name;
204
205                 if (!get_trust_pw_hash(domain, machine_pwd, &account_name,
206                                        &sec_chan_type))
207                 {
208                         DEBUG(0, ("connect_to_domain_password_server: could not fetch "
209                         "trust account password for domain '%s'\n",
210                                 domain));
211                         cli_shutdown(*cli);
212                         *cli = NULL;
213                         TALLOC_FREE(mutex);
214                         return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
215                 }
216
217                 result = rpccli_netlogon_setup_creds(netlogon_pipe,
218                                         dc_name, /* server name */
219                                         domain, /* domain */
220                                         global_myname(), /* client name */
221                                         account_name, /* machine account name */
222                                         machine_pwd,
223                                         sec_chan_type,
224                                         &neg_flags);
225
226                 if (!NT_STATUS_IS_OK(result)) {
227                         cli_shutdown(*cli);
228                         *cli = NULL;
229                         TALLOC_FREE(mutex);
230                         return result;
231                 }
232         }
233
234         if(!netlogon_pipe) {
235                 DEBUG(0,("connect_to_domain_password_server: unable to open the domain client session to \
236 machine %s. Error was : %s.\n", dc_name, cli_errstr(*cli)));
237                 cli_shutdown(*cli);
238                 *cli = NULL;
239                 TALLOC_FREE(mutex);
240                 return NT_STATUS_NO_LOGON_SERVERS;
241         }
242
243         /* We exit here with the mutex *locked*. JRA */
244
245         *pipe_ret = netlogon_pipe;
246
247         return NT_STATUS_OK;
248 }
249
250 /***********************************************************************
251  Do the same as security=server, but using NT Domain calls and a session
252  key from the machine password.  If the server parameter is specified
253  use it, otherwise figure out a server from the 'password server' param.
254 ************************************************************************/
255
256 static NTSTATUS domain_client_validate(TALLOC_CTX *mem_ctx,
257                                         const struct auth_usersupplied_info *user_info,
258                                         const char *domain,
259                                         uchar chal[8],
260                                         struct auth_serversupplied_info **server_info,
261                                         const char *dc_name,
262                                         struct sockaddr_storage *dc_ss)
263
264 {
265         struct netr_SamInfo3 *info3 = NULL;
266         struct cli_state *cli = NULL;
267         struct rpc_pipe_client *netlogon_pipe = NULL;
268         NTSTATUS nt_status = NT_STATUS_NO_LOGON_SERVERS;
269         int i;
270
271         /*
272          * At this point, smb_apasswd points to the lanman response to
273          * the challenge in local_challenge, and smb_ntpasswd points to
274          * the NT response to the challenge in local_challenge. Ship
275          * these over the secure channel to a domain controller and
276          * see if they were valid.
277          */
278
279         /* rety loop for robustness */
280
281         for (i = 0; !NT_STATUS_IS_OK(nt_status) && (i < 3); i++) {
282                 nt_status = connect_to_domain_password_server(&cli,
283                                                         domain,
284                                                         dc_name,
285                                                         dc_ss,
286                                                         &netlogon_pipe);
287         }
288
289         if ( !NT_STATUS_IS_OK(nt_status) ) {
290                 DEBUG(0,("domain_client_validate: Domain password server not available.\n"));
291                 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCESS_DENIED)) {
292                         return NT_STATUS_TRUSTED_RELATIONSHIP_FAILURE;
293                 }
294                 return nt_status;
295         }
296
297         /* store a successful connection */
298
299         saf_store( domain, cli->desthost );
300
301         /*
302          * If this call succeeds, we now have lots of info about the user
303          * in the info3 structure.  
304          */
305
306         nt_status = rpccli_netlogon_sam_network_logon(netlogon_pipe,
307                                                       mem_ctx,
308                                                       user_info->logon_parameters,         /* flags such as 'allow workstation logon' */
309                                                       dc_name,                             /* server name */
310                                                       user_info->client.account_name,      /* user name logging on. */
311                                                       user_info->client.domain_name,       /* domain name */
312                                                       user_info->workstation_name,         /* workstation name */
313                                                       chal,                                /* 8 byte challenge. */
314                                                       3,                                   /* validation level */
315                                                       user_info->password.response.lanman, /* lanman 24 byte response */
316                                                       user_info->password.response.nt,     /* nt 24 byte response */
317                                                       &info3);                             /* info3 out */
318
319         /* Let go as soon as possible so we avoid any potential deadlocks
320            with winbind lookup up users or groups. */
321
322         TALLOC_FREE(mutex);
323
324         if (!NT_STATUS_IS_OK(nt_status)) {
325                 DEBUG(0,("domain_client_validate: unable to validate password "
326                          "for user %s in domain %s to Domain controller %s. "
327                          "Error was %s.\n", user_info->client.account_name,
328                          user_info->client.domain_name, dc_name,
329                          nt_errstr(nt_status)));
330
331                 /* map to something more useful */
332                 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_UNSUCCESSFUL)) {
333                         nt_status = NT_STATUS_NO_LOGON_SERVERS;
334                 }
335         } else {
336                 nt_status = make_server_info_info3(mem_ctx,
337                                                    user_info->client.account_name,
338                                                    domain,
339                                                    server_info,
340                                                    info3);
341
342                 if (NT_STATUS_IS_OK(nt_status)) {
343                         (*server_info)->nss_token |= user_info->was_mapped;
344                         netsamlogon_cache_store(user_info->client.account_name, info3);
345                         TALLOC_FREE(info3);
346                 }
347         }
348
349         /* Note - once the cli stream is shutdown the mem_ctx used
350            to allocate the other_sids and gids structures has been deleted - so
351            these pointers are no longer valid..... */
352
353         cli_shutdown(cli);
354         return nt_status;
355 }
356
357 /****************************************************************************
358  Check for a valid username and password in security=domain mode.
359 ****************************************************************************/
360
361 static NTSTATUS check_ntdomain_security(const struct auth_context *auth_context,
362                                         void *my_private_data, 
363                                         TALLOC_CTX *mem_ctx,
364                                         const struct auth_usersupplied_info *user_info,
365                                         struct auth_serversupplied_info **server_info)
366 {
367         NTSTATUS nt_status = NT_STATUS_LOGON_FAILURE;
368         const char *domain = lp_workgroup();
369         fstring dc_name;
370         struct sockaddr_storage dc_ss;
371
372         if ( lp_server_role() != ROLE_DOMAIN_MEMBER ) {
373                 DEBUG(0,("check_ntdomain_security: Configuration error!  Cannot use "
374                         "ntdomain auth method when not a member of a domain.\n"));
375                 return NT_STATUS_NOT_IMPLEMENTED;
376         }
377
378         if (!user_info || !server_info || !auth_context) {
379                 DEBUG(1,("check_ntdomain_security: Critical variables not present.  Failing.\n"));
380                 return NT_STATUS_INVALID_PARAMETER;
381         }
382
383         DEBUG(10, ("Check auth for: [%s]\n", user_info->mapped.account_name));
384
385         /* 
386          * Check that the requested domain is not our own machine name.
387          * If it is, we should never check the PDC here, we use our own local
388          * password file.
389          */
390
391         if(strequal(get_global_sam_name(), user_info->mapped.domain_name)) {
392                 DEBUG(3,("check_ntdomain_security: Requested domain was for this machine.\n"));
393                 return NT_STATUS_NOT_IMPLEMENTED;
394         }
395
396         /* we need our DC to send the net_sam_logon() request to */
397
398         if ( !get_dc_name(domain, NULL, dc_name, &dc_ss) ) {
399                 DEBUG(5,("check_ntdomain_security: unable to locate a DC for domain %s\n",
400                         user_info->mapped.domain_name));
401                 return NT_STATUS_NO_LOGON_SERVERS;
402         }
403
404         nt_status = domain_client_validate(mem_ctx,
405                                         user_info,
406                                         domain,
407                                         (uchar *)auth_context->challenge.data,
408                                         server_info,
409                                         dc_name,
410                                         &dc_ss);
411
412         return nt_status;
413 }
414
415 /* module initialisation */
416 static NTSTATUS auth_init_ntdomain(struct auth_context *auth_context, const char* param, auth_methods **auth_method) 
417 {
418         struct auth_methods *result;
419
420         result = TALLOC_ZERO_P(auth_context, struct auth_methods);
421         if (result == NULL) {
422                 return NT_STATUS_NO_MEMORY;
423         }
424         result->name = "ntdomain";
425         result->auth = check_ntdomain_security;
426
427         *auth_method = result;
428         return NT_STATUS_OK;
429 }
430
431
432 /****************************************************************************
433  Check for a valid username and password in a trusted domain
434 ****************************************************************************/
435
436 static NTSTATUS check_trustdomain_security(const struct auth_context *auth_context,
437                                            void *my_private_data, 
438                                            TALLOC_CTX *mem_ctx,
439                                            const struct auth_usersupplied_info *user_info,
440                                            struct auth_serversupplied_info **server_info)
441 {
442         NTSTATUS nt_status = NT_STATUS_LOGON_FAILURE;
443         unsigned char trust_md4_password[16];
444         char *trust_password;
445         fstring dc_name;
446         struct sockaddr_storage dc_ss;
447
448         if (!user_info || !server_info || !auth_context) {
449                 DEBUG(1,("check_trustdomain_security: Critical variables not present.  Failing.\n"));
450                 return NT_STATUS_INVALID_PARAMETER;
451         }
452
453         DEBUG(10, ("Check auth for: [%s]\n", user_info->mapped.account_name));
454
455         /* 
456          * Check that the requested domain is not our own machine name or domain name.
457          */
458
459         if( strequal(get_global_sam_name(), user_info->mapped.domain_name)) {
460                 DEBUG(3,("check_trustdomain_security: Requested domain [%s] was for this machine.\n",
461                         user_info->mapped.domain_name));
462                 return NT_STATUS_NOT_IMPLEMENTED;
463         }
464
465         /* No point is bothering if this is not a trusted domain.
466            This return makes "map to guest = bad user" work again.
467            The logic is that if we know nothing about the domain, that
468            user is not known to us and does not exist */
469
470         if ( !is_trusted_domain( user_info->mapped.domain_name ) )
471                 return NT_STATUS_NOT_IMPLEMENTED;
472
473         /*
474          * Get the trusted account password for the trusted domain
475          * No need to become_root() as secrets_init() is done at startup.
476          */
477
478         if (!pdb_get_trusteddom_pw(user_info->mapped.domain_name, &trust_password,
479                                    NULL, NULL)) {
480                 DEBUG(0, ("check_trustdomain_security: could not fetch trust "
481                           "account password for domain %s\n",
482                           user_info->mapped.domain_name));
483                 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
484         }
485
486 #ifdef DEBUG_PASSWORD
487         DEBUG(100, ("Trust password for domain %s is %s\n", user_info->mapped.domain_name,
488                     trust_password));
489 #endif
490         E_md4hash(trust_password, trust_md4_password);
491         SAFE_FREE(trust_password);
492
493 #if 0
494         /* Test if machine password is expired and need to be changed */
495         if (time(NULL) > last_change_time + (time_t)lp_machine_password_timeout())
496         {
497                 global_machine_password_needs_changing = True;
498         }
499 #endif
500
501         /* use get_dc_name() for consistency even through we know that it will be 
502            a netbios name */
503
504         if ( !get_dc_name(user_info->mapped.domain_name, NULL, dc_name, &dc_ss) ) {
505                 DEBUG(5,("check_trustdomain_security: unable to locate a DC for domain %s\n",
506                         user_info->mapped.domain_name));
507                 return NT_STATUS_NO_LOGON_SERVERS;
508         }
509
510         nt_status = domain_client_validate(mem_ctx,
511                                            user_info,
512                                            user_info->mapped.domain_name,
513                                            (uchar *)auth_context->challenge.data,
514                                            server_info,
515                                            dc_name,
516                                            &dc_ss);
517
518         return nt_status;
519 }
520
521 /* module initialisation */
522 static NTSTATUS auth_init_trustdomain(struct auth_context *auth_context, const char* param, auth_methods **auth_method) 
523 {
524         struct auth_methods *result;
525
526         result = TALLOC_ZERO_P(auth_context, struct auth_methods);
527         if (result == NULL) {
528                 return NT_STATUS_NO_MEMORY;
529         }
530         result->name = "trustdomain";
531         result->auth = check_trustdomain_security;
532
533         *auth_method = result;
534         return NT_STATUS_OK;
535 }
536
537 NTSTATUS auth_domain_init(void) 
538 {
539         smb_register_auth(AUTH_INTERFACE_VERSION, "trustdomain", auth_init_trustdomain);
540         smb_register_auth(AUTH_INTERFACE_VERSION, "ntdomain", auth_init_ntdomain);
541         return NT_STATUS_OK;
542 }