9
#include <libsmbclient.h>
10
#include "get_auth_data_fn.h"
13
no_auth_data_fn(const char * pServer,
22
static void browse(char * path,
28
get_auth_data_with_context_fn(SMBCCTX * context,
39
main(int argc, char * argv[])
54
struct poptOption long_options[] =
58
"debug", 'd', POPT_ARG_INT, &debug,
59
0, "Set debug level", "integer"
62
"stderr", 'e', POPT_ARG_NONE, &debug_stderr,
63
0, "Debug log to stderr instead of stdout", "integer"
66
"scan", 's', POPT_ARG_NONE, &scan,
67
0, "Scan for servers and shares", "integer"
70
"iterations", 'i', POPT_ARG_INT, &iterations,
71
0, "Iterations", "integer"
74
"noauth", 'A', POPT_ARG_NONE, &no_auth,
75
0, "Do not request authentication data", "integer"
78
"contextauth", 'C', POPT_ARG_NONE, &context_auth,
79
0, "Use new authentication function with context", "integer"
88
pc = poptGetContext("opendir", argc, (const char **)argv, long_options, 0);
90
poptSetOtherOptionHelp(pc, "");
92
while ((opt = poptGetNextOpt(pc)) != -1) {
93
printf("Got option %d = %c\n", opt, opt);
98
/* Allocate a new context */
99
context = smbc_new_context();
101
printf("Could not allocate new smbc context\n");
105
/* If we're scanning, do no requests for authentication data */
110
/* Set mandatory options (is that a contradiction in terms?) */
111
smbc_setDebug(context, debug);
113
smbc_setFunctionAuthDataWithContext(context,
114
get_auth_data_with_context_fn);
115
smbc_setOptionUserData(context, "hello world");
117
smbc_setFunctionAuthData(context, get_auth_data_fn);
120
smbc_setOptionUseKerberos(context, 1);
121
smbc_setOptionFallbackAfterKerberos(context, 1);
123
/* If we've been asked to log to stderr instead of stdout, ... */
125
/* ... then set the option to do so */
126
smbc_setOptionDebugToStderr(context, 1);
129
/* Initialize the context using the previously specified options */
130
if (!smbc_init_context(context)) {
131
smbc_free_context(context, 0);
132
printf("Could not initialize smbc context\n");
136
/* Tell the compatibility layer to use this context */
137
smbc_set_context(context);
142
iterations == -1 || iterations > 0;
143
iterations = (iterations == -1 ? iterations : --iterations))
145
snprintf(buf, sizeof(buf), "smb://");
146
browse(buf, scan, 0);
152
iterations == -1 || iterations > 0;
153
iterations = (iterations == -1 ? iterations : --iterations))
155
fputs("url: ", stdout);
156
p = fgets(buf, sizeof(buf), stdin);
162
if ((p = strchr(buf, '\n')) != NULL)
167
browse(buf, scan, 0);
176
no_auth_data_fn(const char * pServer,
190
get_auth_data_with_context_fn(SMBCCTX * context,
191
const char * pServer,
200
printf("Authenticating with context %p", context);
201
if (context != NULL) {
202
char *user_data = smbc_getOptionUserData(context);
203
printf(" with user data %s", user_data);
207
get_auth_data_fn(pServer, pShare, pWorkgroup, maxLenWorkgroup,
208
pUsername, maxLenUsername, pPassword, maxLenPassword);
211
static void browse(char * path, int scan, int indent)
217
struct smbc_dirent * dirent;
221
printf("Opening (%s)...\n", path);
224
if ((dir = smbc_opendir(path)) < 0)
226
printf("Could not open directory [%s] (%d:%s)\n",
227
path, errno, strerror(errno));
231
while ((dirent = smbc_readdir(dir)) != NULL)
233
printf("%*.*s%-30s", indent, indent, "", dirent->name);
235
switch(dirent->smbc_type)
245
case SMBC_FILE_SHARE:
246
printf("FILE_SHARE");
249
case SMBC_PRINTER_SHARE:
250
printf("PRINTER_SHARE");
253
case SMBC_COMMS_SHARE:
254
printf("COMMS_SHARE");
268
p = path + strlen(path);
270
strcat(p+1, dirent->name);
271
if (smbc_stat(path, &stat) < 0)
273
printf(" unknown size (reason %d: %s)",
274
errno, strerror(errno));
278
printf(" size %lu", (unsigned long) stat.st_size);
292
(dirent->smbc_type == SMBC_WORKGROUP ||
293
dirent->smbc_type == SMBC_SERVER))
296
* don't append server name to workgroup; what we want is:
298
* smb://workgroup_name
303
snprintf(buf, sizeof(buf), "smb://%s", dirent->name);
304
browse(buf, scan, indent + 2);