| 1 | /*
|
|---|
| 2 | * Unix SMB/CIFS implementation.
|
|---|
| 3 | * Helper routines for net
|
|---|
| 4 | * Copyright (C) Volker Lendecke 2006
|
|---|
| 5 | * Copyright (C) Kai Blin 2008
|
|---|
| 6 | *
|
|---|
| 7 | * This program is free software; you can redistribute it and/or modify
|
|---|
| 8 | * it under the terms of the GNU General Public License as published by
|
|---|
| 9 | * the Free Software Foundation; either version 3 of the License, or
|
|---|
| 10 | * (at your option) any later version.
|
|---|
| 11 | *
|
|---|
| 12 | * This program is distributed in the hope that it will be useful,
|
|---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 15 | * GNU General Public License for more details.
|
|---|
| 16 | *
|
|---|
| 17 | * You should have received a copy of the GNU General Public License
|
|---|
| 18 | * along with this program; if not, see <http://www.gnu.org/licenses/>.
|
|---|
| 19 | */
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 | #include "includes.h"
|
|---|
| 23 | #include "utils/net.h"
|
|---|
| 24 | #include "rpc_client/cli_pipe.h"
|
|---|
| 25 | #include "../librpc/gen_ndr/ndr_lsa_c.h"
|
|---|
| 26 | #include "rpc_client/cli_lsarpc.h"
|
|---|
| 27 | #include "../librpc/gen_ndr/ndr_dssetup_c.h"
|
|---|
| 28 | #include "secrets.h"
|
|---|
| 29 | #include "../libcli/security/security.h"
|
|---|
| 30 | #include "libsmb/libsmb.h"
|
|---|
| 31 |
|
|---|
| 32 | NTSTATUS net_rpc_lookup_name(struct net_context *c,
|
|---|
| 33 | TALLOC_CTX *mem_ctx, struct cli_state *cli,
|
|---|
| 34 | const char *name, const char **ret_domain,
|
|---|
| 35 | const char **ret_name, struct dom_sid *ret_sid,
|
|---|
| 36 | enum lsa_SidType *ret_type)
|
|---|
| 37 | {
|
|---|
| 38 | struct rpc_pipe_client *lsa_pipe = NULL;
|
|---|
| 39 | struct policy_handle pol;
|
|---|
| 40 | NTSTATUS status, result;
|
|---|
| 41 | const char **dom_names;
|
|---|
| 42 | struct dom_sid *sids;
|
|---|
| 43 | enum lsa_SidType *types;
|
|---|
| 44 | struct dcerpc_binding_handle *b;
|
|---|
| 45 |
|
|---|
| 46 | ZERO_STRUCT(pol);
|
|---|
| 47 |
|
|---|
| 48 | status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
|
|---|
| 49 | &lsa_pipe);
|
|---|
| 50 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 51 | d_fprintf(stderr, _("Could not initialise lsa pipe\n"));
|
|---|
| 52 | return status;
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | b = lsa_pipe->binding_handle;
|
|---|
| 56 |
|
|---|
| 57 | status = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, false,
|
|---|
| 58 | SEC_FLAG_MAXIMUM_ALLOWED,
|
|---|
| 59 | &pol);
|
|---|
| 60 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 61 | d_fprintf(stderr, "open_policy %s: %s\n", _("failed"),
|
|---|
| 62 | nt_errstr(status));
|
|---|
| 63 | return status;
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | status = rpccli_lsa_lookup_names(lsa_pipe, mem_ctx, &pol, 1,
|
|---|
| 67 | &name, &dom_names, 1, &sids, &types);
|
|---|
| 68 |
|
|---|
| 69 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 70 | /* This can happen easily, don't log an error */
|
|---|
| 71 | goto done;
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | if (ret_domain != NULL) {
|
|---|
| 75 | *ret_domain = dom_names[0];
|
|---|
| 76 | }
|
|---|
| 77 | if (ret_name != NULL) {
|
|---|
| 78 | *ret_name = talloc_strdup(mem_ctx, name);
|
|---|
| 79 | }
|
|---|
| 80 | if (ret_sid != NULL) {
|
|---|
| 81 | sid_copy(ret_sid, &sids[0]);
|
|---|
| 82 | }
|
|---|
| 83 | if (ret_type != NULL) {
|
|---|
| 84 | *ret_type = types[0];
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | done:
|
|---|
| 88 | if (is_valid_policy_hnd(&pol)) {
|
|---|
| 89 | dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
|
|---|
| 90 | }
|
|---|
| 91 | TALLOC_FREE(lsa_pipe);
|
|---|
| 92 |
|
|---|
| 93 | return status;
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | /****************************************************************************
|
|---|
| 97 | Connect to \\server\service.
|
|---|
| 98 | ****************************************************************************/
|
|---|
| 99 |
|
|---|
| 100 | NTSTATUS connect_to_service(struct net_context *c,
|
|---|
| 101 | struct cli_state **cli_ctx,
|
|---|
| 102 | struct sockaddr_storage *server_ss,
|
|---|
| 103 | const char *server_name,
|
|---|
| 104 | const char *service_name,
|
|---|
| 105 | const char *service_type)
|
|---|
| 106 | {
|
|---|
| 107 | NTSTATUS nt_status;
|
|---|
| 108 | int flags = 0;
|
|---|
| 109 |
|
|---|
| 110 | #ifdef __OS2__
|
|---|
| 111 | if (!c->opt_user_name || c->opt_user_name == NULL) {
|
|---|
| 112 | d_printf("The username was not set.\n");
|
|---|
| 113 | return NT_STATUS_LOGON_FAILURE;
|
|---|
| 114 | }
|
|---|
| 115 | #endif
|
|---|
| 116 | c->opt_password = net_prompt_pass(c, c->opt_user_name);
|
|---|
| 117 |
|
|---|
| 118 | if (c->opt_kerberos) {
|
|---|
| 119 | flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | if (c->opt_kerberos && c->opt_password) {
|
|---|
| 123 | flags |= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | if (c->opt_ccache) {
|
|---|
| 127 | flags |= CLI_FULL_CONNECTION_USE_CCACHE;
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | nt_status = cli_full_connection(cli_ctx, NULL, server_name,
|
|---|
| 131 | server_ss, c->opt_port,
|
|---|
| 132 | service_name, service_type,
|
|---|
| 133 | c->opt_user_name, c->opt_workgroup,
|
|---|
| 134 | c->opt_password, flags, Undefined);
|
|---|
| 135 | if (!NT_STATUS_IS_OK(nt_status)) {
|
|---|
| 136 | d_fprintf(stderr, _("Could not connect to server %s\n"),
|
|---|
| 137 | server_name);
|
|---|
| 138 |
|
|---|
| 139 | /* Display a nicer message depending on the result */
|
|---|
| 140 |
|
|---|
| 141 | if (NT_STATUS_V(nt_status) ==
|
|---|
| 142 | NT_STATUS_V(NT_STATUS_LOGON_FAILURE))
|
|---|
| 143 | d_fprintf(stderr,
|
|---|
| 144 | _("The username or password was not "
|
|---|
| 145 | "correct.\n"));
|
|---|
| 146 |
|
|---|
| 147 | if (NT_STATUS_V(nt_status) ==
|
|---|
| 148 | NT_STATUS_V(NT_STATUS_ACCOUNT_LOCKED_OUT))
|
|---|
| 149 | d_fprintf(stderr, _("The account was locked out.\n"));
|
|---|
| 150 |
|
|---|
| 151 | if (NT_STATUS_V(nt_status) ==
|
|---|
| 152 | NT_STATUS_V(NT_STATUS_ACCOUNT_DISABLED))
|
|---|
| 153 | d_fprintf(stderr, _("The account was disabled.\n"));
|
|---|
| 154 | return nt_status;
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | if (c->smb_encrypt) {
|
|---|
| 158 | nt_status = cli_force_encryption(*cli_ctx,
|
|---|
| 159 | c->opt_user_name,
|
|---|
| 160 | c->opt_password,
|
|---|
| 161 | c->opt_workgroup);
|
|---|
| 162 |
|
|---|
| 163 | if (NT_STATUS_EQUAL(nt_status,NT_STATUS_NOT_SUPPORTED)) {
|
|---|
| 164 | d_printf(_("Encryption required and "
|
|---|
| 165 | "server that doesn't support "
|
|---|
| 166 | "UNIX extensions - failing connect\n"));
|
|---|
| 167 | } else if (NT_STATUS_EQUAL(nt_status,NT_STATUS_UNKNOWN_REVISION)) {
|
|---|
| 168 | d_printf(_("Encryption required and "
|
|---|
| 169 | "can't get UNIX CIFS extensions "
|
|---|
| 170 | "version from server.\n"));
|
|---|
| 171 | } else if (NT_STATUS_EQUAL(nt_status,NT_STATUS_UNSUPPORTED_COMPRESSION)) {
|
|---|
| 172 | d_printf(_("Encryption required and "
|
|---|
| 173 | "share %s doesn't support "
|
|---|
| 174 | "encryption.\n"), service_name);
|
|---|
| 175 | } else if (!NT_STATUS_IS_OK(nt_status)) {
|
|---|
| 176 | d_printf(_("Encryption required and "
|
|---|
| 177 | "setup failed with error %s.\n"),
|
|---|
| 178 | nt_errstr(nt_status));
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | if (!NT_STATUS_IS_OK(nt_status)) {
|
|---|
| 182 | cli_shutdown(*cli_ctx);
|
|---|
| 183 | *cli_ctx = NULL;
|
|---|
| 184 | }
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | return nt_status;
|
|---|
| 188 | }
|
|---|
| 189 |
|
|---|
| 190 | /****************************************************************************
|
|---|
| 191 | Connect to \\server\ipc$.
|
|---|
| 192 | ****************************************************************************/
|
|---|
| 193 |
|
|---|
| 194 | NTSTATUS connect_to_ipc(struct net_context *c,
|
|---|
| 195 | struct cli_state **cli_ctx,
|
|---|
| 196 | struct sockaddr_storage *server_ss,
|
|---|
| 197 | const char *server_name)
|
|---|
| 198 | {
|
|---|
| 199 | return connect_to_service(c, cli_ctx, server_ss, server_name, "IPC$",
|
|---|
| 200 | "IPC");
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | /****************************************************************************
|
|---|
| 204 | Connect to \\server\ipc$ anonymously.
|
|---|
| 205 | ****************************************************************************/
|
|---|
| 206 |
|
|---|
| 207 | NTSTATUS connect_to_ipc_anonymous(struct net_context *c,
|
|---|
| 208 | struct cli_state **cli_ctx,
|
|---|
| 209 | struct sockaddr_storage *server_ss,
|
|---|
| 210 | const char *server_name)
|
|---|
| 211 | {
|
|---|
| 212 | NTSTATUS nt_status;
|
|---|
| 213 |
|
|---|
| 214 | nt_status = cli_full_connection(cli_ctx, c->opt_requester_name,
|
|---|
| 215 | server_name, server_ss, c->opt_port,
|
|---|
| 216 | "IPC$", "IPC",
|
|---|
| 217 | "", "",
|
|---|
| 218 | "", 0, Undefined);
|
|---|
| 219 |
|
|---|
| 220 | if (NT_STATUS_IS_OK(nt_status)) {
|
|---|
| 221 | return nt_status;
|
|---|
| 222 | } else {
|
|---|
| 223 | DEBUG(1,("Cannot connect to server (anonymously). Error was %s\n", nt_errstr(nt_status)));
|
|---|
| 224 | return nt_status;
|
|---|
| 225 | }
|
|---|
| 226 | }
|
|---|
| 227 |
|
|---|
| 228 | /****************************************************************************
|
|---|
| 229 | Return malloced user@realm for krb5 login.
|
|---|
| 230 | ****************************************************************************/
|
|---|
| 231 |
|
|---|
| 232 | static char *get_user_and_realm(const char *username)
|
|---|
| 233 | {
|
|---|
| 234 | char *user_and_realm = NULL;
|
|---|
| 235 |
|
|---|
| 236 | if (!username) {
|
|---|
| 237 | return NULL;
|
|---|
| 238 | }
|
|---|
| 239 | if (strchr_m(username, '@')) {
|
|---|
| 240 | user_and_realm = SMB_STRDUP(username);
|
|---|
| 241 | } else {
|
|---|
| 242 | if (asprintf(&user_and_realm, "%s@%s", username, lp_realm()) == -1) {
|
|---|
| 243 | user_and_realm = NULL;
|
|---|
| 244 | }
|
|---|
| 245 | }
|
|---|
| 246 | return user_and_realm;
|
|---|
| 247 | }
|
|---|
| 248 |
|
|---|
| 249 | /****************************************************************************
|
|---|
| 250 | Connect to \\server\ipc$ using KRB5.
|
|---|
| 251 | ****************************************************************************/
|
|---|
| 252 |
|
|---|
| 253 | NTSTATUS connect_to_ipc_krb5(struct net_context *c,
|
|---|
| 254 | struct cli_state **cli_ctx,
|
|---|
| 255 | struct sockaddr_storage *server_ss,
|
|---|
| 256 | const char *server_name)
|
|---|
| 257 | {
|
|---|
| 258 | NTSTATUS nt_status;
|
|---|
| 259 | char *user_and_realm = NULL;
|
|---|
| 260 |
|
|---|
| 261 | /* FIXME: Should get existing kerberos ticket if possible. */
|
|---|
| 262 | c->opt_password = net_prompt_pass(c, c->opt_user_name);
|
|---|
| 263 | if (!c->opt_password) {
|
|---|
| 264 | return NT_STATUS_NO_MEMORY;
|
|---|
| 265 | }
|
|---|
| 266 |
|
|---|
| 267 | user_and_realm = get_user_and_realm(c->opt_user_name);
|
|---|
| 268 | if (!user_and_realm) {
|
|---|
| 269 | return NT_STATUS_NO_MEMORY;
|
|---|
| 270 | }
|
|---|
| 271 |
|
|---|
| 272 | nt_status = cli_full_connection(cli_ctx, NULL, server_name,
|
|---|
| 273 | server_ss, c->opt_port,
|
|---|
| 274 | "IPC$", "IPC",
|
|---|
| 275 | user_and_realm, c->opt_workgroup,
|
|---|
| 276 | c->opt_password,
|
|---|
| 277 | CLI_FULL_CONNECTION_USE_KERBEROS,
|
|---|
| 278 | Undefined);
|
|---|
| 279 |
|
|---|
| 280 | SAFE_FREE(user_and_realm);
|
|---|
| 281 |
|
|---|
| 282 | if (!NT_STATUS_IS_OK(nt_status)) {
|
|---|
| 283 | DEBUG(1,("Cannot connect to server using kerberos. Error was %s\n", nt_errstr(nt_status)));
|
|---|
| 284 | return nt_status;
|
|---|
| 285 | }
|
|---|
| 286 |
|
|---|
| 287 | if (c->smb_encrypt) {
|
|---|
| 288 | nt_status = cli_cm_force_encryption(*cli_ctx,
|
|---|
| 289 | user_and_realm,
|
|---|
| 290 | c->opt_password,
|
|---|
| 291 | c->opt_workgroup,
|
|---|
| 292 | "IPC$");
|
|---|
| 293 | if (!NT_STATUS_IS_OK(nt_status)) {
|
|---|
| 294 | cli_shutdown(*cli_ctx);
|
|---|
| 295 | *cli_ctx = NULL;
|
|---|
| 296 | }
|
|---|
| 297 | }
|
|---|
| 298 |
|
|---|
| 299 | return nt_status;
|
|---|
| 300 | }
|
|---|
| 301 |
|
|---|
| 302 | /**
|
|---|
| 303 | * Connect a server and open a given pipe
|
|---|
| 304 | *
|
|---|
| 305 | * @param cli_dst A cli_state
|
|---|
| 306 | * @param pipe The pipe to open
|
|---|
| 307 | * @param got_pipe boolean that stores if we got a pipe
|
|---|
| 308 | *
|
|---|
| 309 | * @return Normal NTSTATUS return.
|
|---|
| 310 | **/
|
|---|
| 311 | NTSTATUS connect_dst_pipe(struct net_context *c, struct cli_state **cli_dst,
|
|---|
| 312 | struct rpc_pipe_client **pp_pipe_hnd,
|
|---|
| 313 | const struct ndr_syntax_id *interface)
|
|---|
| 314 | {
|
|---|
| 315 | NTSTATUS nt_status;
|
|---|
| 316 | char *server_name = SMB_STRDUP("127.0.0.1");
|
|---|
| 317 | struct cli_state *cli_tmp = NULL;
|
|---|
| 318 | struct rpc_pipe_client *pipe_hnd = NULL;
|
|---|
| 319 |
|
|---|
| 320 | if (server_name == NULL) {
|
|---|
| 321 | return NT_STATUS_NO_MEMORY;
|
|---|
| 322 | }
|
|---|
| 323 |
|
|---|
| 324 | if (c->opt_destination) {
|
|---|
| 325 | SAFE_FREE(server_name);
|
|---|
| 326 | if ((server_name = SMB_STRDUP(c->opt_destination)) == NULL) {
|
|---|
| 327 | return NT_STATUS_NO_MEMORY;
|
|---|
| 328 | }
|
|---|
| 329 | }
|
|---|
| 330 |
|
|---|
| 331 | /* make a connection to a named pipe */
|
|---|
| 332 | nt_status = connect_to_ipc(c, &cli_tmp, NULL, server_name);
|
|---|
| 333 | if (!NT_STATUS_IS_OK(nt_status)) {
|
|---|
| 334 | SAFE_FREE(server_name);
|
|---|
| 335 | return nt_status;
|
|---|
| 336 | }
|
|---|
| 337 |
|
|---|
| 338 | nt_status = cli_rpc_pipe_open_noauth(cli_tmp, interface,
|
|---|
| 339 | &pipe_hnd);
|
|---|
| 340 | if (!NT_STATUS_IS_OK(nt_status)) {
|
|---|
| 341 | DEBUG(0, ("couldn't not initialize pipe\n"));
|
|---|
| 342 | cli_shutdown(cli_tmp);
|
|---|
| 343 | SAFE_FREE(server_name);
|
|---|
| 344 | return nt_status;
|
|---|
| 345 | }
|
|---|
| 346 |
|
|---|
| 347 | *cli_dst = cli_tmp;
|
|---|
| 348 | *pp_pipe_hnd = pipe_hnd;
|
|---|
| 349 | SAFE_FREE(server_name);
|
|---|
| 350 |
|
|---|
| 351 | return nt_status;
|
|---|
| 352 | }
|
|---|
| 353 |
|
|---|
| 354 | /****************************************************************************
|
|---|
| 355 | Use the local machine account (krb) and password for this session.
|
|---|
| 356 | ****************************************************************************/
|
|---|
| 357 |
|
|---|
| 358 | int net_use_krb_machine_account(struct net_context *c)
|
|---|
| 359 | {
|
|---|
| 360 | char *user_name = NULL;
|
|---|
| 361 |
|
|---|
| 362 | if (!secrets_init()) {
|
|---|
| 363 | d_fprintf(stderr,_("ERROR: Unable to open secrets database\n"));
|
|---|
| 364 | exit(1);
|
|---|
| 365 | }
|
|---|
| 366 |
|
|---|
| 367 | c->opt_password = secrets_fetch_machine_password(
|
|---|
| 368 | c->opt_target_workgroup, NULL, NULL);
|
|---|
| 369 | if (asprintf(&user_name, "%s$@%s", global_myname(), lp_realm()) == -1) {
|
|---|
| 370 | return -1;
|
|---|
| 371 | }
|
|---|
| 372 | c->opt_user_name = user_name;
|
|---|
| 373 | return 0;
|
|---|
| 374 | }
|
|---|
| 375 |
|
|---|
| 376 | /****************************************************************************
|
|---|
| 377 | Use the machine account name and password for this session.
|
|---|
| 378 | ****************************************************************************/
|
|---|
| 379 |
|
|---|
| 380 | int net_use_machine_account(struct net_context *c)
|
|---|
| 381 | {
|
|---|
| 382 | char *user_name = NULL;
|
|---|
| 383 |
|
|---|
| 384 | if (!secrets_init()) {
|
|---|
| 385 | d_fprintf(stderr,_("ERROR: Unable to open secrets database\n"));
|
|---|
| 386 | exit(1);
|
|---|
| 387 | }
|
|---|
| 388 |
|
|---|
| 389 | c->opt_password = secrets_fetch_machine_password(
|
|---|
| 390 | c->opt_target_workgroup, NULL, NULL);
|
|---|
| 391 | if (asprintf(&user_name, "%s$", global_myname()) == -1) {
|
|---|
| 392 | return -1;
|
|---|
| 393 | }
|
|---|
| 394 | c->opt_user_name = user_name;
|
|---|
| 395 | return 0;
|
|---|
| 396 | }
|
|---|
| 397 |
|
|---|
| 398 | bool net_find_server(struct net_context *c,
|
|---|
| 399 | const char *domain,
|
|---|
| 400 | unsigned flags,
|
|---|
| 401 | struct sockaddr_storage *server_ss,
|
|---|
| 402 | char **server_name)
|
|---|
| 403 | {
|
|---|
| 404 | const char *d = domain ? domain : c->opt_target_workgroup;
|
|---|
| 405 |
|
|---|
| 406 | if (c->opt_host) {
|
|---|
| 407 | *server_name = SMB_STRDUP(c->opt_host);
|
|---|
| 408 | }
|
|---|
| 409 |
|
|---|
| 410 | if (c->opt_have_ip) {
|
|---|
| 411 | *server_ss = c->opt_dest_ip;
|
|---|
| 412 | if (!*server_name) {
|
|---|
| 413 | char addr[INET6_ADDRSTRLEN];
|
|---|
| 414 | print_sockaddr(addr, sizeof(addr), &c->opt_dest_ip);
|
|---|
| 415 | *server_name = SMB_STRDUP(addr);
|
|---|
| 416 | }
|
|---|
| 417 | } else if (*server_name) {
|
|---|
| 418 | /* resolve the IP address */
|
|---|
| 419 | if (!resolve_name(*server_name, server_ss, 0x20, false)) {
|
|---|
| 420 | DEBUG(1,("Unable to resolve server name\n"));
|
|---|
| 421 | return false;
|
|---|
| 422 | }
|
|---|
| 423 | } else if (flags & NET_FLAGS_PDC) {
|
|---|
| 424 | fstring dc_name;
|
|---|
| 425 | struct sockaddr_storage pdc_ss;
|
|---|
| 426 |
|
|---|
| 427 | if (!get_pdc_ip(d, &pdc_ss)) {
|
|---|
| 428 | DEBUG(1,("Unable to resolve PDC server address\n"));
|
|---|
| 429 | return false;
|
|---|
| 430 | }
|
|---|
| 431 |
|
|---|
| 432 | if (is_zero_addr(&pdc_ss)) {
|
|---|
| 433 | return false;
|
|---|
| 434 | }
|
|---|
| 435 |
|
|---|
| 436 | if (!name_status_find(d, 0x1b, 0x20, &pdc_ss, dc_name)) {
|
|---|
| 437 | return false;
|
|---|
| 438 | }
|
|---|
| 439 |
|
|---|
| 440 | *server_name = SMB_STRDUP(dc_name);
|
|---|
| 441 | *server_ss = pdc_ss;
|
|---|
| 442 | } else if (flags & NET_FLAGS_DMB) {
|
|---|
| 443 | struct sockaddr_storage msbrow_ss;
|
|---|
| 444 | char addr[INET6_ADDRSTRLEN];
|
|---|
| 445 |
|
|---|
| 446 | /* if (!resolve_name(MSBROWSE, &msbrow_ip, 1, false)) */
|
|---|
| 447 | if (!resolve_name(d, &msbrow_ss, 0x1B, false)) {
|
|---|
| 448 | DEBUG(1,("Unable to resolve domain browser via name lookup\n"));
|
|---|
| 449 | return false;
|
|---|
| 450 | }
|
|---|
| 451 | *server_ss = msbrow_ss;
|
|---|
| 452 | print_sockaddr(addr, sizeof(addr), server_ss);
|
|---|
| 453 | *server_name = SMB_STRDUP(addr);
|
|---|
| 454 | } else if (flags & NET_FLAGS_MASTER) {
|
|---|
| 455 | struct sockaddr_storage brow_ss;
|
|---|
| 456 | char addr[INET6_ADDRSTRLEN];
|
|---|
| 457 | if (!resolve_name(d, &brow_ss, 0x1D, false)) {
|
|---|
| 458 | /* go looking for workgroups */
|
|---|
| 459 | DEBUG(1,("Unable to resolve master browser via name lookup\n"));
|
|---|
| 460 | return false;
|
|---|
| 461 | }
|
|---|
| 462 | *server_ss = brow_ss;
|
|---|
| 463 | print_sockaddr(addr, sizeof(addr), server_ss);
|
|---|
| 464 | *server_name = SMB_STRDUP(addr);
|
|---|
| 465 | } else if (!(flags & NET_FLAGS_LOCALHOST_DEFAULT_INSANE)) {
|
|---|
| 466 | if (!interpret_string_addr(server_ss,
|
|---|
| 467 | "127.0.0.1", AI_NUMERICHOST)) {
|
|---|
| 468 | DEBUG(1,("Unable to resolve 127.0.0.1\n"));
|
|---|
| 469 | return false;
|
|---|
| 470 | }
|
|---|
| 471 | *server_name = SMB_STRDUP("127.0.0.1");
|
|---|
| 472 | }
|
|---|
| 473 |
|
|---|
| 474 | if (!*server_name) {
|
|---|
| 475 | DEBUG(1,("no server to connect to\n"));
|
|---|
| 476 | return false;
|
|---|
| 477 | }
|
|---|
| 478 |
|
|---|
| 479 | return true;
|
|---|
| 480 | }
|
|---|
| 481 |
|
|---|
| 482 | bool net_find_pdc(struct sockaddr_storage *server_ss,
|
|---|
| 483 | fstring server_name,
|
|---|
| 484 | const char *domain_name)
|
|---|
| 485 | {
|
|---|
| 486 | if (!get_pdc_ip(domain_name, server_ss)) {
|
|---|
| 487 | return false;
|
|---|
| 488 | }
|
|---|
| 489 | if (is_zero_addr(server_ss)) {
|
|---|
| 490 | return false;
|
|---|
| 491 | }
|
|---|
| 492 |
|
|---|
| 493 | if (!name_status_find(domain_name, 0x1b, 0x20, server_ss, server_name)) {
|
|---|
| 494 | return false;
|
|---|
| 495 | }
|
|---|
| 496 |
|
|---|
| 497 | return true;
|
|---|
| 498 | }
|
|---|
| 499 |
|
|---|
| 500 | NTSTATUS net_make_ipc_connection(struct net_context *c, unsigned flags,
|
|---|
| 501 | struct cli_state **pcli)
|
|---|
| 502 | {
|
|---|
| 503 | return net_make_ipc_connection_ex(c, c->opt_workgroup, NULL, NULL, flags, pcli);
|
|---|
| 504 | }
|
|---|
| 505 |
|
|---|
| 506 | NTSTATUS net_make_ipc_connection_ex(struct net_context *c ,const char *domain,
|
|---|
| 507 | const char *server,
|
|---|
| 508 | struct sockaddr_storage *pss,
|
|---|
| 509 | unsigned flags, struct cli_state **pcli)
|
|---|
| 510 | {
|
|---|
| 511 | char *server_name = NULL;
|
|---|
| 512 | struct sockaddr_storage server_ss;
|
|---|
| 513 | struct cli_state *cli = NULL;
|
|---|
| 514 | NTSTATUS nt_status;
|
|---|
| 515 |
|
|---|
| 516 | if ( !server || !pss ) {
|
|---|
| 517 | if (!net_find_server(c, domain, flags, &server_ss,
|
|---|
| 518 | &server_name)) {
|
|---|
| 519 | d_fprintf(stderr, _("Unable to find a suitable server "
|
|---|
| 520 | "for domain %s\n"), domain);
|
|---|
| 521 | nt_status = NT_STATUS_UNSUCCESSFUL;
|
|---|
| 522 | goto done;
|
|---|
| 523 | }
|
|---|
| 524 | } else {
|
|---|
| 525 | server_name = SMB_STRDUP( server );
|
|---|
| 526 | server_ss = *pss;
|
|---|
| 527 | }
|
|---|
| 528 |
|
|---|
| 529 | if (flags & NET_FLAGS_ANONYMOUS) {
|
|---|
| 530 | nt_status = connect_to_ipc_anonymous(c, &cli, &server_ss,
|
|---|
| 531 | server_name);
|
|---|
| 532 | } else {
|
|---|
| 533 | nt_status = connect_to_ipc(c, &cli, &server_ss,
|
|---|
| 534 | server_name);
|
|---|
| 535 | }
|
|---|
| 536 |
|
|---|
| 537 | /* store the server in the affinity cache if it was a PDC */
|
|---|
| 538 |
|
|---|
| 539 | if ( (flags & NET_FLAGS_PDC) && NT_STATUS_IS_OK(nt_status) )
|
|---|
| 540 | saf_store( cli->server_domain, cli->desthost );
|
|---|
| 541 |
|
|---|
| 542 | SAFE_FREE(server_name);
|
|---|
| 543 | if (!NT_STATUS_IS_OK(nt_status)) {
|
|---|
| 544 | d_fprintf(stderr, _("Connection failed: %s\n"),
|
|---|
| 545 | nt_errstr(nt_status));
|
|---|
| 546 | cli = NULL;
|
|---|
| 547 | } else if (c->opt_request_timeout) {
|
|---|
| 548 | cli_set_timeout(cli, c->opt_request_timeout * 1000);
|
|---|
| 549 | }
|
|---|
| 550 |
|
|---|
| 551 | done:
|
|---|
| 552 | if (pcli != NULL) {
|
|---|
| 553 | *pcli = cli;
|
|---|
| 554 | }
|
|---|
| 555 | return nt_status;
|
|---|
| 556 | }
|
|---|
| 557 |
|
|---|
| 558 | /****************************************************************************
|
|---|
| 559 | ****************************************************************************/
|
|---|
| 560 |
|
|---|
| 561 | const char *net_prompt_pass(struct net_context *c, const char *user)
|
|---|
| 562 | {
|
|---|
| 563 | char *prompt = NULL;
|
|---|
| 564 | const char *pass = NULL;
|
|---|
| 565 |
|
|---|
| 566 | if (c->opt_password) {
|
|---|
| 567 | return c->opt_password;
|
|---|
| 568 | }
|
|---|
| 569 |
|
|---|
| 570 | if (c->opt_machine_pass) {
|
|---|
| 571 | return NULL;
|
|---|
| 572 | }
|
|---|
| 573 |
|
|---|
| 574 | if (c->opt_kerberos && !c->opt_user_specified) {
|
|---|
| 575 | return NULL;
|
|---|
| 576 | }
|
|---|
| 577 |
|
|---|
| 578 | if (asprintf(&prompt, _("Enter %s's password:"), user) == -1) {
|
|---|
| 579 | return NULL;
|
|---|
| 580 | }
|
|---|
| 581 |
|
|---|
| 582 | pass = getpass(prompt);
|
|---|
| 583 | SAFE_FREE(prompt);
|
|---|
| 584 |
|
|---|
| 585 | return pass;
|
|---|
| 586 | }
|
|---|
| 587 |
|
|---|
| 588 | int net_run_function(struct net_context *c, int argc, const char **argv,
|
|---|
| 589 | const char *whoami, struct functable *table)
|
|---|
| 590 | {
|
|---|
| 591 | int i;
|
|---|
| 592 |
|
|---|
| 593 | if (argc != 0) {
|
|---|
| 594 | for (i=0; table[i].funcname != NULL; i++) {
|
|---|
| 595 | if (StrCaseCmp(argv[0], table[i].funcname) == 0)
|
|---|
| 596 | return table[i].fn(c, argc-1, argv+1);
|
|---|
| 597 | }
|
|---|
| 598 | }
|
|---|
| 599 |
|
|---|
| 600 | if (c->display_usage == false) {
|
|---|
| 601 | d_fprintf(stderr, _("Invalid command: %s %s\n"), whoami,
|
|---|
| 602 | (argc > 0)?argv[0]:"");
|
|---|
| 603 | }
|
|---|
| 604 | d_printf(_("Usage:\n"));
|
|---|
| 605 | for (i=0; table[i].funcname != NULL; i++) {
|
|---|
| 606 | if(c->display_usage == false)
|
|---|
| 607 | d_printf("%s %-15s %s\n", whoami, table[i].funcname,
|
|---|
| 608 | _(table[i].description));
|
|---|
| 609 | else
|
|---|
| 610 | d_printf("%s\n", _(table[i].usage));
|
|---|
| 611 | }
|
|---|
| 612 |
|
|---|
| 613 | return c->display_usage?0:-1;
|
|---|
| 614 | }
|
|---|
| 615 |
|
|---|
| 616 | void net_display_usage_from_functable(struct functable *table)
|
|---|
| 617 | {
|
|---|
| 618 | int i;
|
|---|
| 619 | for (i=0; table[i].funcname != NULL; i++) {
|
|---|
| 620 | d_printf("%s\n", _(table[i].usage));
|
|---|
| 621 | }
|
|---|
| 622 | }
|
|---|
| 623 |
|
|---|
| 624 | const char *net_share_type_str(int num_type)
|
|---|
| 625 | {
|
|---|
| 626 | switch(num_type) {
|
|---|
| 627 | case 0: return _("Disk");
|
|---|
| 628 | case 1: return _("Print");
|
|---|
| 629 | case 2: return _("Dev");
|
|---|
| 630 | case 3: return _("IPC");
|
|---|
| 631 | default: return _("Unknown");
|
|---|
| 632 | }
|
|---|
| 633 | }
|
|---|
| 634 |
|
|---|
| 635 | static NTSTATUS net_scan_dc_noad(struct net_context *c,
|
|---|
| 636 | struct cli_state *cli,
|
|---|
| 637 | struct net_dc_info *dc_info)
|
|---|
| 638 | {
|
|---|
| 639 | TALLOC_CTX *mem_ctx = talloc_tos();
|
|---|
| 640 | struct rpc_pipe_client *pipe_hnd = NULL;
|
|---|
| 641 | struct dcerpc_binding_handle *b;
|
|---|
| 642 | NTSTATUS status, result;
|
|---|
| 643 | struct policy_handle pol;
|
|---|
| 644 | union lsa_PolicyInformation *info;
|
|---|
| 645 |
|
|---|
| 646 | ZERO_STRUCTP(dc_info);
|
|---|
| 647 | ZERO_STRUCT(pol);
|
|---|
| 648 |
|
|---|
| 649 | status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
|
|---|
| 650 | &pipe_hnd);
|
|---|
| 651 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 652 | return status;
|
|---|
| 653 | }
|
|---|
| 654 |
|
|---|
| 655 | b = pipe_hnd->binding_handle;
|
|---|
| 656 |
|
|---|
| 657 | status = dcerpc_lsa_open_policy(b, mem_ctx,
|
|---|
| 658 | false,
|
|---|
| 659 | SEC_FLAG_MAXIMUM_ALLOWED,
|
|---|
| 660 | &pol,
|
|---|
| 661 | &result);
|
|---|
| 662 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 663 | goto done;
|
|---|
| 664 | }
|
|---|
| 665 | if (!NT_STATUS_IS_OK(result)) {
|
|---|
| 666 | status = result;
|
|---|
| 667 | goto done;
|
|---|
| 668 | }
|
|---|
| 669 |
|
|---|
| 670 | status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
|
|---|
| 671 | &pol,
|
|---|
| 672 | LSA_POLICY_INFO_ACCOUNT_DOMAIN,
|
|---|
| 673 | &info,
|
|---|
| 674 | &result);
|
|---|
| 675 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 676 | goto done;
|
|---|
| 677 | }
|
|---|
| 678 | if (!NT_STATUS_IS_OK(result)) {
|
|---|
| 679 | status = result;
|
|---|
| 680 | goto done;
|
|---|
| 681 | }
|
|---|
| 682 |
|
|---|
| 683 | dc_info->netbios_domain_name = talloc_strdup(mem_ctx, info->account_domain.name.string);
|
|---|
| 684 | if (dc_info->netbios_domain_name == NULL) {
|
|---|
| 685 | status = NT_STATUS_NO_MEMORY;
|
|---|
| 686 | goto done;
|
|---|
| 687 | }
|
|---|
| 688 |
|
|---|
| 689 | done:
|
|---|
| 690 | if (is_valid_policy_hnd(&pol)) {
|
|---|
| 691 | dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
|
|---|
| 692 | }
|
|---|
| 693 |
|
|---|
| 694 | TALLOC_FREE(pipe_hnd);
|
|---|
| 695 |
|
|---|
| 696 | return status;
|
|---|
| 697 | }
|
|---|
| 698 |
|
|---|
| 699 | NTSTATUS net_scan_dc(struct net_context *c,
|
|---|
| 700 | struct cli_state *cli,
|
|---|
| 701 | struct net_dc_info *dc_info)
|
|---|
| 702 | {
|
|---|
| 703 | TALLOC_CTX *mem_ctx = talloc_tos();
|
|---|
| 704 | struct rpc_pipe_client *dssetup_pipe = NULL;
|
|---|
| 705 | struct dcerpc_binding_handle *dssetup_handle = NULL;
|
|---|
| 706 | union dssetup_DsRoleInfo info;
|
|---|
| 707 | NTSTATUS status;
|
|---|
| 708 | WERROR werr;
|
|---|
| 709 |
|
|---|
| 710 | ZERO_STRUCTP(dc_info);
|
|---|
| 711 |
|
|---|
| 712 | status = cli_rpc_pipe_open_noauth(cli, &ndr_table_dssetup.syntax_id,
|
|---|
| 713 | &dssetup_pipe);
|
|---|
| 714 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 715 | DEBUG(10,("net_scan_dc: failed to open dssetup pipe with %s, "
|
|---|
| 716 | "retrying with lsa pipe\n", nt_errstr(status)));
|
|---|
| 717 | return net_scan_dc_noad(c, cli, dc_info);
|
|---|
| 718 | }
|
|---|
| 719 | dssetup_handle = dssetup_pipe->binding_handle;
|
|---|
| 720 |
|
|---|
| 721 | status = dcerpc_dssetup_DsRoleGetPrimaryDomainInformation(dssetup_handle, mem_ctx,
|
|---|
| 722 | DS_ROLE_BASIC_INFORMATION,
|
|---|
| 723 | &info,
|
|---|
| 724 | &werr);
|
|---|
| 725 | TALLOC_FREE(dssetup_pipe);
|
|---|
| 726 |
|
|---|
| 727 | if (NT_STATUS_IS_OK(status)) {
|
|---|
| 728 | status = werror_to_ntstatus(werr);
|
|---|
| 729 | }
|
|---|
| 730 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 731 | return status;
|
|---|
| 732 | }
|
|---|
| 733 |
|
|---|
| 734 | dc_info->is_dc = (info.basic.role & (DS_ROLE_PRIMARY_DC|DS_ROLE_BACKUP_DC));
|
|---|
| 735 | dc_info->is_pdc = (info.basic.role & DS_ROLE_PRIMARY_DC);
|
|---|
| 736 | dc_info->is_ad = (info.basic.flags & DS_ROLE_PRIMARY_DS_RUNNING);
|
|---|
| 737 | dc_info->is_mixed_mode = (info.basic.flags & DS_ROLE_PRIMARY_DS_MIXED_MODE);
|
|---|
| 738 | dc_info->netbios_domain_name = talloc_strdup(mem_ctx, info.basic.domain);
|
|---|
| 739 | dc_info->dns_domain_name = talloc_strdup(mem_ctx, info.basic.dns_domain);
|
|---|
| 740 | dc_info->forest_name = talloc_strdup(mem_ctx, info.basic.forest);
|
|---|
| 741 |
|
|---|
| 742 | return NT_STATUS_OK;
|
|---|
| 743 | }
|
|---|