4d87eb3afca76eb42418cbad6bca9215343895b9
[sfrench/samba-autobuild/.git] / libgpo / gpo_util.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  Group Policy Object Support
4  *  Copyright (C) Guenther Deschner 2005-2008
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 "system/filesys.h"
22 #include "librpc/gen_ndr/ndr_misc.h"
23 #include "../librpc/gen_ndr/ndr_security.h"
24 #include "../libgpo/gpo.h"
25 #include "../libcli/security/security.h"
26 #include "registry.h"
27 #include "libgpo/gpo_proto.h"
28
29 #if 0
30 #define DEFAULT_DOMAIN_POLICY "Default Domain Policy"
31 #define DEFAULT_DOMAIN_CONTROLLERS_POLICY "Default Domain Controllers Policy"
32 #endif
33
34 /* should we store a parsed guid ? */
35 struct gp_table {
36         const char *name;
37         const char *guid_string;
38 };
39
40 #if 0 /* unused */
41 static struct gp_table gpo_default_policy[] = {
42         { DEFAULT_DOMAIN_POLICY,
43                 "31B2F340-016D-11D2-945F-00C04FB984F9" },
44         { DEFAULT_DOMAIN_CONTROLLERS_POLICY,
45                 "6AC1786C-016F-11D2-945F-00C04fB984F9" },
46         { NULL, NULL }
47 };
48 #endif
49
50 /* the following is seen in gPCMachineExtensionNames / gPCUserExtensionNames */
51
52 static struct gp_table gpo_cse_extensions[] = {
53         /* used to be "Administrative Templates Extension" */
54         /* "Registry Settings"
55         (http://support.microsoft.com/kb/216357/EN-US/) */
56         { "Registry Settings",
57                 GP_EXT_GUID_REGISTRY },
58         { "Microsoft Disc Quota",
59                 "3610EDA5-77EF-11D2-8DC5-00C04FA31A66" },
60         { "EFS recovery",
61                 "B1BE8D72-6EAC-11D2-A4EA-00C04F79F83A" },
62         { "Folder Redirection",
63                 "25537BA6-77A8-11D2-9B6C-0000F8080861" },
64         { "IP Security",
65                 "E437BC1C-AA7D-11D2-A382-00C04F991E27" },
66         { "Internet Explorer Branding",
67                 "A2E30F80-D7DE-11d2-BBDE-00C04F86AE3B" },
68         { "QoS Packet Scheduler",
69                 "426031c0-0b47-4852-b0ca-ac3d37bfcb39" },
70         { "Scripts",
71                 GP_EXT_GUID_SCRIPTS },
72         { "Security",
73                 GP_EXT_GUID_SECURITY },
74         { "Software Installation",
75                 "C6DC5466-785A-11D2-84D0-00C04FB169F7" },
76         { "Wireless Group Policy",
77                 "0ACDD40C-75AC-BAA0-BF6DE7E7FE63" },
78         { "Application Management",
79                 "C6DC5466-785A-11D2-84D0-00C04FB169F7" },
80         { "unknown",
81                 "3060E8D0-7020-11D2-842D-00C04FA372D4" },
82         { NULL, NULL }
83 };
84
85 /* guess work */
86 static struct gp_table gpo_cse_snapin_extensions[] = {
87         { "Administrative Templates",
88                 "0F6B957D-509E-11D1-A7CC-0000F87571E3" },
89         { "Certificates",
90                 "53D6AB1D-2488-11D1-A28C-00C04FB94F17" },
91         { "EFS recovery policy processing",
92                 "B1BE8D72-6EAC-11D2-A4EA-00C04F79F83A" },
93         { "Folder Redirection policy processing",
94                 "25537BA6-77A8-11D2-9B6C-0000F8080861" },
95         { "Folder Redirection",
96                 "88E729D6-BDC1-11D1-BD2A-00C04FB9603F" },
97         { "Registry policy processing",
98                 "35378EAC-683F-11D2-A89A-00C04FBBCFA2" },
99         { "Remote Installation Services",
100                 "3060E8CE-7020-11D2-842D-00C04FA372D4" },
101         { "Security Settings",
102                 "803E14A0-B4FB-11D0-A0D0-00A0C90F574B" },
103         { "Security policy processing",
104                 "827D319E-6EAC-11D2-A4EA-00C04F79F83A" },
105         { "unknown",
106                 "3060E8D0-7020-11D2-842D-00C04FA372D4" },
107         { "unknown2",
108                 "53D6AB1B-2488-11D1-A28C-00C04FB94F17" },
109         { NULL, NULL }
110 };
111
112 /****************************************************************
113 ****************************************************************/
114
115 static const char *name_to_guid_string(const char *name,
116                                        struct gp_table *table)
117 {
118         int i;
119
120         for (i = 0; table[i].name; i++) {
121                 if (strequal(name, table[i].name)) {
122                         return table[i].guid_string;
123                 }
124         }
125
126         return NULL;
127 }
128
129 /****************************************************************
130 ****************************************************************/
131
132 static const char *guid_string_to_name(const char *guid_string,
133                                        struct gp_table *table)
134 {
135         int i;
136
137         for (i = 0; table[i].guid_string; i++) {
138                 if (strequal(guid_string, table[i].guid_string)) {
139                         return table[i].name;
140                 }
141         }
142
143         return NULL;
144 }
145
146 /****************************************************************
147 ****************************************************************/
148
149 static const char *snapin_guid_string_to_name(const char *guid_string,
150                                               struct gp_table *table)
151 {
152         int i;
153         for (i = 0; table[i].guid_string; i++) {
154                 if (strequal(guid_string, table[i].guid_string)) {
155                         return table[i].name;
156                 }
157         }
158         return NULL;
159 }
160
161 #if 0 /* unused */
162 static const char *default_gpo_name_to_guid_string(const char *name)
163 {
164         return name_to_guid_string(name, gpo_default_policy);
165 }
166
167 static const char *default_gpo_guid_string_to_name(const char *guid)
168 {
169         return guid_string_to_name(guid, gpo_default_policy);
170 }
171 #endif
172
173 /****************************************************************
174 ****************************************************************/
175
176 const char *cse_gpo_guid_string_to_name(const char *guid)
177 {
178         return guid_string_to_name(guid, gpo_cse_extensions);
179 }
180
181 /****************************************************************
182 ****************************************************************/
183
184 const char *cse_gpo_name_to_guid_string(const char *name)
185 {
186         return name_to_guid_string(name, gpo_cse_extensions);
187 }
188
189 /****************************************************************
190 ****************************************************************/
191
192 const char *cse_snapin_gpo_guid_string_to_name(const char *guid)
193 {
194         return snapin_guid_string_to_name(guid, gpo_cse_snapin_extensions);
195 }
196
197 /****************************************************************
198 ****************************************************************/
199
200 void dump_gp_ext(struct GP_EXT *gp_ext, int debuglevel)
201 {
202         int lvl = debuglevel;
203         int i;
204
205         if (gp_ext == NULL) {
206                 return;
207         }
208
209         DEBUG(lvl,("\t---------------------\n\n"));
210         DEBUGADD(lvl,("\tname:\t\t\t%s\n", gp_ext->gp_extension));
211
212         for (i=0; i< gp_ext->num_exts; i++) {
213
214                 DEBUGADD(lvl,("\textension:\t\t\t%s\n",
215                         gp_ext->extensions_guid[i]));
216                 DEBUGADD(lvl,("\textension (name):\t\t\t%s\n",
217                         gp_ext->extensions[i]));
218
219                 DEBUGADD(lvl,("\tsnapin:\t\t\t%s\n",
220                         gp_ext->snapins_guid[i]));
221                 DEBUGADD(lvl,("\tsnapin (name):\t\t\t%s\n",
222                         gp_ext->snapins[i]));
223         }
224 }
225
226 #ifdef HAVE_LDAP
227
228 /****************************************************************
229 ****************************************************************/
230
231 void dump_gpo(const struct GROUP_POLICY_OBJECT *gpo,
232               int debuglevel)
233 {
234         int lvl = debuglevel;
235         TALLOC_CTX *frame = talloc_stackframe();
236
237         if (gpo == NULL) {
238                 goto out;
239         }
240
241         DEBUG(lvl,("---------------------\n\n"));
242
243         DEBUGADD(lvl,("name:\t\t\t%s\n", gpo->name));
244         DEBUGADD(lvl,("displayname:\t\t%s\n", gpo->display_name));
245         DEBUGADD(lvl,("version:\t\t%d (0x%08x)\n", gpo->version, gpo->version));
246         DEBUGADD(lvl,("version_user:\t\t%d (0x%04x)\n",
247                 GPO_VERSION_USER(gpo->version),
248                 GPO_VERSION_USER(gpo->version)));
249         DEBUGADD(lvl,("version_machine:\t%d (0x%04x)\n",
250                 GPO_VERSION_MACHINE(gpo->version),
251                  GPO_VERSION_MACHINE(gpo->version)));
252         DEBUGADD(lvl,("filesyspath:\t\t%s\n", gpo->file_sys_path));
253         DEBUGADD(lvl,("dspath:\t\t%s\n", gpo->ds_path));
254
255         DEBUGADD(lvl,("options:\t\t%d ", gpo->options));
256         switch (gpo->options) {
257                 case GPFLAGS_ALL_ENABLED:
258                         DEBUGADD(lvl,("GPFLAGS_ALL_ENABLED\n"));
259                         break;
260                 case GPFLAGS_USER_SETTINGS_DISABLED:
261                         DEBUGADD(lvl,("GPFLAGS_USER_SETTINGS_DISABLED\n"));
262                         break;
263                 case GPFLAGS_MACHINE_SETTINGS_DISABLED:
264                         DEBUGADD(lvl,("GPFLAGS_MACHINE_SETTINGS_DISABLED\n"));
265                         break;
266                 case GPFLAGS_ALL_DISABLED:
267                         DEBUGADD(lvl,("GPFLAGS_ALL_DISABLED\n"));
268                         break;
269                 default:
270                         DEBUGADD(lvl,("unknown option: %d\n", gpo->options));
271                         break;
272         }
273
274         DEBUGADD(lvl,("link:\t\t\t%s\n", gpo->link));
275         DEBUGADD(lvl,("link_type:\t\t%d ", gpo->link_type));
276         switch (gpo->link_type) {
277                 case GP_LINK_UNKOWN:
278                         DEBUGADD(lvl,("GP_LINK_UNKOWN\n"));
279                         break;
280                 case GP_LINK_OU:
281                         DEBUGADD(lvl,("GP_LINK_OU\n"));
282                         break;
283                 case GP_LINK_DOMAIN:
284                         DEBUGADD(lvl,("GP_LINK_DOMAIN\n"));
285                         break;
286                 case GP_LINK_SITE:
287                         DEBUGADD(lvl,("GP_LINK_SITE\n"));
288                         break;
289                 case GP_LINK_MACHINE:
290                         DEBUGADD(lvl,("GP_LINK_MACHINE\n"));
291                         break;
292                 default:
293                         break;
294         }
295
296         DEBUGADD(lvl,("machine_extensions:\t%s\n", gpo->machine_extensions));
297
298         if (gpo->machine_extensions) {
299
300                 struct GP_EXT *gp_ext = NULL;
301
302                 if (!ads_parse_gp_ext(frame, gpo->machine_extensions,
303                                       &gp_ext)) {
304                         goto out;
305                 }
306                 dump_gp_ext(gp_ext, lvl);
307         }
308
309         DEBUGADD(lvl,("user_extensions:\t%s\n", gpo->user_extensions));
310
311         if (gpo->user_extensions) {
312
313                 struct GP_EXT *gp_ext = NULL;
314
315                 if (!ads_parse_gp_ext(frame, gpo->user_extensions,
316                                       &gp_ext)) {
317                         goto out;
318                 }
319                 dump_gp_ext(gp_ext, lvl);
320         }
321         if (gpo->security_descriptor) {
322                 DEBUGADD(lvl,("security descriptor:\n"));
323
324                 NDR_PRINT_DEBUG(security_descriptor, gpo->security_descriptor);
325         }
326  out:
327         talloc_free(frame);
328 }
329
330 /****************************************************************
331 ****************************************************************/
332
333 void dump_gpo_list(const struct GROUP_POLICY_OBJECT *gpo_list,
334                    int debuglevel)
335 {
336         const struct GROUP_POLICY_OBJECT *gpo = NULL;
337
338         for (gpo = gpo_list; gpo; gpo = gpo->next) {
339                 dump_gpo(gpo, debuglevel);
340         }
341 }
342
343 /****************************************************************
344 ****************************************************************/
345
346 void dump_gplink(const struct GP_LINK *gp_link)
347 {
348         int i;
349         int lvl = 10;
350
351         if (gp_link == NULL) {
352                 return;
353         }
354
355         DEBUG(lvl,("---------------------\n\n"));
356
357         DEBUGADD(lvl,("gplink: %s\n", gp_link->gp_link));
358         DEBUGADD(lvl,("gpopts: %d ", gp_link->gp_opts));
359         switch (gp_link->gp_opts) {
360                 case GPOPTIONS_INHERIT:
361                         DEBUGADD(lvl,("GPOPTIONS_INHERIT\n"));
362                         break;
363                 case GPOPTIONS_BLOCK_INHERITANCE:
364                         DEBUGADD(lvl,("GPOPTIONS_BLOCK_INHERITANCE\n"));
365                         break;
366                 default:
367                         break;
368         }
369
370         DEBUGADD(lvl,("num links: %d\n", gp_link->num_links));
371
372         for (i = 0; i < gp_link->num_links; i++) {
373
374                 DEBUGADD(lvl,("---------------------\n\n"));
375
376                 DEBUGADD(lvl,("link: #%d\n", i + 1));
377                 DEBUGADD(lvl,("name: %s\n", gp_link->link_names[i]));
378
379                 DEBUGADD(lvl,("opt: %d ", gp_link->link_opts[i]));
380                 if (gp_link->link_opts[i] & GPO_LINK_OPT_ENFORCED) {
381                         DEBUGADD(lvl,("GPO_LINK_OPT_ENFORCED "));
382                 }
383                 if (gp_link->link_opts[i] & GPO_LINK_OPT_DISABLED) {
384                         DEBUGADD(lvl,("GPO_LINK_OPT_DISABLED"));
385                 }
386                 DEBUGADD(lvl,("\n"));
387         }
388 }
389
390 #endif /* HAVE_LDAP */
391
392 /****************************************************************
393 ****************************************************************/
394
395 bool gpo_get_gp_ext_from_gpo(TALLOC_CTX *mem_ctx,
396                              uint32_t flags,
397                              const struct GROUP_POLICY_OBJECT *gpo,
398                              struct GP_EXT **gp_ext)
399 {
400         ZERO_STRUCTP(*gp_ext);
401
402         if (flags & GPO_INFO_FLAG_MACHINE) {
403
404                 if (gpo->machine_extensions) {
405
406                         if (!ads_parse_gp_ext(mem_ctx, gpo->machine_extensions,
407                                               gp_ext)) {
408                                 return false;
409                         }
410                 }
411         } else {
412
413                 if (gpo->user_extensions) {
414
415                         if (!ads_parse_gp_ext(mem_ctx, gpo->user_extensions,
416                                               gp_ext)) {
417                                 return false;
418                         }
419                 }
420         }
421
422         return true;
423 }
424
425 /****************************************************************
426 ****************************************************************/
427
428 NTSTATUS gpo_process_gpo_list(TALLOC_CTX *mem_ctx,
429                               const struct security_token *token,
430                               const struct GROUP_POLICY_OBJECT *deleted_gpo_list,
431                               const struct GROUP_POLICY_OBJECT *changed_gpo_list,
432                               const char *extensions_guid_filter,
433                               uint32_t flags)
434 {
435         NTSTATUS status = NT_STATUS_OK;
436         struct registry_key *root_key = NULL;
437         struct gp_registry_context *reg_ctx = NULL;
438         WERROR werr;
439
440         /* get the key here */
441         if (flags & GPO_LIST_FLAG_MACHINE) {
442                 werr = gp_init_reg_ctx(mem_ctx, KEY_HKLM, REG_KEY_WRITE,
443                                        get_system_token(),
444                                        &reg_ctx);
445         } else {
446                 werr = gp_init_reg_ctx(mem_ctx, KEY_HKCU, REG_KEY_WRITE,
447                                        token,
448                                        &reg_ctx);
449         }
450         if (!W_ERROR_IS_OK(werr)) {
451                 talloc_free(reg_ctx);
452                 return werror_to_ntstatus(werr);
453         }
454
455         root_key = reg_ctx->curr_key;
456
457         status = gpext_process_extension(mem_ctx,
458                                          flags, token, root_key,
459                                          deleted_gpo_list,
460                                          changed_gpo_list,
461                                          extensions_guid_filter);
462         talloc_free(reg_ctx);
463         talloc_free(root_key);
464         gpext_free_gp_extensions();
465
466         return status;
467 }
468
469
470 /****************************************************************
471  check wether the version number in a GROUP_POLICY_OBJECT match those of the
472  locally stored version. If not, fetch the required policy via CIFS
473 ****************************************************************/
474
475 NTSTATUS check_refresh_gpo(ADS_STRUCT *ads,
476                            TALLOC_CTX *mem_ctx,
477                            const char *cache_dir,
478                            uint32_t flags,
479                            struct GROUP_POLICY_OBJECT *gpo)
480 {
481         NTSTATUS result;
482         char *server = NULL;
483         char *share = NULL;
484         char *nt_path = NULL;
485         char *unix_path = NULL;
486         uint32_t sysvol_gpt_version = 0;
487         char *display_name = NULL;
488
489         result = gpo_explode_filesyspath(mem_ctx, cache_dir, gpo->file_sys_path,
490                                          &server, &share, &nt_path, &unix_path);
491
492         if (!NT_STATUS_IS_OK(result)) {
493                 goto out;
494         }
495
496         result = gpo_get_sysvol_gpt_version(mem_ctx,
497                                             unix_path,
498                                             &sysvol_gpt_version,
499                                             &display_name);
500         if (!NT_STATUS_IS_OK(result) &&
501             !NT_STATUS_EQUAL(result, NT_STATUS_NO_SUCH_FILE)) {
502                 DEBUG(10,("check_refresh_gpo: "
503                         "failed to get local gpt version: %s\n",
504                         nt_errstr(result)));
505                 goto out;
506         }
507
508         DEBUG(10,("check_refresh_gpo: versions gpo %d sysvol %d\n",
509                 gpo->version, sysvol_gpt_version));
510
511         /* FIXME: handle GPO_INFO_FLAG_FORCED_REFRESH from flags */
512
513         while (gpo->version > sysvol_gpt_version) {
514
515                 DEBUG(1,("check_refresh_gpo: need to refresh GPO\n"));
516
517                 result = gpo_fetch_files(mem_ctx, ads, cache_dir, gpo);
518                 if (!NT_STATUS_IS_OK(result)) {
519                         goto out;
520                 }
521
522                 result = gpo_get_sysvol_gpt_version(mem_ctx,
523                                                     unix_path,
524                                                     &sysvol_gpt_version,
525                                                     &display_name);
526                 if (!NT_STATUS_IS_OK(result)) {
527                         DEBUG(10,("check_refresh_gpo: "
528                                 "failed to get local gpt version: %s\n",
529                                 nt_errstr(result)));
530                         goto out;
531                 }
532
533                 if (gpo->version == sysvol_gpt_version) {
534                         break;
535                 }
536         }
537
538         DEBUG(10,("Name:\t\t\t%s (%s)\n", gpo->display_name, gpo->name));
539         DEBUGADD(10,("sysvol GPT version:\t%d (user: %d, machine: %d)\n",
540                 sysvol_gpt_version,
541                 GPO_VERSION_USER(sysvol_gpt_version),
542                 GPO_VERSION_MACHINE(sysvol_gpt_version)));
543         DEBUGADD(10,("LDAP GPO version:\t%d (user: %d, machine: %d)\n",
544                 gpo->version,
545                 GPO_VERSION_USER(gpo->version),
546                 GPO_VERSION_MACHINE(gpo->version)));
547         DEBUGADD(10,("LDAP GPO link:\t\t%s\n", gpo->link));
548
549         result = NT_STATUS_OK;
550
551  out:
552         return result;
553
554 }
555
556 /****************************************************************
557  check wether the version numbers in the gpo_list match the locally stored, if
558  not, go and get each required GPO via CIFS
559  ****************************************************************/
560
561 NTSTATUS check_refresh_gpo_list(ADS_STRUCT *ads,
562                                 TALLOC_CTX *mem_ctx,
563                                 const char *cache_dir,
564                                 uint32_t flags,
565                                 struct GROUP_POLICY_OBJECT *gpo_list)
566 {
567         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
568         struct GROUP_POLICY_OBJECT *gpo;
569
570         if (!gpo_list) {
571                 return NT_STATUS_INVALID_PARAMETER;
572         }
573
574         for (gpo = gpo_list; gpo; gpo = gpo->next) {
575
576                 result = check_refresh_gpo(ads, mem_ctx, cache_dir, flags, gpo);
577                 if (!NT_STATUS_IS_OK(result)) {
578                         goto out;
579                 }
580         }
581
582         result = NT_STATUS_OK;
583
584  out:
585         /* FIXME close cli connection */
586
587         return result;
588 }
589
590 /****************************************************************
591 ****************************************************************/
592
593 NTSTATUS gpo_get_unix_path(TALLOC_CTX *mem_ctx,
594                            const char *cache_dir,
595                            struct GROUP_POLICY_OBJECT *gpo,
596                            char **unix_path)
597 {
598         char *server, *share, *nt_path;
599         return gpo_explode_filesyspath(mem_ctx, cache_dir, gpo->file_sys_path,
600                                        &server, &share, &nt_path, unix_path);
601 }
602
603 /****************************************************************
604 ****************************************************************/
605
606 char *gpo_flag_str(TALLOC_CTX *ctx, uint32_t flags)
607 {
608         char *str = NULL;
609
610         if (flags == 0) {
611                 return NULL;
612         }
613
614         str = talloc_strdup(ctx, "");
615         if (!str) {
616                 return NULL;
617         }
618
619         if (flags & GPO_INFO_FLAG_SLOWLINK)
620                 str = talloc_strdup_append(str, "GPO_INFO_FLAG_SLOWLINK ");
621         if (flags & GPO_INFO_FLAG_VERBOSE)
622                 str = talloc_strdup_append(str, "GPO_INFO_FLAG_VERBOSE ");
623         if (flags & GPO_INFO_FLAG_SAFEMODE_BOOT)
624                 str = talloc_strdup_append(str, "GPO_INFO_FLAG_SAFEMODE_BOOT ");
625         if (flags & GPO_INFO_FLAG_NOCHANGES)
626                 str = talloc_strdup_append(str, "GPO_INFO_FLAG_NOCHANGES ");
627         if (flags & GPO_INFO_FLAG_MACHINE)
628                 str = talloc_strdup_append(str, "GPO_INFO_FLAG_MACHINE ");
629         if (flags & GPO_INFO_FLAG_LOGRSOP_TRANSITION)
630                 str = talloc_strdup_append(str, "GPO_INFO_FLAG_LOGRSOP_TRANSITION ");
631         if (flags & GPO_INFO_FLAG_LINKTRANSITION)
632                 str = talloc_strdup_append(str, "GPO_INFO_FLAG_LINKTRANSITION ");
633         if (flags & GPO_INFO_FLAG_FORCED_REFRESH)
634                 str = talloc_strdup_append(str, "GPO_INFO_FLAG_FORCED_REFRESH ");
635         if (flags & GPO_INFO_FLAG_BACKGROUND)
636                 str = talloc_strdup_append(str, "GPO_INFO_FLAG_BACKGROUND ");
637
638         return str;
639 }
640
641 /****************************************************************
642 ****************************************************************/
643
644 NTSTATUS gp_find_file(TALLOC_CTX *mem_ctx,
645                       uint32_t flags,
646                       const char *filename,
647                       const char *suffix,
648                       const char **filename_out)
649 {
650         const char *tmp = NULL;
651         struct stat sbuf;
652         const char *path = NULL;
653
654         if (flags & GPO_LIST_FLAG_MACHINE) {
655                 path = "Machine";
656         } else {
657                 path = "User";
658         }
659
660         tmp = talloc_asprintf(mem_ctx, "%s/%s/%s", filename,
661                               path, suffix);
662         NT_STATUS_HAVE_NO_MEMORY(tmp);
663
664         if (stat(tmp, &sbuf) == 0) {
665                 *filename_out = tmp;
666                 return NT_STATUS_OK;
667         }
668
669         path = talloc_strdup_upper(mem_ctx, path);
670         NT_STATUS_HAVE_NO_MEMORY(path);
671
672         tmp = talloc_asprintf(mem_ctx, "%s/%s/%s", filename,
673                               path, suffix);
674         NT_STATUS_HAVE_NO_MEMORY(tmp);
675
676         if (stat(tmp, &sbuf) == 0) {
677                 *filename_out = tmp;
678                 return NT_STATUS_OK;
679         }
680
681         return NT_STATUS_NO_SUCH_FILE;
682 }
683
684 /****************************************************************
685 ****************************************************************/
686
687 ADS_STATUS gp_get_machine_token(ADS_STRUCT *ads,
688                                 TALLOC_CTX *mem_ctx,
689                                 const char *dn,
690                                 struct security_token **token)
691 {
692 #ifdef HAVE_ADS
693         struct security_token *ad_token = NULL;
694         ADS_STATUS status;
695         NTSTATUS ntstatus;
696
697         status = ads_get_sid_token(ads, mem_ctx, dn, &ad_token);
698         if (!ADS_ERR_OK(status)) {
699                 return status;
700         }
701         ntstatus = merge_nt_token(mem_ctx, ad_token, get_system_token(),
702                                   token);
703         if (!NT_STATUS_IS_OK(ntstatus)) {
704                 return ADS_ERROR_NT(ntstatus);
705         }
706         return ADS_SUCCESS;
707 #else
708         return ADS_ERROR_NT(NT_STATUS_NOT_SUPPORTED);
709 #endif
710 }
711
712 /****************************************************************
713 ****************************************************************/
714
715 NTSTATUS gpo_copy(TALLOC_CTX *mem_ctx,
716                   const struct GROUP_POLICY_OBJECT *gpo_src,
717                   struct GROUP_POLICY_OBJECT **gpo_dst)
718 {
719         struct GROUP_POLICY_OBJECT *gpo;
720
721         gpo = talloc_zero(mem_ctx, struct GROUP_POLICY_OBJECT);
722         NT_STATUS_HAVE_NO_MEMORY(gpo);
723
724         gpo->options            = gpo_src->options;
725         gpo->version            = gpo_src->version;
726
727         gpo->ds_path            = talloc_strdup(gpo, gpo_src->ds_path);
728         NT_STATUS_HAVE_NO_MEMORY_AND_FREE(gpo->ds_path, gpo);
729
730         gpo->file_sys_path      = talloc_strdup(gpo, gpo_src->file_sys_path);
731         NT_STATUS_HAVE_NO_MEMORY_AND_FREE(gpo->file_sys_path, gpo);
732
733         gpo->display_name       = talloc_strdup(gpo, gpo_src->display_name);
734         NT_STATUS_HAVE_NO_MEMORY_AND_FREE(gpo->display_name, gpo);
735
736         gpo->name               = talloc_strdup(gpo, gpo_src->name);
737         NT_STATUS_HAVE_NO_MEMORY_AND_FREE(gpo->name, gpo);
738
739         gpo->link               = talloc_strdup(gpo, gpo_src->link);
740         NT_STATUS_HAVE_NO_MEMORY_AND_FREE(gpo->link, gpo);
741
742         gpo->link_type          = gpo_src->link_type;
743
744         if (gpo_src->user_extensions) {
745                 gpo->user_extensions = talloc_strdup(gpo, gpo_src->user_extensions);
746                 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(gpo->user_extensions, gpo);
747         }
748
749         if (gpo_src->machine_extensions) {
750                 gpo->machine_extensions = talloc_strdup(gpo, gpo_src->machine_extensions);
751                 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(gpo->machine_extensions, gpo);
752         }
753
754         gpo->security_descriptor = dup_sec_desc(gpo, gpo_src->security_descriptor);
755         NT_STATUS_HAVE_NO_MEMORY_AND_FREE(gpo->security_descriptor, gpo);
756
757         gpo->next = gpo->prev = NULL;
758
759         *gpo_dst = gpo;
760
761         return NT_STATUS_OK;
762 }