configure: we require autoconf 2.54
[Samba/bb.git] / source3 / utils / net_dom.c
bloba0de818bff2444d3bd24cd400d4da1a56230c92e
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 = WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE |
41 WKSSVC_JOIN_FLAGS_JOIN_TYPE;
42 struct cli_state *cli = NULL;
43 bool reboot = false;
44 NTSTATUS ntstatus;
45 NET_API_STATUS status;
46 int ret = -1;
47 int i;
49 if (argc < 1) {
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 reboot = true;
75 if (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 (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, PI_INITSHUTDOWN, 0,
98 rpc_init_shutdown_internals,
99 argc, argv);
100 if (ret == 0) {
101 goto done;
104 ret = run_rpc_command(c, cli, PI_WINREG, 0,
105 rpc_reg_shutdown_internals,
106 argc, argv);
107 goto done;
110 ret = 0;
112 done:
113 if (cli) {
114 cli_shutdown(cli);
117 return ret;
120 static int net_dom_join(struct net_context *c, int argc, const char **argv)
122 const char *server_name = NULL;
123 const char *domain_name = NULL;
124 const char *account_ou = NULL;
125 const char *Account = NULL;
126 const char *password = NULL;
127 uint32_t join_flags = WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE |
128 WKSSVC_JOIN_FLAGS_JOIN_TYPE;
129 struct cli_state *cli = NULL;
130 bool reboot = false;
131 NTSTATUS ntstatus;
132 NET_API_STATUS status;
133 int ret = -1;
134 int i;
136 if (argc < 1) {
137 return net_dom_usage(c, argc, argv);
140 if (c->opt_host) {
141 server_name = c->opt_host;
144 if (c->opt_force) {
145 join_flags |= WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED;
148 for (i=0; i<argc; i++) {
149 if (strnequal(argv[i], "ou", strlen("ou"))) {
150 account_ou = get_string_param(argv[i]);
151 if (!account_ou) {
152 return -1;
155 if (strnequal(argv[i], "domain", strlen("domain"))) {
156 domain_name = get_string_param(argv[i]);
157 if (!domain_name) {
158 return -1;
161 if (strnequal(argv[i], "account", strlen("account"))) {
162 Account = get_string_param(argv[i]);
163 if (!Account) {
164 return -1;
167 if (strnequal(argv[i], "password", strlen("password"))) {
168 password = get_string_param(argv[i]);
169 if (!password) {
170 return -1;
173 if (strequal(argv[i], "reboot")) {
174 reboot = true;
178 if (reboot) {
179 ntstatus = net_make_ipc_connection_ex(c, c->opt_workgroup,
180 server_name, NULL, 0,
181 &cli);
182 if (!NT_STATUS_IS_OK(ntstatus)) {
183 return -1;
187 /* check if domain is a domain or a workgroup */
189 status = NetJoinDomain(server_name, domain_name, account_ou,
190 Account, password, join_flags);
191 if (status != 0) {
192 printf("Failed to join domain: %s\n",
193 libnetapi_get_error_string(c->netapi_ctx, status));
194 goto done;
197 if (reboot) {
198 c->opt_comment = "Shutting down due to a domain membership "
199 "change";
200 c->opt_reboot = true;
201 c->opt_timeout = 30;
203 ret = run_rpc_command(c, cli, PI_INITSHUTDOWN, 0,
204 rpc_init_shutdown_internals,
205 argc, argv);
206 if (ret == 0) {
207 goto done;
210 ret = run_rpc_command(c, cli, PI_WINREG, 0,
211 rpc_reg_shutdown_internals,
212 argc, argv);
213 goto done;
216 ret = 0;
218 done:
219 if (cli) {
220 cli_shutdown(cli);
223 return ret;
226 int net_dom(struct net_context *c, int argc, const char **argv)
228 NET_API_STATUS status;
230 struct functable func[] = {
231 {"JOIN", net_dom_join},
232 {"UNJOIN", net_dom_unjoin},
233 {"HELP", net_dom_usage},
234 {NULL, NULL}
237 status = libnetapi_init(&c->netapi_ctx);
238 if (status != 0) {
239 return -1;
242 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
243 libnetapi_set_password(c->netapi_ctx, c->opt_password);
245 return net_run_function(c, argc, argv, func, net_dom_usage);