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