This commit was manufactured by cvs2svn to create branch 'SAMBA_3_0'.(This used to...
[samba.git] / source3 / rpc_client / cli_netlogon.c
1 /* 
2    Unix SMB/CIFS implementation.
3    NT Domain Authentication SMB / MSRPC client
4    Copyright (C) Andrew Tridgell 1992-2000
5    Copyright (C) Luke Kenneth Casson Leighton 1996-2000
6    Copyright (C) Tim Potter 2001
7    Copyright (C) Paul Ashton                       1997.
8    Copyright (C) Jeremy Allison                    1998.
9    Copyright (C) Andrew Bartlett                   2001.
10    
11    This program is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 2 of the License, or
14    (at your option) any later version.
15    
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20    
21    You should have received a copy of the GNU General Public License
22    along with this program; if not, write to the Free Software
23    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 */
25
26 #include "includes.h"
27
28 /* LSA Request Challenge. Sends our challenge to server, then gets
29    server response. These are used to generate the credentials. */
30
31 NTSTATUS cli_net_req_chal(struct cli_state *cli, DOM_CHAL *clnt_chal, 
32                           DOM_CHAL *srv_chal)
33 {
34         prs_struct qbuf, rbuf;
35         NET_Q_REQ_CHAL q;
36         NET_R_REQ_CHAL r;
37         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
38
39         prs_init(&qbuf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
40         prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
41         
42         /* create and send a MSRPC command with api NET_REQCHAL */
43
44         DEBUG(4,("cli_net_req_chal: LSA Request Challenge from %s to %s: %s\n",
45                  global_myname(), cli->desthost, credstr(clnt_chal->data)));
46         
47         /* store the parameters */
48         init_q_req_chal(&q, cli->srv_name_slash, global_myname(), clnt_chal);
49         
50         /* Marshall data and send request */
51
52         if (!net_io_q_req_chal("", &q,  &qbuf, 0) ||
53             !rpc_api_pipe_req(cli, NET_REQCHAL, &qbuf, &rbuf)) {
54                 goto done;
55         }
56
57         /* Unmarhall response */
58
59         if (!net_io_r_req_chal("", &r, &rbuf, 0)) {
60                 goto done;
61         }
62
63         result = r.status;
64
65         /* Return result */
66
67         if (NT_STATUS_IS_OK(result)) {
68                 memcpy(srv_chal, r.srv_chal.data, sizeof(srv_chal->data));
69         }
70         
71  done:
72         prs_mem_free(&qbuf);
73         prs_mem_free(&rbuf);
74         
75         return result;
76 }
77
78 /****************************************************************************
79 LSA Authenticate 2
80
81 Send the client credential, receive back a server credential.
82 Ensure that the server credential returned matches the session key 
83 encrypt of the server challenge originally received. JRA.
84 ****************************************************************************/
85
86 NTSTATUS cli_net_auth2(struct cli_state *cli, 
87                        uint16 sec_chan, 
88                        uint32 *neg_flags, DOM_CHAL *srv_chal)
89 {
90         prs_struct qbuf, rbuf;
91         NET_Q_AUTH_2 q;
92         NET_R_AUTH_2 r;
93         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
94
95         prs_init(&qbuf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
96         prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
97
98         /* create and send a MSRPC command with api NET_AUTH2 */
99
100         DEBUG(4,("cli_net_auth2: srv:%s acct:%s sc:%x mc: %s chal %s neg: %x\n",
101                  cli->srv_name_slash, cli->mach_acct, sec_chan, global_myname(),
102                  credstr(cli->clnt_cred.challenge.data), *neg_flags));
103
104         /* store the parameters */
105         init_q_auth_2(&q, cli->srv_name_slash, cli->mach_acct, 
106                       sec_chan, global_myname(), &cli->clnt_cred.challenge, 
107                       *neg_flags);
108
109         /* turn parameters into data stream */
110
111         if (!net_io_q_auth_2("", &q,  &qbuf, 0) ||
112             !rpc_api_pipe_req(cli, NET_AUTH2, &qbuf, &rbuf)) {
113                 goto done;
114         }
115         
116         /* Unmarshall response */
117         
118         if (!net_io_r_auth_2("", &r, &rbuf, 0)) {
119                 goto done;
120         }
121
122         result = r.status;
123
124         if (NT_STATUS_IS_OK(result)) {
125                 UTIME zerotime;
126                 
127                 /*
128                  * Check the returned value using the initial
129                  * server received challenge.
130                  */
131
132                 zerotime.time = 0;
133                 if (cred_assert( &r.srv_chal, cli->sess_key, srv_chal, 
134                                  zerotime) == 0) {
135
136                         /*
137                          * Server replied with bad credential. Fail.
138                          */
139                         DEBUG(0,("cli_net_auth2: server %s replied with bad credential (bad machine \
140 password ?).\n", cli->desthost ));
141                         result = NT_STATUS_ACCESS_DENIED;
142                         goto done;
143                 }
144                 *neg_flags = r.srv_flgs.neg_flags;
145         }
146
147  done:
148         prs_mem_free(&qbuf);
149         prs_mem_free(&rbuf);
150         
151         return result;
152 }
153
154 /****************************************************************************
155 LSA Authenticate 3
156
157 Send the client credential, receive back a server credential.
158 Ensure that the server credential returned matches the session key 
159 encrypt of the server challenge originally received. JRA.
160 ****************************************************************************/
161
162 NTSTATUS cli_net_auth3(struct cli_state *cli, 
163                        uint16 sec_chan, 
164                        uint32 *neg_flags, DOM_CHAL *srv_chal)
165 {
166         prs_struct qbuf, rbuf;
167         NET_Q_AUTH_3 q;
168         NET_R_AUTH_3 r;
169         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
170
171         prs_init(&qbuf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
172         prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
173
174         /* create and send a MSRPC command with api NET_AUTH2 */
175
176         DEBUG(4,("cli_net_auth3: srv:%s acct:%s sc:%x mc: %s chal %s neg: %x\n",
177                  cli->srv_name_slash, cli->mach_acct, sec_chan, global_myname(),
178                  credstr(cli->clnt_cred.challenge.data), *neg_flags));
179
180         /* store the parameters */
181         init_q_auth_3(&q, cli->srv_name_slash, cli->mach_acct, 
182                       sec_chan, global_myname(), &cli->clnt_cred.challenge, 
183                       *neg_flags);
184
185         /* turn parameters into data stream */
186
187         if (!net_io_q_auth_3("", &q,  &qbuf, 0) ||
188             !rpc_api_pipe_req(cli, NET_AUTH3, &qbuf, &rbuf)) {
189                 goto done;
190         }
191         
192         /* Unmarshall response */
193         
194         if (!net_io_r_auth_3("", &r, &rbuf, 0)) {
195                 goto done;
196         }
197
198         result = r.status;
199
200         if (NT_STATUS_IS_OK(result)) {
201                 UTIME zerotime;
202                 
203                 /*
204                  * Check the returned value using the initial
205                  * server received challenge.
206                  */
207
208                 zerotime.time = 0;
209                 if (cred_assert( &r.srv_chal, cli->sess_key, srv_chal, 
210                                  zerotime) == 0) {
211
212                         /*
213                          * Server replied with bad credential. Fail.
214                          */
215                         DEBUG(0,("cli_net_auth3: server %s replied with bad credential (bad machine \
216 password ?).\n", cli->desthost ));
217                         result = NT_STATUS_ACCESS_DENIED;
218                         goto done;
219                 }
220                 *neg_flags = r.srv_flgs.neg_flags;
221         }
222
223  done:
224         prs_mem_free(&qbuf);
225         prs_mem_free(&rbuf);
226         
227         return result;
228 }
229
230 /* Return the secure channel type depending on the server role. */
231
232 uint16 get_sec_chan(void)
233 {
234         uint16 sec_chan = SEC_CHAN_WKSTA;
235
236         switch (lp_server_role()) {
237         case ROLE_DOMAIN_PDC:
238                 sec_chan = SEC_CHAN_DOMAIN;
239                 break;
240         case ROLE_DOMAIN_BDC:
241                 sec_chan = SEC_CHAN_BDC;
242                 break;
243         }
244
245         return sec_chan;
246 }
247
248 /* Initialize domain session credentials */
249
250 NTSTATUS cli_nt_setup_creds(struct cli_state *cli, 
251                             uint16 sec_chan,
252                             const unsigned char mach_pwd[16], uint32 *neg_flags, int level)
253 {
254         DOM_CHAL clnt_chal;
255         DOM_CHAL srv_chal;
256         UTIME zerotime;
257         NTSTATUS result;
258
259         /******************* Request Challenge ********************/
260
261         generate_random_buffer(clnt_chal.data, 8, False);
262         
263         /* send a client challenge; receive a server challenge */
264         result = cli_net_req_chal(cli, &clnt_chal, &srv_chal);
265
266         if (!NT_STATUS_IS_OK(result)) {
267                 DEBUG(0,("cli_nt_setup_creds: request challenge failed\n"));
268                 return result;
269         }
270         
271         /**************** Long-term Session key **************/
272
273         /* calculate the session key */
274         cred_session_key(&clnt_chal, &srv_chal, mach_pwd, 
275                          cli->sess_key);
276         memset((char *)cli->sess_key+8, '\0', 8);
277
278         /******************* Authenticate 2/3 ********************/
279
280         /* calculate auth-2/3 credentials */
281         zerotime.time = 0;
282         cred_create(cli->sess_key, &clnt_chal, zerotime, &cli->clnt_cred.challenge);
283
284         /*  
285          * Send client auth-2/3 challenge.
286          * Receive an auth-2/3 challenge response and check it.
287          */
288         switch (level) {
289                 case 2:
290                         result = cli_net_auth2(cli, sec_chan, neg_flags, &srv_chal);
291                         break;
292                 case 3:
293                         result = cli_net_auth3(cli, sec_chan, neg_flags, &srv_chal);
294                         break;
295                 default:
296                         DEBUG(1,("cli_nt_setup_creds: unsupported auth level: %d\n", level));
297                         break;
298         }
299
300         if (!NT_STATUS_IS_OK(result))
301                 DEBUG(1,("cli_nt_setup_creds: auth%d challenge failed %s\n", level, nt_errstr(result)));
302
303         return result;
304 }
305
306 /* Logon Control 2 */
307
308 NTSTATUS cli_netlogon_logon_ctrl2(struct cli_state *cli, TALLOC_CTX *mem_ctx,
309                                   uint32 query_level)
310 {
311         prs_struct qbuf, rbuf;
312         NET_Q_LOGON_CTRL2 q;
313         NET_R_LOGON_CTRL2 r;
314         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
315
316         ZERO_STRUCT(q);
317         ZERO_STRUCT(r);
318
319         /* Initialise parse structures */
320
321         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
322         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
323
324         /* Initialise input parameters */
325
326         init_net_q_logon_ctrl2(&q, cli->srv_name_slash, query_level);
327
328         /* Marshall data and send request */
329
330         if (!net_io_q_logon_ctrl2("", &q, &qbuf, 0) ||
331             !rpc_api_pipe_req(cli, NET_LOGON_CTRL2, &qbuf, &rbuf)) {
332                 result = NT_STATUS_UNSUCCESSFUL;
333                 goto done;
334         }
335
336         /* Unmarshall response */
337
338         if (!net_io_r_logon_ctrl2("", &r, &rbuf, 0)) {
339                 result = NT_STATUS_UNSUCCESSFUL;
340                 goto done;
341         }
342
343         result = r.status;
344
345  done:
346         prs_mem_free(&qbuf);
347         prs_mem_free(&rbuf);
348
349         return result;
350 }
351
352 /****************************************************************************
353 Generate the next creds to use.  Yuck - this is a cut&paste from another
354 file.  They should be combined at some stage.  )-:
355 ****************************************************************************/
356
357 static void gen_next_creds( struct cli_state *cli, DOM_CRED *new_clnt_cred)
358 {
359         /*
360          * Create the new client credentials.
361          */
362         
363         cli->clnt_cred.timestamp.time = time(NULL);
364         
365         memcpy(new_clnt_cred, &cli->clnt_cred, sizeof(*new_clnt_cred));
366
367         /* Calculate the new credentials. */
368         cred_create(cli->sess_key, &(cli->clnt_cred.challenge),
369                     new_clnt_cred->timestamp, &(new_clnt_cred->challenge));
370 }
371
372 /* Sam synchronisation */
373
374 NTSTATUS cli_netlogon_sam_sync(struct cli_state *cli, TALLOC_CTX *mem_ctx, DOM_CRED *ret_creds,
375                                uint32 database_id, uint32 next_rid, uint32 *num_deltas,
376                                SAM_DELTA_HDR **hdr_deltas, 
377                                SAM_DELTA_CTR **deltas)
378 {
379         prs_struct qbuf, rbuf;
380         NET_Q_SAM_SYNC q;
381         NET_R_SAM_SYNC r;
382         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
383         DOM_CRED clnt_creds;
384
385         ZERO_STRUCT(q);
386         ZERO_STRUCT(r);
387
388         /* Initialise parse structures */
389
390         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
391         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
392
393         /* Initialise input parameters */
394
395         gen_next_creds(cli, &clnt_creds);
396
397         init_net_q_sam_sync(&q, cli->srv_name_slash, cli->clnt_name_slash + 2,
398                             &clnt_creds, ret_creds, database_id, next_rid);
399
400         /* Marshall data and send request */
401
402         if (!net_io_q_sam_sync("", &q, &qbuf, 0) ||
403             !rpc_api_pipe_req(cli, NET_SAM_SYNC, &qbuf, &rbuf)) {
404                 result = NT_STATUS_UNSUCCESSFUL;
405                 goto done;
406         }
407
408         /* Unmarshall response */
409
410         if (!net_io_r_sam_sync("", cli->sess_key, &r, &rbuf, 0)) {
411                 result = NT_STATUS_UNSUCCESSFUL;
412                 goto done;
413         }
414
415         /* Return results */
416
417         result = r.status;
418         *num_deltas = r.num_deltas2;
419         *hdr_deltas = r.hdr_deltas;
420         *deltas = r.deltas;
421
422         memcpy(ret_creds, &r.srv_creds, sizeof(*ret_creds));
423
424  done:
425         prs_mem_free(&qbuf);
426         prs_mem_free(&rbuf);
427
428         return result;
429 }
430
431 /* Sam synchronisation */
432
433 NTSTATUS cli_netlogon_sam_deltas(struct cli_state *cli, TALLOC_CTX *mem_ctx,
434                                  uint32 database_id, UINT64_S seqnum,
435                                  uint32 *num_deltas, 
436                                  SAM_DELTA_HDR **hdr_deltas, 
437                                  SAM_DELTA_CTR **deltas)
438 {
439         prs_struct qbuf, rbuf;
440         NET_Q_SAM_DELTAS q;
441         NET_R_SAM_DELTAS r;
442         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
443         DOM_CRED clnt_creds;
444
445         ZERO_STRUCT(q);
446         ZERO_STRUCT(r);
447
448         /* Initialise parse structures */
449
450         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
451         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
452
453         /* Initialise input parameters */
454
455         gen_next_creds(cli, &clnt_creds);
456
457         init_net_q_sam_deltas(&q, cli->srv_name_slash, 
458                               cli->clnt_name_slash + 2, &clnt_creds, 
459                               database_id, seqnum);
460
461         /* Marshall data and send request */
462
463         if (!net_io_q_sam_deltas("", &q, &qbuf, 0) ||
464             !rpc_api_pipe_req(cli, NET_SAM_DELTAS, &qbuf, &rbuf)) {
465                 result = NT_STATUS_UNSUCCESSFUL;
466                 goto done;
467         }
468
469         /* Unmarshall response */
470
471         if (!net_io_r_sam_deltas("", cli->sess_key, &r, &rbuf, 0)) {
472                 result = NT_STATUS_UNSUCCESSFUL;
473                 goto done;
474         }
475
476         /* Return results */
477
478         result = r.status;
479         *num_deltas = r.num_deltas2;
480         *hdr_deltas = r.hdr_deltas;
481         *deltas = r.deltas;
482
483  done:
484         prs_mem_free(&qbuf);
485         prs_mem_free(&rbuf);
486
487         return result;
488 }
489
490 /* Logon domain user */
491
492 NTSTATUS cli_netlogon_sam_logon(struct cli_state *cli, TALLOC_CTX *mem_ctx,
493                                 const char *username, const char *password,
494                                 int logon_type)
495 {
496         prs_struct qbuf, rbuf;
497         NET_Q_SAM_LOGON q;
498         NET_R_SAM_LOGON r;
499         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
500         DOM_CRED clnt_creds, dummy_rtn_creds;
501         NET_ID_INFO_CTR ctr;
502         NET_USER_INFO_3 user;
503         int validation_level = 3;
504
505         ZERO_STRUCT(q);
506         ZERO_STRUCT(r);
507
508         /* Initialise parse structures */
509
510         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
511         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
512
513         /* Initialise input parameters */
514
515         gen_next_creds(cli, &clnt_creds);
516
517         q.validation_level = validation_level;
518
519         memset(&dummy_rtn_creds, '\0', sizeof(dummy_rtn_creds));
520         dummy_rtn_creds.timestamp.time = time(NULL);
521
522         ctr.switch_value = logon_type;
523
524         switch (logon_type) {
525         case INTERACTIVE_LOGON_TYPE: {
526                 unsigned char lm_owf_user_pwd[16], nt_owf_user_pwd[16];
527
528                 nt_lm_owf_gen(password, nt_owf_user_pwd, lm_owf_user_pwd);
529
530                 init_id_info1(&ctr.auth.id1, lp_workgroup(), 
531                               0, /* param_ctrl */
532                               0xdead, 0xbeef, /* LUID? */
533                               username, cli->clnt_name_slash,
534                               cli->sess_key, lm_owf_user_pwd,
535                               nt_owf_user_pwd);
536
537                 break;
538         }
539         case NET_LOGON_TYPE: {
540                 uint8 chal[8];
541                 unsigned char local_lm_response[24];
542                 unsigned char local_nt_response[24];
543
544                 generate_random_buffer(chal, 8, False);
545
546                 SMBencrypt(password, chal, local_lm_response);
547                 SMBNTencrypt(password, chal, local_nt_response);
548
549                 init_id_info2(&ctr.auth.id2, lp_workgroup(), 
550                               0, /* param_ctrl */
551                               0xdead, 0xbeef, /* LUID? */
552                               username, cli->clnt_name_slash, chal,
553                               local_lm_response, 24, local_nt_response, 24);
554                 break;
555         }
556         default:
557                 DEBUG(0, ("switch value %d not supported\n", 
558                           ctr.switch_value));
559                 goto done;
560         }
561
562         init_sam_info(&q.sam_id, cli->srv_name_slash, global_myname(),
563                       &clnt_creds, &dummy_rtn_creds, logon_type,
564                       &ctr);
565
566         /* Marshall data and send request */
567
568         if (!net_io_q_sam_logon("", &q, &qbuf, 0) ||
569             !rpc_api_pipe_req(cli, NET_SAMLOGON, &qbuf, &rbuf)) {
570                 goto done;
571         }
572
573         /* Unmarshall response */
574
575         r.user = &user;
576
577         if (!net_io_r_sam_logon("", &r, &rbuf, 0)) {
578                 goto done;
579         }
580
581         /* Return results */
582
583         result = r.status;
584
585  done:
586         prs_mem_free(&qbuf);
587         prs_mem_free(&rbuf);
588
589         return result;
590 }
591
592
593 /** 
594  * Logon domain user with an 'network' SAM logon 
595  *
596  * @param info3 Pointer to a NET_USER_INFO_3 already allocated by the caller.
597  **/
598
599 NTSTATUS cli_netlogon_sam_network_logon(struct cli_state *cli, TALLOC_CTX *mem_ctx,
600                                         const char *username, const char *domain, const char *workstation, 
601                                         const uint8 chal[8], 
602                                         DATA_BLOB lm_response, DATA_BLOB nt_response,
603                                         NET_USER_INFO_3 *info3)
604
605 {
606         prs_struct qbuf, rbuf;
607         NET_Q_SAM_LOGON q;
608         NET_R_SAM_LOGON r;
609         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
610         DOM_CRED clnt_creds, dummy_rtn_creds;
611         NET_ID_INFO_CTR ctr;
612         int validation_level = 3;
613         char *workstation_name_slash;
614         uint8 netlogon_sess_key[16];
615         static uint8 zeros[16];
616         
617         ZERO_STRUCT(q);
618         ZERO_STRUCT(r);
619
620         workstation_name_slash = talloc_asprintf(mem_ctx, "\\\\%s", workstation);
621         
622         if (!workstation_name_slash) {
623                 DEBUG(0, ("talloc_asprintf failed!\n"));
624                 return NT_STATUS_NO_MEMORY;
625         }
626
627         /* Initialise parse structures */
628
629         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
630         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
631
632         /* Initialise input parameters */
633
634         gen_next_creds(cli, &clnt_creds);
635
636         q.validation_level = validation_level;
637
638         memset(&dummy_rtn_creds, '\0', sizeof(dummy_rtn_creds));
639         dummy_rtn_creds.timestamp.time = time(NULL);
640
641         ctr.switch_value = NET_LOGON_TYPE;
642
643         init_id_info2(&ctr.auth.id2, domain,
644                       0, /* param_ctrl */
645                       0xdead, 0xbeef, /* LUID? */
646                       username, workstation_name_slash, (const uchar*)chal,
647                       lm_response.data, lm_response.length, nt_response.data, nt_response.length);
648  
649         init_sam_info(&q.sam_id, cli->srv_name_slash, global_myname(),
650                       &clnt_creds, &dummy_rtn_creds, NET_LOGON_TYPE,
651                       &ctr);
652
653         /* Marshall data and send request */
654
655         if (!net_io_q_sam_logon("", &q, &qbuf, 0) ||
656             !rpc_api_pipe_req(cli, NET_SAMLOGON, &qbuf, &rbuf)) {
657                 goto done;
658         }
659
660         /* Unmarshall response */
661
662         r.user = info3;
663
664         if (!net_io_r_sam_logon("", &r, &rbuf, 0)) {
665                 goto done;
666         }
667
668         ZERO_STRUCT(netlogon_sess_key);
669         memcpy(netlogon_sess_key, cli->sess_key, 8);
670         
671         if (memcmp(zeros, info3->user_sess_key, 16) != 0)
672                 SamOEMhash(info3->user_sess_key, netlogon_sess_key, 16);
673                 
674         if (memcmp(zeros, info3->padding, 16) != 0)
675                 SamOEMhash(info3->padding, netlogon_sess_key, 16);
676
677         /* Return results */
678
679         result = r.status;
680
681  done:
682         prs_mem_free(&qbuf);
683         prs_mem_free(&rbuf);
684
685         return result;
686 }
687
688 /***************************************************************************
689 LSA Server Password Set.
690 ****************************************************************************/
691
692 NTSTATUS cli_net_srv_pwset(struct cli_state *cli, TALLOC_CTX *mem_ctx, 
693                            const char *machine_name, uint8 hashed_mach_pwd[16])
694 {
695         prs_struct rbuf;
696         prs_struct qbuf; 
697         DOM_CRED new_clnt_cred;
698         NET_Q_SRV_PWSET q_s;
699         uint16 sec_chan_type = 2;
700         NTSTATUS nt_status;
701         char *mach_acct;
702
703         gen_next_creds( cli, &new_clnt_cred);
704         
705         prs_init(&qbuf , 1024, mem_ctx, MARSHALL);
706         prs_init(&rbuf, 0,    mem_ctx, UNMARSHALL);
707         
708         /* create and send a MSRPC command with api NET_SRV_PWSET */
709         
710         mach_acct = talloc_asprintf(mem_ctx, "%s$", machine_name);
711         
712         if (!mach_acct) {
713                 DEBUG(0,("talloc_asprintf failed!\n"));
714                 nt_status = NT_STATUS_NO_MEMORY;
715                 goto done;
716         }
717
718         DEBUG(4,("cli_net_srv_pwset: srv:%s acct:%s sc: %d mc: %s clnt %s %x\n",
719                  cli->srv_name_slash, mach_acct, sec_chan_type, machine_name,
720                  credstr(new_clnt_cred.challenge.data), new_clnt_cred.timestamp.time));
721         
722         /* store the parameters */
723         init_q_srv_pwset(&q_s, cli->srv_name_slash, cli->sess_key,
724                          mach_acct, sec_chan_type, machine_name, 
725                          &new_clnt_cred, (char *)hashed_mach_pwd);
726         
727         /* turn parameters into data stream */
728         if(!net_io_q_srv_pwset("", &q_s,  &qbuf, 0)) {
729                 DEBUG(0,("cli_net_srv_pwset: Error : failed to marshall NET_Q_SRV_PWSET struct.\n"));
730                 nt_status = NT_STATUS_UNSUCCESSFUL;
731                 goto done;
732         }
733         
734         /* send the data on \PIPE\ */
735         if (rpc_api_pipe_req(cli, NET_SRVPWSET, &qbuf, &rbuf))
736         {
737                 NET_R_SRV_PWSET r_s;
738                 
739                 if (!net_io_r_srv_pwset("", &r_s, &rbuf, 0)) {
740                         nt_status =  NT_STATUS_UNSUCCESSFUL;
741                         goto done;
742                 }
743                 
744                 nt_status = r_s.status;
745
746                 if (!NT_STATUS_IS_OK(r_s.status))
747                 {
748                         /* report error code */
749                         DEBUG(0,("cli_net_srv_pwset: %s\n", nt_errstr(nt_status)));
750                         goto done;
751                 }
752
753                 /* Update the credentials. */
754                 if (!clnt_deal_with_creds(cli->sess_key, &(cli->clnt_cred), &(r_s.srv_cred)))
755                 {
756                         /*
757                          * Server replied with bad credential. Fail.
758                          */
759                         DEBUG(0,("cli_net_srv_pwset: server %s replied with bad credential (bad machine \
760 password ?).\n", cli->desthost ));
761                         nt_status = NT_STATUS_UNSUCCESSFUL;
762                 }
763         }
764
765  done:
766         prs_mem_free(&qbuf);
767         prs_mem_free(&rbuf);
768         
769         return nt_status;
770 }
771