Use the new client error api.
[samba.git] / source3 / auth / auth_domain.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    Authenticate against a remote domain
5    Copyright (C) Andrew Tridgell 1992-1998
6    Copyright (C) Andrew Bartlett 2001
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24
25 extern int DEBUGLEVEL;
26 extern struct in_addr ipzero;
27
28 BOOL global_machine_password_needs_changing = False;
29
30 extern pstring global_myname;
31
32 /***********************************************************************
33  Connect to a remote machine for domain security authentication
34  given a name or IP address.
35  ***********************************************************************/
36
37 static BOOL connect_to_domain_password_server(struct cli_state *pcli, 
38                                               char *server, unsigned char *trust_passwd)
39 {
40         struct in_addr dest_ip;
41         fstring remote_machine;
42
43         if(cli_initialise(pcli) == NULL) {
44                 DEBUG(0,("connect_to_domain_password_server: unable to initialize client connection.\n"));
45                 return False;
46         }
47
48         if (is_ipaddress(server)) {
49                 struct in_addr to_ip;
50           
51                 /* we shouldn't have 255.255.255.255 forthe IP address of 
52                    a password server anyways */
53                 if ((to_ip.s_addr=inet_addr(server)) == 0xFFFFFFFF) {
54                         DEBUG (0,("connect_to_domain_password_server: inet_addr(%s) returned 0xFFFFFFFF!\n", server));
55                         return False;
56                 }
57
58                 if (!name_status_find(0x20, to_ip, remote_machine)) {
59                         DEBUG(0, ("connect_to_domain_password_server: Can't "
60                                   "resolve name for IP %s\n", server));
61                         return False;
62                 }
63         } else {
64                 fstrcpy(remote_machine, server);
65         }
66
67         standard_sub_basic(remote_machine);
68         strupper(remote_machine);
69
70         if(!resolve_name( remote_machine, &dest_ip, 0x20)) {
71                 DEBUG(1,("connect_to_domain_password_server: Can't resolve address for %s\n", remote_machine));
72                 cli_shutdown(pcli);
73                 return False;
74         }
75   
76         if (ismyip(dest_ip)) {
77                 DEBUG(1,("connect_to_domain_password_server: Password server loop - not using password server %s\n",
78                          remote_machine));
79                 cli_shutdown(pcli);
80                 return False;
81         }
82   
83         if (!cli_connect(pcli, remote_machine, &dest_ip)) {
84                 DEBUG(0,("connect_to_domain_password_server: unable to connect to SMB server on \
85 machine %s. Error was : %s.\n", remote_machine, cli_errstr(pcli) ));
86                 cli_shutdown(pcli);
87                 return False;
88         }
89   
90         if (!attempt_netbios_session_request(pcli, global_myname, remote_machine, &dest_ip)) {
91                 DEBUG(0,("connect_to_password_server: machine %s rejected the NetBIOS \
92 session request. Error was : %s.\n", remote_machine, cli_errstr(pcli) ));
93                 return False;
94         }
95   
96         pcli->protocol = PROTOCOL_NT1;
97
98         if (!cli_negprot(pcli)) {
99                 DEBUG(0,("connect_to_domain_password_server: machine %s rejected the negotiate protocol. \
100 Error was : %s.\n", remote_machine, cli_errstr(pcli) ));
101                 cli_shutdown(pcli);
102                 return False;
103         }
104
105         if (pcli->protocol != PROTOCOL_NT1) {
106                 DEBUG(0,("connect_to_domain_password_server: machine %s didn't negotiate NT protocol.\n",
107                          remote_machine));
108                 cli_shutdown(pcli);
109                 return False;
110         }
111
112         /*
113          * Do an anonymous session setup.
114          */
115
116         if (!cli_session_setup(pcli, "", "", 0, "", 0, "")) {
117                 DEBUG(0,("connect_to_domain_password_server: machine %s rejected the session setup. \
118 Error was : %s.\n", remote_machine, cli_errstr(pcli) ));
119                 cli_shutdown(pcli);
120                 return False;
121         }
122
123         if (!(pcli->sec_mode & 1)) {
124                 DEBUG(1,("connect_to_domain_password_server: machine %s isn't in user level security mode\n",
125                          remote_machine));
126                 cli_shutdown(pcli);
127                 return False;
128         }
129
130         if (!cli_send_tconX(pcli, "IPC$", "IPC", "", 1)) {
131                 DEBUG(0,("connect_to_domain_password_server: machine %s rejected the tconX on the IPC$ share. \
132 Error was : %s.\n", remote_machine, cli_errstr(pcli) ));
133                 cli_shutdown(pcli);
134                 return False;
135         }
136
137         /*
138          * We now have an anonymous connection to IPC$ on the domain password server.
139          */
140
141         /*
142          * Even if the connect succeeds we need to setup the netlogon
143          * pipe here. We do this as we may just have changed the domain
144          * account password on the PDC and yet we may be talking to
145          * a BDC that doesn't have this replicated yet. In this case
146          * a successful connect to a DC needs to take the netlogon connect
147          * into account also. This patch from "Bjart Kvarme" <bjart.kvarme@usit.uio.no>.
148          */
149
150         if(cli_nt_session_open(pcli, PIPE_NETLOGON) == False) {
151                 DEBUG(0,("connect_to_domain_password_server: unable to open the domain client session to \
152 machine %s. Error was : %s.\n", remote_machine, cli_errstr(pcli)));
153                 cli_nt_session_close(pcli);
154                 cli_ulogoff(pcli);
155                 cli_shutdown(pcli);
156                 return False;
157         }
158
159         if (cli_nt_setup_creds(pcli, trust_passwd) == False) {
160                 DEBUG(0,("connect_to_domain_password_server: unable to setup the PDC credentials to machine \
161 %s. Error was : %s.\n", remote_machine, cli_errstr(pcli)));
162                 cli_nt_session_close(pcli);
163                 cli_ulogoff(pcli);
164                 cli_shutdown(pcli);
165                 return(False);
166         }
167
168         return True;
169 }
170
171 /***********************************************************************
172  Utility function to attempt a connection to an IP address of a DC.
173 ************************************************************************/
174
175 static BOOL attempt_connect_to_dc(struct cli_state *pcli, struct in_addr *ip, 
176                                   unsigned char *trust_passwd)
177 {
178         fstring dc_name;
179
180         /*
181          * Ignore addresses we have already tried.
182          */
183
184         if (ip_equal(ipzero, *ip))
185                 return False;
186
187         if (!lookup_pdc_name(global_myname, lp_workgroup(), ip, dc_name))
188                 return False;
189
190         return connect_to_domain_password_server(pcli, dc_name, trust_passwd);
191 }
192
193 /***********************************************************************
194  We have been asked to dynamcially determine the IP addresses of
195  the PDC and BDC's for this DOMAIN, and query them in turn.
196 ************************************************************************/
197 static BOOL find_connect_pdc(struct cli_state *pcli, 
198                              unsigned char *trust_passwd, 
199                              time_t last_change_time)
200 {
201         struct in_addr *ip_list = NULL;
202         int count = 0;
203         int i;
204         BOOL connected_ok = False;
205         time_t time_now = time(NULL);
206         BOOL use_pdc_only = False;
207
208         /*
209          * If the time the machine password has changed
210          * was less than an hour ago then we need to contact
211          * the PDC only, as we cannot be sure domain replication
212          * has yet taken place. Bug found by Gerald (way to go
213          * Gerald !). JRA.
214          */
215
216         if (time_now - last_change_time < 3600)
217                 use_pdc_only = True;
218
219         if (!get_dc_list(use_pdc_only, lp_workgroup(), &ip_list, &count))
220                 return False;
221
222         /*
223          * Firstly try and contact a PDC/BDC who has the same
224          * network address as any of our interfaces.
225          */
226         for(i = 0; i < count; i++) {
227                 if(!is_local_net(ip_list[i]))
228                         continue;
229
230                 if((connected_ok = attempt_connect_to_dc(pcli, &ip_list[i], trust_passwd))) 
231                         break;
232                 
233                 ip_list[i] = ipzero; /* Tried and failed. */
234         }
235
236         /*
237          * Secondly try and contact a random PDC/BDC.
238          */
239         if(!connected_ok) {
240                 i = (sys_random() % count);
241
242                 if (!(connected_ok = attempt_connect_to_dc(pcli, &ip_list[i], trust_passwd)))
243                         ip_list[i] = ipzero; /* Tried and failed. */
244         }
245
246         /*
247          * Finally go through the IP list in turn, ignoring any addresses
248          * we have already tried.
249          */
250         if(!connected_ok) {
251                 /*
252                  * Try and connect to any of the other IP addresses in the PDC/BDC list.
253                  * Note that from a WINS server the #1 IP address is the PDC.
254                  */
255                 for(i = 0; i < count; i++) {
256                         if((connected_ok = attempt_connect_to_dc(pcli, &ip_list[i], trust_passwd)))
257                                 break;
258                 }
259         }
260
261         if(ip_list != NULL)
262                 free((char *)ip_list);
263
264
265         return connected_ok;
266 }
267
268 /***********************************************************************
269  Do the same as security=server, but using NT Domain calls and a session
270  key from the machine password.  If the server parameter is specified
271  use it, otherwise figure out a server from the 'password server' param.
272 ************************************************************************/
273
274 uint32 domain_client_validate(const auth_usersupplied_info *user_info, 
275                               auth_serversupplied_info *server_info, 
276                               char *server)
277 {
278         unsigned char trust_passwd[16];
279         fstring remote_machine;
280         char *p, *pserver;
281         NET_ID_INFO_CTR ctr;
282         NET_USER_INFO_3 info3;
283         struct cli_state cli;
284         uint32 smb_uid_low;
285         BOOL connected_ok = False;
286         time_t last_change_time;
287         uint32 nt_status;
288
289         /* 
290          * Check that the requested domain is not our own machine name.
291          * If it is, we should never check the PDC here, we use our own local
292          * password file.
293          */
294
295         if(strequal(user_info->domain.str, global_myname)) {
296                 DEBUG(3,("domain_client_validate: Requested domain was for this machine.\n"));
297                 return NT_STATUS_LOGON_FAILURE;
298         }
299
300         /*
301          * Get the machine account password for our primary domain
302          */
303         if (!secrets_fetch_trust_account_password(lp_workgroup(), trust_passwd, &last_change_time))
304         {
305                 DEBUG(0, ("domain_client_validate: could not fetch trust account password for domain %s\n", lp_workgroup()));
306                 return NT_STATUS_LOGON_FAILURE;
307         }
308
309         /* Test if machine password is expired and need to be changed */
310         if (time(NULL) > last_change_time + lp_machine_password_timeout())
311         {
312                 global_machine_password_needs_changing = True;
313         }
314
315         /*
316          * At this point, smb_apasswd points to the lanman response to
317          * the challenge in local_challenge, and smb_ntpasswd points to
318          * the NT response to the challenge in local_challenge. Ship
319          * these over the secure channel to a domain controller and
320          * see if they were valid.
321          */
322
323         ZERO_STRUCT(cli);
324
325         /*
326          * Treat each name in the 'password server =' line as a potential
327          * PDC/BDC. Contact each in turn and try and authenticate.
328          */
329
330         if (server) {
331                 p = server;
332         } else {
333                 pserver = lp_passwordserver();
334                 if (! *pserver) pserver = "*";
335                 p = pserver;
336         }
337
338         while (!connected_ok &&
339                next_token(&p,remote_machine,LIST_SEP,sizeof(remote_machine))) {
340                 if(strequal(remote_machine, "*")) {
341                         connected_ok = find_connect_pdc(&cli, trust_passwd, last_change_time);
342                 } else {
343                         connected_ok = connect_to_domain_password_server(&cli, remote_machine, trust_passwd);
344                 }
345         }
346
347         if (!connected_ok) {
348                 DEBUG(0,("domain_client_validate: Domain password server not available.\n"));
349                 cli_shutdown(&cli);
350                 return NT_STATUS_LOGON_FAILURE;
351         }
352
353         /* We really don't care what LUID we give the user. */
354         generate_random_buffer( (unsigned char *)&smb_uid_low, 4, False);
355
356         ZERO_STRUCT(info3);
357
358         cli_nt_login_network(&cli, user_info->domain.str, user_info->smb_username.str, smb_uid_low, user_info->chal,
359                              user_info->lm_resp.buffer, user_info->lm_resp.len, 
360                              user_info->nt_resp.buffer, user_info->lm_resp.len,
361                              &ctr, &info3);
362
363         nt_status = cli_nt_error(&cli);
364         if (nt_status != NT_STATUS_NOPROBLEMO) {
365                 DEBUG(0,("domain_client_validate: unable to validate password for user %s in domain \
366 %s to Domain controller %s. Error was %s.\n", user_info->smb_username.str, user_info->domain.str, remote_machine, cli_errstr(&cli)));
367         }
368
369         /*
370          * Here, if we really want it, we have lots of info about the user in info3.
371          */
372
373 #if 0
374         /* 
375          * We don't actually need to do this - plus it fails currently with
376          * NT_STATUS_INVALID_INFO_CLASS - we need to know *exactly* what to
377          * send here. JRA.
378          */
379
380         if (nt_status == NT_STATUS_NOPROBLMO) {
381                 if(cli_nt_logoff(&cli, &ctr) == False) {
382                         DEBUG(0,("domain_client_validate: unable to log off user %s in domain \
383 %s to Domain controller %s. Error was %s.\n", user, domain, remote_machine, cli_errstr(&cli)));        
384                         nt_status = NT_STATUS_LOGON_FAILURE;
385                 }
386         }
387 #endif /* 0 */
388
389         /* Note - once the cli stream is shutdown the mem_ctx used
390            to allocate the other_sids and gids structures has been deleted - so
391            these pointers are no longer valid..... */
392
393         cli_nt_session_close(&cli);
394         cli_ulogoff(&cli);
395         cli_shutdown(&cli);
396         return nt_status;
397 }
398
399 /****************************************************************************
400  Check for a valid username and password in security=domain mode.
401 ****************************************************************************/
402
403 uint32 check_domain_security(const auth_usersupplied_info *user_info, 
404                              auth_serversupplied_info *server_info)
405 {
406         uint32 nt_status = NT_STATUS_LOGON_FAILURE;
407
408         if(lp_security() != SEC_DOMAIN)
409                 return NT_STATUS_LOGON_FAILURE;
410
411         nt_status = domain_client_validate(user_info, server_info, NULL);
412
413         return nt_status;
414 }
415
416
417