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"
25 int net_dom_usage(struct net_context
*c
, int argc
, const char **argv
)
30 "<domain=DOMAIN> <ou=OU> <account=ACCOUNT> "
31 "<password=PASSWORD> <reboot>\n Join a remote machine\n"));
35 "<account=ACCOUNT> <password=PASSWORD> <reboot>\n"
36 " Unjoin a remote machine\n"));
39 _("net dom renamecomputer "
41 "<account=ACCOUNT> <password=PASSWORD> <reboot>\n"
42 " Rename joined computer\n"));
47 static int net_dom_unjoin(struct net_context
*c
, int argc
, const char **argv
)
49 const char *server_name
= NULL
;
50 const char *account
= NULL
;
51 const char *password
= NULL
;
52 uint32_t unjoin_flags
= NETSETUP_ACCT_DELETE
|
53 NETSETUP_JOIN_DOMAIN
|
54 NETSETUP_IGNORE_UNSUPPORTED_FLAGS
;
55 struct cli_state
*cli
= NULL
;
56 bool do_reboot
= false;
58 NET_API_STATUS status
;
62 if (argc
< 1 || c
->display_usage
) {
63 return net_dom_usage(c
, argc
, argv
);
67 server_name
= c
->opt_host
;
70 for (i
=0; i
<argc
; i
++) {
71 if (strnequal(argv
[i
], "account", strlen("account"))) {
72 account
= get_string_param(argv
[i
]);
77 if (strnequal(argv
[i
], "password", strlen("password"))) {
78 password
= get_string_param(argv
[i
]);
83 if (strequal(argv
[i
], "reboot")) {
89 ntstatus
= net_make_ipc_connection_ex(c
, c
->opt_workgroup
,
92 if (!NT_STATUS_IS_OK(ntstatus
)) {
97 status
= NetUnjoinDomain(server_name
, account
, password
, unjoin_flags
);
99 printf(_("Failed to unjoin domain: %s\n"),
100 libnetapi_get_error_string(c
->netapi_ctx
, status
));
105 c
->opt_comment
= _("Shutting down due to a domain membership "
107 c
->opt_reboot
= true;
110 ret
= run_rpc_command(c
, cli
,
111 &ndr_table_initshutdown
.syntax_id
,
112 0, rpc_init_shutdown_internals
,
118 ret
= run_rpc_command(c
, cli
, &ndr_table_winreg
.syntax_id
, 0,
119 rpc_reg_shutdown_internals
,
134 static int net_dom_join(struct net_context
*c
, int argc
, const char **argv
)
136 const char *server_name
= NULL
;
137 const char *domain_name
= NULL
;
138 const char *account_ou
= NULL
;
139 const char *Account
= NULL
;
140 const char *password
= NULL
;
141 uint32_t join_flags
= NETSETUP_ACCT_CREATE
|
142 NETSETUP_JOIN_DOMAIN
;
143 struct cli_state
*cli
= NULL
;
144 bool do_reboot
= false;
146 NET_API_STATUS status
;
150 if (argc
< 1 || c
->display_usage
) {
151 return net_dom_usage(c
, argc
, argv
);
155 server_name
= c
->opt_host
;
159 join_flags
|= NETSETUP_DOMAIN_JOIN_IF_JOINED
;
162 for (i
=0; i
<argc
; i
++) {
163 if (strnequal(argv
[i
], "ou", strlen("ou"))) {
164 account_ou
= get_string_param(argv
[i
]);
169 if (strnequal(argv
[i
], "domain", strlen("domain"))) {
170 domain_name
= get_string_param(argv
[i
]);
175 if (strnequal(argv
[i
], "account", strlen("account"))) {
176 Account
= get_string_param(argv
[i
]);
181 if (strnequal(argv
[i
], "password", strlen("password"))) {
182 password
= get_string_param(argv
[i
]);
187 if (strequal(argv
[i
], "reboot")) {
193 ntstatus
= net_make_ipc_connection_ex(c
, c
->opt_workgroup
,
194 server_name
, NULL
, 0,
196 if (!NT_STATUS_IS_OK(ntstatus
)) {
201 /* check if domain is a domain or a workgroup */
203 status
= NetJoinDomain(server_name
, domain_name
, account_ou
,
204 Account
, password
, join_flags
);
206 printf(_("Failed to join domain: %s\n"),
207 libnetapi_get_error_string(c
->netapi_ctx
, status
));
212 c
->opt_comment
= _("Shutting down due to a domain membership "
214 c
->opt_reboot
= true;
217 ret
= run_rpc_command(c
, cli
, &ndr_table_initshutdown
.syntax_id
, 0,
218 rpc_init_shutdown_internals
,
224 ret
= run_rpc_command(c
, cli
, &ndr_table_winreg
.syntax_id
, 0,
225 rpc_reg_shutdown_internals
,
240 static int net_dom_renamecomputer(struct net_context
*c
, int argc
, const char **argv
)
242 const char *server_name
= NULL
;
243 const char *account
= NULL
;
244 const char *password
= NULL
;
245 const char *newname
= NULL
;
246 uint32_t rename_options
= NETSETUP_ACCT_CREATE
;
247 struct cli_state
*cli
= NULL
;
248 bool do_reboot
= false;
250 NET_API_STATUS status
;
254 if (argc
< 1 || c
->display_usage
) {
255 return net_dom_usage(c
, argc
, argv
);
259 server_name
= c
->opt_host
;
262 for (i
=0; i
<argc
; i
++) {
263 if (strnequal(argv
[i
], "account", strlen("account"))) {
264 account
= get_string_param(argv
[i
]);
269 if (strnequal(argv
[i
], "password", strlen("password"))) {
270 password
= get_string_param(argv
[i
]);
275 if (strnequal(argv
[i
], "newname", strlen("newname"))) {
276 newname
= get_string_param(argv
[i
]);
281 if (strequal(argv
[i
], "reboot")) {
287 ntstatus
= net_make_ipc_connection_ex(c
, c
->opt_workgroup
,
288 server_name
, NULL
, 0,
290 if (!NT_STATUS_IS_OK(ntstatus
)) {
295 status
= NetRenameMachineInDomain(server_name
, newname
,
296 account
, password
, rename_options
);
298 printf(_("Failed to rename machine: "));
299 if (status
== W_ERROR_V(WERR_SETUP_NOT_JOINED
)) {
300 printf(_("Computer is not joined to a Domain\n"));
304 libnetapi_get_error_string(c
->netapi_ctx
, status
));
309 c
->opt_comment
= _("Shutting down due to a computer rename");
310 c
->opt_reboot
= true;
313 ret
= run_rpc_command(c
, cli
,
314 &ndr_table_initshutdown
.syntax_id
,
315 0, rpc_init_shutdown_internals
,
321 ret
= run_rpc_command(c
, cli
, &ndr_table_winreg
.syntax_id
, 0,
322 rpc_reg_shutdown_internals
,
337 int net_dom(struct net_context
*c
, int argc
, const char **argv
)
339 NET_API_STATUS status
;
341 struct functable func
[] = {
346 N_("Join a remote machine"),
347 N_("net dom join <domain=DOMAIN> <ou=OU> "
348 "<account=ACCOUNT> <password=PASSWORD> <reboot>\n"
349 " Join a remote machine")
355 N_("Unjoin a remote machine"),
356 N_("net dom unjoin <account=ACCOUNT> "
357 "<password=PASSWORD> <reboot>\n"
358 " Unjoin a remote machine")
362 net_dom_renamecomputer
,
364 N_("Rename a computer that is joined to a domain"),
365 N_("net dom renamecomputer <newname=NEWNAME> "
366 "<account=ACCOUNT> <password=PASSWORD> "
368 " Rename joined computer")
371 {NULL
, NULL
, 0, NULL
, NULL
}
374 status
= libnetapi_init(&c
->netapi_ctx
);
379 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
380 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
381 if (c
->opt_kerberos
) {
382 libnetapi_set_use_kerberos(c
->netapi_ctx
);
385 return net_run_function(c
, argc
, argv
, "net dom", func
);