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/>.
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)
33 "<domain=DOMAIN> <ou=OU> <account=ACCOUNT> "
34 "<password=PASSWORD> <reboot>\n Join a remote machine\n"));
38 "<account=ACCOUNT> <password=PASSWORD> <reboot>\n"
39 " Unjoin a remote machine\n"));
42 _("net dom renamecomputer "
44 "<account=ACCOUNT> <password=PASSWORD> <reboot>\n"
45 " Rename joined computer\n"));
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;
61 NET_API_STATUS status;
65 if (argc < 1 || c->display_usage) {
66 return net_dom_usage(c, argc, argv);
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]);
80 if (strnequal(argv[i], "password", strlen("password"))) {
81 password = get_string_param(argv[i]);
86 if (strequal(argv[i], "reboot")) {
92 ntstatus = net_make_ipc_connection_ex(c, c->opt_workgroup,
95 if (!NT_STATUS_IS_OK(ntstatus)) {
100 status = NetUnjoinDomain(server_name, account, password, unjoin_flags);
102 printf(_("Failed to unjoin domain: %s\n"),
103 libnetapi_get_error_string(c->netapi_ctx, status));
108 c->opt_comment = _("Shutting down due to a domain membership "
110 c->opt_reboot = true;
113 ret = run_rpc_command(c, cli,
114 &ndr_table_initshutdown.syntax_id,
115 0, rpc_init_shutdown_internals,
121 ret = run_rpc_command(c, cli, &ndr_table_winreg.syntax_id, 0,
122 rpc_reg_shutdown_internals,
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;
149 NET_API_STATUS status;
153 if (argc < 1 || c->display_usage) {
154 return net_dom_usage(c, argc, argv);
158 server_name = c->opt_host;
162 join_flags |= NETSETUP_DOMAIN_JOIN_IF_JOINED;
165 for (i=0; i<argc; i++) {
166 if (strnequal(argv[i], "ou", strlen("ou"))) {
167 account_ou = get_string_param(argv[i]);
172 if (strnequal(argv[i], "domain", strlen("domain"))) {
173 domain_name = get_string_param(argv[i]);
178 if (strnequal(argv[i], "account", strlen("account"))) {
179 Account = get_string_param(argv[i]);
184 if (strnequal(argv[i], "password", strlen("password"))) {
185 password = get_string_param(argv[i]);
190 if (strequal(argv[i], "reboot")) {
196 ntstatus = net_make_ipc_connection_ex(c, c->opt_workgroup,
197 server_name, NULL, 0,
199 if (!NT_STATUS_IS_OK(ntstatus)) {
204 /* check if domain is a domain or a workgroup */
206 status = NetJoinDomain(server_name, domain_name, account_ou,
207 Account, password, join_flags);
209 printf(_("Failed to join domain: %s\n"),
210 libnetapi_get_error_string(c->netapi_ctx, status));
215 c->opt_comment = _("Shutting down due to a domain membership "
217 c->opt_reboot = true;
220 ret = run_rpc_command(c, cli, &ndr_table_initshutdown.syntax_id, 0,
221 rpc_init_shutdown_internals,
227 ret = run_rpc_command(c, cli, &ndr_table_winreg.syntax_id, 0,
228 rpc_reg_shutdown_internals,
243 static int net_dom_renamecomputer(struct net_context *c, int argc, const char **argv)
245 const char *server_name = NULL;
246 const char *account = NULL;
247 const char *password = NULL;
248 const char *newname = NULL;
249 uint32_t rename_options = NETSETUP_ACCT_CREATE;
250 struct cli_state *cli = NULL;
251 bool do_reboot = false;
253 NET_API_STATUS status;
257 if (argc < 1 || c->display_usage) {
258 return net_dom_usage(c, argc, argv);
262 server_name = c->opt_host;
265 for (i=0; i<argc; i++) {
266 if (strnequal(argv[i], "account", strlen("account"))) {
267 account = get_string_param(argv[i]);
272 if (strnequal(argv[i], "password", strlen("password"))) {
273 password = get_string_param(argv[i]);
278 if (strnequal(argv[i], "newname", strlen("newname"))) {
279 newname = get_string_param(argv[i]);
284 if (strequal(argv[i], "reboot")) {
290 ntstatus = net_make_ipc_connection_ex(c, c->opt_workgroup,
291 server_name, NULL, 0,
293 if (!NT_STATUS_IS_OK(ntstatus)) {
298 status = NetRenameMachineInDomain(server_name, newname,
299 account, password, rename_options);
301 printf(_("Failed to rename machine: "));
302 if (status == W_ERROR_V(WERR_SETUP_NOT_JOINED)) {
303 printf(_("Computer is not joined to a Domain\n"));
307 libnetapi_get_error_string(c->netapi_ctx, status));
312 c->opt_comment = _("Shutting down due to a computer rename");
313 c->opt_reboot = true;
316 ret = run_rpc_command(c, cli,
317 &ndr_table_initshutdown.syntax_id,
318 0, rpc_init_shutdown_internals,
324 ret = run_rpc_command(c, cli, &ndr_table_winreg.syntax_id, 0,
325 rpc_reg_shutdown_internals,
340 int net_dom(struct net_context *c, int argc, const char **argv)
342 NET_API_STATUS status;
344 struct functable func[] = {
349 N_("Join a remote machine"),
350 N_("net dom join <domain=DOMAIN> <ou=OU> "
351 "<account=ACCOUNT> <password=PASSWORD> <reboot>\n"
352 " Join a remote machine")
358 N_("Unjoin a remote machine"),
359 N_("net dom unjoin <account=ACCOUNT> "
360 "<password=PASSWORD> <reboot>\n"
361 " Unjoin a remote machine")
365 net_dom_renamecomputer,
367 N_("Rename a computer that is joined to a domain"),
368 N_("net dom renamecomputer <newname=NEWNAME> "
369 "<account=ACCOUNT> <password=PASSWORD> "
371 " Rename joined computer")
374 {NULL, NULL, 0, NULL, NULL}
377 status = libnetapi_net_init(&c->netapi_ctx);
382 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
383 libnetapi_set_password(c->netapi_ctx, c->opt_password);
384 if (c->opt_kerberos) {
385 libnetapi_set_use_kerberos(c->netapi_ctx);
388 return net_run_function(c, argc, argv, "net dom", func);