Removed global_myworkgroup, global_myname, global_myscope. Added liberal
[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         }
145
146  done:
147         prs_mem_free(&qbuf);
148         prs_mem_free(&rbuf);
149         
150         return result;
151 }
152
153 /****************************************************************************
154 LSA Authenticate 3
155
156 Send the client credential, receive back a server credential.
157 Ensure that the server credential returned matches the session key 
158 encrypt of the server challenge originally received. JRA.
159 ****************************************************************************/
160
161 NTSTATUS cli_net_auth3(struct cli_state *cli, 
162                        uint16 sec_chan, 
163                        uint32 *neg_flags, DOM_CHAL *srv_chal)
164 {
165         prs_struct qbuf, rbuf;
166         NET_Q_AUTH_3 q;
167         NET_R_AUTH_3 r;
168         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
169
170         prs_init(&qbuf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
171         prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
172
173         /* create and send a MSRPC command with api NET_AUTH2 */
174
175         DEBUG(4,("cli_net_auth3: srv:%s acct:%s sc:%x mc: %s chal %s neg: %x\n",
176                  cli->srv_name_slash, cli->mach_acct, sec_chan, global_myname(),
177                  credstr(cli->clnt_cred.challenge.data), *neg_flags));
178
179         /* store the parameters */
180         init_q_auth_3(&q, cli->srv_name_slash, cli->mach_acct, 
181                       sec_chan, global_myname(), &cli->clnt_cred.challenge, 
182                       *neg_flags);
183
184         /* turn parameters into data stream */
185
186         if (!net_io_q_auth_3("", &q,  &qbuf, 0) ||
187             !rpc_api_pipe_req(cli, NET_AUTH3, &qbuf, &rbuf)) {
188                 goto done;
189         }
190         
191         /* Unmarshall response */
192         
193         if (!net_io_r_auth_3("", &r, &rbuf, 0)) {
194                 goto done;
195         }
196
197         result = r.status;
198         *neg_flags = r.srv_flgs.neg_flags;
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         }
221
222  done:
223         prs_mem_free(&qbuf);
224         prs_mem_free(&rbuf);
225         
226         return result;
227 }
228
229 /* Return the secure channel type depending on the server role. */
230
231 uint16 get_sec_chan(void)
232 {
233         uint16 sec_chan = SEC_CHAN_WKSTA;
234
235         switch (lp_server_role()) {
236         case ROLE_DOMAIN_PDC:
237                 sec_chan = SEC_CHAN_DOMAIN;
238                 break;
239         case ROLE_DOMAIN_BDC:
240                 sec_chan = SEC_CHAN_BDC;
241                 break;
242         }
243
244         return sec_chan;
245 }
246
247 /* Initialize domain session credentials */
248
249 NTSTATUS cli_nt_setup_creds(struct cli_state *cli, 
250                             uint16 sec_chan,
251                             const unsigned char mach_pwd[16], uint32 *neg_flags, int level)
252 {
253         DOM_CHAL clnt_chal;
254         DOM_CHAL srv_chal;
255         UTIME zerotime;
256         NTSTATUS result;
257
258         /******************* Request Challenge ********************/
259
260         generate_random_buffer(clnt_chal.data, 8, False);
261         
262         /* send a client challenge; receive a server challenge */
263         result = cli_net_req_chal(cli, &clnt_chal, &srv_chal);
264
265         if (!NT_STATUS_IS_OK(result)) {
266                 DEBUG(0,("cli_nt_setup_creds: request challenge failed\n"));
267                 return result;
268         }
269         
270         /**************** Long-term Session key **************/
271
272         /* calculate the session key */
273         cred_session_key(&clnt_chal, &srv_chal, mach_pwd, 
274                          cli->sess_key);
275         memset((char *)cli->sess_key+8, '\0', 8);
276
277         /******************* Authenticate 2/3 ********************/
278
279         /* calculate auth-2/3 credentials */
280         zerotime.time = 0;
281         cred_create(cli->sess_key, &clnt_chal, zerotime, &cli->clnt_cred.challenge);
282
283         /*  
284          * Send client auth-2/3 challenge.
285          * Receive an auth-2/3 challenge response and check it.
286          */
287         switch (level) {
288                 case 2:
289                         result = cli_net_auth2(cli, sec_chan, *neg_flags, &srv_chal);
290                         break;
291                 case 3:
292                         result = cli_net_auth3(cli, sec_chan, neg_flags, &srv_chal);
293                         break;
294                 default:
295                         DEBUG(1,("cli_nt_setup_creds: unsupported auth level: %d\n", level));
296                         break;
297         }
298
299         if (!NT_STATUS_IS_OK(result))
300                 DEBUG(1,("cli_nt_setup_creds: auth%d challenge failed %s\n", level, nt_errstr(result)));
301
302         return result;
303 }
304
305 /* Logon Control 2 */
306
307 NTSTATUS cli_netlogon_logon_ctrl2(struct cli_state *cli, TALLOC_CTX *mem_ctx,
308                                   uint32 query_level)
309 {
310         prs_struct qbuf, rbuf;
311         NET_Q_LOGON_CTRL2 q;
312         NET_R_LOGON_CTRL2 r;
313         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
314
315         ZERO_STRUCT(q);
316         ZERO_STRUCT(r);
317
318         /* Initialise parse structures */
319
320         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
321         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
322
323         /* Initialise input parameters */
324
325         init_net_q_logon_ctrl2(&q, cli->srv_name_slash, query_level);
326
327         /* Marshall data and send request */
328
329         if (!net_io_q_logon_ctrl2("", &q, &qbuf, 0) ||
330             !rpc_api_pipe_req(cli, NET_LOGON_CTRL2, &qbuf, &rbuf)) {
331                 result = NT_STATUS_UNSUCCESSFUL;
332                 goto done;
333         }
334
335         /* Unmarshall response */
336
337         if (!net_io_r_logon_ctrl2("", &r, &rbuf, 0)) {
338                 result = NT_STATUS_UNSUCCESSFUL;
339                 goto done;
340         }
341
342         result = r.status;
343
344  done:
345         prs_mem_free(&qbuf);
346         prs_mem_free(&rbuf);
347
348         return result;
349 }
350
351 /****************************************************************************
352 Generate the next creds to use.  Yuck - this is a cut&paste from another
353 file.  They should be combined at some stage.  )-:
354 ****************************************************************************/
355
356 static void gen_next_creds( struct cli_state *cli, DOM_CRED *new_clnt_cred)
357 {
358         /*
359          * Create the new client credentials.
360          */
361         
362         cli->clnt_cred.timestamp.time = time(NULL);
363         
364         memcpy(new_clnt_cred, &cli->clnt_cred, sizeof(*new_clnt_cred));
365
366         /* Calculate the new credentials. */
367         cred_create(cli->sess_key, &(cli->clnt_cred.challenge),
368                     new_clnt_cred->timestamp, &(new_clnt_cred->challenge));
369 }
370
371 /* Sam synchronisation */
372
373 NTSTATUS cli_netlogon_sam_sync(struct cli_state *cli, TALLOC_CTX *mem_ctx, DOM_CRED *ret_creds,
374                                uint32 database_id, uint32 next_rid, uint32 *num_deltas,
375                                SAM_DELTA_HDR **hdr_deltas, 
376                                SAM_DELTA_CTR **deltas)
377 {
378         prs_struct qbuf, rbuf;
379         NET_Q_SAM_SYNC q;
380         NET_R_SAM_SYNC r;
381         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
382         DOM_CRED clnt_creds;
383
384         ZERO_STRUCT(q);
385         ZERO_STRUCT(r);
386
387         /* Initialise parse structures */
388
389         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
390         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
391
392         /* Initialise input parameters */
393
394         gen_next_creds(cli, &clnt_creds);
395
396         init_net_q_sam_sync(&q, cli->srv_name_slash, cli->clnt_name_slash + 2,
397                             &clnt_creds, ret_creds, database_id, next_rid);
398
399         /* Marshall data and send request */
400
401         if (!net_io_q_sam_sync("", &q, &qbuf, 0) ||
402             !rpc_api_pipe_req(cli, NET_SAM_SYNC, &qbuf, &rbuf)) {
403                 result = NT_STATUS_UNSUCCESSFUL;
404                 goto done;
405         }
406
407         /* Unmarshall response */
408
409         if (!net_io_r_sam_sync("", cli->sess_key, &r, &rbuf, 0)) {
410                 result = NT_STATUS_UNSUCCESSFUL;
411                 goto done;
412         }
413
414         /* Return results */
415
416         result = r.status;
417         *num_deltas = r.num_deltas2;
418         *hdr_deltas = r.hdr_deltas;
419         *deltas = r.deltas;
420
421         memcpy(ret_creds, &r.srv_creds, sizeof(*ret_creds));
422
423  done:
424         prs_mem_free(&qbuf);
425         prs_mem_free(&rbuf);
426
427         return result;
428 }
429
430 /* Sam synchronisation */
431
432 NTSTATUS cli_netlogon_sam_deltas(struct cli_state *cli, TALLOC_CTX *mem_ctx,
433                                  uint32 database_id, UINT64_S seqnum,
434                                  uint32 *num_deltas, 
435                                  SAM_DELTA_HDR **hdr_deltas, 
436                                  SAM_DELTA_CTR **deltas)
437 {
438         prs_struct qbuf, rbuf;
439         NET_Q_SAM_DELTAS q;
440         NET_R_SAM_DELTAS r;
441         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
442         DOM_CRED clnt_creds;
443
444         ZERO_STRUCT(q);
445         ZERO_STRUCT(r);
446
447         /* Initialise parse structures */
448
449         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
450         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
451
452         /* Initialise input parameters */
453
454         gen_next_creds(cli, &clnt_creds);
455
456         init_net_q_sam_deltas(&q, cli->srv_name_slash, 
457                               cli->clnt_name_slash + 2, &clnt_creds, 
458                               database_id, seqnum);
459
460         /* Marshall data and send request */
461
462         if (!net_io_q_sam_deltas("", &q, &qbuf, 0) ||
463             !rpc_api_pipe_req(cli, NET_SAM_DELTAS, &qbuf, &rbuf)) {
464                 result = NT_STATUS_UNSUCCESSFUL;
465                 goto done;
466         }
467
468         /* Unmarshall response */
469
470         if (!net_io_r_sam_deltas("", cli->sess_key, &r, &rbuf, 0)) {
471                 result = NT_STATUS_UNSUCCESSFUL;
472                 goto done;
473         }
474
475         /* Return results */
476
477         result = r.status;
478         *num_deltas = r.num_deltas2;
479         *hdr_deltas = r.hdr_deltas;
480         *deltas = r.deltas;
481
482  done:
483         prs_mem_free(&qbuf);
484         prs_mem_free(&rbuf);
485
486         return result;
487 }
488
489 /* Logon domain user */
490
491 NTSTATUS cli_netlogon_sam_logon(struct cli_state *cli, TALLOC_CTX *mem_ctx,
492                                 char *username, char *password,
493                                 int logon_type)
494 {
495         prs_struct qbuf, rbuf;
496         NET_Q_SAM_LOGON q;
497         NET_R_SAM_LOGON r;
498         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
499         DOM_CRED clnt_creds, dummy_rtn_creds;
500         NET_ID_INFO_CTR ctr;
501         NET_USER_INFO_3 user;
502         int validation_level = 3;
503
504         ZERO_STRUCT(q);
505         ZERO_STRUCT(r);
506
507         /* Initialise parse structures */
508
509         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
510         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
511
512         /* Initialise input parameters */
513
514         gen_next_creds(cli, &clnt_creds);
515
516         q.validation_level = validation_level;
517
518         memset(&dummy_rtn_creds, '\0', sizeof(dummy_rtn_creds));
519         dummy_rtn_creds.timestamp.time = time(NULL);
520
521         ctr.switch_value = logon_type;
522
523         switch (logon_type) {
524         case INTERACTIVE_LOGON_TYPE: {
525                 unsigned char lm_owf_user_pwd[16], nt_owf_user_pwd[16];
526
527                 nt_lm_owf_gen(password, nt_owf_user_pwd, lm_owf_user_pwd);
528
529                 init_id_info1(&ctr.auth.id1, lp_workgroup(), 
530                               0, /* param_ctrl */
531                               0xdead, 0xbeef, /* LUID? */
532                               username, cli->clnt_name_slash,
533                               cli->sess_key, lm_owf_user_pwd,
534                               nt_owf_user_pwd);
535
536                 break;
537         }
538         case NET_LOGON_TYPE: {
539                 uint8 chal[8];
540                 unsigned char local_lm_response[24];
541                 unsigned char local_nt_response[24];
542
543                 generate_random_buffer(chal, 8, False);
544
545                 SMBencrypt(password, chal, local_lm_response);
546                 SMBNTencrypt(password, chal, local_nt_response);
547
548                 init_id_info2(&ctr.auth.id2, lp_workgroup(), 
549                               0, /* param_ctrl */
550                               0xdead, 0xbeef, /* LUID? */
551                               username, cli->clnt_name_slash, chal,
552                               local_lm_response, 24, local_nt_response, 24);
553                 break;
554         }
555         default:
556                 DEBUG(0, ("switch value %d not supported\n", 
557                           ctr.switch_value));
558                 goto done;
559         }
560
561         init_sam_info(&q.sam_id, cli->srv_name_slash, global_myname(),
562                       &clnt_creds, &dummy_rtn_creds, logon_type,
563                       &ctr);
564
565         /* Marshall data and send request */
566
567         if (!net_io_q_sam_logon("", &q, &qbuf, 0) ||
568             !rpc_api_pipe_req(cli, NET_SAMLOGON, &qbuf, &rbuf)) {
569                 goto done;
570         }
571
572         /* Unmarshall response */
573
574         r.user = &user;
575
576         if (!net_io_r_sam_logon("", &r, &rbuf, 0)) {
577                 goto done;
578         }
579
580         /* Return results */
581
582         result = r.status;
583
584  done:
585         prs_mem_free(&qbuf);
586         prs_mem_free(&rbuf);
587
588         return result;
589 }
590
591
592 /** 
593  * Logon domain user with an 'network' SAM logon 
594  *
595  * @param info3 Pointer to a NET_USER_INFO_3 already allocated by the caller.
596  **/
597
598 NTSTATUS cli_netlogon_sam_network_logon(struct cli_state *cli, TALLOC_CTX *mem_ctx,
599                                         const char *username, const char *domain, const char *workstation, 
600                                         const uint8 chal[8],
601                                         DATA_BLOB lm_response, DATA_BLOB nt_response,
602                                         NET_USER_INFO_3 *info3)
603
604 {
605         prs_struct qbuf, rbuf;
606         NET_Q_SAM_LOGON q;
607         NET_R_SAM_LOGON r;
608         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
609         DOM_CRED clnt_creds, dummy_rtn_creds;
610         NET_ID_INFO_CTR ctr;
611         int validation_level = 3;
612         char *workstation_name_slash;
613         
614         ZERO_STRUCT(q);
615         ZERO_STRUCT(r);
616
617         workstation_name_slash = talloc_asprintf(mem_ctx, "\\\\%s", workstation);
618         
619         if (!workstation_name_slash) {
620                 DEBUG(0, ("talloc_asprintf failed!\n"));
621                 return NT_STATUS_NO_MEMORY;
622         }
623
624         /* Initialise parse structures */
625
626         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
627         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
628
629         /* Initialise input parameters */
630
631         gen_next_creds(cli, &clnt_creds);
632
633         q.validation_level = validation_level;
634
635         memset(&dummy_rtn_creds, '\0', sizeof(dummy_rtn_creds));
636         dummy_rtn_creds.timestamp.time = time(NULL);
637
638         ctr.switch_value = NET_LOGON_TYPE;
639
640         init_id_info2(&ctr.auth.id2, domain,
641                       0, /* param_ctrl */
642                       0xdead, 0xbeef, /* LUID? */
643                       username, workstation_name_slash, (const uchar*)chal,
644                       lm_response.data, lm_response.length, nt_response.data, nt_response.length);
645  
646         init_sam_info(&q.sam_id, cli->srv_name_slash, global_myname(),
647                       &clnt_creds, &dummy_rtn_creds, NET_LOGON_TYPE,
648                       &ctr);
649
650         /* Marshall data and send request */
651
652         if (!net_io_q_sam_logon("", &q, &qbuf, 0) ||
653             !rpc_api_pipe_req(cli, NET_SAMLOGON, &qbuf, &rbuf)) {
654                 goto done;
655         }
656
657         /* Unmarshall response */
658
659         r.user = info3;
660
661         if (!net_io_r_sam_logon("", &r, &rbuf, 0)) {
662                 goto done;
663         }
664
665         /* Return results */
666
667         result = r.status;
668
669  done:
670         prs_mem_free(&qbuf);
671         prs_mem_free(&rbuf);
672
673         return result;
674 }
675
676 /***************************************************************************
677 LSA Server Password Set.
678 ****************************************************************************/
679
680 NTSTATUS cli_net_srv_pwset(struct cli_state *cli, TALLOC_CTX *mem_ctx, 
681                            const char *machine_name, uint8 hashed_mach_pwd[16])
682 {
683         prs_struct rbuf;
684         prs_struct qbuf; 
685         DOM_CRED new_clnt_cred;
686         NET_Q_SRV_PWSET q_s;
687         uint16 sec_chan_type = 2;
688         NTSTATUS nt_status;
689         char *mach_acct;
690
691         gen_next_creds( cli, &new_clnt_cred);
692         
693         prs_init(&qbuf , 1024, mem_ctx, MARSHALL);
694         prs_init(&rbuf, 0,    mem_ctx, UNMARSHALL);
695         
696         /* create and send a MSRPC command with api NET_SRV_PWSET */
697         
698         mach_acct = talloc_asprintf(mem_ctx, "%s$", machine_name);
699         
700         if (!mach_acct) {
701                 DEBUG(0,("talloc_asprintf failed!\n"));
702                 nt_status = NT_STATUS_NO_MEMORY;
703                 goto done;
704         }
705
706         DEBUG(4,("cli_net_srv_pwset: srv:%s acct:%s sc: %d mc: %s clnt %s %x\n",
707                  cli->srv_name_slash, mach_acct, sec_chan_type, machine_name,
708                  credstr(new_clnt_cred.challenge.data), new_clnt_cred.timestamp.time));
709         
710         /* store the parameters */
711         init_q_srv_pwset(&q_s, cli->srv_name_slash, cli->sess_key,
712                          mach_acct, sec_chan_type, machine_name, 
713                          &new_clnt_cred, (char *)hashed_mach_pwd);
714         
715         /* turn parameters into data stream */
716         if(!net_io_q_srv_pwset("", &q_s,  &qbuf, 0)) {
717                 DEBUG(0,("cli_net_srv_pwset: Error : failed to marshall NET_Q_SRV_PWSET struct.\n"));
718                 nt_status = NT_STATUS_UNSUCCESSFUL;
719                 goto done;
720         }
721         
722         /* send the data on \PIPE\ */
723         if (rpc_api_pipe_req(cli, NET_SRVPWSET, &qbuf, &rbuf))
724         {
725                 NET_R_SRV_PWSET r_s;
726                 
727                 if (!net_io_r_srv_pwset("", &r_s, &rbuf, 0)) {
728                         nt_status =  NT_STATUS_UNSUCCESSFUL;
729                         goto done;
730                 }
731                 
732                 nt_status = r_s.status;
733
734                 if (!NT_STATUS_IS_OK(r_s.status))
735                 {
736                         /* report error code */
737                         DEBUG(0,("cli_net_srv_pwset: %s\n", nt_errstr(nt_status)));
738                         goto done;
739                 }
740
741                 /* Update the credentials. */
742                 if (!clnt_deal_with_creds(cli->sess_key, &(cli->clnt_cred), &(r_s.srv_cred)))
743                 {
744                         /*
745                          * Server replied with bad credential. Fail.
746                          */
747                         DEBUG(0,("cli_net_srv_pwset: server %s replied with bad credential (bad machine \
748 password ?).\n", cli->desthost ));
749                         nt_status = NT_STATUS_UNSUCCESSFUL;
750                 }
751         }
752
753  done:
754         prs_mem_free(&qbuf);
755         prs_mem_free(&rbuf);
756         
757         return nt_status;
758 }
759