s3: Remove unused "retry" from cli_full_connection
[nivanova/samba-autobuild/.git] / source3 / utils / net_util.c
index be00dde16efb4e4769d806b663e607fb8f8f45aa..2f031c412386f9d2b777f4d652aca481c410d800 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"
+#include "../libcli/security/security.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 +45,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;
        }
@@ -94,33 +100,46 @@ NTSTATUS connect_to_service(struct net_context *c,
                                        const char *service_type)
 {
        NTSTATUS nt_status;
+       int flags = 0;
 
        c->opt_password = net_prompt_pass(c, c->opt_user_name);
-       if (!c->opt_password) {
-               return NT_STATUS_NO_MEMORY;
+
+       if (c->opt_kerberos) {
+               flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
+       }
+
+       if (c->opt_kerberos && c->opt_password) {
+               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, 0, Undefined, NULL);
+                                       c->opt_password, flags, Undefined);
        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;
        }
 
@@ -131,20 +150,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));
                }
 
@@ -185,7 +204,7 @@ NTSTATUS connect_to_ipc_anonymous(struct net_context *c,
                                        server_name, server_ss, c->opt_port,
                                        "IPC$", "IPC",
                                        "", "",
-                                       "", 0, Undefined, NULL);
+                                       "", 0, Undefined);
 
        if (NT_STATUS_IS_OK(nt_status)) {
                return nt_status;
@@ -245,7 +264,7 @@ NTSTATUS connect_to_ipc_krb5(struct net_context *c,
                                        user_and_realm, c->opt_workgroup,
                                        c->opt_password,
                                        CLI_FULL_CONNECTION_USE_KERBEROS,
-                                       Undefined, NULL);
+                                       Undefined);
 
        SAFE_FREE(user_and_realm);
 
@@ -330,7 +349,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);
        }
 
@@ -352,7 +371,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);
        }
 
@@ -386,7 +405,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;
                }
@@ -399,7 +418,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;
                }
 
@@ -413,8 +432,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;
                }
@@ -424,7 +443,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;
@@ -456,7 +475,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;
        }
 
@@ -470,7 +489,7 @@ bool net_find_pdc(struct sockaddr_storage *server_ss,
 NTSTATUS net_make_ipc_connection(struct net_context *c, unsigned flags,
                                 struct cli_state **pcli)
 {
-       return net_make_ipc_connection_ex(c, NULL, NULL, NULL, flags, pcli);
+       return net_make_ipc_connection_ex(c, c->opt_workgroup, NULL, NULL, flags, pcli);
 }
 
 NTSTATUS net_make_ipc_connection_ex(struct net_context *c ,const char *domain,
@@ -486,7 +505,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\n");
+                       d_fprintf(stderr, _("Unable to find a suitable server "
+                               "for domain %s\n"), domain);
                        nt_status = NT_STATUS_UNSUCCESSFUL;
                        goto done;
                }
@@ -510,9 +530,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:
@@ -538,8 +560,11 @@ const char *net_prompt_pass(struct net_context *c, const char *user)
                return NULL;
        }
 
-       asprintf(&prompt, "Enter %s's password:", user);
-       if (!prompt) {
+       if (c->opt_kerberos && !c->opt_user_specified) {
+               return NULL;
+       }
+
+       if (asprintf(&prompt, _("Enter %s's password:"), user) == -1) {
                return NULL;
        }
 
@@ -562,16 +587,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;
@@ -581,6 +606,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;
+}