xref: /titanic_51/usr/src/cmd/sendmail/libmilter/main.c (revision 24472db64c485d6744c0321b7581cf066556cf2d)
17c478bd9Sstevel@tonic-gate /*
2*24472db6Sjbeck  *  Copyright (c) 1999-2003, 2006, 2007 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
127c478bd9Sstevel@tonic-gate 
137c478bd9Sstevel@tonic-gate #include <sm/gen.h>
14*24472db6Sjbeck SM_RCSID("@(#)$Id: main.c,v 8.83 2007/04/23 22:22:50 ca Exp $")
157c478bd9Sstevel@tonic-gate 
167c478bd9Sstevel@tonic-gate #define _DEFINE	1
177c478bd9Sstevel@tonic-gate #include "libmilter.h"
187c478bd9Sstevel@tonic-gate #include <fcntl.h>
197c478bd9Sstevel@tonic-gate #include <sys/stat.h>
207c478bd9Sstevel@tonic-gate 
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate static smfiDesc_ptr smfi = NULL;
237c478bd9Sstevel@tonic-gate 
247c478bd9Sstevel@tonic-gate /*
257c478bd9Sstevel@tonic-gate **  SMFI_REGISTER -- register a filter description
267c478bd9Sstevel@tonic-gate **
277c478bd9Sstevel@tonic-gate **	Parameters:
287c478bd9Sstevel@tonic-gate **		smfilter -- description of filter to register
297c478bd9Sstevel@tonic-gate **
307c478bd9Sstevel@tonic-gate **	Returns:
317c478bd9Sstevel@tonic-gate **		MI_SUCCESS/MI_FAILURE
327c478bd9Sstevel@tonic-gate */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate int
357c478bd9Sstevel@tonic-gate smfi_register(smfilter)
367c478bd9Sstevel@tonic-gate 	smfiDesc_str smfilter;
377c478bd9Sstevel@tonic-gate {
387c478bd9Sstevel@tonic-gate 	size_t len;
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate 	if (smfi == NULL)
417c478bd9Sstevel@tonic-gate 	{
427c478bd9Sstevel@tonic-gate 		smfi = (smfiDesc_ptr) malloc(sizeof *smfi);
437c478bd9Sstevel@tonic-gate 		if (smfi == NULL)
447c478bd9Sstevel@tonic-gate 			return MI_FAILURE;
457c478bd9Sstevel@tonic-gate 	}
467c478bd9Sstevel@tonic-gate 	(void) memcpy(smfi, &smfilter, sizeof *smfi);
477c478bd9Sstevel@tonic-gate 	if (smfilter.xxfi_name == NULL)
487c478bd9Sstevel@tonic-gate 		smfilter.xxfi_name = "Unknown";
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate 	len = strlen(smfilter.xxfi_name) + 1;
517c478bd9Sstevel@tonic-gate 	smfi->xxfi_name = (char *) malloc(len);
527c478bd9Sstevel@tonic-gate 	if (smfi->xxfi_name == NULL)
537c478bd9Sstevel@tonic-gate 		return MI_FAILURE;
547c478bd9Sstevel@tonic-gate 	(void) sm_strlcpy(smfi->xxfi_name, smfilter.xxfi_name, len);
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate 	/* compare milter version with hard coded version */
57*24472db6Sjbeck 	if (smfi->xxfi_version != SMFI_VERSION &&
58*24472db6Sjbeck 	    smfi->xxfi_version != 2 &&
59*24472db6Sjbeck 	    smfi->xxfi_version != 3 &&
60*24472db6Sjbeck 	    smfi->xxfi_version != 4)
617c478bd9Sstevel@tonic-gate 	{
627c478bd9Sstevel@tonic-gate 		/* hard failure for now! */
637c478bd9Sstevel@tonic-gate 		smi_log(SMI_LOG_ERR,
647c478bd9Sstevel@tonic-gate 			"%s: smfi_register: version mismatch application: %d != milter: %d",
657c478bd9Sstevel@tonic-gate 			smfi->xxfi_name, smfi->xxfi_version,
667c478bd9Sstevel@tonic-gate 			(int) SMFI_VERSION);
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 		/* XXX how about smfi? */
697c478bd9Sstevel@tonic-gate 		free(smfi->xxfi_name);
707c478bd9Sstevel@tonic-gate 		return MI_FAILURE;
717c478bd9Sstevel@tonic-gate 	}
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 	return MI_SUCCESS;
747c478bd9Sstevel@tonic-gate }
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate /*
777c478bd9Sstevel@tonic-gate **  SMFI_STOP -- stop milter
787c478bd9Sstevel@tonic-gate **
797c478bd9Sstevel@tonic-gate **	Parameters:
807c478bd9Sstevel@tonic-gate **		none.
817c478bd9Sstevel@tonic-gate **
827c478bd9Sstevel@tonic-gate **	Returns:
837c478bd9Sstevel@tonic-gate **		success.
847c478bd9Sstevel@tonic-gate */
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate int
877c478bd9Sstevel@tonic-gate smfi_stop()
887c478bd9Sstevel@tonic-gate {
897c478bd9Sstevel@tonic-gate 	mi_stop_milters(MILTER_STOP);
907c478bd9Sstevel@tonic-gate 	return MI_SUCCESS;
917c478bd9Sstevel@tonic-gate }
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate /*
947c478bd9Sstevel@tonic-gate **  Default values for some variables.
957c478bd9Sstevel@tonic-gate **	Most of these can be changed with the functions below.
967c478bd9Sstevel@tonic-gate */
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate static int dbg = 0;
997c478bd9Sstevel@tonic-gate static char *conn = NULL;
1007c478bd9Sstevel@tonic-gate static int timeout = MI_TIMEOUT;
1017c478bd9Sstevel@tonic-gate static int backlog = MI_SOMAXCONN;
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate /*
1047c478bd9Sstevel@tonic-gate **  SMFI_OPENSOCKET -- try the socket setup to make sure we'll be
1057c478bd9Sstevel@tonic-gate **		       able to start up
1067c478bd9Sstevel@tonic-gate **
1077c478bd9Sstevel@tonic-gate **	Parameters:
1087c478bd9Sstevel@tonic-gate **		rmsocket -- if true, instructs libmilter to attempt
1097c478bd9Sstevel@tonic-gate **			to remove the socket before creating it;
1107c478bd9Sstevel@tonic-gate **			only applies for "local:" or "unix:" sockets
1117c478bd9Sstevel@tonic-gate **
1127c478bd9Sstevel@tonic-gate **	Return:
1137c478bd9Sstevel@tonic-gate **		MI_SUCCESS/MI_FAILURE
1147c478bd9Sstevel@tonic-gate */
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate int
1177c478bd9Sstevel@tonic-gate smfi_opensocket(rmsocket)
1187c478bd9Sstevel@tonic-gate 	bool rmsocket;
1197c478bd9Sstevel@tonic-gate {
1207c478bd9Sstevel@tonic-gate 	if (smfi == NULL || conn == NULL)
1217c478bd9Sstevel@tonic-gate 		return MI_FAILURE;
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	return mi_opensocket(conn, backlog, dbg, rmsocket, smfi);
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate /*
1277c478bd9Sstevel@tonic-gate **  SMFI_SETDBG -- set debug level.
1287c478bd9Sstevel@tonic-gate **
1297c478bd9Sstevel@tonic-gate **	Parameters:
1307c478bd9Sstevel@tonic-gate **		odbg -- new debug level.
1317c478bd9Sstevel@tonic-gate **
1327c478bd9Sstevel@tonic-gate **	Returns:
1337c478bd9Sstevel@tonic-gate **		MI_SUCCESS
1347c478bd9Sstevel@tonic-gate */
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate int
1377c478bd9Sstevel@tonic-gate smfi_setdbg(odbg)
1387c478bd9Sstevel@tonic-gate 	int odbg;
1397c478bd9Sstevel@tonic-gate {
1407c478bd9Sstevel@tonic-gate 	dbg = odbg;
1417c478bd9Sstevel@tonic-gate 	return MI_SUCCESS;
1427c478bd9Sstevel@tonic-gate }
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate /*
1457c478bd9Sstevel@tonic-gate **  SMFI_SETTIMEOUT -- set timeout (for read/write).
1467c478bd9Sstevel@tonic-gate **
1477c478bd9Sstevel@tonic-gate **	Parameters:
1487c478bd9Sstevel@tonic-gate **		otimeout -- new timeout.
1497c478bd9Sstevel@tonic-gate **
1507c478bd9Sstevel@tonic-gate **	Returns:
1517c478bd9Sstevel@tonic-gate **		MI_SUCCESS
1527c478bd9Sstevel@tonic-gate */
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate int
1557c478bd9Sstevel@tonic-gate smfi_settimeout(otimeout)
1567c478bd9Sstevel@tonic-gate 	int otimeout;
1577c478bd9Sstevel@tonic-gate {
1587c478bd9Sstevel@tonic-gate 	timeout = otimeout;
1597c478bd9Sstevel@tonic-gate 	return MI_SUCCESS;
1607c478bd9Sstevel@tonic-gate }
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate /*
1637c478bd9Sstevel@tonic-gate **  SMFI_SETCONN -- set connection information (socket description)
1647c478bd9Sstevel@tonic-gate **
1657c478bd9Sstevel@tonic-gate **	Parameters:
1667c478bd9Sstevel@tonic-gate **		oconn -- new connection information.
1677c478bd9Sstevel@tonic-gate **
1687c478bd9Sstevel@tonic-gate **	Returns:
1697c478bd9Sstevel@tonic-gate **		MI_SUCCESS/MI_FAILURE
1707c478bd9Sstevel@tonic-gate */
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate int
1737c478bd9Sstevel@tonic-gate smfi_setconn(oconn)
1747c478bd9Sstevel@tonic-gate 	char *oconn;
1757c478bd9Sstevel@tonic-gate {
1767c478bd9Sstevel@tonic-gate 	size_t l;
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	if (oconn == NULL || *oconn == '\0')
1797c478bd9Sstevel@tonic-gate 		return MI_FAILURE;
1807c478bd9Sstevel@tonic-gate 	l = strlen(oconn) + 1;
1817c478bd9Sstevel@tonic-gate 	if ((conn = (char *) malloc(l)) == NULL)
1827c478bd9Sstevel@tonic-gate 		return MI_FAILURE;
1837c478bd9Sstevel@tonic-gate 	if (sm_strlcpy(conn, oconn, l) >= l)
1847c478bd9Sstevel@tonic-gate 		return MI_FAILURE;
1857c478bd9Sstevel@tonic-gate 	return MI_SUCCESS;
1867c478bd9Sstevel@tonic-gate }
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate /*
1897c478bd9Sstevel@tonic-gate **  SMFI_SETBACKLOG -- set backlog
1907c478bd9Sstevel@tonic-gate **
1917c478bd9Sstevel@tonic-gate **	Parameters:
1927c478bd9Sstevel@tonic-gate **		obacklog -- new backlog.
1937c478bd9Sstevel@tonic-gate **
1947c478bd9Sstevel@tonic-gate **	Returns:
1957c478bd9Sstevel@tonic-gate **		MI_SUCCESS/MI_FAILURE
1967c478bd9Sstevel@tonic-gate */
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate int
1997c478bd9Sstevel@tonic-gate smfi_setbacklog(obacklog)
2007c478bd9Sstevel@tonic-gate 	int obacklog;
2017c478bd9Sstevel@tonic-gate {
2027c478bd9Sstevel@tonic-gate 	if (obacklog <= 0)
2037c478bd9Sstevel@tonic-gate 		return MI_FAILURE;
2047c478bd9Sstevel@tonic-gate 	backlog = obacklog;
2057c478bd9Sstevel@tonic-gate 	return MI_SUCCESS;
2067c478bd9Sstevel@tonic-gate }
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate /*
2107c478bd9Sstevel@tonic-gate **  SMFI_MAIN -- setup milter connnection and start listener.
2117c478bd9Sstevel@tonic-gate **
2127c478bd9Sstevel@tonic-gate **	Parameters:
2137c478bd9Sstevel@tonic-gate **		none.
2147c478bd9Sstevel@tonic-gate **
2157c478bd9Sstevel@tonic-gate **	Returns:
2167c478bd9Sstevel@tonic-gate **		MI_SUCCESS/MI_FAILURE
2177c478bd9Sstevel@tonic-gate */
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate int
2207c478bd9Sstevel@tonic-gate smfi_main()
2217c478bd9Sstevel@tonic-gate {
2227c478bd9Sstevel@tonic-gate 	int r;
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	(void) signal(SIGPIPE, SIG_IGN);
2257c478bd9Sstevel@tonic-gate 	if (conn == NULL)
2267c478bd9Sstevel@tonic-gate 	{
2277c478bd9Sstevel@tonic-gate 		smi_log(SMI_LOG_FATAL, "%s: missing connection information",
2287c478bd9Sstevel@tonic-gate 			smfi->xxfi_name);
2297c478bd9Sstevel@tonic-gate 		return MI_FAILURE;
2307c478bd9Sstevel@tonic-gate 	}
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	(void) atexit(mi_clean_signals);
2337c478bd9Sstevel@tonic-gate 	if (mi_control_startup(smfi->xxfi_name) != MI_SUCCESS)
2347c478bd9Sstevel@tonic-gate 	{
2357c478bd9Sstevel@tonic-gate 		smi_log(SMI_LOG_FATAL,
2367c478bd9Sstevel@tonic-gate 			"%s: Couldn't start signal thread",
2377c478bd9Sstevel@tonic-gate 			smfi->xxfi_name);
2387c478bd9Sstevel@tonic-gate 		return MI_FAILURE;
2397c478bd9Sstevel@tonic-gate 	}
240058561cbSjbeck 	r = MI_MONITOR_INIT();
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	/* Startup the listener */
2437c478bd9Sstevel@tonic-gate 	if (mi_listener(conn, dbg, smfi, timeout, backlog) != MI_SUCCESS)
2447c478bd9Sstevel@tonic-gate 		r = MI_FAILURE;
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	return r;
2477c478bd9Sstevel@tonic-gate }
2487c478bd9Sstevel@tonic-gate 
249