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
)
26 d_printf(_("usage: net dom join "
27 "<domain=DOMAIN> <ou=OU> <account=ACCOUNT> "
28 "<password=PASSWORD> <reboot>\n Join a remote machine\n"));
29 d_printf(_("usage: net dom unjoin "
30 "<account=ACCOUNT> <password=PASSWORD> <reboot>\n"
31 " Unjoin a remote machine\n"));
32 d_printf(_("usage: net dom renamecomputer "
34 "<account=ACCOUNT> <password=PASSWORD> <reboot>\n"
35 " Rename joined computer\n"));
40 static int net_dom_unjoin(struct net_context
*c
, int argc
, const char **argv
)
42 const char *server_name
= NULL
;
43 const char *account
= NULL
;
44 const char *password
= NULL
;
45 uint32_t unjoin_flags
= NETSETUP_ACCT_DELETE
|
46 NETSETUP_JOIN_DOMAIN
|
47 NETSETUP_IGNORE_UNSUPPORTED_FLAGS
;
48 struct cli_state
*cli
= NULL
;
49 bool do_reboot
= false;
51 NET_API_STATUS status
;
55 if (argc
< 1 || c
->display_usage
) {
56 return net_dom_usage(c
, argc
, argv
);
60 server_name
= c
->opt_host
;
63 for (i
=0; i
<argc
; i
++) {
64 if (strnequal(argv
[i
], "account", strlen("account"))) {
65 account
= get_string_param(argv
[i
]);
70 if (strnequal(argv
[i
], "password", strlen("password"))) {
71 password
= get_string_param(argv
[i
]);
76 if (strequal(argv
[i
], "reboot")) {
82 ntstatus
= net_make_ipc_connection_ex(c
, c
->opt_workgroup
,
85 if (!NT_STATUS_IS_OK(ntstatus
)) {
90 status
= NetUnjoinDomain(server_name
, account
, password
, unjoin_flags
);
92 printf(_("Failed to unjoin domain: %s\n"),
93 libnetapi_get_error_string(c
->netapi_ctx
, status
));
98 c
->opt_comment
= _("Shutting down due to a domain membership "
100 c
->opt_reboot
= true;
103 ret
= run_rpc_command(c
, cli
,
104 &ndr_table_initshutdown
.syntax_id
,
105 0, rpc_init_shutdown_internals
,
111 ret
= run_rpc_command(c
, cli
, &ndr_table_winreg
.syntax_id
, 0,
112 rpc_reg_shutdown_internals
,
127 static int net_dom_join(struct net_context
*c
, int argc
, const char **argv
)
129 const char *server_name
= NULL
;
130 const char *domain_name
= NULL
;
131 const char *account_ou
= NULL
;
132 const char *Account
= NULL
;
133 const char *password
= NULL
;
134 uint32_t join_flags
= NETSETUP_ACCT_CREATE
|
135 NETSETUP_JOIN_DOMAIN
;
136 struct cli_state
*cli
= NULL
;
137 bool do_reboot
= false;
139 NET_API_STATUS status
;
143 if (argc
< 1 || c
->display_usage
) {
144 return net_dom_usage(c
, argc
, argv
);
148 server_name
= c
->opt_host
;
152 join_flags
|= NETSETUP_DOMAIN_JOIN_IF_JOINED
;
155 for (i
=0; i
<argc
; i
++) {
156 if (strnequal(argv
[i
], "ou", strlen("ou"))) {
157 account_ou
= get_string_param(argv
[i
]);
162 if (strnequal(argv
[i
], "domain", strlen("domain"))) {
163 domain_name
= get_string_param(argv
[i
]);
168 if (strnequal(argv
[i
], "account", strlen("account"))) {
169 Account
= get_string_param(argv
[i
]);
174 if (strnequal(argv
[i
], "password", strlen("password"))) {
175 password
= get_string_param(argv
[i
]);
180 if (strequal(argv
[i
], "reboot")) {
186 ntstatus
= net_make_ipc_connection_ex(c
, c
->opt_workgroup
,
187 server_name
, NULL
, 0,
189 if (!NT_STATUS_IS_OK(ntstatus
)) {
194 /* check if domain is a domain or a workgroup */
196 status
= NetJoinDomain(server_name
, domain_name
, account_ou
,
197 Account
, password
, join_flags
);
199 printf(_("Failed to join domain: %s\n"),
200 libnetapi_get_error_string(c
->netapi_ctx
, status
));
205 c
->opt_comment
= _("Shutting down due to a domain membership "
207 c
->opt_reboot
= true;
210 ret
= run_rpc_command(c
, cli
, &ndr_table_initshutdown
.syntax_id
, 0,
211 rpc_init_shutdown_internals
,
217 ret
= run_rpc_command(c
, cli
, &ndr_table_winreg
.syntax_id
, 0,
218 rpc_reg_shutdown_internals
,
233 static int net_dom_renamecomputer(struct net_context
*c
, int argc
, const char **argv
)
235 const char *server_name
= NULL
;
236 const char *account
= NULL
;
237 const char *password
= NULL
;
238 const char *newname
= NULL
;
239 uint32_t rename_options
= NETSETUP_ACCT_CREATE
;
240 struct cli_state
*cli
= NULL
;
241 bool do_reboot
= false;
243 NET_API_STATUS status
;
247 if (argc
< 1 || c
->display_usage
) {
248 return net_dom_usage(c
, argc
, argv
);
252 server_name
= c
->opt_host
;
255 for (i
=0; i
<argc
; i
++) {
256 if (strnequal(argv
[i
], "account", strlen("account"))) {
257 account
= get_string_param(argv
[i
]);
262 if (strnequal(argv
[i
], "password", strlen("password"))) {
263 password
= get_string_param(argv
[i
]);
268 if (strnequal(argv
[i
], "newname", strlen("newname"))) {
269 newname
= get_string_param(argv
[i
]);
274 if (strequal(argv
[i
], "reboot")) {
280 ntstatus
= net_make_ipc_connection_ex(c
, c
->opt_workgroup
,
281 server_name
, NULL
, 0,
283 if (!NT_STATUS_IS_OK(ntstatus
)) {
288 status
= NetRenameMachineInDomain(server_name
, newname
,
289 account
, password
, rename_options
);
291 printf(_("Failed to rename machine: "));
292 if (status
== W_ERROR_V(WERR_SETUP_NOT_JOINED
)) {
293 printf(_("Computer is not joined to a Domain\n"));
297 libnetapi_get_error_string(c
->netapi_ctx
, status
));
302 c
->opt_comment
= _("Shutting down due to a computer rename");
303 c
->opt_reboot
= true;
306 ret
= run_rpc_command(c
, cli
,
307 &ndr_table_initshutdown
.syntax_id
,
308 0, rpc_init_shutdown_internals
,
314 ret
= run_rpc_command(c
, cli
, &ndr_table_winreg
.syntax_id
, 0,
315 rpc_reg_shutdown_internals
,
330 int net_dom(struct net_context
*c
, int argc
, const char **argv
)
332 NET_API_STATUS status
;
334 struct functable func
[] = {
339 N_("Join a remote machine"),
340 N_("net dom join <domain=DOMAIN> <ou=OU> "
341 "<account=ACCOUNT> <password=PASSWORD> <reboot>\n"
342 " Join a remote machine")
348 N_("Unjoin a remote machine"),
349 N_("net dom unjoin <account=ACCOUNT> "
350 "<password=PASSWORD> <reboot>\n"
351 " Unjoin a remote machine")
355 net_dom_renamecomputer
,
357 N_("Rename a computer that is joined to a domain"),
358 N_("net dom renamecomputer <newname=NEWNAME> "
359 "<account=ACCOUNT> <password=PASSWORD> "
361 " Rename joined computer")
364 {NULL
, NULL
, 0, NULL
, NULL
}
367 status
= libnetapi_init(&c
->netapi_ctx
);
372 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
373 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
374 if (c
->opt_kerberos
) {
375 libnetapi_set_use_kerberos(c
->netapi_ctx
);
378 return net_run_function(c
, argc
, argv
, "net dom", func
);