2 Unix SMB/CIFS implementation.
5 Copyright (C) Guenther Deschner 2009
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "rpcclient.h"
23 #include "../librpc/gen_ndr/cli_winreg.h"
24 #include "../librpc/gen_ndr/ndr_misc.h"
26 static WERROR
cmd_winreg_enumkeys(struct rpc_pipe_client
*cli
,
27 TALLOC_CTX
*mem_ctx
, int argc
,
32 struct policy_handle handle
;
33 uint32_t enum_index
= 0;
34 struct winreg_StringBuf name
;
37 printf("usage: %s [name]\n", argv
[0]);
41 status
= rpccli_winreg_OpenHKLM(cli
, mem_ctx
,
43 SEC_FLAG_MAXIMUM_ALLOWED
,
46 if (!NT_STATUS_IS_OK(status
)) {
47 return ntstatus_to_werror(status
);
49 if (!W_ERROR_IS_OK(werr
)) {
56 name
.length
= strlen_m_term_null(name
.name
)*2;
57 name
.size
= name
.length
;
59 status
= rpccli_winreg_EnumKey(cli
, mem_ctx
,
66 if (!NT_STATUS_IS_OK(status
)) {
67 return ntstatus_to_werror(status
);
69 if (!W_ERROR_IS_OK(werr
)) {
76 /****************************************************************************
77 ****************************************************************************/
79 static WERROR
pull_winreg_Data(TALLOC_CTX
*mem_ctx
,
80 const DATA_BLOB
*blob
,
81 union winreg_Data
*data
,
82 enum winreg_Type type
)
84 enum ndr_err_code ndr_err
;
85 ndr_err
= ndr_pull_union_blob(blob
, mem_ctx
, data
, type
,
86 (ndr_pull_flags_fn_t
)ndr_pull_winreg_Data
);
87 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
88 return WERR_GENERAL_FAILURE
;
93 /****************************************************************************
94 ****************************************************************************/
96 static void display_winreg_data(const char *v
,
97 enum winreg_Type type
,
103 DATA_BLOB blob
= data_blob_const(data
, length
);
106 result
= pull_winreg_Data(talloc_tos(), &blob
, &r
, type
);
107 if (!W_ERROR_IS_OK(result
)) {
113 printf("%s: REG_DWORD: 0x%08x\n", v
, r
.value
);
116 printf("%s: REG_SZ: %s\n", v
, r
.string
);
119 char *hex
= hex_encode_talloc(NULL
,
120 r
.binary
.data
, r
.binary
.length
);
122 printf("%s: REG_BINARY:", v
);
124 for (i
=0; i
<len
; i
++) {
125 if (hex
[i
] == '\0') {
138 printf("%s: REG_MULTI_SZ: ", v
);
139 for (i
=0; r
.string_array
[i
] != NULL
; i
++) {
140 printf("%s ", r
.string_array
[i
]);
145 printf("%s: unknown type 0x%02x:\n", v
, type
);
151 static WERROR
cmd_winreg_querymultiplevalues_ex(struct rpc_pipe_client
*cli
,
152 TALLOC_CTX
*mem_ctx
, int argc
,
153 const char **argv
, bool multiplevalues2
)
157 struct policy_handle handle
, key_handle
;
158 struct winreg_String key_name
;
160 struct QueryMultipleValue
*values_in
, *values_out
;
162 uint8_t *buffer
= NULL
;
167 printf("usage: %s [key] [value1] [value2] ...\n", argv
[0]);
171 status
= rpccli_winreg_OpenHKLM(cli
, mem_ctx
,
173 SEC_FLAG_MAXIMUM_ALLOWED
,
176 if (!NT_STATUS_IS_OK(status
)) {
177 return ntstatus_to_werror(status
);
179 if (!W_ERROR_IS_OK(werr
)) {
183 key_name
.name
= argv
[1];
185 status
= rpccli_winreg_OpenKey(cli
, mem_ctx
,
189 SEC_FLAG_MAXIMUM_ALLOWED
,
192 if (!NT_STATUS_IS_OK(status
)) {
193 return ntstatus_to_werror(status
);
195 if (!W_ERROR_IS_OK(werr
)) {
201 values_in
= talloc_zero_array(mem_ctx
, struct QueryMultipleValue
, num_values
);
202 if (values_in
== NULL
) {
206 values_out
= talloc_zero_array(mem_ctx
, struct QueryMultipleValue
, num_values
);
207 if (values_out
== NULL
) {
211 for (i
=0; i
< num_values
; i
++) {
213 values_in
[i
].ve_valuename
= talloc_zero(values_in
, struct winreg_ValNameBuf
);
214 if (values_in
[i
].ve_valuename
== NULL
) {
218 values_in
[i
].ve_valuename
->name
= talloc_strdup(values_in
[i
].ve_valuename
, argv
[i
+2]);
219 values_in
[i
].ve_valuename
->length
= strlen_m_term_null(values_in
[i
].ve_valuename
->name
)*2;
220 values_in
[i
].ve_valuename
->size
= values_in
[i
].ve_valuename
->length
;
223 if (multiplevalues2
) {
225 uint32_t offered
= 0, needed
= 0;
227 status
= rpccli_winreg_QueryMultipleValues2(cli
, mem_ctx
,
236 if (W_ERROR_EQUAL(werr
, WERR_MORE_DATA
)) {
239 buffer
= talloc_zero_array(mem_ctx
, uint8_t, needed
);
240 if (buffer
== NULL
) {
244 status
= rpccli_winreg_QueryMultipleValues2(cli
, mem_ctx
,
253 if (!NT_STATUS_IS_OK(status
)) {
254 return ntstatus_to_werror(status
);
256 if (!W_ERROR_IS_OK(werr
)) {
263 uint32_t buffer_size
= 0xff;
265 buffer
= talloc_zero_array(mem_ctx
, uint8_t, buffer_size
);
266 if (buffer
== NULL
) {
270 status
= rpccli_winreg_QueryMultipleValues(cli
, mem_ctx
,
278 if (!NT_STATUS_IS_OK(status
)) {
279 return ntstatus_to_werror(status
);
281 if (!W_ERROR_IS_OK(werr
)) {
286 for (i
=0; i
< num_values
; i
++) {
288 display_winreg_data(values_in
[i
].ve_valuename
->name
,
289 values_out
[i
].ve_type
,
290 buffer
+ values_out
[i
].ve_valueptr
,
291 values_out
[i
].ve_valuelen
);
298 static WERROR
cmd_winreg_querymultiplevalues(struct rpc_pipe_client
*cli
,
299 TALLOC_CTX
*mem_ctx
, int argc
,
302 return cmd_winreg_querymultiplevalues_ex(cli
, mem_ctx
, argc
, argv
, false);
305 static WERROR
cmd_winreg_querymultiplevalues2(struct rpc_pipe_client
*cli
,
306 TALLOC_CTX
*mem_ctx
, int argc
,
309 return cmd_winreg_querymultiplevalues_ex(cli
, mem_ctx
, argc
, argv
, true);
312 /* List of commands exported by this module */
314 struct cmd_set winreg_commands
[] = {
317 { "enumkey", RPC_RTYPE_WERROR
, NULL
, cmd_winreg_enumkeys
, &ndr_table_winreg
.syntax_id
, NULL
, "Enumerate Keys", "" },
318 { "querymultiplevalues", RPC_RTYPE_WERROR
, NULL
, cmd_winreg_querymultiplevalues
, &ndr_table_winreg
.syntax_id
, NULL
, "Query multiple values", "" },
319 { "querymultiplevalues2", RPC_RTYPE_WERROR
, NULL
, cmd_winreg_querymultiplevalues2
, &ndr_table_winreg
.syntax_id
, NULL
, "Query multiple values", "" },