s3: Rename new parameter "ldap ref follow" to "ldap follow referral".
[Samba/gebeck_regimport.git] / source3 / utils / net_dom.c
blob433fb6c671c452d6b022674da47288c36f0c1b20
1 /*
2 Samba Unix/Linux SMB client library
3 net dom commands for remote join/unjoin
4 Copyright (C) 2007,2009 Günther Deschner
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/>.
20 #include "includes.h"
21 #include "utils/net.h"
23 int net_dom_usage(struct net_context *c, int argc, const char **argv)
25 d_printf(_("usage: net dom join "
26 "<domain=DOMAIN> <ou=OU> <account=ACCOUNT> "
27 "<password=PASSWORD> <reboot>\n Join a remote machine\n"));
28 d_printf(_("usage: net dom unjoin "
29 "<account=ACCOUNT> <password=PASSWORD> <reboot>\n"
30 " Unjoin a remote machine\n"));
31 d_printf(_("usage: net dom renamecomputer "
32 "<newname=NEWNAME> "
33 "<account=ACCOUNT> <password=PASSWORD> <reboot>\n"
34 " Rename joined computer\n"));
36 return -1;
39 static int net_dom_unjoin(struct net_context *c, int argc, const char **argv)
41 const char *server_name = NULL;
42 const char *account = NULL;
43 const char *password = NULL;
44 uint32_t unjoin_flags = NETSETUP_ACCT_DELETE |
45 NETSETUP_JOIN_DOMAIN |
46 NETSETUP_IGNORE_UNSUPPORTED_FLAGS;
47 struct cli_state *cli = NULL;
48 bool do_reboot = false;
49 NTSTATUS ntstatus;
50 NET_API_STATUS status;
51 int ret = -1;
52 int i;
54 if (argc < 1 || c->display_usage) {
55 return net_dom_usage(c, argc, argv);
58 if (c->opt_host) {
59 server_name = c->opt_host;
62 for (i=0; i<argc; i++) {
63 if (strnequal(argv[i], "account", strlen("account"))) {
64 account = get_string_param(argv[i]);
65 if (!account) {
66 return -1;
69 if (strnequal(argv[i], "password", strlen("password"))) {
70 password = get_string_param(argv[i]);
71 if (!password) {
72 return -1;
75 if (strequal(argv[i], "reboot")) {
76 do_reboot = true;
80 if (do_reboot) {
81 ntstatus = net_make_ipc_connection_ex(c, c->opt_workgroup,
82 server_name, NULL, 0,
83 &cli);
84 if (!NT_STATUS_IS_OK(ntstatus)) {
85 return -1;
89 status = NetUnjoinDomain(server_name, account, password, unjoin_flags);
90 if (status != 0) {
91 printf(_("Failed to unjoin domain: %s\n"),
92 libnetapi_get_error_string(c->netapi_ctx, status));
93 goto done;
96 if (do_reboot) {
97 c->opt_comment = _("Shutting down due to a domain membership "
98 "change");
99 c->opt_reboot = true;
100 c->opt_timeout = 30;
102 ret = run_rpc_command(c, cli,
103 &ndr_table_initshutdown.syntax_id,
104 0, rpc_init_shutdown_internals,
105 argc, argv);
106 if (ret == 0) {
107 goto done;
110 ret = run_rpc_command(c, cli, &ndr_table_winreg.syntax_id, 0,
111 rpc_reg_shutdown_internals,
112 argc, argv);
113 goto done;
116 ret = 0;
118 done:
119 if (cli) {
120 cli_shutdown(cli);
123 return ret;
126 static int net_dom_join(struct net_context *c, int argc, const char **argv)
128 const char *server_name = NULL;
129 const char *domain_name = NULL;
130 const char *account_ou = NULL;
131 const char *Account = NULL;
132 const char *password = NULL;
133 uint32_t join_flags = NETSETUP_ACCT_CREATE |
134 NETSETUP_JOIN_DOMAIN;
135 struct cli_state *cli = NULL;
136 bool do_reboot = false;
137 NTSTATUS ntstatus;
138 NET_API_STATUS status;
139 int ret = -1;
140 int i;
142 if (argc < 1 || c->display_usage) {
143 return net_dom_usage(c, argc, argv);
146 if (c->opt_host) {
147 server_name = c->opt_host;
150 if (c->opt_force) {
151 join_flags |= NETSETUP_DOMAIN_JOIN_IF_JOINED;
154 for (i=0; i<argc; i++) {
155 if (strnequal(argv[i], "ou", strlen("ou"))) {
156 account_ou = get_string_param(argv[i]);
157 if (!account_ou) {
158 return -1;
161 if (strnequal(argv[i], "domain", strlen("domain"))) {
162 domain_name = get_string_param(argv[i]);
163 if (!domain_name) {
164 return -1;
167 if (strnequal(argv[i], "account", strlen("account"))) {
168 Account = get_string_param(argv[i]);
169 if (!Account) {
170 return -1;
173 if (strnequal(argv[i], "password", strlen("password"))) {
174 password = get_string_param(argv[i]);
175 if (!password) {
176 return -1;
179 if (strequal(argv[i], "reboot")) {
180 do_reboot = true;
184 if (do_reboot) {
185 ntstatus = net_make_ipc_connection_ex(c, c->opt_workgroup,
186 server_name, NULL, 0,
187 &cli);
188 if (!NT_STATUS_IS_OK(ntstatus)) {
189 return -1;
193 /* check if domain is a domain or a workgroup */
195 status = NetJoinDomain(server_name, domain_name, account_ou,
196 Account, password, join_flags);
197 if (status != 0) {
198 printf(_("Failed to join domain: %s\n"),
199 libnetapi_get_error_string(c->netapi_ctx, status));
200 goto done;
203 if (do_reboot) {
204 c->opt_comment = _("Shutting down due to a domain membership "
205 "change");
206 c->opt_reboot = true;
207 c->opt_timeout = 30;
209 ret = run_rpc_command(c, cli, &ndr_table_initshutdown.syntax_id, 0,
210 rpc_init_shutdown_internals,
211 argc, argv);
212 if (ret == 0) {
213 goto done;
216 ret = run_rpc_command(c, cli, &ndr_table_winreg.syntax_id, 0,
217 rpc_reg_shutdown_internals,
218 argc, argv);
219 goto done;
222 ret = 0;
224 done:
225 if (cli) {
226 cli_shutdown(cli);
229 return ret;
232 static int net_dom_renamecomputer(struct net_context *c, int argc, const char **argv)
234 const char *server_name = NULL;
235 const char *account = NULL;
236 const char *password = NULL;
237 const char *newname = NULL;
238 uint32_t rename_options = NETSETUP_ACCT_CREATE;
239 struct cli_state *cli = NULL;
240 bool do_reboot = false;
241 NTSTATUS ntstatus;
242 NET_API_STATUS status;
243 int ret = -1;
244 int i;
246 if (argc < 1 || c->display_usage) {
247 return net_dom_usage(c, argc, argv);
250 if (c->opt_host) {
251 server_name = c->opt_host;
254 for (i=0; i<argc; i++) {
255 if (strnequal(argv[i], "account", strlen("account"))) {
256 account = get_string_param(argv[i]);
257 if (!account) {
258 return -1;
261 if (strnequal(argv[i], "password", strlen("password"))) {
262 password = get_string_param(argv[i]);
263 if (!password) {
264 return -1;
267 if (strnequal(argv[i], "newname", strlen("newname"))) {
268 newname = get_string_param(argv[i]);
269 if (!newname) {
270 return -1;
273 if (strequal(argv[i], "reboot")) {
274 do_reboot = true;
278 if (do_reboot) {
279 ntstatus = net_make_ipc_connection_ex(c, c->opt_workgroup,
280 server_name, NULL, 0,
281 &cli);
282 if (!NT_STATUS_IS_OK(ntstatus)) {
283 return -1;
287 status = NetRenameMachineInDomain(server_name, newname,
288 account, password, rename_options);
289 if (status != 0) {
290 printf(_("Failed to rename machine: "));
291 if (status == W_ERROR_V(WERR_SETUP_NOT_JOINED)) {
292 printf(_("Computer is not joined to a Domain\n"));
293 goto done;
295 printf("%s\n",
296 libnetapi_get_error_string(c->netapi_ctx, status));
297 goto done;
300 if (do_reboot) {
301 c->opt_comment = _("Shutting down due to a computer rename");
302 c->opt_reboot = true;
303 c->opt_timeout = 30;
305 ret = run_rpc_command(c, cli,
306 &ndr_table_initshutdown.syntax_id,
307 0, rpc_init_shutdown_internals,
308 argc, argv);
309 if (ret == 0) {
310 goto done;
313 ret = run_rpc_command(c, cli, &ndr_table_winreg.syntax_id, 0,
314 rpc_reg_shutdown_internals,
315 argc, argv);
316 goto done;
319 ret = 0;
321 done:
322 if (cli) {
323 cli_shutdown(cli);
326 return ret;
329 int net_dom(struct net_context *c, int argc, const char **argv)
331 NET_API_STATUS status;
333 struct functable func[] = {
335 "join",
336 net_dom_join,
337 NET_TRANSPORT_LOCAL,
338 N_("Join a remote machine"),
339 N_("net dom join <domain=DOMAIN> <ou=OU> "
340 "<account=ACCOUNT> <password=PASSWORD> <reboot>\n"
341 " Join a remote machine")
344 "unjoin",
345 net_dom_unjoin,
346 NET_TRANSPORT_LOCAL,
347 N_("Unjoin a remote machine"),
348 N_("net dom unjoin <account=ACCOUNT> "
349 "<password=PASSWORD> <reboot>\n"
350 " Unjoin a remote machine")
353 "renamecomputer",
354 net_dom_renamecomputer,
355 NET_TRANSPORT_LOCAL,
356 N_("Rename a computer that is joined to a domain"),
357 N_("net dom renamecomputer <newname=NEWNAME> "
358 "<account=ACCOUNT> <password=PASSWORD> "
359 "<reboot>\n"
360 " Rename joined computer")
363 {NULL, NULL, 0, NULL, NULL}
366 status = libnetapi_init(&c->netapi_ctx);
367 if (status != 0) {
368 return -1;
371 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
372 libnetapi_set_password(c->netapi_ctx, c->opt_password);
373 if (c->opt_kerberos) {
374 libnetapi_set_use_kerberos(c->netapi_ctx);
377 return net_run_function(c, argc, argv, "net dom", func);