librpc/ndr: remove 'async' from ndr_interface_call
[Samba/gebeck_regimport.git] / source3 / utils / net_dom.c
blobd07a1d4f04d5f0f2414581c27b6dbaf2cc1e21d9
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"
22 #include "../librpc/gen_ndr/cli_initshutdown.h"
23 #include "../librpc/gen_ndr/ndr_winreg.h"
24 #include "lib/netapi/netapi.h"
26 int net_dom_usage(struct net_context *c, int argc, const char **argv)
28 d_printf("%s\n%s",
29 _("Usage:"),
30 _("net dom join "
31 "<domain=DOMAIN> <ou=OU> <account=ACCOUNT> "
32 "<password=PASSWORD> <reboot>\n Join a remote machine\n"));
33 d_printf("%s\n%s",
34 _("Usage:"),
35 _("net dom unjoin "
36 "<account=ACCOUNT> <password=PASSWORD> <reboot>\n"
37 " Unjoin a remote machine\n"));
38 d_printf("%s\n%s",
39 _("Usage:"),
40 _("net dom renamecomputer "
41 "<newname=NEWNAME> "
42 "<account=ACCOUNT> <password=PASSWORD> <reboot>\n"
43 " Rename joined computer\n"));
45 return -1;
48 static int net_dom_unjoin(struct net_context *c, int argc, const char **argv)
50 const char *server_name = NULL;
51 const char *account = NULL;
52 const char *password = NULL;
53 uint32_t unjoin_flags = NETSETUP_ACCT_DELETE |
54 NETSETUP_JOIN_DOMAIN |
55 NETSETUP_IGNORE_UNSUPPORTED_FLAGS;
56 struct cli_state *cli = NULL;
57 bool do_reboot = false;
58 NTSTATUS ntstatus;
59 NET_API_STATUS status;
60 int ret = -1;
61 int i;
63 if (argc < 1 || c->display_usage) {
64 return net_dom_usage(c, argc, argv);
67 if (c->opt_host) {
68 server_name = c->opt_host;
71 for (i=0; i<argc; i++) {
72 if (strnequal(argv[i], "account", strlen("account"))) {
73 account = get_string_param(argv[i]);
74 if (!account) {
75 return -1;
78 if (strnequal(argv[i], "password", strlen("password"))) {
79 password = get_string_param(argv[i]);
80 if (!password) {
81 return -1;
84 if (strequal(argv[i], "reboot")) {
85 do_reboot = true;
89 if (do_reboot) {
90 ntstatus = net_make_ipc_connection_ex(c, c->opt_workgroup,
91 server_name, NULL, 0,
92 &cli);
93 if (!NT_STATUS_IS_OK(ntstatus)) {
94 return -1;
98 status = NetUnjoinDomain(server_name, account, password, unjoin_flags);
99 if (status != 0) {
100 printf(_("Failed to unjoin domain: %s\n"),
101 libnetapi_get_error_string(c->netapi_ctx, status));
102 goto done;
105 if (do_reboot) {
106 c->opt_comment = _("Shutting down due to a domain membership "
107 "change");
108 c->opt_reboot = true;
109 c->opt_timeout = 30;
111 ret = run_rpc_command(c, cli,
112 &ndr_table_initshutdown.syntax_id,
113 0, rpc_init_shutdown_internals,
114 argc, argv);
115 if (ret == 0) {
116 goto done;
119 ret = run_rpc_command(c, cli, &ndr_table_winreg.syntax_id, 0,
120 rpc_reg_shutdown_internals,
121 argc, argv);
122 goto done;
125 ret = 0;
127 done:
128 if (cli) {
129 cli_shutdown(cli);
132 return ret;
135 static int net_dom_join(struct net_context *c, int argc, const char **argv)
137 const char *server_name = NULL;
138 const char *domain_name = NULL;
139 const char *account_ou = NULL;
140 const char *Account = NULL;
141 const char *password = NULL;
142 uint32_t join_flags = NETSETUP_ACCT_CREATE |
143 NETSETUP_JOIN_DOMAIN;
144 struct cli_state *cli = NULL;
145 bool do_reboot = false;
146 NTSTATUS ntstatus;
147 NET_API_STATUS status;
148 int ret = -1;
149 int i;
151 if (argc < 1 || c->display_usage) {
152 return net_dom_usage(c, argc, argv);
155 if (c->opt_host) {
156 server_name = c->opt_host;
159 if (c->opt_force) {
160 join_flags |= NETSETUP_DOMAIN_JOIN_IF_JOINED;
163 for (i=0; i<argc; i++) {
164 if (strnequal(argv[i], "ou", strlen("ou"))) {
165 account_ou = get_string_param(argv[i]);
166 if (!account_ou) {
167 return -1;
170 if (strnequal(argv[i], "domain", strlen("domain"))) {
171 domain_name = get_string_param(argv[i]);
172 if (!domain_name) {
173 return -1;
176 if (strnequal(argv[i], "account", strlen("account"))) {
177 Account = get_string_param(argv[i]);
178 if (!Account) {
179 return -1;
182 if (strnequal(argv[i], "password", strlen("password"))) {
183 password = get_string_param(argv[i]);
184 if (!password) {
185 return -1;
188 if (strequal(argv[i], "reboot")) {
189 do_reboot = true;
193 if (do_reboot) {
194 ntstatus = net_make_ipc_connection_ex(c, c->opt_workgroup,
195 server_name, NULL, 0,
196 &cli);
197 if (!NT_STATUS_IS_OK(ntstatus)) {
198 return -1;
202 /* check if domain is a domain or a workgroup */
204 status = NetJoinDomain(server_name, domain_name, account_ou,
205 Account, password, join_flags);
206 if (status != 0) {
207 printf(_("Failed to join domain: %s\n"),
208 libnetapi_get_error_string(c->netapi_ctx, status));
209 goto done;
212 if (do_reboot) {
213 c->opt_comment = _("Shutting down due to a domain membership "
214 "change");
215 c->opt_reboot = true;
216 c->opt_timeout = 30;
218 ret = run_rpc_command(c, cli, &ndr_table_initshutdown.syntax_id, 0,
219 rpc_init_shutdown_internals,
220 argc, argv);
221 if (ret == 0) {
222 goto done;
225 ret = run_rpc_command(c, cli, &ndr_table_winreg.syntax_id, 0,
226 rpc_reg_shutdown_internals,
227 argc, argv);
228 goto done;
231 ret = 0;
233 done:
234 if (cli) {
235 cli_shutdown(cli);
238 return ret;
241 static int net_dom_renamecomputer(struct net_context *c, int argc, const char **argv)
243 const char *server_name = NULL;
244 const char *account = NULL;
245 const char *password = NULL;
246 const char *newname = NULL;
247 uint32_t rename_options = NETSETUP_ACCT_CREATE;
248 struct cli_state *cli = NULL;
249 bool do_reboot = false;
250 NTSTATUS ntstatus;
251 NET_API_STATUS status;
252 int ret = -1;
253 int i;
255 if (argc < 1 || c->display_usage) {
256 return net_dom_usage(c, argc, argv);
259 if (c->opt_host) {
260 server_name = c->opt_host;
263 for (i=0; i<argc; i++) {
264 if (strnequal(argv[i], "account", strlen("account"))) {
265 account = get_string_param(argv[i]);
266 if (!account) {
267 return -1;
270 if (strnequal(argv[i], "password", strlen("password"))) {
271 password = get_string_param(argv[i]);
272 if (!password) {
273 return -1;
276 if (strnequal(argv[i], "newname", strlen("newname"))) {
277 newname = get_string_param(argv[i]);
278 if (!newname) {
279 return -1;
282 if (strequal(argv[i], "reboot")) {
283 do_reboot = true;
287 if (do_reboot) {
288 ntstatus = net_make_ipc_connection_ex(c, c->opt_workgroup,
289 server_name, NULL, 0,
290 &cli);
291 if (!NT_STATUS_IS_OK(ntstatus)) {
292 return -1;
296 status = NetRenameMachineInDomain(server_name, newname,
297 account, password, rename_options);
298 if (status != 0) {
299 printf(_("Failed to rename machine: "));
300 if (status == W_ERROR_V(WERR_SETUP_NOT_JOINED)) {
301 printf(_("Computer is not joined to a Domain\n"));
302 goto done;
304 printf("%s\n",
305 libnetapi_get_error_string(c->netapi_ctx, status));
306 goto done;
309 if (do_reboot) {
310 c->opt_comment = _("Shutting down due to a computer rename");
311 c->opt_reboot = true;
312 c->opt_timeout = 30;
314 ret = run_rpc_command(c, cli,
315 &ndr_table_initshutdown.syntax_id,
316 0, rpc_init_shutdown_internals,
317 argc, argv);
318 if (ret == 0) {
319 goto done;
322 ret = run_rpc_command(c, cli, &ndr_table_winreg.syntax_id, 0,
323 rpc_reg_shutdown_internals,
324 argc, argv);
325 goto done;
328 ret = 0;
330 done:
331 if (cli) {
332 cli_shutdown(cli);
335 return ret;
338 int net_dom(struct net_context *c, int argc, const char **argv)
340 NET_API_STATUS status;
342 struct functable func[] = {
344 "join",
345 net_dom_join,
346 NET_TRANSPORT_LOCAL,
347 N_("Join a remote machine"),
348 N_("net dom join <domain=DOMAIN> <ou=OU> "
349 "<account=ACCOUNT> <password=PASSWORD> <reboot>\n"
350 " Join a remote machine")
353 "unjoin",
354 net_dom_unjoin,
355 NET_TRANSPORT_LOCAL,
356 N_("Unjoin a remote machine"),
357 N_("net dom unjoin <account=ACCOUNT> "
358 "<password=PASSWORD> <reboot>\n"
359 " Unjoin a remote machine")
362 "renamecomputer",
363 net_dom_renamecomputer,
364 NET_TRANSPORT_LOCAL,
365 N_("Rename a computer that is joined to a domain"),
366 N_("net dom renamecomputer <newname=NEWNAME> "
367 "<account=ACCOUNT> <password=PASSWORD> "
368 "<reboot>\n"
369 " Rename joined computer")
372 {NULL, NULL, 0, NULL, NULL}
375 status = libnetapi_init(&c->netapi_ctx);
376 if (status != 0) {
377 return -1;
380 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
381 libnetapi_set_password(c->netapi_ctx, c->opt_password);
382 if (c->opt_kerberos) {
383 libnetapi_set_use_kerberos(c->netapi_ctx);
386 return net_run_function(c, argc, argv, "net dom", func);