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