#include <sys_defs.h>
#include <string.h>
#include <msg.h>
#include <mymalloc.h>
#include <htable.h>
#include <mac_expand.h>
#include <stringops.h>
#include <mail_conf.h>
#include <mail_params.h>
#include <postconf.h>
static HTABLE *pcf_rest_class_table;
#define STR(x) vstring_str(x)
#define NO_SCAN_RESULT ((VSTRING *) 0)
#define NO_SCAN_FILTER ((char *) 0)
#define SCAN_USER_PARAMETER_VALUE(value, class, scope) do { \
PCF_PARAM_CTX _ctx; \
_ctx.local_scope = (scope); \
_ctx.param_class = (class); \
(void) mac_expand(NO_SCAN_RESULT, (value), MAC_EXP_FLAG_SCAN, \
NO_SCAN_FILTER, pcf_flag_user_parameter_wrapper, (void *) &_ctx); \
} while (0)
static const char *pcf_convert_user_parameter(void *unused_ptr)
{
return ("");
}
static const char *pcf_flag_user_parameter(const char *mac_name,
int param_class,
PCF_MASTER_ENT *local_scope)
{
const char *source = local_scope ? MASTER_CONF_FILE : MAIN_CONF_FILE;
int user_supplied = 0;
if (local_scope && dict_get(local_scope->all_params, mac_name)) {
user_supplied = 1;
if (PCF_PARAM_TABLE_LOCATE(local_scope->valid_names, mac_name) == 0) {
PCF_PARAM_TABLE_ENTER(local_scope->valid_names, mac_name,
param_class, PCF_PARAM_NO_DATA,
pcf_convert_user_parameter);
if (msg_verbose)
msg_info("$%s in %s:%s validates %s=value in %s:%s",
mac_name, MASTER_CONF_FILE,
local_scope->name_space,
mac_name, MASTER_CONF_FILE,
local_scope->name_space);
}
} else if (mail_conf_lookup(mac_name) != 0) {
user_supplied = 1;
if (PCF_PARAM_TABLE_LOCATE(pcf_param_table, mac_name) == 0) {
PCF_PARAM_TABLE_ENTER(pcf_param_table, mac_name, param_class,
PCF_PARAM_NO_DATA, pcf_convert_user_parameter);
if (msg_verbose) {
if (local_scope)
msg_info("$%s in %s:%s validates %s=value in %s",
mac_name, MASTER_CONF_FILE,
local_scope->name_space,
mac_name, MAIN_CONF_FILE);
else
msg_info("$%s in %s validates %s=value in %s",
mac_name, MAIN_CONF_FILE,
mac_name, MAIN_CONF_FILE);
}
}
}
if (local_scope == 0) {
for (local_scope = pcf_master_table; local_scope->argv; local_scope++) {
if (local_scope->all_params != 0
&& dict_get(local_scope->all_params, mac_name) != 0) {
user_supplied = 1;
if (PCF_PARAM_TABLE_LOCATE(local_scope->valid_names, mac_name) == 0) {
PCF_PARAM_TABLE_ENTER(local_scope->valid_names, mac_name,
param_class, PCF_PARAM_NO_DATA,
pcf_convert_user_parameter);
if (msg_verbose)
msg_info("$%s in %s validates %s=value in %s:%s",
mac_name, MAIN_CONF_FILE,
mac_name, MASTER_CONF_FILE,
local_scope->name_space);
}
}
}
}
if (user_supplied == 0 && (param_class & PCF_PARAM_FLAG_DBMS) == 0
&& PCF_PARAM_TABLE_LOCATE(pcf_param_table, mac_name) == 0)
msg_warn("%s/%s: undefined parameter: %s",
var_config_dir, source, mac_name);
return (0);
}
static const char *pcf_flag_user_parameter_wrapper(const char *mac_name,
int unused_mode,
void *context)
{
PCF_PARAM_CTX *ctx = (PCF_PARAM_CTX *) context;
return (pcf_flag_user_parameter(mac_name, ctx->param_class, ctx->local_scope));
}
static const char *pcf_lookup_eval(const char *dict_name, const char *name)
{
const char *value;
#define RECURSIVE 1
if ((value = dict_lookup(dict_name, name)) != 0)
value = dict_eval(dict_name, value, RECURSIVE);
return (value);
}
static void pcf_scan_user_parameter_namespace(const char *dict_name,
PCF_MASTER_ENT *local_scope)
{
const char *myname = "pcf_scan_user_parameter_namespace";
const char *class_list;
char *saved_class_list;
char *cp;
DICT *dict;
char *param_name;
int how;
const char *cparam_name;
const char *cparam_value;
PCF_PARAM_NODE *node;
const char *source = local_scope ? MASTER_CONF_FILE : MAIN_CONF_FILE;
if ((class_list = pcf_lookup_eval(dict_name, VAR_REST_CLASSES)) != 0) {
cp = saved_class_list = mystrdup(class_list);
while ((param_name = mystrtok(&cp, CHARS_COMMA_SP)) != 0) {
if (local_scope == 0
&& htable_locate(pcf_rest_class_table, param_name) == 0)
htable_enter(pcf_rest_class_table, param_name, "");
pcf_flag_user_parameter(param_name, PCF_PARAM_FLAG_USER, local_scope);
}
myfree(saved_class_list);
}
if ((dict = dict_handle(dict_name)) == 0)
msg_panic("%s: parameter dictionary %s not found",
myname, dict_name);
if (dict->sequence == 0)
msg_panic("%s: parameter dictionary %s has no iterator",
myname, dict_name);
for (how = DICT_SEQ_FUN_FIRST;
dict->sequence(dict, how, &cparam_name, &cparam_value) == 0;
how = DICT_SEQ_FUN_NEXT) {
if (local_scope != 0
&& PCF_PARAM_TABLE_LOCATE(local_scope->valid_names, cparam_name) == 0
&& htable_locate(pcf_rest_class_table, cparam_name) != 0)
PCF_PARAM_TABLE_ENTER(local_scope->valid_names, cparam_name,
PCF_PARAM_FLAG_USER, PCF_PARAM_NO_DATA,
pcf_convert_user_parameter);
if ((node = PCF_PARAM_TABLE_FIND(pcf_param_table, cparam_name)) != 0) {
if (PCF_READONLY_PARAMETER(node)) {
msg_warn("%s/%s: read-only parameter assignment: %s=%s",
var_config_dir, source, cparam_name, cparam_value);
if (dict_del(dict, cparam_name) != 0)
msg_panic("%s: can't delete %s/%s parameter entry for %s",
myname, var_config_dir, source, cparam_name);
continue;
}
if (PCF_LEGACY_PARAMETER(node))
PCF_PARAM_CLASS_OVERRIDE(node, PCF_PARAM_FLAG_USER);
if (PCF_RAW_PARAMETER(node))
continue;
}
SCAN_USER_PARAMETER_VALUE(cparam_value, PCF_PARAM_FLAG_USER, local_scope);
#ifdef LEGACY_DBMS_SUPPORT
pcf_register_dbms_parameters(cparam_value, pcf_flag_user_parameter,
local_scope);
#endif
}
}
static void pcf_scan_default_parameter_values(HTABLE *valid_params,
const char *dict_name,
PCF_MASTER_ENT *local_scope)
{
const char *myname = "pcf_scan_default_parameter_values";
PCF_PARAM_INFO **list;
PCF_PARAM_INFO **ht;
const char *param_value;
list = PCF_PARAM_TABLE_LIST(valid_params);
for (ht = list; *ht; ht++) {
if (PCF_RAW_PARAMETER(PCF_PARAM_INFO_NODE(*ht)))
continue;
if (dict_lookup(dict_name, PCF_PARAM_INFO_NAME(*ht)))
continue;
if ((param_value = pcf_convert_param_node(PCF_SHOW_DEFS, PCF_PARAM_INFO_NAME(*ht),
PCF_PARAM_INFO_NODE(*ht))) == 0)
msg_panic("%s: parameter %s has no default value",
myname, PCF_PARAM_INFO_NAME(*ht));
SCAN_USER_PARAMETER_VALUE(param_value, PCF_PARAM_FLAG_USER, local_scope);
}
myfree((void *) list);
}
void pcf_register_user_parameters(void)
{
const char *myname = "pcf_register_user_parameters";
PCF_MASTER_ENT *masterp;
ARGV *argv;
char *arg;
char *aval;
int field;
char *saved_arg;
char *param_name;
char *param_value;
DICT *dict;
if (pcf_param_table == 0)
msg_panic("%s: global parameter table is not initialized", myname);
if (pcf_master_table == 0)
msg_panic("%s: master table is not initialized", myname);
if (pcf_rest_class_table != 0)
msg_panic("%s: restriction class table is already initialized", myname);
pcf_rest_class_table = htable_create(1);
for (masterp = pcf_master_table; (argv = masterp->argv) != 0; masterp++) {
for (field = PCF_MASTER_MIN_FIELDS; argv->argv[field] != 0; field++) {
arg = argv->argv[field];
if (arg[0] != '-' || strcmp(arg, "--") == 0)
break;
if (strchr(pcf_daemon_options_expecting_value, arg[1]) == 0
|| (aval = argv->argv[field + 1]) == 0)
continue;
if (strcmp(arg, "-o") == 0) {
saved_arg = mystrdup(aval);
if (split_nameval(saved_arg, ¶m_name, ¶m_value) == 0)
dict_update(masterp->name_space, param_name, param_value);
myfree(saved_arg);
}
field += 1;
}
if ((dict = dict_handle(masterp->name_space)) != 0) {
masterp->all_params = dict;
masterp->valid_names = htable_create(1);
}
}
for (masterp = pcf_master_table; masterp->argv != 0; masterp++)
if (masterp->all_params != 0)
pcf_scan_user_parameter_namespace(masterp->name_space, masterp);
pcf_scan_default_parameter_values(pcf_param_table, CONFIG_DICT,
(PCF_MASTER_ENT *) 0);
pcf_scan_user_parameter_namespace(CONFIG_DICT, (PCF_MASTER_ENT *) 0);
}