Minor comment updates ...
[Samba/gebeck_regimport.git] / source3 / utils / smbtree.c
blobcbe1bd448f8b9ed2bb240590025d83a339b578df
1 /*
2 Unix SMB/CIFS implementation.
3 Network neighbourhood browser.
5 Copyright (C) Tim Potter 2000
6 Copyright (C) Jelmer Vernooij 2003
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "includes.h"
25 static BOOL use_bcast;
27 /* How low can we go? */
29 enum tree_level {LEV_WORKGROUP, LEV_SERVER, LEV_SHARE};
30 static enum tree_level level = LEV_SHARE;
32 /* Holds a list of workgroups or servers */
34 struct name_list {
35 struct name_list *prev, *next;
36 pstring name, comment;
37 uint32 server_type;
40 static struct name_list *workgroups, *servers, *shares;
42 static void free_name_list(struct name_list *list)
44 while(list)
45 DLIST_REMOVE(list, list);
48 static void add_name(const char *machine_name, uint32 server_type,
49 const char *comment, void *state)
51 struct name_list **name_list = (struct name_list **)state;
52 struct name_list *new_name;
54 new_name = (struct name_list *)malloc(sizeof(struct name_list));
56 if (!new_name)
57 return;
59 ZERO_STRUCTP(new_name);
61 pstrcpy(new_name->name, machine_name);
62 pstrcpy(new_name->comment, comment);
63 new_name->server_type = server_type;
65 DLIST_ADD(*name_list, new_name);
68 /****************************************************************************
69 display tree of smb workgroups, servers and shares
70 ****************************************************************************/
71 static BOOL get_workgroups(struct user_auth_info *user_info)
73 struct cli_state *cli;
74 struct in_addr server_ip;
75 pstring master_workgroup;
77 /* Try to connect to a #1d name of our current workgroup. If that
78 doesn't work broadcast for a master browser and then jump off
79 that workgroup. */
81 pstrcpy(master_workgroup, lp_workgroup());
83 if (!use_bcast && !find_master_ip(lp_workgroup(), &server_ip)) {
84 DEBUG(4, ("Unable to find master browser for workgroup %s, falling back to broadcast\n",
85 master_workgroup));
86 use_bcast = True;
87 } else if(!use_bcast) {
88 if (!(cli = get_ipc_connect(inet_ntoa(server_ip), &server_ip, user_info)))
89 return False;
92 if (!(cli = get_ipc_connect_master_ip_bcast(master_workgroup, user_info))) {
93 DEBUG(4, ("Unable to find master browser by "
94 "broadcast\n"));
95 return False;
98 if (!cli_NetServerEnum(cli, master_workgroup,
99 SV_TYPE_DOMAIN_ENUM, add_name, &workgroups))
100 return False;
102 return True;
105 /* Retrieve the list of servers for a given workgroup */
107 static BOOL get_servers(char *workgroup, struct user_auth_info *user_info)
109 struct cli_state *cli;
110 struct in_addr server_ip;
112 /* Open an IPC$ connection to the master browser for the workgroup */
114 if (!find_master_ip(workgroup, &server_ip)) {
115 DEBUG(4, ("Cannot find master browser for workgroup %s\n",
116 workgroup));
117 return False;
120 if (!(cli = get_ipc_connect(inet_ntoa(server_ip), &server_ip, user_info)))
121 return False;
123 if (!cli_NetServerEnum(cli, workgroup, SV_TYPE_ALL, add_name,
124 &servers))
125 return False;
127 return True;
130 static BOOL get_shares(char *server_name, struct user_auth_info *user_info)
132 struct cli_state *cli;
134 if (!(cli = get_ipc_connect(server_name, NULL, user_info)))
135 return False;
137 if (!cli_RNetShareEnum(cli, add_name, &shares))
138 return False;
140 return True;
143 static BOOL print_tree(struct user_auth_info *user_info)
145 struct name_list *wg, *sv, *sh;
147 /* List workgroups */
149 if (!get_workgroups(user_info))
150 return False;
152 for (wg = workgroups; wg; wg = wg->next) {
154 printf("%s\n", wg->name);
156 /* List servers */
158 free_name_list(servers);
159 servers = NULL;
161 if (level == LEV_WORKGROUP ||
162 !get_servers(wg->name, user_info))
163 continue;
165 for (sv = servers; sv; sv = sv->next) {
167 printf("\t\\\\%-15s\t\t%s\n",
168 sv->name, sv->comment);
170 /* List shares */
172 free_name_list(shares);
173 shares = NULL;
175 if (level == LEV_SERVER ||
176 !get_shares(sv->name, user_info))
177 continue;
179 for (sh = shares; sh; sh = sh->next) {
180 printf("\t\t\\\\%s\\%-15s\t%s\n",
181 sv->name, sh->name, sh->comment);
186 return True;
189 /****************************************************************************
190 main program
191 ****************************************************************************/
192 int main(int argc,char *argv[])
194 struct poptOption long_options[] = {
195 POPT_AUTOHELP
196 { "broadcast", 'b', POPT_ARG_VAL, &use_bcast, True, "Use broadcast instead of using the master browser" },
197 { "domains", 'D', POPT_ARG_VAL, &level, LEV_WORKGROUP, "List only domains (workgroups) of tree" },
198 { "servers", 'S', POPT_ARG_VAL, &level, LEV_SERVER, "List domains(workgroups) and servers of tree" },
199 POPT_COMMON_SAMBA
200 POPT_COMMON_CREDENTIALS
201 POPT_TABLEEND
203 poptContext pc;
205 /* Initialise samba stuff */
207 setlinebuf(stdout);
209 dbf = x_stderr;
211 setup_logging(argv[0],True);
213 pc = poptGetContext("smbtree", argc, (const char **)argv, long_options,
214 POPT_CONTEXT_KEEP_FIRST);
215 while(poptGetNextOpt(pc) != -1);
216 poptFreeContext(pc);
218 lp_load(dyn_CONFIGFILE,True,False,False);
219 load_interfaces();
221 /* Parse command line args */
223 if (!cmdline_auth_info.got_pass) {
224 char *pass = getpass("Password: ");
225 if (pass) {
226 pstrcpy(cmdline_auth_info.password, pass);
228 cmdline_auth_info.got_pass = True;
231 /* Now do our stuff */
233 if (!print_tree(&cmdline_auth_info))
234 return 1;
236 return 0;