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