xref: /netbsd-src/external/ibm-public/postfix/dist/src/global/mail_conf_str.c (revision e89934bbf778a6d6d6894877c4da59d0c7835b0f)
1 /*	$NetBSD: mail_conf_str.c,v 1.2 2017/02/14 01:16:45 christos Exp $	*/
2 
3 /*++
4 /* NAME
5 /*	mail_conf_str 3
6 /* SUMMARY
7 /*	string-valued global configuration parameter support
8 /* SYNOPSIS
9 /*	#include <mail_conf.h>
10 /*
11 /*	char	*get_mail_conf_str(name, defval, min, max)
12 /*	const char *name;
13 /*	const char *defval;
14 /*	int	min;
15 /*	int	max;
16 /*
17 /*	char	*get_mail_conf_str_fn(name, defval, min, max)
18 /*	const char *name;
19 /*	const char *(*defval)(void);
20 /*	int	min;
21 /*	int	max;
22 /*
23 /*	void	set_mail_conf_str(name, value)
24 /*	const char *name;
25 /*	const char *value;
26 /*
27 /*	void	get_mail_conf_str_table(table)
28 /*	const CONFIG_STR_TABLE *table;
29 /*
30 /*	void	get_mail_conf_str_fn_table(table)
31 /*	const CONFIG_STR_TABLE *table;
32 /* AUXILIARY FUNCTIONS
33 /*	char	*get_mail_conf_str2(name, suffix, defval, min, max)
34 /*	const char *name;
35 /*	const char *suffix;
36 /*	const char *defval;
37 /*	int	min;
38 /*	int	max;
39 /*
40 /*	void	check_mail_conf_str(name, strval, min, max)
41 /*	const char *name;
42 /*	const char *strval;
43 /*	int	min;
44 /*	int	max;
45 /* DESCRIPTION
46 /*	This module implements support for string-valued global
47 /*	configuration parameters.
48 /*
49 /*	get_mail_conf_str() looks up the named entry in the global
50 /*	configuration dictionary. The default value is returned when
51 /*	no value was found. String results should be passed to myfree()
52 /*	when no longer needed.  \fImin\fR is zero or specifies a lower
53 /*	bound on the string length; \fImax\fR is zero or specifies an
54 /*	upper limit on the string length.
55 /*
56 /*	get_mail_conf_str_fn() is similar but specifies a function that
57 /*	provides the default value. The function is called only when
58 /*	the default value is used.
59 /*
60 /*	set_mail_conf_str() updates the named entry in the global
61 /*	configuration dictionary. This has no effect on values that
62 /*	have been looked up earlier via the get_mail_conf_XXX() routines.
63 /*
64 /*	get_mail_conf_str_table() and get_mail_conf_str_fn_table() read
65 /*	lists of variables, as directed by their table arguments. A table
66 /*	must be terminated by a null entry.
67 /*
68 /*	get_mail_conf_str2() concatenates the two names and is otherwise
69 /*	identical to get_mail_conf_str().
70 /*
71 /*	check_mail_conf_str() exits with a fatal run-time error
72 /*	when the string does not meet its length requirements.
73 /* DIAGNOSTICS
74 /*	Fatal errors: bad string length.
75 /* SEE ALSO
76 /*	config(3) generic config parameter support
77 /* LICENSE
78 /* .ad
79 /* .fi
80 /*	The Secure Mailer license must be distributed with this software.
81 /* AUTHOR(S)
82 /*	Wietse Venema
83 /*	IBM T.J. Watson Research
84 /*	P.O. Box 704
85 /*	Yorktown Heights, NY 10598, USA
86 /*--*/
87 
88 /* System library. */
89 
90 #include <sys_defs.h>
91 #include <stdlib.h>
92 #include <string.h>
93 
94 /* Utility library. */
95 
96 #include <msg.h>
97 #include <mymalloc.h>
98 #include <stringops.h>
99 
100 /* Global library. */
101 
102 #include "mail_conf.h"
103 
104 /* check_mail_conf_str - validate string length */
105 
check_mail_conf_str(const char * name,const char * strval,int min,int max)106 void    check_mail_conf_str(const char *name, const char *strval,
107 			            int min, int max)
108 {
109     ssize_t len = strlen(strval);
110 
111     if (min && len < min)
112 	msg_fatal("bad string length %ld < %d: %s = %s",
113 		  (long) len, min, name, strval);
114     if (max && len > max)
115 	msg_fatal("bad string length %ld > %d: %s = %s",
116 		  (long) len, max, name, strval);
117 }
118 
119 /* get_mail_conf_str - evaluate string-valued configuration variable */
120 
get_mail_conf_str(const char * name,const char * defval,int min,int max)121 char   *get_mail_conf_str(const char *name, const char *defval,
122 			          int min, int max)
123 {
124     const char *strval;
125 
126     if ((strval = mail_conf_lookup_eval(name)) == 0) {
127 	strval = mail_conf_eval(defval);
128 	mail_conf_update(name, strval);
129     }
130     check_mail_conf_str(name, strval, min, max);
131     return (mystrdup(strval));
132 }
133 
134 /* get_mail_conf_str2 - evaluate string-valued configuration variable */
135 
get_mail_conf_str2(const char * name1,const char * name2,const char * defval,int min,int max)136 char   *get_mail_conf_str2(const char *name1, const char *name2,
137 			           const char *defval,
138 			           int min, int max)
139 {
140     const char *strval;
141     char   *name;
142 
143     name = concatenate(name1, name2, (char *) 0);
144     if ((strval = mail_conf_lookup_eval(name)) == 0) {
145 	strval = mail_conf_eval(defval);
146 	mail_conf_update(name, strval);
147     }
148     check_mail_conf_str(name, strval, min, max);
149     myfree(name);
150     return (mystrdup(strval));
151 }
152 
153 /* get_mail_conf_str_fn - evaluate string-valued configuration variable */
154 
155 typedef const char *(*stupid_indent_str) (void);
156 
get_mail_conf_str_fn(const char * name,stupid_indent_str defval,int min,int max)157 char   *get_mail_conf_str_fn(const char *name, stupid_indent_str defval,
158 			             int min, int max)
159 {
160     const char *strval;
161 
162     if ((strval = mail_conf_lookup_eval(name)) == 0) {
163 	strval = mail_conf_eval(defval());
164 	mail_conf_update(name, strval);
165     }
166     check_mail_conf_str(name, strval, min, max);
167     return (mystrdup(strval));
168 }
169 
170 /* set_mail_conf_str - update string-valued configuration dictionary entry */
171 
set_mail_conf_str(const char * name,const char * value)172 void    set_mail_conf_str(const char *name, const char *value)
173 {
174     mail_conf_update(name, value);
175 }
176 
177 /* get_mail_conf_str_table - look up table of strings */
178 
get_mail_conf_str_table(const CONFIG_STR_TABLE * table)179 void    get_mail_conf_str_table(const CONFIG_STR_TABLE *table)
180 {
181     while (table->name) {
182 	if (table->target[0])
183 	    myfree(table->target[0]);
184 	table->target[0] = get_mail_conf_str(table->name, table->defval,
185 					     table->min, table->max);
186 	table++;
187     }
188 }
189 
190 /* get_mail_conf_str_fn_table - look up strings, defaults are functions */
191 
get_mail_conf_str_fn_table(const CONFIG_STR_FN_TABLE * table)192 void    get_mail_conf_str_fn_table(const CONFIG_STR_FN_TABLE *table)
193 {
194     while (table->name) {
195 	if (table->target[0])
196 	    myfree(table->target[0]);
197 	table->target[0] = get_mail_conf_str_fn(table->name, table->defval,
198 						table->min, table->max);
199 	table++;
200     }
201 }
202