2 Samba Unix/Linux SMB client library
3 net dom commands for remote join/unjoin
4 Copyright (C) 2007 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"
23 int net_dom_usage(struct net_context *c, int argc, const char **argv)
25 d_printf("usage: net dom join "
26 "<domain=DOMAIN> <ou=OU> <account=ACCOUNT> "
27 "<password=PASSWORD> <reboot>\n Join a remote machine\n");
28 d_printf("usage: net dom unjoin "
29 "<account=ACCOUNT> <password=PASSWORD> <reboot>\n"
30 " Unjoin a remote machine\n");
35 static int net_dom_unjoin(struct net_context *c, int argc, const char **argv)
37 const char *server_name = NULL;
38 const char *account = NULL;
39 const char *password = NULL;
40 uint32_t unjoin_flags = NETSETUP_ACCT_DELETE |
41 NETSETUP_JOIN_DOMAIN |
42 NETSETUP_IGNORE_UNSUPPORTED_FLAGS;
43 struct cli_state *cli = NULL;
44 bool do_reboot = false;
46 NET_API_STATUS status;
50 if (argc < 1 || c->display_usage) {
51 return net_dom_usage(c, argc, argv);
55 server_name = c->opt_host;
58 for (i=0; i<argc; i++) {
59 if (strnequal(argv[i], "account", strlen("account"))) {
60 account = get_string_param(argv[i]);
65 if (strnequal(argv[i], "password", strlen("password"))) {
66 password = get_string_param(argv[i]);
71 if (strequal(argv[i], "reboot")) {
77 ntstatus = net_make_ipc_connection_ex(c, c->opt_workgroup,
80 if (!NT_STATUS_IS_OK(ntstatus)) {
85 status = NetUnjoinDomain(server_name, account, password, unjoin_flags);
87 printf("Failed to unjoin domain: %s\n",
88 libnetapi_get_error_string(c->netapi_ctx, status));
93 c->opt_comment = "Shutting down due to a domain membership "
98 ret = run_rpc_command(c, cli,
99 &ndr_table_initshutdown.syntax_id,
100 0, rpc_init_shutdown_internals,
106 ret = run_rpc_command(c, cli, &ndr_table_winreg.syntax_id, 0,
107 rpc_reg_shutdown_internals,
122 static int net_dom_join(struct net_context *c, int argc, const char **argv)
124 const char *server_name = NULL;
125 const char *domain_name = NULL;
126 const char *account_ou = NULL;
127 const char *Account = NULL;
128 const char *password = NULL;
129 uint32_t join_flags = NETSETUP_ACCT_CREATE |
130 NETSETUP_JOIN_DOMAIN;
131 struct cli_state *cli = NULL;
132 bool do_reboot = false;
134 NET_API_STATUS status;
138 if (argc < 1 || c->display_usage) {
139 return net_dom_usage(c, argc, argv);
143 server_name = c->opt_host;
147 join_flags |= NETSETUP_DOMAIN_JOIN_IF_JOINED;
150 for (i=0; i<argc; i++) {
151 if (strnequal(argv[i], "ou", strlen("ou"))) {
152 account_ou = get_string_param(argv[i]);
157 if (strnequal(argv[i], "domain", strlen("domain"))) {
158 domain_name = get_string_param(argv[i]);
163 if (strnequal(argv[i], "account", strlen("account"))) {
164 Account = get_string_param(argv[i]);
169 if (strnequal(argv[i], "password", strlen("password"))) {
170 password = get_string_param(argv[i]);
175 if (strequal(argv[i], "reboot")) {
181 ntstatus = net_make_ipc_connection_ex(c, c->opt_workgroup,
182 server_name, NULL, 0,
184 if (!NT_STATUS_IS_OK(ntstatus)) {
189 /* check if domain is a domain or a workgroup */
191 status = NetJoinDomain(server_name, domain_name, account_ou,
192 Account, password, join_flags);
194 printf("Failed to join domain: %s\n",
195 libnetapi_get_error_string(c->netapi_ctx, status));
200 c->opt_comment = "Shutting down due to a domain membership "
202 c->opt_reboot = true;
205 ret = run_rpc_command(c, cli, &ndr_table_initshutdown.syntax_id, 0,
206 rpc_init_shutdown_internals,
212 ret = run_rpc_command(c, cli, &ndr_table_winreg.syntax_id, 0,
213 rpc_reg_shutdown_internals,
228 int net_dom(struct net_context *c, int argc, const char **argv)
230 NET_API_STATUS status;
232 struct functable func[] = {
237 "Join a remote machine",
238 "net dom join <domain=DOMAIN> <ou=OU> "
239 "<account=ACCOUNT> <password=PASSWORD> <reboot>\n"
240 " Join a remote machine"
246 "Unjoin a remote machine",
247 "net dom unjoin <account=ACCOUNT> <password=PASSWORD> "
249 " Unjoin a remote machine"
251 {NULL, NULL, 0, NULL, NULL}
254 status = libnetapi_init(&c->netapi_ctx);
259 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
260 libnetapi_set_password(c->netapi_ctx, c->opt_password);
261 if (c->opt_kerberos) {
262 libnetapi_set_use_kerberos(c->netapi_ctx);
265 return net_run_function(c, argc, argv, "net dom", func);