s3-libgpo: Fix the build of the group policy CSEs
[ambi/samba-autobuild/.git] / source3 / libgpo / gpext / registry.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  Group Policy Support
4  *  Copyright (C) Guenther Deschner 2007-2008,2010
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 #include "includes.h"
21 #include "../libgpo/gpo_ini.h"
22 #include "../libgpo/gpo.h"
23 #include "libgpo/gpo_proto.h"
24 #include "registry.h"
25 #include "../librpc/gen_ndr/ndr_preg.h"
26 #include "libgpo/gpext/gpext.h"
27
28 #define GP_EXT_NAME "registry"
29
30 /* more info can be found at:
31  * http://msdn2.microsoft.com/en-us/library/aa374407.aspx */
32
33 #define GP_REGPOL_FILE  "Registry.pol"
34
35 #define GP_REGPOL_FILE_SIGNATURE 0x67655250 /* 'PReg' */
36 #define GP_REGPOL_FILE_VERSION 1
37
38 static TALLOC_CTX *ctx = NULL;
39
40 /****************************************************************
41 ****************************************************************/
42
43 static bool reg_parse_value(TALLOC_CTX *mem_ctx,
44                             const char **value,
45                             enum gp_reg_action *action)
46 {
47         if (!*value) {
48                 *action = GP_REG_ACTION_ADD_KEY;
49                 return true;
50         }
51
52         if (strncmp(*value, "**", 2) != 0) {
53                 *action = GP_REG_ACTION_ADD_VALUE;
54                 return true;
55         }
56
57         if (strnequal(*value, "**DelVals.", 10)) {
58                 *action = GP_REG_ACTION_DEL_ALL_VALUES;
59                 return true;
60         }
61
62         if (strnequal(*value, "**Del.", 6)) {
63                 *value = talloc_strdup(mem_ctx, *value + 6);
64                 *action = GP_REG_ACTION_DEL_VALUE;
65                 return true;
66         }
67
68         if (strnequal(*value, "**SecureKey", 11)) {
69                 if (strnequal(*value, "**SecureKey=1", 13)) {
70                         *action = GP_REG_ACTION_SEC_KEY_SET;
71                         return true;
72                 }
73
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;
78                         return true;
79                 }
80                 DEBUG(0,("unknown: SecureKey: %s\n", *value));
81                 smb_panic("not supported SecureKey method");
82                 return false;
83         }
84
85         if (strnequal(*value, "**DeleteValues", strlen("**DeleteValues"))) {
86                 smb_panic("not supported: **DeleteValues");
87                 *action = GP_REG_ACTION_DEL_VALUES;
88                 return false;
89         }
90
91         if (strnequal(*value, "**DeleteKeys", strlen("**DeleteKeys"))) {
92                 smb_panic("not supported: **DeleteKeys");
93                 *action = GP_REG_ACTION_DEL_KEYS;
94                 return false;
95         }
96
97         DEBUG(0,("unknown value: %s\n", *value));
98         smb_panic(*value);
99         return false;
100 }
101
102 /****************************************************************
103 ****************************************************************/
104
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)
108 {
109         struct registry_value *data = NULL;
110         struct gp_registry_entry *entry = NULL;
111         enum gp_reg_action action = GP_REG_ACTION_NONE;
112
113         ZERO_STRUCTP(*reg_entry);
114
115         data = talloc_zero(mem_ctx, struct registry_value);
116         if (!data)
117                 return false;
118
119         data->type = r->type;
120         data->data = data_blob_talloc(data, r->data, r->size);
121
122         entry = talloc_zero(mem_ctx, struct gp_registry_entry);
123         if (!entry)
124                 return false;
125
126         if (!reg_parse_value(mem_ctx, &r->valuename, &action))
127                 return false;
128
129         entry->key = talloc_strdup(entry, r->keyname);
130         entry->value = talloc_strdup(entry, r->valuename);
131         entry->data = data;
132         entry->action = action;
133
134         *reg_entry = entry;
135
136         return true;
137 }
138
139 /****************************************************************
140 ****************************************************************/
141
142 static NTSTATUS reg_parse_registry(TALLOC_CTX *mem_ctx,
143                                    uint32_t flags,
144                                    const char *filename,
145                                    struct gp_registry_entry **entries_p,
146                                    size_t *num_entries_p)
147 {
148         DATA_BLOB blob;
149         NTSTATUS status;
150         enum ndr_err_code ndr_err;
151         const char *real_filename = NULL;
152         struct preg_file r;
153         struct gp_registry_entry *entries = NULL;
154         size_t num_entries = 0;
155         int i;
156
157         status = gp_find_file(mem_ctx,
158                               flags,
159                               filename,
160                               GP_REGPOL_FILE,
161                               &real_filename);
162         if (!NT_STATUS_IS_OK(status)) {
163                 return status;
164         }
165
166         blob.data = (uint8_t *)file_load(real_filename, &blob.length, 0, NULL);
167         if (!blob.data) {
168                 return NT_STATUS_CANNOT_LOAD_REGISTRY_FILE;
169         }
170
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);
175                 goto out;
176         }
177
178         if (flags & GPO_INFO_FLAG_VERBOSE) {
179                 NDR_PRINT_DEBUG(preg_file, &r);
180         }
181
182         if (!strequal(r.header.signature, "PReg")) {
183                 status = NT_STATUS_INVALID_PARAMETER;
184                 goto out;
185         }
186
187         if (r.header.version != GP_REGPOL_FILE_VERSION) {
188                 status = NT_STATUS_INVALID_PARAMETER;
189                 goto out;
190         }
191
192         for (i=0; i < r.num_entries; i++) {
193
194                 struct gp_registry_entry *r_entry = NULL;
195
196                 if (!gp_reg_entry_from_file_entry(mem_ctx,
197                                                   &r.entries[i],
198                                                   &r_entry)) {
199                         status = NT_STATUS_NO_MEMORY;
200                         goto out;
201                 }
202
203                 if (!add_gp_registry_entry_to_array(mem_ctx,
204                                                     r_entry,
205                                                     &entries,
206                                                     &num_entries)) {
207                         status = NT_STATUS_NO_MEMORY;
208                         goto out;
209                 }
210         }
211
212         *entries_p = entries;
213         *num_entries_p = num_entries;
214
215         status = NT_STATUS_OK;
216
217  out:
218         data_blob_free(&blob);
219         return status;
220 }
221
222 /****************************************************************
223 ****************************************************************/
224
225 static WERROR reg_apply_registry(TALLOC_CTX *mem_ctx,
226                                  const struct security_token *token,
227                                  struct registry_key *root_key,
228                                  uint32_t flags,
229                                  struct gp_registry_entry *entries,
230                                  size_t num_entries)
231 {
232         struct gp_registry_context *reg_ctx = NULL;
233         WERROR werr;
234         size_t i;
235
236         if (num_entries == 0) {
237                 return WERR_OK;
238         }
239
240 #if 0
241         if (flags & GPO_LIST_FLAG_MACHINE) {
242                 werr = gp_init_reg_ctx(mem_ctx, KEY_HKLM, REG_KEY_WRITE,
243                                        get_system_token(),
244                                        &reg_ctx);
245         } else {
246                 werr = gp_init_reg_ctx(mem_ctx, KEY_HKCU, REG_KEY_WRITE,
247                                        token,
248                                        &reg_ctx);
249         }
250         W_ERROR_NOT_OK_RETURN(werr);
251 #endif
252         for (i=0; i<num_entries; i++) {
253
254                 /* FIXME: maybe we should check here if we attempt to go beyond
255                  * the 4 allowed reg keys */
256
257                 werr = reg_apply_registry_entry(mem_ctx, root_key,
258                                                 reg_ctx,
259                                                 &(entries)[i],
260                                                 token, flags);
261                 if (!W_ERROR_IS_OK(werr)) {
262                         DEBUG(0,("failed to apply registry: %s\n",
263                                 win_errstr(werr)));
264                         goto done;
265                 }
266         }
267
268 done:
269         gp_free_reg_ctx(reg_ctx);
270         return werr;
271 }
272
273
274 /****************************************************************
275 ****************************************************************/
276
277 static NTSTATUS registry_process_group_policy(TALLOC_CTX *mem_ctx,
278                                               uint32_t flags,
279                                               struct registry_key *root_key,
280                                               const struct security_token *token,
281                                               const struct GROUP_POLICY_OBJECT *deleted_gpo_list,
282                                               const struct GROUP_POLICY_OBJECT *changed_gpo_list)
283 {
284         NTSTATUS status;
285         WERROR werr;
286         struct gp_registry_entry *entries = NULL;
287         size_t num_entries = 0;
288         char *unix_path = NULL;
289         const struct GROUP_POLICY_OBJECT *gpo;
290         char *gpo_cache_path = cache_path(GPO_CACHE_DIR);
291         if (gpo_cache_path == NULL) {
292                 return NT_STATUS_NO_MEMORY;
293         }
294
295         /* implementation of the policy callback function, see
296          * http://msdn.microsoft.com/en-us/library/aa373494%28v=vs.85%29.aspx
297          * for details - gd */
298
299         /* for now do not process the list of deleted group policies
300
301         for (gpo = deleted_gpo_list; gpo; gpo = gpo->next) {
302         }
303
304         */
305
306         for (gpo = changed_gpo_list; gpo; gpo = gpo->next) {
307
308                 gpext_debug_header(0, "registry_process_group_policy", flags,
309                                    gpo, GP_EXT_GUID_REGISTRY, NULL);
310
311                 status = gpo_get_unix_path(mem_ctx, gpo_cache_path,
312                                            gpo, &unix_path);
313                 if (!NT_STATUS_IS_OK(status)) {
314                         goto err_cache_path_free;
315                 }
316
317                 status = reg_parse_registry(mem_ctx,
318                                             flags,
319                                             unix_path,
320                                             &entries,
321                                             &num_entries);
322                 if (!NT_STATUS_IS_OK(status)) {
323                         DEBUG(0,("failed to parse registry: %s\n",
324                                 nt_errstr(status)));
325                         goto err_cache_path_free;
326                 }
327
328                 dump_reg_entries(flags, "READ", entries, num_entries);
329
330                 werr = reg_apply_registry(mem_ctx, token, root_key, flags,
331                                           entries, num_entries);
332                 if (!W_ERROR_IS_OK(werr)) {
333                         DEBUG(0,("failed to apply registry: %s\n",
334                                 win_errstr(werr)));
335                         status = werror_to_ntstatus(werr);
336                         goto err_cache_path_free;
337                 }
338         }
339         status = NT_STATUS_OK;
340
341 err_cache_path_free:
342         talloc_free(gpo_cache_path);
343         return status;
344 }
345
346 /****************************************************************
347 ****************************************************************/
348
349 static NTSTATUS registry_get_reg_config(TALLOC_CTX *mem_ctx,
350                                         struct gp_extension_reg_info **reg_info)
351 {
352         NTSTATUS status;
353         struct gp_extension_reg_info *info = NULL;
354         struct gp_extension_reg_table table[] = {
355                 { "ProcessGroupPolicy", REG_SZ, "registry_process_group_policy" },
356                 { NULL, REG_NONE, NULL }
357         };
358
359         info = talloc_zero(mem_ctx, struct gp_extension_reg_info);
360         NT_STATUS_HAVE_NO_MEMORY(info);
361
362         status = gpext_info_add_entry(mem_ctx, GP_EXT_NAME,
363                                       GP_EXT_GUID_REGISTRY,
364                                       table, info);
365         NT_STATUS_NOT_OK_RETURN(status);
366
367         *reg_info = info;
368
369         return NT_STATUS_OK;
370 }
371
372 /****************************************************************
373 ****************************************************************/
374
375 static NTSTATUS registry_initialize(TALLOC_CTX *mem_ctx)
376 {
377         return NT_STATUS_OK;
378 }
379
380 /****************************************************************
381 ****************************************************************/
382
383 static NTSTATUS registry_shutdown(void)
384 {
385         NTSTATUS status;
386
387         status = gpext_unregister_gp_extension(GP_EXT_NAME);
388         if (NT_STATUS_IS_OK(status)) {
389                 return status;
390         }
391
392         TALLOC_FREE(ctx);
393
394         return NT_STATUS_OK;
395 }
396
397 /****************************************************************
398 ****************************************************************/
399
400 static struct gp_extension_methods registry_methods = {
401         .initialize             = registry_initialize,
402         .process_group_policy   = registry_process_group_policy,
403         .get_reg_config         = registry_get_reg_config,
404         .shutdown               = registry_shutdown
405 };
406
407 /****************************************************************
408 ****************************************************************/
409
410 NTSTATUS gpext_registry_init(TALLOC_CTX *mem_ctx)
411 {
412         NTSTATUS status;
413
414         ctx = talloc_init("gpext_registry_init");
415         NT_STATUS_HAVE_NO_MEMORY(ctx);
416
417         status = gpext_register_gp_extension(ctx, SMB_GPEXT_INTERFACE_VERSION,
418                                              GP_EXT_NAME, GP_EXT_GUID_REGISTRY,
419                                              &registry_methods);
420         if (!NT_STATUS_IS_OK(status)) {
421                 TALLOC_FREE(ctx);
422         }
423
424         return status;
425 }