Merged pthread-branch to trunk
[mmondor.git] / mmsoftware / mmmail / src / mmsmtpd / mmsmtpd.h
1 /* $Id: mmsmtpd.h,v 1.34 2007/03/13 20:28: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 <pthread.h>
49 #include <mm_pthread_sleep.h>
50
51 #include <mmtypes.h>
52 #include <mmlist.h>
53 #include <mmpool.h>
54 #include <mmhash.h>
55 #include <mmserver.h>
56 #include <mmfd.h>
57 #include <mmstat.h>
58 #include <mmlimitrate.h>
59
60
61
62
63 /* DEFINITIONS */
64 #define DAEMON_NAME "mmsmtpd"
65 #define DAEMON_VERSION "mmmail-0.1.0/mmondor"
66
67 /* Negative states are used by the state swapper, others are real states */
68 #define STATE_ERROR -3
69 #define STATE_END -2
70 #define STATE_CURRENT -1
71 #define STATE_ALL 0
72
73 /* Invalid RCPT reason */
74 enum rcpt_reason {
75 RCPT_OK = 0,
76 RCPT_NOFROM,
77 RCPT_MANY,
78 RCPT_INVALID,
79 RCPT_UNKNOWN,
80 RCPT_RELAY,
81 RCPT_EXISTS,
82 RCPT_FULL,
83 RCPT_FLOOD,
84 RCPT_FILTER,
85 RCPT_ERROR,
86 RCPT_MAX
87 };
88
89 /* DATA errors */
90 enum data_reason {
91 DATA_SUBMIT = 0,
92 DATA_OK,
93 DATA_OVERFLOW,
94 DATA_HOPS,
95 DATA_INTERNAL,
96 DATA_MAX
97 };
98
99 /* Resolving flags for valid_host() */
100 #define HOST_NORES 0
101 #define HOST_RES 1
102 #define HOST_RES_MX 2
103
104 /* Custom fdbreadbuf() return result */
105 #define CFDBRB_HOPS -1
106
107 /* Asynchroneous functions we attach */
108 #define ASYNC_RESQUERY 1
109
110 /* Error registration macro */
111 #define REGISTER_ERROR(x) do { \
112 (x)->errors++; \
113 if (CONF.DELAY_ON_ERROR) \
114 pthread_sleep((x)->errors); \
115 } while(FALSE)
116
117 /* Evaluates if a character is valid for addresses and hostnames */
118 #define VALID_ADDR_CHAR(c) \
119 (valid_addr_char_table[(int)((unsigned char)(c))] != 0)
120 #define VALID_HOST_CHAR(c) \
121 (valid_addr_host_table[(int)((unsigned char)(c))] != 0)
122
123
124
125 /* STRUCTURES */
126
127 /* We store config file read results in this structure */
128 typedef struct config {
129 char LOCK_PATH[256], CHROOT_DIR[256], PID_PATH[256], USER[32], GROUPS[256],
130 LOG_FACILITY[32], SERVER_NAMES[1024], LISTEN_IPS[1024], DB_HOST[64],
131 DB_USER[32], DB_PASSWORD[32], DB_DATABASE[32], MAIL_DIR[256],
132 MMRELAYD_SOCKET_PATH[256];
133 long ALLOC_BUFFERS, LOG_LEVEL, LISTEN_PORT, MAX_ERRORS, MAX_IPS,
134 MAX_PER_IP, CONNECTION_RATE, CONNECTION_PERIOD, INPUT_TIMEOUT,
135 BANDWIDTH_IN, BANDWIDTH_OUT, GBANDWIDTH_IN, GBANDWIDTH_OUT, MAX_RCPTS,
136 MAX_DATA_LINES, MAX_DATA_SIZE, MAX_HOPS, FLOOD_MESSAGES,
137 FLOOD_EXPIRES, FLOOD_CACHE, ASYNC_PROCESSES;
138 bool RESOLVE_HOSTS, RESOLVE_HELO, RESOLVE_MX_MAIL, RESOLVE_MX_RCPT,
139 REQUIRE_HELO, FLOOD_PROTECTION, STATFAIL_ADDRESS, STATFAIL_RELAY,
140 STATFAIL_FLOOD, STATFAIL_FULL, STATFAIL_TIMEOUT, STATFAIL_EOF,
141 STATFAIL_FILTER, DELAY_ON_ERROR, RELAYING;
142 } CONFIG;
143
144
145 /* This consists of the state-shared clientenv structure */
146 typedef struct clientenv {
147 pnode_t node;
148 fdbuf *fdb; /* Buffered handler around our fd */
149 char *buffer; /* Buffer that points to last command line */
150 char *helo; /* Cached helo hostname */
151 char *from; /* Cached mail sender address */
152 char *c_hostname; /* Pointer to client's hostname */
153 char *c_ipaddr; /* Pointer to client's IP address string */
154 long mesg_size; /* Current cached message size in bytes */
155 long errors; /* Total number of errors that occured */
156 int timeout; /* Timeout in ms */
157 bool nofrom; /* If empty MAIL FROM from allowed server */
158 unsigned long id; /* Our connection ID */
159 unsigned long messages; /* Messages user sent us */
160 unsigned long rcpts; /* Number of RCPT accepted */
161 struct iface *iface; /* Current interface user connected through */
162 struct async_clenv *aclenv; /* Thread context for async_call() */
163 list_t rcpt; /* Cached recepients to send mail to */
164 mmstat_t vstat, pstat; /* mmstat(3) handles */
165 } clientenv;
166
167 /* Used for RCPT addresses */
168 typedef struct rcptnode {
169 pnode_t node;
170 char address[64], foraddress[64];
171 u_int64_t hash;
172 bool relay; /* Non-local */
173 } rcptnode;
174
175 /* This structure is used to keep a cache of recent hosts from which mail was
176 * received, along with information on it to determine if the rate of messages
177 * is too high.
178 */
179 typedef struct hostnode {
180 hashnode_t node;
181 char host[128]; /* Hostname, key */
182 struct limitrate lr;
183 } hostnode;
184
185 struct hosts_expire_thread_iterator_udata {
186 time_t current, soonest;
187 };
188
189 /* Used for mmfd thread support delegation/abstraction */
190 struct mutexnode {
191 pnode_t node;
192 pthread_mutex_t mutex;
193 };
194
195 /* This defines a state */
196 typedef struct state {
197 int (**functions)(clientenv *);
198 int errcode;
199 char *errtext;
200 } state;
201
202 /* A command of a state */
203 typedef struct command {
204 int loglevel;
205 char *name, *args, *desc;
206 } command;
207
208 /* Information for a mailbox */
209 struct box_info {
210 long max_size, size, max_msgs, msgs;
211 bool filter;
212 char filter_type;
213 };
214
215 /* For fast command lookup */
216 struct commandnode {
217 hashnode_t node;
218 u_int32_t hash;
219 struct command *command;
220 int index;
221 };
222
223 /* Used for fast index of result messages */
224 struct reply_messages {
225 int code;
226 char *msg;
227 };
228
229 /* Our validate_msg_line() context */
230 struct validate_udata
231 {
232 /* Used to count number of Received: lines */
233 long hops;
234 /* Headers we consider mandatory, which we'll add if necessary */
235 bool msgid, date, from, to;
236 /* Still considering to read the header */
237 bool header;
238 /* Internal linking */
239 clientenv *clenv;
240 };
241
242 /* Our union for async_resquery() */
243 struct async_resquery_msg {
244 struct async_msg msg;
245 union {
246 struct {
247 int res;
248 char answer[128];
249 } res;
250 struct {
251 int r_class, r_type;
252 char host[128];
253 } args;
254 } un;
255 };
256
257
258
259
260 /* PROTOTYPES */
261
262 int main(int, char **);
263
264 static int all_noop(clientenv *);
265 static int all_rset(clientenv *);
266 static int all_quit(clientenv *);
267 static int all_help(clientenv *);
268 static int all_helo(clientenv *);
269 static int all_mail(clientenv *);
270 static int all_rcpt(clientenv *);
271 static int all_data(clientenv *);
272 static int all_beer(clientenv *);
273
274 static bool hash_commands(struct command *, size_t);
275 static u_int32_t commandnode_keyhash(const void *, size_t);
276 static int commandnode_keycmp(const void *, const void *, size_t);
277 static bool reply(fdbuf *, int, bool, const char *, ...);
278
279 static clientenv *alloc_clientenv(void);
280 static bool init_clientenv(clientenv *, bool);
281 static clientenv *free_clientenv(clientenv *);
282 static void empty_rcpts(list_t *);
283 static bool check_alias(char *);
284 static bool check_nofrom(const char *, const char *);
285 static int lock_check(const char *);
286 static int best_match(const char *, const char *);
287 static bool local_address(struct box_info *, const char *);
288 static bool box_filter_allow(const char *, const char *, char);
289 static void rfc_time(char *);
290 static bool valid_address(clientenv *, char *, size_t, char *, int);
291 static bool valid_host(clientenv *, char *, int, bool, bool);
292 static bool valid_ipaddress(const char *);
293
294 static int validate_msg_line(char *, ssize_t *, int *, void *);
295 static bool do_data(clientenv *);
296 inline static size_t do_data_received(char *, size_t, clientenv *, rcptnode *,
297 const char *);
298 inline static bool do_data_update(rcptnode *, size_t);
299 static void do_data_stats(clientenv *, rcptnode *, size_t);
300 #if defined(MMMAIL_MYSQL)
301 static bool do_data_mysql(clientenv *, struct fdbrb_buffer *);
302 #elif defined(MMMAIL_FILE)
303 static bool address_relay_allow(clientenv *, int *, const char *);
304 static void iso_time(char *);
305 static bool message_write(char *, const char *, size_t, struct fdbrb_buffer *,
306 const char *);
307 static bool do_data_file(clientenv *, struct fdbrb_buffer *);
308 static bool do_data_queue_box(clientenv *, const char *, size_t, struct
309 fdbrb_buffer *, rcptnode *);
310 static bool do_data_queue_relay(clientenv *, const char *, size_t, struct
311 fdbrb_buffer *, rcptnode *);
312 static void do_data_queue_notify(clientenv *);
313 static int do_data_queue_notify_connect(void);
314 #else
315 #error "One of MMMAIL_MYSQL or MMMAIL_FILE must be #defined!"
316 #endif
317
318 static int handleclient(unsigned long, int, clientlistnode *, struct iface *,
319 struct async_clenv *);
320
321 static void thread_init(void);
322 static void *thread_mutex_create(void);
323 static void *thread_mutex_destroy(void *);
324 static void thread_mutex_lock(void *);
325 static void thread_mutex_unlock(void *);
326 static bool thread_eintr(void);
327
328 static void async_resquery(struct async_msg *);
329 static int a_res_query(clientenv *, const char *, int, int, u_char *, int);
330
331 static void *hosts_expire_thread(void *);
332 static bool hosts_expire_thread_iterator(hashnode_t *, void *);
333
334 static void *db_gc_thread(void *);
335 static void db_gc_thread_delete(const char *);
336
337
338
339
340 #endif