s3: copy nbt/netlogon helper from s4.
[Samba.git] / source / utils / net_dom.c
blob132630de554fe8e19b685cddf524e873aaf94902
1 /*
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/>.
20 #include "includes.h"
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");
32 return -1;
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 |
41 NETSETUP_JOIN_DOMAIN;
42 struct cli_state *cli = NULL;
43 bool do_reboot = false;
44 NTSTATUS ntstatus;
45 NET_API_STATUS status;
46 int ret = -1;
47 int i;
49 if (argc < 1 || c->display_usage) {
50 return net_dom_usage(c, argc, argv);
53 if (c->opt_host) {
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]);
60 if (!account) {
61 return -1;
64 if (strnequal(argv[i], "password", strlen("password"))) {
65 password = get_string_param(argv[i]);
66 if (!password) {
67 return -1;
70 if (strequal(argv[i], "reboot")) {
71 do_reboot = true;
75 if (do_reboot) {
76 ntstatus = net_make_ipc_connection_ex(c, c->opt_workgroup,
77 server_name, NULL, 0,
78 &cli);
79 if (!NT_STATUS_IS_OK(ntstatus)) {
80 return -1;
84 status = NetUnjoinDomain(server_name, account, password, unjoin_flags);
85 if (status != 0) {
86 printf("Failed to unjoin domain: %s\n",
87 libnetapi_get_error_string(c->netapi_ctx, status));
88 goto done;
91 if (do_reboot) {
92 c->opt_comment = "Shutting down due to a domain membership "
93 "change";
94 c->opt_reboot = true;
95 c->opt_timeout = 30;
97 ret = run_rpc_command(c, cli,
98 &ndr_table_initshutdown.syntax_id,
99 0, rpc_init_shutdown_internals,
100 argc, argv);
101 if (ret == 0) {
102 goto done;
105 ret = run_rpc_command(c, cli, &ndr_table_winreg.syntax_id, 0,
106 rpc_reg_shutdown_internals,
107 argc, argv);
108 goto done;
111 ret = 0;
113 done:
114 if (cli) {
115 cli_shutdown(cli);
118 return ret;
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;
132 NTSTATUS ntstatus;
133 NET_API_STATUS status;
134 int ret = -1;
135 int i;
137 if (argc < 1 || c->display_usage) {
138 return net_dom_usage(c, argc, argv);
141 if (c->opt_host) {
142 server_name = c->opt_host;
145 if (c->opt_force) {
146 join_flags |= NETSETUP_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]);
152 if (!account_ou) {
153 return -1;
156 if (strnequal(argv[i], "domain", strlen("domain"))) {
157 domain_name = get_string_param(argv[i]);
158 if (!domain_name) {
159 return -1;
162 if (strnequal(argv[i], "account", strlen("account"))) {
163 Account = get_string_param(argv[i]);
164 if (!Account) {
165 return -1;
168 if (strnequal(argv[i], "password", strlen("password"))) {
169 password = get_string_param(argv[i]);
170 if (!password) {
171 return -1;
174 if (strequal(argv[i], "reboot")) {
175 do_reboot = true;
179 if (do_reboot) {
180 ntstatus = net_make_ipc_connection_ex(c, c->opt_workgroup,
181 server_name, NULL, 0,
182 &cli);
183 if (!NT_STATUS_IS_OK(ntstatus)) {
184 return -1;
188 /* check if domain is a domain or a workgroup */
190 status = NetJoinDomain(server_name, domain_name, account_ou,
191 Account, password, join_flags);
192 if (status != 0) {
193 printf("Failed to join domain: %s\n",
194 libnetapi_get_error_string(c->netapi_ctx, status));
195 goto done;
198 if (do_reboot) {
199 c->opt_comment = "Shutting down due to a domain membership "
200 "change";
201 c->opt_reboot = true;
202 c->opt_timeout = 30;
204 ret = run_rpc_command(c, cli, &ndr_table_initshutdown.syntax_id, 0,
205 rpc_init_shutdown_internals,
206 argc, argv);
207 if (ret == 0) {
208 goto done;
211 ret = run_rpc_command(c, cli, &ndr_table_winreg.syntax_id, 0,
212 rpc_reg_shutdown_internals,
213 argc, argv);
214 goto done;
217 ret = 0;
219 done:
220 if (cli) {
221 cli_shutdown(cli);
224 return ret;
227 int net_dom(struct net_context *c, int argc, const char **argv)
229 NET_API_STATUS status;
231 struct functable func[] = {
233 "join",
234 net_dom_join,
235 NET_TRANSPORT_LOCAL,
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"
242 "unjoin",
243 net_dom_unjoin,
244 NET_TRANSPORT_LOCAL,
245 "Unjoin a remote machine",
246 "net dom unjoin <account=ACCOUNT> <password=PASSWORD> "
247 "<reboot>\n"
248 " Unjoin a remote machine"
250 {NULL, NULL, 0, NULL, NULL}
253 status = libnetapi_init(&c->netapi_ctx);
254 if (status != 0) {
255 return -1;
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);