2 * Unix SMB/Netbios implementation.
4 * RPC Pipe client / server routines
5 * Copyright (C) Andrew Tridgell 1992-1998,
6 * Copyright (C) Luke Kenneth Casson Leighton 1996-1998,
7 * Copyright (C) Paul Ashton 1997-1998.
8 * Copyright (C) Jeremy Allison 1999.
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.
28 /****************************************************************************
30 ****************************************************************************/
31 BOOL do_reg_connect(struct cli_state *cli, char *full_keyname, char *key_name,
37 if (full_keyname == NULL)
40 ZERO_STRUCTP(reg_hnd);
43 * open registry receive a policy handle
46 if (!reg_split_key(full_keyname, ®_type, key_name)) {
47 DEBUG(0,("do_reg_connect: unrecognised key name %s\n", full_keyname));
52 case HKEY_LOCAL_MACHINE:
53 res = res ? do_reg_open_hklm(cli, 0x84E0, 0x02000000, reg_hnd) : False;
57 res = res ? do_reg_open_hku(cli, 0x84E0, 0x02000000, reg_hnd) : False;
61 DEBUG(0,("do_reg_connect: unrecognised hive key\n"));
68 /****************************************************************************
70 ****************************************************************************/
71 BOOL do_reg_open_hklm(struct cli_state *cli, uint16 unknown_0, uint32 level,
82 prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
83 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
85 /* create and send a MSRPC command with api REG_OPEN_HKLM */
87 DEBUG(4,("REG Open HKLM\n"));
89 init_reg_q_open_hklm(&q_o, unknown_0, level);
91 /* turn parameters into data stream */
92 if(!reg_io_q_open_hklm("", &q_o, &buf, 0)) {
98 /* send the data on \PIPE\ */
99 if (!rpc_api_pipe_req(cli, REG_OPEN_HKLM, &buf, &rbuf)) {
109 if(!reg_io_r_open_hklm("", &r_o, &rbuf, 0)) {
114 if (r_o.status != 0) {
115 /* report error code */
116 DEBUG(0,("REG_OPEN_HKLM: %s\n", get_nt_error_msg(r_o.status)));
121 /* ok, at last: we're happy. return the policy handle */
129 /****************************************************************************
131 ****************************************************************************/
132 BOOL do_reg_open_hku(struct cli_state *cli, uint16 unknown_0, uint32 level,
143 prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
144 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
146 /* create and send a MSRPC command with api REG_OPEN_HKU */
148 DEBUG(4,("REG Open HKU\n"));
150 init_reg_q_open_hku(&q_o, unknown_0, level);
152 /* turn parameters into data stream */
153 if(!reg_io_q_open_hku("", &q_o, &buf, 0)) {
159 /* send the data on \PIPE\ */
160 if (rpc_api_pipe_req(cli, REG_OPEN_HKU, &buf, &rbuf)) {
170 if(!reg_io_r_open_hku("", &r_o, &rbuf, 0)) {
175 if (r_o.status != 0) {
176 /* report error code */
177 DEBUG(0,("REG_OPEN_HKU: %s\n", get_nt_error_msg(r_o.status)));
182 /* ok, at last: we're happy. return the policy handle */
190 /****************************************************************************
191 do a REG Unknown 0xB command. sent after a create key or create value.
192 this might be some sort of "sync" or "refresh" command, sent after
193 modification of the registry...
194 ****************************************************************************/
195 BOOL do_reg_flush_key(struct cli_state *cli, POLICY_HND *hnd)
205 prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
206 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
208 /* create and send a MSRPC command with api REG_FLUSH_KEY */
210 DEBUG(4,("REG Unknown 0xB\n"));
212 init_reg_q_flush_key(&q_o, hnd);
214 /* turn parameters into data stream */
215 if(!reg_io_q_flush_key("", &q_o, &buf, 0)) {
221 /* send the data on \PIPE\ */
222 if (!rpc_api_pipe_req(cli, REG_FLUSH_KEY, &buf, &rbuf)) {
232 if(!reg_io_r_flush_key("", &r_o, &rbuf, 0)) {
237 if (r_o.status != 0) {
238 /* report error code */
239 DEBUG(0,("REG_FLUSH_KEY: %s\n", get_nt_error_msg(r_o.status)));
249 /****************************************************************************
251 ****************************************************************************/
252 BOOL do_reg_query_key(struct cli_state *cli, POLICY_HND *hnd,
253 char *class, uint32 *class_len,
254 uint32 *num_subkeys, uint32 *max_subkeylen,
255 uint32 *max_subkeysize, uint32 *num_values,
256 uint32 *max_valnamelen, uint32 *max_valbufsize,
257 uint32 *sec_desc, NTTIME *mod_time)
267 prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
268 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
270 /* create and send a MSRPC command with api REG_QUERY_KEY */
272 DEBUG(4,("REG Query Key\n"));
274 init_reg_q_query_key(&q_o, hnd, *class_len);
276 /* turn parameters into data stream */
277 if(!reg_io_q_query_key("", &q_o, &buf, 0)) {
283 /* send the data on \PIPE\ */
284 if (!rpc_api_pipe_req(cli, REG_QUERY_KEY, &buf, &rbuf)) {
294 if(!reg_io_r_query_key("", &r_o, &rbuf, 0)) {
299 if (r_o.status != 0) {
300 /* report error code */
301 DEBUG(0,("REG_QUERY_KEY: %s\n", get_nt_error_msg(r_o.status)));
306 *class_len = r_o.hdr_class.uni_max_len;
307 rpcstr_pull(class, &r_o.uni_class, -1, -1, 0);
308 *num_subkeys = r_o.num_subkeys ;
309 *max_subkeylen = r_o.max_subkeylen ;
310 *max_subkeysize = r_o.max_subkeysize;
311 *num_values = r_o.num_values ;
312 *max_valnamelen = r_o.max_valnamelen;
313 *max_valbufsize = r_o.max_valbufsize;
314 *sec_desc = r_o.sec_desc ;
315 *mod_time = r_o.mod_time ;
322 /****************************************************************************
324 ****************************************************************************/
325 BOOL do_reg_unknown_1a(struct cli_state *cli, POLICY_HND *hnd, uint32 *unk)
335 prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
336 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
338 /* create and send a MSRPC command with api REG_UNKNOWN_1A */
340 DEBUG(4,("REG Unknown 1a\n"));
342 init_reg_q_unk_1a(&q_o, hnd);
344 /* turn parameters into data stream */
345 if(!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)) {
362 if(!reg_io_r_unk_1a("", &r_o, &rbuf, 0)) {
367 if (r_o.status != 0) {
368 /* report error code */
369 DEBUG(0,("REG_UNK_1A: %s\n", get_nt_error_msg(r_o.status)));
374 (*unk) = r_o.unknown;
381 /****************************************************************************
383 ****************************************************************************/
384 BOOL do_reg_query_info(struct cli_state *cli, POLICY_HND *hnd,
385 char *key_value, uint32* key_type)
395 prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
396 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
398 /* create and send a MSRPC command with api REG_INFO */
400 DEBUG(4,("REG Query Info\n"));
402 init_reg_q_info(&q_o, hnd, "ProductType");
404 /* turn parameters into data stream */
405 if(!reg_io_q_info("", &q_o, &buf, 0)) {
411 /* send the data on \PIPE\ */
412 if (!rpc_api_pipe_req(cli, REG_INFO, &buf, &rbuf)) {
422 if(!reg_io_r_info("", &r_o, &rbuf, 0)) {
427 if ( r_o.status != 0) {
428 /* report error code */
429 DEBUG(0,("REG_INFO: %s\n", get_nt_error_msg(r_o.status)));
434 /*fstrcpy(key_value, dos_buffer2_to_str(r_o.uni_val));*/
435 rpcstr_pull(key_value, r_o.uni_val->buffer, sizeof(fstring), r_o.uni_val->buf_len, 0);
436 *key_type = r_o.type;
443 /****************************************************************************
444 do a REG Set Key Security
445 ****************************************************************************/
446 BOOL do_reg_set_key_sec(struct cli_state *cli, POLICY_HND *hnd, SEC_DESC_BUF *sec_desc_buf)
450 REG_Q_SET_KEY_SEC q_o;
451 REG_R_SET_KEY_SEC r_o;
456 prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
457 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
459 /* create and send a MSRPC command with api REG_SET_KEY_SEC */
461 DEBUG(4,("REG Set Key security.\n"));
463 init_reg_q_set_key_sec(&q_o, hnd, sec_desc_buf);
465 /* turn parameters into data stream */
466 if(!reg_io_q_set_key_sec("", &q_o, &buf, 0)) {
472 /* send the data on \PIPE\ */
473 if (!rpc_api_pipe_req(cli, REG_SET_KEY_SEC, &buf, &rbuf)) {
483 if(!reg_io_r_set_key_sec("", &r_o, &rbuf, 0)) {
488 if (r_o.status != 0) {
498 /****************************************************************************
499 do a REG Query Key Security
500 ****************************************************************************/
502 BOOL do_reg_get_key_sec(struct cli_state *cli, POLICY_HND *hnd, uint32 *sec_buf_size, SEC_DESC_BUF **ppsec_desc_buf)
506 REG_Q_GET_KEY_SEC q_o;
507 REG_R_GET_KEY_SEC r_o;
512 prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
513 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
515 /* create and send a MSRPC command with api REG_GET_KEY_SEC */
517 DEBUG(4,("REG query key security. buf_size: %d\n", *sec_buf_size));
519 init_reg_q_get_key_sec(&q_o, hnd, *sec_buf_size, NULL);
521 /* turn parameters into data stream */
522 if(!reg_io_q_get_key_sec("", &q_o, &buf, 0)) {
528 /* send the data on \PIPE\ */
529 if (!rpc_api_pipe_req(cli, REG_GET_KEY_SEC, &buf, &rbuf)) {
539 if(!reg_io_r_get_key_sec("", &r_o, &rbuf, 0)) {
544 if (r_o.status == 0x0000007a) {
546 * get the maximum buffer size: it was too small
548 (*sec_buf_size) = r_o.hdr_sec.buf_max_len;
549 DEBUG(5,("sec_buf_size too small. use %d\n", *sec_buf_size));
550 } else if (r_o.status != 0) {
551 /* report error code */
552 DEBUG(0,("REG_GET_KEY_SEC: %s\n", get_nt_error_msg(r_o.status)));
556 (*sec_buf_size) = r_o.data->len;
557 *ppsec_desc_buf = r_o.data;
565 /****************************************************************************
566 do a REG Delete Value
567 ****************************************************************************/
568 BOOL do_reg_delete_val(struct cli_state *cli, POLICY_HND *hnd, char *val_name)
572 REG_Q_DELETE_VALUE q_o;
573 REG_R_DELETE_VALUE r_o;
578 prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
579 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
581 /* create and send a MSRPC command with api REG_DELETE_VALUE */
583 DEBUG(4,("REG Delete Value: %s\n", val_name));
585 init_reg_q_delete_val(&q_o, hnd, val_name);
587 /* turn parameters into data stream */
588 if(!reg_io_q_delete_val("", &q_o, &buf, 0)) {
594 /* send the data on \PIPE\ */
595 if (rpc_api_pipe_req(cli, REG_DELETE_VALUE, &buf, &rbuf)) {
605 if(!reg_io_r_delete_val("", &r_o, &rbuf, 0)) {
610 if (r_o.status != 0) {
611 /* report error code */
612 DEBUG(0,("REG_DELETE_VALUE: %s\n", get_nt_error_msg(r_o.status)));
622 /****************************************************************************
624 ****************************************************************************/
625 BOOL do_reg_delete_key(struct cli_state *cli, POLICY_HND *hnd, char *key_name)
629 REG_Q_DELETE_KEY q_o;
630 REG_R_DELETE_KEY r_o;
635 prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
636 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
638 /* create and send a MSRPC command with api REG_DELETE_KEY */
640 DEBUG(4,("REG Delete Key: %s\n", key_name));
642 init_reg_q_delete_key(&q_o, hnd, key_name);
644 /* turn parameters into data stream */
645 if(!reg_io_q_delete_key("", &q_o, &buf, 0)) {
651 /* send the data on \PIPE\ */
652 if (!rpc_api_pipe_req(cli, REG_DELETE_KEY, &buf, &rbuf)) {
662 if(!reg_io_r_delete_key("", &r_o, &rbuf, 0)) {
667 if (r_o.status != 0) {
668 /* report error code */
669 DEBUG(0,("REG_DELETE_KEY: %s\n", get_nt_error_msg(r_o.status)));
679 /****************************************************************************
681 ****************************************************************************/
682 BOOL do_reg_create_key(struct cli_state *cli, POLICY_HND *hnd,
683 char *key_name, char *key_class,
684 SEC_ACCESS *sam_access,
689 REG_Q_CREATE_KEY q_o;
690 REG_R_CREATE_KEY r_o;
691 SEC_DESC *sec = NULL;
692 SEC_DESC_BUF *sec_buf = NULL;
700 /* create and send a MSRPC command with api REG_CREATE_KEY */
702 DEBUG(4,("REG Create Key: %s %s 0x%08x\n", key_name, key_class,
703 sam_access != NULL ? sam_access->mask : 0));
705 if((sec = make_sec_desc( cli->mem_ctx, 1, NULL, NULL, NULL, NULL, &sec_len)) == NULL) {
706 DEBUG(0,("make_sec_desc : malloc fail.\n"));
710 DEBUG(10,("make_sec_desc: len = %d\n", (int)sec_len));
712 if((sec_buf = make_sec_desc_buf( cli->mem_ctx, (int)sec_len, sec)) == NULL) {
713 DEBUG(0,("make_sec_desc : malloc fail (1)\n"));
717 prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
718 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
720 init_reg_q_create_key(&q_o, hnd, key_name, key_class, sam_access, sec_buf);
722 /* turn parameters into data stream */
723 if(!reg_io_q_create_key("", &q_o, &buf, 0)) {
729 /* send the data on \PIPE\ */
730 if (rpc_api_pipe_req(cli, REG_CREATE_KEY, &buf, &rbuf)) {
740 if(!reg_io_r_create_key("", &r_o, &rbuf, 0)) {
745 if (r_o.status != 0) {
746 /* report error code */
747 DEBUG(0,("REG_CREATE_KEY: %s\n", get_nt_error_msg(r_o.status)));
759 /****************************************************************************
761 ****************************************************************************/
762 BOOL do_reg_enum_key(struct cli_state *cli, POLICY_HND *hnd,
763 int key_index, char *key_name,
764 uint32 *unk_1, uint32 *unk_2,
775 prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
776 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
778 /* create and send a MSRPC command with api REG_ENUM_KEY */
780 DEBUG(4,("REG Enum Key\n"));
782 init_reg_q_enum_key(&q_o, hnd, key_index);
784 /* turn parameters into data stream */
785 if(!reg_io_q_enum_key("", &q_o, &buf, 0)) {
791 /* send the data on \PIPE\ */
792 if (!rpc_api_pipe_req(cli, REG_ENUM_KEY, &buf, &rbuf)) {
802 if(!reg_io_r_enum_key("", &r_o, &rbuf, 0)) {
807 if (r_o.status != 0) {
808 /* report error code */
809 DEBUG(0,("REG_ENUM_KEY: %s\n", get_nt_error_msg(r_o.status)));
814 (*unk_1) = r_o.unknown_1;
815 (*unk_2) = r_o.unknown_2;
816 rpcstr_pull(key_name, r_o.key_name.str.buffer, -1, -1, 0);
817 (*mod_time) = nt_time_to_unix(&r_o.time);
824 /****************************************************************************
825 do a REG Create Value
826 ****************************************************************************/
827 BOOL do_reg_create_val(struct cli_state *cli, POLICY_HND *hnd,
828 char *val_name, uint32 type, BUFFER3 *data)
832 REG_Q_CREATE_VALUE q_o;
833 REG_R_CREATE_VALUE r_o;
838 prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
839 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
841 /* create and send a MSRPC command with api REG_CREATE_VALUE */
843 DEBUG(4,("REG Create Value: %s\n", val_name));
845 init_reg_q_create_val(&q_o, hnd, val_name, type, data);
847 /* turn parameters into data stream */
848 if(!reg_io_q_create_val("", &q_o, &buf, 0)) {
854 /* send the data on \PIPE\ */
855 if (!rpc_api_pipe_req(cli, REG_CREATE_VALUE, &buf, &rbuf)) {
865 if(!reg_io_r_create_val("", &r_o, &rbuf, 0)) {
870 if (r_o.status != 0) {
871 /* report error code */
872 DEBUG(0,("REG_CREATE_VALUE: %s\n", get_nt_error_msg(r_o.status)));
882 /****************************************************************************
884 ****************************************************************************/
885 BOOL do_reg_enum_val(struct cli_state *cli, POLICY_HND *hnd,
886 int val_index, int max_valnamelen, int max_valbufsize,
888 uint32 *val_type, BUFFER2 *value)
892 REG_Q_ENUM_VALUE q_o;
893 REG_R_ENUM_VALUE r_o;
898 prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
899 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
901 /* create and send a MSRPC command with api REG_ENUM_VALUE */
903 DEBUG(4,("REG Enum Value\n"));
905 init_reg_q_enum_val(&q_o, hnd, val_index, max_valnamelen, max_valbufsize);
907 /* turn parameters into data stream */
908 if(!reg_io_q_enum_val("", &q_o, &buf, 0)) {
914 /* send the data on \PIPE\ */
915 if (!rpc_api_pipe_req(cli, REG_ENUM_VALUE, &buf, &rbuf)) {
924 r_o.buf_value = value;
926 if(!reg_io_r_enum_val("", &r_o, &rbuf, 0)) {
931 if (r_o.status != 0) {
932 /* report error code */
933 DEBUG(0,("REG_ENUM_VALUE: %s\n", get_nt_error_msg(r_o.status)));
938 (*val_type) = r_o.type;
939 rpcstr_pull(val_name, &r_o.uni_name, -1, -1, 0);
946 /****************************************************************************
948 ****************************************************************************/
949 BOOL do_reg_open_entry(struct cli_state *cli, POLICY_HND *hnd,
950 char *key_name, uint32 unk_0,
955 REG_Q_OPEN_ENTRY q_o;
956 REG_R_OPEN_ENTRY r_o;
961 prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
962 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
964 /* create and send a MSRPC command with api REG_OPEN_ENTRY */
966 DEBUG(4,("REG Open Entry\n"));
968 init_reg_q_open_entry(&q_o, hnd, key_name, unk_0);
970 /* turn parameters into data stream */
971 if(!reg_io_q_open_entry("", &q_o, &buf, 0)) {
977 /* send the data on \PIPE\ */
978 if (!rpc_api_pipe_req(cli, REG_OPEN_ENTRY, &buf, &rbuf)) {
988 if(!reg_io_r_open_entry("", &r_o, &rbuf, 0)) {
993 if (r_o.status != 0) {
994 /* report error code */
995 DEBUG(0,("REG_OPEN_ENTRY: %s\n", get_nt_error_msg(r_o.status)));
1002 prs_mem_free(&rbuf);
1007 /****************************************************************************
1009 ****************************************************************************/
1010 BOOL do_reg_close(struct cli_state *cli, POLICY_HND *hnd)
1020 /* create and send a MSRPC command with api REG_CLOSE */
1022 prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
1023 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
1025 DEBUG(4,("REG Close\n"));
1027 /* store the parameters */
1028 init_reg_q_close(&q_c, hnd);
1030 /* turn parameters into data stream */
1031 if(!reg_io_q_close("", &q_c, &buf, 0)) {
1033 prs_mem_free(&rbuf);
1037 /* send the data on \PIPE\ */
1038 if (!rpc_api_pipe_req(cli, REG_CLOSE, &buf, &rbuf)) {
1040 prs_mem_free(&rbuf);
1048 if(!reg_io_r_close("", &r_c, &rbuf, 0)) {
1049 prs_mem_free(&rbuf);
1053 if (r_c.status != 0) {
1054 /* report error code */
1055 DEBUG(0,("REG_CLOSE: %s\n", get_nt_error_msg(r_c.status)));
1056 prs_mem_free(&rbuf);
1060 /* check that the returned policy handle is all zeros */
1062 if (IVAL(&r_c.pol.data1,0) || IVAL(&r_c.pol.data2,0) || SVAL(&r_c.pol.data3,0) ||
1063 SVAL(&r_c.pol.data4,0) || IVAL(r_c.pol.data5,0) || IVAL(r_c.pol.data5,4) ) {
1064 prs_mem_free(&rbuf);
1065 DEBUG(0,("REG_CLOSE: non-zero handle returned\n"));
1069 prs_mem_free(&rbuf);