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