2 * Unix SMB/CIFS implementation.
4 * Copyright (C) Guenther Deschner 2007-2008,2010
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "../libgpo/gpo_ini.h"
22 #include "../libgpo/gpo.h"
23 #include "libgpo/gpo_proto.h"
25 #include "../librpc/gen_ndr/ndr_preg.h"
26 #include "libgpo/gpext/gpext.h"
28 #define GP_EXT_NAME "registry"
30 /* more info can be found at:
31 * http://msdn2.microsoft.com/en-us/library/aa374407.aspx */
33 #define GP_REGPOL_FILE "Registry.pol"
35 #define GP_REGPOL_FILE_SIGNATURE 0x67655250 /* 'PReg' */
36 #define GP_REGPOL_FILE_VERSION 1
38 static TALLOC_CTX
*ctx
= NULL
;
40 /****************************************************************
41 ****************************************************************/
43 static bool reg_parse_value(TALLOC_CTX
*mem_ctx
,
45 enum gp_reg_action
*action
)
48 *action
= GP_REG_ACTION_ADD_KEY
;
52 if (strncmp(*value
, "**", 2) != 0) {
53 *action
= GP_REG_ACTION_ADD_VALUE
;
57 if (strnequal(*value
, "**DelVals.", 10)) {
58 *action
= GP_REG_ACTION_DEL_ALL_VALUES
;
62 if (strnequal(*value
, "**Del.", 6)) {
63 *value
= talloc_strdup(mem_ctx
, *value
+ 6);
64 *action
= GP_REG_ACTION_DEL_VALUE
;
68 if (strnequal(*value
, "**SecureKey", 11)) {
69 if (strnequal(*value
, "**SecureKey=1", 13)) {
70 *action
= GP_REG_ACTION_SEC_KEY_SET
;
74 /*************** not tested from here on ***************/
75 if (strnequal(*value
, "**SecureKey=0", 13)) {
76 smb_panic("not supported: **SecureKey=0");
77 *action
= GP_REG_ACTION_SEC_KEY_RESET
;
80 DEBUG(0,("unknown: SecureKey: %s\n", *value
));
81 smb_panic("not supported SecureKey method");
85 if (strnequal(*value
, "**DeleteValues", strlen("**DeleteValues"))) {
86 smb_panic("not supported: **DeleteValues");
87 *action
= GP_REG_ACTION_DEL_VALUES
;
91 if (strnequal(*value
, "**DeleteKeys", strlen("**DeleteKeys"))) {
92 smb_panic("not supported: **DeleteKeys");
93 *action
= GP_REG_ACTION_DEL_KEYS
;
97 DEBUG(0,("unknown value: %s\n", *value
));
102 /****************************************************************
103 ****************************************************************/
105 static bool gp_reg_entry_from_file_entry(TALLOC_CTX
*mem_ctx
,
106 struct preg_entry
*r
,
107 struct gp_registry_entry
**reg_entry
)
109 struct registry_value
*data
= NULL
;
110 struct gp_registry_entry
*entry
= NULL
;
111 enum gp_reg_action action
= GP_REG_ACTION_NONE
;
113 ZERO_STRUCTP(*reg_entry
);
115 data
= talloc_zero(mem_ctx
, struct registry_value
);
119 data
->type
= r
->type
;
120 data
->data
= data_blob_talloc(data
, r
->data
, r
->size
);
122 entry
= talloc_zero(mem_ctx
, struct gp_registry_entry
);
126 if (!reg_parse_value(mem_ctx
, &r
->valuename
, &action
))
129 entry
->key
= talloc_strdup(entry
, r
->keyname
);
130 entry
->value
= talloc_strdup(entry
, r
->valuename
);
132 entry
->action
= action
;
139 /****************************************************************
140 ****************************************************************/
142 static NTSTATUS
reg_parse_registry(TALLOC_CTX
*mem_ctx
,
144 const char *filename
,
145 struct gp_registry_entry
**entries_p
,
146 size_t *num_entries_p
)
150 enum ndr_err_code ndr_err
;
151 const char *real_filename
= NULL
;
153 struct gp_registry_entry
*entries
= NULL
;
154 size_t num_entries
= 0;
157 status
= gp_find_file(mem_ctx
,
162 if (!NT_STATUS_IS_OK(status
)) {
166 blob
.data
= (uint8_t *)file_load(real_filename
, &blob
.length
, 0, NULL
);
168 return NT_STATUS_CANNOT_LOAD_REGISTRY_FILE
;
171 ndr_err
= ndr_pull_struct_blob(&blob
, mem_ctx
, &r
,
172 (ndr_pull_flags_fn_t
)ndr_pull_preg_file
);
173 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
174 status
= ndr_map_error2ntstatus(ndr_err
);
178 if (!strequal(r
.header
.signature
, "PReg")) {
179 status
= NT_STATUS_INVALID_PARAMETER
;
183 if (r
.header
.version
!= GP_REGPOL_FILE_VERSION
) {
184 status
= NT_STATUS_INVALID_PARAMETER
;
188 for (i
=0; i
< r
.num_entries
; i
++) {
190 struct gp_registry_entry
*r_entry
= NULL
;
192 if (!gp_reg_entry_from_file_entry(mem_ctx
,
195 status
= NT_STATUS_NO_MEMORY
;
199 if (!add_gp_registry_entry_to_array(mem_ctx
,
203 status
= NT_STATUS_NO_MEMORY
;
208 *entries_p
= entries
;
209 *num_entries_p
= num_entries
;
211 status
= NT_STATUS_OK
;
214 data_blob_free(&blob
);
218 /****************************************************************
219 ****************************************************************/
221 static WERROR
reg_apply_registry(TALLOC_CTX
*mem_ctx
,
222 const struct security_token
*token
,
223 struct registry_key
*root_key
,
225 struct gp_registry_entry
*entries
,
228 struct gp_registry_context
*reg_ctx
= NULL
;
232 if (num_entries
== 0) {
237 if (flags
& GPO_LIST_FLAG_MACHINE
) {
238 werr
= gp_init_reg_ctx(mem_ctx
, KEY_HKLM
, REG_KEY_WRITE
,
242 werr
= gp_init_reg_ctx(mem_ctx
, KEY_HKCU
, REG_KEY_WRITE
,
246 W_ERROR_NOT_OK_RETURN(werr
);
248 for (i
=0; i
<num_entries
; i
++) {
250 /* FIXME: maybe we should check here if we attempt to go beyond
251 * the 4 allowed reg keys */
253 werr
= reg_apply_registry_entry(mem_ctx
, root_key
,
257 if (!W_ERROR_IS_OK(werr
)) {
258 DEBUG(0,("failed to apply registry: %s\n",
265 gp_free_reg_ctx(reg_ctx
);
270 /****************************************************************
271 ****************************************************************/
273 static NTSTATUS
registry_process_group_policy(TALLOC_CTX
*mem_ctx
,
275 struct registry_key
*root_key
,
276 const struct security_token
*token
,
277 struct GROUP_POLICY_OBJECT
*deleted_gpo_list
,
278 struct GROUP_POLICY_OBJECT
*changed_gpo_list
)
282 struct gp_registry_entry
*entries
= NULL
;
283 size_t num_entries
= 0;
284 char *unix_path
= NULL
;
285 struct GROUP_POLICY_OBJECT
*gpo
;
287 /* implementation of the policy callback function, see
288 * http://msdn.microsoft.com/en-us/library/aa373494%28v=vs.85%29.aspx
289 * for details - gd */
291 /* for now do not process the list of deleted group policies
293 for (gpo = deleted_gpo_list; gpo; gpo = gpo->next) {
298 for (gpo
= changed_gpo_list
; gpo
; gpo
= gpo
->next
) {
300 gpext_debug_header(0, "registry_process_group_policy", flags
,
301 gpo
, GP_EXT_GUID_REGISTRY
, NULL
);
303 status
= gpo_get_unix_path(mem_ctx
, cache_path(GPO_CACHE_DIR
),
305 NT_STATUS_NOT_OK_RETURN(status
);
307 status
= reg_parse_registry(mem_ctx
,
312 if (!NT_STATUS_IS_OK(status
)) {
313 DEBUG(0,("failed to parse registry: %s\n",
318 dump_reg_entries(flags
, "READ", entries
, num_entries
);
320 werr
= reg_apply_registry(mem_ctx
, token
, root_key
, flags
,
321 entries
, num_entries
);
322 if (!W_ERROR_IS_OK(werr
)) {
323 DEBUG(0,("failed to apply registry: %s\n",
325 return werror_to_ntstatus(werr
);
332 /****************************************************************
333 ****************************************************************/
335 static NTSTATUS
registry_get_reg_config(TALLOC_CTX
*mem_ctx
,
336 struct gp_extension_reg_info
**reg_info
)
339 struct gp_extension_reg_info
*info
= NULL
;
340 struct gp_extension_reg_table table
[] = {
341 { "ProcessGroupPolicy", REG_SZ
, "registry_process_group_policy" },
342 { NULL
, REG_NONE
, NULL
}
345 info
= talloc_zero(mem_ctx
, struct gp_extension_reg_info
);
346 NT_STATUS_HAVE_NO_MEMORY(info
);
348 status
= gpext_info_add_entry(mem_ctx
, GP_EXT_NAME
,
349 GP_EXT_GUID_REGISTRY
,
351 NT_STATUS_NOT_OK_RETURN(status
);
358 /****************************************************************
359 ****************************************************************/
361 static NTSTATUS
registry_initialize(TALLOC_CTX
*mem_ctx
)
366 /****************************************************************
367 ****************************************************************/
369 static NTSTATUS
registry_shutdown(void)
373 status
= gpext_unregister_gp_extension(GP_EXT_NAME
);
374 if (NT_STATUS_IS_OK(status
)) {
383 /****************************************************************
384 ****************************************************************/
386 static struct gp_extension_methods registry_methods
= {
387 .initialize
= registry_initialize
,
388 .process_group_policy
= registry_process_group_policy
,
389 .get_reg_config
= registry_get_reg_config
,
390 .shutdown
= registry_shutdown
393 /****************************************************************
394 ****************************************************************/
396 NTSTATUS
gpext_registry_init(void)
400 ctx
= talloc_init("gpext_registry_init");
401 NT_STATUS_HAVE_NO_MEMORY(ctx
);
403 status
= gpext_register_gp_extension(ctx
, SMB_GPEXT_INTERFACE_VERSION
,
404 GP_EXT_NAME
, GP_EXT_GUID_REGISTRY
,
406 if (!NT_STATUS_IS_OK(status
)) {