lib/param: handle non-constant strings properly by passing in a memory context
[sfrench/samba-autobuild/.git] / lib / param / loadparm.c
index 58712a5248294c020f659cfdfbee438cd88d4363..b42855c0fe16890212b04a0abca3bbb386c49903 100644 (file)
 static bool do_parameter(const char *, const char *, void *);
 static bool defaults_saved = false;
 
-#define LOADPARM_EXTRA_GLOBALS \
-       struct parmlist_entry *param_opt;                               \
-       char *realm_original;                                           \
-       char *szConfigFile;                                             \
-       int iminreceivefile;                                            \
-       char *szPrintcapname;                                           \
-       int CupsEncrypt;                                                \
-       int  iPreferredMaster;                                          \
-       char *szLdapMachineSuffix;                                      \
-       char *szLdapUserSuffix;                                         \
-       char *szLdapIdmapSuffix;                                        \
-       char *szLdapGroupSuffix;                                        \
-       char *szUsershareTemplateShare;                                 \
-       char *szIdmapUID;                                               \
-       char *szIdmapGID;                                               \
-       char *szIdmapBackend;                                           \
-       int winbindMaxDomainConnections;                                \
-       int ismb2_max_credits;                                          \
-       char *tls_keyfile;                                              \
-       char *tls_certfile;                                             \
-       char *tls_cafile;                                               \
-       char *tls_crlfile;                                              \
-       char *tls_dhpfile;                                              \
-       char *loglevel;
-
 #include "lib/param/param_global.h"
 
 #define NUMPARAMETERS (sizeof(parm_table) / sizeof(struct parm_struct))
@@ -232,12 +207,13 @@ static struct loadparm_context *global_loadparm_context;
 #define lpcfg_global_service(i) global_loadparm_context->services[i]
 
 #define FN_GLOBAL_STRING(fn_name,var_name) \
- _PUBLIC_ const char *lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) {\
+ _PUBLIC_ char *lpcfg_ ## fn_name(struct loadparm_context *lp_ctx, TALLOC_CTX *ctx) {\
         if (lp_ctx == NULL) return NULL;                               \
         if (lp_ctx->s3_fns) {                                          \
-                smb_panic( __location__ ": " #fn_name " not implemented because it is an allocated and substiuted string"); \
+                SMB_ASSERT(lp_ctx->s3_fns->fn_name);                   \
+                return lp_ctx->s3_fns->fn_name(ctx);                   \
         }                                                              \
-        return lp_ctx->globals->var_name ? lp_string(lp_ctx->globals->var_name) : ""; \
+        return lp_ctx->globals->var_name ? talloc_strdup(ctx, lp_string(lp_ctx->globals->var_name)) : talloc_strdup(ctx, ""); \
 }
 
 #define FN_GLOBAL_CONST_STRING(fn_name,var_name)                               \
@@ -283,13 +259,17 @@ static struct loadparm_context *global_loadparm_context;
  * loadparm_service is shared and lpcfg_service() checks the ->s3_fns
  * hook */
 #define FN_LOCAL_STRING(fn_name,val) \
+ _PUBLIC_ char *lpcfg_ ## fn_name(struct loadparm_service *service, \
+                                       struct loadparm_service *sDefault, TALLOC_CTX *ctx) { \
+        return(talloc_strdup(ctx, lp_string((const char *)((service != NULL && service->val != NULL) ? service->val : sDefault->val)))); \
+ }
+
+#define FN_LOCAL_CONST_STRING(fn_name,val) \
  _PUBLIC_ const char *lpcfg_ ## fn_name(struct loadparm_service *service, \
                                        struct loadparm_service *sDefault) { \
-        return(lp_string((const char *)((service != NULL && service->val != NULL) ? service->val : sDefault->val))); \
+        return((const char *)((service != NULL && service->val != NULL) ? service->val : sDefault->val)); \
  }
 
-#define FN_LOCAL_CONST_STRING(fn_name,val) FN_LOCAL_STRING(fn_name, val)
-
 #define FN_LOCAL_LIST(fn_name,val) \
  _PUBLIC_ const char **lpcfg_ ## fn_name(struct loadparm_service *service, \
                                         struct loadparm_service *sDefault) {\
@@ -320,11 +300,9 @@ static struct loadparm_context *global_loadparm_context;
 
 #include "lib/param/param_functions.c"
 
-/* These functions remain only in lib/param for now */
-FN_GLOBAL_BOOL(readraw, bReadRaw)
-FN_GLOBAL_BOOL(writeraw, bWriteRaw)
-FN_GLOBAL_CONST_STRING(cachedir, szCacheDir)
-FN_GLOBAL_CONST_STRING(statedir, szStateDir)
+/* These functions cannot be auto-generated */
+FN_LOCAL_BOOL(autoloaded, autoloaded)
+FN_GLOBAL_CONST_STRING(dnsdomain, dnsdomain)
 
 /* local prototypes */
 static struct loadparm_service *getservicebyname(struct loadparm_context *lp_ctx,
@@ -747,7 +725,7 @@ bool lpcfg_add_home(struct loadparm_context *lp_ctx,
            || strequal(default_service->path, lp_ctx->sDefault->path)) {
                service->path = talloc_strdup(service, pszHomedir);
        } else {
-               service->path = string_sub_talloc(service, lpcfg_path(default_service, lp_ctx->sDefault), "%H", pszHomedir);
+               service->path = string_sub_talloc(service, lpcfg_path(default_service, lp_ctx->sDefault, service), "%H", pszHomedir);
        }
 
        if (!(*(service->comment))) {
@@ -787,9 +765,9 @@ bool lpcfg_add_printer(struct loadparm_context *lp_ctx,
        lpcfg_string_set(service, &service->comment, comment);
        service->browseable = default_service->browseable;
        /* Printers cannot be read_only. */
-       service->readonly = false;
+       service->read_only = false;
        /* Printer services must be printable. */
-       service->print_ok = true;
+       service->printable = true;
 
        DEBUG(3, ("adding printer service %s\n", pszPrintername));
 
@@ -1011,10 +989,10 @@ static bool lpcfg_service_ok(struct loadparm_service *service)
        /* The [printers] entry MUST be printable. I'm all for flexibility, but */
        /* I can't see why you'd want a non-printable printer service...        */
        if (strwicmp(service->szService, PRINTERS_NAME) == 0) {
-               if (!service->print_ok) {
+               if (!service->printable) {
                        DEBUG(0, ("WARNING: [%s] service MUST be printable!\n",
                               service->szService));
-                       service->print_ok = true;
+                       service->printable = true;
                }
                /* [printers] service must also be non-browsable. */
                if (service->browseable)
@@ -2008,14 +1986,14 @@ struct loadparm_context *loadparm_init(TALLOC_CTX *mem_ctx)
        lp_ctx->sDefault->iMaxPrintJobs = 1000;
        lp_ctx->sDefault->bAvailable = true;
        lp_ctx->sDefault->browseable = true;
-       lp_ctx->sDefault->readonly = true;
+       lp_ctx->sDefault->read_only = true;
        lp_ctx->sDefault->map_archive = true;
        lp_ctx->sDefault->strict_locking = true;
        lp_ctx->sDefault->oplocks = true;
        lp_ctx->sDefault->create_mask = 0744;
        lp_ctx->sDefault->force_create_mode = 0000;
-       lp_ctx->sDefault->dir_mask = 0755;
-       lp_ctx->sDefault->force_dir_mode = 0000;
+       lp_ctx->sDefault->directory_mask = 0755;
+       lp_ctx->sDefault->force_directory_mode = 0000;
 
        DEBUG(3, ("Initialising global parameters\n"));
 
@@ -2216,7 +2194,7 @@ struct loadparm_context *loadparm_init(TALLOC_CTX *mem_ctx)
 
        lpcfg_do_global_parameter(lp_ctx, "enable privileges", "True");
 
-       lpcfg_do_global_parameter(lp_ctx, "smb2 max write", "1048576");
+       lpcfg_do_global_parameter_var(lp_ctx, "smb2 max write", "%u", DEFAULT_SMB2_MAX_WRITE);
 
        lpcfg_do_global_parameter(lp_ctx, "passdb backend", "tdbsam");
 
@@ -2226,7 +2204,7 @@ struct loadparm_context *loadparm_init(TALLOC_CTX *mem_ctx)
 
        lpcfg_do_global_parameter(lp_ctx, "mangled names", "True");
 
-       lpcfg_do_global_parameter(lp_ctx, "smb2 max credits", "8192");
+       lpcfg_do_global_parameter_var(lp_ctx, "smb2 max credits", "%u", DEFAULT_SMB2_MAX_CREDITS);
 
        lpcfg_do_global_parameter(lp_ctx, "ldap ssl", "start tls");
 
@@ -2314,9 +2292,9 @@ struct loadparm_context *loadparm_init(TALLOC_CTX *mem_ctx)
 
        lpcfg_do_global_parameter(lp_ctx, "lpq cache time", "30");
 
-       lpcfg_do_global_parameter(lp_ctx, "smb2 max trans", "1048576");
+       lpcfg_do_global_parameter_var(lp_ctx, "smb2 max trans", "%u", DEFAULT_SMB2_MAX_TRANSACT);
 
-       lpcfg_do_global_parameter(lp_ctx, "smb2 max read", "1048576");
+       lpcfg_do_global_parameter_var(lp_ctx, "smb2 max read", "%u", DEFAULT_SMB2_MAX_READ);
 
        lpcfg_do_global_parameter(lp_ctx, "durable handles", "yes");
 
@@ -2448,13 +2426,21 @@ const char *lp_default_path(void)
 static bool lpcfg_update(struct loadparm_context *lp_ctx)
 {
        struct debug_settings settings;
-       lpcfg_add_auto_services(lp_ctx, lpcfg_auto_services(lp_ctx));
+       TALLOC_CTX *tmp_ctx;
+
+       tmp_ctx = talloc_new(lp_ctx);
+       if (tmp_ctx == NULL) {
+               return false;
+       }
+
+       lpcfg_add_auto_services(lp_ctx, lpcfg_auto_services(lp_ctx, tmp_ctx));
 
        if (!lp_ctx->globals->wins_server_list && lp_ctx->globals->we_are_a_wins_server) {
                lpcfg_do_global_parameter(lp_ctx, "wins server", "127.0.0.1");
        }
 
        if (!lp_ctx->global) {
+               TALLOC_FREE(tmp_ctx);
                return true;
        }
 
@@ -2483,6 +2469,7 @@ static bool lpcfg_update(struct loadparm_context *lp_ctx)
                unsetenv("SOCKET_TESTNONBLOCK");
        }
 
+       TALLOC_FREE(tmp_ctx);
        return true;
 }
 
@@ -2715,27 +2702,27 @@ _PUBLIC_ void reload_charcnv(struct loadparm_context *lp_ctx)
 
 _PUBLIC_ char *lpcfg_tls_keyfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
 {
-       return lpcfg_private_path(mem_ctx, lp_ctx, lp_ctx->globals->tls_keyfile);
+       return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_keyfile(lp_ctx));
 }
 
 _PUBLIC_ char *lpcfg_tls_certfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
 {
-       return lpcfg_private_path(mem_ctx, lp_ctx, lp_ctx->globals->tls_certfile);
+       return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_certfile(lp_ctx));
 }
 
 _PUBLIC_ char *lpcfg_tls_cafile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
 {
-       return lpcfg_private_path(mem_ctx, lp_ctx, lp_ctx->globals->tls_cafile);
+       return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_cafile(lp_ctx));
 }
 
 _PUBLIC_ char *lpcfg_tls_crlfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
 {
-       return lpcfg_private_path(mem_ctx, lp_ctx, lp_ctx->globals->tls_crlfile);
+       return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_crlfile(lp_ctx));
 }
 
 _PUBLIC_ char *lpcfg_tls_dhpfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
 {
-       return lpcfg_private_path(mem_ctx, lp_ctx, lp_ctx->globals->tls_dhpfile);
+       return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_dhpfile(lp_ctx));
 }
 
 struct gensec_settings *lpcfg_gensec_settings(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)