These should be char *, but replace them with [1024] for now
[Samba/vl.git] / source3 / utils / net_dom.c
blobdc6bfb05619fcd66dffef857cd77eb2ffc3242fb
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"
22 #include "lib/netapi/joindomain.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");
31 return -1;
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");
41 return -1;
44 static int net_dom_unjoin(int argc, const char **argv)
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 status;
54 WERROR werr;
55 int ret = -1;
56 int i;
58 if (argc < 1) {
59 return net_dom_usage(argc, argv);
62 server_name = opt_host;
64 for (i=0; i<argc; i++) {
65 if (strnequal(argv[i], "account", strlen("account"))) {
66 account = get_string_param(argv[i]);
67 if (!account) {
68 return -1;
71 if (strnequal(argv[i], "password", strlen("password"))) {
72 password = get_string_param(argv[i]);
73 if (!password) {
74 return -1;
77 if (strequal(argv[i], "reboot")) {
78 reboot = true;
82 if (reboot) {
83 status = net_make_ipc_connection_ex(opt_workgroup, server_name,
84 NULL, 0, &cli);
85 if (!NT_STATUS_IS_OK(status)) {
86 return -1;
90 werr = NetUnjoinDomain(server_name, account, password, unjoin_flags);
91 if (!W_ERROR_IS_OK(werr)) {
92 printf("Failed to unjoin domain: %s\n",
93 get_friendly_nt_error_msg(werror_to_ntstatus(werr)));
94 goto done;
97 if (reboot) {
98 opt_comment = "Shutting down due to a domain membership change";
99 opt_reboot = true;
100 opt_timeout = 30;
102 ret = run_rpc_command(cli, PI_INITSHUTDOWN, 0,
103 rpc_init_shutdown_internals,
104 argc, argv);
105 if (ret == 0) {
106 goto done;
109 ret = run_rpc_command(cli, PI_WINREG, 0,
110 rpc_reg_shutdown_internals,
111 argc, argv);
112 goto done;
115 ret = 0;
117 done:
118 if (cli) {
119 cli_shutdown(cli);
122 return ret;
125 static int net_dom_join(int argc, const char **argv)
127 const char *server_name = NULL;
128 const char *domain_name = NULL;
129 const char *account_ou = NULL;
130 const char *Account = NULL;
131 const char *password = NULL;
132 uint32_t join_flags = WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE |
133 WKSSVC_JOIN_FLAGS_JOIN_TYPE;
134 struct cli_state *cli = NULL;
135 bool reboot = false;
136 NTSTATUS status;
137 WERROR werr;
138 int ret = -1;
139 int i;
141 if (argc < 1) {
142 return net_dom_usage(argc, argv);
145 server_name = opt_host;
147 if (opt_force) {
148 join_flags |= WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED;
151 for (i=0; i<argc; i++) {
152 if (strnequal(argv[i], "ou", strlen("ou"))) {
153 account_ou = get_string_param(argv[i]);
154 if (!account_ou) {
155 return -1;
158 if (strnequal(argv[i], "domain", strlen("domain"))) {
159 domain_name = get_string_param(argv[i]);
160 if (!domain_name) {
161 return -1;
164 if (strnequal(argv[i], "account", strlen("account"))) {
165 Account = get_string_param(argv[i]);
166 if (!Account) {
167 return -1;
170 if (strnequal(argv[i], "password", strlen("password"))) {
171 password = get_string_param(argv[i]);
172 if (!password) {
173 return -1;
176 if (strequal(argv[i], "reboot")) {
177 reboot = true;
181 if (reboot) {
182 status = net_make_ipc_connection_ex(opt_workgroup, server_name,
183 NULL, 0, &cli);
184 if (!NT_STATUS_IS_OK(status)) {
185 return -1;
189 /* check if domain is a domain or a workgroup */
191 werr = NetJoinDomain(server_name, domain_name, account_ou,
192 Account, password, join_flags);
193 if (!W_ERROR_IS_OK(werr)) {
194 printf("Failed to join domain: %s\n",
195 get_friendly_nt_error_msg(werror_to_ntstatus(werr)));
196 goto done;
199 if (reboot) {
200 opt_comment = "Shutting down due to a domain membership change";
201 opt_reboot = true;
202 opt_timeout = 30;
204 ret = run_rpc_command(cli, PI_INITSHUTDOWN, 0,
205 rpc_init_shutdown_internals,
206 argc, argv);
207 if (ret == 0) {
208 goto done;
211 ret = run_rpc_command(cli, PI_WINREG, 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(int argc, const char **argv)
229 struct functable func[] = {
230 {"JOIN", net_dom_join},
231 {"UNJOIN", net_dom_unjoin},
232 {"HELP", net_help_dom},
233 {NULL, NULL}
236 return net_run_function(argc, argv, func, net_dom_usage);