*** empty log message ***
[mmondor.git] / mmsoftware / mmmail / src / mmsmtpd / mmsmtpd.h
1 /* $Id: mmsmtpd.h,v 1.11 2003/04/27 19:48:04 mmondor Exp $ */
2
3 /*
4 * Copyright (C) 2000-2003, 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 <mmserver.h>
54 #include <mmfd.h>
55 #include <mmstat.h>
56
57
58
59
60 /* DEFINITIONS */
61 #define DAEMON_NAME "mmsmtpd"
62 #define DAEMON_VERSION "mmmail-0.0.22/mmondor"
63
64 /* Negative states are used by the state swapper, others are real states */
65 #define STATE_ERROR -3
66 #define STATE_END -2
67 #define STATE_CURRENT -1
68 #define STATE_ALL 0
69
70 /* Invalid RCPT reason */
71 #define RCPT_OK 0
72 #define RCPT_NOFROM 1
73 #define RCPT_MAX 2
74 #define RCPT_INVALID 3
75 #define RCPT_EXISTS 4
76 #define RCPT_FULL 5
77 #define RCPT_FLOOD 6
78 #define RCPT_ERROR 7
79
80 /* DATA errors */
81 #define DATA_SUBMIT 0
82 #define DATA_OK 1
83 #define DATA_OVERFLOW 2
84 #define DATA_HOPS 3
85 #define DATA_INTERNAL 4
86
87 /* Resolving flags for valid_host() */
88 #define HOST_NORES 0
89 #define HOST_RES 1
90 #define HOST_RES_MX 2
91
92 /* Custom fdbreadbuf() return result */
93 #define CFDBRB_HOPS -1
94
95 /* Asynchroneous functions we attach */
96 #define ASYNC_RESQUERY 1
97
98 /* Error registration macro */
99 #define REGISTER_ERROR(x) do { \
100 (x)->errors++; \
101 if (CONF.DELAY_ON_ERROR) \
102 pth_sleep((x)->errors); \
103 } while(FALSE)
104
105 /* Evaluates if a character is valid for addresses and hostnames */
106 #define VALID_CHAR(c) (((c) >= 'a' && (c) <= 'z') || \
107 ((c) >= '0' && (c) <= '9') || (c) == '.' || (c) == '-' || (c) == '_')
108
109
110
111 /* STRUCTURES */
112 /* We store config file read results in this structure */
113 typedef struct config {
114 char CHROOT_DIR[256], PID_PATH[256], USER[32], GROUPS[256],
115 LOG_FACILITY[32], SERVER_NAMES[1024], LISTEN_IPS[1024], DB_HOST[64],
116 DB_USER[32], DB_PASSWORD[32], DB_DATABASE[32];
117 long ALLOC_BUFFERS, LOG_LEVEL, LISTEN_PORT, MAX_ERRORS, MAX_IPS,
118 MAX_PER_IP, CONNECTION_RATE, CONNECTION_PERIOD, INPUT_TIMEOUT,
119 BANDWIDTH_IN, BANDWIDTH_OUT, GBANDWIDTH_IN, GBANDWIDTH_OUT, MAX_RCPTS,
120 MAX_DATA_LINES, MAX_DATA_SIZE, MAX_HOPS, FLOOD_MESSAGES,
121 FLOOD_EXPIRES, FLOOD_CACHE, ASYNC_PROCESSES;
122 bool RESOLVE_HOSTS, RESOLVE_HELO, RESOLVE_MX_MAIL, REQUIRE_HELO,
123 FLOOD_PROTECTION, STATFAIL_ADDRESS, STATFAIL_FLOOD, STATFAIL_FULL,
124 STATFAIL_TIMEOUT, STATFAIL_EOF, DELAY_ON_ERROR;
125 } CONFIG;
126
127
128 /* This consists of the state-shared clientenv structure */
129 typedef struct clientenv {
130 pnode_t node;
131 fdbuf *fdb; /* Buffered handler around our fd */
132 char *buffer; /* Buffer that points to last command line */
133 char *helo; /* Cached helo hostname */
134 char *from; /* Cached mail sender address */
135 char *c_hostname; /* Pointer to client's hostname */
136 char *c_ipaddr; /* Pointer to client's IP address string */
137 long mesg_size; /* Current cached message size in bytes */
138 long errors; /* Total number of errors that occured */
139 int timeout; /* Timeout in ms */
140 unsigned long id; /* Our connection ID */
141 unsigned long messages; /* Messages user sent us */
142 unsigned long rcpts; /* Number of RCPT accepted */
143 struct iface *iface; /* Current interface user connected through */
144 struct async_clenv *aclenv; /* Thread context for async_call() */
145 list_t rcpt; /* Cached recepients to send mail to */
146 mmstat_t vstat, pstat; /* mmstat(3) handles */
147 } clientenv;
148
149 /* Used for RCPT addresses */
150 typedef struct rcptnode {
151 pnode_t node;
152 char address[64], foraddress[64];
153 u_int64_t hash;
154 } rcptnode;
155
156 /* This structure is used to keep a cache of recent boxes for which mail was
157 * received, along with information on it to determine if the rate of messages
158 * is too high.
159 */
160 typedef struct mnode {
161 pnode_t node;
162 u_int64_t hash; /* 64-bit hash of host address or name */
163 time_t expires; /* Time entry will expire */
164 long posts; /* How many posts since entry creation */
165 } mnode;
166
167 /* Used for mmfd thread support delegation/abstraction */
168 struct mutexnode {
169 pnode_t node;
170 pth_mutex_t mutex;
171 };
172
173 /* This defines a state */
174 typedef struct state {
175 int (**functions)(clientenv *);
176 int errcode;
177 char *errtext;
178 } state;
179
180 /* A command of a state */
181 typedef struct command {
182 int32_t hash;
183 int loglevel;
184 char *name, *args, *desc;
185 } command;
186
187 /* Used for fast index of result messages */
188 struct reply_messages {
189 int code;
190 char *msg;
191 };
192
193 /* Our validate_msg_line() context */
194 struct validate_udata
195 {
196 long hops, nhops;
197 };
198
199 /* Our union for async_resquery() */
200 struct async_resquery_msg {
201 struct async_msg msg;
202 union {
203 struct {
204 int res;
205 char answer[128];
206 } res;
207 struct {
208 int r_class, r_type;
209 char host[128];
210 } args;
211 } un;
212 };
213
214
215
216
217 /* PROTOTYPES */
218
219 int main(int, char **);
220
221 static int all_noop(clientenv *);
222 static int all_rset(clientenv *);
223 static int all_quit(clientenv *);
224 static int all_help(clientenv *);
225 static int all_helo(clientenv *);
226 static int all_mail(clientenv *);
227 static int all_rcpt(clientenv *);
228 static int all_data(clientenv *);
229 static int all_beer(clientenv *);
230
231 static void packcommands(struct command *, size_t);
232 static bool reply(fdbuf *, int, bool, const char *, ...);
233
234 static clientenv *alloc_clientenv(void);
235 static bool init_clientenv(clientenv *, bool);
236 static clientenv *free_clientenv(clientenv *);
237 static void empty_rcpts(list_t *);
238 static bool check_alias(char *);
239 static bool check_nofrom(const char *, const char *);
240 static int best_match(const char *, const char *);
241 static bool local_address(const char *, long *, long *, long *, long *);
242 static void rfc_time(char *);
243 static bool valid_address(clientenv *, char *, char *, int);
244 static bool valid_host(clientenv *, char *, int, bool);
245 static bool valid_ipaddress(const char *);
246
247 static int validate_msg_line(char *, ssize_t *, int *, void *);
248 static bool do_data(clientenv *);
249
250 static int handleclient(unsigned long, int, clientlistnode *, struct iface *,
251 struct async_clenv *);
252
253 static void *_pth_mutex_create(void);
254 static void *_pth_mutex_destroy(void *);
255 static void _pth_mutex_lock(void *);
256 static void _pth_mutex_unlock(void *);
257 static void _pth_thread_yield(void);
258
259 static void async_resquery(struct async_msg *);
260 static int a_res_query(clientenv *, const char *, int, int, u_char *, int);
261
262
263
264
265 #endif