2 Samba Unix/Linux SMB client library
4 Copyright (C) 2001 Andrew Tridgell (tridge@samba.org)
5 Copyright (C) 2001 Remus Koos (remuskoos@yahoo.com)
6 Copyright (C) 2002 Jim McDonough (jmcd@us.ibm.com)
7 Copyright (C) 2006 Gerald (Jerry) Carter (jerry@samba.org)
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "utils/net.h"
25 #include "rpc_client/cli_pipe.h"
26 #include "librpc/gen_ndr/ndr_krb5pac.h"
27 #include "../librpc/gen_ndr/ndr_spoolss.h"
28 #include "nsswitch/libwbclient/wbclient.h"
30 #include "libads/cldap.h"
31 #include "../lib/addns/dnsquery.h"
32 #include "../libds/common/flags.h"
33 #include "librpc/gen_ndr/libnet_join.h"
34 #include "libnet/libnet_join.h"
38 #include "../libcli/security/security.h"
39 #include "libsmb/libsmb.h"
40 #include "lib/param/loadparm.h"
41 #include "utils/net_dns.h"
45 /* when we do not have sufficient input parameters to contact a remote domain
46 * we always fall back to our own realm - Guenther*/
48 static const char *assume_own_realm(struct net_context
*c
)
50 if (!c
->opt_host
&& strequal(lp_workgroup(), c
->opt_target_workgroup
)) {
58 do a cldap netlogon query
60 static int net_ads_cldap_netlogon(struct net_context
*c
, ADS_STRUCT
*ads
)
62 char addr
[INET6_ADDRSTRLEN
];
63 struct NETLOGON_SAM_LOGON_RESPONSE_EX reply
;
65 print_sockaddr(addr
, sizeof(addr
), &ads
->ldap
.ss
);
67 if ( !ads_cldap_netlogon_5(talloc_tos(), &ads
->ldap
.ss
, ads
->server
.realm
, &reply
) ) {
68 d_fprintf(stderr
, _("CLDAP query failed!\n"));
72 d_printf(_("Information for Domain Controller: %s\n\n"),
75 d_printf(_("Response Type: "));
76 switch (reply
.command
) {
77 case LOGON_SAM_LOGON_USER_UNKNOWN_EX
:
78 d_printf("LOGON_SAM_LOGON_USER_UNKNOWN_EX\n");
80 case LOGON_SAM_LOGON_RESPONSE_EX
:
81 d_printf("LOGON_SAM_LOGON_RESPONSE_EX\n");
84 d_printf("0x%x\n", reply
.command
);
88 d_printf(_("GUID: %s\n"), GUID_string(talloc_tos(),&reply
.domain_uuid
));
92 "\tIs a GC of the forest: %s\n"
93 "\tIs an LDAP server: %s\n"
95 "\tIs running a KDC: %s\n"
96 "\tIs running time services: %s\n"
97 "\tIs the closest DC: %s\n"
99 "\tHas a hardware clock: %s\n"
100 "\tIs a non-domain NC serviced by LDAP server: %s\n"
101 "\tIs NT6 DC that has some secrets: %s\n"
102 "\tIs NT6 DC that has all secrets: %s\n"
103 "\tRuns Active Directory Web Services: %s\n"
104 "\tRuns on Windows 2012 or later: %s\n"),
105 (reply
.server_type
& NBT_SERVER_PDC
) ? _("yes") : _("no"),
106 (reply
.server_type
& NBT_SERVER_GC
) ? _("yes") : _("no"),
107 (reply
.server_type
& NBT_SERVER_LDAP
) ? _("yes") : _("no"),
108 (reply
.server_type
& NBT_SERVER_DS
) ? _("yes") : _("no"),
109 (reply
.server_type
& NBT_SERVER_KDC
) ? _("yes") : _("no"),
110 (reply
.server_type
& NBT_SERVER_TIMESERV
) ? _("yes") : _("no"),
111 (reply
.server_type
& NBT_SERVER_CLOSEST
) ? _("yes") : _("no"),
112 (reply
.server_type
& NBT_SERVER_WRITABLE
) ? _("yes") : _("no"),
113 (reply
.server_type
& NBT_SERVER_GOOD_TIMESERV
) ? _("yes") : _("no"),
114 (reply
.server_type
& NBT_SERVER_NDNC
) ? _("yes") : _("no"),
115 (reply
.server_type
& NBT_SERVER_SELECT_SECRET_DOMAIN_6
) ? _("yes") : _("no"),
116 (reply
.server_type
& NBT_SERVER_FULL_SECRET_DOMAIN_6
) ? _("yes") : _("no"),
117 (reply
.server_type
& NBT_SERVER_ADS_WEB_SERVICE
) ? _("yes") : _("no"),
118 (reply
.server_type
& NBT_SERVER_DS_8
) ? _("yes") : _("no"));
121 printf(_("Forest:\t\t\t%s\n"), reply
.forest
);
122 printf(_("Domain:\t\t\t%s\n"), reply
.dns_domain
);
123 printf(_("Domain Controller:\t%s\n"), reply
.pdc_dns_name
);
125 printf(_("Pre-Win2k Domain:\t%s\n"), reply
.domain_name
);
126 printf(_("Pre-Win2k Hostname:\t%s\n"), reply
.pdc_name
);
128 if (*reply
.user_name
) printf(_("User name:\t%s\n"), reply
.user_name
);
130 printf(_("Server Site Name :\t\t%s\n"), reply
.server_site
);
131 printf(_("Client Site Name :\t\t%s\n"), reply
.client_site
);
133 d_printf(_("NT Version: %d\n"), reply
.nt_version
);
134 d_printf(_("LMNT Token: %.2x\n"), reply
.lmnt_token
);
135 d_printf(_("LM20 Token: %.2x\n"), reply
.lm20_token
);
141 this implements the CLDAP based netlogon lookup requests
142 for finding the domain controller of a ADS domain
144 static int net_ads_lookup(struct net_context
*c
, int argc
, const char **argv
)
149 if (c
->display_usage
) {
154 _("Find the ADS DC using CLDAP lookup.\n"));
158 if (!ADS_ERR_OK(ads_startup_nobind(c
, false, &ads
))) {
159 d_fprintf(stderr
, _("Didn't find the cldap server!\n"));
164 if (!ads
->config
.realm
) {
165 ads
->config
.realm
= discard_const_p(char, c
->opt_target_workgroup
);
166 ads
->ldap
.port
= 389;
169 ret
= net_ads_cldap_netlogon(c
, ads
);
176 static int net_ads_info(struct net_context
*c
, int argc
, const char **argv
)
179 char addr
[INET6_ADDRSTRLEN
];
182 if (c
->display_usage
) {
187 _("Display information about an Active Directory "
192 if (!ADS_ERR_OK(ads_startup_nobind(c
, false, &ads
))) {
193 d_fprintf(stderr
, _("Didn't find the ldap server!\n"));
197 if (!ads
|| !ads
->config
.realm
) {
198 d_fprintf(stderr
, _("Didn't find the ldap server!\n"));
203 /* Try to set the server's current time since we didn't do a full
204 TCP LDAP session initially */
206 if ( !ADS_ERR_OK(ads_current_time( ads
)) ) {
207 d_fprintf( stderr
, _("Failed to get server's current time!\n"));
210 pass_time
= secrets_fetch_pass_last_set_time(ads
->server
.workgroup
);
212 print_sockaddr(addr
, sizeof(addr
), &ads
->ldap
.ss
);
214 d_printf(_("LDAP server: %s\n"), addr
);
215 d_printf(_("LDAP server name: %s\n"), ads
->config
.ldap_server_name
);
216 d_printf(_("Realm: %s\n"), ads
->config
.realm
);
217 d_printf(_("Bind Path: %s\n"), ads
->config
.bind_path
);
218 d_printf(_("LDAP port: %d\n"), ads
->ldap
.port
);
219 d_printf(_("Server time: %s\n"),
220 http_timestring(talloc_tos(), ads
->config
.current_time
));
222 d_printf(_("KDC server: %s\n"), ads
->auth
.kdc_server
);
223 d_printf(_("Server time offset: %d\n"), ads
->auth
.time_offset
);
225 d_printf(_("Last machine account password change: %s\n"),
226 http_timestring(talloc_tos(), pass_time
));
232 static void use_in_memory_ccache(void) {
233 /* Use in-memory credentials cache so we do not interfere with
234 * existing credentials */
235 setenv(KRB5_ENV_CCNAME
, "MEMORY:net_ads", 1);
238 static ADS_STATUS
ads_startup_int(struct net_context
*c
, bool only_own_domain
,
239 uint32_t auth_flags
, ADS_STRUCT
**ads_ret
)
241 ADS_STRUCT
*ads
= NULL
;
243 bool need_password
= false;
244 bool second_time
= false;
246 const char *realm
= NULL
;
247 bool tried_closest_dc
= false;
249 /* lp_realm() should be handled by a command line param,
250 However, the join requires that realm be set in smb.conf
251 and compares our realm with the remote server's so this is
252 ok until someone needs more flexibility */
257 if (only_own_domain
) {
260 realm
= assume_own_realm(c
);
263 ads
= ads_init(realm
, c
->opt_target_workgroup
, c
->opt_host
);
265 if (!c
->opt_user_name
) {
266 c
->opt_user_name
= "administrator";
269 if (c
->opt_user_specified
) {
270 need_password
= true;
274 if (!c
->opt_password
&& need_password
&& !c
->opt_machine_pass
) {
275 c
->opt_password
= net_prompt_pass(c
, c
->opt_user_name
);
276 if (!c
->opt_password
) {
278 return ADS_ERROR(LDAP_NO_MEMORY
);
282 if (c
->opt_password
) {
283 use_in_memory_ccache();
284 SAFE_FREE(ads
->auth
.password
);
285 ads
->auth
.password
= smb_xstrdup(c
->opt_password
);
288 ads
->auth
.flags
|= auth_flags
;
289 SAFE_FREE(ads
->auth
.user_name
);
290 ads
->auth
.user_name
= smb_xstrdup(c
->opt_user_name
);
293 * If the username is of the form "name@realm",
294 * extract the realm and convert to upper case.
295 * This is only used to establish the connection.
297 if ((cp
= strchr_m(ads
->auth
.user_name
, '@'))!=0) {
299 SAFE_FREE(ads
->auth
.realm
);
300 ads
->auth
.realm
= smb_xstrdup(cp
);
301 if (!strupper_m(ads
->auth
.realm
)) {
303 return ADS_ERROR(LDAP_NO_MEMORY
);
307 status
= ads_connect(ads
);
309 if (!ADS_ERR_OK(status
)) {
311 if (NT_STATUS_EQUAL(ads_ntstatus(status
),
312 NT_STATUS_NO_LOGON_SERVERS
)) {
313 DEBUG(0,("ads_connect: %s\n", ads_errstr(status
)));
318 if (!need_password
&& !second_time
&& !(auth_flags
& ADS_AUTH_NO_BIND
)) {
319 need_password
= true;
328 /* when contacting our own domain, make sure we use the closest DC.
329 * This is done by reconnecting to ADS because only the first call to
330 * ads_connect will give us our own sitename */
332 if ((only_own_domain
|| !c
->opt_host
) && !tried_closest_dc
) {
334 tried_closest_dc
= true; /* avoid loop */
336 if (!ads_closest_dc(ads
)) {
338 namecache_delete(ads
->server
.realm
, 0x1C);
339 namecache_delete(ads
->server
.workgroup
, 0x1C);
352 ADS_STATUS
ads_startup(struct net_context
*c
, bool only_own_domain
, ADS_STRUCT
**ads
)
354 return ads_startup_int(c
, only_own_domain
, 0, ads
);
357 ADS_STATUS
ads_startup_nobind(struct net_context
*c
, bool only_own_domain
, ADS_STRUCT
**ads
)
359 return ads_startup_int(c
, only_own_domain
, ADS_AUTH_NO_BIND
, ads
);
363 Check to see if connection can be made via ads.
364 ads_startup() stores the password in opt_password if it needs to so
365 that rpc or rap can use it without re-prompting.
367 static int net_ads_check_int(const char *realm
, const char *workgroup
, const char *host
)
372 if ( (ads
= ads_init( realm
, workgroup
, host
)) == NULL
) {
376 ads
->auth
.flags
|= ADS_AUTH_NO_BIND
;
378 status
= ads_connect(ads
);
379 if ( !ADS_ERR_OK(status
) ) {
387 int net_ads_check_our_domain(struct net_context
*c
)
389 return net_ads_check_int(lp_realm(), lp_workgroup(), NULL
);
392 int net_ads_check(struct net_context
*c
)
394 return net_ads_check_int(NULL
, c
->opt_workgroup
, c
->opt_host
);
398 determine the netbios workgroup name for a domain
400 static int net_ads_workgroup(struct net_context
*c
, int argc
, const char **argv
)
403 struct NETLOGON_SAM_LOGON_RESPONSE_EX reply
;
405 if (c
->display_usage
) {
407 "net ads workgroup\n"
410 _("Print the workgroup name"));
414 if (!ADS_ERR_OK(ads_startup_nobind(c
, false, &ads
))) {
415 d_fprintf(stderr
, _("Didn't find the cldap server!\n"));
419 if (!ads
->config
.realm
) {
420 ads
->config
.realm
= discard_const_p(char, c
->opt_target_workgroup
);
421 ads
->ldap
.port
= 389;
424 if ( !ads_cldap_netlogon_5(talloc_tos(), &ads
->ldap
.ss
, ads
->server
.realm
, &reply
) ) {
425 d_fprintf(stderr
, _("CLDAP query failed!\n"));
430 d_printf(_("Workgroup: %s\n"), reply
.domain_name
);
439 static bool usergrp_display(ADS_STRUCT
*ads
, char *field
, void **values
, void *data_area
)
441 char **disp_fields
= (char **) data_area
;
443 if (!field
) { /* must be end of record */
444 if (disp_fields
[0]) {
445 if (!strchr_m(disp_fields
[0], '$')) {
447 d_printf("%-21.21s %s\n",
448 disp_fields
[0], disp_fields
[1]);
450 d_printf("%s\n", disp_fields
[0]);
453 SAFE_FREE(disp_fields
[0]);
454 SAFE_FREE(disp_fields
[1]);
457 if (!values
) /* must be new field, indicate string field */
459 if (strcasecmp_m(field
, "sAMAccountName") == 0) {
460 disp_fields
[0] = SMB_STRDUP((char *) values
[0]);
462 if (strcasecmp_m(field
, "description") == 0)
463 disp_fields
[1] = SMB_STRDUP((char *) values
[0]);
467 static int net_ads_user_usage(struct net_context
*c
, int argc
, const char **argv
)
469 return net_user_usage(c
, argc
, argv
);
472 static int ads_user_add(struct net_context
*c
, int argc
, const char **argv
)
477 LDAPMessage
*res
=NULL
;
481 if (argc
< 1 || c
->display_usage
)
482 return net_ads_user_usage(c
, argc
, argv
);
484 if (!ADS_ERR_OK(ads_startup(c
, false, &ads
))) {
488 status
= ads_find_user_acct(ads
, &res
, argv
[0]);
490 if (!ADS_ERR_OK(status
)) {
491 d_fprintf(stderr
, _("ads_user_add: %s\n"), ads_errstr(status
));
495 if (ads_count_replies(ads
, res
)) {
496 d_fprintf(stderr
, _("ads_user_add: User %s already exists\n"),
501 if (c
->opt_container
) {
502 ou_str
= SMB_STRDUP(c
->opt_container
);
504 ou_str
= ads_default_ou_string(ads
, DS_GUID_USERS_CONTAINER
);
507 status
= ads_add_user_acct(ads
, argv
[0], ou_str
, c
->opt_comment
);
509 if (!ADS_ERR_OK(status
)) {
510 d_fprintf(stderr
, _("Could not add user %s: %s\n"), argv
[0],
515 /* if no password is to be set, we're done */
517 d_printf(_("User %s added\n"), argv
[0]);
522 /* try setting the password */
523 if (asprintf(&upn
, "%s@%s", argv
[0], ads
->config
.realm
) == -1) {
526 status
= ads_krb5_set_password(ads
->auth
.kdc_server
, upn
, argv
[1],
527 ads
->auth
.time_offset
);
529 if (ADS_ERR_OK(status
)) {
530 d_printf(_("User %s added\n"), argv
[0]);
535 /* password didn't set, delete account */
536 d_fprintf(stderr
, _("Could not add user %s. "
537 "Error setting password %s\n"),
538 argv
[0], ads_errstr(status
));
539 ads_msgfree(ads
, res
);
540 status
=ads_find_user_acct(ads
, &res
, argv
[0]);
541 if (ADS_ERR_OK(status
)) {
542 userdn
= ads_get_dn(ads
, talloc_tos(), res
);
543 ads_del_dn(ads
, userdn
);
549 ads_msgfree(ads
, res
);
555 static int ads_user_info(struct net_context
*c
, int argc
, const char **argv
)
557 ADS_STRUCT
*ads
= NULL
;
559 LDAPMessage
*res
= NULL
;
563 const char *attrs
[] = {"memberOf", "primaryGroupID", NULL
};
564 char *searchstring
=NULL
;
568 struct dom_sid primary_group_sid
;
570 enum wbcSidType type
;
572 if (argc
< 1 || c
->display_usage
) {
573 return net_ads_user_usage(c
, argc
, argv
);
576 frame
= talloc_new(talloc_tos());
581 escaped_user
= escape_ldap_string(frame
, argv
[0]);
584 _("ads_user_info: failed to escape user %s\n"),
589 if (!ADS_ERR_OK(ads_startup(c
, false, &ads
))) {
594 if (asprintf(&searchstring
, "(sAMAccountName=%s)", escaped_user
) == -1) {
598 rc
= ads_search(ads
, &res
, searchstring
, attrs
);
599 SAFE_FREE(searchstring
);
601 if (!ADS_ERR_OK(rc
)) {
602 d_fprintf(stderr
, _("ads_search: %s\n"), ads_errstr(rc
));
607 if (!ads_pull_uint32(ads
, res
, "primaryGroupID", &group_rid
)) {
608 d_fprintf(stderr
, _("ads_pull_uint32 failed\n"));
613 rc
= ads_domain_sid(ads
, &primary_group_sid
);
614 if (!ADS_ERR_OK(rc
)) {
615 d_fprintf(stderr
, _("ads_domain_sid: %s\n"), ads_errstr(rc
));
620 sid_append_rid(&primary_group_sid
, group_rid
);
622 wbc_status
= wbcLookupSid((struct wbcDomainSid
*)&primary_group_sid
,
623 NULL
, /* don't look up domain */
626 if (!WBC_ERROR_IS_OK(wbc_status
)) {
627 d_fprintf(stderr
, "wbcLookupSid: %s\n",
628 wbcErrorString(wbc_status
));
633 d_printf("%s\n", primary_group
);
635 wbcFreeMemory(primary_group
);
637 grouplist
= ldap_get_values((LDAP
*)ads
->ldap
.ld
,
638 (LDAPMessage
*)res
, "memberOf");
643 for (i
=0;grouplist
[i
];i
++) {
644 groupname
= ldap_explode_dn(grouplist
[i
], 1);
645 d_printf("%s\n", groupname
[0]);
646 ldap_value_free(groupname
);
648 ldap_value_free(grouplist
);
652 if (res
) ads_msgfree(ads
, res
);
653 if (ads
) ads_destroy(&ads
);
658 static int ads_user_delete(struct net_context
*c
, int argc
, const char **argv
)
662 LDAPMessage
*res
= NULL
;
666 return net_ads_user_usage(c
, argc
, argv
);
669 if (!ADS_ERR_OK(ads_startup(c
, false, &ads
))) {
673 rc
= ads_find_user_acct(ads
, &res
, argv
[0]);
674 if (!ADS_ERR_OK(rc
) || ads_count_replies(ads
, res
) != 1) {
675 d_printf(_("User %s does not exist.\n"), argv
[0]);
676 ads_msgfree(ads
, res
);
680 userdn
= ads_get_dn(ads
, talloc_tos(), res
);
681 ads_msgfree(ads
, res
);
682 rc
= ads_del_dn(ads
, userdn
);
684 if (ADS_ERR_OK(rc
)) {
685 d_printf(_("User %s deleted\n"), argv
[0]);
689 d_fprintf(stderr
, _("Error deleting user %s: %s\n"), argv
[0],
695 int net_ads_user(struct net_context
*c
, int argc
, const char **argv
)
697 struct functable func
[] = {
702 N_("Add an AD user"),
703 N_("net ads user add\n"
710 N_("Display information about an AD user"),
711 N_("net ads user info\n"
712 " Display information about an AD user")
718 N_("Delete an AD user"),
719 N_("net ads user delete\n"
720 " Delete an AD user")
722 {NULL
, NULL
, 0, NULL
, NULL
}
726 const char *shortattrs
[] = {"sAMAccountName", NULL
};
727 const char *longattrs
[] = {"sAMAccountName", "description", NULL
};
728 char *disp_fields
[2] = {NULL
, NULL
};
731 if (c
->display_usage
) {
737 net_display_usage_from_functable(func
);
741 if (!ADS_ERR_OK(ads_startup(c
, false, &ads
))) {
745 if (c
->opt_long_list_entries
)
746 d_printf(_("\nUser name Comment"
747 "\n-----------------------------\n"));
749 rc
= ads_do_search_all_fn(ads
, ads
->config
.bind_path
,
751 "(objectCategory=user)",
752 c
->opt_long_list_entries
? longattrs
:
753 shortattrs
, usergrp_display
,
756 return ADS_ERR_OK(rc
) ? 0 : -1;
759 return net_run_function(c
, argc
, argv
, "net ads user", func
);
762 static int net_ads_group_usage(struct net_context
*c
, int argc
, const char **argv
)
764 return net_group_usage(c
, argc
, argv
);
767 static int ads_group_add(struct net_context
*c
, int argc
, const char **argv
)
771 LDAPMessage
*res
=NULL
;
775 if (argc
< 1 || c
->display_usage
) {
776 return net_ads_group_usage(c
, argc
, argv
);
779 if (!ADS_ERR_OK(ads_startup(c
, false, &ads
))) {
783 status
= ads_find_user_acct(ads
, &res
, argv
[0]);
785 if (!ADS_ERR_OK(status
)) {
786 d_fprintf(stderr
, _("ads_group_add: %s\n"), ads_errstr(status
));
790 if (ads_count_replies(ads
, res
)) {
791 d_fprintf(stderr
, _("ads_group_add: Group %s already exists\n"), argv
[0]);
795 if (c
->opt_container
) {
796 ou_str
= SMB_STRDUP(c
->opt_container
);
798 ou_str
= ads_default_ou_string(ads
, DS_GUID_USERS_CONTAINER
);
801 status
= ads_add_group_acct(ads
, argv
[0], ou_str
, c
->opt_comment
);
803 if (ADS_ERR_OK(status
)) {
804 d_printf(_("Group %s added\n"), argv
[0]);
807 d_fprintf(stderr
, _("Could not add group %s: %s\n"), argv
[0],
813 ads_msgfree(ads
, res
);
819 static int ads_group_delete(struct net_context
*c
, int argc
, const char **argv
)
823 LDAPMessage
*res
= NULL
;
826 if (argc
< 1 || c
->display_usage
) {
827 return net_ads_group_usage(c
, argc
, argv
);
830 if (!ADS_ERR_OK(ads_startup(c
, false, &ads
))) {
834 rc
= ads_find_user_acct(ads
, &res
, argv
[0]);
835 if (!ADS_ERR_OK(rc
) || ads_count_replies(ads
, res
) != 1) {
836 d_printf(_("Group %s does not exist.\n"), argv
[0]);
837 ads_msgfree(ads
, res
);
841 groupdn
= ads_get_dn(ads
, talloc_tos(), res
);
842 ads_msgfree(ads
, res
);
843 rc
= ads_del_dn(ads
, groupdn
);
844 TALLOC_FREE(groupdn
);
845 if (ADS_ERR_OK(rc
)) {
846 d_printf(_("Group %s deleted\n"), argv
[0]);
850 d_fprintf(stderr
, _("Error deleting group %s: %s\n"), argv
[0],
856 int net_ads_group(struct net_context
*c
, int argc
, const char **argv
)
858 struct functable func
[] = {
863 N_("Add an AD group"),
864 N_("net ads group add\n"
871 N_("Delete an AD group"),
872 N_("net ads group delete\n"
873 " Delete an AD group")
875 {NULL
, NULL
, 0, NULL
, NULL
}
879 const char *shortattrs
[] = {"sAMAccountName", NULL
};
880 const char *longattrs
[] = {"sAMAccountName", "description", NULL
};
881 char *disp_fields
[2] = {NULL
, NULL
};
884 if (c
->display_usage
) {
889 _("List AD groups"));
890 net_display_usage_from_functable(func
);
894 if (!ADS_ERR_OK(ads_startup(c
, false, &ads
))) {
898 if (c
->opt_long_list_entries
)
899 d_printf(_("\nGroup name Comment"
900 "\n-----------------------------\n"));
901 rc
= ads_do_search_all_fn(ads
, ads
->config
.bind_path
,
903 "(objectCategory=group)",
904 c
->opt_long_list_entries
? longattrs
:
905 shortattrs
, usergrp_display
,
909 return ADS_ERR_OK(rc
) ? 0 : -1;
911 return net_run_function(c
, argc
, argv
, "net ads group", func
);
914 static int net_ads_status(struct net_context
*c
, int argc
, const char **argv
)
920 if (c
->display_usage
) {
925 _("Display machine account details"));
929 if (!ADS_ERR_OK(ads_startup(c
, true, &ads
))) {
933 rc
= ads_find_machine_acct(ads
, &res
, lp_netbios_name());
934 if (!ADS_ERR_OK(rc
)) {
935 d_fprintf(stderr
, _("ads_find_machine_acct: %s\n"), ads_errstr(rc
));
940 if (ads_count_replies(ads
, res
) == 0) {
941 d_fprintf(stderr
, _("No machine account for '%s' found\n"), lp_netbios_name());
951 /*******************************************************************
952 Leave an AD domain. Windows XP disables the machine account.
953 We'll try the same. The old code would do an LDAP delete.
954 That only worked using the machine creds because added the machine
955 with full control to the computer object's ACL.
956 *******************************************************************/
958 static int net_ads_leave(struct net_context
*c
, int argc
, const char **argv
)
961 struct libnet_UnjoinCtx
*r
= NULL
;
964 if (c
->display_usage
) {
969 _("Leave an AD domain"));
974 d_fprintf(stderr
, _("No realm set, are we joined ?\n"));
978 if (!(ctx
= talloc_init("net_ads_leave"))) {
979 d_fprintf(stderr
, _("Could not initialise talloc context.\n"));
983 if (!c
->opt_kerberos
) {
984 use_in_memory_ccache();
988 d_fprintf(stderr
, _("Could not initialise message context. "
989 "Try running as root\n"));
993 werr
= libnet_init_UnjoinCtx(ctx
, &r
);
994 if (!W_ERROR_IS_OK(werr
)) {
995 d_fprintf(stderr
, _("Could not initialise unjoin context.\n"));
1000 r
->in
.use_kerberos
= c
->opt_kerberos
;
1001 r
->in
.dc_name
= c
->opt_host
;
1002 r
->in
.domain_name
= lp_realm();
1003 r
->in
.admin_account
= c
->opt_user_name
;
1004 r
->in
.admin_password
= net_prompt_pass(c
, c
->opt_user_name
);
1005 r
->in
.modify_config
= lp_config_backend_is_registry();
1007 /* Try to delete it, but if that fails, disable it. The
1008 WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE really means "disable */
1009 r
->in
.unjoin_flags
= WKSSVC_JOIN_FLAGS_JOIN_TYPE
|
1010 WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE
;
1011 r
->in
.delete_machine_account
= true;
1012 r
->in
.msg_ctx
= c
->msg_ctx
;
1014 werr
= libnet_Unjoin(ctx
, r
);
1015 if (!W_ERROR_IS_OK(werr
)) {
1016 d_printf(_("Failed to leave domain: %s\n"),
1017 r
->out
.error_string
? r
->out
.error_string
:
1018 get_friendly_werror_msg(werr
));
1022 if (r
->out
.deleted_machine_account
) {
1023 d_printf(_("Deleted account for '%s' in realm '%s'\n"),
1024 r
->in
.machine_name
, r
->out
.dns_domain_name
);
1028 /* We couldn't delete it - see if the disable succeeded. */
1029 if (r
->out
.disabled_machine_account
) {
1030 d_printf(_("Disabled account for '%s' in realm '%s'\n"),
1031 r
->in
.machine_name
, r
->out
.dns_domain_name
);
1036 /* Based on what we requested, we shouldn't get here, but if
1037 we did, it means the secrets were removed, and therefore
1038 we have left the domain */
1039 d_fprintf(stderr
, _("Machine '%s' Left domain '%s'\n"),
1040 r
->in
.machine_name
, r
->out
.dns_domain_name
);
1046 if (W_ERROR_IS_OK(werr
)) {
1053 static NTSTATUS
net_ads_join_ok(struct net_context
*c
)
1055 ADS_STRUCT
*ads
= NULL
;
1058 struct sockaddr_storage dcip
;
1060 if (!secrets_init()) {
1061 DEBUG(1,("Failed to initialise secrets database\n"));
1062 return NT_STATUS_ACCESS_DENIED
;
1065 net_use_krb_machine_account(c
);
1067 get_dc_name(lp_workgroup(), lp_realm(), dc_name
, &dcip
);
1069 status
= ads_startup(c
, true, &ads
);
1070 if (!ADS_ERR_OK(status
)) {
1071 return ads_ntstatus(status
);
1075 return NT_STATUS_OK
;
1079 check that an existing join is OK
1081 int net_ads_testjoin(struct net_context
*c
, int argc
, const char **argv
)
1084 use_in_memory_ccache();
1086 if (c
->display_usage
) {
1088 "net ads testjoin\n"
1091 _("Test if the existing join is ok"));
1095 /* Display success or failure */
1096 status
= net_ads_join_ok(c
);
1097 if (!NT_STATUS_IS_OK(status
)) {
1098 fprintf(stderr
, _("Join to domain is not valid: %s\n"),
1099 get_friendly_nt_error_msg(status
));
1103 printf(_("Join is OK\n"));
1107 /*******************************************************************
1108 Simple configu checks before beginning the join
1109 ********************************************************************/
1111 static WERROR
check_ads_config( void )
1113 if (lp_server_role() != ROLE_DOMAIN_MEMBER
) {
1114 d_printf(_("Host is not configured as a member server.\n"));
1115 return WERR_INVALID_DOMAIN_ROLE
;
1118 if (strlen(lp_netbios_name()) > 15) {
1119 d_printf(_("Our netbios name can be at most 15 chars long, "
1120 "\"%s\" is %u chars long\n"), lp_netbios_name(),
1121 (unsigned int)strlen(lp_netbios_name()));
1122 return WERR_INVALID_COMPUTERNAME
;
1125 if ( lp_security() == SEC_ADS
&& !*lp_realm()) {
1126 d_fprintf(stderr
, _("realm must be set in in %s for ADS "
1127 "join to succeed.\n"), get_dyn_CONFIGFILE());
1128 return WERR_INVALID_PARAM
;
1134 /*******************************************************************
1135 Send a DNS update request
1136 *******************************************************************/
1138 #if defined(WITH_DNS_UPDATES)
1139 #include "../lib/addns/dns.h"
1141 static NTSTATUS
net_update_dns_internal(struct net_context
*c
,
1142 TALLOC_CTX
*ctx
, ADS_STRUCT
*ads
,
1143 const char *machine_name
,
1144 const struct sockaddr_storage
*addrs
,
1147 struct dns_rr_ns
*nameservers
= NULL
;
1148 int ns_count
= 0, i
;
1149 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
1152 const char *dnsdomain
= NULL
;
1153 char *root_domain
= NULL
;
1155 if ( (dnsdomain
= strchr_m( machine_name
, '.')) == NULL
) {
1156 d_printf(_("No DNS domain configured for %s. "
1157 "Unable to perform DNS Update.\n"), machine_name
);
1158 status
= NT_STATUS_INVALID_PARAMETER
;
1163 status
= ads_dns_lookup_ns(ctx
,
1167 if ( !NT_STATUS_IS_OK(status
) || (ns_count
== 0)) {
1168 /* Child domains often do not have NS records. Look
1169 for the NS record for the forest root domain
1170 (rootDomainNamingContext in therootDSE) */
1172 const char *rootname_attrs
[] = { "rootDomainNamingContext", NULL
};
1173 LDAPMessage
*msg
= NULL
;
1175 ADS_STATUS ads_status
;
1177 if ( !ads
->ldap
.ld
) {
1178 ads_status
= ads_connect( ads
);
1179 if ( !ADS_ERR_OK(ads_status
) ) {
1180 DEBUG(0,("net_update_dns_internal: Failed to connect to our DC!\n"));
1185 ads_status
= ads_do_search(ads
, "", LDAP_SCOPE_BASE
,
1186 "(objectclass=*)", rootname_attrs
, &msg
);
1187 if (!ADS_ERR_OK(ads_status
)) {
1191 root_dn
= ads_pull_string(ads
, ctx
, msg
, "rootDomainNamingContext");
1193 ads_msgfree( ads
, msg
);
1197 root_domain
= ads_build_domain( root_dn
);
1200 ads_msgfree( ads
, msg
);
1202 /* try again for NS servers */
1204 status
= ads_dns_lookup_ns(ctx
,
1209 if ( !NT_STATUS_IS_OK(status
) || (ns_count
== 0)) {
1210 DEBUG(3,("net_update_dns_internal: Failed to find name server for the %s "
1211 "realm\n", ads
->config
.realm
));
1215 dnsdomain
= root_domain
;
1219 for (i
=0; i
< ns_count
; i
++) {
1221 uint32_t flags
= DNS_UPDATE_SIGNED
|
1222 DNS_UPDATE_UNSIGNED
|
1223 DNS_UPDATE_UNSIGNED_SUFFICIENT
|
1225 DNS_UPDATE_PROBE_SUFFICIENT
;
1228 flags
&= ~DNS_UPDATE_PROBE_SUFFICIENT
;
1229 flags
&= ~DNS_UPDATE_UNSIGNED_SUFFICIENT
;
1232 status
= NT_STATUS_UNSUCCESSFUL
;
1234 /* Now perform the dns update - we'll try non-secure and if we fail,
1235 we'll follow it up with a secure update */
1237 fstrcpy( dns_server
, nameservers
[i
].hostname
);
1239 dns_err
= DoDNSUpdate(dns_server
, dnsdomain
, machine_name
, addrs
, num_addrs
, flags
);
1240 if (ERR_DNS_IS_OK(dns_err
)) {
1241 status
= NT_STATUS_OK
;
1245 if (ERR_DNS_EQUAL(dns_err
, ERROR_DNS_INVALID_NAME_SERVER
) ||
1246 ERR_DNS_EQUAL(dns_err
, ERROR_DNS_CONNECTION_FAILED
) ||
1247 ERR_DNS_EQUAL(dns_err
, ERROR_DNS_SOCKET_ERROR
)) {
1248 DEBUG(1,("retrying DNS update with next nameserver after receiving %s\n",
1249 dns_errstr(dns_err
)));
1253 d_printf(_("DNS Update for %s failed: %s\n"),
1254 machine_name
, dns_errstr(dns_err
));
1255 status
= NT_STATUS_UNSUCCESSFUL
;
1261 SAFE_FREE( root_domain
);
1266 static NTSTATUS
net_update_dns_ext(struct net_context
*c
,
1267 TALLOC_CTX
*mem_ctx
, ADS_STRUCT
*ads
,
1268 const char *hostname
,
1269 struct sockaddr_storage
*iplist
,
1272 struct sockaddr_storage
*iplist_alloc
= NULL
;
1273 fstring machine_name
;
1277 fstrcpy(machine_name
, hostname
);
1279 name_to_fqdn( machine_name
, lp_netbios_name() );
1281 if (!strlower_m( machine_name
)) {
1282 return NT_STATUS_INVALID_PARAMETER
;
1285 if (num_addrs
== 0 || iplist
== NULL
) {
1287 * Get our ip address
1288 * (not the 127.0.0.x address but a real ip address)
1290 num_addrs
= get_my_ip_address(&iplist_alloc
);
1291 if ( num_addrs
<= 0 ) {
1292 DEBUG(4, ("net_update_dns_ext: Failed to find my "
1293 "non-loopback IP addresses!\n"));
1294 return NT_STATUS_INVALID_PARAMETER
;
1296 iplist
= iplist_alloc
;
1299 status
= net_update_dns_internal(c
, mem_ctx
, ads
, machine_name
,
1302 SAFE_FREE(iplist_alloc
);
1306 static NTSTATUS
net_update_dns(struct net_context
*c
, TALLOC_CTX
*mem_ctx
, ADS_STRUCT
*ads
, const char *hostname
)
1310 status
= net_update_dns_ext(c
, mem_ctx
, ads
, hostname
, NULL
, 0);
1316 /*******************************************************************
1317 ********************************************************************/
1319 static int net_ads_join_usage(struct net_context
*c
, int argc
, const char **argv
)
1321 d_printf(_("net ads join [--no-dns-updates] [options]\n"
1322 "Valid options:\n"));
1323 d_printf(_(" createupn[=UPN] Set the userPrincipalName attribute during the join.\n"
1324 " The default UPN is in the form host/netbiosname@REALM.\n"));
1325 d_printf(_(" createcomputer=OU Precreate the computer account in a specific OU.\n"
1326 " The OU string read from top to bottom without RDNs\n"
1327 " and delimited by a '/'.\n"
1328 " E.g. \"createcomputer=Computers/Servers/Unix\"\n"
1329 " NB: A backslash '\\' is used as escape at multiple\n"
1330 " levels and may need to be doubled or even\n"
1331 " quadrupled. It is not used as a separator.\n"));
1332 d_printf(_(" machinepass=PASS Set the machine password to a specific value during\n"
1333 " the join. The default password is random.\n"));
1334 d_printf(_(" osName=string Set the operatingSystem attribute during the join.\n"));
1335 d_printf(_(" osVer=string Set the operatingSystemVersion attribute during join.\n"
1336 " NB: osName and osVer must be specified together for\n"
1337 " either to take effect. The operatingSystemService\n"
1338 " attribute is then also set along with the two\n"
1339 " other attributes.\n"));
1340 d_printf(_(" osServicePack=string Set the operatingSystemServicePack attribute\n"
1341 " during the join.\n"
1342 " NB: If not specified then by default the samba\n"
1343 " version string is used instead.\n"));
1348 static void _net_ads_join_dns_updates(struct net_context
*c
, TALLOC_CTX
*ctx
, struct libnet_JoinCtx
*r
)
1350 #if defined(WITH_DNS_UPDATES)
1351 ADS_STRUCT
*ads_dns
= NULL
;
1356 * In a clustered environment, don't do dynamic dns updates:
1357 * Registering the set of ip addresses that are assigned to
1358 * the interfaces of the node that performs the join does usually
1359 * not have the desired effect, since the local interfaces do not
1360 * carry the complete set of the cluster's public IP addresses.
1361 * And it can also contain internal addresses that should not
1362 * be visible to the outside at all.
1363 * In order to do dns updates in a clustererd setup, use
1364 * net ads dns register.
1366 if (lp_clustering()) {
1367 d_fprintf(stderr
, _("Not doing automatic DNS update in a "
1368 "clustered setup.\n"));
1372 if (!r
->out
.domain_is_ad
) {
1377 * We enter this block with user creds.
1378 * kinit with the machine password to do dns update.
1381 ads_dns
= ads_init(lp_realm(), NULL
, r
->in
.dc_name
);
1383 if (ads_dns
== NULL
) {
1384 d_fprintf(stderr
, _("DNS update failed: out of memory!\n"));
1388 use_in_memory_ccache();
1390 ret
= asprintf(&ads_dns
->auth
.user_name
, "%s$", lp_netbios_name());
1392 d_fprintf(stderr
, _("DNS update failed: out of memory\n"));
1396 ads_dns
->auth
.password
= secrets_fetch_machine_password(
1397 r
->out
.netbios_domain_name
, NULL
, NULL
);
1398 if (ads_dns
->auth
.password
== NULL
) {
1399 d_fprintf(stderr
, _("DNS update failed: out of memory\n"));
1403 ads_dns
->auth
.realm
= SMB_STRDUP(r
->out
.dns_domain_name
);
1404 if (ads_dns
->auth
.realm
== NULL
) {
1405 d_fprintf(stderr
, _("DNS update failed: out of memory\n"));
1409 if (!strupper_m(ads_dns
->auth
.realm
)) {
1410 d_fprintf(stderr
, _("strupper_m %s failed\n"), ads_dns
->auth
.realm
);
1414 ret
= ads_kinit_password(ads_dns
);
1417 _("DNS update failed: kinit failed: %s\n"),
1418 error_message(ret
));
1422 status
= net_update_dns(c
, ctx
, ads_dns
, NULL
);
1423 if (!NT_STATUS_IS_OK(status
)) {
1424 d_fprintf( stderr
, _("DNS update failed: %s\n"),
1429 ads_destroy(&ads_dns
);
1436 int net_ads_join(struct net_context
*c
, int argc
, const char **argv
)
1438 TALLOC_CTX
*ctx
= NULL
;
1439 struct libnet_JoinCtx
*r
= NULL
;
1440 const char *domain
= lp_realm();
1441 WERROR werr
= WERR_SETUP_NOT_JOINED
;
1442 bool createupn
= false;
1443 const char *machineupn
= NULL
;
1444 const char *machine_password
= NULL
;
1445 const char *create_in_ou
= NULL
;
1447 const char *os_name
= NULL
;
1448 const char *os_version
= NULL
;
1449 const char *os_servicepack
= NULL
;
1450 bool modify_config
= lp_config_backend_is_registry();
1451 enum libnetjoin_JoinDomNameType domain_name_type
= JoinDomNameTypeDNS
;
1453 if (c
->display_usage
)
1454 return net_ads_join_usage(c
, argc
, argv
);
1456 if (!modify_config
) {
1458 werr
= check_ads_config();
1459 if (!W_ERROR_IS_OK(werr
)) {
1460 d_fprintf(stderr
, _("Invalid configuration. Exiting....\n"));
1465 if (!(ctx
= talloc_init("net_ads_join"))) {
1466 d_fprintf(stderr
, _("Could not initialise talloc context.\n"));
1471 if (!c
->opt_kerberos
) {
1472 use_in_memory_ccache();
1475 werr
= libnet_init_JoinCtx(ctx
, &r
);
1476 if (!W_ERROR_IS_OK(werr
)) {
1480 /* process additional command line args */
1482 for ( i
=0; i
<argc
; i
++ ) {
1483 if ( !strncasecmp_m(argv
[i
], "createupn", strlen("createupn")) ) {
1485 machineupn
= get_string_param(argv
[i
]);
1487 else if ( !strncasecmp_m(argv
[i
], "createcomputer", strlen("createcomputer")) ) {
1488 if ( (create_in_ou
= get_string_param(argv
[i
])) == NULL
) {
1489 d_fprintf(stderr
, _("Please supply a valid OU path.\n"));
1490 werr
= WERR_INVALID_PARAM
;
1494 else if ( !strncasecmp_m(argv
[i
], "osName", strlen("osName")) ) {
1495 if ( (os_name
= get_string_param(argv
[i
])) == NULL
) {
1496 d_fprintf(stderr
, _("Please supply a operating system name.\n"));
1497 werr
= WERR_INVALID_PARAM
;
1501 else if ( !strncasecmp_m(argv
[i
], "osVer", strlen("osVer")) ) {
1502 if ( (os_version
= get_string_param(argv
[i
])) == NULL
) {
1503 d_fprintf(stderr
, _("Please supply a valid operating system version.\n"));
1504 werr
= WERR_INVALID_PARAM
;
1508 else if ( !strncasecmp_m(argv
[i
], "osServicePack", strlen("osServicePack")) ) {
1509 if ( (os_servicepack
= get_string_param(argv
[i
])) == NULL
) {
1510 d_fprintf(stderr
, _("Please supply a valid servicepack identifier.\n"));
1511 werr
= WERR_INVALID_PARAM
;
1515 else if ( !strncasecmp_m(argv
[i
], "machinepass", strlen("machinepass")) ) {
1516 if ( (machine_password
= get_string_param(argv
[i
])) == NULL
) {
1517 d_fprintf(stderr
, _("Please supply a valid password to set as trust account password.\n"));
1518 werr
= WERR_INVALID_PARAM
;
1524 if (strchr(domain
, '.') == NULL
) {
1525 domain_name_type
= JoinDomNameTypeUnknown
;
1527 domain_name_type
= JoinDomNameTypeDNS
;
1533 d_fprintf(stderr
, _("Please supply a valid domain name\n"));
1534 werr
= WERR_INVALID_PARAM
;
1539 d_fprintf(stderr
, _("Could not initialise message context. "
1540 "Try running as root\n"));
1541 werr
= WERR_ACCESS_DENIED
;
1545 /* Do the domain join here */
1547 r
->in
.domain_name
= domain
;
1548 r
->in
.domain_name_type
= domain_name_type
;
1549 r
->in
.create_upn
= createupn
;
1550 r
->in
.upn
= machineupn
;
1551 r
->in
.account_ou
= create_in_ou
;
1552 r
->in
.os_name
= os_name
;
1553 r
->in
.os_version
= os_version
;
1554 r
->in
.os_servicepack
= os_servicepack
;
1555 r
->in
.dc_name
= c
->opt_host
;
1556 r
->in
.admin_account
= c
->opt_user_name
;
1557 r
->in
.admin_password
= net_prompt_pass(c
, c
->opt_user_name
);
1558 r
->in
.machine_password
= machine_password
;
1560 r
->in
.use_kerberos
= c
->opt_kerberos
;
1561 r
->in
.modify_config
= modify_config
;
1562 r
->in
.join_flags
= WKSSVC_JOIN_FLAGS_JOIN_TYPE
|
1563 WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE
|
1564 WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED
;
1565 r
->in
.msg_ctx
= c
->msg_ctx
;
1567 werr
= libnet_Join(ctx
, r
);
1568 if (W_ERROR_EQUAL(werr
, WERR_DCNOTFOUND
) &&
1569 strequal(domain
, lp_realm())) {
1570 r
->in
.domain_name
= lp_workgroup();
1571 r
->in
.domain_name_type
= JoinDomNameTypeNBT
;
1572 werr
= libnet_Join(ctx
, r
);
1574 if (!W_ERROR_IS_OK(werr
)) {
1578 /* Check the short name of the domain */
1580 if (!modify_config
&& !strequal(lp_workgroup(), r
->out
.netbios_domain_name
)) {
1581 d_printf(_("The workgroup in %s does not match the short\n"
1582 "domain name obtained from the server.\n"
1583 "Using the name [%s] from the server.\n"
1584 "You should set \"workgroup = %s\" in %s.\n"),
1585 get_dyn_CONFIGFILE(), r
->out
.netbios_domain_name
,
1586 r
->out
.netbios_domain_name
, get_dyn_CONFIGFILE());
1589 d_printf(_("Using short domain name -- %s\n"), r
->out
.netbios_domain_name
);
1591 if (r
->out
.dns_domain_name
) {
1592 d_printf(_("Joined '%s' to dns domain '%s'\n"), r
->in
.machine_name
,
1593 r
->out
.dns_domain_name
);
1595 d_printf(_("Joined '%s' to domain '%s'\n"), r
->in
.machine_name
,
1596 r
->out
.netbios_domain_name
);
1600 * We try doing the dns update (if it was compiled in
1601 * and if it was not disabled on the command line).
1602 * If the dns update fails, we still consider the join
1603 * operation as succeeded if we came this far.
1605 if (!c
->opt_no_dns_updates
) {
1606 _net_ads_join_dns_updates(c
, ctx
, r
);
1615 /* issue an overall failure message at the end. */
1616 d_printf(_("Failed to join domain: %s\n"),
1617 r
&& r
->out
.error_string
? r
->out
.error_string
:
1618 get_friendly_werror_msg(werr
));
1624 /*******************************************************************
1625 ********************************************************************/
1627 static int net_ads_dns_register(struct net_context
*c
, int argc
, const char **argv
)
1629 #if defined(WITH_DNS_UPDATES)
1634 const char *hostname
= NULL
;
1635 const char **addrs_list
= NULL
;
1636 struct sockaddr_storage
*addrs
= NULL
;
1641 talloc_enable_leak_report();
1644 if (argc
<= 1 && lp_clustering() && lp_cluster_addresses() == NULL
) {
1645 d_fprintf(stderr
, _("Refusing DNS updates with automatic "
1646 "detection of addresses in a clustered "
1648 c
->display_usage
= true;
1651 if (c
->display_usage
) {
1653 "net ads dns register [hostname [IP [IP...]]]\n"
1656 _("Register hostname with DNS\n"));
1660 if (!(ctx
= talloc_init("net_ads_dns"))) {
1661 d_fprintf(stderr
, _("Could not initialise talloc context\n"));
1670 num_addrs
= argc
- 1;
1671 addrs_list
= &argv
[1];
1672 } else if (lp_clustering()) {
1673 addrs_list
= lp_cluster_addresses();
1674 num_addrs
= str_list_length(addrs_list
);
1677 if (num_addrs
> 0) {
1678 addrs
= talloc_zero_array(ctx
, struct sockaddr_storage
, num_addrs
);
1679 if (addrs
== NULL
) {
1680 d_fprintf(stderr
, _("Error allocating memory!\n"));
1686 for (count
= 0; count
< num_addrs
; count
++) {
1687 if (!interpret_string_addr(&addrs
[count
], addrs_list
[count
], 0)) {
1688 d_fprintf(stderr
, "%s '%s'.\n",
1689 _("Cannot interpret address"),
1696 status
= ads_startup(c
, true, &ads
);
1697 if ( !ADS_ERR_OK(status
) ) {
1698 DEBUG(1, ("error on ads_startup: %s\n", ads_errstr(status
)));
1703 ntstatus
= net_update_dns_ext(c
, ctx
, ads
, hostname
, addrs
, num_addrs
);
1704 if (!NT_STATUS_IS_OK(ntstatus
)) {
1705 d_fprintf( stderr
, _("DNS update failed!\n") );
1706 ads_destroy( &ads
);
1711 d_fprintf( stderr
, _("Successfully registered hostname with DNS\n") );
1719 _("DNS update support not enabled at compile time!\n"));
1724 static int net_ads_dns_gethostbyname(struct net_context
*c
, int argc
, const char **argv
)
1726 #if defined(WITH_DNS_UPDATES)
1730 talloc_enable_leak_report();
1733 if (argc
!= 2 || c
->display_usage
) {
1738 _("net ads dns gethostbyname <server> <name>\n"),
1739 _(" Look up hostname from the AD\n"
1740 " server\tName server to use\n"
1741 " name\tName to look up\n"));
1745 err
= do_gethostbyname(argv
[0], argv
[1]);
1747 d_printf(_("do_gethostbyname returned %s (%d)\n"),
1748 dns_errstr(err
), ERROR_DNS_V(err
));
1753 static int net_ads_dns(struct net_context
*c
, int argc
, const char *argv
[])
1755 struct functable func
[] = {
1758 net_ads_dns_register
,
1760 N_("Add host dns entry to AD"),
1761 N_("net ads dns register\n"
1762 " Add host dns entry to AD")
1766 net_ads_dns_gethostbyname
,
1769 N_("net ads dns gethostbyname\n"
1772 {NULL
, NULL
, 0, NULL
, NULL
}
1775 return net_run_function(c
, argc
, argv
, "net ads dns", func
);
1778 /*******************************************************************
1779 ********************************************************************/
1781 int net_ads_printer_usage(struct net_context
*c
, int argc
, const char **argv
)
1784 "\nnet ads printer search <printer>"
1785 "\n\tsearch for a printer in the directory\n"
1786 "\nnet ads printer info <printer> <server>"
1787 "\n\tlookup info in directory for printer on server"
1788 "\n\t(note: printer defaults to \"*\", server defaults to local)\n"
1789 "\nnet ads printer publish <printername>"
1790 "\n\tpublish printer in directory"
1791 "\n\t(note: printer name is required)\n"
1792 "\nnet ads printer remove <printername>"
1793 "\n\tremove printer from directory"
1794 "\n\t(note: printer name is required)\n"));
1798 /*******************************************************************
1799 ********************************************************************/
1801 static int net_ads_printer_search(struct net_context
*c
, int argc
, const char **argv
)
1805 LDAPMessage
*res
= NULL
;
1807 if (c
->display_usage
) {
1809 "net ads printer search\n"
1812 _("List printers in the AD"));
1816 if (!ADS_ERR_OK(ads_startup(c
, false, &ads
))) {
1820 rc
= ads_find_printers(ads
, &res
);
1822 if (!ADS_ERR_OK(rc
)) {
1823 d_fprintf(stderr
, _("ads_find_printer: %s\n"), ads_errstr(rc
));
1824 ads_msgfree(ads
, res
);
1829 if (ads_count_replies(ads
, res
) == 0) {
1830 d_fprintf(stderr
, _("No results found\n"));
1831 ads_msgfree(ads
, res
);
1837 ads_msgfree(ads
, res
);
1842 static int net_ads_printer_info(struct net_context
*c
, int argc
, const char **argv
)
1846 const char *servername
, *printername
;
1847 LDAPMessage
*res
= NULL
;
1849 if (c
->display_usage
) {
1852 _("net ads printer info [printername [servername]]\n"
1853 " Display printer info from AD\n"
1854 " printername\tPrinter name or wildcard\n"
1855 " servername\tName of the print server\n"));
1859 if (!ADS_ERR_OK(ads_startup(c
, false, &ads
))) {
1864 printername
= argv
[0];
1870 servername
= argv
[1];
1872 servername
= lp_netbios_name();
1875 rc
= ads_find_printer_on_server(ads
, &res
, printername
, servername
);
1877 if (!ADS_ERR_OK(rc
)) {
1878 d_fprintf(stderr
, _("Server '%s' not found: %s\n"),
1879 servername
, ads_errstr(rc
));
1880 ads_msgfree(ads
, res
);
1885 if (ads_count_replies(ads
, res
) == 0) {
1886 d_fprintf(stderr
, _("Printer '%s' not found\n"), printername
);
1887 ads_msgfree(ads
, res
);
1893 ads_msgfree(ads
, res
);
1899 static int net_ads_printer_publish(struct net_context
*c
, int argc
, const char **argv
)
1903 const char *servername
, *printername
;
1904 struct cli_state
*cli
= NULL
;
1905 struct rpc_pipe_client
*pipe_hnd
= NULL
;
1906 struct sockaddr_storage server_ss
;
1908 TALLOC_CTX
*mem_ctx
= talloc_init("net_ads_printer_publish");
1909 ADS_MODLIST mods
= ads_init_mods(mem_ctx
);
1910 char *prt_dn
, *srv_dn
, **srv_cn
;
1911 char *srv_cn_escaped
= NULL
, *printername_escaped
= NULL
;
1912 LDAPMessage
*res
= NULL
;
1914 if (argc
< 1 || c
->display_usage
) {
1917 _("net ads printer publish <printername> [servername]\n"
1918 " Publish printer in AD\n"
1919 " printername\tName of the printer\n"
1920 " servername\tName of the print server\n"));
1921 talloc_destroy(mem_ctx
);
1925 if (!ADS_ERR_OK(ads_startup(c
, true, &ads
))) {
1926 talloc_destroy(mem_ctx
);
1930 printername
= argv
[0];
1933 servername
= argv
[1];
1935 servername
= lp_netbios_name();
1938 /* Get printer data from SPOOLSS */
1940 resolve_name(servername
, &server_ss
, 0x20, false);
1942 nt_status
= cli_full_connection(&cli
, lp_netbios_name(), servername
,
1945 c
->opt_user_name
, c
->opt_workgroup
,
1946 c
->opt_password
? c
->opt_password
: "",
1947 CLI_FULL_CONNECTION_USE_KERBEROS
,
1948 SMB_SIGNING_DEFAULT
);
1950 if (NT_STATUS_IS_ERR(nt_status
)) {
1951 d_fprintf(stderr
, _("Unable to open a connection to %s to "
1952 "obtain data for %s\n"),
1953 servername
, printername
);
1955 talloc_destroy(mem_ctx
);
1959 /* Publish on AD server */
1961 ads_find_machine_acct(ads
, &res
, servername
);
1963 if (ads_count_replies(ads
, res
) == 0) {
1964 d_fprintf(stderr
, _("Could not find machine account for server "
1968 talloc_destroy(mem_ctx
);
1972 srv_dn
= ldap_get_dn((LDAP
*)ads
->ldap
.ld
, (LDAPMessage
*)res
);
1973 srv_cn
= ldap_explode_dn(srv_dn
, 1);
1975 srv_cn_escaped
= escape_rdn_val_string_alloc(srv_cn
[0]);
1976 printername_escaped
= escape_rdn_val_string_alloc(printername
);
1977 if (!srv_cn_escaped
|| !printername_escaped
) {
1978 SAFE_FREE(srv_cn_escaped
);
1979 SAFE_FREE(printername_escaped
);
1980 d_fprintf(stderr
, _("Internal error, out of memory!"));
1982 talloc_destroy(mem_ctx
);
1986 if (asprintf(&prt_dn
, "cn=%s-%s,%s", srv_cn_escaped
, printername_escaped
, srv_dn
) == -1) {
1987 SAFE_FREE(srv_cn_escaped
);
1988 SAFE_FREE(printername_escaped
);
1989 d_fprintf(stderr
, _("Internal error, out of memory!"));
1991 talloc_destroy(mem_ctx
);
1995 SAFE_FREE(srv_cn_escaped
);
1996 SAFE_FREE(printername_escaped
);
1998 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_spoolss
, &pipe_hnd
);
1999 if (!NT_STATUS_IS_OK(nt_status
)) {
2000 d_fprintf(stderr
, _("Unable to open a connection to the spoolss pipe on %s\n"),
2004 talloc_destroy(mem_ctx
);
2008 if (!W_ERROR_IS_OK(get_remote_printer_publishing_data(pipe_hnd
, mem_ctx
, &mods
,
2012 talloc_destroy(mem_ctx
);
2016 rc
= ads_add_printer_entry(ads
, prt_dn
, mem_ctx
, &mods
);
2017 if (!ADS_ERR_OK(rc
)) {
2018 d_fprintf(stderr
, "ads_publish_printer: %s\n", ads_errstr(rc
));
2021 talloc_destroy(mem_ctx
);
2025 d_printf("published printer\n");
2028 talloc_destroy(mem_ctx
);
2033 static int net_ads_printer_remove(struct net_context
*c
, int argc
, const char **argv
)
2037 const char *servername
;
2039 LDAPMessage
*res
= NULL
;
2041 if (argc
< 1 || c
->display_usage
) {
2044 _("net ads printer remove <printername> [servername]\n"
2045 " Remove a printer from the AD\n"
2046 " printername\tName of the printer\n"
2047 " servername\tName of the print server\n"));
2051 if (!ADS_ERR_OK(ads_startup(c
, true, &ads
))) {
2056 servername
= argv
[1];
2058 servername
= lp_netbios_name();
2061 rc
= ads_find_printer_on_server(ads
, &res
, argv
[0], servername
);
2063 if (!ADS_ERR_OK(rc
)) {
2064 d_fprintf(stderr
, _("ads_find_printer_on_server: %s\n"), ads_errstr(rc
));
2065 ads_msgfree(ads
, res
);
2070 if (ads_count_replies(ads
, res
) == 0) {
2071 d_fprintf(stderr
, _("Printer '%s' not found\n"), argv
[1]);
2072 ads_msgfree(ads
, res
);
2077 prt_dn
= ads_get_dn(ads
, talloc_tos(), res
);
2078 ads_msgfree(ads
, res
);
2079 rc
= ads_del_dn(ads
, prt_dn
);
2080 TALLOC_FREE(prt_dn
);
2082 if (!ADS_ERR_OK(rc
)) {
2083 d_fprintf(stderr
, _("ads_del_dn: %s\n"), ads_errstr(rc
));
2092 static int net_ads_printer(struct net_context
*c
, int argc
, const char **argv
)
2094 struct functable func
[] = {
2097 net_ads_printer_search
,
2099 N_("Search for a printer"),
2100 N_("net ads printer search\n"
2101 " Search for a printer")
2105 net_ads_printer_info
,
2107 N_("Display printer information"),
2108 N_("net ads printer info\n"
2109 " Display printer information")
2113 net_ads_printer_publish
,
2115 N_("Publish a printer"),
2116 N_("net ads printer publish\n"
2117 " Publish a printer")
2121 net_ads_printer_remove
,
2123 N_("Delete a printer"),
2124 N_("net ads printer remove\n"
2125 " Delete a printer")
2127 {NULL
, NULL
, 0, NULL
, NULL
}
2130 return net_run_function(c
, argc
, argv
, "net ads printer", func
);
2134 static int net_ads_password(struct net_context
*c
, int argc
, const char **argv
)
2137 const char *auth_principal
= c
->opt_user_name
;
2138 const char *auth_password
= c
->opt_password
;
2139 const char *realm
= NULL
;
2140 const char *new_password
= NULL
;
2143 char pwd
[256] = {0};
2146 if (c
->display_usage
) {
2149 _("net ads password <username>\n"
2150 " Change password for user\n"
2151 " username\tName of user to change password for\n"));
2155 if (c
->opt_user_name
== NULL
|| c
->opt_password
== NULL
) {
2156 d_fprintf(stderr
, _("You must supply an administrator "
2157 "username/password\n"));
2162 d_fprintf(stderr
, _("ERROR: You must say which username to "
2163 "change password for\n"));
2168 if (!strchr_m(user
, '@')) {
2169 if (asprintf(&chr
, "%s@%s", argv
[0], lp_realm()) == -1) {
2175 use_in_memory_ccache();
2176 chr
= strchr_m(auth_principal
, '@');
2183 /* use the realm so we can eventually change passwords for users
2184 in realms other than default */
2185 if (!(ads
= ads_init(realm
, c
->opt_workgroup
, c
->opt_host
))) {
2189 /* we don't actually need a full connect, but it's the easy way to
2190 fill in the KDC's addresss */
2193 if (!ads
->config
.realm
) {
2194 d_fprintf(stderr
, _("Didn't find the kerberos server!\n"));
2200 new_password
= (const char *)argv
[1];
2204 if (asprintf(&prompt
, _("Enter new password for %s:"), user
) == -1) {
2207 rc
= samba_getpass(prompt
, pwd
, sizeof(pwd
), false, true);
2215 ret
= kerberos_set_password(ads
->auth
.kdc_server
, auth_principal
,
2216 auth_password
, user
, new_password
, ads
->auth
.time_offset
);
2217 memset(pwd
, '\0', sizeof(pwd
));
2218 if (!ADS_ERR_OK(ret
)) {
2219 d_fprintf(stderr
, _("Password change failed: %s\n"), ads_errstr(ret
));
2224 d_printf(_("Password change for %s completed.\n"), user
);
2230 int net_ads_changetrustpw(struct net_context
*c
, int argc
, const char **argv
)
2233 char *host_principal
;
2237 if (c
->display_usage
) {
2239 "net ads changetrustpw\n"
2242 _("Change the machine account's trust password"));
2246 if (!secrets_init()) {
2247 DEBUG(1,("Failed to initialise secrets database\n"));
2251 net_use_krb_machine_account(c
);
2253 use_in_memory_ccache();
2255 if (!ADS_ERR_OK(ads_startup(c
, true, &ads
))) {
2259 fstrcpy(my_name
, lp_netbios_name());
2260 if (!strlower_m(my_name
)) {
2265 if (asprintf(&host_principal
, "%s$@%s", my_name
, ads
->config
.realm
) == -1) {
2269 d_printf(_("Changing password for principal: %s\n"), host_principal
);
2271 ret
= ads_change_trust_account_password(ads
, host_principal
);
2273 if (!ADS_ERR_OK(ret
)) {
2274 d_fprintf(stderr
, _("Password change failed: %s\n"), ads_errstr(ret
));
2276 SAFE_FREE(host_principal
);
2280 d_printf(_("Password change for principal %s succeeded.\n"), host_principal
);
2282 if (USE_SYSTEM_KEYTAB
) {
2283 d_printf(_("Attempting to update system keytab with new password.\n"));
2284 if (ads_keytab_create_default(ads
)) {
2285 d_printf(_("Failed to update system keytab.\n"));
2290 SAFE_FREE(host_principal
);
2296 help for net ads search
2298 static int net_ads_search_usage(struct net_context
*c
, int argc
, const char **argv
)
2301 "\nnet ads search <expression> <attributes...>\n"
2302 "\nPerform a raw LDAP search on a ADS server and dump the results.\n"
2303 "The expression is a standard LDAP search expression, and the\n"
2304 "attributes are a list of LDAP fields to show in the results.\n\n"
2305 "Example: net ads search '(objectCategory=group)' sAMAccountName\n\n"
2307 net_common_flags_usage(c
, argc
, argv
);
2313 general ADS search function. Useful in diagnosing problems in ADS
2315 static int net_ads_search(struct net_context
*c
, int argc
, const char **argv
)
2319 const char *ldap_exp
;
2321 LDAPMessage
*res
= NULL
;
2323 if (argc
< 1 || c
->display_usage
) {
2324 return net_ads_search_usage(c
, argc
, argv
);
2327 if (!ADS_ERR_OK(ads_startup(c
, false, &ads
))) {
2334 rc
= ads_do_search_retry(ads
, ads
->config
.bind_path
,
2336 ldap_exp
, attrs
, &res
);
2337 if (!ADS_ERR_OK(rc
)) {
2338 d_fprintf(stderr
, _("search failed: %s\n"), ads_errstr(rc
));
2343 d_printf(_("Got %d replies\n\n"), ads_count_replies(ads
, res
));
2345 /* dump the results */
2348 ads_msgfree(ads
, res
);
2356 help for net ads search
2358 static int net_ads_dn_usage(struct net_context
*c
, int argc
, const char **argv
)
2361 "\nnet ads dn <dn> <attributes...>\n"
2362 "\nperform a raw LDAP search on a ADS server and dump the results\n"
2363 "The DN standard LDAP DN, and the attributes are a list of LDAP fields \n"
2364 "to show in the results\n\n"
2365 "Example: net ads dn 'CN=administrator,CN=Users,DC=my,DC=domain' sAMAccountName\n\n"
2366 "Note: the DN must be provided properly escaped. See RFC 4514 for details\n\n"
2368 net_common_flags_usage(c
, argc
, argv
);
2374 general ADS search function. Useful in diagnosing problems in ADS
2376 static int net_ads_dn(struct net_context
*c
, int argc
, const char **argv
)
2382 LDAPMessage
*res
= NULL
;
2384 if (argc
< 1 || c
->display_usage
) {
2385 return net_ads_dn_usage(c
, argc
, argv
);
2388 if (!ADS_ERR_OK(ads_startup(c
, false, &ads
))) {
2395 rc
= ads_do_search_all(ads
, dn
,
2397 "(objectclass=*)", attrs
, &res
);
2398 if (!ADS_ERR_OK(rc
)) {
2399 d_fprintf(stderr
, _("search failed: %s\n"), ads_errstr(rc
));
2404 d_printf("Got %d replies\n\n", ads_count_replies(ads
, res
));
2406 /* dump the results */
2409 ads_msgfree(ads
, res
);
2416 help for net ads sid search
2418 static int net_ads_sid_usage(struct net_context
*c
, int argc
, const char **argv
)
2421 "\nnet ads sid <sid> <attributes...>\n"
2422 "\nperform a raw LDAP search on a ADS server and dump the results\n"
2423 "The SID is in string format, and the attributes are a list of LDAP fields \n"
2424 "to show in the results\n\n"
2425 "Example: net ads sid 'S-1-5-32' distinguishedName\n\n"
2427 net_common_flags_usage(c
, argc
, argv
);
2433 general ADS search function. Useful in diagnosing problems in ADS
2435 static int net_ads_sid(struct net_context
*c
, int argc
, const char **argv
)
2439 const char *sid_string
;
2441 LDAPMessage
*res
= NULL
;
2444 if (argc
< 1 || c
->display_usage
) {
2445 return net_ads_sid_usage(c
, argc
, argv
);
2448 if (!ADS_ERR_OK(ads_startup(c
, false, &ads
))) {
2452 sid_string
= argv
[0];
2455 if (!string_to_sid(&sid
, sid_string
)) {
2456 d_fprintf(stderr
, _("could not convert sid\n"));
2461 rc
= ads_search_retry_sid(ads
, &res
, &sid
, attrs
);
2462 if (!ADS_ERR_OK(rc
)) {
2463 d_fprintf(stderr
, _("search failed: %s\n"), ads_errstr(rc
));
2468 d_printf(_("Got %d replies\n\n"), ads_count_replies(ads
, res
));
2470 /* dump the results */
2473 ads_msgfree(ads
, res
);
2479 static int net_ads_keytab_flush(struct net_context
*c
, int argc
, const char **argv
)
2484 if (c
->display_usage
) {
2486 "net ads keytab flush\n"
2489 _("Delete the whole keytab"));
2493 if (!ADS_ERR_OK(ads_startup(c
, true, &ads
))) {
2496 ret
= ads_keytab_flush(ads
);
2501 static int net_ads_keytab_add(struct net_context
*c
, int argc
, const char **argv
)
2507 if (c
->display_usage
) {
2510 _("net ads keytab add <principal> [principal ...]\n"
2511 " Add principals to local keytab\n"
2512 " principal\tKerberos principal to add to "
2517 d_printf(_("Processing principals to add...\n"));
2518 if (!ADS_ERR_OK(ads_startup(c
, true, &ads
))) {
2521 for (i
= 0; i
< argc
; i
++) {
2522 ret
|= ads_keytab_add_entry(ads
, argv
[i
]);
2528 static int net_ads_keytab_create(struct net_context
*c
, int argc
, const char **argv
)
2533 if (c
->display_usage
) {
2535 "net ads keytab create\n"
2538 _("Create new default keytab"));
2542 if (!ADS_ERR_OK(ads_startup(c
, true, &ads
))) {
2545 ret
= ads_keytab_create_default(ads
);
2550 static int net_ads_keytab_list(struct net_context
*c
, int argc
, const char **argv
)
2552 const char *keytab
= NULL
;
2554 if (c
->display_usage
) {
2557 _("net ads keytab list [keytab]\n"
2558 " List a local keytab\n"
2559 " keytab\tKeytab to list\n"));
2567 return ads_keytab_list(keytab
);
2571 int net_ads_keytab(struct net_context
*c
, int argc
, const char **argv
)
2573 struct functable func
[] = {
2578 N_("Add a service principal"),
2579 N_("net ads keytab add\n"
2580 " Add a service principal")
2584 net_ads_keytab_create
,
2586 N_("Create a fresh keytab"),
2587 N_("net ads keytab create\n"
2588 " Create a fresh keytab")
2592 net_ads_keytab_flush
,
2594 N_("Remove all keytab entries"),
2595 N_("net ads keytab flush\n"
2596 " Remove all keytab entries")
2600 net_ads_keytab_list
,
2602 N_("List a keytab"),
2603 N_("net ads keytab list\n"
2606 {NULL
, NULL
, 0, NULL
, NULL
}
2609 if (!USE_KERBEROS_KEYTAB
) {
2610 d_printf(_("\nWarning: \"kerberos method\" must be set to a "
2611 "keytab method to use keytab functions.\n"));
2614 return net_run_function(c
, argc
, argv
, "net ads keytab", func
);
2617 static int net_ads_kerberos_renew(struct net_context
*c
, int argc
, const char **argv
)
2621 if (c
->display_usage
) {
2623 "net ads kerberos renew\n"
2626 _("Renew TGT from existing credential cache"));
2630 ret
= smb_krb5_renew_ticket(NULL
, NULL
, NULL
, NULL
);
2632 d_printf(_("failed to renew kerberos ticket: %s\n"),
2633 error_message(ret
));
2638 static int net_ads_kerberos_pac_common(struct net_context
*c
, int argc
, const char **argv
,
2639 struct PAC_DATA_CTR
**pac_data_ctr
)
2643 const char *impersonate_princ_s
= NULL
;
2644 const char *local_service
= NULL
;
2647 for (i
=0; i
<argc
; i
++) {
2648 if (strnequal(argv
[i
], "impersonate", strlen("impersonate"))) {
2649 impersonate_princ_s
= get_string_param(argv
[i
]);
2650 if (impersonate_princ_s
== NULL
) {
2654 if (strnequal(argv
[i
], "local_service", strlen("local_service"))) {
2655 local_service
= get_string_param(argv
[i
]);
2656 if (local_service
== NULL
) {
2662 if (local_service
== NULL
) {
2663 local_service
= talloc_asprintf(c
, "%s$@%s",
2664 lp_netbios_name(), lp_realm());
2665 if (local_service
== NULL
) {
2670 c
->opt_password
= net_prompt_pass(c
, c
->opt_user_name
);
2672 status
= kerberos_return_pac(c
,
2681 2592000, /* one month */
2682 impersonate_princ_s
,
2685 if (!NT_STATUS_IS_OK(status
)) {
2686 d_printf(_("failed to query kerberos PAC: %s\n"),
2696 static int net_ads_kerberos_pac_dump(struct net_context
*c
, int argc
, const char **argv
)
2698 struct PAC_DATA_CTR
*pac_data_ctr
= NULL
;
2701 enum PAC_TYPE type
= 0;
2703 if (c
->display_usage
) {
2705 "net ads kerberos pac dump [impersonate=string] [local_service=string] [pac_buffer_type=int]\n"
2708 _("Dump the Kerberos PAC"));
2712 for (i
=0; i
<argc
; i
++) {
2713 if (strnequal(argv
[i
], "pac_buffer_type", strlen("pac_buffer_type"))) {
2714 type
= get_int_param(argv
[i
]);
2718 ret
= net_ads_kerberos_pac_common(c
, argc
, argv
, &pac_data_ctr
);
2727 s
= NDR_PRINT_STRUCT_STRING(c
, PAC_DATA
,
2728 pac_data_ctr
->pac_data
);
2730 d_printf(_("The Pac: %s\n"), s
);
2737 for (i
=0; i
< pac_data_ctr
->pac_data
->num_buffers
; i
++) {
2741 if (pac_data_ctr
->pac_data
->buffers
[i
].type
!= type
) {
2745 s
= NDR_PRINT_UNION_STRING(c
, PAC_INFO
, type
,
2746 pac_data_ctr
->pac_data
->buffers
[i
].info
);
2748 d_printf(_("The Pac: %s\n"), s
);
2757 static int net_ads_kerberos_pac_save(struct net_context
*c
, int argc
, const char **argv
)
2759 struct PAC_DATA_CTR
*pac_data_ctr
= NULL
;
2760 char *filename
= NULL
;
2764 if (c
->display_usage
) {
2766 "net ads kerberos pac save [impersonate=string] [local_service=string] [filename=string]\n"
2769 _("Save the Kerberos PAC"));
2773 for (i
=0; i
<argc
; i
++) {
2774 if (strnequal(argv
[i
], "filename", strlen("filename"))) {
2775 filename
= get_string_param(argv
[i
]);
2776 if (filename
== NULL
) {
2782 ret
= net_ads_kerberos_pac_common(c
, argc
, argv
, &pac_data_ctr
);
2787 if (filename
== NULL
) {
2788 d_printf(_("please define \"filename=<filename>\" to save the PAC\n"));
2792 /* save the raw format */
2793 if (!file_save(filename
, pac_data_ctr
->pac_blob
.data
, pac_data_ctr
->pac_blob
.length
)) {
2794 d_printf(_("failed to save PAC in %s\n"), filename
);
2801 static int net_ads_kerberos_pac(struct net_context
*c
, int argc
, const char **argv
)
2803 struct functable func
[] = {
2806 net_ads_kerberos_pac_dump
,
2808 N_("Dump Kerberos PAC"),
2809 N_("net ads kerberos pac dump\n"
2810 " Dump a Kerberos PAC to stdout")
2814 net_ads_kerberos_pac_save
,
2816 N_("Save Kerberos PAC"),
2817 N_("net ads kerberos pac save\n"
2818 " Save a Kerberos PAC in a file")
2821 {NULL
, NULL
, 0, NULL
, NULL
}
2824 return net_run_function(c
, argc
, argv
, "net ads kerberos pac", func
);
2827 static int net_ads_kerberos_kinit(struct net_context
*c
, int argc
, const char **argv
)
2829 TALLOC_CTX
*mem_ctx
= NULL
;
2833 if (c
->display_usage
) {
2835 "net ads kerberos kinit\n"
2838 _("Get Ticket Granting Ticket (TGT) for the user"));
2842 mem_ctx
= talloc_init("net_ads_kerberos_kinit");
2847 c
->opt_password
= net_prompt_pass(c
, c
->opt_user_name
);
2849 ret
= kerberos_kinit_password_ext(c
->opt_user_name
,
2857 2592000, /* one month */
2860 d_printf(_("failed to kinit password: %s\n"),
2867 int net_ads_kerberos(struct net_context
*c
, int argc
, const char **argv
)
2869 struct functable func
[] = {
2872 net_ads_kerberos_kinit
,
2874 N_("Retrieve Ticket Granting Ticket (TGT)"),
2875 N_("net ads kerberos kinit\n"
2876 " Receive Ticket Granting Ticket (TGT)")
2880 net_ads_kerberos_renew
,
2882 N_("Renew Ticket Granting Ticket from credential cache"),
2883 N_("net ads kerberos renew\n"
2884 " Renew Ticket Granting Ticket (TGT) from "
2889 net_ads_kerberos_pac
,
2891 N_("Dump Kerberos PAC"),
2892 N_("net ads kerberos pac\n"
2893 " Dump Kerberos PAC")
2895 {NULL
, NULL
, 0, NULL
, NULL
}
2898 return net_run_function(c
, argc
, argv
, "net ads kerberos", func
);
2901 static int net_ads_enctype_lookup_account(struct net_context
*c
,
2903 const char *account
,
2905 const char **enctype_str
)
2908 const char *attrs
[] = {
2909 "msDS-SupportedEncryptionTypes",
2916 filter
= talloc_asprintf(c
, "(&(objectclass=user)(sAMAccountName=%s))",
2918 if (filter
== NULL
) {
2922 status
= ads_search(ads
, res
, filter
, attrs
);
2923 if (!ADS_ERR_OK(status
)) {
2924 d_printf(_("no account found with filter: %s\n"), filter
);
2928 count
= ads_count_replies(ads
, *res
);
2933 d_printf(_("no account found with filter: %s\n"), filter
);
2936 d_printf(_("multiple accounts found with filter: %s\n"), filter
);
2941 *enctype_str
= ads_pull_string(ads
, c
, *res
,
2942 "msDS-SupportedEncryptionTypes");
2943 if (*enctype_str
== NULL
) {
2944 d_printf(_("no msDS-SupportedEncryptionTypes attribute found\n"));
2954 static void net_ads_enctype_dump_enctypes(const char *username
,
2955 const char *enctype_str
)
2957 int enctypes
= atoi(enctype_str
);
2959 d_printf(_("'%s' uses \"msDS-SupportedEncryptionTypes\": %d (0x%08x)\n"),
2960 username
, enctypes
, enctypes
);
2962 printf("[%s] 0x%08x DES-CBC-CRC\n",
2963 enctypes
& ENC_CRC32
? "X" : " ",
2965 printf("[%s] 0x%08x DES-CBC-MD5\n",
2966 enctypes
& ENC_RSA_MD5
? "X" : " ",
2968 printf("[%s] 0x%08x RC4-HMAC\n",
2969 enctypes
& ENC_RC4_HMAC_MD5
? "X" : " ",
2971 printf("[%s] 0x%08x AES128-CTS-HMAC-SHA1-96\n",
2972 enctypes
& ENC_HMAC_SHA1_96_AES128
? "X" : " ",
2973 ENC_HMAC_SHA1_96_AES128
);
2974 printf("[%s] 0x%08x AES256-CTS-HMAC-SHA1-96\n",
2975 enctypes
& ENC_HMAC_SHA1_96_AES256
? "X" : " ",
2976 ENC_HMAC_SHA1_96_AES256
);
2979 static int net_ads_enctypes_list(struct net_context
*c
, int argc
, const char **argv
)
2983 ADS_STRUCT
*ads
= NULL
;
2984 LDAPMessage
*res
= NULL
;
2985 const char *str
= NULL
;
2987 if (c
->display_usage
|| (argc
< 1)) {
2989 "net ads enctypes list\n"
2992 _("List supported enctypes"));
2996 status
= ads_startup(c
, false, &ads
);
2997 if (!ADS_ERR_OK(status
)) {
2998 printf("startup failed\n");
3002 ret
= net_ads_enctype_lookup_account(c
, ads
, argv
[0], &res
, &str
);
3007 net_ads_enctype_dump_enctypes(argv
[0], str
);
3011 ads_msgfree(ads
, res
);
3017 static int net_ads_enctypes_set(struct net_context
*c
, int argc
, const char **argv
)
3022 LDAPMessage
*res
= NULL
;
3023 const char *etype_list_str
;
3026 uint32_t etype_list
;
3029 if (c
->display_usage
|| argc
< 1) {
3031 "net ads enctypes set <sAMAccountName> [enctypes]\n"
3034 _("Set supported enctypes"));
3038 status
= ads_startup(c
, false, &ads
);
3039 if (!ADS_ERR_OK(status
)) {
3040 printf("startup failed\n");
3044 ret
= net_ads_enctype_lookup_account(c
, ads
, argv
[0], &res
, NULL
);
3049 dn
= ads_get_dn(ads
, c
, res
);
3054 etype_list
= ENC_CRC32
| ENC_RSA_MD5
| ENC_RC4_HMAC_MD5
;
3055 #ifdef HAVE_ENCTYPE_AES128_CTS_HMAC_SHA1_96
3056 etype_list
|= ENC_HMAC_SHA1_96_AES128
;
3058 #ifdef HAVE_ENCTYPE_AES256_CTS_HMAC_SHA1_96
3059 etype_list
|= ENC_HMAC_SHA1_96_AES256
;
3062 if (argv
[1] != NULL
) {
3063 sscanf(argv
[1], "%i", &etype_list
);
3066 etype_list_str
= talloc_asprintf(c
, "%d", etype_list
);
3067 if (!etype_list_str
) {
3071 mods
= ads_init_mods(c
);
3076 status
= ads_mod_str(c
, &mods
, "msDS-SupportedEncryptionTypes",
3078 if (!ADS_ERR_OK(status
)) {
3082 status
= ads_gen_mod(ads
, dn
, mods
);
3083 if (!ADS_ERR_OK(status
)) {
3084 d_printf(_("failed to add msDS-SupportedEncryptionTypes: %s\n"),
3085 ads_errstr(status
));
3089 ads_msgfree(ads
, res
);
3091 ret
= net_ads_enctype_lookup_account(c
, ads
, argv
[0], &res
, &str
);
3096 net_ads_enctype_dump_enctypes(argv
[0], str
);
3100 ads_msgfree(ads
, res
);
3106 static int net_ads_enctypes_delete(struct net_context
*c
, int argc
, const char **argv
)
3111 LDAPMessage
*res
= NULL
;
3115 if (c
->display_usage
|| argc
< 1) {
3117 "net ads enctypes delete <sAMAccountName>\n"
3120 _("Delete supported enctypes"));
3124 status
= ads_startup(c
, false, &ads
);
3125 if (!ADS_ERR_OK(status
)) {
3126 printf("startup failed\n");
3130 ret
= net_ads_enctype_lookup_account(c
, ads
, argv
[0], &res
, NULL
);
3135 dn
= ads_get_dn(ads
, c
, res
);
3140 mods
= ads_init_mods(c
);
3145 status
= ads_mod_str(c
, &mods
, "msDS-SupportedEncryptionTypes", NULL
);
3146 if (!ADS_ERR_OK(status
)) {
3150 status
= ads_gen_mod(ads
, dn
, mods
);
3151 if (!ADS_ERR_OK(status
)) {
3152 d_printf(_("failed to remove msDS-SupportedEncryptionTypes: %s\n"),
3153 ads_errstr(status
));
3160 ads_msgfree(ads
, res
);
3165 static int net_ads_enctypes(struct net_context
*c
, int argc
, const char **argv
)
3167 struct functable func
[] = {
3170 net_ads_enctypes_list
,
3172 N_("List the supported encryption types"),
3173 N_("net ads enctypes list\n"
3174 " List the supported encryption types")
3178 net_ads_enctypes_set
,
3180 N_("Set the supported encryption types"),
3181 N_("net ads enctypes set\n"
3182 " Set the supported encryption types")
3186 net_ads_enctypes_delete
,
3188 N_("Delete the supported encryption types"),
3189 N_("net ads enctypes delete\n"
3190 " Delete the supported encryption types")
3193 {NULL
, NULL
, 0, NULL
, NULL
}
3196 return net_run_function(c
, argc
, argv
, "net ads enctypes", func
);
3200 int net_ads(struct net_context
*c
, int argc
, const char **argv
)
3202 struct functable func
[] = {
3207 N_("Display details on remote ADS server"),
3209 " Display details on remote ADS server")
3215 N_("Join the local machine to ADS realm"),
3217 " Join the local machine to ADS realm")
3223 N_("Validate machine account"),
3224 N_("net ads testjoin\n"
3225 " Validate machine account")
3231 N_("Remove the local machine from ADS"),
3232 N_("net ads leave\n"
3233 " Remove the local machine from ADS")
3239 N_("Display machine account details"),
3240 N_("net ads status\n"
3241 " Display machine account details")
3247 N_("List/modify users"),
3249 " List/modify users")
3255 N_("List/modify groups"),
3256 N_("net ads group\n"
3257 " List/modify groups")
3263 N_("Issue dynamic DNS update"),
3265 " Issue dynamic DNS update")
3271 N_("Change user passwords"),
3272 N_("net ads password\n"
3273 " Change user passwords")
3277 net_ads_changetrustpw
,
3279 N_("Change trust account password"),
3280 N_("net ads changetrustpw\n"
3281 " Change trust account password")
3287 N_("List/modify printer entries"),
3288 N_("net ads printer\n"
3289 " List/modify printer entries")
3295 N_("Issue LDAP search using filter"),
3296 N_("net ads search\n"
3297 " Issue LDAP search using filter")
3303 N_("Issue LDAP search by DN"),
3305 " Issue LDAP search by DN")
3311 N_("Issue LDAP search by SID"),
3313 " Issue LDAP search by SID")
3319 N_("Display workgroup name"),
3320 N_("net ads workgroup\n"
3321 " Display the workgroup name")
3327 N_("Perfom CLDAP query on DC"),
3328 N_("net ads lookup\n"
3329 " Find the ADS DC using CLDAP lookups")
3335 N_("Manage local keytab file"),
3336 N_("net ads keytab\n"
3337 " Manage local keytab file")
3343 N_("Manage group policy objects"),
3345 " Manage group policy objects")
3351 N_("Manage kerberos keytab"),
3352 N_("net ads kerberos\n"
3353 " Manage kerberos keytab")
3359 N_("List/modify supported encryption types"),
3360 N_("net ads enctypes\n"
3361 " List/modify enctypes")
3363 {NULL
, NULL
, 0, NULL
, NULL
}
3366 return net_run_function(c
, argc
, argv
, "net ads", func
);
3371 static int net_ads_noads(void)
3373 d_fprintf(stderr
, _("ADS support not compiled in\n"));
3377 int net_ads_keytab(struct net_context
*c
, int argc
, const char **argv
)
3379 return net_ads_noads();
3382 int net_ads_kerberos(struct net_context
*c
, int argc
, const char **argv
)
3384 return net_ads_noads();
3387 int net_ads_changetrustpw(struct net_context
*c
, int argc
, const char **argv
)
3389 return net_ads_noads();
3392 int net_ads_join(struct net_context
*c
, int argc
, const char **argv
)
3394 return net_ads_noads();
3397 int net_ads_user(struct net_context
*c
, int argc
, const char **argv
)
3399 return net_ads_noads();
3402 int net_ads_group(struct net_context
*c
, int argc
, const char **argv
)
3404 return net_ads_noads();
3407 int net_ads_gpo(struct net_context
*c
, int argc
, const char **argv
)
3409 return net_ads_noads();
3412 /* this one shouldn't display a message */
3413 int net_ads_check(struct net_context
*c
)
3418 int net_ads_check_our_domain(struct net_context
*c
)
3423 int net_ads(struct net_context
*c
, int argc
, const char **argv
)
3425 return net_ads_noads();
3428 #endif /* HAVE_ADS */