net: Remove globals
[gd/samba-autobuild/.git] / source3 / utils / net_dom.c
1 /*
2    Samba Unix/Linux SMB client library
3    net dom commands for remote join/unjoin
4    Copyright (C) 2007 Günther Deschner
5
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.
10
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.
15
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/>.
18 */
19
20 #include "includes.h"
21 #include "utils/net.h"
22
23 static int net_dom_usage(struct net_context *c, int argc, const char **argv)
24 {
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");
29
30         return -1;
31 }
32
33 int net_help_dom(struct net_context *c, int argc, const char **argv)
34 {
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");
39
40         return -1;
41 }
42
43 static int net_dom_unjoin(struct net_context *c, int argc, const char **argv)
44 {
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;
57
58         if (argc < 1) {
59                 return net_dom_usage(c, argc, argv);
60         }
61
62         if (c->opt_host) {
63                 server_name = c->opt_host;
64         }
65
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;
71                         }
72                 }
73                 if (strnequal(argv[i], "password", strlen("password"))) {
74                         password = get_string_param(argv[i]);
75                         if (!password) {
76                                 return -1;
77                         }
78                 }
79                 if (strequal(argv[i], "reboot")) {
80                         reboot = true;
81                 }
82         }
83
84         if (reboot) {
85                 ntstatus = net_make_ipc_connection_ex(c, c->opt_workgroup,
86                                                       server_name, NULL, 0,
87                                                       &cli);
88                 if (!NT_STATUS_IS_OK(ntstatus)) {
89                         return -1;
90                 }
91         }
92
93         status = libnetapi_init(&ctx);
94         if (status != 0) {
95                 return -1;
96         }
97
98         libnetapi_set_username(ctx, c->opt_user_name);
99         libnetapi_set_password(ctx, c->opt_password);
100
101         status = NetUnjoinDomain(server_name, account, password, unjoin_flags);
102         if (status != 0) {
103                 printf("Failed to unjoin domain: %s\n",
104                         libnetapi_get_error_string(ctx, status));
105                 goto done;
106         }
107
108         if (reboot) {
109                 c->opt_comment = "Shutting down due to a domain membership "
110                                  "change";
111                 c->opt_reboot = true;
112                 c->opt_timeout = 30;
113
114                 ret = run_rpc_command(c, cli, PI_INITSHUTDOWN, 0,
115                                       rpc_init_shutdown_internals,
116                                       argc, argv);
117                 if (ret == 0) {
118                         goto done;
119                 }
120
121                 ret = run_rpc_command(c, cli, PI_WINREG, 0,
122                                       rpc_reg_shutdown_internals,
123                                       argc, argv);
124                 goto done;
125         }
126
127         ret = 0;
128
129  done:
130         if (cli) {
131                 cli_shutdown(cli);
132         }
133
134         return ret;
135 }
136
137 static int net_dom_join(struct net_context *c, int argc, const char **argv)
138 {
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;
153
154         if (argc < 1) {
155                 return net_dom_usage(c, argc, argv);
156         }
157
158         if (c->opt_host) {
159                 server_name = c->opt_host;
160         }
161
162         if (c->opt_force) {
163                 join_flags |= WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED;
164         }
165
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;
171                         }
172                 }
173                 if (strnequal(argv[i], "domain", strlen("domain"))) {
174                         domain_name = get_string_param(argv[i]);
175                         if (!domain_name) {
176                                 return -1;
177                         }
178                 }
179                 if (strnequal(argv[i], "account", strlen("account"))) {
180                         Account = get_string_param(argv[i]);
181                         if (!Account) {
182                                 return -1;
183                         }
184                 }
185                 if (strnequal(argv[i], "password", strlen("password"))) {
186                         password = get_string_param(argv[i]);
187                         if (!password) {
188                                 return -1;
189                         }
190                 }
191                 if (strequal(argv[i], "reboot")) {
192                         reboot = true;
193                 }
194         }
195
196         if (reboot) {
197                 ntstatus = net_make_ipc_connection_ex(c, c->opt_workgroup,
198                                                       server_name, NULL, 0,
199                                                       &cli);
200                 if (!NT_STATUS_IS_OK(ntstatus)) {
201                         return -1;
202                 }
203         }
204
205         /* check if domain is a domain or a workgroup */
206
207         status = libnetapi_init(&ctx);
208         if (status != 0) {
209                 return -1;
210         }
211
212         libnetapi_set_username(ctx, c->opt_user_name);
213         libnetapi_set_password(ctx, c->opt_password);
214
215         status = NetJoinDomain(server_name, domain_name, account_ou,
216                                Account, password, join_flags);
217         if (status != 0) {
218                 printf("Failed to join domain: %s\n",
219                         libnetapi_get_error_string(ctx, status));
220                 goto done;
221         }
222
223         if (reboot) {
224                 c->opt_comment = "Shutting down due to a domain membership "
225                                  "change";
226                 c->opt_reboot = true;
227                 c->opt_timeout = 30;
228
229                 ret = run_rpc_command(c, cli, PI_INITSHUTDOWN, 0,
230                                       rpc_init_shutdown_internals,
231                                       argc, argv);
232                 if (ret == 0) {
233                         goto done;
234                 }
235
236                 ret = run_rpc_command(c, cli, PI_WINREG, 0,
237                                       rpc_reg_shutdown_internals,
238                                       argc, argv);
239                 goto done;
240         }
241
242         ret = 0;
243
244  done:
245         if (cli) {
246                 cli_shutdown(cli);
247         }
248
249         return ret;
250 }
251
252 int net_dom(struct net_context *c, int argc, const char **argv)
253 {
254         struct functable func[] = {
255                 {"JOIN", net_dom_join},
256                 {"UNJOIN", net_dom_unjoin},
257                 {"HELP", net_help_dom},
258                 {NULL, NULL}
259         };
260
261         return net_run_function(c, argc, argv, func, net_dom_usage);
262 }