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 "rpc_server/common/common.h"
28 #include "librpc/gen_ndr/ndr_security.h"
29 #include "param/param.h"
30 #include "libcli/security/security.h"
32 enum handle_types { HTYPE_REGVAL, HTYPE_REGKEY };
34 static NTSTATUS dcerpc_winreg_bind(struct dcesrv_call_state *dce_call,
35 const struct dcesrv_interface *iface)
37 struct registry_context *ctx;
40 err = reg_open_samba(dce_call->context,
41 &ctx, dce_call->event_ctx, dce_call->conn->dce_ctx->lp_ctx, dce_call->conn->auth_state.session_info,
44 if (!W_ERROR_IS_OK(err)) {
45 DEBUG(0, ("Error opening registry: %s\n", win_errstr(err)));
46 return NT_STATUS_UNSUCCESSFUL;
49 dce_call->context->private = ctx;
54 #define DCESRV_INTERFACE_WINREG_BIND dcerpc_winreg_bind
56 static WERROR dcesrv_winreg_openhive(struct dcesrv_call_state *dce_call,
57 TALLOC_CTX *mem_ctx, uint32_t hkey,
58 struct policy_handle **outh)
60 struct registry_context *ctx = dce_call->context->private;
61 struct dcesrv_handle *h;
64 h = dcesrv_handle_new(dce_call->context, HTYPE_REGKEY);
66 result = reg_get_predefined_key(ctx, hkey,
67 (struct registry_key **)&h->data);
68 if (!W_ERROR_IS_OK(result)) {
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;
117 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 result = 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(result)) {
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;
191 DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
193 switch (security_session_user_level(dce_call->conn->auth_state.session_info))
195 case SECURITY_SYSTEM:
196 case SECURITY_ADMINISTRATOR:
197 return reg_del_value((struct registry_key *)h->data, r->in.value.name);
199 return WERR_ACCESS_DENIED;
207 static WERROR dcesrv_winreg_EnumKey(struct dcesrv_call_state *dce_call,
209 struct winreg_EnumKey *r)
211 struct dcesrv_handle *h;
212 const char *name, *classname;
216 DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
218 result = reg_key_get_subkey_by_index(mem_ctx,
219 (struct registry_key *)h->data, r->in.enum_index,
220 &name, &classname, &last_mod);
222 if (2*strlen_m_term(name) > r->in.name->size) {
223 return WERR_MORE_DATA;
227 r->out.name->name = name;
228 r->out.name->length = 2*strlen_m_term(name);
230 r->out.name->name = r->in.name->name;
231 r->out.name->length = r->in.name->length;
233 r->out.name->size = r->in.name->size;
235 r->out.keyclass = r->in.keyclass;
236 if (classname != NULL) {
237 r->out.keyclass->name = classname;
238 r->out.keyclass->length = 2*strlen_m_term(classname);
240 r->out.keyclass->name = r->in.keyclass->name;
241 r->out.keyclass->length = r->in.keyclass->length;
243 r->out.keyclass->size = r->in.keyclass->size;
245 if (r->in.last_changed_time != NULL)
246 r->out.last_changed_time = &last_mod;
255 static WERROR dcesrv_winreg_EnumValue(struct dcesrv_call_state *dce_call,
257 struct winreg_EnumValue *r)
259 struct dcesrv_handle *h;
260 struct registry_key *key;
261 const char *data_name;
266 DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
270 result = reg_key_get_value_by_index(mem_ctx, (struct registry_key *)h->data,
271 r->in.enum_index, &data_name, &data_type, &data);
273 if (!W_ERROR_IS_OK(result)) {
274 /* if the lookup wasn't successful, send client query back */
275 data_name = r->in.name->name;
276 data_type = *r->in.type;
277 data.data = r->in.value;
278 data.length = *r->in.length;
281 /* and enough room for the name */
282 if (r->in.name->size < 2*strlen_m_term(data_name)) {
283 return WERR_MORE_DATA;
286 /* "data_name" is NULL when we query the default attribute */
287 if (data_name != NULL) {
288 r->out.name->name = data_name;
289 r->out.name->length = 2*strlen_m_term(data_name);
291 r->out.name->name = r->in.name->name;
292 r->out.name->length = r->in.name->length;
294 r->out.name->size = r->in.name->size;
296 *r->out.value = data_type;
298 /* check the client has enough room for the value */
299 if (r->in.value != NULL &&
300 r->in.size != NULL &&
301 data.length > *r->in.size) {
302 return WERR_MORE_DATA;
305 if (r->in.value != NULL) {
306 r->out.value = data.data;
309 if (r->in.size != NULL) {
310 r->out.size = talloc(mem_ctx, uint32_t);
311 *r->out.size = data.length;
312 r->out.length = r->out.size;
322 static WERROR dcesrv_winreg_FlushKey(struct dcesrv_call_state *dce_call,
324 struct winreg_FlushKey *r)
326 struct dcesrv_handle *h;
328 DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
330 switch (security_session_user_level(dce_call->conn->auth_state.session_info))
332 case SECURITY_SYSTEM:
333 case SECURITY_ADMINISTRATOR:
334 return reg_key_flush(h->data);
336 return WERR_ACCESS_DENIED;
342 winreg_GetKeySecurity
344 static WERROR dcesrv_winreg_GetKeySecurity(struct dcesrv_call_state *dce_call,
346 struct winreg_GetKeySecurity *r)
348 struct dcesrv_handle *h;
350 DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
352 return WERR_NOT_SUPPORTED;
359 static WERROR dcesrv_winreg_LoadKey(struct dcesrv_call_state *dce_call,
361 struct winreg_LoadKey *r)
363 return WERR_NOT_SUPPORTED;
368 winreg_NotifyChangeKeyValue
370 static WERROR dcesrv_winreg_NotifyChangeKeyValue(struct dcesrv_call_state *dce_call,
372 struct winreg_NotifyChangeKeyValue *r)
374 return WERR_NOT_SUPPORTED;
381 static WERROR dcesrv_winreg_OpenKey(struct dcesrv_call_state *dce_call,
383 struct winreg_OpenKey *r)
385 struct dcesrv_handle *h, *newh;
388 DCESRV_PULL_HANDLE_FAULT(h, r->in.parent_handle, HTYPE_REGKEY);
390 switch (security_session_user_level(dce_call->conn->auth_state.session_info))
392 case SECURITY_SYSTEM:
393 case SECURITY_ADMINISTRATOR:
395 if (r->in.keyname.name && strcmp(r->in.keyname.name, "") == 0) {
396 newh = talloc_reference(dce_call->context, h);
399 newh = dcesrv_handle_new(dce_call->context, HTYPE_REGKEY);
400 result = reg_open_key(newh, (struct registry_key *)h->data,
402 (struct registry_key **)&newh->data);
405 if (W_ERROR_IS_OK(result)) {
406 r->out.handle = &newh->wire_handle;
412 return WERR_ACCESS_DENIED;
421 static WERROR dcesrv_winreg_QueryInfoKey(struct dcesrv_call_state *dce_call,
423 struct winreg_QueryInfoKey *r)
425 struct dcesrv_handle *h;
426 const char *classname = NULL;
429 DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
431 switch (security_session_user_level(dce_call->conn->auth_state.session_info))
433 case SECURITY_SYSTEM:
434 case SECURITY_ADMINISTRATOR:
436 result = reg_key_get_info(mem_ctx, (struct registry_key *)h->data,
437 &classname, r->out.num_subkeys, r->out.num_values,
438 r->out.last_changed_time, r->out.max_subkeylen,
439 r->out.max_valnamelen, r->out.max_valbufsize);
441 if (classname != NULL) {
442 r->out.classname->name = classname;
443 r->out.classname->name_len = 2*strlen_m_term(classname);
445 r->out.classname->name = r->in.classname->name;
446 r->out.classname->name_len = r->in.classname->name_len;
448 r->out.classname->name_size = r->in.classname->name_size;
452 return WERR_ACCESS_DENIED;
460 static WERROR dcesrv_winreg_QueryValue(struct dcesrv_call_state *dce_call,
462 struct winreg_QueryValue *r)
464 struct dcesrv_handle *h;
466 DATA_BLOB value_data;
469 DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
471 switch (security_session_user_level(dce_call->conn->auth_state.session_info))
473 case SECURITY_SYSTEM:
474 case SECURITY_ADMINISTRATOR:
476 result = reg_key_get_value_by_name(mem_ctx,
477 (struct registry_key *)h->data, r->in.value_name.name,
478 &value_type, &value_data);
480 if (!W_ERROR_IS_OK(result)) {
481 /* if the lookup wasn't successful, send client query back */
482 value_type = *r->in.type;
483 value_data.data = r->in.data;
484 value_data.length = *r->in.length;
487 /* Just asking for the size of the buffer */
488 r->out.type = talloc(mem_ctx, uint32_t);
492 *r->out.type = value_type;
493 r->out.length = talloc(mem_ctx, uint32_t);
494 if (!r->out.length) {
497 *r->out.length = value_data.length;
498 r->out.size = talloc(mem_ctx, uint32_t);
502 *r->out.size = value_data.length;
504 r->out.data = value_data.data;
508 return WERR_ACCESS_DENIED;
516 static WERROR dcesrv_winreg_ReplaceKey(struct dcesrv_call_state *dce_call,
518 struct winreg_ReplaceKey *r)
520 return WERR_NOT_SUPPORTED;
527 static WERROR dcesrv_winreg_RestoreKey(struct dcesrv_call_state *dce_call,
529 struct winreg_RestoreKey *r)
531 return WERR_NOT_SUPPORTED;
538 static WERROR dcesrv_winreg_SaveKey(struct dcesrv_call_state *dce_call,
540 struct winreg_SaveKey *r)
542 return WERR_NOT_SUPPORTED;
547 winreg_SetKeySecurity
549 static WERROR dcesrv_winreg_SetKeySecurity(struct dcesrv_call_state *dce_call,
551 struct winreg_SetKeySecurity *r)
553 return WERR_NOT_SUPPORTED;
560 static WERROR dcesrv_winreg_SetValue(struct dcesrv_call_state *dce_call,
562 struct winreg_SetValue *r)
564 struct dcesrv_handle *h;
568 DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
570 switch (security_session_user_level(dce_call->conn->auth_state.session_info))
572 case SECURITY_SYSTEM:
573 case SECURITY_ADMINISTRATOR:
574 data.data = r->in.data;
575 data.length = r->in.size;
576 result = reg_val_set((struct registry_key *)h->data,
577 r->in.name.name, r->in.type, data);
580 return WERR_ACCESS_DENIED;
588 static WERROR dcesrv_winreg_UnLoadKey(struct dcesrv_call_state *dce_call,
590 struct winreg_UnLoadKey *r)
592 return WERR_NOT_SUPPORTED;
597 winreg_InitiateSystemShutdown
599 static WERROR dcesrv_winreg_InitiateSystemShutdown(struct dcesrv_call_state *dce_call,
601 struct winreg_InitiateSystemShutdown *r)
603 return WERR_NOT_SUPPORTED;
608 winreg_AbortSystemShutdown
610 static WERROR dcesrv_winreg_AbortSystemShutdown(struct dcesrv_call_state *dce_call,
612 struct winreg_AbortSystemShutdown *r)
614 return WERR_NOT_SUPPORTED;
621 static WERROR dcesrv_winreg_GetVersion(struct dcesrv_call_state *dce_call,
623 struct winreg_GetVersion *r)
625 struct dcesrv_handle *h;
627 DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
629 r->out.version = talloc(mem_ctx, uint32_t);
630 W_ERROR_HAVE_NO_MEMORY(r->out.version);
639 winreg_QueryMultipleValues
641 static WERROR dcesrv_winreg_QueryMultipleValues(struct dcesrv_call_state *dce_call,
643 struct winreg_QueryMultipleValues *r)
645 return WERR_NOT_SUPPORTED;
650 winreg_InitiateSystemShutdownEx
652 static WERROR dcesrv_winreg_InitiateSystemShutdownEx(struct dcesrv_call_state *dce_call,
654 struct winreg_InitiateSystemShutdownEx *r)
656 return WERR_NOT_SUPPORTED;
663 static WERROR dcesrv_winreg_SaveKeyEx(struct dcesrv_call_state *dce_call,
665 struct winreg_SaveKeyEx *r)
667 return WERR_NOT_SUPPORTED;
672 winreg_QueryMultipleValues2
674 static WERROR dcesrv_winreg_QueryMultipleValues2(struct dcesrv_call_state *dce_call,
676 struct winreg_QueryMultipleValues2 *r)
678 return WERR_NOT_SUPPORTED;
682 /* include the generated boilerplate */
683 #include "librpc/gen_ndr/ndr_winreg_s.c"