00001 #include <sys/types.h>
00002 #include <unistd.h>
00003 #include <dirent.h>
00004 #include <errno.h>
00005 #include <stdio.h>
00006 #include <string.h>
00007 #include <popt.h>
00008 #include <stdlib.h>
00009 #include <libsmbclient.h>
00010 #include "get_auth_data_fn.h"
00011
00012 static void
00013 no_auth_data_fn(const char * pServer,
00014 const char * pShare,
00015 char * pWorkgroup,
00016 int maxLenWorkgroup,
00017 char * pUsername,
00018 int maxLenUsername,
00019 char * pPassword,
00020 int maxLenPassword);
00021
00022 static void browse(char * path,
00023 int scan,
00024 int indent);
00025
00026
00027 static void
00028 get_auth_data_with_context_fn(SMBCCTX * context,
00029 const char * pServer,
00030 const char * pShare,
00031 char * pWorkgroup,
00032 int maxLenWorkgroup,
00033 char * pUsername,
00034 int maxLenUsername,
00035 char * pPassword,
00036 int maxLenPassword);
00037
00038 int
00039 main(int argc, char * argv[])
00040 {
00041 int debug = 0;
00042 int debug_stderr = 0;
00043 int no_auth = 0;
00044 int context_auth = 0;
00045 int scan = 0;
00046 int iterations = -1;
00047 int again;
00048 int opt;
00049 char * p;
00050 char * q;
00051 char buf[1024];
00052 poptContext pc;
00053 SMBCCTX * context;
00054 struct poptOption long_options[] =
00055 {
00056 POPT_AUTOHELP
00057 {
00058 "debug", 'd', POPT_ARG_INT, &debug,
00059 0, "Set debug level", "integer"
00060 },
00061 {
00062 "stderr", 'e', POPT_ARG_NONE, &debug_stderr,
00063 0, "Debug log to stderr instead of stdout", "integer"
00064 },
00065 {
00066 "scan", 's', POPT_ARG_NONE, &scan,
00067 0, "Scan for servers and shares", "integer"
00068 },
00069 {
00070 "iterations", 'i', POPT_ARG_INT, &iterations,
00071 0, "Iterations", "integer"
00072 },
00073 {
00074 "noauth", 'A', POPT_ARG_NONE, &no_auth,
00075 0, "Do not request authentication data", "integer"
00076 },
00077 {
00078 "contextauth", 'C', POPT_ARG_NONE, &context_auth,
00079 0, "Use new authentication function with context", "integer"
00080 },
00081 {
00082 NULL
00083 }
00084 };
00085
00086 setbuf(stdout, NULL);
00087
00088 pc = poptGetContext("opendir", argc, (const char **)argv, long_options, 0);
00089
00090 poptSetOtherOptionHelp(pc, "");
00091
00092 while ((opt = poptGetNextOpt(pc)) != -1) {
00093 printf("Got option %d = %c\n", opt, opt);
00094 switch (opt) {
00095 }
00096 }
00097
00098
00099 context = smbc_new_context();
00100 if (!context) {
00101 printf("Could not allocate new smbc context\n");
00102 return 1;
00103 }
00104
00105
00106 if (scan) {
00107 no_auth = 1;
00108 }
00109
00110
00111 context->debug = debug;
00112 if (context_auth) {
00113 context->callbacks.auth_fn = NULL;
00114 smbc_option_set(context,
00115 "auth_function",
00116 (void *) get_auth_data_with_context_fn);
00117 smbc_option_set(context, "user_data", "hello world");
00118 } else {
00119 context->callbacks.auth_fn =
00120 (no_auth ? no_auth_data_fn : get_auth_data_fn);
00121 }
00122
00123
00124 if (debug_stderr) {
00125
00126 smbc_option_set(context, "debug_to_stderr", 1);
00127 }
00128
00129
00130 if (!smbc_init_context(context)) {
00131 smbc_free_context(context, 0);
00132 printf("Could not initialize smbc context\n");
00133 return 1;
00134 }
00135
00136
00137 smbc_set_context(context);
00138
00139 if (scan)
00140 {
00141 for (;
00142 iterations == -1 || iterations > 0;
00143 iterations = (iterations == -1 ? iterations : --iterations))
00144 {
00145 snprintf(buf, sizeof(buf), "smb://");
00146 browse(buf, scan, 0);
00147 }
00148 }
00149 else
00150 {
00151 for (;
00152 iterations == -1 || iterations > 0;
00153 iterations = (iterations == -1 ? iterations : --iterations))
00154 {
00155 fputs("url: ", stdout);
00156 p = fgets(buf, sizeof(buf), stdin);
00157 if (! p)
00158 {
00159 break;
00160 }
00161
00162 if ((p = strchr(buf, '\n')) != NULL)
00163 {
00164 *p = '\0';
00165 }
00166
00167 browse(buf, scan, 0);
00168 }
00169 }
00170
00171 exit(0);
00172 }
00173
00174
00175 static void
00176 no_auth_data_fn(const char * pServer,
00177 const char * pShare,
00178 char * pWorkgroup,
00179 int maxLenWorkgroup,
00180 char * pUsername,
00181 int maxLenUsername,
00182 char * pPassword,
00183 int maxLenPassword)
00184 {
00185 return;
00186 }
00187
00188
00189 static void
00190 get_auth_data_with_context_fn(SMBCCTX * context,
00191 const char * pServer,
00192 const char * pShare,
00193 char * pWorkgroup,
00194 int maxLenWorkgroup,
00195 char * pUsername,
00196 int maxLenUsername,
00197 char * pPassword,
00198 int maxLenPassword)
00199 {
00200 printf("Authenticating with context 0x%lx", context);
00201 if (context != NULL) {
00202 char *user_data = smbc_option_get(context, "user_data");
00203 printf(" with user data %s", user_data);
00204 }
00205 printf("\n");
00206
00207 get_auth_data_fn(pServer, pShare, pWorkgroup, maxLenWorkgroup,
00208 pUsername, maxLenUsername, pPassword, maxLenPassword);
00209 }
00210
00211 static void browse(char * path, int scan, int indent)
00212 {
00213 char * p;
00214 char buf[1024];
00215 int dir;
00216 struct stat stat;
00217 struct smbc_dirent * dirent;
00218
00219 if (! scan)
00220 {
00221 printf("Opening (%s)...\n", path);
00222 }
00223
00224 if ((dir = smbc_opendir(path)) < 0)
00225 {
00226 printf("Could not open directory [%s] (%d:%s)\n",
00227 path, errno, strerror(errno));
00228 return;
00229 }
00230
00231 while ((dirent = smbc_readdir(dir)) != NULL)
00232 {
00233 printf("%*.*s%-30s", indent, indent, "", dirent->name);
00234
00235 switch(dirent->smbc_type)
00236 {
00237 case SMBC_WORKGROUP:
00238 printf("WORKGROUP");
00239 break;
00240
00241 case SMBC_SERVER:
00242 printf("SERVER");
00243 break;
00244
00245 case SMBC_FILE_SHARE:
00246 printf("FILE_SHARE");
00247 break;
00248
00249 case SMBC_PRINTER_SHARE:
00250 printf("PRINTER_SHARE");
00251 break;
00252
00253 case SMBC_COMMS_SHARE:
00254 printf("COMMS_SHARE");
00255 break;
00256
00257 case SMBC_IPC_SHARE:
00258 printf("IPC_SHARE");
00259 break;
00260
00261 case SMBC_DIR:
00262 printf("DIR");
00263 break;
00264
00265 case SMBC_FILE:
00266 printf("FILE");
00267
00268 p = path + strlen(path);
00269 strcat(p, "/");
00270 strcat(p+1, dirent->name);
00271 if (smbc_stat(path, &stat) < 0)
00272 {
00273 printf(" unknown size (reason %d: %s)",
00274 errno, strerror(errno));
00275 }
00276 else
00277 {
00278 printf(" size %lu", (unsigned long) stat.st_size);
00279 }
00280 *p = '\0';
00281
00282 break;
00283
00284 case SMBC_LINK:
00285 printf("LINK");
00286 break;
00287 }
00288
00289 printf("\n");
00290
00291 if (scan &&
00292 (dirent->smbc_type == SMBC_WORKGROUP ||
00293 dirent->smbc_type == SMBC_SERVER))
00294 {
00295
00296
00297
00298
00299
00300
00301
00302
00303 snprintf(buf, sizeof(buf), "smb://%s", dirent->name);
00304 browse(buf, scan, indent + 2);
00305 }
00306 }
00307
00308 smbc_closedir(dir);
00309 }
00310