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