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 NTSTATUS
dcerpc_winreg_bind(struct dcesrv_call_state
*dce_call
,
33 const struct dcesrv_interface
*iface
)
35 struct registry_context
*ctx
;
38 err
= reg_open_samba(dce_call
->context
,
39 &ctx
, dce_call
->event_ctx
, dce_call
->conn
->dce_ctx
->lp_ctx
,
40 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 werror_to_ntstatus(err
);
48 dce_call
->context
->private_data
= 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_data
;
60 struct dcesrv_handle
*h
;
63 h
= dcesrv_handle_new(dce_call
->context
, HTYPE_REGKEY
);
64 W_ERROR_HAVE_NO_MEMORY(h
);
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
);
102 talloc_unlink(dce_call
->context
, h
);
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
;
118 struct registry_key
*key
;
121 DCESRV_PULL_HANDLE_FAULT(h
, r
->in
.handle
, HTYPE_REGKEY
);
124 newh
= dcesrv_handle_new(dce_call
->context
, HTYPE_REGKEY
);
126 switch (security_session_user_level(dce_call
->conn
->auth_state
.session_info
, NULL
))
128 case SECURITY_SYSTEM
:
129 case SECURITY_ADMINISTRATOR
:
130 /* we support only non volatile keys */
131 if (r
->in
.options
!= REG_OPTION_NON_VOLATILE
) {
132 return WERR_NOT_SUPPORTED
;
135 /* the security descriptor is optional */
136 if (r
->in
.secdesc
!= NULL
) {
138 enum ndr_err_code ndr_err
;
139 sdblob
.data
= r
->in
.secdesc
->sd
.data
;
140 sdblob
.length
= r
->in
.secdesc
->sd
.len
;
141 if (sdblob
.data
== NULL
) {
142 return WERR_INVALID_PARAMETER
;
144 ndr_err
= ndr_pull_struct_blob_all(&sdblob
, mem_ctx
, &sd
,
145 (ndr_pull_flags_fn_t
)ndr_pull_security_descriptor
);
146 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
147 return WERR_INVALID_PARAMETER
;
151 result
= reg_key_add_name(newh
, key
, r
->in
.name
.name
, NULL
,
152 r
->in
.secdesc
?&sd
:NULL
, (struct registry_key
**)&newh
->data
);
154 r
->out
.action_taken
= talloc(mem_ctx
, enum winreg_CreateAction
);
155 if (r
->out
.action_taken
== NULL
) {
157 return WERR_NOT_ENOUGH_MEMORY
;
159 *r
->out
.action_taken
= REG_ACTION_NONE
;
161 if (W_ERROR_IS_OK(result
)) {
162 r
->out
.new_handle
= &newh
->wire_handle
;
163 *r
->out
.action_taken
= REG_CREATED_NEW_KEY
;
170 return WERR_ACCESS_DENIED
;
178 static WERROR
dcesrv_winreg_DeleteKey(struct dcesrv_call_state
*dce_call
,
180 struct winreg_DeleteKey
*r
)
182 struct dcesrv_handle
*h
;
183 struct registry_key
*key
;
186 DCESRV_PULL_HANDLE_FAULT(h
, r
->in
.handle
, HTYPE_REGKEY
);
189 switch (security_session_user_level(dce_call
->conn
->auth_state
.session_info
, NULL
))
191 case SECURITY_SYSTEM
:
192 case SECURITY_ADMINISTRATOR
:
193 result
= reg_key_del(mem_ctx
, key
, r
->in
.key
.name
);
194 talloc_unlink(dce_call
->context
, h
);
198 return WERR_ACCESS_DENIED
;
206 static WERROR
dcesrv_winreg_DeleteValue(struct dcesrv_call_state
*dce_call
,
208 struct winreg_DeleteValue
*r
)
210 struct dcesrv_handle
*h
;
211 struct registry_key
*key
;
213 DCESRV_PULL_HANDLE_FAULT(h
, r
->in
.handle
, HTYPE_REGKEY
);
216 switch (security_session_user_level(dce_call
->conn
->auth_state
.session_info
, NULL
))
218 case SECURITY_SYSTEM
:
219 case SECURITY_ADMINISTRATOR
:
220 return reg_del_value(mem_ctx
, key
, r
->in
.value
.name
);
222 return WERR_ACCESS_DENIED
;
230 static WERROR
dcesrv_winreg_EnumKey(struct dcesrv_call_state
*dce_call
,
232 struct winreg_EnumKey
*r
)
234 struct dcesrv_handle
*h
;
235 struct registry_key
*key
;
236 const char *name
, *classname
;
240 DCESRV_PULL_HANDLE_FAULT(h
, r
->in
.handle
, HTYPE_REGKEY
);
243 result
= reg_key_get_subkey_by_index(mem_ctx
,
244 key
, r
->in
.enum_index
, &name
, &classname
, &last_mod
);
246 if (2*strlen_m_term(name
) > r
->in
.name
->size
) {
247 return WERR_MORE_DATA
;
251 r
->out
.name
->name
= name
;
252 r
->out
.name
->length
= 2*strlen_m_term(name
);
254 r
->out
.name
->name
= r
->in
.name
->name
;
255 r
->out
.name
->length
= r
->in
.name
->length
;
257 r
->out
.name
->size
= r
->in
.name
->size
;
259 r
->out
.keyclass
= r
->in
.keyclass
;
260 if (classname
!= NULL
) {
261 r
->out
.keyclass
->name
= classname
;
262 r
->out
.keyclass
->length
= 2*strlen_m_term(classname
);
264 r
->out
.keyclass
->name
= r
->in
.keyclass
->name
;
265 r
->out
.keyclass
->length
= r
->in
.keyclass
->length
;
267 r
->out
.keyclass
->size
= r
->in
.keyclass
->size
;
269 if (r
->in
.last_changed_time
!= NULL
)
270 r
->out
.last_changed_time
= &last_mod
;
279 static WERROR
dcesrv_winreg_EnumValue(struct dcesrv_call_state
*dce_call
,
281 struct winreg_EnumValue
*r
)
283 struct dcesrv_handle
*h
;
284 struct registry_key
*key
;
285 const char *data_name
;
290 DCESRV_PULL_HANDLE_FAULT(h
, r
->in
.handle
, HTYPE_REGKEY
);
293 result
= reg_key_get_value_by_index(mem_ctx
, key
,
294 r
->in
.enum_index
, &data_name
, &data_type
, &data
);
296 if (!W_ERROR_IS_OK(result
)) {
297 /* if the lookup wasn't successful, send client query back */
298 data_name
= r
->in
.name
->name
;
299 data_type
= *r
->in
.type
;
300 data
.data
= r
->in
.value
;
301 data
.length
= *r
->in
.length
;
304 /* "data_name" is NULL when we query the default attribute */
305 if (data_name
!= NULL
) {
306 r
->out
.name
->name
= data_name
;
307 r
->out
.name
->length
= 2*strlen_m_term(data_name
);
309 r
->out
.name
->name
= r
->in
.name
->name
;
310 r
->out
.name
->length
= r
->in
.name
->length
;
312 r
->out
.name
->size
= r
->in
.name
->size
;
314 r
->out
.type
= talloc(mem_ctx
, enum winreg_Type
);
316 return WERR_NOT_ENOUGH_MEMORY
;
318 *r
->out
.type
= (enum winreg_Type
) data_type
;
320 /* check the client has enough room for the value */
321 if (r
->in
.value
!= NULL
&&
322 r
->in
.size
!= NULL
&&
323 data
.length
> *r
->in
.size
) {
324 return WERR_MORE_DATA
;
327 if (r
->in
.value
!= NULL
) {
328 r
->out
.value
= data
.data
;
331 if (r
->in
.size
!= NULL
) {
332 r
->out
.size
= talloc(mem_ctx
, uint32_t);
333 *r
->out
.size
= data
.length
;
334 r
->out
.length
= r
->out
.size
;
344 static WERROR
dcesrv_winreg_FlushKey(struct dcesrv_call_state
*dce_call
,
346 struct winreg_FlushKey
*r
)
348 struct dcesrv_handle
*h
;
349 struct registry_key
*key
;
351 DCESRV_PULL_HANDLE_FAULT(h
, r
->in
.handle
, HTYPE_REGKEY
);
354 switch (security_session_user_level(dce_call
->conn
->auth_state
.session_info
, NULL
))
356 case SECURITY_SYSTEM
:
357 case SECURITY_ADMINISTRATOR
:
358 return reg_key_flush(key
);
360 return WERR_ACCESS_DENIED
;
366 winreg_GetKeySecurity
368 static WERROR
dcesrv_winreg_GetKeySecurity(struct dcesrv_call_state
*dce_call
,
370 struct winreg_GetKeySecurity
*r
)
372 struct dcesrv_handle
*h
;
374 DCESRV_PULL_HANDLE_FAULT(h
, r
->in
.handle
, HTYPE_REGKEY
);
376 return WERR_NOT_SUPPORTED
;
383 static WERROR
dcesrv_winreg_LoadKey(struct dcesrv_call_state
*dce_call
,
385 struct winreg_LoadKey
*r
)
387 return WERR_NOT_SUPPORTED
;
392 winreg_NotifyChangeKeyValue
394 static WERROR
dcesrv_winreg_NotifyChangeKeyValue(struct dcesrv_call_state
*dce_call
,
396 struct winreg_NotifyChangeKeyValue
*r
)
398 return WERR_NOT_SUPPORTED
;
405 static WERROR
dcesrv_winreg_OpenKey(struct dcesrv_call_state
*dce_call
,
407 struct winreg_OpenKey
*r
)
409 struct dcesrv_handle
*h
, *newh
;
410 struct registry_key
*key
;
413 DCESRV_PULL_HANDLE_FAULT(h
, r
->in
.parent_handle
, HTYPE_REGKEY
);
416 switch (security_session_user_level(dce_call
->conn
->auth_state
.session_info
, NULL
))
418 case SECURITY_SYSTEM
:
419 case SECURITY_ADMINISTRATOR
:
421 if (r
->in
.keyname
.name
&& strcmp(r
->in
.keyname
.name
, "") == 0) {
422 newh
= talloc_reference(dce_call
->context
, h
);
425 newh
= dcesrv_handle_new(dce_call
->context
, HTYPE_REGKEY
);
426 result
= reg_open_key(newh
, key
, r
->in
.keyname
.name
,
427 (struct registry_key
**)&newh
->data
);
430 if (W_ERROR_IS_OK(result
)) {
431 r
->out
.handle
= &newh
->wire_handle
;
437 return WERR_ACCESS_DENIED
;
445 static WERROR
dcesrv_winreg_QueryInfoKey(struct dcesrv_call_state
*dce_call
,
447 struct winreg_QueryInfoKey
*r
)
449 struct dcesrv_handle
*h
;
450 struct registry_key
*key
;
451 const char *classname
= NULL
;
454 DCESRV_PULL_HANDLE_FAULT(h
, r
->in
.handle
, HTYPE_REGKEY
);
457 switch (security_session_user_level(dce_call
->conn
->auth_state
.session_info
, NULL
))
459 case SECURITY_SYSTEM
:
460 case SECURITY_ADMINISTRATOR
:
462 result
= reg_key_get_info(mem_ctx
, key
, &classname
,
463 r
->out
.num_subkeys
, r
->out
.num_values
,
464 r
->out
.last_changed_time
, r
->out
.max_subkeylen
,
465 r
->out
.max_valnamelen
, r
->out
.max_valbufsize
);
467 if (r
->out
.max_subkeylen
!= NULL
) {
468 /* for UTF16 encoding */
469 *r
->out
.max_subkeylen
*= 2;
471 if (r
->out
.max_valnamelen
!= NULL
) {
472 /* for UTF16 encoding */
473 *r
->out
.max_valnamelen
*= 2;
476 if (classname
!= NULL
) {
477 r
->out
.classname
->name
= classname
;
478 r
->out
.classname
->name_len
= 2*strlen_m_term(classname
);
480 r
->out
.classname
->name
= r
->in
.classname
->name
;
481 r
->out
.classname
->name_len
= r
->in
.classname
->name_len
;
483 r
->out
.classname
->name_size
= r
->in
.classname
->name_size
;
487 return WERR_ACCESS_DENIED
;
495 static WERROR
dcesrv_winreg_QueryValue(struct dcesrv_call_state
*dce_call
,
497 struct winreg_QueryValue
*r
)
499 struct dcesrv_handle
*h
;
500 struct registry_key
*key
;
502 DATA_BLOB value_data
;
505 DCESRV_PULL_HANDLE_FAULT(h
, r
->in
.handle
, HTYPE_REGKEY
);
508 switch (security_session_user_level(dce_call
->conn
->auth_state
.session_info
, NULL
))
510 case SECURITY_SYSTEM
:
511 case SECURITY_ADMINISTRATOR
:
513 if ((r
->in
.type
== NULL
) || (r
->in
.data_length
== NULL
) ||
514 (r
->in
.data_size
== NULL
)) {
515 return WERR_INVALID_PARAMETER
;
518 result
= reg_key_get_value_by_name(mem_ctx
, key
,
519 r
->in
.value_name
->name
, &value_type
, &value_data
);
521 if (!W_ERROR_IS_OK(result
)) {
522 /* if the lookup wasn't successful, send client query back */
523 value_type
= *r
->in
.type
;
524 value_data
.data
= r
->in
.data
;
525 value_data
.length
= *r
->in
.data_length
;
527 if ((r
->in
.data
!= NULL
)
528 && (*r
->in
.data_size
< value_data
.length
)) {
529 result
= WERR_MORE_DATA
;
533 r
->out
.type
= talloc(mem_ctx
, enum winreg_Type
);
535 return WERR_NOT_ENOUGH_MEMORY
;
537 *r
->out
.type
= (enum winreg_Type
) value_type
;
538 r
->out
.data_length
= talloc(mem_ctx
, uint32_t);
539 if (!r
->out
.data_length
) {
540 return WERR_NOT_ENOUGH_MEMORY
;
542 *r
->out
.data_length
= value_data
.length
;
543 r
->out
.data_size
= talloc(mem_ctx
, uint32_t);
544 if (!r
->out
.data_size
) {
545 return WERR_NOT_ENOUGH_MEMORY
;
547 *r
->out
.data_size
= value_data
.length
;
548 r
->out
.data
= value_data
.data
;
552 return WERR_ACCESS_DENIED
;
560 static WERROR
dcesrv_winreg_ReplaceKey(struct dcesrv_call_state
*dce_call
,
562 struct winreg_ReplaceKey
*r
)
564 return WERR_NOT_SUPPORTED
;
571 static WERROR
dcesrv_winreg_RestoreKey(struct dcesrv_call_state
*dce_call
,
573 struct winreg_RestoreKey
*r
)
575 return WERR_NOT_SUPPORTED
;
582 static WERROR
dcesrv_winreg_SaveKey(struct dcesrv_call_state
*dce_call
,
584 struct winreg_SaveKey
*r
)
586 return WERR_NOT_SUPPORTED
;
591 winreg_SetKeySecurity
593 static WERROR
dcesrv_winreg_SetKeySecurity(struct dcesrv_call_state
*dce_call
,
595 struct winreg_SetKeySecurity
*r
)
597 return WERR_NOT_SUPPORTED
;
604 static WERROR
dcesrv_winreg_SetValue(struct dcesrv_call_state
*dce_call
,
606 struct winreg_SetValue
*r
)
608 struct dcesrv_handle
*h
;
609 struct registry_key
*key
;
613 DCESRV_PULL_HANDLE_FAULT(h
, r
->in
.handle
, HTYPE_REGKEY
);
616 switch (security_session_user_level(dce_call
->conn
->auth_state
.session_info
, NULL
))
618 case SECURITY_SYSTEM
:
619 case SECURITY_ADMINISTRATOR
:
620 data
.data
= r
->in
.data
;
621 data
.length
= r
->in
.size
;
622 result
= reg_val_set(key
, r
->in
.name
.name
, r
->in
.type
, data
);
625 return WERR_ACCESS_DENIED
;
633 static WERROR
dcesrv_winreg_UnLoadKey(struct dcesrv_call_state
*dce_call
,
635 struct winreg_UnLoadKey
*r
)
637 return WERR_NOT_SUPPORTED
;
642 winreg_InitiateSystemShutdown
644 static WERROR
dcesrv_winreg_InitiateSystemShutdown(struct dcesrv_call_state
*dce_call
,
646 struct winreg_InitiateSystemShutdown
*r
)
648 return WERR_NOT_SUPPORTED
;
653 winreg_AbortSystemShutdown
655 static WERROR
dcesrv_winreg_AbortSystemShutdown(struct dcesrv_call_state
*dce_call
,
657 struct winreg_AbortSystemShutdown
*r
)
659 return WERR_NOT_SUPPORTED
;
666 static WERROR
dcesrv_winreg_GetVersion(struct dcesrv_call_state
*dce_call
,
668 struct winreg_GetVersion
*r
)
670 struct dcesrv_handle
*h
;
672 DCESRV_PULL_HANDLE_FAULT(h
, r
->in
.handle
, HTYPE_REGKEY
);
674 r
->out
.version
= talloc(mem_ctx
, uint32_t);
675 W_ERROR_HAVE_NO_MEMORY(r
->out
.version
);
684 winreg_QueryMultipleValues
686 static WERROR
dcesrv_winreg_QueryMultipleValues(struct dcesrv_call_state
*dce_call
,
688 struct winreg_QueryMultipleValues
*r
)
690 return WERR_NOT_SUPPORTED
;
695 winreg_InitiateSystemShutdownEx
697 static WERROR
dcesrv_winreg_InitiateSystemShutdownEx(struct dcesrv_call_state
*dce_call
,
699 struct winreg_InitiateSystemShutdownEx
*r
)
701 return WERR_NOT_SUPPORTED
;
708 static WERROR
dcesrv_winreg_SaveKeyEx(struct dcesrv_call_state
*dce_call
,
710 struct winreg_SaveKeyEx
*r
)
712 return WERR_NOT_SUPPORTED
;
717 winreg_QueryMultipleValues2
719 static WERROR
dcesrv_winreg_QueryMultipleValues2(struct dcesrv_call_state
*dce_call
,
721 struct winreg_QueryMultipleValues2
*r
)
723 return WERR_NOT_SUPPORTED
;
729 static WERROR
dcesrv_winreg_DeleteKeyEx(struct dcesrv_call_state
*dce_call
,
731 struct winreg_DeleteKeyEx
*r
)
733 return WERR_NOT_SUPPORTED
;
736 /* include the generated boilerplate */
737 #include "librpc/gen_ndr/ndr_winreg_s.c"