xref: /illumos-gate/usr/src/cmd/sendmail/libmilter/libmilter.h (revision 22028508fd28d36ff74dc02c5774a8ba1f0db045)
1 /*
2  * Copyright (c) 1999-2003, 2006 Sendmail, Inc. and its suppliers.
3  *	All rights reserved.
4  *
5  * By using this file, you agree to the terms and conditions set
6  * forth in the LICENSE file which can be found at the top level of
7  * the sendmail distribution.
8  */
9 
10 /*
11 **  LIBMILTER.H -- include file for mail filter library functions
12 */
13 
14 #ifndef _LIBMILTER_H
15 # define _LIBMILTER_H	1
16 
17 #include <sm/gen.h>
18 
19 #ifdef _DEFINE
20 # define EXTERN
21 # define INIT(x)	= x
22 SM_IDSTR(MilterlId, "@(#)$Id: libmilter.h,v 8.77 2008/11/25 18:28:18 ca Exp $")
23 #else /* _DEFINE */
24 # define EXTERN extern
25 # define INIT(x)
26 #endif /* _DEFINE */
27 
28 
29 #include "sm/tailq.h"
30 
31 #define NOT_SENDMAIL	1
32 #define _SOCK_ADDR	union bigsockaddr
33 #include "sendmail.h"
34 
35 #ifdef SM_ASSERT
36 #undef SM_ASSERT
37 #endif
38 #ifndef SM_ASSERT
39 #include <assert.h>
40 #define SM_ASSERT(x) assert(x)
41 #endif
42 
43 #include "libmilter/milter.h"
44 
45 #define MAX_MACROS_ENTRIES	7	/* max size of macro pointer array */
46 
47 typedef SM_TAILQ_HEAD(, smfi_str)	smfi_hd_T;
48 typedef struct smfi_str smfi_str_S;
49 
50 /*
51 **  Context for one milter session.
52 **
53 **  Notes:
54 **	There is a 1-1 correlation between a sendmail SMTP server process,
55 **	an SMTP session, and an milter context. Due to the nature of SMTP
56 **	session handling in sendmail 8, this libmilter implementation deals
57 **	only with a single SMTP session per MTA - libmilter connection.
58 **
59 **	There is no "global" context for libmilter, global variables are
60 **	just that (they are not "collected" in a context).
61 **
62 **  Implementation hint:
63 **  macros are stored in mac_buf[] as sequence of:
64 **  macro_name \0 macro_value
65 **  (just as read from the MTA)
66 **  mac_ptr is a list of pointers into mac_buf to the beginning of each
67 **  entry, i.e., macro_name, macro_value, ...
68 */
69 
70 struct smfi_str
71 {
72 	sthread_t	ctx_id;		/* thread id */
73 	socket_t	ctx_sd;		/* socket descriptor */
74 	int		ctx_dbg;	/* debug level */
75 	time_t		ctx_timeout;	/* timeout */
76 	int		ctx_state;	/* state */
77 	smfiDesc_ptr	ctx_smfi;	/* filter description */
78 
79 	int		ctx_prot_vers;	/* libmilter protocol version */
80 	unsigned long	ctx_aflags;	/* milter action flags */
81 
82 	unsigned long	ctx_pflags;	/* milter protocol flags */
83 
84 	/*
85 	**  milter protocol flags that are sent to the MTA;
86 	**  this is the same as ctx_pflags except for those flags that
87 	**  are not offered by the MTA but emulated in libmilter.
88 	*/
89 
90 	unsigned long	ctx_pflags2mta;
91 
92 	/*
93 	**  milter protocol version that is sent to the MTA;
94 	**  this is the same as ctx_prot_vers unless the
95 	**  MTA protocol version (ctx_mta_prot_vers) is smaller
96 	**  but still "acceptable".
97 	*/
98 
99 	int		ctx_prot_vers2mta;
100 
101 	char		**ctx_mac_ptr[MAX_MACROS_ENTRIES];
102 	char		*ctx_mac_buf[MAX_MACROS_ENTRIES];
103 	char		*ctx_mac_list[MAX_MACROS_ENTRIES];
104 	char		*ctx_reply;	/* reply code */
105 	void		*ctx_privdata;	/* private data */
106 
107 	int		ctx_mta_prot_vers;	/* MTA protocol version */
108 	unsigned long	ctx_mta_pflags;	/* MTA protocol flags */
109 	unsigned long	ctx_mta_aflags;	/* MTA action flags */
110 
111 #if _FFR_THREAD_MONITOR
112 	time_t		ctx_start;	/* start time of thread */
113 	SM_TAILQ_ENTRY(smfi_str)	ctx_mon_link;
114 #endif /* _FFR_THREAD_MONITOR */
115 
116 #if _FFR_WORKERS_POOL
117 	long		ctx_sid;	/* session identifier */
118 	int		ctx_wstate;	/* state of the session (worker pool) */
119 	int		ctx_wait;	/* elapsed time waiting for sm cmd */
120 	SM_TAILQ_ENTRY(smfi_str)	ctx_link;
121 #endif /* _FFR_WORKERS_POOL */
122 };
123 
124 # define ValidSocket(sd)	((sd) >= 0)
125 # define INVALID_SOCKET		(-1)
126 # define closesocket		close
127 # define MI_SOCK_READ(s, b, l)	read(s, b, l)
128 # define MI_SOCK_READ_FAIL(x)	((x) < 0)
129 # define MI_SOCK_WRITE(s, b, l)	write(s, b, l)
130 
131 # define thread_create(ptid,wr,arg) pthread_create(ptid, NULL, wr, arg)
132 # define sthread_get_id()	pthread_self()
133 
134 typedef pthread_mutex_t smutex_t;
135 # define smutex_init(mp)	(pthread_mutex_init(mp, NULL) == 0)
136 # define smutex_destroy(mp)	(pthread_mutex_destroy(mp) == 0)
137 # define smutex_lock(mp)	(pthread_mutex_lock(mp) == 0)
138 # define smutex_unlock(mp)	(pthread_mutex_unlock(mp) == 0)
139 # define smutex_trylock(mp)	(pthread_mutex_trylock(mp) == 0)
140 
141 #if _FFR_WORKERS_POOL
142 /* SM_CONF_POLL shall be defined with _FFR_WORKERS_POOL */
143 # if !SM_CONF_POLL
144 #  define SM_CONF_POLL 1
145 # endif /* SM_CONF_POLL */
146 #endif /* _FFR_WORKERS_POOL */
147 
148 typedef pthread_cond_t scond_t;
149 #define scond_init(cp)			pthread_cond_init(cp, NULL)
150 #define scond_destroy(cp)		pthread_cond_destroy(cp)
151 #define scond_wait(cp, mp)		pthread_cond_wait(cp, mp)
152 #define scond_signal(cp)		pthread_cond_signal(cp)
153 #define scond_broadcast(cp)		pthread_cond_broadcast(cp)
154 #define scond_timedwait(cp, mp, to)					\
155 	do								\
156 	{								\
157 		struct timespec timeout;				\
158 		struct timeval now;					\
159 		gettimeofday(&now, NULL);				\
160 		timeout.tv_sec = now.tv_sec + to;			\
161 		timeout.tv_nsec = now.tv_usec / 1000;			\
162 		r = pthread_cond_timedwait(cp,mp,&timeout);		\
163 		if (r != 0 && r != ETIMEDOUT)				\
164 			smi_log(SMI_LOG_ERR,				\
165 				"pthread_cond_timedwait error %d", r);	\
166 	} while (0)
167 
168 
169 #if SM_CONF_POLL
170 
171 # include <poll.h>
172 # define MI_POLLSELECT  "poll"
173 
174 # define MI_POLL_RD_FLAGS (POLLIN | POLLPRI)
175 # define MI_POLL_WR_FLAGS (POLLOUT)
176 # define MI_MS(timeout)	(((timeout)->tv_sec * 1000) + (timeout)->tv_usec)
177 
178 # define FD_RD_VAR(rds, excs) struct pollfd rds
179 # define FD_WR_VAR(wrs) struct pollfd wrs
180 
181 # define FD_RD_INIT(sd, rds, excs)			\
182 		(rds).fd = (sd);			\
183 		(rds).events = MI_POLL_RD_FLAGS;	\
184 		(rds).revents = 0
185 
186 # define FD_WR_INIT(sd, wrs)				\
187 		(wrs).fd = (sd);			\
188 		(wrs).events = MI_POLL_WR_FLAGS;	\
189 		(wrs).revents = 0
190 
191 # define FD_IS_RD_EXC(sd, rds, excs)	\
192 		(((rds).revents & (POLLERR | POLLHUP | POLLNVAL)) != 0)
193 
194 # define FD_IS_WR_RDY(sd, wrs)		\
195 		(((wrs).revents & MI_POLL_WR_FLAGS) != 0)
196 
197 # define FD_IS_RD_RDY(sd, rds, excs)			\
198 		(((rds).revents & MI_POLL_RD_FLAGS) != 0)
199 
200 # define FD_WR_READY(sd, excs, timeout)	\
201 		poll(&(wrs), 1, MI_MS(timeout))
202 
203 # define FD_RD_READY(sd, rds, excs, timeout)	\
204 		poll(&(rds), 1, MI_MS(timeout))
205 
206 #else /* SM_CONF_POLL */
207 
208 # include <sm/fdset.h>
209 # define MI_POLLSELECT  "select"
210 
211 # define FD_RD_VAR(rds, excs) fd_set rds, excs
212 # define FD_WR_VAR(wrs) fd_set wrs
213 
214 # define FD_RD_INIT(sd, rds, excs)			\
215 		FD_ZERO(&(rds));			\
216 		FD_SET((unsigned int) (sd), &(rds));	\
217 		FD_ZERO(&(excs));			\
218 		FD_SET((unsigned int) (sd), &(excs))
219 
220 # define FD_WR_INIT(sd, wrs)			\
221 		FD_ZERO(&(wrs));			\
222 		FD_SET((unsigned int) (sd), &(wrs))
223 
224 # define FD_IS_RD_EXC(sd, rds, excs) FD_ISSET(sd, &(excs))
225 # define FD_IS_WR_RDY(sd, wrs) FD_ISSET((sd), &(wrs))
226 # define FD_IS_RD_RDY(sd, rds, excs) FD_ISSET((sd), &(rds))
227 
228 # define FD_WR_READY(sd, wrs, timeout)	\
229 		select((sd) + 1, NULL, &(wrs), NULL, (timeout))
230 # define FD_RD_READY(sd, rds, excs, timeout)	\
231 		select((sd) + 1, &(rds), NULL, &(excs), (timeout))
232 
233 #endif /* SM_CONF_POLL */
234 
235 #include <sys/time.h>
236 
237 /* some defaults */
238 #define MI_TIMEOUT	7210		/* default timeout for read/write */
239 #define MI_CHK_TIME	5		/* checking whether to terminate */
240 
241 #ifndef MI_SOMAXCONN
242 # if SOMAXCONN > 20
243 #  define MI_SOMAXCONN	SOMAXCONN
244 # else /* SOMAXCONN */
245 #  define MI_SOMAXCONN	20
246 # endif /* SOMAXCONN */
247 #endif /* ! MI_SOMAXCONN */
248 
249 /* maximum number of repeated failures in mi_listener() */
250 #define MAX_FAILS_M	16	/* malloc() */
251 #define MAX_FAILS_T	16	/* thread creation */
252 #define MAX_FAILS_A	16	/* accept() */
253 #define MAX_FAILS_S	16	/* select() */
254 
255 /* internal "commands", i.e., error codes */
256 #define SMFIC_TIMEOUT	((char) 1)	/* timeout */
257 #define SMFIC_SELECT	((char) 2)	/* select error */
258 #define SMFIC_MALLOC	((char) 3)	/* malloc error */
259 #define SMFIC_RECVERR	((char) 4)	/* recv() error */
260 #define SMFIC_EOF	((char) 5)	/* eof */
261 #define SMFIC_UNKNERR	((char) 6)	/* unknown error */
262 #define SMFIC_TOOBIG	((char) 7)	/* body chunk too big */
263 #define SMFIC_VALIDCMD	' '		/* first valid command */
264 
265 /* hack */
266 #define smi_log		syslog
267 #define sm_dprintf	(void) printf
268 #define milter_ret	int
269 #define SMI_LOG_ERR	LOG_ERR
270 #define SMI_LOG_FATAL	LOG_ERR
271 #define SMI_LOG_WARN	LOG_WARNING
272 #define SMI_LOG_INFO	LOG_INFO
273 #define SMI_LOG_DEBUG	LOG_DEBUG
274 
275 /* stop? */
276 #define MILTER_CONT	0
277 #define MILTER_STOP	1
278 #define MILTER_ABRT	2
279 
280 /* functions */
281 extern int	mi_handle_session __P((SMFICTX_PTR));
282 extern int	mi_engine __P((SMFICTX_PTR));
283 extern int	mi_listener __P((char *, int, smfiDesc_ptr, time_t, int));
284 extern void	mi_clr_macros __P((SMFICTX_PTR, int));
285 extern void	mi_clr_ctx __P((SMFICTX_PTR));
286 extern int	mi_stop __P((void));
287 extern int	mi_control_startup __P((char *));
288 extern void	mi_stop_milters __P((int));
289 extern void	mi_clean_signals __P((void));
290 extern struct hostent *mi_gethostbyname __P((char *, int));
291 extern int	mi_inet_pton __P((int, const char *, void *));
292 extern void	mi_closener __P((void));
293 extern int	mi_opensocket __P((char *, int, int, bool, smfiDesc_ptr));
294 
295 /* communication functions */
296 extern char	*mi_rd_cmd __P((socket_t, struct timeval *, char *, size_t *, char *));
297 extern int	mi_wr_cmd __P((socket_t, struct timeval *, int, char *, size_t));
298 extern bool	mi_sendok __P((SMFICTX_PTR, int));
299 
300 
301 #if _FFR_THREAD_MONITOR
302 extern bool Monitor;
303 
304 #define MI_MONITOR_INIT()	mi_monitor_init()
305 #define MI_MONITOR_BEGIN(ctx, cmd)			\
306 	do						\
307 	{						\
308 		if (Monitor)				\
309 			mi_monitor_work_begin(ctx, cmd);\
310 	} while (0)
311 
312 #define MI_MONITOR_END(ctx, cmd)			\
313 	do						\
314 	{						\
315 		if (Monitor)				\
316 			mi_monitor_work_end(ctx, cmd);	\
317 	} while (0)
318 
319 int mi_monitor_init __P((void));
320 int mi_monitor_work_begin __P((SMFICTX_PTR, int));
321 int mi_monitor_work_end __P((SMFICTX_PTR, int));
322 
323 #else /* _FFR_THREAD_MONITOR */
324 #define MI_MONITOR_INIT()	MI_SUCCESS
325 #define MI_MONITOR_BEGIN(ctx, cmd)
326 #define MI_MONITOR_END(ctx, cmd)
327 #endif /* _FFR_THREAD_MONITOR */
328 
329 #if _FFR_WORKERS_POOL
330 extern int mi_pool_manager_init __P((void));
331 extern int mi_pool_controller_init __P((void));
332 extern int mi_start_session __P((SMFICTX_PTR));
333 #endif /* _FFR_WORKERS_POOL */
334 
335 #endif /* ! _LIBMILTER_H */
336