masqmail

view src/masqmail.h @ 388:aa40710f09fe

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