s3-secrets: only include secrets.h when needed.
[samba.git] / source3 / utils / net_util.c
index 88850d29df125849db31ea04115f33429fcaacd6..11ef42cd61c7ac09f35bce730efd0051694b97c3 100644 (file)
@@ -2,6 +2,7 @@
  *  Unix SMB/CIFS implementation.
  *  Helper routines for net
  *  Copyright (C) Volker Lendecke 2006
+ *  Copyright (C) Kai Blin 2008
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
 
 #include "includes.h"
 #include "utils/net.h"
+#include "../librpc/gen_ndr/cli_lsa.h"
+#include "rpc_client/cli_lsarpc.h"
+#include "../librpc/gen_ndr/cli_dssetup.h"
+#include "secrets.h"
 
 NTSTATUS net_rpc_lookup_name(struct net_context *c,
                             TALLOC_CTX *mem_ctx, struct cli_state *cli,
                             const char *name, const char **ret_domain,
-                            const char **ret_name, DOM_SID *ret_sid,
+                            const char **ret_name, struct dom_sid *ret_sid,
                             enum lsa_SidType *ret_type)
 {
-       struct rpc_pipe_client *lsa_pipe;
-       POLICY_HND pol;
+       struct rpc_pipe_client *lsa_pipe = NULL;
+       struct policy_handle pol;
        NTSTATUS result = NT_STATUS_OK;
        const char **dom_names;
-       DOM_SID *sids;
+       struct dom_sid *sids;
        enum lsa_SidType *types;
 
        ZERO_STRUCT(pol);
@@ -39,15 +44,15 @@ NTSTATUS net_rpc_lookup_name(struct net_context *c,
        result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
                                          &lsa_pipe);
        if (!NT_STATUS_IS_OK(result)) {
-               d_fprintf(stderr, "Could not initialise lsa pipe\n");
+               d_fprintf(stderr, _("Could not initialise lsa pipe\n"));
                return result;
        }
 
        result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, false,
-                                       SEC_RIGHTS_MAXIMUM_ALLOWED,
+                                       SEC_FLAG_MAXIMUM_ALLOWED,
                                        &pol);
        if (!NT_STATUS_IS_OK(result)) {
-               d_fprintf(stderr, "open_policy failed: %s\n",
+               d_fprintf(stderr, "open_policy %s: %s\n", _("failed"),
                          nt_errstr(result));
                return result;
        }
@@ -106,27 +111,34 @@ NTSTATUS connect_to_service(struct net_context *c,
                flags |= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
        }
 
+       if (c->opt_ccache) {
+               flags |= CLI_FULL_CONNECTION_USE_CCACHE;
+       }
+
        nt_status = cli_full_connection(cli_ctx, NULL, server_name,
                                        server_ss, c->opt_port,
                                        service_name, service_type,
                                        c->opt_user_name, c->opt_workgroup,
                                        c->opt_password, flags, Undefined, NULL);
        if (!NT_STATUS_IS_OK(nt_status)) {
-               d_fprintf(stderr, "Could not connect to server %s\n", server_name);
+               d_fprintf(stderr, _("Could not connect to server %s\n"),
+                         server_name);
 
                /* Display a nicer message depending on the result */
 
                if (NT_STATUS_V(nt_status) ==
                    NT_STATUS_V(NT_STATUS_LOGON_FAILURE))
-                       d_fprintf(stderr, "The username or password was not correct.\n");
+                       d_fprintf(stderr,
+                                 _("The username or password was not "
+                                   "correct.\n"));
 
                if (NT_STATUS_V(nt_status) ==
                    NT_STATUS_V(NT_STATUS_ACCOUNT_LOCKED_OUT))
-                       d_fprintf(stderr, "The account was locked out.\n");
+                       d_fprintf(stderr, _("The account was locked out.\n"));
 
                if (NT_STATUS_V(nt_status) ==
                    NT_STATUS_V(NT_STATUS_ACCOUNT_DISABLED))
-                       d_fprintf(stderr, "The account was disabled.\n");
+                       d_fprintf(stderr, _("The account was disabled.\n"));
                return nt_status;
        }
 
@@ -137,20 +149,20 @@ NTSTATUS connect_to_service(struct net_context *c,
                                        c->opt_workgroup);
 
                if (NT_STATUS_EQUAL(nt_status,NT_STATUS_NOT_SUPPORTED)) {
-                       d_printf("Encryption required and "
+                       d_printf(_("Encryption required and "
                                "server that doesn't support "
-                               "UNIX extensions - failing connect\n");
+                               "UNIX extensions - failing connect\n"));
                } else if (NT_STATUS_EQUAL(nt_status,NT_STATUS_UNKNOWN_REVISION)) {
-                       d_printf("Encryption required and "
+                       d_printf(_("Encryption required and "
                                "can't get UNIX CIFS extensions "
-                               "version from server.\n");
+                               "version from server.\n"));
                } else if (NT_STATUS_EQUAL(nt_status,NT_STATUS_UNSUPPORTED_COMPRESSION)) {
-                       d_printf("Encryption required and "
+                       d_printf(_("Encryption required and "
                                "share %s doesn't support "
-                               "encryption.\n", service_name);
+                               "encryption.\n"), service_name);
                } else if (!NT_STATUS_IS_OK(nt_status)) {
-                       d_printf("Encryption required and "
-                               "setup failed with error %s.\n",
+                       d_printf(_("Encryption required and "
+                               "setup failed with error %s.\n"),
                                nt_errstr(nt_status));
                }
 
@@ -336,7 +348,7 @@ int net_use_krb_machine_account(struct net_context *c)
        char *user_name = NULL;
 
        if (!secrets_init()) {
-               d_fprintf(stderr, "ERROR: Unable to open secrets database\n");
+               d_fprintf(stderr,_("ERROR: Unable to open secrets database\n"));
                exit(1);
        }
 
@@ -358,7 +370,7 @@ int net_use_machine_account(struct net_context *c)
        char *user_name = NULL;
 
        if (!secrets_init()) {
-               d_fprintf(stderr, "ERROR: Unable to open secrets database\n");
+               d_fprintf(stderr,_("ERROR: Unable to open secrets database\n"));
                exit(1);
        }
 
@@ -392,7 +404,7 @@ bool net_find_server(struct net_context *c,
                }
        } else if (*server_name) {
                /* resolve the IP address */
-               if (!resolve_name(*server_name, server_ss, 0x20))  {
+               if (!resolve_name(*server_name, server_ss, 0x20, false))  {
                        DEBUG(1,("Unable to resolve server name\n"));
                        return false;
                }
@@ -405,7 +417,7 @@ bool net_find_server(struct net_context *c,
                        return false;
                }
 
-               if (is_zero_addr(&pdc_ss)) {
+               if (is_zero_addr((struct sockaddr *)&pdc_ss)) {
                        return false;
                }
 
@@ -419,8 +431,8 @@ bool net_find_server(struct net_context *c,
                struct sockaddr_storage msbrow_ss;
                char addr[INET6_ADDRSTRLEN];
 
-               /*  if (!resolve_name(MSBROWSE, &msbrow_ip, 1)) */
-               if (!resolve_name(d, &msbrow_ss, 0x1B))  {
+               /*  if (!resolve_name(MSBROWSE, &msbrow_ip, 1, false)) */
+               if (!resolve_name(d, &msbrow_ss, 0x1B, false))  {
                        DEBUG(1,("Unable to resolve domain browser via name lookup\n"));
                        return false;
                }
@@ -430,7 +442,7 @@ bool net_find_server(struct net_context *c,
        } else if (flags & NET_FLAGS_MASTER) {
                struct sockaddr_storage brow_ss;
                char addr[INET6_ADDRSTRLEN];
-               if (!resolve_name(d, &brow_ss, 0x1D))  {
+               if (!resolve_name(d, &brow_ss, 0x1D, false))  {
                                /* go looking for workgroups */
                        DEBUG(1,("Unable to resolve master browser via name lookup\n"));
                        return false;
@@ -462,7 +474,7 @@ bool net_find_pdc(struct sockaddr_storage *server_ss,
        if (!get_pdc_ip(domain_name, server_ss)) {
                return false;
        }
-       if (is_zero_addr(server_ss)) {
+       if (is_zero_addr((struct sockaddr *)server_ss)) {
                return false;
        }
 
@@ -492,8 +504,8 @@ NTSTATUS net_make_ipc_connection_ex(struct net_context *c ,const char *domain,
        if ( !server || !pss ) {
                if (!net_find_server(c, domain, flags, &server_ss,
                                     &server_name)) {
-                       d_fprintf(stderr, "Unable to find a suitable server "
-                               "for domain %s\n", domain);
+                       d_fprintf(stderr, _("Unable to find a suitable server "
+                               "for domain %s\n"), domain);
                        nt_status = NT_STATUS_UNSUCCESSFUL;
                        goto done;
                }
@@ -517,9 +529,11 @@ NTSTATUS net_make_ipc_connection_ex(struct net_context *c ,const char *domain,
 
        SAFE_FREE(server_name);
        if (!NT_STATUS_IS_OK(nt_status)) {
-               d_fprintf(stderr, "Connection failed: %s\n",
+               d_fprintf(stderr, _("Connection failed: %s\n"),
                          nt_errstr(nt_status));
                cli = NULL;
+       } else if (c->opt_request_timeout) {
+               cli_set_timeout(cli, c->opt_request_timeout * 1000);
        }
 
 done:
@@ -549,8 +563,7 @@ const char *net_prompt_pass(struct net_context *c, const char *user)
                return NULL;
        }
 
-       asprintf(&prompt, "Enter %s's password:", user);
-       if (!prompt) {
+       if (asprintf(&prompt, _("Enter %s's password:"), user) == -1) {
                return NULL;
        }
 
@@ -573,16 +586,16 @@ int net_run_function(struct net_context *c, int argc, const char **argv,
        }
 
        if (c->display_usage == false) {
-               d_fprintf(stderr, "Invalid command: %s %s\n", whoami,
+               d_fprintf(stderr, _("Invalid command: %s %s\n"), whoami,
                          (argc > 0)?argv[0]:"");
        }
-       d_printf("Usage:\n");
+       d_printf(_("Usage:\n"));
        for (i=0; table[i].funcname != NULL; i++) {
                if(c->display_usage == false)
                        d_printf("%s %-15s %s\n", whoami, table[i].funcname,
-                                table[i].description);
+                                _(table[i].description));
                else
-                       d_printf("%s\n", table[i].usage);
+                       d_printf("%s\n", _(table[i].usage));
        }
 
        return c->display_usage?0:-1;
@@ -592,6 +605,55 @@ void net_display_usage_from_functable(struct functable *table)
 {
        int i;
        for (i=0; table[i].funcname != NULL; i++) {
-               d_printf("%s\n", table[i].usage);
+               d_printf("%s\n", _(table[i].usage));
        }
 }
+
+const char *net_share_type_str(int num_type)
+{
+       switch(num_type) {
+               case 0: return _("Disk");
+               case 1: return _("Print");
+               case 2: return _("Dev");
+               case 3: return _("IPC");
+               default: return _("Unknown");
+       }
+}
+
+NTSTATUS net_scan_dc(struct net_context *c,
+                    struct cli_state *cli,
+                    struct net_dc_info *dc_info)
+{
+       TALLOC_CTX *mem_ctx = talloc_tos();
+       struct rpc_pipe_client *dssetup_pipe = NULL;
+       union dssetup_DsRoleInfo info;
+       NTSTATUS status;
+
+       ZERO_STRUCTP(dc_info);
+
+       status = cli_rpc_pipe_open_noauth(cli, &ndr_table_dssetup.syntax_id,
+                                         &dssetup_pipe);
+        if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+
+       status = rpccli_dssetup_DsRoleGetPrimaryDomainInformation(dssetup_pipe, mem_ctx,
+                                                                 DS_ROLE_BASIC_INFORMATION,
+                                                                 &info,
+                                                                 NULL);
+       TALLOC_FREE(dssetup_pipe);
+
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+
+       dc_info->is_dc  = (info.basic.role & (DS_ROLE_PRIMARY_DC|DS_ROLE_BACKUP_DC));
+       dc_info->is_pdc = (info.basic.role & DS_ROLE_PRIMARY_DC);
+       dc_info->is_ad  = (info.basic.flags & DS_ROLE_PRIMARY_DS_RUNNING);
+       dc_info->is_mixed_mode = (info.basic.flags & DS_ROLE_PRIMARY_DS_MIXED_MODE);
+       dc_info->netbios_domain_name = talloc_strdup(mem_ctx, info.basic.domain);
+       dc_info->dns_domain_name = talloc_strdup(mem_ctx, info.basic.dns_domain);
+       dc_info->forest_name = talloc_strdup(mem_ctx, info.basic.forest);
+
+       return NT_STATUS_OK;
+}