#include "includes.h"
static BOOL use_bcast;
enum tree_level {LEV_WORKGROUP, LEV_SERVER, LEV_SHARE};
static enum tree_level level = LEV_SHARE;
struct name_list {
struct name_list *prev, *next;
pstring name, comment;
uint32 server_type;
};
static struct name_list *workgroups, *servers, *shares;
static void free_name_list(struct name_list *list)
{
while(list)
DLIST_REMOVE(list, list);
}
static void add_name(const char *machine_name, uint32 server_type,
const char *comment, void *state)
{
struct name_list **name_list = (struct name_list **)state;
struct name_list *new_name;
new_name = (struct name_list *)malloc(sizeof(struct name_list));
if (!new_name)
return;
ZERO_STRUCTP(new_name);
pstrcpy(new_name->name, machine_name);
pstrcpy(new_name->comment, comment);
new_name->server_type = server_type;
DLIST_ADD(*name_list, new_name);
}
static BOOL get_workgroups(struct user_auth_info *user_info)
{
struct cli_state *cli;
struct in_addr server_ip;
pstring master_workgroup;
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, falling back to broadcast\n",
master_workgroup));
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_NetServerEnum(cli, master_workgroup,
SV_TYPE_DOMAIN_ENUM, add_name, &workgroups))
return False;
return True;
}
static BOOL get_servers(char *workgroup, struct user_auth_info *user_info)
{
struct cli_state *cli;
struct in_addr server_ip;
if (!find_master_ip(workgroup, &server_ip)) {
DEBUG(4, ("Cannot find master browser for workgroup %s\n",
workgroup));
return False;
}
if (!(cli = get_ipc_connect(inet_ntoa(server_ip), &server_ip, user_info)))
return False;
if (!cli_NetServerEnum(cli, workgroup, SV_TYPE_ALL, add_name,
&servers))
return False;
return True;
}
static BOOL get_shares(char *server_name, struct user_auth_info *user_info)
{
struct cli_state *cli;
if (!(cli = get_ipc_connect(server_name, NULL, user_info)))
return False;
if (!cli_RNetShareEnum(cli, add_name, &shares))
return False;
return True;
}
static BOOL print_tree(struct user_auth_info *user_info)
{
struct name_list *wg, *sv, *sh;
if (!get_workgroups(user_info))
return False;
for (wg = workgroups; wg; wg = wg->next) {
printf("%s\n", wg->name);
free_name_list(servers);
servers = NULL;
if (level == LEV_WORKGROUP ||
!get_servers(wg->name, user_info))
continue;
for (sv = servers; sv; sv = sv->next) {
printf("\t\\\\%-15s\t\t%s\n",
sv->name, sv->comment);
free_name_list(shares);
shares = NULL;
if (level == LEV_SERVER ||
!get_shares(sv->name, user_info))
continue;
for (sh = shares; sh; sh = sh->next) {
printf("\t\t\\\\%s\\%-15s\t%s\n",
sv->name, sh->name, sh->comment);
}
}
}
return True;
}
int main(int argc,char *argv[])
{
struct poptOption long_options[] = {
POPT_AUTOHELP
{ "broadcast", 'b', POPT_ARG_VAL, &use_bcast, True, "Use broadcast instead of using the master browser" },
{ "domains", 'D', POPT_ARG_VAL, &level, LEV_WORKGROUP, "List only domains (workgroups) of tree" },
{ "servers", 'S', POPT_ARG_VAL, &level, LEV_SERVER, "List domains(workgroups) and servers of tree" },
POPT_COMMON_SAMBA
POPT_COMMON_CREDENTIALS
POPT_TABLEEND
};
poptContext pc;
setlinebuf(stdout);
dbf = x_stderr;
setup_logging(argv[0],True);
pc = poptGetContext("smbtree", argc, (const char **)argv, long_options,
POPT_CONTEXT_KEEP_FIRST);
while(poptGetNextOpt(pc) != -1);
poptFreeContext(pc);
lp_load(dyn_CONFIGFILE,True,False,False);
load_interfaces();
if (!cmdline_auth_info.got_pass) {
char *pass = getpass("Password: ");
if (pass) {
pstrcpy(cmdline_auth_info.password, pass);
}
cmdline_auth_info.got_pass = True;
}
if (!print_tree(&cmdline_auth_info))
return 1;
return 0;
}