Remove smbmount.
[Samba/gebeck_regimport.git] / source3 / utils / net_dom.c
blob30993ae2fa7742df290b762633c9cd129d702c1b
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/netapi.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 struct libnetapi_ctx *ctx = NULL;
47 const char *server_name = NULL;
48 const char *account = NULL;
49 const char *password = NULL;
50 uint32_t unjoin_flags = WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE |
51 WKSSVC_JOIN_FLAGS_JOIN_TYPE;
52 struct cli_state *cli = NULL;
53 bool reboot = false;
54 NTSTATUS ntstatus;
55 NET_API_STATUS status;
56 int ret = -1;
57 int i;
59 if (argc < 1) {
60 return net_dom_usage(argc, argv);
63 if (opt_host) {
64 server_name = opt_host;
67 for (i=0; i<argc; i++) {
68 if (strnequal(argv[i], "account", strlen("account"))) {
69 account = get_string_param(argv[i]);
70 if (!account) {
71 return -1;
74 if (strnequal(argv[i], "password", strlen("password"))) {
75 password = get_string_param(argv[i]);
76 if (!password) {
77 return -1;
80 if (strequal(argv[i], "reboot")) {
81 reboot = true;
85 if (reboot) {
86 ntstatus = net_make_ipc_connection_ex(opt_workgroup, server_name,
87 NULL, 0, &cli);
88 if (!NT_STATUS_IS_OK(ntstatus)) {
89 return -1;
93 status = libnetapi_init(&ctx);
94 if (status != 0) {
95 return -1;
98 libnetapi_set_username(ctx, opt_user_name);
99 libnetapi_set_password(ctx, opt_password);
101 status = NetUnjoinDomain(server_name, account, password, unjoin_flags);
102 if (status != 0) {
103 printf("Failed to unjoin domain: %s\n",
104 libnetapi_errstr(status));
105 goto done;
108 if (reboot) {
109 opt_comment = "Shutting down due to a domain membership change";
110 opt_reboot = true;
111 opt_timeout = 30;
113 ret = run_rpc_command(cli, PI_INITSHUTDOWN, 0,
114 rpc_init_shutdown_internals,
115 argc, argv);
116 if (ret == 0) {
117 goto done;
120 ret = run_rpc_command(cli, PI_WINREG, 0,
121 rpc_reg_shutdown_internals,
122 argc, argv);
123 goto done;
126 ret = 0;
128 done:
129 if (cli) {
130 cli_shutdown(cli);
133 /* libnetapi_free(ctx); */
134 return ret;
137 static int net_dom_join(int argc, const char **argv)
139 struct libnetapi_ctx *ctx = NULL;
140 const char *server_name = NULL;
141 const char *domain_name = NULL;
142 const char *account_ou = NULL;
143 const char *Account = NULL;
144 const char *password = NULL;
145 uint32_t join_flags = WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE |
146 WKSSVC_JOIN_FLAGS_JOIN_TYPE;
147 struct cli_state *cli = NULL;
148 bool reboot = false;
149 NTSTATUS ntstatus;
150 NET_API_STATUS status;
151 int ret = -1;
152 int i;
154 if (argc < 1) {
155 return net_dom_usage(argc, argv);
158 if (opt_host) {
159 server_name = opt_host;
162 if (opt_force) {
163 join_flags |= WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED;
166 for (i=0; i<argc; i++) {
167 if (strnequal(argv[i], "ou", strlen("ou"))) {
168 account_ou = get_string_param(argv[i]);
169 if (!account_ou) {
170 return -1;
173 if (strnequal(argv[i], "domain", strlen("domain"))) {
174 domain_name = get_string_param(argv[i]);
175 if (!domain_name) {
176 return -1;
179 if (strnequal(argv[i], "account", strlen("account"))) {
180 Account = get_string_param(argv[i]);
181 if (!Account) {
182 return -1;
185 if (strnequal(argv[i], "password", strlen("password"))) {
186 password = get_string_param(argv[i]);
187 if (!password) {
188 return -1;
191 if (strequal(argv[i], "reboot")) {
192 reboot = true;
196 if (reboot) {
197 ntstatus = net_make_ipc_connection_ex(opt_workgroup, server_name,
198 NULL, 0, &cli);
199 if (!NT_STATUS_IS_OK(ntstatus)) {
200 return -1;
204 /* check if domain is a domain or a workgroup */
206 status = libnetapi_init(&ctx);
207 if (status != 0) {
208 return -1;
211 libnetapi_set_username(ctx, opt_user_name);
212 libnetapi_set_password(ctx, opt_password);
214 status = NetJoinDomain(server_name, domain_name, account_ou,
215 Account, password, join_flags);
216 if (status != 0) {
217 printf("Failed to join domain: %s\n",
218 libnetapi_errstr(status));
219 goto done;
222 if (reboot) {
223 opt_comment = "Shutting down due to a domain membership change";
224 opt_reboot = true;
225 opt_timeout = 30;
227 ret = run_rpc_command(cli, PI_INITSHUTDOWN, 0,
228 rpc_init_shutdown_internals,
229 argc, argv);
230 if (ret == 0) {
231 goto done;
234 ret = run_rpc_command(cli, PI_WINREG, 0,
235 rpc_reg_shutdown_internals,
236 argc, argv);
237 goto done;
240 ret = 0;
242 done:
243 if (cli) {
244 cli_shutdown(cli);
247 /* libnetapi_free(ctx); */
248 return ret;
251 int net_dom(int argc, const char **argv)
253 struct functable func[] = {
254 {"JOIN", net_dom_join},
255 {"UNJOIN", net_dom_unjoin},
256 {"HELP", net_help_dom},
257 {NULL, NULL}
260 return net_run_function(argc, argv, func, net_dom_usage);