1/*++
2/* NAME
3/*	postconf_dbms 3
4/* SUMMARY
5/*	legacy support for database-defined main.cf parameter names
6/* SYNOPSIS
7/*	#include <postconf.h>
8/*
9/*	void	pcf_register_dbms_parameters(param_value, flag_parameter,
10/*					local_scope)
11/*	const char *param_value;
12/*	const char *(flag_parameter) (const char *, int, PCF_MASTER_ENT *);
13/*	PCF_MASTER_ENT *local_scope;
14/* DESCRIPTION
15/*	This module implements legacy support for database configuration
16/*	where main.cf parameter names are generated by prepending
17/*	the database name to a database-defined suffix.
18/*
19/*	Arguments:
20/* .IP param_value
21/*	A parameter value to be searched for "type:table" strings.
22/*	When a database type is found that supports legacy-style
23/*	configuration, the table name is combined with each of the
24/*	database-defined suffixes to generate candidate parameter
25/*	names for that database type.
26/* .IP flag_parameter
27/*	A function that takes as arguments a candidate parameter
28/*	name, parameter flags, and a PCF_MASTER_ENT pointer.  The
29/*	function will flag the parameter as "used" if it has a
30/*	"name=value" entry in the local or global namespace.
31/* .IP local_scope
32/*	The local namespace.
33/* DIAGNOSTICS
34/*	No explicit diagnostics.
35/* LICENSE
36/* .ad
37/* .fi
38/*	The Secure Mailer license must be distributed with this software.
39/* AUTHOR(S)
40/*	Wietse Venema
41/*	IBM T.J. Watson Research
42/*	P.O. Box 704
43/*	Yorktown Heights, NY 10598, USA
44/*--*/
45
46/* System library. */
47
48#include <sys_defs.h>
49#include <string.h>
50
51/* Utility library. */
52
53#include <stringops.h>
54#include <split_at.h>
55#include <mac_expand.h>
56#include <dict.h>
57
58/* Global library. */
59
60#include <mail_conf.h>
61#include <dict_proxy.h>
62#include <dict_ldap.h>
63#include <dict_mysql.h>
64#include <dict_pgsql.h>
65#include <dict_sqlite.h>
66#include <dict_memcache.h>
67
68/* Application-specific. */
69
70#include <postconf.h>
71
72 /*
73  * SLMs.
74  */
75#define STR(x)	vstring_str(x)
76
77#ifdef LEGACY_DBMS_SUPPORT
78
79 /*
80  * The legacy database interface automagically instantiates a list of
81  * parameters by prepending the table name to database-specific suffixes.
82  */
83
84/* See ldap_table(5). */
85
86static const char *pcf_ldap_suffixes[] = {
87    "bind", "bind_dn", "bind_pw", "cache", "cache_expiry", "cache_size",
88    "chase_referrals", "debuglevel", "dereference", "domain",
89    "expansion_limit", "leaf_result_attribute", "query_filter",
90    "recursion_limit", "result_attribute", "result_format", "scope",
91    "search_base", "server_host", "server_port", "size_limit",
92    "special_result_attribute", "terminal_result_attribute",
93    "timeout", "version", 0,
94};
95
96/* See mysql_table(5). */
97
98static const char *pcf_mysql_suffixes[] = {
99    "additional_conditions", "dbname", "domain", "expansion_limit",
100    "hosts", "password", "query", "result_format", "select_field",
101    "table", "user", "where_field", 0,
102};
103
104/* See pgsql_table(5). */
105
106static const char *pcf_pgsql_suffixes[] = {
107    "additional_conditions", "dbname", "domain", "expansion_limit",
108    "hosts", "password", "query", "result_format", "select_field",
109    "select_function", "table", "user", "where_field", 0,
110};
111
112/* See sqlite_table(5). */
113
114static const char *pcf_sqlite_suffixes[] = {
115    "additional_conditions", "dbpath", "domain", "expansion_limit",
116    "query", "result_format", "select_field", "table", "where_field",
117    0,
118};
119
120/* See memcache_table(5). */
121
122static const char *pcf_memcache_suffixes[] = {
123    "backup", "data_size_limit", "domain", "flags", "key_format",
124    "line_size_limit", "max_try", "memcache", "retry_pause",
125    "timeout", "ttl", 0,
126};
127
128 /*
129  * Bundle up the database types and their suffix lists.
130  */
131typedef struct {
132    const char *db_type;
133    const char **db_suffixes;
134} PCF_DBMS_INFO;
135
136static const PCF_DBMS_INFO pcf_dbms_info[] = {
137    DICT_TYPE_LDAP, pcf_ldap_suffixes,
138    DICT_TYPE_MYSQL, pcf_mysql_suffixes,
139    DICT_TYPE_PGSQL, pcf_pgsql_suffixes,
140    DICT_TYPE_SQLITE, pcf_sqlite_suffixes,
141    DICT_TYPE_MEMCACHE, pcf_memcache_suffixes,
142    0,
143};
144
145/* pcf_register_dbms_parameters - look for database_type:prefix_name */
146
147void    pcf_register_dbms_parameters(const char *param_value,
148         const char *(flag_parameter) (const char *, int, PCF_MASTER_ENT *),
149				             PCF_MASTER_ENT *local_scope)
150{
151    const PCF_DBMS_INFO *dp;
152    char   *bufp;
153    char   *db_type;
154    char   *prefix;
155    static VSTRING *buffer = 0;
156    static VSTRING *candidate = 0;
157    const char **cpp;
158
159    /*
160     * XXX This does not examine both sides of conditional macro expansion,
161     * and may expand the "wrong" conditional macros. This is the best we can
162     * do for legacy database configuration support.
163     */
164    if (buffer == 0)
165	buffer = vstring_alloc(100);
166    bufp = pcf_expand_parameter_value(buffer, PCF_SHOW_EVAL, param_value,
167				      local_scope);
168
169    /*
170     * Naive parsing. We don't really know if the parameter specifies free
171     * text or a list of databases.
172     */
173    while ((db_type = mystrtok(&bufp, " ,\t\r\n")) != 0) {
174
175	/*
176	 * Skip over "proxy:" maptypes, to emulate the proxymap(8) server's
177	 * behavior when opening a local database configuration file.
178	 */
179	while ((prefix = split_at(db_type, ':')) != 0
180	       && strcmp(db_type, DICT_TYPE_PROXY) == 0)
181	    db_type = prefix;
182
183	/*
184	 * Look for database:prefix where the prefix is not a pathname and
185	 * the database is a known type. Synthesize candidate parameter names
186	 * from the user-defined prefix and from the database-defined suffix
187	 * list, and see if those parameters have a "name=value" entry in the
188	 * local or global namespace.
189	 */
190	if (prefix != 0 && *prefix != '/' && *prefix != '.') {
191	    for (dp = pcf_dbms_info; dp->db_type != 0; dp++) {
192		if (strcmp(db_type, dp->db_type) == 0) {
193		    for (cpp = dp->db_suffixes; *cpp; cpp++) {
194			vstring_sprintf(candidate ? candidate :
195					(candidate = vstring_alloc(30)),
196					"%s_%s", prefix, *cpp);
197			flag_parameter(STR(candidate),
198				  PCF_PARAM_FLAG_DBMS | PCF_PARAM_FLAG_USER,
199				       local_scope);
200		    }
201		    break;
202		}
203	    }
204	}
205    }
206}
207
208#endif
209