trying to get HEAD building again. If you want the code
[tprouty/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 /* Initialize domain session credentials */
231
232 NTSTATUS cli_nt_setup_creds(struct cli_state *cli, 
233                             uint16 sec_chan,
234                             const unsigned char mach_pwd[16], uint32 *neg_flags, int level)
235 {
236         DOM_CHAL clnt_chal;
237         DOM_CHAL srv_chal;
238         UTIME zerotime;
239         NTSTATUS result;
240
241         /******************* Request Challenge ********************/
242
243         generate_random_buffer(clnt_chal.data, 8, False);
244         
245         /* send a client challenge; receive a server challenge */
246         result = cli_net_req_chal(cli, &clnt_chal, &srv_chal);
247
248         if (!NT_STATUS_IS_OK(result)) {
249                 DEBUG(0,("cli_nt_setup_creds: request challenge failed\n"));
250                 return result;
251         }
252         
253         /**************** Long-term Session key **************/
254
255         /* calculate the session key */
256         cred_session_key(&clnt_chal, &srv_chal, mach_pwd, 
257                          cli->sess_key);
258         memset((char *)cli->sess_key+8, '\0', 8);
259
260         /******************* Authenticate 2/3 ********************/
261
262         /* calculate auth-2/3 credentials */
263         zerotime.time = 0;
264         cred_create(cli->sess_key, &clnt_chal, zerotime, &cli->clnt_cred.challenge);
265
266         /*  
267          * Send client auth-2/3 challenge.
268          * Receive an auth-2/3 challenge response and check it.
269          */
270         switch (level) {
271                 case 2:
272                         result = cli_net_auth2(cli, sec_chan, neg_flags, &srv_chal);
273                         break;
274                 case 3:
275                         result = cli_net_auth3(cli, sec_chan, neg_flags, &srv_chal);
276                         break;
277                 default:
278                         DEBUG(1,("cli_nt_setup_creds: unsupported auth level: %d\n", level));
279                         break;
280         }
281
282         if (!NT_STATUS_IS_OK(result))
283                 DEBUG(3,("cli_nt_setup_creds: auth%d challenge failed %s\n", level, nt_errstr(result)));
284
285         return result;
286 }
287
288 /* Logon Control 2 */
289
290 NTSTATUS cli_netlogon_logon_ctrl2(struct cli_state *cli, TALLOC_CTX *mem_ctx,
291                                   uint32 query_level)
292 {
293         prs_struct qbuf, rbuf;
294         NET_Q_LOGON_CTRL2 q;
295         NET_R_LOGON_CTRL2 r;
296         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
297
298         ZERO_STRUCT(q);
299         ZERO_STRUCT(r);
300
301         /* Initialise parse structures */
302
303         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
304         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
305
306         /* Initialise input parameters */
307
308         init_net_q_logon_ctrl2(&q, cli->srv_name_slash, query_level);
309
310         /* Marshall data and send request */
311
312         if (!net_io_q_logon_ctrl2("", &q, &qbuf, 0) ||
313             !rpc_api_pipe_req(cli, NET_LOGON_CTRL2, &qbuf, &rbuf)) {
314                 result = NT_STATUS_UNSUCCESSFUL;
315                 goto done;
316         }
317
318         /* Unmarshall response */
319
320         if (!net_io_r_logon_ctrl2("", &r, &rbuf, 0)) {
321                 result = NT_STATUS_UNSUCCESSFUL;
322                 goto done;
323         }
324
325         result = r.status;
326
327  done:
328         prs_mem_free(&qbuf);
329         prs_mem_free(&rbuf);
330
331         return result;
332 }
333
334 /****************************************************************************
335 Generate the next creds to use.  Yuck - this is a cut&paste from another
336 file.  They should be combined at some stage.  )-:
337 ****************************************************************************/
338
339 static void gen_next_creds( struct cli_state *cli, DOM_CRED *new_clnt_cred)
340 {
341         /*
342          * Create the new client credentials.
343          */
344         
345         cli->clnt_cred.timestamp.time = time(NULL);
346         
347         memcpy(new_clnt_cred, &cli->clnt_cred, sizeof(*new_clnt_cred));
348
349         /* Calculate the new credentials. */
350         cred_create(cli->sess_key, &(cli->clnt_cred.challenge),
351                     new_clnt_cred->timestamp, &(new_clnt_cred->challenge));
352 }
353
354 /* Sam synchronisation */
355
356 NTSTATUS cli_netlogon_sam_sync(struct cli_state *cli, TALLOC_CTX *mem_ctx, DOM_CRED *ret_creds,
357                                uint32 database_id, uint32 next_rid, uint32 *num_deltas,
358                                SAM_DELTA_HDR **hdr_deltas, 
359                                SAM_DELTA_CTR **deltas)
360 {
361         prs_struct qbuf, rbuf;
362         NET_Q_SAM_SYNC q;
363         NET_R_SAM_SYNC r;
364         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
365         DOM_CRED clnt_creds;
366
367         ZERO_STRUCT(q);
368         ZERO_STRUCT(r);
369
370         /* Initialise parse structures */
371
372         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
373         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
374
375         /* Initialise input parameters */
376
377         gen_next_creds(cli, &clnt_creds);
378
379         init_net_q_sam_sync(&q, cli->srv_name_slash, cli->clnt_name_slash + 2,
380                             &clnt_creds, ret_creds, database_id, next_rid);
381
382         /* Marshall data and send request */
383
384         if (!net_io_q_sam_sync("", &q, &qbuf, 0) ||
385             !rpc_api_pipe_req(cli, NET_SAM_SYNC, &qbuf, &rbuf)) {
386                 result = NT_STATUS_UNSUCCESSFUL;
387                 goto done;
388         }
389
390         /* Unmarshall response */
391
392         if (!net_io_r_sam_sync("", cli->sess_key, &r, &rbuf, 0)) {
393                 result = NT_STATUS_UNSUCCESSFUL;
394                 goto done;
395         }
396
397         /* Return results */
398
399         result = r.status;
400         *num_deltas = r.num_deltas2;
401         *hdr_deltas = r.hdr_deltas;
402         *deltas = r.deltas;
403
404         memcpy(ret_creds, &r.srv_creds, sizeof(*ret_creds));
405
406  done:
407         prs_mem_free(&qbuf);
408         prs_mem_free(&rbuf);
409
410         return result;
411 }
412
413 /* Sam synchronisation */
414
415 NTSTATUS cli_netlogon_sam_deltas(struct cli_state *cli, TALLOC_CTX *mem_ctx,
416                                  uint32 database_id, UINT64_S seqnum,
417                                  uint32 *num_deltas, 
418                                  SAM_DELTA_HDR **hdr_deltas, 
419                                  SAM_DELTA_CTR **deltas)
420 {
421         prs_struct qbuf, rbuf;
422         NET_Q_SAM_DELTAS q;
423         NET_R_SAM_DELTAS r;
424         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
425         DOM_CRED clnt_creds;
426
427         ZERO_STRUCT(q);
428         ZERO_STRUCT(r);
429
430         /* Initialise parse structures */
431
432         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
433         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
434
435         /* Initialise input parameters */
436
437         gen_next_creds(cli, &clnt_creds);
438
439         init_net_q_sam_deltas(&q, cli->srv_name_slash, 
440                               cli->clnt_name_slash + 2, &clnt_creds, 
441                               database_id, seqnum);
442
443         /* Marshall data and send request */
444
445         if (!net_io_q_sam_deltas("", &q, &qbuf, 0) ||
446             !rpc_api_pipe_req(cli, NET_SAM_DELTAS, &qbuf, &rbuf)) {
447                 result = NT_STATUS_UNSUCCESSFUL;
448                 goto done;
449         }
450
451         /* Unmarshall response */
452
453         if (!net_io_r_sam_deltas("", cli->sess_key, &r, &rbuf, 0)) {
454                 result = NT_STATUS_UNSUCCESSFUL;
455                 goto done;
456         }
457
458         /* Return results */
459
460         result = r.status;
461         *num_deltas = r.num_deltas2;
462         *hdr_deltas = r.hdr_deltas;
463         *deltas = r.deltas;
464
465  done:
466         prs_mem_free(&qbuf);
467         prs_mem_free(&rbuf);
468
469         return result;
470 }
471
472 /* Logon domain user */
473
474 NTSTATUS cli_netlogon_sam_logon(struct cli_state *cli, TALLOC_CTX *mem_ctx,
475                                 DOM_CRED *ret_creds,
476                                 const char *username, const char *password,
477                                 int logon_type)
478 {
479         prs_struct qbuf, rbuf;
480         NET_Q_SAM_LOGON q;
481         NET_R_SAM_LOGON r;
482         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
483         DOM_CRED clnt_creds, dummy_rtn_creds;
484         NET_ID_INFO_CTR ctr;
485         NET_USER_INFO_3 user;
486         int validation_level = 3;
487
488         ZERO_STRUCT(q);
489         ZERO_STRUCT(r);
490         ZERO_STRUCT(dummy_rtn_creds);
491
492         /* Initialise parse structures */
493
494         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
495         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
496
497         /* Initialise input parameters */
498
499         gen_next_creds(cli, &clnt_creds);
500
501         q.validation_level = validation_level;
502
503         if (ret_creds == NULL)
504                 ret_creds = &dummy_rtn_creds;
505
506         ctr.switch_value = logon_type;
507
508         switch (logon_type) {
509         case INTERACTIVE_LOGON_TYPE: {
510                 unsigned char lm_owf_user_pwd[16], nt_owf_user_pwd[16];
511
512                 nt_lm_owf_gen(password, nt_owf_user_pwd, lm_owf_user_pwd);
513
514                 init_id_info1(&ctr.auth.id1, lp_workgroup(), 
515                               0, /* param_ctrl */
516                               0xdead, 0xbeef, /* LUID? */
517                               username, cli->clnt_name_slash,
518                               cli->sess_key, lm_owf_user_pwd,
519                               nt_owf_user_pwd);
520
521                 break;
522         }
523         case NET_LOGON_TYPE: {
524                 uint8 chal[8];
525                 unsigned char local_lm_response[24];
526                 unsigned char local_nt_response[24];
527
528                 generate_random_buffer(chal, 8, False);
529
530                 SMBencrypt(password, chal, local_lm_response);
531                 SMBNTencrypt(password, chal, local_nt_response);
532
533                 init_id_info2(&ctr.auth.id2, lp_workgroup(), 
534                               0, /* param_ctrl */
535                               0xdead, 0xbeef, /* LUID? */
536                               username, cli->clnt_name_slash, chal,
537                               local_lm_response, 24, local_nt_response, 24);
538                 break;
539         }
540         default:
541                 DEBUG(0, ("switch value %d not supported\n", 
542                           ctr.switch_value));
543                 goto done;
544         }
545
546         init_sam_info(&q.sam_id, cli->srv_name_slash, global_myname(),
547                       &clnt_creds, ret_creds, logon_type,
548                       &ctr);
549
550         /* Marshall data and send request */
551
552         if (!net_io_q_sam_logon("", &q, &qbuf, 0) ||
553             !rpc_api_pipe_req(cli, NET_SAMLOGON, &qbuf, &rbuf)) {
554                 goto done;
555         }
556
557         /* Unmarshall response */
558
559         r.user = &user;
560
561         if (!net_io_r_sam_logon("", &r, &rbuf, 0)) {
562                 goto done;
563         }
564
565         /* Return results */
566
567         result = r.status;
568         memcpy(ret_creds, &r.srv_creds, sizeof(*ret_creds));
569
570  done:
571         prs_mem_free(&qbuf);
572         prs_mem_free(&rbuf);
573
574         return result;
575 }
576
577
578 /** 
579  * Logon domain user with an 'network' SAM logon 
580  *
581  * @param info3 Pointer to a NET_USER_INFO_3 already allocated by the caller.
582  **/
583
584 NTSTATUS cli_netlogon_sam_network_logon(struct cli_state *cli, TALLOC_CTX *mem_ctx,
585                                         DOM_CRED *ret_creds,
586                                         const char *username, const char *domain, const char *workstation, 
587                                         const uint8 chal[8], 
588                                         DATA_BLOB lm_response, DATA_BLOB nt_response,
589                                         NET_USER_INFO_3 *info3)
590
591 {
592         prs_struct qbuf, rbuf;
593         NET_Q_SAM_LOGON q;
594         NET_R_SAM_LOGON r;
595         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
596         DOM_CRED clnt_creds, dummy_rtn_creds;
597         NET_ID_INFO_CTR ctr;
598         int validation_level = 3;
599         char *workstation_name_slash;
600         uint8 netlogon_sess_key[16];
601         static uint8 zeros[16];
602         
603         ZERO_STRUCT(q);
604         ZERO_STRUCT(r);
605         ZERO_STRUCT(dummy_rtn_creds);
606
607         workstation_name_slash = talloc_asprintf(mem_ctx, "\\\\%s", workstation);
608         
609         if (!workstation_name_slash) {
610                 DEBUG(0, ("talloc_asprintf failed!\n"));
611                 return NT_STATUS_NO_MEMORY;
612         }
613
614         /* Initialise parse structures */
615
616         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
617         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
618
619         /* Initialise input parameters */
620
621         gen_next_creds(cli, &clnt_creds);
622
623         q.validation_level = validation_level;
624
625         if (ret_creds == NULL)
626                 ret_creds = &dummy_rtn_creds;
627
628         ctr.switch_value = NET_LOGON_TYPE;
629
630         init_id_info2(&ctr.auth.id2, domain,
631                       0, /* param_ctrl */
632                       0xdead, 0xbeef, /* LUID? */
633                       username, workstation_name_slash, (const uchar*)chal,
634                       lm_response.data, lm_response.length, nt_response.data, nt_response.length);
635  
636         init_sam_info(&q.sam_id, cli->srv_name_slash, global_myname(),
637                       &clnt_creds, ret_creds, NET_LOGON_TYPE,
638                       &ctr);
639
640         /* Marshall data and send request */
641
642         if (!net_io_q_sam_logon("", &q, &qbuf, 0) ||
643             !rpc_api_pipe_req(cli, NET_SAMLOGON, &qbuf, &rbuf)) {
644                 goto done;
645         }
646
647         /* Unmarshall response */
648
649         r.user = info3;
650
651         if (!net_io_r_sam_logon("", &r, &rbuf, 0)) {
652                 goto done;
653         }
654
655         ZERO_STRUCT(netlogon_sess_key);
656         memcpy(netlogon_sess_key, cli->sess_key, 8);
657         
658         if (memcmp(zeros, info3->user_sess_key, 16) != 0)
659                 SamOEMhash(info3->user_sess_key, netlogon_sess_key, 16);
660                 
661         if (memcmp(zeros, info3->padding, 16) != 0)
662                 SamOEMhash(info3->padding, netlogon_sess_key, 16);
663
664         /* Return results */
665
666         result = r.status;
667         memcpy(ret_creds, &r.srv_creds, sizeof(*ret_creds));
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