17c478bd9Sstevel@tonic-gate /* 2*445f2479Sjbeck * Copyright (c) 1999-2003, 2006 Sendmail, Inc. and its suppliers. 37c478bd9Sstevel@tonic-gate * All rights reserved. 47c478bd9Sstevel@tonic-gate * 57c478bd9Sstevel@tonic-gate * By using this file, you agree to the terms and conditions set 67c478bd9Sstevel@tonic-gate * forth in the LICENSE file which can be found at the top level of 77c478bd9Sstevel@tonic-gate * the sendmail distribution. 87c478bd9Sstevel@tonic-gate */ 97c478bd9Sstevel@tonic-gate 107c478bd9Sstevel@tonic-gate /* 117c478bd9Sstevel@tonic-gate ** LIBMILTER.H -- include file for mail filter library functions 127c478bd9Sstevel@tonic-gate */ 137c478bd9Sstevel@tonic-gate 147c478bd9Sstevel@tonic-gate #ifndef _LIBMILTER_H 157c478bd9Sstevel@tonic-gate # define _LIBMILTER_H 1 167c478bd9Sstevel@tonic-gate 177c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 187c478bd9Sstevel@tonic-gate 197c478bd9Sstevel@tonic-gate #include <sm/gen.h> 207c478bd9Sstevel@tonic-gate 217c478bd9Sstevel@tonic-gate #ifdef _DEFINE 227c478bd9Sstevel@tonic-gate # define EXTERN 237c478bd9Sstevel@tonic-gate # define INIT(x) = x 24*445f2479Sjbeck SM_IDSTR(MilterlId, "@(#)$Id: libmilter.h,v 8.51 2006/01/04 02:24:37 ca Exp $") 257c478bd9Sstevel@tonic-gate #else /* _DEFINE */ 267c478bd9Sstevel@tonic-gate # define EXTERN extern 277c478bd9Sstevel@tonic-gate # define INIT(x) 287c478bd9Sstevel@tonic-gate #endif /* _DEFINE */ 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gate 317c478bd9Sstevel@tonic-gate #define NOT_SENDMAIL 1 327c478bd9Sstevel@tonic-gate #define _SOCK_ADDR union bigsockaddr 337c478bd9Sstevel@tonic-gate #include "sendmail.h" 347c478bd9Sstevel@tonic-gate 357c478bd9Sstevel@tonic-gate #include "libmilter/milter.h" 367c478bd9Sstevel@tonic-gate 377c478bd9Sstevel@tonic-gate # define ValidSocket(sd) ((sd) >= 0) 387c478bd9Sstevel@tonic-gate # define INVALID_SOCKET (-1) 397c478bd9Sstevel@tonic-gate # define closesocket close 407c478bd9Sstevel@tonic-gate # define MI_SOCK_READ(s, b, l) read(s, b, l) 417c478bd9Sstevel@tonic-gate # define MI_SOCK_READ_FAIL(x) ((x) < 0) 427c478bd9Sstevel@tonic-gate # define MI_SOCK_WRITE(s, b, l) write(s, b, l) 437c478bd9Sstevel@tonic-gate 447c478bd9Sstevel@tonic-gate # define thread_create(ptid,wr,arg) pthread_create(ptid, NULL, wr, arg) 457c478bd9Sstevel@tonic-gate # define sthread_get_id() pthread_self() 467c478bd9Sstevel@tonic-gate 477c478bd9Sstevel@tonic-gate typedef pthread_mutex_t smutex_t; 487c478bd9Sstevel@tonic-gate # define smutex_init(mp) (pthread_mutex_init(mp, NULL) == 0) 497c478bd9Sstevel@tonic-gate # define smutex_destroy(mp) (pthread_mutex_destroy(mp) == 0) 507c478bd9Sstevel@tonic-gate # define smutex_lock(mp) (pthread_mutex_lock(mp) == 0) 517c478bd9Sstevel@tonic-gate # define smutex_unlock(mp) (pthread_mutex_unlock(mp) == 0) 527c478bd9Sstevel@tonic-gate # define smutex_trylock(mp) (pthread_mutex_trylock(mp) == 0) 537c478bd9Sstevel@tonic-gate 547c478bd9Sstevel@tonic-gate #if SM_CONF_POLL 557c478bd9Sstevel@tonic-gate 567c478bd9Sstevel@tonic-gate # include <poll.h> 577c478bd9Sstevel@tonic-gate # define MI_POLLSELECT "poll" 587c478bd9Sstevel@tonic-gate 597c478bd9Sstevel@tonic-gate # define MI_POLL_RD_FLAGS (POLLIN | POLLPRI) 607c478bd9Sstevel@tonic-gate # define MI_POLL_WR_FLAGS (POLLOUT) 617c478bd9Sstevel@tonic-gate # define MI_MS(timeout) (((timeout)->tv_sec * 1000) + (timeout)->tv_usec) 627c478bd9Sstevel@tonic-gate 637c478bd9Sstevel@tonic-gate # define FD_RD_VAR(rds, excs) struct pollfd rds 647c478bd9Sstevel@tonic-gate # define FD_WR_VAR(wrs) struct pollfd wrs 657c478bd9Sstevel@tonic-gate 667c478bd9Sstevel@tonic-gate # define FD_RD_INIT(sd, rds, excs) \ 677c478bd9Sstevel@tonic-gate (rds).fd = (sd); \ 687c478bd9Sstevel@tonic-gate (rds).events = MI_POLL_RD_FLAGS; \ 697c478bd9Sstevel@tonic-gate (rds).revents = 0 707c478bd9Sstevel@tonic-gate 717c478bd9Sstevel@tonic-gate # define FD_WR_INIT(sd, wrs) \ 727c478bd9Sstevel@tonic-gate (wrs).fd = (sd); \ 737c478bd9Sstevel@tonic-gate (wrs).events = MI_POLL_WR_FLAGS; \ 747c478bd9Sstevel@tonic-gate (wrs).revents = 0 757c478bd9Sstevel@tonic-gate 767c478bd9Sstevel@tonic-gate # define FD_IS_RD_EXC(sd, rds, excs) \ 777c478bd9Sstevel@tonic-gate (((rds).revents & (POLLERR | POLLHUP | POLLNVAL)) != 0) 787c478bd9Sstevel@tonic-gate 797c478bd9Sstevel@tonic-gate # define FD_IS_WR_RDY(sd, wrs) \ 807c478bd9Sstevel@tonic-gate (((wrs).revents & MI_POLL_WR_FLAGS) != 0) 817c478bd9Sstevel@tonic-gate 827c478bd9Sstevel@tonic-gate # define FD_IS_RD_RDY(sd, rds, excs) \ 837c478bd9Sstevel@tonic-gate (((rds).revents & MI_POLL_RD_FLAGS) != 0) 847c478bd9Sstevel@tonic-gate 857c478bd9Sstevel@tonic-gate # define FD_WR_READY(sd, excs, timeout) \ 867c478bd9Sstevel@tonic-gate poll(&(wrs), 1, MI_MS(timeout)) 877c478bd9Sstevel@tonic-gate 887c478bd9Sstevel@tonic-gate # define FD_RD_READY(sd, rds, excs, timeout) \ 897c478bd9Sstevel@tonic-gate poll(&(rds), 1, MI_MS(timeout)) 907c478bd9Sstevel@tonic-gate 917c478bd9Sstevel@tonic-gate #else /* SM_CONF_POLL */ 927c478bd9Sstevel@tonic-gate 937c478bd9Sstevel@tonic-gate # include <sm/fdset.h> 947c478bd9Sstevel@tonic-gate # define MI_POLLSELECT "select" 957c478bd9Sstevel@tonic-gate 967c478bd9Sstevel@tonic-gate # define FD_RD_VAR(rds, excs) fd_set rds, excs 977c478bd9Sstevel@tonic-gate # define FD_WR_VAR(wrs) fd_set wrs 987c478bd9Sstevel@tonic-gate 997c478bd9Sstevel@tonic-gate # define FD_RD_INIT(sd, rds, excs) \ 1007c478bd9Sstevel@tonic-gate FD_ZERO(&(rds)); \ 1017c478bd9Sstevel@tonic-gate FD_SET((unsigned int) (sd), &(rds)); \ 1027c478bd9Sstevel@tonic-gate FD_ZERO(&(excs)); \ 1037c478bd9Sstevel@tonic-gate FD_SET((unsigned int) (sd), &(excs)) 1047c478bd9Sstevel@tonic-gate 1057c478bd9Sstevel@tonic-gate # define FD_WR_INIT(sd, wrs) \ 1067c478bd9Sstevel@tonic-gate FD_ZERO(&(wrs)); \ 107*445f2479Sjbeck FD_SET((unsigned int) (sd), &(wrs)) 1087c478bd9Sstevel@tonic-gate 1097c478bd9Sstevel@tonic-gate # define FD_IS_RD_EXC(sd, rds, excs) FD_ISSET(sd, &(excs)) 1107c478bd9Sstevel@tonic-gate # define FD_IS_WR_RDY(sd, wrs) FD_ISSET((sd), &(wrs)) 1117c478bd9Sstevel@tonic-gate # define FD_IS_RD_RDY(sd, rds, excs) FD_ISSET((sd), &(rds)) 1127c478bd9Sstevel@tonic-gate 1137c478bd9Sstevel@tonic-gate # define FD_WR_READY(sd, wrs, timeout) \ 1147c478bd9Sstevel@tonic-gate select((sd) + 1, NULL, &(wrs), NULL, (timeout)) 1157c478bd9Sstevel@tonic-gate # define FD_RD_READY(sd, rds, excs, timeout) \ 1167c478bd9Sstevel@tonic-gate select((sd) + 1, &(rds), NULL, &(excs), (timeout)) 1177c478bd9Sstevel@tonic-gate 1187c478bd9Sstevel@tonic-gate #endif /* SM_CONF_POLL */ 1197c478bd9Sstevel@tonic-gate 1207c478bd9Sstevel@tonic-gate #include <sys/time.h> 1217c478bd9Sstevel@tonic-gate 1227c478bd9Sstevel@tonic-gate /* version info */ 1237c478bd9Sstevel@tonic-gate #define MILTER_PRODUCT_NAME "libmilter" 1247c478bd9Sstevel@tonic-gate #define MILTER_VERSION 100 1257c478bd9Sstevel@tonic-gate 1267c478bd9Sstevel@tonic-gate /* some defaults */ 1277c478bd9Sstevel@tonic-gate #define MI_TIMEOUT 7210 /* default timeout for read/write */ 1287c478bd9Sstevel@tonic-gate #define MI_CHK_TIME 5 /* checking whether to terminate */ 1297c478bd9Sstevel@tonic-gate 1307c478bd9Sstevel@tonic-gate #ifndef MI_SOMAXCONN 1317c478bd9Sstevel@tonic-gate # if SOMAXCONN > 20 1327c478bd9Sstevel@tonic-gate # define MI_SOMAXCONN SOMAXCONN 1337c478bd9Sstevel@tonic-gate # else /* SOMAXCONN */ 1347c478bd9Sstevel@tonic-gate # define MI_SOMAXCONN 20 1357c478bd9Sstevel@tonic-gate # endif /* SOMAXCONN */ 1367c478bd9Sstevel@tonic-gate #endif /* ! MI_SOMAXCONN */ 1377c478bd9Sstevel@tonic-gate 1387c478bd9Sstevel@tonic-gate /* maximum number of repeated failures in mi_listener() */ 1397c478bd9Sstevel@tonic-gate #define MAX_FAILS_M 16 /* malloc() */ 1407c478bd9Sstevel@tonic-gate #define MAX_FAILS_T 16 /* thread creation */ 1417c478bd9Sstevel@tonic-gate #define MAX_FAILS_A 16 /* accept() */ 1427c478bd9Sstevel@tonic-gate #define MAX_FAILS_S 16 /* select() */ 1437c478bd9Sstevel@tonic-gate 1447c478bd9Sstevel@tonic-gate /* internal "commands", i.e., error codes */ 1457c478bd9Sstevel@tonic-gate #define SMFIC_TIMEOUT ((char) 1) /* timeout */ 1467c478bd9Sstevel@tonic-gate #define SMFIC_SELECT ((char) 2) /* select error */ 1477c478bd9Sstevel@tonic-gate #define SMFIC_MALLOC ((char) 3) /* malloc error */ 1487c478bd9Sstevel@tonic-gate #define SMFIC_RECVERR ((char) 4) /* recv() error */ 1497c478bd9Sstevel@tonic-gate #define SMFIC_EOF ((char) 5) /* eof */ 1507c478bd9Sstevel@tonic-gate #define SMFIC_UNKNERR ((char) 6) /* unknown error */ 1517c478bd9Sstevel@tonic-gate #define SMFIC_TOOBIG ((char) 7) /* body chunk too big */ 1527c478bd9Sstevel@tonic-gate #define SMFIC_VALIDCMD ' ' /* first valid command */ 1537c478bd9Sstevel@tonic-gate 1547c478bd9Sstevel@tonic-gate /* hack */ 1557c478bd9Sstevel@tonic-gate #define smi_log syslog 1567c478bd9Sstevel@tonic-gate #define sm_dprintf (void) printf 1577c478bd9Sstevel@tonic-gate #define milter_ret int 1587c478bd9Sstevel@tonic-gate #define SMI_LOG_ERR LOG_ERR 1597c478bd9Sstevel@tonic-gate #define SMI_LOG_FATAL LOG_ERR 1607c478bd9Sstevel@tonic-gate #define SMI_LOG_WARN LOG_WARNING 1617c478bd9Sstevel@tonic-gate #define SMI_LOG_INFO LOG_INFO 1627c478bd9Sstevel@tonic-gate #define SMI_LOG_DEBUG LOG_DEBUG 1637c478bd9Sstevel@tonic-gate 1647c478bd9Sstevel@tonic-gate /* stop? */ 1657c478bd9Sstevel@tonic-gate #define MILTER_CONT 0 1667c478bd9Sstevel@tonic-gate #define MILTER_STOP 1 1677c478bd9Sstevel@tonic-gate #define MILTER_ABRT 2 1687c478bd9Sstevel@tonic-gate 1697c478bd9Sstevel@tonic-gate /* functions */ 1707c478bd9Sstevel@tonic-gate extern int mi_handle_session __P((SMFICTX_PTR)); 1717c478bd9Sstevel@tonic-gate extern int mi_engine __P((SMFICTX_PTR)); 1727c478bd9Sstevel@tonic-gate extern int mi_listener __P((char *, int, smfiDesc_ptr, time_t, int)); 1737c478bd9Sstevel@tonic-gate extern void mi_clr_macros __P((SMFICTX_PTR, int)); 1747c478bd9Sstevel@tonic-gate extern int mi_stop __P((void)); 1757c478bd9Sstevel@tonic-gate extern int mi_control_startup __P((char *)); 1767c478bd9Sstevel@tonic-gate extern void mi_stop_milters __P((int)); 1777c478bd9Sstevel@tonic-gate extern void mi_clean_signals __P((void)); 1787c478bd9Sstevel@tonic-gate extern struct hostent *mi_gethostbyname __P((char *, int)); 1797c478bd9Sstevel@tonic-gate extern int mi_inet_pton __P((int, const char *, void *)); 1807c478bd9Sstevel@tonic-gate extern void mi_closener __P((void)); 1817c478bd9Sstevel@tonic-gate extern int mi_opensocket __P((char *, int, int, bool, smfiDesc_ptr)); 1827c478bd9Sstevel@tonic-gate 1837c478bd9Sstevel@tonic-gate /* communication functions */ 1847c478bd9Sstevel@tonic-gate extern char *mi_rd_cmd __P((socket_t, struct timeval *, char *, size_t *, char *)); 1857c478bd9Sstevel@tonic-gate extern int mi_wr_cmd __P((socket_t, struct timeval *, int, char *, size_t)); 1867c478bd9Sstevel@tonic-gate extern bool mi_sendok __P((SMFICTX_PTR, int)); 1877c478bd9Sstevel@tonic-gate 1887c478bd9Sstevel@tonic-gate 1897c478bd9Sstevel@tonic-gate #endif /* ! _LIBMILTER_H */ 190