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
,
28 int main(int argc
, const char *argv
[])
41 struct poptOption long_options
[] =
45 "debug", 'd', POPT_ARG_INT
, &debug
,
46 0, "Set debug level", "integer"
49 "stderr", 'e', POPT_ARG_NONE
, &debug_stderr
,
50 0, "Debug log to stderr instead of stdout", "integer"
53 "scan", 's', POPT_ARG_NONE
, &scan
,
54 0, "Scan for servers and shares", "integer"
57 "iterations", 'i', POPT_ARG_INT
, &iterations
,
58 0, "Iterations", "integer"
61 "noauth", 'A', POPT_ARG_NONE
, &no_auth
,
62 0, "Do not request authentication data", "integer"
65 "contextauth", 'C', POPT_ARG_NONE
, &context_auth
,
66 0, "Use new authentication function with context", "integer"
75 pc
= poptGetContext("opendir", argc
, argv
, long_options
, 0);
77 poptSetOtherOptionHelp(pc
, "");
79 while ((opt
= poptGetNextOpt(pc
)) != -1) {
80 printf("Got option %d = %c\n", opt
, opt
);
85 /* Allocate a new context */
86 context
= smbc_new_context();
88 printf("Could not allocate new smbc context\n");
92 /* If we're scanning, do no requests for authentication data */
97 /* Set mandatory options (is that a contradiction in terms?) */
98 smbc_setDebug(context
, debug
);
100 smbc_setFunctionAuthDataWithContext(context
,
101 get_auth_data_with_context_fn
);
102 smbc_setOptionUserData(context
, strdup("hello world"));
104 smbc_setFunctionAuthData(context
, get_auth_data_fn
);
107 smbc_setOptionUseKerberos(context
, 1);
108 smbc_setOptionFallbackAfterKerberos(context
, 1);
110 /* If we've been asked to log to stderr instead of stdout, ... */
112 /* ... then set the option to do so */
113 smbc_setOptionDebugToStderr(context
, 1);
116 /* Initialize the context using the previously specified options */
117 if (!smbc_init_context(context
)) {
118 smbc_free_context(context
, 0);
119 printf("Could not initialize smbc context\n");
123 /* Tell the compatibility layer to use this context */
124 smbc_set_context(context
);
128 for (; iterations
!= 0;) {
129 if (iterations
> 0) {
133 snprintf(buf
, sizeof(buf
), "smb://");
134 browse(buf
, scan
, 0);
139 for (; iterations
!= 0;) {
140 if (iterations
> 0) {
144 fputs("url: ", stdout
);
145 p
= fgets(buf
, sizeof(buf
), stdin
);
151 if ((p
= strchr(buf
, '\n')) != NULL
)
156 browse(buf
, scan
, 0);
164 get_auth_data_with_context_fn(SMBCCTX
* context
,
165 const char * pServer
,
174 printf("Authenticating with context %p", context
);
175 if (context
!= NULL
) {
176 char *user_data
= smbc_getOptionUserData(context
);
177 printf(" with user data %s", user_data
);
181 get_auth_data_fn(pServer
, pShare
, pWorkgroup
, maxLenWorkgroup
,
182 pUsername
, maxLenUsername
, pPassword
, maxLenPassword
);
185 static void browse(char * path
, int scan
, int indent
)
191 struct smbc_dirent
* dirent
;
195 printf("Opening (%s)...\n", path
);
198 if ((dir
= smbc_opendir(path
)) < 0)
200 printf("Could not open directory [%s] (%d:%s)\n",
201 path
, errno
, strerror(errno
));
205 while ((dirent
= smbc_readdir(dir
)) != NULL
)
207 printf("%*.*s%-30s", indent
, indent
, "", dirent
->name
);
209 switch(dirent
->smbc_type
)
219 case SMBC_FILE_SHARE
:
220 printf("FILE_SHARE");
223 case SMBC_PRINTER_SHARE
:
224 printf("PRINTER_SHARE");
227 case SMBC_COMMS_SHARE
:
228 printf("COMMS_SHARE");
242 p
= path
+ strlen(path
);
244 strcat(p
+1, dirent
->name
);
245 if (smbc_stat(path
, &st
) < 0)
247 printf(" unknown size (reason %d: %s)",
248 errno
, strerror(errno
));
252 printf(" size %lu", (unsigned long) st
.st_size
);
266 (dirent
->smbc_type
== SMBC_WORKGROUP
||
267 dirent
->smbc_type
== SMBC_SERVER
))
270 * don't append server name to workgroup; what we want is:
272 * smb://workgroup_name
277 snprintf(buf
, sizeof(buf
), "smb://%s", dirent
->name
);
278 browse(buf
, scan
, indent
+ 2);