| 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(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(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(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 |
|
|---|
| 135 | static 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) {
|
|---|
| 161 | join_flags |= WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED;
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | for (i=0; i<argc; i++) {
|
|---|
| 165 | if (strnequal(argv[i], "ou", strlen("ou"))) {
|
|---|
| 166 | account_ou = get_string_param(argv[i]);
|
|---|
| 167 | if (!account_ou) {
|
|---|
| 168 | return -1;
|
|---|
| 169 | }
|
|---|
| 170 | }
|
|---|
| 171 | if (strnequal(argv[i], "domain", strlen("domain"))) {
|
|---|
| 172 | domain_name = get_string_param(argv[i]);
|
|---|
| 173 | if (!domain_name) {
|
|---|
| 174 | return -1;
|
|---|
| 175 | }
|
|---|
| 176 | }
|
|---|
| 177 | if (strnequal(argv[i], "account", strlen("account"))) {
|
|---|
| 178 | Account = get_string_param(argv[i]);
|
|---|
| 179 | if (!Account) {
|
|---|
| 180 | return -1;
|
|---|
| 181 | }
|
|---|
| 182 | }
|
|---|
| 183 | if (strnequal(argv[i], "password", strlen("password"))) {
|
|---|
| 184 | password = get_string_param(argv[i]);
|
|---|
| 185 | if (!password) {
|
|---|
| 186 | return -1;
|
|---|
| 187 | }
|
|---|
| 188 | }
|
|---|
| 189 | if (strequal(argv[i], "reboot")) {
|
|---|
| 190 | reboot = true;
|
|---|
| 191 | }
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 | if (reboot) {
|
|---|
| 195 | ntstatus = net_make_ipc_connection_ex(opt_workgroup, server_name,
|
|---|
| 196 | NULL, 0, &cli);
|
|---|
| 197 | if (!NT_STATUS_IS_OK(ntstatus)) {
|
|---|
| 198 | return -1;
|
|---|
| 199 | }
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | /* check if domain is a domain or a workgroup */
|
|---|
| 203 |
|
|---|
| 204 | status = libnetapi_init(&ctx);
|
|---|
| 205 | if (status != 0) {
|
|---|
| 206 | return -1;
|
|---|
| 207 | }
|
|---|
| 208 |
|
|---|
| 209 | libnetapi_set_username(ctx, opt_user_name);
|
|---|
| 210 | libnetapi_set_password(ctx, opt_password);
|
|---|
| 211 |
|
|---|
| 212 | status = NetJoinDomain(server_name, domain_name, account_ou,
|
|---|
| 213 | Account, password, join_flags);
|
|---|
| 214 | if (status != 0) {
|
|---|
| 215 | printf("Failed to join domain: %s\n",
|
|---|
| 216 | libnetapi_get_error_string(ctx, status));
|
|---|
| 217 | goto done;
|
|---|
| 218 | }
|
|---|
| 219 |
|
|---|
| 220 | if (reboot) {
|
|---|
| 221 | opt_comment = "Shutting down due to a domain membership change";
|
|---|
| 222 | opt_reboot = true;
|
|---|
| 223 | opt_timeout = 30;
|
|---|
| 224 |
|
|---|
| 225 | ret = run_rpc_command(cli, PI_INITSHUTDOWN, 0,
|
|---|
| 226 | rpc_init_shutdown_internals,
|
|---|
| 227 | argc, argv);
|
|---|
| 228 | if (ret == 0) {
|
|---|
| 229 | goto done;
|
|---|
| 230 | }
|
|---|
| 231 |
|
|---|
| 232 | ret = run_rpc_command(cli, PI_WINREG, 0,
|
|---|
| 233 | rpc_reg_shutdown_internals,
|
|---|
| 234 | argc, argv);
|
|---|
| 235 | goto done;
|
|---|
| 236 | }
|
|---|
| 237 |
|
|---|
| 238 | ret = 0;
|
|---|
| 239 |
|
|---|
| 240 | done:
|
|---|
| 241 | if (cli) {
|
|---|
| 242 | cli_shutdown(cli);
|
|---|
| 243 | }
|
|---|
| 244 |
|
|---|
| 245 | return ret;
|
|---|
| 246 | }
|
|---|
| 247 |
|
|---|
| 248 | int net_dom(int argc, const char **argv)
|
|---|
| 249 | {
|
|---|
| 250 | struct functable func[] = {
|
|---|
| 251 | {"JOIN", net_dom_join},
|
|---|
| 252 | {"UNJOIN", net_dom_unjoin},
|
|---|
| 253 | {"HELP", net_help_dom},
|
|---|
| 254 | {NULL, NULL}
|
|---|
| 255 | };
|
|---|
| 256 |
|
|---|
| 257 | return net_run_function(argc, argv, func, net_dom_usage);
|
|---|
| 258 | }
|
|---|