xref: /titanic_50/usr/src/cmd/sendmail/libmilter/main.c (revision 03831d35f7499c87d51205817c93e9a8d42c4bae)
1 /*
2  *  Copyright (c) 1999-2003 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
12 
13 #include <sm/gen.h>
14 SM_RCSID("@(#)$Id: main.c,v 8.79 2003/10/20 22:25:09 ca Exp $")
15 
16 #define _DEFINE	1
17 #include "libmilter.h"
18 #include <fcntl.h>
19 #include <sys/stat.h>
20 
21 
22 static smfiDesc_ptr smfi = NULL;
23 
24 /*
25 **  SMFI_REGISTER -- register a filter description
26 **
27 **	Parameters:
28 **		smfilter -- description of filter to register
29 **
30 **	Returns:
31 **		MI_SUCCESS/MI_FAILURE
32 */
33 
34 int
35 smfi_register(smfilter)
36 	smfiDesc_str smfilter;
37 {
38 	size_t len;
39 
40 	if (smfi == NULL)
41 	{
42 		smfi = (smfiDesc_ptr) malloc(sizeof *smfi);
43 		if (smfi == NULL)
44 			return MI_FAILURE;
45 	}
46 	(void) memcpy(smfi, &smfilter, sizeof *smfi);
47 	if (smfilter.xxfi_name == NULL)
48 		smfilter.xxfi_name = "Unknown";
49 
50 	len = strlen(smfilter.xxfi_name) + 1;
51 	smfi->xxfi_name = (char *) malloc(len);
52 	if (smfi->xxfi_name == NULL)
53 		return MI_FAILURE;
54 	(void) sm_strlcpy(smfi->xxfi_name, smfilter.xxfi_name, len);
55 
56 	/* compare milter version with hard coded version */
57 	if (smfi->xxfi_version != SMFI_VERSION)
58 	{
59 		/* hard failure for now! */
60 		smi_log(SMI_LOG_ERR,
61 			"%s: smfi_register: version mismatch application: %d != milter: %d",
62 			smfi->xxfi_name, smfi->xxfi_version,
63 			(int) SMFI_VERSION);
64 
65 		/* XXX how about smfi? */
66 		free(smfi->xxfi_name);
67 		return MI_FAILURE;
68 	}
69 
70 	return MI_SUCCESS;
71 }
72 
73 /*
74 **  SMFI_STOP -- stop milter
75 **
76 **	Parameters:
77 **		none.
78 **
79 **	Returns:
80 **		success.
81 */
82 
83 int
84 smfi_stop()
85 {
86 	mi_stop_milters(MILTER_STOP);
87 	return MI_SUCCESS;
88 }
89 
90 /*
91 **  Default values for some variables.
92 **	Most of these can be changed with the functions below.
93 */
94 
95 static int dbg = 0;
96 static char *conn = NULL;
97 static int timeout = MI_TIMEOUT;
98 static int backlog = MI_SOMAXCONN;
99 
100 /*
101 **  SMFI_OPENSOCKET -- try the socket setup to make sure we'll be
102 **		       able to start up
103 **
104 **	Parameters:
105 **		rmsocket -- if true, instructs libmilter to attempt
106 **			to remove the socket before creating it;
107 **			only applies for "local:" or "unix:" sockets
108 **
109 **	Return:
110 **		MI_SUCCESS/MI_FAILURE
111 */
112 
113 int
114 smfi_opensocket(rmsocket)
115 	bool rmsocket;
116 {
117 	if (smfi == NULL || conn == NULL)
118 		return MI_FAILURE;
119 
120 	return mi_opensocket(conn, backlog, dbg, rmsocket, smfi);
121 }
122 
123 /*
124 **  SMFI_SETDBG -- set debug level.
125 **
126 **	Parameters:
127 **		odbg -- new debug level.
128 **
129 **	Returns:
130 **		MI_SUCCESS
131 */
132 
133 int
134 smfi_setdbg(odbg)
135 	int odbg;
136 {
137 	dbg = odbg;
138 	return MI_SUCCESS;
139 }
140 
141 /*
142 **  SMFI_SETTIMEOUT -- set timeout (for read/write).
143 **
144 **	Parameters:
145 **		otimeout -- new timeout.
146 **
147 **	Returns:
148 **		MI_SUCCESS
149 */
150 
151 int
152 smfi_settimeout(otimeout)
153 	int otimeout;
154 {
155 	timeout = otimeout;
156 	return MI_SUCCESS;
157 }
158 
159 /*
160 **  SMFI_SETCONN -- set connection information (socket description)
161 **
162 **	Parameters:
163 **		oconn -- new connection information.
164 **
165 **	Returns:
166 **		MI_SUCCESS/MI_FAILURE
167 */
168 
169 int
170 smfi_setconn(oconn)
171 	char *oconn;
172 {
173 	size_t l;
174 
175 	if (oconn == NULL || *oconn == '\0')
176 		return MI_FAILURE;
177 	l = strlen(oconn) + 1;
178 	if ((conn = (char *) malloc(l)) == NULL)
179 		return MI_FAILURE;
180 	if (sm_strlcpy(conn, oconn, l) >= l)
181 		return MI_FAILURE;
182 	return MI_SUCCESS;
183 }
184 
185 /*
186 **  SMFI_SETBACKLOG -- set backlog
187 **
188 **	Parameters:
189 **		obacklog -- new backlog.
190 **
191 **	Returns:
192 **		MI_SUCCESS/MI_FAILURE
193 */
194 
195 int
196 smfi_setbacklog(obacklog)
197 	int obacklog;
198 {
199 	if (obacklog <= 0)
200 		return MI_FAILURE;
201 	backlog = obacklog;
202 	return MI_SUCCESS;
203 }
204 
205 
206 /*
207 **  SMFI_MAIN -- setup milter connnection and start listener.
208 **
209 **	Parameters:
210 **		none.
211 **
212 **	Returns:
213 **		MI_SUCCESS/MI_FAILURE
214 */
215 
216 int
217 smfi_main()
218 {
219 	int r;
220 
221 	(void) signal(SIGPIPE, SIG_IGN);
222 	if (conn == NULL)
223 	{
224 		smi_log(SMI_LOG_FATAL, "%s: missing connection information",
225 			smfi->xxfi_name);
226 		return MI_FAILURE;
227 	}
228 
229 	(void) atexit(mi_clean_signals);
230 	if (mi_control_startup(smfi->xxfi_name) != MI_SUCCESS)
231 	{
232 		smi_log(SMI_LOG_FATAL,
233 			"%s: Couldn't start signal thread",
234 			smfi->xxfi_name);
235 		return MI_FAILURE;
236 	}
237 	r = MI_SUCCESS;
238 
239 	/* Startup the listener */
240 	if (mi_listener(conn, dbg, smfi, timeout, backlog) != MI_SUCCESS)
241 		r = MI_FAILURE;
242 
243 	return r;
244 }
245 
246