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 const char *server_name = NULL;
46 const char *account = NULL;
47 const char *password = NULL;
48 uint32_t unjoin_flags = WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE |
49 WKSSVC_JOIN_FLAGS_JOIN_TYPE;
50 struct cli_state *cli = NULL;
53 NET_API_STATUS status;
58 return net_dom_usage(c, argc, argv);
62 server_name = c->opt_host;
65 for (i=0; i<argc; i++) {
66 if (strnequal(argv[i], "account", strlen("account"))) {
67 account = get_string_param(argv[i]);
72 if (strnequal(argv[i], "password", strlen("password"))) {
73 password = get_string_param(argv[i]);
78 if (strequal(argv[i], "reboot")) {
84 ntstatus = net_make_ipc_connection_ex(c, c->opt_workgroup,
87 if (!NT_STATUS_IS_OK(ntstatus)) {
92 status = NetUnjoinDomain(server_name, account, password, unjoin_flags);
94 printf("Failed to unjoin domain: %s\n",
95 libnetapi_get_error_string(c->netapi_ctx, status));
100 c->opt_comment = "Shutting down due to a domain membership "
102 c->opt_reboot = true;
105 ret = run_rpc_command(c, cli, PI_INITSHUTDOWN, 0,
106 rpc_init_shutdown_internals,
112 ret = run_rpc_command(c, cli, PI_WINREG, 0,
113 rpc_reg_shutdown_internals,
128 static int net_dom_join(struct net_context *c, int argc, const char **argv)
130 const char *server_name = NULL;
131 const char *domain_name = NULL;
132 const char *account_ou = NULL;
133 const char *Account = NULL;
134 const char *password = NULL;
135 uint32_t join_flags = WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE |
136 WKSSVC_JOIN_FLAGS_JOIN_TYPE;
137 struct cli_state *cli = NULL;
140 NET_API_STATUS status;
145 return net_dom_usage(c, argc, argv);
149 server_name = c->opt_host;
153 join_flags |= WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED;
156 for (i=0; i<argc; i++) {
157 if (strnequal(argv[i], "ou", strlen("ou"))) {
158 account_ou = get_string_param(argv[i]);
163 if (strnequal(argv[i], "domain", strlen("domain"))) {
164 domain_name = get_string_param(argv[i]);
169 if (strnequal(argv[i], "account", strlen("account"))) {
170 Account = get_string_param(argv[i]);
175 if (strnequal(argv[i], "password", strlen("password"))) {
176 password = get_string_param(argv[i]);
181 if (strequal(argv[i], "reboot")) {
187 ntstatus = net_make_ipc_connection_ex(c, c->opt_workgroup,
188 server_name, NULL, 0,
190 if (!NT_STATUS_IS_OK(ntstatus)) {
195 /* check if domain is a domain or a workgroup */
197 status = NetJoinDomain(server_name, domain_name, account_ou,
198 Account, password, join_flags);
200 printf("Failed to join domain: %s\n",
201 libnetapi_get_error_string(c->netapi_ctx, status));
206 c->opt_comment = "Shutting down due to a domain membership "
208 c->opt_reboot = true;
211 ret = run_rpc_command(c, cli, PI_INITSHUTDOWN, 0,
212 rpc_init_shutdown_internals,
218 ret = run_rpc_command(c, cli, PI_WINREG, 0,
219 rpc_reg_shutdown_internals,
234 int net_dom(struct net_context *c, int argc, const char **argv)
236 NET_API_STATUS status;
238 struct functable func[] = {
239 {"JOIN", net_dom_join},
240 {"UNJOIN", net_dom_unjoin},
241 {"HELP", net_help_dom},
245 status = libnetapi_init(&c->netapi_ctx);
250 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
251 libnetapi_set_password(c->netapi_ctx, c->opt_password);
253 return net_run_function(c, argc, argv, func, net_dom_usage);