2 * Unix SMB/CIFS implementation.
3 * Group Policy Object Support
4 * Copyright (C) Guenther Deschner 2005
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.
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.
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.
25 /****************************************************************
26 parse the raw extension string into a GP_EXT structure
27 ****************************************************************/
29 ADS_STATUS ads_parse_gp_ext(TALLOC_CTX *mem_ctx,
30 const char *extension_raw,
31 struct GP_EXT **gp_ext)
33 struct GP_EXT *ext = NULL;
35 char **ext_strings = NULL;
42 DEBUG(20,("ads_parse_gp_ext: %s\n", extension_raw));
44 ext = TALLOC_ZERO_P(mem_ctx, struct GP_EXT);
49 ext_list = str_list_make_talloc(mem_ctx, extension_raw, "]");
50 if (ext_list == NULL) {
54 for (i = 0; ext_list[i] != NULL; i++) {
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);
66 ext->extensions = NULL;
67 ext->extensions_guid = NULL;
69 ext->snapins_guid = NULL;
72 ext->gp_extension = talloc_strdup(mem_ctx, extension_raw);
74 if (ext->extensions == NULL || ext->extensions_guid == NULL ||
75 ext->snapins == NULL || ext->snapins_guid == NULL ||
76 ext->gp_extension == NULL) {
80 for (i = 0; ext_list[i] != NULL; i++) {
85 DEBUGADD(10,("extension #%d\n", i));
93 ext_strings = str_list_make_talloc(mem_ctx, p, "}");
94 if (ext_strings == NULL) {
98 for (k = 0; ext_strings[k] != NULL; k++) {
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);
111 /* we might have no name for the guid */
112 if (ext->extensions_guid[i] == NULL) {
116 for (k = 1; ext_strings[k] != NULL; k++) {
118 char *m = ext_strings[k];
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);
128 /* we might have no name for the guid */
129 if (ext->snapins_guid[i] == NULL) {
136 str_list_free_talloc(mem_ctx, &ext_list);
139 str_list_free_talloc(mem_ctx, &ext_strings);
144 return ADS_ERROR(LDAP_SUCCESS);
148 str_list_free_talloc(mem_ctx, &ext_list);
151 str_list_free_talloc(mem_ctx, &ext_strings);
154 return ADS_ERROR(LDAP_NO_MEMORY);
157 /****************************************************************
158 parse the raw link string into a GP_LINK structure
159 ****************************************************************/
161 static ADS_STATUS gpo_parse_gplink(TALLOC_CTX *mem_ctx,
162 const char *gp_link_raw,
164 struct GP_LINK *gp_link)
169 DEBUG(10,("gpo_parse_gplink: gPLink: %s\n", gp_link_raw));
171 link_list = str_list_make_talloc(mem_ctx, gp_link_raw, "]");
172 if (link_list == NULL) {
176 for (i = 0; link_list[i] != NULL; i++) {
180 gp_link->gp_opts = options;
181 gp_link->num_links = i;
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);
187 gp_link->link_names = NULL;
188 gp_link->link_opts = NULL;
191 gp_link->gp_link = talloc_strdup(mem_ctx, gp_link_raw);
193 if (gp_link->link_names == NULL || gp_link->link_opts == NULL || gp_link->gp_link == NULL) {
197 for (i = 0; link_list[i] != NULL; i++) {
201 DEBUGADD(10,("gpo_parse_gplink: processing link #%d\n", i));
214 gp_link->link_names[i] = talloc_strdup(mem_ctx, q);
215 if (gp_link->link_names[i] == NULL) {
218 gp_link->link_names[i][PTR_DIFF(p, q)] = 0;
220 gp_link->link_opts[i] = atoi(p + 1);
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]));
228 str_list_free_talloc(mem_ctx, &link_list);
231 return ADS_ERROR(LDAP_SUCCESS);
235 str_list_free_talloc(mem_ctx, &link_list);
238 return ADS_ERROR(LDAP_NO_MEMORY);
241 /****************************************************************
242 helper call to get a GP_LINK structure from a linkdn
243 ****************************************************************/
245 ADS_STATUS ads_get_gpo_link(ADS_STRUCT *ads,
248 struct GP_LINK *gp_link_struct)
251 const char *attrs[] = {"gPLink", "gPOptions", NULL};
252 LDAPMessage *res = NULL;
256 ZERO_STRUCTP(gp_link_struct);
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)));
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);
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);
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"));
283 ads_msgfree(ads, res);
285 return gpo_parse_gplink(mem_ctx, gp_link, gp_options, gp_link_struct);
288 /****************************************************************
289 helper call to add a gp link
290 ****************************************************************/
292 ADS_STATUS ads_add_gpo_link(ADS_STRUCT *ads,
299 const char *attrs[] = {"gPLink", NULL};
300 LDAPMessage *res = NULL;
301 const char *gp_link, *gp_link_new;
304 /* although ADS allows to set anything here, we better check here if
305 * the gpo_dn is sane */
307 if (!strnequal(gpo_dn, "LDAP://CN={", strlen("LDAP://CN={")) != 0) {
308 return ADS_ERROR(LDAP_INVALID_DN_SYNTAX);
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)));
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);
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);
327 gp_link_new = talloc_asprintf(mem_ctx, "%s[%s;%d]", gp_link, gpo_dn, gpo_opt);
330 ads_msgfree(ads, res);
331 ADS_ERROR_HAVE_NO_MEMORY(gp_link_new);
333 mods = ads_init_mods(mem_ctx);
334 ADS_ERROR_HAVE_NO_MEMORY(mods);
336 status = ads_mod_str(mem_ctx, &mods, "gPLink", gp_link_new);
337 if (!ADS_ERR_OK(status)) {
341 return ads_gen_mod(ads, link_dn, mods);
344 /****************************************************************
345 helper call to delete add a gp link
346 ****************************************************************/
348 /* untested & broken */
349 ADS_STATUS ads_delete_gpo_link(ADS_STRUCT *ads,
355 const char *attrs[] = {"gPLink", NULL};
356 LDAPMessage *res = NULL;
357 const char *gp_link, *gp_link_new = NULL;
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);
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);
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)));
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);
383 gp_link = ads_pull_string(ads, mem_ctx, res, "gPLink");
384 if (gp_link == NULL) {
385 return ADS_ERROR(LDAP_NO_SUCH_ATTRIBUTE);
388 /* find link to delete */
389 /* gp_link_new = talloc_asprintf(mem_ctx, "%s[%s;%d]", gp_link, gpo_dn, gpo_opt); */
391 ads_msgfree(ads, res);
392 ADS_ERROR_HAVE_NO_MEMORY(gp_link_new);
394 mods = ads_init_mods(mem_ctx);
395 ADS_ERROR_HAVE_NO_MEMORY(mods);
397 status = ads_mod_str(mem_ctx, &mods, "gPLink", gp_link_new);
398 if (!ADS_ERR_OK(status)) {
402 return ads_gen_mod(ads, link_dn, mods);
405 /****************************************************************
406 parse a GROUP_POLICY_OBJECT structure from an LDAPMessage result
407 ****************************************************************/
409 ADS_STATUS ads_parse_gpo(ADS_STRUCT *ads,
413 struct GROUP_POLICY_OBJECT *gpo)
417 ADS_ERROR_HAVE_NO_MEMORY(res);
420 gpo->ds_path = talloc_strdup(mem_ctx, gpo_dn);
422 gpo->ds_path = ads_get_dn(ads, res);
425 ADS_ERROR_HAVE_NO_MEMORY(gpo->ds_path);
427 if (!ads_pull_uint32(ads, res, "versionNumber", &gpo->version)) {
428 return ADS_ERROR(LDAP_NO_MEMORY);
432 if (!ads_pull_uint32(ads, res, "flags", &gpo->options)) {
433 return ADS_ERROR(LDAP_NO_MEMORY);
436 gpo->file_sys_path = ads_pull_string(ads, mem_ctx, res, "gPCFileSysPath");
437 ADS_ERROR_HAVE_NO_MEMORY(gpo->file_sys_path);
439 gpo->display_name = ads_pull_string(ads, mem_ctx, res, "displayName");
440 ADS_ERROR_HAVE_NO_MEMORY(gpo->display_name);
442 gpo->name = ads_pull_string(ads, mem_ctx, res, "name");
443 ADS_ERROR_HAVE_NO_MEMORY(gpo->name);
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");
449 ads_pull_sd(ads, mem_ctx, res, "ntSecurityDescriptor", &gpo->security_descriptor);
450 ADS_ERROR_HAVE_NO_MEMORY(gpo->security_descriptor);
452 return ADS_ERROR(LDAP_SUCCESS);
455 /****************************************************************
456 get a GROUP_POLICY_OBJECT structure based on different input paramters
457 ****************************************************************/
459 ADS_STATUS ads_get_gpo(ADS_STRUCT *ads,
462 const char *display_name,
463 const char *guid_name,
464 struct GROUP_POLICY_OBJECT *gpo)
467 LDAPMessage *res = NULL;
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;
478 if (!gpo_dn && !display_name && !guid_name) {
479 return ADS_ERROR(LDAP_NO_SUCH_OBJECT);
484 if (strnequal(gpo_dn, "LDAP://", strlen("LDAP://")) != 0) {
485 gpo_dn = gpo_dn + strlen("LDAP://");
488 status = ads_search_retry_dn_sd_flags(ads, &res,
492 } else if (display_name || guid_name) {
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);
500 status = ads_do_search_all_sd_flags(ads, ads->config.bind_path,
501 LDAP_SCOPE_SUBTREE, filter,
502 attrs, sd_flags, &res);
505 if (!ADS_ERR_OK(status)) {
506 DEBUG(10,("ads_get_gpo: search failed with %s\n", ads_errstr(status)));
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);
516 dn = ads_get_dn(ads, res);
518 ads_msgfree(ads, res);
519 return ADS_ERROR(LDAP_NO_MEMORY);
522 status = ads_parse_gpo(ads, mem_ctx, res, dn, gpo);
523 ads_msgfree(ads, res);
524 ads_memfree(ads, dn);
529 /****************************************************************
530 add a gplink to the GROUP_POLICY_OBJECT linked list
531 ****************************************************************/
533 static ADS_STATUS add_gplink_to_gpo_list(ADS_STRUCT *ads,
535 struct GROUP_POLICY_OBJECT **gpo_list,
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)
545 for (i = 0; i < gp_link->num_links; i++) {
547 struct GROUP_POLICY_OBJECT *new_gpo = NULL;
549 if (gp_link->link_opts[i] & GPO_LINK_OPT_DISABLED) {
550 DEBUG(10,("skipping disabled GPO\n"));
554 if (only_add_forced_gpos) {
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"));
560 DEBUG(10,("adding enforced GPO link although the GPOPTIONS_BLOCK_INHERITANCE has been set\n"));
564 new_gpo = TALLOC_ZERO_P(mem_ctx, struct GROUP_POLICY_OBJECT);
565 ADS_ERROR_HAVE_NO_MEMORY(new_gpo);
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]));
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);
581 new_gpo->link = link_dn;
582 new_gpo->link_type = link_type;
584 DLIST_ADD(*gpo_list, new_gpo);
586 DEBUG(10,("add_gplink_to_gplist: added GPLINK #%d %s to GPO list\n",
587 i, gp_link->link_names[i]));
590 return ADS_ERROR(LDAP_SUCCESS);
593 /****************************************************************
594 ****************************************************************/
596 static ADS_STATUS ads_get_gpo_sid_token(ADS_STRUCT *ads,
599 struct GPO_SID_TOKEN **token)
603 DOM_SID primary_group_sid;
604 DOM_SID *ad_token_sids;
605 size_t num_ad_token_sids = 0;
607 size_t num_token_sids = 0;
608 struct GPO_SID_TOKEN *new_token = NULL;
611 new_token = TALLOC_ZERO_P(mem_ctx, struct GPO_SID_TOKEN);
612 ADS_ERROR_HAVE_NO_MEMORY(new_token);
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)) {
621 new_token->object_sid = object_sid;
622 new_token->primary_group_sid = primary_group_sid;
624 token_sids = TALLOC_ARRAY(mem_ctx, DOM_SID, 1);
625 ADS_ERROR_HAVE_NO_MEMORY(token_sids);
627 for (i = 0; i < num_ad_token_sids; i++) {
629 if (sid_check_is_in_builtin(&ad_token_sids[i])) {
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);
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);
646 new_token->token_sids = token_sids;
647 new_token->num_token_sids = num_token_sids;
651 return ADS_ERROR_LDAP(LDAP_SUCCESS);
655 /****************************************************************
656 get the full list of GROUP_POLICY_OBJECTs for a given dn
657 ****************************************************************/
659 ADS_STATUS ads_get_gpo_list(ADS_STRUCT *ads,
663 struct GROUP_POLICY_OBJECT **gpo_list)
665 /* (L)ocal (S)ite (D)omain (O)rganizational(U)nit */
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;
673 ZERO_STRUCTP(gpo_list);
675 DEBUG(10,("ads_get_gpo_list: getting GPO list for [%s]\n", dn));
677 status = ads_get_gpo_sid_token(ads, mem_ctx, dn, &token);
678 if (!ADS_ERR_OK(status)) {
687 /* are site GPOs valid for users as well ??? */
688 if (flags & GPO_LIST_FLAG_MACHINE) {
690 status = ads_site_dn_for_machine(ads, mem_ctx, ads->config.ldap_server_name, &site_dn);
691 if (!ADS_ERR_OK(status)) {
695 DEBUG(10,("ads_get_gpo_list: query SITE: [%s] for GPOs\n", site_dn));
697 status = ads_get_gpo_link(ads, mem_ctx, site_dn, &gp_link);
698 if (ADS_ERR_OK(status)) {
700 if (DEBUGLEVEL >= 100) {
701 dump_gplink(ads, mem_ctx, &gp_link);
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,
708 if (!ADS_ERR_OK(status)) {
712 if (flags & GPO_LIST_FLAG_SITEONLY) {
713 return ADS_ERROR(LDAP_SUCCESS);
716 /* inheritance can't be blocked at the site level */
722 while ( (parent_dn = ads_parent_dn(tmp_dn)) &&
723 (!strequal(parent_dn, ads_parent_dn(ads->config.bind_path))) ) {
727 /* An account can just be a member of one domain */
728 if (strncmp(parent_dn, "DC=", strlen("DC=")) == 0) {
730 DEBUG(10,("ads_get_gpo_list: query DC: [%s] for GPOs\n", parent_dn));
732 status = ads_get_gpo_link(ads, mem_ctx, parent_dn, &gp_link);
733 if (ADS_ERR_OK(status)) {
735 if (DEBUGLEVEL >= 100) {
736 dump_gplink(ads, mem_ctx, &gp_link);
739 /* block inheritance from now on */
740 if (gp_link.gp_opts & GPOPTIONS_BLOCK_INHERITANCE) {
741 add_only_forced_gpos = True;
744 status = add_gplink_to_gpo_list(ads, mem_ctx,
746 &gp_link, GP_LINK_DOMAIN,
747 add_only_forced_gpos,
749 if (!ADS_ERR_OK(status)) {
761 while ( (parent_dn = ads_parent_dn(tmp_dn)) &&
762 (!strequal(parent_dn, ads_parent_dn(ads->config.bind_path))) ) {
765 /* (O)rganizational(U)nit */
767 /* An account can be a member of more OUs */
768 if (strncmp(parent_dn, "OU=", strlen("OU=")) == 0) {
770 DEBUG(10,("ads_get_gpo_list: query OU: [%s] for GPOs\n", parent_dn));
772 status = ads_get_gpo_link(ads, mem_ctx, parent_dn, &gp_link);
773 if (ADS_ERR_OK(status)) {
775 if (DEBUGLEVEL >= 100) {
776 dump_gplink(ads, mem_ctx, &gp_link);
779 /* block inheritance from now on */
780 if (gp_link.gp_opts & GPOPTIONS_BLOCK_INHERITANCE) {
781 add_only_forced_gpos = True;
784 status = add_gplink_to_gpo_list(ads, mem_ctx,
786 &gp_link, GP_LINK_OU,
787 add_only_forced_gpos,
789 if (!ADS_ERR_OK(status)) {
799 return ADS_ERROR(LDAP_SUCCESS);
802 #endif /* HAVE_LDAP */