more debug classess activated
[nivanova/samba-autobuild/.git] / source3 / rpc_client / cli_reg.c
1 /* 
2  *  Unix SMB/CIFS implementation.
3  *  RPC Pipe client / server routines
4  *  Copyright (C) Andrew Tridgell              1992-1998,
5  *  Copyright (C) Luke Kenneth Casson Leighton 1996-1998,
6  *  Copyright (C) Paul Ashton                  1997-1998.
7  *  Copyright (C) Jeremy Allison                    1999.
8  *  
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or
12  *  (at your option) any later version.
13  *  
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *  
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the Free Software
21  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24
25 #include "includes.h"
26
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_RPC_CLI
29
30 /****************************************************************************
31 do a REG Open Policy
32 ****************************************************************************/
33 BOOL do_reg_connect(struct cli_state *cli, char *full_keyname, char *key_name,
34                                 POLICY_HND *reg_hnd)
35 {
36         BOOL res = True;
37         uint32 reg_type = 0;
38
39         if (full_keyname == NULL)
40                 return False;
41
42         ZERO_STRUCTP(reg_hnd);
43
44         /*
45          * open registry receive a policy handle
46          */
47
48         if (!reg_split_key(full_keyname, &reg_type, key_name)) {
49                 DEBUG(0,("do_reg_connect: unrecognised key name %s\n", full_keyname));  
50                 return False;
51         }
52
53         switch (reg_type) {
54         case HKEY_LOCAL_MACHINE:
55                 res = res ? do_reg_open_hklm(cli, 0x84E0, 0x02000000, reg_hnd) : False;
56                 break;
57         
58         case HKEY_USERS:
59                 res = res ? do_reg_open_hku(cli, 0x84E0, 0x02000000, reg_hnd) : False;
60                 break;
61
62         default:
63                 DEBUG(0,("do_reg_connect: unrecognised hive key\n"));   
64                 return False;
65         }
66
67         return res;
68 }
69
70 /****************************************************************************
71 do a REG Open Policy
72 ****************************************************************************/
73 BOOL do_reg_open_hklm(struct cli_state *cli, uint16 unknown_0, uint32 level,
74                                 POLICY_HND *hnd)
75 {
76         prs_struct rbuf;
77         prs_struct buf; 
78         REG_Q_OPEN_HKLM q_o;
79         REG_R_OPEN_HKLM r_o;
80
81         if (hnd == NULL)
82                 return False;
83
84         prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
85         prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
86
87         /* create and send a MSRPC command with api REG_OPEN_HKLM */
88
89         DEBUG(4,("REG Open HKLM\n"));
90
91         init_reg_q_open_hklm(&q_o, unknown_0, level);
92
93         /* turn parameters into data stream */
94         if(!reg_io_q_open_hklm("", &q_o, &buf, 0)) {
95                 prs_mem_free(&buf);
96                 prs_mem_free(&rbuf);
97                 return False;
98         }
99
100         /* send the data on \PIPE\ */
101         if (!rpc_api_pipe_req(cli, REG_OPEN_HKLM, &buf, &rbuf)) {
102                 prs_mem_free(&buf);
103                 prs_mem_free(&rbuf);
104                 return False;
105         }
106
107         prs_mem_free(&buf);
108
109         ZERO_STRUCT(r_o);
110
111         if(!reg_io_r_open_hklm("", &r_o, &rbuf, 0)) {
112                 prs_mem_free(&rbuf);
113                 return False;
114         }
115
116         if (r_o.status != 0) {
117                 /* report error code */
118                 DEBUG(0,("REG_OPEN_HKLM: %s\n", nt_errstr(r_o.status)));
119                 prs_mem_free(&rbuf);
120                 return False;
121         }
122
123         /* ok, at last: we're happy. return the policy handle */
124         *hnd = r_o.pol;
125
126         prs_mem_free(&rbuf);
127
128         return True;
129 }
130
131 /****************************************************************************
132 do a REG Open HKU
133 ****************************************************************************/
134 BOOL do_reg_open_hku(struct cli_state *cli, uint16 unknown_0, uint32 level,
135                                 POLICY_HND *hnd)
136 {
137         prs_struct rbuf;
138         prs_struct buf; 
139         REG_Q_OPEN_HKU q_o;
140         REG_R_OPEN_HKU r_o;
141
142         if (hnd == NULL)
143                 return False;
144
145         prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
146         prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
147
148         /* create and send a MSRPC command with api REG_OPEN_HKU */
149
150         DEBUG(4,("REG Open HKU\n"));
151
152         init_reg_q_open_hku(&q_o, unknown_0, level);
153
154         /* turn parameters into data stream */
155         if(!reg_io_q_open_hku("", &q_o, &buf, 0)) {
156                 prs_mem_free(&buf);
157                 prs_mem_free(&rbuf);
158                 return False;
159         }
160
161         /* send the data on \PIPE\ */
162         if (rpc_api_pipe_req(cli, REG_OPEN_HKU, &buf, &rbuf)) {
163                 prs_mem_free(&buf);
164                 prs_mem_free(&rbuf);
165                 return False;
166         }
167
168         prs_mem_free(&buf);
169
170         ZERO_STRUCT(r_o);
171
172         if(!reg_io_r_open_hku("", &r_o, &rbuf, 0)) {
173                 prs_mem_free(&rbuf);
174                 return False;
175         }
176
177         if (r_o.status != 0) {
178                 /* report error code */
179                 DEBUG(0,("REG_OPEN_HKU: %s\n", nt_errstr(r_o.status)));
180                 prs_mem_free(&rbuf);
181                 return False;
182         }
183
184         /* ok, at last: we're happy. return the policy handle */
185         *hnd = r_o.pol;
186
187         prs_mem_free(&rbuf);
188
189         return True;
190 }
191
192 /****************************************************************************
193 do a REG Unknown 0xB command.  sent after a create key or create value.
194 this might be some sort of "sync" or "refresh" command, sent after
195 modification of the registry...
196 ****************************************************************************/
197 BOOL do_reg_flush_key(struct cli_state *cli, POLICY_HND *hnd)
198 {
199         prs_struct rbuf;
200         prs_struct buf; 
201         REG_Q_FLUSH_KEY q_o;
202         REG_R_FLUSH_KEY r_o;
203
204         if (hnd == NULL)
205                 return False;
206
207         prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
208         prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
209
210         /* create and send a MSRPC command with api REG_FLUSH_KEY */
211
212         DEBUG(4,("REG Unknown 0xB\n"));
213
214         init_reg_q_flush_key(&q_o, hnd);
215
216         /* turn parameters into data stream */
217         if(!reg_io_q_flush_key("", &q_o, &buf, 0)) {
218                 prs_mem_free(&buf);
219                 prs_mem_free(&rbuf);
220                 return False;
221         }
222
223         /* send the data on \PIPE\ */
224         if (!rpc_api_pipe_req(cli, REG_FLUSH_KEY, &buf, &rbuf)) {
225                 prs_mem_free(&buf);
226                 prs_mem_free(&rbuf);
227                 return False;
228         }
229
230         prs_mem_free(&buf);
231
232         ZERO_STRUCT(r_o);
233
234         if(!reg_io_r_flush_key("", &r_o, &rbuf, 0)) {
235                 prs_mem_free(&rbuf);
236                 return False;
237         }
238
239         if (r_o.status != 0) {
240                 /* report error code */
241                 DEBUG(0,("REG_FLUSH_KEY: %s\n", nt_errstr(r_o.status)));
242                 prs_mem_free(&rbuf);
243                 return False;
244         }
245
246         prs_mem_free(&rbuf);
247
248         return True;
249 }
250
251 /****************************************************************************
252 do a REG Query Key
253 ****************************************************************************/
254 BOOL do_reg_query_key(struct cli_state *cli, POLICY_HND *hnd,
255                                 char *class, uint32 *class_len,
256                                 uint32 *num_subkeys, uint32 *max_subkeylen,
257                                 uint32 *max_subkeysize, uint32 *num_values,
258                                 uint32 *max_valnamelen, uint32 *max_valbufsize,
259                                 uint32 *sec_desc, NTTIME *mod_time)
260 {
261         prs_struct rbuf;
262         prs_struct buf; 
263         REG_Q_QUERY_KEY q_o;
264         REG_R_QUERY_KEY r_o;
265
266         if (hnd == NULL)
267                 return False;
268
269         prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
270         prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
271
272         /* create and send a MSRPC command with api REG_QUERY_KEY */
273
274         DEBUG(4,("REG Query Key\n"));
275
276         init_reg_q_query_key(&q_o, hnd, *class_len);
277
278         /* turn parameters into data stream */
279         if(!reg_io_q_query_key("", &q_o, &buf, 0)) {
280                 prs_mem_free(&buf);
281                 prs_mem_free(&rbuf);
282                 return False;
283         }
284
285         /* send the data on \PIPE\ */
286         if (!rpc_api_pipe_req(cli, REG_QUERY_KEY, &buf, &rbuf)) {
287                 prs_mem_free(&buf);
288                 prs_mem_free(&rbuf);
289                 return False;
290         }
291
292         prs_mem_free(&buf);
293
294         ZERO_STRUCT(r_o);
295
296         if(!reg_io_r_query_key("", &r_o, &rbuf, 0)) {
297                 prs_mem_free(&rbuf);
298                 return False;
299         }
300
301         if (r_o.status != 0) {
302                 /* report error code */
303                 DEBUG(0,("REG_QUERY_KEY: %s\n", nt_errstr(r_o.status)));
304                 prs_mem_free(&rbuf);
305                 return False;
306         }
307
308         *class_len      = r_o.hdr_class.uni_max_len;
309         rpcstr_pull(class, &r_o.uni_class, -1, -1, 0);
310         *num_subkeys    = r_o.num_subkeys   ;
311         *max_subkeylen  = r_o.max_subkeylen ;
312         *max_subkeysize = r_o.max_subkeysize;
313         *num_values     = r_o.num_values    ;
314         *max_valnamelen = r_o.max_valnamelen;
315         *max_valbufsize = r_o.max_valbufsize;
316         *sec_desc       = r_o.sec_desc      ;
317         *mod_time       = r_o.mod_time      ;
318
319         prs_mem_free(&rbuf);
320
321         return True;
322 }
323
324 /****************************************************************************
325 do a REG Unknown 1A
326 ****************************************************************************/
327 BOOL do_reg_unknown_1a(struct cli_state *cli, POLICY_HND *hnd, uint32 *unk)
328 {
329         prs_struct rbuf;
330         prs_struct buf; 
331         REG_Q_UNK_1A q_o;
332         REG_R_UNK_1A r_o;
333
334         if (hnd == NULL)
335                 return False;
336
337         prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
338         prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
339
340         /* create and send a MSRPC command with api REG_UNKNOWN_1A */
341
342         DEBUG(4,("REG Unknown 1a\n"));
343
344         init_reg_q_unk_1a(&q_o, hnd);
345
346         /* turn parameters into data stream */
347         if(!reg_io_q_unk_1a("", &q_o, &buf, 0)) {
348                 prs_mem_free(&buf);
349                 prs_mem_free(&rbuf);
350                 return False;
351         }
352
353         /* send the data on \PIPE\ */
354         if (rpc_api_pipe_req(cli, REG_UNK_1A, &buf, &rbuf)) {
355                 prs_mem_free(&buf);
356                 prs_mem_free(&rbuf);
357                 return False;
358         }
359
360         prs_mem_free(&buf);
361
362         ZERO_STRUCT(r_o);
363
364         if(!reg_io_r_unk_1a("", &r_o, &rbuf, 0)) {
365                 prs_mem_free(&rbuf);
366                 return False;
367         }
368
369         if (r_o.status != 0) {
370                 /* report error code */
371                 DEBUG(0,("REG_UNK_1A: %s\n", nt_errstr(r_o.status)));
372                 prs_mem_free(&rbuf);
373                 return False;
374         }
375
376         (*unk) = r_o.unknown;
377
378         prs_mem_free(&rbuf);
379
380         return True;
381 }
382
383 /****************************************************************************
384 do a REG Query Info
385 ****************************************************************************/
386 BOOL do_reg_query_info(struct cli_state *cli, POLICY_HND *hnd,
387                                 char *key_value, uint32* key_type)
388 {
389         prs_struct rbuf;
390         prs_struct buf; 
391         REG_Q_INFO q_o;
392         REG_R_INFO r_o;
393
394         if (hnd == NULL)
395                 return False;
396
397         prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
398         prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
399
400         /* create and send a MSRPC command with api REG_INFO */
401
402         DEBUG(4,("REG Query Info\n"));
403
404         init_reg_q_info(&q_o, hnd, "ProductType");
405
406         /* turn parameters into data stream */
407         if(!reg_io_q_info("", &q_o, &buf, 0)) {
408                 prs_mem_free(&buf);
409                 prs_mem_free(&rbuf);
410                 return False;
411         }
412
413         /* send the data on \PIPE\ */
414         if (!rpc_api_pipe_req(cli, REG_INFO, &buf, &rbuf)) {
415                 prs_mem_free(&buf);
416                 prs_mem_free(&rbuf);
417                 return False;
418         }
419
420         prs_mem_free(&buf);
421
422         ZERO_STRUCT(r_o);
423
424         if(!reg_io_r_info("", &r_o, &rbuf, 0)) {
425                 prs_mem_free(&rbuf);
426                 return False;
427         }
428
429         if ( r_o.status != 0) {
430                 /* report error code */
431                 DEBUG(0,("REG_INFO: %s\n", nt_errstr(r_o.status)));
432                 prs_mem_free(&rbuf);
433                 return False;
434         }
435
436         /*fstrcpy(key_value, dos_buffer2_to_str(r_o.uni_val));*/
437         rpcstr_pull(key_value, r_o.uni_val->buffer, sizeof(fstring), r_o.uni_val->buf_len, 0);
438         *key_type = r_o.type;
439
440         prs_mem_free(&rbuf);
441
442         return True;
443 }
444
445 /****************************************************************************
446 do a REG Set Key Security 
447 ****************************************************************************/
448 BOOL do_reg_set_key_sec(struct cli_state *cli, POLICY_HND *hnd, SEC_DESC_BUF *sec_desc_buf)
449 {
450         prs_struct rbuf;
451         prs_struct buf; 
452         REG_Q_SET_KEY_SEC q_o;
453         REG_R_SET_KEY_SEC r_o;
454
455         if (hnd == NULL)
456                 return False;
457
458         prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
459         prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
460
461         /* create and send a MSRPC command with api REG_SET_KEY_SEC */
462
463         DEBUG(4,("REG Set Key security.\n"));
464
465         init_reg_q_set_key_sec(&q_o, hnd, sec_desc_buf);
466
467         /* turn parameters into data stream */
468         if(!reg_io_q_set_key_sec("", &q_o, &buf, 0)) {
469                 prs_mem_free(&buf);
470                 prs_mem_free(&rbuf);
471                 return False;
472         }
473
474         /* send the data on \PIPE\ */
475         if (!rpc_api_pipe_req(cli, REG_SET_KEY_SEC, &buf, &rbuf)) {
476                 prs_mem_free(&buf);
477                 prs_mem_free(&rbuf);
478                 return False;
479         }
480
481         prs_mem_free(&buf);
482
483         ZERO_STRUCT(r_o);
484
485         if(!reg_io_r_set_key_sec("", &r_o, &rbuf, 0)) {
486                 prs_mem_free(&rbuf);
487                 return False;
488         }
489
490         if (r_o.status != 0) {
491                 prs_mem_free(&rbuf);
492                 return False;
493         }
494
495         prs_mem_free(&rbuf);
496
497         return True;
498 }
499
500 /****************************************************************************
501 do a REG Query Key Security 
502 ****************************************************************************/
503
504 BOOL do_reg_get_key_sec(struct cli_state *cli, POLICY_HND *hnd, uint32 *sec_buf_size, SEC_DESC_BUF **ppsec_desc_buf)
505 {
506         prs_struct rbuf;
507         prs_struct buf; 
508         REG_Q_GET_KEY_SEC q_o;
509         REG_R_GET_KEY_SEC r_o;
510
511         if (hnd == NULL)
512                 return False;
513
514         prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
515         prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
516
517         /* create and send a MSRPC command with api REG_GET_KEY_SEC */
518
519         DEBUG(4,("REG query key security.  buf_size: %d\n", *sec_buf_size));
520
521         init_reg_q_get_key_sec(&q_o, hnd, *sec_buf_size, NULL);
522
523         /* turn parameters into data stream */
524         if(!reg_io_q_get_key_sec("", &q_o, &buf, 0)) {
525                 prs_mem_free(&buf);
526                 prs_mem_free(&rbuf);
527                 return False;
528         }
529
530         /* send the data on \PIPE\ */
531         if (!rpc_api_pipe_req(cli, REG_GET_KEY_SEC, &buf, &rbuf)) {
532                 prs_mem_free(&buf);
533                 prs_mem_free(&rbuf);
534                 return False;
535         }
536
537         prs_mem_free(&buf);
538
539         ZERO_STRUCT(r_o);
540
541         if(!reg_io_r_get_key_sec("", &r_o, &rbuf, 0)) {
542                 prs_mem_free(&rbuf);
543                 return False;
544         }
545
546         if (r_o.status == 0x0000007a) {
547                 /*
548                  * get the maximum buffer size: it was too small
549                  */
550                 (*sec_buf_size) = r_o.hdr_sec.buf_max_len;
551                 DEBUG(5,("sec_buf_size too small.  use %d\n", *sec_buf_size));
552         } else if (r_o.status != 0) {
553                 /* report error code */
554                 DEBUG(0,("REG_GET_KEY_SEC: %s\n", nt_errstr(r_o.status)));
555                 prs_mem_free(&rbuf);
556                 return False;
557         } else {
558                 (*sec_buf_size) = r_o.data->len;
559                 *ppsec_desc_buf = r_o.data;
560         }
561
562         prs_mem_free(&rbuf);
563
564         return True;
565 }
566
567 /****************************************************************************
568 do a REG Delete Value
569 ****************************************************************************/
570 BOOL do_reg_delete_val(struct cli_state *cli, POLICY_HND *hnd, char *val_name)
571 {
572         prs_struct rbuf;
573         prs_struct buf; 
574         REG_Q_DELETE_VALUE q_o;
575         REG_R_DELETE_VALUE r_o;
576
577         if (hnd == NULL)
578                 return False;
579
580         prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
581         prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
582
583         /* create and send a MSRPC command with api REG_DELETE_VALUE */
584
585         DEBUG(4,("REG Delete Value: %s\n", val_name));
586
587         init_reg_q_delete_val(&q_o, hnd, val_name);
588
589         /* turn parameters into data stream */
590         if(!reg_io_q_delete_val("", &q_o, &buf, 0)) {
591                 prs_mem_free(&buf);
592                 prs_mem_free(&rbuf);
593                 return False;
594         }
595
596         /* send the data on \PIPE\ */
597         if (rpc_api_pipe_req(cli, REG_DELETE_VALUE, &buf, &rbuf)) {
598                 prs_mem_free(&buf);
599                 prs_mem_free(&rbuf);
600                 return False;
601         }
602
603         prs_mem_free(&buf);
604
605         ZERO_STRUCT(r_o);
606
607         if(!reg_io_r_delete_val("", &r_o, &rbuf, 0)) {
608                 prs_mem_free(&rbuf);
609                 return False;
610         }
611
612         if (r_o.status != 0) {
613                 /* report error code */
614                 DEBUG(0,("REG_DELETE_VALUE: %s\n", nt_errstr(r_o.status)));
615                 prs_mem_free(&rbuf);
616                 return False;
617         }
618
619         prs_mem_free(&rbuf);
620
621         return True;
622 }
623
624 /****************************************************************************
625 do a REG Delete Key
626 ****************************************************************************/
627 BOOL do_reg_delete_key(struct cli_state *cli, POLICY_HND *hnd, char *key_name)
628 {
629         prs_struct rbuf;
630         prs_struct buf; 
631         REG_Q_DELETE_KEY q_o;
632         REG_R_DELETE_KEY r_o;
633
634         if (hnd == NULL)
635                 return False;
636
637         prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
638         prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
639
640         /* create and send a MSRPC command with api REG_DELETE_KEY */
641
642         DEBUG(4,("REG Delete Key: %s\n", key_name));
643
644         init_reg_q_delete_key(&q_o, hnd, key_name);
645
646         /* turn parameters into data stream */
647         if(!reg_io_q_delete_key("", &q_o, &buf, 0)) {
648                 prs_mem_free(&buf);
649                 prs_mem_free(&rbuf);
650                 return False;
651         }
652
653         /* send the data on \PIPE\ */
654         if (!rpc_api_pipe_req(cli, REG_DELETE_KEY, &buf, &rbuf)) {
655                 prs_mem_free(&buf);
656                 prs_mem_free(&rbuf);
657                 return False;
658         }
659
660         prs_mem_free(&buf);
661
662         ZERO_STRUCT(r_o);
663
664         if(!reg_io_r_delete_key("", &r_o, &rbuf, 0)) {
665                 prs_mem_free(&rbuf);
666                 return False;
667         }
668
669         if (r_o.status != 0) {
670                 /* report error code */
671                 DEBUG(0,("REG_DELETE_KEY: %s\n", nt_errstr(r_o.status)));
672                 prs_mem_free(&rbuf);
673                 return False;
674         }
675
676         prs_mem_free(&rbuf);
677
678         return True;
679 }
680
681 /****************************************************************************
682 do a REG Create Key
683 ****************************************************************************/
684 BOOL do_reg_create_key(struct cli_state *cli, POLICY_HND *hnd,
685                                 char *key_name, char *key_class,
686                                 SEC_ACCESS *sam_access,
687                                 POLICY_HND *key)
688 {
689         prs_struct rbuf;
690         prs_struct buf; 
691         REG_Q_CREATE_KEY q_o;
692         REG_R_CREATE_KEY r_o;
693         SEC_DESC *sec = NULL;
694         SEC_DESC_BUF *sec_buf = NULL;
695         size_t sec_len;
696
697         ZERO_STRUCT(q_o);
698
699         if (hnd == NULL)
700                 return False;
701
702         /* create and send a MSRPC command with api REG_CREATE_KEY */
703
704         DEBUG(4,("REG Create Key: %s %s 0x%08x\n", key_name, key_class,
705                 sam_access != NULL ? sam_access->mask : 0));
706
707         if((sec = make_sec_desc( cli->mem_ctx, 1, NULL, NULL, NULL, NULL, &sec_len)) == NULL) {
708                 DEBUG(0,("make_sec_desc : malloc fail.\n"));
709                 return False;
710         }
711
712         DEBUG(10,("make_sec_desc: len = %d\n", (int)sec_len));
713
714         if((sec_buf = make_sec_desc_buf( cli->mem_ctx, (int)sec_len, sec)) == NULL) {
715                 DEBUG(0,("make_sec_desc : malloc fail (1)\n"));
716                 return False;
717         }
718
719         prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
720         prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
721
722         init_reg_q_create_key(&q_o, hnd, key_name, key_class, sam_access, sec_buf);
723
724         /* turn parameters into data stream */
725         if(!reg_io_q_create_key("", &q_o, &buf, 0)) {
726                 prs_mem_free(&buf);
727                 prs_mem_free(&rbuf);
728                 return False;
729         }
730
731         /* send the data on \PIPE\ */
732         if (rpc_api_pipe_req(cli, REG_CREATE_KEY, &buf, &rbuf)) {
733                 prs_mem_free(&buf);
734                 prs_mem_free(&rbuf);
735                 return False;
736         }
737
738         prs_mem_free(&buf);
739
740         ZERO_STRUCT(r_o);
741
742         if(!reg_io_r_create_key("", &r_o, &rbuf, 0)) {
743                 prs_mem_free(&rbuf);
744                 return False;
745         }
746
747         if (r_o.status != 0) {
748                 /* report error code */
749                 DEBUG(0,("REG_CREATE_KEY: %s\n", nt_errstr(r_o.status)));
750                 prs_mem_free(&rbuf);
751                 return False;
752         }
753
754         *key = r_o.key_pol;
755
756         prs_mem_free(&rbuf);
757
758         return True;
759 }
760
761 /****************************************************************************
762 do a REG Enum Key
763 ****************************************************************************/
764 BOOL do_reg_enum_key(struct cli_state *cli, POLICY_HND *hnd,
765                                 int key_index, char *key_name,
766                                 uint32 *unk_1, uint32 *unk_2,
767                                 time_t *mod_time)
768 {
769         prs_struct rbuf;
770         prs_struct buf; 
771         REG_Q_ENUM_KEY q_o;
772         REG_R_ENUM_KEY r_o;
773
774         if (hnd == NULL)
775                 return False;
776
777         prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
778         prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
779
780         /* create and send a MSRPC command with api REG_ENUM_KEY */
781
782         DEBUG(4,("REG Enum Key\n"));
783
784         init_reg_q_enum_key(&q_o, hnd, key_index);
785
786         /* turn parameters into data stream */
787         if(!reg_io_q_enum_key("", &q_o, &buf, 0)) {
788                 prs_mem_free(&buf);
789                 prs_mem_free(&rbuf);
790                 return False;
791         }
792
793         /* send the data on \PIPE\ */
794         if (!rpc_api_pipe_req(cli, REG_ENUM_KEY, &buf, &rbuf)) {
795                 prs_mem_free(&buf);
796                 prs_mem_free(&rbuf);
797                 return False;
798         }
799
800         prs_mem_free(&buf);
801
802         ZERO_STRUCT(r_o);
803
804         if(!reg_io_r_enum_key("", &r_o, &rbuf, 0)) {
805                 prs_mem_free(&rbuf);
806                 return False;
807         }
808
809         if (r_o.status != 0) {
810                 /* report error code */
811                 DEBUG(0,("REG_ENUM_KEY: %s\n", nt_errstr(r_o.status)));
812                 prs_mem_free(&rbuf);
813                 return False;
814         }
815
816         (*unk_1) = r_o.unknown_1;
817         (*unk_2) = r_o.unknown_2;
818         rpcstr_pull(key_name, r_o.key_name.str.buffer, -1, -1, 0);
819         (*mod_time) = nt_time_to_unix(&r_o.time);
820
821         prs_mem_free(&rbuf);
822
823         return True;
824 }
825
826 /****************************************************************************
827 do a REG Create Value
828 ****************************************************************************/
829 BOOL do_reg_create_val(struct cli_state *cli, POLICY_HND *hnd,
830                                 char *val_name, uint32 type, BUFFER3 *data)
831 {
832         prs_struct rbuf;
833         prs_struct buf; 
834         REG_Q_CREATE_VALUE q_o;
835         REG_R_CREATE_VALUE r_o;
836
837         if (hnd == NULL)
838                 return False;
839
840         prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
841         prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
842
843         /* create and send a MSRPC command with api REG_CREATE_VALUE */
844
845         DEBUG(4,("REG Create Value: %s\n", val_name));
846
847         init_reg_q_create_val(&q_o, hnd, val_name, type, data);
848
849         /* turn parameters into data stream */
850         if(!reg_io_q_create_val("", &q_o, &buf, 0)) {
851                 prs_mem_free(&buf);
852                 prs_mem_free(&rbuf);
853                 return False;
854         }
855
856         /* send the data on \PIPE\ */
857         if (!rpc_api_pipe_req(cli, REG_CREATE_VALUE, &buf, &rbuf)) {
858                 prs_mem_free(&buf);
859                 prs_mem_free(&rbuf);
860                 return False;
861         }
862
863         prs_mem_free(&buf);
864
865         ZERO_STRUCT(r_o);
866
867         if(!reg_io_r_create_val("", &r_o, &rbuf, 0)) {
868                 prs_mem_free(&rbuf);
869                 return False;
870         }
871
872         if (r_o.status != 0) {
873                 /* report error code */
874                 DEBUG(0,("REG_CREATE_VALUE: %s\n", nt_errstr(r_o.status)));
875                 prs_mem_free(&rbuf);
876                 return False;
877         }
878
879         prs_mem_free(&rbuf);
880
881         return True;
882 }
883
884 /****************************************************************************
885 do a REG Enum Value
886 ****************************************************************************/
887 BOOL do_reg_enum_val(struct cli_state *cli, POLICY_HND *hnd,
888                                 int val_index, int max_valnamelen, int max_valbufsize,
889                                 fstring val_name,
890                                 uint32 *val_type, BUFFER2 *value)
891 {
892         prs_struct rbuf;
893         prs_struct buf; 
894         REG_Q_ENUM_VALUE q_o;
895         REG_R_ENUM_VALUE r_o;
896
897         if (hnd == NULL)
898                 return False;
899
900         prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
901         prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
902
903         /* create and send a MSRPC command with api REG_ENUM_VALUE */
904
905         DEBUG(4,("REG Enum Value\n"));
906
907         init_reg_q_enum_val(&q_o, hnd, val_index, max_valnamelen, max_valbufsize);
908
909         /* turn parameters into data stream */
910         if(!reg_io_q_enum_val("", &q_o, &buf, 0)) {
911                 prs_mem_free(&buf);
912                 prs_mem_free(&rbuf);
913                 return False;
914         }
915
916         /* send the data on \PIPE\ */
917         if (!rpc_api_pipe_req(cli, REG_ENUM_VALUE, &buf, &rbuf)) {
918                 prs_mem_free(&buf);
919                 prs_mem_free(&rbuf);
920                 return False;
921         }
922
923         prs_mem_free(&buf);
924
925         ZERO_STRUCT(r_o);
926         r_o.buf_value = value;
927
928         if(!reg_io_r_enum_val("", &r_o, &rbuf, 0)) {
929                 prs_mem_free(&rbuf);
930                 return False;
931         }
932
933         if (r_o.status != 0) {
934                 /* report error code */
935                 DEBUG(0,("REG_ENUM_VALUE: %s\n", nt_errstr(r_o.status)));
936                 prs_mem_free(&rbuf);
937                 return False;
938         }
939
940         (*val_type) = r_o.type;
941         rpcstr_pull(val_name, &r_o.uni_name, -1, -1, 0);
942
943         prs_mem_free(&rbuf);
944
945         return True;
946 }
947
948 /****************************************************************************
949 do a REG Open Key
950 ****************************************************************************/
951 BOOL do_reg_open_entry(struct cli_state *cli, POLICY_HND *hnd,
952                                 char *key_name, uint32 unk_0,
953                                 POLICY_HND *key_hnd)
954 {
955         prs_struct rbuf;
956         prs_struct buf; 
957         REG_Q_OPEN_ENTRY q_o;
958         REG_R_OPEN_ENTRY r_o;
959
960         if (hnd == NULL)
961                 return False;
962
963         prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
964         prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
965
966         /* create and send a MSRPC command with api REG_OPEN_ENTRY */
967
968         DEBUG(4,("REG Open Entry\n"));
969
970         init_reg_q_open_entry(&q_o, hnd, key_name, unk_0);
971
972         /* turn parameters into data stream */
973         if(!reg_io_q_open_entry("", &q_o, &buf, 0)) {
974                 prs_mem_free(&buf);
975                 prs_mem_free(&rbuf);
976                 return False;
977         }
978
979         /* send the data on \PIPE\ */
980         if (!rpc_api_pipe_req(cli, REG_OPEN_ENTRY, &buf, &rbuf)) {
981                 prs_mem_free(&buf);
982                 prs_mem_free(&rbuf);
983                 return False;
984         }
985
986         prs_mem_free(&buf);
987
988         ZERO_STRUCT(r_o);
989
990         if(!reg_io_r_open_entry("", &r_o, &rbuf, 0)) {
991                 prs_mem_free(&rbuf);
992                 return False;
993         }
994
995         if (r_o.status != 0) {
996                 /* report error code */
997                 DEBUG(0,("REG_OPEN_ENTRY: %s\n", nt_errstr(r_o.status)));
998                 prs_mem_free(&rbuf);
999                 return False;
1000         }
1001
1002         *key_hnd = r_o.pol;
1003
1004         prs_mem_free(&rbuf);
1005
1006         return True;
1007 }
1008
1009 /****************************************************************************
1010 do a REG Close
1011 ****************************************************************************/
1012 BOOL do_reg_close(struct cli_state *cli, POLICY_HND *hnd)
1013 {
1014         prs_struct rbuf;
1015         prs_struct buf; 
1016         REG_Q_CLOSE q_c;
1017         REG_R_CLOSE r_c;
1018
1019         if (hnd == NULL)
1020                 return False;
1021
1022         /* create and send a MSRPC command with api REG_CLOSE */
1023
1024         prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
1025         prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
1026
1027         DEBUG(4,("REG Close\n"));
1028
1029         /* store the parameters */
1030         init_reg_q_close(&q_c, hnd);
1031
1032         /* turn parameters into data stream */
1033         if(!reg_io_q_close("", &q_c, &buf, 0)) {
1034                 prs_mem_free(&buf);
1035                 prs_mem_free(&rbuf);
1036                 return False;
1037         }
1038
1039         /* send the data on \PIPE\ */
1040         if (!rpc_api_pipe_req(cli, REG_CLOSE, &buf, &rbuf)) {
1041                 prs_mem_free(&buf);
1042                 prs_mem_free(&rbuf);
1043                 return False;
1044         }
1045
1046         prs_mem_free(&buf);
1047
1048         ZERO_STRUCT(r_c);
1049
1050         if(!reg_io_r_close("", &r_c, &rbuf, 0)) {
1051                 prs_mem_free(&rbuf);
1052                 return False;
1053         }
1054
1055         if (r_c.status != 0) {
1056                 /* report error code */
1057                 DEBUG(0,("REG_CLOSE: %s\n", nt_errstr(r_c.status)));
1058                 prs_mem_free(&rbuf);
1059                 return False;
1060         }
1061
1062         /* check that the returned policy handle is all zeros */
1063
1064         if (IVAL(&r_c.pol.data1,0) || IVAL(&r_c.pol.data2,0) || SVAL(&r_c.pol.data3,0) ||
1065                 SVAL(&r_c.pol.data4,0) || IVAL(r_c.pol.data5,0) || IVAL(r_c.pol.data5,4) ) {
1066                         prs_mem_free(&rbuf);
1067                         DEBUG(0,("REG_CLOSE: non-zero handle returned\n"));
1068                         return False;
1069         }       
1070
1071         prs_mem_free(&rbuf);
1072
1073         return True;
1074 }