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"
22 #include "../librpc/gen_ndr/cli_initshutdown.h"
24 int net_dom_usage(struct net_context
*c
, int argc
, const char **argv
)
29 "<domain=DOMAIN> <ou=OU> <account=ACCOUNT> "
30 "<password=PASSWORD> <reboot>\n Join a remote machine\n"));
34 "<account=ACCOUNT> <password=PASSWORD> <reboot>\n"
35 " Unjoin a remote machine\n"));
38 _("net dom renamecomputer "
40 "<account=ACCOUNT> <password=PASSWORD> <reboot>\n"
41 " Rename joined computer\n"));
46 static int net_dom_unjoin(struct net_context
*c
, int argc
, const char **argv
)
48 const char *server_name
= NULL
;
49 const char *account
= NULL
;
50 const char *password
= NULL
;
51 uint32_t unjoin_flags
= NETSETUP_ACCT_DELETE
|
52 NETSETUP_JOIN_DOMAIN
|
53 NETSETUP_IGNORE_UNSUPPORTED_FLAGS
;
54 struct cli_state
*cli
= NULL
;
55 bool do_reboot
= false;
57 NET_API_STATUS status
;
61 if (argc
< 1 || c
->display_usage
) {
62 return net_dom_usage(c
, argc
, argv
);
66 server_name
= c
->opt_host
;
69 for (i
=0; i
<argc
; i
++) {
70 if (strnequal(argv
[i
], "account", strlen("account"))) {
71 account
= get_string_param(argv
[i
]);
76 if (strnequal(argv
[i
], "password", strlen("password"))) {
77 password
= get_string_param(argv
[i
]);
82 if (strequal(argv
[i
], "reboot")) {
88 ntstatus
= net_make_ipc_connection_ex(c
, c
->opt_workgroup
,
91 if (!NT_STATUS_IS_OK(ntstatus
)) {
96 status
= NetUnjoinDomain(server_name
, account
, password
, unjoin_flags
);
98 printf(_("Failed to unjoin domain: %s\n"),
99 libnetapi_get_error_string(c
->netapi_ctx
, status
));
104 c
->opt_comment
= _("Shutting down due to a domain membership "
106 c
->opt_reboot
= true;
109 ret
= run_rpc_command(c
, cli
,
110 &ndr_table_initshutdown
.syntax_id
,
111 0, rpc_init_shutdown_internals
,
117 ret
= run_rpc_command(c
, cli
, &ndr_table_winreg
.syntax_id
, 0,
118 rpc_reg_shutdown_internals
,
133 static int net_dom_join(struct net_context
*c
, int argc
, const char **argv
)
135 const char *server_name
= NULL
;
136 const char *domain_name
= NULL
;
137 const char *account_ou
= NULL
;
138 const char *Account
= NULL
;
139 const char *password
= NULL
;
140 uint32_t join_flags
= NETSETUP_ACCT_CREATE
|
141 NETSETUP_JOIN_DOMAIN
;
142 struct cli_state
*cli
= NULL
;
143 bool do_reboot
= false;
145 NET_API_STATUS status
;
149 if (argc
< 1 || c
->display_usage
) {
150 return net_dom_usage(c
, argc
, argv
);
154 server_name
= c
->opt_host
;
158 join_flags
|= NETSETUP_DOMAIN_JOIN_IF_JOINED
;
161 for (i
=0; i
<argc
; i
++) {
162 if (strnequal(argv
[i
], "ou", strlen("ou"))) {
163 account_ou
= get_string_param(argv
[i
]);
168 if (strnequal(argv
[i
], "domain", strlen("domain"))) {
169 domain_name
= get_string_param(argv
[i
]);
174 if (strnequal(argv
[i
], "account", strlen("account"))) {
175 Account
= get_string_param(argv
[i
]);
180 if (strnequal(argv
[i
], "password", strlen("password"))) {
181 password
= get_string_param(argv
[i
]);
186 if (strequal(argv
[i
], "reboot")) {
192 ntstatus
= net_make_ipc_connection_ex(c
, c
->opt_workgroup
,
193 server_name
, NULL
, 0,
195 if (!NT_STATUS_IS_OK(ntstatus
)) {
200 /* check if domain is a domain or a workgroup */
202 status
= NetJoinDomain(server_name
, domain_name
, account_ou
,
203 Account
, password
, join_flags
);
205 printf(_("Failed to join domain: %s\n"),
206 libnetapi_get_error_string(c
->netapi_ctx
, status
));
211 c
->opt_comment
= _("Shutting down due to a domain membership "
213 c
->opt_reboot
= true;
216 ret
= run_rpc_command(c
, cli
, &ndr_table_initshutdown
.syntax_id
, 0,
217 rpc_init_shutdown_internals
,
223 ret
= run_rpc_command(c
, cli
, &ndr_table_winreg
.syntax_id
, 0,
224 rpc_reg_shutdown_internals
,
239 static int net_dom_renamecomputer(struct net_context
*c
, int argc
, const char **argv
)
241 const char *server_name
= NULL
;
242 const char *account
= NULL
;
243 const char *password
= NULL
;
244 const char *newname
= NULL
;
245 uint32_t rename_options
= NETSETUP_ACCT_CREATE
;
246 struct cli_state
*cli
= NULL
;
247 bool do_reboot
= false;
249 NET_API_STATUS status
;
253 if (argc
< 1 || c
->display_usage
) {
254 return net_dom_usage(c
, argc
, argv
);
258 server_name
= c
->opt_host
;
261 for (i
=0; i
<argc
; i
++) {
262 if (strnequal(argv
[i
], "account", strlen("account"))) {
263 account
= get_string_param(argv
[i
]);
268 if (strnequal(argv
[i
], "password", strlen("password"))) {
269 password
= get_string_param(argv
[i
]);
274 if (strnequal(argv
[i
], "newname", strlen("newname"))) {
275 newname
= get_string_param(argv
[i
]);
280 if (strequal(argv
[i
], "reboot")) {
286 ntstatus
= net_make_ipc_connection_ex(c
, c
->opt_workgroup
,
287 server_name
, NULL
, 0,
289 if (!NT_STATUS_IS_OK(ntstatus
)) {
294 status
= NetRenameMachineInDomain(server_name
, newname
,
295 account
, password
, rename_options
);
297 printf(_("Failed to rename machine: "));
298 if (status
== W_ERROR_V(WERR_SETUP_NOT_JOINED
)) {
299 printf(_("Computer is not joined to a Domain\n"));
303 libnetapi_get_error_string(c
->netapi_ctx
, status
));
308 c
->opt_comment
= _("Shutting down due to a computer rename");
309 c
->opt_reboot
= true;
312 ret
= run_rpc_command(c
, cli
,
313 &ndr_table_initshutdown
.syntax_id
,
314 0, rpc_init_shutdown_internals
,
320 ret
= run_rpc_command(c
, cli
, &ndr_table_winreg
.syntax_id
, 0,
321 rpc_reg_shutdown_internals
,
336 int net_dom(struct net_context
*c
, int argc
, const char **argv
)
338 NET_API_STATUS status
;
340 struct functable func
[] = {
345 N_("Join a remote machine"),
346 N_("net dom join <domain=DOMAIN> <ou=OU> "
347 "<account=ACCOUNT> <password=PASSWORD> <reboot>\n"
348 " Join a remote machine")
354 N_("Unjoin a remote machine"),
355 N_("net dom unjoin <account=ACCOUNT> "
356 "<password=PASSWORD> <reboot>\n"
357 " Unjoin a remote machine")
361 net_dom_renamecomputer
,
363 N_("Rename a computer that is joined to a domain"),
364 N_("net dom renamecomputer <newname=NEWNAME> "
365 "<account=ACCOUNT> <password=PASSWORD> "
367 " Rename joined computer")
370 {NULL
, NULL
, 0, NULL
, NULL
}
373 status
= libnetapi_init(&c
->netapi_ctx
);
378 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
379 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
380 if (c
->opt_kerberos
) {
381 libnetapi_set_use_kerberos(c
->netapi_ctx
);
384 return net_run_function(c
, argc
, argv
, "net dom", func
);