8a2d8e28cc144ba0e13788f340b11173d8da01d5
[vlendec/samba-autobuild/.git] / source3 / rpc_client / cli_netlogon.c
1 /* 
2  *  Unix SMB/Netbios implementation.
3  *  Version 1.9.
4  *  RPC Pipe client / server routines
5  *  Copyright (C) Andrew Tridgell              1992-1997,
6  *  Copyright (C) Luke Kenneth Casson Leighton 1996-1997,
7  *  Copyright (C) Paul Ashton                       1997.
8  *  Copyright (C) Jeremy Allison                    1998.
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *  
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *  
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software
22  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24
25
26 #include "includes.h"
27
28 extern pstring global_myname;
29 extern fstring global_myworkgroup;
30
31 /****************************************************************************
32 Generate the next creds to use.
33 ****************************************************************************/
34
35 static void gen_next_creds( struct cli_state *cli, DOM_CRED *new_clnt_cred)
36 {
37   /*
38    * Create the new client credentials.
39    */
40
41   cli->clnt_cred.timestamp.time = time(NULL);
42
43   memcpy(new_clnt_cred, &cli->clnt_cred, sizeof(*new_clnt_cred));
44
45   /* Calculate the new credentials. */
46   cred_create(cli->sess_key, &(cli->clnt_cred.challenge),
47               new_clnt_cred->timestamp, &(new_clnt_cred->challenge));
48
49 }
50
51 #if UNUSED_CODE
52 /****************************************************************************
53 do a LSA Logon Control2
54 ****************************************************************************/
55 BOOL cli_net_logon_ctrl2(struct cli_state *cli, NTSTATUS status_level)
56 {
57   prs_struct rbuf;
58   prs_struct buf; 
59   NET_Q_LOGON_CTRL2 q_l;
60   BOOL ok = False;
61
62   prs_init(&buf , 1024, cli->mem_ctx, MARSHALL);
63   prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
64
65   /* create and send a MSRPC command with api NET_LOGON_CTRL2 */
66
67   DEBUG(4,("do_net_logon_ctrl2 from %s status level:%x\n",
68            global_myname, status_level));
69
70   /* store the parameters */
71   init_q_logon_ctrl2(&q_l, cli->srv_name_slash, 
72                      status_level);
73
74   /* turn parameters into data stream */
75   if(!net_io_q_logon_ctrl2("", &q_l,  &buf, 0)) {
76     DEBUG(0,("cli_net_logon_ctrl2: Error : failed to marshall NET_Q_LOGON_CTRL2 struct.\n"));
77     prs_mem_free(&buf);
78     prs_mem_free(&rbuf);
79     return False;
80   }
81
82   /* send the data on \PIPE\ */
83   if (rpc_api_pipe_req(cli, NET_LOGON_CTRL2, &buf, &rbuf))
84   {
85     NET_R_LOGON_CTRL2 r_l;
86
87     /*
88      * Unmarshall the return buffer.
89      */
90     ok = net_io_r_logon_ctrl2("", &r_l, &rbuf, 0);
91                 
92     if (ok && r_l.status != 0)
93     {
94       /* report error code */
95       DEBUG(0,("do_net_logon_ctrl2: Error %s\n", get_nt_error_msg(r_l.status)));
96       cli->nt_error = r_l.status;
97       ok = False;
98     }
99   }
100
101   prs_mem_free(&buf);
102   prs_mem_free(&rbuf);
103
104   return ok;
105 }
106 #endif
107
108 /****************************************************************************
109 LSA Authenticate 2
110
111 Send the client credential, receive back a server credential.
112 Ensure that the server credential returned matches the session key 
113 encrypt of the server challenge originally received. JRA.
114 ****************************************************************************/
115
116 NTSTATUS cli_net_auth2(struct cli_state *cli, uint16 sec_chan, 
117                        uint32 neg_flags, DOM_CHAL *srv_chal)
118 {
119   prs_struct rbuf;
120   prs_struct buf; 
121   NET_Q_AUTH_2 q_a;
122   BOOL ok = False;
123   NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
124
125   prs_init(&buf , 1024, cli->mem_ctx, MARSHALL);
126   prs_init(&rbuf, 0,    cli->mem_ctx, UNMARSHALL);
127
128   /* create and send a MSRPC command with api NET_AUTH2 */
129
130   DEBUG(4,("cli_net_auth2: srv:%s acct:%s sc:%x mc: %s chal %s neg: %x\n",
131            cli->srv_name_slash, cli->mach_acct, sec_chan, global_myname,
132            credstr(cli->clnt_cred.challenge.data), neg_flags));
133
134   /* store the parameters */
135   init_q_auth_2(&q_a, cli->srv_name_slash, cli->mach_acct, 
136                 sec_chan, global_myname, &cli->clnt_cred.challenge, neg_flags);
137
138   /* turn parameters into data stream */
139   if(!net_io_q_auth_2("", &q_a,  &buf, 0)) {
140     DEBUG(0,("cli_net_auth2: Error : failed to marshall NET_Q_AUTH_2 struct.\n"));
141     prs_mem_free(&buf);
142     prs_mem_free(&rbuf);
143     return result;
144   }
145
146   /* send the data on \PIPE\ */
147   if (rpc_api_pipe_req(cli, NET_AUTH2, &buf, &rbuf))
148   {
149     NET_R_AUTH_2 r_a;
150
151     ok = net_io_r_auth_2("", &r_a, &rbuf, 0);
152     result = r_a.status;
153
154     if (ok && !NT_STATUS_IS_OK(result))
155     {
156       /* report error code */
157       DEBUG(0,("cli_net_auth2: Error %s\n", get_nt_error_msg(result)));
158       ok = False;
159     }
160
161     if (ok)
162     {
163       /* 
164        * Check the returned value using the initial
165        * server received challenge.
166        */
167       UTIME zerotime;
168
169       zerotime.time = 0;
170       if(cred_assert( &r_a.srv_chal, cli->sess_key, srv_chal, zerotime) == 0) {
171         /*
172          * Server replied with bad credential. Fail.
173          */
174         DEBUG(0,("cli_net_auth2: server %s replied with bad credential (bad machine \
175 password ?).\n", cli->desthost ));
176         ok = False;
177       }
178     }
179
180 #if 0
181     /*
182      * Try commenting this out to see if this makes the connect
183      * work for a NT 3.51 PDC. JRA.
184      */
185
186     if (ok && r_a.srv_flgs.neg_flags != q_a.clnt_flgs.neg_flags)
187     {
188       /* report different neg_flags */
189       DEBUG(0,("cli_net_auth2: error neg_flags (q,r) differ - (%x,%x)\n",
190           q_a.clnt_flgs.neg_flags, r_a.srv_flgs.neg_flags));
191       ok = False;
192     }
193 #endif
194
195   }
196
197   prs_mem_free(&buf);
198   prs_mem_free(&rbuf);
199
200   return result;
201 }
202
203 /****************************************************************************
204 LSA Request Challenge. Sends our challenge to server, then gets
205 server response. These are used to generate the credentials.
206 ****************************************************************************/
207
208 BOOL cli_net_req_chal(struct cli_state *cli, DOM_CHAL *clnt_chal, DOM_CHAL *srv_chal)
209 {
210   prs_struct rbuf;
211   prs_struct buf; 
212   NET_Q_REQ_CHAL q_c;
213   BOOL valid_chal = False;
214
215   prs_init(&buf , 1024, cli->mem_ctx, MARSHALL);
216   prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
217
218   /* create and send a MSRPC command with api NET_REQCHAL */
219
220   DEBUG(4,("cli_net_req_chal: LSA Request Challenge from %s to %s: %s\n",
221          cli->desthost, global_myname, credstr(clnt_chal->data)));
222
223   /* store the parameters */
224   init_q_req_chal(&q_c, cli->srv_name_slash, 
225                   global_myname, clnt_chal);
226
227   /* turn parameters into data stream */
228   if(!net_io_q_req_chal("", &q_c,  &buf, 0)) {
229     DEBUG(0,("cli_net_req_chal: Error : failed to marshall NET_Q_REQ_CHAL struct.\n"));
230     prs_mem_free(&buf);
231     prs_mem_free(&rbuf);
232     return False;
233   }
234
235   /* send the data on \PIPE\ */
236   if (rpc_api_pipe_req(cli, NET_REQCHAL, &buf, &rbuf))
237   {
238     NET_R_REQ_CHAL r_c;
239     BOOL ok;
240
241     ok = net_io_r_req_chal("", &r_c, &rbuf, 0);
242                 
243     if (ok && !NT_STATUS_IS_OK(r_c.status))
244     {
245       /* report error code */
246       DEBUG(0,("cli_net_req_chal: Error %s\n", get_nt_error_msg(r_c.status)));
247       ok = False;
248     }
249
250     if (ok)
251     {
252       /* ok, at last: we're happy. return the challenge */
253       memcpy(srv_chal, r_c.srv_chal.data, sizeof(srv_chal->data));
254       valid_chal = True;
255     }
256   }
257
258   prs_mem_free(&buf);
259   prs_mem_free(&rbuf);
260
261   return valid_chal;
262 }
263 /***************************************************************************
264  LSA SAM Logon internal - interactive or network. Does level 2 or 3 but always
265  returns level 3.
266 ****************************************************************************/
267
268 static NTSTATUS cli_net_sam_logon_internal(struct cli_state *cli, NET_ID_INFO_CTR *ctr, 
269                                            NET_USER_INFO_3 *user_info3, 
270                                            uint16 validation_level)
271 {
272         DOM_CRED new_clnt_cred;
273         DOM_CRED dummy_rtn_creds;
274         prs_struct rbuf;
275         prs_struct buf; 
276         NET_Q_SAM_LOGON q_s;
277         NET_R_SAM_LOGON r_s;
278         NTSTATUS retval = NT_STATUS_OK;
279
280         gen_next_creds( cli, &new_clnt_cred);
281
282         prs_init(&buf , 1024, cli->mem_ctx, MARSHALL);
283         prs_init(&rbuf, 0,    cli->mem_ctx, UNMARSHALL);
284
285         /* create and send a MSRPC command with api NET_SAMLOGON */
286
287         DEBUG(4,("cli_net_sam_logon_internal: srv:%s mc:%s clnt %s %x ll: %d\n",
288              cli->srv_name_slash, global_myname, 
289              credstr(new_clnt_cred.challenge.data), cli->clnt_cred.timestamp.time,
290              ctr->switch_value));
291
292         memset(&dummy_rtn_creds, '\0', sizeof(dummy_rtn_creds));
293         dummy_rtn_creds.timestamp.time = time(NULL);
294
295         /* store the parameters */
296         q_s.validation_level = validation_level;
297         init_sam_info(&q_s.sam_id, cli->srv_name_slash, 
298                 global_myname, &new_clnt_cred, &dummy_rtn_creds, 
299                 ctr->switch_value, ctr);
300
301         /* turn parameters into data stream */
302         if(!net_io_q_sam_logon("", &q_s,  &buf, 0)) {
303                 DEBUG(0,("cli_net_sam_logon_internal: Error : failed to marshall NET_Q_SAM_LOGON struct.\n"));
304                 retval = NT_STATUS_NO_MEMORY;
305                 goto out;
306         }
307
308         /* send the data on \PIPE\ */
309         if (!rpc_api_pipe_req(cli, NET_SAMLOGON, &buf, &rbuf)) {
310                 DEBUG(0,("cli_net_sam_logon_internal: Error rpc_api_pipe_req failed.\n"));
311                 retval = NT_STATUS_UNSUCCESSFUL;
312                 goto out;
313         }
314
315         r_s.user = user_info3;
316
317         if(!net_io_r_sam_logon("", &r_s, &rbuf, 0)) {
318                 DEBUG(0,("cli_net_sam_logon_internal: Error : failed to unmarshal NET_R_SAM_LOGON struct.\n"));
319                 retval = NT_STATUS_NO_MEMORY;
320                 goto out;
321         }
322                 
323         retval = r_s.status;
324
325         /*
326          * Don't treat NT_STATUS_INVALID_INFO_CLASS as an error - we will re-issue
327          * the call.
328          */
329         
330         if (NT_STATUS_V(retval) == NT_STATUS_V(NT_STATUS_INVALID_INFO_CLASS)) {
331                 goto out;
332         }
333
334         if (!NT_STATUS_IS_OK(retval)) {
335                 /* report error code */
336                 DEBUG(0,("cli_net_sam_logon_internal: %s\n", get_nt_error_msg(r_s.status)));
337                 goto out;
338         }
339
340         /* Update the credentials. */
341         if (!clnt_deal_with_creds(cli->sess_key, &cli->clnt_cred, &r_s.srv_creds)) {
342                 /*
343                  * Server replied with bad credential. Fail.
344                  */
345                 DEBUG(0,("cli_net_sam_logon_internal: server %s replied with bad credential (bad machine \
346 password ?).\n", cli->desthost ));
347                 retval = NT_STATUS_WRONG_PASSWORD;
348         }
349         
350         if (r_s.switch_value != validation_level) {
351                 /* report different switch_value */
352                 DEBUG(0,("cli_net_sam_logon: switch_value of %x expected %x\n", (unsigned int)validation_level,
353                          (unsigned int)r_s.switch_value));
354                 retval = NT_STATUS_INVALID_PARAMETER;
355         }
356
357 out:
358
359         prs_mem_free(&buf);
360         prs_mem_free(&rbuf);
361         
362         return retval;
363 }
364
365 /***************************************************************************
366 LSA SAM Logon - interactive or network.
367 ****************************************************************************/
368
369 NTSTATUS cli_net_sam_logon(struct cli_state *cli, NET_ID_INFO_CTR *ctr, 
370                          NET_USER_INFO_3 *user_info3)
371 {
372         uint16 validation_level=3;
373         NTSTATUS result;
374
375         result = cli_net_sam_logon_internal(cli, ctr, user_info3, 
376                                             validation_level);
377
378         if (NT_STATUS_IS_OK(result)) {
379                 DEBUG(10,("cli_net_sam_logon: Success \n"));
380         } else if (NT_STATUS_V(result) == NT_STATUS_V(NT_STATUS_INVALID_INFO_CLASS)) {
381                 DEBUG(10,("cli_net_sam_logon: STATUS INVALID INFO CLASS \n"));
382
383                 validation_level=2;
384
385                 /*
386                  * Since this is the second time we call this function, don't care
387                  * for the error. If its error, return False. 
388                  */
389
390                 result = cli_net_sam_logon_internal(cli, ctr, user_info3,
391                                                     validation_level);
392         }
393
394         return result;
395 }
396
397 /***************************************************************************
398 LSA SAM Logoff.
399
400 This currently doesnt work correctly as the domain controller 
401 returns NT_STATUS_INVALID_INFO_CLASS - we obviously need to
402 send a different info level. Right now though, I'm not sure
403 what that needs to be (I need to see one on the wire before
404 I can be sure). JRA.
405 ****************************************************************************/
406 BOOL cli_net_sam_logoff(struct cli_state *cli, NET_ID_INFO_CTR *ctr)
407 {
408   DOM_CRED new_clnt_cred;
409   DOM_CRED dummy_rtn_creds;
410   prs_struct rbuf;
411   prs_struct buf; 
412   NET_Q_SAM_LOGOFF q_s;
413   BOOL ok = False;
414
415   gen_next_creds( cli, &new_clnt_cred);
416
417   prs_init(&buf , 1024, cli->mem_ctx, MARSHALL);
418   prs_init(&rbuf, 0,    cli->mem_ctx, UNMARSHALL);
419
420   /* create and send a MSRPC command with api NET_SAMLOGOFF */
421
422   DEBUG(4,("cli_net_sam_logoff: srv:%s mc:%s clnt %s %x ll: %d\n",
423             cli->srv_name_slash, global_myname,
424             credstr(new_clnt_cred.challenge.data), new_clnt_cred.timestamp.time,
425             ctr->switch_value));
426
427   memset(&dummy_rtn_creds, '\0', sizeof(dummy_rtn_creds));
428
429   init_sam_info(&q_s.sam_id, cli->srv_name_slash, 
430                 global_myname, &new_clnt_cred, &dummy_rtn_creds, 
431                 ctr->switch_value, ctr);
432
433   /* turn parameters into data stream */
434   if(!net_io_q_sam_logoff("", &q_s,  &buf, 0)) {
435     DEBUG(0,("cli_net_sam_logoff: Error : failed to marshall NET_Q_SAM_LOGOFF struct.\n"));
436     prs_mem_free(&buf);
437     prs_mem_free(&rbuf);
438     return False;
439   }
440
441   /* send the data on \PIPE\ */
442   if (rpc_api_pipe_req(cli, NET_SAMLOGOFF, &buf, &rbuf))
443   {
444     NET_R_SAM_LOGOFF r_s;
445
446     ok = net_io_r_sam_logoff("", &r_s, &rbuf, 0);
447                 
448     if (ok && !NT_STATUS_IS_OK(r_s.status))
449     {
450       /* report error code */
451       DEBUG(0,("cli_net_sam_logoff: %s\n", get_nt_error_msg(r_s.status)));
452       ok = False;
453     }
454
455     /* Update the credentials. */
456     if (ok && !clnt_deal_with_creds(cli->sess_key, &(cli->clnt_cred), &(r_s.srv_creds)))
457     {
458       /*
459        * Server replied with bad credential. Fail.
460        */
461       DEBUG(0,("cli_net_sam_logoff: server %s replied with bad credential (bad machine \
462 password ?).\n", cli->desthost ));
463       ok = False;
464     }
465   }
466
467   prs_mem_free(&buf);
468   prs_mem_free(&rbuf);
469
470   return ok;
471 }