2 Unix SMB/CIFS implementation.
3 Network neighbourhood browser.
5 Copyright (C) Tim Potter 2000
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 static BOOL use_bcast;
26 struct user_auth_info {
32 /* How low can we go? */
34 enum tree_level {LEV_WORKGROUP, LEV_SERVER, LEV_SHARE};
35 static enum tree_level level = LEV_SHARE;
37 static void usage(void)
40 "Usage: smbtree [options]\n\
42 \t-d debuglevel set debug output level\n\
43 \t-U username user to autheticate as\n\
44 \t-W workgroup workgroup of user to authenticate as\n\
45 \t-D list only domains (workgroups) of tree\n\
46 \t-S list domains and servers of tree\n\
47 \t-b use bcast instead of using the master browser\n\
49 The username can be of the form username%%password or\n\
50 workgroup\\username%%password.\n\n\
54 /* Holds a list of workgroups or servers */
57 struct name_list *prev, *next;
58 pstring name, comment;
62 static struct name_list *workgroups, *servers, *shares;
64 static void free_name_list(struct name_list *list)
67 DLIST_REMOVE(list, list);
70 static void add_name(const char *machine_name, uint32 server_type,
71 const char *comment, void *state)
73 struct name_list **name_list = (struct name_list **)state;
74 struct name_list *new_name;
76 new_name = (struct name_list *)malloc(sizeof(struct name_list));
81 ZERO_STRUCTP(new_name);
83 pstrcpy(new_name->name, machine_name);
84 pstrcpy(new_name->comment, comment);
85 new_name->server_type = server_type;
87 DLIST_ADD(*name_list, new_name);
90 /* Return a cli_state pointing at the IPC$ share for the given server */
92 static struct cli_state *get_ipc_connect(char *server, struct in_addr *server_ip,
93 struct user_auth_info *user_info)
95 struct cli_state *cli;
99 myname = get_myname();
101 nt_status = cli_full_connection(&cli, myname, server, server_ip, 0, "IPC$", "IPC",
102 user_info->username, lp_workgroup(), user_info->password,
103 CLI_FULL_CONNECTION_ANNONYMOUS_FALLBACK, NULL);
106 if (NT_STATUS_IS_OK(nt_status)) {
113 /* Return the IP address and workgroup of a master browser on the
116 static BOOL find_master_ip_bcast(pstring workgroup, struct in_addr *server_ip)
118 struct in_addr *ip_list;
121 /* Go looking for workgroups by broadcasting on the local network */
123 if (!name_resolve_bcast(MSBROWSE, 1, &ip_list, &count)) {
127 for (i = 0; i < count; i++) {
130 if (!name_status_find("*", 0, 0x1d, ip_list[i], name))
133 if (!find_master_ip(name, server_ip))
136 pstrcpy(workgroup, name);
138 DEBUG(4, ("found master browser %s, %s\n",
139 name, inet_ntoa(ip_list[i])));
147 /****************************************************************************
148 display tree of smb workgroups, servers and shares
149 ****************************************************************************/
150 static BOOL get_workgroups(struct user_auth_info *user_info)
152 struct cli_state *cli;
153 struct in_addr server_ip;
154 pstring master_workgroup;
156 /* Try to connect to a #1d name of our current workgroup. If that
157 doesn't work broadcast for a master browser and then jump off
160 pstrcpy(master_workgroup, lp_workgroup());
162 if (use_bcast || !find_master_ip(lp_workgroup(), &server_ip)) {
163 DEBUG(4, ("Unable to find master browser for workgroup %s\n",
165 if (!find_master_ip_bcast(master_workgroup, &server_ip)) {
166 DEBUG(4, ("Unable to find master browser by "
172 if (!(cli = get_ipc_connect(inet_ntoa(server_ip), &server_ip, user_info)))
175 if (!cli_NetServerEnum(cli, master_workgroup,
176 SV_TYPE_DOMAIN_ENUM, add_name, &workgroups))
182 /* Retrieve the list of servers for a given workgroup */
184 static BOOL get_servers(char *workgroup, struct user_auth_info *user_info)
186 struct cli_state *cli;
187 struct in_addr server_ip;
189 /* Open an IPC$ connection to the master browser for the workgroup */
191 if (!find_master_ip(workgroup, &server_ip)) {
192 DEBUG(4, ("Cannot find master browser for workgroup %s\n",
197 if (!(cli = get_ipc_connect(inet_ntoa(server_ip), &server_ip, user_info)))
200 if (!cli_NetServerEnum(cli, workgroup, SV_TYPE_ALL, add_name,
207 static BOOL get_shares(char *server_name, struct user_auth_info *user_info)
209 struct cli_state *cli;
211 if (!(cli = get_ipc_connect(server_name, NULL, user_info)))
214 if (!cli_RNetShareEnum(cli, add_name, &shares))
220 static BOOL print_tree(struct user_auth_info *user_info)
222 struct name_list *wg, *sv, *sh;
224 /* List workgroups */
226 if (!get_workgroups(user_info))
229 for (wg = workgroups; wg; wg = wg->next) {
231 printf("%s\n", wg->name);
235 free_name_list(servers);
238 if (level == LEV_WORKGROUP ||
239 !get_servers(wg->name, user_info))
242 for (sv = servers; sv; sv = sv->next) {
244 printf("\t\\\\%-15s\t\t%s\n",
245 sv->name, sv->comment);
249 free_name_list(shares);
252 if (level == LEV_SERVER ||
253 !get_shares(sv->name, user_info))
256 for (sh = shares; sh; sh = sh->next) {
257 printf("\t\t\\\\%s\\%-15s\t%s\n",
258 sv->name, sh->name, sh->comment);
266 /****************************************************************************
268 ****************************************************************************/
269 int main(int argc,char *argv[])
275 struct user_auth_info user_info;
276 BOOL got_pass = False;
278 /* Initialise samba stuff */
284 setup_logging(argv[0],True);
286 lp_load(dyn_CONFIGFILE,True,False,False);
289 if (getenv("USER")) {
290 pstrcpy(user_info.username, getenv("USER"));
292 if ((p=strchr(user_info.username, '%'))) {
294 pstrcpy(user_info.password, p+1);
296 memset(strchr(getenv("USER"), '%') + 1, 'X',
297 strlen(user_info.password));
301 pstrcpy(user_info.workgroup, lp_workgroup());
303 /* Parse command line args */
305 while ((opt = getopt(argc, argv, "U:hd:W:DSb")) != EOF) {
308 pstrcpy(user_info.username,optarg);
309 p = strchr(user_info.username,'%');
312 pstrcpy(user_info.password, p+1);
326 DEBUGLEVEL = atoi(optarg);
330 pstrcpy(user_info.workgroup, optarg);
334 level = LEV_WORKGROUP;
342 printf("Unknown option %c (%d)\n", (char)opt, opt);
356 char *pass = getpass("Password: ");
358 pstrcpy(user_info.password, pass);
363 /* Now do our stuff */
365 if (!print_tree(&user_info))