Fix bug 7005 - mangle method = hash truncates files with dot '. ' character
[Samba.git] / source / utils / net_dom.c
blob3bf6a27289ab6890c9101b24573f5915ce3f2f3a
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"
23 int net_dom_usage(struct net_context *c, int argc, const char **argv)
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");
32 return -1;
35 static int net_dom_unjoin(struct net_context *c, int argc, const char **argv)
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 NETSETUP_IGNORE_UNSUPPORTED_FLAGS;
43 struct cli_state *cli = NULL;
44 bool do_reboot = false;
45 NTSTATUS ntstatus;
46 NET_API_STATUS status;
47 int ret = -1;
48 int i;
50 if (argc < 1 || c->display_usage) {
51 return net_dom_usage(c, argc, argv);
54 if (c->opt_host) {
55 server_name = c->opt_host;
58 for (i=0; i<argc; i++) {
59 if (strnequal(argv[i], "account", strlen("account"))) {
60 account = get_string_param(argv[i]);
61 if (!account) {
62 return -1;
65 if (strnequal(argv[i], "password", strlen("password"))) {
66 password = get_string_param(argv[i]);
67 if (!password) {
68 return -1;
71 if (strequal(argv[i], "reboot")) {
72 do_reboot = true;
76 if (do_reboot) {
77 ntstatus = net_make_ipc_connection_ex(c, c->opt_workgroup,
78 server_name, NULL, 0,
79 &cli);
80 if (!NT_STATUS_IS_OK(ntstatus)) {
81 return -1;
85 status = NetUnjoinDomain(server_name, account, password, unjoin_flags);
86 if (status != 0) {
87 printf("Failed to unjoin domain: %s\n",
88 libnetapi_get_error_string(c->netapi_ctx, status));
89 goto done;
92 if (do_reboot) {
93 c->opt_comment = "Shutting down due to a domain membership "
94 "change";
95 c->opt_reboot = true;
96 c->opt_timeout = 30;
98 ret = run_rpc_command(c, cli,
99 &ndr_table_initshutdown.syntax_id,
100 0, rpc_init_shutdown_internals,
101 argc, argv);
102 if (ret == 0) {
103 goto done;
106 ret = run_rpc_command(c, cli, &ndr_table_winreg.syntax_id, 0,
107 rpc_reg_shutdown_internals,
108 argc, argv);
109 goto done;
112 ret = 0;
114 done:
115 if (cli) {
116 cli_shutdown(cli);
119 return ret;
122 static int net_dom_join(struct net_context *c, int argc, const char **argv)
124 const char *server_name = NULL;
125 const char *domain_name = NULL;
126 const char *account_ou = NULL;
127 const char *Account = NULL;
128 const char *password = NULL;
129 uint32_t join_flags = NETSETUP_ACCT_CREATE |
130 NETSETUP_JOIN_DOMAIN;
131 struct cli_state *cli = NULL;
132 bool do_reboot = false;
133 NTSTATUS ntstatus;
134 NET_API_STATUS status;
135 int ret = -1;
136 int i;
138 if (argc < 1 || c->display_usage) {
139 return net_dom_usage(c, argc, argv);
142 if (c->opt_host) {
143 server_name = c->opt_host;
146 if (c->opt_force) {
147 join_flags |= NETSETUP_DOMAIN_JOIN_IF_JOINED;
150 for (i=0; i<argc; i++) {
151 if (strnequal(argv[i], "ou", strlen("ou"))) {
152 account_ou = get_string_param(argv[i]);
153 if (!account_ou) {
154 return -1;
157 if (strnequal(argv[i], "domain", strlen("domain"))) {
158 domain_name = get_string_param(argv[i]);
159 if (!domain_name) {
160 return -1;
163 if (strnequal(argv[i], "account", strlen("account"))) {
164 Account = get_string_param(argv[i]);
165 if (!Account) {
166 return -1;
169 if (strnequal(argv[i], "password", strlen("password"))) {
170 password = get_string_param(argv[i]);
171 if (!password) {
172 return -1;
175 if (strequal(argv[i], "reboot")) {
176 do_reboot = true;
180 if (do_reboot) {
181 ntstatus = net_make_ipc_connection_ex(c, c->opt_workgroup,
182 server_name, NULL, 0,
183 &cli);
184 if (!NT_STATUS_IS_OK(ntstatus)) {
185 return -1;
189 /* check if domain is a domain or a workgroup */
191 status = NetJoinDomain(server_name, domain_name, account_ou,
192 Account, password, join_flags);
193 if (status != 0) {
194 printf("Failed to join domain: %s\n",
195 libnetapi_get_error_string(c->netapi_ctx, status));
196 goto done;
199 if (do_reboot) {
200 c->opt_comment = "Shutting down due to a domain membership "
201 "change";
202 c->opt_reboot = true;
203 c->opt_timeout = 30;
205 ret = run_rpc_command(c, cli, &ndr_table_initshutdown.syntax_id, 0,
206 rpc_init_shutdown_internals,
207 argc, argv);
208 if (ret == 0) {
209 goto done;
212 ret = run_rpc_command(c, cli, &ndr_table_winreg.syntax_id, 0,
213 rpc_reg_shutdown_internals,
214 argc, argv);
215 goto done;
218 ret = 0;
220 done:
221 if (cli) {
222 cli_shutdown(cli);
225 return ret;
228 int net_dom(struct net_context *c, int argc, const char **argv)
230 NET_API_STATUS status;
232 struct functable func[] = {
234 "join",
235 net_dom_join,
236 NET_TRANSPORT_LOCAL,
237 "Join a remote machine",
238 "net dom join <domain=DOMAIN> <ou=OU> "
239 "<account=ACCOUNT> <password=PASSWORD> <reboot>\n"
240 " Join a remote machine"
243 "unjoin",
244 net_dom_unjoin,
245 NET_TRANSPORT_LOCAL,
246 "Unjoin a remote machine",
247 "net dom unjoin <account=ACCOUNT> <password=PASSWORD> "
248 "<reboot>\n"
249 " Unjoin a remote machine"
251 {NULL, NULL, 0, NULL, NULL}
254 status = libnetapi_init(&c->netapi_ctx);
255 if (status != 0) {
256 return -1;
259 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
260 libnetapi_set_password(c->netapi_ctx, c->opt_password);
261 if (c->opt_kerberos) {
262 libnetapi_set_use_kerberos(c->netapi_ctx);
265 return net_run_function(c, argc, argv, "net dom", func);