static int Context_init (Context *self, PyObject *args, PyObject *kwds) { PyObject *auth = NULL; int debug = 0; SMBCCTX *ctx; static char *kwlist[] = { "auth_fn", "debug", NULL }; if (!PyArg_ParseTupleAndKeywords (args, kwds, "|Oi", kwlist, &auth, &debug)) { return -1; } if (auth) { if (!PyCallable_Check (auth)) { PyErr_SetString (PyExc_TypeError, "auth_fn must be callable"); return -1; } Py_XINCREF (auth); self->auth_fn = auth; } debugprintf ("-> Context_init ()\n"); errno = 0; ctx = smbc_new_context (); if (ctx == NULL) { PyErr_SetFromErrno (PyExc_RuntimeError); debugprintf ("<- Context_init() EXCEPTION\n"); return -1; } smbc_setDebug (ctx, debug); self->context = ctx; smbc_setOptionUserData (ctx, self); if (auth) smbc_setFunctionAuthDataWithContext (ctx, auth_fn); if (smbc_init_context (ctx) == NULL) { PyErr_SetFromErrno (PyExc_RuntimeError); smbc_free_context (ctx, 0); debugprintf ("<- Context_init() EXCEPTION\n"); return -1; } debugprintf ("%p <- Context_init() = 0\n", self->context); return 0; }
SmbFs::SmbFs(QString name, QObject *parent) : BaseFs(name,parent) { _ctx = smbc_new_context(); smbc_setTimeout(_ctx,10000); smbc_setOptionUserData(_ctx,this); //set this class as the user data, so we can get it from the auth function smbc_setOptionOneSharePerServer(_ctx,true); smbc_setFunctionAuthDataWithContext(_ctx,auth_fn); //set the authentication function _ctx = smbc_init_context(_ctx); if(_ctx == NULL) qDebug() << "Null Context!" << strerror(errno); }
static void _pp_samba_get_devices_thread (GSimpleAsyncResult *res, GObject *object, GCancellable *cancellable) { static GMutex mutex; SMBData *data; SMBCCTX *smb_context; gchar *dirname; gchar *path; gchar *hostname = NULL; data = g_simple_async_result_get_op_res_gpointer (res); data->devices = g_new0 (PpDevicesList, 1); data->devices->devices = NULL; data->samba = PP_SAMBA (object); g_mutex_lock (&mutex); smb_context = smbc_new_context (); if (smb_context) { if (smbc_init_context (smb_context)) { smbc_setOptionUserData (smb_context, data); g_object_get (object, "hostname", &hostname, NULL); if (hostname != NULL) { dirname = g_strdup_printf ("smb://%s", hostname); path = g_strdup_printf ("//%s", hostname); g_free (hostname); } else { dirname = g_strdup_printf ("smb://"); path = g_strdup_printf ("//"); } smbc_setFunctionAuthDataWithContext (smb_context, anonymous_auth_fn); list_dir (smb_context, dirname, path, cancellable, data); g_free (dirname); g_free (path); } smbc_free_context (smb_context, 1); } g_mutex_unlock (&mutex); }
csync_vio_method_t *vio_module_init(const char *method_name, const char *args, csync_auth_callback cb, void *userdata) { smb_context = smbc_new_context(); DEBUG_SMB(("csync_smb - method_name: %s\n", method_name)); DEBUG_SMB(("csync_smb - args: %s\n", args)); (void) method_name; (void) args; (void) cb; if (smb_context == NULL) { fprintf(stderr, "csync_smb - failed to create new smbc context\n"); return NULL; } if (cb != NULL) { _authcb = cb; } /* set debug level and authentication function callback */ smbc_setDebug(smb_context, 0); smbc_setOptionUserData(smb_context, userdata); smbc_setFunctionAuthDataWithContext(smb_context, get_auth_data_with_context_fn); /* Kerberos support */ smbc_setOptionUseKerberos(smb_context, 1); smbc_setOptionFallbackAfterKerberos(smb_context, 1); DEBUG_SMB(("csync_smb - use kerberos = %d\n", smbc_getOptionUseKerberos(smb_context))); DEBUG_SMB(("csync_smb - use fallback after kerberos = %d\n", smbc_getOptionFallbackAfterKerberos(smb_context))); if (smbc_init_context(smb_context) == NULL) { fprintf(stderr, "csync_smb - failed to initialize the smbc context"); smbc_free_context(smb_context, 0); smb_context = NULL; return NULL; } DEBUG_SMB(("csync_smb - KRB5CCNAME = %s\n", getenv("KRB5CCNAME") != NULL ? getenv("KRB5CCNAME") : "not set")); smbc_set_context(smb_context); return &_method; }
/* * Get a new empty handle to fill in with your own info */ SMBCCTX * smbc_new_context(void) { SMBCCTX *context; /* The first call to this function should initialize the module */ SMB_THREAD_ONCE(&SMBC_initialized, SMBC_module_init, NULL); /* * All newly added context fields should be placed in * SMBC_internal_data, not directly in SMBCCTX. */ context = SMB_MALLOC_P(SMBCCTX); if (!context) { errno = ENOMEM; return NULL; } ZERO_STRUCTP(context); context->internal = SMB_MALLOC_P(struct SMBC_internal_data); if (!context->internal) { SAFE_FREE(context); errno = ENOMEM; return NULL; } /* Initialize the context and establish reasonable defaults */ ZERO_STRUCTP(context->internal); smbc_setDebug(context, 0); smbc_setTimeout(context, 20000); smbc_setOptionFullTimeNames(context, False); smbc_setOptionOpenShareMode(context, SMBC_SHAREMODE_DENY_NONE); smbc_setOptionSmbEncryptionLevel(context, SMBC_ENCRYPTLEVEL_NONE); smbc_setOptionCaseSensitive(context, False); smbc_setOptionBrowseMaxLmbCount(context, 3); /* # LMBs to query */ smbc_setOptionUrlEncodeReaddirEntries(context, False); smbc_setOptionOneSharePerServer(context, False); smbc_setFunctionAuthData(context, SMBC_get_auth_data); smbc_setFunctionCheckServer(context, SMBC_check_server); smbc_setFunctionRemoveUnusedServer(context, SMBC_remove_unused_server); smbc_setOptionUserData(context, NULL); smbc_setFunctionAddCachedServer(context, SMBC_add_cached_server); smbc_setFunctionGetCachedServer(context, SMBC_get_cached_server); smbc_setFunctionRemoveCachedServer(context, SMBC_remove_cached_server); smbc_setFunctionPurgeCachedServers(context, SMBC_purge_cached_servers); smbc_setFunctionOpen(context, SMBC_open_ctx); smbc_setFunctionCreat(context, SMBC_creat_ctx); smbc_setFunctionRead(context, SMBC_read_ctx); smbc_setFunctionWrite(context, SMBC_write_ctx); smbc_setFunctionClose(context, SMBC_close_ctx); smbc_setFunctionUnlink(context, SMBC_unlink_ctx); smbc_setFunctionRename(context, SMBC_rename_ctx); smbc_setFunctionLseek(context, SMBC_lseek_ctx); smbc_setFunctionFtruncate(context, SMBC_ftruncate_ctx); smbc_setFunctionStat(context, SMBC_stat_ctx); smbc_setFunctionStatVFS(context, SMBC_statvfs_ctx); smbc_setFunctionFstatVFS(context, SMBC_fstatvfs_ctx); smbc_setFunctionFstat(context, SMBC_fstat_ctx); smbc_setFunctionOpendir(context, SMBC_opendir_ctx); smbc_setFunctionClosedir(context, SMBC_closedir_ctx); smbc_setFunctionReaddir(context, SMBC_readdir_ctx); smbc_setFunctionGetdents(context, SMBC_getdents_ctx); smbc_setFunctionMkdir(context, SMBC_mkdir_ctx); smbc_setFunctionRmdir(context, SMBC_rmdir_ctx); smbc_setFunctionTelldir(context, SMBC_telldir_ctx); smbc_setFunctionLseekdir(context, SMBC_lseekdir_ctx); smbc_setFunctionFstatdir(context, SMBC_fstatdir_ctx); smbc_setFunctionChmod(context, SMBC_chmod_ctx); smbc_setFunctionUtimes(context, SMBC_utimes_ctx); smbc_setFunctionSetxattr(context, SMBC_setxattr_ctx); smbc_setFunctionGetxattr(context, SMBC_getxattr_ctx); smbc_setFunctionRemovexattr(context, SMBC_removexattr_ctx); smbc_setFunctionListxattr(context, SMBC_listxattr_ctx); smbc_setFunctionOpenPrintJob(context, SMBC_open_print_job_ctx); smbc_setFunctionPrintFile(context, SMBC_print_file_ctx); smbc_setFunctionListPrintJobs(context, SMBC_list_print_jobs_ctx); smbc_setFunctionUnlinkPrintJob(context, SMBC_unlink_print_job_ctx); return context; }
int main(int argc, char * argv[]) { int debug = 0; int debug_stderr = 0; int no_auth = 0; int context_auth = 0; int scan = 0; int iterations = -1; int opt; char * p; char buf[1024]; poptContext pc; SMBCCTX * context; struct poptOption long_options[] = { POPT_AUTOHELP { "debug", 'd', POPT_ARG_INT, &debug, 0, "Set debug level", "integer" }, { "stderr", 'e', POPT_ARG_NONE, &debug_stderr, 0, "Debug log to stderr instead of stdout", "integer" }, { "scan", 's', POPT_ARG_NONE, &scan, 0, "Scan for servers and shares", "integer" }, { "iterations", 'i', POPT_ARG_INT, &iterations, 0, "Iterations", "integer" }, { "noauth", 'A', POPT_ARG_NONE, &no_auth, 0, "Do not request authentication data", "integer" }, { "contextauth", 'C', POPT_ARG_NONE, &context_auth, 0, "Use new authentication function with context", "integer" }, { NULL } }; setbuf(stdout, NULL); pc = poptGetContext("opendir", argc, (const char **)argv, long_options, 0); poptSetOtherOptionHelp(pc, ""); while ((opt = poptGetNextOpt(pc)) != -1) { printf("Got option %d = %c\n", opt, opt); switch (opt) { } } /* Allocate a new context */ context = smbc_new_context(); if (!context) { printf("Could not allocate new smbc context\n"); return 1; } /* If we're scanning, do no requests for authentication data */ if (scan) { no_auth = 1; } /* Set mandatory options (is that a contradiction in terms?) */ smbc_setDebug(context, debug); if (context_auth) { smbc_setFunctionAuthDataWithContext(context, get_auth_data_with_context_fn); smbc_setOptionUserData(context, (void *)"hello world"); } else { smbc_setFunctionAuthData(context, get_auth_data_fn); } smbc_setOptionUseKerberos(context, 1); smbc_setOptionFallbackAfterKerberos(context, 1); /* If we've been asked to log to stderr instead of stdout, ... */ if (debug_stderr) { /* ... then set the option to do so */ smbc_setOptionDebugToStderr(context, 1); } /* Initialize the context using the previously specified options */ if (!smbc_init_context(context)) { smbc_free_context(context, 0); printf("Could not initialize smbc context\n"); return 1; } /* Tell the compatibility layer to use this context */ smbc_set_context(context); if (scan) { for (; iterations == -1 || iterations > 0; iterations = (iterations == -1 ? iterations : --iterations)) { snprintf(buf, sizeof(buf), "smb://"); browse(buf, scan, 0); } } else { for (; iterations == -1 || iterations > 0; iterations = (iterations == -1 ? iterations : --iterations)) { fputs("url: ", stdout); p = fgets(buf, sizeof(buf), stdin); if (! p) { break; } if ((p = strchr(buf, '\n')) != NULL) { *p = '\0'; } browse(buf, scan, 0); } } exit(0); }