s3: client tools: Call popt_free_cmdline_auth_info() on all normal exits.
[bbaumbach/samba-autobuild/.git] / source3 / utils / smbtree.c
index c2b364d1e9ac80250dbb3d60ab97bbf36f361933..fb0c130d350f5638b72b8d8556863944148c8e9e 100644 (file)
 */
 
 #include "includes.h"
+#include "popt_common.h"
+#include "rpc_client/cli_pipe.h"
+#include "../librpc/gen_ndr/ndr_srvsvc_c.h"
+#include "libsmb/libsmb.h"
+#include "libsmb/clirap.h"
 
 static int use_bcast;
 
@@ -30,27 +35,27 @@ static enum tree_level level = LEV_SHARE;
 
 /* Holds a list of workgroups or servers */
 
-struct name_list {
-        struct name_list *prev, *next;
+struct smb_name_list {
+        struct smb_name_list *prev, *next;
         char *name, *comment;
-        uint32 server_type;
+        uint32_t server_type;
 };
 
-static struct name_list *workgroups, *servers, *shares;
+static struct smb_name_list *workgroups, *servers, *shares;
 
-static void free_name_list(struct name_list *list)
+static void free_name_list(struct smb_name_list *list)
 {
         while(list)
                 DLIST_REMOVE(list, list);
 }
 
-static void add_name(const char *machine_name, uint32 server_type,
+static void add_name(const char *machine_name, uint32_t server_type,
                      const char *comment, void *state)
 {
-        struct name_list **name_list = (struct name_list **)state;
-        struct name_list *new_name;
+        struct smb_name_list **name_list = (struct smb_name_list **)state;
+        struct smb_name_list *new_name;
 
-        new_name = SMB_MALLOC_P(struct name_list);
+        new_name = SMB_MALLOC_P(struct smb_name_list);
 
         if (!new_name)
                 return;
@@ -74,7 +79,7 @@ static void add_name(const char *machine_name, uint32 server_type,
 /****************************************************************************
   display tree of smb workgroups, servers and shares
 ****************************************************************************/
-static bool get_workgroups(struct user_auth_info *user_info)
+static bool get_workgroups(const struct user_auth_info *user_info)
 {
         struct cli_state *cli;
         struct sockaddr_storage server_ss;
@@ -90,24 +95,32 @@ static bool get_workgroups(struct user_auth_info *user_info)
                return false;
        }
 
-        if (!use_bcast && !find_master_ip(lp_workgroup(), &server_ss)) {
-                DEBUG(4, ("Unable to find master browser for workgroup %s, falling back to broadcast\n", 
-                         master_workgroup));
-                               use_bcast = True;
-               } else if(!use_bcast) {
-                       char addr[INET6_ADDRSTRLEN];
-                       print_sockaddr(addr, sizeof(addr), &server_ss);
-                       if (!(cli = get_ipc_connect(addr, &server_ss, user_info)))
-                               return False;
-               }
+       if (!use_bcast && !find_master_ip(lp_workgroup(), &server_ss)) {
+               DEBUG(4,("Unable to find master browser for workgroup %s, "
+                        "falling back to broadcast\n",
+                        master_workgroup));
+               use_bcast = true;
+       }
+
+       if (!use_bcast) {
+               char addr[INET6_ADDRSTRLEN];
+
+               print_sockaddr(addr, sizeof(addr), &server_ss);
 
-               if (!(cli = get_ipc_connect_master_ip_bcast(talloc_tos(),
-                                                       user_info,
-                                                       &master_workgroup))) {
+               cli = get_ipc_connect(addr, &server_ss, user_info);
+               if (cli == NULL) {
+                       return false;
+               }
+       } else {
+               cli = get_ipc_connect_master_ip_bcast(talloc_tos(),
+                                                     user_info,
+                                                     &master_workgroup);
+               if (cli == NULL) {
                        DEBUG(4, ("Unable to find master browser by "
                                  "broadcast\n"));
-                       return False;
-        }
+                       return false;
+               }
+       }
 
         if (!cli_NetServerEnum(cli, master_workgroup,
                                SV_TYPE_DOMAIN_ENUM, add_name, &workgroups))
@@ -118,7 +131,7 @@ static bool get_workgroups(struct user_auth_info *user_info)
 
 /* Retrieve the list of servers for a given workgroup */
 
-static bool get_servers(char *workgroup, struct user_auth_info *user_info)
+static bool get_servers(char *workgroup, const struct user_auth_info *user_info)
 {
         struct cli_state *cli;
         struct sockaddr_storage server_ss;
@@ -144,11 +157,11 @@ static bool get_servers(char *workgroup, struct user_auth_info *user_info)
 }
 
 static bool get_rpc_shares(struct cli_state *cli,
-                          void (*fn)(const char *, uint32, const char *, void *),
+                          void (*fn)(const char *, uint32_t, const char *, void *),
                           void *state)
 {
        NTSTATUS status;
-       struct rpc_pipe_client *pipe_hnd;
+       struct rpc_pipe_client *pipe_hnd = NULL;
        TALLOC_CTX *mem_ctx;
        WERROR werr;
        struct srvsvc_NetShareInfoCtr info_ctr;
@@ -156,6 +169,7 @@ static bool get_rpc_shares(struct cli_state *cli,
        int i;
        uint32_t resume_handle = 0;
        uint32_t total_entries = 0;
+       struct dcerpc_binding_handle *b;
 
        mem_ctx = talloc_new(NULL);
        if (mem_ctx == NULL) {
@@ -163,22 +177,25 @@ static bool get_rpc_shares(struct cli_state *cli,
                return False;
        }
 
-       pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SRVSVC, &status);
+       status = cli_rpc_pipe_open_noauth(cli, &ndr_table_srvsvc,
+                                         &pipe_hnd);
 
-       if (pipe_hnd == NULL) {
+       if (!NT_STATUS_IS_OK(status)) {
                DEBUG(10, ("Could not connect to srvsvc pipe: %s\n",
                           nt_errstr(status)));
                TALLOC_FREE(mem_ctx);
                return False;
        }
 
+       b = pipe_hnd->binding_handle;
+
        ZERO_STRUCT(info_ctr);
        ZERO_STRUCT(ctr1);
 
        info_ctr.level = 1;
        info_ctr.ctr.ctr1 = &ctr1;
 
-       status = rpccli_srvsvc_NetShareEnumAll(pipe_hnd, mem_ctx,
+       status = dcerpc_srvsvc_NetShareEnumAll(b, mem_ctx,
                                               pipe_hnd->desthost,
                                               &info_ctr,
                                               0xffffffff,
@@ -192,7 +209,7 @@ static bool get_rpc_shares(struct cli_state *cli,
                return False;
        }
 
-       for (i=0; i<total_entries; i++) {
+       for (i=0; i < info_ctr.ctr.ctr1->count; i++) {
                struct srvsvc_NetShareInfo1 info = info_ctr.ctr.ctr1->array[i];
                fn(info.name, info.type, info.comment, state);
        }
@@ -203,7 +220,7 @@ static bool get_rpc_shares(struct cli_state *cli,
 }
 
 
-static bool get_shares(char *server_name, struct user_auth_info *user_info)
+static bool get_shares(char *server_name, const struct user_auth_info *user_info)
 {
         struct cli_state *cli;
 
@@ -219,9 +236,9 @@ static bool get_shares(char *server_name, struct user_auth_info *user_info)
         return True;
 }
 
-static bool print_tree(struct user_auth_info *user_info)
+static bool print_tree(const struct user_auth_info *user_info)
 {
-        struct name_list *wg, *sv, *sh;
+        struct smb_name_list *wg, *sv, *sh;
 
         /* List workgroups */
 
@@ -268,10 +285,10 @@ static bool print_tree(struct user_auth_info *user_info)
 /****************************************************************************
   main program
 ****************************************************************************/
- int main(int argc,char *argv[])
+int main(int argc, char *argv[])
 {
        TALLOC_CTX *frame = talloc_stackframe();
-       struct user_auth_info local_auth_info;
+       const char **argv_const = discard_const_p(const char *, argv);
        struct poptOption long_options[] = {
                POPT_AUTOHELP
                { "broadcast", 'b', POPT_ARG_VAL, &use_bcast, True, "Use broadcast instead of using the master browser" },
@@ -284,42 +301,28 @@ static bool print_tree(struct user_auth_info *user_info)
        poptContext pc;
 
        /* Initialise samba stuff */
-       load_case_tables();
+       smb_init_locale();
 
        setlinebuf(stdout);
 
-       dbf = x_stderr;
+       setup_logging(argv[0], DEBUG_STDERR);
 
-       setup_logging(argv[0],True);
+       popt_common_credentials_set_ignore_missing_conf();
 
-       pc = poptGetContext("smbtree", argc, (const char **)argv, long_options,
-                                               POPT_CONTEXT_KEEP_FIRST);
+       pc = poptGetContext("smbtree", argc, argv_const, long_options,
+                           POPT_CONTEXT_KEEP_FIRST);
        while(poptGetNextOpt(pc) != -1);
        poptFreeContext(pc);
-
-       lp_load(get_dyn_CONFIGFILE(),True,False,False,True);
-       load_interfaces();
-
-       /* Parse command line args */
-
-       if (!get_cmdline_auth_info_got_pass()) {
-               char *pass = getpass("Password: ");
-               if (pass) {
-                       set_cmdline_auth_info_password(pass);
-               }
-       }
+       popt_burn_cmdline_password(argc, argv);
 
        /* Now do our stuff */
 
-       if (!get_cmdline_auth_info_copy(&local_auth_info)) {
-               return 1;
-       }
-
-        if (!print_tree(&local_auth_info)) {
+        if (!print_tree(popt_get_cmdline_auth_info())) {
                TALLOC_FREE(frame);
                 return 1;
        }
 
+       popt_free_cmdline_auth_info();
        TALLOC_FREE(frame);
        return 0;
 }