summaryrefslogtreecommitdiffstats
path: root/src/global/mail_conf_time.c
blob: 5237dad29950a88859e91befa1d844476508ca47 (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*++
/* NAME
/*	mail_conf_time 3
/* SUMMARY
/*	time interval configuration parameter support
/* SYNOPSIS
/*	#include <mail_conf.h>
/*
/*	int	get_mail_conf_time(name, defval, min, max);
/*	const char *name;
/*	const char *defval;
/*	int	min;
/*	int	max;
/*
/*	void	set_mail_conf_time(name, value)
/*	const char *name;
/*	const char *value;
/*
/*	void	set_mail_conf_time_int(name, value)
/*	const char *name;
/*	int     value;
/*
/*	void	get_mail_conf_time_table(table)
/*	const CONFIG_TIME_TABLE *table;
/* AUXILIARY FUNCTIONS
/*	int	get_mail_conf_time2(name1, name2, defval, def_unit, min, max);
/*	const char *name1;
/*	const char *name2;
/*	int	defval;
/*	int	def_unit;
/*	int	min;
/*	int	max;
/*
/*	void	check_mail_conf_time(name, intval, min, max)
/*	const char *name;
/*	int	intval;
/*	int	min;
/*	int	max;
/* DESCRIPTION
/*	This module implements configuration parameter support
/*	for time interval values. The conversion routines understand
/*	one-letter suffixes to specify an explicit time unit: s
/*	(seconds), m (minutes), h (hours), d (days) or w (weeks).
/*	Internally, time is represented in seconds.
/*
/*	get_mail_conf_time() looks up the named entry in the global
/*	configuration dictionary. The default value is returned
/*	when no value was found. \fIdef_unit\fR supplies the default
/*	time unit for numbers specified without explicit unit.
/*	\fImin\fR is zero or specifies a lower limit on the integer
/*	value or string length; \fImax\fR is zero or specifies an
/*	upper limit on the integer value or string length.
/*
/*	set_mail_conf_time() 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_time_table() and get_mail_conf_time_fn_table() initialize
/*	lists of variables, as directed by their table arguments. A table
/*	must be terminated by a null entry.
/*
/*	check_mail_conf_time() terminates the program with a fatal
/*	runtime error when the time does not meet its requirements.
/* DIAGNOSTICS
/*	Fatal errors: malformed numerical value, unknown time unit.
/* BUGS
/*	Values and defaults are given in any unit; upper and lower
/*	bounds are given in seconds.
/* SEE ALSO
/*	config(3) general configuration
/*	mail_conf_str(3) string-valued configuration parameters
/* 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
/*
/*	Wietse Venema
/*	Google, Inc.
/*	111 8th Avenue
/*	New York, NY 10011, USA
/*--*/

/* System library. */

#include <sys_defs.h>
#include <stdlib.h>
#include <stdio.h>			/* BUFSIZ */
#include <ctype.h>

/* Utility library. */

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

/* Global library. */

#include "conv_time.h"
#include "mail_conf.h"

/* convert_mail_conf_time - look up and convert integer parameter value */

static int convert_mail_conf_time(const char *name, int *intval, int def_unit)
{
    const char *strval;

    if ((strval = mail_conf_lookup_eval(name)) == 0)
	return (0);
    if (conv_time(strval, intval, def_unit) == 0)
	msg_fatal("parameter %s: bad time value or unit: %s", name, strval);
    return (1);
}

/* check_mail_conf_time - validate integer value */

void    check_mail_conf_time(const char *name, int intval, int min, int max)
{
    if (min && intval < min)
	msg_fatal("invalid %s: %d (min %d)", name, intval, min);
    if (max && intval > max)
	msg_fatal("invalid %s: %d (max %d)", name, intval, max);
}

/* get_def_time_unit - extract time unit from default value */

static int get_def_time_unit(const char *name, const char *defval)
{
    const char *cp;

    for (cp = mail_conf_eval(defval); /* void */ ; cp++) {
	if (*cp == 0)
	    msg_panic("parameter %s: missing time unit in default value: %s",
		      name, defval);
	if (ISALPHA(*cp)) {
#if 0
	    if (cp[1] != 0)
		msg_panic("parameter %s: bad time unit in default value: %s",
			  name, defval);
#endif
	    return (*cp);
	}
    }
}

/* get_mail_conf_time - evaluate integer-valued configuration variable */

int     get_mail_conf_time(const char *name, const char *defval, int min, int max)
{
    int     intval;
    int     def_unit;

    def_unit = get_def_time_unit(name, defval);
    if (convert_mail_conf_time(name, &intval, def_unit) == 0)
	set_mail_conf_time(name, defval);
    if (convert_mail_conf_time(name, &intval, def_unit) == 0)
	msg_panic("get_mail_conf_time: parameter not found: %s", name);
    check_mail_conf_time(name, intval, min, max);
    return (intval);
}

/* get_mail_conf_time2 - evaluate integer-valued configuration variable */

int     get_mail_conf_time2(const char *name1, const char *name2,
			         int defval, int def_unit, int min, int max)
{
    int     intval;
    char   *name;

    name = concatenate(name1, name2, (char *) 0);
    if (convert_mail_conf_time(name, &intval, def_unit) == 0)
	set_mail_conf_time_int(name, defval);
    if (convert_mail_conf_time(name, &intval, def_unit) == 0)
	msg_panic("get_mail_conf_time2: parameter not found: %s", name);
    check_mail_conf_time(name, intval, min, max);
    myfree(name);
    return (intval);
}

/* set_mail_conf_time - update integer-valued configuration dictionary entry */

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

/* set_mail_conf_time_int - update integer-valued configuration dictionary entry */

void    set_mail_conf_time_int(const char *name, int value)
{
    const char myname[] = "set_mail_conf_time_int";
    char    buf[BUFSIZ];		/* yeah! crappy code! */

#ifndef NO_SNPRINTF
    ssize_t ret;

    ret = snprintf(buf, sizeof(buf), "%ds", value);
    if (ret < 0)
	msg_panic("%s: output error for %%ds", myname);
    if (ret >= sizeof(buf))
	msg_panic("%s: output for %%ds exceeds space %ld",
		  myname, (long) sizeof(buf));
#else
    sprintf(buf, "%ds", value);			/* yeah! more crappy code! */
#endif
    mail_conf_update(name, buf);
}

/* get_mail_conf_time_table - look up table of integers */

void    get_mail_conf_time_table(const CONFIG_TIME_TABLE *table)
{
    while (table->name) {
	table->target[0] = get_mail_conf_time(table->name, table->defval,
					      table->min, table->max);
	table++;
    }
}

#ifdef TEST

 /*
  * Stand-alone driver program for regression testing.
  */
#include <vstream.h>

int     main(int unused_argc, char **unused_argv)
{
    static int seconds;
    static int minutes;
    static int hours;
    static int days;
    static int weeks;
    static const CONFIG_TIME_TABLE time_table[] = {
	"seconds", "10s", &seconds, 0, 0,
	"minutes", "10m", &minutes, 0, 0,
	"hours", "10h", &hours, 0, 0,
	"days", "10d", &days, 0, 0,
	"weeks", "10w", &weeks, 0, 0,
	0,
    };

    get_mail_conf_time_table(time_table);
    vstream_printf("10 seconds = %d\n", seconds);
    vstream_printf("10 minutes = %d\n", minutes);
    vstream_printf("10 hours = %d\n", hours);
    vstream_printf("10 days = %d\n", days);
    vstream_printf("10 weeks = %d\n", weeks);
    vstream_fflush(VSTREAM_OUT);
    return (0);
}

#endif