xref: /titanic_52/usr/src/cmd/sendmail/libmilter/libmilter.h (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1999-2003 Sendmail, Inc. and its suppliers.
3*7c478bd9Sstevel@tonic-gate  *	All rights reserved.
4*7c478bd9Sstevel@tonic-gate  *
5*7c478bd9Sstevel@tonic-gate  * By using this file, you agree to the terms and conditions set
6*7c478bd9Sstevel@tonic-gate  * forth in the LICENSE file which can be found at the top level of
7*7c478bd9Sstevel@tonic-gate  * the sendmail distribution.
8*7c478bd9Sstevel@tonic-gate  */
9*7c478bd9Sstevel@tonic-gate 
10*7c478bd9Sstevel@tonic-gate /*
11*7c478bd9Sstevel@tonic-gate **  LIBMILTER.H -- include file for mail filter library functions
12*7c478bd9Sstevel@tonic-gate */
13*7c478bd9Sstevel@tonic-gate 
14*7c478bd9Sstevel@tonic-gate #ifndef _LIBMILTER_H
15*7c478bd9Sstevel@tonic-gate # define _LIBMILTER_H	1
16*7c478bd9Sstevel@tonic-gate 
17*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
18*7c478bd9Sstevel@tonic-gate 
19*7c478bd9Sstevel@tonic-gate #include <sm/gen.h>
20*7c478bd9Sstevel@tonic-gate 
21*7c478bd9Sstevel@tonic-gate #ifdef _DEFINE
22*7c478bd9Sstevel@tonic-gate # define EXTERN
23*7c478bd9Sstevel@tonic-gate # define INIT(x)	= x
24*7c478bd9Sstevel@tonic-gate SM_IDSTR(MilterlId, "@(#)$Id: libmilter.h,v 8.50 2003/12/11 18:14:34 ca Exp $")
25*7c478bd9Sstevel@tonic-gate #else /* _DEFINE */
26*7c478bd9Sstevel@tonic-gate # define EXTERN extern
27*7c478bd9Sstevel@tonic-gate # define INIT(x)
28*7c478bd9Sstevel@tonic-gate #endif /* _DEFINE */
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #define NOT_SENDMAIL	1
32*7c478bd9Sstevel@tonic-gate #define _SOCK_ADDR	union bigsockaddr
33*7c478bd9Sstevel@tonic-gate #include "sendmail.h"
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate #include "libmilter/milter.h"
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate # define ValidSocket(sd)	((sd) >= 0)
38*7c478bd9Sstevel@tonic-gate # define INVALID_SOCKET		(-1)
39*7c478bd9Sstevel@tonic-gate # define closesocket		close
40*7c478bd9Sstevel@tonic-gate # define MI_SOCK_READ(s, b, l)	read(s, b, l)
41*7c478bd9Sstevel@tonic-gate # define MI_SOCK_READ_FAIL(x)	((x) < 0)
42*7c478bd9Sstevel@tonic-gate # define MI_SOCK_WRITE(s, b, l)	write(s, b, l)
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate # define thread_create(ptid,wr,arg) pthread_create(ptid, NULL, wr, arg)
45*7c478bd9Sstevel@tonic-gate # define sthread_get_id()	pthread_self()
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate typedef pthread_mutex_t smutex_t;
48*7c478bd9Sstevel@tonic-gate # define smutex_init(mp)	(pthread_mutex_init(mp, NULL) == 0)
49*7c478bd9Sstevel@tonic-gate # define smutex_destroy(mp)	(pthread_mutex_destroy(mp) == 0)
50*7c478bd9Sstevel@tonic-gate # define smutex_lock(mp)	(pthread_mutex_lock(mp) == 0)
51*7c478bd9Sstevel@tonic-gate # define smutex_unlock(mp)	(pthread_mutex_unlock(mp) == 0)
52*7c478bd9Sstevel@tonic-gate # define smutex_trylock(mp)	(pthread_mutex_trylock(mp) == 0)
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate #if SM_CONF_POLL
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate # include <poll.h>
57*7c478bd9Sstevel@tonic-gate # define MI_POLLSELECT  "poll"
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate # define MI_POLL_RD_FLAGS (POLLIN | POLLPRI)
60*7c478bd9Sstevel@tonic-gate # define MI_POLL_WR_FLAGS (POLLOUT)
61*7c478bd9Sstevel@tonic-gate # define MI_MS(timeout)	(((timeout)->tv_sec * 1000) + (timeout)->tv_usec)
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate # define FD_RD_VAR(rds, excs) struct pollfd rds
64*7c478bd9Sstevel@tonic-gate # define FD_WR_VAR(wrs) struct pollfd wrs
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate # define FD_RD_INIT(sd, rds, excs)			\
67*7c478bd9Sstevel@tonic-gate 		(rds).fd = (sd);			\
68*7c478bd9Sstevel@tonic-gate 		(rds).events = MI_POLL_RD_FLAGS;	\
69*7c478bd9Sstevel@tonic-gate 		(rds).revents = 0
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate # define FD_WR_INIT(sd, wrs)				\
72*7c478bd9Sstevel@tonic-gate 		(wrs).fd = (sd);			\
73*7c478bd9Sstevel@tonic-gate 		(wrs).events = MI_POLL_WR_FLAGS;	\
74*7c478bd9Sstevel@tonic-gate 		(wrs).revents = 0
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate # define FD_IS_RD_EXC(sd, rds, excs)	\
77*7c478bd9Sstevel@tonic-gate 		(((rds).revents & (POLLERR | POLLHUP | POLLNVAL)) != 0)
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate # define FD_IS_WR_RDY(sd, wrs)		\
80*7c478bd9Sstevel@tonic-gate 		(((wrs).revents & MI_POLL_WR_FLAGS) != 0)
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate # define FD_IS_RD_RDY(sd, rds, excs)			\
83*7c478bd9Sstevel@tonic-gate 		(((rds).revents & MI_POLL_RD_FLAGS) != 0)
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate # define FD_WR_READY(sd, excs, timeout)	\
86*7c478bd9Sstevel@tonic-gate 		poll(&(wrs), 1, MI_MS(timeout))
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate # define FD_RD_READY(sd, rds, excs, timeout)	\
89*7c478bd9Sstevel@tonic-gate 		poll(&(rds), 1, MI_MS(timeout))
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate #else /* SM_CONF_POLL */
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate # include <sm/fdset.h>
94*7c478bd9Sstevel@tonic-gate # define MI_POLLSELECT  "select"
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate # define FD_RD_VAR(rds, excs) fd_set rds, excs
97*7c478bd9Sstevel@tonic-gate # define FD_WR_VAR(wrs) fd_set wrs
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate # define FD_RD_INIT(sd, rds, excs)			\
100*7c478bd9Sstevel@tonic-gate 		FD_ZERO(&(rds));			\
101*7c478bd9Sstevel@tonic-gate 		FD_SET((unsigned int) (sd), &(rds));	\
102*7c478bd9Sstevel@tonic-gate 		FD_ZERO(&(excs));			\
103*7c478bd9Sstevel@tonic-gate 		FD_SET((unsigned int) (sd), &(excs))
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate # define FD_WR_INIT(sd, wrs)			\
106*7c478bd9Sstevel@tonic-gate 		FD_ZERO(&(wrs));			\
107*7c478bd9Sstevel@tonic-gate 		FD_SET((unsigned int) (sd), &(wrs));	\
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate # define FD_IS_RD_EXC(sd, rds, excs) FD_ISSET(sd, &(excs))
110*7c478bd9Sstevel@tonic-gate # define FD_IS_WR_RDY(sd, wrs) FD_ISSET((sd), &(wrs))
111*7c478bd9Sstevel@tonic-gate # define FD_IS_RD_RDY(sd, rds, excs) FD_ISSET((sd), &(rds))
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate # define FD_WR_READY(sd, wrs, timeout)	\
114*7c478bd9Sstevel@tonic-gate 		select((sd) + 1, NULL, &(wrs), NULL, (timeout))
115*7c478bd9Sstevel@tonic-gate # define FD_RD_READY(sd, rds, excs, timeout)	\
116*7c478bd9Sstevel@tonic-gate 		select((sd) + 1, &(rds), NULL, &(excs), (timeout))
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate #endif /* SM_CONF_POLL */
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate #include <sys/time.h>
121*7c478bd9Sstevel@tonic-gate 
122*7c478bd9Sstevel@tonic-gate /* version info */
123*7c478bd9Sstevel@tonic-gate #define MILTER_PRODUCT_NAME	"libmilter"
124*7c478bd9Sstevel@tonic-gate #define MILTER_VERSION		100
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate /* some defaults */
127*7c478bd9Sstevel@tonic-gate #define MI_TIMEOUT	7210		/* default timeout for read/write */
128*7c478bd9Sstevel@tonic-gate #define MI_CHK_TIME	5		/* checking whether to terminate */
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate #ifndef MI_SOMAXCONN
131*7c478bd9Sstevel@tonic-gate # if SOMAXCONN > 20
132*7c478bd9Sstevel@tonic-gate #  define MI_SOMAXCONN	SOMAXCONN
133*7c478bd9Sstevel@tonic-gate # else /* SOMAXCONN */
134*7c478bd9Sstevel@tonic-gate #  define MI_SOMAXCONN	20
135*7c478bd9Sstevel@tonic-gate # endif /* SOMAXCONN */
136*7c478bd9Sstevel@tonic-gate #endif /* ! MI_SOMAXCONN */
137*7c478bd9Sstevel@tonic-gate 
138*7c478bd9Sstevel@tonic-gate /* maximum number of repeated failures in mi_listener() */
139*7c478bd9Sstevel@tonic-gate #define MAX_FAILS_M	16	/* malloc() */
140*7c478bd9Sstevel@tonic-gate #define MAX_FAILS_T	16	/* thread creation */
141*7c478bd9Sstevel@tonic-gate #define MAX_FAILS_A	16	/* accept() */
142*7c478bd9Sstevel@tonic-gate #define MAX_FAILS_S	16	/* select() */
143*7c478bd9Sstevel@tonic-gate 
144*7c478bd9Sstevel@tonic-gate /* internal "commands", i.e., error codes */
145*7c478bd9Sstevel@tonic-gate #define SMFIC_TIMEOUT	((char) 1)	/* timeout */
146*7c478bd9Sstevel@tonic-gate #define SMFIC_SELECT	((char) 2)	/* select error */
147*7c478bd9Sstevel@tonic-gate #define SMFIC_MALLOC	((char) 3)	/* malloc error */
148*7c478bd9Sstevel@tonic-gate #define SMFIC_RECVERR	((char) 4)	/* recv() error */
149*7c478bd9Sstevel@tonic-gate #define SMFIC_EOF	((char) 5)	/* eof */
150*7c478bd9Sstevel@tonic-gate #define SMFIC_UNKNERR	((char) 6)	/* unknown error */
151*7c478bd9Sstevel@tonic-gate #define SMFIC_TOOBIG	((char) 7)	/* body chunk too big */
152*7c478bd9Sstevel@tonic-gate #define SMFIC_VALIDCMD	' '		/* first valid command */
153*7c478bd9Sstevel@tonic-gate 
154*7c478bd9Sstevel@tonic-gate /* hack */
155*7c478bd9Sstevel@tonic-gate #define smi_log		syslog
156*7c478bd9Sstevel@tonic-gate #define sm_dprintf	(void) printf
157*7c478bd9Sstevel@tonic-gate #define milter_ret	int
158*7c478bd9Sstevel@tonic-gate #define SMI_LOG_ERR	LOG_ERR
159*7c478bd9Sstevel@tonic-gate #define SMI_LOG_FATAL	LOG_ERR
160*7c478bd9Sstevel@tonic-gate #define SMI_LOG_WARN	LOG_WARNING
161*7c478bd9Sstevel@tonic-gate #define SMI_LOG_INFO	LOG_INFO
162*7c478bd9Sstevel@tonic-gate #define SMI_LOG_DEBUG	LOG_DEBUG
163*7c478bd9Sstevel@tonic-gate 
164*7c478bd9Sstevel@tonic-gate /* stop? */
165*7c478bd9Sstevel@tonic-gate #define MILTER_CONT	0
166*7c478bd9Sstevel@tonic-gate #define MILTER_STOP	1
167*7c478bd9Sstevel@tonic-gate #define MILTER_ABRT	2
168*7c478bd9Sstevel@tonic-gate 
169*7c478bd9Sstevel@tonic-gate /* functions */
170*7c478bd9Sstevel@tonic-gate extern int	mi_handle_session __P((SMFICTX_PTR));
171*7c478bd9Sstevel@tonic-gate extern int	mi_engine __P((SMFICTX_PTR));
172*7c478bd9Sstevel@tonic-gate extern int	mi_listener __P((char *, int, smfiDesc_ptr, time_t, int));
173*7c478bd9Sstevel@tonic-gate extern void	mi_clr_macros __P((SMFICTX_PTR, int));
174*7c478bd9Sstevel@tonic-gate extern int	mi_stop __P((void));
175*7c478bd9Sstevel@tonic-gate extern int	mi_control_startup __P((char *));
176*7c478bd9Sstevel@tonic-gate extern void	mi_stop_milters __P((int));
177*7c478bd9Sstevel@tonic-gate extern void	mi_clean_signals __P((void));
178*7c478bd9Sstevel@tonic-gate extern struct hostent *mi_gethostbyname __P((char *, int));
179*7c478bd9Sstevel@tonic-gate extern int	mi_inet_pton __P((int, const char *, void *));
180*7c478bd9Sstevel@tonic-gate extern void	mi_closener __P((void));
181*7c478bd9Sstevel@tonic-gate extern int	mi_opensocket __P((char *, int, int, bool, smfiDesc_ptr));
182*7c478bd9Sstevel@tonic-gate 
183*7c478bd9Sstevel@tonic-gate /* communication functions */
184*7c478bd9Sstevel@tonic-gate extern char	*mi_rd_cmd __P((socket_t, struct timeval *, char *, size_t *, char *));
185*7c478bd9Sstevel@tonic-gate extern int	mi_wr_cmd __P((socket_t, struct timeval *, int, char *, size_t));
186*7c478bd9Sstevel@tonic-gate extern bool	mi_sendok __P((SMFICTX_PTR, int));
187*7c478bd9Sstevel@tonic-gate 
188*7c478bd9Sstevel@tonic-gate 
189*7c478bd9Sstevel@tonic-gate #endif /* ! _LIBMILTER_H */
190