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

/* System library. */

#include <sys_defs.h>
#include <stdlib.h>
#include <string.h>

/* Utility library. */

#include <msg.h>
#include <mymalloc.h>
#include <stringops.h>

/* Global library. */

#include "mail_conf.h"

/* check_mail_conf_str - validate string length */

void    check_mail_conf_str(const char *name, const char *strval,
			            int min, int max)
{
    ssize_t len = strlen(strval);

    if (min && len < min)
	msg_fatal("bad string length %ld < %d: %s = %s",
		  (long) len, min, name, strval);
    if (max && len > max)
	msg_fatal("bad string length %ld > %d: %s = %s",
		  (long) len, max, name, strval);
}

/* get_mail_conf_str - evaluate string-valued configuration variable */

char   *get_mail_conf_str(const char *name, const char *defval,
			          int min, int max)
{
    const char *strval;

    if ((strval = mail_conf_lookup_eval(name)) == 0) {
	strval = mail_conf_eval(defval);
	mail_conf_update(name, strval);
    }
    check_mail_conf_str(name, strval, min, max);
    return (mystrdup(strval));
}

/* get_mail_conf_str2 - evaluate string-valued configuration variable */

char   *get_mail_conf_str2(const char *name1, const char *name2,
			           const char *defval,
			           int min, int max)
{
    const char *strval;
    char   *name;

    name = concatenate(name1, name2, (char *) 0);
    if ((strval = mail_conf_lookup_eval(name)) == 0) {
	strval = mail_conf_eval(defval);
	mail_conf_update(name, strval);
    }
    check_mail_conf_str(name, strval, min, max);
    myfree(name);
    return (mystrdup(strval));
}

/* get_mail_conf_str_fn - evaluate string-valued configuration variable */

typedef const char *(*stupid_indent_str) (void);

char   *get_mail_conf_str_fn(const char *name, stupid_indent_str defval,
			             int min, int max)
{
    const char *strval;

    if ((strval = mail_conf_lookup_eval(name)) == 0) {
	strval = mail_conf_eval(defval());
	mail_conf_update(name, strval);
    }
    check_mail_conf_str(name, strval, min, max);
    return (mystrdup(strval));
}

/* set_mail_conf_str - update string-valued configuration dictionary entry */

void    set_mail_conf_str(const char *name, const char *value)
{
    mail_conf_update(name, value);
}

/* get_mail_conf_str_table - look up table of strings */

void    get_mail_conf_str_table(const CONFIG_STR_TABLE *table)
{
    while (table->name) {
	if (table->target[0])
	    myfree(table->target[0]);
	table->target[0] = get_mail_conf_str(table->name, table->defval,
					     table->min, table->max);
	table++;
    }
}

/* get_mail_conf_str_fn_table - look up strings, defaults are functions */

void    get_mail_conf_str_fn_table(const CONFIG_STR_FN_TABLE *table)
{
    while (table->name) {
	if (table->target[0])
	    myfree(table->target[0]);
	table->target[0] = get_mail_conf_str_fn(table->name, table->defval,
						table->min, table->max);
	table++;
    }
}