r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text
[vlendec/samba-autobuild/.git] / source3 / utils / smbtree.c
index 52de5ab467a36abff8d609de2bb91f0954e8dee2..b30ce08c5a4166f82669e8536086513a69ffd0d2 100644 (file)
@@ -7,7 +7,7 @@
    
    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
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
@@ -16,8 +16,7 @@
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "includes.h"
@@ -51,7 +50,7 @@ static void add_name(const char *machine_name, uint32 server_type,
         struct name_list **name_list = (struct name_list **)state;
         struct name_list *new_name;
 
-        new_name = (struct name_list *)malloc(sizeof(struct name_list));
+        new_name = SMB_MALLOC_P(struct name_list);
 
         if (!new_name)
                 return;
@@ -65,62 +64,6 @@ static void add_name(const char *machine_name, uint32 server_type,
         DLIST_ADD(*name_list, new_name);
 }
 
-/* Return a cli_state pointing at the IPC$ share for the given server */
-
-static struct cli_state *get_ipc_connect(char *server, struct in_addr *server_ip,
-                                         struct user_auth_info *user_info)
-{
-        struct cli_state *cli;
-        pstring myname;
-       NTSTATUS nt_status;
-
-        get_myname(myname);
-       
-       nt_status = cli_full_connection(&cli, myname, server, server_ip, 0, "IPC$", "IPC", 
-                                       user_info->username, lp_workgroup(), user_info->password, 
-                                       CLI_FULL_CONNECTION_ANNONYMOUS_FALLBACK, NULL);
-
-       if (NT_STATUS_IS_OK(nt_status)) {
-               return cli;
-       } else {
-               return NULL;
-       }
-}
-
-/* Return the IP address and workgroup of a master browser on the 
-   network. */
-
-static BOOL find_master_ip_bcast(pstring workgroup, struct in_addr *server_ip)
-{
-       struct in_addr *ip_list;
-       int i, count;
-
-        /* Go looking for workgroups by broadcasting on the local network */ 
-
-        if (!name_resolve_bcast(MSBROWSE, 1, &ip_list, &count)) {
-                return False;
-        }
-
-       for (i = 0; i < count; i++) {
-               static fstring name;
-
-               if (!name_status_find("*", 0, 0x1d, ip_list[i], name))
-                       continue;
-
-                if (!find_master_ip(name, server_ip))
-                       continue;
-
-                pstrcpy(workgroup, name);
-
-                DEBUG(4, ("found master browser %s, %s\n", 
-                          name, inet_ntoa(ip_list[i])));
-
-                return True;
-       }
-
-       return False;
-}
-
 /****************************************************************************
   display tree of smb workgroups, servers and shares
 ****************************************************************************/
@@ -136,19 +79,21 @@ static BOOL get_workgroups(struct user_auth_info *user_info)
 
        pstrcpy(master_workgroup, lp_workgroup());
 
-        if (use_bcast || !find_master_ip(lp_workgroup(), &server_ip)) {
-                DEBUG(4, ("Unable to find master browser for workgroup %s\n", 
+        if (!use_bcast && !find_master_ip(lp_workgroup(), &server_ip)) {
+                DEBUG(4, ("Unable to find master browser for workgroup %s, falling back to broadcast\n", 
                          master_workgroup));
-               if (!find_master_ip_bcast(master_workgroup, &server_ip)) {
+                               use_bcast = True;
+               } else if(!use_bcast) {
+               if (!(cli = get_ipc_connect(inet_ntoa(server_ip), &server_ip, user_info)))
+                               return False;
+               }
+               
+               if (!(cli = get_ipc_connect_master_ip_bcast(master_workgroup, user_info))) {
                        DEBUG(4, ("Unable to find master browser by "
                                  "broadcast\n"));
                        return False;
-               }
         }
 
-        if (!(cli = get_ipc_connect(inet_ntoa(server_ip), &server_ip, user_info)))
-                return False;
-
         if (!cli_NetServerEnum(cli, master_workgroup, 
                                SV_TYPE_DOMAIN_ENUM, add_name, &workgroups))
                 return False;
@@ -181,6 +126,62 @@ static BOOL get_servers(char *workgroup, struct user_auth_info *user_info)
         return True;
 }
 
+static BOOL get_rpc_shares(struct cli_state *cli, 
+                          void (*fn)(const char *, uint32, const char *, void *),
+                          void *state)
+{
+       NTSTATUS status;
+       struct rpc_pipe_client *pipe_hnd;
+       TALLOC_CTX *mem_ctx;
+       uint32 enum_hnd;
+       struct srvsvc_NetShareCtr1 ctr1;
+       union srvsvc_NetShareCtr ctr;
+       uint32 numentries;
+       int i;
+       uint32 info_level = 1;
+
+       mem_ctx = talloc_new(NULL);
+       if (mem_ctx == NULL) {
+               DEBUG(0, ("talloc_new failed\n"));
+               return False;
+       }
+
+       enum_hnd = 0;
+       pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SRVSVC, &status);
+
+       if (pipe_hnd == NULL) {
+               DEBUG(10, ("Could not connect to srvsvc pipe: %s\n",
+                          nt_errstr(status)));
+               TALLOC_FREE(mem_ctx);
+               return False;
+       }
+
+       ZERO_STRUCT(ctr1);
+       level = 1;
+       ctr.ctr1 = &ctr1;
+
+       status = rpccli_srvsvc_NetShareEnum(pipe_hnd, mem_ctx, NULL,
+                                           &info_level, &ctr,
+                                           0xffffffff, &numentries,
+                                           &enum_hnd);
+
+       if (!NT_STATUS_IS_OK(status)) {
+               TALLOC_FREE(mem_ctx);
+               cli_rpc_pipe_close(pipe_hnd);
+               return False;
+       }
+
+       for (i=0; i<numentries; i++) {
+               fn(ctr.ctr1->array[i].name, ctr.ctr1->array[i].type, 
+                  ctr.ctr1->array[i].comment, state);
+       }
+
+       TALLOC_FREE(mem_ctx);
+       cli_rpc_pipe_close(pipe_hnd);
+       return True;
+}
+
+
 static BOOL get_shares(char *server_name, struct user_auth_info *user_info)
 {
         struct cli_state *cli;
@@ -188,6 +189,9 @@ static BOOL get_shares(char *server_name, struct user_auth_info *user_info)
         if (!(cli = get_ipc_connect(server_name, NULL, user_info)))
                 return False;
 
+       if (get_rpc_shares(cli, add_name, &shares))
+               return True;
+       
         if (!cli_RNetShareEnum(cli, add_name, &shares))
                 return False;
 
@@ -257,6 +261,7 @@ static BOOL print_tree(struct user_auth_info *user_info)
        poptContext pc;
        
        /* Initialise samba stuff */
+       load_case_tables();
 
        setlinebuf(stdout);
 
@@ -269,7 +274,7 @@ static BOOL print_tree(struct user_auth_info *user_info)
        while(poptGetNextOpt(pc) != -1);
        poptFreeContext(pc);
 
-       lp_load(dyn_CONFIGFILE,True,False,False);
+       lp_load(dyn_CONFIGFILE,True,False,False,True);
        load_interfaces();
 
        /* Parse command line args */