What to do about debugging in a multi-threaded application?
[ira/wip.git] / source3 / libsmb / libsmb_context.c
index b9b74fe7d245b6076ed72ef1838006317aeaed0a..bacd9075120dd03cd29fec3ecf87291b3df85a73 100644 (file)
 /*
  * Is the logging working / configfile read ? 
  */
-static int SMBC_initialized = 0;
+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
+ */
+static void
+SMBC_module_init(void * punused)
+{
+    bool conf_loaded = False;
+    char *home = NULL;
+    TALLOC_CTX *frame = talloc_stackframe();
+                
+    load_case_tables();
+                
+    setup_logging("libsmbclient", True);
+
+    /* 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);
+            }
+        }
+    }
+                
+    load_interfaces();  /* Load the list of interfaces ... */
+                
+    reopen_logs();  /* Get logging working ... */
+                
+    /*
+     * 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);
+}
+
+
+void
+SMBC_module_terminate(void)
+{
+    gencache_shutdown();
+    secrets_shutdown();
+    gfree_all();
+    SMBC_initialized = false;
+}
 
 
 /*
@@ -42,6 +138,9 @@ 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.
@@ -70,6 +169,7 @@ smbc_new_context(void)
         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);
@@ -94,6 +194,8 @@ smbc_new_context(void)
         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);
@@ -200,7 +302,31 @@ smbc_free_context(SMBCCTX *context,
         smbc_setUser(context, NULL);
         
         DEBUG(3, ("Context %p successfully freed\n", context));
+
+       /* Free any DFS auth context. */
+       TALLOC_FREE(context->internal->auth_info);
+
+       SAFE_FREE(context->internal);
         SAFE_FREE(context);
+
+        /* Protect access to the count of contexts in use */
+       if (SMB_THREAD_LOCK(initialized_ctx_count_mutex) != 0) {
+                smb_panic("error locking 'initialized_ctx_count'");
+       }
+
+       if (initialized_ctx_count) {
+               initialized_ctx_count--;
+       }
+
+       if (initialized_ctx_count == 0) {
+            SMBC_module_terminate();
+       }
+
+        /* Unlock the mutex */
+       if (SMB_THREAD_UNLOCK(initialized_ctx_count_mutex) != 0) {
+                smb_panic("error unlocking 'initialized_ctx_count'");
+       }
+        
         return 0;
 }
 
@@ -409,7 +535,6 @@ smbc_init_context(SMBCCTX *context)
 {
         int pid;
         char *user = NULL;
-        char *home = NULL;
         
         if (!context) {
                 errno = EBADF;
@@ -421,6 +546,16 @@ smbc_init_context(SMBCCTX *context)
                 return NULL;
         }
         
+        if (context->internal->debug_stderr) {
+            /*
+             * Hmmm... Do we want a unique dbf per-thread? For now, we'll just
+             * leave it up to the user. If any one context spefies debug to
+             * stderr then all will be.
+             */
+            dbf = x_stderr;
+            x_setbuf(x_stderr, NULL);
+        }
+                
         if ((!smbc_getFunctionAuthData(context) &&
              !smbc_getFunctionAuthDataWithContext(context)) ||
             smbc_getDebug(context) < 0 ||
@@ -431,88 +566,6 @@ smbc_init_context(SMBCCTX *context)
                 
         }
         
-        if (!SMBC_initialized) {
-                /*
-                 * Do some library-wide intializations the first time we get
-                 * called
-                 */
-                bool conf_loaded = False;
-                TALLOC_CTX *frame = talloc_stackframe();
-                
-                load_case_tables();
-                
-                setup_logging("libsmbclient", True);
-                if (context->internal->debug_stderr) {
-                        dbf = x_stderr;
-                        x_setbuf(x_stderr, NULL);
-                }
-                
-                /* 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);
-                                }
-                        }
-                }
-                
-                load_interfaces();  /* Load the list of interfaces ... */
-                
-                reopen_logs();  /* Get logging working ... */
-                
-                /*
-                 * Block SIGPIPE (from lib/util_sock.c: write())
-                 * It is not needed and should not stop execution
-                 */
-                BlockSignals(True, SIGPIPE);
-                
-                /* Done with one-time initialisation */
-                SMBC_initialized = 1;
-                
-                TALLOC_FREE(frame);
-        }
-        
         if (!smbc_getUser(context)) {
                 /*
                  * FIXME: Is this the best way to get the user info?
@@ -592,11 +645,19 @@ smbc_init_context(SMBCCTX *context)
         if (smbc_getTimeout(context) > 0 && smbc_getTimeout(context) < 1000)
                 smbc_setTimeout(context, 1000);
         
-        /*
-         * FIXME: Should we check the function pointers here?
-         */
-        
         context->internal->initialized = True;
+
+        /* Protect access to the count of contexts in use */
+       if (SMB_THREAD_LOCK(initialized_ctx_count_mutex) != 0) {
+                smb_panic("error locking 'initialized_ctx_count'");
+       }
+
+       initialized_ctx_count++;
+
+        /* Unlock the mutex */
+       if (SMB_THREAD_UNLOCK(initialized_ctx_count_mutex) != 0) {
+                smb_panic("error unlocking 'initialized_ctx_count'");
+       }
         
         return context;
 }
@@ -609,24 +670,70 @@ smbc_version(void)
         return samba_version_string();
 }
 
-
 /*
  * Set the credentials so DFS will work when following referrals.
+ * This function is broken and must be removed. No SMBCCTX arg...
+ * JRA.
  */
+
 void
-smbc_set_credentials(char *workgroup,
-                     char *user,
-                     char *password,
-                     smbc_bool use_kerberos,
-                     char *signing_state)
+smbc_set_credentials(const char *workgroup,
+                       const char *user,
+                       const char *password,
+                       smbc_bool use_kerberos,
+                       const char *signing_state)
 {
-        
-        set_cmdline_auth_info_username(user);
-        set_cmdline_auth_info_password(password);
-        set_cmdline_auth_info_use_kerberos(use_kerberos);
-        if (! set_cmdline_auth_info_signing_state(signing_state)) {
-                DEBUG(0, ("Invalid signing state: %s", signing_state));
-        }
+       d_printf("smbc_set_credentials is obsolete. Replace with smbc_set_credentials_with_fallback().\n");
+}
+
+void smbc_set_credentials_with_fallback(SMBCCTX *context,
+                                       const char *workgroup,
+                                       const char *user,
+                                       const char *password)
+{
+       smbc_bool use_kerberos = false;
+       const char *signing_state = "off";
+       struct user_auth_info *auth_info = user_auth_info_init(NULL);
+
+       if (auth_info) {
+       }
+
+       if (! context ||
+           ! workgroup || ! *workgroup ||
+           ! user || ! *user ||
+           ! password || ! *password) {
+
+               return;
+       }
+
+       auth_info = user_auth_info_init(NULL);
+
+       if (auth_info) {
+               DEBUG(0, ("smbc_set_credentials_with_fallback: allocation fail\n"));
+               return;
+       }
+
+       if (smbc_getOptionUseKerberos(context)) {
+               use_kerberos = True;
+       }
+
+       if (lp_client_signing()) {
+               signing_state = "on";
+       }
+
+       if (lp_client_signing() == Required) {
+               signing_state = "force";
+       }
+
+        set_cmdline_auth_info_username(auth_info, user);
+        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_global_myworkgroup(workgroup);
-        cli_cm_set_credentials();
+
+       TALLOC_FREE(context->internal->auth_info);
+
+        context->internal->auth_info = auth_info;
 }