Renamed get_nt_error_msg() to nt_errstr().
[jra/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 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 BOOL global_machine_password_needs_changing = False;
25
26 extern pstring global_myname;
27 extern userdom_struct current_user_info;
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_password 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 *server, 
42                                                   const char *setup_creds_as,
43                                                   uint16 sec_chan,
44                                                   const unsigned char *trust_passwd)
45 {
46         struct in_addr dest_ip;
47         fstring remote_machine;
48         NTSTATUS result;
49
50         if (is_ipaddress(server)) {
51                 struct in_addr to_ip;
52           
53                 /* we shouldn't have 255.255.255.255 forthe IP address of 
54                    a password server anyways */
55                 if ((to_ip.s_addr=inet_addr(server)) == 0xFFFFFFFF) {
56                         DEBUG (0,("connect_to_domain_password_server: inet_addr(%s) returned 0xFFFFFFFF!\n", server));
57                         return NT_STATUS_UNSUCCESSFUL;
58                 }
59
60                 if (!name_status_find("*", 0x20, 0x20, to_ip, remote_machine)) {
61                         DEBUG(0, ("connect_to_domain_password_server: Can't "
62                                   "resolve name for IP %s\n", server));
63                         return NT_STATUS_UNSUCCESSFUL;
64                 }
65         } else {
66                 fstrcpy(remote_machine, server);
67         }
68
69         standard_sub_basic(current_user_info.smb_name, remote_machine);
70         strupper(remote_machine);
71
72         if(!resolve_name( remote_machine, &dest_ip, 0x20)) {
73                 DEBUG(1,("connect_to_domain_password_server: Can't resolve address for %s\n", remote_machine));
74                 return NT_STATUS_UNSUCCESSFUL;
75         }
76   
77         if (ismyip(dest_ip)) {
78                 DEBUG(1,("connect_to_domain_password_server: Password server loop - not using password server %s\n",
79                          remote_machine));
80                 return NT_STATUS_UNSUCCESSFUL;
81         }
82   
83         /* TODO: Send a SAMLOGON request to determine whether this is a valid
84            logonserver.  We can avoid a 30-second timeout if the DC is down
85            if the SAMLOGON request fails as it is only over UDP. */
86
87         /* we use a mutex to prevent two connections at once - when a NT PDC gets
88            two connections where one hasn't completed a negprot yet it will send a 
89            TCP reset to the first connection (tridge) */
90         if (!message_named_mutex(server)) {
91                 DEBUG(1,("domain mutex failed for %s\n", server));
92                 return NT_STATUS_UNSUCCESSFUL;
93         }
94         
95         /* Attempt connection */
96         result = cli_full_connection(cli, global_myname, server,
97                                      &dest_ip, 0, "IPC$", "IPC", "", "", "", 0);
98
99         message_named_mutex_release(server);
100         
101         if (!NT_STATUS_IS_OK(result)) {
102                 return result;
103         }
104
105         /*
106          * We now have an anonymous connection to IPC$ on the domain password server.
107          */
108
109         /*
110          * Even if the connect succeeds we need to setup the netlogon
111          * pipe here. We do this as we may just have changed the domain
112          * account password on the PDC and yet we may be talking to
113          * a BDC that doesn't have this replicated yet. In this case
114          * a successful connect to a DC needs to take the netlogon connect
115          * into account also. This patch from "Bjart Kvarme" <bjart.kvarme@usit.uio.no>.
116          */
117
118         if(cli_nt_session_open(*cli, PIPE_NETLOGON) == False) {
119                 DEBUG(0,("connect_to_domain_password_server: unable to open the domain client session to \
120 machine %s. Error was : %s.\n", remote_machine, cli_errstr(*cli)));
121                 cli_nt_session_close(*cli);
122                 cli_ulogoff(*cli);
123                 cli_shutdown(*cli);
124                 return NT_STATUS_UNSUCCESSFUL;
125         }
126
127         snprintf((*cli)->mach_acct, sizeof((*cli)->mach_acct) - 1, "%s$", setup_creds_as);
128
129         if (!(*cli)->mach_acct) {
130                 return NT_STATUS_NO_MEMORY;
131         }
132
133         result = new_cli_nt_setup_creds(*cli, sec_chan, trust_passwd);
134
135         if (!NT_STATUS_IS_OK(result)) {
136                 DEBUG(0,("connect_to_domain_password_server: unable to setup the PDC credentials to machine \
137 %s. Error was : %s.\n", remote_machine, nt_errstr(result)));
138                 cli_nt_session_close(*cli);
139                 cli_ulogoff(*cli);
140                 cli_shutdown(*cli);
141                 return result;
142         }
143
144         return NT_STATUS_OK;
145 }
146
147 /***********************************************************************
148  Utility function to attempt a connection to an IP address of a DC.
149 ************************************************************************/
150
151 static NTSTATUS attempt_connect_to_dc(struct cli_state **cli, 
152                                       const char *domain, 
153                                       struct in_addr *ip, 
154                                       const char *setup_creds_as, 
155                                       uint16 sec_chan,
156                                       const unsigned char *trust_passwd)
157 {
158         fstring dc_name;
159
160         /*
161          * Ignore addresses we have already tried.
162          */
163
164         if (is_zero_ip(*ip))
165                 return NT_STATUS_UNSUCCESSFUL;
166
167         if (!lookup_dc_name(global_myname, domain, ip, dc_name))
168                 return NT_STATUS_UNSUCCESSFUL;
169
170         return connect_to_domain_password_server(cli, dc_name, setup_creds_as, sec_chan, trust_passwd);
171 }
172
173 /***********************************************************************
174  We have been asked to dynamcially determine the IP addresses of
175  the PDC and BDC's for DOMAIN, and query them in turn.
176 ************************************************************************/
177 static NTSTATUS find_connect_pdc(struct cli_state **cli, 
178                                  const char *domain,
179                                  const char *setup_creds_as,
180                                  uint16 sec_chan,
181                                  unsigned char *trust_passwd, 
182                                  time_t last_change_time)
183 {
184         struct in_addr *ip_list = NULL;
185         int count = 0;
186         int i;
187         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
188         time_t time_now = time(NULL);
189         BOOL use_pdc_only = False;
190
191         /*
192          * If the time the machine password has changed
193          * was less than an hour ago then we need to contact
194          * the PDC only, as we cannot be sure domain replication
195          * has yet taken place. Bug found by Gerald (way to go
196          * Gerald !). JRA.
197          */
198
199         if (time_now - last_change_time < 3600)
200                 use_pdc_only = True;
201
202         if (!get_dc_list(use_pdc_only, domain, &ip_list, &count))
203                 return NT_STATUS_UNSUCCESSFUL;
204
205         /*
206          * Firstly try and contact a PDC/BDC who has the same
207          * network address as any of our interfaces.
208          */
209         for(i = 0; i < count; i++) {
210                 if(!is_local_net(ip_list[i]))
211                         continue;
212
213                 if(NT_STATUS_IS_OK(nt_status = 
214                                    attempt_connect_to_dc(cli, domain, 
215                                                          &ip_list[i], setup_creds_as, 
216                                                          sec_chan, trust_passwd))) 
217                         break;
218                 
219                 zero_ip(&ip_list[i]); /* Tried and failed. */
220         }
221
222         /*
223          * Secondly try and contact a random PDC/BDC.
224          */
225         if(!NT_STATUS_IS_OK(nt_status)) {
226                 i = (sys_random() % count);
227
228                 if (!is_zero_ip(ip_list[i])) {
229                         if (!NT_STATUS_IS_OK(nt_status = 
230                                              attempt_connect_to_dc(cli, domain, 
231                                                                    &ip_list[i], setup_creds_as, 
232                                                                    sec_chan, trust_passwd)))
233                                 zero_ip(&ip_list[i]); /* Tried and failed. */
234                 }
235         }
236
237         /*
238          * Finally go through the IP list in turn, ignoring any addresses
239          * we have already tried.
240          */
241         if(!NT_STATUS_IS_OK(nt_status)) {
242                 /*
243                  * Try and connect to any of the other IP addresses in the PDC/BDC list.
244                  * Note that from a WINS server the #1 IP address is the PDC.
245                  */
246                 for(i = 0; i < count; i++) {
247                         if (is_zero_ip(ip_list[i]))
248                                 continue;
249
250                         if (NT_STATUS_IS_OK(nt_status = attempt_connect_to_dc(cli, domain, 
251                                                   &ip_list[i], setup_creds_as, sec_chan, trust_passwd)))
252                                 break;
253                 }
254         }
255
256         SAFE_FREE(ip_list);
257         return nt_status;
258 }
259
260 /***********************************************************************
261  Do the same as security=server, but using NT Domain calls and a session
262  key from the machine password.  If the server parameter is specified
263  use it, otherwise figure out a server from the 'password server' param.
264 ************************************************************************/
265
266 static NTSTATUS domain_client_validate(TALLOC_CTX *mem_ctx,
267                                        const auth_usersupplied_info *user_info, 
268                                        const char *domain,
269                                        uchar chal[8],
270                                        auth_serversupplied_info **server_info, 
271                                        char *server, char *setup_creds_as,
272                                        uint16 sec_chan,
273                                        unsigned char *trust_passwd,
274                                        time_t last_change_time)
275 {
276         fstring remote_machine;
277         NET_USER_INFO_3 info3;
278         struct cli_state *cli = NULL;
279         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
280         struct passwd *pass;
281
282         /*
283          * At this point, smb_apasswd points to the lanman response to
284          * the challenge in local_challenge, and smb_ntpasswd points to
285          * the NT response to the challenge in local_challenge. Ship
286          * these over the secure channel to a domain controller and
287          * see if they were valid.
288          */
289
290         while (!NT_STATUS_IS_OK(nt_status) &&
291                next_token(&server,remote_machine,LIST_SEP,sizeof(remote_machine))) {
292                 if(strequal(remote_machine, "*")) {
293                         nt_status = find_connect_pdc(&cli, domain, setup_creds_as, sec_chan, trust_passwd, last_change_time);
294                 } else {
295                         nt_status = connect_to_domain_password_server(&cli, remote_machine, setup_creds_as, sec_chan, trust_passwd);
296                 }
297         }
298
299         if (!NT_STATUS_IS_OK(nt_status)) {
300                 DEBUG(0,("domain_client_validate: Domain password server not available.\n"));
301                 return nt_status;
302         }
303
304         ZERO_STRUCT(info3);
305
306         /*
307          * If this call succeeds, we now have lots of info about the user
308          * in the info3 structure.  
309          */
310
311         nt_status = cli_netlogon_sam_network_logon(cli, mem_ctx,
312                                                    user_info->smb_name.str, user_info->domain.str, 
313                                                    user_info->wksta_name.str, chal, 
314                                                    user_info->lm_resp, user_info->nt_resp, 
315                                                    &info3);
316         
317         if (!NT_STATUS_IS_OK(nt_status)) {
318                 DEBUG(0,("domain_client_validate: unable to validate password "
319                          "for user %s in domain %s to Domain controller %s. "
320                          "Error was %s.\n", user_info->smb_name.str,
321                          user_info->domain.str, cli->srv_name_slash, 
322                          nt_errstr(nt_status)));
323         } else {
324                 char *dom_user;
325
326                 /* Check DOMAIN\username first to catch winbind users, then
327                    just the username for local users. */
328
329                 dom_user = talloc_asprintf(mem_ctx, "%s%s%s", user_info->domain.str,
330                                            lp_winbind_separator(),
331                                            user_info->internal_username.str);
332                 
333                 if (!dom_user) {
334                         DEBUG(0, ("talloc_asprintf failed!\n"));
335                         nt_status = NT_STATUS_NO_MEMORY;
336                 } else { 
337
338                         if (!(pass = Get_Pwnam(dom_user)))
339                                 pass = Get_Pwnam(user_info->internal_username.str);
340                         
341                         if (pass) {
342                                 make_server_info_pw(server_info, pass);
343                                 if (!server_info) {
344                                         nt_status = NT_STATUS_NO_MEMORY;
345                                 }
346                         } else {
347                                 nt_status = NT_STATUS_NO_SUCH_USER;
348                         }
349                 }
350         }
351
352         /* Store the user group information in the server_info returned to the caller. */
353         
354         if (NT_STATUS_IS_OK(nt_status) && (info3.num_groups2 != 0)) {
355                 int i;
356                 NT_USER_TOKEN *ptok;
357                 auth_serversupplied_info *pserver_info = *server_info;
358
359                 if ((pserver_info->ptok = malloc( sizeof(NT_USER_TOKEN) ) ) == NULL) {
360                         DEBUG(0, ("domain_client_validate: out of memory allocating rid group membership\n"));
361                         nt_status = NT_STATUS_NO_MEMORY;
362                         free_server_info(server_info);
363                         goto done;
364                 }
365
366                 ptok = pserver_info->ptok;
367                 ptok->num_sids = (size_t)info3.num_groups2;
368
369                 if ((ptok->user_sids = (DOM_SID *)malloc( sizeof(DOM_SID) * ptok->num_sids )) == NULL) {
370                         DEBUG(0, ("domain_client_validate: Out of memory allocating group SIDS\n"));
371                         nt_status = NT_STATUS_NO_MEMORY;
372                         free_server_info(server_info);
373                         goto done;
374                 }
375  
376                 for (i = 0; i < ptok->num_sids; i++) {
377                         sid_copy(&ptok->user_sids[i], &info3.dom_sid.sid);
378                         sid_append_rid(&ptok->user_sids[i], info3.gids[i].g_rid);
379                 }
380                 
381                 uni_group_cache_store_netlogon(mem_ctx, &info3);
382         }
383
384 #if 0
385         /* 
386          * We don't actually need to do this - plus it fails currently with
387          * NT_STATUS_INVALID_INFO_CLASS - we need to know *exactly* what to
388          * send here. JRA.
389          */
390
391         if (NT_STATUS_IS_OK(status)) {
392                 if(cli_nt_logoff(&cli, &ctr) == False) {
393                         DEBUG(0,("domain_client_validate: unable to log off user %s in domain \
394 %s to Domain controller %s. Error was %s.\n", user, domain, remote_machine, cli_errstr(&cli)));        
395                         nt_status = NT_STATUS_LOGON_FAILURE;
396                 }
397         }
398 #endif /* 0 */
399
400   done:
401
402         /* Note - once the cli stream is shutdown the mem_ctx used
403            to allocate the other_sids and gids structures has been deleted - so
404            these pointers are no longer valid..... */
405
406         cli_nt_session_close(cli);
407         cli_ulogoff(cli);
408         cli_shutdown(cli);
409         return nt_status;
410 }
411
412 /****************************************************************************
413  Check for a valid username and password in security=domain mode.
414 ****************************************************************************/
415
416 static NTSTATUS check_ntdomain_security(const struct auth_context *auth_context,
417                                         void *my_private_data, 
418                                         TALLOC_CTX *mem_ctx,
419                                         const auth_usersupplied_info *user_info, 
420                                         auth_serversupplied_info **server_info)
421 {
422         NTSTATUS nt_status = NT_STATUS_LOGON_FAILURE;
423         char *password_server;
424         unsigned char trust_passwd[16];
425         time_t last_change_time;
426         char *domain = lp_workgroup();
427
428         if (!user_info || !server_info || !auth_context) {
429                 DEBUG(1,("check_ntdomain_security: Critical variables not present.  Failing.\n"));
430                 return NT_STATUS_INVALID_PARAMETER;
431         }
432
433         /* 
434          * Check that the requested domain is not our own machine name.
435          * If it is, we should never check the PDC here, we use our own local
436          * password file.
437          */
438
439         if(is_netbios_alias_or_name(user_info->domain.str)) {
440                 DEBUG(3,("check_ntdomain_security: Requested domain was for this machine.\n"));
441                 return NT_STATUS_LOGON_FAILURE;
442         }
443
444         /*
445          * Get the machine account password for our primary domain
446          * No need to become_root() as secrets_init() is done at startup.
447          */
448
449         if (!secrets_fetch_trust_account_password(domain, trust_passwd, &last_change_time))
450         {
451                 DEBUG(0, ("check_ntdomain_security: could not fetch trust account password for domain %s\n", lp_workgroup()));
452                 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
453         }
454
455         /* Test if machine password is expired and need to be changed */
456         if (time(NULL) > last_change_time + lp_machine_password_timeout())
457         {
458                 global_machine_password_needs_changing = True;
459         }
460
461         /*
462          * Treat each name in the 'password server =' line as a potential
463          * PDC/BDC. Contact each in turn and try and authenticate.
464          */
465
466         password_server = lp_passwordserver();
467
468         nt_status = domain_client_validate(mem_ctx, user_info, domain,
469                                            (uchar *)auth_context->challenge.data, 
470                                            server_info, 
471                                            password_server, global_myname, SEC_CHAN_WKSTA, trust_passwd, last_change_time);
472         return nt_status;
473 }
474
475 /* module initialisation */
476 BOOL auth_init_ntdomain(struct auth_context *auth_context, auth_methods **auth_method) 
477 {
478         if (!make_auth_methods(auth_context, auth_method)) {
479                 return False;
480         }
481
482         (*auth_method)->auth = check_ntdomain_security;
483         return True;
484 }
485
486
487 /****************************************************************************
488  Check for a valid username and password in a trusted domain
489 ****************************************************************************/
490
491 static NTSTATUS check_trustdomain_security(const struct auth_context *auth_context,
492                                            void *my_private_data, 
493                                            TALLOC_CTX *mem_ctx,
494                                            const auth_usersupplied_info *user_info, 
495                                            auth_serversupplied_info **server_info)
496 {
497         NTSTATUS nt_status = NT_STATUS_LOGON_FAILURE;
498         unsigned char trust_md4_password[16];
499         char *trust_password;
500         time_t last_change_time;
501         DOM_SID sid;
502
503         if (!user_info || !server_info || !auth_context) {
504                 DEBUG(1,("check_trustdomain_security: Critical variables not present.  Failing.\n"));
505                 return NT_STATUS_INVALID_PARAMETER;
506         }
507
508         /* 
509          * Check that the requested domain is not our own machine name.
510          * If it is, we should never check the PDC here, we use our own local
511          * password file.
512          */
513
514         if(is_netbios_alias_or_name(user_info->domain.str)) {
515                 DEBUG(3,("check_trustdomain_security: Requested domain was for this machine.\n"));
516                 return NT_STATUS_LOGON_FAILURE;
517         }
518
519         /* 
520          * Check that the requested domain is not our own domain,
521          * If it is, we should use our own local password file.
522          */
523
524         if(strequal(lp_workgroup(), (user_info->domain.str))) {
525                 DEBUG(3,("check_trustdomain_security: Requested domain was for this domain.\n"));
526                 return NT_STATUS_LOGON_FAILURE;
527         }
528
529         /*
530          * Get the machine account password for the trusted domain
531          * No need to become_root() as secrets_init() is done at startup.
532          */
533
534         if (!secrets_fetch_trusted_domain_password(user_info->domain.str, &trust_password, &sid, &last_change_time))
535         {
536                 DEBUG(0, ("check_trustdomain_security: could not fetch trust account password for domain %s\n", user_info->domain.str));
537                 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
538         }
539
540 #ifdef DEBUG_PASSWORD
541         DEBUG(100, ("Trust password for domain %s is %s\n", user_info->domain.str, trust_password));
542 #endif
543         E_md4hash((uchar *)trust_password, trust_md4_password);
544         SAFE_FREE(trust_password);
545
546 #if 0
547         /* Test if machine password is expired and need to be changed */
548         if (time(NULL) > last_change_time + lp_machine_password_timeout())
549         {
550                 global_machine_password_needs_changing = True;
551         }
552 #endif
553
554         nt_status = domain_client_validate(mem_ctx, user_info, user_info->domain.str,
555                                            (uchar *)auth_context->challenge.data, 
556                                            server_info, "*" /* Do a lookup */, 
557                                            lp_workgroup(), SEC_CHAN_DOMAIN, trust_md4_password, last_change_time);
558         
559         return nt_status;
560 }
561
562 /* module initialisation */
563 BOOL auth_init_trustdomain(struct auth_context *auth_context, auth_methods **auth_method) 
564 {
565         if (!make_auth_methods(auth_context, auth_method)) {
566                 return False;
567         }
568
569         (*auth_method)->auth = check_trustdomain_security;
570         return True;
571 }