2 Samba Unix/Linux SMB client library
3 net dom commands for remote join/unjoin
4 Copyright (C) 2007 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"
23 int net_dom_usage(struct net_context
*c
, int argc
, const char **argv
)
25 d_printf("usage: net dom join "
26 "<domain=DOMAIN> <ou=OU> <account=ACCOUNT> "
27 "<password=PASSWORD> <reboot>\n Join a remote machine\n");
28 d_printf("usage: net dom unjoin "
29 "<account=ACCOUNT> <password=PASSWORD> <reboot>\n"
30 " Unjoin a remote machine\n");
35 static int net_dom_unjoin(struct net_context
*c
, int argc
, const char **argv
)
37 const char *server_name
= NULL
;
38 const char *account
= NULL
;
39 const char *password
= NULL
;
40 uint32_t unjoin_flags
= NETSETUP_ACCT_DELETE
|
42 struct cli_state
*cli
= NULL
;
43 bool do_reboot
= false;
45 NET_API_STATUS status
;
49 if (argc
< 1 || c
->display_usage
) {
50 return net_dom_usage(c
, argc
, argv
);
54 server_name
= c
->opt_host
;
57 for (i
=0; i
<argc
; i
++) {
58 if (strnequal(argv
[i
], "account", strlen("account"))) {
59 account
= get_string_param(argv
[i
]);
64 if (strnequal(argv
[i
], "password", strlen("password"))) {
65 password
= get_string_param(argv
[i
]);
70 if (strequal(argv
[i
], "reboot")) {
76 ntstatus
= net_make_ipc_connection_ex(c
, c
->opt_workgroup
,
79 if (!NT_STATUS_IS_OK(ntstatus
)) {
84 status
= NetUnjoinDomain(server_name
, account
, password
, unjoin_flags
);
86 printf("Failed to unjoin domain: %s\n",
87 libnetapi_get_error_string(c
->netapi_ctx
, status
));
92 c
->opt_comment
= "Shutting down due to a domain membership "
97 ret
= run_rpc_command(c
, cli
,
98 &ndr_table_initshutdown
.syntax_id
,
99 0, rpc_init_shutdown_internals
,
105 ret
= run_rpc_command(c
, cli
, &ndr_table_winreg
.syntax_id
, 0,
106 rpc_reg_shutdown_internals
,
121 static int net_dom_join(struct net_context
*c
, int argc
, const char **argv
)
123 const char *server_name
= NULL
;
124 const char *domain_name
= NULL
;
125 const char *account_ou
= NULL
;
126 const char *Account
= NULL
;
127 const char *password
= NULL
;
128 uint32_t join_flags
= NETSETUP_ACCT_CREATE
|
129 NETSETUP_JOIN_DOMAIN
;
130 struct cli_state
*cli
= NULL
;
131 bool do_reboot
= false;
133 NET_API_STATUS status
;
137 if (argc
< 1 || c
->display_usage
) {
138 return net_dom_usage(c
, argc
, argv
);
142 server_name
= c
->opt_host
;
146 join_flags
|= WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED
;
149 for (i
=0; i
<argc
; i
++) {
150 if (strnequal(argv
[i
], "ou", strlen("ou"))) {
151 account_ou
= get_string_param(argv
[i
]);
156 if (strnequal(argv
[i
], "domain", strlen("domain"))) {
157 domain_name
= get_string_param(argv
[i
]);
162 if (strnequal(argv
[i
], "account", strlen("account"))) {
163 Account
= get_string_param(argv
[i
]);
168 if (strnequal(argv
[i
], "password", strlen("password"))) {
169 password
= get_string_param(argv
[i
]);
174 if (strequal(argv
[i
], "reboot")) {
180 ntstatus
= net_make_ipc_connection_ex(c
, c
->opt_workgroup
,
181 server_name
, NULL
, 0,
183 if (!NT_STATUS_IS_OK(ntstatus
)) {
188 /* check if domain is a domain or a workgroup */
190 status
= NetJoinDomain(server_name
, domain_name
, account_ou
,
191 Account
, password
, join_flags
);
193 printf("Failed to join domain: %s\n",
194 libnetapi_get_error_string(c
->netapi_ctx
, status
));
199 c
->opt_comment
= "Shutting down due to a domain membership "
201 c
->opt_reboot
= true;
204 ret
= run_rpc_command(c
, cli
, &ndr_table_initshutdown
.syntax_id
, 0,
205 rpc_init_shutdown_internals
,
211 ret
= run_rpc_command(c
, cli
, &ndr_table_winreg
.syntax_id
, 0,
212 rpc_reg_shutdown_internals
,
227 int net_dom(struct net_context
*c
, int argc
, const char **argv
)
229 NET_API_STATUS status
;
231 struct functable func
[] = {
236 "Join a remote machine",
237 "net dom join <domain=DOMAIN> <ou=OU> "
238 "<account=ACCOUNT> <password=PASSWORD> <reboot>\n"
239 " Join a remote machine"
245 "Unjoin a remote machine",
246 "net dom unjoin <account=ACCOUNT> <password=PASSWORD> "
248 " Unjoin a remote machine"
250 {NULL
, NULL
, 0, NULL
, NULL
}
253 status
= libnetapi_init(&c
->netapi_ctx
);
258 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
259 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
260 if (c
->opt_kerberos
) {
261 libnetapi_set_use_kerberos(c
->netapi_ctx
);
264 return net_run_function(c
, argc
, argv
, "net dom", func
);