2 Unix SMB/CIFS implementation.
4 endpoint server for the winreg pipe
6 Copyright (C) 2004 Jelmer Vernooij, jelmer@samba.org
7 Copyright (C) 2008 Matthias Dieter Wallnöfer, mwallnoefer@yahoo.de
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "rpc_server/dcerpc_server.h"
25 #include "lib/registry/registry.h"
26 #include "librpc/gen_ndr/ndr_winreg.h"
27 #include "librpc/gen_ndr/ndr_security.h"
28 #include "libcli/security/session.h"
30 enum handle_types { HTYPE_REGVAL, HTYPE_REGKEY };
32 static WERROR dcesrv_winreg_openhive(struct dcesrv_call_state *dce_call,
33 TALLOC_CTX *mem_ctx, uint32_t hkey,
34 struct policy_handle **outh)
36 struct auth_session_info *session_info =
37 dcesrv_call_session_info(dce_call);
38 struct registry_context *ctx = NULL;
39 struct dcesrv_handle *h;
42 h = dcesrv_handle_create(dce_call, HTYPE_REGKEY);
43 W_ERROR_HAVE_NO_MEMORY(h);
45 result = reg_open_samba(h, &ctx,
47 dce_call->conn->dce_ctx->lp_ctx,
50 if (!W_ERROR_IS_OK(result)) {
51 DEBUG(0, ("Error opening registry: %s\n", win_errstr(result)));
55 result = reg_get_predefined_key(ctx, hkey,
56 (struct registry_key **)&h->data);
57 if (!W_ERROR_IS_OK(result)) {
60 *outh = &h->wire_handle;
65 #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) \
67 return dcesrv_winreg_openhive (dce_call, mem_ctx, n, &r->out.handle);\
70 func_winreg_OpenHive(HKCR,HKEY_CLASSES_ROOT)
71 func_winreg_OpenHive(HKCU,HKEY_CURRENT_USER)
72 func_winreg_OpenHive(HKLM,HKEY_LOCAL_MACHINE)
73 func_winreg_OpenHive(HKPD,HKEY_PERFORMANCE_DATA)
74 func_winreg_OpenHive(HKU,HKEY_USERS)
75 func_winreg_OpenHive(HKCC,HKEY_CURRENT_CONFIG)
76 func_winreg_OpenHive(HKDD,HKEY_DYN_DATA)
77 func_winreg_OpenHive(HKPT,HKEY_PERFORMANCE_TEXT)
78 func_winreg_OpenHive(HKPN,HKEY_PERFORMANCE_NLSTEXT)
83 static WERROR dcesrv_winreg_CloseKey(struct dcesrv_call_state *dce_call,
85 struct winreg_CloseKey *r)
87 struct dcesrv_handle *h;
89 DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
91 talloc_unlink(dce_call->context, h);
93 ZERO_STRUCTP(r->out.handle);
101 static WERROR dcesrv_winreg_CreateKey(struct dcesrv_call_state *dce_call,
103 struct winreg_CreateKey *r)
105 struct auth_session_info *session_info =
106 dcesrv_call_session_info(dce_call);
107 struct dcesrv_handle *h, *newh;
108 struct security_descriptor sd;
109 struct registry_key *key;
112 DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
115 newh = dcesrv_handle_create(dce_call, HTYPE_REGKEY);
117 switch (security_session_user_level(session_info, NULL))
119 case SECURITY_SYSTEM:
120 case SECURITY_ADMINISTRATOR:
121 /* we support only non volatile keys */
122 if (r->in.options != REG_OPTION_NON_VOLATILE) {
123 return WERR_NOT_SUPPORTED;
126 /* the security descriptor is optional */
127 if (r->in.secdesc != NULL) {
129 enum ndr_err_code ndr_err;
130 sdblob.data = r->in.secdesc->sd.data;
131 sdblob.length = r->in.secdesc->sd.len;
132 if (sdblob.data == NULL) {
133 return WERR_INVALID_PARAMETER;
135 ndr_err = ndr_pull_struct_blob_all(&sdblob, mem_ctx, &sd,
136 (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
137 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
138 return WERR_INVALID_PARAMETER;
142 result = reg_key_add_name(newh, key, r->in.name.name, NULL,
143 r->in.secdesc?&sd:NULL, (struct registry_key **)&newh->data);
145 r->out.action_taken = talloc(mem_ctx, enum winreg_CreateAction);
146 if (r->out.action_taken == NULL) {
148 return WERR_NOT_ENOUGH_MEMORY;
150 *r->out.action_taken = REG_ACTION_NONE;
152 if (W_ERROR_IS_OK(result)) {
153 r->out.new_handle = &newh->wire_handle;
154 *r->out.action_taken = REG_CREATED_NEW_KEY;
161 return WERR_ACCESS_DENIED;
169 static WERROR dcesrv_winreg_DeleteKey(struct dcesrv_call_state *dce_call,
171 struct winreg_DeleteKey *r)
173 struct auth_session_info *session_info =
174 dcesrv_call_session_info(dce_call);
175 struct dcesrv_handle *h;
176 struct registry_key *key;
179 DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
182 switch (security_session_user_level(session_info, NULL))
184 case SECURITY_SYSTEM:
185 case SECURITY_ADMINISTRATOR:
186 result = reg_key_del(mem_ctx, key, r->in.key.name);
187 talloc_unlink(dce_call->context, h);
191 return WERR_ACCESS_DENIED;
199 static WERROR dcesrv_winreg_DeleteValue(struct dcesrv_call_state *dce_call,
201 struct winreg_DeleteValue *r)
203 struct auth_session_info *session_info =
204 dcesrv_call_session_info(dce_call);
205 struct dcesrv_handle *h;
206 struct registry_key *key;
208 DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
211 switch (security_session_user_level(session_info, NULL))
213 case SECURITY_SYSTEM:
214 case SECURITY_ADMINISTRATOR:
215 return reg_del_value(mem_ctx, key, r->in.value.name);
217 return WERR_ACCESS_DENIED;
225 static WERROR dcesrv_winreg_EnumKey(struct dcesrv_call_state *dce_call,
227 struct winreg_EnumKey *r)
229 struct dcesrv_handle *h;
230 struct registry_key *key;
231 const char *name, *classname;
235 DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
238 result = reg_key_get_subkey_by_index(mem_ctx,
239 key, r->in.enum_index, &name, &classname, &last_mod);
241 if (2*strlen_m_term(name) > r->in.name->size) {
242 return WERR_MORE_DATA;
246 r->out.name->name = name;
247 r->out.name->length = 2*strlen_m_term(name);
249 r->out.name->name = r->in.name->name;
250 r->out.name->length = r->in.name->length;
252 r->out.name->size = r->in.name->size;
254 r->out.keyclass = r->in.keyclass;
255 if (classname != NULL) {
256 r->out.keyclass->name = classname;
257 r->out.keyclass->length = 2*strlen_m_term(classname);
259 r->out.keyclass->name = r->in.keyclass->name;
260 r->out.keyclass->length = r->in.keyclass->length;
262 r->out.keyclass->size = r->in.keyclass->size;
264 if (r->in.last_changed_time != NULL)
265 r->out.last_changed_time = &last_mod;
274 static WERROR dcesrv_winreg_EnumValue(struct dcesrv_call_state *dce_call,
276 struct winreg_EnumValue *r)
278 struct dcesrv_handle *h;
279 struct registry_key *key;
280 const char *data_name;
285 DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
288 result = reg_key_get_value_by_index(mem_ctx, key,
289 r->in.enum_index, &data_name, &data_type, &data);
291 if (!W_ERROR_IS_OK(result)) {
292 /* if the lookup wasn't successful, send client query back */
293 data_name = r->in.name->name;
294 data_type = *r->in.type;
295 data.data = r->in.value;
296 data.length = *r->in.length;
299 /* "data_name" is NULL when we query the default attribute */
300 if (data_name != NULL) {
301 r->out.name->name = data_name;
302 r->out.name->length = 2*strlen_m_term(data_name);
304 r->out.name->name = r->in.name->name;
305 r->out.name->length = r->in.name->length;
307 r->out.name->size = r->in.name->size;
309 r->out.type = talloc(mem_ctx, enum winreg_Type);
311 return WERR_NOT_ENOUGH_MEMORY;
313 *r->out.type = (enum winreg_Type) data_type;
315 /* check the client has enough room for the value */
316 if (r->in.value != NULL &&
317 r->in.size != NULL &&
318 data.length > *r->in.size) {
319 return WERR_MORE_DATA;
322 if (r->in.value != NULL) {
323 r->out.value = data.data;
326 if (r->in.size != NULL) {
327 r->out.size = talloc(mem_ctx, uint32_t);
328 *r->out.size = data.length;
329 r->out.length = r->out.size;
339 static WERROR dcesrv_winreg_FlushKey(struct dcesrv_call_state *dce_call,
341 struct winreg_FlushKey *r)
343 struct auth_session_info *session_info =
344 dcesrv_call_session_info(dce_call);
345 struct dcesrv_handle *h;
346 struct registry_key *key;
348 DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
351 switch (security_session_user_level(session_info, NULL))
353 case SECURITY_SYSTEM:
354 case SECURITY_ADMINISTRATOR:
355 return reg_key_flush(key);
357 return WERR_ACCESS_DENIED;
363 winreg_GetKeySecurity
365 static WERROR dcesrv_winreg_GetKeySecurity(struct dcesrv_call_state *dce_call,
367 struct winreg_GetKeySecurity *r)
369 struct dcesrv_handle *h;
371 DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
373 return WERR_NOT_SUPPORTED;
380 static WERROR dcesrv_winreg_LoadKey(struct dcesrv_call_state *dce_call,
382 struct winreg_LoadKey *r)
384 return WERR_NOT_SUPPORTED;
389 winreg_NotifyChangeKeyValue
391 static WERROR dcesrv_winreg_NotifyChangeKeyValue(struct dcesrv_call_state *dce_call,
393 struct winreg_NotifyChangeKeyValue *r)
395 return WERR_NOT_SUPPORTED;
402 static WERROR dcesrv_winreg_OpenKey(struct dcesrv_call_state *dce_call,
404 struct winreg_OpenKey *r)
406 struct auth_session_info *session_info =
407 dcesrv_call_session_info(dce_call);
408 struct dcesrv_handle *h, *newh;
409 struct registry_key *key;
412 DCESRV_PULL_HANDLE_FAULT(h, r->in.parent_handle, HTYPE_REGKEY);
415 switch (security_session_user_level(session_info, NULL))
417 case SECURITY_SYSTEM:
418 case SECURITY_ADMINISTRATOR:
420 if (r->in.keyname.name && strcmp(r->in.keyname.name, "") == 0) {
421 newh = talloc_reference(dce_call->context, h);
424 newh = dcesrv_handle_create(dce_call, HTYPE_REGKEY);
425 result = reg_open_key(newh, key, r->in.keyname.name,
426 (struct registry_key **)&newh->data);
429 if (W_ERROR_IS_OK(result)) {
430 r->out.handle = &newh->wire_handle;
436 return WERR_ACCESS_DENIED;
444 static WERROR dcesrv_winreg_QueryInfoKey(struct dcesrv_call_state *dce_call,
446 struct winreg_QueryInfoKey *r)
448 struct auth_session_info *session_info =
449 dcesrv_call_session_info(dce_call);
450 struct dcesrv_handle *h;
451 struct registry_key *key;
452 const char *classname = NULL;
455 DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
458 switch (security_session_user_level(session_info, NULL))
460 case SECURITY_SYSTEM:
461 case SECURITY_ADMINISTRATOR:
463 result = reg_key_get_info(mem_ctx, key, &classname,
464 r->out.num_subkeys, r->out.num_values,
465 r->out.last_changed_time, r->out.max_subkeylen,
466 r->out.max_valnamelen, r->out.max_valbufsize);
468 if (r->out.max_subkeylen != NULL) {
469 /* for UTF16 encoding */
470 *r->out.max_subkeylen *= 2;
472 if (r->out.max_valnamelen != NULL) {
473 /* for UTF16 encoding */
474 *r->out.max_valnamelen *= 2;
477 if (classname != NULL) {
478 r->out.classname->name = classname;
479 r->out.classname->name_len = 2*strlen_m_term(classname);
481 r->out.classname->name = r->in.classname->name;
482 r->out.classname->name_len = r->in.classname->name_len;
484 r->out.classname->name_size = r->in.classname->name_size;
488 return WERR_ACCESS_DENIED;
496 static WERROR dcesrv_winreg_QueryValue(struct dcesrv_call_state *dce_call,
498 struct winreg_QueryValue *r)
500 struct auth_session_info *session_info =
501 dcesrv_call_session_info(dce_call);
502 struct dcesrv_handle *h;
503 struct registry_key *key;
505 DATA_BLOB value_data;
508 DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
511 switch (security_session_user_level(session_info, NULL))
513 case SECURITY_SYSTEM:
514 case SECURITY_ADMINISTRATOR:
516 if ((r->in.type == NULL) || (r->in.data_length == NULL) ||
517 (r->in.data_size == NULL)) {
518 return WERR_INVALID_PARAMETER;
521 result = reg_key_get_value_by_name(mem_ctx, key,
522 r->in.value_name->name, &value_type, &value_data);
524 if (!W_ERROR_IS_OK(result)) {
525 /* if the lookup wasn't successful, send client query back */
526 value_type = *r->in.type;
527 value_data.data = r->in.data;
528 value_data.length = *r->in.data_length;
530 if ((r->in.data != NULL)
531 && (*r->in.data_size < value_data.length)) {
532 result = WERR_MORE_DATA;
536 r->out.type = talloc(mem_ctx, enum winreg_Type);
538 return WERR_NOT_ENOUGH_MEMORY;
540 *r->out.type = (enum winreg_Type) value_type;
541 r->out.data_length = talloc(mem_ctx, uint32_t);
542 if (!r->out.data_length) {
543 return WERR_NOT_ENOUGH_MEMORY;
545 *r->out.data_length = value_data.length;
546 r->out.data_size = talloc(mem_ctx, uint32_t);
547 if (!r->out.data_size) {
548 return WERR_NOT_ENOUGH_MEMORY;
550 *r->out.data_size = value_data.length;
551 r->out.data = value_data.data;
555 return WERR_ACCESS_DENIED;
563 static WERROR dcesrv_winreg_ReplaceKey(struct dcesrv_call_state *dce_call,
565 struct winreg_ReplaceKey *r)
567 return WERR_NOT_SUPPORTED;
574 static WERROR dcesrv_winreg_RestoreKey(struct dcesrv_call_state *dce_call,
576 struct winreg_RestoreKey *r)
578 return WERR_NOT_SUPPORTED;
585 static WERROR dcesrv_winreg_SaveKey(struct dcesrv_call_state *dce_call,
587 struct winreg_SaveKey *r)
589 return WERR_NOT_SUPPORTED;
594 winreg_SetKeySecurity
596 static WERROR dcesrv_winreg_SetKeySecurity(struct dcesrv_call_state *dce_call,
598 struct winreg_SetKeySecurity *r)
600 return WERR_NOT_SUPPORTED;
607 static WERROR dcesrv_winreg_SetValue(struct dcesrv_call_state *dce_call,
609 struct winreg_SetValue *r)
611 struct auth_session_info *session_info =
612 dcesrv_call_session_info(dce_call);
613 struct dcesrv_handle *h;
614 struct registry_key *key;
618 DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
621 switch (security_session_user_level(session_info, NULL))
623 case SECURITY_SYSTEM:
624 case SECURITY_ADMINISTRATOR:
625 data.data = r->in.data;
626 data.length = r->in.size;
627 result = reg_val_set(key, r->in.name.name, r->in.type, data);
630 return WERR_ACCESS_DENIED;
638 static WERROR dcesrv_winreg_UnLoadKey(struct dcesrv_call_state *dce_call,
640 struct winreg_UnLoadKey *r)
642 return WERR_NOT_SUPPORTED;
647 winreg_InitiateSystemShutdown
649 static WERROR dcesrv_winreg_InitiateSystemShutdown(struct dcesrv_call_state *dce_call,
651 struct winreg_InitiateSystemShutdown *r)
653 return WERR_NOT_SUPPORTED;
658 winreg_AbortSystemShutdown
660 static WERROR dcesrv_winreg_AbortSystemShutdown(struct dcesrv_call_state *dce_call,
662 struct winreg_AbortSystemShutdown *r)
664 return WERR_NOT_SUPPORTED;
671 static WERROR dcesrv_winreg_GetVersion(struct dcesrv_call_state *dce_call,
673 struct winreg_GetVersion *r)
675 struct dcesrv_handle *h;
677 DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
679 r->out.version = talloc(mem_ctx, uint32_t);
680 W_ERROR_HAVE_NO_MEMORY(r->out.version);
689 winreg_QueryMultipleValues
691 static WERROR dcesrv_winreg_QueryMultipleValues(struct dcesrv_call_state *dce_call,
693 struct winreg_QueryMultipleValues *r)
695 return WERR_NOT_SUPPORTED;
700 winreg_InitiateSystemShutdownEx
702 static WERROR dcesrv_winreg_InitiateSystemShutdownEx(struct dcesrv_call_state *dce_call,
704 struct winreg_InitiateSystemShutdownEx *r)
706 return WERR_NOT_SUPPORTED;
713 static WERROR dcesrv_winreg_SaveKeyEx(struct dcesrv_call_state *dce_call,
715 struct winreg_SaveKeyEx *r)
717 return WERR_NOT_SUPPORTED;
722 winreg_QueryMultipleValues2
724 static WERROR dcesrv_winreg_QueryMultipleValues2(struct dcesrv_call_state *dce_call,
726 struct winreg_QueryMultipleValues2 *r)
728 return WERR_NOT_SUPPORTED;
734 static WERROR dcesrv_winreg_DeleteKeyEx(struct dcesrv_call_state *dce_call,
736 struct winreg_DeleteKeyEx *r)
738 return WERR_NOT_SUPPORTED;
741 /* include the generated boilerplate */
742 #include "librpc/gen_ndr/ndr_winreg_s.c"