2 Samba Unix/Linux SMB client library
5 Copyright (C) 2001 Andrew Tridgell (tridge@samba.org)
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 int net_ads_usage(int argc, const char **argv)
29 "\nnet ads join <org_unit>"\
30 "\n\tjoins the local machine to a ADS realm\n"\
32 "\n\tremoves the local machine from a ADS realm\n"\
34 "\n\tlist users in the realm\n"\
36 "\n\tlist groups in the realm\n"\
38 "\n\tshows some info on the server\n"\
40 "\n\tdump the machine account details to stdout\n"
46 static int net_ads_info(int argc, const char **argv)
50 ads = ads_init(NULL, NULL, NULL, NULL);
54 d_printf("Didn't find the ldap server!\n");
58 d_printf("LDAP server: %s\n", ads->ldap_server);
59 d_printf("LDAP server name: %s\n", ads->ldap_server_name);
60 d_printf("Realm: %s\n", ads->realm);
61 d_printf("Bind Path: %s\n", ads->bind_path);
62 d_printf("LDAP port: %d\n", ads->ldap_port);
68 static ADS_STRUCT *ads_startup(void)
72 extern char *opt_password;
73 extern char *opt_user_name;
75 ads = ads_init(NULL, NULL, NULL, NULL);
78 opt_user_name = "administrator";
83 asprintf(&prompt,"%s password: ", opt_user_name);
84 opt_password = getpass(prompt);
87 ads->password = strdup(opt_password);
88 ads->user_name = strdup(opt_user_name);
90 rc = ads_connect(ads);
92 d_printf("ads_connect: %s\n", ads_errstr(rc));
98 static int net_ads_user(int argc, const char **argv)
103 const char *attrs[] = {"sAMAccountName", "name", "objectSid", NULL};
105 if (!(ads = ads_startup())) return -1;
106 rc = ads_search(ads, &res, "(objectclass=user)", attrs);
108 d_printf("ads_search: %s\n", ads_errstr(rc));
112 if (ads_count_replies(ads, res) == 0) {
113 d_printf("No users found\n");
122 static int net_ads_group(int argc, const char **argv)
127 const char *attrs[] = {"sAMAccountName", "name", "objectSid", NULL};
129 if (!(ads = ads_startup())) return -1;
130 rc = ads_search(ads, &res, "(objectclass=group)", attrs);
132 d_printf("ads_search: %s\n", ads_errstr(rc));
136 if (ads_count_replies(ads, res) == 0) {
137 d_printf("No groups found\n");
145 static int net_ads_status(int argc, const char **argv)
149 extern pstring global_myname;
152 if (!(ads = ads_startup())) return -1;
154 rc = ads_find_machine_acct(ads, &res, global_myname);
156 d_printf("ads_find_machine_acct: %s\n", ads_errstr(rc));
160 if (ads_count_replies(ads, res) == 0) {
161 d_printf("No machine account for '%s' found\n", global_myname);
170 static int net_ads_leave(int argc, const char **argv)
172 ADS_STRUCT *ads = NULL;
174 extern pstring global_myname;
176 if (!(ads = ads_startup())) {
180 if (!secrets_init()) {
181 DEBUG(1,("Failed to initialise secrets database\n"));
185 rc = ads_leave_realm(ads, global_myname);
187 d_printf("Failed to delete host '%s' from the '%s' realm.\n",
188 global_myname, ads->realm);
192 d_printf("Removed '%s' from realm '%s'\n", global_myname, ads->realm);
197 static int net_ads_join(int argc, const char **argv)
203 extern pstring global_myname;
205 const char *org_unit = "Computers";
209 if (argc > 0) org_unit = argv[0];
211 if (!secrets_init()) {
212 DEBUG(1,("Failed to initialise secrets database\n"));
216 tmp_password = generate_random_str(DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
217 password = strdup(tmp_password);
219 if (!(ads = ads_startup())) return -1;
221 asprintf(&dn, "cn=%s,%s", org_unit, ads->bind_path);
223 rc = ads_search_dn(ads, &res, dn, NULL);
225 ads_msgfree(ads, res);
227 if (rc == LDAP_NO_SUCH_OBJECT) {
228 d_printf("ads_join_realm: organisational unit %s does not exist\n", org_unit);
233 d_printf("ads_join_realm: %s\n", ads_errstr(rc));
237 rc = ads_join_realm(ads, global_myname, org_unit);
239 d_printf("ads_join_realm: %s\n", ads_errstr(rc));
243 status = ads_set_machine_password(ads, global_myname, password);
244 if (!NT_STATUS_IS_OK(status)) {
245 d_printf("ads_set_machine_password: %s\n", get_nt_error_msg(status));
249 if (!secrets_store_machine_password(password)) {
250 DEBUG(1,("Failed to save machine password\n"));
254 d_printf("Joined '%s' to realm '%s'\n", global_myname, ads->realm);
261 int net_ads(int argc, const char **argv)
263 struct functable func[] = {
264 {"INFO", net_ads_info},
265 {"JOIN", net_ads_join},
266 {"LEAVE", net_ads_leave},
267 {"STATUS", net_ads_status},
268 {"USER", net_ads_user},
269 {"GROUP", net_ads_group},
273 return net_run_function(argc, argv, func, net_ads_usage);
278 int net_ads_usage(int argc, const char **argv)
280 d_printf("ADS support not compiled in\n");
284 int net_ads(int argc, const char **argv)
286 return net_ads_usage(argc, argv);