Export more lyx -> tex
[mmondor.git] / mmsoftware / mmmail / src / mmsmtpd / mmsmtpd.h
1 /* $Id: mmsmtpd.h,v 1.58 2014/05/26 12:24:45 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.4.0"
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, DB_VACUUM;
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 bool list; /* Mailing list */
185 } rcptnode;
186
187 /* This structure is used to keep a cache of recent hosts from which mail was
188 * received, along with information on it to determine if the rate of messages
189 * is too high.
190 */
191 typedef struct hostnode {
192 hashnode_t node;
193 char host[128]; /* Hostname, key */
194 struct limitrate lr;
195 } hostnode;
196
197 struct hosts_expire_thread_iterator_udata {
198 time_t current, soonest;
199 };
200
201 /* Used for mmfd thread support delegation/abstraction */
202 struct mutexnode {
203 pnode_t node;
204 pthread_mutex_t mutex;
205 };
206
207 /* This defines a state */
208 typedef struct state {
209 int (**functions)(clientenv *);
210 int errcode;
211 char *errtext;
212 } state;
213
214 /* A command of a state */
215 typedef struct command {
216 int loglevel;
217 char *name, *args, *desc;
218 } command;
219
220 /* Information for a mailbox */
221 struct box_info {
222 long max_size, size, max_msgs, msgs;
223 bool filter, filter_type, list;
224 };
225
226 /* For fast command lookup */
227 struct commandnode {
228 hashnode_t node;
229 uint32_t hash;
230 struct command *command;
231 int index;
232 };
233
234 /* Used for fast index of result messages */
235 struct reply_messages {
236 int code;
237 char *msg;
238 };
239
240 /* Our validate_msg_line() context */
241 struct validate_udata
242 {
243 /* clientenv->id */
244 unsigned long id;
245 /* Used to count number of Received: lines */
246 long hops;
247 /* Headers we consider mandatory, which we'll add if necessary */
248 bool msgid, date, from, to, subject, inreply;
249 /* Still considering to read the header */
250 bool header;
251 /* Internal linking */
252 clientenv *clenv;
253 char *h_from, *h_to, *h_subject, *h_id, *h_reply;
254 };
255
256 /* Our union for async_resquery() */
257 struct async_resquery_msg {
258 struct async_msg msg;
259 union {
260 struct {
261 int res;
262 char answer[128];
263 } res;
264 struct {
265 int r_class, r_type;
266 char host[128];
267 } args;
268 } un;
269 };
270
271
272
273
274 /* PROTOTYPES */
275
276 int main(int, char **);
277
278 static int all_noop(clientenv *);
279 static int all_rset(clientenv *);
280 static int all_quit(clientenv *);
281 static int all_help(clientenv *);
282 static int all_helo(clientenv *);
283 static int all_mail(clientenv *);
284 static int all_rcpt(clientenv *);
285 static int all_data(clientenv *);
286 static int all_beer(clientenv *);
287
288 static bool hash_commands(struct command *, size_t);
289 static uint32_t commandnode_keyhash(const void *, size_t);
290 static int commandnode_keycmp(const void *, const void *, size_t);
291 static bool reply(clientenv *, int, bool, const char *, ...);
292
293 static bool clientenv_constructor(pnode_t *);
294 static void clientenv_destructor(pnode_t *);
295 static void *utdata_constructor(void);
296 static void utdata_destructor(void *);
297 static clientenv *alloc_clientenv(void);
298 static bool init_clientenv(clientenv *, bool);
299 static clientenv *free_clientenv(clientenv *);
300 static void empty_rcpts(list_t *);
301 static bool check_alias(clientenv *, char *);
302 static bool check_nofrom(clientenv *);
303 static int lock_check(const char *);
304 static int best_match(const char *, const char *);
305 static bool local_address(clientenv *, struct box_info *, const char *);
306 static bool box_filter_allow(clientenv *, const char *, const char *, bool);
307 static void rfc_time(char *);
308 static bool valid_address(clientenv *, char *, size_t, char *, int);
309 static bool valid_host(clientenv *, char *, int, bool, bool);
310 static bool valid_ipaddress(clientenv *, const char *);
311
312 static int validate_msg_line(char *, ssize_t *, int *, void *);
313 static bool do_data(clientenv *);
314 inline static size_t do_data_received(char *, size_t, clientenv *, rcptnode *,
315 const char *);
316 static void do_data_stats(clientenv *, rcptnode *, size_t);
317 static bool address_relay_allow(clientenv *, int *, const char *);
318 static void iso_time(char *);
319 static bool message_write(char *, const char *, size_t, struct fdbrb_buffer *,
320 rcptnode *rnode);
321 static bool do_data_file(clientenv *, struct fdbrb_buffer *,
322 struct validate_udata *);
323 static bool do_data_queue_box(clientenv *, const char *, size_t, struct
324 fdbrb_buffer *, rcptnode *, struct validate_udata *);
325 static bool do_data_queue_relay(clientenv *, const char *, size_t, struct
326 fdbrb_buffer *, rcptnode *);
327 static void do_data_queue_notify(clientenv *);
328 static int do_data_queue_notify_connect(void);
329
330 static int handleclient(unsigned long, int, clientlistnode *, struct iface *,
331 struct async_clenv *, void *);
332
333 static void thread_init(void);
334 static void *thread_mutex_create(void);
335 static void *thread_mutex_destroy(void *);
336 static void thread_mutex_lock(void *);
337 static void thread_mutex_unlock(void *);
338 static bool thread_eintr(void);
339
340 static void async_resquery(struct async_msg *);
341 static int a_res_query(clientenv *, const char *, int, int, char *, int);
342
343 static void *hosts_expire_thread(void *);
344 static bool hosts_expire_thread_iterator(hashnode_t *, void *);
345
346 static void *db_gc_thread(void *);
347
348
349
350
351 #endif