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