1 /*	$NetBSD: mail_conf_raw.c,v 1.1.1.1 2009/06/23 10:08:46 tron Exp $	*/
2 
3 /*++
4 /* NAME
5 /*	mail_conf_raw 3
6 /* SUMMARY
7 /*	raw string-valued global configuration parameter support
8 /* SYNOPSIS
9 /*	#include <mail_conf.h>
10 /*
11 /*	char	*get_mail_conf_raw(name, defval, min, max)
12 /*	const char *name;
13 /*	const char *defval;
14 /*	int	min;
15 /*	int	max;
16 /*
17 /*	char	*get_mail_conf_raw_fn(name, defval, min, max)
18 /*	const char *name;
19 /*	const char *(*defval)(void);
20 /*	int	min;
21 /*	int	max;
22 /*
23 /*	void	get_mail_conf_raw_table(table)
24 /*	const CONFIG_RAW_TABLE *table;
25 /*
26 /*	void	get_mail_conf_raw_fn_table(table)
27 /*	const CONFIG_RAW_TABLE *table;
28 /* DESCRIPTION
29 /*	This module implements support for string-valued global
30 /*	configuration parameters that are loaded without $name expansion.
31 /*
32 /*	get_mail_conf_raw() looks up the named entry in the global
33 /*	configuration dictionary. The default value is returned when
34 /*	no value was found. String results should be passed to myfree()
35 /*	when no longer needed.  \fImin\fR is zero or specifies a lower
36 /*	bound on the string length; \fImax\fR is zero or specifies an
37 /*	upper limit on the string length.
38 /*
39 /*	get_mail_conf_raw_fn() is similar but specifies a function that
40 /*	provides the default value. The function is called only when
41 /*	the default value is used.
42 /*
43 /*	get_mail_conf_raw_table() and get_mail_conf_raw_fn_table() read
44 /*	lists of variables, as directed by their table arguments. A table
45 /*	must be terminated by a null entry.
46 /* DIAGNOSTICS
47 /*	Fatal errors: bad string length.
48 /* SEE ALSO
49 /*	config(3) generic config parameter support
50 /* LICENSE
51 /* .ad
52 /* .fi
53 /*	The Secure Mailer license must be distributed with this software.
54 /* AUTHOR(S)
55 /*	Wietse Venema
56 /*	IBM T.J. Watson Research
57 /*	P.O. Box 704
58 /*	Yorktown Heights, NY 10598, USA
59 /*--*/
60 
61 /* System library. */
62 
63 #include <sys_defs.h>
64 #include <stdlib.h>
65 #include <string.h>
66 
67 /* Utility library. */
68 
69 #include <msg.h>
70 #include <mymalloc.h>
71 
72 /* Global library. */
73 
74 #include "mail_conf.h"
75 
76 /* check_mail_conf_raw - validate string length */
77 
78 static void check_mail_conf_raw(const char *name, const char *strval,
79 			             int min, int max)
80 {
81     ssize_t len = strlen(strval);
82 
83     if (min && len < min)
84 	msg_fatal("bad string length (%ld < %d): %s = %s",
85 		  (long) len, min, name, strval);
86     if (max && len > max)
87 	msg_fatal("bad string length (%ld > %d): %s = %s",
88 		  (long) len, max, name, strval);
89 }
90 
91 /* get_mail_conf_raw - evaluate string-valued configuration variable */
92 
93 char   *get_mail_conf_raw(const char *name, const char *defval,
94 		               int min, int max)
95 {
96     const char *strval;
97 
98     if ((strval = mail_conf_lookup(name)) == 0) {
99 	strval = defval;
100 	mail_conf_update(name, strval);
101     }
102     check_mail_conf_raw(name, strval, min, max);
103     return (mystrdup(strval));
104 }
105 
106 /* get_mail_conf_raw_fn - evaluate string-valued configuration variable */
107 
108 typedef const char *(*stupid_indent_str) (void);
109 
110 char   *get_mail_conf_raw_fn(const char *name, stupid_indent_str defval,
111 			          int min, int max)
112 {
113     const char *strval;
114 
115     if ((strval = mail_conf_lookup(name)) == 0) {
116 	strval = defval();
117 	mail_conf_update(name, strval);
118     }
119     check_mail_conf_raw(name, strval, min, max);
120     return (mystrdup(strval));
121 }
122 
123 /* get_mail_conf_raw_table - look up table of strings */
124 
125 void    get_mail_conf_raw_table(const CONFIG_RAW_TABLE *table)
126 {
127     while (table->name) {
128 	if (table->target[0])
129 	    myfree(table->target[0]);
130 	table->target[0] = get_mail_conf_raw(table->name, table->defval,
131 					  table->min, table->max);
132 	table++;
133     }
134 }
135 
136 /* get_mail_conf_raw_fn_table - look up strings, defaults are functions */
137 
138 void    get_mail_conf_raw_fn_table(const CONFIG_RAW_FN_TABLE *table)
139 {
140     while (table->name) {
141 	if (table->target[0])
142 	    myfree(table->target[0]);
143 	table->target[0] = get_mail_conf_raw_fn(table->name, table->defval,
144 					     table->min, table->max);
145 	table++;
146     }
147 }
148