xref: /netbsd-src/external/ibm-public/postfix/dist/src/postconf/postconf_node.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: postconf_node.c,v 1.1.1.2 2013/09/25 19:06:33 tron Exp $	*/
2 
3 /*++
4 /* NAME
5 /*	postconf_node 3
6 /* SUMMARY
7 /*	low-level parameter node support
8 /* SYNOPSIS
9 /*	#include <postconf.h>
10 /*
11 /*	PC_PARAM_TABLE *PC_PARAM_TABLE_CREATE(size)
12 /*	ssize_t	size;
13 /*
14 /*	PC_PARAM_INFO **PC_PARAM_TABLE_LIST(table)
15 /*	PC_PARAM_TABLE *table;
16 /*
17 /*	const char *PC_PARAM_INFO_NAME(info)
18 /*	PC_PARAM_INFO *info;
19 /*
20 /*	PC_PARAM_NODE *PC_PARAM_INFO_NODE(info)
21 /*	PC_PARAM_INFO *info;
22 /*
23 /*	PC_PARAM_NODE *PC_PARAM_TABLE_FIND(table, name)
24 /*	PC_PARAM_TABLE *table;
25 /*	const char *name;
26 /*
27 /*	PC_PARAM_INFO *PC_PARAM_TABLE_LOCATE(table, name)
28 /*	PC_PARAM_TABLE *table;
29 /*	const char *name;
30 /*
31 /*	PC_PARAM_INFO *PC_PARAM_TABLE_ENTER(table, name, flags,
32 /*						param_data, convert_fn)
33 /*	PC_PARAM_TABLE *table;
34 /*	const char *name;
35 /*	int	flags;
36 /*	char	*param_data;
37 /*	const char *(*convert_fn)(char *);
38 /*
39 /*	PC_PARAM_NODE *make_param_node(flags, param_data, convert_fn)
40 /*	int	flags;
41 /*	char	*param_data;
42 /*	const char *(*convert_fn) (char *);
43 /*
44 /*	const char *convert_param_node(mode, name, node)
45 /*	int	mode;
46 /*	const char *name;
47 /*	PC_PARAM_NODE *node;
48 /*
49 /*	VSTRING *param_string_buf;
50 /*
51 /*	PC_RAW_PARAMETER(node)
52 /*	const PC_PARAM_NODE *node;
53 /* DESCRIPTION
54 /*	This module maintains data structures (PC_PARAM_NODE) with
55 /*	information about known-legitimate parameters.  These data
56 /*	structures are stored in a hash table.
57 /*
58 /*	The PC_PARAM_MUMBLE() macros are wrappers around the htable(3)
59 /*	module. Their sole purpose is to encapsulate all the pointer
60 /*	casting from and to (PC_PARAM_NODE *). Apart from that, the
61 /*	macros have no features worth discussing.
62 /*
63 /*	make_param_node() creates a node for the global parameter
64 /*	table. This node provides a parameter default value, and a
65 /*	function that converts the default value to string.
66 /*
67 /*	convert_param_node() produces a string representation for
68 /*	a global parameter default value.
69 /*
70 /*	PC_RAW_PARAMETER() returns non-zero if the specified parameter
71 /*	node represents a "raw parameter". The value of such
72 /*	parameters must not be scanned for macro names.  Some "raw
73 /*	parameter" values contain "$" without macros, such as the
74 /*	smtpd_expansion_filter "safe character" set; and some contain
75 /*	$name from a private name space, such as forward_path.  Some
76 /*	"raw parameter" values in postscreen(8) are safe to expand
77 /*	by one level.  Support for that may be added later.
78 /*
79 /*	param_string_buf is a buffer that is initialized on the fly
80 /*	and that parameter-to-string conversion functions may use for
81 /*	temporary result storage.
82 /*
83 /*	Arguments:
84 /* .IP size
85 /*	The initial size of the hash table.
86 /* .IP table
87 /*	A hash table for storage of "valid parameter" information.
88 /* .IP info
89 /*	A data structure with a name component and a PC_PARAM_NODE
90 /*	component. Use PC_PARAM_INFO_NAME() and PC_PARAM_INFO_NODE()
91 /*	to access these components.
92 /* .IP name
93 /*	The name of a "valid parameter".
94 /* .IP flags
95 /*	PC_PARAM_FLAG_RAW for a "raw parameter", PC_PARAM_FLAG_NONE
96 /*	otherwise. See the PC_RAW_PARAMETER() discussion above for
97 /*	discussion of "raw parameter" values.
98 /* .IP param_data
99 /*	Information about the parameter value.  Specify PC_PARAM_NO_DATA
100 /*	if this is not applicable.
101 /* .IP convert_fn
102 /*	The function that will be invoked to produce a string
103 /*	representation of the information in param_data. The function
104 /*	receives the param_data value as argument.
105 /* .IP mode
106 /*	For now, the SHOW_DEFS flag is required.
107 /* .IP name
108 /*	The name of the parameter whose value is requested.  This
109 /*	is used for diagnostics.
110 /* .IP node
111 /*	The (flags, param_data, convert_fn) information that needs
112 /*	to be converted to a string representation of the default
113 /*	value.
114 /* DIAGNOSTICS
115 /*	Problems are reported to the standard error stream.
116 /* LICENSE
117 /* .ad
118 /* .fi
119 /*	The Secure Mailer license must be distributed with this software.
120 /* AUTHOR(S)
121 /*	Wietse Venema
122 /*	IBM T.J. Watson Research
123 /*	P.O. Box 704
124 /*	Yorktown Heights, NY 10598, USA
125 /*--*/
126 
127 
128 /* System library. */
129 
130 #include <sys_defs.h>
131 
132 /* Utility library. */
133 
134 #include <msg.h>
135 #include <mymalloc.h>
136 #include <vstring.h>
137 
138 /* Application-specific. */
139 
140 #include <postconf.h>
141 
142 VSTRING *param_string_buf;
143 
144 /* make_param_node - make node for global parameter table */
145 
146 PC_PARAM_NODE *make_param_node(int flags, char *param_data,
147 			               const char *(*convert_fn) (char *))
148 {
149     PC_PARAM_NODE *node;
150 
151     node = (PC_PARAM_NODE *) mymalloc(sizeof(*node));
152     node->flags = flags;
153     node->param_data = param_data;
154     node->convert_fn = convert_fn;
155     return (node);
156 }
157 
158 /* convert_param_node - get default parameter value */
159 
160 const char *convert_param_node(int mode, const char *name, PC_PARAM_NODE *node)
161 {
162     const char *myname = "convert_param_node";
163     const char *value;
164 
165     /*
166      * One-off initialization.
167      */
168     if (param_string_buf == 0)
169 	param_string_buf = vstring_alloc(100);
170 
171     /*
172      * Sanity check. A null value indicates that a parameter does not have
173      * the requested value. At this time, the only requested value can be the
174      * default value, and a null pointer value makes no sense here.
175      */
176     if ((mode & SHOW_DEFS) == 0)
177 	msg_panic("%s: request for non-default value of parameter %s",
178 		  myname, name);
179     if ((value = node->convert_fn(node->param_data)) == 0)
180 	msg_panic("%s: parameter %s has null pointer default value",
181 		  myname, name);
182 
183     /*
184      * Return the parameter default value.
185      */
186     return (value);
187 }
188