Postfix3.3.1
postconf_builtin.c
[詳解]
1 /*++
2 /* NAME
3 /* postconf_builtin 3
4 /* SUMMARY
5 /* built-in main.cf parameter support
6 /* SYNOPSIS
7 /* #include <postconf.h>
8 /*
9 /* void pcf_register_builtin_parameters(procname, pid)
10 /* const char *procname;
11 /* pid_t pid;
12 /* DESCRIPTION
13 /* pcf_register_builtin_parameters() initializes the global
14 /* main.cf parameter name space and adds all built-in parameter
15 /* information.
16 /*
17 /* Arguments:
18 /*.IP procname
19 /* Provides the default value for the "process_name" parameter.
20 /*.IP pid
21 /* Provides the default value for the "process_id" parameter.
22 /* DIAGNOSTICS
23 /* Problems are reported to the standard error stream.
24 /* LICENSE
25 /* .ad
26 /* .fi
27 /* The Secure Mailer license must be distributed with this software.
28 /* AUTHOR(S)
29 /* Wietse Venema
30 /* IBM T.J. Watson Research
31 /* P.O. Box 704
32 /* Yorktown Heights, NY 10598, USA
33 /*
34 /* Wietse Venema
35 /* Google, Inc.
36 /* 111 8th Avenue
37 /* New York, NY 10011, USA
38 /*--*/
39 
40 /* System library. */
41 
42 #include <sys_defs.h>
43 #include <string.h>
44 
45 #ifdef USE_PATHS_H
46 #include <paths.h>
47 #endif
48 
49 /* Utility library. */
50 
51 #include <msg.h>
52 #include <mymalloc.h>
53 #include <htable.h>
54 #include <vstring.h>
55 #include <get_hostname.h>
56 #include <stringops.h>
57 
58 /* Global library. */
59 
60 #include <mynetworks.h>
61 #include <mail_conf.h>
62 #include <mail_params.h>
63 #include <mail_version.h>
64 #include <mail_proto.h>
65 #include <mail_addr.h>
66 #include <inet_proto.h>
67 #include <server_acl.h>
68 
69 /* Application-specific. */
70 
71 #include <postconf.h>
72 
73  /*
74  * Support for built-in parameters: declarations generated by scanning
75  * actual C source files.
76  */
77 #include "time_vars.h"
78 #include "bool_vars.h"
79 #include "int_vars.h"
80 #include "str_vars.h"
81 #include "raw_vars.h"
82 #include "nint_vars.h"
83 #include "nbool_vars.h"
84 #include "long_vars.h"
85 
86  /*
87  * Support for built-in parameters: manually extracted.
88  */
89 #include "install_vars.h"
90 
91  /*
92  * Support for built-in parameters: lookup tables generated by scanning
93  * actual C source files.
94  */
95 static const CONFIG_TIME_TABLE pcf_time_table[] = {
96 #include "time_table.h"
97  0,
98 };
99 
100 static const CONFIG_BOOL_TABLE pcf_bool_table[] = {
101 #include "bool_table.h"
102  0,
103 };
104 
105 static const CONFIG_INT_TABLE pcf_int_table[] = {
106 #include "int_table.h"
107  0,
108 };
109 
110 static const CONFIG_STR_TABLE pcf_str_table[] = {
111 #include "str_table.h"
112 #include "install_table.h"
113  0,
114 };
115 
116 static const CONFIG_RAW_TABLE pcf_raw_table[] = {
117 #include "raw_table.h"
118  0,
119 };
120 
121 static const CONFIG_NINT_TABLE pcf_nint_table[] = {
122 #include "nint_table.h"
123  0,
124 };
125 
126 static const CONFIG_NBOOL_TABLE pcf_nbool_table[] = {
127 #include "nbool_table.h"
128  0,
129 };
130 
131 static const CONFIG_LONG_TABLE pcf_long_table[] = {
132 #include "long_table.h"
133  0,
134 };
135 
136  /*
137  * Legacy parameters for backwards compatibility.
138  */
139 static const CONFIG_STR_TABLE pcf_legacy_str_table[] = {
140  {"virtual_maps", ""},
141  {"fallback_relay", ""},
142  {"authorized_verp_clients", ""},
143  {"smtpd_client_connection_limit_exceptions", ""},
144  {"postscreen_dnsbl_ttl", ""},
145  0,
146 };
147 
148  /*
149  * Parameters whose default values are normally initialized by calling a
150  * function. We direct the calls to our own versions of those functions
151  * because the run-time conditions are slightly different.
152  *
153  * Important: if the evaluation of a parameter default value has any side
154  * effects, then those side effects must happen only once.
155  */
156 static const char *pcf_check_myhostname(void);
157 static const char *pcf_check_mydomainname(void);
158 static const char *pcf_mynetworks(void);
159 
160 #include "str_fn_vars.h"
161 
162 static const CONFIG_STR_FN_TABLE pcf_str_fn_table[] = {
163 #include "str_fn_table.h"
164  0,
165 };
166 
167  /*
168  * Parameters whose default values are normally initialized by ad-hoc code.
169  * The AWK script cannot identify these parameters or values, so we provide
170  * our own.
171  *
172  * Important: if the evaluation of a parameter default value has any side
173  * effects, then those side effects must happen only once.
174  */
175 static CONFIG_STR_TABLE pcf_adhoc_procname = {VAR_PROCNAME};
176 static CONFIG_STR_TABLE pcf_adhoc_servname = {VAR_SERVNAME};
177 static CONFIG_INT_TABLE pcf_adhoc_pid = {VAR_PID};
178 
179 #define STR(x) vstring_str(x)
180 
181 /* pcf_check_myhostname - lookup hostname and validate */
182 
183 static const char *pcf_check_myhostname(void)
184 {
185  static const char *name;
186  const char *dot;
187  const char *domain;
188 
189  /*
190  * Use cached result.
191  */
192  if (name)
193  return (name);
194 
195  /*
196  * If the local machine name is not in FQDN form, try to append the
197  * contents of $mydomain.
198  */
199  name = get_hostname();
200  if ((dot = strchr(name, '.')) == 0) {
201  if ((domain = mail_conf_lookup_eval(VAR_MYDOMAIN)) == 0)
202  domain = DEF_MYDOMAIN;
203  name = concatenate(name, ".", domain, (char *) 0);
204  }
205  return (name);
206 }
207 
208 /* pcf_get_myhostname - look up and store my hostname */
209 
210 static void pcf_get_myhostname(void)
211 {
212  const char *name;
213 
214  if ((name = mail_conf_lookup_eval(VAR_MYHOSTNAME)) == 0)
215  name = pcf_check_myhostname();
216  var_myhostname = mystrdup(name);
217 }
218 
219 /* pcf_check_mydomainname - lookup domain name and validate */
220 
221 static const char *pcf_check_mydomainname(void)
222 {
223  static const char *domain;
224  char *dot;
225 
226  /*
227  * Use cached result.
228  */
229  if (domain)
230  return (domain);
231 
232  /*
233  * Use a default domain when the hostname is not a FQDN ("foo").
234  */
235  if (var_myhostname == 0)
236  pcf_get_myhostname();
237  if ((dot = strchr(var_myhostname, '.')) == 0)
238  return (domain = DEF_MYDOMAIN);
239  return (domain = mystrdup(dot + 1));
240 }
241 
242 /* pcf_mynetworks - lookup network address list */
243 
244 static const char *pcf_mynetworks(void)
245 {
246  static const char *networks;
247  const char *junk;
248 
249  /*
250  * Use cached result.
251  */
252  if (networks)
253  return (networks);
254 
255  if (var_inet_interfaces == 0) {
257  || (junk = mail_conf_lookup_eval(VAR_INET_INTERFACES)) == 0)
260  (PCF_MASTER_ENT *) 0);
262  }
263  if (var_mynetworks_style == 0) {
264  if ((pcf_cmd_mode & PCF_SHOW_DEFS)
268  (PCF_MASTER_ENT *) 0);
270  }
271  if (var_inet_protocols == 0) {
272  if ((pcf_cmd_mode & PCF_SHOW_DEFS)
273  || (junk = mail_conf_lookup_eval(VAR_INET_PROTOCOLS)) == 0)
276  (PCF_MASTER_ENT *) 0);
279  }
280  return (networks = mystrdup(mynetworks()));
281 }
282 
283 /* pcf_conv_bool_parameter - get boolean parameter string value */
284 
285 static const char *pcf_conv_bool_parameter(void *ptr)
286 {
287  CONFIG_BOOL_TABLE *cbt = (CONFIG_BOOL_TABLE *) ptr;
288 
289  return (cbt->defval ? "yes" : "no");
290 }
291 
292 /* pcf_conv_time_parameter - get relative time parameter string value */
293 
294 static const char *pcf_conv_time_parameter(void *ptr)
295 {
296  CONFIG_TIME_TABLE *ctt = (CONFIG_TIME_TABLE *) ptr;
297 
298  return (ctt->defval);
299 }
300 
301 /* pcf_conv_int_parameter - get integer parameter string value */
302 
303 static const char *pcf_conv_int_parameter(void *ptr)
304 {
305  CONFIG_INT_TABLE *cit = (CONFIG_INT_TABLE *) ptr;
306 
307  return (STR(vstring_sprintf(pcf_param_string_buf, "%d", cit->defval)));
308 }
309 
310 /* pcf_conv_str_parameter - get string parameter string value */
311 
312 static const char *pcf_conv_str_parameter(void *ptr)
313 {
314  CONFIG_STR_TABLE *cst = (CONFIG_STR_TABLE *) ptr;
315 
316  return (cst->defval);
317 }
318 
319 /* pcf_conv_str_fn_parameter - get string-function parameter string value */
320 
321 static const char *pcf_conv_str_fn_parameter(void *ptr)
322 {
324 
325  return (cft->defval());
326 }
327 
328 /* pcf_conv_raw_parameter - get raw string parameter string value */
329 
330 static const char *pcf_conv_raw_parameter(void *ptr)
331 {
332  CONFIG_RAW_TABLE *rst = (CONFIG_RAW_TABLE *) ptr;
333 
334  return (rst->defval);
335 }
336 
337 /* pcf_conv_nint_parameter - get new integer parameter string value */
338 
339 static const char *pcf_conv_nint_parameter(void *ptr)
340 {
341  CONFIG_NINT_TABLE *rst = (CONFIG_NINT_TABLE *) ptr;
342 
343  return (rst->defval);
344 }
345 
346 /* pcf_conv_nbool_parameter - get new boolean parameter string value */
347 
348 static const char *pcf_conv_nbool_parameter(void *ptr)
349 {
350  CONFIG_NBOOL_TABLE *bst = (CONFIG_NBOOL_TABLE *) ptr;
351 
352  return (bst->defval);
353 }
354 
355 /* pcf_conv_long_parameter - get long parameter string value */
356 
357 static const char *pcf_conv_long_parameter(void *ptr)
358 {
359  CONFIG_LONG_TABLE *clt = (CONFIG_LONG_TABLE *) ptr;
360 
361  return (STR(vstring_sprintf(pcf_param_string_buf, "%ld", clt->defval)));
362 }
363 
364 /* pcf_register_builtin_parameters - add built-ins to the global name space */
365 
366 void pcf_register_builtin_parameters(const char *procname, pid_t pid)
367 {
368  const char *myname = "pcf_register_builtin_parameters";
369  const CONFIG_TIME_TABLE *ctt;
370  const CONFIG_BOOL_TABLE *cbt;
371  const CONFIG_INT_TABLE *cit;
372  const CONFIG_STR_TABLE *cst;
373  const CONFIG_STR_FN_TABLE *cft;
374  const CONFIG_RAW_TABLE *rst;
375  const CONFIG_NINT_TABLE *nst;
376  const CONFIG_NBOOL_TABLE *bst;
377  const CONFIG_LONG_TABLE *lst;
378 
379  /*
380  * Sanity checks.
381  */
382  if (pcf_param_table != 0)
383  msg_panic("%s: global parameter table is already initialized", myname);
384 
385  /*
386  * Initialize the global parameter table.
387  */
389 
390  /*
391  * Add the built-in parameters to the global name space. The class
392  * (built-in) is tentative; some parameters are actually service-defined,
393  * but they have their own default value.
394  */
395  for (ctt = pcf_time_table; ctt->name; ctt++)
397  PCF_PARAM_FLAG_BUILTIN, (void *) ctt,
398  pcf_conv_time_parameter);
399  for (cbt = pcf_bool_table; cbt->name; cbt++)
401  PCF_PARAM_FLAG_BUILTIN, (void *) cbt,
402  pcf_conv_bool_parameter);
403  for (cit = pcf_int_table; cit->name; cit++)
405  PCF_PARAM_FLAG_BUILTIN, (void *) cit,
406  pcf_conv_int_parameter);
407  for (cst = pcf_str_table; cst->name; cst++)
409  PCF_PARAM_FLAG_BUILTIN, (void *) cst,
410  pcf_conv_str_parameter);
411  for (cft = pcf_str_fn_table; cft->name; cft++)
413  PCF_PARAM_FLAG_BUILTIN, (void *) cft,
414  pcf_conv_str_fn_parameter);
415  for (rst = pcf_raw_table; rst->name; rst++)
418  (void *) rst, pcf_conv_raw_parameter);
419  for (nst = pcf_nint_table; nst->name; nst++)
421  PCF_PARAM_FLAG_BUILTIN, (void *) nst,
422  pcf_conv_nint_parameter);
423  for (bst = pcf_nbool_table; bst->name; bst++)
425  PCF_PARAM_FLAG_BUILTIN, (void *) bst,
426  pcf_conv_nbool_parameter);
427  for (lst = pcf_long_table; lst->name; lst++)
429  PCF_PARAM_FLAG_BUILTIN, (void *) lst,
430  pcf_conv_long_parameter);
431 
432  /*
433  * Register legacy parameters (used as a backwards-compatible migration
434  * aid).
435  */
436  for (cst = pcf_legacy_str_table; cst->name; cst++)
438  PCF_PARAM_FLAG_LEGACY, (void *) cst,
439  pcf_conv_str_parameter);
440 
441  /*
442  * Register parameters whose default value is normally initialized by
443  * ad-hoc code.
444  */
445  pcf_adhoc_procname.defval = mystrdup(procname);
446  PCF_PARAM_TABLE_ENTER(pcf_param_table, pcf_adhoc_procname.name,
448  (void *) &pcf_adhoc_procname, pcf_conv_str_parameter);
449  pcf_adhoc_servname.defval = mystrdup("");
450  PCF_PARAM_TABLE_ENTER(pcf_param_table, pcf_adhoc_servname.name,
452  (void *) &pcf_adhoc_servname, pcf_conv_str_parameter);
453  pcf_adhoc_pid.defval = pid;
456  (void *) &pcf_adhoc_pid, pcf_conv_int_parameter);
457 }
#define PCF_PARAM_FLAG_LEGACY
Definition: postconf.h:68
char * mystrdup(const char *str)
Definition: mymalloc.c:225
#define VAR_SERVNAME
Definition: mail_params.h:2438
NORETURN msg_panic(const char *fmt,...)
Definition: msg.c:295
#define VAR_PROCNAME
Definition: mail_params.h:2435
INET_PROTO_INFO * inet_proto_init(const char *context, const char *protocols)
Definition: inet_proto.c:180
char * pcf_expand_parameter_value(VSTRING *, int, const char *, PCF_MASTER_ENT *)
const char * defval
Definition: mail_conf.h:102
#define STR(x)
#define DEF_INET_INTERFACES
Definition: mail_params.h:179
#define VAR_INET_INTERFACES
Definition: mail_params.h:176
int pcf_cmd_mode
Definition: postconf.c:612
const char * mynetworks(void)
Definition: mynetworks.c:295
const char * name
Definition: mail_conf.h:155
#define VAR_PID
Definition: mail_params.h:2441
const char * name
Definition: mail_conf.h:139
const char * defval
Definition: mail_conf.h:140
const char * name
Definition: mail_conf.h:133
const char * defval
Definition: mail_conf.h:148
const char * name
Definition: mail_conf.h:117
#define VAR_MYHOSTNAME
Definition: mail_params.h:140
const char * name
Definition: mail_conf.h:109
#define PCF_PARAM_TABLE_ENTER(table, name, flags, data, func)
Definition: postconf.h:109
#define VAR_INET_PROTOCOLS
Definition: mail_params.h:994
const char * name
Definition: mail_conf.h:147
VSTRING * vstring_sprintf(VSTRING *vp, const char *format,...)
Definition: vstring.c:602
#define PCF_PARAM_TABLE_CREATE(size)
Definition: postconf.h:99
#define DEF_MYNETWORKS_STYLE
Definition: mail_params.h:2039
#define DEF_MYDOMAIN
Definition: mail_params.h:144
const char * mail_conf_lookup_eval(const char *name)
Definition: mail_conf.c:262
char * concatenate(const char *arg0,...)
Definition: concatenate.c:42
const char * defval
Definition: mail_conf.h:156
#define PCF_PARAM_FLAG_READONLY
Definition: postconf.h:69
#define VAR_MYDOMAIN
Definition: mail_params.h:143
#define PCF_PARAM_FLAG_BUILTIN
Definition: postconf.h:65
#define PCF_SHOW_DEFS
Definition: postconf.h:28
VSTRING * pcf_param_string_buf
const char * name
Definition: mail_conf.h:125
#define VAR_MYNETWORKS_STYLE
Definition: mail_params.h:2038
#define DEF_INET_PROTOCOLS
Definition: sys_defs.h:1403
PCF_PARAM_TABLE * pcf_param_table
Definition: postconf.c:610
char * var_inet_protocols
Definition: mail_params.c:260
const char * name
Definition: mail_conf.h:101
const char * get_hostname(void)
Definition: get_hostname.c:55
char * var_myhostname
Definition: mail_params.c:223
#define PCF_PARAM_FLAG_RAW
Definition: postconf.h:64
const char * defval
Definition: mail_conf.h:110
char * var_inet_interfaces
Definition: mail_params.c:258
const char *(* defval)(void)
Definition: mail_conf.h:175
void pcf_register_builtin_parameters(const char *procname, pid_t pid)
char * var_mynetworks_style
Definition: mail_params.c:288
const char * name
Definition: mail_conf.h:174