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