freebsd-nq/contrib/sendmail/libmilter/main.c

244 lines
4.1 KiB
C
Raw Normal View History

/*
2003-03-03 17:09:13 +00:00
* Copyright (c) 1999-2003 Sendmail, Inc. and its suppliers.
* All rights reserved.
*
* By using this file, you agree to the terms and conditions set
* forth in the LICENSE file which can be found at the top level of
* the sendmail distribution.
*
*/
2002-02-17 21:56:45 +00:00
#include <sm/gen.h>
2004-08-01 01:04:57 +00:00
SM_RCSID("@(#)$Id: main.c,v 8.79 2003/10/20 22:25:09 ca Exp $")
#define _DEFINE 1
#include "libmilter.h"
#include <fcntl.h>
#include <sys/stat.h>
static smfiDesc_ptr smfi = NULL;
/*
** SMFI_REGISTER -- register a filter description
**
** Parameters:
** smfilter -- description of filter to register
**
** Returns:
** MI_SUCCESS/MI_FAILURE
*/
int
smfi_register(smfilter)
smfiDesc_str smfilter;
{
size_t len;
if (smfi == NULL)
{
smfi = (smfiDesc_ptr) malloc(sizeof *smfi);
if (smfi == NULL)
return MI_FAILURE;
}
(void) memcpy(smfi, &smfilter, sizeof *smfi);
if (smfilter.xxfi_name == NULL)
smfilter.xxfi_name = "Unknown";
len = strlen(smfilter.xxfi_name) + 1;
smfi->xxfi_name = (char *) malloc(len);
if (smfi->xxfi_name == NULL)
return MI_FAILURE;
2002-02-17 21:56:45 +00:00
(void) sm_strlcpy(smfi->xxfi_name, smfilter.xxfi_name, len);
/* compare milter version with hard coded version */
if (smfi->xxfi_version != SMFI_VERSION)
{
/* hard failure for now! */
smi_log(SMI_LOG_ERR,
"%s: smfi_register: version mismatch application: %d != milter: %d",
smfi->xxfi_name, smfi->xxfi_version,
(int) SMFI_VERSION);
2002-02-17 21:56:45 +00:00
/* XXX how about smfi? */
free(smfi->xxfi_name);
return MI_FAILURE;
}
return MI_SUCCESS;
}
2002-02-17 21:56:45 +00:00
/*
** SMFI_STOP -- stop milter
**
** Parameters:
** none.
**
** Returns:
** success.
*/
int
smfi_stop()
{
mi_stop_milters(MILTER_STOP);
return MI_SUCCESS;
}
2002-02-17 21:56:45 +00:00
/*
2004-02-14 21:53:31 +00:00
** Default values for some variables.
2002-02-17 21:56:45 +00:00
** Most of these can be changed with the functions below.
*/
static int dbg = 0;
static char *conn = NULL;
static int timeout = MI_TIMEOUT;
2002-06-11 21:12:04 +00:00
static int backlog = MI_SOMAXCONN;
/*
** SMFI_OPENSOCKET -- try the socket setup to make sure we'll be
2004-08-01 01:04:57 +00:00
** able to start up
2002-06-11 21:12:04 +00:00
**
2003-02-08 20:31:29 +00:00
** Parameters:
2004-02-14 21:53:31 +00:00
** rmsocket -- if true, instructs libmilter to attempt
** to remove the socket before creating it;
** only applies for "local:" or "unix:" sockets
2002-06-11 21:12:04 +00:00
**
2003-02-08 20:31:29 +00:00
** Return:
** MI_SUCCESS/MI_FAILURE
2002-06-11 21:12:04 +00:00
*/
int
2004-02-14 21:53:31 +00:00
smfi_opensocket(rmsocket)
bool rmsocket;
2002-06-11 21:12:04 +00:00
{
if (smfi == NULL || conn == NULL)
return MI_FAILURE;
2004-02-14 21:53:31 +00:00
return mi_opensocket(conn, backlog, dbg, rmsocket, smfi);
2002-06-11 21:12:04 +00:00
}
2002-02-17 21:56:45 +00:00
/*
** SMFI_SETDBG -- set debug level.
**
** Parameters:
** odbg -- new debug level.
**
** Returns:
** MI_SUCCESS
*/
int
smfi_setdbg(odbg)
int odbg;
{
dbg = odbg;
return MI_SUCCESS;
}
2002-02-17 21:56:45 +00:00
/*
** SMFI_SETTIMEOUT -- set timeout (for read/write).
**
** Parameters:
** otimeout -- new timeout.
**
** Returns:
** MI_SUCCESS
*/
int
smfi_settimeout(otimeout)
int otimeout;
{
timeout = otimeout;
return MI_SUCCESS;
}
2002-02-17 21:56:45 +00:00
/*
** SMFI_SETCONN -- set connection information (socket description)
**
** Parameters:
** oconn -- new connection information.
**
** Returns:
** MI_SUCCESS/MI_FAILURE
*/
int
smfi_setconn(oconn)
char *oconn;
{
size_t l;
if (oconn == NULL || *oconn == '\0')
return MI_FAILURE;
l = strlen(oconn) + 1;
if ((conn = (char *) malloc(l)) == NULL)
return MI_FAILURE;
2002-02-17 21:56:45 +00:00
if (sm_strlcpy(conn, oconn, l) >= l)
return MI_FAILURE;
return MI_SUCCESS;
}
2002-02-17 21:56:45 +00:00
/*
** SMFI_SETBACKLOG -- set backlog
**
** Parameters:
2002-04-10 03:05:00 +00:00
** obacklog -- new backlog.
2002-02-17 21:56:45 +00:00
**
** Returns:
** MI_SUCCESS/MI_FAILURE
*/
int
smfi_setbacklog(obacklog)
int obacklog;
{
if (obacklog <= 0)
return MI_FAILURE;
backlog = obacklog;
return MI_SUCCESS;
}
2001-05-28 17:08:52 +00:00
2002-02-17 21:56:45 +00:00
/*
** SMFI_MAIN -- setup milter connnection and start listener.
**
** Parameters:
** none.
**
** Returns:
** MI_SUCCESS/MI_FAILURE
*/
int
smfi_main()
{
2002-02-17 21:56:45 +00:00
int r;
2001-05-28 17:08:52 +00:00
2002-02-17 21:56:45 +00:00
(void) signal(SIGPIPE, SIG_IGN);
if (conn == NULL)
{
smi_log(SMI_LOG_FATAL, "%s: missing connection information",
smfi->xxfi_name);
return MI_FAILURE;
}
(void) atexit(mi_clean_signals);
if (mi_control_startup(smfi->xxfi_name) != MI_SUCCESS)
{
smi_log(SMI_LOG_FATAL,
"%s: Couldn't start signal thread",
smfi->xxfi_name);
return MI_FAILURE;
}
2002-02-17 21:56:45 +00:00
r = MI_SUCCESS;
2001-05-28 17:08:52 +00:00
/* Startup the listener */
if (mi_listener(conn, dbg, smfi, timeout, backlog) != MI_SUCCESS)
2002-02-17 21:56:45 +00:00
r = MI_FAILURE;
2004-08-01 01:04:57 +00:00
2002-02-17 21:56:45 +00:00
return r;
}
2003-02-08 20:31:29 +00:00