1 | /*
|
---|
2 | Samba Unix/Linux SMB client library
|
---|
3 | net dom commands for remote join/unjoin
|
---|
4 | Copyright (C) 2007,2009 GÃŒnther Deschner
|
---|
5 |
|
---|
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.
|
---|
10 |
|
---|
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.
|
---|
15 |
|
---|
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/>.
|
---|
18 | */
|
---|
19 |
|
---|
20 | #include "includes.h"
|
---|
21 | #include "utils/net.h"
|
---|
22 | #include "../librpc/gen_ndr/cli_initshutdown.h"
|
---|
23 |
|
---|
24 | int net_dom_usage(struct net_context *c, int argc, const char **argv)
|
---|
25 | {
|
---|
26 | d_printf("%s\n%s",
|
---|
27 | _("Usage:"),
|
---|
28 | _("net dom join "
|
---|
29 | "<domain=DOMAIN> <ou=OU> <account=ACCOUNT> "
|
---|
30 | "<password=PASSWORD> <reboot>\n Join a remote machine\n"));
|
---|
31 | d_printf("%s\n%s",
|
---|
32 | _("Usage:"),
|
---|
33 | _("net dom unjoin "
|
---|
34 | "<account=ACCOUNT> <password=PASSWORD> <reboot>\n"
|
---|
35 | " Unjoin a remote machine\n"));
|
---|
36 | d_printf("%s\n%s",
|
---|
37 | _("Usage:"),
|
---|
38 | _("net dom renamecomputer "
|
---|
39 | "<newname=NEWNAME> "
|
---|
40 | "<account=ACCOUNT> <password=PASSWORD> <reboot>\n"
|
---|
41 | " Rename joined computer\n"));
|
---|
42 |
|
---|
43 | return -1;
|
---|
44 | }
|
---|
45 |
|
---|
46 | static int net_dom_unjoin(struct net_context *c, int argc, const char **argv)
|
---|
47 | {
|
---|
48 | const char *server_name = NULL;
|
---|
49 | const char *account = NULL;
|
---|
50 | const char *password = NULL;
|
---|
51 | uint32_t unjoin_flags = NETSETUP_ACCT_DELETE |
|
---|
52 | NETSETUP_JOIN_DOMAIN |
|
---|
53 | NETSETUP_IGNORE_UNSUPPORTED_FLAGS;
|
---|
54 | struct cli_state *cli = NULL;
|
---|
55 | bool do_reboot = false;
|
---|
56 | NTSTATUS ntstatus;
|
---|
57 | NET_API_STATUS status;
|
---|
58 | int ret = -1;
|
---|
59 | int i;
|
---|
60 |
|
---|
61 | if (argc < 1 || c->display_usage) {
|
---|
62 | return net_dom_usage(c, argc, argv);
|
---|
63 | }
|
---|
64 |
|
---|
65 | if (c->opt_host) {
|
---|
66 | server_name = c->opt_host;
|
---|
67 | }
|
---|
68 |
|
---|
69 | for (i=0; i<argc; i++) {
|
---|
70 | if (strnequal(argv[i], "account", strlen("account"))) {
|
---|
71 | account = get_string_param(argv[i]);
|
---|
72 | if (!account) {
|
---|
73 | return -1;
|
---|
74 | }
|
---|
75 | }
|
---|
76 | if (strnequal(argv[i], "password", strlen("password"))) {
|
---|
77 | password = get_string_param(argv[i]);
|
---|
78 | if (!password) {
|
---|
79 | return -1;
|
---|
80 | }
|
---|
81 | }
|
---|
82 | if (strequal(argv[i], "reboot")) {
|
---|
83 | do_reboot = true;
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | if (do_reboot) {
|
---|
88 | ntstatus = net_make_ipc_connection_ex(c, c->opt_workgroup,
|
---|
89 | server_name, NULL, 0,
|
---|
90 | &cli);
|
---|
91 | if (!NT_STATUS_IS_OK(ntstatus)) {
|
---|
92 | return -1;
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | status = NetUnjoinDomain(server_name, account, password, unjoin_flags);
|
---|
97 | if (status != 0) {
|
---|
98 | printf(_("Failed to unjoin domain: %s\n"),
|
---|
99 | libnetapi_get_error_string(c->netapi_ctx, status));
|
---|
100 | goto done;
|
---|
101 | }
|
---|
102 |
|
---|
103 | if (do_reboot) {
|
---|
104 | c->opt_comment = _("Shutting down due to a domain membership "
|
---|
105 | "change");
|
---|
106 | c->opt_reboot = true;
|
---|
107 | c->opt_timeout = 30;
|
---|
108 |
|
---|
109 | ret = run_rpc_command(c, cli,
|
---|
110 | &ndr_table_initshutdown.syntax_id,
|
---|
111 | 0, rpc_init_shutdown_internals,
|
---|
112 | argc, argv);
|
---|
113 | if (ret == 0) {
|
---|
114 | goto done;
|
---|
115 | }
|
---|
116 |
|
---|
117 | ret = run_rpc_command(c, cli, &ndr_table_winreg.syntax_id, 0,
|
---|
118 | rpc_reg_shutdown_internals,
|
---|
119 | argc, argv);
|
---|
120 | goto done;
|
---|
121 | }
|
---|
122 |
|
---|
123 | ret = 0;
|
---|
124 |
|
---|
125 | done:
|
---|
126 | if (cli) {
|
---|
127 | cli_shutdown(cli);
|
---|
128 | }
|
---|
129 |
|
---|
130 | return ret;
|
---|
131 | }
|
---|
132 |
|
---|
133 | static int net_dom_join(struct net_context *c, int argc, const char **argv)
|
---|
134 | {
|
---|
135 | const char *server_name = NULL;
|
---|
136 | const char *domain_name = NULL;
|
---|
137 | const char *account_ou = NULL;
|
---|
138 | const char *Account = NULL;
|
---|
139 | const char *password = NULL;
|
---|
140 | uint32_t join_flags = NETSETUP_ACCT_CREATE |
|
---|
141 | NETSETUP_JOIN_DOMAIN;
|
---|
142 | struct cli_state *cli = NULL;
|
---|
143 | bool do_reboot = false;
|
---|
144 | NTSTATUS ntstatus;
|
---|
145 | NET_API_STATUS status;
|
---|
146 | int ret = -1;
|
---|
147 | int i;
|
---|
148 |
|
---|
149 | if (argc < 1 || c->display_usage) {
|
---|
150 | return net_dom_usage(c, argc, argv);
|
---|
151 | }
|
---|
152 |
|
---|
153 | if (c->opt_host) {
|
---|
154 | server_name = c->opt_host;
|
---|
155 | }
|
---|
156 |
|
---|
157 | if (c->opt_force) {
|
---|
158 | join_flags |= NETSETUP_DOMAIN_JOIN_IF_JOINED;
|
---|
159 | }
|
---|
160 |
|
---|
161 | for (i=0; i<argc; i++) {
|
---|
162 | if (strnequal(argv[i], "ou", strlen("ou"))) {
|
---|
163 | account_ou = get_string_param(argv[i]);
|
---|
164 | if (!account_ou) {
|
---|
165 | return -1;
|
---|
166 | }
|
---|
167 | }
|
---|
168 | if (strnequal(argv[i], "domain", strlen("domain"))) {
|
---|
169 | domain_name = get_string_param(argv[i]);
|
---|
170 | if (!domain_name) {
|
---|
171 | return -1;
|
---|
172 | }
|
---|
173 | }
|
---|
174 | if (strnequal(argv[i], "account", strlen("account"))) {
|
---|
175 | Account = get_string_param(argv[i]);
|
---|
176 | if (!Account) {
|
---|
177 | return -1;
|
---|
178 | }
|
---|
179 | }
|
---|
180 | if (strnequal(argv[i], "password", strlen("password"))) {
|
---|
181 | password = get_string_param(argv[i]);
|
---|
182 | if (!password) {
|
---|
183 | return -1;
|
---|
184 | }
|
---|
185 | }
|
---|
186 | if (strequal(argv[i], "reboot")) {
|
---|
187 | do_reboot = true;
|
---|
188 | }
|
---|
189 | }
|
---|
190 |
|
---|
191 | if (do_reboot) {
|
---|
192 | ntstatus = net_make_ipc_connection_ex(c, c->opt_workgroup,
|
---|
193 | server_name, NULL, 0,
|
---|
194 | &cli);
|
---|
195 | if (!NT_STATUS_IS_OK(ntstatus)) {
|
---|
196 | return -1;
|
---|
197 | }
|
---|
198 | }
|
---|
199 |
|
---|
200 | /* check if domain is a domain or a workgroup */
|
---|
201 |
|
---|
202 | status = NetJoinDomain(server_name, domain_name, account_ou,
|
---|
203 | Account, password, join_flags);
|
---|
204 | if (status != 0) {
|
---|
205 | printf(_("Failed to join domain: %s\n"),
|
---|
206 | libnetapi_get_error_string(c->netapi_ctx, status));
|
---|
207 | goto done;
|
---|
208 | }
|
---|
209 |
|
---|
210 | if (do_reboot) {
|
---|
211 | c->opt_comment = _("Shutting down due to a domain membership "
|
---|
212 | "change");
|
---|
213 | c->opt_reboot = true;
|
---|
214 | c->opt_timeout = 30;
|
---|
215 |
|
---|
216 | ret = run_rpc_command(c, cli, &ndr_table_initshutdown.syntax_id, 0,
|
---|
217 | rpc_init_shutdown_internals,
|
---|
218 | argc, argv);
|
---|
219 | if (ret == 0) {
|
---|
220 | goto done;
|
---|
221 | }
|
---|
222 |
|
---|
223 | ret = run_rpc_command(c, cli, &ndr_table_winreg.syntax_id, 0,
|
---|
224 | rpc_reg_shutdown_internals,
|
---|
225 | argc, argv);
|
---|
226 | goto done;
|
---|
227 | }
|
---|
228 |
|
---|
229 | ret = 0;
|
---|
230 |
|
---|
231 | done:
|
---|
232 | if (cli) {
|
---|
233 | cli_shutdown(cli);
|
---|
234 | }
|
---|
235 |
|
---|
236 | return ret;
|
---|
237 | }
|
---|
238 |
|
---|
239 | static int net_dom_renamecomputer(struct net_context *c, int argc, const char **argv)
|
---|
240 | {
|
---|
241 | const char *server_name = NULL;
|
---|
242 | const char *account = NULL;
|
---|
243 | const char *password = NULL;
|
---|
244 | const char *newname = NULL;
|
---|
245 | uint32_t rename_options = NETSETUP_ACCT_CREATE;
|
---|
246 | struct cli_state *cli = NULL;
|
---|
247 | bool do_reboot = false;
|
---|
248 | NTSTATUS ntstatus;
|
---|
249 | NET_API_STATUS status;
|
---|
250 | int ret = -1;
|
---|
251 | int i;
|
---|
252 |
|
---|
253 | if (argc < 1 || c->display_usage) {
|
---|
254 | return net_dom_usage(c, argc, argv);
|
---|
255 | }
|
---|
256 |
|
---|
257 | if (c->opt_host) {
|
---|
258 | server_name = c->opt_host;
|
---|
259 | }
|
---|
260 |
|
---|
261 | for (i=0; i<argc; i++) {
|
---|
262 | if (strnequal(argv[i], "account", strlen("account"))) {
|
---|
263 | account = get_string_param(argv[i]);
|
---|
264 | if (!account) {
|
---|
265 | return -1;
|
---|
266 | }
|
---|
267 | }
|
---|
268 | if (strnequal(argv[i], "password", strlen("password"))) {
|
---|
269 | password = get_string_param(argv[i]);
|
---|
270 | if (!password) {
|
---|
271 | return -1;
|
---|
272 | }
|
---|
273 | }
|
---|
274 | if (strnequal(argv[i], "newname", strlen("newname"))) {
|
---|
275 | newname = get_string_param(argv[i]);
|
---|
276 | if (!newname) {
|
---|
277 | return -1;
|
---|
278 | }
|
---|
279 | }
|
---|
280 | if (strequal(argv[i], "reboot")) {
|
---|
281 | do_reboot = true;
|
---|
282 | }
|
---|
283 | }
|
---|
284 |
|
---|
285 | if (do_reboot) {
|
---|
286 | ntstatus = net_make_ipc_connection_ex(c, c->opt_workgroup,
|
---|
287 | server_name, NULL, 0,
|
---|
288 | &cli);
|
---|
289 | if (!NT_STATUS_IS_OK(ntstatus)) {
|
---|
290 | return -1;
|
---|
291 | }
|
---|
292 | }
|
---|
293 |
|
---|
294 | status = NetRenameMachineInDomain(server_name, newname,
|
---|
295 | account, password, rename_options);
|
---|
296 | if (status != 0) {
|
---|
297 | printf(_("Failed to rename machine: "));
|
---|
298 | if (status == W_ERROR_V(WERR_SETUP_NOT_JOINED)) {
|
---|
299 | printf(_("Computer is not joined to a Domain\n"));
|
---|
300 | goto done;
|
---|
301 | }
|
---|
302 | printf("%s\n",
|
---|
303 | libnetapi_get_error_string(c->netapi_ctx, status));
|
---|
304 | goto done;
|
---|
305 | }
|
---|
306 |
|
---|
307 | if (do_reboot) {
|
---|
308 | c->opt_comment = _("Shutting down due to a computer rename");
|
---|
309 | c->opt_reboot = true;
|
---|
310 | c->opt_timeout = 30;
|
---|
311 |
|
---|
312 | ret = run_rpc_command(c, cli,
|
---|
313 | &ndr_table_initshutdown.syntax_id,
|
---|
314 | 0, rpc_init_shutdown_internals,
|
---|
315 | argc, argv);
|
---|
316 | if (ret == 0) {
|
---|
317 | goto done;
|
---|
318 | }
|
---|
319 |
|
---|
320 | ret = run_rpc_command(c, cli, &ndr_table_winreg.syntax_id, 0,
|
---|
321 | rpc_reg_shutdown_internals,
|
---|
322 | argc, argv);
|
---|
323 | goto done;
|
---|
324 | }
|
---|
325 |
|
---|
326 | ret = 0;
|
---|
327 |
|
---|
328 | done:
|
---|
329 | if (cli) {
|
---|
330 | cli_shutdown(cli);
|
---|
331 | }
|
---|
332 |
|
---|
333 | return ret;
|
---|
334 | }
|
---|
335 |
|
---|
336 | int net_dom(struct net_context *c, int argc, const char **argv)
|
---|
337 | {
|
---|
338 | NET_API_STATUS status;
|
---|
339 |
|
---|
340 | struct functable func[] = {
|
---|
341 | {
|
---|
342 | "join",
|
---|
343 | net_dom_join,
|
---|
344 | NET_TRANSPORT_LOCAL,
|
---|
345 | N_("Join a remote machine"),
|
---|
346 | N_("net dom join <domain=DOMAIN> <ou=OU> "
|
---|
347 | "<account=ACCOUNT> <password=PASSWORD> <reboot>\n"
|
---|
348 | " Join a remote machine")
|
---|
349 | },
|
---|
350 | {
|
---|
351 | "unjoin",
|
---|
352 | net_dom_unjoin,
|
---|
353 | NET_TRANSPORT_LOCAL,
|
---|
354 | N_("Unjoin a remote machine"),
|
---|
355 | N_("net dom unjoin <account=ACCOUNT> "
|
---|
356 | "<password=PASSWORD> <reboot>\n"
|
---|
357 | " Unjoin a remote machine")
|
---|
358 | },
|
---|
359 | {
|
---|
360 | "renamecomputer",
|
---|
361 | net_dom_renamecomputer,
|
---|
362 | NET_TRANSPORT_LOCAL,
|
---|
363 | N_("Rename a computer that is joined to a domain"),
|
---|
364 | N_("net dom renamecomputer <newname=NEWNAME> "
|
---|
365 | "<account=ACCOUNT> <password=PASSWORD> "
|
---|
366 | "<reboot>\n"
|
---|
367 | " Rename joined computer")
|
---|
368 | },
|
---|
369 |
|
---|
370 | {NULL, NULL, 0, NULL, NULL}
|
---|
371 | };
|
---|
372 |
|
---|
373 | status = libnetapi_init(&c->netapi_ctx);
|
---|
374 | if (status != 0) {
|
---|
375 | return -1;
|
---|
376 | }
|
---|
377 |
|
---|
378 | libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
|
---|
379 | libnetapi_set_password(c->netapi_ctx, c->opt_password);
|
---|
380 | if (c->opt_kerberos) {
|
---|
381 | libnetapi_set_use_kerberos(c->netapi_ctx);
|
---|
382 | }
|
---|
383 |
|
---|
384 | return net_run_function(c, argc, argv, "net dom", func);
|
---|
385 | }
|
---|