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