Avoid including libds/common/roles.h in public loadparm.h header.
[obnox/samba/samba-obnox.git] / lib / param / loadparm.c
index a0700a96f02d61c0b161f28b256ad64b23f9f124..e5e3a30b9a83a4e579a8cb713c1903e593c9a20d 100644 (file)
@@ -68,6 +68,7 @@
 #include "libcli/smb/smb_constants.h"
 #include "tdb.h"
 #include "librpc/gen_ndr/nbt.h"
+#include "libds/common/roles.h"
 
 #ifdef HAVE_HTTPCONNECTENCRYPT
 #include <cups/http.h>
@@ -514,16 +515,36 @@ bool lpcfg_parm_bool(struct loadparm_context *lp_ctx,
 }
 
 
+/* this is used to prevent lots of mallocs of size 1 */
+static const char lpcfg_string_emtpy[] = "";
+
+/**
+ Free a string value.
+**/
+void lpcfg_string_free(char **s)
+{
+       if (s == NULL) {
+               return;
+       }
+       if (*s == lpcfg_string_emtpy) {
+               *s = NULL;
+               return;
+       }
+       TALLOC_FREE(*s);
+}
+
 /**
  * Set a string value, deallocating any existing space, and allocing the space
  * for the string
  */
 bool lpcfg_string_set(TALLOC_CTX *mem_ctx, char **dest, const char *src)
 {
-       talloc_free(*dest);
+       lpcfg_string_free(dest);
 
-       if (src == NULL)
-               src = "";
+       if ((src == NULL) || (*src == '\0')) {
+               *dest = discard_const_p(char, lpcfg_string_emtpy);
+               return true;
+       }
 
        *dest = talloc_strdup(mem_ctx, src);
        if ((*dest) == NULL) {
@@ -540,10 +561,12 @@ bool lpcfg_string_set(TALLOC_CTX *mem_ctx, char **dest, const char *src)
  */
 bool lpcfg_string_set_upper(TALLOC_CTX *mem_ctx, char **dest, const char *src)
 {
-       talloc_free(*dest);
+       lpcfg_string_free(dest);
 
-       if (src == NULL)
-               src = "";
+       if ((src == NULL) || (*src == '\0')) {
+               *dest = discard_const_p(char, lpcfg_string_emtpy);
+               return true;
+       }
 
        *dest = strupper_talloc(mem_ctx, src);
        if ((*dest) == NULL) {
@@ -815,9 +838,8 @@ void set_param_opt(TALLOC_CTX *mem_ctx,
                                   overridden */
                                return;
                        }
-                       TALLOC_FREE(opt->value);
                        TALLOC_FREE(opt->list);
-                       opt->value = talloc_strdup(opt, opt_value);
+                       lpcfg_string_set(opt, &opt->value, opt_value);
                        opt->priority = priority;
                        return;
                }
@@ -830,8 +852,10 @@ void set_param_opt(TALLOC_CTX *mem_ctx,
        if (new_opt == NULL) {
                smb_panic("OOM");
        }
-       new_opt->key = talloc_strdup(new_opt, opt_name);
-       new_opt->value = talloc_strdup(new_opt, opt_value);
+       new_opt->key = NULL;
+       lpcfg_string_set(new_opt, &new_opt->key, opt_name);
+       new_opt->value = NULL;
+       lpcfg_string_set(new_opt, &new_opt->value, opt_value);
 
        new_opt->list = NULL;
        new_opt->priority = priority;
@@ -1090,6 +1114,8 @@ bool handle_include(struct loadparm_context *lp_ctx, struct loadparm_service *se
                           const char *pszParmValue, char **ptr)
 {
        char *fname;
+       const char *substitution_variable_substring;
+       char next_char;
 
        if (lp_ctx->s3_fns) {
                return lp_ctx->s3_fns->lp_include(lp_ctx, service, pszParmValue, ptr);
@@ -1104,6 +1130,22 @@ bool handle_include(struct loadparm_context *lp_ctx, struct loadparm_service *se
        if (file_exist(fname))
                return pm_process(fname, do_section, lpcfg_do_parameter, lp_ctx);
 
+       /*
+        * If the file doesn't exist, we check that it isn't due to variable
+        * substitution
+        */
+       substitution_variable_substring = strchr(fname, '%');
+
+       if (substitution_variable_substring != NULL) {
+               next_char = substitution_variable_substring[1];
+               if ((next_char >= 'a' && next_char <= 'z')
+                   || (next_char >= 'A' && next_char <= 'Z')) {
+                       DEBUG(2, ("Tried to load %s but variable substitution in "
+                                "filename, ignoring file.\n", fname));
+                       return true;
+               }
+       }
+
        DEBUG(2, ("Can't find include file %s\n", fname));
 
        return false;
@@ -2054,22 +2096,28 @@ void lpcfg_dump_globals(struct loadparm_context *lp_ctx, FILE *f,
 
        fprintf(f, "# Global parameters\n[global]\n");
 
-       for (i = 0; parm_table[i].label; i++)
-               if (parm_table[i].p_class == P_GLOBAL &&
-                   (i == 0 || (parm_table[i].offset != parm_table[i - 1].offset))) {
-                       if (!show_defaults) {
-                               if (lp_ctx->flags && (lp_ctx->flags[i] & FLAG_DEFAULT)) {
-                                       continue;
-                               }
+       for (i = 0; parm_table[i].label; i++) {
+               if (parm_table[i].p_class != P_GLOBAL) {
+                       continue;
+               }
 
-                               if (is_default(lp_ctx->globals, i)) {
-                                       continue;
-                               }
+               if (parm_table[i].flags & FLAG_SYNONYM) {
+                       continue;
+               }
+
+               if (!show_defaults) {
+                       if (lp_ctx->flags && (lp_ctx->flags[i] & FLAG_DEFAULT)) {
+                               continue;
                        }
 
-                       fprintf(f, "\t%s = ", parm_table[i].label);
-                       lpcfg_print_parameter(&parm_table[i], lpcfg_parm_ptr(lp_ctx, NULL, &parm_table[i]), f);
-                       fprintf(f, "\n");
+                       if (is_default(lp_ctx->globals, i)) {
+                               continue;
+                       }
+               }
+
+               fprintf(f, "\t%s = ", parm_table[i].label);
+               lpcfg_print_parameter(&parm_table[i], lpcfg_parm_ptr(lp_ctx, NULL, &parm_table[i]), f);
+               fprintf(f, "\n");
        }
        if (lp_ctx->globals->param_opt != NULL) {
                for (data = lp_ctx->globals->param_opt; data;
@@ -2097,34 +2145,45 @@ void lpcfg_dump_a_service(struct loadparm_service * pService, struct loadparm_se
                fprintf(f, "\n[%s]\n", pService->szService);
 
        for (i = 0; parm_table[i].label; i++) {
-               if (parm_table[i].p_class == P_LOCAL &&
-                   (*parm_table[i].label != '-') &&
-                   (i == 0 || (parm_table[i].offset != parm_table[i - 1].offset)))
-               {
-                       if (pService == sDefault) {
-                               if (!show_defaults) {
-                                       if (flags && (flags[i] & FLAG_DEFAULT)) {
-                                               continue;
-                                       }
+               if (parm_table[i].p_class != P_LOCAL) {
+                       continue;
+               }
 
-                                       if (is_default(sDefault, i)) {
-                                               continue;
-                                       }
+               if (parm_table[i].flags & FLAG_SYNONYM) {
+                       continue;
+               }
+
+               if (*parm_table[i].label == '-') {
+                       continue;
+               }
+
+               if (pService == sDefault) {
+                       if (!show_defaults) {
+                               if (flags && (flags[i] & FLAG_DEFAULT)) {
+                                       continue;
                                }
-                       } else {
-                               if (lpcfg_equal_parameter(parm_table[i].type,
-                                                         ((char *)pService) +
-                                                         parm_table[i].offset,
-                                                         ((char *)sDefault) +
-                                                         parm_table[i].offset))
+
+                               if (is_default(sDefault, i)) {
                                        continue;
+                               }
+                       }
+               } else {
+                       bool equal;
+
+                       equal = lpcfg_equal_parameter(parm_table[i].type,
+                                                     ((char *)pService) +
+                                                     parm_table[i].offset,
+                                                     ((char *)sDefault) +
+                                                     parm_table[i].offset);
+                       if (equal) {
+                               continue;
                        }
-
-                       fprintf(f, "\t%s = ", parm_table[i].label);
-                       lpcfg_print_parameter(&parm_table[i],
-                                       ((char *)pService) + parm_table[i].offset, f);
-                       fprintf(f, "\n");
                }
+
+               fprintf(f, "\t%s = ", parm_table[i].label);
+               lpcfg_print_parameter(&parm_table[i],
+                               ((char *)pService) + parm_table[i].offset, f);
+               fprintf(f, "\n");
        }
        if (pService->param_opt != NULL) {
                for (data = pService->param_opt; data; data = data->next) {
@@ -2427,13 +2486,16 @@ struct loadparm_context *loadparm_init(TALLOC_CTX *mem_ctx)
                if ((parm_table[i].type == P_STRING ||
                     parm_table[i].type == P_USTRING) &&
                    !(lp_ctx->flags[i] & FLAG_CMDLINE)) {
+                       TALLOC_CTX *parent_mem;
                        char **r;
                        if (parm_table[i].p_class == P_LOCAL) {
+                               parent_mem = lp_ctx->sDefault;
                                r = (char **)(((char *)lp_ctx->sDefault) + parm_table[i].offset);
                        } else {
+                               parent_mem = lp_ctx->globals;
                                r = (char **)(((char *)lp_ctx->globals) + parm_table[i].offset);
                        }
-                       *r = talloc_strdup(lp_ctx, "");
+                       lpcfg_string_set(parent_mem, r, "");
                }
        }
 
@@ -2792,6 +2854,8 @@ struct loadparm_context *loadparm_init(TALLOC_CTX *mem_ctx)
 
        lpcfg_do_global_parameter(lp_ctx, "printjob username", "%U");
 
+       lpcfg_do_global_parameter(lp_ctx, "aio max threads", "100");
+
        /* Allow modules to adjust defaults */
        for (defaults_hook = defaults_hooks; defaults_hook;
                 defaults_hook = defaults_hook->next) {