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.
32 /****************************************************************************
34 ****************************************************************************/
35 BOOL do_reg_connect(struct cli_state *cli, char *full_keyname, char *key_name,
41 if (full_keyname == NULL)
44 ZERO_STRUCTP(reg_hnd);
47 * open registry receive a policy handle
50 if (!reg_split_key(full_keyname, ®_type, key_name)) {
51 DEBUG(0,("do_reg_connect: unrecognised key name %s\n", full_keyname));
56 case HKEY_LOCAL_MACHINE:
57 res = res ? do_reg_open_hklm(cli, 0x84E0, 0x02000000, reg_hnd) : False;
61 res = res ? do_reg_open_hku(cli, 0x84E0, 0x02000000, reg_hnd) : False;
65 DEBUG(0,("do_reg_connect: unrecognised hive key\n"));
72 /****************************************************************************
74 ****************************************************************************/
75 BOOL do_reg_open_hklm(struct cli_state *cli, uint16 unknown_0, uint32 level,
86 prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
87 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
89 /* create and send a MSRPC command with api REG_OPEN_HKLM */
91 DEBUG(4,("REG Open HKLM\n"));
93 init_reg_q_open_hklm(&q_o, unknown_0, level);
95 /* turn parameters into data stream */
96 if(!reg_io_q_open_hklm("", &q_o, &buf, 0)) {
102 /* send the data on \PIPE\ */
103 if (!rpc_api_pipe_req(cli, REG_OPEN_HKLM, &buf, &rbuf)) {
113 if(!reg_io_r_open_hklm("", &r_o, &rbuf, 0)) {
118 if (r_o.status != 0) {
119 /* report error code */
120 DEBUG(0,("REG_OPEN_HKLM: %s\n", get_nt_error_msg(r_o.status)));
125 /* ok, at last: we're happy. return the policy handle */
133 /****************************************************************************
135 ****************************************************************************/
136 BOOL do_reg_open_hku(struct cli_state *cli, uint16 unknown_0, uint32 level,
147 prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
148 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
150 /* create and send a MSRPC command with api REG_OPEN_HKU */
152 DEBUG(4,("REG Open HKU\n"));
154 init_reg_q_open_hku(&q_o, unknown_0, level);
156 /* turn parameters into data stream */
157 if(!reg_io_q_open_hku("", &q_o, &buf, 0)) {
163 /* send the data on \PIPE\ */
164 if (rpc_api_pipe_req(cli, REG_OPEN_HKU, &buf, &rbuf)) {
174 if(!reg_io_r_open_hku("", &r_o, &rbuf, 0)) {
179 if (r_o.status != 0) {
180 /* report error code */
181 DEBUG(0,("REG_OPEN_HKU: %s\n", get_nt_error_msg(r_o.status)));
186 /* ok, at last: we're happy. return the policy handle */
194 /****************************************************************************
195 do a REG Unknown 0xB command. sent after a create key or create value.
196 this might be some sort of "sync" or "refresh" command, sent after
197 modification of the registry...
198 ****************************************************************************/
199 BOOL do_reg_flush_key(struct cli_state *cli, POLICY_HND *hnd)
209 prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
210 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
212 /* create and send a MSRPC command with api REG_FLUSH_KEY */
214 DEBUG(4,("REG Unknown 0xB\n"));
216 init_reg_q_flush_key(&q_o, hnd);
218 /* turn parameters into data stream */
219 if(!reg_io_q_flush_key("", &q_o, &buf, 0)) {
225 /* send the data on \PIPE\ */
226 if (!rpc_api_pipe_req(cli, REG_FLUSH_KEY, &buf, &rbuf)) {
236 if(!reg_io_r_flush_key("", &r_o, &rbuf, 0)) {
241 if (r_o.status != 0) {
242 /* report error code */
243 DEBUG(0,("REG_FLUSH_KEY: %s\n", get_nt_error_msg(r_o.status)));
253 /****************************************************************************
255 ****************************************************************************/
256 BOOL do_reg_query_key(struct cli_state *cli, POLICY_HND *hnd,
257 char *class, uint32 *class_len,
258 uint32 *num_subkeys, uint32 *max_subkeylen,
259 uint32 *max_subkeysize, uint32 *num_values,
260 uint32 *max_valnamelen, uint32 *max_valbufsize,
261 uint32 *sec_desc, NTTIME *mod_time)
271 prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
272 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
274 /* create and send a MSRPC command with api REG_QUERY_KEY */
276 DEBUG(4,("REG Query Key\n"));
278 init_reg_q_query_key(&q_o, hnd, *class_len);
280 /* turn parameters into data stream */
281 if(!reg_io_q_query_key("", &q_o, &buf, 0)) {
287 /* send the data on \PIPE\ */
288 if (!rpc_api_pipe_req(cli, REG_QUERY_KEY, &buf, &rbuf)) {
298 if(!reg_io_r_query_key("", &r_o, &rbuf, 0)) {
303 if (r_o.status != 0) {
304 /* report error code */
305 DEBUG(0,("REG_QUERY_KEY: %s\n", get_nt_error_msg(r_o.status)));
310 *class_len = r_o.hdr_class.uni_max_len;
311 rpcstr_pull(class, &r_o.uni_class, -1, -1, 0);
312 *num_subkeys = r_o.num_subkeys ;
313 *max_subkeylen = r_o.max_subkeylen ;
314 *max_subkeysize = r_o.max_subkeysize;
315 *num_values = r_o.num_values ;
316 *max_valnamelen = r_o.max_valnamelen;
317 *max_valbufsize = r_o.max_valbufsize;
318 *sec_desc = r_o.sec_desc ;
319 *mod_time = r_o.mod_time ;
326 /****************************************************************************
328 ****************************************************************************/
329 BOOL do_reg_unknown_1a(struct cli_state *cli, POLICY_HND *hnd, uint32 *unk)
339 prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
340 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
342 /* create and send a MSRPC command with api REG_UNKNOWN_1A */
344 DEBUG(4,("REG Unknown 1a\n"));
346 init_reg_q_unk_1a(&q_o, hnd);
348 /* turn parameters into data stream */
349 if(!reg_io_q_unk_1a("", &q_o, &buf, 0)) {
355 /* send the data on \PIPE\ */
356 if (rpc_api_pipe_req(cli, REG_UNK_1A, &buf, &rbuf)) {
366 if(!reg_io_r_unk_1a("", &r_o, &rbuf, 0)) {
371 if (r_o.status != 0) {
372 /* report error code */
373 DEBUG(0,("REG_UNK_1A: %s\n", get_nt_error_msg(r_o.status)));
378 (*unk) = r_o.unknown;
385 /****************************************************************************
387 ****************************************************************************/
388 BOOL do_reg_query_info(struct cli_state *cli, POLICY_HND *hnd,
389 char *key_value, uint32* key_type)
399 prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
400 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
402 /* create and send a MSRPC command with api REG_INFO */
404 DEBUG(4,("REG Query Info\n"));
406 init_reg_q_info(&q_o, hnd, "ProductType");
408 /* turn parameters into data stream */
409 if(!reg_io_q_info("", &q_o, &buf, 0)) {
415 /* send the data on \PIPE\ */
416 if (!rpc_api_pipe_req(cli, REG_INFO, &buf, &rbuf)) {
426 if(!reg_io_r_info("", &r_o, &rbuf, 0)) {
431 if ( r_o.status != 0) {
432 /* report error code */
433 DEBUG(0,("REG_INFO: %s\n", get_nt_error_msg(r_o.status)));
438 /*fstrcpy(key_value, dos_buffer2_to_str(r_o.uni_val));*/
439 rpcstr_pull(key_value, r_o.uni_val->buffer, sizeof(fstring), r_o.uni_val->buf_len, 0);
440 *key_type = r_o.type;
447 /****************************************************************************
448 do a REG Set Key Security
449 ****************************************************************************/
450 BOOL do_reg_set_key_sec(struct cli_state *cli, POLICY_HND *hnd, SEC_DESC_BUF *sec_desc_buf)
454 REG_Q_SET_KEY_SEC q_o;
455 REG_R_SET_KEY_SEC r_o;
460 prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
461 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
463 /* create and send a MSRPC command with api REG_SET_KEY_SEC */
465 DEBUG(4,("REG Set Key security.\n"));
467 init_reg_q_set_key_sec(&q_o, hnd, sec_desc_buf);
469 /* turn parameters into data stream */
470 if(!reg_io_q_set_key_sec("", &q_o, &buf, 0)) {
476 /* send the data on \PIPE\ */
477 if (!rpc_api_pipe_req(cli, REG_SET_KEY_SEC, &buf, &rbuf)) {
487 if(!reg_io_r_set_key_sec("", &r_o, &rbuf, 0)) {
492 if (r_o.status != 0) {
502 /****************************************************************************
503 do a REG Query Key Security
504 ****************************************************************************/
506 BOOL do_reg_get_key_sec(struct cli_state *cli, POLICY_HND *hnd, uint32 *sec_buf_size, SEC_DESC_BUF **ppsec_desc_buf)
510 REG_Q_GET_KEY_SEC q_o;
511 REG_R_GET_KEY_SEC r_o;
516 prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
517 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
519 /* create and send a MSRPC command with api REG_GET_KEY_SEC */
521 DEBUG(4,("REG query key security. buf_size: %d\n", *sec_buf_size));
523 init_reg_q_get_key_sec(&q_o, hnd, *sec_buf_size, NULL);
525 /* turn parameters into data stream */
526 if(!reg_io_q_get_key_sec("", &q_o, &buf, 0)) {
532 /* send the data on \PIPE\ */
533 if (!rpc_api_pipe_req(cli, REG_GET_KEY_SEC, &buf, &rbuf)) {
543 if(!reg_io_r_get_key_sec("", &r_o, &rbuf, 0)) {
548 if (r_o.status == 0x0000007a) {
550 * get the maximum buffer size: it was too small
552 (*sec_buf_size) = r_o.hdr_sec.buf_max_len;
553 DEBUG(5,("sec_buf_size too small. use %d\n", *sec_buf_size));
554 } else if (r_o.status != 0) {
555 /* report error code */
556 DEBUG(0,("REG_GET_KEY_SEC: %s\n", get_nt_error_msg(r_o.status)));
560 (*sec_buf_size) = r_o.data->len;
561 *ppsec_desc_buf = r_o.data;
569 /****************************************************************************
570 do a REG Delete Value
571 ****************************************************************************/
572 BOOL do_reg_delete_val(struct cli_state *cli, POLICY_HND *hnd, char *val_name)
576 REG_Q_DELETE_VALUE q_o;
577 REG_R_DELETE_VALUE r_o;
582 prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
583 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
585 /* create and send a MSRPC command with api REG_DELETE_VALUE */
587 DEBUG(4,("REG Delete Value: %s\n", val_name));
589 init_reg_q_delete_val(&q_o, hnd, val_name);
591 /* turn parameters into data stream */
592 if(!reg_io_q_delete_val("", &q_o, &buf, 0)) {
598 /* send the data on \PIPE\ */
599 if (rpc_api_pipe_req(cli, REG_DELETE_VALUE, &buf, &rbuf)) {
609 if(!reg_io_r_delete_val("", &r_o, &rbuf, 0)) {
614 if (r_o.status != 0) {
615 /* report error code */
616 DEBUG(0,("REG_DELETE_VALUE: %s\n", get_nt_error_msg(r_o.status)));
626 /****************************************************************************
628 ****************************************************************************/
629 BOOL do_reg_delete_key(struct cli_state *cli, POLICY_HND *hnd, char *key_name)
633 REG_Q_DELETE_KEY q_o;
634 REG_R_DELETE_KEY r_o;
639 prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
640 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
642 /* create and send a MSRPC command with api REG_DELETE_KEY */
644 DEBUG(4,("REG Delete Key: %s\n", key_name));
646 init_reg_q_delete_key(&q_o, hnd, key_name);
648 /* turn parameters into data stream */
649 if(!reg_io_q_delete_key("", &q_o, &buf, 0)) {
655 /* send the data on \PIPE\ */
656 if (!rpc_api_pipe_req(cli, REG_DELETE_KEY, &buf, &rbuf)) {
666 if(!reg_io_r_delete_key("", &r_o, &rbuf, 0)) {
671 if (r_o.status != 0) {
672 /* report error code */
673 DEBUG(0,("REG_DELETE_KEY: %s\n", get_nt_error_msg(r_o.status)));
683 /****************************************************************************
685 ****************************************************************************/
686 BOOL do_reg_create_key(struct cli_state *cli, POLICY_HND *hnd,
687 char *key_name, char *key_class,
688 SEC_ACCESS *sam_access,
693 REG_Q_CREATE_KEY q_o;
694 REG_R_CREATE_KEY r_o;
695 SEC_DESC *sec = NULL;
696 SEC_DESC_BUF *sec_buf = NULL;
704 /* create and send a MSRPC command with api REG_CREATE_KEY */
706 DEBUG(4,("REG Create Key: %s %s 0x%08x\n", key_name, key_class,
707 sam_access != NULL ? sam_access->mask : 0));
709 if((sec = make_sec_desc( cli->mem_ctx, 1, NULL, NULL, NULL, NULL, &sec_len)) == NULL) {
710 DEBUG(0,("make_sec_desc : malloc fail.\n"));
714 DEBUG(10,("make_sec_desc: len = %d\n", (int)sec_len));
716 if((sec_buf = make_sec_desc_buf( cli->mem_ctx, (int)sec_len, sec)) == NULL) {
717 DEBUG(0,("make_sec_desc : malloc fail (1)\n"));
721 prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
722 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
724 init_reg_q_create_key(&q_o, hnd, key_name, key_class, sam_access, sec_buf);
726 /* turn parameters into data stream */
727 if(!reg_io_q_create_key("", &q_o, &buf, 0)) {
733 /* send the data on \PIPE\ */
734 if (rpc_api_pipe_req(cli, REG_CREATE_KEY, &buf, &rbuf)) {
744 if(!reg_io_r_create_key("", &r_o, &rbuf, 0)) {
749 if (r_o.status != 0) {
750 /* report error code */
751 DEBUG(0,("REG_CREATE_KEY: %s\n", get_nt_error_msg(r_o.status)));
763 /****************************************************************************
765 ****************************************************************************/
766 BOOL do_reg_enum_key(struct cli_state *cli, POLICY_HND *hnd,
767 int key_index, char *key_name,
768 uint32 *unk_1, uint32 *unk_2,
779 prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
780 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
782 /* create and send a MSRPC command with api REG_ENUM_KEY */
784 DEBUG(4,("REG Enum Key\n"));
786 init_reg_q_enum_key(&q_o, hnd, key_index);
788 /* turn parameters into data stream */
789 if(!reg_io_q_enum_key("", &q_o, &buf, 0)) {
795 /* send the data on \PIPE\ */
796 if (!rpc_api_pipe_req(cli, REG_ENUM_KEY, &buf, &rbuf)) {
806 if(!reg_io_r_enum_key("", &r_o, &rbuf, 0)) {
811 if (r_o.status != 0) {
812 /* report error code */
813 DEBUG(0,("REG_ENUM_KEY: %s\n", get_nt_error_msg(r_o.status)));
818 (*unk_1) = r_o.unknown_1;
819 (*unk_2) = r_o.unknown_2;
820 rpcstr_pull(key_name, r_o.key_name.str.buffer, -1, -1, 0);
821 (*mod_time) = nt_time_to_unix(&r_o.time);
828 /****************************************************************************
829 do a REG Create Value
830 ****************************************************************************/
831 BOOL do_reg_create_val(struct cli_state *cli, POLICY_HND *hnd,
832 char *val_name, uint32 type, BUFFER3 *data)
836 REG_Q_CREATE_VALUE q_o;
837 REG_R_CREATE_VALUE r_o;
842 prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
843 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
845 /* create and send a MSRPC command with api REG_CREATE_VALUE */
847 DEBUG(4,("REG Create Value: %s\n", val_name));
849 init_reg_q_create_val(&q_o, hnd, val_name, type, data);
851 /* turn parameters into data stream */
852 if(!reg_io_q_create_val("", &q_o, &buf, 0)) {
858 /* send the data on \PIPE\ */
859 if (!rpc_api_pipe_req(cli, REG_CREATE_VALUE, &buf, &rbuf)) {
869 if(!reg_io_r_create_val("", &r_o, &rbuf, 0)) {
874 if (r_o.status != 0) {
875 /* report error code */
876 DEBUG(0,("REG_CREATE_VALUE: %s\n", get_nt_error_msg(r_o.status)));
886 /****************************************************************************
888 ****************************************************************************/
889 BOOL do_reg_enum_val(struct cli_state *cli, POLICY_HND *hnd,
890 int val_index, int max_valnamelen, int max_valbufsize,
892 uint32 *val_type, BUFFER2 *value)
896 REG_Q_ENUM_VALUE q_o;
897 REG_R_ENUM_VALUE r_o;
902 prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
903 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
905 /* create and send a MSRPC command with api REG_ENUM_VALUE */
907 DEBUG(4,("REG Enum Value\n"));
909 init_reg_q_enum_val(&q_o, hnd, val_index, max_valnamelen, max_valbufsize);
911 /* turn parameters into data stream */
912 if(!reg_io_q_enum_val("", &q_o, &buf, 0)) {
918 /* send the data on \PIPE\ */
919 if (!rpc_api_pipe_req(cli, REG_ENUM_VALUE, &buf, &rbuf)) {
928 r_o.buf_value = value;
930 if(!reg_io_r_enum_val("", &r_o, &rbuf, 0)) {
935 if (r_o.status != 0) {
936 /* report error code */
937 DEBUG(0,("REG_ENUM_VALUE: %s\n", get_nt_error_msg(r_o.status)));
942 (*val_type) = r_o.type;
943 rpcstr_pull(val_name, &r_o.uni_name, -1, -1, 0);
950 /****************************************************************************
952 ****************************************************************************/
953 BOOL do_reg_open_entry(struct cli_state *cli, POLICY_HND *hnd,
954 char *key_name, uint32 unk_0,
959 REG_Q_OPEN_ENTRY q_o;
960 REG_R_OPEN_ENTRY r_o;
965 prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
966 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
968 /* create and send a MSRPC command with api REG_OPEN_ENTRY */
970 DEBUG(4,("REG Open Entry\n"));
972 init_reg_q_open_entry(&q_o, hnd, key_name, unk_0);
974 /* turn parameters into data stream */
975 if(!reg_io_q_open_entry("", &q_o, &buf, 0)) {
981 /* send the data on \PIPE\ */
982 if (!rpc_api_pipe_req(cli, REG_OPEN_ENTRY, &buf, &rbuf)) {
992 if(!reg_io_r_open_entry("", &r_o, &rbuf, 0)) {
997 if (r_o.status != 0) {
998 /* report error code */
999 DEBUG(0,("REG_OPEN_ENTRY: %s\n", get_nt_error_msg(r_o.status)));
1000 prs_mem_free(&rbuf);
1006 prs_mem_free(&rbuf);
1011 /****************************************************************************
1013 ****************************************************************************/
1014 BOOL do_reg_close(struct cli_state *cli, POLICY_HND *hnd)
1024 /* create and send a MSRPC command with api REG_CLOSE */
1026 prs_init(&buf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
1027 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
1029 DEBUG(4,("REG Close\n"));
1031 /* store the parameters */
1032 init_reg_q_close(&q_c, hnd);
1034 /* turn parameters into data stream */
1035 if(!reg_io_q_close("", &q_c, &buf, 0)) {
1037 prs_mem_free(&rbuf);
1041 /* send the data on \PIPE\ */
1042 if (!rpc_api_pipe_req(cli, REG_CLOSE, &buf, &rbuf)) {
1044 prs_mem_free(&rbuf);
1052 if(!reg_io_r_close("", &r_c, &rbuf, 0)) {
1053 prs_mem_free(&rbuf);
1057 if (r_c.status != 0) {
1058 /* report error code */
1059 DEBUG(0,("REG_CLOSE: %s\n", get_nt_error_msg(r_c.status)));
1060 prs_mem_free(&rbuf);
1064 /* check that the returned policy handle is all zeros */
1066 if (IVAL(&r_c.pol.data1,0) || IVAL(&r_c.pol.data2,0) || SVAL(&r_c.pol.data3,0) ||
1067 SVAL(&r_c.pol.data4,0) || IVAL(r_c.pol.data5,0) || IVAL(r_c.pol.data5,4) ) {
1068 prs_mem_free(&rbuf);
1069 DEBUG(0,("REG_CLOSE: non-zero handle returned\n"));
1073 prs_mem_free(&rbuf);