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