xref: /freebsd/contrib/sendmail/libmilter/main.c (revision 2ef40764f06885f97d380ee8de0ced64930423db)
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 8.64.2.8 2002/12/18 23:13:45 ca 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 #if _FFR_SMFI_OPENSOCKET
99 /*
100 **  SMFI_OPENSOCKET -- try the socket setup to make sure we'll be
101 **		able to start up
102 **
103 **	Parameters:
104 **		None.
105 **
106 **	Return:
107 **		MI_SUCCESS/MI_FAILURE
108 */
109 
110 int
111 smfi_opensocket()
112 {
113 	if (smfi == NULL || conn == NULL)
114 		return MI_FAILURE;
115 
116 	return mi_opensocket(conn, backlog, dbg, smfi);
117 }
118 #endif /* _FFR_SMFI_OPENSOCKET */
119 
120 /*
121 **  SMFI_SETDBG -- set debug level.
122 **
123 **	Parameters:
124 **		odbg -- new debug level.
125 **
126 **	Returns:
127 **		MI_SUCCESS
128 */
129 
130 int
131 smfi_setdbg(odbg)
132 	int odbg;
133 {
134 	dbg = odbg;
135 	return MI_SUCCESS;
136 }
137 
138 /*
139 **  SMFI_SETTIMEOUT -- set timeout (for read/write).
140 **
141 **	Parameters:
142 **		otimeout -- new timeout.
143 **
144 **	Returns:
145 **		MI_SUCCESS
146 */
147 
148 int
149 smfi_settimeout(otimeout)
150 	int otimeout;
151 {
152 	timeout = otimeout;
153 	return MI_SUCCESS;
154 }
155 
156 /*
157 **  SMFI_SETCONN -- set connection information (socket description)
158 **
159 **	Parameters:
160 **		oconn -- new connection information.
161 **
162 **	Returns:
163 **		MI_SUCCESS/MI_FAILURE
164 */
165 
166 int
167 smfi_setconn(oconn)
168 	char *oconn;
169 {
170 	size_t l;
171 
172 	if (oconn == NULL || *oconn == '\0')
173 		return MI_FAILURE;
174 	l = strlen(oconn) + 1;
175 	if ((conn = (char *) malloc(l)) == NULL)
176 		return MI_FAILURE;
177 	if (sm_strlcpy(conn, oconn, l) >= l)
178 		return MI_FAILURE;
179 	return MI_SUCCESS;
180 }
181 
182 /*
183 **  SMFI_SETBACKLOG -- set backlog
184 **
185 **	Parameters:
186 **		obacklog -- new backlog.
187 **
188 **	Returns:
189 **		MI_SUCCESS/MI_FAILURE
190 */
191 
192 int
193 smfi_setbacklog(obacklog)
194 	int obacklog;
195 {
196 	if (obacklog <= 0)
197 		return MI_FAILURE;
198 	backlog = obacklog;
199 	return MI_SUCCESS;
200 }
201 
202 
203 /*
204 **  SMFI_MAIN -- setup milter connnection and start listener.
205 **
206 **	Parameters:
207 **		none.
208 **
209 **	Returns:
210 **		MI_SUCCESS/MI_FAILURE
211 */
212 
213 int
214 smfi_main()
215 {
216 	int r;
217 
218 	(void) signal(SIGPIPE, SIG_IGN);
219 	if (conn == NULL)
220 	{
221 		smi_log(SMI_LOG_FATAL, "%s: missing connection information",
222 			smfi->xxfi_name);
223 		return MI_FAILURE;
224 	}
225 
226 	(void) atexit(mi_clean_signals);
227 	if (mi_control_startup(smfi->xxfi_name) != MI_SUCCESS)
228 	{
229 		smi_log(SMI_LOG_FATAL,
230 			"%s: Couldn't start signal thread",
231 			smfi->xxfi_name);
232 		return MI_FAILURE;
233 	}
234 	r = MI_SUCCESS;
235 
236 	/* Startup the listener */
237 	if (mi_listener(conn, dbg, smfi, timeout, backlog) != MI_SUCCESS)
238 		r = MI_FAILURE;
239 	return r;
240 }
241 
242