ac5f4ccec01849b43aa158fdad901deb48a43719
[mmondor.git] / mmsoftware / mmmail / src / mmsmtpd / mmsmtpd.h
1 /* $Id: mmsmtpd.h,v 1.50 2009/02/04 09:02:25 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.2/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 #define CFDBRB_NOHOP -3
116
117 /* Asynchroneous functions we attach */
118 #define ASYNC_RESQUERY 1
119
120 /* Error registration macro */
121 #define REGISTER_ERROR(x) do { \
122 (x)->errors++; \
123 if (CONF.DELAY_ON_ERROR) \
124 pthread_sleep((x)->errors); \
125 } while (/* CONSTCOND */0)
126
127 /* Evaluates if a character is valid for addresses and hostnames */
128 #define VALID_ADDR_CHAR(c) \
129 (valid_addr_char_table[(int)((unsigned char)(c))] != 0)
130 #define VALID_HOST_CHAR(c) \
131 (valid_addr_host_table[(int)((unsigned char)(c))] != 0)
132
133
134
135 /* STRUCTURES */
136
137 /* We store config file read results in this structure */
138 typedef struct config {
139 char LOCK_PATH[256], CHROOT_DIR[256], PID_PATH[256], USER[32], GROUPS[256],
140 LOG_FACILITY[32], SERVER_NAMES[1024], LISTEN_IPS[1024], DB_INFO[1024],
141 MAIL_DIR[256], MMRELAYD_SOCKET_PATH[256];
142 long ALLOC_BUFFERS, LOG_LEVEL, LISTEN_PORT, MAX_ERRORS, MAX_IPS,
143 MAX_PER_IP, CONNECTION_RATE, CONNECTION_PERIOD, INPUT_TIMEOUT,
144 BANDWIDTH_IN, BANDWIDTH_OUT, GBANDWIDTH_IN, GBANDWIDTH_OUT, MAX_RCPTS,
145 MAX_DATA_LINES, MAX_DATA_SIZE, MAX_HOPS, FLOOD_MESSAGES,
146 FLOOD_EXPIRES, FLOOD_CACHE, ASYNC_PROCESSES;
147 bool RESOLVE_HOSTS, RESOLVE_HELO, RESOLVE_MX_MAIL, RESOLVE_MX_RCPT,
148 REQUIRE_HELO, REQUIRE_HOP, FLOOD_PROTECTION, STATFAIL_HELO,
149 STATFAIL_NOHELO, STATFAIL_NOFROM, STATFAIL_ADDRESS, STATFAIL_RELAY,
150 STATFAIL_FLOOD, STATFAIL_FULL, STATFAIL_TIMEOUT, STATFAIL_EOF,
151 STATFAIL_FILTER, DELAY_ON_ERROR, RELAYING;
152 } CONFIG;
153
154
155 /* This consists of the state-shared clientenv structure */
156 typedef struct clientenv {
157 pnode_t node;
158 fdbuf *fdb; /* Buffered handler around our fd */
159 char *buffer; /* Buffer that points to last command line */
160 char *helo; /* Cached helo hostname */
161 char *from; /* Cached mail sender address */
162 char *c_hostname; /* Pointer to client's hostname */
163 char *c_ipaddr; /* Pointer to client's IP address string */
164 long mesg_size; /* Current cached message size in bytes */
165 long errors; /* Total number of errors that occured */
166 int timeout; /* Timeout in ms */
167 bool nofrom; /* If empty MAIL FROM from allowed server */
168 unsigned long id; /* Our connection ID */
169 unsigned long messages; /* Messages user sent us */
170 unsigned long rcpts; /* Number of RCPT accepted */
171 struct iface *iface; /* Current interface user connected through */
172 list_t rcpt; /* Cached recepients to send mail to */
173 struct async_clenv *aclenv; /* Thread context for async_call() */
174 mmstat_t vstat, pstat; /* Persistent mmstat(3) handles */
175 PGconn *pgconn; /* Persistent pgsql connection */
176 } clientenv;
177
178 /* Used for RCPT addresses */
179 typedef struct rcptnode {
180 pnode_t node;
181 char address[64], foraddress[64];
182 uint64_t hash;
183 bool relay; /* Non-local */
184 } rcptnode;
185
186 /* This structure is used to keep a cache of recent hosts from which mail was
187 * received, along with information on it to determine if the rate of messages
188 * is too high.
189 */
190 typedef struct hostnode {
191 hashnode_t node;
192 char host[128]; /* Hostname, key */
193 struct limitrate lr;
194 } hostnode;
195
196 struct hosts_expire_thread_iterator_udata {
197 time_t current, soonest;
198 };
199
200 /* Used for mmfd thread support delegation/abstraction */
201 struct mutexnode {
202 pnode_t node;
203 pthread_mutex_t mutex;
204 };
205
206 /* This defines a state */
207 typedef struct state {
208 int (**functions)(clientenv *);
209 int errcode;
210 char *errtext;
211 } state;
212
213 /* A command of a state */
214 typedef struct command {
215 int loglevel;
216 char *name, *args, *desc;
217 } command;
218
219 /* Information for a mailbox */
220 struct box_info {
221 long max_size, size, max_msgs, msgs;
222 bool filter, filter_type;
223 };
224
225 /* For fast command lookup */
226 struct commandnode {
227 hashnode_t node;
228 uint32_t hash;
229 struct command *command;
230 int index;
231 };
232
233 /* Used for fast index of result messages */
234 struct reply_messages {
235 int code;
236 char *msg;
237 };
238
239 /* Our validate_msg_line() context */
240 struct validate_udata
241 {
242 /* Used to count number of Received: lines */
243 long hops;
244 /* Headers we consider mandatory, which we'll add if necessary */
245 bool msgid, date, from, to, subject, inreply;
246 /* Still considering to read the header */
247 bool header;
248 /* Internal linking */
249 clientenv *clenv;
250 char *h_from, *h_to, *h_subject, *h_id, *h_reply;
251 };
252
253 /* Our union for async_resquery() */
254 struct async_resquery_msg {
255 struct async_msg msg;
256 union {
257 struct {
258 int res;
259 char answer[128];
260 } res;
261 struct {
262 int r_class, r_type;
263 char host[128];
264 } args;
265 } un;
266 };
267
268
269
270
271 /* PROTOTYPES */
272
273 int main(int, char **);
274
275 static int all_noop(clientenv *);
276 static int all_rset(clientenv *);
277 static int all_quit(clientenv *);
278 static int all_help(clientenv *);
279 static int all_helo(clientenv *);
280 static int all_mail(clientenv *);
281 static int all_rcpt(clientenv *);
282 static int all_data(clientenv *);
283 static int all_beer(clientenv *);
284
285 static bool hash_commands(struct command *, size_t);
286 static uint32_t commandnode_keyhash(const void *, size_t);
287 static int commandnode_keycmp(const void *, const void *, size_t);
288 static bool reply(fdbuf *, int, bool, const char *, ...);
289
290 static bool clientenv_constructor(pnode_t *);
291 static void clientenv_destructor(pnode_t *);
292 static void *utdata_constructor(void);
293 static void utdata_destructor(void *);
294 static clientenv *alloc_clientenv(void);
295 static bool init_clientenv(clientenv *, bool);
296 static clientenv *free_clientenv(clientenv *);
297 static void empty_rcpts(list_t *);
298 static bool check_alias(clientenv *, char *);
299 static bool check_nofrom(clientenv *);
300 static int lock_check(const char *);
301 static int best_match(const char *, const char *);
302 static bool local_address(clientenv *, struct box_info *, const char *);
303 static bool box_filter_allow(clientenv *, const char *, const char *, bool);
304 static void rfc_time(char *);
305 static bool valid_address(clientenv *, char *, size_t, char *, int);
306 static bool valid_host(clientenv *, char *, int, bool, bool);
307 static bool valid_ipaddress(clientenv *, const char *);
308
309 static int validate_msg_line(char *, ssize_t *, int *, void *);
310 static bool do_data(clientenv *);
311 inline static size_t do_data_received(char *, size_t, clientenv *, rcptnode *,
312 const char *);
313 static void do_data_stats(clientenv *, rcptnode *, size_t);
314 static bool address_relay_allow(clientenv *, int *, const char *);
315 static void iso_time(char *);
316 static bool message_write(char *, const char *, size_t, struct fdbrb_buffer *,
317 const char *);
318 static bool do_data_file(clientenv *, struct fdbrb_buffer *,
319 struct validate_udata *);
320 static bool do_data_queue_box(clientenv *, const char *, size_t, struct
321 fdbrb_buffer *, rcptnode *, struct validate_udata *);
322 static bool do_data_queue_relay(clientenv *, const char *, size_t, struct
323 fdbrb_buffer *, rcptnode *);
324 static void do_data_queue_notify(clientenv *);
325 static int do_data_queue_notify_connect(void);
326
327 static int handleclient(unsigned long, int, clientlistnode *, struct iface *,
328 struct async_clenv *, void *);
329
330 static void thread_init(void);
331 static void *thread_mutex_create(void);
332 static void *thread_mutex_destroy(void *);
333 static void thread_mutex_lock(void *);
334 static void thread_mutex_unlock(void *);
335 static bool thread_eintr(void);
336
337 static void async_resquery(struct async_msg *);
338 static int a_res_query(clientenv *, const char *, int, int, char *, int);
339
340 static void *hosts_expire_thread(void *);
341 static bool hosts_expire_thread_iterator(hashnode_t *, void *);
342
343 static void *db_gc_thread(void *);
344
345
346
347
348 #endif