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"
23 #include "../librpc/gen_ndr/ndr_winreg.h"
24 #include "lib/netapi/netapi.h"
26 int net_dom_usage(struct net_context
*c
, int argc
, const char **argv
)
31 "<domain=DOMAIN> <ou=OU> <account=ACCOUNT> "
32 "<password=PASSWORD> <reboot>\n Join a remote machine\n"));
36 "<account=ACCOUNT> <password=PASSWORD> <reboot>\n"
37 " Unjoin a remote machine\n"));
40 _("net dom renamecomputer "
42 "<account=ACCOUNT> <password=PASSWORD> <reboot>\n"
43 " Rename joined computer\n"));
48 static int net_dom_unjoin(struct net_context
*c
, int argc
, const char **argv
)
50 const char *server_name
= NULL
;
51 const char *account
= NULL
;
52 const char *password
= NULL
;
53 uint32_t unjoin_flags
= NETSETUP_ACCT_DELETE
|
54 NETSETUP_JOIN_DOMAIN
|
55 NETSETUP_IGNORE_UNSUPPORTED_FLAGS
;
56 struct cli_state
*cli
= NULL
;
57 bool do_reboot
= false;
59 NET_API_STATUS status
;
63 if (argc
< 1 || c
->display_usage
) {
64 return net_dom_usage(c
, argc
, argv
);
68 server_name
= c
->opt_host
;
71 for (i
=0; i
<argc
; i
++) {
72 if (strnequal(argv
[i
], "account", strlen("account"))) {
73 account
= get_string_param(argv
[i
]);
78 if (strnequal(argv
[i
], "password", strlen("password"))) {
79 password
= get_string_param(argv
[i
]);
84 if (strequal(argv
[i
], "reboot")) {
90 ntstatus
= net_make_ipc_connection_ex(c
, c
->opt_workgroup
,
93 if (!NT_STATUS_IS_OK(ntstatus
)) {
98 status
= NetUnjoinDomain(server_name
, account
, password
, unjoin_flags
);
100 printf(_("Failed to unjoin domain: %s\n"),
101 libnetapi_get_error_string(c
->netapi_ctx
, status
));
106 c
->opt_comment
= _("Shutting down due to a domain membership "
108 c
->opt_reboot
= true;
111 ret
= run_rpc_command(c
, cli
,
112 &ndr_table_initshutdown
.syntax_id
,
113 0, rpc_init_shutdown_internals
,
119 ret
= run_rpc_command(c
, cli
, &ndr_table_winreg
.syntax_id
, 0,
120 rpc_reg_shutdown_internals
,
135 static int net_dom_join(struct net_context
*c
, int argc
, const char **argv
)
137 const char *server_name
= NULL
;
138 const char *domain_name
= NULL
;
139 const char *account_ou
= NULL
;
140 const char *Account
= NULL
;
141 const char *password
= NULL
;
142 uint32_t join_flags
= NETSETUP_ACCT_CREATE
|
143 NETSETUP_JOIN_DOMAIN
;
144 struct cli_state
*cli
= NULL
;
145 bool do_reboot
= false;
147 NET_API_STATUS status
;
151 if (argc
< 1 || c
->display_usage
) {
152 return net_dom_usage(c
, argc
, argv
);
156 server_name
= c
->opt_host
;
160 join_flags
|= NETSETUP_DOMAIN_JOIN_IF_JOINED
;
163 for (i
=0; i
<argc
; i
++) {
164 if (strnequal(argv
[i
], "ou", strlen("ou"))) {
165 account_ou
= get_string_param(argv
[i
]);
170 if (strnequal(argv
[i
], "domain", strlen("domain"))) {
171 domain_name
= get_string_param(argv
[i
]);
176 if (strnequal(argv
[i
], "account", strlen("account"))) {
177 Account
= get_string_param(argv
[i
]);
182 if (strnequal(argv
[i
], "password", strlen("password"))) {
183 password
= get_string_param(argv
[i
]);
188 if (strequal(argv
[i
], "reboot")) {
194 ntstatus
= net_make_ipc_connection_ex(c
, c
->opt_workgroup
,
195 server_name
, NULL
, 0,
197 if (!NT_STATUS_IS_OK(ntstatus
)) {
202 /* check if domain is a domain or a workgroup */
204 status
= NetJoinDomain(server_name
, domain_name
, account_ou
,
205 Account
, password
, join_flags
);
207 printf(_("Failed to join domain: %s\n"),
208 libnetapi_get_error_string(c
->netapi_ctx
, status
));
213 c
->opt_comment
= _("Shutting down due to a domain membership "
215 c
->opt_reboot
= true;
218 ret
= run_rpc_command(c
, cli
, &ndr_table_initshutdown
.syntax_id
, 0,
219 rpc_init_shutdown_internals
,
225 ret
= run_rpc_command(c
, cli
, &ndr_table_winreg
.syntax_id
, 0,
226 rpc_reg_shutdown_internals
,
241 static int net_dom_renamecomputer(struct net_context
*c
, int argc
, const char **argv
)
243 const char *server_name
= NULL
;
244 const char *account
= NULL
;
245 const char *password
= NULL
;
246 const char *newname
= NULL
;
247 uint32_t rename_options
= NETSETUP_ACCT_CREATE
;
248 struct cli_state
*cli
= NULL
;
249 bool do_reboot
= false;
251 NET_API_STATUS status
;
255 if (argc
< 1 || c
->display_usage
) {
256 return net_dom_usage(c
, argc
, argv
);
260 server_name
= c
->opt_host
;
263 for (i
=0; i
<argc
; i
++) {
264 if (strnequal(argv
[i
], "account", strlen("account"))) {
265 account
= get_string_param(argv
[i
]);
270 if (strnequal(argv
[i
], "password", strlen("password"))) {
271 password
= get_string_param(argv
[i
]);
276 if (strnequal(argv
[i
], "newname", strlen("newname"))) {
277 newname
= get_string_param(argv
[i
]);
282 if (strequal(argv
[i
], "reboot")) {
288 ntstatus
= net_make_ipc_connection_ex(c
, c
->opt_workgroup
,
289 server_name
, NULL
, 0,
291 if (!NT_STATUS_IS_OK(ntstatus
)) {
296 status
= NetRenameMachineInDomain(server_name
, newname
,
297 account
, password
, rename_options
);
299 printf(_("Failed to rename machine: "));
300 if (status
== W_ERROR_V(WERR_SETUP_NOT_JOINED
)) {
301 printf(_("Computer is not joined to a Domain\n"));
305 libnetapi_get_error_string(c
->netapi_ctx
, status
));
310 c
->opt_comment
= _("Shutting down due to a computer rename");
311 c
->opt_reboot
= true;
314 ret
= run_rpc_command(c
, cli
,
315 &ndr_table_initshutdown
.syntax_id
,
316 0, rpc_init_shutdown_internals
,
322 ret
= run_rpc_command(c
, cli
, &ndr_table_winreg
.syntax_id
, 0,
323 rpc_reg_shutdown_internals
,
338 int net_dom(struct net_context
*c
, int argc
, const char **argv
)
340 NET_API_STATUS status
;
342 struct functable func
[] = {
347 N_("Join a remote machine"),
348 N_("net dom join <domain=DOMAIN> <ou=OU> "
349 "<account=ACCOUNT> <password=PASSWORD> <reboot>\n"
350 " Join a remote machine")
356 N_("Unjoin a remote machine"),
357 N_("net dom unjoin <account=ACCOUNT> "
358 "<password=PASSWORD> <reboot>\n"
359 " Unjoin a remote machine")
363 net_dom_renamecomputer
,
365 N_("Rename a computer that is joined to a domain"),
366 N_("net dom renamecomputer <newname=NEWNAME> "
367 "<account=ACCOUNT> <password=PASSWORD> "
369 " Rename joined computer")
372 {NULL
, NULL
, 0, NULL
, NULL
}
375 status
= libnetapi_init(&c
->netapi_ctx
);
380 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
381 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
382 if (c
->opt_kerberos
) {
383 libnetapi_set_use_kerberos(c
->netapi_ctx
);
386 return net_run_function(c
, argc
, argv
, "net dom", func
);