python: models: rename argument ldb to samdb
[samba.git] / source3 / utils / net_util.c
blob9c7c74b52926a17b8c26335c87faad354ef7df69
1 /*
2 * Unix SMB/CIFS implementation.
3 * Helper routines for net
4 * Copyright (C) Volker Lendecke 2006
5 * Copyright (C) Kai Blin 2008
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.
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.
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/>.
22 #include "includes.h"
23 #include "utils/net.h"
24 #include "libsmb/namequery.h"
25 #include "rpc_client/cli_pipe.h"
26 #include "../librpc/gen_ndr/ndr_lsa_c.h"
27 #include "rpc_client/cli_lsarpc.h"
28 #include "../librpc/gen_ndr/ndr_dssetup_c.h"
29 #include "secrets.h"
30 #include "../libcli/security/security.h"
31 #include "libsmb/libsmb.h"
32 #include "lib/param/param.h"
33 #include "auth/gensec/gensec.h"
34 #include "libcli/auth/netlogon_creds_cli.h"
35 #include "lib/cmdline/cmdline.h"
37 NTSTATUS net_rpc_lookup_name(struct net_context *c,
38 TALLOC_CTX *mem_ctx, struct cli_state *cli,
39 const char *name, const char **ret_domain,
40 const char **ret_name, struct dom_sid *ret_sid,
41 enum lsa_SidType *ret_type)
43 struct rpc_pipe_client *lsa_pipe = NULL;
44 struct policy_handle pol;
45 NTSTATUS status, result;
46 const char **dom_names;
47 struct dom_sid *sids;
48 enum lsa_SidType *types;
49 struct dcerpc_binding_handle *b;
51 ZERO_STRUCT(pol);
53 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc,
54 &lsa_pipe);
55 if (!NT_STATUS_IS_OK(status)) {
56 d_fprintf(stderr, _("Could not initialise lsa pipe\n"));
57 return status;
60 b = lsa_pipe->binding_handle;
62 status = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, false,
63 SEC_FLAG_MAXIMUM_ALLOWED,
64 &pol);
65 if (!NT_STATUS_IS_OK(status)) {
66 d_fprintf(stderr, "open_policy %s: %s\n", _("failed"),
67 nt_errstr(status));
68 return status;
71 status = rpccli_lsa_lookup_names(lsa_pipe, mem_ctx, &pol, 1,
72 &name, &dom_names, 1, &sids, &types);
74 if (!NT_STATUS_IS_OK(status)) {
75 /* This can happen easily, don't log an error */
76 goto done;
79 if (ret_domain != NULL) {
80 *ret_domain = dom_names[0];
82 if (ret_name != NULL) {
83 *ret_name = talloc_strdup(mem_ctx, name);
85 if (ret_sid != NULL) {
86 sid_copy(ret_sid, &sids[0]);
88 if (ret_type != NULL) {
89 *ret_type = types[0];
92 done:
93 if (is_valid_policy_hnd(&pol)) {
94 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
96 TALLOC_FREE(lsa_pipe);
98 return status;
101 /****************************************************************************
102 Connect to \\server\service.
103 ****************************************************************************/
105 NTSTATUS connect_to_service(struct net_context *c,
106 struct cli_state **cli_ctx,
107 const struct sockaddr_storage *server_ss,
108 const char *server_name,
109 const char *service_name,
110 const char *service_type)
112 NTSTATUS nt_status;
113 int flags = 0;
115 if (strequal(service_type, "IPC")) {
116 flags |= CLI_FULL_CONNECTION_IPC;
119 nt_status = cli_full_connection_creds(c,
120 cli_ctx,
121 NULL,
122 server_name,
123 server_ss,
124 c->opt_port,
125 service_name,
126 service_type,
127 c->creds,
128 flags);
129 if (!NT_STATUS_IS_OK(nt_status)) {
130 d_fprintf(stderr, _("Could not connect to server %s\n"),
131 server_name);
133 /* Display a nicer message depending on the result */
135 if (NT_STATUS_V(nt_status) ==
136 NT_STATUS_V(NT_STATUS_LOGON_FAILURE))
137 d_fprintf(stderr,
138 _("The username or password was not "
139 "correct.\n"));
141 if (NT_STATUS_V(nt_status) ==
142 NT_STATUS_V(NT_STATUS_ACCOUNT_LOCKED_OUT))
143 d_fprintf(stderr, _("The account was locked out.\n"));
145 if (NT_STATUS_V(nt_status) ==
146 NT_STATUS_V(NT_STATUS_ACCOUNT_DISABLED))
147 d_fprintf(stderr, _("The account was disabled.\n"));
148 return nt_status;
151 return nt_status;
154 /****************************************************************************
155 Connect to \\server\ipc$.
156 ****************************************************************************/
158 NTSTATUS connect_to_ipc(struct net_context *c,
159 struct cli_state **cli_ctx,
160 const struct sockaddr_storage *server_ss,
161 const char *server_name)
163 return connect_to_service(c, cli_ctx, server_ss, server_name, "IPC$",
164 "IPC");
167 /****************************************************************************
168 Connect to \\server\ipc$ anonymously.
169 ****************************************************************************/
171 NTSTATUS connect_to_ipc_anonymous(struct net_context *c,
172 struct cli_state **cli_ctx,
173 const struct sockaddr_storage *server_ss,
174 const char *server_name)
176 NTSTATUS nt_status;
177 struct cli_credentials *anon_creds = NULL;
179 anon_creds = cli_credentials_init_anon(c);
180 if (anon_creds == NULL) {
181 DBG_ERR("cli_credentials_init_anon() failed\n");
182 return NT_STATUS_NO_MEMORY;
185 nt_status = cli_full_connection_creds(c,
186 cli_ctx,
187 c->opt_requester_name,
188 server_name,
189 server_ss,
190 c->opt_port,
191 "IPC$",
192 "IPC",
193 anon_creds,
194 CLI_FULL_CONNECTION_IPC);
196 if (NT_STATUS_IS_OK(nt_status)) {
197 return nt_status;
198 } else {
199 DEBUG(1,("Cannot connect to server (anonymously). Error was %s\n", nt_errstr(nt_status)));
200 return nt_status;
205 * Connect a server and open a given pipe
207 * @param cli_dst A cli_state
208 * @param pipe The pipe to open
209 * @param got_pipe boolean that stores if we got a pipe
211 * @return Normal NTSTATUS return.
213 NTSTATUS connect_dst_pipe(struct net_context *c, struct cli_state **cli_dst,
214 struct rpc_pipe_client **pp_pipe_hnd,
215 const struct ndr_interface_table *table)
217 NTSTATUS nt_status;
218 char *server_name = SMB_STRDUP("127.0.0.1");
219 struct cli_state *cli_tmp = NULL;
220 struct rpc_pipe_client *pipe_hnd = NULL;
222 if (server_name == NULL) {
223 return NT_STATUS_NO_MEMORY;
226 if (c->opt_destination) {
227 SAFE_FREE(server_name);
228 if ((server_name = SMB_STRDUP(c->opt_destination)) == NULL) {
229 return NT_STATUS_NO_MEMORY;
233 /* make a connection to a named pipe */
234 nt_status = connect_to_ipc(c, &cli_tmp, NULL, server_name);
235 if (!NT_STATUS_IS_OK(nt_status)) {
236 SAFE_FREE(server_name);
237 return nt_status;
240 nt_status = cli_rpc_pipe_open_noauth(cli_tmp, table,
241 &pipe_hnd);
242 if (!NT_STATUS_IS_OK(nt_status)) {
243 DEBUG(0, ("couldn't not initialize pipe\n"));
244 cli_shutdown(cli_tmp);
245 SAFE_FREE(server_name);
246 return nt_status;
249 *cli_dst = cli_tmp;
250 *pp_pipe_hnd = pipe_hnd;
251 SAFE_FREE(server_name);
253 return nt_status;
256 /****************************************************************************
257 Use the local machine account (krb) and password for this session.
258 ****************************************************************************/
260 int net_use_krb_machine_account(struct net_context *c)
262 char *user_name = NULL;
264 if (!secrets_init()) {
265 d_fprintf(stderr,_("ERROR: Unable to open secrets database\n"));
266 exit(1);
269 c->opt_password = secrets_fetch_machine_password(
270 c->opt_target_workgroup, NULL, NULL);
271 if (asprintf(&user_name, "%s$@%s", lp_netbios_name(), lp_realm()) == -1) {
272 return -1;
274 c->opt_user_name = user_name;
275 c->opt_user_specified = true;
277 cli_credentials_set_machine_account(c->creds, c->lp_ctx);
278 return 0;
281 bool net_find_server(struct net_context *c,
282 const char *domain,
283 unsigned flags,
284 struct sockaddr_storage *server_ss,
285 char **server_name)
287 const char *d = domain ? domain : c->opt_target_workgroup;
289 if (c->opt_host) {
290 *server_name = SMB_STRDUP(c->opt_host);
293 if (c->opt_have_ip) {
294 *server_ss = c->opt_dest_ip;
295 if (!*server_name) {
296 char addr[INET6_ADDRSTRLEN];
297 print_sockaddr(addr, sizeof(addr), &c->opt_dest_ip);
298 *server_name = SMB_STRDUP(addr);
300 } else if (*server_name) {
301 /* resolve the IP address */
302 if (!resolve_name(*server_name, server_ss, 0x20, false)) {
303 DEBUG(1,("Unable to resolve server name\n"));
304 return false;
306 } else if (flags & NET_FLAGS_PDC) {
307 fstring dc_name;
308 struct sockaddr_storage pdc_ss;
310 if (!get_pdc_ip(d, &pdc_ss)) {
311 DEBUG(1,("Unable to resolve PDC server address\n"));
312 return false;
315 if (is_zero_addr(&pdc_ss)) {
316 return false;
319 if (!name_status_find(d, 0x1b, 0x20, &pdc_ss, dc_name)) {
320 return false;
323 *server_name = SMB_STRDUP(dc_name);
324 *server_ss = pdc_ss;
325 } else if (flags & NET_FLAGS_DMB) {
326 struct sockaddr_storage msbrow_ss;
327 char addr[INET6_ADDRSTRLEN];
329 /* if (!resolve_name(MSBROWSE, &msbrow_ip, 1, false)) */
330 if (!resolve_name(d, &msbrow_ss, 0x1B, false)) {
331 DEBUG(1,("Unable to resolve domain browser via name lookup\n"));
332 return false;
334 *server_ss = msbrow_ss;
335 print_sockaddr(addr, sizeof(addr), server_ss);
336 *server_name = SMB_STRDUP(addr);
337 } else if (flags & NET_FLAGS_MASTER) {
338 struct sockaddr_storage brow_ss;
339 char addr[INET6_ADDRSTRLEN];
340 if (!resolve_name(d, &brow_ss, 0x1D, false)) {
341 /* go looking for workgroups */
342 DEBUG(1,("Unable to resolve master browser via name lookup\n"));
343 return false;
345 *server_ss = brow_ss;
346 print_sockaddr(addr, sizeof(addr), server_ss);
347 *server_name = SMB_STRDUP(addr);
348 } else if (!(flags & NET_FLAGS_LOCALHOST_DEFAULT_INSANE)) {
349 if (!interpret_string_addr(server_ss,
350 "127.0.0.1", AI_NUMERICHOST)) {
351 DEBUG(1,("Unable to resolve 127.0.0.1\n"));
352 return false;
354 *server_name = SMB_STRDUP("127.0.0.1");
357 if (!*server_name) {
358 DEBUG(1,("no server to connect to\n"));
359 return false;
362 return true;
365 bool net_find_pdc(struct sockaddr_storage *server_ss,
366 fstring server_name,
367 const char *domain_name)
369 if (!get_pdc_ip(domain_name, server_ss)) {
370 return false;
372 if (is_zero_addr(server_ss)) {
373 return false;
376 if (!name_status_find(domain_name, 0x1b, 0x20, server_ss, server_name)) {
377 return false;
380 return true;
383 NTSTATUS net_make_ipc_connection(struct net_context *c, unsigned flags,
384 struct cli_state **pcli)
386 return net_make_ipc_connection_ex(c, c->opt_workgroup, NULL, NULL, flags, pcli);
389 NTSTATUS net_make_ipc_connection_ex(struct net_context *c ,const char *domain,
390 const char *server,
391 const struct sockaddr_storage *pss,
392 unsigned flags, struct cli_state **pcli)
394 char *server_name = NULL;
395 struct sockaddr_storage server_ss;
396 struct cli_state *cli = NULL;
397 NTSTATUS nt_status;
399 if ( !server || !pss ) {
400 if (!net_find_server(c, domain, flags, &server_ss,
401 &server_name)) {
402 d_fprintf(stderr, _("Unable to find a suitable server "
403 "for domain %s\n"), domain);
404 nt_status = NT_STATUS_UNSUCCESSFUL;
405 goto done;
407 } else {
408 server_name = SMB_STRDUP( server );
409 server_ss = *pss;
412 if (flags & NET_FLAGS_ANONYMOUS) {
413 nt_status = connect_to_ipc_anonymous(c, &cli, &server_ss,
414 server_name);
415 } else {
416 nt_status = connect_to_ipc(c, &cli, &server_ss,
417 server_name);
420 /* store the server in the affinity cache if it was a PDC */
422 if ( (flags & NET_FLAGS_PDC) && NT_STATUS_IS_OK(nt_status) )
423 saf_store(cli->server_domain, server_name);
425 SAFE_FREE(server_name);
426 if (!NT_STATUS_IS_OK(nt_status)) {
427 d_fprintf(stderr, _("Connection failed: %s\n"),
428 nt_errstr(nt_status));
429 cli = NULL;
430 } else if (c->opt_request_timeout) {
431 cli_set_timeout(cli, c->opt_request_timeout * 1000);
434 done:
435 if (pcli != NULL) {
436 *pcli = cli;
438 return nt_status;
441 /****************************************************************************
442 ****************************************************************************/
444 /* TODO FIXME: Pass cli_creds via net_context and get rid of this function. */
445 const char *net_prompt_pass(struct net_context *c, const char *user)
447 struct cli_credentials *creds = samba_cmdline_get_creds();
449 if (c->opt_password == NULL) {
450 c->opt_password = cli_credentials_get_password(creds);
453 return c->opt_password;
456 int net_run_function(struct net_context *c, int argc, const char **argv,
457 const char *whoami, struct functable *table)
459 int i;
461 if (argc != 0) {
462 for (i=0; table[i].funcname != NULL; i++) {
463 if (strcasecmp_m(argv[0], table[i].funcname) == 0)
464 return table[i].fn(c, argc-1, argv+1);
468 if (c->display_usage == false) {
469 d_fprintf(stderr, _("Invalid command: %s %s\n"), whoami,
470 (argc > 0)?argv[0]:"");
472 d_printf(_("Usage:\n"));
473 for (i=0; table[i].funcname != NULL; i++) {
474 if(c->display_usage == false)
475 d_printf("%s %-15s %s\n", whoami, table[i].funcname,
476 _(table[i].description));
477 else
478 d_printf("%s\n", _(table[i].usage));
481 return c->display_usage?0:-1;
484 void net_display_usage_from_functable(struct functable *table)
486 int i;
487 for (i=0; table[i].funcname != NULL; i++) {
488 d_printf("%s\n", _(table[i].usage));
492 void net_warn_member_options(void)
494 TALLOC_CTX *frame = talloc_stackframe();
495 struct loadparm_context *lp_ctx = NULL;
497 lp_ctx = loadparm_init_s3(frame, loadparm_s3_helpers());
498 if (lp_ctx != NULL) {
499 netlogon_creds_cli_warn_options(lp_ctx);
502 TALLOC_FREE(frame);
505 const char *net_share_type_str(int num_type)
507 switch(num_type) {
508 case 0: return _("Disk");
509 case 1: return _("Print");
510 case 2: return _("Dev");
511 case 3: return _("IPC");
512 default: return _("Unknown");
516 static NTSTATUS net_scan_dc_noad(struct net_context *c,
517 struct cli_state *cli,
518 struct net_dc_info *dc_info)
520 TALLOC_CTX *mem_ctx = talloc_tos();
521 struct rpc_pipe_client *pipe_hnd = NULL;
522 struct dcerpc_binding_handle *b;
523 NTSTATUS status, result;
524 struct policy_handle pol;
525 union lsa_PolicyInformation *info;
527 ZERO_STRUCTP(dc_info);
528 ZERO_STRUCT(pol);
530 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc,
531 &pipe_hnd);
532 if (!NT_STATUS_IS_OK(status)) {
533 return status;
536 b = pipe_hnd->binding_handle;
538 status = dcerpc_lsa_open_policy(b, mem_ctx,
539 false,
540 SEC_FLAG_MAXIMUM_ALLOWED,
541 &pol,
542 &result);
543 if (!NT_STATUS_IS_OK(status)) {
544 goto done;
546 if (!NT_STATUS_IS_OK(result)) {
547 status = result;
548 goto done;
551 status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
552 &pol,
553 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
554 &info,
555 &result);
556 if (!NT_STATUS_IS_OK(status)) {
557 goto done;
559 if (!NT_STATUS_IS_OK(result)) {
560 status = result;
561 goto done;
564 dc_info->netbios_domain_name = talloc_strdup(mem_ctx, info->account_domain.name.string);
565 if (dc_info->netbios_domain_name == NULL) {
566 status = NT_STATUS_NO_MEMORY;
567 goto done;
570 done:
571 if (is_valid_policy_hnd(&pol)) {
572 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
575 TALLOC_FREE(pipe_hnd);
577 return status;
580 NTSTATUS net_scan_dc(struct net_context *c,
581 struct cli_state *cli,
582 struct net_dc_info *dc_info)
584 TALLOC_CTX *mem_ctx = talloc_tos();
585 struct rpc_pipe_client *dssetup_pipe = NULL;
586 struct dcerpc_binding_handle *dssetup_handle = NULL;
587 union dssetup_DsRoleInfo info;
588 NTSTATUS status;
589 WERROR werr;
591 ZERO_STRUCTP(dc_info);
593 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_dssetup,
594 &dssetup_pipe);
595 if (!NT_STATUS_IS_OK(status)) {
596 DEBUG(10,("net_scan_dc: failed to open dssetup pipe with %s, "
597 "retrying with lsa pipe\n", nt_errstr(status)));
598 return net_scan_dc_noad(c, cli, dc_info);
600 dssetup_handle = dssetup_pipe->binding_handle;
602 status = dcerpc_dssetup_DsRoleGetPrimaryDomainInformation(dssetup_handle, mem_ctx,
603 DS_ROLE_BASIC_INFORMATION,
604 &info,
605 &werr);
606 TALLOC_FREE(dssetup_pipe);
608 if (NT_STATUS_IS_OK(status)) {
609 status = werror_to_ntstatus(werr);
611 if (!NT_STATUS_IS_OK(status)) {
612 return status;
615 dc_info->is_dc = (info.basic.role & (DS_ROLE_PRIMARY_DC|DS_ROLE_BACKUP_DC));
616 dc_info->is_pdc = (info.basic.role & DS_ROLE_PRIMARY_DC);
617 dc_info->is_ad = (info.basic.flags & DS_ROLE_PRIMARY_DS_RUNNING);
618 dc_info->is_mixed_mode = (info.basic.flags & DS_ROLE_PRIMARY_DS_MIXED_MODE);
619 dc_info->netbios_domain_name = talloc_strdup(mem_ctx, info.basic.domain);
620 dc_info->dns_domain_name = talloc_strdup(mem_ctx, info.basic.dns_domain);
621 dc_info->forest_name = talloc_strdup(mem_ctx, info.basic.forest);
623 return NT_STATUS_OK;