s3:registry: extract the reg_cachehook prototypes into their own header.
[Samba/gebeck_regimport.git] / source3 / utils / net_dom.c
blob89cc228f4771669a8772dde96f098d7dc2e73779
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"
25 int net_dom_usage(struct net_context *c, int argc, const char **argv)
27 d_printf("%s\n%s",
28 _("Usage:"),
29 _("net dom join "
30 "<domain=DOMAIN> <ou=OU> <account=ACCOUNT> "
31 "<password=PASSWORD> <reboot>\n Join a remote machine\n"));
32 d_printf("%s\n%s",
33 _("Usage:"),
34 _("net dom unjoin "
35 "<account=ACCOUNT> <password=PASSWORD> <reboot>\n"
36 " Unjoin a remote machine\n"));
37 d_printf("%s\n%s",
38 _("Usage:"),
39 _("net dom renamecomputer "
40 "<newname=NEWNAME> "
41 "<account=ACCOUNT> <password=PASSWORD> <reboot>\n"
42 " Rename joined computer\n"));
44 return -1;
47 static int net_dom_unjoin(struct net_context *c, int argc, const char **argv)
49 const char *server_name = NULL;
50 const char *account = NULL;
51 const char *password = NULL;
52 uint32_t unjoin_flags = NETSETUP_ACCT_DELETE |
53 NETSETUP_JOIN_DOMAIN |
54 NETSETUP_IGNORE_UNSUPPORTED_FLAGS;
55 struct cli_state *cli = NULL;
56 bool do_reboot = false;
57 NTSTATUS ntstatus;
58 NET_API_STATUS status;
59 int ret = -1;
60 int i;
62 if (argc < 1 || c->display_usage) {
63 return net_dom_usage(c, argc, argv);
66 if (c->opt_host) {
67 server_name = c->opt_host;
70 for (i=0; i<argc; i++) {
71 if (strnequal(argv[i], "account", strlen("account"))) {
72 account = get_string_param(argv[i]);
73 if (!account) {
74 return -1;
77 if (strnequal(argv[i], "password", strlen("password"))) {
78 password = get_string_param(argv[i]);
79 if (!password) {
80 return -1;
83 if (strequal(argv[i], "reboot")) {
84 do_reboot = true;
88 if (do_reboot) {
89 ntstatus = net_make_ipc_connection_ex(c, c->opt_workgroup,
90 server_name, NULL, 0,
91 &cli);
92 if (!NT_STATUS_IS_OK(ntstatus)) {
93 return -1;
97 status = NetUnjoinDomain(server_name, account, password, unjoin_flags);
98 if (status != 0) {
99 printf(_("Failed to unjoin domain: %s\n"),
100 libnetapi_get_error_string(c->netapi_ctx, status));
101 goto done;
104 if (do_reboot) {
105 c->opt_comment = _("Shutting down due to a domain membership "
106 "change");
107 c->opt_reboot = true;
108 c->opt_timeout = 30;
110 ret = run_rpc_command(c, cli,
111 &ndr_table_initshutdown.syntax_id,
112 0, rpc_init_shutdown_internals,
113 argc, argv);
114 if (ret == 0) {
115 goto done;
118 ret = run_rpc_command(c, cli, &ndr_table_winreg.syntax_id, 0,
119 rpc_reg_shutdown_internals,
120 argc, argv);
121 goto done;
124 ret = 0;
126 done:
127 if (cli) {
128 cli_shutdown(cli);
131 return ret;
134 static int net_dom_join(struct net_context *c, int argc, const char **argv)
136 const char *server_name = NULL;
137 const char *domain_name = NULL;
138 const char *account_ou = NULL;
139 const char *Account = NULL;
140 const char *password = NULL;
141 uint32_t join_flags = NETSETUP_ACCT_CREATE |
142 NETSETUP_JOIN_DOMAIN;
143 struct cli_state *cli = NULL;
144 bool do_reboot = false;
145 NTSTATUS ntstatus;
146 NET_API_STATUS status;
147 int ret = -1;
148 int i;
150 if (argc < 1 || c->display_usage) {
151 return net_dom_usage(c, argc, argv);
154 if (c->opt_host) {
155 server_name = c->opt_host;
158 if (c->opt_force) {
159 join_flags |= NETSETUP_DOMAIN_JOIN_IF_JOINED;
162 for (i=0; i<argc; i++) {
163 if (strnequal(argv[i], "ou", strlen("ou"))) {
164 account_ou = get_string_param(argv[i]);
165 if (!account_ou) {
166 return -1;
169 if (strnequal(argv[i], "domain", strlen("domain"))) {
170 domain_name = get_string_param(argv[i]);
171 if (!domain_name) {
172 return -1;
175 if (strnequal(argv[i], "account", strlen("account"))) {
176 Account = get_string_param(argv[i]);
177 if (!Account) {
178 return -1;
181 if (strnequal(argv[i], "password", strlen("password"))) {
182 password = get_string_param(argv[i]);
183 if (!password) {
184 return -1;
187 if (strequal(argv[i], "reboot")) {
188 do_reboot = true;
192 if (do_reboot) {
193 ntstatus = net_make_ipc_connection_ex(c, c->opt_workgroup,
194 server_name, NULL, 0,
195 &cli);
196 if (!NT_STATUS_IS_OK(ntstatus)) {
197 return -1;
201 /* check if domain is a domain or a workgroup */
203 status = NetJoinDomain(server_name, domain_name, account_ou,
204 Account, password, join_flags);
205 if (status != 0) {
206 printf(_("Failed to join domain: %s\n"),
207 libnetapi_get_error_string(c->netapi_ctx, status));
208 goto done;
211 if (do_reboot) {
212 c->opt_comment = _("Shutting down due to a domain membership "
213 "change");
214 c->opt_reboot = true;
215 c->opt_timeout = 30;
217 ret = run_rpc_command(c, cli, &ndr_table_initshutdown.syntax_id, 0,
218 rpc_init_shutdown_internals,
219 argc, argv);
220 if (ret == 0) {
221 goto done;
224 ret = run_rpc_command(c, cli, &ndr_table_winreg.syntax_id, 0,
225 rpc_reg_shutdown_internals,
226 argc, argv);
227 goto done;
230 ret = 0;
232 done:
233 if (cli) {
234 cli_shutdown(cli);
237 return ret;
240 static int net_dom_renamecomputer(struct net_context *c, int argc, const char **argv)
242 const char *server_name = NULL;
243 const char *account = NULL;
244 const char *password = NULL;
245 const char *newname = NULL;
246 uint32_t rename_options = NETSETUP_ACCT_CREATE;
247 struct cli_state *cli = NULL;
248 bool do_reboot = false;
249 NTSTATUS ntstatus;
250 NET_API_STATUS status;
251 int ret = -1;
252 int i;
254 if (argc < 1 || c->display_usage) {
255 return net_dom_usage(c, argc, argv);
258 if (c->opt_host) {
259 server_name = c->opt_host;
262 for (i=0; i<argc; i++) {
263 if (strnequal(argv[i], "account", strlen("account"))) {
264 account = get_string_param(argv[i]);
265 if (!account) {
266 return -1;
269 if (strnequal(argv[i], "password", strlen("password"))) {
270 password = get_string_param(argv[i]);
271 if (!password) {
272 return -1;
275 if (strnequal(argv[i], "newname", strlen("newname"))) {
276 newname = get_string_param(argv[i]);
277 if (!newname) {
278 return -1;
281 if (strequal(argv[i], "reboot")) {
282 do_reboot = true;
286 if (do_reboot) {
287 ntstatus = net_make_ipc_connection_ex(c, c->opt_workgroup,
288 server_name, NULL, 0,
289 &cli);
290 if (!NT_STATUS_IS_OK(ntstatus)) {
291 return -1;
295 status = NetRenameMachineInDomain(server_name, newname,
296 account, password, rename_options);
297 if (status != 0) {
298 printf(_("Failed to rename machine: "));
299 if (status == W_ERROR_V(WERR_SETUP_NOT_JOINED)) {
300 printf(_("Computer is not joined to a Domain\n"));
301 goto done;
303 printf("%s\n",
304 libnetapi_get_error_string(c->netapi_ctx, status));
305 goto done;
308 if (do_reboot) {
309 c->opt_comment = _("Shutting down due to a computer rename");
310 c->opt_reboot = true;
311 c->opt_timeout = 30;
313 ret = run_rpc_command(c, cli,
314 &ndr_table_initshutdown.syntax_id,
315 0, rpc_init_shutdown_internals,
316 argc, argv);
317 if (ret == 0) {
318 goto done;
321 ret = run_rpc_command(c, cli, &ndr_table_winreg.syntax_id, 0,
322 rpc_reg_shutdown_internals,
323 argc, argv);
324 goto done;
327 ret = 0;
329 done:
330 if (cli) {
331 cli_shutdown(cli);
334 return ret;
337 int net_dom(struct net_context *c, int argc, const char **argv)
339 NET_API_STATUS status;
341 struct functable func[] = {
343 "join",
344 net_dom_join,
345 NET_TRANSPORT_LOCAL,
346 N_("Join a remote machine"),
347 N_("net dom join <domain=DOMAIN> <ou=OU> "
348 "<account=ACCOUNT> <password=PASSWORD> <reboot>\n"
349 " Join a remote machine")
352 "unjoin",
353 net_dom_unjoin,
354 NET_TRANSPORT_LOCAL,
355 N_("Unjoin a remote machine"),
356 N_("net dom unjoin <account=ACCOUNT> "
357 "<password=PASSWORD> <reboot>\n"
358 " Unjoin a remote machine")
361 "renamecomputer",
362 net_dom_renamecomputer,
363 NET_TRANSPORT_LOCAL,
364 N_("Rename a computer that is joined to a domain"),
365 N_("net dom renamecomputer <newname=NEWNAME> "
366 "<account=ACCOUNT> <password=PASSWORD> "
367 "<reboot>\n"
368 " Rename joined computer")
371 {NULL, NULL, 0, NULL, NULL}
374 status = libnetapi_init(&c->netapi_ctx);
375 if (status != 0) {
376 return -1;
379 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
380 libnetapi_set_password(c->netapi_ctx, c->opt_password);
381 if (c->opt_kerberos) {
382 libnetapi_set_use_kerberos(c->netapi_ctx);
385 return net_run_function(c, argc, argv, "net dom", func);