s3-secrets: only include secrets.h when needed.
[samba.git] / source3 / utils / net_util.c
index 771c7e4f4646ca691b44756939fcfa07667ec8c7..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);
 
-       lsa_pipe = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &result);
-       if (lsa_pipe == NULL) {
-               d_fprintf(stderr, "Could not initialise lsa pipe\n");
+       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"));
                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;
        }
@@ -93,33 +99,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, 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;
        }
 
@@ -130,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));
                }
 
@@ -278,7 +297,8 @@ NTSTATUS connect_to_ipc_krb5(struct net_context *c,
  * @return Normal NTSTATUS return.
  **/
 NTSTATUS connect_dst_pipe(struct net_context *c, struct cli_state **cli_dst,
-                         struct rpc_pipe_client **pp_pipe_hnd, int pipe_num)
+                         struct rpc_pipe_client **pp_pipe_hnd,
+                         const struct ndr_syntax_id *interface)
 {
        NTSTATUS nt_status;
        char *server_name = SMB_STRDUP("127.0.0.1");
@@ -303,8 +323,9 @@ NTSTATUS connect_dst_pipe(struct net_context *c, struct cli_state **cli_dst,
                return nt_status;
        }
 
-       pipe_hnd = cli_rpc_pipe_open_noauth(cli_tmp, pipe_num, &nt_status);
-       if (!pipe_hnd) {
+       nt_status = cli_rpc_pipe_open_noauth(cli_tmp, interface,
+                                            &pipe_hnd);
+       if (!NT_STATUS_IS_OK(nt_status)) {
                DEBUG(0, ("couldn't not initialize pipe\n"));
                cli_shutdown(cli_tmp);
                SAFE_FREE(server_name);
@@ -327,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);
        }
 
@@ -349,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);
        }
 
@@ -383,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;
                }
@@ -396,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;
                }
 
@@ -410,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;
                }
@@ -421,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;
@@ -453,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;
        }
 
@@ -467,7 +488,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,
@@ -483,7 +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\n");
+                       d_fprintf(stderr, _("Unable to find a suitable server "
+                               "for domain %s\n"), domain);
                        nt_status = NT_STATUS_UNSUCCESSFUL;
                        goto done;
                }
@@ -507,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:
@@ -535,8 +559,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;
        }
 
@@ -546,3 +573,87 @@ const char *net_prompt_pass(struct net_context *c, const char *user)
        return pass;
 }
 
+int net_run_function(struct net_context *c, int argc, const char **argv,
+                     const char *whoami, struct functable *table)
+{
+       int i;
+
+       if (argc != 0) {
+               for (i=0; table[i].funcname != NULL; i++) {
+                       if (StrCaseCmp(argv[0], table[i].funcname) == 0)
+                               return table[i].fn(c, argc-1, argv+1);
+               }
+       }
+
+       if (c->display_usage == false) {
+               d_fprintf(stderr, _("Invalid command: %s %s\n"), whoami,
+                         (argc > 0)?argv[0]:"");
+       }
+       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));
+               else
+                       d_printf("%s\n", _(table[i].usage));
+       }
+
+       return c->display_usage?0:-1;
+}
+
+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));
+       }
+}
+
+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;
+}