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