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.
9 * Copyright (C) Jeremy Allison 1999.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33 extern int DEBUGLEVEL;
35 /****************************************************************************
37 ****************************************************************************/
38 BOOL do_reg_connect(struct cli_state *cli, char *full_keyname, char *key_name,
44 if (full_keyname == NULL)
47 ZERO_STRUCTP(reg_hnd);
50 * open registry receive a policy handle
53 if (!reg_split_key(full_keyname, ®_type, key_name)) {
54 DEBUG(0,("do_reg_connect: unrecognised key name %s\n", full_keyname));
59 case HKEY_LOCAL_MACHINE:
60 res = res ? do_reg_open_hklm(cli, 0x84E0, 0x02000000, reg_hnd) : False;
64 res = res ? do_reg_open_hku(cli, 0x84E0, 0x02000000, reg_hnd) : False;
68 DEBUG(0,("do_reg_connect: unrecognised hive key\n"));
75 /****************************************************************************
77 ****************************************************************************/
78 BOOL do_reg_open_hklm(struct cli_state *cli, uint16 unknown_0, uint32 level,
89 prs_init(&buf , MAX_PDU_FRAG_LEN, 4, MARSHALL);
90 prs_init(&rbuf, 0, 4, UNMARSHALL);
92 /* create and send a MSRPC command with api REG_OPEN_HKLM */
94 DEBUG(4,("REG Open HKLM\n"));
96 init_reg_q_open_hklm(&q_o, unknown_0, level);
98 /* turn parameters into data stream */
99 if(!reg_io_q_open_hklm("", &q_o, &buf, 0)) {
105 /* send the data on \PIPE\ */
106 if (!rpc_api_pipe_req(cli, REG_OPEN_HKLM, &buf, &rbuf)) {
116 if(!reg_io_r_open_hklm("", &r_o, &rbuf, 0)) {
121 if (r_o.status != 0) {
122 /* report error code */
123 DEBUG(0,("REG_OPEN_HKLM: %s\n", get_nt_error_msg(r_o.status)));
128 /* ok, at last: we're happy. return the policy handle */
129 memcpy(hnd, r_o.pol.data, sizeof(hnd->data));
136 /****************************************************************************
138 ****************************************************************************/
139 BOOL do_reg_open_hku(struct cli_state *cli, uint16 unknown_0, uint32 level,
150 prs_init(&buf , MAX_PDU_FRAG_LEN, 4, MARSHALL);
151 prs_init(&rbuf, 0, 4, UNMARSHALL);
153 /* create and send a MSRPC command with api REG_OPEN_HKU */
155 DEBUG(4,("REG Open HKU\n"));
157 init_reg_q_open_hku(&q_o, unknown_0, level);
159 /* turn parameters into data stream */
160 if(!reg_io_q_open_hku("", &q_o, &buf, 0)) {
166 /* send the data on \PIPE\ */
167 if (rpc_api_pipe_req(cli, REG_OPEN_HKU, &buf, &rbuf)) {
177 if(!reg_io_r_open_hku("", &r_o, &rbuf, 0)) {
182 if (r_o.status != 0) {
183 /* report error code */
184 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));
197 /****************************************************************************
198 do a REG Unknown 0xB command. sent after a create key or create value.
199 this might be some sort of "sync" or "refresh" command, sent after
200 modification of the registry...
201 ****************************************************************************/
202 BOOL do_reg_flush_key(struct cli_state *cli, POLICY_HND *hnd)
212 prs_init(&buf , MAX_PDU_FRAG_LEN, 4, MARSHALL);
213 prs_init(&rbuf, 0, 4, UNMARSHALL);
215 /* create and send a MSRPC command with api REG_FLUSH_KEY */
217 DEBUG(4,("REG Unknown 0xB\n"));
219 init_reg_q_flush_key(&q_o, hnd);
221 /* turn parameters into data stream */
222 if(!reg_io_q_flush_key("", &q_o, &buf, 0)) {
228 /* send the data on \PIPE\ */
229 if (!rpc_api_pipe_req(cli, REG_FLUSH_KEY, &buf, &rbuf)) {
239 if(!reg_io_r_flush_key("", &r_o, &rbuf, 0)) {
244 if (r_o.status != 0) {
245 /* report error code */
246 DEBUG(0,("REG_FLUSH_KEY: %s\n", get_nt_error_msg(r_o.status)));
256 /****************************************************************************
258 ****************************************************************************/
259 BOOL do_reg_query_key(struct cli_state *cli, POLICY_HND *hnd,
260 char *class, uint32 *class_len,
261 uint32 *num_subkeys, uint32 *max_subkeylen,
262 uint32 *max_subkeysize, uint32 *num_values,
263 uint32 *max_valnamelen, uint32 *max_valbufsize,
264 uint32 *sec_desc, NTTIME *mod_time)
274 prs_init(&buf , MAX_PDU_FRAG_LEN, 4, MARSHALL);
275 prs_init(&rbuf, 0, 4, UNMARSHALL);
277 /* create and send a MSRPC command with api REG_QUERY_KEY */
279 DEBUG(4,("REG Query Key\n"));
281 init_reg_q_query_key(&q_o, hnd, *class_len);
283 /* turn parameters into data stream */
284 if(!reg_io_q_query_key("", &q_o, &buf, 0)) {
290 /* send the data on \PIPE\ */
291 if (!rpc_api_pipe_req(cli, REG_QUERY_KEY, &buf, &rbuf)) {
301 if(!reg_io_r_query_key("", &r_o, &rbuf, 0)) {
306 if (r_o.status != 0) {
307 /* report error code */
308 DEBUG(0,("REG_QUERY_KEY: %s\n", get_nt_error_msg(r_o.status)));
313 *class_len = r_o.hdr_class.uni_max_len;
314 fstrcpy(class, dos_unistr2_to_str(&r_o.uni_class));
315 *num_subkeys = r_o.num_subkeys ;
316 *max_subkeylen = r_o.max_subkeylen ;
317 *max_subkeysize = r_o.max_subkeysize;
318 *num_values = r_o.num_values ;
319 *max_valnamelen = r_o.max_valnamelen;
320 *max_valbufsize = r_o.max_valbufsize;
321 *sec_desc = r_o.sec_desc ;
322 *mod_time = r_o.mod_time ;
329 /****************************************************************************
331 ****************************************************************************/
332 BOOL do_reg_unknown_1a(struct cli_state *cli, POLICY_HND *hnd, uint32 *unk)
342 prs_init(&buf , MAX_PDU_FRAG_LEN, 4, MARSHALL);
343 prs_init(&rbuf, 0, 4, UNMARSHALL);
345 /* create and send a MSRPC command with api REG_UNKNOWN_1A */
347 DEBUG(4,("REG Unknown 1a\n"));
349 init_reg_q_unk_1a(&q_o, hnd);
351 /* turn parameters into data stream */
352 if(!reg_io_q_unk_1a("", &q_o, &buf, 0)) {
358 /* send the data on \PIPE\ */
359 if (rpc_api_pipe_req(cli, REG_UNK_1A, &buf, &rbuf)) {
369 if(!reg_io_r_unk_1a("", &r_o, &rbuf, 0)) {
374 if (r_o.status != 0) {
375 /* report error code */
376 DEBUG(0,("REG_UNK_1A: %s\n", get_nt_error_msg(r_o.status)));
381 (*unk) = r_o.unknown;
388 /****************************************************************************
390 ****************************************************************************/
391 BOOL do_reg_query_info(struct cli_state *cli, POLICY_HND *hnd,
392 char *key_value, uint32* key_type)
402 prs_init(&buf , MAX_PDU_FRAG_LEN, 4, MARSHALL);
403 prs_init(&rbuf, 0, 4, UNMARSHALL);
405 /* create and send a MSRPC command with api REG_INFO */
407 DEBUG(4,("REG Query Info\n"));
409 init_reg_q_info(&q_o, hnd, "ProductType");
411 /* turn parameters into data stream */
412 if(!reg_io_q_info("", &q_o, &buf, 0)) {
418 /* send the data on \PIPE\ */
419 if (!rpc_api_pipe_req(cli, REG_INFO, &buf, &rbuf)) {
429 if(!reg_io_r_info("", &r_o, &rbuf, 0)) {
434 if ( r_o.status != 0) {
435 /* report error code */
436 DEBUG(0,("REG_INFO: %s\n", get_nt_error_msg(r_o.status)));
441 fstrcpy(key_value, dos_buffer2_to_str(r_o.uni_val));
442 *key_type = r_o.type;
449 /****************************************************************************
450 do a REG Set Key Security
451 ****************************************************************************/
452 BOOL do_reg_set_key_sec(struct cli_state *cli, POLICY_HND *hnd, SEC_DESC_BUF *sec_desc_buf)
456 REG_Q_SET_KEY_SEC q_o;
457 REG_R_SET_KEY_SEC r_o;
462 prs_init(&buf , MAX_PDU_FRAG_LEN, 4, MARSHALL);
463 prs_init(&rbuf, 0, 4, UNMARSHALL);
465 /* create and send a MSRPC command with api REG_SET_KEY_SEC */
467 DEBUG(4,("REG Set Key security.\n"));
469 init_reg_q_set_key_sec(&q_o, hnd, sec_desc_buf);
471 /* turn parameters into data stream */
472 if(!reg_io_q_set_key_sec("", &q_o, &buf, 0)) {
478 /* send the data on \PIPE\ */
479 if (!rpc_api_pipe_req(cli, REG_SET_KEY_SEC, &buf, &rbuf)) {
489 if(!reg_io_r_set_key_sec("", &r_o, &rbuf, 0)) {
494 if (r_o.status != 0) {
504 /****************************************************************************
505 do a REG Query Key Security
506 ****************************************************************************/
508 BOOL do_reg_get_key_sec(struct cli_state *cli, POLICY_HND *hnd, uint32 *sec_buf_size, SEC_DESC_BUF **ppsec_desc_buf)
512 REG_Q_GET_KEY_SEC q_o;
513 REG_R_GET_KEY_SEC r_o;
518 prs_init(&buf , MAX_PDU_FRAG_LEN, 4, MARSHALL);
519 prs_init(&rbuf, 0, 4, UNMARSHALL);
521 /* create and send a MSRPC command with api REG_GET_KEY_SEC */
523 DEBUG(4,("REG query key security. buf_size: %d\n", *sec_buf_size));
525 init_reg_q_get_key_sec(&q_o, hnd, *sec_buf_size, NULL);
527 /* turn parameters into data stream */
528 if(!reg_io_q_get_key_sec("", &q_o, &buf, 0)) {
534 /* send the data on \PIPE\ */
535 if (!rpc_api_pipe_req(cli, REG_GET_KEY_SEC, &buf, &rbuf)) {
545 if(!reg_io_r_get_key_sec("", &r_o, &rbuf, 0)) {
550 if (r_o.status == 0x0000007a) {
552 * get the maximum buffer size: it was too small
554 (*sec_buf_size) = r_o.hdr_sec.buf_max_len;
555 DEBUG(5,("sec_buf_size too small. use %d\n", *sec_buf_size));
556 } else if (r_o.status != 0) {
557 /* report error code */
558 DEBUG(0,("REG_GET_KEY_SEC: %s\n", get_nt_error_msg(r_o.status)));
562 (*sec_buf_size) = r_o.data->len;
563 *ppsec_desc_buf = r_o.data;
571 /****************************************************************************
572 do a REG Delete Value
573 ****************************************************************************/
574 BOOL do_reg_delete_val(struct cli_state *cli, POLICY_HND *hnd, char *val_name)
578 REG_Q_DELETE_VALUE q_o;
579 REG_R_DELETE_VALUE r_o;
584 prs_init(&buf , MAX_PDU_FRAG_LEN, 4, MARSHALL);
585 prs_init(&rbuf, 0, 4, UNMARSHALL);
587 /* create and send a MSRPC command with api REG_DELETE_VALUE */
589 DEBUG(4,("REG Delete Value: %s\n", val_name));
591 init_reg_q_delete_val(&q_o, hnd, val_name);
593 /* turn parameters into data stream */
594 if(!reg_io_q_delete_val("", &q_o, &buf, 0)) {
600 /* send the data on \PIPE\ */
601 if (rpc_api_pipe_req(cli, REG_DELETE_VALUE, &buf, &rbuf)) {
611 if(!reg_io_r_delete_val("", &r_o, &rbuf, 0)) {
616 if (r_o.status != 0) {
617 /* report error code */
618 DEBUG(0,("REG_DELETE_VALUE: %s\n", get_nt_error_msg(r_o.status)));
628 /****************************************************************************
630 ****************************************************************************/
631 BOOL do_reg_delete_key(struct cli_state *cli, POLICY_HND *hnd, char *key_name)
635 REG_Q_DELETE_KEY q_o;
636 REG_R_DELETE_KEY r_o;
641 prs_init(&buf , MAX_PDU_FRAG_LEN, 4, MARSHALL);
642 prs_init(&rbuf, 0, 4, UNMARSHALL);
644 /* create and send a MSRPC command with api REG_DELETE_KEY */
646 DEBUG(4,("REG Delete Key: %s\n", key_name));
648 init_reg_q_delete_key(&q_o, hnd, key_name);
650 /* turn parameters into data stream */
651 if(!reg_io_q_delete_key("", &q_o, &buf, 0)) {
657 /* send the data on \PIPE\ */
658 if (!rpc_api_pipe_req(cli, REG_DELETE_KEY, &buf, &rbuf)) {
668 if(!reg_io_r_delete_key("", &r_o, &rbuf, 0)) {
673 if (r_o.status != 0) {
674 /* report error code */
675 DEBUG(0,("REG_DELETE_KEY: %s\n", get_nt_error_msg(r_o.status)));
685 /****************************************************************************
687 ****************************************************************************/
688 BOOL do_reg_create_key(struct cli_state *cli, POLICY_HND *hnd,
689 char *key_name, char *key_class,
690 SEC_ACCESS *sam_access,
695 REG_Q_CREATE_KEY q_o;
696 REG_R_CREATE_KEY r_o;
697 SEC_DESC *sec = NULL;
698 SEC_DESC_BUF *sec_buf = NULL;
706 /* create and send a MSRPC command with api REG_CREATE_KEY */
708 DEBUG(4,("REG Create Key: %s %s 0x%08x\n", key_name, key_class,
709 sam_access != NULL ? sam_access->mask : 0));
711 if((sec = make_sec_desc( 1, SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL, NULL, &sec_len)) == NULL) {
712 DEBUG(0,("make_sec_desc : malloc fail.\n"));
716 DEBUG(10,("make_sec_desc: len = %d\n", (int)sec_len));
718 if((sec_buf = make_sec_desc_buf( (int)sec_len, sec)) == NULL) {
719 DEBUG(0,("make_sec_desc : malloc fail (1)\n"));
725 prs_init(&buf, MAX_PDU_FRAG_LEN, 4, MARSHALL);
726 prs_init(&rbuf, 0, 4, UNMARSHALL);
728 init_reg_q_create_key(&q_o, hnd, key_name, key_class, sam_access, sec_buf);
730 /* turn parameters into data stream */
731 if(!reg_io_q_create_key("", &q_o, &buf, 0)) {
732 free_sec_desc_buf(&sec_buf);
738 /* send the data on \PIPE\ */
739 if (rpc_api_pipe_req(cli, REG_CREATE_KEY, &buf, &rbuf)) {
740 free_sec_desc_buf(&sec_buf);
746 free_sec_desc_buf(&sec_buf);
751 if(!reg_io_r_create_key("", &r_o, &rbuf, 0)) {
756 if (r_o.status != 0) {
757 /* report error code */
758 DEBUG(0,("REG_CREATE_KEY: %s\n", get_nt_error_msg(r_o.status)));
763 memcpy(key, r_o.key_pol.data, sizeof(key->data));
770 /****************************************************************************
772 ****************************************************************************/
773 BOOL do_reg_enum_key(struct cli_state *cli, POLICY_HND *hnd,
774 int key_index, char *key_name,
775 uint32 *unk_1, uint32 *unk_2,
786 prs_init(&buf, MAX_PDU_FRAG_LEN, 4, MARSHALL);
787 prs_init(&rbuf, 0, 4, UNMARSHALL);
789 /* create and send a MSRPC command with api REG_ENUM_KEY */
791 DEBUG(4,("REG Enum Key\n"));
793 init_reg_q_enum_key(&q_o, hnd, key_index);
795 /* turn parameters into data stream */
796 if(!reg_io_q_enum_key("", &q_o, &buf, 0)) {
802 /* send the data on \PIPE\ */
803 if (!rpc_api_pipe_req(cli, REG_ENUM_KEY, &buf, &rbuf)) {
813 if(!reg_io_r_enum_key("", &r_o, &rbuf, 0)) {
818 if (r_o.status != 0) {
819 /* report error code */
820 DEBUG(0,("REG_ENUM_KEY: %s\n", get_nt_error_msg(r_o.status)));
825 (*unk_1) = r_o.unknown_1;
826 (*unk_2) = r_o.unknown_2;
827 fstrcpy(key_name, dos_unistr2(r_o.key_name.str.buffer));
828 (*mod_time) = nt_time_to_unix(&r_o.time);
835 /****************************************************************************
836 do a REG Create Value
837 ****************************************************************************/
838 BOOL do_reg_create_val(struct cli_state *cli, POLICY_HND *hnd,
839 char *val_name, uint32 type, BUFFER3 *data)
843 REG_Q_CREATE_VALUE q_o;
844 REG_R_CREATE_VALUE r_o;
849 prs_init(&buf, MAX_PDU_FRAG_LEN, 4, MARSHALL);
850 prs_init(&rbuf, 0, 4, UNMARSHALL);
852 /* create and send a MSRPC command with api REG_CREATE_VALUE */
854 DEBUG(4,("REG Create Value: %s\n", val_name));
856 init_reg_q_create_val(&q_o, hnd, val_name, type, data);
858 /* turn parameters into data stream */
859 if(!reg_io_q_create_val("", &q_o, &buf, 0)) {
865 /* send the data on \PIPE\ */
866 if (!rpc_api_pipe_req(cli, REG_CREATE_VALUE, &buf, &rbuf)) {
876 if(!reg_io_r_create_val("", &r_o, &rbuf, 0)) {
881 if (r_o.status != 0) {
882 /* report error code */
883 DEBUG(0,("REG_CREATE_VALUE: %s\n", get_nt_error_msg(r_o.status)));
893 /****************************************************************************
895 ****************************************************************************/
896 BOOL do_reg_enum_val(struct cli_state *cli, POLICY_HND *hnd,
897 int val_index, int max_valnamelen, int max_valbufsize,
899 uint32 *val_type, BUFFER2 *value)
903 REG_Q_ENUM_VALUE q_o;
904 REG_R_ENUM_VALUE r_o;
909 prs_init(&buf, MAX_PDU_FRAG_LEN, 4, MARSHALL);
910 prs_init(&rbuf, 0, 4, UNMARSHALL);
912 /* create and send a MSRPC command with api REG_ENUM_VALUE */
914 DEBUG(4,("REG Enum Value\n"));
916 init_reg_q_enum_val(&q_o, hnd, val_index, max_valnamelen, max_valbufsize);
918 /* turn parameters into data stream */
919 if(!reg_io_q_enum_val("", &q_o, &buf, 0)) {
925 /* send the data on \PIPE\ */
926 if (!rpc_api_pipe_req(cli, REG_ENUM_VALUE, &buf, &rbuf)) {
935 r_o.buf_value = value;
937 if(!reg_io_r_enum_val("", &r_o, &rbuf, 0)) {
942 if (r_o.status != 0) {
943 /* report error code */
944 DEBUG(0,("REG_ENUM_VALUE: %s\n", get_nt_error_msg(r_o.status)));
949 (*val_type) = r_o.type;
950 fstrcpy(val_name, dos_unistr2_to_str(&r_o.uni_name));
957 /****************************************************************************
959 ****************************************************************************/
960 BOOL do_reg_open_entry(struct cli_state *cli, POLICY_HND *hnd,
961 char *key_name, uint32 unk_0,
966 REG_Q_OPEN_ENTRY q_o;
967 REG_R_OPEN_ENTRY r_o;
972 prs_init(&buf, MAX_PDU_FRAG_LEN, 4, MARSHALL);
973 prs_init(&rbuf, 0, 4, UNMARSHALL);
975 /* create and send a MSRPC command with api REG_OPEN_ENTRY */
977 DEBUG(4,("REG Open Entry\n"));
979 init_reg_q_open_entry(&q_o, hnd, key_name, unk_0);
981 /* turn parameters into data stream */
982 if(!reg_io_q_open_entry("", &q_o, &buf, 0)) {
988 /* send the data on \PIPE\ */
989 if (!rpc_api_pipe_req(cli, REG_OPEN_ENTRY, &buf, &rbuf)) {
999 if(!reg_io_r_open_entry("", &r_o, &rbuf, 0)) {
1000 prs_mem_free(&rbuf);
1004 if (r_o.status != 0) {
1005 /* report error code */
1006 DEBUG(0,("REG_OPEN_ENTRY: %s\n", get_nt_error_msg(r_o.status)));
1007 prs_mem_free(&rbuf);
1011 memcpy(key_hnd, r_o.pol.data, sizeof(key_hnd->data));
1013 prs_mem_free(&rbuf);
1018 /****************************************************************************
1020 ****************************************************************************/
1021 BOOL do_reg_close(struct cli_state *cli, POLICY_HND *hnd)
1032 /* create and send a MSRPC command with api REG_CLOSE */
1034 prs_init(&buf, MAX_PDU_FRAG_LEN, 4, MARSHALL);
1035 prs_init(&rbuf, 0, 4, UNMARSHALL);
1037 DEBUG(4,("REG Close\n"));
1039 /* store the parameters */
1040 init_reg_q_close(&q_c, hnd);
1042 /* turn parameters into data stream */
1043 if(!reg_io_q_close("", &q_c, &buf, 0)) {
1045 prs_mem_free(&rbuf);
1049 /* send the data on \PIPE\ */
1050 if (!rpc_api_pipe_req(cli, REG_CLOSE, &buf, &rbuf)) {
1052 prs_mem_free(&rbuf);
1060 if(!reg_io_r_close("", &r_c, &rbuf, 0)) {
1061 prs_mem_free(&rbuf);
1065 if (r_c.status != 0) {
1066 /* report error code */
1067 DEBUG(0,("REG_CLOSE: %s\n", get_nt_error_msg(r_c.status)));
1068 prs_mem_free(&rbuf);
1072 /* check that the returned policy handle is all zeros */
1074 for (i = 0; i < sizeof(r_c.pol.data); i++) {
1075 if (r_c.pol.data[i] != 0) {
1076 prs_mem_free(&rbuf);
1077 DEBUG(0,("REG_CLOSE: non-zero handle returned\n"));
1082 prs_mem_free(&rbuf);