9 #include <libsmbclient.h>
10 #include "get_auth_data_fn.h"
12 static void browse(char * path,
18 get_auth_data_with_context_fn(SMBCCTX * context,
29 main(int argc, char * argv[])
42 struct poptOption long_options[] =
46 "debug", 'd', POPT_ARG_INT, &debug,
47 0, "Set debug level", "integer"
50 "stderr", 'e', POPT_ARG_NONE, &debug_stderr,
51 0, "Debug log to stderr instead of stdout", "integer"
54 "scan", 's', POPT_ARG_NONE, &scan,
55 0, "Scan for servers and shares", "integer"
58 "iterations", 'i', POPT_ARG_INT, &iterations,
59 0, "Iterations", "integer"
62 "noauth", 'A', POPT_ARG_NONE, &no_auth,
63 0, "Do not request authentication data", "integer"
66 "contextauth", 'C', POPT_ARG_NONE, &context_auth,
67 0, "Use new authentication function with context", "integer"
76 pc = poptGetContext("opendir", argc, (const char **)argv, long_options, 0);
78 poptSetOtherOptionHelp(pc, "");
80 while ((opt = poptGetNextOpt(pc)) != -1) {
81 printf("Got option %d = %c\n", opt, opt);
86 /* Allocate a new context */
87 context = smbc_new_context();
89 printf("Could not allocate new smbc context\n");
93 /* If we're scanning, do no requests for authentication data */
98 /* Set mandatory options (is that a contradiction in terms?) */
99 smbc_setDebug(context, debug);
101 smbc_setFunctionAuthDataWithContext(context,
102 get_auth_data_with_context_fn);
103 smbc_setOptionUserData(context, (void *)"hello world");
105 smbc_setFunctionAuthData(context, get_auth_data_fn);
108 smbc_setOptionUseKerberos(context, 1);
109 smbc_setOptionFallbackAfterKerberos(context, 1);
111 /* If we've been asked to log to stderr instead of stdout, ... */
113 /* ... then set the option to do so */
114 smbc_setOptionDebugToStderr(context, 1);
117 /* Initialize the context using the previously specified options */
118 if (!smbc_init_context(context)) {
119 smbc_free_context(context, 0);
120 printf("Could not initialize smbc context\n");
124 /* Tell the compatibility layer to use this context */
125 smbc_set_context(context);
130 iterations == -1 || iterations > 0;
131 iterations = (iterations == -1 ? iterations : --iterations))
133 snprintf(buf, sizeof(buf), "smb://");
134 browse(buf, scan, 0);
140 iterations == -1 || iterations > 0;
141 iterations = (iterations == -1 ? iterations : --iterations))
143 fputs("url: ", stdout);
144 p = fgets(buf, sizeof(buf), stdin);
150 if ((p = strchr(buf, '\n')) != NULL)
155 browse(buf, scan, 0);
163 get_auth_data_with_context_fn(SMBCCTX * context,
164 const char * pServer,
173 printf("Authenticating with context %p", context);
174 if (context != NULL) {
175 char *user_data = smbc_getOptionUserData(context);
176 printf(" with user data %s", user_data);
180 get_auth_data_fn(pServer, pShare, pWorkgroup, maxLenWorkgroup,
181 pUsername, maxLenUsername, pPassword, maxLenPassword);
184 static void browse(char * path, int scan, int indent)
190 struct smbc_dirent * dirent;
194 printf("Opening (%s)...\n", path);
197 if ((dir = smbc_opendir(path)) < 0)
199 printf("Could not open directory [%s] (%d:%s)\n",
200 path, errno, strerror(errno));
204 while ((dirent = smbc_readdir(dir)) != NULL)
206 printf("%*.*s%-30s", indent, indent, "", dirent->name);
208 switch(dirent->smbc_type)
218 case SMBC_FILE_SHARE:
219 printf("FILE_SHARE");
222 case SMBC_PRINTER_SHARE:
223 printf("PRINTER_SHARE");
226 case SMBC_COMMS_SHARE:
227 printf("COMMS_SHARE");
241 p = path + strlen(path);
243 strcat(p+1, dirent->name);
244 if (smbc_stat(path, &st) < 0)
246 printf(" unknown size (reason %d: %s)",
247 errno, strerror(errno));
251 printf(" size %lu", (unsigned long) st.st_size);
265 (dirent->smbc_type == SMBC_WORKGROUP ||
266 dirent->smbc_type == SMBC_SERVER))
269 * don't append server name to workgroup; what we want is:
271 * smb://workgroup_name
276 snprintf(buf, sizeof(buf), "smb://%s", dirent->name);
277 browse(buf, scan, indent + 2);