s4-python: Add missing prototypes.
[Samba.git] / source3 / utils / net_dom.c
blobd1eb9edba82e3a27f115eea6d2ca9834ae3a5cf1
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"
25 #include "lib/netapi/netapi_net.h"
27 int net_dom_usage(struct net_context *c, int argc, const char **argv)
29 d_printf("%s\n%s",
30 _("Usage:"),
31 _("net dom join "
32 "<domain=DOMAIN> <ou=OU> <account=ACCOUNT> "
33 "<password=PASSWORD> <reboot>\n Join a remote machine\n"));
34 d_printf("%s\n%s",
35 _("Usage:"),
36 _("net dom unjoin "
37 "<account=ACCOUNT> <password=PASSWORD> <reboot>\n"
38 " Unjoin a remote machine\n"));
39 d_printf("%s\n%s",
40 _("Usage:"),
41 _("net dom renamecomputer "
42 "<newname=NEWNAME> "
43 "<account=ACCOUNT> <password=PASSWORD> <reboot>\n"
44 " Rename joined computer\n"));
46 return -1;
49 static int net_dom_unjoin(struct net_context *c, int argc, const char **argv)
51 const char *server_name = NULL;
52 const char *account = NULL;
53 const char *password = NULL;
54 uint32_t unjoin_flags = NETSETUP_ACCT_DELETE |
55 NETSETUP_JOIN_DOMAIN |
56 NETSETUP_IGNORE_UNSUPPORTED_FLAGS;
57 struct cli_state *cli = NULL;
58 bool do_reboot = false;
59 NTSTATUS ntstatus;
60 NET_API_STATUS status;
61 int ret = -1;
62 int i;
64 if (argc < 1 || c->display_usage) {
65 return net_dom_usage(c, argc, argv);
68 if (c->opt_host) {
69 server_name = c->opt_host;
72 for (i=0; i<argc; i++) {
73 if (strnequal(argv[i], "account", strlen("account"))) {
74 account = get_string_param(argv[i]);
75 if (!account) {
76 return -1;
79 if (strnequal(argv[i], "password", strlen("password"))) {
80 password = get_string_param(argv[i]);
81 if (!password) {
82 return -1;
85 if (strequal(argv[i], "reboot")) {
86 do_reboot = true;
90 if (do_reboot) {
91 ntstatus = net_make_ipc_connection_ex(c, c->opt_workgroup,
92 server_name, NULL, 0,
93 &cli);
94 if (!NT_STATUS_IS_OK(ntstatus)) {
95 return -1;
99 status = NetUnjoinDomain(server_name, account, password, unjoin_flags);
100 if (status != 0) {
101 printf(_("Failed to unjoin domain: %s\n"),
102 libnetapi_get_error_string(c->netapi_ctx, status));
103 goto done;
106 if (do_reboot) {
107 c->opt_comment = _("Shutting down due to a domain membership "
108 "change");
109 c->opt_reboot = true;
110 c->opt_timeout = 30;
112 ret = run_rpc_command(c, cli,
113 &ndr_table_initshutdown.syntax_id,
114 0, rpc_init_shutdown_internals,
115 argc, argv);
116 if (ret == 0) {
117 goto done;
120 ret = run_rpc_command(c, cli, &ndr_table_winreg.syntax_id, 0,
121 rpc_reg_shutdown_internals,
122 argc, argv);
123 goto done;
126 ret = 0;
128 done:
129 if (cli) {
130 cli_shutdown(cli);
133 return ret;
136 static int net_dom_join(struct net_context *c, int argc, const char **argv)
138 const char *server_name = NULL;
139 const char *domain_name = NULL;
140 const char *account_ou = NULL;
141 const char *Account = NULL;
142 const char *password = NULL;
143 uint32_t join_flags = NETSETUP_ACCT_CREATE |
144 NETSETUP_JOIN_DOMAIN;
145 struct cli_state *cli = NULL;
146 bool do_reboot = false;
147 NTSTATUS ntstatus;
148 NET_API_STATUS status;
149 int ret = -1;
150 int i;
152 if (argc < 1 || c->display_usage) {
153 return net_dom_usage(c, argc, argv);
156 if (c->opt_host) {
157 server_name = c->opt_host;
160 if (c->opt_force) {
161 join_flags |= NETSETUP_DOMAIN_JOIN_IF_JOINED;
164 for (i=0; i<argc; i++) {
165 if (strnequal(argv[i], "ou", strlen("ou"))) {
166 account_ou = get_string_param(argv[i]);
167 if (!account_ou) {
168 return -1;
171 if (strnequal(argv[i], "domain", strlen("domain"))) {
172 domain_name = get_string_param(argv[i]);
173 if (!domain_name) {
174 return -1;
177 if (strnequal(argv[i], "account", strlen("account"))) {
178 Account = get_string_param(argv[i]);
179 if (!Account) {
180 return -1;
183 if (strnequal(argv[i], "password", strlen("password"))) {
184 password = get_string_param(argv[i]);
185 if (!password) {
186 return -1;
189 if (strequal(argv[i], "reboot")) {
190 do_reboot = true;
194 if (do_reboot) {
195 ntstatus = net_make_ipc_connection_ex(c, c->opt_workgroup,
196 server_name, NULL, 0,
197 &cli);
198 if (!NT_STATUS_IS_OK(ntstatus)) {
199 return -1;
203 /* check if domain is a domain or a workgroup */
205 status = NetJoinDomain(server_name, domain_name, account_ou,
206 Account, password, join_flags);
207 if (status != 0) {
208 printf(_("Failed to join domain: %s\n"),
209 libnetapi_get_error_string(c->netapi_ctx, status));
210 goto done;
213 if (do_reboot) {
214 c->opt_comment = _("Shutting down due to a domain membership "
215 "change");
216 c->opt_reboot = true;
217 c->opt_timeout = 30;
219 ret = run_rpc_command(c, cli, &ndr_table_initshutdown.syntax_id, 0,
220 rpc_init_shutdown_internals,
221 argc, argv);
222 if (ret == 0) {
223 goto done;
226 ret = run_rpc_command(c, cli, &ndr_table_winreg.syntax_id, 0,
227 rpc_reg_shutdown_internals,
228 argc, argv);
229 goto done;
232 ret = 0;
234 done:
235 if (cli) {
236 cli_shutdown(cli);
239 return ret;
242 static int net_dom_renamecomputer(struct net_context *c, int argc, const char **argv)
244 const char *server_name = NULL;
245 const char *account = NULL;
246 const char *password = NULL;
247 const char *newname = NULL;
248 uint32_t rename_options = NETSETUP_ACCT_CREATE;
249 struct cli_state *cli = NULL;
250 bool do_reboot = false;
251 NTSTATUS ntstatus;
252 NET_API_STATUS status;
253 int ret = -1;
254 int i;
256 if (argc < 1 || c->display_usage) {
257 return net_dom_usage(c, argc, argv);
260 if (c->opt_host) {
261 server_name = c->opt_host;
264 for (i=0; i<argc; i++) {
265 if (strnequal(argv[i], "account", strlen("account"))) {
266 account = get_string_param(argv[i]);
267 if (!account) {
268 return -1;
271 if (strnequal(argv[i], "password", strlen("password"))) {
272 password = get_string_param(argv[i]);
273 if (!password) {
274 return -1;
277 if (strnequal(argv[i], "newname", strlen("newname"))) {
278 newname = get_string_param(argv[i]);
279 if (!newname) {
280 return -1;
283 if (strequal(argv[i], "reboot")) {
284 do_reboot = true;
288 if (do_reboot) {
289 ntstatus = net_make_ipc_connection_ex(c, c->opt_workgroup,
290 server_name, NULL, 0,
291 &cli);
292 if (!NT_STATUS_IS_OK(ntstatus)) {
293 return -1;
297 status = NetRenameMachineInDomain(server_name, newname,
298 account, password, rename_options);
299 if (status != 0) {
300 printf(_("Failed to rename machine: "));
301 if (status == W_ERROR_V(WERR_SETUP_NOT_JOINED)) {
302 printf(_("Computer is not joined to a Domain\n"));
303 goto done;
305 printf("%s\n",
306 libnetapi_get_error_string(c->netapi_ctx, status));
307 goto done;
310 if (do_reboot) {
311 c->opt_comment = _("Shutting down due to a computer rename");
312 c->opt_reboot = true;
313 c->opt_timeout = 30;
315 ret = run_rpc_command(c, cli,
316 &ndr_table_initshutdown.syntax_id,
317 0, rpc_init_shutdown_internals,
318 argc, argv);
319 if (ret == 0) {
320 goto done;
323 ret = run_rpc_command(c, cli, &ndr_table_winreg.syntax_id, 0,
324 rpc_reg_shutdown_internals,
325 argc, argv);
326 goto done;
329 ret = 0;
331 done:
332 if (cli) {
333 cli_shutdown(cli);
336 return ret;
339 int net_dom(struct net_context *c, int argc, const char **argv)
341 NET_API_STATUS status;
343 struct functable func[] = {
345 "join",
346 net_dom_join,
347 NET_TRANSPORT_LOCAL,
348 N_("Join a remote machine"),
349 N_("net dom join <domain=DOMAIN> <ou=OU> "
350 "<account=ACCOUNT> <password=PASSWORD> <reboot>\n"
351 " Join a remote machine")
354 "unjoin",
355 net_dom_unjoin,
356 NET_TRANSPORT_LOCAL,
357 N_("Unjoin a remote machine"),
358 N_("net dom unjoin <account=ACCOUNT> "
359 "<password=PASSWORD> <reboot>\n"
360 " Unjoin a remote machine")
363 "renamecomputer",
364 net_dom_renamecomputer,
365 NET_TRANSPORT_LOCAL,
366 N_("Rename a computer that is joined to a domain"),
367 N_("net dom renamecomputer <newname=NEWNAME> "
368 "<account=ACCOUNT> <password=PASSWORD> "
369 "<reboot>\n"
370 " Rename joined computer")
373 {NULL, NULL, 0, NULL, NULL}
376 status = libnetapi_net_init(&c->netapi_ctx);
377 if (status != 0) {
378 return -1;
381 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
382 libnetapi_set_password(c->netapi_ctx, c->opt_password);
383 if (c->opt_kerberos) {
384 libnetapi_set_use_kerberos(c->netapi_ctx);
387 return net_run_function(c, argc, argv, "net dom", func);