2 Samba Unix/Linux SMB client library
3 net ads commands for Group Policy
4 Copyright (C) 2005 Guenther Deschner (gd@samba.org)
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.
22 #include "utils/net.h"
26 static int net_ads_gpo_usage(int argc
, const char **argv
)
29 "net ads gpo <COMMAND>\n"\
30 "<COMMAND> can be either:\n"\
31 " ADDLINK Link a container to a GPO\n"\
32 " APPLY Apply all GPOs\n"\
33 " DELETELINK Delete a gPLink from a container\n"\
34 " EFFECTIVE Lists all GPOs assigned to a machine\n"\
35 " GETGPO Lists specified GPO\n"\
36 " GETLINK Lists gPLink of a containter\n"\
37 " HELP Prints this help message\n"\
38 " LIST Lists all GPOs\n"\
44 static int net_ads_gpo_effective(int argc
, const char **argv
)
49 const char *attrs
[] = {"distinguishedName", "userAccountControl", NULL
};
53 struct GROUP_POLICY_OBJECT
*gpo_list
;
61 mem_ctx
= talloc_init("net_ads_gpo_effective");
62 if (mem_ctx
== NULL
) {
66 filter
= talloc_asprintf(mem_ctx
, "(&(objectclass=user)(sAMAccountName=%s))", argv
[0]);
71 if (!(ads
= ads_startup())) {
75 status
= ads_do_search_all(ads
, ads
->config
.bind_path
,
79 if (!ADS_ERR_OK(status
)) {
83 if (ads_count_replies(ads
, res
) != 1) {
84 printf("no result\n");
88 dn
= ads_get_dn(ads
, res
);
93 if (!ads_pull_uint32(ads
, res
, "userAccountControl", &uac
)) {
97 if (uac
& UF_WORKSTATION_TRUST_ACCOUNT
) {
98 flags
|= GPO_LIST_FLAG_MACHINE
;
101 printf("%s: '%s' has dn: '%s'\n",
102 (uac
& UF_WORKSTATION_TRUST_ACCOUNT
) ? "machine" : "user",
105 status
= ads_get_gpo_list(ads
, mem_ctx
, dn
, flags
, &gpo_list
);
106 if (!ADS_ERR_OK(status
)) {
110 printf("unsorted full dump of all GPOs for this machine:\n");
113 struct GROUP_POLICY_OBJECT
*gpo
= gpo_list
;
115 for (gpo
= gpo_list
; gpo
; gpo
= gpo
->next
) {
116 dump_gpo(mem_ctx
, gpo
);
120 printf("sorted full dump of all GPOs valid for this machine:\n");
123 ads_memfree(ads
, dn
);
124 ads_msgfree(ads
, res
);
127 talloc_destroy(mem_ctx
);
131 static int net_ads_gpo_list(int argc
, const char **argv
)
138 struct GROUP_POLICY_OBJECT gpo
;
141 mem_ctx
= talloc_init("net_ads_gpo_list");
142 if (mem_ctx
== NULL
) {
146 if (!(ads
= ads_startup())) {
150 status
= ads_do_search_all(ads
, ads
->config
.bind_path
,
152 "(objectclass=groupPolicyContainer)", NULL
, &res
);
153 if (!ADS_ERR_OK(status
)) {
154 d_printf("search failed: %s\n", ads_errstr(status
));
158 num_reply
= ads_count_replies(ads
, res
);
160 d_printf("Got %d replies\n\n", num_reply
);
162 /* dump the results */
163 for (msg
= ads_first_entry(ads
, res
); msg
; msg
= ads_next_entry(ads
, msg
)) {
165 status
= ads_parse_gpo(ads
, mem_ctx
, msg
, ads_get_dn(ads
, msg
), &gpo
);
167 if (!ADS_ERR_OK(status
)) {
168 d_printf("parse failed: %s\n", ads_errstr(status
));
172 dump_gpo(mem_ctx
, &gpo
);
177 ads_msgfree(ads
, res
);
179 talloc_destroy(mem_ctx
);
185 static int net_ads_gpo_apply(int argc
, const char **argv
)
190 const char *attrs
[] = {"distinguishedName", "userAccountControl", NULL
};
194 struct GROUP_POLICY_OBJECT
*gpo_list
;
202 mem_ctx
= talloc_init("net_ads_gpo_apply");
203 if (mem_ctx
== NULL
) {
207 filter
= talloc_asprintf(mem_ctx
, "(&(objectclass=user)(sAMAccountName=%s))", argv
[0]);
208 if (filter
== NULL
) {
212 if (!(ads
= ads_startup())) {
216 status
= ads_do_search_all(ads
, ads
->config
.bind_path
,
218 filter
, attrs
, &res
);
220 if (!ADS_ERR_OK(status
)) {
224 if (ads_count_replies(ads
, res
) != 1) {
225 printf("no result\n");
229 dn
= ads_get_dn(ads
, res
);
234 if (!ads_pull_uint32(ads
, res
, "userAccountControl", &uac
)) {
238 if (uac
& UF_WORKSTATION_TRUST_ACCOUNT
) {
239 flags
|= GPO_LIST_FLAG_MACHINE
;
242 printf("%s: '%s' has dn: '%s'\n",
243 (uac
& UF_WORKSTATION_TRUST_ACCOUNT
) ? "machine" : "user",
246 status
= ads_get_gpo_list(ads
, mem_ctx
, dn
, flags
, &gpo_list
);
247 if (!ADS_ERR_OK(status
)) {
251 /* FIXME: allow to process just a single extension */
252 status
= gpo_process_gpo_list(ads
, mem_ctx
, &gpo_list
, NULL
, flags
);
253 if (!ADS_ERR_OK(status
)) {
258 ads_memfree(ads
, dn
);
259 ads_msgfree(ads
, res
);
262 talloc_destroy(mem_ctx
);
267 static int net_ads_gpo_get_link(int argc
, const char **argv
)
272 struct GP_LINK gp_link
;
278 mem_ctx
= talloc_init("add_gpo_link");
279 if (mem_ctx
== NULL
) {
283 if (!(ads
= ads_startup())) {
287 status
= ads_get_gpo_link(ads
, mem_ctx
, argv
[0], &gp_link
);
288 if (!ADS_ERR_OK(status
)) {
289 d_printf("get link for %s failed: %s\n", argv
[0], ads_errstr(status
));
293 dump_gplink(ads
, mem_ctx
, &gp_link
);
296 talloc_destroy(mem_ctx
);
302 static int net_ads_gpo_add_link(int argc
, const char **argv
)
313 mem_ctx
= talloc_init("add_gpo_link");
314 if (mem_ctx
== NULL
) {
319 gpo_opt
= atoi(argv
[2]);
322 if (!(ads
= ads_startup())) {
326 status
= ads_add_gpo_link(ads
, mem_ctx
, argv
[0], argv
[1], gpo_opt
);
327 if (!ADS_ERR_OK(status
)) {
328 d_printf("add link failed: %s\n", ads_errstr(status
));
333 talloc_destroy(mem_ctx
);
339 static int net_ads_gpo_delete_link(int argc
, const char **argv
)
349 mem_ctx
= talloc_init("delete_gpo_link");
350 if (mem_ctx
== NULL
) {
354 if (!(ads
= ads_startup())) {
358 status
= ads_delete_gpo_link(ads
, mem_ctx
, argv
[0], argv
[1]);
359 if (!ADS_ERR_OK(status
)) {
360 d_printf("delete link failed: %s\n", ads_errstr(status
));
365 talloc_destroy(mem_ctx
);
371 static int net_ads_gpo_get_gpo(int argc
, const char **argv
)
376 struct GROUP_POLICY_OBJECT gpo
;
377 uint32 sysvol_gpt_version
;
383 mem_ctx
= talloc_init("add_gpo_get_gpo");
384 if (mem_ctx
== NULL
) {
388 if (!(ads
= ads_startup())) {
392 if (strnequal(argv
[0], "CN={", strlen("CN={"))) {
393 status
= ads_get_gpo(ads
, mem_ctx
, argv
[0], NULL
, NULL
, &gpo
);
395 status
= ads_get_gpo(ads
, mem_ctx
, NULL
, argv
[0], NULL
, &gpo
);
398 if (!ADS_ERR_OK(status
)) {
399 d_printf("get gpo for [%s] failed: %s\n", argv
[0], ads_errstr(status
));
403 dump_gpo(mem_ctx
, &gpo
);
405 status
= ADS_ERROR_NT(ads_gpo_get_sysvol_gpt_version(ads
, mem_ctx
, gpo
.file_sys_path
, &sysvol_gpt_version
));
406 if (!ADS_ERR_OK(status
)) {
410 printf("sysvol GPT version: %d\n", sysvol_gpt_version
);
413 talloc_destroy(mem_ctx
);
419 int net_ads_gpo(int argc
, const char **argv
)
421 struct functable func
[] = {
422 {"LIST", net_ads_gpo_list
},
423 {"EFFECTIVE", net_ads_gpo_effective
},
424 {"ADDLINK", net_ads_gpo_add_link
},
425 {"DELETELINK", net_ads_gpo_delete_link
},
426 {"GETLINK", net_ads_gpo_get_link
},
427 {"GETGPO", net_ads_gpo_get_gpo
},
428 {"HELP", net_ads_gpo_usage
},
429 {"APPLY", net_ads_gpo_apply
},
433 return net_run_function(argc
, argv
, func
, net_ads_gpo_usage
);