summaryrefslogtreecommitdiffstats
path: root/src/global/scache_clnt.c
blob: 97fe8aa283e820da7acc67a93f695f5cc0dde287 (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
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/*++
/* NAME
/*	scache_clnt 3
/* SUMMARY
/*	session cache manager client
/* SYNOPSIS
/*	#include <scache.h>
/* DESCRIPTION
/*	SCACHE *scache_clnt_create(server, timeout, idle_limit, ttl_limit)
/*	const char *server;
/*	int	timeout;
/*	int	idle_limit;
/*	int	ttl_limit;
/* DESCRIPTION
/*	This module implements the client-side protocol of the
/*	session cache service.
/*
/*	scache_clnt_create() creates a session cache service client.
/*
/*	Arguments:
/* .IP server
/*	The session cache service name.
/* .IP timeout
/*	Time limit for connect, send or receive operations.
/* .IP idle_limit
/*	Idle time after which the client disconnects.
/* .IP ttl_limit
/*	Upper bound on the time that a connection is allowed to persist.
/* DIAGNOSTICS
/*	Fatal error: memory allocation problem;
/*	warning: communication error;
/*	panic: internal consistency failure.
/* SEE ALSO
/*	scache(3), generic session cache API
/* 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 <errno.h>

/* Utility library. */

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

/*#define msg_verbose 1*/

/* Global library. */

#include <mail_proto.h>
#include <mail_params.h>
#include <scache.h>

/* Application-specific. */

 /*
  * SCACHE_CLNT is a derived type from the SCACHE super-class.
  */
typedef struct {
    SCACHE  scache[1];			/* super-class */
    AUTO_CLNT *auto_clnt;		/* client endpoint */
#ifdef CANT_WRITE_BEFORE_SENDING_FD
    VSTRING *dummy;			/* dummy buffer */
#endif
} SCACHE_CLNT;

#define STR(x) vstring_str(x)

#define SCACHE_MAX_TRIES	2

/* scache_clnt_handshake - receive server protocol announcement */

static int scache_clnt_handshake(VSTREAM *stream)
{
    return (attr_scan(stream, ATTR_FLAG_STRICT,
		   RECV_ATTR_STREQ(MAIL_ATTR_PROTO, MAIL_ATTR_PROTO_SCACHE),
		      ATTR_TYPE_END));
}

/* scache_clnt_save_endp - save endpoint */

static void scache_clnt_save_endp(SCACHE *scache, int endp_ttl,
				          const char *endp_label,
				          const char *endp_prop, int fd)
{
    SCACHE_CLNT *sp = (SCACHE_CLNT *) scache;
    const char *myname = "scache_clnt_save_endp";
    VSTREAM *stream;
    int     status;
    int     tries;
    int     count = 0;

    if (msg_verbose)
	msg_info("%s: endp=%s prop=%s fd=%d",
		 myname, endp_label, endp_prop, fd);

    /*
     * Sanity check.
     */
    if (endp_ttl <= 0)
	msg_panic("%s: bad endp_ttl: %d", myname, endp_ttl);

    /*
     * Try a few times before disabling the cache. We use synchronous calls;
     * the session cache service is CPU bound and making the client
     * asynchronous would just complicate the code.
     */
    for (tries = 0; sp->auto_clnt != 0; tries++) {
	if ((stream = auto_clnt_access(sp->auto_clnt)) != 0) {
	    errno = 0;
	    count += 1;
	    if (attr_print(stream, ATTR_FLAG_NONE,
			 SEND_ATTR_STR(MAIL_ATTR_REQ, SCACHE_REQ_SAVE_ENDP),
			   SEND_ATTR_INT(MAIL_ATTR_TTL, endp_ttl),
			   SEND_ATTR_STR(MAIL_ATTR_LABEL, endp_label),
			   SEND_ATTR_STR(MAIL_ATTR_PROP, endp_prop),
			   ATTR_TYPE_END) != 0
		|| vstream_fflush(stream)
#ifdef CANT_WRITE_BEFORE_SENDING_FD
		|| attr_scan(stream, ATTR_FLAG_STRICT,
			     RECV_ATTR_STR(MAIL_ATTR_DUMMY, sp->dummy),
			     ATTR_TYPE_END) != 1
#endif
		|| LOCAL_SEND_FD(vstream_fileno(stream), fd) < 0
		|| attr_scan(stream, ATTR_FLAG_STRICT,
			     RECV_ATTR_INT(MAIL_ATTR_STATUS, &status),
			     ATTR_TYPE_END) != 1) {
		if (msg_verbose || count > 1 || (errno && errno != EPIPE && errno != ENOENT))
		    msg_warn("problem talking to service %s: %m",
			     VSTREAM_PATH(stream));
		/* Give up or recover. */
	    } else {
		if (msg_verbose && status != 0)
		    msg_warn("%s: descriptor save failed with status %d",
			     myname, status);
		break;
	    }
	}
	/* Give up or recover. */
	if (tries >= SCACHE_MAX_TRIES - 1) {
	    msg_warn("disabling connection caching");
	    auto_clnt_free(sp->auto_clnt);
	    sp->auto_clnt = 0;
	    break;
	}
	sleep(1);				/* XXX make configurable */
	auto_clnt_recover(sp->auto_clnt);
    }
    /* Always close the descriptor before returning. */
    if (close(fd) < 0)
	msg_warn("%s: close(%d): %m", myname, fd);
}

/* scache_clnt_find_endp - look up cached session */

static int scache_clnt_find_endp(SCACHE *scache, const char *endp_label,
				         VSTRING *endp_prop)
{
    SCACHE_CLNT *sp = (SCACHE_CLNT *) scache;
    const char *myname = "scache_clnt_find_endp";
    VSTREAM *stream;
    int     status;
    int     tries;
    int     fd;

    /*
     * Try a few times before disabling the cache. We use synchronous calls;
     * the session cache service is CPU bound and making the client
     * asynchronous would just complicate the code.
     */
    for (tries = 0; sp->auto_clnt != 0; tries++) {
	if ((stream = auto_clnt_access(sp->auto_clnt)) != 0) {
	    errno = 0;
	    if (attr_print(stream, ATTR_FLAG_NONE,
			 SEND_ATTR_STR(MAIL_ATTR_REQ, SCACHE_REQ_FIND_ENDP),
			   SEND_ATTR_STR(MAIL_ATTR_LABEL, endp_label),
			   ATTR_TYPE_END) != 0
		|| vstream_fflush(stream)
		|| attr_scan(stream, ATTR_FLAG_STRICT,
			     RECV_ATTR_INT(MAIL_ATTR_STATUS, &status),
			     RECV_ATTR_STR(MAIL_ATTR_PROP, endp_prop),
			     ATTR_TYPE_END) != 2) {
		if (msg_verbose || (errno != EPIPE && errno != ENOENT))
		    msg_warn("problem talking to service %s: %m",
			     VSTREAM_PATH(stream));
		/* Give up or recover. */
	    } else if (status != 0) {
		if (msg_verbose)
		    msg_info("%s: not found: %s", myname, endp_label);
		return (-1);
	    } else if (
#ifdef CANT_WRITE_BEFORE_SENDING_FD
		       attr_print(stream, ATTR_FLAG_NONE,
				  SEND_ATTR_STR(MAIL_ATTR_DUMMY, ""),
				  ATTR_TYPE_END) != 0
		       || vstream_fflush(stream) != 0
		       || read_wait(vstream_fileno(stream),
				    stream->timeout) < 0 ||	/* XXX */
#endif
		       (fd = LOCAL_RECV_FD(vstream_fileno(stream))) < 0) {
		if (msg_verbose || (errno != EPIPE && errno != ENOENT))
		    msg_warn("problem talking to service %s: %m",
			     VSTREAM_PATH(stream));
		/* Give up or recover. */
	    } else {
#ifdef MUST_READ_AFTER_SENDING_FD
		(void) attr_print(stream, ATTR_FLAG_NONE,
				  SEND_ATTR_STR(MAIL_ATTR_DUMMY, ""),
				  ATTR_TYPE_END);
		(void) vstream_fflush(stream);
#endif
		if (msg_verbose)
		    msg_info("%s: endp=%s prop=%s fd=%d",
			     myname, endp_label, STR(endp_prop), fd);
		return (fd);
	    }
	}
	/* Give up or recover. */
	if (tries >= SCACHE_MAX_TRIES - 1) {
	    msg_warn("disabling connection caching");
	    auto_clnt_free(sp->auto_clnt);
	    sp->auto_clnt = 0;
	    return (-1);
	}
	sleep(1);				/* XXX make configurable */
	auto_clnt_recover(sp->auto_clnt);
    }
    return (-1);
}

/* scache_clnt_save_dest - create destination/endpoint association */

static void scache_clnt_save_dest(SCACHE *scache, int dest_ttl,
				          const char *dest_label,
				          const char *dest_prop,
				          const char *endp_label)
{
    SCACHE_CLNT *sp = (SCACHE_CLNT *) scache;
    const char *myname = "scache_clnt_save_dest";
    VSTREAM *stream;
    int     status;
    int     tries;

    if (msg_verbose)
	msg_info("%s: dest_label=%s dest_prop=%s endp_label=%s",
		 myname, dest_label, dest_prop, endp_label);

    /*
     * Sanity check.
     */
    if (dest_ttl <= 0)
	msg_panic("%s: bad dest_ttl: %d", myname, dest_ttl);

    /*
     * Try a few times before disabling the cache. We use synchronous calls;
     * the session cache service is CPU bound and making the client
     * asynchronous would just complicate the code.
     */
    for (tries = 0; sp->auto_clnt != 0; tries++) {
	if ((stream = auto_clnt_access(sp->auto_clnt)) != 0) {
	    errno = 0;
	    if (attr_print(stream, ATTR_FLAG_NONE,
			 SEND_ATTR_STR(MAIL_ATTR_REQ, SCACHE_REQ_SAVE_DEST),
			   SEND_ATTR_INT(MAIL_ATTR_TTL, dest_ttl),
			   SEND_ATTR_STR(MAIL_ATTR_LABEL, dest_label),
			   SEND_ATTR_STR(MAIL_ATTR_PROP, dest_prop),
			   SEND_ATTR_STR(MAIL_ATTR_LABEL, endp_label),
			   ATTR_TYPE_END) != 0
		|| vstream_fflush(stream)
		|| attr_scan(stream, ATTR_FLAG_STRICT,
			     RECV_ATTR_INT(MAIL_ATTR_STATUS, &status),
			     ATTR_TYPE_END) != 1) {
		if (msg_verbose || (errno != EPIPE && errno != ENOENT))
		    msg_warn("problem talking to service %s: %m",
			     VSTREAM_PATH(stream));
		/* Give up or recover. */
	    } else {
		if (msg_verbose && status != 0)
		    msg_warn("%s: destination save failed with status %d",
			     myname, status);
		break;
	    }
	}
	/* Give up or recover. */
	if (tries >= SCACHE_MAX_TRIES - 1) {
	    msg_warn("disabling connection caching");
	    auto_clnt_free(sp->auto_clnt);
	    sp->auto_clnt = 0;
	    break;
	}
	sleep(1);				/* XXX make configurable */
	auto_clnt_recover(sp->auto_clnt);
    }
}

/* scache_clnt_find_dest - look up cached session */

static int scache_clnt_find_dest(SCACHE *scache, const char *dest_label,
				         VSTRING *dest_prop,
				         VSTRING *endp_prop)
{
    SCACHE_CLNT *sp = (SCACHE_CLNT *) scache;
    const char *myname = "scache_clnt_find_dest";
    VSTREAM *stream;
    int     status;
    int     tries;
    int     fd;

    /*
     * Try a few times before disabling the cache. We use synchronous calls;
     * the session cache service is CPU bound and making the client
     * asynchronous would just complicate the code.
     */
    for (tries = 0; sp->auto_clnt != 0; tries++) {
	if ((stream = auto_clnt_access(sp->auto_clnt)) != 0) {
	    errno = 0;
	    if (attr_print(stream, ATTR_FLAG_NONE,
			 SEND_ATTR_STR(MAIL_ATTR_REQ, SCACHE_REQ_FIND_DEST),
			   SEND_ATTR_STR(MAIL_ATTR_LABEL, dest_label),
			   ATTR_TYPE_END) != 0
		|| vstream_fflush(stream)
		|| attr_scan(stream, ATTR_FLAG_STRICT,
			     RECV_ATTR_INT(MAIL_ATTR_STATUS, &status),
			     RECV_ATTR_STR(MAIL_ATTR_PROP, dest_prop),
			     RECV_ATTR_STR(MAIL_ATTR_PROP, endp_prop),
			     ATTR_TYPE_END) != 3) {
		if (msg_verbose || (errno != EPIPE && errno != ENOENT))
		    msg_warn("problem talking to service %s: %m",
			     VSTREAM_PATH(stream));
		/* Give up or recover. */
	    } else if (status != 0) {
		if (msg_verbose)
		    msg_info("%s: not found: %s", myname, dest_label);
		return (-1);
	    } else if (
#ifdef CANT_WRITE_BEFORE_SENDING_FD
		       attr_print(stream, ATTR_FLAG_NONE,
				  SEND_ATTR_STR(MAIL_ATTR_DUMMY, ""),
				  ATTR_TYPE_END) != 0
		       || vstream_fflush(stream) != 0
		       || read_wait(vstream_fileno(stream),
				    stream->timeout) < 0 ||	/* XXX */
#endif
		       (fd = LOCAL_RECV_FD(vstream_fileno(stream))) < 0) {
		if (msg_verbose || (errno != EPIPE && errno != ENOENT))
		    msg_warn("problem talking to service %s: %m",
			     VSTREAM_PATH(stream));
		/* Give up or recover. */
	    } else {
#ifdef MUST_READ_AFTER_SENDING_FD
		(void) attr_print(stream, ATTR_FLAG_NONE,
				  SEND_ATTR_STR(MAIL_ATTR_DUMMY, ""),
				  ATTR_TYPE_END);
		(void) vstream_fflush(stream);
#endif
		if (msg_verbose)
		    msg_info("%s: dest=%s dest_prop=%s endp_prop=%s fd=%d",
		    myname, dest_label, STR(dest_prop), STR(endp_prop), fd);
		return (fd);
	    }
	}
	/* Give up or recover. */
	if (tries >= SCACHE_MAX_TRIES - 1) {
	    msg_warn("disabling connection caching");
	    auto_clnt_free(sp->auto_clnt);
	    sp->auto_clnt = 0;
	    return (-1);
	}
	sleep(1);				/* XXX make configurable */
	auto_clnt_recover(sp->auto_clnt);
    }
    return (-1);
}

/* scache_clnt_size - dummy */

static void scache_clnt_size(SCACHE *unused_scache, SCACHE_SIZE *size)
{
    /* XXX Crap in a hurry. */
    size->dest_count = 0;
    size->endp_count = 0;
    size->sess_count = 0;
}

/* scache_clnt_free - destroy cache */

static void scache_clnt_free(SCACHE *scache)
{
    SCACHE_CLNT *sp = (SCACHE_CLNT *) scache;

    if (sp->auto_clnt)
	auto_clnt_free(sp->auto_clnt);
#ifdef CANT_WRITE_BEFORE_SENDING_FD
    vstring_free(sp->dummy);
#endif
    myfree((void *) sp);
}

/* scache_clnt_create - initialize */

SCACHE *scache_clnt_create(const char *server, int timeout,
			           int idle_limit, int ttl_limit)
{
    SCACHE_CLNT *sp = (SCACHE_CLNT *) mymalloc(sizeof(*sp));
    char   *service;

    sp->scache->save_endp = scache_clnt_save_endp;
    sp->scache->find_endp = scache_clnt_find_endp;
    sp->scache->save_dest = scache_clnt_save_dest;
    sp->scache->find_dest = scache_clnt_find_dest;
    sp->scache->size = scache_clnt_size;
    sp->scache->free = scache_clnt_free;

    service = concatenate("local:" MAIL_CLASS_PRIVATE "/", server, (char *) 0);
    sp->auto_clnt = auto_clnt_create(service, timeout, idle_limit, ttl_limit);
    auto_clnt_control(sp->auto_clnt,
		      AUTO_CLNT_CTL_HANDSHAKE, scache_clnt_handshake,
		      AUTO_CLNT_CTL_END);
    myfree(service);

#ifdef CANT_WRITE_BEFORE_SENDING_FD
    sp->dummy = vstring_alloc(1);
#endif

    return (sp->scache);
}