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