Removed duplicate debug.
[samba.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 /***************************************************************************
265 LSA Server Password Set.
266 ****************************************************************************/
267
268 BOOL cli_net_srv_pwset(struct cli_state *cli, uint8 hashed_mach_pwd[16])
269 {
270   prs_struct rbuf;
271   prs_struct buf; 
272   DOM_CRED new_clnt_cred;
273   NET_Q_SRV_PWSET q_s;
274   BOOL ok = False;
275   uint16 sec_chan_type = 2;
276
277   gen_next_creds( cli, &new_clnt_cred);
278
279   prs_init(&buf , 1024, cli->mem_ctx, MARSHALL);
280   prs_init(&rbuf, 0,    cli->mem_ctx, UNMARSHALL);
281
282   /* create and send a MSRPC command with api NET_SRV_PWSET */
283
284   DEBUG(4,("cli_net_srv_pwset: srv:%s acct:%s sc: %d mc: %s clnt %s %x\n",
285            cli->srv_name_slash, cli->mach_acct, sec_chan_type, global_myname,
286            credstr(new_clnt_cred.challenge.data), new_clnt_cred.timestamp.time));
287
288   /* store the parameters */
289   init_q_srv_pwset(&q_s, cli->srv_name_slash, 
290                    cli->mach_acct, sec_chan_type, global_myname, 
291                    &new_clnt_cred, (char *)hashed_mach_pwd);
292
293   /* turn parameters into data stream */
294   if(!net_io_q_srv_pwset("", &q_s,  &buf, 0)) {
295     DEBUG(0,("cli_net_srv_pwset: Error : failed to marshall NET_Q_SRV_PWSET struct.\n"));
296     prs_mem_free(&buf);
297     prs_mem_free(&rbuf);
298     return False;
299   }
300
301   /* send the data on \PIPE\ */
302   if (rpc_api_pipe_req(cli, NET_SRVPWSET, &buf, &rbuf))
303   {
304     NET_R_SRV_PWSET r_s;
305
306     ok = net_io_r_srv_pwset("", &r_s, &rbuf, 0);
307                 
308     if (ok && !NT_STATUS_IS_OK(r_s.status))
309     {
310       /* report error code */
311       DEBUG(0,("cli_net_srv_pwset: %s\n", get_nt_error_msg(r_s.status)));
312       ok = False;
313     }
314
315     /* Update the credentials. */
316     if (ok && !clnt_deal_with_creds(cli->sess_key, &(cli->clnt_cred), &(r_s.srv_cred)))
317     {
318       /*
319        * Server replied with bad credential. Fail.
320        */
321       DEBUG(0,("cli_net_srv_pwset: server %s replied with bad credential (bad machine \
322 password ?).\n", cli->desthost ));
323       ok = False;
324     }
325   }
326
327   prs_mem_free(&buf);
328   prs_mem_free(&rbuf);
329
330   return ok;
331 }
332
333 /***************************************************************************
334  LSA SAM Logon internal - interactive or network. Does level 2 or 3 but always
335  returns level 3.
336 ****************************************************************************/
337
338 static NTSTATUS cli_net_sam_logon_internal(struct cli_state *cli, NET_ID_INFO_CTR *ctr, 
339                                            NET_USER_INFO_3 *user_info3, 
340                                            uint16 validation_level)
341 {
342         DOM_CRED new_clnt_cred;
343         DOM_CRED dummy_rtn_creds;
344         prs_struct rbuf;
345         prs_struct buf; 
346         NET_Q_SAM_LOGON q_s;
347         NET_R_SAM_LOGON r_s;
348         NTSTATUS retval = NT_STATUS_OK;
349
350         gen_next_creds( cli, &new_clnt_cred);
351
352         prs_init(&buf , 1024, cli->mem_ctx, MARSHALL);
353         prs_init(&rbuf, 0,    cli->mem_ctx, UNMARSHALL);
354
355         /* create and send a MSRPC command with api NET_SAMLOGON */
356
357         DEBUG(4,("cli_net_sam_logon_internal: srv:%s mc:%s clnt %s %x ll: %d\n",
358              cli->srv_name_slash, global_myname, 
359              credstr(new_clnt_cred.challenge.data), cli->clnt_cred.timestamp.time,
360              ctr->switch_value));
361
362         memset(&dummy_rtn_creds, '\0', sizeof(dummy_rtn_creds));
363         dummy_rtn_creds.timestamp.time = time(NULL);
364
365         /* store the parameters */
366         q_s.validation_level = validation_level;
367         init_sam_info(&q_s.sam_id, cli->srv_name_slash, 
368                 global_myname, &new_clnt_cred, &dummy_rtn_creds, 
369                 ctr->switch_value, ctr);
370
371         /* turn parameters into data stream */
372         if(!net_io_q_sam_logon("", &q_s,  &buf, 0)) {
373                 DEBUG(0,("cli_net_sam_logon_internal: Error : failed to marshall NET_Q_SAM_LOGON struct.\n"));
374                 retval = NT_STATUS_NO_MEMORY;
375                 goto out;
376         }
377
378         /* send the data on \PIPE\ */
379         if (!rpc_api_pipe_req(cli, NET_SAMLOGON, &buf, &rbuf)) {
380                 DEBUG(0,("cli_net_sam_logon_internal: Error rpc_api_pipe_req failed.\n"));
381                 retval = NT_STATUS_UNSUCCESSFUL;
382                 goto out;
383         }
384
385         r_s.user = user_info3;
386
387         if(!net_io_r_sam_logon("", &r_s, &rbuf, 0)) {
388                 DEBUG(0,("cli_net_sam_logon_internal: Error : failed to unmarshal NET_R_SAM_LOGON struct.\n"));
389                 retval = NT_STATUS_NO_MEMORY;
390                 goto out;
391         }
392                 
393         retval = r_s.status;
394
395         /*
396          * Don't treat NT_STATUS_INVALID_INFO_CLASS as an error - we will re-issue
397          * the call.
398          */
399         
400         if (NT_STATUS_V(retval) == NT_STATUS_V(NT_STATUS_INVALID_INFO_CLASS)) {
401                 goto out;
402         }
403
404         if (!NT_STATUS_IS_OK(retval)) {
405                 /* report error code */
406                 DEBUG(0,("cli_net_sam_logon_internal: %s\n", get_nt_error_msg(r_s.status)));
407                 goto out;
408         }
409
410         /* Update the credentials. */
411         if (!clnt_deal_with_creds(cli->sess_key, &cli->clnt_cred, &r_s.srv_creds)) {
412                 /*
413                  * Server replied with bad credential. Fail.
414                  */
415                 DEBUG(0,("cli_net_sam_logon_internal: server %s replied with bad credential (bad machine \
416 password ?).\n", cli->desthost ));
417                 retval = NT_STATUS_WRONG_PASSWORD;
418         }
419         
420         if (r_s.switch_value != validation_level) {
421                 /* report different switch_value */
422                 DEBUG(0,("cli_net_sam_logon: switch_value of %x expected %x\n", (unsigned int)validation_level,
423                          (unsigned int)r_s.switch_value));
424                 retval = NT_STATUS_INVALID_PARAMETER;
425         }
426
427 out:
428
429         prs_mem_free(&buf);
430         prs_mem_free(&rbuf);
431         
432         return retval;
433 }
434
435 /***************************************************************************
436 LSA SAM Logon - interactive or network.
437 ****************************************************************************/
438
439 NTSTATUS cli_net_sam_logon(struct cli_state *cli, NET_ID_INFO_CTR *ctr, 
440                          NET_USER_INFO_3 *user_info3)
441 {
442         uint16 validation_level=3;
443         NTSTATUS result;
444
445         result = cli_net_sam_logon_internal(cli, ctr, user_info3, 
446                                             validation_level);
447
448         if (NT_STATUS_IS_OK(result)) {
449                 DEBUG(10,("cli_net_sam_logon: Success \n"));
450         } else if (NT_STATUS_V(result) == NT_STATUS_V(NT_STATUS_INVALID_INFO_CLASS)) {
451                 DEBUG(10,("cli_net_sam_logon: STATUS INVALID INFO CLASS \n"));
452
453                 validation_level=2;
454
455                 /*
456                  * Since this is the second time we call this function, don't care
457                  * for the error. If its error, return False. 
458                  */
459
460                 result = cli_net_sam_logon_internal(cli, ctr, user_info3,
461                                                     validation_level);
462         }
463
464         return result;
465 }
466
467 /***************************************************************************
468 LSA SAM Logoff.
469
470 This currently doesnt work correctly as the domain controller 
471 returns NT_STATUS_INVALID_INFO_CLASS - we obviously need to
472 send a different info level. Right now though, I'm not sure
473 what that needs to be (I need to see one on the wire before
474 I can be sure). JRA.
475 ****************************************************************************/
476 BOOL cli_net_sam_logoff(struct cli_state *cli, NET_ID_INFO_CTR *ctr)
477 {
478   DOM_CRED new_clnt_cred;
479   DOM_CRED dummy_rtn_creds;
480   prs_struct rbuf;
481   prs_struct buf; 
482   NET_Q_SAM_LOGOFF q_s;
483   BOOL ok = False;
484
485   gen_next_creds( cli, &new_clnt_cred);
486
487   prs_init(&buf , 1024, cli->mem_ctx, MARSHALL);
488   prs_init(&rbuf, 0,    cli->mem_ctx, UNMARSHALL);
489
490   /* create and send a MSRPC command with api NET_SAMLOGOFF */
491
492   DEBUG(4,("cli_net_sam_logoff: srv:%s mc:%s clnt %s %x ll: %d\n",
493             cli->srv_name_slash, global_myname,
494             credstr(new_clnt_cred.challenge.data), new_clnt_cred.timestamp.time,
495             ctr->switch_value));
496
497   memset(&dummy_rtn_creds, '\0', sizeof(dummy_rtn_creds));
498
499   init_sam_info(&q_s.sam_id, cli->srv_name_slash, 
500                 global_myname, &new_clnt_cred, &dummy_rtn_creds, 
501                 ctr->switch_value, ctr);
502
503   /* turn parameters into data stream */
504   if(!net_io_q_sam_logoff("", &q_s,  &buf, 0)) {
505     DEBUG(0,("cli_net_sam_logoff: Error : failed to marshall NET_Q_SAM_LOGOFF struct.\n"));
506     prs_mem_free(&buf);
507     prs_mem_free(&rbuf);
508     return False;
509   }
510
511   /* send the data on \PIPE\ */
512   if (rpc_api_pipe_req(cli, NET_SAMLOGOFF, &buf, &rbuf))
513   {
514     NET_R_SAM_LOGOFF r_s;
515
516     ok = net_io_r_sam_logoff("", &r_s, &rbuf, 0);
517                 
518     if (ok && !NT_STATUS_IS_OK(r_s.status))
519     {
520       /* report error code */
521       DEBUG(0,("cli_net_sam_logoff: %s\n", get_nt_error_msg(r_s.status)));
522       ok = False;
523     }
524
525     /* Update the credentials. */
526     if (ok && !clnt_deal_with_creds(cli->sess_key, &(cli->clnt_cred), &(r_s.srv_creds)))
527     {
528       /*
529        * Server replied with bad credential. Fail.
530        */
531       DEBUG(0,("cli_net_sam_logoff: server %s replied with bad credential (bad machine \
532 password ?).\n", cli->desthost ));
533       ok = False;
534     }
535   }
536
537   prs_mem_free(&buf);
538   prs_mem_free(&rbuf);
539
540   return ok;
541 }