Postfix3.3.1
postconf_main.c
[詳解]
1 /*++
2 /* NAME
3 /* postconf_main 3
4 /* SUMMARY
5 /* basic support for main.cf
6 /* SYNOPSIS
7 /* #include <postconf.h>
8 /*
9 /* void pcf_read_parameters()
10 /*
11 /* void pcf_show_parameters(fp, mode, param_class, names)
12 /* VSTREAM *fp;
13 /* int mode;
14 /* int param_class;
15 /* char **names;
16 /* DESCRIPTION
17 /* pcf_read_parameters() reads parameters from main.cf.
18 /*
19 /* pcf_set_parameters() takes an array of \fIname=value\fR
20 /* pairs and overrides settings read with pcf_read_parameters().
21 /*
22 /* pcf_show_parameters() writes main.cf parameters to the
23 /* specified output stream.
24 /*
25 /* Arguments:
26 /* .IP fp
27 /* Output stream.
28 /* .IP mode
29 /* Bit-wise OR of zero or more of the following:
30 /* .RS
31 /* .IP PCF_FOLD_LINE
32 /* Fold long lines.
33 /* .IP PCF_SHOW_DEFS
34 /* Output default parameter values.
35 /* .IP PCF_SHOW_NONDEF
36 /* Output explicit settings only.
37 /* .IP PCF_HIDE_NAME
38 /* Output parameter values without the "name =" prefix.
39 /* .IP PCF_SHOW_EVAL
40 /* Expand $name in parameter values.
41 /* .RE
42 /* .IP param_class
43 /* Bit-wise OR of one or more of the following:
44 /* .RS
45 /* .IP PCF_PARAM_FLAG_BUILTIN
46 /* Show built-in parameters.
47 /* .IP PCF_PARAM_FLAG_SERVICE
48 /* Show service-defined parameters.
49 /* .IP PCF_PARAM_FLAG_USER
50 /* Show user-defined parameters.
51 /* .RE
52 /* .IP names
53 /* List of zero or more parameter names. If the list is empty,
54 /* output all parameters.
55 /* DIAGNOSTICS
56 /* Problems are reported to the standard error stream.
57 /* LICENSE
58 /* .ad
59 /* .fi
60 /* The Secure Mailer license must be distributed with this software.
61 /* AUTHOR(S)
62 /* Wietse Venema
63 /* IBM T.J. Watson Research
64 /* P.O. Box 704
65 /* Yorktown Heights, NY 10598, USA
66 /*
67 /* Wietse Venema
68 /* Google, Inc.
69 /* 111 8th Avenue
70 /* New York, NY 10011, USA
71 /*--*/
72 
73 /* System library. */
74 
75 #include <sys_defs.h>
76 #include <stdarg.h>
77 #include <stdlib.h>
78 #include <string.h>
79 
80 /* Utility library. */
81 
82 #include <msg.h>
83 #include <mymalloc.h>
84 #include <vstream.h>
85 #include <vstring.h>
86 #include <readlline.h>
87 #include <dict.h>
88 #include <stringops.h>
89 #include <htable.h>
90 #include <mac_expand.h>
91 
92 /* Global library. */
93 
94 #include <mail_params.h>
95 #include <mail_conf.h>
96 
97 /* Application-specific. */
98 
99 #include <postconf.h>
100 
101 #define STR(x) vstring_str(x)
102 
103 /* pcf_read_parameters - read parameter info from file */
104 
106 {
107  char *path;
108 
109  /*
110  * A direct rip-off of mail_conf_read(). XXX Avoid code duplication by
111  * better code decomposition.
112  */
114  path = concatenate(var_config_dir, "/", MAIN_CONF_FILE, (char *) 0);
115  if (dict_load_file_xt(CONFIG_DICT, path) == 0)
116  msg_fatal("open %s: %m", path);
117  myfree(path);
118 }
119 
120 /* pcf_set_parameters - add or override name=value pairs */
121 
122 void pcf_set_parameters(char **name_val_array)
123 {
124  char *name, *value, *junk;
125  const char *err;
126  char **cpp;
127 
128  for (cpp = name_val_array; *cpp; cpp++) {
129  junk = mystrdup(*cpp);
130  if ((err = split_nameval(junk, &name, &value)) != 0)
131  msg_fatal("invalid parameter override: %s: %s", *cpp, err);
132  mail_conf_update(name, value);
133  myfree(junk);
134  }
135 }
136 
137 /* pcf_print_parameter - show specific parameter */
138 
139 static void pcf_print_parameter(VSTREAM *fp, int mode, const char *name,
140  PCF_PARAM_NODE *node)
141 {
142  const char *value;
143 
144  /*
145  * Use the default or actual value.
146  */
147  value = pcf_lookup_parameter_value(mode, name, (PCF_MASTER_ENT *) 0, node);
148 
149  /*
150  * Optionally expand $name in the parameter value. Print the result with
151  * or without the name= prefix.
152  */
153  if (value != 0) {
154  if (mode & PCF_HIDE_VALUE) {
155  pcf_print_line(fp, mode, "%s\n", name);
156  } else {
157  if ((mode & PCF_SHOW_EVAL) != 0 && PCF_RAW_PARAMETER(node) == 0)
158  value = pcf_expand_parameter_value((VSTRING *) 0, mode, value,
159  (PCF_MASTER_ENT *) 0);
160  if ((mode & PCF_HIDE_NAME) == 0) {
161  pcf_print_line(fp, mode, "%s = %s\n", name, value);
162  } else {
163  pcf_print_line(fp, mode, "%s\n", value);
164  }
165  }
166  if (msg_verbose)
167  vstream_fflush(fp);
168  }
169 }
170 
171 /* pcf_comp_names - qsort helper */
172 
173 static int pcf_comp_names(const void *a, const void *b)
174 {
175  PCF_PARAM_INFO **ap = (PCF_PARAM_INFO **) a;
176  PCF_PARAM_INFO **bp = (PCF_PARAM_INFO **) b;
177 
178  return (strcmp(PCF_PARAM_INFO_NAME(ap[0]),
179  PCF_PARAM_INFO_NAME(bp[0])));
180 }
181 
182 /* pcf_show_parameters - show parameter info */
183 
184 void pcf_show_parameters(VSTREAM *fp, int mode, int param_class, char **names)
185 {
186  PCF_PARAM_INFO **list;
187  PCF_PARAM_INFO **ht;
188  char **namep;
189  PCF_PARAM_NODE *node;
190 
191  /*
192  * Show all parameters.
193  */
194  if (*names == 0) {
196  qsort((void *) list, pcf_param_table->used, sizeof(*list),
197  pcf_comp_names);
198  for (ht = list; *ht; ht++)
199  if (param_class & PCF_PARAM_INFO_NODE(*ht)->flags)
200  pcf_print_parameter(fp, mode, PCF_PARAM_INFO_NAME(*ht),
201  PCF_PARAM_INFO_NODE(*ht));
202  myfree((void *) list);
203  return;
204  }
205 
206  /*
207  * Show named parameters.
208  */
209  for (namep = names; *namep; namep++) {
210  if ((node = PCF_PARAM_TABLE_FIND(pcf_param_table, *namep)) == 0) {
211  msg_warn("%s: unknown parameter", *namep);
212  } else {
213  pcf_print_parameter(fp, mode, *namep, node);
214  }
215  }
216 }
int msg_verbose
Definition: msg.c:177
void pcf_read_parameters(void)
void myfree(void *ptr)
Definition: mymalloc.c:207
char * mystrdup(const char *str)
Definition: mymalloc.c:225
int dict_load_file_xt(const char *dict_name, const char *path)
Definition: dict.c:441
char * pcf_expand_parameter_value(VSTRING *, int, const char *, PCF_MASTER_ENT *)
#define PCF_PARAM_TABLE_FIND(table, name)
Definition: postconf.h:106
char * var_config_dir
Definition: mail_params.c:241
#define PCF_PARAM_TABLE_LIST(table)
Definition: postconf.h:102
#define CONFIG_DICT
Definition: mail_conf.h:17
#define PCF_RAW_PARAMETER(node)
Definition: postconf.h:77
const char * split_nameval(char *buf, char **name, char **value)
Definition: split_nameval.c:61
#define MAIN_CONF_FILE
Definition: mail_params.h:334
#define PCF_PARAM_INFO_NODE(ht)
Definition: postconf.h:104
#define PCF_HIDE_VALUE
Definition: postconf.h:47
#define PCF_PARAM_INFO_NAME(ht)
Definition: postconf.h:103
#define PCF_SHOW_EVAL
Definition: postconf.h:33
void msg_warn(const char *fmt,...)
Definition: msg.c:215
void pcf_set_parameters(char **name_val_array)
const char * pcf_lookup_parameter_value(int, const char *, PCF_MASTER_ENT *, PCF_PARAM_NODE *)
NORETURN msg_fatal(const char *fmt,...)
Definition: msg.c:249
int vstream_fflush(VSTREAM *stream)
Definition: vstream.c:1257
char * concatenate(const char *arg0,...)
Definition: concatenate.c:42
void pcf_set_config_dir(void)
Definition: postconf_misc.c:48
PCF_PARAM_TABLE * pcf_param_table
Definition: postconf.c:610
#define PCF_HIDE_NAME
Definition: postconf.h:29
void mail_conf_update(const char *key, const char *value)
Definition: mail_conf.c:275
void pcf_show_parameters(VSTREAM *fp, int mode, int param_class, char **names)
void pcf_print_line(VSTREAM *fp, int mode, const char *fmt,...)
#define PCF_PARAM_INFO
Definition: postconf.h:92