int main() { SMBCCTX* smbCTX = smbc_new_context(); if ( !smbCTX ) { return 1; } if ( !smbc_init_context( smbCTX ) ) { return 1; } smbc_set_context( smbCTX ); smbc_setFunctionAuthData( smbCTX, smbcAuth ); smbc_setOptionUrlEncodeReaddirEntries( smbCTX, 0 ); return 0; }
static void InitSmb() { MutexLock lock( &smbMutex ); if ( smbCTX ) { return; } smbCTX = smbc_new_context(); if ( !smbCTX ) { throw_syserr( 0, "smbclient can`t allocate context" ); } if ( !smbc_init_context( smbCTX ) ) { smbc_free_context( smbCTX, 0 ); smbCTX = 0; throw_syserr( 0, "smbclient can`t init context" ); } smbc_set_context( smbCTX ); smbc_setFunctionAuthData( smbCTX, smbcAuth ); smbc_setOptionUrlEncodeReaddirEntries( smbCTX, 0 ); }
int smbc_init(smbc_get_auth_data_fn fn, int debug) { if (!smbc_compat_initialized) { statcont = smbc_new_context(); if (!statcont) return -1; smbc_setDebug(statcont, debug); smbc_setFunctionAuthData(statcont, fn); if (!smbc_init_context(statcont)) { smbc_free_context(statcont, False); return -1; } smbc_compat_initialized = 1; return 0; } return 0; }
static SMBCCTX* create_context(void) { SMBCCTX *ctx; extern int gTimeout; extern int gPassIsHash; //Create the Samba context struct , if it didn't work quit. if((ctx = smbc_new_context()) == NULL) return NULL; #ifdef DEBUG //Set the options for our context. a //If its enabled at the command line, turn on Samba library debugging smbc_setDebug(ctx, 1); //We want to log our errors to STDERR instead of STDOUT smbc_setOptionDebugToStderr(ctx, 1); #endif //Set the function that the Samba library will call when it needs //to authenticate smbc_setFunctionAuthData(ctx, auth_fn); //Set the timeout, we get the command line option as seconds and the //function takes milliseconds. smbc_setTimeout(ctx, 200); //If we got a hash at the command line, let the context know we're //giving it a hash smbc_setOptionUseNTHash(ctx, gPassIsHash); //Initialize our context with the options that we have set or null on fail. if(smbc_init_context(ctx) == NULL) { smbc_free_context(ctx, 1); return NULL; } return ctx; }
/* * Using its smb_uri components and cached_gpt_version inputs, this function * does several things: * - it downloads the GPT_INI file to GPO_CACHE * - it parses the sysvol_gpt_version field from the GPT_INI file * - if the sysvol_gpt_version is greater than the cached_gpt_version * - it downloads the policy file to GPO_CACHE * - else * - it doesn't retrieve the policy file * - in this case, the backend will use the existing policy file in GPO_CACHE * - it returns the sysvol_gpt_version in the _sysvol_gpt_version output param * * Note that if the cached_gpt_version sent by the backend is -1 (to indicate * that no gpt_version has been set in the cache for the corresponding gpo_guid), * then the parsed sysvol_gpt_version (which must be at least 0) will be greater * than the cached_gpt_version, thereby triggering a fresh download. * * Note that the backend will later do the following: * - backend will save the the sysvol_gpt_version to sysdb cache * - backend will read the policy file from the GPO_CACHE */ static errno_t perform_smb_operations(int cached_gpt_version, const char *smb_server, const char *smb_share, const char *smb_path, const char *smb_cse_suffix, int *_sysvol_gpt_version) { SMBCCTX *smbc_ctx; int ret; int sysvol_gpt_version; smbc_ctx = smbc_new_context(); if (smbc_ctx == NULL) { DEBUG(SSSDBG_CRIT_FAILURE, "Could not allocate new smbc context\n"); ret = ENOMEM; goto done; } smbc_setFunctionAuthData(smbc_ctx, sssd_krb_get_auth_data_fn); smbc_setOptionUseKerberos(smbc_ctx, 1); /* Initialize the context using the previously specified options */ if (smbc_init_context(smbc_ctx) == NULL) { DEBUG(SSSDBG_CRIT_FAILURE, "Could not initialize smbc context\n"); ret = ENOMEM; goto done; } /* download ini file */ ret = copy_smb_file_to_gpo_cache(smbc_ctx, smb_server, smb_share, smb_path, GPT_INI); if (ret != EOK) { DEBUG(SSSDBG_CRIT_FAILURE, "copy_smb_file_to_gpo_cache failed [%d][%s]\n", ret, strerror(ret)); goto done; } ret = ad_gpo_parse_ini_file(smb_path, &sysvol_gpt_version); if (ret != EOK) { DEBUG(SSSDBG_CRIT_FAILURE, "Cannot parse ini file: [%d][%s]\n", ret, strerror(ret)); goto done; } DEBUG(SSSDBG_TRACE_FUNC, "sysvol_gpt_version: %d\n", sysvol_gpt_version); if (sysvol_gpt_version > cached_gpt_version) { /* download policy file */ ret = copy_smb_file_to_gpo_cache(smbc_ctx, smb_server, smb_share, smb_path, smb_cse_suffix); if (ret != EOK) { DEBUG(SSSDBG_CRIT_FAILURE, "copy_smb_file_to_gpo_cache failed [%d][%s]\n", ret, strerror(ret)); goto done; } } *_sysvol_gpt_version = sysvol_gpt_version; done: smbc_free_context(smbc_ctx, 0); return ret; }
/* * 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); }