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