summaryrefslogtreecommitdiffstats
path: root/contrib/sendmail/libmilter/main.c
blob: d3150d140cd5927d2f489e7b385201fc9d3d601f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
 *  Copyright (c) 1999-2003, 2006 Sendmail, Inc. and its suppliers.
 *	All rights reserved.
 *
 * By using this file, you agree to the terms and conditions set
 * forth in the LICENSE file which can be found at the top level of
 * the sendmail distribution.
 *
 */

#include <sm/gen.h>
SM_RCSID("@(#)$Id: main.c,v 8.81 2006/11/02 18:31:43 ca Exp $")

#define _DEFINE	1
#include "libmilter.h"
#include <fcntl.h>
#include <sys/stat.h>


static smfiDesc_ptr smfi = NULL;

/*
**  SMFI_REGISTER -- register a filter description
**
**	Parameters:
**		smfilter -- description of filter to register
**
**	Returns:
**		MI_SUCCESS/MI_FAILURE
*/

int
smfi_register(smfilter)
	smfiDesc_str smfilter;
{
	size_t len;

	if (smfi == NULL)
	{
		smfi = (smfiDesc_ptr) malloc(sizeof *smfi);
		if (smfi == NULL)
			return MI_FAILURE;
	}
	(void) memcpy(smfi, &smfilter, sizeof *smfi);
	if (smfilter.xxfi_name == NULL)
		smfilter.xxfi_name = "Unknown";

	len = strlen(smfilter.xxfi_name) + 1;
	smfi->xxfi_name = (char *) malloc(len);
	if (smfi->xxfi_name == NULL)
		return MI_FAILURE;
	(void) sm_strlcpy(smfi->xxfi_name, smfilter.xxfi_name, len);

	/* compare milter version with hard coded version */
	if (smfi->xxfi_version != SMFI_VERSION)
	{
		/* hard failure for now! */
		smi_log(SMI_LOG_ERR,
			"%s: smfi_register: version mismatch application: %d != milter: %d",
			smfi->xxfi_name, smfi->xxfi_version,
			(int) SMFI_VERSION);

		/* XXX how about smfi? */
		free(smfi->xxfi_name);
		return MI_FAILURE;
	}

	return MI_SUCCESS;
}

/*
**  SMFI_STOP -- stop milter
**
**	Parameters:
**		none.
**
**	Returns:
**		success.
*/

int
smfi_stop()
{
	mi_stop_milters(MILTER_STOP);
	return MI_SUCCESS;
}

/*
**  Default values for some variables.
**	Most of these can be changed with the functions below.
*/

static int dbg = 0;
static char *conn = NULL;
static int timeout = MI_TIMEOUT;
static int backlog = MI_SOMAXCONN;

/*
**  SMFI_OPENSOCKET -- try the socket setup to make sure we'll be
**		       able to start up
**
**	Parameters:
**		rmsocket -- if true, instructs libmilter to attempt
**			to remove the socket before creating it;
**			only applies for "local:" or "unix:" sockets
**
**	Return:
**		MI_SUCCESS/MI_FAILURE
*/

int
smfi_opensocket(rmsocket)
	bool rmsocket;
{
	if (smfi == NULL || conn == NULL)
		return MI_FAILURE;

	return mi_opensocket(conn, backlog, dbg, rmsocket, smfi);
}

/*
**  SMFI_SETDBG -- set debug level.
**
**	Parameters:
**		odbg -- new debug level.
**
**	Returns:
**		MI_SUCCESS
*/

int
smfi_setdbg(odbg)
	int odbg;
{
	dbg = odbg;
	return MI_SUCCESS;
}

/*
**  SMFI_SETTIMEOUT -- set timeout (for read/write).
**
**	Parameters:
**		otimeout -- new timeout.
**
**	Returns:
**		MI_SUCCESS
*/

int
smfi_settimeout(otimeout)
	int otimeout;
{
	timeout = otimeout;
	return MI_SUCCESS;
}

/*
**  SMFI_SETCONN -- set connection information (socket description)
**
**	Parameters:
**		oconn -- new connection information.
**
**	Returns:
**		MI_SUCCESS/MI_FAILURE
*/

int
smfi_setconn(oconn)
	char *oconn;
{
	size_t l;

	if (oconn == NULL || *oconn == '\0')
		return MI_FAILURE;
	l = strlen(oconn) + 1;
	if ((conn = (char *) malloc(l)) == NULL)
		return MI_FAILURE;
	if (sm_strlcpy(conn, oconn, l) >= l)
		return MI_FAILURE;
	return MI_SUCCESS;
}

/*
**  SMFI_SETBACKLOG -- set backlog
**
**	Parameters:
**		obacklog -- new backlog.
**
**	Returns:
**		MI_SUCCESS/MI_FAILURE
*/

int
smfi_setbacklog(obacklog)
	int obacklog;
{
	if (obacklog <= 0)
		return MI_FAILURE;
	backlog = obacklog;
	return MI_SUCCESS;
}


/*
**  SMFI_MAIN -- setup milter connnection and start listener.
**
**	Parameters:
**		none.
**
**	Returns:
**		MI_SUCCESS/MI_FAILURE
*/

int
smfi_main()
{
	int r;

	(void) signal(SIGPIPE, SIG_IGN);
	if (conn == NULL)
	{
		smi_log(SMI_LOG_FATAL, "%s: missing connection information",
			smfi->xxfi_name);
		return MI_FAILURE;
	}

	(void) atexit(mi_clean_signals);
	if (mi_control_startup(smfi->xxfi_name) != MI_SUCCESS)
	{
		smi_log(SMI_LOG_FATAL,
			"%s: Couldn't start signal thread",
			smfi->xxfi_name);
		return MI_FAILURE;
	}
	r = MI_MONITOR_INIT();

	/* Startup the listener */
	if (mi_listener(conn, dbg, smfi, timeout, backlog) != MI_SUCCESS)
		r = MI_FAILURE;

	return r;
}

OpenPOWER on IntegriCloud