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