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 *type, uint32 *unk_0, uint32 *unk_1)
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", time(NULL), 4, 1);
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(type, dos_buffer2_to_str(&r_o.uni_type));
442 (*unk_0) = r_o.unknown_0;
443 (*unk_1) = r_o.unknown_1;
450 /****************************************************************************
451 do a REG Set Key Security
452 ****************************************************************************/
453 BOOL do_reg_set_key_sec(struct cli_state *cli, POLICY_HND *hnd, SEC_DESC_BUF *sec_desc_buf)
457 REG_Q_SET_KEY_SEC q_o;
458 REG_R_SET_KEY_SEC r_o;
463 prs_init(&buf , MAX_PDU_FRAG_LEN, 4, MARSHALL);
464 prs_init(&rbuf, 0, 4, UNMARSHALL);
466 /* create and send a MSRPC command with api REG_SET_KEY_SEC */
468 DEBUG(4,("REG Set Key security.\n"));
470 init_reg_q_set_key_sec(&q_o, hnd, sec_desc_buf);
472 /* turn parameters into data stream */
473 if(!reg_io_q_set_key_sec("", &q_o, &buf, 0)) {
479 /* send the data on \PIPE\ */
480 if (!rpc_api_pipe_req(cli, REG_SET_KEY_SEC, &buf, &rbuf)) {
490 if(!reg_io_r_set_key_sec("", &r_o, &rbuf, 0)) {
495 if (r_o.status != 0) {
505 /****************************************************************************
506 do a REG Query Key Security
507 ****************************************************************************/
509 BOOL do_reg_get_key_sec(struct cli_state *cli, POLICY_HND *hnd, uint32 *sec_buf_size, SEC_DESC_BUF **ppsec_desc_buf)
513 REG_Q_GET_KEY_SEC q_o;
514 REG_R_GET_KEY_SEC r_o;
519 prs_init(&buf , MAX_PDU_FRAG_LEN, 4, MARSHALL);
520 prs_init(&rbuf, 0, 4, UNMARSHALL);
522 /* create and send a MSRPC command with api REG_GET_KEY_SEC */
524 DEBUG(4,("REG query key security. buf_size: %d\n", *sec_buf_size));
526 init_reg_q_get_key_sec(&q_o, hnd, *sec_buf_size, NULL);
528 /* turn parameters into data stream */
529 if(!reg_io_q_get_key_sec("", &q_o, &buf, 0)) {
535 /* send the data on \PIPE\ */
536 if (!rpc_api_pipe_req(cli, REG_GET_KEY_SEC, &buf, &rbuf)) {
546 if(!reg_io_r_get_key_sec("", &r_o, &rbuf, 0)) {
551 if (r_o.status == 0x0000007a) {
553 * get the maximum buffer size: it was too small
555 (*sec_buf_size) = r_o.hdr_sec.buf_max_len;
556 DEBUG(5,("sec_buf_size too small. use %d\n", *sec_buf_size));
557 } else if (r_o.status != 0) {
558 /* report error code */
559 DEBUG(0,("REG_GET_KEY_SEC: %s\n", get_nt_error_msg(r_o.status)));
563 (*sec_buf_size) = r_o.data->len;
564 *ppsec_desc_buf = r_o.data;
572 /****************************************************************************
573 do a REG Delete Value
574 ****************************************************************************/
575 BOOL do_reg_delete_val(struct cli_state *cli, POLICY_HND *hnd, char *val_name)
579 REG_Q_DELETE_VALUE q_o;
580 REG_R_DELETE_VALUE r_o;
585 prs_init(&buf , MAX_PDU_FRAG_LEN, 4, MARSHALL);
586 prs_init(&rbuf, 0, 4, UNMARSHALL);
588 /* create and send a MSRPC command with api REG_DELETE_VALUE */
590 DEBUG(4,("REG Delete Value: %s\n", val_name));
592 init_reg_q_delete_val(&q_o, hnd, val_name);
594 /* turn parameters into data stream */
595 if(!reg_io_q_delete_val("", &q_o, &buf, 0)) {
601 /* send the data on \PIPE\ */
602 if (rpc_api_pipe_req(cli, REG_DELETE_VALUE, &buf, &rbuf)) {
612 if(!reg_io_r_delete_val("", &r_o, &rbuf, 0)) {
617 if (r_o.status != 0) {
618 /* report error code */
619 DEBUG(0,("REG_DELETE_VALUE: %s\n", get_nt_error_msg(r_o.status)));
629 /****************************************************************************
631 ****************************************************************************/
632 BOOL do_reg_delete_key(struct cli_state *cli, POLICY_HND *hnd, char *key_name)
636 REG_Q_DELETE_KEY q_o;
637 REG_R_DELETE_KEY r_o;
642 prs_init(&buf , MAX_PDU_FRAG_LEN, 4, MARSHALL);
643 prs_init(&rbuf, 0, 4, UNMARSHALL);
645 /* create and send a MSRPC command with api REG_DELETE_KEY */
647 DEBUG(4,("REG Delete Key: %s\n", key_name));
649 init_reg_q_delete_key(&q_o, hnd, key_name);
651 /* turn parameters into data stream */
652 if(!reg_io_q_delete_key("", &q_o, &buf, 0)) {
658 /* send the data on \PIPE\ */
659 if (!rpc_api_pipe_req(cli, REG_DELETE_KEY, &buf, &rbuf)) {
669 if(!reg_io_r_delete_key("", &r_o, &rbuf, 0)) {
674 if (r_o.status != 0) {
675 /* report error code */
676 DEBUG(0,("REG_DELETE_KEY: %s\n", get_nt_error_msg(r_o.status)));
686 /****************************************************************************
688 ****************************************************************************/
689 BOOL do_reg_create_key(struct cli_state *cli, POLICY_HND *hnd,
690 char *key_name, char *key_class,
691 SEC_ACCESS *sam_access,
696 REG_Q_CREATE_KEY q_o;
697 REG_R_CREATE_KEY r_o;
698 SEC_DESC *sec = NULL;
699 SEC_DESC_BUF *sec_buf = NULL;
707 /* create and send a MSRPC command with api REG_CREATE_KEY */
709 DEBUG(4,("REG Create Key: %s %s 0x%08x\n", key_name, key_class,
710 sam_access != NULL ? sam_access->mask : 0));
712 if((sec = make_sec_desc( 1, SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL, NULL, &sec_len)) == NULL) {
713 DEBUG(0,("make_sec_desc : malloc fail.\n"));
717 DEBUG(10,("make_sec_desc: len = %d\n", (int)sec_len));
719 if((sec_buf = make_sec_desc_buf( (int)sec_len, sec)) == NULL) {
720 DEBUG(0,("make_sec_desc : malloc fail (1)\n"));
726 prs_init(&buf, MAX_PDU_FRAG_LEN, 4, MARSHALL);
727 prs_init(&rbuf, 0, 4, UNMARSHALL);
729 init_reg_q_create_key(&q_o, hnd, key_name, key_class, sam_access, sec_buf);
731 /* turn parameters into data stream */
732 if(!reg_io_q_create_key("", &q_o, &buf, 0)) {
733 free_sec_desc_buf(&sec_buf);
739 /* send the data on \PIPE\ */
740 if (rpc_api_pipe_req(cli, REG_CREATE_KEY, &buf, &rbuf)) {
741 free_sec_desc_buf(&sec_buf);
747 free_sec_desc_buf(&sec_buf);
752 if(!reg_io_r_create_key("", &r_o, &rbuf, 0)) {
757 if (r_o.status != 0) {
758 /* report error code */
759 DEBUG(0,("REG_CREATE_KEY: %s\n", get_nt_error_msg(r_o.status)));
764 memcpy(key, r_o.key_pol.data, sizeof(key->data));
771 /****************************************************************************
773 ****************************************************************************/
774 BOOL do_reg_enum_key(struct cli_state *cli, POLICY_HND *hnd,
775 int key_index, char *key_name,
776 uint32 *unk_1, uint32 *unk_2,
787 prs_init(&buf, MAX_PDU_FRAG_LEN, 4, MARSHALL);
788 prs_init(&rbuf, 0, 4, UNMARSHALL);
790 /* create and send a MSRPC command with api REG_ENUM_KEY */
792 DEBUG(4,("REG Enum Key\n"));
794 init_reg_q_enum_key(&q_o, hnd, key_index);
796 /* turn parameters into data stream */
797 if(!reg_io_q_enum_key("", &q_o, &buf, 0)) {
803 /* send the data on \PIPE\ */
804 if (!rpc_api_pipe_req(cli, REG_ENUM_KEY, &buf, &rbuf)) {
814 if(!reg_io_r_enum_key("", &r_o, &rbuf, 0)) {
819 if (r_o.status != 0) {
820 /* report error code */
821 DEBUG(0,("REG_ENUM_KEY: %s\n", get_nt_error_msg(r_o.status)));
826 (*unk_1) = r_o.unknown_1;
827 (*unk_2) = r_o.unknown_2;
828 fstrcpy(key_name, dos_unistr2(r_o.key_name.str.buffer));
829 (*mod_time) = nt_time_to_unix(&r_o.time);
836 /****************************************************************************
837 do a REG Create Value
838 ****************************************************************************/
839 BOOL do_reg_create_val(struct cli_state *cli, POLICY_HND *hnd,
840 char *val_name, uint32 type, BUFFER3 *data)
844 REG_Q_CREATE_VALUE q_o;
845 REG_R_CREATE_VALUE r_o;
850 prs_init(&buf, MAX_PDU_FRAG_LEN, 4, MARSHALL);
851 prs_init(&rbuf, 0, 4, UNMARSHALL);
853 /* create and send a MSRPC command with api REG_CREATE_VALUE */
855 DEBUG(4,("REG Create Value: %s\n", val_name));
857 init_reg_q_create_val(&q_o, hnd, val_name, type, data);
859 /* turn parameters into data stream */
860 if(!reg_io_q_create_val("", &q_o, &buf, 0)) {
866 /* send the data on \PIPE\ */
867 if (!rpc_api_pipe_req(cli, REG_CREATE_VALUE, &buf, &rbuf)) {
877 if(!reg_io_r_create_val("", &r_o, &rbuf, 0)) {
882 if (r_o.status != 0) {
883 /* report error code */
884 DEBUG(0,("REG_CREATE_VALUE: %s\n", get_nt_error_msg(r_o.status)));
894 /****************************************************************************
896 ****************************************************************************/
897 BOOL do_reg_enum_val(struct cli_state *cli, POLICY_HND *hnd,
898 int val_index, int max_valnamelen, int max_valbufsize,
900 uint32 *val_type, BUFFER2 *value)
904 REG_Q_ENUM_VALUE q_o;
905 REG_R_ENUM_VALUE r_o;
910 prs_init(&buf, MAX_PDU_FRAG_LEN, 4, MARSHALL);
911 prs_init(&rbuf, 0, 4, UNMARSHALL);
913 /* create and send a MSRPC command with api REG_ENUM_VALUE */
915 DEBUG(4,("REG Enum Value\n"));
917 init_reg_q_enum_val(&q_o, hnd, val_index, max_valnamelen, max_valbufsize);
919 /* turn parameters into data stream */
920 if(!reg_io_q_enum_val("", &q_o, &buf, 0)) {
926 /* send the data on \PIPE\ */
927 if (!rpc_api_pipe_req(cli, REG_ENUM_VALUE, &buf, &rbuf)) {
936 r_o.buf_value = value;
938 if(!reg_io_r_enum_val("", &r_o, &rbuf, 0)) {
943 if (r_o.status != 0) {
944 /* report error code */
945 DEBUG(0,("REG_ENUM_VALUE: %s\n", get_nt_error_msg(r_o.status)));
950 (*val_type) = r_o.type;
951 fstrcpy(val_name, dos_unistr2_to_str(&r_o.uni_name));
958 /****************************************************************************
960 ****************************************************************************/
961 BOOL do_reg_open_entry(struct cli_state *cli, POLICY_HND *hnd,
962 char *key_name, uint32 unk_0,
967 REG_Q_OPEN_ENTRY q_o;
968 REG_R_OPEN_ENTRY r_o;
973 prs_init(&buf, MAX_PDU_FRAG_LEN, 4, MARSHALL);
974 prs_init(&rbuf, 0, 4, UNMARSHALL);
976 /* create and send a MSRPC command with api REG_OPEN_ENTRY */
978 DEBUG(4,("REG Open Entry\n"));
980 init_reg_q_open_entry(&q_o, hnd, key_name, unk_0);
982 /* turn parameters into data stream */
983 if(!reg_io_q_open_entry("", &q_o, &buf, 0)) {
989 /* send the data on \PIPE\ */
990 if (!rpc_api_pipe_req(cli, REG_OPEN_ENTRY, &buf, &rbuf)) {
1000 if(!reg_io_r_open_entry("", &r_o, &rbuf, 0)) {
1001 prs_mem_free(&rbuf);
1005 if (r_o.status != 0) {
1006 /* report error code */
1007 DEBUG(0,("REG_OPEN_ENTRY: %s\n", get_nt_error_msg(r_o.status)));
1008 prs_mem_free(&rbuf);
1012 memcpy(key_hnd, r_o.pol.data, sizeof(key_hnd->data));
1014 prs_mem_free(&rbuf);
1019 /****************************************************************************
1021 ****************************************************************************/
1022 BOOL do_reg_close(struct cli_state *cli, POLICY_HND *hnd)
1033 /* create and send a MSRPC command with api REG_CLOSE */
1035 prs_init(&buf, MAX_PDU_FRAG_LEN, 4, MARSHALL);
1036 prs_init(&rbuf, 0, 4, UNMARSHALL);
1038 DEBUG(4,("REG Close\n"));
1040 /* store the parameters */
1041 init_reg_q_close(&q_c, hnd);
1043 /* turn parameters into data stream */
1044 if(!reg_io_q_close("", &q_c, &buf, 0)) {
1046 prs_mem_free(&rbuf);
1050 /* send the data on \PIPE\ */
1051 if (!rpc_api_pipe_req(cli, REG_CLOSE, &buf, &rbuf)) {
1053 prs_mem_free(&rbuf);
1061 if(!reg_io_r_close("", &r_c, &rbuf, 0)) {
1062 prs_mem_free(&rbuf);
1066 if (r_c.status != 0) {
1067 /* report error code */
1068 DEBUG(0,("REG_CLOSE: %s\n", get_nt_error_msg(r_c.status)));
1069 prs_mem_free(&rbuf);
1073 /* check that the returned policy handle is all zeros */
1075 for (i = 0; i < sizeof(r_c.pol.data); i++) {
1076 if (r_c.pol.data[i] != 0) {
1077 prs_mem_free(&rbuf);
1078 DEBUG(0,("REG_CLOSE: non-zero handle returned\n"));
1083 prs_mem_free(&rbuf);