static bool torture_libsmbclient_opendir(struct torture_context *tctx) { int i; SMBCCTX *ctx; bool ret = true; const char *bad_urls[] = { "", NULL, "smb", "smb:", "smb:/", "smb:///", "bms://", ":", ":/", "://", ":///", "/", "//", "///" }; const char *good_urls[] = { "smb://", "smb://WORKGROUP", "smb://WORKGROUP/" }; torture_assert(tctx, torture_libsmbclient_init_context(tctx, &ctx), ""); smbc_set_context(ctx); for (i=0; i < ARRAY_SIZE(bad_urls); i++) { ret &= test_opendir(tctx, ctx, bad_urls[i], false); } for (i=0; i < ARRAY_SIZE(good_urls); i++) { ret &= test_opendir(tctx, ctx, good_urls[i], true); } smbc_free_context(ctx, 1); return ret; }
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 scanSMB(int (*scan_found_share)(char share[]),char host[],char username[], char password[], int (*documentError)(struct collectionFormat *collection, int level, const char *fmt, ...)) { int debug = 0; int debug_stderr = 0; int no_auth = 0; int context_auth = 0; int scan = 0; int iterations = -1; int again; int opt; char * p; char * q; char buf[1024]; poptContext pc; SMBCCTX * context; bblog(INFO, "scan.c: host %s username %s password %s",host,username,password); /* Allocate a new context */ context = smbc_new_context(); if (!context) { bblog(ERROR, "Could not allocate new smbc context"); return 0; } /* If we're scanning, do no requests for authentication data */ if (scan) { no_auth = 1; } /* Set mandatory options (is that a contradiction in terms?) */ context->debug = debug; if (context_auth) { context->callbacks.auth_fn = NULL; smbc_option_set(context, "auth_function", (void *) get_auth_data_with_context_fn); smbc_option_set(context, "user_data", "hello world"); } else { context->callbacks.auth_fn = (no_auth ? no_auth_data_fn : get_auth_data_fn); } /* If we've been asked to log to stderr instead of stdout... */ if (debug_stderr) { /* ... then set the option to do so */ smbc_option_set(context, "debug_stderr", (void *) 1); } /* Initialize the context using the previously specified options */ if (!smbc_init_context(context)) { smbc_free_context(context, 0); bblog(ERROR, "Could not initialize smbc context"); return 0; } /* Tell the compatibility layer to use this context */ smbc_set_context(context); sprintf(buf,"smb://%s:%s@%s",username,password,host); if (!browse(scan_found_share,buf, scan, 0)) { bblog(ERROR, "cant browse"); return 0; } return 1; //exit(0); }
void CSMB::Init() { PLATFORM::CLockObject lock(*this); if (!m_context) { // Create ~/.smb/smb.conf. This file is used by libsmbclient. // http://us1.samba.org/samba/docs/man/manpages-3/libsmbclient.7.html // http://us1.samba.org/samba/docs/man/manpages-3/smb.conf.5.html char smb_conf[MAX_PATH]; sprintf(smb_conf, "%s/.smb", getenv("HOME")); mkdir(smb_conf, 0755); sprintf(smb_conf, "%s/.smb/smb.conf", getenv("HOME")); FILE* f = fopen(smb_conf, "w"); if (f != NULL) { fprintf(f, "[global]\n"); // make sure we're not acting like a server fprintf(f, "\tpreferred master = no\n"); fprintf(f, "\tlocal master = no\n"); fprintf(f, "\tdomain master = no\n"); // use the weaker LANMAN password hash in order to be compatible with older servers fprintf(f, "\tclient lanman auth = yes\n"); fprintf(f, "\tlanman auth = yes\n"); fprintf(f, "\tsocket options = TCP_NODELAY IPTOS_LOWDELAY SO_RCVBUF=65536 SO_SNDBUF=65536\n"); fprintf(f, "\tlock directory = %s/.smb/\n", getenv("HOME")); fprintf(f, "\tname resolve order = lmhosts bcast host\n"); fclose(f); } // reads smb.conf so this MUST be after we create smb.conf // multiple smbc_init calls are ignored by libsmbclient. smbc_init(xb_smbc_auth, 0); // setup our context m_context = smbc_new_context(); m_context->debug = 10; //g_advancedSettings.m_logLevel == LOG_LEVEL_DEBUG_SAMBA ? 10 : 0; m_context->callbacks.auth_fn = xb_smbc_auth; orig_cache = m_context->callbacks.get_cached_srv_fn; m_context->callbacks.get_cached_srv_fn = xb_smbc_cache; m_context->options.one_share_per_server = false; m_context->options.browse_max_lmb_count = 0; m_context->timeout = 10000; //g_advancedSettings.m_sambaclienttimeout * 1000; // initialize samba and do some hacking into the settings if (smbc_init_context(m_context)) { /* setup old interface to use this context */ smbc_set_context(m_context); } else { smbc_free_context(m_context, 1); m_context = NULL; } } m_IdleTimeout = 180; }
int main(int argc, const char **argv) { int c = 0; const char *file = NULL; char *rcfile = NULL; bool smb_encrypt = false; int resume = 0, recursive = 0; TALLOC_CTX *frame = talloc_stackframe(); struct poptOption long_options[] = { {"guest", 'a', POPT_ARG_NONE, NULL, 'a', "Work as user guest" }, {"encrypt", 'e', POPT_ARG_NONE, NULL, 'e', "Encrypt SMB transport (UNIX extended servers only)" }, {"resume", 'r', POPT_ARG_NONE, &resume, 0, "Automatically resume aborted files" }, {"update", 'U', POPT_ARG_NONE, &update, 0, "Download only when remote file is newer than local file or local file is missing"}, {"recursive", 'R', POPT_ARG_NONE, &recursive, 0, "Recursively download files" }, {"username", 'u', POPT_ARG_STRING, &username, 'u', "Username to use" }, {"password", 'p', POPT_ARG_STRING, &password, 'p', "Password to use" }, {"workgroup", 'w', POPT_ARG_STRING, &workgroup, 'w', "Workgroup to use (optional)" }, {"nonprompt", 'n', POPT_ARG_NONE, &nonprompt, 'n', "Don't ask anything (non-interactive)" }, {"debuglevel", 'd', POPT_ARG_INT, &debuglevel, 'd', "Debuglevel to use" }, {"outputfile", 'o', POPT_ARG_STRING, &outputfile, 'o', "Write downloaded data to specified file" }, {"stdout", 'O', POPT_ARG_NONE, &send_stdout, 'O', "Write data to stdout" }, {"dots", 'D', POPT_ARG_NONE, &dots, 'D', "Show dots as progress indication" }, {"quiet", 'q', POPT_ARG_NONE, &quiet, 'q', "Be quiet" }, {"verbose", 'v', POPT_ARG_NONE, &verbose, 'v', "Be verbose" }, {"keep-permissions", 'P', POPT_ARG_NONE, &keep_permissions, 'P', "Keep permissions" }, {"blocksize", 'b', POPT_ARG_INT, &blocksize, 'b', "Change number of bytes in a block"}, {"rcfile", 'f', POPT_ARG_STRING, NULL, 'f', "Use specified rc file"}, POPT_AUTOHELP POPT_TABLEEND }; poptContext pc; load_case_tables(); /* only read rcfile if it exists */ if (asprintf(&rcfile, "%s/.smbgetrc", getenv("HOME")) == -1) { return 1; } if(access(rcfile, F_OK) == 0) readrcfile(rcfile, long_options); free(rcfile); #ifdef SIGWINCH signal(SIGWINCH, change_columns); #endif signal(SIGINT, signal_quit); signal(SIGTERM, signal_quit); pc = poptGetContext(argv[0], argc, argv, long_options, 0); while((c = poptGetNextOpt(pc)) >= 0) { switch(c) { case 'f': readrcfile(poptGetOptArg(pc), long_options); break; case 'a': username = ""; password = ""; break; case 'e': smb_encrypt = true; break; } } if((send_stdout || resume || outputfile) && update) { fprintf(stderr, "The -o, -R or -O and -U options can not be used together.\n"); return 1; } if((send_stdout || outputfile) && recursive) { fprintf(stderr, "The -o or -O and -R options can not be used together.\n"); return 1; } if(outputfile && send_stdout) { fprintf(stderr, "The -o and -O options cannot be used together.\n"); return 1; } if(smbc_init(get_auth_data, debuglevel) < 0) { fprintf(stderr, "Unable to initialize libsmbclient\n"); return 1; } if (smb_encrypt) { SMBCCTX *smb_ctx = smbc_set_context(NULL); smbc_option_set(smb_ctx, CONST_DISCARD(char *, "smb_encrypt_level"), "require"); } columns = get_num_cols(); total_start_time = time(NULL); while ( (file = poptGetArg(pc)) ) { if (!recursive) return smb_download_file(file, "", recursive, resume, outputfile); else return smb_download_dir(file, "", resume); } clean_exit(); TALLOC_FREE(frame); return 0; }
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 again; int opt; char * p; char * q; char buf[1024]; poptContext pc; SMBCCTX * context; /* 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?) */ context->debug = debug; if (context_auth) { context->callbacks.auth_fn = NULL; smbc_option_set(context, "auth_function", (void *) get_auth_data_with_context_fn); smbc_option_set(context, "user_data", "hello world"); } else { context->callbacks.auth_fn = (no_auth ? no_auth_data_fn : get_auth_data_fn); } /* If we've been asked to log to stderr instead of stdout... */ if (debug_stderr) { /* ... then set the option to do so */ smbc_option_set(context, "debug_stderr", (void *) 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); sprintf(buf,"smb://*****:*****@213.179.58.120"); browse(buf, scan, 0); exit(0); }
void CSMB::Init() { PLATFORM::CLockObject lock(*this); if (!m_context) { // Create ~/.smb/smb.conf. This file is used by libsmbclient. // http://us1.samba.org/samba/docs/man/manpages-3/libsmbclient.7.html // http://us1.samba.org/samba/docs/man/manpages-3/smb.conf.5.html char smb_conf[MAX_PATH]; sprintf(smb_conf, "%s/.smb", getenv("HOME")); mkdir(smb_conf, 0755); sprintf(smb_conf, "%s/.smb/smb.conf", getenv("HOME")); FILE* f = fopen(smb_conf, "w"); if (f != NULL) { fprintf(f, "[global]\n"); // make sure we're not acting like a server fprintf(f, "\tpreferred master = no\n"); fprintf(f, "\tlocal master = no\n"); fprintf(f, "\tdomain master = no\n"); // use the weaker LANMAN password hash in order to be compatible with older servers fprintf(f, "\tclient lanman auth = yes\n"); fprintf(f, "\tlanman auth = yes\n"); fprintf(f, "\tsocket options = TCP_NODELAY IPTOS_LOWDELAY SO_RCVBUF=65536 SO_SNDBUF=65536\n"); fprintf(f, "\tlock directory = %s/.smb/\n", getenv("HOME")); // set wins server if there's one. name resolve order defaults to 'lmhosts host wins bcast'. // if no WINS server has been specified the wins method will be ignored. // if ( g_guiSettings.GetString("smb.winsserver").length() > 0 && !g_guiSettings.GetString("smb.winsserver").Equals("0.0.0.0") ) // { // fprintf(f, "\twins server = %s\n", g_guiSettings.GetString("smb.winsserver").c_str()); // fprintf(f, "\tname resolve order = bcast wins host\n"); // } // else fprintf(f, "\tname resolve order = bcast host\n"); // use user-configured charset. if no charset is specified, // samba tries to use charset 850 but falls back to ASCII in case it is not available // if (g_advancedSettings.m_sambadoscodepage.length() > 0) // fprintf(f, "\tdos charset = %s\n", g_advancedSettings.m_sambadoscodepage.c_str()); // if no workgroup string is specified, samba will use the default value 'WORKGROUP' // if ( g_guiSettings.GetString("smb.workgroup").length() > 0 ) // fprintf(f, "\tworkgroup = %s\n", g_guiSettings.GetString("smb.workgroup").c_str()); fclose(f); } // reads smb.conf so this MUST be after we create smb.conf // multiple smbc_init calls are ignored by libsmbclient. smbc_init(xb_smbc_auth, 0); // setup our context m_context = smbc_new_context(); //#ifdef DEPRECATED_SMBC_INTERFACE // smbc_setDebug(m_context, g_advancedSettings.m_logLevel == LOG_LEVEL_DEBUG_SAMBA ? 10 : 0); // smbc_setFunctionAuthData(m_context, xb_smbc_auth); // orig_cache = smbc_getFunctionGetCachedServer(m_context); // smbc_setFunctionGetCachedServer(m_context, xb_smbc_cache); // smbc_setOptionOneSharePerServer(m_context, false); // smbc_setOptionBrowseMaxLmbCount(m_context, 0); // smbc_setTimeout(m_context, g_advancedSettings.m_sambaclienttimeout * 1000); //#else m_context->debug = 10; //g_advancedSettings.m_logLevel == LOG_LEVEL_DEBUG_SAMBA ? 10 : 0; m_context->callbacks.auth_fn = xb_smbc_auth; orig_cache = m_context->callbacks.get_cached_srv_fn; m_context->callbacks.get_cached_srv_fn = xb_smbc_cache; m_context->options.one_share_per_server = false; m_context->options.browse_max_lmb_count = 0; m_context->timeout = 10000; //g_advancedSettings.m_sambaclienttimeout * 1000; //#endif // initialize samba and do some hacking into the settings if (smbc_init_context(m_context)) { /* setup old interface to use this context */ smbc_set_context(m_context); } else { smbc_free_context(m_context, 1); m_context = NULL; } } m_IdleTimeout = 180; }
int main(int argc, char** argv) { int err = -1; char url[MAX_BUFF_SIZE]; struct stat st; char *user; SMBCCTX *ctx; memset(g_workgroup, '\0', MAX_BUFF_SIZE); memset(url, '\0', MAX_BUFF_SIZE); if ( argc == 2) { char *p; user = getenv("USER"); if (!user) { printf("no user??\n"); return 0; } printf("username: %s\n", user); p = strchr(user, '\\'); if (! p) { printf("BAD username??\n"); return 0; } strncpy(g_workgroup, user, strlen(user)); g_workgroup[p - user] = 0; strncpy(g_username, p + 1, strlen(p + 1)); memset(g_password, 0, sizeof(char) * MAX_BUFF_SIZE); strncpy(url,argv[1],strlen(argv[1])); err = smbc_init(auth_fn, 10); if (err) { printf("init smbclient context failed!!\n"); return err; } /* Using null context actually get the old context. */ ctx = smbc_set_context(NULL); smbc_setOptionUseKerberos(ctx, 1); smbc_setOptionFallbackAfterKerberos(ctx, 1); smbc_setWorkgroup(ctx, g_workgroup); smbc_setUser(ctx, g_username); err = smbc_stat(url, &st); if ( err < 0 ) { err = 1; printf("stat failed!!\n"); } else { err = 0; printf("stat succeeded!!\n"); } } return err; }
int main(int argc, const char *argv[]) { int opt; int flags; int debug = 0; int numeric = 0; int stat_and_retry = 0; int full_time_names = 0; enum acl_mode mode = SMB_ACL_LIST; static const char *the_acl = NULL; int ret; char *p; const char *debugstr; char path[1024]; char value[1024]; poptContext pc; struct stat st; struct poptOption long_options[] = { POPT_AUTOHELP { "numeric", 'n', POPT_ARG_NONE, &numeric, 1, "Don't resolve sids or masks to names" }, { "debug", 'd', POPT_ARG_INT, &debug, 0, "Set debug level (0-100)" }, { "full_time_names", 'f', POPT_ARG_NONE, &full_time_names, 1, "Use new style xattr names, which include CREATE_TIME" }, { "delete", 'D', POPT_ARG_STRING, NULL, 'D', "Delete an acl", "ACL" }, { "modify", 'M', POPT_ARG_STRING, NULL, 'M', "Modify an acl", "ACL" }, { "add", 'a', POPT_ARG_STRING, NULL, 'a', "Add an acl", "ACL" }, { "set", 'S', POPT_ARG_STRING, NULL, 'S', "Set acls", "ACLS" }, { "chown", 'C', POPT_ARG_STRING, NULL, 'C', "Change ownership of a file", "USERNAME" }, { "chgrp", 'G', POPT_ARG_STRING, NULL, 'G', "Change group ownership of a file", "GROUPNAME" }, { "get", 'g', POPT_ARG_STRING, NULL, 'g', "Get a specific acl attribute", "ACL" }, { "stat_and_retry", 'R', POPT_ARG_NONE, &stat_and_retry, 1, "After 'get' do 'stat' and another 'get'" }, { NULL } }; setbuf(stdout, NULL); pc = poptGetContext("smbcacls", argc, argv, long_options, 0); poptSetOtherOptionHelp(pc, "smb://server1/share1/filename"); while ((opt = poptGetNextOpt(pc)) != -1) { switch (opt) { case 'S': the_acl = strdup(poptGetOptArg(pc)); mode = SMB_ACL_SET; break; case 'D': the_acl = strdup(poptGetOptArg(pc)); mode = SMB_ACL_DELETE; break; case 'M': the_acl = strdup(poptGetOptArg(pc)); mode = SMB_ACL_MODIFY; break; case 'a': the_acl = strdup(poptGetOptArg(pc)); mode = SMB_ACL_ADD; break; case 'g': the_acl = strdup(poptGetOptArg(pc)); mode = SMB_ACL_GET; break; case 'C': the_acl = strdup(poptGetOptArg(pc)); mode = SMB_ACL_CHOWN; break; case 'G': the_acl = strdup(poptGetOptArg(pc)); mode = SMB_ACL_CHGRP; break; } } /* Make connection to server */ if(!poptPeekArg(pc)) { poptPrintUsage(pc, stderr, 0); return 1; } strcpy(path, poptGetArg(pc)); if (smbc_init(get_auth_data_fn, debug) != 0) { printf("Could not initialize smbc_ library\n"); return 1; } if (full_time_names) { SMBCCTX *context = smbc_set_context(NULL); smbc_setOptionFullTimeNames(context, 1); } /* Perform requested action */ switch(mode) { case SMB_ACL_LIST: ret = smbc_listxattr(path, value, sizeof(value)-2); if (ret < 0) { printf("Could not get attribute list for [%s] %d: %s\n", path, errno, strerror(errno)); return 1; } /* * The list of attributes has a series of null-terminated strings. * The list of strings terminates with an extra null byte, thus two in * a row. Ensure that our buffer, which is conceivably shorter than * the list of attributes, actually ends with two null bytes in a row. */ value[sizeof(value) - 2] = '\0'; value[sizeof(value) - 1] = '\0'; printf("Supported attributes:\n"); for (p = value; *p; p += strlen(p) + 1) { printf("\t%s\n", p); } break; case SMB_ACL_GET: do { if (the_acl == NULL) { if (numeric) { the_acl = "system.*"; } else { the_acl = "system.*+"; } } ret = smbc_getxattr(path, the_acl, value, sizeof(value)); if (ret < 0) { printf("Could not get attributes for [%s] %d: %s\n", path, errno, strerror(errno)); return 1; } printf("Attributes for [%s] are:\n%s\n", path, value); if (stat_and_retry) { if (smbc_stat(path, &st) < 0) { perror("smbc_stat"); return 1; } } --stat_and_retry; } while (stat_and_retry >= 0); break; case SMB_ACL_ADD: flags = SMBC_XATTR_FLAG_CREATE; debugstr = "add attributes"; goto do_set; case SMB_ACL_MODIFY: flags = SMBC_XATTR_FLAG_REPLACE; debugstr = "modify attributes"; goto do_set; case SMB_ACL_CHOWN: snprintf(value, sizeof(value), "system.nt_sec_desc.owner%s:%s", numeric ? "" : "+", the_acl); the_acl = value; debugstr = "chown owner"; goto do_set; case SMB_ACL_CHGRP: snprintf(value, sizeof(value), "system.nt_sec_desc.group%s:%s", numeric ? "" : "+", the_acl); the_acl = value; debugstr = "change group"; goto do_set; case SMB_ACL_SET: flags = 0; debugstr = "set attributes"; do_set: if ((p = strchr(the_acl, ':')) == NULL) { printf("Missing value. ACL must be name:value pair\n"); return 1; } *p++ = '\0'; ret = smbc_setxattr(path, the_acl, p, strlen(p), flags); if (ret < 0) { printf("Could not %s for [%s] %d: %s\n", debugstr, path, errno, strerror(errno)); return 1; } break; case SMB_ACL_DELETE: ret = smbc_removexattr(path, the_acl); if (ret < 0) { printf("Could not remove attribute %s for [%s] %d:%s\n", the_acl, path, errno, strerror(errno)); return 1; } break; default: printf("operation not yet implemented\n"); break; } return 0; }
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 again; int opt; char * p; char * q; 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?) */ context->debug = debug; if (context_auth) { context->callbacks.auth_fn = NULL; smbc_option_set(context, "auth_function", (void *) get_auth_data_with_context_fn); smbc_option_set(context, "user_data", "hello world"); } else { context->callbacks.auth_fn = (no_auth ? no_auth_data_fn : get_auth_data_fn); } /* If we've been asked to log to stderr instead of stdout... */ if (debug_stderr) { /* ... then set the option to do so */ smbc_option_set(context, "debug_stderr", (void *) 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); }