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