xref: /freebsd/contrib/sendmail/libmilter/main.c (revision 13058a916175518dfbac6ce66b9b8e22ecf43155)
1 /*
2  *  Copyright (c) 1999-2001 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 #ifndef lint
12 static char id[] = "@(#)$Id: main.c,v 8.34.4.11 2001/05/07 22:06:37 gshapiro Exp $";
13 #endif /* ! lint */
14 
15 #if _FFR_MILTER
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) 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 		return MI_FAILURE;
65 	}
66 
67 	return MI_SUCCESS;
68 }
69 
70 /*
71 **  SMFI_STOP -- stop milter
72 **
73 **	Parameters:
74 **		none.
75 **
76 **	Returns:
77 **		success.
78 */
79 
80 int
81 smfi_stop()
82 {
83 	mi_stop_milters(MILTER_STOP);
84 	return MI_SUCCESS;
85 }
86 
87 static int dbg = 0;
88 static char *conn = NULL;
89 static int timeout = MI_TIMEOUT;
90 static int backlog= MI_SOMAXCONN;
91 
92 int
93 smfi_setdbg(odbg)
94 	int odbg;
95 {
96 	dbg = odbg;
97 	return MI_SUCCESS;
98 }
99 
100 int
101 smfi_settimeout(otimeout)
102 	int otimeout;
103 {
104 	timeout = otimeout;
105 	return MI_SUCCESS;
106 }
107 
108 int
109 smfi_setconn(oconn)
110 	char *oconn;
111 {
112 	size_t l;
113 
114 	if (oconn == NULL || *oconn == '\0')
115 		return MI_FAILURE;
116 	l = strlen(oconn) + 1;
117 	if ((conn = (char *) malloc(l)) == NULL)
118 		return MI_FAILURE;
119 	if (strlcpy(conn, oconn, l) >= l)
120 		return MI_FAILURE;
121 	return MI_SUCCESS;
122 }
123 
124 int
125 smfi_setbacklog(obacklog)
126 	int obacklog;
127 {
128 	if (obacklog <= 0)
129 		return MI_FAILURE;
130 	backlog = obacklog;
131 	return MI_SUCCESS;
132 }
133 
134 
135 int
136 smfi_main()
137 {
138 
139 	signal(SIGPIPE, SIG_IGN);
140 	if (conn == NULL)
141 	{
142 		smi_log(SMI_LOG_FATAL, "%s: missing connection information",
143 			smfi->xxfi_name);
144 		return MI_FAILURE;
145 	}
146 
147 	(void) atexit(mi_clean_signals);
148 	if (mi_control_startup(smfi->xxfi_name) != MI_SUCCESS)
149 	{
150 		smi_log(SMI_LOG_FATAL,
151 			"%s: Couldn't start signal thread",
152 			smfi->xxfi_name);
153 		return MI_FAILURE;
154 	}
155 
156 
157 	/* Startup the listener */
158 	if (mi_listener(conn, dbg, smfi, timeout, backlog) != MI_SUCCESS)
159 		return MI_FAILURE;
160 
161 	return MI_SUCCESS;
162 }
163 #endif /* _FFR_MILTER */
164