libmilter.h revision 363466
1285SN/A/* 2367SN/A * Copyright (c) 1999-2003, 2006 Proofpoint, Inc. and its suppliers. 3285SN/A * All rights reserved. 4285SN/A * 5285SN/A * By using this file, you agree to the terms and conditions set 6285SN/A * forth in the LICENSE file which can be found at the top level of 7285SN/A * the sendmail distribution. 8285SN/A */ 9285SN/A 10285SN/A/* 11285SN/A** LIBMILTER.H -- include file for mail filter library functions 12285SN/A*/ 13285SN/A 14285SN/A#ifndef _LIBMILTER_H 15285SN/A# define _LIBMILTER_H 1 16285SN/A 17285SN/A#include <sm/gen.h> 18285SN/A 19285SN/A#ifdef _DEFINE 20285SN/A# define EXTERN 21285SN/A# define INIT(x) = x 22285SN/ASM_IDSTR(MilterlId, "@(#)$Id: libmilter.h,v 8.78 2013-11-22 20:51:36 ca Exp $") 23285SN/A#else 24285SN/A# define EXTERN extern 25285SN/A# define INIT(x) 26285SN/A#endif 27285SN/A 28285SN/A 29285SN/A#include "sm/tailq.h" 30285SN/A 31285SN/A#define NOT_SENDMAIL 1 32285SN/A#define _SOCK_ADDR union bigsockaddr 33285SN/A#include "sendmail.h" 34285SN/A 35285SN/A#ifdef SM_ASSERT 36285SN/A#undef SM_ASSERT 37285SN/A#endif 38285SN/A#ifndef SM_ASSERT 39285SN/A#include <assert.h> 40285SN/A#define SM_ASSERT(x) assert(x) 41285SN/A#endif 42285SN/A 43285SN/A#include "libmilter/milter.h" 44285SN/A 45285SN/A#define MAX_MACROS_ENTRIES 7 /* max size of macro pointer array */ 46285SN/A 47285SN/Atypedef SM_TAILQ_HEAD(, smfi_str) smfi_hd_T; 48285SN/Atypedef struct smfi_str smfi_str_S; 49285SN/A 50285SN/A/* 51285SN/A** Context for one milter session. 52285SN/A** 53285SN/A** Notes: 54285SN/A** There is a 1-1 correlation between a sendmail SMTP server process, 55285SN/A** an SMTP session, and an milter context. Due to the nature of SMTP 56285SN/A** session handling in sendmail 8, this libmilter implementation deals 57285SN/A** only with a single SMTP session per MTA - libmilter connection. 58285SN/A** 59285SN/A** There is no "global" context for libmilter, global variables are 60285SN/A** just that (they are not "collected" in a context). 61285SN/A** 62285SN/A** Implementation hint: 63367SN/A** macros are stored in mac_buf[] as sequence of: 64367SN/A** macro_name \0 macro_value 65285SN/A** (just as read from the MTA) 66367SN/A** mac_ptr is a list of pointers into mac_buf to the beginning of each 67367SN/A** entry, i.e., macro_name, macro_value, ... 68285SN/A*/ 69285SN/A 70285SN/Astruct smfi_str 71285SN/A{ 72285SN/A sthread_t ctx_id; /* thread id */ 73285SN/A socket_t ctx_sd; /* socket descriptor */ 74285SN/A int ctx_dbg; /* debug level */ 75285SN/A time_t ctx_timeout; /* timeout */ 76285SN/A int ctx_state; /* state */ 77285SN/A smfiDesc_ptr ctx_smfi; /* filter description */ 78285SN/A 79285SN/A int ctx_prot_vers; /* libmilter protocol version */ 80285SN/A unsigned long ctx_aflags; /* milter action flags */ 81285SN/A 82285SN/A unsigned long ctx_pflags; /* milter protocol flags */ 83285SN/A 84285SN/A /* 85285SN/A ** milter protocol flags that are sent to the MTA; 86285SN/A ** this is the same as ctx_pflags except for those flags that 87285SN/A ** are not offered by the MTA but emulated in libmilter. 88285SN/A */ 89285SN/A 90285SN/A unsigned long ctx_pflags2mta; 91285SN/A 92285SN/A /* 93285SN/A ** milter protocol version that is sent to the MTA; 94285SN/A ** this is the same as ctx_prot_vers unless the 95285SN/A ** MTA protocol version (ctx_mta_prot_vers) is smaller 96285SN/A ** but still "acceptable". 97285SN/A */ 98285SN/A 99367SN/A int ctx_prot_vers2mta; 100285SN/A 101285SN/A char **ctx_mac_ptr[MAX_MACROS_ENTRIES]; 102285SN/A char *ctx_mac_buf[MAX_MACROS_ENTRIES]; 103285SN/A char *ctx_mac_list[MAX_MACROS_ENTRIES]; 104285SN/A char *ctx_reply; /* reply code */ 105285SN/A void *ctx_privdata; /* private data */ 106367SN/A 107285SN/A int ctx_mta_prot_vers; /* MTA protocol version */ 108285SN/A unsigned long ctx_mta_pflags; /* MTA protocol flags */ 109285SN/A unsigned long ctx_mta_aflags; /* MTA action flags */ 110285SN/A 111285SN/A#if _FFR_THREAD_MONITOR 112285SN/A time_t ctx_start; /* start time of thread */ 113367SN/A SM_TAILQ_ENTRY(smfi_str) ctx_mon_link; 114285SN/A#endif 115285SN/A 116285SN/A#if _FFR_WORKERS_POOL 117285SN/A long ctx_sid; /* session identifier */ 118285SN/A int ctx_wstate; /* state of the session (worker pool) */ 119285SN/A int ctx_wait; /* elapsed time waiting for sm cmd */ 120367SN/A SM_TAILQ_ENTRY(smfi_str) ctx_link; 121285SN/A#endif /* _FFR_WORKERS_POOL */ 122285SN/A}; 123367SN/A 124285SN/A# define ValidSocket(sd) ((sd) >= 0) 125367SN/A# define INVALID_SOCKET (-1) 126285SN/A# define closesocket close 127285SN/A# define MI_SOCK_READ(s, b, l) read(s, b, l) 128285SN/A# define MI_SOCK_READ_FAIL(x) ((x) < 0) 129285SN/A# define MI_SOCK_WRITE(s, b, l) write(s, b, l) 130285SN/A 131285SN/A# define thread_create(ptid,wr,arg) pthread_create(ptid, NULL, wr, arg) 132285SN/A# define sthread_get_id() pthread_self() 133285SN/A 134285SN/Atypedef pthread_mutex_t smutex_t; 135285SN/A# define smutex_init(mp) (pthread_mutex_init(mp, NULL) == 0) 136367SN/A# define smutex_destroy(mp) (pthread_mutex_destroy(mp) == 0) 137367SN/A# define smutex_lock(mp) (pthread_mutex_lock(mp) == 0) 138285SN/A# define smutex_unlock(mp) (pthread_mutex_unlock(mp) == 0) 139367SN/A# define smutex_trylock(mp) (pthread_mutex_trylock(mp) == 0) 140367SN/A 141285SN/A#if _FFR_WORKERS_POOL 142367SN/A/* SM_CONF_POLL shall be defined with _FFR_WORKERS_POOL */ 143367SN/A# if !SM_CONF_POLL 144285SN/A# define SM_CONF_POLL 1 145367SN/A# endif 146367SN/A#endif /* _FFR_WORKERS_POOL */ 147285SN/A 148367SN/Atypedef pthread_cond_t scond_t; 149367SN/A#define scond_init(cp) pthread_cond_init(cp, NULL) 150285SN/A#define scond_destroy(cp) pthread_cond_destroy(cp) 151285SN/A#define scond_wait(cp, mp) pthread_cond_wait(cp, mp) 152285SN/A#define scond_signal(cp) pthread_cond_signal(cp) 153285SN/A#define scond_broadcast(cp) pthread_cond_broadcast(cp) 154285SN/A#define scond_timedwait(cp, mp, to) \ 155285SN/A do \ 156285SN/A { \ 157285SN/A struct timespec timeout; \ 158285SN/A struct timeval now; \ 159285SN/A gettimeofday(&now, NULL); \ 160285SN/A timeout.tv_sec = now.tv_sec + to; \ 161285SN/A timeout.tv_nsec = now.tv_usec / 1000; \ 162285SN/A r = pthread_cond_timedwait(cp,mp,&timeout); \ 163285SN/A if (r != 0 && r != ETIMEDOUT) \ 164285SN/A smi_log(SMI_LOG_ERR, \ 165285SN/A "pthread_cond_timedwait error %d", r); \ 166285SN/A } while (0) 167285SN/A 168285SN/A 169367SN/A#if SM_CONF_POLL 170285SN/A 171367SN/A# include <poll.h> 172285SN/A# define MI_POLLSELECT "poll" 173285SN/A 174285SN/A# define MI_POLL_RD_FLAGS (POLLIN | POLLPRI) 175285SN/A# define MI_POLL_WR_FLAGS (POLLOUT) 176285SN/A# define MI_MS(timeout) (((timeout)->tv_sec * 1000) + (((timeout)->tv_usec) / 1000)) 177285SN/A 178285SN/A# define FD_RD_VAR(rds, excs) struct pollfd rds 179285SN/A# define FD_WR_VAR(wrs) struct pollfd wrs 180285SN/A 181285SN/A# define FD_RD_INIT(sd, rds, excs) \ 182285SN/A (rds).fd = (sd); \ 183285SN/A (rds).events = MI_POLL_RD_FLAGS; \ 184285SN/A (rds).revents = 0 185285SN/A 186285SN/A# define FD_WR_INIT(sd, wrs) \ 187285SN/A (wrs).fd = (sd); \ 188285SN/A (wrs).events = MI_POLL_WR_FLAGS; \ 189367SN/A (wrs).revents = 0 190285SN/A 191367SN/A# define FD_IS_RD_EXC(sd, rds, excs) \ 192285SN/A (((rds).revents & (POLLERR | POLLHUP | POLLNVAL)) != 0) 193285SN/A 194285SN/A# define FD_IS_WR_RDY(sd, wrs) \ 195285SN/A (((wrs).revents & MI_POLL_WR_FLAGS) != 0) 196285SN/A 197285SN/A# define FD_IS_RD_RDY(sd, rds, excs) \ 198285SN/A (((rds).revents & MI_POLL_RD_FLAGS) != 0) 199285SN/A 200285SN/A# define FD_WR_READY(sd, wrs, timeout) \ 201285SN/A poll(&(wrs), 1, MI_MS(timeout)) 202285SN/A 203285SN/A# define FD_RD_READY(sd, rds, excs, timeout) \ 204285SN/A poll(&(rds), 1, MI_MS(timeout)) 205285SN/A 206285SN/A#else /* SM_CONF_POLL */ 207285SN/A 208285SN/A# include <sm/fdset.h> 209367SN/A# define MI_POLLSELECT "select" 210285SN/A 211285SN/A# define FD_RD_VAR(rds, excs) fd_set rds, excs 212367SN/A# define FD_WR_VAR(wrs) fd_set wrs 213367SN/A 214367SN/A# define FD_RD_INIT(sd, rds, excs) \ 215367SN/A FD_ZERO(&(rds)); \ 216367SN/A FD_SET((unsigned int) (sd), &(rds)); \ 217367SN/A FD_ZERO(&(excs)); \ 218367SN/A FD_SET((unsigned int) (sd), &(excs)) 219367SN/A 220367SN/A# define FD_WR_INIT(sd, wrs) \ 221367SN/A FD_ZERO(&(wrs)); \ 222367SN/A FD_SET((unsigned int) (sd), &(wrs)) 223367SN/A 224367SN/A# define FD_IS_RD_EXC(sd, rds, excs) FD_ISSET(sd, &(excs)) 225367SN/A# define FD_IS_WR_RDY(sd, wrs) FD_ISSET((sd), &(wrs)) 226367SN/A# define FD_IS_RD_RDY(sd, rds, excs) FD_ISSET((sd), &(rds)) 227285SN/A 228285SN/A# define FD_WR_READY(sd, wrs, timeout) \ 229285SN/A select((sd) + 1, NULL, &(wrs), NULL, (timeout)) 230285SN/A# define FD_RD_READY(sd, rds, excs, timeout) \ 231285SN/A select((sd) + 1, &(rds), NULL, &(excs), (timeout)) 232285SN/A 233285SN/A#endif /* SM_CONF_POLL */ 234285SN/A 235285SN/A#include <sys/time.h> 236285SN/A 237285SN/A/* some defaults */ 238285SN/A#define MI_TIMEOUT 7210 /* default timeout for read/write */ 239285SN/A#define MI_CHK_TIME 5 /* checking whether to terminate */ 240285SN/A 241285SN/A#ifndef MI_SOMAXCONN 242285SN/A# if SOMAXCONN > 20 243285SN/A# define MI_SOMAXCONN SOMAXCONN 244285SN/A# else 245# define MI_SOMAXCONN 20 246# endif 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 */ 281extern int mi_handle_session __P((SMFICTX_PTR)); 282extern int mi_engine __P((SMFICTX_PTR)); 283extern int mi_listener __P((char *, int, smfiDesc_ptr, time_t, int)); 284extern void mi_clr_macros __P((SMFICTX_PTR, int)); 285extern void mi_clr_ctx __P((SMFICTX_PTR)); 286extern int mi_stop __P((void)); 287extern int mi_control_startup __P((char *)); 288extern void mi_stop_milters __P((int)); 289extern void mi_clean_signals __P((void)); 290extern struct hostent *mi_gethostbyname __P((char *, int)); 291extern int mi_inet_pton __P((int, const char *, void *)); 292extern void mi_closener __P((void)); 293extern int mi_opensocket __P((char *, int, int, bool, smfiDesc_ptr)); 294 295/* communication functions */ 296extern char *mi_rd_cmd __P((socket_t, struct timeval *, char *, size_t *, char *)); 297extern int mi_wr_cmd __P((socket_t, struct timeval *, int, char *, size_t)); 298extern bool mi_sendok __P((SMFICTX_PTR, int)); 299 300 301#if _FFR_THREAD_MONITOR 302extern 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 319int mi_monitor_init __P((void)); 320int mi_monitor_work_begin __P((SMFICTX_PTR, int)); 321int 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 330extern int mi_pool_manager_init __P((void)); 331extern int mi_pool_controller_init __P((void)); 332extern int mi_start_session __P((SMFICTX_PTR)); 333#endif /* _FFR_WORKERS_POOL */ 334 335#endif /* ! _LIBMILTER_H */ 336