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"
25 #include "lib/netapi/netapi_net.h"
27 int net_dom_usage(struct net_context
*c
, int argc
, const char **argv
)
32 "<domain=DOMAIN> <ou=OU> <account=ACCOUNT> "
33 "<password=PASSWORD> <reboot>\n Join a remote machine\n"));
37 "<account=ACCOUNT> <password=PASSWORD> <reboot>\n"
38 " Unjoin a remote machine\n"));
41 _("net dom renamecomputer "
43 "<account=ACCOUNT> <password=PASSWORD> <reboot>\n"
44 " Rename joined computer\n"));
49 static int net_dom_unjoin(struct net_context
*c
, int argc
, const char **argv
)
51 const char *server_name
= NULL
;
52 const char *account
= NULL
;
53 const char *password
= NULL
;
54 uint32_t unjoin_flags
= NETSETUP_ACCT_DELETE
|
55 NETSETUP_JOIN_DOMAIN
|
56 NETSETUP_IGNORE_UNSUPPORTED_FLAGS
;
57 struct cli_state
*cli
= NULL
;
58 bool do_reboot
= false;
60 NET_API_STATUS status
;
64 if (argc
< 1 || c
->display_usage
) {
65 return net_dom_usage(c
, argc
, argv
);
69 server_name
= c
->opt_host
;
72 for (i
=0; i
<argc
; i
++) {
73 if (strnequal(argv
[i
], "account", strlen("account"))) {
74 account
= get_string_param(argv
[i
]);
79 if (strnequal(argv
[i
], "password", strlen("password"))) {
80 password
= get_string_param(argv
[i
]);
85 if (strequal(argv
[i
], "reboot")) {
91 ntstatus
= net_make_ipc_connection_ex(c
, c
->opt_workgroup
,
94 if (!NT_STATUS_IS_OK(ntstatus
)) {
99 status
= NetUnjoinDomain(server_name
, account
, password
, unjoin_flags
);
101 printf(_("Failed to unjoin domain: %s\n"),
102 libnetapi_get_error_string(c
->netapi_ctx
, status
));
107 c
->opt_comment
= _("Shutting down due to a domain membership "
109 c
->opt_reboot
= true;
112 ret
= run_rpc_command(c
, cli
,
113 &ndr_table_initshutdown
.syntax_id
,
114 0, rpc_init_shutdown_internals
,
120 ret
= run_rpc_command(c
, cli
, &ndr_table_winreg
.syntax_id
, 0,
121 rpc_reg_shutdown_internals
,
136 static int net_dom_join(struct net_context
*c
, int argc
, const char **argv
)
138 const char *server_name
= NULL
;
139 const char *domain_name
= NULL
;
140 const char *account_ou
= NULL
;
141 const char *Account
= NULL
;
142 const char *password
= NULL
;
143 uint32_t join_flags
= NETSETUP_ACCT_CREATE
|
144 NETSETUP_JOIN_DOMAIN
;
145 struct cli_state
*cli
= NULL
;
146 bool do_reboot
= false;
148 NET_API_STATUS status
;
152 if (argc
< 1 || c
->display_usage
) {
153 return net_dom_usage(c
, argc
, argv
);
157 server_name
= c
->opt_host
;
161 join_flags
|= NETSETUP_DOMAIN_JOIN_IF_JOINED
;
164 for (i
=0; i
<argc
; i
++) {
165 if (strnequal(argv
[i
], "ou", strlen("ou"))) {
166 account_ou
= get_string_param(argv
[i
]);
171 if (strnequal(argv
[i
], "domain", strlen("domain"))) {
172 domain_name
= get_string_param(argv
[i
]);
177 if (strnequal(argv
[i
], "account", strlen("account"))) {
178 Account
= get_string_param(argv
[i
]);
183 if (strnequal(argv
[i
], "password", strlen("password"))) {
184 password
= get_string_param(argv
[i
]);
189 if (strequal(argv
[i
], "reboot")) {
195 ntstatus
= net_make_ipc_connection_ex(c
, c
->opt_workgroup
,
196 server_name
, NULL
, 0,
198 if (!NT_STATUS_IS_OK(ntstatus
)) {
203 /* check if domain is a domain or a workgroup */
205 status
= NetJoinDomain(server_name
, domain_name
, account_ou
,
206 Account
, password
, join_flags
);
208 printf(_("Failed to join domain: %s\n"),
209 libnetapi_get_error_string(c
->netapi_ctx
, status
));
214 c
->opt_comment
= _("Shutting down due to a domain membership "
216 c
->opt_reboot
= true;
219 ret
= run_rpc_command(c
, cli
, &ndr_table_initshutdown
.syntax_id
, 0,
220 rpc_init_shutdown_internals
,
226 ret
= run_rpc_command(c
, cli
, &ndr_table_winreg
.syntax_id
, 0,
227 rpc_reg_shutdown_internals
,
242 static int net_dom_renamecomputer(struct net_context
*c
, int argc
, const char **argv
)
244 const char *server_name
= NULL
;
245 const char *account
= NULL
;
246 const char *password
= NULL
;
247 const char *newname
= NULL
;
248 uint32_t rename_options
= NETSETUP_ACCT_CREATE
;
249 struct cli_state
*cli
= NULL
;
250 bool do_reboot
= false;
252 NET_API_STATUS status
;
256 if (argc
< 1 || c
->display_usage
) {
257 return net_dom_usage(c
, argc
, argv
);
261 server_name
= c
->opt_host
;
264 for (i
=0; i
<argc
; i
++) {
265 if (strnequal(argv
[i
], "account", strlen("account"))) {
266 account
= get_string_param(argv
[i
]);
271 if (strnequal(argv
[i
], "password", strlen("password"))) {
272 password
= get_string_param(argv
[i
]);
277 if (strnequal(argv
[i
], "newname", strlen("newname"))) {
278 newname
= get_string_param(argv
[i
]);
283 if (strequal(argv
[i
], "reboot")) {
289 ntstatus
= net_make_ipc_connection_ex(c
, c
->opt_workgroup
,
290 server_name
, NULL
, 0,
292 if (!NT_STATUS_IS_OK(ntstatus
)) {
297 status
= NetRenameMachineInDomain(server_name
, newname
,
298 account
, password
, rename_options
);
300 printf(_("Failed to rename machine: "));
301 if (status
== W_ERROR_V(WERR_SETUP_NOT_JOINED
)) {
302 printf(_("Computer is not joined to a Domain\n"));
306 libnetapi_get_error_string(c
->netapi_ctx
, status
));
311 c
->opt_comment
= _("Shutting down due to a computer rename");
312 c
->opt_reboot
= true;
315 ret
= run_rpc_command(c
, cli
,
316 &ndr_table_initshutdown
.syntax_id
,
317 0, rpc_init_shutdown_internals
,
323 ret
= run_rpc_command(c
, cli
, &ndr_table_winreg
.syntax_id
, 0,
324 rpc_reg_shutdown_internals
,
339 int net_dom(struct net_context
*c
, int argc
, const char **argv
)
341 NET_API_STATUS status
;
343 struct functable func
[] = {
348 N_("Join a remote machine"),
349 N_("net dom join <domain=DOMAIN> <ou=OU> "
350 "<account=ACCOUNT> <password=PASSWORD> <reboot>\n"
351 " Join a remote machine")
357 N_("Unjoin a remote machine"),
358 N_("net dom unjoin <account=ACCOUNT> "
359 "<password=PASSWORD> <reboot>\n"
360 " Unjoin a remote machine")
364 net_dom_renamecomputer
,
366 N_("Rename a computer that is joined to a domain"),
367 N_("net dom renamecomputer <newname=NEWNAME> "
368 "<account=ACCOUNT> <password=PASSWORD> "
370 " Rename joined computer")
373 {NULL
, NULL
, 0, NULL
, NULL
}
376 status
= libnetapi_net_init(&c
->netapi_ctx
);
381 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
382 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
383 if (c
->opt_kerberos
) {
384 libnetapi_set_use_kerberos(c
->netapi_ctx
);
387 return net_run_function(c
, argc
, argv
, "net dom", func
);