xref: /netbsd-src/external/ibm-public/postfix/dist/src/global/flush_clnt.c (revision 33881f779a77dce6440bdc44610d94de75bebefe)
1 /*	$NetBSD: flush_clnt.c,v 1.2 2017/02/14 01:16:45 christos 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(VAR_FFLUSH_DOMAINS, MATCH_FLAG_RETURN
109 				   | match_parent_style(VAR_FFLUSH_DOMAINS),
110 				     var_fflush_domains);
111 }
112 
113 /* flush_purge - house keeping */
114 
115 int     flush_purge(void)
116 {
117     const char *myname = "flush_purge";
118     int     status;
119 
120     if (msg_verbose)
121 	msg_info("%s", myname);
122 
123     /*
124      * Don't bother the server if the service is turned off.
125      */
126     if (*var_fflush_domains == 0)
127 	status = FLUSH_STAT_DENY;
128     else
129 	status = mail_command_client(MAIL_CLASS_PUBLIC, var_flush_service,
130 			      SEND_ATTR_STR(MAIL_ATTR_REQ, FLUSH_REQ_PURGE),
131 				     ATTR_TYPE_END);
132 
133     if (msg_verbose)
134 	msg_info("%s: status %d", myname, status);
135 
136     return (status);
137 }
138 
139 /* flush_refresh - house keeping */
140 
141 int     flush_refresh(void)
142 {
143     const char *myname = "flush_refresh";
144     int     status;
145 
146     if (msg_verbose)
147 	msg_info("%s", myname);
148 
149     /*
150      * Don't bother the server if the service is turned off.
151      */
152     if (*var_fflush_domains == 0)
153 	status = FLUSH_STAT_DENY;
154     else
155 	status = mail_command_client(MAIL_CLASS_PUBLIC, var_flush_service,
156 			    SEND_ATTR_STR(MAIL_ATTR_REQ, FLUSH_REQ_REFRESH),
157 				     ATTR_TYPE_END);
158 
159     if (msg_verbose)
160 	msg_info("%s: status %d", myname, status);
161 
162     return (status);
163 }
164 
165 /* flush_send_site - deliver mail queued for site */
166 
167 int     flush_send_site(const char *site)
168 {
169     const char *myname = "flush_send_site";
170     int     status;
171 
172     if (msg_verbose)
173 	msg_info("%s: site %s", myname, site);
174 
175     /*
176      * Don't bother the server if the service is turned off, or if the site
177      * is not eligible.
178      */
179     if (flush_domains == 0)
180 	msg_panic("missing flush client initialization");
181     if (domain_list_match(flush_domains, site) != 0) {
182 	if (warn_compat_break_flush_domains)
183 	    msg_info("using backwards-compatible default setting "
184 		     VAR_RELAY_DOMAINS "=$mydestination to flush "
185 		     "mail for domain \"%s\"", site);
186 	status = mail_command_client(MAIL_CLASS_PUBLIC, var_flush_service,
187 			  SEND_ATTR_STR(MAIL_ATTR_REQ, FLUSH_REQ_SEND_SITE),
188 				     SEND_ATTR_STR(MAIL_ATTR_SITE, site),
189 				     ATTR_TYPE_END);
190     } else if (flush_domains->error == 0)
191 	status = FLUSH_STAT_DENY;
192     else
193 	status = FLUSH_STAT_FAIL;
194 
195     if (msg_verbose)
196 	msg_info("%s: site %s status %d", myname, site, status);
197 
198     return (status);
199 }
200 
201 /* flush_send_file - deliver specific message */
202 
203 int     flush_send_file(const char *queue_id)
204 {
205     const char *myname = "flush_send_file";
206     int     status;
207 
208     if (msg_verbose)
209 	msg_info("%s: queue_id %s", myname, queue_id);
210 
211     /*
212      * Require that the service is turned on.
213      */
214     status = mail_command_client(MAIL_CLASS_PUBLIC, var_flush_service,
215 			  SEND_ATTR_STR(MAIL_ATTR_REQ, FLUSH_REQ_SEND_FILE),
216 				 SEND_ATTR_STR(MAIL_ATTR_QUEUEID, queue_id),
217 				 ATTR_TYPE_END);
218 
219     if (msg_verbose)
220 	msg_info("%s: queue_id %s status %d", myname, queue_id, status);
221 
222     return (status);
223 }
224 
225 /* flush_add - inform "fast flush" cache manager */
226 
227 int     flush_add(const char *site, const char *queue_id)
228 {
229     const char *myname = "flush_add";
230     int     status;
231 
232     if (msg_verbose)
233 	msg_info("%s: site %s id %s", myname, site, queue_id);
234 
235     /*
236      * Don't bother the server if the service is turned off, or if the site
237      * is not eligible.
238      */
239     if (flush_domains == 0)
240 	msg_panic("missing flush client initialization");
241     if (domain_list_match(flush_domains, site) != 0) {
242 	if (warn_compat_break_flush_domains)
243 	    msg_info("using backwards-compatible default setting "
244 		     VAR_RELAY_DOMAINS "=$mydestination to update "
245 		     "fast-flush logfile for domain \"%s\"", site);
246 	status = mail_command_client(MAIL_CLASS_PUBLIC, var_flush_service,
247 				SEND_ATTR_STR(MAIL_ATTR_REQ, FLUSH_REQ_ADD),
248 				     SEND_ATTR_STR(MAIL_ATTR_SITE, site),
249 				 SEND_ATTR_STR(MAIL_ATTR_QUEUEID, queue_id),
250 				     ATTR_TYPE_END);
251     } else if (flush_domains->error == 0)
252 	status = FLUSH_STAT_DENY;
253     else
254 	status = FLUSH_STAT_FAIL;
255 
256     if (msg_verbose)
257 	msg_info("%s: site %s id %s status %d", myname, site, queue_id,
258 		 status);
259 
260     return (status);
261 }
262