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 if (r
->in
.size
!= NULL
) {
316 r
->out
.size
= talloc(mem_ctx
, uint32_t);
317 *r
->out
.size
= data
.length
;
319 /* check the client has enough room for the value */
320 if (r
->in
.value
!= NULL
&&
321 r
->in
.size
!= NULL
&&
322 data
.length
> *r
->in
.size
) {
323 return WERR_MORE_DATA
;
326 if (r
->in
.value
!= NULL
) {
327 r
->out
.value
= data
.data
;
330 if (r
->in
.length
!= NULL
) {
331 r
->out
.length
= r
->out
.size
;
341 static WERROR
dcesrv_winreg_FlushKey(struct dcesrv_call_state
*dce_call
,
343 struct winreg_FlushKey
*r
)
345 struct auth_session_info
*session_info
=
346 dcesrv_call_session_info(dce_call
);
347 struct dcesrv_handle
*h
;
348 struct registry_key
*key
;
350 DCESRV_PULL_HANDLE_FAULT(h
, r
->in
.handle
, HTYPE_REGKEY
);
353 switch (security_session_user_level(session_info
, NULL
))
355 case SECURITY_SYSTEM
:
356 case SECURITY_ADMINISTRATOR
:
357 return reg_key_flush(key
);
359 return WERR_ACCESS_DENIED
;
365 winreg_GetKeySecurity
367 static WERROR
dcesrv_winreg_GetKeySecurity(struct dcesrv_call_state
*dce_call
,
369 struct winreg_GetKeySecurity
*r
)
371 struct dcesrv_handle
*h
;
373 DCESRV_PULL_HANDLE_FAULT(h
, r
->in
.handle
, HTYPE_REGKEY
);
375 return WERR_NOT_SUPPORTED
;
382 static WERROR
dcesrv_winreg_LoadKey(struct dcesrv_call_state
*dce_call
,
384 struct winreg_LoadKey
*r
)
386 return WERR_NOT_SUPPORTED
;
391 winreg_NotifyChangeKeyValue
393 static WERROR
dcesrv_winreg_NotifyChangeKeyValue(struct dcesrv_call_state
*dce_call
,
395 struct winreg_NotifyChangeKeyValue
*r
)
397 return WERR_NOT_SUPPORTED
;
404 static WERROR
dcesrv_winreg_OpenKey(struct dcesrv_call_state
*dce_call
,
406 struct winreg_OpenKey
*r
)
408 struct auth_session_info
*session_info
=
409 dcesrv_call_session_info(dce_call
);
410 struct dcesrv_handle
*h
, *newh
;
411 struct registry_key
*key
;
414 DCESRV_PULL_HANDLE_FAULT(h
, r
->in
.parent_handle
, HTYPE_REGKEY
);
417 switch (security_session_user_level(session_info
, NULL
))
419 case SECURITY_SYSTEM
:
420 case SECURITY_ADMINISTRATOR
:
422 if (r
->in
.keyname
.name
&& strcmp(r
->in
.keyname
.name
, "") == 0) {
423 newh
= talloc_reference(dce_call
->context
, h
);
426 newh
= dcesrv_handle_create(dce_call
, HTYPE_REGKEY
);
427 result
= reg_open_key(newh
, key
, r
->in
.keyname
.name
,
428 (struct registry_key
**)&newh
->data
);
431 if (W_ERROR_IS_OK(result
)) {
432 r
->out
.handle
= &newh
->wire_handle
;
438 return WERR_ACCESS_DENIED
;
446 static WERROR
dcesrv_winreg_QueryInfoKey(struct dcesrv_call_state
*dce_call
,
448 struct winreg_QueryInfoKey
*r
)
450 struct auth_session_info
*session_info
=
451 dcesrv_call_session_info(dce_call
);
452 struct dcesrv_handle
*h
;
453 struct registry_key
*key
;
454 const char *classname
= NULL
;
457 DCESRV_PULL_HANDLE_FAULT(h
, r
->in
.handle
, HTYPE_REGKEY
);
460 switch (security_session_user_level(session_info
, NULL
))
462 case SECURITY_SYSTEM
:
463 case SECURITY_ADMINISTRATOR
:
465 result
= reg_key_get_info(mem_ctx
, key
, &classname
,
466 r
->out
.num_subkeys
, r
->out
.num_values
,
467 r
->out
.last_changed_time
, r
->out
.max_subkeylen
,
468 r
->out
.max_valnamelen
, r
->out
.max_valbufsize
);
470 if (r
->out
.max_subkeylen
!= NULL
) {
471 /* for UTF16 encoding */
472 *r
->out
.max_subkeylen
*= 2;
474 if (r
->out
.max_valnamelen
!= NULL
) {
475 /* for UTF16 encoding */
476 *r
->out
.max_valnamelen
*= 2;
479 if (classname
!= NULL
) {
480 r
->out
.classname
->name
= classname
;
481 r
->out
.classname
->name_len
= 2*strlen_m_term(classname
);
483 r
->out
.classname
->name
= r
->in
.classname
->name
;
484 r
->out
.classname
->name_len
= r
->in
.classname
->name_len
;
486 r
->out
.classname
->name_size
= r
->in
.classname
->name_size
;
490 return WERR_ACCESS_DENIED
;
498 static WERROR
dcesrv_winreg_QueryValue(struct dcesrv_call_state
*dce_call
,
500 struct winreg_QueryValue
*r
)
502 struct auth_session_info
*session_info
=
503 dcesrv_call_session_info(dce_call
);
504 struct dcesrv_handle
*h
;
505 struct registry_key
*key
;
507 DATA_BLOB value_data
;
510 DCESRV_PULL_HANDLE_FAULT(h
, r
->in
.handle
, HTYPE_REGKEY
);
513 switch (security_session_user_level(session_info
, NULL
))
515 case SECURITY_SYSTEM
:
516 case SECURITY_ADMINISTRATOR
:
518 if ((r
->in
.type
== NULL
) || (r
->in
.data_length
== NULL
) ||
519 (r
->in
.data_size
== NULL
)) {
520 return WERR_INVALID_PARAMETER
;
523 result
= reg_key_get_value_by_name(mem_ctx
, key
,
524 r
->in
.value_name
->name
, &value_type
, &value_data
);
526 if (!W_ERROR_IS_OK(result
)) {
527 /* if the lookup wasn't successful, send client query back */
528 value_type
= *r
->in
.type
;
529 value_data
.data
= r
->in
.data
;
530 value_data
.length
= *r
->in
.data_length
;
532 if ((r
->in
.data
!= NULL
)
533 && (*r
->in
.data_size
< value_data
.length
)) {
534 result
= WERR_MORE_DATA
;
538 r
->out
.type
= talloc(mem_ctx
, enum winreg_Type
);
540 return WERR_NOT_ENOUGH_MEMORY
;
542 *r
->out
.type
= (enum winreg_Type
) value_type
;
543 r
->out
.data_length
= talloc(mem_ctx
, uint32_t);
544 if (!r
->out
.data_length
) {
545 return WERR_NOT_ENOUGH_MEMORY
;
547 *r
->out
.data_length
= value_data
.length
;
548 r
->out
.data_size
= talloc(mem_ctx
, uint32_t);
549 if (!r
->out
.data_size
) {
550 return WERR_NOT_ENOUGH_MEMORY
;
552 *r
->out
.data_size
= value_data
.length
;
553 r
->out
.data
= value_data
.data
;
557 return WERR_ACCESS_DENIED
;
565 static WERROR
dcesrv_winreg_ReplaceKey(struct dcesrv_call_state
*dce_call
,
567 struct winreg_ReplaceKey
*r
)
569 return WERR_NOT_SUPPORTED
;
576 static WERROR
dcesrv_winreg_RestoreKey(struct dcesrv_call_state
*dce_call
,
578 struct winreg_RestoreKey
*r
)
580 return WERR_NOT_SUPPORTED
;
587 static WERROR
dcesrv_winreg_SaveKey(struct dcesrv_call_state
*dce_call
,
589 struct winreg_SaveKey
*r
)
591 return WERR_NOT_SUPPORTED
;
596 winreg_SetKeySecurity
598 static WERROR
dcesrv_winreg_SetKeySecurity(struct dcesrv_call_state
*dce_call
,
600 struct winreg_SetKeySecurity
*r
)
602 return WERR_NOT_SUPPORTED
;
609 static WERROR
dcesrv_winreg_SetValue(struct dcesrv_call_state
*dce_call
,
611 struct winreg_SetValue
*r
)
613 struct auth_session_info
*session_info
=
614 dcesrv_call_session_info(dce_call
);
615 struct dcesrv_handle
*h
;
616 struct registry_key
*key
;
620 DCESRV_PULL_HANDLE_FAULT(h
, r
->in
.handle
, HTYPE_REGKEY
);
623 switch (security_session_user_level(session_info
, NULL
))
625 case SECURITY_SYSTEM
:
626 case SECURITY_ADMINISTRATOR
:
627 data
.data
= r
->in
.data
;
628 data
.length
= r
->in
.size
;
629 result
= reg_val_set(key
, r
->in
.name
.name
, r
->in
.type
, data
);
632 return WERR_ACCESS_DENIED
;
640 static WERROR
dcesrv_winreg_UnLoadKey(struct dcesrv_call_state
*dce_call
,
642 struct winreg_UnLoadKey
*r
)
644 return WERR_NOT_SUPPORTED
;
649 winreg_InitiateSystemShutdown
651 static WERROR
dcesrv_winreg_InitiateSystemShutdown(struct dcesrv_call_state
*dce_call
,
653 struct winreg_InitiateSystemShutdown
*r
)
655 return WERR_NOT_SUPPORTED
;
660 winreg_AbortSystemShutdown
662 static WERROR
dcesrv_winreg_AbortSystemShutdown(struct dcesrv_call_state
*dce_call
,
664 struct winreg_AbortSystemShutdown
*r
)
666 return WERR_NOT_SUPPORTED
;
673 static WERROR
dcesrv_winreg_GetVersion(struct dcesrv_call_state
*dce_call
,
675 struct winreg_GetVersion
*r
)
677 struct dcesrv_handle
*h
;
679 DCESRV_PULL_HANDLE_FAULT(h
, r
->in
.handle
, HTYPE_REGKEY
);
681 r
->out
.version
= talloc(mem_ctx
, uint32_t);
682 W_ERROR_HAVE_NO_MEMORY(r
->out
.version
);
691 winreg_QueryMultipleValues
693 static WERROR
dcesrv_winreg_QueryMultipleValues(struct dcesrv_call_state
*dce_call
,
695 struct winreg_QueryMultipleValues
*r
)
697 return WERR_NOT_SUPPORTED
;
702 winreg_InitiateSystemShutdownEx
704 static WERROR
dcesrv_winreg_InitiateSystemShutdownEx(struct dcesrv_call_state
*dce_call
,
706 struct winreg_InitiateSystemShutdownEx
*r
)
708 return WERR_NOT_SUPPORTED
;
715 static WERROR
dcesrv_winreg_SaveKeyEx(struct dcesrv_call_state
*dce_call
,
717 struct winreg_SaveKeyEx
*r
)
719 return WERR_NOT_SUPPORTED
;
724 winreg_QueryMultipleValues2
726 static WERROR
dcesrv_winreg_QueryMultipleValues2(struct dcesrv_call_state
*dce_call
,
728 struct winreg_QueryMultipleValues2
*r
)
730 return WERR_NOT_SUPPORTED
;
736 static WERROR
dcesrv_winreg_DeleteKeyEx(struct dcesrv_call_state
*dce_call
,
738 struct winreg_DeleteKeyEx
*r
)
740 return WERR_NOT_SUPPORTED
;
743 /* include the generated boilerplate */
744 #include "librpc/gen_ndr/ndr_winreg_s.c"