1 /* 2 * Copyright (c) 1999-2002 Sendmail, Inc. and its suppliers. 3 * All rights reserved. 4 * 5 * By using this file, you agree to the terms and conditions set 6 * forth in the LICENSE file which can be found at the top level of 7 * the sendmail distribution. 8 * 9 */ 10 11 #include <sm/gen.h> 12 SM_RCSID("@(#)$Id: main.c,v 1.1.1.5 2002/04/10 03:05:00 gshapiro Exp $") 13 14 #define _DEFINE 1 15 #include "libmilter.h" 16 #include <fcntl.h> 17 #include <sys/stat.h> 18 19 20 static smfiDesc_ptr smfi = NULL; 21 22 /* 23 ** SMFI_REGISTER -- register a filter description 24 ** 25 ** Parameters: 26 ** smfilter -- description of filter to register 27 ** 28 ** Returns: 29 ** MI_SUCCESS/MI_FAILURE 30 */ 31 32 int 33 smfi_register(smfilter) 34 smfiDesc_str smfilter; 35 { 36 size_t len; 37 38 if (smfi == NULL) 39 { 40 smfi = (smfiDesc_ptr) malloc(sizeof *smfi); 41 if (smfi == NULL) 42 return MI_FAILURE; 43 } 44 (void) memcpy(smfi, &smfilter, sizeof *smfi); 45 if (smfilter.xxfi_name == NULL) 46 smfilter.xxfi_name = "Unknown"; 47 48 len = strlen(smfilter.xxfi_name) + 1; 49 smfi->xxfi_name = (char *) malloc(len); 50 if (smfi->xxfi_name == NULL) 51 return MI_FAILURE; 52 (void) sm_strlcpy(smfi->xxfi_name, smfilter.xxfi_name, len); 53 54 /* compare milter version with hard coded version */ 55 if (smfi->xxfi_version != SMFI_VERSION) 56 { 57 /* hard failure for now! */ 58 smi_log(SMI_LOG_ERR, 59 "%s: smfi_register: version mismatch application: %d != milter: %d", 60 smfi->xxfi_name, smfi->xxfi_version, 61 (int) SMFI_VERSION); 62 63 /* XXX how about smfi? */ 64 free(smfi->xxfi_name); 65 return MI_FAILURE; 66 } 67 68 return MI_SUCCESS; 69 } 70 71 /* 72 ** SMFI_STOP -- stop milter 73 ** 74 ** Parameters: 75 ** none. 76 ** 77 ** Returns: 78 ** success. 79 */ 80 81 int 82 smfi_stop() 83 { 84 mi_stop_milters(MILTER_STOP); 85 return MI_SUCCESS; 86 } 87 88 /* 89 ** default values for some variables. 90 ** Most of these can be changed with the functions below. 91 */ 92 93 static int dbg = 0; 94 static char *conn = NULL; 95 static int timeout = MI_TIMEOUT; 96 static int backlog= MI_SOMAXCONN; 97 98 /* 99 ** SMFI_SETDBG -- set debug level. 100 ** 101 ** Parameters: 102 ** odbg -- new debug level. 103 ** 104 ** Returns: 105 ** MI_SUCCESS 106 */ 107 108 int 109 smfi_setdbg(odbg) 110 int odbg; 111 { 112 dbg = odbg; 113 return MI_SUCCESS; 114 } 115 116 /* 117 ** SMFI_SETTIMEOUT -- set timeout (for read/write). 118 ** 119 ** Parameters: 120 ** otimeout -- new timeout. 121 ** 122 ** Returns: 123 ** MI_SUCCESS 124 */ 125 126 int 127 smfi_settimeout(otimeout) 128 int otimeout; 129 { 130 timeout = otimeout; 131 return MI_SUCCESS; 132 } 133 134 /* 135 ** SMFI_SETCONN -- set connection information (socket description) 136 ** 137 ** Parameters: 138 ** oconn -- new connection information. 139 ** 140 ** Returns: 141 ** MI_SUCCESS/MI_FAILURE 142 */ 143 144 int 145 smfi_setconn(oconn) 146 char *oconn; 147 { 148 size_t l; 149 150 if (oconn == NULL || *oconn == '\0') 151 return MI_FAILURE; 152 l = strlen(oconn) + 1; 153 if ((conn = (char *) malloc(l)) == NULL) 154 return MI_FAILURE; 155 if (sm_strlcpy(conn, oconn, l) >= l) 156 return MI_FAILURE; 157 return MI_SUCCESS; 158 } 159 160 /* 161 ** SMFI_SETBACKLOG -- set backlog 162 ** 163 ** Parameters: 164 ** obacklog -- new backlog. 165 ** 166 ** Returns: 167 ** MI_SUCCESS/MI_FAILURE 168 */ 169 170 int 171 smfi_setbacklog(obacklog) 172 int obacklog; 173 { 174 if (obacklog <= 0) 175 return MI_FAILURE; 176 backlog = obacklog; 177 return MI_SUCCESS; 178 } 179 180 181 /* 182 ** SMFI_MAIN -- setup milter connnection and start listener. 183 ** 184 ** Parameters: 185 ** none. 186 ** 187 ** Returns: 188 ** MI_SUCCESS/MI_FAILURE 189 */ 190 191 int 192 smfi_main() 193 { 194 int r; 195 196 (void) signal(SIGPIPE, SIG_IGN); 197 if (conn == NULL) 198 { 199 smi_log(SMI_LOG_FATAL, "%s: missing connection information", 200 smfi->xxfi_name); 201 return MI_FAILURE; 202 } 203 204 (void) atexit(mi_clean_signals); 205 if (mi_control_startup(smfi->xxfi_name) != MI_SUCCESS) 206 { 207 smi_log(SMI_LOG_FATAL, 208 "%s: Couldn't start signal thread", 209 smfi->xxfi_name); 210 return MI_FAILURE; 211 } 212 r = MI_SUCCESS; 213 214 /* Startup the listener */ 215 if (mi_listener(conn, dbg, smfi, timeout, backlog) != MI_SUCCESS) 216 r = MI_FAILURE; 217 218 return r; 219 } 220