source: branches/samba-3.2.x/source/utils/net_dom.c@ 335

Last change on this file since 335 was 133, checked in by Paul Smedley, 18 years ago

Update trunk to 3.2.0pre3

File size: 5.6 KB
Line 
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
23static int net_dom_usage(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
33int net_help_dom(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
43static int net_dom_unjoin(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(argc, argv);
60 }
61
62 if (opt_host) {
63 server_name = 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(opt_workgroup, server_name,
86 NULL, 0, &cli);
87 if (!NT_STATUS_IS_OK(ntstatus)) {
88 return -1;
89 }
90 }
91
92 status = libnetapi_init(&ctx);
93 if (status != 0) {
94 return -1;
95 }
96
97 libnetapi_set_username(ctx, opt_user_name);
98 libnetapi_set_password(ctx, opt_password);
99
100 status = NetUnjoinDomain(server_name, account, password, unjoin_flags);
101 if (status != 0) {
102 printf("Failed to unjoin domain: %s\n",
103 libnetapi_get_error_string(ctx, status));
104 goto done;
105 }
106
107 if (reboot) {
108 opt_comment = "Shutting down due to a domain membership change";
109 opt_reboot = true;
110 opt_timeout = 30;
111
112 ret = run_rpc_command(cli, PI_INITSHUTDOWN, 0,
113 rpc_init_shutdown_internals,
114 argc, argv);
115 if (ret == 0) {
116 goto done;
117 }
118
119 ret = run_rpc_command(cli, PI_WINREG, 0,
120 rpc_reg_shutdown_internals,
121 argc, argv);
122 goto done;
123 }
124
125 ret = 0;
126
127 done:
128 if (cli) {
129 cli_shutdown(cli);
130 }
131
132 return ret;
133}
134
135static int net_dom_join(int argc, const char **argv)
136{
137 struct libnetapi_ctx *ctx = NULL;
138 const char *server_name = NULL;
139 const char *domain_name = NULL;
140 const char *account_ou = NULL;
141 const char *Account = NULL;
142 const char *password = NULL;
143 uint32_t join_flags = WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE |
144 WKSSVC_JOIN_FLAGS_JOIN_TYPE;
145 struct cli_state *cli = NULL;
146 bool reboot = false;
147 NTSTATUS ntstatus;
148 NET_API_STATUS status;
149 int ret = -1;
150 int i;
151
152 if (argc < 1) {
153 return net_dom_usage(argc, argv);
154 }
155
156 if (opt_host) {
157 server_name = opt_host;
158 }
159
160 if (opt_force) {