Fix bug 8920, null dereference
[samba.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 #define TALLOC_DEPRECATED 1
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 #undef strdup
27
28 #if 0
29 #define DEFAULT_DOMAIN_POLICY "Default Domain Policy"
30 #define DEFAULT_DOMAIN_CONTROLLERS_POLICY "Default Domain Controllers Policy"
31 #endif
32
33 /* should we store a parsed guid ? */
34 struct gp_table {
35         const char *name;
36         const char *guid_string;
37 };
38
39 #if 0 /* unused */
40 static struct gp_table gpo_default_policy[] = {
41         { DEFAULT_DOMAIN_POLICY,
42                 "31B2F340-016D-11D2-945F-00C04FB984F9" },
43         { DEFAULT_DOMAIN_CONTROLLERS_POLICY,
44                 "6AC1786C-016F-11D2-945F-00C04fB984F9" },
45         { NULL, NULL }
46 };
47 #endif
48
49 /* the following is seen in gPCMachineExtensionNames / gPCUserExtensionNames */
50
51 static struct gp_table gpo_cse_extensions[] = {
52         /* used to be "Administrative Templates Extension" */
53         /* "Registry Settings"
54         (http://support.microsoft.com/kb/216357/EN-US/) */
55         { "Registry Settings",
56                 GP_EXT_GUID_REGISTRY },
57         { "Microsoft Disc Quota",
58                 "3610EDA5-77EF-11D2-8DC5-00C04FA31A66" },
59         { "EFS recovery",
60                 "B1BE8D72-6EAC-11D2-A4EA-00C04F79F83A" },
61         { "Folder Redirection",
62                 "25537BA6-77A8-11D2-9B6C-0000F8080861" },
63         { "IP Security",
64                 "E437BC1C-AA7D-11D2-A382-00C04F991E27" },
65         { "Internet Explorer Branding",
66                 "A2E30F80-D7DE-11d2-BBDE-00C04F86AE3B" },
67         { "QoS Packet Scheduler",
68                 "426031c0-0b47-4852-b0ca-ac3d37bfcb39" },
69         { "Scripts",
70                 GP_EXT_GUID_SCRIPTS },
71         { "Security",
72                 GP_EXT_GUID_SECURITY },
73         { "Software Installation",
74                 "C6DC5466-785A-11D2-84D0-00C04FB169F7" },
75         { "Wireless Group Policy",
76                 "0ACDD40C-75AC-BAA0-BF6DE7E7FE63" },
77         { "Application Management",
78                 "C6DC5466-785A-11D2-84D0-00C04FB169F7" },
79         { "unknown",
80                 "3060E8D0-7020-11D2-842D-00C04FA372D4" },
81         { NULL, NULL }
82 };
83
84 /* guess work */
85 static struct gp_table gpo_cse_snapin_extensions[] = {
86         { "Administrative Templates",
87                 "0F6B957D-509E-11D1-A7CC-0000F87571E3" },
88         { "Certificates",
89                 "53D6AB1D-2488-11D1-A28C-00C04FB94F17" },
90         { "EFS recovery policy processing",
91                 "B1BE8D72-6EAC-11D2-A4EA-00C04F79F83A" },
92         { "Folder Redirection policy processing",
93                 "25537BA6-77A8-11D2-9B6C-0000F8080861" },
94         { "Folder Redirection",
95                 "88E729D6-BDC1-11D1-BD2A-00C04FB9603F" },
96         { "Registry policy processing",
97                 "35378EAC-683F-11D2-A89A-00C04FBBCFA2" },
98         { "Remote Installation Services",
99                 "3060E8CE-7020-11D2-842D-00C04FA372D4" },
100         { "Security Settings",
101                 "803E14A0-B4FB-11D0-A0D0-00A0C90F574B" },
102         { "Security policy processing",
103                 "827D319E-6EAC-11D2-A4EA-00C04F79F83A" },
104         { "unknown",
105                 "3060E8D0-7020-11D2-842D-00C04FA372D4" },
106         { "unknown2",
107                 "53D6AB1B-2488-11D1-A28C-00C04FB94F17" },
108         { NULL, NULL }
109 };
110
111 /****************************************************************
112 ****************************************************************/
113
114 static const char *name_to_guid_string(const char *name,
115                                        struct gp_table *table)
116 {
117         int i;
118
119         for (i = 0; table[i].name; i++) {
120                 if (strequal(name, table[i].name)) {
121                         return table[i].guid_string;
122                 }
123         }
124
125         return NULL;
126 }
127
128 /****************************************************************
129 ****************************************************************/
130
131 static const char *guid_string_to_name(const char *guid_string,
132                                        struct gp_table *table)
133 {
134         int i;
135
136         for (i = 0; table[i].guid_string; i++) {
137                 if (strequal(guid_string, table[i].guid_string)) {
138                         return table[i].name;
139                 }
140         }
141
142         return NULL;
143 }
144
145 /****************************************************************
146 ****************************************************************/
147
148 static const char *snapin_guid_string_to_name(const char *guid_string,
149                                               struct gp_table *table)
150 {
151         int i;
152         for (i = 0; table[i].guid_string; i++) {
153                 if (strequal(guid_string, table[i].guid_string)) {
154                         return table[i].name;
155                 }
156         }
157         return NULL;
158 }
159
160 #if 0 /* unused */
161 static const char *default_gpo_name_to_guid_string(const char *name)
162 {
163         return name_to_guid_string(name, gpo_default_policy);
164 }
165
166 static const char *default_gpo_guid_string_to_name(const char *guid)
167 {
168         return guid_string_to_name(guid, gpo_default_policy);
169 }
170 #endif
171
172 /****************************************************************
173 ****************************************************************/
174
175 const char *cse_gpo_guid_string_to_name(const char *guid)
176 {
177         return guid_string_to_name(guid, gpo_cse_extensions);
178 }
179
180 /****************************************************************
181 ****************************************************************/
182
183 const char *cse_gpo_name_to_guid_string(const char *name)
184 {
185         return name_to_guid_string(name, gpo_cse_extensions);
186 }
187
188 /****************************************************************
189 ****************************************************************/
190
191 const char *cse_snapin_gpo_guid_string_to_name(const char *guid)
192 {
193         return snapin_guid_string_to_name(guid, gpo_cse_snapin_extensions);
194 }
195
196 /****************************************************************
197 ****************************************************************/
198
199 void dump_gp_ext(struct GP_EXT *gp_ext, int debuglevel)
200 {
201         int lvl = debuglevel;
202         int i;
203
204         if (gp_ext == NULL) {
205                 return;
206         }
207
208         DEBUG(lvl,("\t---------------------\n\n"));
209         DEBUGADD(lvl,("\tname:\t\t\t%s\n", gp_ext->gp_extension));
210
211         for (i=0; i< gp_ext->num_exts; i++) {
212
213                 DEBUGADD(lvl,("\textension:\t\t\t%s\n",
214                         gp_ext->extensions_guid[i]));
215                 DEBUGADD(lvl,("\textension (name):\t\t\t%s\n",
216                         gp_ext->extensions[i]));
217
218                 DEBUGADD(lvl,("\tsnapin:\t\t\t%s\n",
219                         gp_ext->snapins_guid[i]));
220                 DEBUGADD(lvl,("\tsnapin (name):\t\t\t%s\n",
221                         gp_ext->snapins[i]));
222         }
223 }
224
225 #ifdef HAVE_LDAP
226
227 /****************************************************************
228 ****************************************************************/
229
230 void dump_gpo(ADS_STRUCT *ads,
231               TALLOC_CTX *mem_ctx,
232               struct GROUP_POLICY_OBJECT *gpo,
233               int debuglevel)
234 {
235         int lvl = debuglevel;
236
237         if (gpo == NULL) {
238                 return;
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(mem_ctx, gpo->machine_extensions,
303                                       &gp_ext)) {
304                         return;
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(mem_ctx, gpo->user_extensions,
316                                       &gp_ext)) {
317                         return;
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 }
327
328 /****************************************************************
329 ****************************************************************/
330
331 void dump_gpo_list(ADS_STRUCT *ads,
332                    TALLOC_CTX *mem_ctx,
333                    struct GROUP_POLICY_OBJECT *gpo_list,
334                    int debuglevel)
335 {
336         struct GROUP_POLICY_OBJECT *gpo = NULL;
337
338         for (gpo = gpo_list; gpo; gpo = gpo->next) {
339                 dump_gpo(ads, mem_ctx, gpo, debuglevel);
340         }
341 }
342
343 /****************************************************************
344 ****************************************************************/
345
346 void dump_gplink(ADS_STRUCT *ads, TALLOC_CTX *mem_ctx, struct GP_LINK *gp_link)
347 {
348         ADS_STATUS status;
349         int i;
350         int lvl = 10;
351
352         if (gp_link == NULL) {
353                 return;
354         }
355
356         DEBUG(lvl,("---------------------\n\n"));
357
358         DEBUGADD(lvl,("gplink: %s\n", gp_link->gp_link));
359         DEBUGADD(lvl,("gpopts: %d ", gp_link->gp_opts));
360         switch (gp_link->gp_opts) {
361                 case GPOPTIONS_INHERIT:
362                         DEBUGADD(lvl,("GPOPTIONS_INHERIT\n"));
363                         break;
364                 case GPOPTIONS_BLOCK_INHERITANCE:
365                         DEBUGADD(lvl,("GPOPTIONS_BLOCK_INHERITANCE\n"));
366                         break;
367                 default:
368                         break;
369         }
370
371         DEBUGADD(lvl,("num links: %d\n", gp_link->num_links));
372
373         for (i = 0; i < gp_link->num_links; i++) {
374
375                 DEBUGADD(lvl,("---------------------\n\n"));
376
377                 DEBUGADD(lvl,("link: #%d\n", i + 1));
378                 DEBUGADD(lvl,("name: %s\n", gp_link->link_names[i]));
379
380                 DEBUGADD(lvl,("opt: %d ", gp_link->link_opts[i]));
381                 if (gp_link->link_opts[i] & GPO_LINK_OPT_ENFORCED) {
382                         DEBUGADD(lvl,("GPO_LINK_OPT_ENFORCED "));
383                 }
384                 if (gp_link->link_opts[i] & GPO_LINK_OPT_DISABLED) {
385                         DEBUGADD(lvl,("GPO_LINK_OPT_DISABLED"));
386                 }
387                 DEBUGADD(lvl,("\n"));
388
389                 if (ads != NULL && mem_ctx != NULL) {
390
391                         struct GROUP_POLICY_OBJECT gpo;
392
393                         status = ads_get_gpo(ads, mem_ctx,
394                                              gp_link->link_names[i],
395                                              NULL, NULL, &gpo);
396                         if (!ADS_ERR_OK(status)) {
397                                 DEBUG(lvl,("get gpo for %s failed: %s\n",
398                                         gp_link->link_names[i],
399                                         ads_errstr(status)));
400                                 return;
401                         }
402                         dump_gpo(ads, mem_ctx, &gpo, lvl);
403                 }
404         }
405 }
406
407 #endif /* HAVE_LDAP */
408
409 /****************************************************************
410 ****************************************************************/
411
412 static bool gpo_get_gp_ext_from_gpo(TALLOC_CTX *mem_ctx,
413                                     uint32_t flags,
414                                     struct GROUP_POLICY_OBJECT *gpo,
415                                     struct GP_EXT **gp_ext)
416 {
417         ZERO_STRUCTP(*gp_ext);
418
419         if (flags & GPO_INFO_FLAG_MACHINE) {
420
421                 if (gpo->machine_extensions) {
422
423                         if (!ads_parse_gp_ext(mem_ctx, gpo->machine_extensions,
424                                               gp_ext)) {
425                                 return false;
426                         }
427                 }
428         } else {
429
430                 if (gpo->user_extensions) {
431
432                         if (!ads_parse_gp_ext(mem_ctx, gpo->user_extensions,
433                                               gp_ext)) {
434                                 return false;
435                         }
436                 }
437         }
438
439         return true;
440 }
441
442 /****************************************************************
443 ****************************************************************/
444
445 ADS_STATUS gpo_process_a_gpo(ADS_STRUCT *ads,
446                              TALLOC_CTX *mem_ctx,
447                              const struct security_token *token,
448                              struct registry_key *root_key,
449                              struct GROUP_POLICY_OBJECT *gpo,
450                              const char *extension_guid_filter,
451                              uint32_t flags)
452 {
453         struct GP_EXT *gp_ext = NULL;
454         int i;
455
456         DEBUG(10,("gpo_process_a_gpo: processing gpo %s (%s)\n",
457                 gpo->name, gpo->display_name));
458         if (extension_guid_filter) {
459                 DEBUGADD(10,("gpo_process_a_gpo: using filter %s (%s)\n",
460                         extension_guid_filter,
461                         cse_gpo_guid_string_to_name(extension_guid_filter)));
462         }
463
464         if (!gpo_get_gp_ext_from_gpo(mem_ctx, flags, gpo, &gp_ext)) {
465                 return ADS_ERROR_NT(NT_STATUS_INVALID_PARAMETER);
466         }
467
468         if (!gp_ext || !gp_ext->num_exts) {
469                 if (flags & GPO_INFO_FLAG_VERBOSE) {
470                         DEBUG(0,("gpo_process_a_gpo: "
471                                 "no policies in %s (%s) for this extension\n",
472                                 gpo->name, gpo->display_name));
473                 }
474                 return ADS_SUCCESS;
475         }
476
477         for (i=0; i<gp_ext->num_exts; i++) {
478
479                 NTSTATUS ntstatus;
480
481                 if (extension_guid_filter &&
482                     !strequal(extension_guid_filter,
483                               gp_ext->extensions_guid[i])) {
484                         continue;
485                 }
486
487                 ntstatus = gpext_process_extension(ads, mem_ctx,
488                                                    flags, token, root_key, gpo,
489                                                    gp_ext->extensions_guid[i],
490                                                    gp_ext->snapins_guid[i]);
491                 if (!NT_STATUS_IS_OK(ntstatus)) {
492                         ADS_ERROR_NT(ntstatus);
493                 }
494         }
495
496         return ADS_SUCCESS;
497 }
498
499 /****************************************************************
500 ****************************************************************/
501
502 static ADS_STATUS gpo_process_gpo_list_by_ext(ADS_STRUCT *ads,
503                                               TALLOC_CTX *mem_ctx,
504                                               const struct security_token *token,
505                                               struct registry_key *root_key,
506                                               struct GROUP_POLICY_OBJECT *gpo_list,
507                                               const char *extensions_guid,
508                                               uint32_t flags)
509 {
510         ADS_STATUS status;
511         struct GROUP_POLICY_OBJECT *gpo;
512
513         for (gpo = gpo_list; gpo; gpo = gpo->next) {
514
515                 if (gpo->link_type == GP_LINK_LOCAL) {
516                         continue;
517                 }
518
519
520                 /* FIXME: we need to pass down the *list* down to the
521                  * extension, otherwise we cannot store the e.g. the *list* of
522                  * logon-scripts correctly (for more then one GPO) */
523
524                 status = gpo_process_a_gpo(ads, mem_ctx, token, root_key,
525                                            gpo, extensions_guid, flags);
526
527                 if (!ADS_ERR_OK(status)) {
528                         DEBUG(0,("failed to process gpo by ext: %s\n",
529                                 ads_errstr(status)));
530                         return status;
531                 }
532         }
533
534         return ADS_SUCCESS;
535 }
536
537 /****************************************************************
538 ****************************************************************/
539
540 ADS_STATUS gpo_process_gpo_list(ADS_STRUCT *ads,
541                                 TALLOC_CTX *mem_ctx,
542                                 const struct security_token *token,
543                                 struct GROUP_POLICY_OBJECT *gpo_list,
544                                 const char *extensions_guid_filter,
545                                 uint32_t flags)
546 {
547         ADS_STATUS status = ADS_SUCCESS;
548         struct gp_extension *gp_ext_list = NULL;
549         struct gp_extension *gp_ext = NULL;
550         struct registry_key *root_key = NULL;
551         struct gp_registry_context *reg_ctx = NULL;
552 #if 0
553         WERROR werr;
554 #endif
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 /* FIXME Needs to be replaced with new patchfile_preg calls */
565 #if 0
566         /* get the key here */
567         if (flags & GPO_LIST_FLAG_MACHINE) {
568                 werr = gp_init_reg_ctx(mem_ctx, KEY_HKLM, REG_KEY_WRITE,
569                                        get_system_token(),
570                                        &reg_ctx);
571         } else {
572                 werr = gp_init_reg_ctx(mem_ctx, KEY_HKCU, REG_KEY_WRITE,
573                                        token,
574                                        &reg_ctx);
575         }
576         if (!W_ERROR_IS_OK(werr)) {
577                 talloc_free(reg_ctx);
578                 return ADS_ERROR_NT(werror_to_ntstatus(werr));
579         }
580 #endif
581
582         root_key = reg_ctx->curr_key;
583
584         for (gp_ext = gp_ext_list; gp_ext; gp_ext = gp_ext->next) {
585
586                 const char *guid_str = NULL;
587
588                 guid_str = GUID_string(mem_ctx, gp_ext->guid);
589                 if (!guid_str) {
590                         status = ADS_ERROR_NT(NT_STATUS_NO_MEMORY);
591                         goto done;
592                 }
593
594                 if (extensions_guid_filter &&
595                     (!strequal(guid_str, extensions_guid_filter)))  {
596                         continue;
597                 }
598
599                 DEBUG(0,("-------------------------------------------------\n"));
600                 DEBUG(0,("gpo_process_gpo_list: processing ext: %s {%s}\n",
601                         gp_ext->name, guid_str));
602
603
604                 status = gpo_process_gpo_list_by_ext(ads, mem_ctx, token,
605                                                      root_key, gpo_list,
606                                                      guid_str, flags);
607                 if (!ADS_ERR_OK(status)) {
608                         goto done;
609                 }
610         }
611
612  done:
613         talloc_free(reg_ctx);
614         talloc_free(root_key);
615         free_gp_extensions();
616
617         return status;
618 }
619
620
621 /****************************************************************
622  check wether the version number in a GROUP_POLICY_OBJECT match those of the
623  locally stored version. If not, fetch the required policy via CIFS
624 ****************************************************************/
625
626 NTSTATUS check_refresh_gpo(ADS_STRUCT *ads,
627                            TALLOC_CTX *mem_ctx,
628                            const char *cache_dir,
629                            struct loadparm_context *lp_ctx,
630                            uint32_t flags,
631                            struct GROUP_POLICY_OBJECT *gpo)
632 {
633         NTSTATUS result;
634         char *server = NULL;
635         char *share = NULL;
636         char *nt_path = NULL;
637         char *unix_path = NULL;
638         uint32_t sysvol_gpt_version = 0;
639         char *display_name = NULL;
640
641         result = gpo_explode_filesyspath(mem_ctx, cache_dir, gpo->file_sys_path,
642                                          &server, &share, &nt_path, &unix_path);
643
644         if (!NT_STATUS_IS_OK(result)) {
645                 goto out;
646         }
647
648         result = gpo_get_sysvol_gpt_version(mem_ctx,
649                                             unix_path,
650                                             &sysvol_gpt_version,
651                                             &display_name);
652         if (!NT_STATUS_IS_OK(result) &&
653             !NT_STATUS_EQUAL(result, NT_STATUS_NO_SUCH_FILE)) {
654                 DEBUG(10,("check_refresh_gpo: "
655                         "failed to get local gpt version: %s\n",
656                         nt_errstr(result)));
657                 goto out;
658         }
659
660         DEBUG(10,("check_refresh_gpo: versions gpo %d sysvol %d\n",
661                 gpo->version, sysvol_gpt_version));
662
663         /* FIXME: handle GPO_INFO_FLAG_FORCED_REFRESH from flags */
664
665         while (gpo->version > sysvol_gpt_version) {
666
667                 DEBUG(1,("check_refresh_gpo: need to refresh GPO\n"));
668
669                 result = gpo_fetch_files(mem_ctx, ads, lp_ctx, cache_dir, gpo);
670                 if (!NT_STATUS_IS_OK(result)) {
671                         goto out;
672                 }
673
674                 result = gpo_get_sysvol_gpt_version(mem_ctx,
675                                                     unix_path,
676                                                     &sysvol_gpt_version,
677                                                     &display_name);
678                 if (!NT_STATUS_IS_OK(result)) {
679                         DEBUG(10,("check_refresh_gpo: "
680                                 "failed to get local gpt version: %s\n",
681                                 nt_errstr(result)));
682                         goto out;
683                 }
684
685                 if (gpo->version == sysvol_gpt_version) {
686                         break;
687                 }
688         }
689
690         DEBUG(10,("Name:\t\t\t%s (%s)\n", gpo->display_name, gpo->name));
691         DEBUGADD(10,("sysvol GPT version:\t%d (user: %d, machine: %d)\n",
692                 sysvol_gpt_version,
693                 GPO_VERSION_USER(sysvol_gpt_version),
694                 GPO_VERSION_MACHINE(sysvol_gpt_version)));
695         DEBUGADD(10,("LDAP GPO version:\t%d (user: %d, machine: %d)\n",
696                 gpo->version,
697                 GPO_VERSION_USER(gpo->version),
698                 GPO_VERSION_MACHINE(gpo->version)));
699         DEBUGADD(10,("LDAP GPO link:\t\t%s\n", gpo->link));
700
701         result = NT_STATUS_OK;
702
703  out:
704         return result;
705
706 }
707
708 /****************************************************************
709  check wether the version numbers in the gpo_list match the locally stored, if
710  not, go and get each required GPO via CIFS
711  ****************************************************************/
712
713 NTSTATUS check_refresh_gpo_list(ADS_STRUCT *ads,
714                                 TALLOC_CTX *mem_ctx,
715                                 const char *cache_dir,
716                                 struct loadparm_context *lp_ctx,
717                                 uint32_t flags,
718                                 struct GROUP_POLICY_OBJECT *gpo_list)
719 {
720         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
721         struct GROUP_POLICY_OBJECT *gpo;
722
723         if (!gpo_list) {
724                 return NT_STATUS_INVALID_PARAMETER;
725         }
726
727         for (gpo = gpo_list; gpo; gpo = gpo->next) {
728
729                 result = check_refresh_gpo(ads, mem_ctx, cache_dir, lp_ctx, flags, gpo);
730                 if (!NT_STATUS_IS_OK(result)) {
731                         goto out;
732                 }
733         }
734
735         result = NT_STATUS_OK;
736
737  out:
738         /* FIXME close cli connection */
739
740         return result;
741 }
742
743 /****************************************************************
744 ****************************************************************/
745
746 NTSTATUS gpo_get_unix_path(TALLOC_CTX *mem_ctx,
747                            const char *cache_dir,
748                            struct GROUP_POLICY_OBJECT *gpo,
749                            char **unix_path)
750 {
751         char *server, *share, *nt_path;
752         return gpo_explode_filesyspath(mem_ctx, cache_dir, gpo->file_sys_path,
753                                        &server, &share, &nt_path, unix_path);
754 }
755
756 /****************************************************************
757 ****************************************************************/
758
759 char *gpo_flag_str(TALLOC_CTX *ctx, uint32_t flags)
760 {
761         char *str = NULL;
762
763         if (flags == 0) {
764                 return NULL;
765         }
766
767         if (flags & GPO_INFO_FLAG_SLOWLINK)
768                 str = talloc_append_string(ctx, str, "GPO_INFO_FLAG_SLOWLINK ");
769         if (flags & GPO_INFO_FLAG_VERBOSE)
770                 str = talloc_append_string(ctx, str, "GPO_INFO_FLAG_VERBOSE ");
771         if (flags & GPO_INFO_FLAG_SAFEMODE_BOOT)
772                 str = talloc_append_string(ctx, str, "GPO_INFO_FLAG_SAFEMODE_BOOT ");
773         if (flags & GPO_INFO_FLAG_NOCHANGES)
774                 str = talloc_append_string(ctx, str, "GPO_INFO_FLAG_NOCHANGES ");
775         if (flags & GPO_INFO_FLAG_MACHINE)
776                 str = talloc_append_string(ctx, str, "GPO_INFO_FLAG_MACHINE ");
777         if (flags & GPO_INFO_FLAG_LOGRSOP_TRANSITION)
778                 str = talloc_append_string(ctx, str, "GPO_INFO_FLAG_LOGRSOP_TRANSITION ");
779         if (flags & GPO_INFO_FLAG_LINKTRANSITION)
780                 str = talloc_append_string(ctx, str, "GPO_INFO_FLAG_LINKTRANSITION ");
781         if (flags & GPO_INFO_FLAG_FORCED_REFRESH)
782                 str = talloc_append_string(ctx, str, "GPO_INFO_FLAG_FORCED_REFRESH ");
783         if (flags & GPO_INFO_FLAG_BACKGROUND)
784                 str = talloc_append_string(ctx, str, "GPO_INFO_FLAG_BACKGROUND ");
785
786         return str;
787 }
788
789 /****************************************************************
790 ****************************************************************/
791
792 NTSTATUS gp_find_file(TALLOC_CTX *mem_ctx,
793                       uint32_t flags,
794                       const char *filename,
795                       const char *suffix,
796                       const char **filename_out)
797 {
798         const char *tmp = NULL;
799         struct stat sbuf;
800         const char *path = NULL;
801
802         if (flags & GPO_LIST_FLAG_MACHINE) {
803                 path = "Machine";
804         } else {
805                 path = "User";
806         }
807
808         tmp = talloc_asprintf(mem_ctx, "%s/%s/%s", filename,
809                               path, suffix);
810         NT_STATUS_HAVE_NO_MEMORY(tmp);
811
812         if (stat(tmp, &sbuf) == 0) {
813                 *filename_out = tmp;
814                 return NT_STATUS_OK;
815         }
816
817         path = talloc_strdup_upper(mem_ctx, path);
818         NT_STATUS_HAVE_NO_MEMORY(path);
819
820         tmp = talloc_asprintf(mem_ctx, "%s/%s/%s", filename,
821                               path, suffix);
822         NT_STATUS_HAVE_NO_MEMORY(tmp);
823
824         if (stat(tmp, &sbuf) == 0) {
825                 *filename_out = tmp;
826                 return NT_STATUS_OK;
827         }
828
829         return NT_STATUS_NO_SUCH_FILE;
830 }
831
832 /****************************************************************
833 ****************************************************************/
834
835 ADS_STATUS gp_get_machine_token(ADS_STRUCT *ads,
836                                 TALLOC_CTX *mem_ctx,
837                                 struct loadparm_context *lp_ctx,
838                                 const char *dn,
839                                 struct security_token **token)
840 {
841         struct security_token *ad_token = NULL;
842         ADS_STATUS status;
843         NTSTATUS ntstatus;
844
845 #ifndef HAVE_ADS
846         return ADS_ERROR_NT(NT_STATUS_NOT_SUPPORTED);
847 #endif
848         status = ads_get_sid_token(ads, mem_ctx, dn, &ad_token);
849         if (!ADS_ERR_OK(status)) {
850                 return status;
851         }
852         ntstatus = merge_nt_token(mem_ctx, ad_token, get_system_token(),
853                                   token);
854         if (!NT_STATUS_IS_OK(ntstatus)) {
855                 return ADS_ERROR_NT(ntstatus);
856         }
857         return ADS_SUCCESS;
858 }