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