1 /*	$NetBSD: flush_clnt.c,v 1.1.1.1 2009/06/23 10:08:46 tron Exp $	*/
2 
3 /*++
4 /* NAME
5 /*	flush_clnt 3
6 /* SUMMARY
7 /*	fast flush cache manager client interface
8 /* SYNOPSIS
9 /*	#include <flush_clnt.h>
10 /*
11 /*	void	flush_init()
12 /*
13 /*	int	flush_add(site, queue_id)
14 /*	const char *site;
15 /*	const char *queue_id;
16 /*
17 /*	int	flush_send_site(site)
18 /*	const char *site;
19 /*
20 /*	int	flush_send_file(queue_id)
21 /*	const char *queue_id;
22 /*
23 /*	int	flush_refresh()
24 /*
25 /*	int	flush_purge()
26 /* DESCRIPTION
27 /*	The following routines operate through the "fast flush" service.
28 /*	This service maintains a cache of what mail is queued. The cache
29 /*	is maintained for eligible destinations. A destination is the
30 /*	right-hand side of a user@domain email address.
31 /*
32 /*	flush_init() initializes. It must be called before dropping
33 /*	privileges in a daemon process.
34 /*
35 /*	flush_add() informs the "fast flush" cache manager that mail is
36 /*	queued for the specified site with the specified queue ID.
37 /*
38 /*	flush_send_site() requests delivery of all mail that is queued for
39 /*	the specified destination.
40 /*
41 /*	flush_send_file() requests delivery of mail with the specified
42 /*	queue ID.
43 /*
44 /*	flush_refresh() requests the "fast flush" cache manager to refresh
45 /*	cached information that was not used for some configurable amount
46 /*	time.
47 /*
48 /*	flush_purge() requests the "fast flush" cache manager to refresh
49 /*	all cached information. This is incredibly expensive, and is not
50 /*	recommended.
51 /* DIAGNOSTICS
52 /*	The result codes and their meanings are (see flush_clnt(5h)):
53 /* .IP MAIL_FLUSH_OK
54 /*	The request completed successfully (in case of requests that
55 /*	complete in the background: the request was accepted by the server).
56 /* .IP MAIL_FLUSH_FAIL
57 /*	The request failed (the request could not be sent to the server,
58 /*	or the server reported failure).
59 /* .IP MAIL_FLUSH_BAD
60 /*	The "fast flush" server rejected the request (invalid request
61 /*	parameter).
62 /* .IP MAIL_FLUSH_DENY
63 /*	The specified domain is not eligible for "fast flush" service,
64 /*	or the "fast flush" service is disabled.
65 /* SEE ALSO
66 /*	flush(8) Postfix fast flush cache manager
67 /* LICENSE
68 /* .ad
69 /* .fi
70 /*	The Secure Mailer license must be distributed with this software.
71 /* AUTHOR(S)
72 /*	Wietse Venema
73 /*	IBM T.J. Watson Research
74 /*	P.O. Box 704
75 /*	Yorktown Heights, NY 10598, USA
76 /*--*/
77 
78 /* System library. */
79 
80 #include "sys_defs.h"
81 #include <unistd.h>
82 #include <stdarg.h>
83 
84 /* Utility library. */
85 
86 #include <msg.h>
87 #include <vstream.h>
88 
89 /* Global library. */
90 
91 #include <mail_proto.h>
92 #include <mail_flush.h>
93 #include <mail_params.h>
94 #include <domain_list.h>
95 #include <match_parent_style.h>
96 #include <flush_clnt.h>
97 
98 /* Application-specific. */
99 
100 #define STR(x)	vstring_str(x)
101 
102 static DOMAIN_LIST *flush_domains;
103 
104 /* flush_init - initialize */
105 
106 void    flush_init(void)
107 {
108     flush_domains = domain_list_init(match_parent_style(VAR_FFLUSH_DOMAINS),
109 				     var_fflush_domains);
110 }
111 
112 /* flush_purge - house keeping */
113 
114 int     flush_purge(void)
115 {
116     const char *myname = "flush_purge";
117     int     status;
118 
119     if (msg_verbose)
120 	msg_info("%s", myname);
121 
122     /*
123      * Don't bother the server if the service is turned off.
124      */
125     if (*var_fflush_domains == 0)
126 	status = FLUSH_STAT_DENY;
127     else
128 	status = mail_command_client(MAIL_CLASS_PUBLIC, var_flush_service,
129 			      ATTR_TYPE_STR, MAIL_ATTR_REQ, FLUSH_REQ_PURGE,
130 				     ATTR_TYPE_END);
131 
132     if (msg_verbose)
133 	msg_info("%s: status %d", myname, status);
134 
135     return (status);
136 }
137 
138 /* flush_refresh - house keeping */
139 
140 int     flush_refresh(void)
141 {
142     const char *myname = "flush_refresh";
143     int     status;
144 
145     if (msg_verbose)
146 	msg_info("%s", myname);
147 
148     /*
149      * Don't bother the server if the service is turned off.
150      */
151     if (*var_fflush_domains == 0)
152 	status = FLUSH_STAT_DENY;
153     else
154 	status = mail_command_client(MAIL_CLASS_PUBLIC, var_flush_service,
155 			    ATTR_TYPE_STR, MAIL_ATTR_REQ, FLUSH_REQ_REFRESH,
156 				     ATTR_TYPE_END);
157 
158     if (msg_verbose)
159 	msg_info("%s: status %d", myname, status);
160 
161     return (status);
162 }
163 
164 /* flush_send_site - deliver mail queued for site */
165 
166 int     flush_send_site(const char *site)
167 {
168     const char *myname = "flush_send_site";
169     int     status;
170 
171     if (msg_verbose)
172 	msg_info("%s: site %s", myname, site);
173 
174     /*
175      * Don't bother the server if the service is turned off, or if the site
176      * is not eligible.
177      */
178     if (flush_domains == 0)
179 	msg_panic("missing flush client initialization");
180     if (domain_list_match(flush_domains, site) == 0)
181 	status = FLUSH_STAT_DENY;
182     else
183 	status = mail_command_client(MAIL_CLASS_PUBLIC, var_flush_service,
184 			  ATTR_TYPE_STR, MAIL_ATTR_REQ, FLUSH_REQ_SEND_SITE,
185 				     ATTR_TYPE_STR, MAIL_ATTR_SITE, site,
186 				     ATTR_TYPE_END);
187 
188     if (msg_verbose)
189 	msg_info("%s: site %s status %d", myname, site, status);
190 
191     return (status);
192 }
193 
194 /* flush_send_file - deliver specific message */
195 
196 int     flush_send_file(const char *queue_id)
197 {
198     const char *myname = "flush_send_file";
199     int     status;
200 
201     if (msg_verbose)
202 	msg_info("%s: queue_id %s", myname, queue_id);
203 
204     /*
205      * Require that the service is turned on.
206      */
207     status = mail_command_client(MAIL_CLASS_PUBLIC, var_flush_service,
208 			  ATTR_TYPE_STR, MAIL_ATTR_REQ, FLUSH_REQ_SEND_FILE,
209 				 ATTR_TYPE_STR, MAIL_ATTR_QUEUEID, queue_id,
210 				 ATTR_TYPE_END);
211 
212     if (msg_verbose)
213 	msg_info("%s: queue_id %s status %d", myname, queue_id, status);
214 
215     return (status);
216 }
217 
218 /* flush_add - inform "fast flush" cache manager */
219 
220 int     flush_add(const char *site, const char *queue_id)
221 {
222     const char *myname = "flush_add";
223     int     status;
224 
225     if (msg_verbose)
226 	msg_info("%s: site %s id %s", myname, site, queue_id);
227 
228     /*
229      * Don't bother the server if the service is turned off, or if the site
230      * is not eligible.
231      */
232     if (flush_domains == 0)
233 	msg_panic("missing flush client initialization");
234     if (domain_list_match(flush_domains, site) == 0)
235 	status = FLUSH_STAT_DENY;
236     else
237 	status = mail_command_client(MAIL_CLASS_PUBLIC, var_flush_service,
238 				ATTR_TYPE_STR, MAIL_ATTR_REQ, FLUSH_REQ_ADD,
239 				     ATTR_TYPE_STR, MAIL_ATTR_SITE, site,
240 				 ATTR_TYPE_STR, MAIL_ATTR_QUEUEID, queue_id,
241 				     ATTR_TYPE_END);
242 
243     if (msg_verbose)
244 	msg_info("%s: site %s id %s status %d", myname, site, queue_id,
245 		 status);
246 
247     return (status);
248 }
249