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