meillo@0: /* MasqMail meillo@0: Copyright (C) 1999-2001 Oliver Kurth meillo@224: Copyright (C) 2010 markus schnalke meillo@0: meillo@0: This program is free software; you can redistribute it and/or modify meillo@0: it under the terms of the GNU General Public License as published by meillo@0: the Free Software Foundation; either version 2 of the License, or meillo@0: (at your option) any later version. meillo@0: meillo@0: This program is distributed in the hope that it will be useful, meillo@0: but WITHOUT ANY WARRANTY; without even the implied warranty of meillo@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the meillo@0: GNU General Public License for more details. meillo@0: meillo@0: You should have received a copy of the GNU General Public License meillo@0: along with this program; if not, write to the Free Software meillo@0: Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. meillo@0: */ meillo@0: #include meillo@0: meillo@0: #include meillo@0: #include meillo@0: #include meillo@0: #include meillo@0: #include meillo@0: #include meillo@0: #include meillo@0: #include meillo@0: #include meillo@0: #include meillo@0: #include meillo@0: #include meillo@0: #include meillo@0: #include meillo@0: #include meillo@0: #include meillo@0: #include meillo@0: #include meillo@0: #include meillo@0: meillo@0: #include meillo@0: meillo@0: #ifdef ENABLE_IDENT meillo@0: #include "libident/ident.h" meillo@0: #endif meillo@0: meillo@0: #include "lookup.h" meillo@0: meillo@10: typedef struct _interface { meillo@10: gchar *address; meillo@10: gint port; meillo@0: } interface; meillo@0: meillo@0: #define ADDR_FLAG_DELIVERED 0x01 meillo@0: #define ADDR_FLAG_DEFERED 0x02 meillo@0: #define ADDR_FLAG_FAILED 0x04 meillo@0: #define ADDR_FLAG_LAST_ROUTE 0x40 meillo@0: meillo@10: typedef struct _address { meillo@10: gchar *address; meillo@10: gchar *local_part; meillo@10: gchar *domain; meillo@10: gint flags; meillo@10: GList *children; meillo@10: struct _address *parent; meillo@0: } address; meillo@0: meillo@0: #define addr_mark_delivered(addr) { addr->flags |= ADDR_FLAG_DELIVERED; } meillo@0: #define addr_unmark_delivered(addr) { addr->flags &= ~ADDR_FLAG_DELIVERED; } meillo@0: #define addr_is_delivered(addr) ((addr->flags & ADDR_FLAG_DELIVERED) != 0 ) meillo@0: meillo@0: #define addr_mark_defered(addr) { addr->flags |= ADDR_FLAG_DEFERED; } meillo@0: #define addr_unmark_defered(addr) { addr->flags &= ~ADDR_FLAG_DEFERED; } meillo@0: #define addr_is_defered(addr) ((addr->flags & ADDR_FLAG_DEFERED) != 0 ) meillo@0: meillo@0: #define addr_mark_failed(addr) { addr->flags |= ADDR_FLAG_FAILED; } meillo@0: #define addr_unmark_failed(addr) { addr->flags &= ~ADDR_FLAG_FAILED; } meillo@0: #define addr_is_failed(addr) ((addr->flags & ADDR_FLAG_FAILED) != 0 ) meillo@0: meillo@10: typedef struct _connect_route { meillo@10: gchar *name; meillo@10: gchar *filename; meillo@0: meillo@10: gboolean is_local_net; meillo@10: gboolean last_route; meillo@0: meillo@317: GList *allowed_senders; meillo@317: GList *denied_senders; meillo@317: GList *allowed_recipients; meillo@317: GList *denied_recipients; meillo@0: meillo@10: interface *mail_host; meillo@10: gboolean connect_error_fail; meillo@320: GList *resolve_list; meillo@10: gchar *helo_name; meillo@10: gboolean do_correct_helo; meillo@222: gboolean instant_helo; meillo@10: gboolean do_pipelining; meillo@320: gchar *auth_name; meillo@320: gchar *auth_login; meillo@320: gchar *auth_secret; meillo@320: gchar *wrapper; meillo@0: meillo@10: gchar *set_h_from_domain; meillo@10: gchar *set_h_reply_to_domain; meillo@10: gchar *set_return_path_domain; meillo@10: GList *map_h_from_addresses; meillo@10: GList *map_h_reply_to_addresses; meillo@10: GList *map_h_mail_followup_to_addresses; meillo@10: GList *map_return_path_addresses; meillo@10: gboolean expand_h_sender_domain; meillo@10: gboolean expand_h_sender_address; meillo@0: meillo@10: gchar *pipe; meillo@10: gboolean pipe_fromline; meillo@10: gboolean pipe_fromhack; meillo@0: } connect_route; meillo@0: meillo@10: typedef struct _masqmail_conf { meillo@10: gint mail_uid; meillo@10: gint mail_gid; meillo@0: meillo@10: gint orig_uid; meillo@10: gint orig_gid; meillo@0: meillo@10: gboolean run_as_user; meillo@0: meillo@10: gchar *mail_dir; meillo@10: gchar *lock_dir; meillo@10: gchar *spool_dir; meillo@10: gchar *log_dir; meillo@0: meillo@10: gint debug_level; meillo@10: gboolean use_syslog; meillo@10: guint log_max_pri; meillo@0: meillo@10: gchar *host_name; meillo@10: GList *local_hosts; meillo@10: GList *local_addresses; meillo@10: GList *not_local_addresses; meillo@10: GList *local_nets; meillo@10: GList *listen_addresses; meillo@0: meillo@117: /* ANSI C defines unsigned long to be at least 32bit meillo@117: i.e. ca. 4GB max; that should be enough. */ meillo@117: gulong max_msg_size; meillo@117: meillo@10: gboolean do_save_envelope_to; meillo@0: meillo@10: gboolean defer_all; meillo@10: gboolean do_relay; meillo@0: meillo@10: GList *ident_trusted_nets; meillo@0: meillo@10: gboolean do_queue; meillo@0: meillo@10: gboolean do_verbose; meillo@0: meillo@10: gchar *mbox_default; meillo@10: GList *mbox_users; meillo@10: GList *mda_users; meillo@0: meillo@10: gchar *mda; meillo@10: gboolean mda_fromline; meillo@10: gboolean mda_fromhack; meillo@0: meillo@10: gboolean pipe_fromline; meillo@10: gboolean pipe_fromhack; meillo@0: meillo@10: gchar *alias_file; meillo@244: int (*localpartcmp) (const char *, const char *); meillo@0: meillo@10: GList *local_net_routes; meillo@10: GList *connect_routes; /* list of pairs which point to lists */ meillo@0: meillo@310: gchar *online_query; meillo@0: meillo@10: gchar *errmsg_file; meillo@10: gchar *warnmsg_file; meillo@10: GList *warn_intervals; meillo@10: gint max_defer_time; meillo@0: meillo@10: gchar *log_user; meillo@0: } masqmail_conf; meillo@0: meillo@0: extern masqmail_conf conf; meillo@0: meillo@10: typedef struct _table_pair { meillo@10: gchar *key; meillo@10: gpointer *value; meillo@0: } table_pair; meillo@0: meillo@0: meillo@299: /* must match the contents of prot_names[] in accept.c */ meillo@10: typedef enum _prot_id { meillo@10: PROT_LOCAL = 0, meillo@10: PROT_SMTP, meillo@10: PROT_ESMTP, meillo@10: PROT_NUM meillo@10: } prot_id; meillo@0: meillo@0: extern gchar *prot_names[]; meillo@0: meillo@10: typedef enum _header_id { meillo@10: HEAD_FROM = 0, meillo@10: HEAD_SENDER, meillo@10: HEAD_TO, meillo@10: HEAD_CC, meillo@10: HEAD_BCC, meillo@10: HEAD_DATE, meillo@10: HEAD_MESSAGE_ID, meillo@10: HEAD_REPLY_TO, meillo@10: HEAD_SUBJECT, meillo@10: HEAD_RETURN_PATH, meillo@10: HEAD_ENVELOPE_TO, meillo@10: HEAD_RECEIVED, meillo@10: HEAD_NUM_IDS, meillo@10: HEAD_STATUS, meillo@10: HEAD_UNKNOWN = HEAD_NUM_IDS, meillo@10: HEAD_NONE = -1, meillo@10: } header_id; meillo@0: meillo@10: typedef struct _header_name { meillo@10: gchar *header; meillo@10: header_id id; meillo@10: } header_name; meillo@0: meillo@10: typedef struct _header { meillo@10: header_id id; meillo@10: gchar *header; meillo@10: gchar *value; meillo@10: } header; meillo@0: meillo@0: meillo@10: typedef struct _message { meillo@10: gchar *uid; meillo@0: meillo@10: gchar *received_host; meillo@10: prot_id received_prot; meillo@10: gchar *ident; meillo@10: gint transfer_id; /* for multiple messages per transfer */ meillo@0: meillo@10: address *return_path; meillo@10: GList *rcpt_list; meillo@10: GList *non_rcpt_list; meillo@0: meillo@10: GList *hdr_list; meillo@10: GList *data_list; meillo@0: meillo@10: gint data_size; meillo@10: time_t received_time; meillo@10: time_t warned_time; meillo@0: meillo@10: gchar *full_sender_name; meillo@10: } message; meillo@0: meillo@10: typedef struct _msg_out { meillo@10: message *msg; meillo@0: meillo@10: address *return_path; meillo@10: GList *rcpt_list; meillo@0: meillo@10: GList *hdr_list; meillo@10: GList *xtra_hdr_list; meillo@10: } msg_out; meillo@10: meillo@10: typedef struct _msgout_perhost { meillo@10: gchar *host; meillo@10: GList *msgout_list; meillo@0: } msgout_perhost; meillo@0: meillo@0: /* flags for accept() */ meillo@10: #define ACC_RCPT_FROM_HEAD 0x08 /* -t option, get rcpts from headers */ meillo@110: #define ACC_DOT_IGNORE 0x10 /* a dot on a line itself does not end the message (-oi option) */ meillo@10: #define ACC_MAIL_FROM_HEAD 0x40 /* get return path from header */ meillo@10: #define ACC_NODOT_RELAX 0x80 /* do not be picky if message ist not terminated by a dot on a line */ meillo@10: #define ACC_SAVE_ENVELOPE_TO 0x0100 /* save an existent Envelope-to header as X-Orig-Envelope-to */ meillo@0: meillo@0: #define DLVR_LOCAL 0x01 meillo@0: #define DLVR_LAN 0x02 meillo@0: #define DLVR_ONLINE 0x04 meillo@0: #define DLVR_ALL (DLVR_LOCAL|DLVR_LAN|DLVR_ONLINE) meillo@0: meillo@0: /* transport flags */ meillo@0: #define MSGSTR_FROMLINE 0x01 meillo@0: #define MSGSTR_FROMHACK 0x02 meillo@0: meillo@10: typedef enum _accept_error { meillo@10: AERR_OK = 0, meillo@10: AERR_TIMEOUT, meillo@10: AERR_EOF, meillo@10: AERR_OVERFLOW, meillo@10: AERR_SYNTAX, meillo@10: AERR_NOSPOOL, meillo@10: AERR_NORCPT, meillo@117: AERR_SIZE, /* max msg size exeeded (SMTP SIZE) */ meillo@10: AERR_UNKNOWN meillo@10: } accept_error; meillo@0: meillo@0: #define BUF_LEN 1024 meillo@0: #define MAX_ADDRESS 256 meillo@0: #define MAX_DATALINE 4096 meillo@0: meillo@10: typedef enum _smtp_cmd_id { meillo@10: SMTP_HELO = 0, meillo@10: SMTP_EHLO, meillo@10: SMTP_MAIL_FROM, meillo@10: SMTP_RCPT_TO, meillo@10: SMTP_DATA, meillo@10: SMTP_QUIT, meillo@10: SMTP_RSET, meillo@10: SMTP_NOOP, meillo@10: SMTP_HELP, meillo@10: SMTP_NUM_IDS, meillo@10: SMTP_EOF = -1, meillo@10: SMTP_ERROR = -2, meillo@0: } smtp_cmd_id; meillo@0: meillo@10: typedef struct _smtp_cmd { meillo@10: smtp_cmd_id id; meillo@10: gchar *cmd; meillo@0: } smtp_cmd; meillo@0: meillo@10: typedef struct _smtp_connection { meillo@10: gchar *remote_host; meillo@0: meillo@10: prot_id prot; meillo@10: gint next_id; meillo@0: meillo@10: gboolean helo_seen; meillo@10: gboolean from_seen; meillo@10: gboolean rcpt_seen; meillo@10: meillo@10: message *msg; meillo@10: } smtp_connection; meillo@0: meillo@0: /* alias.c*/ meillo@10: gboolean addr_is_local(address * addr); meillo@10: GList *alias_expand(GList * alias_table, GList * rcpt_list, GList * non_rcpt_list); meillo@0: meillo@0: /* child.c */ meillo@0: int child(const char *command); meillo@0: meillo@0: /* conf.c */ meillo@0: void init_conf(); meillo@10: gboolean read_conf(gchar * filename); meillo@10: connect_route *read_route(gchar * filename, gboolean is_local_net); meillo@10: GList *read_route_list(GList * rf_list, gboolean is_local_net); meillo@10: void destroy_route(connect_route * r); meillo@10: void destroy_route_list(GList * list); meillo@0: connect_route *create_local_route(); meillo@0: meillo@0: /* expand.c */ meillo@10: GList *var_table_rcpt(GList * var_table, address * rcpt); meillo@10: GList *var_table_msg(GList * var_table, message * msg); meillo@10: GList *var_table_conf(GList * var_table); meillo@10: gint expand(GList * var_list, gchar * format, gchar * result, gint result_len); meillo@0: meillo@0: /* message.c */ meillo@0: message *create_message(void); meillo@10: void destroy_message(message * msg); meillo@10: void destroy_msg_list(GList * msg_list); meillo@10: void msg_free_data(message * msg); meillo@10: gint msg_calc_size(message * msg, gboolean is_smtp); meillo@0: meillo@10: msg_out *create_msg_out(message * msg); meillo@10: msg_out *clone_msg_out(msg_out * msgout_orig); meillo@10: void destroy_msg_out(msg_out * msgout); meillo@10: void destroy_msg_out_list(GList * msgout_list); meillo@0: meillo@0: /* address.c */ meillo@10: address *create_address(gchar * path, gboolean is_rfc821); meillo@10: address *create_address_qualified(gchar * path, gboolean is_rfc821, gchar * domain); meillo@10: address *create_address_pipe(gchar * path); meillo@10: void destroy_address(address * addr); meillo@10: address *copy_modify_address(const address * orig, gchar * l_part, gchar * dom); meillo@0: #define copy_address(addr) copy_modify_address(addr, NULL, NULL) meillo@242: gboolean addr_isequal(address * addr1, address * addr2, int (*cmpfunc) (const char*, const char*)); meillo@242: gboolean addr_isequal_parent(address * addr1, address * addr2, int (*cmpfunc) (const char*, const char*)); meillo@10: address *addr_find_ancestor(address * addr); meillo@10: gboolean addr_is_delivered_children(address * addr); meillo@10: gboolean addr_is_finished_children(address * addr); meillo@10: gchar *addr_string(address * addr); meillo@0: meillo@0: /* accept.c */ meillo@10: accept_error accept_message(FILE * in, message * msg, guint flags); meillo@10: accept_error accept_message_prepare(message * msg, guint flags); meillo@0: meillo@0: /* header.c */ meillo@0: gchar *rec_timestamp(); meillo@10: GList *find_header(GList * hdr_list, header_id id, gchar * hdr_str); meillo@10: void header_unfold(header * hdr); meillo@303: void header_fold(header * hdr, unsigned int maxlen); meillo@10: header *create_header(header_id id, gchar * fmt, ...); meillo@10: void destroy_header(header * hdr); meillo@10: header *copy_header(header * hdr); meillo@10: header *get_header(gchar * line); meillo@0: meillo@0: /* smtp_in.c */ meillo@10: void smtp_in(FILE * in, FILE * out, gchar * remote_host, gchar * ident); meillo@0: meillo@0: /* listen.c */ meillo@10: void listen_port(GList * addr_list, gint qival, char *argv[]); meillo@0: meillo@0: /* parse.c */ meillo@10: gboolean split_address(const gchar * path, gchar ** local_part, gchar ** domain, gboolean is_rfc821); meillo@10: gboolean parse_address_rfc822(gchar * string, gchar ** local_begin, gchar ** local_end, gchar ** domain_begin, gchar ** domain_end, gchar ** address_end); meillo@10: gboolean parse_address_rfc821(gchar * string, gchar ** local_begin, gchar ** local_end, gchar ** domain_begin, gchar ** domain_end, gchar ** address_end); meillo@10: address *_create_address(gchar * string, gchar ** end, gboolean is_rfc821); meillo@10: address *create_address_rfc821(gchar * string, gchar ** end); meillo@10: address *create_address_rfc822(gchar * string, gchar ** end); meillo@10: GList *addr_list_append_rfc822(GList * addr_list, gchar * string, gchar * domain); meillo@0: meillo@0: /* connect.c */ meillo@10: mxip_addr *connect_hostlist(int *psockfd, gchar * host, guint port, GList * addr_list); meillo@10: mxip_addr *connect_resolvelist(int *psockfd, gchar * host, guint port, GList * res_funcs); meillo@0: meillo@0: /* deliver.c */ meillo@10: void msg_rcptlist_local(GList * rcpt_list, GList **, GList **); meillo@10: gboolean deliver_local(msg_out * msgout); meillo@10: gboolean deliver_msglist_host(connect_route * route, GList * msg_list, gchar * host, GList * res_list); meillo@10: gboolean deliver_route_msgout_list(connect_route * route, GList * msgout_list); meillo@10: gboolean deliver_route_msg_list(connect_route * route, GList * msgout_list); meillo@10: gboolean deliver_finish(msg_out * msgout); meillo@10: gboolean deliver_msg_list(GList * msg_list, guint flags); meillo@10: gboolean deliver(message * msg); meillo@0: meillo@0: /* fail_msg.c */ meillo@10: gboolean fail_msg(message * msg, gchar * template, GList * failed_rcpts, gchar * err_fmt, va_list args); meillo@10: gboolean warn_msg(message * msg, gchar * template, GList * failed_rcpts, gchar * err_fmt, va_list args); meillo@0: meillo@0: /* interface.c */ meillo@10: gboolean init_sockaddr(struct sockaddr_in *name, interface * iface); meillo@10: int make_server_socket(interface * iface); meillo@0: meillo@0: /* local.c */ meillo@10: gboolean append_file(message * msg, GList * hdr_list, gchar * user); meillo@10: gboolean pipe_out(message * msg, GList * hdr_list, address * rcpt, gchar * cmd, guint flags); meillo@0: meillo@0: /* log.c */ meillo@0: gchar *ext_strerror(int err); meillo@0: gboolean logopen(void); meillo@0: void logclose(void); meillo@0: void vlogwrite(int pri, const char *fmt, va_list args); meillo@0: void logwrite(int pri, const char *fmt, ...); meillo@0: void debugf(const char *fmt, ...); meillo@0: void vdebugf(const char *fmt, va_list args); meillo@0: void maillog(const char *fmt, ...); meillo@0: meillo@0: /* spool.c */ meillo@10: gboolean spool_read_data(message * msg); meillo@349: message *msg_spool_read(gchar * uid); meillo@10: gboolean spool_write(message * msg, gboolean do_writedata); meillo@10: gboolean spool_lock(gchar * uid); meillo@10: gboolean spool_unlock(gchar * uid); meillo@10: gboolean spool_delete_all(message * msg); meillo@0: meillo@0: /* queue.c */ meillo@348: GList *read_queue(void); meillo@0: gboolean queue_run(void); meillo@0: gboolean queue_run_online(void); meillo@0: void queue_list(void); meillo@10: gboolean queue_delete(gchar * uid); meillo@0: meillo@0: /* online.c */ meillo@310: gchar *online_query(); meillo@0: meillo@0: /* permissions.c */ meillo@0: gboolean is_ingroup(uid_t uid, gid_t gid); meillo@10: void set_euidgid(gint uid, gint gid, uid_t * old_uid, gid_t * old_gid); meillo@10: void set_identity(uid_t old_uid, gchar * task_name); meillo@0: meillo@0: /* rewrite.c */ meillo@10: gboolean set_address_header_domain(header * hdr, gchar * domain); meillo@10: gboolean map_address_header(header * hdr, GList * table); meillo@0: meillo@0: /* route.c */ meillo@10: msgout_perhost *create_msgout_perhost(gchar * host); meillo@10: void destroy_msgout_perhost(msgout_perhost * mo_ph); meillo@10: void rewrite_headers(msg_out * msgout, connect_route * route); meillo@237: void split_rcpts(GList* rcpt_list, GList* localnets, GList** rl_local, GList** rl_localnet, GList** rl_others); meillo@345: GList* local_rcpts(GList* rcpt_list); meillo@345: GList* remote_rcpts(GList* rcpt_list); meillo@10: gboolean route_strip_msgout(connect_route * route, msg_out * msgout); meillo@10: msg_out *route_prepare_msgout(connect_route * route, msg_out * msgout); meillo@10: GList *route_msgout_list(connect_route * route, GList * msgout_list); meillo@317: gboolean route_sender_is_allowed(connect_route * route, address * ret_path); meillo@317: void route_split_rcpts(connect_route * route, GList * rcpt_list, GList ** p_rcpt_list, GList ** p_non_rcpt_list); meillo@0: meillo@0: /* tables.c */ meillo@10: table_pair *create_pair(gchar * key, gpointer value); meillo@10: table_pair *create_pair_string(gchar * key, gpointer value); meillo@10: table_pair *parse_table_pair(gchar * line, char delim); meillo@10: gpointer *table_find_func(GList * table_list, gchar * key, int (*cmp_func) (const char *, const char *)); meillo@10: gpointer *table_find(GList * table_list, gchar * key); meillo@10: gpointer *table_find_case(GList * table_list, gchar * key); meillo@10: gpointer *table_find_fnmatch(GList * table_list, gchar * key); meillo@10: GList *table_read(gchar * fname, gchar delim); meillo@10: void destroy_table(GList * table); meillo@0: meillo@0: /* timeival.c */ meillo@254: gint time_interval(gchar * str); meillo@0: meillo@0: /* permissions.c */ meillo@0: gboolean is_privileged_user(uid_t uid); meillo@0: meillo@0: /* other things */ meillo@0: meillo@0: #define foreach(list, node)\ meillo@0: for((node) = g_list_first(list);\ meillo@0: (node);\ meillo@0: (node) = g_list_next(node)) meillo@0: meillo@0: #ifdef ENABLE_DEBUG meillo@0: #define DEBUG(level) if(level <= conf.debug_level) meillo@0: #else meillo@0: /* hopefully the compiler optmizes this away... */ meillo@0: #define DEBUG(level) if(0) meillo@0: #endif meillo@0: meillo@0: #define LOG_VERBOSE 0x100 meillo@0: meillo@0: #ifndef HAVE_GETLINE meillo@0: #define getline(buf, size, file) getdelim(buf, size, '\n', file) meillo@0: #endif meillo@0: meillo@0: #ifndef HAVE_FDATASYNC meillo@0: #define fdatasync(fd) fsync(fd) meillo@0: #endif meillo@0: meillo@0: #ifndef CONF_DIR meillo@0: #define CONF_DIR "/etc/masqmail" meillo@0: #endif meillo@0: meillo@0: #define CONF_FILE CONF_DIR"/masqmail.conf" meillo@0: meillo@0: #define PIDFILEDIR "/var/run/masqmail/" meillo@0: meillo@0: #ifndef va_copy meillo@0: #ifdef __va_copy meillo@0: #define va_copy(ap1, ap2) __va_copy(ap1, ap2) meillo@0: #else meillo@0: #define va_copy(ap1, ap2) G_VA_COPY(ap1, ap2) meillo@0: #endif meillo@0: #endif meillo@0: meillo@0: /* *BSD needs this: */ meillo@0: extern char **environ;