Initial revamp of the libsmbclient interface.
[Samba.git] / examples / libsmbclient / testbrowse.c
blob495cf0fbec033f9dbccccdb9986515821db7edc3
1 #include <sys/types.h>
2 #include <unistd.h>
3 #include <dirent.h>
4 #include <errno.h>
5 #include <stdio.h>
6 #include <string.h>
7 #include <popt.h>
8 #include <stdlib.h>
9 #include <libsmbclient.h>
10 #include "get_auth_data_fn.h"
12 static void
13 no_auth_data_fn(const char * pServer,
14 const char * pShare,
15 char * pWorkgroup,
16 int maxLenWorkgroup,
17 char * pUsername,
18 int maxLenUsername,
19 char * pPassword,
20 int maxLenPassword);
22 static void browse(char * path,
23 int scan,
24 int indent);
27 static void
28 get_auth_data_with_context_fn(SMBCCTX * context,
29 const char * pServer,
30 const char * pShare,
31 char * pWorkgroup,
32 int maxLenWorkgroup,
33 char * pUsername,
34 int maxLenUsername,
35 char * pPassword,
36 int maxLenPassword);
38 int
39 main(int argc, char * argv[])
41 int debug = 0;
42 int debug_stderr = 0;
43 int no_auth = 0;
44 int context_auth = 0;
45 int scan = 0;
46 int iterations = -1;
47 int again;
48 int opt;
49 char * p;
50 char * q;
51 char buf[1024];
52 poptContext pc;
53 SMBCCTX * context;
54 struct poptOption long_options[] =
56 POPT_AUTOHELP
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"
82 NULL
86 setbuf(stdout, NULL);
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);
94 switch (opt) {
98 /* Allocate a new context */
99 context = smbc_new_context();
100 if (!context) {
101 printf("Could not allocate new smbc context\n");
102 return 1;
105 /* If we're scanning, do no requests for authentication data */
106 if (scan) {
107 no_auth = 1;
110 /* Set mandatory options (is that a contradiction in terms?) */
111 smbc_setDebug(context, debug);
112 #if 0
113 if (context_auth) {
114 context->callbacks.auth_fn = NULL;
115 smbc_option_set(context,
116 "auth_function",
117 (void *) get_auth_data_with_context_fn);
118 smbc_option_set(context, "user_data", "hello world");
119 } else {
120 context->callbacks.auth_fn =
121 (no_auth ? no_auth_data_fn : get_auth_data_fn);
123 #else
124 #warning "temporarily remove setting alternate auth function"
125 smbc_setFunctionAuthData(context,
126 (no_auth ? no_auth_data_fn : get_auth_data_fn));
127 #endif
129 /* If we've been asked to log to stderr instead of stdout, ... */
130 if (debug_stderr) {
131 /* ... then set the option to do so */
132 smbc_option_set(context, "debug_to_stderr", 1);
135 /* Initialize the context using the previously specified options */
136 if (!smbc_init_context(context)) {
137 smbc_free_context(context, 0);
138 printf("Could not initialize smbc context\n");
139 return 1;
142 /* Tell the compatibility layer to use this context */
143 smbc_set_context(context);
145 if (scan)
147 for (;
148 iterations == -1 || iterations > 0;
149 iterations = (iterations == -1 ? iterations : --iterations))
151 snprintf(buf, sizeof(buf), "smb://");
152 browse(buf, scan, 0);
155 else
157 for (;
158 iterations == -1 || iterations > 0;
159 iterations = (iterations == -1 ? iterations : --iterations))
161 fputs("url: ", stdout);
162 p = fgets(buf, sizeof(buf), stdin);
163 if (! p)
165 break;
168 if ((p = strchr(buf, '\n')) != NULL)
170 *p = '\0';
173 browse(buf, scan, 0);
177 exit(0);
181 static void
182 no_auth_data_fn(const char * pServer,
183 const char * pShare,
184 char * pWorkgroup,
185 int maxLenWorkgroup,
186 char * pUsername,
187 int maxLenUsername,
188 char * pPassword,
189 int maxLenPassword)
191 return;
195 static void
196 get_auth_data_with_context_fn(SMBCCTX * context,
197 const char * pServer,
198 const char * pShare,
199 char * pWorkgroup,
200 int maxLenWorkgroup,
201 char * pUsername,
202 int maxLenUsername,
203 char * pPassword,
204 int maxLenPassword)
206 printf("Authenticating with context 0x%lx", context);
207 if (context != NULL) {
208 char *user_data = smbc_option_get(context, "user_data");
209 printf(" with user data %s", user_data);
211 printf("\n");
213 get_auth_data_fn(pServer, pShare, pWorkgroup, maxLenWorkgroup,
214 pUsername, maxLenUsername, pPassword, maxLenPassword);
217 static void browse(char * path, int scan, int indent)
219 char * p;
220 char buf[1024];
221 int dir;
222 struct stat stat;
223 struct smbc_dirent * dirent;
225 if (! scan)
227 printf("Opening (%s)...\n", path);
230 if ((dir = smbc_opendir(path)) < 0)
232 printf("Could not open directory [%s] (%d:%s)\n",
233 path, errno, strerror(errno));
234 return;
237 while ((dirent = smbc_readdir(dir)) != NULL)
239 printf("%*.*s%-30s", indent, indent, "", dirent->name);
241 switch(dirent->smbc_type)
243 case SMBC_WORKGROUP:
244 printf("WORKGROUP");
245 break;
247 case SMBC_SERVER:
248 printf("SERVER");
249 break;
251 case SMBC_FILE_SHARE:
252 printf("FILE_SHARE");
253 break;
255 case SMBC_PRINTER_SHARE:
256 printf("PRINTER_SHARE");
257 break;
259 case SMBC_COMMS_SHARE:
260 printf("COMMS_SHARE");
261 break;
263 case SMBC_IPC_SHARE:
264 printf("IPC_SHARE");
265 break;
267 case SMBC_DIR:
268 printf("DIR");
269 break;
271 case SMBC_FILE:
272 printf("FILE");
274 p = path + strlen(path);
275 strcat(p, "/");
276 strcat(p+1, dirent->name);
277 if (smbc_stat(path, &stat) < 0)
279 printf(" unknown size (reason %d: %s)",
280 errno, strerror(errno));
282 else
284 printf(" size %lu", (unsigned long) stat.st_size);
286 *p = '\0';
288 break;
290 case SMBC_LINK:
291 printf("LINK");
292 break;
295 printf("\n");
297 if (scan &&
298 (dirent->smbc_type == SMBC_WORKGROUP ||
299 dirent->smbc_type == SMBC_SERVER))
302 * don't append server name to workgroup; what we want is:
304 * smb://workgroup_name
305 * or
306 * smb://server_name
309 snprintf(buf, sizeof(buf), "smb://%s", dirent->name);
310 browse(buf, scan, indent + 2);
314 smbc_closedir(dir);