Added possibility to refuse messages with no Received: hop
[mmondor.git] / mmsoftware / mmmail / src / mmsmtpd / mmsmtpd.h
CommitLineData
cc337670 1/* $Id: mmsmtpd.h,v 1.50 2009/02/04 09:02:25 mmondor Exp $ */
47071c2b
MM
2
3/*
667273fa 4 * Copyright (C) 2001-2008, Matthew Mondor
47071c2b
MM
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.
667273fa
MM
21 * 5. Redistribution of source code may not be released under the terms of
22 * any GNU Public License derivate.
47071c2b
MM
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>
b232bd02
MM
48#include <stdbool.h>
49#include <stdint.h>
47071c2b
MM
50#include <time.h>
51
3dd832e3
MM
52#include <pthread.h>
53#include <mm_pthread_sleep.h>
5eb34fba
MM
54
55#include <mmtypes.h>
47071c2b 56#include <mmlist.h>
7a56f31f 57#include <mmpool.h>
904cd663 58#include <mmhash.h>
47071c2b
MM
59#include <mmserver.h>
60#include <mmfd.h>
61#include <mmstat.h>
da634739 62#include <mmlimitrate.h>
47071c2b 63
46173fd8
MM
64#include <libpq-fe.h>
65
47071c2b
MM
66
67
68
69/* DEFINITIONS */
70#define DAEMON_NAME "mmsmtpd"
1099fea4 71#define DAEMON_VERSION "mmmail-0.3.2/mmondor"
47071c2b
MM
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 */
f36e2a66
MM
80enum rcpt_reason {
81 RCPT_OK = 0,
82 RCPT_NOFROM,
193955a0 83 RCPT_MANY,
f36e2a66 84 RCPT_INVALID,
193955a0
MM
85 RCPT_UNKNOWN,
86 RCPT_RELAY,
f36e2a66
MM
87 RCPT_EXISTS,
88 RCPT_FULL,
89 RCPT_FLOOD,
edc0a306 90 RCPT_FILTER,
193955a0
MM
91 RCPT_ERROR,
92 RCPT_MAX
f36e2a66 93};
47071c2b
MM
94
95/* DATA errors */
f36e2a66
MM
96enum data_reason {
97 DATA_SUBMIT = 0,
98 DATA_OK,
99 DATA_OVERFLOW,
0aea6503 100 DATA_TIMEOUT,
f36e2a66 101 DATA_HOPS,
0aea6503 102 DATA_HEADER,
193955a0
MM
103 DATA_INTERNAL,
104 DATA_MAX
f36e2a66 105};
47071c2b 106
46cf2cd5
MM
107/* Resolving flags for valid_host() */
108#define HOST_NORES 0
109#define HOST_RES 1
110#define HOST_RES_MX 2
111
47071c2b
MM
112/* Custom fdbreadbuf() return result */
113#define CFDBRB_HOPS -1
0aea6503 114#define CFDBRB_HEADER -2
cc337670 115#define CFDBRB_NOHOP -3
47071c2b
MM
116
117/* Asynchroneous functions we attach */
118#define ASYNC_RESQUERY 1
119
e334174e 120/* Error registration macro */
46173fd8
MM
121#define REGISTER_ERROR(x) do { \
122 (x)->errors++; \
123 if (CONF.DELAY_ON_ERROR) \
124 pthread_sleep((x)->errors); \
125} while (/* CONSTCOND */0)
e334174e 126
c363ee92 127/* Evaluates if a character is valid for addresses and hostnames */
82396359
MM
128#define VALID_ADDR_CHAR(c) \
129 (valid_addr_char_table[(int)((unsigned char)(c))] != 0)
a4da92fc
MM
130#define VALID_HOST_CHAR(c) \
131 (valid_addr_host_table[(int)((unsigned char)(c))] != 0)
47071c2b
MM
132
133
134
135/* STRUCTURES */
82396359 136
47071c2b
MM
137/* We store config file read results in this structure */
138typedef struct config {
7fd2c069 139 char LOCK_PATH[256], CHROOT_DIR[256], PID_PATH[256], USER[32], GROUPS[256],
46173fd8
MM
140 LOG_FACILITY[32], SERVER_NAMES[1024], LISTEN_IPS[1024], DB_INFO[1024],
141 MAIL_DIR[256], MMRELAYD_SOCKET_PATH[256];
47071c2b
MM
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;
f2c550b1 147 bool RESOLVE_HOSTS, RESOLVE_HELO, RESOLVE_MX_MAIL, RESOLVE_MX_RCPT,
cc337670
MM
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;
47071c2b
MM
152} CONFIG;
153
154
155/* This consists of the state-shared clientenv structure */
156typedef struct clientenv {
7a56f31f 157 pnode_t node;
47071c2b
MM
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 */
3dd832e3 167 bool nofrom; /* If empty MAIL FROM from allowed server */
47071c2b
MM
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 */
7a56f31f 172 list_t rcpt; /* Cached recepients to send mail to */
46173fd8
MM
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 */
47071c2b
MM
176} clientenv;
177
178/* Used for RCPT addresses */
179typedef struct rcptnode {
7a56f31f 180 pnode_t node;
47071c2b 181 char address[64], foraddress[64];
b232bd02 182 uint64_t hash;
193955a0 183 bool relay; /* Non-local */
47071c2b
MM
184} rcptnode;
185
904cd663 186/* This structure is used to keep a cache of recent hosts from which mail was
47071c2b
MM
187 * received, along with information on it to determine if the rate of messages
188 * is too high.
189 */
399db776 190typedef struct hostnode {
904cd663 191 hashnode_t node;
da634739
MM
192 char host[128]; /* Hostname, key */
193 struct limitrate lr;
399db776 194} hostnode;
47071c2b 195
399db776 196struct hosts_expire_thread_iterator_udata {
904cd663 197 time_t current, soonest;
904cd663
MM
198};
199
5eb34fba
MM
200/* Used for mmfd thread support delegation/abstraction */
201struct mutexnode {
7a56f31f 202 pnode_t node;
3dd832e3 203 pthread_mutex_t mutex;
5eb34fba
MM
204};
205
47071c2b
MM
206/* This defines a state */
207typedef struct state {
208 int (**functions)(clientenv *);
209 int errcode;
210 char *errtext;
211} state;
212
213/* A command of a state */
214typedef struct command {
47071c2b
MM
215 int loglevel;
216 char *name, *args, *desc;
217} command;
218
edc0a306
MM
219/* Information for a mailbox */
220struct box_info {
0aea6503
MM
221 long max_size, size, max_msgs, msgs;
222 bool filter, filter_type;
edc0a306
MM
223};
224
399db776
MM
225/* For fast command lookup */
226struct commandnode {
227 hashnode_t node;
b232bd02 228 uint32_t hash;
399db776
MM
229 struct command *command;
230 int index;
231};
232
47071c2b
MM
233/* Used for fast index of result messages */
234struct reply_messages {
235 int code;
236 char *msg;
237};
238
239/* Our validate_msg_line() context */
240struct validate_udata
241{
1bb24f38
MM
242 /* Used to count number of Received: lines */
243 long hops;
244 /* Headers we consider mandatory, which we'll add if necessary */
d6511325 245 bool msgid, date, from, to, subject, inreply;
1bb24f38
MM
246 /* Still considering to read the header */
247 bool header;
248 /* Internal linking */
249 clientenv *clenv;
d6511325 250 char *h_from, *h_to, *h_subject, *h_id, *h_reply;
47071c2b
MM
251};
252
253/* Our union for async_resquery() */
254struct async_resquery_msg {
255 struct async_msg msg;
256 union {
257 struct {
258 int res;
259 char answer[128];
260 } res;
261 struct {
262 int r_class, r_type;
263 char host[128];
264 } args;
265 } un;
266};
267
268
269
270
271/* PROTOTYPES */
272
273int main(int, char **);
274
275static int all_noop(clientenv *);
276static int all_rset(clientenv *);
277static int all_quit(clientenv *);
278static int all_help(clientenv *);
279static int all_helo(clientenv *);
280static int all_mail(clientenv *);
281static int all_rcpt(clientenv *);
282static int all_data(clientenv *);
283static int all_beer(clientenv *);
284
399db776 285static bool hash_commands(struct command *, size_t);
b232bd02 286static uint32_t commandnode_keyhash(const void *, size_t);
399db776 287static int commandnode_keycmp(const void *, const void *, size_t);
47071c2b
MM
288static bool reply(fdbuf *, int, bool, const char *, ...);
289
46173fd8
MM
290static bool clientenv_constructor(pnode_t *);
291static void clientenv_destructor(pnode_t *);
52122f72
MM
292static void *utdata_constructor(void);
293static void utdata_destructor(void *);
47071c2b
MM
294static clientenv *alloc_clientenv(void);
295static bool init_clientenv(clientenv *, bool);
296static clientenv *free_clientenv(clientenv *);
7a56f31f 297static void empty_rcpts(list_t *);
46173fd8
MM
298static bool check_alias(clientenv *, char *);
299static bool check_nofrom(clientenv *);
7fd2c069 300static int lock_check(const char *);
c023a59c 301static int best_match(const char *, const char *);
46173fd8
MM
302static bool local_address(clientenv *, struct box_info *, const char *);
303static bool box_filter_allow(clientenv *, const char *, const char *, bool);
47071c2b 304static void rfc_time(char *);
a4da92fc 305static bool valid_address(clientenv *, char *, size_t, char *, int);
f2c550b1 306static bool valid_host(clientenv *, char *, int, bool, bool);
0d424a56 307static bool valid_ipaddress(clientenv *, const char *);
47071c2b
MM
308
309static int validate_msg_line(char *, ssize_t *, int *, void *);
310static bool do_data(clientenv *);
193955a0 311inline static size_t do_data_received(char *, size_t, clientenv *, rcptnode *,
5f8db290 312 const char *);
5f8db290 313static void do_data_stats(clientenv *, rcptnode *, size_t);
193955a0
MM
314static bool address_relay_allow(clientenv *, int *, const char *);
315static void iso_time(char *);
316static bool message_write(char *, const char *, size_t, struct fdbrb_buffer *,
317 const char *);
1ed0e44f
MM
318static bool do_data_file(clientenv *, struct fdbrb_buffer *,
319 struct validate_udata *);
193955a0 320static bool do_data_queue_box(clientenv *, const char *, size_t, struct
1ed0e44f 321 fdbrb_buffer *, rcptnode *, struct validate_udata *);
193955a0
MM
322static bool do_data_queue_relay(clientenv *, const char *, size_t, struct
323 fdbrb_buffer *, rcptnode *);
20e5cbf1 324static void do_data_queue_notify(clientenv *);
d6eecfe4 325static int do_data_queue_notify_connect(void);
47071c2b
MM
326
327static int handleclient(unsigned long, int, clientlistnode *, struct iface *,
52122f72 328 struct async_clenv *, void *);
47071c2b 329
3dd832e3
MM
330static void thread_init(void);
331static void *thread_mutex_create(void);
332static void *thread_mutex_destroy(void *);
333static void thread_mutex_lock(void *);
334static void thread_mutex_unlock(void *);
335static bool thread_eintr(void);
5eb34fba 336
47071c2b 337static void async_resquery(struct async_msg *);
b232bd02 338static int a_res_query(clientenv *, const char *, int, int, char *, int);
47071c2b 339
399db776
MM
340static void *hosts_expire_thread(void *);
341static bool hosts_expire_thread_iterator(hashnode_t *, void *);
904cd663 342
978cad00 343static void *db_gc_thread(void *);
978cad00 344
47071c2b
MM
345
346
347
348#endif