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"
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");
31 d_printf("usage: net dom renamecomputer "
33 "<account=ACCOUNT> <password=PASSWORD> <reboot>\n"
34 " Rename joined computer\n");
39 static int net_dom_unjoin(struct net_context
*c
, int argc
, const char **argv
)
41 const char *server_name
= NULL
;
42 const char *account
= NULL
;
43 const char *password
= NULL
;
44 uint32_t unjoin_flags
= NETSETUP_ACCT_DELETE
|
45 NETSETUP_JOIN_DOMAIN
|
46 NETSETUP_IGNORE_UNSUPPORTED_FLAGS
;
47 struct cli_state
*cli
= NULL
;
48 bool do_reboot
= false;
50 NET_API_STATUS status
;
54 if (argc
< 1 || c
->display_usage
) {
55 return net_dom_usage(c
, argc
, argv
);
59 server_name
= c
->opt_host
;
62 for (i
=0; i
<argc
; i
++) {
63 if (strnequal(argv
[i
], "account", strlen("account"))) {
64 account
= get_string_param(argv
[i
]);
69 if (strnequal(argv
[i
], "password", strlen("password"))) {
70 password
= get_string_param(argv
[i
]);
75 if (strequal(argv
[i
], "reboot")) {
81 ntstatus
= net_make_ipc_connection_ex(c
, c
->opt_workgroup
,
84 if (!NT_STATUS_IS_OK(ntstatus
)) {
89 status
= NetUnjoinDomain(server_name
, account
, password
, unjoin_flags
);
91 printf("Failed to unjoin domain: %s\n",
92 libnetapi_get_error_string(c
->netapi_ctx
, status
));
97 c
->opt_comment
= "Shutting down due to a domain membership "
102 ret
= run_rpc_command(c
, cli
,
103 &ndr_table_initshutdown
.syntax_id
,
104 0, rpc_init_shutdown_internals
,
110 ret
= run_rpc_command(c
, cli
, &ndr_table_winreg
.syntax_id
, 0,
111 rpc_reg_shutdown_internals
,
126 static int net_dom_join(struct net_context
*c
, int argc
, const char **argv
)
128 const char *server_name
= NULL
;
129 const char *domain_name
= NULL
;
130 const char *account_ou
= NULL
;
131 const char *Account
= NULL
;
132 const char *password
= NULL
;
133 uint32_t join_flags
= NETSETUP_ACCT_CREATE
|
134 NETSETUP_JOIN_DOMAIN
;
135 struct cli_state
*cli
= NULL
;
136 bool do_reboot
= false;
138 NET_API_STATUS status
;
142 if (argc
< 1 || c
->display_usage
) {
143 return net_dom_usage(c
, argc
, argv
);
147 server_name
= c
->opt_host
;
151 join_flags
|= NETSETUP_DOMAIN_JOIN_IF_JOINED
;
154 for (i
=0; i
<argc
; i
++) {
155 if (strnequal(argv
[i
], "ou", strlen("ou"))) {
156 account_ou
= get_string_param(argv
[i
]);
161 if (strnequal(argv
[i
], "domain", strlen("domain"))) {
162 domain_name
= get_string_param(argv
[i
]);
167 if (strnequal(argv
[i
], "account", strlen("account"))) {
168 Account
= get_string_param(argv
[i
]);
173 if (strnequal(argv
[i
], "password", strlen("password"))) {
174 password
= get_string_param(argv
[i
]);
179 if (strequal(argv
[i
], "reboot")) {
185 ntstatus
= net_make_ipc_connection_ex(c
, c
->opt_workgroup
,
186 server_name
, NULL
, 0,
188 if (!NT_STATUS_IS_OK(ntstatus
)) {
193 /* check if domain is a domain or a workgroup */
195 status
= NetJoinDomain(server_name
, domain_name
, account_ou
,
196 Account
, password
, join_flags
);
198 printf("Failed to join domain: %s\n",
199 libnetapi_get_error_string(c
->netapi_ctx
, status
));
204 c
->opt_comment
= "Shutting down due to a domain membership "
206 c
->opt_reboot
= true;
209 ret
= run_rpc_command(c
, cli
, &ndr_table_initshutdown
.syntax_id
, 0,
210 rpc_init_shutdown_internals
,
216 ret
= run_rpc_command(c
, cli
, &ndr_table_winreg
.syntax_id
, 0,
217 rpc_reg_shutdown_internals
,
232 static int net_dom_renamecomputer(struct net_context
*c
, int argc
, const char **argv
)
234 const char *server_name
= NULL
;
235 const char *account
= NULL
;
236 const char *password
= NULL
;
237 const char *newname
= NULL
;
238 uint32_t rename_options
= NETSETUP_ACCT_CREATE
;
239 struct cli_state
*cli
= NULL
;
240 bool do_reboot
= false;
242 NET_API_STATUS status
;
246 if (argc
< 1 || c
->display_usage
) {
247 return net_dom_usage(c
, argc
, argv
);
251 server_name
= c
->opt_host
;
254 for (i
=0; i
<argc
; i
++) {
255 if (strnequal(argv
[i
], "account", strlen("account"))) {
256 account
= get_string_param(argv
[i
]);
261 if (strnequal(argv
[i
], "password", strlen("password"))) {
262 password
= get_string_param(argv
[i
]);
267 if (strnequal(argv
[i
], "newname", strlen("newname"))) {
268 newname
= get_string_param(argv
[i
]);
273 if (strequal(argv
[i
], "reboot")) {
279 ntstatus
= net_make_ipc_connection_ex(c
, c
->opt_workgroup
,
280 server_name
, NULL
, 0,
282 if (!NT_STATUS_IS_OK(ntstatus
)) {
287 status
= NetRenameMachineInDomain(server_name
, newname
,
288 account
, password
, rename_options
);
290 printf("Failed to rename machine: ");
291 if (status
== W_ERROR_V(WERR_SETUP_NOT_JOINED
)) {
292 printf("Computer is not joined to a Domain\n");
296 libnetapi_get_error_string(c
->netapi_ctx
, status
));
301 c
->opt_comment
= "Shutting down due to a computer rename";
302 c
->opt_reboot
= true;
305 ret
= run_rpc_command(c
, cli
,
306 &ndr_table_initshutdown
.syntax_id
,
307 0, rpc_init_shutdown_internals
,
313 ret
= run_rpc_command(c
, cli
, &ndr_table_winreg
.syntax_id
, 0,
314 rpc_reg_shutdown_internals
,
329 int net_dom(struct net_context
*c
, int argc
, const char **argv
)
331 NET_API_STATUS status
;
333 struct functable func
[] = {
338 "Join a remote machine",
339 "net dom join <domain=DOMAIN> <ou=OU> "
340 "<account=ACCOUNT> <password=PASSWORD> <reboot>\n"
341 " Join a remote machine"
347 "Unjoin a remote machine",
348 "net dom unjoin <account=ACCOUNT> <password=PASSWORD> "
350 " Unjoin a remote machine"
354 net_dom_renamecomputer
,
356 "Rename a computer that is joined to a domain",
357 "net dom renamecomputer <newname=NEWNAME> "
358 "<account=ACCOUNT> <password=PASSWORD> "
360 " Rename joined computer"
363 {NULL
, NULL
, 0, NULL
, NULL
}
366 status
= libnetapi_init(&c
->netapi_ctx
);
371 libnetapi_set_username(c
->netapi_ctx
,
372 get_cmdline_auth_info_username(c
->auth_info
));
373 libnetapi_set_password(c
->netapi_ctx
,
374 get_cmdline_auth_info_password(c
->auth_info
));
375 if (get_cmdline_auth_info_use_kerberos(c
->auth_info
)) {
376 libnetapi_set_use_kerberos(c
->netapi_ctx
);
379 return net_run_function(c
, argc
, argv
, "net dom", func
);