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/ndr_initshutdown.h"
23 #include "../librpc/gen_ndr/ndr_winreg.h"
24 #include "lib/netapi/netapi.h"
25 #include "lib/netapi/netapi_net.h"
26 #include "libsmb/libsmb.h"
28 int net_dom_usage(struct net_context
*c
, int argc
, const char **argv
)
33 "<domain=DOMAIN> <ou=OU> <account=ACCOUNT> "
34 "<password=PASSWORD> <reboot>\n Join a remote machine\n"));
38 "<account=ACCOUNT> <password=PASSWORD> <reboot>\n"
39 " Unjoin a remote machine\n"));
42 _("net dom renamecomputer "
44 "<account=ACCOUNT> <password=PASSWORD> <reboot>\n"
45 " Rename joined computer\n"));
50 static int net_dom_unjoin(struct net_context
*c
, int argc
, const char **argv
)
52 const char *server_name
= NULL
;
53 const char *account
= NULL
;
54 const char *password
= NULL
;
55 uint32_t unjoin_flags
= NETSETUP_ACCT_DELETE
|
56 NETSETUP_JOIN_DOMAIN
|
57 NETSETUP_IGNORE_UNSUPPORTED_FLAGS
;
58 struct cli_state
*cli
= NULL
;
59 bool do_reboot
= false;
61 NET_API_STATUS status
;
65 if (argc
< 1 || c
->display_usage
) {
66 return net_dom_usage(c
, argc
, argv
);
70 server_name
= c
->opt_host
;
73 for (i
=0; i
<argc
; i
++) {
74 if (strnequal(argv
[i
], "account", strlen("account"))) {
75 account
= get_string_param(argv
[i
]);
80 if (strnequal(argv
[i
], "password", strlen("password"))) {
81 password
= get_string_param(argv
[i
]);
86 if (strequal(argv
[i
], "reboot")) {
92 ntstatus
= net_make_ipc_connection_ex(c
, c
->opt_workgroup
,
95 if (!NT_STATUS_IS_OK(ntstatus
)) {
100 status
= NetUnjoinDomain(server_name
, account
, password
, unjoin_flags
);
102 printf(_("Failed to unjoin domain: %s\n"),
103 libnetapi_get_error_string(c
->netapi_ctx
, status
));
108 c
->opt_comment
= _("Shutting down due to a domain membership "
110 c
->opt_reboot
= true;
113 ret
= run_rpc_command(c
, cli
,
114 &ndr_table_initshutdown
,
115 0, rpc_init_shutdown_internals
,
121 ret
= run_rpc_command(c
, cli
, &ndr_table_winreg
, 0,
122 rpc_reg_shutdown_internals
,
137 static int net_dom_join(struct net_context
*c
, int argc
, const char **argv
)
139 const char *server_name
= NULL
;
140 const char *domain_name
= NULL
;
141 const char *account_ou
= NULL
;
142 const char *Account
= NULL
;
143 const char *password
= NULL
;
144 uint32_t join_flags
= NETSETUP_ACCT_CREATE
|
145 NETSETUP_JOIN_DOMAIN
;
146 struct cli_state
*cli
= NULL
;
147 bool do_reboot
= false;
149 NET_API_STATUS status
;
153 if (argc
< 1 || c
->display_usage
) {
154 return net_dom_usage(c
, argc
, argv
);
157 net_warn_member_options();
160 server_name
= c
->opt_host
;
164 join_flags
|= NETSETUP_DOMAIN_JOIN_IF_JOINED
;
167 for (i
=0; i
<argc
; i
++) {
168 if (strnequal(argv
[i
], "ou", strlen("ou"))) {
169 account_ou
= get_string_param(argv
[i
]);
174 if (strnequal(argv
[i
], "domain", strlen("domain"))) {
175 domain_name
= get_string_param(argv
[i
]);
180 if (strnequal(argv
[i
], "account", strlen("account"))) {
181 Account
= get_string_param(argv
[i
]);
186 if (strnequal(argv
[i
], "password", strlen("password"))) {
187 password
= get_string_param(argv
[i
]);
192 if (strequal(argv
[i
], "reboot")) {
198 ntstatus
= net_make_ipc_connection_ex(c
, c
->opt_workgroup
,
199 server_name
, NULL
, 0,
201 if (!NT_STATUS_IS_OK(ntstatus
)) {
206 /* check if domain is a domain or a workgroup */
208 status
= NetJoinDomain(server_name
, domain_name
, account_ou
,
209 Account
, password
, join_flags
);
211 printf(_("Failed to join domain: %s\n"),
212 libnetapi_get_error_string(c
->netapi_ctx
, status
));
217 c
->opt_comment
= _("Shutting down due to a domain membership "
219 c
->opt_reboot
= true;
222 ret
= run_rpc_command(c
, cli
, &ndr_table_initshutdown
, 0,
223 rpc_init_shutdown_internals
,
229 ret
= run_rpc_command(c
, cli
, &ndr_table_winreg
, 0,
230 rpc_reg_shutdown_internals
,
245 static int net_dom_renamecomputer(struct net_context
*c
, int argc
, const char **argv
)
247 const char *server_name
= NULL
;
248 const char *account
= NULL
;
249 const char *password
= NULL
;
250 const char *newname
= NULL
;
251 uint32_t rename_options
= NETSETUP_ACCT_CREATE
;
252 struct cli_state
*cli
= NULL
;
253 bool do_reboot
= false;
255 NET_API_STATUS status
;
259 if (argc
< 1 || c
->display_usage
) {
260 return net_dom_usage(c
, argc
, argv
);
264 server_name
= c
->opt_host
;
267 for (i
=0; i
<argc
; i
++) {
268 if (strnequal(argv
[i
], "account", strlen("account"))) {
269 account
= get_string_param(argv
[i
]);
274 if (strnequal(argv
[i
], "password", strlen("password"))) {
275 password
= get_string_param(argv
[i
]);
280 if (strnequal(argv
[i
], "newname", strlen("newname"))) {
281 newname
= get_string_param(argv
[i
]);
286 if (strequal(argv
[i
], "reboot")) {
292 ntstatus
= net_make_ipc_connection_ex(c
, c
->opt_workgroup
,
293 server_name
, NULL
, 0,
295 if (!NT_STATUS_IS_OK(ntstatus
)) {
300 status
= NetRenameMachineInDomain(server_name
, newname
,
301 account
, password
, rename_options
);
303 printf(_("Failed to rename machine: "));
304 if (status
== W_ERROR_V(WERR_NERR_SETUPNOTJOINED
)) {
305 printf(_("Computer is not joined to a Domain\n"));
309 libnetapi_get_error_string(c
->netapi_ctx
, status
));
314 c
->opt_comment
= _("Shutting down due to a computer rename");
315 c
->opt_reboot
= true;
318 ret
= run_rpc_command(c
, cli
,
319 &ndr_table_initshutdown
,
320 0, rpc_init_shutdown_internals
,
326 ret
= run_rpc_command(c
, cli
, &ndr_table_winreg
, 0,
327 rpc_reg_shutdown_internals
,
342 int net_dom(struct net_context
*c
, int argc
, const char **argv
)
344 NET_API_STATUS status
;
346 struct functable func
[] = {
351 N_("Join a remote machine"),
352 N_("net dom join <domain=DOMAIN> <ou=OU> "
353 "<account=ACCOUNT> <password=PASSWORD> <reboot>\n"
354 " Join a remote machine")
360 N_("Unjoin a remote machine"),
361 N_("net dom unjoin <account=ACCOUNT> "
362 "<password=PASSWORD> <reboot>\n"
363 " Unjoin a remote machine")
367 net_dom_renamecomputer
,
369 N_("Rename a computer that is joined to a domain"),
370 N_("net dom renamecomputer <newname=NEWNAME> "
371 "<account=ACCOUNT> <password=PASSWORD> "
373 " Rename joined computer")
376 {NULL
, NULL
, 0, NULL
, NULL
}
379 status
= libnetapi_net_init(&c
->netapi_ctx
);
384 status
= libnetapi_set_creds(c
->netapi_ctx
, c
->creds
);
389 return net_run_function(c
, argc
, argv
, "net dom", func
);