2 * Unix SMB/CIFS implementation.
3 * RPC Pipe client / server routines
4 * Copyright (C) Andrew Tridgell 1992-1998,
5 * Copyright (C) Luke Kenneth Casson Leighton 1996-1998,
6 * Copyright (C) Paul Ashton 1997-1998.
7 * Copyright (C) Jeremy Allison 1999.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 /****************************************************************************
29 ****************************************************************************/
30 BOOL do_reg_connect(struct cli_state *cli, char *full_keyname, char *key_name,
36 if (full_keyname == NULL)
39 ZERO_STRUCTP(reg_hnd);
42 * open registry receive a policy handle
45 if (!reg_split_key(full_keyname, ®_type, key_name)) {
46 DEBUG(0,("do_reg_connect: unrecognised key name %s\n", full_keyname));
51 case HKEY_LOCAL_MACHINE:
52 res = res ? do_reg_open_hklm(cli, 0x84E0, 0x02000000, reg_hnd) : False;
56 res = res ? do_reg_open_hku(cli, 0x84E0, 0x02000000, reg_hnd) : False;
60 DEBUG(0,("do_reg_connect: unrecognised hive key\n"));
67 /****************************************************************************
69 ****************************************************************************/
70 BOOL do_reg_open_hklm(struct cli_state *cli, uint16 unknown_0, uint32 level,
81 prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
82 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
84 /* create and send a MSRPC command with api REG_OPEN_HKLM */
86 DEBUG(4,("REG Open HKLM\n"));
88 init_reg_q_open_hklm(&q_o, unknown_0, level);
90 /* turn parameters into data stream */
91 if(!reg_io_q_open_hklm("", &q_o, &buf, 0)) {
97 /* send the data on \PIPE\ */
98 if (!rpc_api_pipe_req(cli, REG_OPEN_HKLM, &buf, &rbuf)) {
108 if(!reg_io_r_open_hklm("", &r_o, &rbuf, 0)) {
113 if (r_o.status != 0) {
114 /* report error code */
115 DEBUG(0,("REG_OPEN_HKLM: %s\n", get_nt_error_msg(r_o.status)));
120 /* ok, at last: we're happy. return the policy handle */
128 /****************************************************************************
130 ****************************************************************************/
131 BOOL do_reg_open_hku(struct cli_state *cli, uint16 unknown_0, uint32 level,
142 prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
143 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
145 /* create and send a MSRPC command with api REG_OPEN_HKU */
147 DEBUG(4,("REG Open HKU\n"));
149 init_reg_q_open_hku(&q_o, unknown_0, level);
151 /* turn parameters into data stream */
152 if(!reg_io_q_open_hku("", &q_o, &buf, 0)) {
158 /* send the data on \PIPE\ */
159 if (rpc_api_pipe_req(cli, REG_OPEN_HKU, &buf, &rbuf)) {
169 if(!reg_io_r_open_hku("", &r_o, &rbuf, 0)) {
174 if (r_o.status != 0) {
175 /* report error code */
176 DEBUG(0,("REG_OPEN_HKU: %s\n", get_nt_error_msg(r_o.status)));
181 /* ok, at last: we're happy. return the policy handle */
189 /****************************************************************************
190 do a REG Unknown 0xB command. sent after a create key or create value.
191 this might be some sort of "sync" or "refresh" command, sent after
192 modification of the registry...
193 ****************************************************************************/
194 BOOL do_reg_flush_key(struct cli_state *cli, POLICY_HND *hnd)
204 prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
205 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
207 /* create and send a MSRPC command with api REG_FLUSH_KEY */
209 DEBUG(4,("REG Unknown 0xB\n"));
211 init_reg_q_flush_key(&q_o, hnd);
213 /* turn parameters into data stream */
214 if(!reg_io_q_flush_key("", &q_o, &buf, 0)) {
220 /* send the data on \PIPE\ */
221 if (!rpc_api_pipe_req(cli, REG_FLUSH_KEY, &buf, &rbuf)) {
231 if(!reg_io_r_flush_key("", &r_o, &rbuf, 0)) {
236 if (r_o.status != 0) {
237 /* report error code */
238 DEBUG(0,("REG_FLUSH_KEY: %s\n", get_nt_error_msg(r_o.status)));
248 /****************************************************************************
250 ****************************************************************************/
251 BOOL do_reg_query_key(struct cli_state *cli, POLICY_HND *hnd,
252 char *class, uint32 *class_len,
253 uint32 *num_subkeys, uint32 *max_subkeylen,
254 uint32 *max_subkeysize, uint32 *num_values,
255 uint32 *max_valnamelen, uint32 *max_valbufsize,
256 uint32 *sec_desc, NTTIME *mod_time)
266 prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
267 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
269 /* create and send a MSRPC command with api REG_QUERY_KEY */
271 DEBUG(4,("REG Query Key\n"));
273 init_reg_q_query_key(&q_o, hnd, *class_len);
275 /* turn parameters into data stream */
276 if(!reg_io_q_query_key("", &q_o, &buf, 0)) {
282 /* send the data on \PIPE\ */
283 if (!rpc_api_pipe_req(cli, REG_QUERY_KEY, &buf, &rbuf)) {
293 if(!reg_io_r_query_key("", &r_o, &rbuf, 0)) {
298 if (r_o.status != 0) {
299 /* report error code */
300 DEBUG(0,("REG_QUERY_KEY: %s\n", get_nt_error_msg(r_o.status)));
305 *class_len = r_o.hdr_class.uni_max_len;
306 rpcstr_pull(class, &r_o.uni_class, -1, -1, 0);
307 *num_subkeys = r_o.num_subkeys ;
308 *max_subkeylen = r_o.max_subkeylen ;
309 *max_subkeysize = r_o.max_subkeysize;
310 *num_values = r_o.num_values ;
311 *max_valnamelen = r_o.max_valnamelen;
312 *max_valbufsize = r_o.max_valbufsize;
313 *sec_desc = r_o.sec_desc ;
314 *mod_time = r_o.mod_time ;
321 /****************************************************************************
323 ****************************************************************************/
324 BOOL do_reg_unknown_1a(struct cli_state *cli, POLICY_HND *hnd, uint32 *unk)
334 prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
335 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
337 /* create and send a MSRPC command with api REG_UNKNOWN_1A */
339 DEBUG(4,("REG Unknown 1a\n"));
341 init_reg_q_unk_1a(&q_o, hnd);
343 /* turn parameters into data stream */
344 if(!reg_io_q_unk_1a("", &q_o, &buf, 0)) {
350 /* send the data on \PIPE\ */
351 if (rpc_api_pipe_req(cli, REG_UNK_1A, &buf, &rbuf)) {
361 if(!reg_io_r_unk_1a("", &r_o, &rbuf, 0)) {
366 if (r_o.status != 0) {
367 /* report error code */
368 DEBUG(0,("REG_UNK_1A: %s\n", get_nt_error_msg(r_o.status)));
373 (*unk) = r_o.unknown;
380 /****************************************************************************
382 ****************************************************************************/
383 BOOL do_reg_query_info(struct cli_state *cli, POLICY_HND *hnd,
384 char *key_value, uint32* key_type)
394 prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
395 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
397 /* create and send a MSRPC command with api REG_INFO */
399 DEBUG(4,("REG Query Info\n"));
401 init_reg_q_info(&q_o, hnd, "ProductType");
403 /* turn parameters into data stream */
404 if(!reg_io_q_info("", &q_o, &buf, 0)) {
410 /* send the data on \PIPE\ */
411 if (!rpc_api_pipe_req(cli, REG_INFO, &buf, &rbuf)) {
421 if(!reg_io_r_info("", &r_o, &rbuf, 0)) {
426 if ( r_o.status != 0) {
427 /* report error code */
428 DEBUG(0,("REG_INFO: %s\n", get_nt_error_msg(r_o.status)));
433 /*fstrcpy(key_value, dos_buffer2_to_str(r_o.uni_val));*/
434 rpcstr_pull(key_value, r_o.uni_val->buffer, sizeof(fstring), r_o.uni_val->buf_len, 0);
435 *key_type = r_o.type;
442 /****************************************************************************
443 do a REG Set Key Security
444 ****************************************************************************/
445 BOOL do_reg_set_key_sec(struct cli_state *cli, POLICY_HND *hnd, SEC_DESC_BUF *sec_desc_buf)
449 REG_Q_SET_KEY_SEC q_o;
450 REG_R_SET_KEY_SEC r_o;
455 prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
456 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
458 /* create and send a MSRPC command with api REG_SET_KEY_SEC */
460 DEBUG(4,("REG Set Key security.\n"));
462 init_reg_q_set_key_sec(&q_o, hnd, sec_desc_buf);
464 /* turn parameters into data stream */
465 if(!reg_io_q_set_key_sec("", &q_o, &buf, 0)) {
471 /* send the data on \PIPE\ */
472 if (!rpc_api_pipe_req(cli, REG_SET_KEY_SEC, &buf, &rbuf)) {
482 if(!reg_io_r_set_key_sec("", &r_o, &rbuf, 0)) {
487 if (r_o.status != 0) {
497 /****************************************************************************
498 do a REG Query Key Security
499 ****************************************************************************/
501 BOOL do_reg_get_key_sec(struct cli_state *cli, POLICY_HND *hnd, uint32 *sec_buf_size, SEC_DESC_BUF **ppsec_desc_buf)
505 REG_Q_GET_KEY_SEC q_o;
506 REG_R_GET_KEY_SEC r_o;
511 prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
512 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
514 /* create and send a MSRPC command with api REG_GET_KEY_SEC */
516 DEBUG(4,("REG query key security. buf_size: %d\n", *sec_buf_size));
518 init_reg_q_get_key_sec(&q_o, hnd, *sec_buf_size, NULL);
520 /* turn parameters into data stream */
521 if(!reg_io_q_get_key_sec("", &q_o, &buf, 0)) {
527 /* send the data on \PIPE\ */
528 if (!rpc_api_pipe_req(cli, REG_GET_KEY_SEC, &buf, &rbuf)) {
538 if(!reg_io_r_get_key_sec("", &r_o, &rbuf, 0)) {
543 if (r_o.status == 0x0000007a) {
545 * get the maximum buffer size: it was too small
547 (*sec_buf_size) = r_o.hdr_sec.buf_max_len;
548 DEBUG(5,("sec_buf_size too small. use %d\n", *sec_buf_size));
549 } else if (r_o.status != 0) {
550 /* report error code */
551 DEBUG(0,("REG_GET_KEY_SEC: %s\n", get_nt_error_msg(r_o.status)));
555 (*sec_buf_size) = r_o.data->len;
556 *ppsec_desc_buf = r_o.data;
564 /****************************************************************************
565 do a REG Delete Value
566 ****************************************************************************/
567 BOOL do_reg_delete_val(struct cli_state *cli, POLICY_HND *hnd, char *val_name)
571 REG_Q_DELETE_VALUE q_o;
572 REG_R_DELETE_VALUE r_o;
577 prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
578 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
580 /* create and send a MSRPC command with api REG_DELETE_VALUE */
582 DEBUG(4,("REG Delete Value: %s\n", val_name));
584 init_reg_q_delete_val(&q_o, hnd, val_name);
586 /* turn parameters into data stream */
587 if(!reg_io_q_delete_val("", &q_o, &buf, 0)) {
593 /* send the data on \PIPE\ */
594 if (rpc_api_pipe_req(cli, REG_DELETE_VALUE, &buf, &rbuf)) {
604 if(!reg_io_r_delete_val("", &r_o, &rbuf, 0)) {
609 if (r_o.status != 0) {
610 /* report error code */
611 DEBUG(0,("REG_DELETE_VALUE: %s\n", get_nt_error_msg(r_o.status)));
621 /****************************************************************************
623 ****************************************************************************/
624 BOOL do_reg_delete_key(struct cli_state *cli, POLICY_HND *hnd, char *key_name)
628 REG_Q_DELETE_KEY q_o;
629 REG_R_DELETE_KEY r_o;
634 prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
635 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
637 /* create and send a MSRPC command with api REG_DELETE_KEY */
639 DEBUG(4,("REG Delete Key: %s\n", key_name));
641 init_reg_q_delete_key(&q_o, hnd, key_name);
643 /* turn parameters into data stream */
644 if(!reg_io_q_delete_key("", &q_o, &buf, 0)) {
650 /* send the data on \PIPE\ */
651 if (!rpc_api_pipe_req(cli, REG_DELETE_KEY, &buf, &rbuf)) {
661 if(!reg_io_r_delete_key("", &r_o, &rbuf, 0)) {
666 if (r_o.status != 0) {
667 /* report error code */
668 DEBUG(0,("REG_DELETE_KEY: %s\n", get_nt_error_msg(r_o.status)));
678 /****************************************************************************
680 ****************************************************************************/
681 BOOL do_reg_create_key(struct cli_state *cli, POLICY_HND *hnd,
682 char *key_name, char *key_class,
683 SEC_ACCESS *sam_access,
688 REG_Q_CREATE_KEY q_o;
689 REG_R_CREATE_KEY r_o;
690 SEC_DESC *sec = NULL;
691 SEC_DESC_BUF *sec_buf = NULL;
699 /* create and send a MSRPC command with api REG_CREATE_KEY */
701 DEBUG(4,("REG Create Key: %s %s 0x%08x\n", key_name, key_class,
702 sam_access != NULL ? sam_access->mask : 0));
704 if((sec = make_sec_desc( cli->mem_ctx, 1, NULL, NULL, NULL, NULL, &sec_len)) == NULL) {
705 DEBUG(0,("make_sec_desc : malloc fail.\n"));
709 DEBUG(10,("make_sec_desc: len = %d\n", (int)sec_len));
711 if((sec_buf = make_sec_desc_buf( cli->mem_ctx, (int)sec_len, sec)) == NULL) {
712 DEBUG(0,("make_sec_desc : malloc fail (1)\n"));
716 prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
717 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
719 init_reg_q_create_key(&q_o, hnd, key_name, key_class, sam_access, sec_buf);
721 /* turn parameters into data stream */
722 if(!reg_io_q_create_key("", &q_o, &buf, 0)) {
728 /* send the data on \PIPE\ */
729 if (rpc_api_pipe_req(cli, REG_CREATE_KEY, &buf, &rbuf)) {
739 if(!reg_io_r_create_key("", &r_o, &rbuf, 0)) {
744 if (r_o.status != 0) {
745 /* report error code */
746 DEBUG(0,("REG_CREATE_KEY: %s\n", get_nt_error_msg(r_o.status)));
758 /****************************************************************************
760 ****************************************************************************/
761 BOOL do_reg_enum_key(struct cli_state *cli, POLICY_HND *hnd,
762 int key_index, char *key_name,
763 uint32 *unk_1, uint32 *unk_2,
774 prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
775 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
777 /* create and send a MSRPC command with api REG_ENUM_KEY */
779 DEBUG(4,("REG Enum Key\n"));
781 init_reg_q_enum_key(&q_o, hnd, key_index);
783 /* turn parameters into data stream */
784 if(!reg_io_q_enum_key("", &q_o, &buf, 0)) {
790 /* send the data on \PIPE\ */
791 if (!rpc_api_pipe_req(cli, REG_ENUM_KEY, &buf, &rbuf)) {
801 if(!reg_io_r_enum_key("", &r_o, &rbuf, 0)) {
806 if (r_o.status != 0) {
807 /* report error code */
808 DEBUG(0,("REG_ENUM_KEY: %s\n", get_nt_error_msg(r_o.status)));
813 (*unk_1) = r_o.unknown_1;
814 (*unk_2) = r_o.unknown_2;
815 rpcstr_pull(key_name, r_o.key_name.str.buffer, -1, -1, 0);
816 (*mod_time) = nt_time_to_unix(&r_o.time);
823 /****************************************************************************
824 do a REG Create Value
825 ****************************************************************************/
826 BOOL do_reg_create_val(struct cli_state *cli, POLICY_HND *hnd,
827 char *val_name, uint32 type, BUFFER3 *data)
831 REG_Q_CREATE_VALUE q_o;
832 REG_R_CREATE_VALUE r_o;
837 prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
838 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
840 /* create and send a MSRPC command with api REG_CREATE_VALUE */
842 DEBUG(4,("REG Create Value: %s\n", val_name));
844 init_reg_q_create_val(&q_o, hnd, val_name, type, data);
846 /* turn parameters into data stream */
847 if(!reg_io_q_create_val("", &q_o, &buf, 0)) {
853 /* send the data on \PIPE\ */
854 if (!rpc_api_pipe_req(cli, REG_CREATE_VALUE, &buf, &rbuf)) {
864 if(!reg_io_r_create_val("", &r_o, &rbuf, 0)) {
869 if (r_o.status != 0) {
870 /* report error code */
871 DEBUG(0,("REG_CREATE_VALUE: %s\n", get_nt_error_msg(r_o.status)));
881 /****************************************************************************
883 ****************************************************************************/
884 BOOL do_reg_enum_val(struct cli_state *cli, POLICY_HND *hnd,
885 int val_index, int max_valnamelen, int max_valbufsize,
887 uint32 *val_type, BUFFER2 *value)
891 REG_Q_ENUM_VALUE q_o;
892 REG_R_ENUM_VALUE r_o;
897 prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
898 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
900 /* create and send a MSRPC command with api REG_ENUM_VALUE */
902 DEBUG(4,("REG Enum Value\n"));
904 init_reg_q_enum_val(&q_o, hnd, val_index, max_valnamelen, max_valbufsize);
906 /* turn parameters into data stream */
907 if(!reg_io_q_enum_val("", &q_o, &buf, 0)) {
913 /* send the data on \PIPE\ */
914 if (!rpc_api_pipe_req(cli, REG_ENUM_VALUE, &buf, &rbuf)) {
923 r_o.buf_value = value;
925 if(!reg_io_r_enum_val("", &r_o, &rbuf, 0)) {
930 if (r_o.status != 0) {
931 /* report error code */
932 DEBUG(0,("REG_ENUM_VALUE: %s\n", get_nt_error_msg(r_o.status)));
937 (*val_type) = r_o.type;
938 rpcstr_pull(val_name, &r_o.uni_name, -1, -1, 0);
945 /****************************************************************************
947 ****************************************************************************/
948 BOOL do_reg_open_entry(struct cli_state *cli, POLICY_HND *hnd,
949 char *key_name, uint32 unk_0,
954 REG_Q_OPEN_ENTRY q_o;
955 REG_R_OPEN_ENTRY r_o;
960 prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
961 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
963 /* create and send a MSRPC command with api REG_OPEN_ENTRY */
965 DEBUG(4,("REG Open Entry\n"));
967 init_reg_q_open_entry(&q_o, hnd, key_name, unk_0);
969 /* turn parameters into data stream */
970 if(!reg_io_q_open_entry("", &q_o, &buf, 0)) {
976 /* send the data on \PIPE\ */
977 if (!rpc_api_pipe_req(cli, REG_OPEN_ENTRY, &buf, &rbuf)) {
987 if(!reg_io_r_open_entry("", &r_o, &rbuf, 0)) {
992 if (r_o.status != 0) {
993 /* report error code */
994 DEBUG(0,("REG_OPEN_ENTRY: %s\n", get_nt_error_msg(r_o.status)));
1001 prs_mem_free(&rbuf);
1006 /****************************************************************************
1008 ****************************************************************************/
1009 BOOL do_reg_close(struct cli_state *cli, POLICY_HND *hnd)
1019 /* create and send a MSRPC command with api REG_CLOSE */
1021 prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
1022 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
1024 DEBUG(4,("REG Close\n"));
1026 /* store the parameters */
1027 init_reg_q_close(&q_c, hnd);
1029 /* turn parameters into data stream */
1030 if(!reg_io_q_close("", &q_c, &buf, 0)) {
1032 prs_mem_free(&rbuf);
1036 /* send the data on \PIPE\ */
1037 if (!rpc_api_pipe_req(cli, REG_CLOSE, &buf, &rbuf)) {
1039 prs_mem_free(&rbuf);
1047 if(!reg_io_r_close("", &r_c, &rbuf, 0)) {
1048 prs_mem_free(&rbuf);
1052 if (r_c.status != 0) {
1053 /* report error code */
1054 DEBUG(0,("REG_CLOSE: %s\n", get_nt_error_msg(r_c.status)));
1055 prs_mem_free(&rbuf);
1059 /* check that the returned policy handle is all zeros */
1061 if (IVAL(&r_c.pol.data1,0) || IVAL(&r_c.pol.data2,0) || SVAL(&r_c.pol.data3,0) ||
1062 SVAL(&r_c.pol.data4,0) || IVAL(r_c.pol.data5,0) || IVAL(r_c.pol.data5,4) ) {
1063 prs_mem_free(&rbuf);
1064 DEBUG(0,("REG_CLOSE: non-zero handle returned\n"));
1068 prs_mem_free(&rbuf);