summaryrefslogtreecommitdiffstats
path: root/source3/utils/smbtree.c
blob: 80dfa0a5944b7df9ca61741f15994c8d07d16052 (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
/*
   Unix SMB/CIFS implementation.
   Network neighbourhood browser.

   Copyright (C) Tim Potter      2000
   Copyright (C) Jelmer Vernooij 2003

   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 3 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, see <http://www.gnu.org/licenses/>.
*/

#include "includes.h"
#include "lib/cmdline/cmdline.h"
#include "rpc_client/cli_pipe.h"
#include "../librpc/gen_ndr/ndr_srvsvc_c.h"
#include "libsmb/libsmb.h"
#include "libsmb/namequery.h"
#include "libsmb/clirap.h"
#include "../libcli/smb/smbXcli_base.h"
#include "nameserv.h"
#include "libsmbclient.h"

/* How low can we go? */

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

static void get_auth_data_with_context_fn(
	SMBCCTX *context,
	const char *server,
	const char *share,
	char *domain,
	int domain_len,
	char *user,
	int user_len,
	char *password,
	int password_len)
{
	struct cli_credentials *creds = samba_cmdline_get_creds();
	size_t len;

	len = strlcpy(domain, cli_credentials_get_domain(creds), domain_len);
	if ((int)len >= domain_len) {
		return;
	}
	len = strlcpy(
		user, cli_credentials_get_username(creds), user_len);
	if ((int)len >= user_len) {
		return;
	}
	len = strlcpy(
		password, cli_credentials_get_password(creds), password_len);
	if ((int)len >= password_len) {
		/* pointless, but what can you do... */
		return;
	}
}

/****************************************************************************
  main program
****************************************************************************/
int main(int argc, char *argv[])
{
	TALLOC_CTX *frame = talloc_stackframe();
	const char **argv_const = discard_const_p(const char *, argv);
	struct poptOption long_options[] = {
		POPT_AUTOHELP
		{
			.longName   = "domains",
			.shortName  = 'D',
			.argInfo    = POPT_ARG_VAL,
			.arg        = &level,
			.val        = LEV_WORKGROUP,
			.descrip    = "List only domains (workgroups) of tree" ,
		},
		{
			.longName   = "servers",
			.shortName  = 'S',
			.argInfo    = POPT_ARG_VAL,
			.arg        = &level,
			.val        = LEV_SERVER,
			.descrip    = "List domains(workgroups) and servers of tree" ,
		},
		POPT_COMMON_SAMBA
		POPT_COMMON_CREDENTIALS
		POPT_COMMON_VERSION
		POPT_TABLEEND
	};
	poptContext pc;
	SMBCCTX *ctx = NULL;
	SMBCFILE *workgroups = NULL;
	struct smbc_dirent *dirent = NULL;
	bool ok;
	int ret, result = 1;
	int opt;
	int debuglevel;

	/* Initialise samba stuff */
	smb_init_locale();

	setlinebuf(stdout);

	ok = samba_cmdline_init(frame,
				SAMBA_CMDLINE_CONFIG_CLIENT,
				false /* require_smbconf */);
	if (!ok) {
		DBG_ERR("Failed to init cmdline parser!\n");
		TALLOC_FREE(frame);
		exit(1);
	}

	pc = samba_popt_get_context(getprogname(),
				    argc,
				    argv_const,
				    long_options,
				    POPT_CONTEXT_KEEP_FIRST);
	if (pc == NULL) {
		DBG_ERR("Failed to setup popt context!\n");
		TALLOC_FREE(frame);
		exit(1);
	}

	while((opt = poptGetNextOpt(pc)) != -1) {
		switch (opt) {
		case POPT_ERROR_BADOPT:
			fprintf(stderr, "\nInvalid option %s: %s\n\n",
				poptBadOption(pc, 0), poptStrerror(opt));
			poptPrintUsage(pc, stderr, 0);
			exit(1);
		}
	}

	samba_cmdline_burn(argc, argv);

	debuglevel = DEBUGLEVEL;

	ctx = smbc_new_context();
	if (ctx == NULL) {
		perror("smbc_new_context");
		goto fail;
	}
	ret = smbc_setConfiguration(ctx, get_dyn_CONFIGFILE());
	if (ret == -1) {
		perror("smbc_setConfiguration");
		goto fail;
	}
	smbc_setDebug(ctx, debuglevel);
	ok = smbc_setOptionProtocols(ctx, NULL, "NT1");
	if (!ok) {
		perror("smbc_setOptionProtocols");
		goto fail;
	}
	smbc_setFunctionAuthDataWithContext(
		ctx, get_auth_data_with_context_fn);

	ok = smbc_init_context(ctx);
	if (!ok) {
		perror("smbc_init_context");
		goto fail;
	}

	workgroups = smbc_getFunctionOpendir(ctx)(ctx, "smb://");
	if (workgroups == NULL) {
		DBG_ERR("This is utility doesn't work if netbios name "
			"resolution is not configured.\n"
			"If you are using SMB2 or SMB3, network browsing uses "
			"WSD/LLMNR, which is not yet supported by Samba. SMB1 "
			"is disabled by default on the latest Windows versions "
			"for security reasons. It is still possible to access "
			"the Samba resources directly via \\name or "
			"\\ip.address.\n");
		goto fail;
	}

	while ((dirent = smbc_getFunctionReaddir(ctx)(ctx, workgroups))
	       != NULL) {
		char *url = NULL;
		SMBCFILE *servers = NULL;

		if (dirent->smbc_type != SMBC_WORKGROUP) {
			continue;
		}

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

		if (level == LEV_WORKGROUP) {
			continue;
		}

		url = talloc_asprintf(
			talloc_tos(), "smb://%s/", dirent->name);
		if (url == NULL) {
			perror("talloc_asprintf");
			goto fail;
		}

		servers = smbc_getFunctionOpendir(ctx)(ctx, url);
		if (servers == NULL) {
			perror("smbc_opendir");
			goto fail;
		}
		TALLOC_FREE(url);

		while ((dirent = smbc_getFunctionReaddir(ctx)(ctx, servers))
		       != NULL) {
			SMBCFILE *shares = NULL;
			char *servername = NULL;

			if (dirent->smbc_type != SMBC_SERVER) {
				continue;
			}

			printf("\t\\\\%-15s\t\t%s\n",
			       dirent->name,
			       dirent->comment);

			if (level == LEV_SERVER) {
				continue;
			}

			/*
			 * The subsequent readdir for shares will
			 * overwrite the "server" readdir
			 */
			servername = talloc_strdup(talloc_tos(), dirent->name);
			if (servername == NULL) {
				continue;
			}

			url = talloc_asprintf(
				talloc_tos(), "smb://%s/", servername);
			if (url == NULL) {
				perror("talloc_asprintf");
				goto fail;
			}

			shares = smbc_getFunctionOpendir(ctx)(ctx, url);
			if (shares == NULL) {
				perror("smbc_opendir");
				goto fail;
			}

			while ((dirent = smbc_getFunctionReaddir(
					ctx)(ctx, shares))
			       != NULL) {
				printf("\t\t\\\\%s\\%-15s\t%s\n",
				       servername,
				       dirent->name,
				       dirent->comment);
			}

			ret = smbc_getFunctionClosedir(ctx)(ctx, shares);
			if (ret == -1) {
				perror("smbc_closedir");
				goto fail;
			}

			TALLOC_FREE(servername);
			TALLOC_FREE(url);
		}

		ret = smbc_getFunctionClosedir(ctx)(ctx, servers);
		if (ret == -1) {
			perror("smbc_closedir");
			goto fail;
		}
	}

	ret = smbc_getFunctionClosedir(ctx)(ctx, workgroups);
	if (ret == -1) {
		perror("smbc_closedir");
		goto fail;
	}

	result = 0;
fail:
	if (ctx != NULL) {
		smbc_free_context(ctx, 0);
		ctx = NULL;
	}
	gfree_all();
	poptFreeContext(pc);
	TALLOC_FREE(frame);
	return result;
}