ldb-samba: ldif_read_objectSid avoids VLA
[samba.git] / source3 / libsmb / libsmb_context.c
index 9d473d19b2fe1e37ee9420ba9d58da07e1819ac5..441dec170e2cb0b3584be4f7d9003ddbfe10d336 100644 (file)
@@ -1,10 +1,10 @@
-/* 
+/*
    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);
 }
 
 
@@ -138,6 +140,7 @@ SMBCCTX *
 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);
@@ -148,6 +151,7 @@ smbc_new_context(void)
          */
         context = SMB_MALLOC_P(SMBCCTX);
         if (!context) {
+               TALLOC_FREE(frame);
                 errno = ENOMEM;
                 return NULL;
         }
@@ -156,6 +160,7 @@ smbc_new_context(void)
 
         context->internal = SMB_MALLOC_P(struct SMBC_internal_data);
         if (!context->internal) {
+               TALLOC_FREE(frame);
                 SAFE_FREE(context);
                 errno = ENOMEM;
                 return NULL;
@@ -164,19 +169,31 @@ smbc_new_context(void)
         /* 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);
@@ -192,6 +209,7 @@ smbc_new_context(void)
         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);
@@ -205,12 +223,15 @@ smbc_new_context(void)
         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);
@@ -223,6 +244,7 @@ smbc_new_context(void)
         smbc_setFunctionListPrintJobs(context, SMBC_list_print_jobs_ctx);
         smbc_setFunctionUnlinkPrintJob(context, SMBC_unlink_print_job_ctx);
 
+       TALLOC_FREE(frame);
         return context;
 }
 
@@ -237,19 +259,23 @@ int
 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;
 
@@ -261,8 +287,8 @@ smbc_free_context(SMBCCTX *context,
                                   "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);
@@ -280,18 +306,21 @@ smbc_free_context(SMBCCTX *context,
                         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;
                 }
         }
@@ -304,8 +333,9 @@ smbc_free_context(SMBCCTX *context,
         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);
 
@@ -327,6 +357,7 @@ smbc_free_context(SMBCCTX *context,
                 smb_panic("error unlocking 'initialized_ctx_count'");
        }
 
+       TALLOC_FREE(frame);
         return 0;
 }
 
@@ -349,6 +380,8 @@ smbc_option_set(SMBCCTX *context,
                 const char *s;
         } option_value;
 
+       TALLOC_CTX *frame = talloc_stackframe();
+
         va_start(ap, option_name);
 
         if (strcmp(option_name, "debug_to_stderr") == 0) {
@@ -415,6 +448,7 @@ smbc_option_set(SMBCCTX *context,
         }
 
         va_end(ap);
+       TALLOC_FREE(frame);
 }
 
 
@@ -456,6 +490,8 @@ smbc_option_get(SMBCCTX *context,
         } 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:
@@ -470,7 +506,7 @@ smbc_option_get(SMBCCTX *context,
 
                 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;
                         }
                 }
@@ -545,6 +581,7 @@ SMBCCTX *
 smbc_init_context(SMBCCTX *context)
 {
         int pid;
+        TALLOC_CTX *frame;
 
         if (!context) {
                 errno = EBADF;
@@ -556,11 +593,14 @@ smbc_init_context(SMBCCTX *context)
                 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;
 
@@ -579,6 +619,7 @@ smbc_init_context(SMBCCTX *context)
                 }
 
                 if (!user) {
+                        TALLOC_FREE(frame);
                         errno = ENOMEM;
                         return NULL;
                 }
@@ -587,6 +628,7 @@ smbc_init_context(SMBCCTX *context)
                SAFE_FREE(user);
 
                if (!smbc_getUser(context)) {
+                        TALLOC_FREE(frame);
                         errno = ENOMEM;
                         return NULL;
                 }
@@ -599,16 +641,17 @@ smbc_init_context(SMBCCTX *context)
                  * 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;
                         }
@@ -617,6 +660,7 @@ smbc_init_context(SMBCCTX *context)
                 }
 
                 if (!netbios_name) {
+                        TALLOC_FREE(frame);
                         errno = ENOMEM;
                         return NULL;
                 }
@@ -625,6 +669,7 @@ smbc_init_context(SMBCCTX *context)
                SAFE_FREE(netbios_name);
 
                 if (!smbc_getNetbiosName(context)) {
+                        TALLOC_FREE(frame);
                         errno = ENOMEM;
                         return NULL;
                 }
@@ -633,25 +678,19 @@ smbc_init_context(SMBCCTX *context)
         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;
                }
@@ -677,11 +716,12 @@ smbc_init_context(SMBCCTX *context)
                 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)
 {
@@ -709,9 +749,9 @@ void smbc_set_credentials_with_fallback(SMBCCTX *context,
                                        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) {
 
@@ -730,36 +770,38 @@ void smbc_set_credentials_with_fallback(SMBCCTX *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;
 }