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