1*e89934bbSchristos /* $NetBSD: postconf_node.c,v 1.2 2017/02/14 01:16:46 christos Exp $ */
2a30b880eStron
3a30b880eStron /*++
4a30b880eStron /* NAME
5a30b880eStron /* postconf_node 3
6a30b880eStron /* SUMMARY
7a30b880eStron /* low-level parameter node support
8a30b880eStron /* SYNOPSIS
9a30b880eStron /* #include <postconf.h>
10a30b880eStron /*
1116d67a18Stron /* PCF_PARAM_TABLE *PCF_PARAM_TABLE_CREATE(size)
12a30b880eStron /* ssize_t size;
13a30b880eStron /*
1416d67a18Stron /* PCF_PARAM_INFO **PCF_PARAM_TABLE_LIST(table)
1516d67a18Stron /* PCF_PARAM_TABLE *table;
16a30b880eStron /*
1716d67a18Stron /* const char *PCF_PARAM_INFO_NAME(info)
1816d67a18Stron /* PCF_PARAM_INFO *info;
19a30b880eStron /*
2016d67a18Stron /* PCF_PARAM_NODE *PCF_PARAM_INFO_NODE(info)
2116d67a18Stron /* PCF_PARAM_INFO *info;
22a30b880eStron /*
2316d67a18Stron /* PCF_PARAM_NODE *PCF_PARAM_TABLE_FIND(table, name)
2416d67a18Stron /* PCF_PARAM_TABLE *table;
25a30b880eStron /* const char *name;
26a30b880eStron /*
2716d67a18Stron /* PCF_PARAM_INFO *PCF_PARAM_TABLE_LOCATE(table, name)
2816d67a18Stron /* PCF_PARAM_TABLE *table;
29a30b880eStron /* const char *name;
30a30b880eStron /*
3116d67a18Stron /* PCF_PARAM_INFO *PCF_PARAM_TABLE_ENTER(table, name, flags,
32a30b880eStron /* param_data, convert_fn)
3316d67a18Stron /* PCF_PARAM_TABLE *table;
34a30b880eStron /* const char *name;
35a30b880eStron /* int flags;
36e262b48eSchristos /* void *param_data;
37e262b48eSchristos /* const char *(*convert_fn)(void *);
38a30b880eStron /*
3916d67a18Stron /* PCF_PARAM_NODE *pcf_make_param_node(flags, param_data, convert_fn)
40a30b880eStron /* int flags;
41e262b48eSchristos /* void *param_data;
42e262b48eSchristos /* const char *(*convert_fn) (void *);
43a30b880eStron /*
4416d67a18Stron /* const char *pcf_convert_param_node(mode, name, node)
45a30b880eStron /* int mode;
46a30b880eStron /* const char *name;
4716d67a18Stron /* PCF_PARAM_NODE *node;
48a30b880eStron /*
4916d67a18Stron /* VSTRING *pcf_param_string_buf;
50a30b880eStron /*
5116d67a18Stron /* PCF_RAW_PARAMETER(node)
5216d67a18Stron /* const PCF_PARAM_NODE *node;
53a30b880eStron /* DESCRIPTION
5416d67a18Stron /* This module maintains data structures (PCF_PARAM_NODE) with
55a30b880eStron /* information about known-legitimate parameters. These data
56a30b880eStron /* structures are stored in a hash table.
57a30b880eStron /*
5816d67a18Stron /* The PCF_PARAM_MUMBLE() macros are wrappers around the
5916d67a18Stron /* htable(3) module. Their sole purpose is to encapsulate all
6016d67a18Stron /* the pointer casting from and to (PCF_PARAM_NODE *). Apart
6116d67a18Stron /* from that, the macros have no features worth discussing.
62a30b880eStron /*
6316d67a18Stron /* pcf_make_param_node() creates a node for the global parameter
64a30b880eStron /* table. This node provides a parameter default value, and a
65a30b880eStron /* function that converts the default value to string.
66a30b880eStron /*
6716d67a18Stron /* pcf_convert_param_node() produces a string representation
6816d67a18Stron /* for a global parameter default value.
69a30b880eStron /*
7016d67a18Stron /* PCF_RAW_PARAMETER() returns non-zero if the specified
7116d67a18Stron /* parameter node represents a "raw parameter". The value of
7216d67a18Stron /* such parameters must not be scanned for macro names. Some
7316d67a18Stron /* "raw parameter" values contain "$" without macros, such as
7416d67a18Stron /* the smtpd_expansion_filter "safe character" set; and some
7516d67a18Stron /* contain $name from a private name space, such as forward_path.
7616d67a18Stron /* Some "raw parameter" values in postscreen(8) are safe to
7716d67a18Stron /* expand by one level. Support for that may be added later.
78a30b880eStron /*
7916d67a18Stron /* pcf_param_string_buf is a buffer that is initialized on the
8016d67a18Stron /* fly and that parameter-to-string conversion functions may
8116d67a18Stron /* use for temporary result storage.
82a30b880eStron /*
83a30b880eStron /* Arguments:
84a30b880eStron /* .IP size
85a30b880eStron /* The initial size of the hash table.
86a30b880eStron /* .IP table
87a30b880eStron /* A hash table for storage of "valid parameter" information.
88a30b880eStron /* .IP info
8916d67a18Stron /* A data structure with a name component and a PCF_PARAM_NODE
9016d67a18Stron /* component. Use PCF_PARAM_INFO_NAME() and PCF_PARAM_INFO_NODE()
91a30b880eStron /* to access these components.
92a30b880eStron /* .IP name
93a30b880eStron /* The name of a "valid parameter".
94a30b880eStron /* .IP flags
9516d67a18Stron /* PCF_PARAM_FLAG_RAW for a "raw parameter", PCF_PARAM_FLAG_NONE
9616d67a18Stron /* otherwise. See the PCF_RAW_PARAMETER() discussion above for
97a30b880eStron /* discussion of "raw parameter" values.
98a30b880eStron /* .IP param_data
9916d67a18Stron /* Information about the parameter value. Specify PCF_PARAM_NO_DATA
100a30b880eStron /* if this is not applicable.
101a30b880eStron /* .IP convert_fn
102a30b880eStron /* The function that will be invoked to produce a string
103a30b880eStron /* representation of the information in param_data. The function
104a30b880eStron /* receives the param_data value as argument.
105a30b880eStron /* .IP mode
10616d67a18Stron /* For now, the PCF_SHOW_DEFS flag is required.
107a30b880eStron /* .IP name
108a30b880eStron /* The name of the parameter whose value is requested. This
109a30b880eStron /* is used for diagnostics.
110a30b880eStron /* .IP node
111a30b880eStron /* The (flags, param_data, convert_fn) information that needs
112a30b880eStron /* to be converted to a string representation of the default
113a30b880eStron /* value.
114a30b880eStron /* DIAGNOSTICS
115a30b880eStron /* Problems are reported to the standard error stream.
116a30b880eStron /* LICENSE
117a30b880eStron /* .ad
118a30b880eStron /* .fi
119a30b880eStron /* The Secure Mailer license must be distributed with this software.
120a30b880eStron /* AUTHOR(S)
121a30b880eStron /* Wietse Venema
122a30b880eStron /* IBM T.J. Watson Research
123a30b880eStron /* P.O. Box 704
124a30b880eStron /* Yorktown Heights, NY 10598, USA
125a30b880eStron /*--*/
126a30b880eStron
127a30b880eStron
128a30b880eStron /* System library. */
129a30b880eStron
130a30b880eStron #include <sys_defs.h>
131a30b880eStron
132a30b880eStron /* Utility library. */
133a30b880eStron
134a30b880eStron #include <msg.h>
135a30b880eStron #include <mymalloc.h>
136a30b880eStron #include <vstring.h>
137a30b880eStron
138a30b880eStron /* Application-specific. */
139a30b880eStron
140a30b880eStron #include <postconf.h>
141a30b880eStron
14216d67a18Stron VSTRING *pcf_param_string_buf;
143a30b880eStron
14416d67a18Stron /* pcf_make_param_node - make node for global parameter table */
145a30b880eStron
pcf_make_param_node(int flags,void * param_data,const char * (* convert_fn)(void *))146e262b48eSchristos PCF_PARAM_NODE *pcf_make_param_node(int flags, void *param_data,
147e262b48eSchristos const char *(*convert_fn) (void *))
148a30b880eStron {
14916d67a18Stron PCF_PARAM_NODE *node;
150a30b880eStron
15116d67a18Stron node = (PCF_PARAM_NODE *) mymalloc(sizeof(*node));
152a30b880eStron node->flags = flags;
153a30b880eStron node->param_data = param_data;
154a30b880eStron node->convert_fn = convert_fn;
155a30b880eStron return (node);
156a30b880eStron }
157a30b880eStron
15816d67a18Stron /* pcf_convert_param_node - get default parameter value */
159a30b880eStron
pcf_convert_param_node(int mode,const char * name,PCF_PARAM_NODE * node)16016d67a18Stron const char *pcf_convert_param_node(int mode, const char *name, PCF_PARAM_NODE *node)
161a30b880eStron {
16216d67a18Stron const char *myname = "pcf_convert_param_node";
163a30b880eStron const char *value;
164a30b880eStron
165a30b880eStron /*
166a30b880eStron * One-off initialization.
167a30b880eStron */
16816d67a18Stron if (pcf_param_string_buf == 0)
16916d67a18Stron pcf_param_string_buf = vstring_alloc(100);
170a30b880eStron
171a30b880eStron /*
172a30b880eStron * Sanity check. A null value indicates that a parameter does not have
173a30b880eStron * the requested value. At this time, the only requested value can be the
174a30b880eStron * default value, and a null pointer value makes no sense here.
175a30b880eStron */
17616d67a18Stron if ((mode & PCF_SHOW_DEFS) == 0)
177a30b880eStron msg_panic("%s: request for non-default value of parameter %s",
178a30b880eStron myname, name);
179a30b880eStron if ((value = node->convert_fn(node->param_data)) == 0)
180a30b880eStron msg_panic("%s: parameter %s has null pointer default value",
181a30b880eStron myname, name);
182a30b880eStron
183a30b880eStron /*
184a30b880eStron * Return the parameter default value.
185a30b880eStron */
186a30b880eStron return (value);
187a30b880eStron }
188