3f90fa4a6211af3690355d8e0df3aefa12cd4661
[samba.git] / source3 / libgpo / gpo_ldap.c
1 /* 
2  *  Unix SMB/CIFS implementation.
3  *  Group Policy Object Support
4  *  Copyright (C) Guenther Deschner 2005
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 2 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, write to the Free Software
18  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20
21 #include "includes.h"
22
23 #ifdef HAVE_LDAP
24
25 /****************************************************************
26  parse the raw extension string into a GP_EXT structure
27 ****************************************************************/
28
29 ADS_STATUS ads_parse_gp_ext(TALLOC_CTX *mem_ctx,
30                             const char *extension_raw,
31                             struct GP_EXT **gp_ext)
32 {
33         struct GP_EXT *ext = NULL;
34         char **ext_list;
35         char **ext_strings = NULL;
36         int i;
37
38         if (!extension_raw) {
39                 goto parse_error;
40         }
41
42         DEBUG(20,("ads_parse_gp_ext: %s\n", extension_raw));
43
44         ext = TALLOC_ZERO_P(mem_ctx, struct GP_EXT);
45         if (!ext) {
46                 goto parse_error;
47         }
48
49         ext_list = str_list_make_talloc(mem_ctx, extension_raw, "]");
50         if (ext_list == NULL) {
51                 goto parse_error;
52         }
53
54         for (i = 0; ext_list[i] != NULL; i++) {
55                 /* no op */
56         }
57
58         ext->num_exts = i;
59         
60         if (ext->num_exts) {
61                 ext->extensions = TALLOC_ZERO_ARRAY(mem_ctx, char *, ext->num_exts);
62                 ext->extensions_guid = TALLOC_ZERO_ARRAY(mem_ctx, char *, ext->num_exts);
63                 ext->snapins = TALLOC_ZERO_ARRAY(mem_ctx, char *, ext->num_exts);
64                 ext->snapins_guid = TALLOC_ZERO_ARRAY(mem_ctx, char *, ext->num_exts);
65         } else {
66                 ext->extensions = NULL;
67                 ext->extensions_guid = NULL;
68                 ext->snapins = NULL;
69                 ext->snapins_guid = NULL;
70         }
71
72         ext->gp_extension = talloc_strdup(mem_ctx, extension_raw);
73
74         if (ext->extensions == NULL || ext->extensions_guid == NULL || 
75             ext->snapins == NULL || ext->snapins_guid == NULL || 
76             ext->gp_extension == NULL) {
77                 goto parse_error;
78         }
79
80         for (i = 0; ext_list[i] != NULL; i++) {
81
82                 int k;
83                 char *p, *q;
84                 
85                 DEBUGADD(10,("extension #%d\n", i));
86
87                 p = ext_list[i];
88
89                 if (p[0] == '[') {
90                         p++;
91                 }
92
93                 ext_strings = str_list_make_talloc(mem_ctx, p, "}");
94                 if (ext_strings == NULL) {
95                         goto parse_error;
96                 }
97
98                 for (k = 0; ext_strings[k] != NULL; k++) {
99                         /* no op */
100                 }
101
102                 q = ext_strings[0];
103
104                 if (q[0] == '{') {
105                         q++;
106                 }
107
108                 ext->extensions[i] = talloc_strdup(mem_ctx, cse_gpo_guid_string_to_name(q));
109                 ext->extensions_guid[i] = talloc_strdup(mem_ctx, q);
110
111                 /* we might have no name for the guid */
112                 if (ext->extensions_guid[i] == NULL) {
113                         goto parse_error;
114                 }
115                 
116                 for (k = 1; ext_strings[k] != NULL; k++) {
117
118                         char *m = ext_strings[k];
119
120                         if (m[0] == '{') {
121                                 m++;
122                         }
123
124                         /* FIXME: theoretically there could be more than one snapin per extension */
125                         ext->snapins[i] = talloc_strdup(mem_ctx, cse_snapin_gpo_guid_string_to_name(m));
126                         ext->snapins_guid[i] = talloc_strdup(mem_ctx, m);
127
128                         /* we might have no name for the guid */
129                         if (ext->snapins_guid[i] == NULL) {
130                                 goto parse_error;
131                         }
132                 }
133         }
134
135         if (ext_list) {
136                 str_list_free_talloc(mem_ctx, &ext_list); 
137         }
138         if (ext_strings) {
139                 str_list_free_talloc(mem_ctx, &ext_strings); 
140         }
141
142         *gp_ext = ext;
143
144         return ADS_ERROR(LDAP_SUCCESS);
145
146 parse_error:
147         if (ext_list) {
148                 str_list_free_talloc(mem_ctx, &ext_list); 
149         }
150         if (ext_strings) {
151                 str_list_free_talloc(mem_ctx, &ext_strings); 
152         }
153
154         return ADS_ERROR(LDAP_NO_MEMORY);
155 }
156
157 /****************************************************************
158  parse the raw link string into a GP_LINK structure
159 ****************************************************************/
160
161 static ADS_STATUS gpo_parse_gplink(TALLOC_CTX *mem_ctx, 
162                                    const char *gp_link_raw,
163                                    uint32 options,
164                                    struct GP_LINK *gp_link)
165 {
166         char **link_list;
167         int i;
168         
169         DEBUG(10,("gpo_parse_gplink: gPLink: %s\n", gp_link_raw));
170
171         link_list = str_list_make_talloc(mem_ctx, gp_link_raw, "]");
172         if (link_list == NULL) {
173                 goto parse_error;
174         }
175
176         for (i = 0; link_list[i] != NULL; i++) {
177                 /* no op */
178         }
179
180         gp_link->gp_opts = options;
181         gp_link->num_links = i;
182         
183         if (gp_link->num_links) {
184                 gp_link->link_names = TALLOC_ZERO_ARRAY(mem_ctx, char *, gp_link->num_links);
185                 gp_link->link_opts = TALLOC_ZERO_ARRAY(mem_ctx, uint32, gp_link->num_links);
186         } else {
187                 gp_link->link_names = NULL;
188                 gp_link->link_opts = NULL;
189         }
190         
191         gp_link->gp_link = talloc_strdup(mem_ctx, gp_link_raw);
192
193         if (gp_link->link_names == NULL || gp_link->link_opts == NULL || gp_link->gp_link == NULL) {
194                 goto parse_error;
195         }
196
197         for (i = 0; link_list[i] != NULL; i++) {
198
199                 char *p, *q;
200
201                 DEBUGADD(10,("gpo_parse_gplink: processing link #%d\n", i));
202
203                 q = link_list[i];
204                 if (q[0] == '[') {
205                         q++;
206                 };
207
208                 p = strchr(q, ';');
209                 
210                 if (p == NULL) {
211                         goto parse_error;
212                 }
213
214                 gp_link->link_names[i] = talloc_strdup(mem_ctx, q);
215                 if (gp_link->link_names[i] == NULL) {
216                         goto parse_error;
217                 }
218                 gp_link->link_names[i][PTR_DIFF(p, q)] = 0;
219
220                 gp_link->link_opts[i] = atoi(p + 1);
221
222                 DEBUGADD(10,("gpo_parse_gplink: link: %s\n", gp_link->link_names[i]));
223                 DEBUGADD(10,("gpo_parse_gplink: opt: %d\n", gp_link->link_opts[i]));
224
225         }
226
227         if (link_list) {
228                 str_list_free_talloc(mem_ctx, &link_list);
229         }
230
231         return ADS_ERROR(LDAP_SUCCESS);
232
233 parse_error:
234         if (link_list) {
235                 str_list_free_talloc(mem_ctx, &link_list);
236         }
237
238         return ADS_ERROR(LDAP_NO_MEMORY);
239 }
240
241 /****************************************************************
242  helper call to get a GP_LINK structure from a linkdn
243 ****************************************************************/
244
245 ADS_STATUS ads_get_gpo_link(ADS_STRUCT *ads,
246                             TALLOC_CTX *mem_ctx,
247                             const char *link_dn,
248                             struct GP_LINK *gp_link_struct)
249 {
250         ADS_STATUS status;
251         const char *attrs[] = {"gPLink", "gPOptions", NULL};
252         LDAPMessage *res = NULL;
253         const char *gp_link;
254         uint32 gp_options;
255
256         ZERO_STRUCTP(gp_link_struct);
257
258         status = ads_search_dn(ads, &res, link_dn, attrs);
259         if (!ADS_ERR_OK(status)) {
260                 DEBUG(10,("ads_get_gpo_link: search failed with %s\n", ads_errstr(status)));
261                 return status;
262         }
263
264         if (ads_count_replies(ads, res) != 1) {
265                 DEBUG(10,("ads_get_gpo_link: no result\n"));
266                 ads_msgfree(ads, res);
267                 return ADS_ERROR(LDAP_NO_SUCH_OBJECT);
268         }
269
270         gp_link = ads_pull_string(ads, mem_ctx, res, "gPLink"); 
271         if (gp_link == NULL) {
272                 DEBUG(10,("ads_get_gpo_link: no 'gPLink' attribute found\n"));
273                 ads_msgfree(ads, res);
274                 return ADS_ERROR(LDAP_NO_SUCH_ATTRIBUTE);       
275         }
276
277         /* perfectly legal to have no options */
278         if (!ads_pull_uint32(ads, res, "gPOptions", &gp_options)) {
279                 DEBUG(10,("ads_get_gpo_link: no 'gPOptions' attribute found\n"));
280                 gp_options = 0;
281         }
282
283         ads_msgfree(ads, res);
284
285         return gpo_parse_gplink(mem_ctx, gp_link, gp_options, gp_link_struct); 
286 }
287
288 /****************************************************************
289  helper call to add a gp link
290 ****************************************************************/
291
292 ADS_STATUS ads_add_gpo_link(ADS_STRUCT *ads, 
293                             TALLOC_CTX *mem_ctx, 
294                             const char *link_dn, 
295                             const char *gpo_dn, 
296                             uint32 gpo_opt)
297 {
298         ADS_STATUS status;
299         const char *attrs[] = {"gPLink", NULL};
300         LDAPMessage *res = NULL;
301         const char *gp_link, *gp_link_new;
302         ADS_MODLIST mods;
303
304         /* although ADS allows to set anything here, we better check here if
305          * the gpo_dn is sane */
306
307         if (!strnequal(gpo_dn, "LDAP://CN={", strlen("LDAP://CN={")) != 0) {
308                 return ADS_ERROR(LDAP_INVALID_DN_SYNTAX);       
309         }
310
311         status = ads_search_dn(ads, &res, link_dn, attrs);
312         if (!ADS_ERR_OK(status)) {
313                 DEBUG(10,("ads_add_gpo_link: search failed with %s\n", ads_errstr(status)));
314                 return status;
315         }
316
317         if (ads_count_replies(ads, res) != 1) {
318                 DEBUG(10,("ads_add_gpo_link: no result\n"));
319                 ads_msgfree(ads, res);
320                 return ADS_ERROR(LDAP_NO_SUCH_OBJECT);
321         }
322
323         gp_link = ads_pull_string(ads, mem_ctx, res, "gPLink"); 
324         if (gp_link == NULL) {
325                 gp_link_new = talloc_asprintf(mem_ctx, "[%s;%d]", gpo_dn, gpo_opt);
326         } else {
327                 gp_link_new = talloc_asprintf(mem_ctx, "%s[%s;%d]", gp_link, gpo_dn, gpo_opt);
328         }
329
330         ads_msgfree(ads, res);
331         ADS_ERROR_HAVE_NO_MEMORY(gp_link_new);
332
333         mods = ads_init_mods(mem_ctx);
334         ADS_ERROR_HAVE_NO_MEMORY(mods);
335
336         status = ads_mod_str(mem_ctx, &mods, "gPLink", gp_link_new);
337         if (!ADS_ERR_OK(status)) {
338                 return status;
339         }
340
341         return ads_gen_mod(ads, link_dn, mods); 
342 }
343
344 /****************************************************************
345  helper call to delete add a gp link
346 ****************************************************************/
347
348 /* untested & broken */
349 ADS_STATUS ads_delete_gpo_link(ADS_STRUCT *ads, 
350                                TALLOC_CTX *mem_ctx, 
351                                const char *link_dn, 
352                                const char *gpo_dn)
353 {
354         ADS_STATUS status;
355         const char *attrs[] = {"gPLink", NULL};
356         LDAPMessage *res = NULL;
357         const char *gp_link, *gp_link_new = NULL;
358         ADS_MODLIST mods;
359
360         /* check for a sane gpo_dn */
361         if (gpo_dn[0] != '[') {
362                 DEBUG(10,("ads_delete_gpo_link: first char not: [\n"));
363                 return ADS_ERROR(LDAP_INVALID_DN_SYNTAX);
364         }
365                 
366         if (gpo_dn[strlen(gpo_dn)] != ']') {
367                 DEBUG(10,("ads_delete_gpo_link: last char not: ]\n"));
368                 return ADS_ERROR(LDAP_INVALID_DN_SYNTAX);
369         }
370
371         status = ads_search_dn(ads, &res, link_dn, attrs);
372         if (!ADS_ERR_OK(status)) {
373                 DEBUG(10,("ads_delete_gpo_link: search failed with %s\n", ads_errstr(status)));
374                 return status;
375         }
376
377         if (ads_count_replies(ads, res) != 1) {
378                 DEBUG(10,("ads_delete_gpo_link: no result\n"));
379                 ads_msgfree(ads, res);
380                 return ADS_ERROR(LDAP_NO_SUCH_OBJECT);
381         }
382
383         gp_link = ads_pull_string(ads, mem_ctx, res, "gPLink"); 
384         if (gp_link == NULL) {
385                 return ADS_ERROR(LDAP_NO_SUCH_ATTRIBUTE);
386         }
387
388         /* find link to delete */
389         /* gp_link_new = talloc_asprintf(mem_ctx, "%s[%s;%d]", gp_link, gpo_dn, gpo_opt); */
390
391         ads_msgfree(ads, res);
392         ADS_ERROR_HAVE_NO_MEMORY(gp_link_new);
393
394         mods = ads_init_mods(mem_ctx);
395         ADS_ERROR_HAVE_NO_MEMORY(mods);
396
397         status = ads_mod_str(mem_ctx, &mods, "gPLink", gp_link_new);
398         if (!ADS_ERR_OK(status)) {
399                 return status;
400         }
401
402         return ads_gen_mod(ads, link_dn, mods); 
403 }
404
405 /****************************************************************
406  parse a GROUP_POLICY_OBJECT structure from an LDAPMessage result
407 ****************************************************************/
408
409  ADS_STATUS ads_parse_gpo(ADS_STRUCT *ads,
410                           TALLOC_CTX *mem_ctx,
411                           LDAPMessage *res,
412                           const char *gpo_dn,
413                           struct GROUP_POLICY_OBJECT *gpo)
414 {
415         ZERO_STRUCTP(gpo);
416
417         ADS_ERROR_HAVE_NO_MEMORY(res);
418
419         if (gpo_dn) {
420                 gpo->ds_path = talloc_strdup(mem_ctx, gpo_dn);
421         } else {
422                 gpo->ds_path = ads_get_dn(ads, res);
423         }
424
425         ADS_ERROR_HAVE_NO_MEMORY(gpo->ds_path);
426
427         if (!ads_pull_uint32(ads, res, "versionNumber", &gpo->version)) {
428                 return ADS_ERROR(LDAP_NO_MEMORY);
429         }
430
431         /* sure ??? */
432         if (!ads_pull_uint32(ads, res, "flags", &gpo->options)) {
433                 return ADS_ERROR(LDAP_NO_MEMORY);
434         }
435
436         gpo->file_sys_path = ads_pull_string(ads, mem_ctx, res, "gPCFileSysPath");
437         ADS_ERROR_HAVE_NO_MEMORY(gpo->file_sys_path);
438
439         gpo->display_name = ads_pull_string(ads, mem_ctx, res, "displayName");
440         ADS_ERROR_HAVE_NO_MEMORY(gpo->display_name);
441
442         gpo->name = ads_pull_string(ads, mem_ctx, res, "name");
443         ADS_ERROR_HAVE_NO_MEMORY(gpo->name);
444
445         /* ???, this is optional to have and what does it depend on, the 'flags' ?) */
446         gpo->machine_extensions = ads_pull_string(ads, mem_ctx, res, "gPCMachineExtensionNames");
447         gpo->user_extensions = ads_pull_string(ads, mem_ctx, res, "gPCUserExtensionNames");
448
449         ads_pull_sd(ads, mem_ctx, res, "ntSecurityDescriptor", &gpo->security_descriptor);
450         ADS_ERROR_HAVE_NO_MEMORY(gpo->security_descriptor);
451
452         return ADS_ERROR(LDAP_SUCCESS);
453 }
454
455 /****************************************************************
456  get a GROUP_POLICY_OBJECT structure based on different input paramters
457 ****************************************************************/
458
459 ADS_STATUS ads_get_gpo(ADS_STRUCT *ads,
460                        TALLOC_CTX *mem_ctx,
461                        const char *gpo_dn,
462                        const char *display_name,
463                        const char *guid_name,
464                        struct GROUP_POLICY_OBJECT *gpo)
465 {
466         ADS_STATUS status;
467         LDAPMessage *res = NULL;
468         char *dn;
469         const char *filter;
470         const char *attrs[] = { "cn", "displayName", "flags", "gPCFileSysPath", 
471                                 "gPCFunctionalityVersion", "gPCMachineExtensionNames", 
472                                 "gPCUserExtensionNames", "gPCWQLFilter", "name", 
473                                 "versionNumber", "ntSecurityDescriptor", NULL};
474         uint32 sd_flags = DACL_SECURITY_INFORMATION;
475
476         ZERO_STRUCTP(gpo);
477
478         if (!gpo_dn && !display_name && !guid_name) {
479                 return ADS_ERROR(LDAP_NO_SUCH_OBJECT);
480         }
481
482         if (gpo_dn) {
483         
484                 if (strnequal(gpo_dn, "LDAP://", strlen("LDAP://")) != 0) {
485                         gpo_dn = gpo_dn + strlen("LDAP://");
486                 }
487
488                 status = ads_search_retry_dn_sd_flags(ads, &res, 
489                                                       sd_flags,
490                                                       gpo_dn, attrs);
491                 
492         } else if (display_name || guid_name) {
493
494                 filter = talloc_asprintf(mem_ctx, 
495                                          "(&(objectclass=groupPolicyContainer)(%s=%s))", 
496                                          display_name ? "displayName" : "name",
497                                          display_name ? display_name : guid_name);
498                 ADS_ERROR_HAVE_NO_MEMORY(filter);
499
500                 status = ads_do_search_all_sd_flags(ads, ads->config.bind_path,
501                                                     LDAP_SCOPE_SUBTREE, filter, 
502                                                     attrs, sd_flags, &res);
503         }
504
505         if (!ADS_ERR_OK(status)) {
506                 DEBUG(10,("ads_get_gpo: search failed with %s\n", ads_errstr(status)));
507                 return status;
508         }
509
510         if (ads_count_replies(ads, res) != 1) {
511                 DEBUG(10,("ads_get_gpo: no result\n"));
512                 ads_msgfree(ads, res);
513                 return ADS_ERROR(LDAP_NO_SUCH_OBJECT);
514         }
515
516         dn = ads_get_dn(ads, res);
517         if (dn == NULL) {
518                 ads_msgfree(ads, res);
519                 return ADS_ERROR(LDAP_NO_MEMORY);
520         }
521         
522         status = ads_parse_gpo(ads, mem_ctx, res, dn, gpo);
523         ads_msgfree(ads, res);
524         ads_memfree(ads, dn);
525
526         return status;
527 }
528
529 /****************************************************************
530  add a gplink to the GROUP_POLICY_OBJECT linked list
531 ****************************************************************/
532
533 static ADS_STATUS add_gplink_to_gpo_list(ADS_STRUCT *ads,
534                                          TALLOC_CTX *mem_ctx, 
535                                          struct GROUP_POLICY_OBJECT **gpo_list,
536                                          const char *link_dn,
537                                          struct GP_LINK *gp_link,
538                                          enum GPO_LINK_TYPE link_type,
539                                          BOOL only_add_forced_gpos,
540                                          struct GPO_SID_TOKEN *token)
541 {
542         ADS_STATUS status;
543         int i;
544
545         for (i = 0; i < gp_link->num_links; i++) {
546
547                 struct GROUP_POLICY_OBJECT *new_gpo = NULL;
548
549                 if (gp_link->link_opts[i] & GPO_LINK_OPT_DISABLED) {
550                         DEBUG(10,("skipping disabled GPO\n"));
551                         continue;
552                 }
553
554                 if (only_add_forced_gpos) {
555                 
556                         if (! (gp_link->link_opts[i] & GPO_LINK_OPT_ENFORCED)) {
557                                 DEBUG(10,("skipping nonenforced GPO link because GPOPTIONS_BLOCK_INHERITANCE has been set\n"));
558                                 continue;
559                         } else {
560                                 DEBUG(10,("adding enforced GPO link although the GPOPTIONS_BLOCK_INHERITANCE has been set\n"));
561                         }
562                 }
563
564                 new_gpo = TALLOC_ZERO_P(mem_ctx, struct GROUP_POLICY_OBJECT);
565                 ADS_ERROR_HAVE_NO_MEMORY(new_gpo);
566
567                 status = ads_get_gpo(ads, mem_ctx, gp_link->link_names[i], NULL, NULL, new_gpo);
568                 if (!ADS_ERR_OK(status)) {
569                         DEBUG(10,("failed to get gpo: %s\n", gp_link->link_names[i]));
570                         return status;
571                 }
572
573                 status = ADS_ERROR_NT(gpo_apply_security_filtering(new_gpo, token));
574                 if (!ADS_ERR_OK(status)) {
575                         DEBUG(10,("skipping GPO \"%s\" as object has no access to it\n", 
576                                 new_gpo->display_name));
577                         TALLOC_FREE(new_gpo);
578                         continue;
579                 }
580
581                 new_gpo->link = link_dn;
582                 new_gpo->link_type = link_type; 
583
584                 DLIST_ADD(*gpo_list, new_gpo);
585
586                 DEBUG(10,("add_gplink_to_gplist: added GPLINK #%d %s to GPO list\n", 
587                         i, gp_link->link_names[i]));
588         }
589
590         return ADS_ERROR(LDAP_SUCCESS);
591 }
592
593 /****************************************************************
594 ****************************************************************/
595
596 static ADS_STATUS ads_get_gpo_sid_token(ADS_STRUCT *ads,
597                                         TALLOC_CTX *mem_ctx,
598                                         const char *dn,
599                                         struct GPO_SID_TOKEN **token)
600 {
601         ADS_STATUS status;
602         DOM_SID object_sid;
603         DOM_SID primary_group_sid;
604         DOM_SID *ad_token_sids;
605         size_t num_ad_token_sids = 0;
606         DOM_SID *token_sids;
607         size_t num_token_sids = 0;
608         struct GPO_SID_TOKEN *new_token = NULL;
609         int i;
610
611         new_token = TALLOC_ZERO_P(mem_ctx, struct GPO_SID_TOKEN);
612         ADS_ERROR_HAVE_NO_MEMORY(new_token);
613
614         status = ads_get_tokensids(ads, mem_ctx, dn, 
615                                    &object_sid, &primary_group_sid,
616                                    &ad_token_sids, &num_ad_token_sids);
617         if (!ADS_ERR_OK(status)) {
618                 return status;
619         }
620
621         new_token->object_sid = object_sid;
622         new_token->primary_group_sid = primary_group_sid;
623
624         token_sids = TALLOC_ARRAY(mem_ctx, DOM_SID, 1);
625         ADS_ERROR_HAVE_NO_MEMORY(token_sids);
626
627         for (i = 0; i < num_ad_token_sids; i++) {
628                 
629                 if (sid_check_is_in_builtin(&ad_token_sids[i])) {
630                         continue;
631                 }
632
633                 if (!add_sid_to_array_unique(mem_ctx, &ad_token_sids[i], 
634                                              &token_sids, &num_token_sids)) {
635                         return ADS_ERROR(LDAP_NO_MEMORY);
636                 }
637         }
638
639         /* Add S-1-5-11 to token */
640         if (!add_sid_to_array_unique(mem_ctx, &global_sid_Authenticated_Users,
641                                      &token_sids, &num_token_sids)) {
642                 return ADS_ERROR(LDAP_NO_MEMORY);
643         }
644
645
646         new_token->token_sids = token_sids;
647         new_token->num_token_sids = num_token_sids;
648
649         *token = new_token;
650
651         return ADS_ERROR_LDAP(LDAP_SUCCESS);
652 }
653
654
655 /****************************************************************
656  get the full list of GROUP_POLICY_OBJECTs for a given dn
657 ****************************************************************/
658
659 ADS_STATUS ads_get_gpo_list(ADS_STRUCT *ads, 
660                             TALLOC_CTX *mem_ctx, 
661                             const char *dn,
662                             uint32 flags,
663                             struct GROUP_POLICY_OBJECT **gpo_list)
664 {
665         /* (L)ocal (S)ite (D)omain (O)rganizational(U)nit */
666         
667         ADS_STATUS status;
668         struct GP_LINK gp_link;
669         struct GPO_SID_TOKEN *token = NULL;
670         const char *parent_dn, *site_dn, *tmp_dn;
671         BOOL add_only_forced_gpos = False;
672
673         ZERO_STRUCTP(gpo_list);
674
675         DEBUG(10,("ads_get_gpo_list: getting GPO list for [%s]\n", dn));
676
677         status = ads_get_gpo_sid_token(ads, mem_ctx, dn, &token);
678         if (!ADS_ERR_OK(status)) {
679                 return status;
680         }
681
682         /* (L)ocal */
683         /* not yet... */
684         
685         /* (S)ite */
686
687         /* are site GPOs valid for users as well ??? */
688         if (flags & GPO_LIST_FLAG_MACHINE) {
689
690                 status = ads_site_dn_for_machine(ads, mem_ctx, ads->config.ldap_server_name, &site_dn);
691                 if (!ADS_ERR_OK(status)) {
692                         return status;
693                 }
694
695                 DEBUG(10,("ads_get_gpo_list: query SITE: [%s] for GPOs\n", site_dn));
696
697                 status = ads_get_gpo_link(ads, mem_ctx, site_dn, &gp_link);
698                 if (ADS_ERR_OK(status)) {
699
700                         if (DEBUGLEVEL >= 100) {
701                                 dump_gplink(ads, mem_ctx, &gp_link);
702                         }
703
704                         status = add_gplink_to_gpo_list(ads, mem_ctx, gpo_list, 
705                                                         site_dn, &gp_link, GP_LINK_SITE, 
706                                                         add_only_forced_gpos,
707                                                         token);
708                         if (!ADS_ERR_OK(status)) {
709                                 return status;
710                         }
711
712                         if (flags & GPO_LIST_FLAG_SITEONLY) {
713                                 return ADS_ERROR(LDAP_SUCCESS);
714                         }
715
716                         /* inheritance can't be blocked at the site level */
717                 }
718         }
719
720         tmp_dn = dn;
721
722         while ( (parent_dn = ads_parent_dn(tmp_dn)) && 
723                 (!strequal(parent_dn, ads_parent_dn(ads->config.bind_path))) ) {
724
725                 /* (D)omain */
726
727                 /* An account can just be a member of one domain */
728                 if (strncmp(parent_dn, "DC=", strlen("DC=")) == 0) {
729
730                         DEBUG(10,("ads_get_gpo_list: query DC: [%s] for GPOs\n", parent_dn));
731
732                         status = ads_get_gpo_link(ads, mem_ctx, parent_dn, &gp_link);
733                         if (ADS_ERR_OK(status)) {
734                                 
735                                 if (DEBUGLEVEL >= 100) {
736                                         dump_gplink(ads, mem_ctx, &gp_link);
737                                 }
738
739                                 /* block inheritance from now on */
740                                 if (gp_link.gp_opts & GPOPTIONS_BLOCK_INHERITANCE) {
741                                         add_only_forced_gpos = True;
742                                 }
743
744                                 status = add_gplink_to_gpo_list(ads, mem_ctx, 
745                                                                 gpo_list, parent_dn, 
746                                                                 &gp_link, GP_LINK_DOMAIN, 
747                                                                 add_only_forced_gpos,
748                                                                 token);
749                                 if (!ADS_ERR_OK(status)) {
750                                         return status;
751                                 }
752                         }
753                 }
754
755                 tmp_dn = parent_dn;
756         }
757
758         /* reset dn again */
759         tmp_dn = dn;
760         
761         while ( (parent_dn = ads_parent_dn(tmp_dn)) && 
762                 (!strequal(parent_dn, ads_parent_dn(ads->config.bind_path))) ) {
763
764
765                 /* (O)rganizational(U)nit */
766
767                 /* An account can be a member of more OUs */
768                 if (strncmp(parent_dn, "OU=", strlen("OU=")) == 0) {
769                 
770                         DEBUG(10,("ads_get_gpo_list: query OU: [%s] for GPOs\n", parent_dn));
771
772                         status = ads_get_gpo_link(ads, mem_ctx, parent_dn, &gp_link);
773                         if (ADS_ERR_OK(status)) {
774
775                                 if (DEBUGLEVEL >= 100) {
776                                         dump_gplink(ads, mem_ctx, &gp_link);
777                                 }
778
779                                 /* block inheritance from now on */
780                                 if (gp_link.gp_opts & GPOPTIONS_BLOCK_INHERITANCE) {
781                                         add_only_forced_gpos = True;
782                                 }
783
784                                 status = add_gplink_to_gpo_list(ads, mem_ctx, 
785                                                                 gpo_list, parent_dn, 
786                                                                 &gp_link, GP_LINK_OU, 
787                                                                 add_only_forced_gpos,
788                                                                 token);
789                                 if (!ADS_ERR_OK(status)) {
790                                         return status;
791                                 }
792                         }
793                 }
794
795                 tmp_dn = parent_dn;
796
797         };
798
799         return ADS_ERROR(LDAP_SUCCESS);
800 }
801
802 #endif /* HAVE_LDAP */