Merge Samba3 and Samba4 together
[ira/wip.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 = NETSETUP_ACCT_DELETE |
41                                 NETSETUP_JOIN_DOMAIN;
42         struct cli_state *cli = NULL;
43         bool do_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                         do_reboot = true;
72                 }
73         }
74
75         if (do_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 (do_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,
98                                       &ndr_table_initshutdown.syntax_id,
99                                       0, rpc_init_shutdown_internals,
100                                       argc, argv);
101                 if (ret == 0) {
102                         goto done;
103                 }
104
105                 ret = run_rpc_command(c, cli, &ndr_table_winreg.syntax_id, 0,
106                                       rpc_reg_shutdown_internals,
107                                       argc, argv);
108                 goto done;
109         }
110
111         ret = 0;
112
113  done:
114         if (cli) {
115                 cli_shutdown(cli);
116         }
117
118         return ret;
119 }
120
121 static int net_dom_join(struct net_context *c, int argc, const char **argv)
122 {
123         const char *server_name = NULL;
124         const char *domain_name = NULL;
125         const char *account_ou = NULL;
126         const char *Account = NULL;
127         const char *password = NULL;
128         uint32_t join_flags = NETSETUP_ACCT_CREATE |
129                               NETSETUP_JOIN_DOMAIN;
130         struct cli_state *cli = NULL;
131         bool do_reboot = false;
132         NTSTATUS ntstatus;
133         NET_API_STATUS status;
134         int ret = -1;
135         int i;
136
137         if (argc < 1 || c->display_usage) {
138                 return net_dom_usage(c, argc, argv);
139         }
140
141         if (c->opt_host) {
142                 server_name = c->opt_host;
143         }
144
145         if (c->opt_force) {
146                 join_flags |= NETSETUP_DOMAIN_JOIN_IF_JOINED;
147         }
148
149         for (i=0; i<argc; i++) {
150                 if (strnequal(argv[i], "ou", strlen("ou"))) {
151                         account_ou = get_string_param(argv[i]);
152                         if (!account_ou) {
153                                 return -1;
154                         }
155                 }
156                 if (strnequal(argv[i], "domain", strlen("domain"))) {
157                         domain_name = get_string_param(argv[i]);
158                         if (!domain_name) {
159                                 return -1;
160                         }
161                 }
162                 if (strnequal(argv[i], "account", strlen("account"))) {
163                         Account = get_string_param(argv[i]);
164                         if (!Account) {
165                                 return -1;
166                         }
167                 }
168                 if (strnequal(argv[i], "password", strlen("password"))) {
169                         password = get_string_param(argv[i]);
170                         if (!password) {
171                                 return -1;
172                         }
173                 }
174                 if (strequal(argv[i], "reboot")) {
175                         do_reboot = true;
176                 }
177         }
178
179         if (do_reboot) {
180                 ntstatus = net_make_ipc_connection_ex(c, c->opt_workgroup,
181                                                       server_name, NULL, 0,
182                                                       &cli);
183                 if (!NT_STATUS_IS_OK(ntstatus)) {
184                         return -1;
185                 }
186         }
187
188         /* check if domain is a domain or a workgroup */
189
190         status = NetJoinDomain(server_name, domain_name, account_ou,
191                                Account, password, join_flags);
192         if (status != 0) {
193                 printf("Failed to join domain: %s\n",
194                         libnetapi_get_error_string(c->netapi_ctx, status));
195                 goto done;
196         }
197
198         if (do_reboot) {
199                 c->opt_comment = "Shutting down due to a domain membership "
200                                  "change";
201                 c->opt_reboot = true;
202                 c->opt_timeout = 30;
203
204                 ret = run_rpc_command(c, cli, &ndr_table_initshutdown.syntax_id, 0,
205                                       rpc_init_shutdown_internals,
206                                       argc, argv);
207                 if (ret == 0) {
208                         goto done;
209                 }
210
211                 ret = run_rpc_command(c, cli, &ndr_table_winreg.syntax_id, 0,
212                                       rpc_reg_shutdown_internals,
213                                       argc, argv);
214                 goto done;
215         }
216
217         ret = 0;
218
219  done:
220         if (cli) {
221                 cli_shutdown(cli);
222         }
223
224         return ret;
225 }
226
227 int net_dom(struct net_context *c, int argc, const char **argv)
228 {
229         NET_API_STATUS status;
230
231         struct functable func[] = {
232                 {
233                         "join",
234                         net_dom_join,
235                         NET_TRANSPORT_LOCAL,
236                         "Join a remote machine",
237                         "net dom join <domain=DOMAIN> <ou=OU> "
238                         "<account=ACCOUNT> <password=PASSWORD> <reboot>\n"
239                         "  Join a remote machine"
240                 },
241                 {
242                         "unjoin",
243                         net_dom_unjoin,
244                         NET_TRANSPORT_LOCAL,
245                         "Unjoin a remote machine",
246                         "net dom unjoin <account=ACCOUNT> <password=PASSWORD> "
247                         "<reboot>\n"
248                         "  Unjoin a remote machine"
249                 },
250                 {NULL, NULL, 0, NULL, NULL}
251         };
252
253         status = libnetapi_init(&c->netapi_ctx);
254         if (status != 0) {
255                 return -1;
256         }
257
258         libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
259         libnetapi_set_password(c->netapi_ctx, c->opt_password);
260         if (c->opt_kerberos) {
261                 libnetapi_set_use_kerberos(c->netapi_ctx);
262         }
263
264         return net_run_function(c, argc, argv, "net dom", func);
265 }