Added support for Allowed by default filters in addition to Denied by default
[mmondor.git] / mmsoftware / mmmail / src / mmsmtpd / mmsmtpd.h
1 /* $Id: mmsmtpd.h,v 1.28 2005/02/15 22:30:22 mmondor Exp $ */
2
3 /*
4 * Copyright (C) 2001-2004, Matthew Mondor
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software written by Matthew Mondor.
18 * 4. The name of Matthew Mondor may not be used to endorse or promote
19 * products derived from this software without specific prior written
20 * permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY MATTHEW MONDOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL MATTHEW MONDOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
28 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34
35
36
37 #ifndef MMSMTPD_H
38 #define MMSMTPD_H
39
40
41
42
43 /* HEADERS */
44
45 #include <sys/types.h>
46 #include <time.h>
47
48 #include <pth.h>
49
50 #include <mmtypes.h>
51 #include <mmlist.h>
52 #include <mmpool.h>
53 #include <mmhash.h>
54 #include <mmserver.h>
55 #include <mmfd.h>
56 #include <mmstat.h>
57 #include <mmlimitrate.h>
58
59
60
61
62 /* DEFINITIONS */
63 #define DAEMON_NAME "mmsmtpd"
64 #define DAEMON_VERSION "mmmail-0.0.24/mmondor"
65
66 /* Negative states are used by the state swapper, others are real states */
67 #define STATE_ERROR -3
68 #define STATE_END -2
69 #define STATE_CURRENT -1
70 #define STATE_ALL 0
71
72 /* Invalid RCPT reason */
73 enum rcpt_reason {
74 RCPT_OK = 0,
75 RCPT_NOFROM,
76 RCPT_MANY,
77 RCPT_INVALID,
78 RCPT_UNKNOWN,
79 RCPT_RELAY,
80 RCPT_EXISTS,
81 RCPT_FULL,
82 RCPT_FLOOD,
83 RCPT_FILTER,
84 RCPT_ERROR,
85 RCPT_MAX
86 };
87
88 /* DATA errors */
89 enum data_reason {
90 DATA_SUBMIT = 0,
91 DATA_OK,
92 DATA_OVERFLOW,
93 DATA_HOPS,
94 DATA_INTERNAL,
95 DATA_MAX
96 };
97
98 /* Resolving flags for valid_host() */
99 #define HOST_NORES 0
100 #define HOST_RES 1
101 #define HOST_RES_MX 2
102
103 /* Custom fdbreadbuf() return result */
104 #define CFDBRB_HOPS -1
105
106 /* Asynchroneous functions we attach */
107 #define ASYNC_RESQUERY 1
108
109 /* Error registration macro */
110 #define REGISTER_ERROR(x) do { \
111 (x)->errors++; \
112 if (CONF.DELAY_ON_ERROR) \
113 pth_sleep((x)->errors); \
114 } while(FALSE)
115
116 /* Evaluates if a character is valid for addresses and hostnames */
117 #define VALID_CHAR(c) (((c) >= 'a' && (c) <= 'z') || \
118 ((c) >= '0' && (c) <= '9') || (c) == '.' || (c) == '-' || (c) == '_')
119
120
121
122 /* STRUCTURES */
123 /* We store config file read results in this structure */
124 typedef struct config {
125 char CHROOT_DIR[256], PID_PATH[256], USER[32], GROUPS[256],
126 LOG_FACILITY[32], SERVER_NAMES[1024], LISTEN_IPS[1024], DB_HOST[64],
127 DB_USER[32], DB_PASSWORD[32], DB_DATABASE[32], MAIL_DIR[256];
128 long ALLOC_BUFFERS, LOG_LEVEL, LISTEN_PORT, MAX_ERRORS, MAX_IPS,
129 MAX_PER_IP, CONNECTION_RATE, CONNECTION_PERIOD, INPUT_TIMEOUT,
130 BANDWIDTH_IN, BANDWIDTH_OUT, GBANDWIDTH_IN, GBANDWIDTH_OUT, MAX_RCPTS,
131 MAX_DATA_LINES, MAX_DATA_SIZE, MAX_HOPS, FLOOD_MESSAGES,
132 FLOOD_EXPIRES, FLOOD_CACHE, ASYNC_PROCESSES;
133 bool RESOLVE_HOSTS, RESOLVE_HELO, RESOLVE_MX_MAIL, RESOLVE_MX_RCPT,
134 REQUIRE_HELO, FLOOD_PROTECTION, STATFAIL_ADDRESS, STATFAIL_RELAY,
135 STATFAIL_FLOOD, STATFAIL_FULL, STATFAIL_TIMEOUT, STATFAIL_EOF,
136 STATFAIL_FILTER, DELAY_ON_ERROR, RELAYING;
137 } CONFIG;
138
139
140 /* This consists of the state-shared clientenv structure */
141 typedef struct clientenv {
142 pnode_t node;
143 fdbuf *fdb; /* Buffered handler around our fd */
144 char *buffer; /* Buffer that points to last command line */
145 char *helo; /* Cached helo hostname */
146 char *from; /* Cached mail sender address */
147 char *c_hostname; /* Pointer to client's hostname */
148 char *c_ipaddr; /* Pointer to client's IP address string */
149 long mesg_size; /* Current cached message size in bytes */
150 long errors; /* Total number of errors that occured */
151 int timeout; /* Timeout in ms */
152 unsigned long id; /* Our connection ID */
153 unsigned long messages; /* Messages user sent us */
154 unsigned long rcpts; /* Number of RCPT accepted */
155 struct iface *iface; /* Current interface user connected through */
156 struct async_clenv *aclenv; /* Thread context for async_call() */
157 list_t rcpt; /* Cached recepients to send mail to */
158 mmstat_t vstat, pstat; /* mmstat(3) handles */
159 } clientenv;
160
161 /* Used for RCPT addresses */
162 typedef struct rcptnode {
163 pnode_t node;
164 char address[64], foraddress[64];
165 u_int64_t hash;
166 bool relay; /* Non-local */
167 } rcptnode;
168
169 /* This structure is used to keep a cache of recent hosts from which mail was
170 * received, along with information on it to determine if the rate of messages
171 * is too high.
172 */
173 typedef struct hostnode {
174 hashnode_t node;
175 char host[128]; /* Hostname, key */
176 struct limitrate lr;
177 } hostnode;
178
179 struct hosts_expire_thread_iterator_udata {
180 time_t current, soonest;
181 int cnt;
182 };
183
184 /* Used for mmfd thread support delegation/abstraction */
185 struct mutexnode {
186 pnode_t node;
187 pth_mutex_t mutex;
188 };
189
190 /* This defines a state */
191 typedef struct state {
192 int (**functions)(clientenv *);
193 int errcode;
194 char *errtext;
195 } state;
196
197 /* A command of a state */
198 typedef struct command {
199 int loglevel;
200 char *name, *args, *desc;
201 } command;
202
203 /* Information for a mailbox */
204 struct box_info {
205 long max_size, size, max_msgs, msgs;
206 bool filter;
207 char filter_type;
208 };
209
210 /* For fast command lookup */
211 struct commandnode {
212 hashnode_t node;
213 u_int32_t hash;
214 struct command *command;
215 int index;
216 };
217
218 /* Used for fast index of result messages */
219 struct reply_messages {
220 int code;
221 char *msg;
222 };
223
224 /* Our validate_msg_line() context */
225 struct validate_udata
226 {
227 /* Used to count number of Received: lines */
228 long hops;
229 /* Headers we consider mandatory, which we'll add if necessary */
230 bool msgid, date, from, to;
231 /* Still considering to read the header */
232 bool header;
233 /* Internal linking */
234 clientenv *clenv;
235 };
236
237 /* Our union for async_resquery() */
238 struct async_resquery_msg {
239 struct async_msg msg;
240 union {
241 struct {
242 int res;
243 char answer[128];
244 } res;
245 struct {
246 int r_class, r_type;
247 char host[128];
248 } args;
249 } un;
250 };
251
252
253
254
255 /* PROTOTYPES */
256
257 int main(int, char **);
258
259 static int all_noop(clientenv *);
260 static int all_rset(clientenv *);
261 static int all_quit(clientenv *);
262 static int all_help(clientenv *);
263 static int all_helo(clientenv *);
264 static int all_mail(clientenv *);
265 static int all_rcpt(clientenv *);
266 static int all_data(clientenv *);
267 static int all_beer(clientenv *);
268
269 static bool hash_commands(struct command *, size_t);
270 static u_int32_t commandnode_keyhash(const void *, size_t);
271 static int commandnode_keycmp(const void *, const void *, size_t);
272 static bool reply(fdbuf *, int, bool, const char *, ...);
273
274 static clientenv *alloc_clientenv(void);
275 static bool init_clientenv(clientenv *, bool);
276 static clientenv *free_clientenv(clientenv *);
277 static void empty_rcpts(list_t *);
278 static bool check_alias(char *);
279 static bool check_nofrom(const char *, const char *);
280 static int best_match(const char *, const char *);
281 static bool local_address(struct box_info *, const char *);
282 static bool box_filter_allow(const char *, const char *, char);
283 static void rfc_time(char *);
284 static bool valid_address(clientenv *, char *, char *, int);
285 static bool valid_host(clientenv *, char *, int, bool, bool);
286 static bool valid_ipaddress(const char *);
287
288 static int validate_msg_line(char *, ssize_t *, int *, void *);
289 static bool do_data(clientenv *);
290 inline static size_t do_data_received(char *, size_t, clientenv *, rcptnode *,
291 const char *);
292 inline static bool do_data_update(rcptnode *, size_t);
293 static void do_data_stats(clientenv *, rcptnode *, size_t);
294 #if defined(MMMAIL_MYSQL)
295 static bool do_data_mysql(clientenv *, struct fdbrb_buffer *);
296 #elif defined(MMMAIL_FILE)
297 static bool address_relay_allow(clientenv *, int *, const char *);
298 static void iso_time(char *);
299 static bool message_write(char *, const char *, size_t, struct fdbrb_buffer *,
300 const char *);
301 static bool do_data_file(clientenv *, struct fdbrb_buffer *);
302 static bool do_data_queue_box(clientenv *, const char *, size_t, struct
303 fdbrb_buffer *, rcptnode *);
304 static bool do_data_queue_relay(clientenv *, const char *, size_t, struct
305 fdbrb_buffer *, rcptnode *);
306 #else
307 #error "One of MMMAIL_MYSQL or MMMAIL_FILE must be #defined!"
308 #endif
309
310 static int handleclient(unsigned long, int, clientlistnode *, struct iface *,
311 struct async_clenv *);
312
313 static void *_pth_mutex_create(void);
314 static void *_pth_mutex_destroy(void *);
315 static void _pth_mutex_lock(void *);
316 static void _pth_mutex_unlock(void *);
317 static void _pth_thread_yield(void);
318 static void _pth_thread_sleep(int);
319 static bool _pth_eintr(void);
320
321 static void async_resquery(struct async_msg *);
322 static int a_res_query(clientenv *, const char *, int, int, u_char *, int);
323
324 static void *hosts_expire_thread(void *);
325 static bool hosts_expire_thread_iterator(hashnode_t *, void *);
326
327 static void *db_gc_thread(void *);
328 static void db_gc_thread_delete(const char *);
329
330
331
332
333 #endif