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 static 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> <password=PASSWORD> <reboot>\n");
27 d_printf("usage: net dom unjoin "
28 "<account=ACCOUNT> <password=PASSWORD> <reboot>\n");
33 int net_help_dom(struct net_context *c, int argc, const char **argv)
35 d_printf("net dom join"\
36 "\n Join a remote machine\n");
37 d_printf("net dom unjoin"\
38 "\n Unjoin a remote machine\n");
43 static int net_dom_unjoin(struct net_context *c, int argc, const char **argv)
45 struct libnetapi_ctx *ctx = NULL;
46 const char *server_name = NULL;
47 const char *account = NULL;
48 const char *password = NULL;
49 uint32_t unjoin_flags = WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE |
50 WKSSVC_JOIN_FLAGS_JOIN_TYPE;
51 struct cli_state *cli = NULL;
54 NET_API_STATUS status;
59 return net_dom_usage(c, argc, argv);
63 server_name = c->opt_host;
66 for (i=0; i<argc; i++) {
67 if (strnequal(argv[i], "account", strlen("account"))) {
68 account = get_string_param(argv[i]);
73 if (strnequal(argv[i], "password", strlen("password"))) {
74 password = get_string_param(argv[i]);
79 if (strequal(argv[i], "reboot")) {
85 ntstatus = net_make_ipc_connection_ex(c, c->opt_workgroup,
88 if (!NT_STATUS_IS_OK(ntstatus)) {
93 status = libnetapi_init(&ctx);
98 libnetapi_set_username(ctx, c->opt_user_name);
99 libnetapi_set_password(ctx, c->opt_password);
101 status = NetUnjoinDomain(server_name, account, password, unjoin_flags);
103 printf("Failed to unjoin domain: %s\n",
104 libnetapi_get_error_string(ctx, status));
109 c->opt_comment = "Shutting down due to a domain membership "
111 c->opt_reboot = true;
114 ret = run_rpc_command(c, cli, PI_INITSHUTDOWN, 0,
115 rpc_init_shutdown_internals,
121 ret = run_rpc_command(c, cli, PI_WINREG, 0,
122 rpc_reg_shutdown_internals,
137 static int net_dom_join(struct net_context *c, int argc, const char **argv)
139 struct libnetapi_ctx *ctx = NULL;
140 const char *server_name = NULL;
141 const char *domain_name = NULL;
142 const char *account_ou = NULL;
143 const char *Account = NULL;
144 const char *password = NULL;
145 uint32_t join_flags = WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE |
146 WKSSVC_JOIN_FLAGS_JOIN_TYPE;
147 struct cli_state *cli = NULL;
150 NET_API_STATUS status;
155 return net_dom_usage(c, argc, argv);
159 server_name = c->opt_host;
163 join_flags |= WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED;
166 for (i=0; i<argc; i++) {
167 if (strnequal(argv[i], "ou", strlen("ou"))) {
168 account_ou = get_string_param(argv[i]);
173 if (strnequal(argv[i], "domain", strlen("domain"))) {
174 domain_name = get_string_param(argv[i]);
179 if (strnequal(argv[i], "account", strlen("account"))) {
180 Account = get_string_param(argv[i]);
185 if (strnequal(argv[i], "password", strlen("password"))) {
186 password = get_string_param(argv[i]);
191 if (strequal(argv[i], "reboot")) {
197 ntstatus = net_make_ipc_connection_ex(c, c->opt_workgroup,
198 server_name, NULL, 0,
200 if (!NT_STATUS_IS_OK(ntstatus)) {
205 /* check if domain is a domain or a workgroup */
207 status = libnetapi_init(&ctx);
212 libnetapi_set_username(ctx, c->opt_user_name);
213 libnetapi_set_password(ctx, c->opt_password);
215 status = NetJoinDomain(server_name, domain_name, account_ou,
216 Account, password, join_flags);
218 printf("Failed to join domain: %s\n",
219 libnetapi_get_error_string(ctx, status));
224 c->opt_comment = "Shutting down due to a domain membership "
226 c->opt_reboot = true;
229 ret = run_rpc_command(c, cli, PI_INITSHUTDOWN, 0,
230 rpc_init_shutdown_internals,
236 ret = run_rpc_command(c, cli, PI_WINREG, 0,
237 rpc_reg_shutdown_internals,
252 int net_dom(struct net_context *c, int argc, const char **argv)
254 struct functable func[] = {
255 {"JOIN", net_dom_join},
256 {"UNJOIN", net_dom_unjoin},
257 {"HELP", net_help_dom},
261 return net_run_function(c, argc, argv, func, net_dom_usage);