3 * Unix SMB/Netbios implementation.
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.
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.
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.
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.
32 extern int DEBUGLEVEL;
34 /****************************************************************************
36 ****************************************************************************/
37 BOOL do_reg_connect(struct cli_state *cli, char *full_keyname, char *key_name,
43 if (full_keyname == NULL)
48 ZERO_STRUCTP(reg_hnd);
51 * open registry receive a policy handle
54 if (!reg_split_key(full_keyname, ®_type, key_name))
56 DEBUG(0,("do_reg_connect: unrecognised key name %s\n", full_keyname));
62 case HKEY_LOCAL_MACHINE:
64 res = res ? do_reg_open_hklm(cli,
72 res = res ? do_reg_open_hku(cli,
79 DEBUG(0,("do_reg_connect: unrecognised hive key\n"));
87 /****************************************************************************
89 ****************************************************************************/
90 BOOL do_reg_open_hklm(struct cli_state *cli, uint16 unknown_0, uint32 level,
96 BOOL valid_pol = False;
98 if (hnd == NULL) return False;
100 prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
101 prs_init(&rbuf, 0 , 4, SAFETY_MARGIN, True );
103 /* create and send a MSRPC command with api REG_OPEN_HKLM */
105 DEBUG(4,("REG Open HKLM\n"));
107 make_reg_q_open_hklm(&q_o, unknown_0, level);
109 /* turn parameters into data stream */
110 reg_io_q_open_hklm("", &q_o, &buf, 0);
112 /* send the data on \PIPE\ */
113 if (rpc_api_pipe_req(cli, REG_OPEN_HKLM, &buf, &rbuf))
120 reg_io_r_open_hklm("", &r_o, &rbuf, 0);
121 p = rbuf.offset != 0;
123 if (p && r_o.status != 0)
125 /* report error code */
126 DEBUG(0,("REG_OPEN_HKLM: %s\n", get_nt_error_msg(r_o.status)));
132 /* ok, at last: we're happy. return the policy handle */
133 memcpy(hnd, r_o.pol.data, sizeof(hnd->data));
144 /****************************************************************************
146 ****************************************************************************/
147 BOOL do_reg_open_hku(struct cli_state *cli, uint16 unknown_0, uint32 level,
153 BOOL valid_pol = False;
155 if (hnd == NULL) return False;
157 prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
158 prs_init(&rbuf, 0 , 4, SAFETY_MARGIN, True );
160 /* create and send a MSRPC command with api REG_OPEN_HKU */
162 DEBUG(4,("REG Open HKU\n"));
164 make_reg_q_open_hku(&q_o, unknown_0, level);
166 /* turn parameters into data stream */
167 reg_io_q_open_hku("", &q_o, &buf, 0);
169 /* send the data on \PIPE\ */
170 if (rpc_api_pipe_req(cli, REG_OPEN_HKU, &buf, &rbuf))
177 reg_io_r_open_hku("", &r_o, &rbuf, 0);
178 p = rbuf.offset != 0;
180 if (p && r_o.status != 0)
182 /* report error code */
183 DEBUG(0,("REG_OPEN_HKU: %s\n", get_nt_error_msg(r_o.status)));
189 /* ok, at last: we're happy. return the policy handle */
190 memcpy(hnd, r_o.pol.data, sizeof(hnd->data));
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)
211 BOOL valid_query = False;
213 if (hnd == NULL) return False;
215 prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
216 prs_init(&rbuf, 0 , 4, SAFETY_MARGIN, True );
218 /* create and send a MSRPC command with api REG_FLUSH_KEY */
220 DEBUG(4,("REG Unknown 0xB\n"));
222 make_reg_q_flush_key(&q_o, hnd);
224 /* turn parameters into data stream */
225 reg_io_q_flush_key("", &q_o, &buf, 0);
227 /* send the data on \PIPE\ */
228 if (rpc_api_pipe_req(cli, REG_FLUSH_KEY, &buf, &rbuf))
235 reg_io_r_flush_key("", &r_o, &rbuf, 0);
236 p = rbuf.offset != 0;
238 if (p && r_o.status != 0)
240 /* report error code */
241 DEBUG(0,("REG_FLUSH_KEY: %s\n", get_nt_error_msg(r_o.status)));
257 /****************************************************************************
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)
270 BOOL valid_query = False;
272 if (hnd == NULL) return False;
274 prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
275 prs_init(&rbuf, 0 , 4, SAFETY_MARGIN, True );
277 /* create and send a MSRPC command with api REG_QUERY_KEY */
279 DEBUG(4,("REG Query Key\n"));
281 make_reg_q_query_key(&q_o, hnd, *class_len);
283 /* turn parameters into data stream */
284 reg_io_q_query_key("", &q_o, &buf, 0);
286 /* send the data on \PIPE\ */
287 if (rpc_api_pipe_req(cli, REG_QUERY_KEY, &buf, &rbuf))
294 reg_io_r_query_key("", &r_o, &rbuf, 0);
295 p = rbuf.offset != 0;
297 if (p && r_o.status != 0)
299 /* report error code */
300 DEBUG(0,("REG_QUERY_KEY: %s\n", get_nt_error_msg(r_o.status)));
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 ;
327 /****************************************************************************
329 ****************************************************************************/
330 BOOL do_reg_unknown_1a(struct cli_state *cli, POLICY_HND *hnd, uint32 *unk)
335 BOOL valid_query = False;
337 if (hnd == NULL) return False;
339 prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
340 prs_init(&rbuf, 0 , 4, SAFETY_MARGIN, True );
342 /* create and send a MSRPC command with api REG_UNKNOWN_1A */
344 DEBUG(4,("REG Unknown 1a\n"));
346 make_reg_q_unk_1a(&q_o, hnd);
348 /* turn parameters into data stream */
349 reg_io_q_unk_1a("", &q_o, &buf, 0);
351 /* send the data on \PIPE\ */
352 if (rpc_api_pipe_req(cli, REG_UNK_1A, &buf, &rbuf))
359 reg_io_r_unk_1a("", &r_o, &rbuf, 0);
360 p = rbuf.offset != 0;
362 if (p && r_o.status != 0)
364 /* report error code */
365 DEBUG(0,("REG_UNK_1A: %s\n", get_nt_error_msg(r_o.status)));
372 (*unk) = r_o.unknown;
382 /****************************************************************************
384 ****************************************************************************/
385 BOOL do_reg_query_info(struct cli_state *cli, POLICY_HND *hnd,
386 char *type, uint32 *unk_0, uint32 *unk_1)
391 BOOL valid_query = False;
393 if (hnd == NULL) return False;
395 prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
396 prs_init(&rbuf, 0 , 4, SAFETY_MARGIN, True );
398 /* create and send a MSRPC command with api REG_INFO */
400 DEBUG(4,("REG Query Info\n"));
402 make_reg_q_info(&q_o, hnd, "ProductType", time(NULL), 4, 1);
404 /* turn parameters into data stream */
405 reg_io_q_info("", &q_o, &buf, 0);
407 /* send the data on \PIPE\ */
408 if (rpc_api_pipe_req(cli, REG_INFO, &buf, &rbuf))
415 reg_io_r_info("", &r_o, &rbuf, 0);
416 p = rbuf.offset != 0;
418 if (p && r_o.status != 0)
420 /* report error code */
421 DEBUG(0,("REG_INFO: %s\n", get_nt_error_msg(r_o.status)));
428 fstrcpy(type, buffer2_to_str(&r_o.uni_type));
429 (*unk_0) = r_o.unknown_0;
430 (*unk_1) = r_o.unknown_1;
440 /****************************************************************************
441 do a REG Set Key Security
442 ****************************************************************************/
443 BOOL do_reg_set_key_sec(struct cli_state *cli, POLICY_HND *hnd,
444 uint32 sec_buf_size, SEC_DESC *sec_buf)
448 REG_Q_SET_KEY_SEC q_o;
449 BOOL valid_query = False;
451 if (hnd == NULL) return False;
453 prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
454 prs_init(&rbuf, 0 , 4, SAFETY_MARGIN, True );
456 /* create and send a MSRPC command with api REG_SET_KEY_SEC */
458 DEBUG(4,("REG Set Key security.\n"));
460 make_reg_q_set_key_sec(&q_o, hnd, sec_buf_size, sec_buf);
462 /* turn parameters into data stream */
463 reg_io_q_set_key_sec("", &q_o, &buf, 0);
465 /* send the data on \PIPE\ */
466 if (rpc_api_pipe_req(cli, REG_SET_KEY_SEC, &buf, &rbuf))
468 REG_R_SET_KEY_SEC r_o;
473 reg_io_r_set_key_sec("", &r_o, &rbuf, 0);
474 p = rbuf.offset != 0;
476 if (p && r_o.status != 0)
488 /****************************************************************************
489 do a REG Query Key Security
490 ****************************************************************************/
491 BOOL do_reg_get_key_sec(struct cli_state *cli, POLICY_HND *hnd,
492 uint32 *sec_buf_size, SEC_DESC_BUF *sec_buf)
496 REG_Q_GET_KEY_SEC q_o;
497 BOOL valid_query = False;
499 if (hnd == NULL) return False;
501 prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
502 prs_init(&rbuf, 0 , 4, SAFETY_MARGIN, True );
504 /* create and send a MSRPC command with api REG_GET_KEY_SEC */
506 DEBUG(4,("REG query key security. buf_size: %d\n", *sec_buf_size));
508 make_reg_q_get_key_sec(&q_o, hnd, *sec_buf_size, sec_buf);
510 /* turn parameters into data stream */
511 reg_io_q_get_key_sec("", &q_o, &buf, 0);
513 /* send the data on \PIPE\ */
514 if (rpc_api_pipe_req(cli, REG_GET_KEY_SEC, &buf, &rbuf))
516 REG_R_GET_KEY_SEC r_o;
522 if (*sec_buf_size != 0)
524 sec_buf->sec = (SEC_DESC*)malloc(*sec_buf_size);
526 reg_io_r_get_key_sec("", &r_o, &rbuf, 0);
527 p = rbuf.offset != 0;
529 if (p && r_o.status == 0x0000007a)
532 * get the maximum buffer size: it was too small
534 (*sec_buf_size) = r_o.hdr_sec.buf_max_len;
535 DEBUG(5,("sec_buf_size too small. use %d\n", *sec_buf_size));
538 else if (p && r_o.status != 0)
540 /* report error code */
541 DEBUG(0,("REG_GET_KEY_SEC: %s\n", get_nt_error_msg(r_o.status)));
547 (*sec_buf_size) = r_o.data->len;
557 /****************************************************************************
558 do a REG Delete Value
559 ****************************************************************************/
560 BOOL do_reg_delete_val(struct cli_state *cli, POLICY_HND *hnd, char *val_name)
564 REG_Q_DELETE_VALUE q_o;
565 BOOL valid_delete = False;
567 if (hnd == NULL) return False;
569 prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
570 prs_init(&rbuf, 0 , 4, SAFETY_MARGIN, True );
572 /* create and send a MSRPC command with api REG_DELETE_VALUE */
574 DEBUG(4,("REG Delete Value: %s\n", val_name));
576 make_reg_q_delete_val(&q_o, hnd, val_name);
578 /* turn parameters into data stream */
579 reg_io_q_delete_val("", &q_o, &buf, 0);
581 /* send the data on \PIPE\ */
582 if (rpc_api_pipe_req(cli, REG_DELETE_VALUE, &buf, &rbuf))
584 REG_R_DELETE_VALUE r_o;
589 reg_io_r_delete_val("", &r_o, &rbuf, 0);
590 p = rbuf.offset != 0;
592 if (p && r_o.status != 0)
594 /* report error code */
595 DEBUG(0,("REG_DELETE_VALUE: %s\n", get_nt_error_msg(r_o.status)));
611 /****************************************************************************
613 ****************************************************************************/
614 BOOL do_reg_delete_key(struct cli_state *cli, POLICY_HND *hnd, char *key_name)
618 REG_Q_DELETE_KEY q_o;
619 BOOL valid_delete = False;
621 if (hnd == NULL) return False;
623 prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
624 prs_init(&rbuf, 0 , 4, SAFETY_MARGIN, True );
626 /* create and send a MSRPC command with api REG_DELETE_KEY */
628 DEBUG(4,("REG Delete Key: %s\n", key_name));
630 make_reg_q_delete_key(&q_o, hnd, key_name);
632 /* turn parameters into data stream */
633 reg_io_q_delete_key("", &q_o, &buf, 0);
635 /* send the data on \PIPE\ */
636 if (rpc_api_pipe_req(cli, REG_DELETE_KEY, &buf, &rbuf))
638 REG_R_DELETE_KEY r_o;
643 reg_io_r_delete_key("", &r_o, &rbuf, 0);
644 p = rbuf.offset != 0;
646 if (p && r_o.status != 0)
648 /* report error code */
649 DEBUG(0,("REG_DELETE_KEY: %s\n", get_nt_error_msg(r_o.status)));
665 /****************************************************************************
667 ****************************************************************************/
668 BOOL do_reg_create_key(struct cli_state *cli, POLICY_HND *hnd,
669 char *key_name, char *key_class,
670 SEC_INFO *sam_access,
675 REG_Q_CREATE_KEY q_o;
676 BOOL valid_create = False;
678 if (hnd == NULL) return False;
680 prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
681 prs_init(&rbuf, 0 , 4, SAFETY_MARGIN, True );
683 /* create and send a MSRPC command with api REG_CREATE_KEY */
685 DEBUG(4,("REG Create Key: %s %s 0x%08x\n", key_name, key_class,
686 sam_access != NULL ? sam_access->perms : 0));
688 make_reg_q_create_key(&q_o, hnd, key_name, key_class, sam_access);
690 /* turn parameters into data stream */
691 reg_io_q_create_key("", &q_o, &buf, 0);
693 /* send the data on \PIPE\ */
694 if (rpc_api_pipe_req(cli, REG_CREATE_KEY, &buf, &rbuf))
696 REG_R_CREATE_KEY r_o;
701 reg_io_r_create_key("", &r_o, &rbuf, 0);
702 p = rbuf.offset != 0;
704 if (p && r_o.status != 0)
706 /* report error code */
707 DEBUG(0,("REG_CREATE_KEY: %s\n", get_nt_error_msg(r_o.status)));
714 memcpy(key, r_o.key_pol.data, sizeof(key->data));
724 /****************************************************************************
726 ****************************************************************************/
727 BOOL do_reg_enum_key(struct cli_state *cli, POLICY_HND *hnd,
728 int key_index, char *key_name,
729 uint32 *unk_1, uint32 *unk_2,
735 BOOL valid_query = False;
737 if (hnd == NULL) return False;
739 prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
740 prs_init(&rbuf, 0 , 4, SAFETY_MARGIN, True );
742 /* create and send a MSRPC command with api REG_ENUM_KEY */
744 DEBUG(4,("REG Enum Key\n"));
746 make_reg_q_enum_key(&q_o, hnd, key_index);
748 /* turn parameters into data stream */
749 reg_io_q_enum_key("", &q_o, &buf, 0);
751 /* send the data on \PIPE\ */
752 if (rpc_api_pipe_req(cli, REG_ENUM_KEY, &buf, &rbuf))
759 reg_io_r_enum_key("", &r_o, &rbuf, 0);
760 p = rbuf.offset != 0;
762 if (p && r_o.status != 0)
764 /* report error code */
765 DEBUG(0,("REG_ENUM_KEY: %s\n", get_nt_error_msg(r_o.status)));
772 (*unk_1) = r_o.unknown_1;
773 (*unk_2) = r_o.unknown_2;
774 fstrcpy(key_name, unistr2(r_o.key_name.str.buffer));
775 (*mod_time) = nt_time_to_unix(&r_o.time);
785 /****************************************************************************
786 do a REG Create Value
787 ****************************************************************************/
788 BOOL do_reg_create_val(struct cli_state *cli, POLICY_HND *hnd,
789 char *val_name, uint32 type, BUFFER3 *data)
793 REG_Q_CREATE_VALUE q_o;
794 BOOL valid_create = False;
796 if (hnd == NULL) return False;
798 prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
799 prs_init(&rbuf, 0 , 4, SAFETY_MARGIN, True );
801 /* create and send a MSRPC command with api REG_CREATE_VALUE */
803 DEBUG(4,("REG Create Value: %s\n", val_name));
805 make_reg_q_create_val(&q_o, hnd, val_name, type, data);
807 /* turn parameters into data stream */
808 reg_io_q_create_val("", &q_o, &buf, 0);
810 /* send the data on \PIPE\ */
811 if (rpc_api_pipe_req(cli, REG_CREATE_VALUE, &buf, &rbuf))
813 REG_R_CREATE_VALUE r_o;
818 reg_io_r_create_val("", &r_o, &rbuf, 0);
819 p = rbuf.offset != 0;
821 if (p && r_o.status != 0)
823 /* report error code */
824 DEBUG(0,("REG_CREATE_VALUE: %s\n", get_nt_error_msg(r_o.status)));
840 /****************************************************************************
842 ****************************************************************************/
843 BOOL do_reg_enum_val(struct cli_state *cli, POLICY_HND *hnd,
844 int val_index, int max_valnamelen, int max_valbufsize,
846 uint32 *val_type, BUFFER2 *value)
850 REG_Q_ENUM_VALUE q_o;
851 BOOL valid_query = False;
853 if (hnd == NULL) return False;
855 prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
856 prs_init(&rbuf, 0 , 4, SAFETY_MARGIN, True );
858 /* create and send a MSRPC command with api REG_ENUM_VALUE */
860 DEBUG(4,("REG Enum Value\n"));
862 make_reg_q_enum_val(&q_o, hnd, val_index, max_valnamelen, max_valbufsize);
864 /* turn parameters into data stream */
865 reg_io_q_enum_val("", &q_o, &buf, 0);
867 /* send the data on \PIPE\ */
868 if (rpc_api_pipe_req(cli, REG_ENUM_VALUE, &buf, &rbuf))
870 REG_R_ENUM_VALUE r_o;
874 r_o.buf_value = value;
876 reg_io_r_enum_val("", &r_o, &rbuf, 0);
877 p = rbuf.offset != 0;
879 if (p && r_o.status != 0)
881 /* report error code */
882 DEBUG(0,("REG_ENUM_VALUE: %s\n", get_nt_error_msg(r_o.status)));
889 (*val_type) = r_o.type;
890 fstrcpy(val_name, unistr2_to_str(&r_o.uni_name));
900 /****************************************************************************
902 ****************************************************************************/
903 BOOL do_reg_open_entry(struct cli_state *cli, POLICY_HND *hnd,
904 char *key_name, uint32 unk_0,
909 REG_Q_OPEN_ENTRY q_o;
910 BOOL valid_pol = False;
912 if (hnd == NULL) return False;
914 prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
915 prs_init(&rbuf, 0 , 4, SAFETY_MARGIN, True );
917 /* create and send a MSRPC command with api REG_OPEN_ENTRY */
919 DEBUG(4,("REG Open Entry\n"));
921 make_reg_q_open_entry(&q_o, hnd, key_name, unk_0);
923 /* turn parameters into data stream */
924 reg_io_q_open_entry("", &q_o, &buf, 0);
926 /* send the data on \PIPE\ */
927 if (rpc_api_pipe_req(cli, REG_OPEN_ENTRY, &buf, &rbuf))
929 REG_R_OPEN_ENTRY r_o;
934 reg_io_r_open_entry("", &r_o, &rbuf, 0);
935 p = rbuf.offset != 0;
937 if (p && r_o.status != 0)
939 /* report error code */
940 DEBUG(0,("REG_OPEN_ENTRY: %s\n", get_nt_error_msg(r_o.status)));
947 memcpy(key_hnd, r_o.pol.data, sizeof(key_hnd->data));
957 /****************************************************************************
959 ****************************************************************************/
960 BOOL do_reg_close(struct cli_state *cli, POLICY_HND *hnd)
965 BOOL valid_close = False;
967 if (hnd == NULL) return False;
969 /* create and send a MSRPC command with api REG_CLOSE */
971 prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
972 prs_init(&rbuf, 0 , 4, SAFETY_MARGIN, True );
974 DEBUG(4,("REG Close\n"));
976 /* store the parameters */
977 make_reg_q_close(&q_c, hnd);
979 /* turn parameters into data stream */
980 reg_io_q_close("", &q_c, &buf, 0);
982 /* send the data on \PIPE\ */
983 if (rpc_api_pipe_req(cli, REG_CLOSE, &buf, &rbuf))
990 reg_io_r_close("", &r_c, &rbuf, 0);
991 p = rbuf.offset != 0;
993 if (p && r_c.status != 0)
995 /* report error code */
996 DEBUG(0,("REG_CLOSE: %s\n", get_nt_error_msg(r_c.status)));
1002 /* check that the returned policy handle is all zeros */
1006 for (i = 0; i < sizeof(r_c.pol.data); i++)
1008 if (r_c.pol.data[i] != 0)
1010 valid_close = False;
1016 DEBUG(0,("REG_CLOSE: non-zero handle returned\n"));
1021 prs_mem_free(&rbuf);
1022 prs_mem_free(&buf );