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