summaryrefslogtreecommitdiff
path: root/source3/utils/smbtree.c
blob: 41940d9a6cad0f0059d1f0ad2588289b71417b76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/* 
   Unix SMB/Netbios implementation.
   Network neighbourhood browser.
   Version 3.0
   
   Copyright (C) Tim Potter      2000
   
   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
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   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.
*/

#include "includes.h"

static BOOL use_bcast;

struct user_auth_info {
	pstring username;
	pstring password;
	pstring workgroup;
};

/* How low can we go? */

enum tree_level {LEV_WORKGROUP, LEV_SERVER, LEV_SHARE};
enum tree_level level = LEV_SHARE;

static void usage(void)
{
	printf(
"Usage: smbtree [options]\n\
\n\
\t-d debuglevel           set debug output level\n\
\t-U username             user to autheticate as\n\
\t-W workgroup            workgroup of user to authenticate as\n\
\t-D                      list only domains (workgroups) of tree\n\
\t-S                      list domains and servers of tree\n\
\t-b                      use bcast instead of using the master browser\n\
\n\
The username can be of the form username%%password or\n\
workgroup\\username%%password.\n\n\
");
}

/* Holds a list of workgroups or servers */

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);
}

/* Return a cli_state pointing at the IPC$ share for the given workgroup */

static struct cli_state *get_ipc_connect(char *server,
                                         struct user_auth_info *user_info)
{
        struct nmb_name calling, called;
        extern struct in_addr ipzero;
        struct in_addr server_ip = ipzero;
        struct cli_state *cli;
        pstring myname;

        get_myname(myname);

        make_nmb_name(&called, myname, 0x0);
        make_nmb_name(&calling, server, 0x20);

        if (is_ipaddress(server))
                if (!resolve_name(server, &server_ip, 0x20))
                        return False;
                
 again:
	if (!(cli = cli_initialise(NULL))) {
                DEBUG(4, ("Unable to initialise cli structure\n"));
                goto error;
        }

        if (!cli_connect(cli, server, &server_ip)) {
                DEBUG(4, ("Unable to connect to %s\n", server));
                goto error;
        }

        if (!cli_session_request(cli, &calling, &called)) {
                cli_shutdown(cli);
                if (!strequal(called.name, "*SMBSERVER")) {
                        make_nmb_name(&called , "*SMBSERVER", 0x20);
                        goto again;
                }
                DEBUG(4, ("Session request failed to %s\n", called.name));
                goto error;
	}

        if (!cli_negprot(cli)) {
                DEBUG(4, ("Negprot failed\n"));
                goto error;
	}

	if (!cli_session_setup(cli, user_info->username, user_info->password, 
                               strlen(user_info->password),
			       user_info->password, 
                               strlen(user_info->password), server) &&
	    /* try an anonymous login if it failed */
	    !cli_session_setup(cli, "", "", 1,"", 0, server)) {
                DEBUG(4, ("Session setup failed\n"));
                goto error;
	}

	DEBUG(4,(" session setup ok\n"));

	if (!cli_send_tconX(cli, "IPC$", "?????",
			    user_info->password, 
                            strlen(user_info->password)+1)) {
                DEBUG(4, ("Tconx failed\n"));
                goto error;
	}

        return cli;

        /* Clean up after error */

 error:
        if (cli && cli->initialised)
                cli_shutdown(cli);

        free(cli);
        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(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
****************************************************************************/
static BOOL get_workgroups(struct user_auth_info *user_info)
{
        struct cli_state *cli;
        struct in_addr server_ip;
	pstring master_workgroup;

        /* Try to connect to a #1d name of our current workgroup.  If that
           doesn't work broadcast for a master browser and then jump off
           that 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\n", 
			  master_workgroup));
		if (!find_master_ip_bcast(master_workgroup, &server_ip)) {
			DEBUG(4, ("Unable to find master browser by "
				  "broadcast\n"));
			return False;
		}
        }

        if (!(cli = get_ipc_connect(inet_ntoa(server_ip), user_info)))
                return False;

        if (!cli_NetServerEnum(cli, master_workgroup, 
                               SV_TYPE_DOMAIN_ENUM, add_name, &workgroups))
                return False;

        return True;
}

/* Retrieve the list of servers for a given workgroup */

static BOOL get_servers(char *workgroup, struct user_auth_info *user_info)
{
        struct cli_state *cli;
        struct in_addr server_ip;

        /* Open an IPC$ connection to the master browser for the workgroup */

        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), 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, 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;

        /* List workgroups */

        if (!get_workgroups(user_info))
                return False;

        for (wg = workgroups; wg; wg = wg->next) {

                printf("%s\n", wg->name);

                /* List servers */

                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);

                        /* List shares */

                        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;
}

/****************************************************************************
  main program
****************************************************************************/
 int main(int argc,char *argv[])
{
	extern char *optarg;
	extern int optind;
	extern FILE *dbf;
	int opt;
	char *p;
	pstring servicesf = CONFIGFILE;
	struct user_auth_info user_info;
	BOOL got_pass = False;

	/* Initialise samba stuff */

	setlinebuf(stdout);

	dbf = stderr;

	setup_logging(argv[0],True);

	TimeInit();

	lp_load(servicesf,True,False,False);
	load_interfaces();

	if (getenv("USER")) {
		pstrcpy(user_info.username, getenv("USER"));

		if ((p=strchr(user_info.username, '%'))) {
			*p = 0;
			pstrcpy(user_info.password, p+1);
			got_pass = True;
			memset(strchr(getenv("USER"), '%') + 1, 'X',
			       strlen(user_info.password));
		}
	}

        pstrcpy(user_info.workgroup, lp_workgroup());

	/* Parse command line args */

	while ((opt = getopt(argc, argv, "U:hd:W:DSb")) != EOF) {
		switch (opt) {
		case 'U':
			pstrcpy(user_info.username,optarg);
			p = strchr(user_info.username,'%');
			if (p) {
				*p = 0;
				pstrcpy(user_info.password, p+1);
				got_pass = 1;
			}
			break;

		case 'b':
			use_bcast = True;
			break;

		case 'h':
			usage();
			exit(1);

		case 'd':
			DEBUGLEVEL = atoi(optarg);
			break;

		case 'W':
			pstrcpy(user_info.workgroup, optarg);
			break;

                case 'D':
                        level = LEV_WORKGROUP;
                        break;

                case 'S':
                        level = LEV_SERVER;
                        break;

		default:
			printf("Unknown option %c (%d)\n", (char)opt, opt);
			exit(1);
		}
	}

	argc -= optind;
	argv += optind;
	
	if (argc > 0) {
		usage();
		exit(1);
	}

	if (!got_pass) {
		char *pass = getpass("Password: ");
		if (pass) {
			pstrcpy(user_info.password, pass);
		}
                got_pass = True;
	}

	/* Now do our stuff */

        if (!print_tree(&user_info))
                return 1;

	return 0;
}