Initial revamp of the libsmbclient interface.
[samba.git] / examples / libsmbclient / testbrowse.c
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"
11
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);
21
22 static void browse(char * path,
23                    int scan,
24                    int indent);
25
26
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);
37
38 int
39 main(int argc, char * argv[])
40 {
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[] =
55         {
56             POPT_AUTOHELP
57             {
58                 "debug", 'd', POPT_ARG_INT, &debug,
59                 0, "Set debug level", "integer"
60             },
61             {
62                 "stderr", 'e', POPT_ARG_NONE, &debug_stderr,
63                 0, "Debug log to stderr instead of stdout", "integer"
64             },
65             {
66                 "scan", 's', POPT_ARG_NONE, &scan,
67                 0, "Scan for servers and shares", "integer"
68             },
69             {
70                 "iterations", 'i', POPT_ARG_INT, &iterations,
71                 0, "Iterations", "integer"
72             },
73             {
74                 "noauth", 'A', POPT_ARG_NONE, &no_auth,
75                 0, "Do not request authentication data", "integer"
76             },
77             {
78                 "contextauth", 'C', POPT_ARG_NONE, &context_auth,
79                 0, "Use new authentication function with context", "integer"
80             },
81             {
82                 NULL
83             }
84         };
85     
86     setbuf(stdout, NULL);
87
88     pc = poptGetContext("opendir", argc, (const char **)argv, long_options, 0);
89     
90     poptSetOtherOptionHelp(pc, "");
91     
92     while ((opt = poptGetNextOpt(pc)) != -1) {
93         printf("Got option %d = %c\n", opt, opt);
94         switch (opt) {
95         }
96     }
97
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;
103     }
104         
105     /* If we're scanning, do no requests for authentication data */
106     if (scan) {
107         no_auth = 1;
108     }
109
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);
122     }
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
128
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);
133     }
134         
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;
140     }
141
142     /* Tell the compatibility layer to use this context */
143     smbc_set_context(context);
144
145     if (scan)
146     {
147         for (;
148              iterations == -1 || iterations > 0;
149              iterations = (iterations == -1 ? iterations : --iterations))
150         {
151             snprintf(buf, sizeof(buf), "smb://");
152             browse(buf, scan, 0);
153         }
154     }
155     else
156     {
157         for (;
158              iterations == -1 || iterations > 0;
159              iterations = (iterations == -1 ? iterations : --iterations))
160         {
161             fputs("url: ", stdout);
162             p = fgets(buf, sizeof(buf), stdin);
163             if (! p)
164             {
165                 break;
166             }
167
168             if ((p = strchr(buf, '\n')) != NULL)
169             {
170                 *p = '\0';
171             }
172             
173             browse(buf, scan, 0);
174         }
175     }
176
177     exit(0);
178 }
179
180
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)
190 {
191     return;
192 }
193
194
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)
205 {
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);
210     }
211     printf("\n");
212
213     get_auth_data_fn(pServer, pShare, pWorkgroup, maxLenWorkgroup,
214                      pUsername, maxLenUsername, pPassword, maxLenPassword);
215 }
216
217 static void browse(char * path, int scan, int indent)
218 {
219     char *                      p;
220     char                        buf[1024];
221     int                         dir;
222     struct stat                 stat;
223     struct smbc_dirent *        dirent;
224
225     if (! scan)
226     {
227         printf("Opening (%s)...\n", path);
228     }
229         
230     if ((dir = smbc_opendir(path)) < 0)
231     {
232         printf("Could not open directory [%s] (%d:%s)\n",
233                path, errno, strerror(errno));
234         return;
235     }
236
237     while ((dirent = smbc_readdir(dir)) != NULL)
238     {
239         printf("%*.*s%-30s", indent, indent, "", dirent->name);
240
241         switch(dirent->smbc_type)
242         {
243         case SMBC_WORKGROUP:
244             printf("WORKGROUP");
245             break;
246             
247         case SMBC_SERVER:
248             printf("SERVER");
249             break;
250             
251         case SMBC_FILE_SHARE:
252             printf("FILE_SHARE");
253             break;
254             
255         case SMBC_PRINTER_SHARE:
256             printf("PRINTER_SHARE");
257             break;
258             
259         case SMBC_COMMS_SHARE:
260             printf("COMMS_SHARE");
261             break;
262             
263         case SMBC_IPC_SHARE:
264             printf("IPC_SHARE");
265             break;
266             
267         case SMBC_DIR:
268             printf("DIR");
269             break;
270             
271         case SMBC_FILE:
272             printf("FILE");
273
274             p = path + strlen(path);
275             strcat(p, "/");
276             strcat(p+1, dirent->name);
277             if (smbc_stat(path, &stat) < 0)
278             {
279                 printf(" unknown size (reason %d: %s)",
280                        errno, strerror(errno));
281             }
282             else
283             {
284                 printf(" size %lu", (unsigned long) stat.st_size);
285             }
286             *p = '\0';
287
288             break;
289             
290         case SMBC_LINK:
291             printf("LINK");
292             break;
293         }
294
295         printf("\n");
296
297         if (scan &&
298             (dirent->smbc_type == SMBC_WORKGROUP ||
299              dirent->smbc_type == SMBC_SERVER))
300         {
301             /*
302              * don't append server name to workgroup; what we want is:
303              *
304              *   smb://workgroup_name
305              * or
306              *   smb://server_name
307              *
308              */
309             snprintf(buf, sizeof(buf), "smb://%s", dirent->name);
310             browse(buf, scan, indent + 2);
311         }
312     }
313
314     smbc_closedir(dir);
315 }
316