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