net: use the netapi_ctx from the net_context struct.
[samba.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         const char *server_name = NULL;
46         const char *account = NULL;
47         const char *password = NULL;
48         uint32_t unjoin_flags = WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE |
49                                 WKSSVC_JOIN_FLAGS_JOIN_TYPE;
50         struct cli_state *cli = NULL;
51         bool reboot = false;
52         NTSTATUS ntstatus;
53         NET_API_STATUS status;
54         int ret = -1;
55         int i;
56
57         if (argc < 1) {
58                 return net_dom_usage(c, argc, argv);
59         }
60
61         if (c->opt_host) {
62                 server_name = c->opt_host;
63         }
64
65         for (i=0; i<argc; i++) {
66                 if (strnequal(argv[i], "account", strlen("account"))) {
67                         account = get_string_param(argv[i]);
68                         if (!account) {
69                                 return -1;
70                         }
71                 }
72                 if (strnequal(argv[i], "password", strlen("password"))) {
73                         password = get_string_param(argv[i]);
74                         if (!password) {
75                                 return -1;
76                         }
77                 }
78                 if (strequal(argv[i], "reboot")) {
79                         reboot = true;
80                 }
81         }
82
83         if (reboot) {
84                 ntstatus = net_make_ipc_connection_ex(c, c->opt_workgroup,
85                                                       server_name, NULL, 0,
86                                                       &cli);
87                 if (!NT_STATUS_IS_OK(ntstatus)) {
88                         return -1;
89                 }
90         }
91
92         status = NetUnjoinDomain(server_name, account, password, unjoin_flags);
93         if (status != 0) {
94                 printf("Failed to unjoin domain: %s\n",
95                         libnetapi_get_error_string(c->netapi_ctx, status));
96                 goto done;
97         }
98
99         if (reboot) {
100                 c->opt_comment = "Shutting down due to a domain membership "
101                                  "change";
102                 c->opt_reboot = true;
103                 c->opt_timeout = 30;
104
105                 ret = run_rpc_command(c, cli, PI_INITSHUTDOWN, 0,
106                                       rpc_init_shutdown_internals,
107                                       argc, argv);
108                 if (ret == 0) {
109                         goto done;
110                 }
111
112                 ret = run_rpc_command(c, cli, PI_WINREG, 0,
113                                       rpc_reg_shutdown_internals,
114                                       argc, argv);
115                 goto done;
116         }
117
118         ret = 0;
119
120  done:
121         if (cli) {
122                 cli_shutdown(cli);
123         }
124
125         return ret;
126 }
127
128 static int net_dom_join(struct net_context *c, int argc, const char **argv)
129 {
130         const char *server_name = NULL;
131         const char *domain_name = NULL;
132         const char *account_ou = NULL;
133         const char *Account = NULL;
134         const char *password = NULL;
135         uint32_t join_flags = WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE |
136                               WKSSVC_JOIN_FLAGS_JOIN_TYPE;
137         struct cli_state *cli = NULL;
138         bool reboot = false;
139         NTSTATUS ntstatus;
140         NET_API_STATUS status;
141         int ret = -1;
142         int i;
143
144         if (argc < 1) {
145                 return net_dom_usage(c, argc, argv);
146         }
147
148         if (c->opt_host) {
149                 server_name = c->opt_host;
150         }
151
152         if (c->opt_force) {
153                 join_flags |= WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED;
154         }
155
156         for (i=0; i<argc; i++) {
157                 if (strnequal(argv[i], "ou", strlen("ou"))) {
158                         account_ou = get_string_param(argv[i]);
159                         if (!account_ou) {
160                                 return -1;
161                         }
162                 }
163                 if (strnequal(argv[i], "domain", strlen("domain"))) {
164                         domain_name = get_string_param(argv[i]);
165                         if (!domain_name) {
166                                 return -1;
167                         }
168                 }
169                 if (strnequal(argv[i], "account", strlen("account"))) {
170                         Account = get_string_param(argv[i]);
171                         if (!Account) {
172                                 return -1;
173                         }
174                 }
175                 if (strnequal(argv[i], "password", strlen("password"))) {
176                         password = get_string_param(argv[i]);
177                         if (!password) {
178                                 return -1;
179                         }
180                 }
181                 if (strequal(argv[i], "reboot")) {
182                         reboot = true;
183                 }
184         }
185
186         if (reboot) {
187                 ntstatus = net_make_ipc_connection_ex(c, c->opt_workgroup,
188                                                       server_name, NULL, 0,
189                                                       &cli);
190                 if (!NT_STATUS_IS_OK(ntstatus)) {
191                         return -1;
192                 }
193         }
194
195         /* check if domain is a domain or a workgroup */
196
197         status = NetJoinDomain(server_name, domain_name, account_ou,
198                                Account, password, join_flags);
199         if (status != 0) {
200                 printf("Failed to join domain: %s\n",
201                         libnetapi_get_error_string(c->netapi_ctx, status));
202                 goto done;
203         }
204
205         if (reboot) {
206                 c->opt_comment = "Shutting down due to a domain membership "
207                                  "change";
208                 c->opt_reboot = true;
209                 c->opt_timeout = 30;
210
211                 ret = run_rpc_command(c, cli, PI_INITSHUTDOWN, 0,
212                                       rpc_init_shutdown_internals,
213                                       argc, argv);
214                 if (ret == 0) {
215                         goto done;
216                 }
217
218                 ret = run_rpc_command(c, cli, PI_WINREG, 0,
219                                       rpc_reg_shutdown_internals,
220                                       argc, argv);
221                 goto done;
222         }
223
224         ret = 0;
225
226  done:
227         if (cli) {
228                 cli_shutdown(cli);
229         }
230
231         return ret;
232 }
233
234 int net_dom(struct net_context *c, int argc, const char **argv)
235 {
236         NET_API_STATUS status;
237
238         struct functable func[] = {
239                 {"JOIN", net_dom_join},
240                 {"UNJOIN", net_dom_unjoin},
241                 {"HELP", net_help_dom},
242                 {NULL, NULL}
243         };
244
245         status = libnetapi_init(&c->netapi_ctx);
246         if (status != 0) {
247                 return -1;
248         }
249
250         libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
251         libnetapi_set_password(c->netapi_ctx, c->opt_password);
252
253         return net_run_function(c, argc, argv, func, net_dom_usage);
254 }