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