masqmail
view src/masqmail.h @ 317:55b7bde95d37
reworked allowed and denied addrs for routes
The following refactorings had been made:
- allowed_mail_locals + allowed_return_paths -> allowed_senders
- not_allowed_mail_locals + not_allowed_return_paths -> denied_senders
- allowed_rcpt_domains -> allowed_recipients
- not_allowed_rcpt_domains -> denied_recipients
The new options allow more consistent and more flexible matching.
author | meillo@marmaro.de |
---|---|
date | Thu, 28 Apr 2011 09:55:06 +0200 |
parents | e230bcd0f1c6 |
children | 4c705c34182f |
line source
1 /* MasqMail
2 Copyright (C) 1999-2001 Oliver Kurth
3 Copyright (C) 2010 markus schnalke <meillo@marmaro.de>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19 #include <config.h>
21 #include <stdio.h>
22 #include <stdarg.h>
23 #include <errno.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <ctype.h>
27 #include <unistd.h>
28 #include <pwd.h>
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <time.h>
33 #include <sys/time.h>
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
36 #include <netdb.h>
37 #include <syslog.h>
38 #include <signal.h>
39 #include <fcntl.h>
41 #include <glib.h>
43 #ifdef ENABLE_IDENT
44 #include "libident/ident.h"
45 #endif
47 #include "lookup.h"
49 typedef struct _interface {
50 gchar *address;
51 gint port;
52 } interface;
54 #define ADDR_FLAG_DELIVERED 0x01
55 #define ADDR_FLAG_DEFERED 0x02
56 #define ADDR_FLAG_FAILED 0x04
57 #define ADDR_FLAG_LAST_ROUTE 0x40
59 typedef struct _address {
60 gchar *address;
61 gchar *local_part;
62 gchar *domain;
63 gint flags;
64 GList *children;
65 struct _address *parent;
66 } address;
68 #define addr_mark_delivered(addr) { addr->flags |= ADDR_FLAG_DELIVERED; }
69 #define addr_unmark_delivered(addr) { addr->flags &= ~ADDR_FLAG_DELIVERED; }
70 #define addr_is_delivered(addr) ((addr->flags & ADDR_FLAG_DELIVERED) != 0 )
72 #define addr_mark_defered(addr) { addr->flags |= ADDR_FLAG_DEFERED; }
73 #define addr_unmark_defered(addr) { addr->flags &= ~ADDR_FLAG_DEFERED; }
74 #define addr_is_defered(addr) ((addr->flags & ADDR_FLAG_DEFERED) != 0 )
76 #define addr_mark_failed(addr) { addr->flags |= ADDR_FLAG_FAILED; }
77 #define addr_unmark_failed(addr) { addr->flags &= ~ADDR_FLAG_FAILED; }
78 #define addr_is_failed(addr) ((addr->flags & ADDR_FLAG_FAILED) != 0 )
80 typedef struct _connect_route {
81 gchar *name;
82 gchar *filename;
84 gboolean is_local_net;
85 gboolean last_route;
87 GList *allowed_senders;
88 GList *denied_senders;
89 GList *allowed_recipients;
90 GList *denied_recipients;
92 interface *mail_host;
93 gchar *wrapper;
94 gboolean connect_error_fail;
96 gchar *helo_name;
97 gboolean do_correct_helo;
98 gboolean instant_helo;
99 gboolean do_pipelining;
101 gchar *set_h_from_domain;
102 gchar *set_h_reply_to_domain;
103 gchar *set_return_path_domain;
105 GList *map_h_from_addresses;
106 GList *map_h_reply_to_addresses;
107 GList *map_h_mail_followup_to_addresses;
108 GList *map_return_path_addresses;
110 gboolean expand_h_sender_domain;
111 gboolean expand_h_sender_address;
113 GList *resolve_list;
115 gchar *auth_name;
116 gchar *auth_login;
117 gchar *auth_secret;
119 gchar *pipe;
121 gboolean pipe_fromline;
122 gboolean pipe_fromhack;
123 } connect_route;
125 typedef struct _masqmail_conf {
126 gint mail_uid;
127 gint mail_gid;
129 gint orig_uid;
130 gint orig_gid;
132 gboolean run_as_user;
134 gchar *mail_dir;
135 gchar *lock_dir;
136 gchar *spool_dir;
137 gchar *log_dir;
139 gint debug_level;
140 gboolean use_syslog;
141 guint log_max_pri;
143 gchar *host_name;
144 GList *local_hosts;
145 GList *local_addresses;
146 GList *not_local_addresses;
147 GList *local_nets;
148 GList *listen_addresses;
150 /* ANSI C defines unsigned long to be at least 32bit
151 i.e. ca. 4GB max; that should be enough. */
152 gulong max_msg_size;
154 gboolean do_save_envelope_to;
156 gboolean defer_all;
157 gboolean do_relay;
159 GList *ident_trusted_nets;
161 gboolean do_queue;
163 gboolean do_verbose;
165 gchar *mbox_default;
166 GList *mbox_users;
167 GList *mda_users;
169 gchar *mda;
170 gboolean mda_fromline;
171 gboolean mda_fromhack;
173 gboolean pipe_fromline;
174 gboolean pipe_fromhack;
176 gchar *alias_file;
177 int (*localpartcmp) (const char *, const char *);
179 GList *local_net_routes;
180 GList *connect_routes; /* list of pairs which point to lists */
182 gchar *online_query;
184 gchar *errmsg_file;
185 gchar *warnmsg_file;
186 GList *warn_intervals;
187 gint max_defer_time;
189 gchar *log_user;
190 } masqmail_conf;
192 extern masqmail_conf conf;
194 typedef struct _table_pair {
195 gchar *key;
196 gpointer *value;
197 } table_pair;
200 /* must match the contents of prot_names[] in accept.c */
201 typedef enum _prot_id {
202 PROT_LOCAL = 0,
203 PROT_SMTP,
204 PROT_ESMTP,
205 PROT_NUM
206 } prot_id;
208 extern gchar *prot_names[];
210 typedef enum _header_id {
211 HEAD_FROM = 0,
212 HEAD_SENDER,
213 HEAD_TO,
214 HEAD_CC,
215 HEAD_BCC,
216 HEAD_DATE,
217 HEAD_MESSAGE_ID,
218 HEAD_REPLY_TO,
219 HEAD_SUBJECT,
220 HEAD_RETURN_PATH,
221 HEAD_ENVELOPE_TO,
222 HEAD_RECEIVED,
223 HEAD_NUM_IDS,
224 HEAD_STATUS,
225 HEAD_UNKNOWN = HEAD_NUM_IDS,
226 HEAD_NONE = -1,
227 } header_id;
229 typedef struct _header_name {
230 gchar *header;
231 header_id id;
232 } header_name;
234 typedef struct _header {
235 header_id id;
236 gchar *header;
237 gchar *value;
238 } header;
241 typedef struct _message {
242 gchar *uid;
244 gchar *received_host;
245 prot_id received_prot;
246 gchar *ident;
247 gint transfer_id; /* for multiple messages per transfer */
249 address *return_path;
250 GList *rcpt_list;
251 GList *non_rcpt_list;
253 GList *hdr_list;
254 GList *data_list;
256 gint data_size;
257 time_t received_time;
258 time_t warned_time;
260 gchar *full_sender_name;
261 } message;
263 typedef struct _msg_out {
264 message *msg;
266 address *return_path;
267 GList *rcpt_list;
269 GList *hdr_list;
270 GList *xtra_hdr_list;
271 } msg_out;
273 typedef struct _msgout_perhost {
274 gchar *host;
275 GList *msgout_list;
276 } msgout_perhost;
278 /* flags for accept() */
279 #define ACC_RCPT_FROM_HEAD 0x08 /* -t option, get rcpts from headers */
280 #define ACC_DOT_IGNORE 0x10 /* a dot on a line itself does not end the message (-oi option) */
281 #define ACC_MAIL_FROM_HEAD 0x40 /* get return path from header */
282 #define ACC_NODOT_RELAX 0x80 /* do not be picky if message ist not terminated by a dot on a line */
283 #define ACC_SAVE_ENVELOPE_TO 0x0100 /* save an existent Envelope-to header as X-Orig-Envelope-to */
285 #define DLVR_LOCAL 0x01
286 #define DLVR_LAN 0x02
287 #define DLVR_ONLINE 0x04
288 #define DLVR_ALL (DLVR_LOCAL|DLVR_LAN|DLVR_ONLINE)
290 /* transport flags */
291 #define MSGSTR_FROMLINE 0x01
292 #define MSGSTR_FROMHACK 0x02
294 typedef enum _accept_error {
295 AERR_OK = 0,
296 AERR_TIMEOUT,
297 AERR_EOF,
298 AERR_OVERFLOW,
299 AERR_SYNTAX,
300 AERR_NOSPOOL,
301 AERR_NORCPT,
302 AERR_SIZE, /* max msg size exeeded (SMTP SIZE) */
303 AERR_UNKNOWN
304 } accept_error;
306 #define BUF_LEN 1024
307 #define MAX_ADDRESS 256
308 #define MAX_DATALINE 4096
310 typedef enum _smtp_cmd_id {
311 SMTP_HELO = 0,
312 SMTP_EHLO,
313 SMTP_MAIL_FROM,
314 SMTP_RCPT_TO,
315 SMTP_DATA,
316 SMTP_QUIT,
317 SMTP_RSET,
318 SMTP_NOOP,
319 SMTP_HELP,
320 SMTP_NUM_IDS,
321 SMTP_EOF = -1,
322 SMTP_ERROR = -2,
323 } smtp_cmd_id;
325 typedef struct _smtp_cmd {
326 smtp_cmd_id id;
327 gchar *cmd;
328 } smtp_cmd;
330 typedef struct _smtp_connection {
331 gchar *remote_host;
333 prot_id prot;
334 gint next_id;
336 gboolean helo_seen;
337 gboolean from_seen;
338 gboolean rcpt_seen;
340 message *msg;
341 } smtp_connection;
343 /* alias.c*/
344 gboolean addr_is_local(address * addr);
345 GList *alias_expand(GList * alias_table, GList * rcpt_list, GList * non_rcpt_list);
347 /* child.c */
348 int child(const char *command);
350 /* conf.c */
351 void init_conf();
352 gboolean read_conf(gchar * filename);
353 connect_route *read_route(gchar * filename, gboolean is_local_net);
354 GList *read_route_list(GList * rf_list, gboolean is_local_net);
355 void destroy_route(connect_route * r);
356 void destroy_route_list(GList * list);
357 connect_route *create_local_route();
359 /* expand.c */
360 GList *var_table_rcpt(GList * var_table, address * rcpt);
361 GList *var_table_msg(GList * var_table, message * msg);
362 GList *var_table_conf(GList * var_table);
363 gint expand(GList * var_list, gchar * format, gchar * result, gint result_len);
365 /* message.c */
366 message *create_message(void);
367 void destroy_message(message * msg);
368 void destroy_msg_list(GList * msg_list);
369 void msg_free_data(message * msg);
370 gint msg_calc_size(message * msg, gboolean is_smtp);
372 msg_out *create_msg_out(message * msg);
373 msg_out *clone_msg_out(msg_out * msgout_orig);
374 GList *create_msg_out_list(GList * msg_list);
375 void destroy_msg_out(msg_out * msgout);
376 void destroy_msg_out_list(GList * msgout_list);
378 /* address.c */
379 address *create_address(gchar * path, gboolean is_rfc821);
380 address *create_address_qualified(gchar * path, gboolean is_rfc821, gchar * domain);
381 address *create_address_pipe(gchar * path);
382 void destroy_address(address * addr);
383 address *copy_modify_address(const address * orig, gchar * l_part, gchar * dom);
384 #define copy_address(addr) copy_modify_address(addr, NULL, NULL)
385 gboolean addr_isequal(address * addr1, address * addr2, int (*cmpfunc) (const char*, const char*));
386 gboolean addr_isequal_parent(address * addr1, address * addr2, int (*cmpfunc) (const char*, const char*));
387 address *addr_find_ancestor(address * addr);
388 gboolean addr_is_delivered_children(address * addr);
389 gboolean addr_is_finished_children(address * addr);
390 gchar *addr_string(address * addr);
392 /* accept.c */
393 accept_error accept_message(FILE * in, message * msg, guint flags);
394 accept_error accept_message_prepare(message * msg, guint flags);
396 /* header.c */
397 gchar *rec_timestamp();
398 GList *find_header(GList * hdr_list, header_id id, gchar * hdr_str);
399 void header_unfold(header * hdr);
400 void header_fold(header * hdr, unsigned int maxlen);
401 header *create_header(header_id id, gchar * fmt, ...);
402 void destroy_header(header * hdr);
403 header *copy_header(header * hdr);
404 header *get_header(gchar * line);
406 /* smtp_in.c */
407 void smtp_in(FILE * in, FILE * out, gchar * remote_host, gchar * ident);
409 /* listen.c */
410 void listen_port(GList * addr_list, gint qival, char *argv[]);
412 /* parse.c */
413 gboolean split_address(const gchar * path, gchar ** local_part, gchar ** domain, gboolean is_rfc821);
414 gboolean parse_address_rfc822(gchar * string, gchar ** local_begin, gchar ** local_end, gchar ** domain_begin, gchar ** domain_end, gchar ** address_end);
415 gboolean parse_address_rfc821(gchar * string, gchar ** local_begin, gchar ** local_end, gchar ** domain_begin, gchar ** domain_end, gchar ** address_end);
416 address *_create_address(gchar * string, gchar ** end, gboolean is_rfc821);
417 address *create_address_rfc821(gchar * string, gchar ** end);
418 address *create_address_rfc822(gchar * string, gchar ** end);
419 GList *addr_list_append_rfc822(GList * addr_list, gchar * string, gchar * domain);
421 /* connect.c */
422 mxip_addr *connect_hostlist(int *psockfd, gchar * host, guint port, GList * addr_list);
423 mxip_addr *connect_resolvelist(int *psockfd, gchar * host, guint port, GList * res_funcs);
425 /* deliver.c */
426 void msg_rcptlist_local(GList * rcpt_list, GList **, GList **);
427 gboolean deliver_local(msg_out * msgout);
428 gboolean deliver_msglist_host(connect_route * route, GList * msg_list, gchar * host, GList * res_list);
429 gboolean deliver_route_msgout_list(connect_route * route, GList * msgout_list);
430 gboolean deliver_route_msg_list(connect_route * route, GList * msgout_list);
431 gboolean deliver_finish(msg_out * msgout);
432 gboolean deliver_finish_list(GList * msgout_list);
433 gboolean deliver_msg_list(GList * msg_list, guint flags);
434 gboolean deliver(message * msg);
436 /* fail_msg.c */
437 gboolean fail_msg(message * msg, gchar * template, GList * failed_rcpts, gchar * err_fmt, va_list args);
438 gboolean warn_msg(message * msg, gchar * template, GList * failed_rcpts, gchar * err_fmt, va_list args);
440 /* interface.c */
441 gboolean init_sockaddr(struct sockaddr_in *name, interface * iface);
442 int make_server_socket(interface * iface);
444 /* local.c */
445 gboolean append_file(message * msg, GList * hdr_list, gchar * user);
446 gboolean pipe_out(message * msg, GList * hdr_list, address * rcpt, gchar * cmd, guint flags);
448 /* log.c */
449 gchar *ext_strerror(int err);
450 gboolean logopen(void);
451 void logclose(void);
452 void vlogwrite(int pri, const char *fmt, va_list args);
453 void logwrite(int pri, const char *fmt, ...);
454 void debugf(const char *fmt, ...);
455 void vdebugf(const char *fmt, va_list args);
456 void maillog(const char *fmt, ...);
458 /* spool.c */
459 gboolean spool_read_data(message * msg);
460 message *msg_spool_read(gchar * uid, gboolean do_readdata);
461 gboolean spool_write(message * msg, gboolean do_writedata);
462 gboolean spool_lock(gchar * uid);
463 gboolean spool_unlock(gchar * uid);
464 gboolean spool_delete_all(message * msg);
466 /* queue.c */
467 GList *read_queue(gboolean do_readdata);
468 gboolean queue_run(void);
469 gboolean queue_run_online(void);
470 void queue_list(void);
471 gboolean queue_delete(gchar * uid);
473 /* online.c */
474 gchar *online_query();
476 /* permissions.c */
477 gboolean is_ingroup(uid_t uid, gid_t gid);
478 void set_euidgid(gint uid, gint gid, uid_t * old_uid, gid_t * old_gid);
479 void set_identity(uid_t old_uid, gchar * task_name);
481 /* rewrite.c */
482 gboolean set_address_header_domain(header * hdr, gchar * domain);
483 gboolean map_address_header(header * hdr, GList * table);
485 /* route.c */
486 msgout_perhost *create_msgout_perhost(gchar * host);
487 void destroy_msgout_perhost(msgout_perhost * mo_ph);
488 void rewrite_headers(msg_out * msgout, connect_route * route);
489 void split_rcpts(GList* rcpt_list, GList* localnets, GList** rl_local, GList** rl_localnet, GList** rl_others);
490 gboolean route_strip_msgout(connect_route * route, msg_out * msgout);
491 msg_out *route_prepare_msgout(connect_route * route, msg_out * msgout);
492 GList *route_msgout_list(connect_route * route, GList * msgout_list);
493 gboolean route_sender_is_allowed(connect_route * route, address * ret_path);
494 void route_split_rcpts(connect_route * route, GList * rcpt_list, GList ** p_rcpt_list, GList ** p_non_rcpt_list);
496 /* tables.c */
497 table_pair *create_pair(gchar * key, gpointer value);
498 table_pair *create_pair_string(gchar * key, gpointer value);
499 table_pair *parse_table_pair(gchar * line, char delim);
500 gpointer *table_find_func(GList * table_list, gchar * key, int (*cmp_func) (const char *, const char *));
501 gpointer *table_find(GList * table_list, gchar * key);
502 gpointer *table_find_case(GList * table_list, gchar * key);
503 gpointer *table_find_fnmatch(GList * table_list, gchar * key);
504 GList *table_read(gchar * fname, gchar delim);
505 void destroy_table(GList * table);
507 /* timeival.c */
508 gint time_interval(gchar * str);
510 /* permissions.c */
511 gboolean is_privileged_user(uid_t uid);
513 /* other things */
515 #define foreach(list, node)\
516 for((node) = g_list_first(list);\
517 (node);\
518 (node) = g_list_next(node))
520 #ifdef ENABLE_DEBUG
521 #define DEBUG(level) if(level <= conf.debug_level)
522 #else
523 /* hopefully the compiler optmizes this away... */
524 #define DEBUG(level) if(0)
525 #endif
527 #define LOG_VERBOSE 0x100
529 #ifndef HAVE_GETLINE
530 #define getline(buf, size, file) getdelim(buf, size, '\n', file)
531 #endif
533 #ifndef HAVE_FDATASYNC
534 #define fdatasync(fd) fsync(fd)
535 #endif
537 #ifndef CONF_DIR
538 #define CONF_DIR "/etc/masqmail"
539 #endif
541 #define CONF_FILE CONF_DIR"/masqmail.conf"
543 #define PIDFILEDIR "/var/run/masqmail/"
545 #ifndef va_copy
546 #ifdef __va_copy
547 #define va_copy(ap1, ap2) __va_copy(ap1, ap2)
548 #else
549 #define va_copy(ap1, ap2) G_VA_COPY(ap1, ap2)
550 #endif
551 #endif
553 /* *BSD needs this: */
554 extern char **environ;