2f2f54e61cdb385406ea3b6a5e222fdb1b14b7b8
[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 browse(char * path,
13                    int scan,
14                    int indent);
15
16
17 static void
18 get_auth_data_with_context_fn(SMBCCTX * context,
19                               const char * pServer,
20                               const char * pShare,
21                               char * pWorkgroup,
22                               int maxLenWorkgroup,
23                               char * pUsername,
24                               int maxLenUsername,
25                               char * pPassword,
26                               int maxLenPassword);
27
28 int main(int argc, const char *argv[])
29 {
30         int                         debug = 0;
31         int                         debug_stderr = 0;
32         int                         no_auth = 0;
33         int                         context_auth = 0;
34         int                         scan = 0;
35         int                         iterations = -1;
36         int                         opt;
37         char *                      p;
38         char                        buf[1024];
39         poptContext                 pc;
40         SMBCCTX *                   context;
41         struct poptOption           long_options[] =
42                 {
43                         POPT_AUTOHELP
44                         {
45                                 "debug", 'd', POPT_ARG_INT, &debug,
46                                 0, "Set debug level", "integer"
47                         },
48                         {
49                                 "stderr", 'e', POPT_ARG_NONE, &debug_stderr,
50                                 0, "Debug log to stderr instead of stdout", "integer"
51                         },
52                         {
53                                 "scan", 's', POPT_ARG_NONE, &scan,
54                                 0, "Scan for servers and shares", "integer"
55                         },
56                         {
57                                 "iterations", 'i', POPT_ARG_INT, &iterations,
58                                 0, "Iterations", "integer"
59                         },
60                         {
61                                 "noauth", 'A', POPT_ARG_NONE, &no_auth,
62                                 0, "Do not request authentication data", "integer"
63                         },
64                         {
65                                 "contextauth", 'C', POPT_ARG_NONE, &context_auth,
66                                 0, "Use new authentication function with context", "integer"
67                         },
68                         {0}
69                 };
70
71         setbuf(stdout, NULL);
72
73         pc = poptGetContext("opendir", argc, argv, long_options, 0);
74
75         poptSetOtherOptionHelp(pc, "");
76
77         while ((opt = poptGetNextOpt(pc)) != -1) {
78                 printf("Got option %d = %c\n", opt, opt);
79                 switch (opt) {
80                 }
81         }
82
83         /* Allocate a new context */
84         context = smbc_new_context();
85         if (!context) {
86                 printf("Could not allocate new smbc context\n");
87                 return 1;
88         }
89
90         /* If we're scanning, do no requests for authentication data */
91         if (scan) {
92                 no_auth = 1;
93         }
94
95         /* Set mandatory options (is that a contradiction in terms?) */
96         smbc_setDebug(context, debug);
97         if (context_auth) {
98                 smbc_setFunctionAuthDataWithContext(context,
99                                                     get_auth_data_with_context_fn);
100                 smbc_setOptionUserData(context, strdup("hello world"));
101         } else {
102                 smbc_setFunctionAuthData(context, get_auth_data_fn);
103         }
104
105         smbc_setOptionUseKerberos(context, 1);
106         smbc_setOptionFallbackAfterKerberos(context, 1);
107
108         /* If we've been asked to log to stderr instead of stdout, ... */
109         if (debug_stderr) {
110                 /* ... then set the option to do so */
111                 smbc_setOptionDebugToStderr(context, 1);
112         }
113
114         /* Initialize the context using the previously specified options */
115         if (!smbc_init_context(context)) {
116                 smbc_free_context(context, 0);
117                 printf("Could not initialize smbc context\n");
118                 return 1;
119         }
120
121         /* Tell the compatibility layer to use this context */
122         smbc_set_context(context);
123
124         if (scan)
125         {
126                 for (; iterations != 0;) {
127                         if (iterations > 0) {
128                                 iterations--;
129                         }
130
131                         snprintf(buf, sizeof(buf), "smb://");
132                         browse(buf, scan, 0);
133                 }
134         }
135         else
136         {
137                 for (; iterations != 0;) {
138                         if (iterations > 0) {
139                                 iterations--;
140                         }
141
142                         fputs("url: ", stdout);
143                         p = fgets(buf, sizeof(buf), stdin);
144                         if (! p)
145                         {
146                                 break;
147                         }
148
149                         if ((p = strchr(buf, '\n')) != NULL)
150                         {
151                                 *p = '\0';
152                         }
153
154                         browse(buf, scan, 0);
155                 }
156         }
157
158         exit(0);
159 }
160
161 static void
162 get_auth_data_with_context_fn(SMBCCTX * context,
163                               const char * pServer,
164                               const char * pShare,
165                               char * pWorkgroup,
166                               int maxLenWorkgroup,
167                               char * pUsername,
168                               int maxLenUsername,
169                               char * pPassword,
170                               int maxLenPassword)
171 {
172         printf("Authenticating with context %p", context);
173         if (context != NULL) {
174                 char *user_data = smbc_getOptionUserData(context);
175                 printf(" with user data %s", user_data);
176         }
177         printf("\n");
178
179         get_auth_data_fn(pServer, pShare, pWorkgroup, maxLenWorkgroup,
180                          pUsername, maxLenUsername, pPassword, maxLenPassword);
181 }
182
183 static void browse(char * path, int scan, int indent)
184 {
185         char *                      p;
186         char                        buf[1024];
187         int                         dir;
188         struct stat                 st;
189         struct smbc_dirent *        dirent;
190
191         if (! scan)
192         {
193                 printf("Opening (%s)...\n", path);
194         }
195
196         if ((dir = smbc_opendir(path)) < 0)
197         {
198                 printf("Could not open directory [%s] (%d:%s)\n",
199                        path, errno, strerror(errno));
200                 return;
201         }
202
203         while ((dirent = smbc_readdir(dir)) != NULL)
204         {
205                 printf("%*.*s%-30s", indent, indent, "", dirent->name);
206
207                 switch(dirent->smbc_type)
208                 {
209                 case SMBC_WORKGROUP:
210                         printf("WORKGROUP");
211                         break;
212
213                 case SMBC_SERVER:
214                         printf("SERVER");
215                         break;
216
217                 case SMBC_FILE_SHARE:
218                         printf("FILE_SHARE");
219                         break;
220
221                 case SMBC_PRINTER_SHARE:
222                         printf("PRINTER_SHARE");
223                         break;
224
225                 case SMBC_COMMS_SHARE:
226                         printf("COMMS_SHARE");
227                         break;
228
229                 case SMBC_IPC_SHARE:
230                         printf("IPC_SHARE");
231                         break;
232
233                 case SMBC_DIR:
234                         printf("DIR");
235                         break;
236
237                 case SMBC_FILE:
238                         printf("FILE");
239
240                         p = path + strlen(path);
241                         strcat(p, "/");
242                         strcat(p+1, dirent->name);
243                         if (smbc_stat(path, &st) < 0)
244                         {
245                                 printf(" unknown size (reason %d: %s)",
246                                        errno, strerror(errno));
247                         }
248                         else
249                         {
250                                 printf(" size %lu", (unsigned long) st.st_size);
251                         }
252                         *p = '\0';
253
254                         break;
255
256                 case SMBC_LINK:
257                         printf("LINK");
258                         break;
259                 }
260
261                 printf("\n");
262
263                 if (scan &&
264                     (dirent->smbc_type == SMBC_WORKGROUP ||
265                      dirent->smbc_type == SMBC_SERVER))
266                 {
267                         /*
268                          * don't append server name to workgroup; what we want is:
269                          *
270                          *   smb://workgroup_name
271                          * or
272                          *   smb://server_name
273                          *
274                          */
275                         snprintf(buf, sizeof(buf), "smb://%s", dirent->name);
276                         browse(buf, scan, indent + 2);
277                 }
278         }
279
280         smbc_closedir(dir);
281 }