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 = WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE |
41 WKSSVC_JOIN_FLAGS_JOIN_TYPE;
42 struct cli_state *cli = NULL;
45 NET_API_STATUS status;
49 if (argc < 1 || c->display_usage) {
50 return net_dom_usage(c, argc, argv);
54 server_name = c->opt_host;
57 for (i=0; i<argc; i++) {
58 if (strnequal(argv[i], "account", strlen("account"))) {
59 account = get_string_param(argv[i]);
64 if (strnequal(argv[i], "password", strlen("password"))) {
65 password = get_string_param(argv[i]);
70 if (strequal(argv[i], "reboot")) {
76 ntstatus = net_make_ipc_connection_ex(c, c->opt_workgroup,
79 if (!NT_STATUS_IS_OK(ntstatus)) {
84 status = NetUnjoinDomain(server_name, account, password, unjoin_flags);
86 printf("Failed to unjoin domain: %s\n",
87 libnetapi_get_error_string(c->netapi_ctx, status));
92 c->opt_comment = "Shutting down due to a domain membership "
97 ret = run_rpc_command(c, cli, PI_INITSHUTDOWN, 0,
98 rpc_init_shutdown_internals,
104 ret = run_rpc_command(c, cli, PI_WINREG, 0,
105 rpc_reg_shutdown_internals,
120 static int net_dom_join(struct net_context *c, int argc, const char **argv)
122 const char *server_name = NULL;
123 const char *domain_name = NULL;
124 const char *account_ou = NULL;
125 const char *Account = NULL;
126 const char *password = NULL;
127 uint32_t join_flags = WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE |
128 WKSSVC_JOIN_FLAGS_JOIN_TYPE;
129 struct cli_state *cli = NULL;
132 NET_API_STATUS status;
136 if (argc < 1 || c->display_usage) {
137 return net_dom_usage(c, argc, argv);
141 server_name = c->opt_host;
145 join_flags |= WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED;
148 for (i=0; i<argc; i++) {
149 if (strnequal(argv[i], "ou", strlen("ou"))) {
150 account_ou = get_string_param(argv[i]);
155 if (strnequal(argv[i], "domain", strlen("domain"))) {
156 domain_name = get_string_param(argv[i]);
161 if (strnequal(argv[i], "account", strlen("account"))) {
162 Account = get_string_param(argv[i]);
167 if (strnequal(argv[i], "password", strlen("password"))) {
168 password = get_string_param(argv[i]);
173 if (strequal(argv[i], "reboot")) {
179 ntstatus = net_make_ipc_connection_ex(c, c->opt_workgroup,
180 server_name, NULL, 0,
182 if (!NT_STATUS_IS_OK(ntstatus)) {
187 /* check if domain is a domain or a workgroup */
189 status = NetJoinDomain(server_name, domain_name, account_ou,
190 Account, password, join_flags);
192 printf("Failed to join domain: %s\n",
193 libnetapi_get_error_string(c->netapi_ctx, status));
198 c->opt_comment = "Shutting down due to a domain membership "
200 c->opt_reboot = true;
203 ret = run_rpc_command(c, cli, PI_INITSHUTDOWN, 0,
204 rpc_init_shutdown_internals,
210 ret = run_rpc_command(c, cli, PI_WINREG, 0,
211 rpc_reg_shutdown_internals,
226 int net_dom(struct net_context *c, int argc, const char **argv)
228 NET_API_STATUS status;
230 struct functable3 func[] = {
235 "Join a remote machine",
236 "net dom join <domain=DOMAIN> <ou=OU> "
237 "<account=ACCOUNT> <password=PASSWORD> <reboot>\n"
238 " Join a remote machine"
244 "Unjoin a remote machine",
245 "net dom unjoin <account=ACCOUNT> <password=PASSWORD> "
247 " Unjoin a remote machine"
249 {NULL, NULL, 0, NULL, NULL}
252 status = libnetapi_init(&c->netapi_ctx);
257 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
258 libnetapi_set_password(c->netapi_ctx, c->opt_password);
260 return net_run_function3(c, argc, argv, "net dom", func);