-/*
+/*
Unix SMB/Netbios implementation.
SMB client library implementation
Copyright (C) Andrew Tridgell 1998
Copyright (C) Richard Sharpe 2000, 2002
Copyright (C) John Terpstra 2000
- Copyright (C) Tom Jansen (Ninja ISD) 2002
+ Copyright (C) Tom Jansen (Ninja ISD) 2002
Copyright (C) Derrell Lipman 2003-2008
Copyright (C) Jeremy Allison 2007, 2008
#include "libsmbclient.h"
#include "libsmb_internal.h"
#include "secrets.h"
-
+#include "../libcli/smb/smbXcli_base.h"
+#include "auth/credentials/credentials.h"
+#include "auth/gensec/gensec.h"
+#include "lib/param/param.h"
+#include "../lib/util/smb_threads.h"
+#include "../lib/util/smb_threads_internal.h"
/*
- * Is the logging working / configfile read ?
+ * Is the logging working / configfile read ?
*/
static bool SMBC_initialized = false;
static unsigned int initialized_ctx_count = 0;
static void *initialized_ctx_count_mutex = NULL;
/*
- * Do some module- and library-wide intializations
+ * Do some module- and library-wide initializations
*/
static void
SMBC_module_init(void * punused)
{
- bool conf_loaded = False;
- char *home = NULL;
- TALLOC_CTX *frame = talloc_stackframe();
-
- load_case_tables_library();
-
- setup_logging("libsmbclient", DEBUG_STDOUT);
-
- /* Here we would open the smb.conf file if needed ... */
-
- lp_set_in_client(True);
-
- home = getenv("HOME");
- if (home) {
- char *conf = NULL;
- if (asprintf(&conf, "%s/.smb/smb.conf", home) > 0) {
- if (lp_load(conf, True, False, False, True)) {
- conf_loaded = True;
- } else {
- DEBUG(5, ("Could not load config file: %s\n",
- conf));
- }
- SAFE_FREE(conf);
- }
- }
-
- if (!conf_loaded) {
- /*
- * Well, if that failed, try the get_dyn_CONFIGFILE
- * Which points to the standard locn, and if that
- * fails, silently ignore it and use the internal
- * defaults ...
- */
-
- if (!lp_load(get_dyn_CONFIGFILE(), True, False, False, False)) {
- DEBUG(5, ("Could not load config file: %s\n",
- get_dyn_CONFIGFILE()));
- } else if (home) {
- char *conf;
- /*
- * We loaded the global config file. Now lets
- * load user-specific modifications to the
- * global config.
- */
- if (asprintf(&conf,
- "%s/.smb/smb.conf.append",
- home) > 0) {
- if (!lp_load(conf, True, False, False, False)) {
- DEBUG(10,
- ("Could not append config file: "
- "%s\n",
- conf));
- }
- SAFE_FREE(conf);
- }
- }
- }
+ bool conf_loaded = False;
+ char *home = NULL;
+ TALLOC_CTX *frame = talloc_stackframe();
+
+ setup_logging("libsmbclient", DEBUG_STDOUT);
+
+ /* Here we would open the smb.conf file if needed ... */
+
+ home = getenv("HOME");
+ if (home) {
+ char *conf = NULL;
+ if (asprintf(&conf, "%s/.smb/smb.conf", home) > 0) {
+ if (lp_load_client(conf)) {
+ conf_loaded = True;
+ } else {
+ DEBUG(5, ("Could not load config file: %s\n",
+ conf));
+ }
+ SAFE_FREE(conf);
+ }
+ }
- load_interfaces(); /* Load the list of interfaces ... */
+ if (!conf_loaded) {
+ /*
+ * Well, if that failed, try the get_dyn_CONFIGFILE
+ * Which points to the standard locn, and if that
+ * fails, silently ignore it and use the internal
+ * defaults ...
+ */
+
+ if (!lp_load_client(get_dyn_CONFIGFILE())) {
+ DEBUG(5, ("Could not load config file: %s\n",
+ get_dyn_CONFIGFILE()));
+ } else if (home) {
+ char *conf;
+ /*
+ * We loaded the global config file. Now lets
+ * load user-specific modifications to the
+ * global config.
+ */
+ if (asprintf(&conf,
+ "%s/.smb/smb.conf.append",
+ home) > 0) {
+ if (!lp_load_client_no_reinit(conf)) {
+ DEBUG(10,
+ ("Could not append config file: "
+ "%s\n",
+ conf));
+ }
+ SAFE_FREE(conf);
+ }
+ }
+ }
- reopen_logs(); /* Get logging working ... */
+ load_interfaces(); /* Load the list of interfaces ... */
- /*
- * Block SIGPIPE (from lib/util_sock.c: write())
- * It is not needed and should not stop execution
- */
- BlockSignals(True, SIGPIPE);
+ reopen_logs(); /* Get logging working ... */
- /* Create the mutex we'll use to protect initialized_ctx_count */
- if (SMB_THREAD_CREATE_MUTEX("initialized_ctx_count_mutex",
- initialized_ctx_count_mutex) != 0) {
- smb_panic("SMBC_module_init: "
- "failed to create 'initialized_ctx_count' mutex");
- }
+ /*
+ * Block SIGPIPE (from lib/util_sock.c: write())
+ * It is not needed and should not stop execution
+ */
+ BlockSignals(True, SIGPIPE);
+ /* Create the mutex we'll use to protect initialized_ctx_count */
+ if (SMB_THREAD_CREATE_MUTEX("initialized_ctx_count_mutex",
+ initialized_ctx_count_mutex) != 0) {
+ smb_panic("SMBC_module_init: "
+ "failed to create 'initialized_ctx_count' mutex");
+ }
- TALLOC_FREE(frame);
+ TALLOC_FREE(frame);
}
static void
SMBC_module_terminate(void)
{
- secrets_shutdown();
- gfree_all();
- SMBC_initialized = false;
+ TALLOC_CTX *frame = talloc_stackframe();
+ secrets_shutdown();
+ gfree_all();
+ SMBC_initialized = false;
+ TALLOC_FREE(frame);
}
smbc_new_context(void)
{
SMBCCTX *context;
+ TALLOC_CTX *frame = talloc_stackframe();
/* The first call to this function should initialize the module */
SMB_THREAD_ONCE(&SMBC_initialized, SMBC_module_init, NULL);
*/
context = SMB_MALLOC_P(SMBCCTX);
if (!context) {
+ TALLOC_FREE(frame);
errno = ENOMEM;
return NULL;
}
context->internal = SMB_MALLOC_P(struct SMBC_internal_data);
if (!context->internal) {
+ TALLOC_FREE(frame);
SAFE_FREE(context);
errno = ENOMEM;
return NULL;
/* Initialize the context and establish reasonable defaults */
ZERO_STRUCTP(context->internal);
+ context->internal->lp_ctx = loadparm_init_s3(NULL,
+ loadparm_s3_helpers());
+ if (context->internal->lp_ctx == NULL) {
+ SAFE_FREE(context->internal);
+ SAFE_FREE(context);
+ TALLOC_FREE(frame);
+ errno = ENOMEM;
+ return NULL;
+ }
+
smbc_setDebug(context, 0);
smbc_setTimeout(context, 20000);
+ smbc_setPort(context, 0);
smbc_setOptionFullTimeNames(context, False);
smbc_setOptionOpenShareMode(context, SMBC_SHAREMODE_DENY_NONE);
- smbc_setOptionSmbEncryptionLevel(context, SMBC_ENCRYPTLEVEL_NONE);
+ smbc_setOptionSmbEncryptionLevel(context, SMBC_ENCRYPTLEVEL_DEFAULT);
smbc_setOptionUseCCache(context, True);
smbc_setOptionCaseSensitive(context, False);
smbc_setOptionBrowseMaxLmbCount(context, 3); /* # LMBs to query */
smbc_setOptionUrlEncodeReaddirEntries(context, False);
smbc_setOptionOneSharePerServer(context, False);
- if (getenv("LIBSMBCLIENT_NO_CCACHE") == NULL) {
- smbc_setOptionUseCCache(context, true);
+ smbc_setOptionPosixExtensions(context, false);
+ if (getenv("LIBSMBCLIENT_NO_CCACHE") != NULL) {
+ smbc_setOptionUseCCache(context, false);
}
smbc_setFunctionAuthData(context, SMBC_get_auth_data);
smbc_setFunctionOpen(context, SMBC_open_ctx);
smbc_setFunctionCreat(context, SMBC_creat_ctx);
smbc_setFunctionRead(context, SMBC_read_ctx);
+ smbc_setFunctionSplice(context, SMBC_splice_ctx);
smbc_setFunctionWrite(context, SMBC_write_ctx);
smbc_setFunctionClose(context, SMBC_close_ctx);
smbc_setFunctionUnlink(context, SMBC_unlink_ctx);
smbc_setFunctionOpendir(context, SMBC_opendir_ctx);
smbc_setFunctionClosedir(context, SMBC_closedir_ctx);
smbc_setFunctionReaddir(context, SMBC_readdir_ctx);
+ smbc_setFunctionReaddirPlus(context, SMBC_readdirplus_ctx);
+ smbc_setFunctionReaddirPlus2(context, SMBC_readdirplus2_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_setFunctionNotify(context, SMBC_notify_ctx);
smbc_setFunctionChmod(context, SMBC_chmod_ctx);
smbc_setFunctionUtimes(context, SMBC_utimes_ctx);
smbc_setFunctionSetxattr(context, SMBC_setxattr_ctx);
smbc_setFunctionListPrintJobs(context, SMBC_list_print_jobs_ctx);
smbc_setFunctionUnlinkPrintJob(context, SMBC_unlink_print_job_ctx);
+ TALLOC_FREE(frame);
return context;
}
smbc_free_context(SMBCCTX *context,
int shutdown_ctx)
{
+ TALLOC_CTX *frame;
if (!context) {
errno = EBADF;
return 1;
}
+ frame = talloc_stackframe();
+
if (shutdown_ctx) {
SMBCFILE * f;
DEBUG(1,("Performing aggressive shutdown.\n"));
f = context->internal->files;
while (f) {
+ SMBCFILE *next = f->next;
smbc_getFunctionClose(context)(context, f);
- f = f->next;
+ f = next;
}
context->internal->files = NULL;
"Nice way shutdown failed.\n"));
s = context->internal->servers;
while (s) {
- DEBUG(1, ("Forced shutdown: %p (fd=%d)\n",
- s, s->cli->fd));
+ DEBUG(1, ("Forced shutdown: %p (cli=%p)\n",
+ s, s->cli));
cli_shutdown(s->cli);
smbc_getFunctionRemoveCachedServer(context)(context,
s);
DEBUG(1, ("Could not purge all servers, "
"free_context failed.\n"));
errno = EBUSY;
+ TALLOC_FREE(frame);
return 1;
}
if (context->internal->servers) {
DEBUG(1, ("Active servers in context, "
"free_context failed.\n"));
errno = EBUSY;
+ TALLOC_FREE(frame);
return 1;
}
if (context->internal->files) {
DEBUG(1, ("Active files in context, "
"free_context failed.\n"));
errno = EBUSY;
+ TALLOC_FREE(frame);
return 1;
}
}
DEBUG(3, ("Context %p successfully freed\n", context));
/* Free any DFS auth context. */
- TALLOC_FREE(context->internal->auth_info);
+ TALLOC_FREE(context->internal->creds);
+ TALLOC_FREE(context->internal->lp_ctx);
SAFE_FREE(context->internal);
SAFE_FREE(context);
smb_panic("error unlocking 'initialized_ctx_count'");
}
+ TALLOC_FREE(frame);
return 0;
}
const char *s;
} option_value;
+ TALLOC_CTX *frame = talloc_stackframe();
+
va_start(ap, option_name);
if (strcmp(option_name, "debug_to_stderr") == 0) {
}
va_end(ap);
+ TALLOC_FREE(frame);
}
} else if (strcmp(option_name, "smb_encrypt_level") == 0) {
switch(smbc_getOptionSmbEncryptionLevel(context))
{
+ case SMBC_ENCRYPTLEVEL_DEFAULT:
+ return discard_const_p(void, "default");
case 0:
return discard_const_p(void, "none");
case 1:
for (s = context->internal->servers; s; s = s->next) {
num_servers++;
- if (s->cli->trans_enc_state == NULL) {
+ if (!cli_state_is_encryption_on(s->cli)) {
return (void *)false;
}
}
smbc_init_context(SMBCCTX *context)
{
int pid;
+ TALLOC_CTX *frame;
if (!context) {
errno = EBADF;
return NULL;
}
+ frame = talloc_stackframe();
+
if ((!smbc_getFunctionAuthData(context) &&
!smbc_getFunctionAuthDataWithContext(context)) ||
smbc_getDebug(context) < 0 ||
smbc_getDebug(context) > 100) {
+ TALLOC_FREE(frame);
errno = EINVAL;
return NULL;
}
if (!user) {
+ TALLOC_FREE(frame);
errno = ENOMEM;
return NULL;
}
SAFE_FREE(user);
if (!smbc_getUser(context)) {
+ TALLOC_FREE(frame);
errno = ENOMEM;
return NULL;
}
* our hostname etc
*/
char *netbios_name;
- if (global_myname()) {
- netbios_name = SMB_STRDUP(global_myname());
+ if (lp_netbios_name()) {
+ netbios_name = SMB_STRDUP(lp_netbios_name());
} else {
/*
* Hmmm, I want to get hostname as well, but I am too
* lazy for the moment
*/
- pid = sys_getpid();
+ pid = getpid();
netbios_name = (char *)SMB_MALLOC(17);
if (!netbios_name) {
+ TALLOC_FREE(frame);
errno = ENOMEM;
return NULL;
}
}
if (!netbios_name) {
+ TALLOC_FREE(frame);
errno = ENOMEM;
return NULL;
}
SAFE_FREE(netbios_name);
if (!smbc_getNetbiosName(context)) {
+ TALLOC_FREE(frame);
errno = ENOMEM;
return NULL;
}
DEBUG(1, ("Using netbios name %s.\n", smbc_getNetbiosName(context)));
if (!smbc_getWorkgroup(context)) {
- char *workgroup;
+ const char *workgroup;
if (lp_workgroup()) {
- workgroup = SMB_STRDUP(lp_workgroup());
- }
- else {
+ workgroup = lp_workgroup();
+ } else {
/* TODO: Think about a decent default workgroup */
- workgroup = SMB_STRDUP("samba");
- }
-
- if (!workgroup) {
- errno = ENOMEM;
- return NULL;
+ workgroup = "samba";
}
smbc_setWorkgroup(context, workgroup);
- SAFE_FREE(workgroup);
if (!smbc_getWorkgroup(context)) {
+ TALLOC_FREE(frame);
errno = ENOMEM;
return NULL;
}
smb_panic("error unlocking 'initialized_ctx_count'");
}
+ TALLOC_FREE(frame);
return context;
}
-/* Return the verion of samba, and thus libsmbclient */
+/* Return the version of samba, and thus libsmbclient */
const char *
smbc_version(void)
{
const char *user,
const char *password)
{
- smbc_bool use_kerberos = false;
- const char *signing_state = "off";
- struct user_auth_info *auth_info = NULL;
+ struct cli_credentials *creds = NULL;
+ enum credentials_use_kerberos kerberos_state =
+ CRED_USE_KERBEROS_DISABLED;
if (! context) {
password = "";
}
- auth_info = user_auth_info_init(NULL);
-
- if (! auth_info) {
+ creds = cli_credentials_init(NULL);
+ if (creds == NULL) {
DEBUG(0, ("smbc_set_credentials_with_fallback: allocation fail\n"));
return;
}
+ cli_credentials_set_conf(creds, context->internal->lp_ctx);
+
if (smbc_getOptionUseKerberos(context)) {
- use_kerberos = True;
- }
+ kerberos_state = CRED_USE_KERBEROS_REQUIRED;
- if (lp_client_signing()) {
- signing_state = "on";
+ if (smbc_getOptionFallbackAfterKerberos(context)) {
+ kerberos_state = CRED_USE_KERBEROS_DESIRED;
+ }
}
- if (lp_client_signing() == Required) {
- signing_state = "force";
+ cli_credentials_set_username(creds, user, CRED_SPECIFIED);
+ cli_credentials_set_password(creds, password, CRED_SPECIFIED);
+ cli_credentials_set_domain(creds, workgroup, CRED_SPECIFIED);
+ cli_credentials_set_kerberos_state(creds,
+ kerberos_state,
+ CRED_SPECIFIED);
+ if (smbc_getOptionUseCCache(context)) {
+ uint32_t gensec_features;
+
+ gensec_features = cli_credentials_get_gensec_features(creds);
+ gensec_features |= GENSEC_FEATURE_NTLM_CCACHE;
+ cli_credentials_set_gensec_features(creds,
+ gensec_features,
+ CRED_SPECIFIED);
}
- set_cmdline_auth_info_username(auth_info, user);
- set_cmdline_auth_info_domain(auth_info, workgroup);
- set_cmdline_auth_info_password(auth_info, password);
- set_cmdline_auth_info_use_kerberos(auth_info, use_kerberos);
- set_cmdline_auth_info_signing_state(auth_info, signing_state);
- set_cmdline_auth_info_fallback_after_kerberos(auth_info,
- smbc_getOptionFallbackAfterKerberos(context));
- set_cmdline_auth_info_use_ccache(
- auth_info, smbc_getOptionUseCCache(context));
-
- TALLOC_FREE(context->internal->auth_info);
-
- context->internal->auth_info = auth_info;
+ TALLOC_FREE(context->internal->creds);
+ context->internal->creds = creds;
}