xref: /netbsd-src/external/ibm-public/postfix/dist/src/util/dict.h (revision 6d322f2f4598f0d8a138f10ea648ec4fabe41f8b)
1 /*	$NetBSD: dict.h,v 1.1.1.4 2013/09/25 19:06:36 tron Exp $	*/
2 
3 #ifndef _DICT_H_INCLUDED_
4 #define _DICT_H_INCLUDED_
5 
6 /*++
7 /* NAME
8 /*	dict 3h
9 /* SUMMARY
10 /*	dictionary manager
11 /* SYNOPSIS
12 /*	#include <dict.h>
13 /* DESCRIPTION
14 /* .nf
15 
16  /*
17   * System library.
18   */
19 #include <fcntl.h>
20 
21  /*
22   * Utility library.
23   */
24 #include <vstream.h>
25 #include <argv.h>
26 #include <vstring.h>
27 #include <myflock.h>
28 
29  /*
30   * Provenance information.
31   */
32 typedef struct DICT_OWNER {
33     int     status;			/* see below */
34     uid_t   uid;			/* use only if status == UNTRUSTED */
35 } DICT_OWNER;
36 
37 #define DICT_OWNER_UNKNOWN	(-1)	/* ex: unauthenticated tcp, proxy */
38 #define DICT_OWNER_TRUSTED	(!1)	/* ex: root-owned config file */
39 #define DICT_OWNER_UNTRUSTED	(!0)	/* ex: non-root config file */
40 
41  /*
42   * Generic dictionary interface - in reality, a dictionary extends this
43   * structure with private members to maintain internal state.
44   */
45 typedef struct DICT {
46     char   *type;			/* for diagnostics */
47     char   *name;			/* for diagnostics */
48     int     flags;			/* see below */
49     const char *(*lookup) (struct DICT *, const char *);
50     int     (*update) (struct DICT *, const char *, const char *);
51     int     (*delete) (struct DICT *, const char *);
52     int     (*sequence) (struct DICT *, int, const char **, const char **);
53     int     (*lock) (struct DICT *, int);
54     void    (*close) (struct DICT *);
55     int     lock_fd;			/* for dict_update() lock */
56     int     stat_fd;			/* change detection */
57     time_t  mtime;			/* mod time at open */
58     VSTRING *fold_buf;			/* key folding buffer */
59     DICT_OWNER owner;			/* provenance */
60     int     error;			/* last operation only */
61 } DICT;
62 
63 extern DICT *dict_alloc(const char *, const char *, ssize_t);
64 extern void dict_free(DICT *);
65 
66 extern DICT *dict_debug(DICT *);
67 
68 #define DICT_DEBUG(d) ((d)->flags & DICT_FLAG_DEBUG ? dict_debug(d) : (d))
69 
70  /*
71   * See dict_open.c embedded manpage for flag definitions.
72   */
73 #define DICT_FLAG_NONE		(0)
74 #define DICT_FLAG_DUP_WARN	(1<<0)	/* warn about dups if not supported */
75 #define DICT_FLAG_DUP_IGNORE	(1<<1)	/* ignore dups if not supported */
76 #define DICT_FLAG_TRY0NULL	(1<<2)	/* do not append 0 to key/value */
77 #define DICT_FLAG_TRY1NULL	(1<<3)	/* append 0 to key/value */
78 #define DICT_FLAG_FIXED		(1<<4)	/* fixed key map */
79 #define DICT_FLAG_PATTERN	(1<<5)	/* keys are patterns */
80 #define DICT_FLAG_LOCK		(1<<6)	/* use temp lock before access */
81 #define DICT_FLAG_DUP_REPLACE	(1<<7)	/* replace dups if supported */
82 #define DICT_FLAG_SYNC_UPDATE	(1<<8)	/* sync updates if supported */
83 #define DICT_FLAG_DEBUG		(1<<9)	/* log access */
84 /*#define DICT_FLAG_FOLD_KEY	(1<<10)	/* lowercase the lookup key */
85 #define DICT_FLAG_NO_REGSUB	(1<<11)	/* disallow regexp substitution */
86 #define DICT_FLAG_NO_PROXY	(1<<12)	/* disallow proxy mapping */
87 #define DICT_FLAG_NO_UNAUTH	(1<<13)	/* disallow unauthenticated data */
88 #define DICT_FLAG_FOLD_FIX	(1<<14)	/* case-fold key with fixed-case map */
89 #define DICT_FLAG_FOLD_MUL	(1<<15)	/* case-fold key with multi-case map */
90 #define DICT_FLAG_FOLD_ANY	(DICT_FLAG_FOLD_FIX | DICT_FLAG_FOLD_MUL)
91 #define DICT_FLAG_OPEN_LOCK	(1<<16)	/* perm lock if not multi-writer safe */
92 
93  /* IMPORTANT: Update the dict_mask[] table when the above changes */
94 
95  /*
96   * The subsets of flags that control how a map is used. These are relevant
97   * mainly for proxymap support. Note: some categories overlap.
98   *
99   * DICT_FLAG_IMPL_MASK - flags that are set by the map implementation itself.
100   *
101   * DICT_FLAG_PARANOID - requestor flags that forbid the use of insecure map
102   * types for security-sensitive operations. These flags are checked by the
103   * map implementation itself upon open, lookup etc. requests.
104   *
105   * DICT_FLAG_RQST_MASK - all requestor flags, including paranoid flags, that
106   * the requestor may change between open, lookup etc. requests. These
107   * specify requestor properties, not map properties.
108   *
109   * DICT_FLAG_INST_MASK - none of the above flags. The requestor may not change
110   * these flags between open, lookup, etc. requests (although a map may make
111   * changes to its copy of some of these flags). The proxymap server opens
112   * only one map instance for all client requests with the same values of
113   * these flags, and the proxymap client uses its own saved copy of these
114   * flags.
115   */
116 #define DICT_FLAG_PARANOID \
117 	(DICT_FLAG_NO_REGSUB | DICT_FLAG_NO_PROXY | DICT_FLAG_NO_UNAUTH)
118 #define DICT_FLAG_IMPL_MASK	(DICT_FLAG_FIXED | DICT_FLAG_PATTERN)
119 #define DICT_FLAG_RQST_MASK	(DICT_FLAG_FOLD_ANY | DICT_FLAG_LOCK | \
120 				DICT_FLAG_DUP_REPLACE | DICT_FLAG_DUP_WARN | \
121 				DICT_FLAG_DUP_IGNORE | DICT_FLAG_SYNC_UPDATE | \
122 				DICT_FLAG_PARANOID)
123 #define DICT_FLAG_INST_MASK	~(DICT_FLAG_IMPL_MASK | DICT_FLAG_RQST_MASK)
124 
125  /*
126   * dict->error values. Errors must be negative; smtpd_check depends on this.
127   */
128 #define DICT_ERR_NONE	0		/* no error */
129 #define DICT_ERR_RETRY	(-1)		/* soft error */
130 #define DICT_ERR_CONFIG	(-2)		/* configuration error */
131 
132  /*
133   * Result values for exposed functions except lookup. FAIL/ERROR are
134   * suggested values, not for use in comparisons for equality.
135   */
136 #define DICT_STAT_FAIL		1	/* any value > 0: notfound, conflict */
137 #define DICT_STAT_SUCCESS	0	/* request satisfied */
138 #define DICT_STAT_ERROR		(-1)	/* any value < 0: database error */
139 
140  /*
141   * Set an error code and return a result value.
142   */
143 #define DICT_ERR_VAL_RETURN(dict, err, val) do { \
144 	(dict)->error = (err); \
145 	return (val); \
146     } while (0)
147 
148  /*
149   * Sequence function types.
150   */
151 #define DICT_SEQ_FUN_FIRST     0	/* set cursor to first record */
152 #define DICT_SEQ_FUN_NEXT      1	/* set cursor to next record */
153 
154  /*
155   * Interface for dictionary types.
156   */
157 extern ARGV *dict_mapnames(void);
158 
159  /*
160   * High-level interface, with logical dictionary names.
161   */
162 extern void dict_register(const char *, DICT *);
163 extern DICT *dict_handle(const char *);
164 extern void dict_unregister(const char *);
165 extern int dict_update(const char *, const char *, const char *);
166 extern const char *dict_lookup(const char *, const char *);
167 extern int dict_delete(const char *, const char *);
168 extern int dict_sequence(const char *, const int, const char **, const char **);
169 extern int dict_load_file_xt(const char *, const char *);
170 extern void dict_load_fp(const char *, VSTREAM *);
171 extern const char *dict_eval(const char *, const char *, int);
172 extern int dict_error(const char *);
173 
174  /*
175   * Low-level interface, with physical dictionary handles.
176   */
177 extern DICT *dict_open(const char *, int, int);
178 extern DICT *dict_open3(const char *, const char *, int, int);
179 extern void dict_open_register(const char *, DICT *(*) (const char *, int, int));
180 
181 #define dict_get(dp, key)	((const char *) (dp)->lookup((dp), (key)))
182 #define dict_put(dp, key, val)	(dp)->update((dp), (key), (val))
183 #define dict_del(dp, key)	(dp)->delete((dp), (key))
184 #define dict_seq(dp, f, key, val) (dp)->sequence((dp), (f), (key), (val))
185 #define dict_close(dp)		(dp)->close(dp)
186 typedef void (*DICT_WALK_ACTION) (const char *, DICT *, char *);
187 extern void dict_walk(DICT_WALK_ACTION, char *);
188 extern int dict_changed(void);
189 extern const char *dict_changed_name(void);
190 extern const char *dict_flags_str(int);
191 
192  /*
193   * Driver for interactive or scripted tests.
194   */
195 void    dict_test(int, char **);
196 
197  /*
198   * Behind-the-scenes support to continue execution with reduced
199   * functionality.
200   */
201 extern int dict_allow_surrogate;
202 extern DICT *dict_surrogate(const char *, const char *, int, int, const char *,...);
203 
204  /*
205   * This name is reserved for matchlist error handling.
206   */
207 #define DICT_TYPE_NOFILE	"non-existent"
208 
209 /* LICENSE
210 /* .ad
211 /* .fi
212 /*	The Secure Mailer license must be distributed with this software.
213 /* AUTHOR(S)
214 /*	Wietse Venema
215 /*	IBM T.J. Watson Research
216 /*	P.O. Box 704
217 /*	Yorktown Heights, NY 10598, USA
218 /*--*/
219 
220 #endif
221