fc7f6a5b534c26f4a6c8cdc04ee9c33e2b884835
[samba.git] / source3 / 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-1998,
7  *  Copyright (C) Luke Kenneth Casson Leighton 1996-1998,
8  *  Copyright (C) Paul Ashton                  1997-1998.
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, uint16 fnum, 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, fnum,
65                                         0x84E0, 0x02000000,
66                                         reg_hnd) : False;
67                         break;
68                 }
69         
70                 case HKEY_USERS:
71                 {
72                         res = res ? do_reg_open_hku(cli, fnum,
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 fnum, 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, fnum, 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 fnum, 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, fnum, 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, uint16 fnum, 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, fnum, 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, uint16 fnum, 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, fnum, 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                         unistr2_to_ascii(class, &r_o.uni_class, sizeof(fstring)-1);
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, uint16 fnum, 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, fnum, 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, uint16 fnum, 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, fnum, 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                         unistr_to_ascii(type, r_o.uni_type.buffer,
429                                 MIN(r_o.uni_type.buf_len, sizeof(fstring)-1));
430                         (*unk_0) = r_o.unknown_0;
431                         (*unk_1) = r_o.unknown_1;
432                 }
433         }
434
435         prs_mem_free(&rbuf);
436         prs_mem_free(&buf );
437
438         return valid_query;
439 }
440
441 /****************************************************************************
442 do a REG Set Key Security 
443 ****************************************************************************/
444 BOOL do_reg_set_key_sec(struct cli_state *cli, uint16 fnum, POLICY_HND *hnd,
445                                 uint32 sec_buf_size, SEC_DESC *sec_buf)
446 {
447         prs_struct rbuf;
448         prs_struct buf; 
449         REG_Q_SET_KEY_SEC q_o;
450         BOOL valid_query = False;
451
452         if (hnd == NULL) return False;
453
454         prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
455         prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );
456
457         /* create and send a MSRPC command with api REG_SET_KEY_SEC */
458
459         DEBUG(4,("REG Set Key security.\n"));
460
461         make_reg_q_set_key_sec(&q_o, hnd, sec_buf_size, sec_buf);
462
463         /* turn parameters into data stream */
464         reg_io_q_set_key_sec("", &q_o, &buf, 0);
465
466         /* send the data on \PIPE\ */
467         if (rpc_api_pipe_req(cli, fnum, REG_SET_KEY_SEC, &buf, &rbuf))
468         {
469                 REG_R_SET_KEY_SEC r_o;
470                 BOOL p;
471
472                 ZERO_STRUCT(r_o);
473
474                 reg_io_r_set_key_sec("", &r_o, &rbuf, 0);
475                 p = rbuf.offset != 0;
476
477                 if (p && r_o.status != 0)
478                 {
479                         valid_query = True;
480                 }
481         }
482
483         prs_mem_free(&rbuf);
484         prs_mem_free(&buf );
485
486         return valid_query;
487 }
488
489 /****************************************************************************
490 do a REG Query Key Security 
491 ****************************************************************************/
492 BOOL do_reg_get_key_sec(struct cli_state *cli, uint16 fnum, POLICY_HND *hnd,
493                                 uint32 *sec_buf_size, SEC_DESC_BUF *sec_buf)
494 {
495         prs_struct rbuf;
496         prs_struct buf; 
497         REG_Q_GET_KEY_SEC q_o;
498         BOOL valid_query = False;
499
500         if (hnd == NULL) return False;
501
502         prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
503         prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );
504
505         /* create and send a MSRPC command with api REG_GET_KEY_SEC */
506
507         DEBUG(4,("REG query key security.  buf_size: %d\n", *sec_buf_size));
508
509         make_reg_q_get_key_sec(&q_o, hnd, *sec_buf_size, sec_buf);
510
511         /* turn parameters into data stream */
512         reg_io_q_get_key_sec("", &q_o, &buf, 0);
513
514         /* send the data on \PIPE\ */
515         if (rpc_api_pipe_req(cli, fnum, REG_GET_KEY_SEC, &buf, &rbuf))
516         {
517                 REG_R_GET_KEY_SEC r_o;
518                 BOOL p;
519
520                 ZERO_STRUCT(r_o);
521
522                 r_o.data = sec_buf;
523                 if (*sec_buf_size != 0)
524                 {
525                         sec_buf->sec = (SEC_DESC*)malloc(*sec_buf_size);
526                 }
527                 reg_io_r_get_key_sec("", &r_o, &rbuf, 0);
528                 p = rbuf.offset != 0;
529
530                 if (p && r_o.status == 0x0000007a)
531                 {
532                         /*
533                          * get the maximum buffer size: it was too small
534                          */
535                         (*sec_buf_size) = r_o.hdr_sec.buf_max_len;
536                         DEBUG(5,("sec_buf_size too small.  use %d\n", *sec_buf_size));
537                         valid_query = True;
538                 }
539                 else if (p && r_o.status != 0)
540                 {
541                         /* report error code */
542                         DEBUG(0,("REG_GET_KEY_SEC: %s\n", get_nt_error_msg(r_o.status)));
543                         p = False;
544                 }
545                 else
546                 {
547                         valid_query = True;
548                         (*sec_buf_size) = r_o.data->len;
549                 }
550         }
551
552         prs_mem_free(&rbuf);
553         prs_mem_free(&buf );
554
555         return valid_query;
556 }
557
558 /****************************************************************************
559 do a REG Delete Value
560 ****************************************************************************/
561 BOOL do_reg_delete_val(struct cli_state *cli, uint16 fnum, POLICY_HND *hnd, char *val_name)
562 {
563         prs_struct rbuf;
564         prs_struct buf; 
565         REG_Q_DELETE_VALUE q_o;
566         BOOL valid_delete = False;
567
568         if (hnd == NULL) return False;
569
570         prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
571         prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );
572
573         /* create and send a MSRPC command with api REG_DELETE_VALUE */
574
575         DEBUG(4,("REG Delete Value: %s\n", val_name));
576
577         make_reg_q_delete_val(&q_o, hnd, val_name);
578
579         /* turn parameters into data stream */
580         reg_io_q_delete_val("", &q_o, &buf, 0);
581
582         /* send the data on \PIPE\ */
583         if (rpc_api_pipe_req(cli, fnum, REG_DELETE_VALUE, &buf, &rbuf))
584         {
585                 REG_R_DELETE_VALUE r_o;
586                 BOOL p;
587
588                 ZERO_STRUCT(r_o);
589
590                 reg_io_r_delete_val("", &r_o, &rbuf, 0);
591                 p = rbuf.offset != 0;
592
593                 if (p && r_o.status != 0)
594                 {
595                         /* report error code */
596                         DEBUG(0,("REG_DELETE_VALUE: %s\n", get_nt_error_msg(r_o.status)));
597                         p = False;
598                 }
599
600                 if (p)
601                 {
602                         valid_delete = True;
603                 }
604         }
605
606         prs_mem_free(&rbuf);
607         prs_mem_free(&buf );
608
609         return valid_delete;
610 }
611
612 /****************************************************************************
613 do a REG Delete Key
614 ****************************************************************************/
615 BOOL do_reg_delete_key(struct cli_state *cli, uint16 fnum, POLICY_HND *hnd, char *key_name)
616 {
617         prs_struct rbuf;
618         prs_struct buf; 
619         REG_Q_DELETE_KEY q_o;
620         BOOL valid_delete = False;
621
622         if (hnd == NULL) return False;
623
624         prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
625         prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );
626
627         /* create and send a MSRPC command with api REG_DELETE_KEY */
628
629         DEBUG(4,("REG Delete Key: %s\n", key_name));
630
631         make_reg_q_delete_key(&q_o, hnd, key_name);
632
633         /* turn parameters into data stream */
634         reg_io_q_delete_key("", &q_o, &buf, 0);
635
636         /* send the data on \PIPE\ */
637         if (rpc_api_pipe_req(cli, fnum, REG_DELETE_KEY, &buf, &rbuf))
638         {
639                 REG_R_DELETE_KEY r_o;
640                 BOOL p;
641
642                 ZERO_STRUCT(r_o);
643
644                 reg_io_r_delete_key("", &r_o, &rbuf, 0);
645                 p = rbuf.offset != 0;
646
647                 if (p && r_o.status != 0)
648                 {
649                         /* report error code */
650                         DEBUG(0,("REG_DELETE_KEY: %s\n", get_nt_error_msg(r_o.status)));
651                         p = False;
652                 }
653
654                 if (p)
655                 {
656                         valid_delete = True;
657                 }
658         }
659
660         prs_mem_free(&rbuf);
661         prs_mem_free(&buf );
662
663         return valid_delete;
664 }
665
666 /****************************************************************************
667 do a REG Create Key
668 ****************************************************************************/
669 BOOL do_reg_create_key(struct cli_state *cli, uint16 fnum, POLICY_HND *hnd,
670                                 char *key_name, char *key_class,
671                                 SEC_ACCESS *sam_access,
672                                 POLICY_HND *key)
673 {
674         prs_struct rbuf;
675         prs_struct buf; 
676         REG_Q_CREATE_KEY q_o;
677         BOOL valid_create = False;
678         SEC_DESC sec;
679         SEC_DESC_BUF sec_buf;
680         int sec_len;
681
682         ZERO_STRUCT(sec);
683         ZERO_STRUCT(sec_buf);
684         ZERO_STRUCT(q_o);
685
686         if (hnd == NULL) return False;
687
688         prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
689         prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );
690
691         /* create and send a MSRPC command with api REG_CREATE_KEY */
692
693         DEBUG(4,("REG Create Key: %s %s 0x%08x\n", key_name, key_class,
694                 sam_access != NULL ? sam_access->mask : 0));
695
696         sec_len = make_sec_desc(&sec, 1, SEC_DESC_SELF_RELATIVE,
697                                 NULL, NULL, NULL, NULL);
698
699         DEBUG(10,("make_sec_desc: len = %d\n", sec_len));
700
701         make_reg_q_create_key(&q_o, hnd, key_name, key_class, sam_access,
702                               &sec_buf, sec_len, &sec);
703
704         /* turn parameters into data stream */
705         reg_io_q_create_key("", &q_o, &buf, 0);
706
707         /* send the data on \PIPE\ */
708         if (rpc_api_pipe_req(cli, fnum, REG_CREATE_KEY, &buf, &rbuf))
709         {
710                 REG_R_CREATE_KEY r_o;
711                 BOOL p;
712
713                 ZERO_STRUCT(r_o);
714
715                 reg_io_r_create_key("", &r_o, &rbuf, 0);
716                 p = rbuf.offset != 0;
717
718                 if (p && r_o.status != 0)
719                 {
720                         /* report error code */
721                         DEBUG(0,("REG_CREATE_KEY: %s\n", get_nt_error_msg(r_o.status)));
722                         p = False;
723                 }
724
725                 if (p)
726                 {
727                         valid_create = True;
728                         memcpy(key, r_o.key_pol.data, sizeof(key->data));
729                 }
730         }
731
732         free_sec_desc(&sec);
733
734         prs_mem_free(&rbuf);
735         prs_mem_free(&buf );
736
737         return valid_create;
738 }
739
740 /****************************************************************************
741 do a REG Enum Key
742 ****************************************************************************/
743 BOOL do_reg_enum_key(struct cli_state *cli, uint16 fnum, POLICY_HND *hnd,
744                                 int key_index, char *key_name,
745                                 uint32 *unk_1, uint32 *unk_2,
746                                 time_t *mod_time)
747 {
748         prs_struct rbuf;
749         prs_struct buf; 
750         REG_Q_ENUM_KEY q_o;
751         BOOL valid_query = False;
752
753         if (hnd == NULL) return False;
754
755         prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
756         prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );
757
758         /* create and send a MSRPC command with api REG_ENUM_KEY */
759
760         DEBUG(4,("REG Enum Key\n"));
761
762         make_reg_q_enum_key(&q_o, hnd, key_index);
763
764         /* turn parameters into data stream */
765         reg_io_q_enum_key("", &q_o, &buf, 0);
766
767         /* send the data on \PIPE\ */
768         if (rpc_api_pipe_req(cli, fnum, REG_ENUM_KEY, &buf, &rbuf))
769         {
770                 REG_R_ENUM_KEY r_o;
771                 BOOL p;
772
773                 ZERO_STRUCT(r_o);
774
775                 reg_io_r_enum_key("", &r_o, &rbuf, 0);
776                 p = rbuf.offset != 0;
777
778                 if (p && r_o.status != 0)
779                 {
780                         /* report error code */
781                         DEBUG(0,("REG_ENUM_KEY: %s\n", get_nt_error_msg(r_o.status)));
782                         p = False;
783                 }
784
785                 if (p)
786                 {
787                         valid_query = True;
788                         (*unk_1) = r_o.unknown_1;
789                         (*unk_2) = r_o.unknown_2;
790                         unistr_to_ascii(key_name, r_o.key_name.str.buffer,
791                                         sizeof(fstring)-1);
792                         (*mod_time) = nt_time_to_unix(&r_o.time);
793                 }
794         }
795
796         prs_mem_free(&rbuf);
797         prs_mem_free(&buf );
798
799         return valid_query;
800 }
801
802 /****************************************************************************
803 do a REG Create Value
804 ****************************************************************************/
805 BOOL do_reg_create_val(struct cli_state *cli, uint16 fnum, POLICY_HND *hnd,
806                                 char *val_name, uint32 type, BUFFER3 *data)
807 {
808         prs_struct rbuf;
809         prs_struct buf; 
810         REG_Q_CREATE_VALUE q_o;
811         BOOL valid_create = False;
812
813         if (hnd == NULL) return False;
814
815         prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
816         prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );
817
818         /* create and send a MSRPC command with api REG_CREATE_VALUE */
819
820         DEBUG(4,("REG Create Value: %s\n", val_name));
821
822         make_reg_q_create_val(&q_o, hnd, val_name, type, data);
823
824         /* turn parameters into data stream */
825         reg_io_q_create_val("", &q_o, &buf, 0);
826
827         /* send the data on \PIPE\ */
828         if (rpc_api_pipe_req(cli, fnum, REG_CREATE_VALUE, &buf, &rbuf))
829         {
830                 REG_R_CREATE_VALUE r_o;
831                 BOOL p;
832
833                 ZERO_STRUCT(r_o);
834
835                 reg_io_r_create_val("", &r_o, &rbuf, 0);
836                 p = rbuf.offset != 0;
837
838                 if (p && r_o.status != 0)
839                 {
840                         /* report error code */
841                         DEBUG(0,("REG_CREATE_VALUE: %s\n", get_nt_error_msg(r_o.status)));
842                         p = False;
843                 }
844
845                 if (p)
846                 {
847                         valid_create = True;
848                 }
849         }
850
851         prs_mem_free(&rbuf);
852         prs_mem_free(&buf );
853
854         return valid_create;
855 }
856
857 /****************************************************************************
858 do a REG Enum Value
859 ****************************************************************************/
860 BOOL do_reg_enum_val(struct cli_state *cli, uint16 fnum, POLICY_HND *hnd,
861                                 int val_index, int max_valnamelen, int max_valbufsize,
862                                 fstring val_name,
863                                 uint32 *val_type, BUFFER2 *value)
864 {
865         prs_struct rbuf;
866         prs_struct buf; 
867         REG_Q_ENUM_VALUE q_o;
868         BOOL valid_query = False;
869
870         if (hnd == NULL) return False;
871
872         prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
873         prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );
874
875         /* create and send a MSRPC command with api REG_ENUM_VALUE */
876
877         DEBUG(4,("REG Enum Value\n"));
878
879         make_reg_q_enum_val(&q_o, hnd, val_index, max_valnamelen, max_valbufsize);
880
881         /* turn parameters into data stream */
882         reg_io_q_enum_val("", &q_o, &buf, 0);
883
884         /* send the data on \PIPE\ */
885         if (rpc_api_pipe_req(cli, fnum, REG_ENUM_VALUE, &buf, &rbuf))
886         {
887                 REG_R_ENUM_VALUE r_o;
888                 BOOL p;
889
890                 ZERO_STRUCT(r_o);
891                 r_o.buf_value = value;
892
893                 reg_io_r_enum_val("", &r_o, &rbuf, 0);
894                 p = rbuf.offset != 0;
895
896                 if (p && r_o.status != 0)
897                 {
898                         /* report error code */
899                         DEBUG(0,("REG_ENUM_VALUE: %s\n", get_nt_error_msg(r_o.status)));
900                         p = False;
901                 }
902
903                 if (p)
904                 {
905                         valid_query = True;
906                         (*val_type) = r_o.type;
907                         unistr2_to_ascii(val_name, &r_o.uni_name, sizeof(fstring)-1);
908                 }
909         }
910
911         prs_mem_free(&rbuf);
912         prs_mem_free(&buf );
913
914         return valid_query;
915 }
916
917 /****************************************************************************
918 do a REG Open Key
919 ****************************************************************************/
920 BOOL do_reg_open_entry(struct cli_state *cli, uint16 fnum, POLICY_HND *hnd,
921                                 char *key_name, uint32 unk_0,
922                                 POLICY_HND *key_hnd)
923 {
924         prs_struct rbuf;
925         prs_struct buf; 
926         REG_Q_OPEN_ENTRY q_o;
927         BOOL valid_pol = False;
928
929         if (hnd == NULL) return False;
930
931         prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
932         prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );
933
934         /* create and send a MSRPC command with api REG_OPEN_ENTRY */
935
936         DEBUG(4,("REG Open Entry\n"));
937
938         make_reg_q_open_entry(&q_o, hnd, key_name, unk_0);
939
940         /* turn parameters into data stream */
941         reg_io_q_open_entry("", &q_o, &buf, 0);
942
943         /* send the data on \PIPE\ */
944         if (rpc_api_pipe_req(cli, fnum, REG_OPEN_ENTRY, &buf, &rbuf))
945         {
946                 REG_R_OPEN_ENTRY r_o;
947                 BOOL p;
948
949                 ZERO_STRUCT(r_o);
950
951                 reg_io_r_open_entry("", &r_o, &rbuf, 0);
952                 p = rbuf.offset != 0;
953
954                 if (p && r_o.status != 0)
955                 {
956                         /* report error code */
957                         DEBUG(0,("REG_OPEN_ENTRY: %s\n", get_nt_error_msg(r_o.status)));
958                         p = False;
959                 }
960
961                 if (p)
962                 {
963                         valid_pol = True;
964                         memcpy(key_hnd, r_o.pol.data, sizeof(key_hnd->data));
965                 }
966         }
967
968         prs_mem_free(&rbuf);
969         prs_mem_free(&buf );
970
971         return valid_pol;
972 }
973
974 /****************************************************************************
975 do a REG Close
976 ****************************************************************************/
977 BOOL do_reg_close(struct cli_state *cli, uint16 fnum, POLICY_HND *hnd)
978 {
979         prs_struct rbuf;
980         prs_struct buf; 
981         REG_Q_CLOSE q_c;
982         BOOL valid_close = False;
983
984         if (hnd == NULL) return False;
985
986         /* create and send a MSRPC command with api REG_CLOSE */
987
988         prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
989         prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );
990
991         DEBUG(4,("REG Close\n"));
992
993         /* store the parameters */
994         make_reg_q_close(&q_c, hnd);
995
996         /* turn parameters into data stream */
997         reg_io_q_close("", &q_c, &buf, 0);
998
999         /* send the data on \PIPE\ */
1000         if (rpc_api_pipe_req(cli, fnum, REG_CLOSE, &buf, &rbuf))
1001         {
1002                 REG_R_CLOSE r_c;
1003                 BOOL p;
1004
1005                 ZERO_STRUCT(r_c);
1006
1007                 reg_io_r_close("", &r_c, &rbuf, 0);
1008                 p = rbuf.offset != 0;
1009
1010                 if (p && r_c.status != 0)
1011                 {
1012                         /* report error code */
1013                         DEBUG(0,("REG_CLOSE: %s\n", get_nt_error_msg(r_c.status)));
1014                         p = False;
1015                 }
1016
1017                 if (p)
1018                 {
1019                         /* check that the returned policy handle is all zeros */
1020                         int i;
1021                         valid_close = True;
1022
1023                         for (i = 0; i < sizeof(r_c.pol.data); i++)
1024                         {
1025                                 if (r_c.pol.data[i] != 0)
1026                                 {
1027                                         valid_close = False;
1028                                         break;
1029                                 }
1030                         }       
1031                         if (!valid_close)
1032                         {
1033                                 DEBUG(0,("REG_CLOSE: non-zero handle returned\n"));
1034                         }
1035                 }
1036         }
1037
1038         prs_mem_free(&rbuf);
1039         prs_mem_free(&buf );
1040
1041         return valid_close;
1042 }
1043
1044 /****************************************************************************
1045 do a REG Shutdown Server
1046 ****************************************************************************/
1047 BOOL do_reg_shutdown(struct cli_state *cli, uint16 fnum, 
1048                                 char *msg, uint32 timeout, uint16 flags)
1049 {
1050         prs_struct rbuf;
1051         prs_struct buf; 
1052         REG_Q_SHUTDOWN q_o;
1053         BOOL valid_shutdown = False;
1054
1055         if (msg == NULL) return False;
1056
1057         prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
1058         prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );
1059
1060         /* create and send a MSRPC command with api REG_SHUTDOWN */
1061
1062         DEBUG(4,("REG Shutdown: (timeout: %d secs) %s\n", timeout, msg));
1063
1064         make_reg_q_shutdown(&q_o, msg, timeout, flags);
1065
1066         /* turn parameters into data stream */
1067         reg_io_q_shutdown("", &q_o, &buf, 0);
1068
1069         /* send the data on \PIPE\ */
1070         if (rpc_api_pipe_req(cli, fnum, REG_SHUTDOWN, &buf, &rbuf))
1071         {
1072                 REG_R_SHUTDOWN r_o;
1073                 BOOL p;
1074
1075                 ZERO_STRUCT(r_o);
1076
1077                 reg_io_r_shutdown("", &r_o, &rbuf, 0);
1078                 p = rbuf.offset != 0;
1079
1080                 if (p && r_o.status != 0)
1081                 {
1082                         /* report error code */
1083                         DEBUG(0,("REG_SHUTDOWN: %s\n", get_nt_error_msg(r_o.status)));
1084                         p = False;
1085                 }
1086
1087                 if (p)
1088                 {
1089                         valid_shutdown = True;
1090                 }
1091         }
1092
1093         prs_mem_free(&rbuf);
1094         prs_mem_free(&buf );
1095
1096         return valid_shutdown;
1097 }
1098
1099