s3-rpc_client: Added a winreg set expand sz helper.
[Samba.git] / source3 / rpc_client / cli_winreg.c
blob640820b928cfcdccdb8519d14ef6ef8799b82c3b
1 /*
2 * Unix SMB/CIFS implementation.
4 * WINREG client routines
6 * Copyright (c) 2011 Andreas Schneider <asn@samba.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "../librpc/gen_ndr/ndr_winreg_c.h"
24 #include "rpc_client/cli_winreg.h"
26 NTSTATUS dcerpc_winreg_query_dword(TALLOC_CTX *mem_ctx,
27 struct dcerpc_binding_handle *h,
28 struct policy_handle *key_handle,
29 const char *value,
30 uint32_t *data,
31 WERROR *pwerr)
33 struct winreg_String wvalue;
34 enum winreg_Type type;
35 uint32_t value_len = 0;
36 uint32_t data_size = 0;
37 WERROR result = WERR_OK;
38 NTSTATUS status;
39 DATA_BLOB blob;
41 wvalue.name = value;
43 status = dcerpc_winreg_QueryValue(h,
44 mem_ctx,
45 key_handle,
46 &wvalue,
47 &type,
48 NULL,
49 &data_size,
50 &value_len,
51 &result);
52 if (!NT_STATUS_IS_OK(status)) {
53 return status;
55 if (!W_ERROR_IS_OK(result)) {
56 *pwerr = result;
57 return status;
60 if (type != REG_DWORD) {
61 *pwerr = WERR_INVALID_DATATYPE;
62 return status;
65 if (data_size != 4) {
66 *pwerr = WERR_INVALID_DATA;
67 return status;
70 blob = data_blob_talloc(mem_ctx, NULL, data_size);
71 if (blob.data == NULL) {
72 *pwerr = WERR_NOMEM;
73 return status;
75 value_len = 0;
77 status = dcerpc_winreg_QueryValue(h,
78 mem_ctx,
79 key_handle,
80 &wvalue,
81 &type,
82 blob.data,
83 &data_size,
84 &value_len,
85 &result);
86 if (!NT_STATUS_IS_OK(status)) {
87 return status;
89 if (!W_ERROR_IS_OK(result)) {
90 *pwerr = result;
91 return status;
94 if (data) {
95 *data = IVAL(blob.data, 0);
98 return status;
101 NTSTATUS dcerpc_winreg_query_binary(TALLOC_CTX *mem_ctx,
102 struct dcerpc_binding_handle *h,
103 struct policy_handle *key_handle,
104 const char *value,
105 DATA_BLOB *data,
106 WERROR *pwerr)
108 struct winreg_String wvalue;
109 enum winreg_Type type;
110 WERROR result = WERR_OK;
111 uint32_t value_len = 0;
112 uint32_t data_size = 0;
113 NTSTATUS status;
114 DATA_BLOB blob;
116 wvalue.name = value;
118 status = dcerpc_winreg_QueryValue(h,
119 mem_ctx,
120 key_handle,
121 &wvalue,
122 &type,
123 NULL,
124 &data_size,
125 &value_len,
126 &result);
127 if (!NT_STATUS_IS_OK(status)) {
128 return status;
130 if (!W_ERROR_IS_OK(result)) {
131 *pwerr = result;
132 return status;
135 if (type != REG_BINARY) {
136 *pwerr = WERR_INVALID_DATATYPE;
137 return status;
140 blob = data_blob_talloc(mem_ctx, NULL, data_size);
141 if (blob.data == NULL) {
142 *pwerr = WERR_NOMEM;
143 return status;
145 value_len = 0;
147 status = dcerpc_winreg_QueryValue(h,
148 mem_ctx,
149 key_handle,
150 &wvalue,
151 &type,
152 blob.data,
153 &data_size,
154 &value_len,
155 &result);
156 if (!NT_STATUS_IS_OK(status)) {
157 return status;
159 if (!W_ERROR_IS_OK(result)) {
160 *pwerr = result;
161 return status;
164 if (data) {
165 data->data = blob.data;
166 data->length = blob.length;
169 return status;
172 NTSTATUS dcerpc_winreg_set_dword(TALLOC_CTX *mem_ctx,
173 struct dcerpc_binding_handle *h,
174 struct policy_handle *key_handle,
175 const char *value,
176 uint32_t data,
177 WERROR *pwerr)
179 struct winreg_String wvalue;
180 DATA_BLOB blob;
181 WERROR result = WERR_OK;
182 NTSTATUS status;
184 wvalue.name = value;
185 blob = data_blob_talloc(mem_ctx, NULL, 4);
186 SIVAL(blob.data, 0, data);
188 status = dcerpc_winreg_SetValue(h,
189 mem_ctx,
190 key_handle,
191 wvalue,
192 REG_DWORD,
193 blob.data,
194 blob.length,
195 &result);
196 if (!NT_STATUS_IS_OK(status)) {
197 return status;
199 if (!W_ERROR_IS_OK(result)) {
200 *pwerr = result;
203 return status;
206 NTSTATUS dcerpc_winreg_set_sz(TALLOC_CTX *mem_ctx,
207 struct dcerpc_binding_handle *h,
208 struct policy_handle *key_handle,
209 const char *value,
210 const char *data,
211 WERROR *pwerr)
213 struct winreg_String wvalue;
214 DATA_BLOB blob;
215 WERROR result = WERR_OK;
216 NTSTATUS status;
218 wvalue.name = value;
219 if (data == NULL) {
220 blob = data_blob_string_const("");
221 } else {
222 if (!push_reg_sz(mem_ctx, &blob, data)) {
223 DEBUG(2, ("dcerpc_winreg_set_sz: Could not marshall "
224 "string %s for %s\n",
225 data, wvalue.name));
226 *pwerr = WERR_NOMEM;
227 return NT_STATUS_OK;
231 status = dcerpc_winreg_SetValue(h,
232 mem_ctx,
233 key_handle,
234 wvalue,
235 REG_SZ,
236 blob.data,
237 blob.length,
238 &result);
239 if (!NT_STATUS_IS_OK(status)) {
240 return status;
242 if (!W_ERROR_IS_OK(result)) {
243 *pwerr = result;
246 return status;
249 NTSTATUS dcerpc_winreg_set_expand_sz(TALLOC_CTX *mem_ctx,
250 struct dcerpc_binding_handle *h,
251 struct policy_handle *key_handle,
252 const char *value,
253 const char *data,
254 WERROR *pwerr)
256 struct winreg_String wvalue;
257 DATA_BLOB blob;
258 WERROR result = WERR_OK;
259 NTSTATUS status;
261 wvalue.name = value;
262 if (data == NULL) {
263 blob = data_blob_string_const("");
264 } else {
265 if (!push_reg_sz(mem_ctx, &blob, data)) {
266 DEBUG(2, ("dcerpc_winreg_set_expand_sz: Could not marshall "
267 "string %s for %s\n",
268 data, wvalue.name));
269 *pwerr = WERR_NOMEM;
270 return NT_STATUS_OK;
274 status = dcerpc_winreg_SetValue(h,
275 mem_ctx,
276 key_handle,
277 wvalue,
278 REG_EXPAND_SZ,
279 blob.data,
280 blob.length,
281 &result);
282 if (!NT_STATUS_IS_OK(status)) {
283 return status;
285 if (!W_ERROR_IS_OK(result)) {
286 *pwerr = result;
289 return status;
292 /* vim: set ts=8 sw=8 noet cindent syntax=c.doxygen: */