Removed version number from file header.
[samba.git] / source3 / utils / smbtree.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Network neighbourhood browser.
4    
5    Copyright (C) Tim Potter      2000
6    
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.
11    
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.
16    
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.
20 */
21
22 #include "includes.h"
23
24 static BOOL use_bcast;
25
26 struct user_auth_info {
27         pstring username;
28         pstring password;
29         pstring workgroup;
30 };
31
32 /* How low can we go? */
33
34 enum tree_level {LEV_WORKGROUP, LEV_SERVER, LEV_SHARE};
35 enum tree_level level = LEV_SHARE;
36
37 static void usage(void)
38 {
39         printf(
40 "Usage: smbtree [options]\n\
41 \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\
48 \n\
49 The username can be of the form username%%password or\n\
50 workgroup\\username%%password.\n\n\
51 ");
52 }
53
54 /* Holds a list of workgroups or servers */
55
56 struct name_list {
57         struct name_list *prev, *next;
58         pstring name, comment;
59         uint32 server_type;
60 };
61
62 static struct name_list *workgroups, *servers, *shares;
63
64 static void free_name_list(struct name_list *list)
65 {
66         while(list)
67                 DLIST_REMOVE(list, list);
68 }
69
70 static void add_name(const char *machine_name, uint32 server_type,
71                      const char *comment, void *state)
72 {
73         struct name_list **name_list = (struct name_list **)state;
74         struct name_list *new_name;
75
76         new_name = (struct name_list *)malloc(sizeof(struct name_list));
77
78         if (!new_name)
79                 return;
80
81         ZERO_STRUCTP(new_name);
82
83         pstrcpy(new_name->name, machine_name);
84         pstrcpy(new_name->comment, comment);
85         new_name->server_type = server_type;
86
87         DLIST_ADD(*name_list, new_name);
88 }
89
90 /* Return a cli_state pointing at the IPC$ share for the given workgroup */
91
92 static struct cli_state *get_ipc_connect(char *server,
93                                          struct user_auth_info *user_info)
94 {
95         struct nmb_name calling, called;
96         struct in_addr server_ip;
97         struct cli_state *cli;
98         pstring myname;
99
100         zero_ip(&server_ip);
101
102         get_myname(myname);
103
104         make_nmb_name(&called, myname, 0x0);
105         make_nmb_name(&calling, server, 0x20);
106
107         if (is_ipaddress(server))
108                 if (!resolve_name(server, &server_ip, 0x20))
109                         return False;
110                 
111  again:
112         if (!(cli = cli_initialise(NULL))) {
113                 DEBUG(4, ("Unable to initialise cli structure\n"));
114                 goto error;
115         }
116
117         if (!cli_connect(cli, server, &server_ip)) {
118                 DEBUG(4, ("Unable to connect to %s\n", server));
119                 goto error;
120         }
121
122         if (!cli_session_request(cli, &calling, &called)) {
123                 cli_shutdown(cli);
124                 if (!strequal(called.name, "*SMBSERVER")) {
125                         make_nmb_name(&called , "*SMBSERVER", 0x20);
126                         goto again;
127                 }
128                 DEBUG(4, ("Session request failed to %s\n", called.name));
129                 goto error;
130         }
131
132         if (!cli_negprot(cli)) {
133                 DEBUG(4, ("Negprot failed\n"));
134                 goto error;
135         }
136
137         if (!cli_session_setup(cli, user_info->username, user_info->password, 
138                                strlen(user_info->password),
139                                user_info->password, 
140                                strlen(user_info->password), server) &&
141             /* try an anonymous login if it failed */
142             !cli_session_setup(cli, "", "", 1,"", 0, server)) {
143                 DEBUG(4, ("Session setup failed\n"));
144                 goto error;
145         }
146
147         DEBUG(4,(" session setup ok\n"));
148
149         if (!cli_send_tconX(cli, "IPC$", "?????",
150                             user_info->password, 
151                             strlen(user_info->password)+1)) {
152                 DEBUG(4, ("Tconx failed\n"));
153                 goto error;
154         }
155
156         return cli;
157
158         /* Clean up after error */
159
160  error:
161         if (cli && cli->initialised)
162                 cli_shutdown(cli);
163
164         return NULL;
165 }
166
167 /* Return the IP address and workgroup of a master browser on the 
168    network. */
169
170 static BOOL find_master_ip_bcast(pstring workgroup, struct in_addr *server_ip)
171 {
172         struct in_addr *ip_list;
173         int i, count;
174
175         /* Go looking for workgroups by broadcasting on the local network */ 
176
177         if (!name_resolve_bcast(MSBROWSE, 1, &ip_list, &count)) {
178                 return False;
179         }
180
181         for (i = 0; i < count; i++) {
182                 static fstring name;
183
184                 if (!name_status_find("*", 0, 0x1d, ip_list[i], name))
185                         continue;
186
187                 if (!find_master_ip(name, server_ip))
188                         continue;
189
190                 pstrcpy(workgroup, name);
191
192                 DEBUG(4, ("found master browser %s, %s\n", 
193                           name, inet_ntoa(ip_list[i])));
194
195                 return True;
196         }
197
198         return False;
199 }
200
201 /****************************************************************************
202   display tree of smb workgroups, servers and shares
203 ****************************************************************************/
204 static BOOL get_workgroups(struct user_auth_info *user_info)
205 {
206         struct cli_state *cli;
207         struct in_addr server_ip;
208         pstring master_workgroup;
209
210         /* Try to connect to a #1d name of our current workgroup.  If that
211            doesn't work broadcast for a master browser and then jump off
212            that workgroup. */
213
214         pstrcpy(master_workgroup, lp_workgroup());
215
216         if (use_bcast || !find_master_ip(lp_workgroup(), &server_ip)) {
217                 DEBUG(4, ("Unable to find master browser for workgroup %s\n", 
218                           master_workgroup));
219                 if (!find_master_ip_bcast(master_workgroup, &server_ip)) {
220                         DEBUG(4, ("Unable to find master browser by "
221                                   "broadcast\n"));
222                         return False;
223                 }
224         }
225
226         if (!(cli = get_ipc_connect(inet_ntoa(server_ip), user_info)))
227                 return False;
228
229         if (!cli_NetServerEnum(cli, master_workgroup, 
230                                SV_TYPE_DOMAIN_ENUM, add_name, &workgroups))
231                 return False;
232
233         return True;
234 }
235
236 /* Retrieve the list of servers for a given workgroup */
237
238 static BOOL get_servers(char *workgroup, struct user_auth_info *user_info)
239 {
240         struct cli_state *cli;
241         struct in_addr server_ip;
242
243         /* Open an IPC$ connection to the master browser for the workgroup */
244
245         if (!find_master_ip(workgroup, &server_ip)) {
246                 DEBUG(4, ("Cannot find master browser for workgroup %s\n",
247                           workgroup));
248                 return False;
249         }
250
251         if (!(cli = get_ipc_connect(inet_ntoa(server_ip), user_info)))
252                 return False;
253
254         if (!cli_NetServerEnum(cli, workgroup, SV_TYPE_ALL, add_name, 
255                                &servers))
256                 return False;
257
258         return True;
259 }
260
261 static BOOL get_shares(char *server_name, struct user_auth_info *user_info)
262 {
263         struct cli_state *cli;
264
265         if (!(cli = get_ipc_connect(server_name, user_info)))
266                 return False;
267
268         if (!cli_RNetShareEnum(cli, add_name, &shares))
269                 return False;
270
271         return True;
272 }
273
274 static BOOL print_tree(struct user_auth_info *user_info)
275 {
276         struct name_list *wg, *sv, *sh;
277
278         /* List workgroups */
279
280         if (!get_workgroups(user_info))
281                 return False;
282
283         for (wg = workgroups; wg; wg = wg->next) {
284
285                 printf("%s\n", wg->name);
286
287                 /* List servers */
288
289                 free_name_list(servers);
290                 servers = NULL;
291
292                 if (level == LEV_WORKGROUP || 
293                     !get_servers(wg->name, user_info))
294                         continue;
295
296                 for (sv = servers; sv; sv = sv->next) {
297
298                         printf("\t\\\\%-15s\t\t%s\n", 
299                                sv->name, sv->comment);
300
301                         /* List shares */
302
303                         free_name_list(shares);
304                         shares = NULL;
305
306                         if (level == LEV_SERVER ||
307                             !get_shares(sv->name, user_info))
308                                 continue;
309
310                         for (sh = shares; sh; sh = sh->next) {
311                                 printf("\t\t\\\\%s\\%-15s\t%s\n", 
312                                        sv->name, sh->name, sh->comment);
313                         }
314                 }
315         }
316
317         return True;
318 }
319
320 /****************************************************************************
321   main program
322 ****************************************************************************/
323  int main(int argc,char *argv[])
324 {
325         extern char *optarg;
326         extern int optind;
327         int opt;
328         char *p;
329         struct user_auth_info user_info;
330         BOOL got_pass = False;
331
332         /* Initialise samba stuff */
333
334         setlinebuf(stdout);
335
336         dbf = x_stderr;
337
338         setup_logging(argv[0],True);
339
340         lp_load(dyn_CONFIGFILE,True,False,False);
341         load_interfaces();
342
343         if (getenv("USER")) {
344                 pstrcpy(user_info.username, getenv("USER"));
345
346                 if ((p=strchr(user_info.username, '%'))) {
347                         *p = 0;
348                         pstrcpy(user_info.password, p+1);
349                         got_pass = True;
350                         memset(strchr(getenv("USER"), '%') + 1, 'X',
351                                strlen(user_info.password));
352                 }
353         }
354
355         pstrcpy(user_info.workgroup, lp_workgroup());
356
357         /* Parse command line args */
358
359         while ((opt = getopt(argc, argv, "U:hd:W:DSb")) != EOF) {
360                 switch (opt) {
361                 case 'U':
362                         pstrcpy(user_info.username,optarg);
363                         p = strchr(user_info.username,'%');
364                         if (p) {
365                                 *p = 0;
366                                 pstrcpy(user_info.password, p+1);
367                                 got_pass = 1;
368                         }
369                         break;
370
371                 case 'b':
372                         use_bcast = True;
373                         break;
374
375                 case 'h':
376                         usage();
377                         exit(1);
378
379                 case 'd':
380                         DEBUGLEVEL = atoi(optarg);
381                         break;
382
383                 case 'W':
384                         pstrcpy(user_info.workgroup, optarg);
385                         break;
386
387                 case 'D':
388                         level = LEV_WORKGROUP;
389                         break;
390
391                 case 'S':
392                         level = LEV_SERVER;
393                         break;
394
395                 default:
396                         printf("Unknown option %c (%d)\n", (char)opt, opt);
397                         exit(1);
398                 }
399         }
400
401         argc -= optind;
402         argv += optind;
403         
404         if (argc > 0) {
405                 usage();
406                 exit(1);
407         }
408
409         if (!got_pass) {
410                 char *pass = getpass("Password: ");
411                 if (pass) {
412                         pstrcpy(user_info.password, pass);
413                 }
414                 got_pass = True;
415         }
416
417         /* Now do our stuff */
418
419         if (!print_tree(&user_info))
420                 return 1;
421
422         return 0;
423 }