s3: fix guest auth when winbindd is running
[Samba.git] / source / utils / net_dom.c
blob6e4bf14c84dc1d02e6c59af2c5b620ba0cfdc5e0
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 static int net_dom_usage(int argc, const char **argv)
25 d_printf("usage: net dom join "
26 "<domain=DOMAIN> <ou=OU> <account=ACCOUNT> <password=PASSWORD> <reboot>\n");
27 d_printf("usage: net dom unjoin "
28 "<account=ACCOUNT> <password=PASSWORD> <reboot>\n");
30 return -1;
33 int net_help_dom(int argc, const char **argv)
35 d_printf("net dom join"\
36 "\n Join a remote machine\n");
37 d_printf("net dom unjoin"\
38 "\n Unjoin a remote machine\n");
40 return -1;
43 static int net_dom_unjoin(int argc, const char **argv)
45 struct libnetapi_ctx *ctx = NULL;
46 const char *server_name = NULL;
47 const char *account = NULL;
48 const char *password = NULL;
49 uint32_t unjoin_flags = WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE |
50 WKSSVC_JOIN_FLAGS_JOIN_TYPE;
51 struct cli_state *cli = NULL;
52 bool reboot = false;
53 NTSTATUS ntstatus;
54 NET_API_STATUS status;
55 int ret = -1;
56 int i;
58 if (argc < 1) {
59 return net_dom_usage(argc, argv);
62 if (opt_host) {
63 server_name = opt_host;
66 for (i=0; i<argc; i++) {
67 if (strnequal(argv[i], "account", strlen("account"))) {
68 account = get_string_param(argv[i]);
69 if (!account) {
70 return -1;
73 if (strnequal(argv[i], "password", strlen("password"))) {
74 password = get_string_param(argv[i]);
75 if (!password) {
76 return -1;
79 if (strequal(argv[i], "reboot")) {
80 reboot = true;
84 if (reboot) {
85 ntstatus = net_make_ipc_connection_ex(opt_workgroup, server_name,
86 NULL, 0, &cli);
87 if (!NT_STATUS_IS_OK(ntstatus)) {
88 return -1;
92 status = libnetapi_init(&ctx);
93 if (status != 0) {
94 return -1;
97 libnetapi_set_username(ctx, opt_user_name);
98 libnetapi_set_password(ctx, opt_password);
100 status = NetUnjoinDomain(server_name, account, password, unjoin_flags);
101 if (status != 0) {
102 printf("Failed to unjoin domain: %s\n",
103 libnetapi_get_error_string(ctx, status));
104 goto done;
107 if (reboot) {
108 opt_comment = "Shutting down due to a domain membership change";
109 opt_reboot = true;
110 opt_timeout = 30;
112 ret = run_rpc_command(cli, PI_INITSHUTDOWN, 0,
113 rpc_init_shutdown_internals,
114 argc, argv);
115 if (ret == 0) {
116 goto done;
119 ret = run_rpc_command(cli, PI_WINREG, 0,
120 rpc_reg_shutdown_internals,
121 argc, argv);
122 goto done;
125 ret = 0;
127 done:
128 if (cli) {
129 cli_shutdown(cli);
132 return ret;
135 static int net_dom_join(int argc, const char **argv)
137 struct libnetapi_ctx *ctx = NULL;
138 const char *server_name = NULL;
139 const char *domain_name = NULL;
140 const char *account_ou = NULL;
141 const char *Account = NULL;
142 const char *password = NULL;
143 uint32_t join_flags = WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE |
144 WKSSVC_JOIN_FLAGS_JOIN_TYPE;
145 struct cli_state *cli = NULL;
146 bool reboot = false;
147 NTSTATUS ntstatus;
148 NET_API_STATUS status;
149 int ret = -1;
150 int i;
152 if (argc < 1) {
153 return net_dom_usage(argc, argv);
156 if (opt_host) {
157 server_name = opt_host;
160 if (opt_force) {
161 join_flags |= WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED;
164 for (i=0; i<argc; i++) {
165 if (strnequal(argv[i], "ou", strlen("ou"))) {
166 account_ou = get_string_param(argv[i]);
167 if (!account_ou) {
168 return -1;
171 if (strnequal(argv[i], "domain", strlen("domain"))) {
172 domain_name = get_string_param(argv[i]);
173 if (!domain_name) {
174 return -1;
177 if (strnequal(argv[i], "account", strlen("account"))) {
178 Account = get_string_param(argv[i]);
179 if (!Account) {
180 return -1;
183 if (strnequal(argv[i], "password", strlen("password"))) {
184 password = get_string_param(argv[i]);
185 if (!password) {
186 return -1;
189 if (strequal(argv[i], "reboot")) {
190 reboot = true;
194 if (reboot) {
195 ntstatus = net_make_ipc_connection_ex(opt_workgroup, server_name,
196 NULL, 0, &cli);
197 if (!NT_STATUS_IS_OK(ntstatus)) {
198 return -1;
202 /* check if domain is a domain or a workgroup */
204 status = libnetapi_init(&ctx);
205 if (status != 0) {
206 return -1;
209 libnetapi_set_username(ctx, opt_user_name);
210 libnetapi_set_password(ctx, opt_password);
212 status = NetJoinDomain(server_name, domain_name, account_ou,
213 Account, password, join_flags);
214 if (status != 0) {
215 printf("Failed to join domain: %s\n",
216 libnetapi_get_error_string(ctx, status));
217 goto done;
220 if (reboot) {
221 opt_comment = "Shutting down due to a domain membership change";
222 opt_reboot = true;
223 opt_timeout = 30;
225 ret = run_rpc_command(cli, PI_INITSHUTDOWN, 0,
226 rpc_init_shutdown_internals,
227 argc, argv);
228 if (ret == 0) {
229 goto done;
232 ret = run_rpc_command(cli, PI_WINREG, 0,
233 rpc_reg_shutdown_internals,
234 argc, argv);
235 goto done;
238 ret = 0;
240 done:
241 if (cli) {
242 cli_shutdown(cli);
245 return ret;
248 int net_dom(int argc, const char **argv)
250 struct functable func[] = {
251 {"JOIN", net_dom_join},
252 {"UNJOIN", net_dom_unjoin},
253 {"HELP", net_help_dom},
254 {NULL, NULL}
257 return net_run_function(argc, argv, func, net_dom_usage);