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"
22 #include "lib/netapi/netapi.h"
24 static int net_dom_usage(int argc
, const char **argv
)
26 d_printf("usage: net dom join "
27 "<domain=DOMAIN> <ou=OU> <account=ACCOUNT> <password=PASSWORD> <reboot>\n");
28 d_printf("usage: net dom unjoin "
29 "<account=ACCOUNT> <password=PASSWORD> <reboot>\n");
34 int net_help_dom(int argc
, const char **argv
)
36 d_printf("net dom join"\
37 "\n Join a remote machine\n");
38 d_printf("net dom unjoin"\
39 "\n Unjoin a remote machine\n");
44 static int net_dom_unjoin(int argc
, const char **argv
)
46 struct libnetapi_ctx
*ctx
= NULL
;
47 const char *server_name
= NULL
;
48 const char *account
= NULL
;
49 const char *password
= NULL
;
50 uint32_t unjoin_flags
= WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE
|
51 WKSSVC_JOIN_FLAGS_JOIN_TYPE
;
52 struct cli_state
*cli
= NULL
;
55 NET_API_STATUS status
;
60 return net_dom_usage(argc
, argv
);
64 server_name
= opt_host
;
67 for (i
=0; i
<argc
; i
++) {
68 if (strnequal(argv
[i
], "account", strlen("account"))) {
69 account
= get_string_param(argv
[i
]);
74 if (strnequal(argv
[i
], "password", strlen("password"))) {
75 password
= get_string_param(argv
[i
]);
80 if (strequal(argv
[i
], "reboot")) {
86 ntstatus
= net_make_ipc_connection_ex(opt_workgroup
, server_name
,
88 if (!NT_STATUS_IS_OK(ntstatus
)) {
93 status
= libnetapi_init(&ctx
);
98 libnetapi_set_username(ctx
, opt_user_name
);
99 libnetapi_set_password(ctx
, opt_password
);
101 status
= NetUnjoinDomain(server_name
, account
, password
, unjoin_flags
);
103 printf("Failed to unjoin domain: %s\n",
104 libnetapi_errstr(status
));
109 opt_comment
= "Shutting down due to a domain membership change";
113 ret
= run_rpc_command(cli
, PI_INITSHUTDOWN
, 0,
114 rpc_init_shutdown_internals
,
120 ret
= run_rpc_command(cli
, PI_WINREG
, 0,
121 rpc_reg_shutdown_internals
,
133 /* libnetapi_free(ctx); */
137 static int net_dom_join(int argc
, const char **argv
)
139 struct libnetapi_ctx
*ctx
= NULL
;
140 const char *server_name
= NULL
;
141 const char *domain_name
= NULL
;
142 const char *account_ou
= NULL
;
143 const char *Account
= NULL
;
144 const char *password
= NULL
;
145 uint32_t join_flags
= WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE
|
146 WKSSVC_JOIN_FLAGS_JOIN_TYPE
;
147 struct cli_state
*cli
= NULL
;
150 NET_API_STATUS status
;
155 return net_dom_usage(argc
, argv
);
159 server_name
= opt_host
;
163 join_flags
|= WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED
;
166 for (i
=0; i
<argc
; i
++) {
167 if (strnequal(argv
[i
], "ou", strlen("ou"))) {
168 account_ou
= get_string_param(argv
[i
]);
173 if (strnequal(argv
[i
], "domain", strlen("domain"))) {
174 domain_name
= get_string_param(argv
[i
]);
179 if (strnequal(argv
[i
], "account", strlen("account"))) {
180 Account
= get_string_param(argv
[i
]);
185 if (strnequal(argv
[i
], "password", strlen("password"))) {
186 password
= get_string_param(argv
[i
]);
191 if (strequal(argv
[i
], "reboot")) {
197 ntstatus
= net_make_ipc_connection_ex(opt_workgroup
, server_name
,
199 if (!NT_STATUS_IS_OK(ntstatus
)) {
204 /* check if domain is a domain or a workgroup */
206 status
= libnetapi_init(&ctx
);
211 libnetapi_set_username(ctx
, opt_user_name
);
212 libnetapi_set_password(ctx
, opt_password
);
214 status
= NetJoinDomain(server_name
, domain_name
, account_ou
,
215 Account
, password
, join_flags
);
217 printf("Failed to join domain: %s\n",
218 libnetapi_errstr(status
));
223 opt_comment
= "Shutting down due to a domain membership change";
227 ret
= run_rpc_command(cli
, PI_INITSHUTDOWN
, 0,
228 rpc_init_shutdown_internals
,
234 ret
= run_rpc_command(cli
, PI_WINREG
, 0,
235 rpc_reg_shutdown_internals
,
247 /* libnetapi_free(ctx); */
251 int net_dom(int argc
, const char **argv
)
253 struct functable func
[] = {
254 {"JOIN", net_dom_join
},
255 {"UNJOIN", net_dom_unjoin
},
256 {"HELP", net_help_dom
},
260 return net_run_function(argc
, argv
, func
, net_dom_usage
);