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