2 Unix SMB/CIFS implementation.
4 endpoint server for the winreg pipe
6 Copyright (C) Jelmer Vernooij 2004
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/>.
23 #include "rpc_server/dcerpc_server.h"
24 #include "lib/registry/registry.h"
25 #include "librpc/gen_ndr/ndr_winreg.h"
26 #include "rpc_server/common/common.h"
27 #include "librpc/gen_ndr/ndr_security.h"
28 #include "param/param.h"
29 #include "libcli/security/security.h"
31 enum handle_types { HTYPE_REGVAL, HTYPE_REGKEY };
33 static NTSTATUS dcerpc_winreg_bind(struct dcesrv_call_state *dce_call,
34 const struct dcesrv_interface *iface)
36 struct registry_context *ctx;
39 err = reg_open_samba(dce_call->context,
40 &ctx, dce_call->event_ctx, dce_call->conn->dce_ctx->lp_ctx, dce_call->conn->auth_state.session_info,
43 if (!W_ERROR_IS_OK(err)) {
44 DEBUG(0, ("Error opening registry: %s\n", win_errstr(err)));
45 return NT_STATUS_UNSUCCESSFUL;
48 dce_call->context->private = ctx;
53 #define DCESRV_INTERFACE_WINREG_BIND dcerpc_winreg_bind
55 static WERROR dcesrv_winreg_openhive(struct dcesrv_call_state *dce_call,
56 TALLOC_CTX *mem_ctx, uint32_t hkey,
57 struct policy_handle **outh)
59 struct registry_context *ctx = dce_call->context->private;
60 struct dcesrv_handle *h;
63 h = dcesrv_handle_new(dce_call->context, HTYPE_REGKEY);
65 error = reg_get_predefined_key(ctx, hkey,
66 (struct registry_key **)&h->data);
67 if (!W_ERROR_IS_OK(error)) {
71 *outh = &h->wire_handle;
76 #define func_winreg_OpenHive(k,n) static WERROR dcesrv_winreg_Open ## k (struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct winreg_Open ## k *r) \
78 return dcesrv_winreg_openhive (dce_call, mem_ctx, n, &r->out.handle);\
81 func_winreg_OpenHive(HKCR,HKEY_CLASSES_ROOT)
82 func_winreg_OpenHive(HKCU,HKEY_CURRENT_USER)
83 func_winreg_OpenHive(HKLM,HKEY_LOCAL_MACHINE)
84 func_winreg_OpenHive(HKPD,HKEY_PERFORMANCE_DATA)
85 func_winreg_OpenHive(HKU,HKEY_USERS)
86 func_winreg_OpenHive(HKCC,HKEY_CURRENT_CONFIG)
87 func_winreg_OpenHive(HKDD,HKEY_DYN_DATA)
88 func_winreg_OpenHive(HKPT,HKEY_PERFORMANCE_TEXT)
89 func_winreg_OpenHive(HKPN,HKEY_PERFORMANCE_NLSTEXT)
94 static WERROR dcesrv_winreg_CloseKey(struct dcesrv_call_state *dce_call,
96 struct winreg_CloseKey *r)
98 struct dcesrv_handle *h;
100 DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
104 ZERO_STRUCTP(r->out.handle);
112 static WERROR dcesrv_winreg_CreateKey(struct dcesrv_call_state *dce_call,
114 struct winreg_CreateKey *r)
116 struct dcesrv_handle *h, *newh;
118 struct security_descriptor sd;
120 DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
122 newh = dcesrv_handle_new(dce_call->context, HTYPE_REGKEY);
124 switch (security_session_user_level(dce_call->conn->auth_state.session_info))
126 case SECURITY_SYSTEM:
127 case SECURITY_ADMINISTRATOR:
128 /* the security descriptor is optional */
129 if (r->in.secdesc != NULL) {
131 enum ndr_err_code ndr_err;
132 sdblob.data = r->in.secdesc->sd.data;
133 sdblob.length = r->in.secdesc->sd.len;
134 if (sdblob.data == NULL) {
135 return WERR_INVALID_PARAM;
137 ndr_err = ndr_pull_struct_blob_all(&sdblob, mem_ctx, NULL, &sd,
138 (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
139 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
140 return WERR_INVALID_PARAM;
144 error = reg_key_add_name(newh, (struct registry_key *)h->data,
145 r->in.name.name, NULL, r->in.secdesc?&sd:NULL,
146 (struct registry_key **)&newh->data);
147 if (W_ERROR_IS_OK(error)) {
148 r->out.new_handle = &newh->wire_handle;
155 return WERR_ACCESS_DENIED;
163 static WERROR dcesrv_winreg_DeleteKey(struct dcesrv_call_state *dce_call,
165 struct winreg_DeleteKey *r)
167 struct dcesrv_handle *h;
169 DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
171 switch (security_session_user_level(dce_call->conn->auth_state.session_info))
173 case SECURITY_SYSTEM:
174 case SECURITY_ADMINISTRATOR:
175 return reg_key_del((struct registry_key *)h->data, r->in.key.name);
177 return WERR_ACCESS_DENIED;
185 static WERROR dcesrv_winreg_DeleteValue(struct dcesrv_call_state *dce_call,
187 struct winreg_DeleteValue *r)
189 struct dcesrv_handle *h;
190 struct registry_key *key;
192 DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
194 switch (security_session_user_level(dce_call->conn->auth_state.session_info))
196 case SECURITY_SYSTEM:
197 case SECURITY_ADMINISTRATOR:
200 return reg_del_value(key, r->in.value.name);
202 return WERR_ACCESS_DENIED;
210 static WERROR dcesrv_winreg_EnumKey(struct dcesrv_call_state *dce_call,
212 struct winreg_EnumKey *r)
214 struct dcesrv_handle *h;
218 DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
220 r->out.result = reg_key_get_subkey_by_index(mem_ctx,
221 (struct registry_key *)h->data,
223 &name, NULL, &last_mod);
225 if (W_ERROR_IS_OK(r->out.result)) {
226 if (2*strlen_m_term(name) > r->in.name->size) {
227 return WERR_MORE_DATA;
229 r->out.name->length = 2*strlen_m_term(name);
230 r->out.name->name = name;
231 r->out.keyclass = talloc_zero(mem_ctx, struct winreg_StringBuf);
232 if (r->in.last_changed_time) {
233 r->out.last_changed_time = &last_mod;
237 return r->out.result;
244 static WERROR dcesrv_winreg_EnumValue(struct dcesrv_call_state *dce_call,
246 struct winreg_EnumValue *r)
248 struct dcesrv_handle *h;
249 struct registry_key *key;
251 const char *data_name;
255 DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
259 result = reg_key_get_value_by_index(mem_ctx, key, r->in.enum_index,
262 if (!W_ERROR_IS_OK(result)) {
266 /* the client can optionally pass a NULL for type, meaning they don't
268 if (r->in.type != NULL) {
269 r->out.type = talloc(mem_ctx, enum winreg_Type);
270 *r->out.type = data_type;
273 /* check the client has enough room for the value */
274 if (r->in.value != NULL &&
275 r->in.size != NULL &&
276 data.length > *r->in.size) {
277 return WERR_MORE_DATA;
280 /* and enough room for the name */
281 if (r->in.name->size < 2*strlen_m_term(data_name)) {
282 return WERR_MORE_DATA;
285 r->out.name->name = data_name;
286 r->out.name->length = 2*strlen_m_term(data_name);
287 r->out.name->size = r->in.name->size;
290 r->out.value = data.data;
294 r->out.size = talloc(mem_ctx, uint32_t);
295 *r->out.size = data.length;
296 r->out.length = r->out.size;
306 static WERROR dcesrv_winreg_FlushKey(struct dcesrv_call_state *dce_call,
308 struct winreg_FlushKey *r)
310 struct dcesrv_handle *h;
312 DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
314 switch (security_session_user_level(dce_call->conn->auth_state.session_info))
316 case SECURITY_SYSTEM:
317 case SECURITY_ADMINISTRATOR:
318 return reg_key_flush(h->data);
320 return WERR_ACCESS_DENIED;
326 winreg_GetKeySecurity
328 static WERROR dcesrv_winreg_GetKeySecurity(struct dcesrv_call_state *dce_call,
330 struct winreg_GetKeySecurity *r)
332 struct dcesrv_handle *h;
334 DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
336 return WERR_NOT_SUPPORTED;
343 static WERROR dcesrv_winreg_LoadKey(struct dcesrv_call_state *dce_call,
345 struct winreg_LoadKey *r)
347 return WERR_NOT_SUPPORTED;
352 winreg_NotifyChangeKeyValue
354 static WERROR dcesrv_winreg_NotifyChangeKeyValue(struct dcesrv_call_state *dce_call,
356 struct winreg_NotifyChangeKeyValue *r)
358 return WERR_NOT_SUPPORTED;
365 static WERROR dcesrv_winreg_OpenKey(struct dcesrv_call_state *dce_call,
367 struct winreg_OpenKey *r)
369 struct dcesrv_handle *h, *newh;
372 DCESRV_PULL_HANDLE_FAULT(h, r->in.parent_handle, HTYPE_REGKEY);
374 switch (security_session_user_level(dce_call->conn->auth_state.session_info))
376 case SECURITY_SYSTEM:
377 case SECURITY_ADMINISTRATOR:
379 if (r->in.keyname.name && strcmp(r->in.keyname.name, "") == 0) {
380 newh = talloc_reference(dce_call->context, h);
383 newh = dcesrv_handle_new(dce_call->context, HTYPE_REGKEY);
384 result = reg_open_key(newh, (struct registry_key *)h->data,
386 (struct registry_key **)&newh->data);
389 if (W_ERROR_IS_OK(result)) {
390 r->out.handle = &newh->wire_handle;
396 return WERR_ACCESS_DENIED;
405 static WERROR dcesrv_winreg_QueryInfoKey(struct dcesrv_call_state *dce_call,
407 struct winreg_QueryInfoKey *r)
409 struct dcesrv_handle *h;
410 struct registry_key *k;
412 const char *classname = NULL;
414 DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
416 switch (security_session_user_level(dce_call->conn->auth_state.session_info))
418 case SECURITY_SYSTEM:
419 case SECURITY_ADMINISTRATOR:
423 ret = reg_key_get_info(mem_ctx, k, &classname, r->out.num_subkeys,
424 r->out.num_values, r->out.last_changed_time,
425 r->out.max_subkeylen, r->out.max_valnamelen,
426 r->out.max_valbufsize);
428 if (r->out.classname != NULL)
429 r->out.classname->name = classname;
433 return WERR_ACCESS_DENIED;
441 static WERROR dcesrv_winreg_QueryValue(struct dcesrv_call_state *dce_call,
443 struct winreg_QueryValue *r)
445 struct dcesrv_handle *h;
446 struct registry_key *key;
448 DATA_BLOB value_data;
451 DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
453 switch (security_session_user_level(dce_call->conn->auth_state.session_info))
455 case SECURITY_SYSTEM:
456 case SECURITY_ADMINISTRATOR:
460 result = reg_key_get_value_by_name(mem_ctx, key, r->in.value_name.name,
461 &value_type, &value_data);
463 if (!W_ERROR_IS_OK(result)) {
467 /* Just asking for the size of the buffer */
468 r->out.type = talloc(mem_ctx, uint32_t);
472 *r->out.type = value_type;
473 r->out.length = talloc(mem_ctx, uint32_t);
474 if (!r->out.length) {
477 *r->out.length = value_data.length;
478 if (r->in.data == NULL) {
479 r->out.size = talloc(mem_ctx, uint32_t);
480 *r->out.size = value_data.length;
482 r->out.size = r->in.size;
483 r->out.data = value_data.data;
488 return WERR_ACCESS_DENIED;
496 static WERROR dcesrv_winreg_ReplaceKey(struct dcesrv_call_state *dce_call,
498 struct winreg_ReplaceKey *r)
500 return WERR_NOT_SUPPORTED;
507 static WERROR dcesrv_winreg_RestoreKey(struct dcesrv_call_state *dce_call,
509 struct winreg_RestoreKey *r)
511 return WERR_NOT_SUPPORTED;
518 static WERROR dcesrv_winreg_SaveKey(struct dcesrv_call_state *dce_call,
520 struct winreg_SaveKey *r)
522 return WERR_NOT_SUPPORTED;
527 winreg_SetKeySecurity
529 static WERROR dcesrv_winreg_SetKeySecurity(struct dcesrv_call_state *dce_call,
531 struct winreg_SetKeySecurity *r)
533 return WERR_NOT_SUPPORTED;
540 static WERROR dcesrv_winreg_SetValue(struct dcesrv_call_state *dce_call,
542 struct winreg_SetValue *r)
544 struct dcesrv_handle *h;
545 struct registry_key *key;
549 DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
553 switch (security_session_user_level(dce_call->conn->auth_state.session_info))
555 case SECURITY_SYSTEM:
556 case SECURITY_ADMINISTRATOR:
557 data.data = r->in.data;
558 data.length = r->in.size;
559 result = reg_val_set(key, r->in.name.name, r->in.type, data);
562 return WERR_ACCESS_DENIED;
570 static WERROR dcesrv_winreg_UnLoadKey(struct dcesrv_call_state *dce_call,
572 struct winreg_UnLoadKey *r)
574 return WERR_NOT_SUPPORTED;
579 winreg_InitiateSystemShutdown
581 static WERROR dcesrv_winreg_InitiateSystemShutdown(struct dcesrv_call_state *dce_call,
583 struct winreg_InitiateSystemShutdown *r)
585 return WERR_NOT_SUPPORTED;
590 winreg_AbortSystemShutdown
592 static WERROR dcesrv_winreg_AbortSystemShutdown(struct dcesrv_call_state *dce_call,
594 struct winreg_AbortSystemShutdown *r)
596 return WERR_NOT_SUPPORTED;
603 static WERROR dcesrv_winreg_GetVersion(struct dcesrv_call_state *dce_call,
605 struct winreg_GetVersion *r)
607 struct dcesrv_handle *h;
609 DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
611 r->out.version = talloc(mem_ctx, uint32_t);
612 W_ERROR_HAVE_NO_MEMORY(r->out.version);
621 winreg_QueryMultipleValues
623 static WERROR dcesrv_winreg_QueryMultipleValues(struct dcesrv_call_state *dce_call,
625 struct winreg_QueryMultipleValues *r)
627 return WERR_NOT_SUPPORTED;
632 winreg_InitiateSystemShutdownEx
634 static WERROR dcesrv_winreg_InitiateSystemShutdownEx(struct dcesrv_call_state *dce_call,
636 struct winreg_InitiateSystemShutdownEx *r)
638 return WERR_NOT_SUPPORTED;
645 static WERROR dcesrv_winreg_SaveKeyEx(struct dcesrv_call_state *dce_call,
647 struct winreg_SaveKeyEx *r)
649 return WERR_NOT_SUPPORTED;
654 winreg_QueryMultipleValues2
656 static WERROR dcesrv_winreg_QueryMultipleValues2(struct dcesrv_call_state *dce_call,
658 struct winreg_QueryMultipleValues2 *r)
660 return WERR_NOT_SUPPORTED;
664 /* include the generated boilerplate */
665 #include "librpc/gen_ndr/ndr_winreg_s.c"