talloc: release 2.4.2
[Samba.git] / source3 / utils / net_dom.c
blob4342990eaa1aeaefa5c25a865d3ba4557a08afcf
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/ndr_initshutdown.h"
23 #include "../librpc/gen_ndr/ndr_winreg.h"
24 #include "lib/netapi/netapi.h"
25 #include "lib/netapi/netapi_net.h"
26 #include "libsmb/libsmb.h"
28 int net_dom_usage(struct net_context *c, int argc, const char **argv)
30 d_printf("%s\n%s",
31 _("Usage:"),
32 _("net dom join "
33 "<domain=DOMAIN> <ou=OU> <account=ACCOUNT> "
34 "<password=PASSWORD> <reboot>\n Join a remote machine\n"));
35 d_printf("%s\n%s",
36 _("Usage:"),
37 _("net dom unjoin "
38 "<account=ACCOUNT> <password=PASSWORD> <reboot>\n"
39 " Unjoin a remote machine\n"));
40 d_printf("%s\n%s",
41 _("Usage:"),
42 _("net dom renamecomputer "
43 "<newname=NEWNAME> "
44 "<account=ACCOUNT> <password=PASSWORD> <reboot>\n"
45 " Rename joined computer\n"));
47 return -1;
50 static int net_dom_unjoin(struct net_context *c, int argc, const char **argv)
52 const char *server_name = NULL;
53 const char *account = NULL;
54 const char *password = NULL;
55 uint32_t unjoin_flags = NETSETUP_ACCT_DELETE |
56 NETSETUP_JOIN_DOMAIN |
57 NETSETUP_IGNORE_UNSUPPORTED_FLAGS;
58 struct cli_state *cli = NULL;
59 bool do_reboot = false;
60 NTSTATUS ntstatus;
61 NET_API_STATUS status;
62 int ret = -1;
63 int i;
65 if (argc < 1 || c->display_usage) {
66 return net_dom_usage(c, argc, argv);
69 if (c->opt_host) {
70 server_name = c->opt_host;
73 for (i=0; i<argc; i++) {
74 if (strnequal(argv[i], "account", strlen("account"))) {
75 account = get_string_param(argv[i]);
76 if (!account) {
77 return -1;
80 if (strnequal(argv[i], "password", strlen("password"))) {
81 password = get_string_param(argv[i]);
82 if (!password) {
83 return -1;
86 if (strequal(argv[i], "reboot")) {
87 do_reboot = true;
91 if (do_reboot) {
92 ntstatus = net_make_ipc_connection_ex(c, c->opt_workgroup,
93 server_name, NULL, 0,
94 &cli);
95 if (!NT_STATUS_IS_OK(ntstatus)) {
96 return -1;
100 status = NetUnjoinDomain(server_name, account, password, unjoin_flags);
101 if (status != 0) {
102 printf(_("Failed to unjoin domain: %s\n"),
103 libnetapi_get_error_string(c->netapi_ctx, status));
104 goto done;
107 if (do_reboot) {
108 c->opt_comment = _("Shutting down due to a domain membership "
109 "change");
110 c->opt_reboot = true;
111 c->opt_timeout = 30;
113 ret = run_rpc_command(c, cli,
114 &ndr_table_initshutdown,
115 0, rpc_init_shutdown_internals,
116 argc, argv);
117 if (ret == 0) {
118 goto done;
121 ret = run_rpc_command(c, cli, &ndr_table_winreg, 0,
122 rpc_reg_shutdown_internals,
123 argc, argv);
124 goto done;
127 ret = 0;
129 done:
130 if (cli) {
131 cli_shutdown(cli);
134 return ret;
137 static int net_dom_join(struct net_context *c, int argc, const char **argv)
139 const char *server_name = NULL;
140 const char *domain_name = NULL;
141 const char *account_ou = NULL;
142 const char *Account = NULL;
143 const char *password = NULL;
144 uint32_t join_flags = NETSETUP_ACCT_CREATE |
145 NETSETUP_JOIN_DOMAIN;
146 struct cli_state *cli = NULL;
147 bool do_reboot = false;
148 NTSTATUS ntstatus;
149 NET_API_STATUS status;
150 int ret = -1;
151 int i;
153 if (argc < 1 || c->display_usage) {
154 return net_dom_usage(c, argc, argv);
157 net_warn_member_options();
159 if (c->opt_host) {
160 server_name = c->opt_host;
163 if (c->opt_force) {
164 join_flags |= NETSETUP_DOMAIN_JOIN_IF_JOINED;
167 for (i=0; i<argc; i++) {
168 if (strnequal(argv[i], "ou", strlen("ou"))) {
169 account_ou = get_string_param(argv[i]);
170 if (!account_ou) {
171 return -1;
174 if (strnequal(argv[i], "domain", strlen("domain"))) {
175 domain_name = get_string_param(argv[i]);
176 if (!domain_name) {
177 return -1;
180 if (strnequal(argv[i], "account", strlen("account"))) {
181 Account = get_string_param(argv[i]);
182 if (!Account) {
183 return -1;
186 if (strnequal(argv[i], "password", strlen("password"))) {
187 password = get_string_param(argv[i]);
188 if (!password) {
189 return -1;
192 if (strequal(argv[i], "reboot")) {
193 do_reboot = true;
197 if (do_reboot) {
198 ntstatus = net_make_ipc_connection_ex(c, c->opt_workgroup,
199 server_name, NULL, 0,
200 &cli);
201 if (!NT_STATUS_IS_OK(ntstatus)) {
202 return -1;
206 /* check if domain is a domain or a workgroup */
208 status = NetJoinDomain(server_name, domain_name, account_ou,
209 Account, password, join_flags);
210 if (status != 0) {
211 printf(_("Failed to join domain: %s\n"),
212 libnetapi_get_error_string(c->netapi_ctx, status));
213 goto done;
216 if (do_reboot) {
217 c->opt_comment = _("Shutting down due to a domain membership "
218 "change");
219 c->opt_reboot = true;
220 c->opt_timeout = 30;
222 ret = run_rpc_command(c, cli, &ndr_table_initshutdown, 0,
223 rpc_init_shutdown_internals,
224 argc, argv);
225 if (ret == 0) {
226 goto done;
229 ret = run_rpc_command(c, cli, &ndr_table_winreg, 0,
230 rpc_reg_shutdown_internals,
231 argc, argv);
232 goto done;
235 ret = 0;
237 done:
238 if (cli) {
239 cli_shutdown(cli);
242 return ret;
245 static int net_dom_renamecomputer(struct net_context *c, int argc, const char **argv)
247 const char *server_name = NULL;
248 const char *account = NULL;
249 const char *password = NULL;
250 const char *newname = NULL;
251 uint32_t rename_options = NETSETUP_ACCT_CREATE;
252 struct cli_state *cli = NULL;
253 bool do_reboot = false;
254 NTSTATUS ntstatus;
255 NET_API_STATUS status;
256 int ret = -1;
257 int i;
259 if (argc < 1 || c->display_usage) {
260 return net_dom_usage(c, argc, argv);
263 if (c->opt_host) {
264 server_name = c->opt_host;
267 for (i=0; i<argc; i++) {
268 if (strnequal(argv[i], "account", strlen("account"))) {
269 account = get_string_param(argv[i]);
270 if (!account) {
271 return -1;
274 if (strnequal(argv[i], "password", strlen("password"))) {
275 password = get_string_param(argv[i]);
276 if (!password) {
277 return -1;
280 if (strnequal(argv[i], "newname", strlen("newname"))) {
281 newname = get_string_param(argv[i]);
282 if (!newname) {
283 return -1;
286 if (strequal(argv[i], "reboot")) {
287 do_reboot = true;
291 if (do_reboot) {
292 ntstatus = net_make_ipc_connection_ex(c, c->opt_workgroup,
293 server_name, NULL, 0,
294 &cli);
295 if (!NT_STATUS_IS_OK(ntstatus)) {
296 return -1;
300 status = NetRenameMachineInDomain(server_name, newname,
301 account, password, rename_options);
302 if (status != 0) {
303 printf(_("Failed to rename machine: "));
304 if (status == W_ERROR_V(WERR_NERR_SETUPNOTJOINED)) {
305 printf(_("Computer is not joined to a Domain\n"));
306 goto done;
308 printf("%s\n",
309 libnetapi_get_error_string(c->netapi_ctx, status));
310 goto done;
313 if (do_reboot) {
314 c->opt_comment = _("Shutting down due to a computer rename");
315 c->opt_reboot = true;
316 c->opt_timeout = 30;
318 ret = run_rpc_command(c, cli,
319 &ndr_table_initshutdown,
320 0, rpc_init_shutdown_internals,
321 argc, argv);
322 if (ret == 0) {
323 goto done;
326 ret = run_rpc_command(c, cli, &ndr_table_winreg, 0,
327 rpc_reg_shutdown_internals,
328 argc, argv);
329 goto done;
332 ret = 0;
334 done:
335 if (cli) {
336 cli_shutdown(cli);
339 return ret;
342 int net_dom(struct net_context *c, int argc, const char **argv)
344 NET_API_STATUS status;
346 struct functable func[] = {
348 "join",
349 net_dom_join,
350 NET_TRANSPORT_LOCAL,
351 N_("Join a remote machine"),
352 N_("net dom join <domain=DOMAIN> <ou=OU> "
353 "<account=ACCOUNT> <password=PASSWORD> <reboot>\n"
354 " Join a remote machine")
357 "unjoin",
358 net_dom_unjoin,
359 NET_TRANSPORT_LOCAL,
360 N_("Unjoin a remote machine"),
361 N_("net dom unjoin <account=ACCOUNT> "
362 "<password=PASSWORD> <reboot>\n"
363 " Unjoin a remote machine")
366 "renamecomputer",
367 net_dom_renamecomputer,
368 NET_TRANSPORT_LOCAL,
369 N_("Rename a computer that is joined to a domain"),
370 N_("net dom renamecomputer <newname=NEWNAME> "
371 "<account=ACCOUNT> <password=PASSWORD> "
372 "<reboot>\n"
373 " Rename joined computer")
376 {NULL, NULL, 0, NULL, NULL}
379 status = libnetapi_net_init(&c->netapi_ctx, c->lp_ctx, c->creds);
380 if (status != 0) {
381 return -1;
384 return net_run_function(c, argc, argv, "net dom", func);