17c478bd9Sstevel@tonic-gate /* 2*058561cbSjbeck * 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 #pragma ident "%Z%%M% %I% %E% SMI" 127c478bd9Sstevel@tonic-gate 137c478bd9Sstevel@tonic-gate #include <sm/gen.h> 14*058561cbSjbeck SM_RCSID("@(#)$Id: main.c,v 8.81 2006/11/02 18:31:43 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 */ 577c478bd9Sstevel@tonic-gate if (smfi->xxfi_version != SMFI_VERSION) 587c478bd9Sstevel@tonic-gate { 597c478bd9Sstevel@tonic-gate /* hard failure for now! */ 607c478bd9Sstevel@tonic-gate smi_log(SMI_LOG_ERR, 617c478bd9Sstevel@tonic-gate "%s: smfi_register: version mismatch application: %d != milter: %d", 627c478bd9Sstevel@tonic-gate smfi->xxfi_name, smfi->xxfi_version, 637c478bd9Sstevel@tonic-gate (int) SMFI_VERSION); 647c478bd9Sstevel@tonic-gate 657c478bd9Sstevel@tonic-gate /* XXX how about smfi? */ 667c478bd9Sstevel@tonic-gate free(smfi->xxfi_name); 677c478bd9Sstevel@tonic-gate return MI_FAILURE; 687c478bd9Sstevel@tonic-gate } 697c478bd9Sstevel@tonic-gate 707c478bd9Sstevel@tonic-gate return MI_SUCCESS; 717c478bd9Sstevel@tonic-gate } 727c478bd9Sstevel@tonic-gate 737c478bd9Sstevel@tonic-gate /* 747c478bd9Sstevel@tonic-gate ** SMFI_STOP -- stop milter 757c478bd9Sstevel@tonic-gate ** 767c478bd9Sstevel@tonic-gate ** Parameters: 777c478bd9Sstevel@tonic-gate ** none. 787c478bd9Sstevel@tonic-gate ** 797c478bd9Sstevel@tonic-gate ** Returns: 807c478bd9Sstevel@tonic-gate ** success. 817c478bd9Sstevel@tonic-gate */ 827c478bd9Sstevel@tonic-gate 837c478bd9Sstevel@tonic-gate int 847c478bd9Sstevel@tonic-gate smfi_stop() 857c478bd9Sstevel@tonic-gate { 867c478bd9Sstevel@tonic-gate mi_stop_milters(MILTER_STOP); 877c478bd9Sstevel@tonic-gate return MI_SUCCESS; 887c478bd9Sstevel@tonic-gate } 897c478bd9Sstevel@tonic-gate 907c478bd9Sstevel@tonic-gate /* 917c478bd9Sstevel@tonic-gate ** Default values for some variables. 927c478bd9Sstevel@tonic-gate ** Most of these can be changed with the functions below. 937c478bd9Sstevel@tonic-gate */ 947c478bd9Sstevel@tonic-gate 957c478bd9Sstevel@tonic-gate static int dbg = 0; 967c478bd9Sstevel@tonic-gate static char *conn = NULL; 977c478bd9Sstevel@tonic-gate static int timeout = MI_TIMEOUT; 987c478bd9Sstevel@tonic-gate static int backlog = MI_SOMAXCONN; 997c478bd9Sstevel@tonic-gate 1007c478bd9Sstevel@tonic-gate /* 1017c478bd9Sstevel@tonic-gate ** SMFI_OPENSOCKET -- try the socket setup to make sure we'll be 1027c478bd9Sstevel@tonic-gate ** able to start up 1037c478bd9Sstevel@tonic-gate ** 1047c478bd9Sstevel@tonic-gate ** Parameters: 1057c478bd9Sstevel@tonic-gate ** rmsocket -- if true, instructs libmilter to attempt 1067c478bd9Sstevel@tonic-gate ** to remove the socket before creating it; 1077c478bd9Sstevel@tonic-gate ** only applies for "local:" or "unix:" sockets 1087c478bd9Sstevel@tonic-gate ** 1097c478bd9Sstevel@tonic-gate ** Return: 1107c478bd9Sstevel@tonic-gate ** MI_SUCCESS/MI_FAILURE 1117c478bd9Sstevel@tonic-gate */ 1127c478bd9Sstevel@tonic-gate 1137c478bd9Sstevel@tonic-gate int 1147c478bd9Sstevel@tonic-gate smfi_opensocket(rmsocket) 1157c478bd9Sstevel@tonic-gate bool rmsocket; 1167c478bd9Sstevel@tonic-gate { 1177c478bd9Sstevel@tonic-gate if (smfi == NULL || conn == NULL) 1187c478bd9Sstevel@tonic-gate return MI_FAILURE; 1197c478bd9Sstevel@tonic-gate 1207c478bd9Sstevel@tonic-gate return mi_opensocket(conn, backlog, dbg, rmsocket, smfi); 1217c478bd9Sstevel@tonic-gate } 1227c478bd9Sstevel@tonic-gate 1237c478bd9Sstevel@tonic-gate /* 1247c478bd9Sstevel@tonic-gate ** SMFI_SETDBG -- set debug level. 1257c478bd9Sstevel@tonic-gate ** 1267c478bd9Sstevel@tonic-gate ** Parameters: 1277c478bd9Sstevel@tonic-gate ** odbg -- new debug level. 1287c478bd9Sstevel@tonic-gate ** 1297c478bd9Sstevel@tonic-gate ** Returns: 1307c478bd9Sstevel@tonic-gate ** MI_SUCCESS 1317c478bd9Sstevel@tonic-gate */ 1327c478bd9Sstevel@tonic-gate 1337c478bd9Sstevel@tonic-gate int 1347c478bd9Sstevel@tonic-gate smfi_setdbg(odbg) 1357c478bd9Sstevel@tonic-gate int odbg; 1367c478bd9Sstevel@tonic-gate { 1377c478bd9Sstevel@tonic-gate dbg = odbg; 1387c478bd9Sstevel@tonic-gate return MI_SUCCESS; 1397c478bd9Sstevel@tonic-gate } 1407c478bd9Sstevel@tonic-gate 1417c478bd9Sstevel@tonic-gate /* 1427c478bd9Sstevel@tonic-gate ** SMFI_SETTIMEOUT -- set timeout (for read/write). 1437c478bd9Sstevel@tonic-gate ** 1447c478bd9Sstevel@tonic-gate ** Parameters: 1457c478bd9Sstevel@tonic-gate ** otimeout -- new timeout. 1467c478bd9Sstevel@tonic-gate ** 1477c478bd9Sstevel@tonic-gate ** Returns: 1487c478bd9Sstevel@tonic-gate ** MI_SUCCESS 1497c478bd9Sstevel@tonic-gate */ 1507c478bd9Sstevel@tonic-gate 1517c478bd9Sstevel@tonic-gate int 1527c478bd9Sstevel@tonic-gate smfi_settimeout(otimeout) 1537c478bd9Sstevel@tonic-gate int otimeout; 1547c478bd9Sstevel@tonic-gate { 1557c478bd9Sstevel@tonic-gate timeout = otimeout; 1567c478bd9Sstevel@tonic-gate return MI_SUCCESS; 1577c478bd9Sstevel@tonic-gate } 1587c478bd9Sstevel@tonic-gate 1597c478bd9Sstevel@tonic-gate /* 1607c478bd9Sstevel@tonic-gate ** SMFI_SETCONN -- set connection information (socket description) 1617c478bd9Sstevel@tonic-gate ** 1627c478bd9Sstevel@tonic-gate ** Parameters: 1637c478bd9Sstevel@tonic-gate ** oconn -- new connection information. 1647c478bd9Sstevel@tonic-gate ** 1657c478bd9Sstevel@tonic-gate ** Returns: 1667c478bd9Sstevel@tonic-gate ** MI_SUCCESS/MI_FAILURE 1677c478bd9Sstevel@tonic-gate */ 1687c478bd9Sstevel@tonic-gate 1697c478bd9Sstevel@tonic-gate int 1707c478bd9Sstevel@tonic-gate smfi_setconn(oconn) 1717c478bd9Sstevel@tonic-gate char *oconn; 1727c478bd9Sstevel@tonic-gate { 1737c478bd9Sstevel@tonic-gate size_t l; 1747c478bd9Sstevel@tonic-gate 1757c478bd9Sstevel@tonic-gate if (oconn == NULL || *oconn == '\0') 1767c478bd9Sstevel@tonic-gate return MI_FAILURE; 1777c478bd9Sstevel@tonic-gate l = strlen(oconn) + 1; 1787c478bd9Sstevel@tonic-gate if ((conn = (char *) malloc(l)) == NULL) 1797c478bd9Sstevel@tonic-gate return MI_FAILURE; 1807c478bd9Sstevel@tonic-gate if (sm_strlcpy(conn, oconn, l) >= l) 1817c478bd9Sstevel@tonic-gate return MI_FAILURE; 1827c478bd9Sstevel@tonic-gate return MI_SUCCESS; 1837c478bd9Sstevel@tonic-gate } 1847c478bd9Sstevel@tonic-gate 1857c478bd9Sstevel@tonic-gate /* 1867c478bd9Sstevel@tonic-gate ** SMFI_SETBACKLOG -- set backlog 1877c478bd9Sstevel@tonic-gate ** 1887c478bd9Sstevel@tonic-gate ** Parameters: 1897c478bd9Sstevel@tonic-gate ** obacklog -- new backlog. 1907c478bd9Sstevel@tonic-gate ** 1917c478bd9Sstevel@tonic-gate ** Returns: 1927c478bd9Sstevel@tonic-gate ** MI_SUCCESS/MI_FAILURE 1937c478bd9Sstevel@tonic-gate */ 1947c478bd9Sstevel@tonic-gate 1957c478bd9Sstevel@tonic-gate int 1967c478bd9Sstevel@tonic-gate smfi_setbacklog(obacklog) 1977c478bd9Sstevel@tonic-gate int obacklog; 1987c478bd9Sstevel@tonic-gate { 1997c478bd9Sstevel@tonic-gate if (obacklog <= 0) 2007c478bd9Sstevel@tonic-gate return MI_FAILURE; 2017c478bd9Sstevel@tonic-gate backlog = obacklog; 2027c478bd9Sstevel@tonic-gate return MI_SUCCESS; 2037c478bd9Sstevel@tonic-gate } 2047c478bd9Sstevel@tonic-gate 2057c478bd9Sstevel@tonic-gate 2067c478bd9Sstevel@tonic-gate /* 2077c478bd9Sstevel@tonic-gate ** SMFI_MAIN -- setup milter connnection and start listener. 2087c478bd9Sstevel@tonic-gate ** 2097c478bd9Sstevel@tonic-gate ** Parameters: 2107c478bd9Sstevel@tonic-gate ** none. 2117c478bd9Sstevel@tonic-gate ** 2127c478bd9Sstevel@tonic-gate ** Returns: 2137c478bd9Sstevel@tonic-gate ** MI_SUCCESS/MI_FAILURE 2147c478bd9Sstevel@tonic-gate */ 2157c478bd9Sstevel@tonic-gate 2167c478bd9Sstevel@tonic-gate int 2177c478bd9Sstevel@tonic-gate smfi_main() 2187c478bd9Sstevel@tonic-gate { 2197c478bd9Sstevel@tonic-gate int r; 2207c478bd9Sstevel@tonic-gate 2217c478bd9Sstevel@tonic-gate (void) signal(SIGPIPE, SIG_IGN); 2227c478bd9Sstevel@tonic-gate if (conn == NULL) 2237c478bd9Sstevel@tonic-gate { 2247c478bd9Sstevel@tonic-gate smi_log(SMI_LOG_FATAL, "%s: missing connection information", 2257c478bd9Sstevel@tonic-gate smfi->xxfi_name); 2267c478bd9Sstevel@tonic-gate return MI_FAILURE; 2277c478bd9Sstevel@tonic-gate } 2287c478bd9Sstevel@tonic-gate 2297c478bd9Sstevel@tonic-gate (void) atexit(mi_clean_signals); 2307c478bd9Sstevel@tonic-gate if (mi_control_startup(smfi->xxfi_name) != MI_SUCCESS) 2317c478bd9Sstevel@tonic-gate { 2327c478bd9Sstevel@tonic-gate smi_log(SMI_LOG_FATAL, 2337c478bd9Sstevel@tonic-gate "%s: Couldn't start signal thread", 2347c478bd9Sstevel@tonic-gate smfi->xxfi_name); 2357c478bd9Sstevel@tonic-gate return MI_FAILURE; 2367c478bd9Sstevel@tonic-gate } 237*058561cbSjbeck r = MI_MONITOR_INIT(); 2387c478bd9Sstevel@tonic-gate 2397c478bd9Sstevel@tonic-gate /* Startup the listener */ 2407c478bd9Sstevel@tonic-gate if (mi_listener(conn, dbg, smfi, timeout, backlog) != MI_SUCCESS) 2417c478bd9Sstevel@tonic-gate r = MI_FAILURE; 2427c478bd9Sstevel@tonic-gate 2437c478bd9Sstevel@tonic-gate return r; 2447c478bd9Sstevel@tonic-gate } 2457c478bd9Sstevel@tonic-gate 246