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