libgpo: fix segfault in gpo_process_gpo_list().
[metze/samba/wip.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(ADS_STRUCT *ads,
232               TALLOC_CTX *mem_ctx,
233               struct GROUP_POLICY_OBJECT *gpo,
234               int debuglevel)
235 {
236         int lvl = debuglevel;
237
238         if (gpo == NULL) {
239                 return;
240         }
241
242         DEBUG(lvl,("---------------------\n\n"));
243
244         DEBUGADD(lvl,("name:\t\t\t%s\n", gpo->name));
245         DEBUGADD(lvl,("displayname:\t\t%s\n", gpo->display_name));
246         DEBUGADD(lvl,("version:\t\t%d (0x%08x)\n", gpo->version, gpo->version));
247         DEBUGADD(lvl,("version_user:\t\t%d (0x%04x)\n",
248                 GPO_VERSION_USER(gpo->version),
249                 GPO_VERSION_USER(gpo->version)));
250         DEBUGADD(lvl,("version_machine:\t%d (0x%04x)\n",
251                 GPO_VERSION_MACHINE(gpo->version),
252                  GPO_VERSION_MACHINE(gpo->version)));
253         DEBUGADD(lvl,("filesyspath:\t\t%s\n", gpo->file_sys_path));
254         DEBUGADD(lvl,("dspath:\t\t%s\n", gpo->ds_path));
255
256         DEBUGADD(lvl,("options:\t\t%d ", gpo->options));
257         switch (gpo->options) {
258                 case GPFLAGS_ALL_ENABLED:
259                         DEBUGADD(lvl,("GPFLAGS_ALL_ENABLED\n"));
260                         break;
261                 case GPFLAGS_USER_SETTINGS_DISABLED:
262                         DEBUGADD(lvl,("GPFLAGS_USER_SETTINGS_DISABLED\n"));
263                         break;
264                 case GPFLAGS_MACHINE_SETTINGS_DISABLED:
265                         DEBUGADD(lvl,("GPFLAGS_MACHINE_SETTINGS_DISABLED\n"));
266                         break;
267                 case GPFLAGS_ALL_DISABLED:
268                         DEBUGADD(lvl,("GPFLAGS_ALL_DISABLED\n"));
269                         break;
270                 default:
271                         DEBUGADD(lvl,("unknown option: %d\n", gpo->options));
272                         break;
273         }
274
275         DEBUGADD(lvl,("link:\t\t\t%s\n", gpo->link));
276         DEBUGADD(lvl,("link_type:\t\t%d ", gpo->link_type));
277         switch (gpo->link_type) {
278                 case GP_LINK_UNKOWN:
279                         DEBUGADD(lvl,("GP_LINK_UNKOWN\n"));
280                         break;
281                 case GP_LINK_OU:
282                         DEBUGADD(lvl,("GP_LINK_OU\n"));
283                         break;
284                 case GP_LINK_DOMAIN:
285                         DEBUGADD(lvl,("GP_LINK_DOMAIN\n"));
286                         break;
287                 case GP_LINK_SITE:
288                         DEBUGADD(lvl,("GP_LINK_SITE\n"));
289                         break;
290                 case GP_LINK_MACHINE:
291                         DEBUGADD(lvl,("GP_LINK_MACHINE\n"));
292                         break;
293                 default:
294                         break;
295         }
296
297         DEBUGADD(lvl,("machine_extensions:\t%s\n", gpo->machine_extensions));
298
299         if (gpo->machine_extensions) {
300
301                 struct GP_EXT *gp_ext = NULL;
302
303                 if (!ads_parse_gp_ext(mem_ctx, gpo->machine_extensions,
304                                       &gp_ext)) {
305                         return;
306                 }
307                 dump_gp_ext(gp_ext, lvl);
308         }
309
310         DEBUGADD(lvl,("user_extensions:\t%s\n", gpo->user_extensions));
311
312         if (gpo->user_extensions) {
313
314                 struct GP_EXT *gp_ext = NULL;
315
316                 if (!ads_parse_gp_ext(mem_ctx, gpo->user_extensions,
317                                       &gp_ext)) {
318                         return;
319                 }
320                 dump_gp_ext(gp_ext, lvl);
321         }
322         if (gpo->security_descriptor) {
323                 DEBUGADD(lvl,("security descriptor:\n"));
324
325                 NDR_PRINT_DEBUG(security_descriptor, gpo->security_descriptor);
326         }
327 }
328
329 /****************************************************************
330 ****************************************************************/
331
332 void dump_gpo_list(ADS_STRUCT *ads,
333                    TALLOC_CTX *mem_ctx,
334                    struct GROUP_POLICY_OBJECT *gpo_list,
335                    int debuglevel)
336 {
337         struct GROUP_POLICY_OBJECT *gpo = NULL;
338
339         for (gpo = gpo_list; gpo; gpo = gpo->next) {
340                 dump_gpo(ads, mem_ctx, gpo, debuglevel);
341         }
342 }
343
344 /****************************************************************
345 ****************************************************************/
346
347 void dump_gplink(ADS_STRUCT *ads, TALLOC_CTX *mem_ctx, struct GP_LINK *gp_link)
348 {
349         ADS_STATUS status;
350         int i;
351         int lvl = 10;
352
353         if (gp_link == NULL) {
354                 return;
355         }
356
357         DEBUG(lvl,("---------------------\n\n"));
358
359         DEBUGADD(lvl,("gplink: %s\n", gp_link->gp_link));
360         DEBUGADD(lvl,("gpopts: %d ", gp_link->gp_opts));
361         switch (gp_link->gp_opts) {
362                 case GPOPTIONS_INHERIT:
363                         DEBUGADD(lvl,("GPOPTIONS_INHERIT\n"));
364                         break;
365                 case GPOPTIONS_BLOCK_INHERITANCE:
366                         DEBUGADD(lvl,("GPOPTIONS_BLOCK_INHERITANCE\n"));
367                         break;
368                 default:
369                         break;
370         }
371
372         DEBUGADD(lvl,("num links: %d\n", gp_link->num_links));
373
374         for (i = 0; i < gp_link->num_links; i++) {
375
376                 DEBUGADD(lvl,("---------------------\n\n"));
377
378                 DEBUGADD(lvl,("link: #%d\n", i + 1));
379                 DEBUGADD(lvl,("name: %s\n", gp_link->link_names[i]));
380
381                 DEBUGADD(lvl,("opt: %d ", gp_link->link_opts[i]));
382                 if (gp_link->link_opts[i] & GPO_LINK_OPT_ENFORCED) {
383                         DEBUGADD(lvl,("GPO_LINK_OPT_ENFORCED "));
384                 }
385                 if (gp_link->link_opts[i] & GPO_LINK_OPT_DISABLED) {
386                         DEBUGADD(lvl,("GPO_LINK_OPT_DISABLED"));
387                 }
388                 DEBUGADD(lvl,("\n"));
389
390                 if (ads != NULL && mem_ctx != NULL) {
391
392                         struct GROUP_POLICY_OBJECT gpo;
393
394                         status = ads_get_gpo(ads, mem_ctx,
395                                              gp_link->link_names[i],
396                                              NULL, NULL, &gpo);
397                         if (!ADS_ERR_OK(status)) {
398                                 DEBUG(lvl,("get gpo for %s failed: %s\n",
399                                         gp_link->link_names[i],
400                                         ads_errstr(status)));
401                                 return;
402                         }
403                         dump_gpo(ads, mem_ctx, &gpo, lvl);
404                 }
405         }
406 }
407
408 #endif /* HAVE_LDAP */
409
410 /****************************************************************
411 ****************************************************************/
412
413 static bool gpo_get_gp_ext_from_gpo(TALLOC_CTX *mem_ctx,
414                                     uint32_t flags,
415                                     struct GROUP_POLICY_OBJECT *gpo,
416                                     struct GP_EXT **gp_ext)
417 {
418         ZERO_STRUCTP(*gp_ext);
419
420         if (flags & GPO_INFO_FLAG_MACHINE) {
421
422                 if (gpo->machine_extensions) {
423
424                         if (!ads_parse_gp_ext(mem_ctx, gpo->machine_extensions,
425                                               gp_ext)) {
426                                 return false;
427                         }
428                 }
429         } else {
430
431                 if (gpo->user_extensions) {
432
433                         if (!ads_parse_gp_ext(mem_ctx, gpo->user_extensions,
434                                               gp_ext)) {
435                                 return false;
436                         }
437                 }
438         }
439
440         return true;
441 }
442
443 /****************************************************************
444 ****************************************************************/
445
446 ADS_STATUS gpo_process_a_gpo(ADS_STRUCT *ads,
447                              TALLOC_CTX *mem_ctx,
448                              const struct security_token *token,
449                              struct registry_key *root_key,
450                              struct GROUP_POLICY_OBJECT *gpo,
451                              const char *extension_guid_filter,
452                              uint32_t flags)
453 {
454         struct GP_EXT *gp_ext = NULL;
455         int i;
456
457         DEBUG(10,("gpo_process_a_gpo: processing gpo %s (%s)\n",
458                 gpo->name, gpo->display_name));
459         if (extension_guid_filter) {
460                 DEBUGADD(10,("gpo_process_a_gpo: using filter %s (%s)\n",
461                         extension_guid_filter,
462                         cse_gpo_guid_string_to_name(extension_guid_filter)));
463         }
464
465         if (!gpo_get_gp_ext_from_gpo(mem_ctx, flags, gpo, &gp_ext)) {
466                 return ADS_ERROR_NT(NT_STATUS_INVALID_PARAMETER);
467         }
468
469         if (!gp_ext || !gp_ext->num_exts) {
470                 if (flags & GPO_INFO_FLAG_VERBOSE) {
471                         DEBUG(0,("gpo_process_a_gpo: "
472                                 "no policies in %s (%s) for this extension\n",
473                                 gpo->name, gpo->display_name));
474                 }
475                 return ADS_SUCCESS;
476         }
477
478         for (i=0; i<gp_ext->num_exts; i++) {
479
480                 NTSTATUS ntstatus;
481
482                 if (extension_guid_filter &&
483                     !strequal(extension_guid_filter,
484                               gp_ext->extensions_guid[i])) {
485                         continue;
486                 }
487
488                 ntstatus = gpext_process_extension(ads, mem_ctx,
489                                                    flags, token, root_key, gpo,
490                                                    gp_ext->extensions_guid[i],
491                                                    gp_ext->snapins_guid[i]);
492                 if (!NT_STATUS_IS_OK(ntstatus)) {
493                         ADS_ERROR_NT(ntstatus);
494                 }
495         }
496
497         return ADS_SUCCESS;
498 }
499
500 /****************************************************************
501 ****************************************************************/
502
503 static ADS_STATUS gpo_process_gpo_list_by_ext(ADS_STRUCT *ads,
504                                               TALLOC_CTX *mem_ctx,
505                                               const struct security_token *token,
506                                               struct registry_key *root_key,
507                                               struct GROUP_POLICY_OBJECT *gpo_list,
508                                               const char *extensions_guid,
509                                               uint32_t flags)
510 {
511         ADS_STATUS status;
512         struct GROUP_POLICY_OBJECT *gpo;
513
514         for (gpo = gpo_list; gpo; gpo = gpo->next) {
515
516                 if (gpo->link_type == GP_LINK_LOCAL) {
517                         continue;
518                 }
519
520
521                 /* FIXME: we need to pass down the *list* down to the
522                  * extension, otherwise we cannot store the e.g. the *list* of
523                  * logon-scripts correctly (for more then one GPO) */
524
525                 status = gpo_process_a_gpo(ads, mem_ctx, token, root_key,
526                                            gpo, extensions_guid, flags);
527
528                 if (!ADS_ERR_OK(status)) {
529                         DEBUG(0,("failed to process gpo by ext: %s\n",
530                                 ads_errstr(status)));
531                         return status;
532                 }
533         }
534
535         return ADS_SUCCESS;
536 }
537
538 /****************************************************************
539 ****************************************************************/
540
541 ADS_STATUS gpo_process_gpo_list(ADS_STRUCT *ads,
542                                 TALLOC_CTX *mem_ctx,
543                                 const struct security_token *token,
544                                 struct GROUP_POLICY_OBJECT *gpo_list,
545                                 const char *extensions_guid_filter,
546                                 uint32_t flags)
547 {
548         ADS_STATUS status = ADS_SUCCESS;
549         struct gp_extension *gp_ext_list = NULL;
550         struct gp_extension *gp_ext = NULL;
551         struct registry_key *root_key = NULL;
552         struct gp_registry_context *reg_ctx = NULL;
553         WERROR werr;
554
555         status = ADS_ERROR_NT(init_gp_extensions(mem_ctx));
556         if (!ADS_ERR_OK(status)) {
557                 return status;
558         }
559
560         gp_ext_list = get_gp_extension_list();
561         if (!gp_ext_list) {
562                 return ADS_ERROR_NT(NT_STATUS_DLL_INIT_FAILED);
563         }
564
565         /* get the key here */
566         if (flags & GPO_LIST_FLAG_MACHINE) {
567                 werr = gp_init_reg_ctx(mem_ctx, KEY_HKLM, REG_KEY_WRITE,
568                                        get_system_token(),
569                                        &reg_ctx);
570         } else {
571                 werr = gp_init_reg_ctx(mem_ctx, KEY_HKCU, REG_KEY_WRITE,
572                                        token,
573                                        &reg_ctx);
574         }
575         if (!W_ERROR_IS_OK(werr)) {
576                 talloc_free(reg_ctx);
577                 return ADS_ERROR_NT(werror_to_ntstatus(werr));
578         }
579
580         root_key = reg_ctx->curr_key;
581
582         for (gp_ext = gp_ext_list; gp_ext; gp_ext = gp_ext->next) {
583
584                 const char *guid_str = NULL;
585
586                 guid_str = GUID_string(mem_ctx, gp_ext->guid);
587                 if (!guid_str) {
588                         status = ADS_ERROR_NT(NT_STATUS_NO_MEMORY);
589                         goto done;
590                 }
591
592                 if (extensions_guid_filter &&
593                     (!strequal(guid_str, extensions_guid_filter)))  {
594                         continue;
595                 }
596
597                 DEBUG(0,("-------------------------------------------------\n"));
598                 DEBUG(0,("gpo_process_gpo_list: processing ext: %s {%s}\n",
599                         gp_ext->name, guid_str));
600
601
602                 status = gpo_process_gpo_list_by_ext(ads, mem_ctx, token,
603                                                      root_key, gpo_list,
604                                                      guid_str, flags);
605                 if (!ADS_ERR_OK(status)) {
606                         goto done;
607                 }
608         }
609
610  done:
611         talloc_free(reg_ctx);
612         talloc_free(root_key);
613         free_gp_extensions();
614
615         return status;
616 }
617
618
619 /****************************************************************
620  check wether the version number in a GROUP_POLICY_OBJECT match those of the
621  locally stored version. If not, fetch the required policy via CIFS
622 ****************************************************************/
623
624 NTSTATUS check_refresh_gpo(ADS_STRUCT *ads,
625                            TALLOC_CTX *mem_ctx,
626                            const char *cache_dir,
627                            uint32_t flags,
628                            struct GROUP_POLICY_OBJECT *gpo)
629 {
630         NTSTATUS result;
631         char *server = NULL;
632         char *share = NULL;
633         char *nt_path = NULL;
634         char *unix_path = NULL;
635         uint32_t sysvol_gpt_version = 0;
636         char *display_name = NULL;
637
638         result = gpo_explode_filesyspath(mem_ctx, cache_dir, gpo->file_sys_path,
639                                          &server, &share, &nt_path, &unix_path);
640
641         if (!NT_STATUS_IS_OK(result)) {
642                 goto out;
643         }
644
645         result = gpo_get_sysvol_gpt_version(mem_ctx,
646                                             unix_path,
647                                             &sysvol_gpt_version,
648                                             &display_name);
649         if (!NT_STATUS_IS_OK(result) &&
650             !NT_STATUS_EQUAL(result, NT_STATUS_NO_SUCH_FILE)) {
651                 DEBUG(10,("check_refresh_gpo: "
652                         "failed to get local gpt version: %s\n",
653                         nt_errstr(result)));
654                 goto out;
655         }
656
657         DEBUG(10,("check_refresh_gpo: versions gpo %d sysvol %d\n",
658                 gpo->version, sysvol_gpt_version));
659
660         /* FIXME: handle GPO_INFO_FLAG_FORCED_REFRESH from flags */
661
662         while (gpo->version > sysvol_gpt_version) {
663
664                 DEBUG(1,("check_refresh_gpo: need to refresh GPO\n"));
665
666                 result = gpo_fetch_files(mem_ctx, ads, cache_dir, gpo);
667                 if (!NT_STATUS_IS_OK(result)) {
668                         goto out;
669                 }
670
671                 result = gpo_get_sysvol_gpt_version(mem_ctx,
672                                                     unix_path,
673                                                     &sysvol_gpt_version,
674                                                     &display_name);
675                 if (!NT_STATUS_IS_OK(result)) {
676                         DEBUG(10,("check_refresh_gpo: "
677                                 "failed to get local gpt version: %s\n",
678                                 nt_errstr(result)));
679                         goto out;
680                 }
681
682                 if (gpo->version == sysvol_gpt_version) {
683                         break;
684                 }
685         }
686
687         DEBUG(10,("Name:\t\t\t%s (%s)\n", gpo->display_name, gpo->name));
688         DEBUGADD(10,("sysvol GPT version:\t%d (user: %d, machine: %d)\n",
689                 sysvol_gpt_version,
690                 GPO_VERSION_USER(sysvol_gpt_version),
691                 GPO_VERSION_MACHINE(sysvol_gpt_version)));
692         DEBUGADD(10,("LDAP GPO version:\t%d (user: %d, machine: %d)\n",
693                 gpo->version,
694                 GPO_VERSION_USER(gpo->version),
695                 GPO_VERSION_MACHINE(gpo->version)));
696         DEBUGADD(10,("LDAP GPO link:\t\t%s\n", gpo->link));
697
698         result = NT_STATUS_OK;
699
700  out:
701         return result;
702
703 }
704
705 /****************************************************************
706  check wether the version numbers in the gpo_list match the locally stored, if
707  not, go and get each required GPO via CIFS
708  ****************************************************************/
709
710 NTSTATUS check_refresh_gpo_list(ADS_STRUCT *ads,
711                                 TALLOC_CTX *mem_ctx,
712                                 const char *cache_dir,
713                                 uint32_t flags,
714                                 struct GROUP_POLICY_OBJECT *gpo_list)
715 {
716         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
717         struct GROUP_POLICY_OBJECT *gpo;
718
719         if (!gpo_list) {
720                 return NT_STATUS_INVALID_PARAMETER;
721         }
722
723         for (gpo = gpo_list; gpo; gpo = gpo->next) {
724
725                 result = check_refresh_gpo(ads, mem_ctx, cache_dir, flags, gpo);
726                 if (!NT_STATUS_IS_OK(result)) {
727                         goto out;
728                 }
729         }
730
731         result = NT_STATUS_OK;
732
733  out:
734         /* FIXME close cli connection */
735
736         return result;
737 }
738
739 /****************************************************************
740 ****************************************************************/
741
742 NTSTATUS gpo_get_unix_path(TALLOC_CTX *mem_ctx,
743                            const char *cache_dir,
744                            struct GROUP_POLICY_OBJECT *gpo,
745                            char **unix_path)
746 {
747         char *server, *share, *nt_path;
748         return gpo_explode_filesyspath(mem_ctx, cache_dir, gpo->file_sys_path,
749                                        &server, &share, &nt_path, unix_path);
750 }
751
752 /****************************************************************
753 ****************************************************************/
754
755 char *gpo_flag_str(TALLOC_CTX *ctx, uint32_t flags)
756 {
757         char *str = NULL;
758
759         if (flags == 0) {
760                 return NULL;
761         }
762
763         str = talloc_strdup(ctx, "");
764         if (!str) {
765                 return NULL;
766         }
767
768         if (flags & GPO_INFO_FLAG_SLOWLINK)
769                 str = talloc_strdup_append(str, "GPO_INFO_FLAG_SLOWLINK ");
770         if (flags & GPO_INFO_FLAG_VERBOSE)
771                 str = talloc_strdup_append(str, "GPO_INFO_FLAG_VERBOSE ");
772         if (flags & GPO_INFO_FLAG_SAFEMODE_BOOT)
773                 str = talloc_strdup_append(str, "GPO_INFO_FLAG_SAFEMODE_BOOT ");
774         if (flags & GPO_INFO_FLAG_NOCHANGES)
775                 str = talloc_strdup_append(str, "GPO_INFO_FLAG_NOCHANGES ");
776         if (flags & GPO_INFO_FLAG_MACHINE)
777                 str = talloc_strdup_append(str, "GPO_INFO_FLAG_MACHINE ");
778         if (flags & GPO_INFO_FLAG_LOGRSOP_TRANSITION)
779                 str = talloc_strdup_append(str, "GPO_INFO_FLAG_LOGRSOP_TRANSITION ");
780         if (flags & GPO_INFO_FLAG_LINKTRANSITION)
781                 str = talloc_strdup_append(str, "GPO_INFO_FLAG_LINKTRANSITION ");
782         if (flags & GPO_INFO_FLAG_FORCED_REFRESH)
783                 str = talloc_strdup_append(str, "GPO_INFO_FLAG_FORCED_REFRESH ");
784         if (flags & GPO_INFO_FLAG_BACKGROUND)
785                 str = talloc_strdup_append(str, "GPO_INFO_FLAG_BACKGROUND ");
786
787         return str;
788 }
789
790 /****************************************************************
791 ****************************************************************/
792
793 NTSTATUS gp_find_file(TALLOC_CTX *mem_ctx,
794                       uint32_t flags,
795                       const char *filename,
796                       const char *suffix,
797                       const char **filename_out)
798 {
799         const char *tmp = NULL;
800         struct stat sbuf;
801         const char *path = NULL;
802
803         if (flags & GPO_LIST_FLAG_MACHINE) {
804                 path = "Machine";
805         } else {
806                 path = "User";
807         }
808
809         tmp = talloc_asprintf(mem_ctx, "%s/%s/%s", filename,
810                               path, suffix);
811         NT_STATUS_HAVE_NO_MEMORY(tmp);
812
813         if (stat(tmp, &sbuf) == 0) {
814                 *filename_out = tmp;
815                 return NT_STATUS_OK;
816         }
817
818         path = talloc_strdup_upper(mem_ctx, path);
819         NT_STATUS_HAVE_NO_MEMORY(path);
820
821         tmp = talloc_asprintf(mem_ctx, "%s/%s/%s", filename,
822                               path, suffix);
823         NT_STATUS_HAVE_NO_MEMORY(tmp);
824
825         if (stat(tmp, &sbuf) == 0) {
826                 *filename_out = tmp;
827                 return NT_STATUS_OK;
828         }
829
830         return NT_STATUS_NO_SUCH_FILE;
831 }
832
833 /****************************************************************
834 ****************************************************************/
835
836 ADS_STATUS gp_get_machine_token(ADS_STRUCT *ads,
837                                 TALLOC_CTX *mem_ctx,
838                                 const char *dn,
839                                 struct security_token **token)
840 {
841 #ifdef HAVE_ADS
842         struct security_token *ad_token = NULL;
843         ADS_STATUS status;
844         NTSTATUS ntstatus;
845
846         status = ads_get_sid_token(ads, mem_ctx, dn, &ad_token);
847         if (!ADS_ERR_OK(status)) {
848                 return status;
849         }
850         ntstatus = merge_nt_token(mem_ctx, ad_token, get_system_token(),
851                                   token);
852         if (!NT_STATUS_IS_OK(ntstatus)) {
853                 return ADS_ERROR_NT(ntstatus);
854         }
855         return ADS_SUCCESS;
856 #else
857         return ADS_ERROR_NT(NT_STATUS_NOT_SUPPORTED);
858 #endif
859 }