2 * Unix SMB/CIFS implementation.
3 * Group Policy Object Support
4 * Copyright (C) Guenther Deschner 2005,2007
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.
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, see <http://www.gnu.org/licenses/>.
21 #include "libgpo/gpo.h"
23 #include "../libcli/security/security.h"
25 /****************************************************************
26 parse the raw extension string into a GP_EXT structure
27 ****************************************************************/
29 bool ads_parse_gp_ext(TALLOC_CTX *mem_ctx,
30 const char *extension_raw,
31 struct GP_EXT **gp_ext)
34 struct GP_EXT *ext = NULL;
35 char **ext_list = NULL;
36 char **ext_strings = NULL;
43 DEBUG(20,("ads_parse_gp_ext: %s\n", extension_raw));
45 ext = talloc_zero(mem_ctx, struct GP_EXT);
50 ext_list = str_list_make(mem_ctx, extension_raw, "]");
55 for (i = 0; ext_list[i] != NULL; i++) {
62 ext->extensions = talloc_zero_array(mem_ctx, char *,
64 ext->extensions_guid = talloc_zero_array(mem_ctx, char *,
66 ext->snapins = talloc_zero_array(mem_ctx, char *,
68 ext->snapins_guid = talloc_zero_array(mem_ctx, char *,
72 ext->gp_extension = talloc_strdup(mem_ctx, extension_raw);
74 if (!ext->extensions || !ext->extensions_guid ||
75 !ext->snapins || !ext->snapins_guid ||
80 for (i = 0; ext_list[i] != NULL; i++) {
85 DEBUGADD(10,("extension #%d\n", i));
93 ext_strings = str_list_make(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,
109 cse_gpo_guid_string_to_name(q));
110 ext->extensions_guid[i] = talloc_strdup(mem_ctx, q);
112 /* we might have no name for the guid */
113 if (ext->extensions_guid[i] == NULL) {
117 for (k = 1; ext_strings[k] != NULL; k++) {
119 char *m = ext_strings[k];
125 /* FIXME: theoretically there could be more than one
126 * snapin per extension */
127 ext->snapins[i] = talloc_strdup(mem_ctx,
128 cse_snapin_gpo_guid_string_to_name(m));
129 ext->snapins_guid[i] = talloc_strdup(mem_ctx, m);
131 /* we might have no name for the guid */
132 if (ext->snapins_guid[i] == NULL) {
143 talloc_free(ext_list);
144 talloc_free(ext_strings);
151 /****************************************************************
152 parse the raw link string into a GP_LINK structure
153 ****************************************************************/
155 static ADS_STATUS gpo_parse_gplink(TALLOC_CTX *mem_ctx,
156 const char *gp_link_raw,
158 struct GP_LINK *gp_link)
160 ADS_STATUS status = ADS_ERROR_NT(NT_STATUS_NO_MEMORY);
164 ZERO_STRUCTP(gp_link);
166 DEBUG(10,("gpo_parse_gplink: gPLink: %s\n", gp_link_raw));
168 link_list = str_list_make_v3(mem_ctx, gp_link_raw, "]");
173 for (i = 0; link_list[i] != NULL; i++) {
177 gp_link->gp_opts = options;
178 gp_link->num_links = i;
180 if (gp_link->num_links) {
181 gp_link->link_names = talloc_zero_array(mem_ctx, char *,
183 gp_link->link_opts = talloc_zero_array(mem_ctx, uint32_t,
187 gp_link->gp_link = talloc_strdup(mem_ctx, gp_link_raw);
189 if (!gp_link->link_names || !gp_link->link_opts || !gp_link->gp_link) {
193 for (i = 0; link_list[i] != NULL; i++) {
197 DEBUGADD(10,("gpo_parse_gplink: processing link #%d\n", i));
210 gp_link->link_names[i] = talloc_strdup(mem_ctx, q);
211 if (gp_link->link_names[i] == NULL) {
214 gp_link->link_names[i][PTR_DIFF(p, q)] = 0;
216 gp_link->link_opts[i] = atoi(p + 1);
218 DEBUGADD(10,("gpo_parse_gplink: link: %s\n",
219 gp_link->link_names[i]));
220 DEBUGADD(10,("gpo_parse_gplink: opt: %d\n",
221 gp_link->link_opts[i]));
225 status = ADS_SUCCESS;
228 talloc_free(link_list);
233 /****************************************************************
234 helper call to get a GP_LINK structure from a linkdn
235 ****************************************************************/
237 ADS_STATUS ads_get_gpo_link(ADS_STRUCT *ads,
240 struct GP_LINK *gp_link_struct)
243 const char *attrs[] = {"gPLink", "gPOptions", NULL};
244 LDAPMessage *res = NULL;
248 ZERO_STRUCTP(gp_link_struct);
250 status = ads_search_dn(ads, &res, link_dn, attrs);
251 if (!ADS_ERR_OK(status)) {
252 DEBUG(10,("ads_get_gpo_link: search failed with %s\n",
253 ads_errstr(status)));
257 if (ads_count_replies(ads, res) != 1) {
258 DEBUG(10,("ads_get_gpo_link: no result\n"));
259 ads_msgfree(ads, res);
260 return ADS_ERROR(LDAP_NO_SUCH_OBJECT);
263 gp_link = ads_pull_string(ads, mem_ctx, res, "gPLink");
264 if (gp_link == NULL) {
265 DEBUG(10,("ads_get_gpo_link: no 'gPLink' attribute found\n"));
266 ads_msgfree(ads, res);
267 return ADS_ERROR(LDAP_NO_SUCH_ATTRIBUTE);
270 /* perfectly legal to have no options */
271 if (!ads_pull_uint32(ads, res, "gPOptions", &gp_options)) {
272 DEBUG(10,("ads_get_gpo_link: "
273 "no 'gPOptions' attribute found\n"));
277 ads_msgfree(ads, res);
279 return gpo_parse_gplink(mem_ctx, gp_link, gp_options, gp_link_struct);
282 /****************************************************************
283 helper call to add a gp link
284 ****************************************************************/
286 ADS_STATUS ads_add_gpo_link(ADS_STRUCT *ads,
293 const char *attrs[] = {"gPLink", NULL};
294 LDAPMessage *res = NULL;
295 const char *gp_link, *gp_link_new;
298 /* although ADS allows one to set anything here, we better check here if
299 * the gpo_dn is sane */
301 if (!strnequal(gpo_dn, "LDAP://CN={", strlen("LDAP://CN={")) != 0) {
302 return ADS_ERROR(LDAP_INVALID_DN_SYNTAX);
305 status = ads_search_dn(ads, &res, link_dn, attrs);
306 if (!ADS_ERR_OK(status)) {
307 DEBUG(10,("ads_add_gpo_link: search failed with %s\n",
308 ads_errstr(status)));
312 if (ads_count_replies(ads, res) != 1) {
313 DEBUG(10,("ads_add_gpo_link: no result\n"));
314 ads_msgfree(ads, res);
315 return ADS_ERROR(LDAP_NO_SUCH_OBJECT);
318 gp_link = ads_pull_string(ads, mem_ctx, res, "gPLink");
319 if (gp_link == NULL) {
320 gp_link_new = talloc_asprintf(mem_ctx, "[%s;%d]",
323 gp_link_new = talloc_asprintf(mem_ctx, "%s[%s;%d]",
324 gp_link, gpo_dn, gpo_opt);
327 ads_msgfree(ads, res);
328 ADS_ERROR_HAVE_NO_MEMORY(gp_link_new);
330 mods = ads_init_mods(mem_ctx);
331 ADS_ERROR_HAVE_NO_MEMORY(mods);
333 status = ads_mod_str(mem_ctx, &mods, "gPLink", gp_link_new);
334 if (!ADS_ERR_OK(status)) {
338 return ads_gen_mod(ads, link_dn, mods);
341 /****************************************************************
342 helper call to delete add a gp link
343 ****************************************************************/
345 /* untested & broken */
346 ADS_STATUS ads_delete_gpo_link(ADS_STRUCT *ads,
352 const char *attrs[] = {"gPLink", NULL};
353 LDAPMessage *res = NULL;
354 const char *gp_link, *gp_link_new = NULL;
357 /* check for a sane gpo_dn */
358 if (gpo_dn[0] != '[') {
359 DEBUG(10,("ads_delete_gpo_link: first char not: [\n"));
360 return ADS_ERROR(LDAP_INVALID_DN_SYNTAX);
363 if (gpo_dn[strlen(gpo_dn)] != ']') {
364 DEBUG(10,("ads_delete_gpo_link: last char not: ]\n"));
365 return ADS_ERROR(LDAP_INVALID_DN_SYNTAX);
368 status = ads_search_dn(ads, &res, link_dn, attrs);
369 if (!ADS_ERR_OK(status)) {
370 DEBUG(10,("ads_delete_gpo_link: search failed with %s\n",
371 ads_errstr(status)));
375 if (ads_count_replies(ads, res) != 1) {
376 DEBUG(10,("ads_delete_gpo_link: no result\n"));
377 ads_msgfree(ads, res);
378 return ADS_ERROR(LDAP_NO_SUCH_OBJECT);
381 gp_link = ads_pull_string(ads, mem_ctx, res, "gPLink");
382 if (gp_link == NULL) {
383 return ADS_ERROR(LDAP_NO_SUCH_ATTRIBUTE);
386 /* find link to delete */
387 /* gp_link_new = talloc_asprintf(mem_ctx, "%s[%s;%d]", gp_link,
390 ads_msgfree(ads, res);
391 ADS_ERROR_HAVE_NO_MEMORY(gp_link_new);
393 mods = ads_init_mods(mem_ctx);
394 ADS_ERROR_HAVE_NO_MEMORY(mods);
396 status = ads_mod_str(mem_ctx, &mods, "gPLink", gp_link_new);
397 if (!ADS_ERR_OK(status)) {
401 return ads_gen_mod(ads, link_dn, mods);
404 /****************************************************************
405 parse a GROUP_POLICY_OBJECT structure from an LDAPMessage result
406 ****************************************************************/
408 ADS_STATUS ads_parse_gpo(ADS_STRUCT *ads,
412 struct GROUP_POLICY_OBJECT *gpo)
416 ADS_ERROR_HAVE_NO_MEMORY(res);
419 gpo->ds_path = talloc_strdup(mem_ctx, gpo_dn);
421 gpo->ds_path = ads_get_dn(ads, mem_ctx, res);
424 ADS_ERROR_HAVE_NO_MEMORY(gpo->ds_path);
426 if (!ads_pull_uint32(ads, res, "versionNumber", &gpo->version)) {
427 return ADS_ERROR(LDAP_NO_SUCH_ATTRIBUTE);
430 if (!ads_pull_uint32(ads, res, "flags", &gpo->options)) {
431 return ADS_ERROR(LDAP_NO_SUCH_ATTRIBUTE);
434 gpo->file_sys_path = ads_pull_string(ads, mem_ctx, res,
436 if (gpo->file_sys_path == NULL) {
437 return ADS_ERROR(LDAP_NO_SUCH_ATTRIBUTE);
440 gpo->display_name = ads_pull_string(ads, mem_ctx, res,
442 if (gpo->display_name == NULL) {
443 return ADS_ERROR(LDAP_NO_SUCH_ATTRIBUTE);
446 gpo->name = ads_pull_string(ads, mem_ctx, res,
448 if (gpo->name == NULL) {
449 return ADS_ERROR(LDAP_NO_SUCH_ATTRIBUTE);
452 gpo->machine_extensions = ads_pull_string(ads, mem_ctx, res,
453 "gPCMachineExtensionNames");
454 gpo->user_extensions = ads_pull_string(ads, mem_ctx, res,
455 "gPCUserExtensionNames");
457 ads_pull_sd(ads, mem_ctx, res, "ntSecurityDescriptor",
458 &gpo->security_descriptor);
459 if (gpo->security_descriptor == NULL) {
460 return ADS_ERROR(LDAP_NO_SUCH_ATTRIBUTE);
463 return ADS_ERROR(LDAP_SUCCESS);
466 /****************************************************************
467 get a GROUP_POLICY_OBJECT structure based on different input parameters
468 ****************************************************************/
470 ADS_STATUS ads_get_gpo(ADS_STRUCT *ads,
473 const char *display_name,
474 const char *guid_name,
475 struct GROUP_POLICY_OBJECT *gpo)
478 LDAPMessage *res = NULL;
481 const char *attrs[] = {
486 "gPCFunctionalityVersion",
487 "gPCMachineExtensionNames",
488 "gPCUserExtensionNames",
491 "ntSecurityDescriptor",
494 uint32_t sd_flags = SECINFO_DACL;
498 if (!gpo_dn && !display_name && !guid_name) {
499 return ADS_ERROR(LDAP_NO_SUCH_OBJECT);
504 if (strnequal(gpo_dn, "LDAP://", strlen("LDAP://")) != 0) {
505 gpo_dn = gpo_dn + strlen("LDAP://");
508 status = ads_search_retry_dn_sd_flags(ads, &res,
512 } else if (display_name || guid_name) {
514 filter = talloc_asprintf(mem_ctx,
515 "(&(objectclass=groupPolicyContainer)(%s=%s))",
516 display_name ? "displayName" : "name",
517 display_name ? display_name : guid_name);
518 ADS_ERROR_HAVE_NO_MEMORY(filter);
520 status = ads_do_search_all_sd_flags(ads, ads->config.bind_path,
521 LDAP_SCOPE_SUBTREE, filter,
522 attrs, sd_flags, &res);
525 if (!ADS_ERR_OK(status)) {
526 DEBUG(10,("ads_get_gpo: search failed with %s\n",
527 ads_errstr(status)));
531 if (ads_count_replies(ads, res) != 1) {
532 DEBUG(10,("ads_get_gpo: no result\n"));
533 ads_msgfree(ads, res);
534 return ADS_ERROR(LDAP_NO_SUCH_OBJECT);
537 dn = ads_get_dn(ads, mem_ctx, res);
539 ads_msgfree(ads, res);
540 return ADS_ERROR(LDAP_NO_MEMORY);
543 status = ads_parse_gpo(ads, mem_ctx, res, dn, gpo);
544 ads_msgfree(ads, res);
550 /****************************************************************
551 add a gplink to the GROUP_POLICY_OBJECT linked list
552 ****************************************************************/
554 static ADS_STATUS add_gplink_to_gpo_list(ADS_STRUCT *ads,
556 struct GROUP_POLICY_OBJECT **gpo_list,
558 struct GP_LINK *gp_link,
559 enum GPO_LINK_TYPE link_type,
560 bool only_add_forced_gpos,
561 const struct security_token *token)
567 * Note: DLIST_ADD pushes to the front,
568 * so loop from last to first to get the
571 for (count = gp_link->num_links; count > 0; count--) {
572 /* NB. Index into arrays is one less than counter. */
573 uint32_t i = count - 1;
574 struct GROUP_POLICY_OBJECT *new_gpo = NULL;
576 if (gp_link->link_opts[i] & GPO_LINK_OPT_DISABLED) {
577 DEBUG(10,("skipping disabled GPO\n"));
581 if (only_add_forced_gpos) {
583 if (!(gp_link->link_opts[i] & GPO_LINK_OPT_ENFORCED)) {
584 DEBUG(10,("skipping nonenforced GPO link "
585 "because GPOPTIONS_BLOCK_INHERITANCE "
589 DEBUG(10,("adding enforced GPO link although "
590 "the GPOPTIONS_BLOCK_INHERITANCE "
595 new_gpo = talloc_zero(mem_ctx, struct GROUP_POLICY_OBJECT);
596 ADS_ERROR_HAVE_NO_MEMORY(new_gpo);
598 status = ads_get_gpo(ads, mem_ctx, gp_link->link_names[i],
599 NULL, NULL, new_gpo);
600 if (!ADS_ERR_OK(status)) {
601 DEBUG(10,("failed to get gpo: %s\n",
602 gp_link->link_names[i]));
603 if ((status.error_type == ENUM_ADS_ERROR_LDAP) &&
604 (status.err.rc == LDAP_NO_SUCH_ATTRIBUTE)) {
605 DEBUG(10,("skipping empty gpo: %s\n",
606 gp_link->link_names[i]));
607 talloc_free(new_gpo);
613 status = ADS_ERROR_NT(gpo_apply_security_filtering(new_gpo,
615 if (!ADS_ERR_OK(status)) {
616 DEBUG(10,("skipping GPO \"%s\" as object "
617 "has no access to it\n",
618 new_gpo->display_name));
619 talloc_free(new_gpo);
623 new_gpo->link = link_dn;
624 new_gpo->link_type = link_type;
626 DLIST_ADD(*gpo_list, new_gpo);
628 DEBUG(10,("add_gplink_to_gplist: added GPLINK #%d %s "
629 "to GPO list\n", i, gp_link->link_names[i]));
632 return ADS_ERROR(LDAP_SUCCESS);
635 /****************************************************************
636 ****************************************************************/
638 ADS_STATUS ads_get_sid_token(ADS_STRUCT *ads,
641 struct security_token **token)
644 struct dom_sid object_sid;
645 struct dom_sid primary_group_sid;
646 struct dom_sid *ad_token_sids;
647 size_t num_ad_token_sids = 0;
648 struct dom_sid *token_sids;
649 uint32_t num_token_sids = 0;
650 struct security_token *new_token = NULL;
653 status = ads_get_tokensids(ads, mem_ctx, dn,
654 &object_sid, &primary_group_sid,
655 &ad_token_sids, &num_ad_token_sids);
656 if (!ADS_ERR_OK(status)) {
660 token_sids = talloc_array(mem_ctx, struct dom_sid, 1);
661 ADS_ERROR_HAVE_NO_MEMORY(token_sids);
663 status = ADS_ERROR_NT(add_sid_to_array_unique(mem_ctx,
667 if (!ADS_ERR_OK(status)) {
671 for (i = 0; i < num_ad_token_sids; i++) {
673 if (sid_check_is_in_builtin(&ad_token_sids[i])) {
677 status = ADS_ERROR_NT(add_sid_to_array_unique(mem_ctx,
681 if (!ADS_ERR_OK(status)) {
686 new_token = create_local_nt_token(mem_ctx, &object_sid, false,
687 num_token_sids, token_sids);
688 ADS_ERROR_HAVE_NO_MEMORY(new_token);
692 security_token_debug(DBGC_CLASS, 5, *token);
694 return ADS_ERROR_LDAP(LDAP_SUCCESS);
697 /****************************************************************
698 ****************************************************************/
700 static ADS_STATUS add_local_policy_to_gpo_list(TALLOC_CTX *mem_ctx,
701 struct GROUP_POLICY_OBJECT **gpo_list,
702 enum GPO_LINK_TYPE link_type)
704 struct GROUP_POLICY_OBJECT *gpo = NULL;
706 ADS_ERROR_HAVE_NO_MEMORY(gpo_list);
708 gpo = talloc_zero(mem_ctx, struct GROUP_POLICY_OBJECT);
709 ADS_ERROR_HAVE_NO_MEMORY(gpo);
711 gpo->name = talloc_strdup(mem_ctx, "Local Policy");
712 ADS_ERROR_HAVE_NO_MEMORY(gpo->name);
714 gpo->display_name = talloc_strdup(mem_ctx, "Local Policy");
715 ADS_ERROR_HAVE_NO_MEMORY(gpo->display_name);
717 gpo->link_type = link_type;
719 DLIST_ADD(*gpo_list, gpo);
721 return ADS_ERROR_NT(NT_STATUS_OK);
724 /****************************************************************
725 get the full list of GROUP_POLICY_OBJECTs for a given dn
726 ****************************************************************/
728 ADS_STATUS ads_get_gpo_list(ADS_STRUCT *ads,
732 const struct security_token *token,
733 struct GROUP_POLICY_OBJECT **gpo_list)
736 * Push GPOs to gpo_list so that the traversal order of the list matches
737 * the order of application:
738 * (L)ocal (S)ite (D)omain (O)rganizational(U)nit
739 * Within domains and OUs: parent-to-child.
740 * Since GPOs are pushed to the front of gpo_list, GPOs have to be
741 * pushed in the opposite order of application (OUs first, local last,
746 struct GP_LINK gp_link;
747 const char *parent_dn, *site_dn, *tmp_dn;
748 bool add_only_forced_gpos = false;
750 ZERO_STRUCTP(gpo_list);
753 return ADS_ERROR_NT(NT_STATUS_INVALID_PARAMETER);
756 if (!ads_set_sasl_wrap_flags(ads, ADS_AUTH_SASL_SIGN)) {
757 return ADS_ERROR(LDAP_INVALID_CREDENTIALS);
760 DEBUG(10,("ads_get_gpo_list: getting GPO list for [%s]\n", dn));
764 while ((parent_dn = ads_parent_dn(tmp_dn)) &&
765 (!strequal(parent_dn, ads_parent_dn(ads->config.bind_path)))) {
768 /* (O)rganizational(U)nit */
770 /* An account can be a member of more OUs */
771 if (strncmp(parent_dn, "OU=", strlen("OU=")) == 0) {
773 DEBUG(10,("ads_get_gpo_list: query OU: [%s] for GPOs\n",
776 status = ads_get_gpo_link(ads, mem_ctx, parent_dn,
778 if (ADS_ERR_OK(status)) {
780 if (DEBUGLEVEL >= 100) {
781 dump_gplink(&gp_link);
784 status = add_gplink_to_gpo_list(ads,
790 add_only_forced_gpos,
792 if (!ADS_ERR_OK(status)) {
796 /* block inheritance from now on */
797 if (gp_link.gp_opts &
798 GPOPTIONS_BLOCK_INHERITANCE) {
799 add_only_forced_gpos = true;
811 while ((parent_dn = ads_parent_dn(tmp_dn)) &&
812 (!strequal(parent_dn, ads_parent_dn(ads->config.bind_path)))) {
816 /* An account can just be a member of one domain */
817 if (strncmp(parent_dn, "DC=", strlen("DC=")) == 0) {
819 DEBUG(10,("ads_get_gpo_list: query DC: [%s] for GPOs\n",
822 status = ads_get_gpo_link(ads, mem_ctx, parent_dn,
824 if (ADS_ERR_OK(status)) {
826 if (DEBUGLEVEL >= 100) {
827 dump_gplink(&gp_link);
830 status = add_gplink_to_gpo_list(ads,
836 add_only_forced_gpos,
838 if (!ADS_ERR_OK(status)) {
842 /* block inheritance from now on */
843 if (gp_link.gp_opts &
844 GPOPTIONS_BLOCK_INHERITANCE) {
845 add_only_forced_gpos = true;
855 /* are site GPOs valid for users as well ??? */
856 if (flags & GPO_LIST_FLAG_MACHINE) {
858 status = ads_site_dn_for_machine(ads, mem_ctx,
859 ads->config.ldap_server_name,
861 if (!ADS_ERR_OK(status)) {
865 DEBUG(10,("ads_get_gpo_list: query SITE: [%s] for GPOs\n",
868 status = ads_get_gpo_link(ads, mem_ctx, site_dn, &gp_link);
869 if (ADS_ERR_OK(status)) {
871 if (DEBUGLEVEL >= 100) {
872 dump_gplink(&gp_link);
875 status = add_gplink_to_gpo_list(ads, mem_ctx, gpo_list,
878 add_only_forced_gpos,
880 if (!ADS_ERR_OK(status)) {
884 if (flags & GPO_LIST_FLAG_SITEONLY) {
885 return ADS_ERROR(LDAP_SUCCESS);
888 /* inheritance can't be blocked at the site level */
893 status = add_local_policy_to_gpo_list(mem_ctx, gpo_list,
895 if (!ADS_ERR_OK(status)) {
899 return ADS_ERROR(LDAP_SUCCESS);
902 #endif /* HAVE_LDAP */