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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
|
/*++
/* NAME
/* dict_sockmap 3
/* SUMMARY
/* dictionary manager interface to Sendmail-style socketmap server
/* SYNOPSIS
/* #include <dict_sockmap.h>
/*
/* DICT *dict_sockmap_open(map, open_flags, dict_flags)
/* const char *map;
/* int open_flags;
/* int dict_flags;
/* DESCRIPTION
/* dict_sockmap_open() makes a Sendmail-style socketmap server
/* accessible via the generic dictionary operations described
/* in dict_open(3). The only implemented operation is dictionary
/* lookup. This map type can be useful for simulating a dynamic
/* lookup table.
/*
/* Postfix socketmap names have the form inet:host:port:socketmap-name
/* or unix:pathname:socketmap-name, where socketmap-name
/* specifies the socketmap name that the socketmap server uses.
/*
/* To test this module, build the netstring and dict_open test
/* programs. Run "./netstring nc -l portnumber" as the server,
/* and "./dict_open socketmap:127.0.0.1:portnumber:socketmapname"
/* as the client.
/* PROTOCOL
/* .ad
/* .fi
/* The socketmap class implements a simple protocol: the client
/* sends one request, and the server sends one reply.
/* ENCODING
/* .ad
/* .fi
/* Each request and reply are sent as one netstring object.
/* REQUEST FORMAT
/* .ad
/* .fi
/* .IP "<mapname> <space> <key>"
/* Search the specified socketmap under the specified key.
/* REPLY FORMAT
/* .ad
/* .fi
/* Replies must be no longer than 100000 characters (not including
/* the netstring encapsulation), and must have the following
/* form:
/* .IP "OK <space> <data>"
/* The requested data was found.
/* .IP "NOTFOUND <space>"
/* The requested data was not found.
/* .IP "TEMP <space> <reason>"
/* .IP "TIMEOUT <space> <reason>"
/* .IP "PERM <space> <reason>"
/* The request failed. The reason, if non-empty, is descriptive
/* text.
/* SECURITY
/* This map cannot be used for security-sensitive information,
/* because neither the connection nor the server are authenticated.
/* SEE ALSO
/* dict(3) generic dictionary manager
/* netstring(3) netstring stream I/O support
/* DIAGNOSTICS
/* Fatal errors: out of memory, unknown host or service name,
/* attempt to update or iterate over map.
/* BUGS
/* The protocol limits are not yet configurable.
/* 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 <errno.h>
#include <string.h>
#include <ctype.h>
/*
* Utility library.
*/
#include <mymalloc.h>
#include <msg.h>
#include <vstream.h>
#include <auto_clnt.h>
#include <netstring.h>
#include <split_at.h>
#include <stringops.h>
#include <htable.h>
#include <dict_sockmap.h>
/*
* Socket map data structure.
*/
typedef struct {
DICT dict; /* parent class */
char *sockmap_name; /* on-the-wire socketmap name */
VSTRING *rdwr_buf; /* read/write buffer */
HTABLE_INFO *client_info; /* shared endpoint name and handle */
} DICT_SOCKMAP;
/*
* Default limits.
*/
#define DICT_SOCKMAP_DEF_TIMEOUT 100 /* connect/read/write timeout */
#define DICT_SOCKMAP_DEF_MAX_REPLY 100000 /* reply size limit */
#define DICT_SOCKMAP_DEF_MAX_IDLE 10 /* close idle socket */
#define DICT_SOCKMAP_DEF_MAX_TTL 100 /* close old socket */
/*
* Class variables.
*/
static int dict_sockmap_timeout = DICT_SOCKMAP_DEF_TIMEOUT;
static int dict_sockmap_max_reply = DICT_SOCKMAP_DEF_MAX_REPLY;
static int dict_sockmap_max_idle = DICT_SOCKMAP_DEF_MAX_IDLE;
static int dict_sockmap_max_ttl = DICT_SOCKMAP_DEF_MAX_TTL;
/*
* The client handle is shared between socketmap instances that have the
* same inet:host:port or unix:pathame information. This could be factored
* out as a general module for reference-counted handles of any kind.
*/
static HTABLE *dict_sockmap_handles; /* shared handles */
typedef struct {
AUTO_CLNT *client_handle; /* the client handle */
int refcount; /* the reference count */
} DICT_SOCKMAP_REFC_HANDLE;
#define DICT_SOCKMAP_RH_NAME(ht) (ht)->key
#define DICT_SOCKMAP_RH_HANDLE(ht) \
((DICT_SOCKMAP_REFC_HANDLE *) (ht)->value)->client_handle
#define DICT_SOCKMAP_RH_REFCOUNT(ht) \
((DICT_SOCKMAP_REFC_HANDLE *) (ht)->value)->refcount
/*
* Socketmap protocol elements.
*/
#define DICT_SOCKMAP_PROT_OK "OK"
#define DICT_SOCKMAP_PROT_NOTFOUND "NOTFOUND"
#define DICT_SOCKMAP_PROT_TEMP "TEMP"
#define DICT_SOCKMAP_PROT_TIMEOUT "TIMEOUT"
#define DICT_SOCKMAP_PROT_PERM "PERM"
/*
* SLMs.
*/
#define STR(x) vstring_str(x)
#define LEN(x) VSTRING_LEN(x)
/* dict_sockmap_lookup - socket map lookup */
static const char *dict_sockmap_lookup(DICT *dict, const char *key)
{
const char *myname = "dict_sockmap_lookup";
DICT_SOCKMAP *dp = (DICT_SOCKMAP *) dict;
AUTO_CLNT *sockmap_clnt = DICT_SOCKMAP_RH_HANDLE(dp->client_info);
VSTREAM *fp;
int netstring_err;
char *reply_payload;
int except_count;
const char *error_class;
if (msg_verbose)
msg_info("%s: key %s", myname, key);
/*
* Optionally fold the key.
*/
if (dict->flags & DICT_FLAG_FOLD_MUL) {
if (dict->fold_buf == 0)
dict->fold_buf = vstring_alloc(100);
vstring_strcpy(dict->fold_buf, key);
key = lowercase(STR(dict->fold_buf));
}
/*
* We retry connection-level errors once, to make server restarts
* transparent.
*/
for (except_count = 0; /* see below */ ; except_count++) {
/*
* Look up the stream.
*/
if ((fp = auto_clnt_access(sockmap_clnt)) == 0) {
msg_warn("table %s:%s lookup error: %m", dict->type, dict->name);
dict->error = DICT_ERR_RETRY;
return (0);
}
/*
* Set up an exception handler.
*/
netstring_setup(fp, dict_sockmap_timeout);
if ((netstring_err = vstream_setjmp(fp)) == 0) {
/*
* Send the query. This may raise an exception.
*/
vstring_sprintf(dp->rdwr_buf, "%s %s", dp->sockmap_name, key);
NETSTRING_PUT_BUF(fp, dp->rdwr_buf);
/*
* Receive the response. This may raise an exception.
*/
netstring_get(fp, dp->rdwr_buf, dict_sockmap_max_reply);
/*
* If we got here, then no exception was raised.
*/
break;
}
/*
* Handle exceptions.
*/
else {
/*
* We retry a broken connection only once.
*/
if (except_count == 0 && netstring_err == NETSTRING_ERR_EOF
&& errno != ETIMEDOUT) {
auto_clnt_recover(sockmap_clnt);
continue;
}
/*
* We do not retry other errors.
*/
else {
msg_warn("table %s:%s lookup error: %s",
dict->type, dict->name,
netstring_strerror(netstring_err));
dict->error = DICT_ERR_RETRY;
return (0);
}
}
}
/*
* Parse the reply.
*/
VSTRING_TERMINATE(dp->rdwr_buf);
reply_payload = split_at(STR(dp->rdwr_buf), ' ');
if (strcmp(STR(dp->rdwr_buf), DICT_SOCKMAP_PROT_OK) == 0) {
dict->error = 0;
return (reply_payload);
} else if (strcmp(STR(dp->rdwr_buf), DICT_SOCKMAP_PROT_NOTFOUND) == 0) {
dict->error = 0;
return (0);
}
/* We got no definitive reply. */
if (strcmp(STR(dp->rdwr_buf), DICT_SOCKMAP_PROT_TEMP) == 0) {
error_class = "temporary";
dict->error = DICT_ERR_RETRY;
} else if (strcmp(STR(dp->rdwr_buf), DICT_SOCKMAP_PROT_TIMEOUT) == 0) {
error_class = "timeout";
dict->error = DICT_ERR_RETRY;
} else if (strcmp(STR(dp->rdwr_buf), DICT_SOCKMAP_PROT_PERM) == 0) {
error_class = "permanent";
dict->error = DICT_ERR_CONFIG;
} else {
error_class = "unknown";
dict->error = DICT_ERR_RETRY;
}
while (reply_payload && ISSPACE(*reply_payload))
reply_payload++;
msg_warn("%s:%s socketmap server %s error%s%.200s",
dict->type, dict->name, error_class,
reply_payload && *reply_payload ? ": " : "",
reply_payload && *reply_payload ?
printable(reply_payload, '?') : "");
return (0);
}
/* dict_sockmap_close - close socket map */
static void dict_sockmap_close(DICT *dict)
{
const char *myname = "dict_sockmap_close";
DICT_SOCKMAP *dp = (DICT_SOCKMAP *) dict;
if (dict_sockmap_handles == 0 || dict_sockmap_handles->used == 0)
msg_panic("%s: attempt to close a non-existent map", myname);
vstring_free(dp->rdwr_buf);
myfree(dp->sockmap_name);
if (--DICT_SOCKMAP_RH_REFCOUNT(dp->client_info) == 0) {
auto_clnt_free(DICT_SOCKMAP_RH_HANDLE(dp->client_info));
htable_delete(dict_sockmap_handles,
DICT_SOCKMAP_RH_NAME(dp->client_info), myfree);
}
if (dict->fold_buf)
vstring_free(dict->fold_buf);
dict_free(dict);
}
/* dict_sockmap_open - open socket map */
DICT *dict_sockmap_open(const char *mapname, int open_flags, int dict_flags)
{
DICT_SOCKMAP *dp;
char *saved_name = 0;
char *sockmap;
DICT_SOCKMAP_REFC_HANDLE *ref_handle;
HTABLE_INFO *client_info;
/*
* Let the optimizer worry about eliminating redundant code.
*/
#define DICT_SOCKMAP_OPEN_RETURN(d) do { \
DICT *__d = (d); \
if (saved_name != 0) \
myfree(saved_name); \
return (__d); \
} while (0)
/*
* Sanity checks.
*/
if (open_flags != O_RDONLY)
DICT_SOCKMAP_OPEN_RETURN(dict_surrogate(DICT_TYPE_SOCKMAP, mapname,
open_flags, dict_flags,
"%s:%s map requires O_RDONLY access mode",
DICT_TYPE_SOCKMAP, mapname));
if (dict_flags & DICT_FLAG_NO_UNAUTH)
DICT_SOCKMAP_OPEN_RETURN(dict_surrogate(DICT_TYPE_SOCKMAP, mapname,
open_flags, dict_flags,
"%s:%s map is not allowed for security-sensitive data",
DICT_TYPE_SOCKMAP, mapname));
/*
* Separate the socketmap name from the socketmap server name.
*/
saved_name = mystrdup(mapname);
if ((sockmap = split_at_right(saved_name, ':')) == 0)
DICT_SOCKMAP_OPEN_RETURN(dict_surrogate(DICT_TYPE_SOCKMAP, mapname,
open_flags, dict_flags,
"%s requires server:socketmap argument",
DICT_TYPE_SOCKMAP));
/*
* Use one reference-counted client handle for all socketmaps with the
* same inet:host:port or unix:pathname information.
*
* XXX Todo: graceful degradation after endpoint syntax error.
*/
if (dict_sockmap_handles == 0)
dict_sockmap_handles = htable_create(1);
if ((client_info = htable_locate(dict_sockmap_handles, saved_name)) == 0) {
ref_handle = (DICT_SOCKMAP_REFC_HANDLE *) mymalloc(sizeof(*ref_handle));
client_info = htable_enter(dict_sockmap_handles,
saved_name, (void *) ref_handle);
/* XXX Late initialization, so we can reuse macros for consistency. */
DICT_SOCKMAP_RH_REFCOUNT(client_info) = 1;
DICT_SOCKMAP_RH_HANDLE(client_info) =
auto_clnt_create(saved_name, dict_sockmap_timeout,
dict_sockmap_max_idle, dict_sockmap_max_ttl);
} else
DICT_SOCKMAP_RH_REFCOUNT(client_info) += 1;
/*
* Instantiate a socket map handle.
*/
dp = (DICT_SOCKMAP *) dict_alloc(DICT_TYPE_SOCKMAP, mapname, sizeof(*dp));
dp->rdwr_buf = vstring_alloc(100);
dp->sockmap_name = mystrdup(sockmap);
dp->client_info = client_info;
dp->dict.lookup = dict_sockmap_lookup;
dp->dict.close = dict_sockmap_close;
/* Don't look up parent domains or network superblocks. */
dp->dict.flags = dict_flags | DICT_FLAG_PATTERN;
DICT_SOCKMAP_OPEN_RETURN(DICT_DEBUG (&dp->dict));
}
|