Expand PMF_FN_* macros.
[netbsd-mini2440.git] / external / ibm-public / postfix / dist / src / global / mail_conf.c
blobe6f14b02d3de0ca5d758a0fcca55e0501277580a
1 /* $NetBSD$ */
3 /*++
4 /* NAME
5 /* mail_conf 3
6 /* SUMMARY
7 /* global configuration parameter management
8 /* SYNOPSIS
9 /* #include <mail_conf.h>
11 /* void mail_conf_read()
13 /* void mail_conf_suck()
15 /* void mail_conf_flush()
17 /* void mail_conf_update(name, value)
18 /* const char *name;
19 /* const char *value;
21 /* const char *mail_conf_lookup(name)
22 /* const char *name;
24 /* const char *mail_conf_eval(string)
25 /* const char *string;
27 /* const char *mail_conf_lookup_eval(name)
28 /* const char *name;
29 /* DESCRIPTION
30 /* mail_conf_suck() reads the global Postfix configuration file, and
31 /* stores its values into a global configuration dictionary.
33 /* mail_conf_read() invokes mail_conf_suck() and assigns the values
34 /* to global variables by calling mail_params_init().
36 /* mail_conf_flush() discards the global configuration dictionary.
37 /* This is needed in programs that read main.cf multiple times, to
38 /* ensure that deleted parameter settings are handled properly.
40 /* The following routines are wrappers around the generic dictionary
41 /* access routines.
43 /* mail_conf_update() updates the named global parameter. This has
44 /* no effect on parameters whose value has already been looked up.
45 /* The update succeeds or the program terminates with fatal error.
47 /* mail_conf_lookup() looks up the value of the named parameter.
48 /* A null pointer result means the parameter was not found.
49 /* The result is volatile and should be copied if it is to be
50 /* used for any appreciable amount of time.
52 /* mail_conf_eval() recursively expands any $parameters in the
53 /* string argument. The result is volatile and should be copied
54 /* if it is to be used for any appreciable amount of time.
56 /* mail_conf_lookup_eval() looks up the named parameter, and expands any
57 /* $parameters in the result. The result is volatile and should be
58 /* copied if it is to be used for any appreciable amount of time.
59 /* DIAGNOSTICS
60 /* Fatal errors: malformed numerical value.
61 /* ENVIRONMENT
62 /* MAIL_CONFIG, non-default configuration database
63 /* MAIL_VERBOSE, enable verbose mode
64 /* FILES
65 /* /etc/postfix: default Postfix configuration directory.
66 /* SEE ALSO
67 /* dict(3) generic dictionary manager
68 /* mail_conf_int(3) integer-valued parameters
69 /* mail_conf_str(3) string-valued parameters
70 /* LICENSE
71 /* .ad
72 /* .fi
73 /* The Secure Mailer license must be distributed with this software.
74 /* AUTHOR(S)
75 /* Wietse Venema
76 /* IBM T.J. Watson Research
77 /* P.O. Box 704
78 /* Yorktown Heights, NY 10598, USA
79 /*--*/
81 /* System library. */
83 #include <sys_defs.h>
84 #include <unistd.h>
85 #include <stdlib.h>
86 #include <string.h>
88 /* Utility library. */
90 #include <msg.h>
91 #include <mymalloc.h>
92 #include <vstream.h>
93 #include <vstring.h>
94 #include <dict.h>
95 #include <safe.h>
96 #include <stringops.h>
97 #include <readlline.h>
99 /* Global library. */
101 #include "mail_params.h"
102 #include "mail_conf.h"
104 /* mail_conf_checkdir - authorize non-default directory */
106 static void mail_conf_checkdir(const char *config_dir)
108 VSTRING *buf;
109 VSTREAM *fp;
110 char *path;
111 char *name;
112 char *value;
113 char *cp;
114 int found = 0;
117 * If running set-[ug]id, require that a non-default configuration
118 * directory name is blessed as a bona fide configuration directory in
119 * the default main.cf file.
121 path = concatenate(DEF_CONFIG_DIR, "/", "main.cf", (char *) 0);
122 if ((fp = vstream_fopen(path, O_RDONLY, 0)) == 0)
123 msg_fatal("open file %s: %m", path);
125 buf = vstring_alloc(1);
126 while (found == 0 && readlline(buf, fp, (int *) 0)) {
127 if (split_nameval(vstring_str(buf), &name, &value) == 0
128 && (strcmp(name, VAR_CONFIG_DIRS) == 0
129 || strcmp(name, VAR_MULTI_CONF_DIRS) == 0)) {
130 while (found == 0 && (cp = mystrtok(&value, ", \t\r\n")) != 0)
131 if (strcmp(cp, config_dir) == 0)
132 found = 1;
135 if (vstream_fclose(fp))
136 msg_fatal("read file %s: %m", path);
137 vstring_free(buf);
139 if (found == 0) {
140 msg_error("untrusted configuration directory name: %s", config_dir);
141 msg_fatal("specify \"%s = %s\" in %s",
142 VAR_CONFIG_DIRS, config_dir, path);
144 myfree(path);
147 /* mail_conf_read - read global configuration file */
149 void mail_conf_read(void)
151 mail_conf_suck();
152 mail_params_init();
155 /* mail_conf_suck - suck in the global configuration file */
157 void mail_conf_suck(void)
159 char *config_dir;
160 char *path;
163 * Permit references to unknown configuration variable names. We rely on
164 * a separate configuration checking tool to spot misspelled names and
165 * other kinds of trouble. Enter the configuration directory into the
166 * default dictionary.
168 dict_unknown_allowed = 1;
169 if (var_config_dir)
170 myfree(var_config_dir);
171 if ((config_dir = getenv(CONF_ENV_PATH)) == 0)
172 config_dir = DEF_CONFIG_DIR;
173 var_config_dir = mystrdup(config_dir);
174 set_mail_conf_str(VAR_CONFIG_DIR, var_config_dir);
177 * If the configuration directory name comes from a different trust
178 * domain, require that it is listed in the default main.cf file.
180 if (strcmp(var_config_dir, DEF_CONFIG_DIR) != 0 /* non-default */
181 && safe_getenv(CONF_ENV_PATH) == 0 /* non-default */
182 && geteuid() != 0) /* untrusted */
183 mail_conf_checkdir(var_config_dir);
184 path = concatenate(var_config_dir, "/", "main.cf", (char *) 0);
185 dict_load_file(CONFIG_DICT, path);
186 myfree(path);
189 /* mail_conf_flush - discard configuration dictionary */
191 void mail_conf_flush(void)
193 if (dict_handle(CONFIG_DICT) != 0)
194 dict_unregister(CONFIG_DICT);
197 /* mail_conf_eval - expand macros in string */
199 const char *mail_conf_eval(const char *string)
201 #define RECURSIVE 1
203 return (dict_eval(CONFIG_DICT, string, RECURSIVE));
206 /* mail_conf_lookup - lookup named variable */
208 const char *mail_conf_lookup(const char *name)
210 return (dict_lookup(CONFIG_DICT, name));
213 /* mail_conf_lookup_eval - expand named variable */
215 const char *mail_conf_lookup_eval(const char *name)
217 const char *value;
219 #define RECURSIVE 1
221 if ((value = dict_lookup(CONFIG_DICT, name)) != 0)
222 value = dict_eval(CONFIG_DICT, value, RECURSIVE);
223 return (value);
226 /* mail_conf_update - update parameter */
228 void mail_conf_update(const char *key, const char *value)
230 dict_update(CONFIG_DICT, key, value);