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