masqmail

view src/masqmail.h @ 375:3f923f97563b

local_hosts now understands glob patterns like `*example.org'.
author markus schnalke <meillo@marmaro.de>
date Sat, 14 Jan 2012 18:14:07 +0100
parents 41958685480d
children 5781ba87df95
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 #ifdef ENABLE_IDENT
45 #include "libident/ident.h"
46 #endif
48 #include "lookup.h"
50 typedef struct _interface {
51 gchar *address;
52 gint port;
53 } interface;
55 #define ADDR_FLAG_DELIVERED 0x01
56 #define ADDR_FLAG_DEFERED 0x02
57 #define ADDR_FLAG_FAILED 0x04
58 #define ADDR_FLAG_LAST_ROUTE 0x40
60 typedef struct _address {
61 gchar *address;
62 gchar *local_part;
63 gchar *domain;
64 gint flags;
65 GList *children;
66 struct _address *parent;
67 } address;
69 #define addr_mark_delivered(addr) { addr->flags |= ADDR_FLAG_DELIVERED; }
70 #define addr_unmark_delivered(addr) { addr->flags &= ~ADDR_FLAG_DELIVERED; }
71 #define addr_is_delivered(addr) ((addr->flags & ADDR_FLAG_DELIVERED) != 0 )
73 #define addr_mark_defered(addr) { addr->flags |= ADDR_FLAG_DEFERED; }
74 #define addr_unmark_defered(addr) { addr->flags &= ~ADDR_FLAG_DEFERED; }
75 #define addr_is_defered(addr) ((addr->flags & ADDR_FLAG_DEFERED) != 0 )
77 #define addr_mark_failed(addr) { addr->flags |= ADDR_FLAG_FAILED; }
78 #define addr_unmark_failed(addr) { addr->flags &= ~ADDR_FLAG_FAILED; }
79 #define addr_is_failed(addr) ((addr->flags & ADDR_FLAG_FAILED) != 0 )
81 typedef struct _connect_route {
82 gchar *name;
83 gchar *filename;
85 gboolean is_perma;
86 gboolean last_route;
88 GList *allowed_senders;
89 GList *denied_senders;
90 GList *allowed_recipients;
91 GList *denied_recipients;
93 interface *mail_host;
94 gboolean connect_error_fail;
95 GList *resolve_list;
96 gchar *helo_name;
97 gboolean do_correct_helo;
98 gboolean instant_helo;
99 gboolean do_pipelining;
100 gchar *auth_name;
101 gchar *auth_login;
102 gchar *auth_secret;
103 gchar *wrapper;
105 gchar *set_h_from_domain;
106 gchar *set_h_reply_to_domain;
107 gchar *set_return_path_domain;
108 GList *map_h_from_addresses;
109 GList *map_h_reply_to_addresses;
110 GList *map_h_mail_followup_to_addresses;
111 GList *map_return_path_addresses;
112 gboolean expand_h_sender_domain;
113 gboolean expand_h_sender_address;
115 gchar *pipe;
116 gboolean pipe_fromline;
117 gboolean pipe_fromhack;
118 } connect_route;
120 typedef struct _masqmail_conf {
121 gint mail_uid;
122 gint mail_gid;
124 gint orig_uid;
125 gint orig_gid;
127 gboolean run_as_user;
129 gchar *mail_dir;
130 gchar *lock_dir;
131 gchar *spool_dir;
132 gchar *log_dir;
134 gint debug_level;
135 gboolean use_syslog;
136 guint log_max_pri;
138 gchar *host_name;
139 GList *local_hosts;
140 GList *local_addresses;
141 GList *not_local_addresses;
142 GList *listen_addresses;
144 /*
145 ** ANSI C defines unsigned long to be at least 32bit
146 ** i.e. ca. 4GB max; that should be enough.
147 */
148 gulong max_msg_size;
150 gboolean do_save_envelope_to;
152 gboolean defer_all;
153 gboolean do_relay;
155 GList *ident_trusted_nets;
157 gboolean do_queue;
159 gboolean do_verbose;
161 gchar *mbox_default;
162 GList *mbox_users;
163 GList *mda_users;
165 gchar *mda;
166 gboolean mda_fromline;
167 gboolean mda_fromhack;
169 gboolean pipe_fromline;
170 gboolean pipe_fromhack;
172 gchar *alias_file;
173 int (*localpartcmp) (const char *, const char *);
175 GList *perma_routes;
176 GList *query_routes; /* list of pairs which point to lists */
178 gchar *online_query;
180 gchar *errmsg_file;
181 gchar *warnmsg_file;
182 GList *warn_intervals;
183 gint max_defer_time;
185 gchar *log_user;
186 } masqmail_conf;
188 extern masqmail_conf conf;
190 typedef struct _table_pair {
191 gchar *key;
192 gpointer *value;
193 } table_pair;
196 /* must match the contents of prot_names[] in accept.c */
197 typedef enum _prot_id {
198 PROT_LOCAL = 0,
199 PROT_SMTP,
200 PROT_ESMTP,
201 PROT_NUM
202 } prot_id;
204 extern gchar *prot_names[];
206 typedef enum _header_id {
207 HEAD_FROM = 0,
208 HEAD_SENDER,
209 HEAD_TO,
210 HEAD_CC,
211 HEAD_BCC,
212 HEAD_DATE,
213 HEAD_MESSAGE_ID,
214 HEAD_REPLY_TO,
215 HEAD_SUBJECT,
216 HEAD_RETURN_PATH,
217 HEAD_ENVELOPE_TO,
218 HEAD_RECEIVED,
219 HEAD_NUM_IDS,
220 HEAD_STATUS,
221 HEAD_UNKNOWN = HEAD_NUM_IDS,
222 HEAD_NONE = -1,
223 } header_id;
225 typedef struct _header_name {
226 gchar *header;
227 header_id id;
228 } header_name;
230 typedef struct _header {
231 header_id id;
232 gchar *header;
233 gchar *value;
234 } header;
237 typedef struct _message {
238 gchar *uid;
240 gchar *received_host;
241 prot_id received_prot;
242 gchar *ident;
243 gint transfer_id; /* for multiple messages per transfer */
245 address *return_path;
246 GList *rcpt_list;
247 GList *non_rcpt_list;
249 GList *hdr_list;
250 GList *data_list;
252 gint data_size;
253 time_t received_time;
254 time_t warned_time;
256 gchar *full_sender_name;
257 } message;
259 typedef struct _msg_out {
260 message *msg;
262 address *return_path;
263 GList *rcpt_list;
265 GList *hdr_list;
266 GList *xtra_hdr_list; /* rewritten headers */
267 } msg_out;
269 typedef struct _msgout_perhost {
270 gchar *host;
271 GList *msgout_list;
272 } msgout_perhost;
274 /* flags for accept() */
275 #define ACC_RCPT_FROM_HEAD 0x08 /* -t option, get rcpts from headers */
276 #define ACC_DOT_IGNORE 0x10 /* a dot on a line itself does not end the message (-oi option) */
277 #define ACC_MAIL_FROM_HEAD 0x40 /* get return path from header */
278 #define ACC_NODOT_RELAX 0x80 /* do not be picky if message ist not terminated by a dot on a line */
279 #define ACC_SAVE_ENVELOPE_TO 0x0100 /* save an existent Envelope-to header as X-Orig-Envelope-to */
281 #define DLVR_LOCAL 0x01
282 #define DLVR_ONLINE 0x02
283 #define DLVR_ALL (DLVR_LOCAL|DLVR_ONLINE)
285 /* transport flags */
286 #define MSGSTR_FROMLINE 0x01
287 #define MSGSTR_FROMHACK 0x02
289 typedef enum _accept_error {
290 AERR_OK = 0,
291 AERR_TIMEOUT,
292 AERR_EOF,
293 AERR_OVERFLOW,
294 AERR_SYNTAX,
295 AERR_NOSPOOL,
296 AERR_NORCPT,
297 AERR_SIZE, /* max msg size exeeded (SMTP SIZE) */
298 AERR_UNKNOWN
299 } accept_error;
301 #define BUF_LEN 1024
302 #define MAX_ADDRESS 256
303 #define MAX_DATALINE 4096
305 typedef enum _smtp_cmd_id {
306 SMTP_HELO = 0,
307 SMTP_EHLO,
308 SMTP_MAIL_FROM,
309 SMTP_RCPT_TO,
310 SMTP_DATA,
311 SMTP_QUIT,
312 SMTP_RSET,
313 SMTP_NOOP,
314 SMTP_HELP,
315 SMTP_NUM_IDS,
316 SMTP_EOF = -1,
317 SMTP_ERROR = -2,
318 } smtp_cmd_id;
320 typedef struct _smtp_cmd {
321 smtp_cmd_id id;
322 gchar *cmd;
323 } smtp_cmd;
325 typedef struct _smtp_connection {
326 gchar *remote_host;
328 prot_id prot;
329 gint next_id;
331 gboolean helo_seen;
332 gboolean from_seen;
333 gboolean rcpt_seen;
335 message *msg;
336 } smtp_connection;
338 /* alias.c*/
339 gboolean addr_is_local(address *addr);
340 GList *alias_expand(GList *alias_table, GList *rcpt_list, GList *non_rcpt_list);
342 /* child.c */
343 int child(const char *command);
345 /* conf.c */
346 void init_conf();
347 gboolean read_conf(gchar *filename);
348 connect_route *read_route(gchar *filename, gboolean is_perma);
349 GList *read_route_list(GList *rf_list, gboolean is_perma);
350 void destroy_route(connect_route *r);
351 void destroy_route_list(GList *list);
353 /* expand.c */
354 GList *var_table_rcpt(GList *var_table, address *rcpt);
355 GList *var_table_msg(GList *var_table, message *msg);
356 GList *var_table_conf(GList *var_table);
357 gint expand(GList *var_list, gchar *format, gchar *result, gint result_len);
359 /* message.c */
360 message *create_message(void);
361 void destroy_message(message *msg);
362 void destroy_msg_list(GList *msg_list);
363 void msg_free_data(message *msg);
364 gint msg_calc_size(message *msg, gboolean is_smtp);
366 msg_out *create_msg_out(message *msg);
367 msg_out *clone_msg_out(msg_out *msgout_orig);
368 void destroy_msg_out(msg_out *msgout);
369 void destroy_msg_out_list(GList *msgout_list);
371 /* address.c */
372 address *create_address(gchar *path, gboolean is_rfc821);
373 address *create_address_qualified(gchar *path, gboolean is_rfc821, gchar *domain);
374 address *create_address_pipe(gchar *path);
375 void destroy_address(address *addr);
376 address *copy_modify_address(const address *orig, gchar *l_part, gchar *dom);
377 #define copy_address(addr) copy_modify_address(addr, NULL, NULL)
378 gboolean addr_isequal(address *addr1, address *addr2, int (*cmpfunc) (const char*, const char*));
379 gboolean addr_isequal_parent(address *addr1, address *addr2, int (*cmpfunc) (const char*, const char*));
380 address *addr_find_ancestor(address *addr);
381 gboolean addr_is_delivered_children(address *addr);
382 gboolean addr_is_finished_children(address *addr);
383 gchar *addr_string(address *addr);
385 /* accept.c */
386 accept_error accept_message(FILE *in, message *msg, guint flags);
387 accept_error accept_message_prepare(message *msg, guint flags);
389 /* header.c */
390 gchar *rec_timestamp();
391 GList *find_header(GList *hdr_list, header_id id, gchar *hdr_str);
392 void header_unfold(header *hdr);
393 void header_fold(header *hdr, unsigned int maxlen);
394 header *create_header(header_id id, gchar *fmt, ...);
395 void destroy_header(header *hdr);
396 header *copy_header(header *hdr);
397 header *get_header(gchar *line);
399 /* smtp_in.c */
400 void smtp_in(FILE *in, FILE *out, gchar *remote_host, gchar *ident);
402 /* listen.c */
403 void listen_port(GList *addr_list, gint qival, char *argv[]);
405 /* parse.c */
406 gboolean split_address(const gchar *path, gchar **local_part, gchar **domain, gboolean is_rfc821);
407 gboolean parse_address_rfc822(gchar *string, gchar **local_begin, gchar **local_end, gchar **domain_begin, gchar **domain_end, gchar **address_end);
408 gboolean parse_address_rfc821(gchar *string, gchar **local_begin, gchar **local_end, gchar **domain_begin, gchar **domain_end, gchar **address_end);
409 address *_create_address(gchar *string, gchar **end, gboolean is_rfc821);
410 address *create_address_rfc821(gchar *string, gchar **end);
411 address *create_address_rfc822(gchar *string, gchar **end);
412 GList *addr_list_append_rfc822(GList *addr_list, gchar *string, gchar *domain);
414 /* connect.c */
415 mxip_addr *connect_hostlist(int *psockfd, gchar *host, guint port, GList *addr_list);
416 mxip_addr *connect_resolvelist(int *psockfd, gchar *host, guint port, GList *res_funcs);
418 /* deliver.c */
419 void msg_rcptlist_local(GList *rcpt_list, GList **, GList **);
420 gboolean deliver_local(msg_out *msgout);
421 gboolean deliver_msglist_host(connect_route *route, GList *msg_list, gchar *host, GList *res_list);
422 gboolean deliver_route_msgout_list(connect_route *route, GList *msgout_list);
423 gboolean deliver_route_msg_list(connect_route *route, GList *msgout_list);
424 gboolean deliver_finish(msg_out *msgout);
425 gboolean deliver_msg_list(GList *msg_list, guint flags);
426 gboolean deliver(message *msg);
428 /* fail_msg.c */
429 gboolean fail_msg(message *msg, gchar *template, GList *failed_rcpts, gchar *err_fmt, va_list args);
430 gboolean warn_msg(message *msg, gchar *template, GList *failed_rcpts, gchar *err_fmt, va_list args);
432 /* interface.c */
433 gboolean init_sockaddr(struct sockaddr_in *name, interface *iface);
434 int make_server_socket(interface *iface);
436 /* local.c */
437 gboolean append_file(message *msg, GList *hdr_list, gchar *user);
438 gboolean pipe_out(message *msg, GList *hdr_list, address *rcpt, gchar *cmd, guint flags);
440 /* log.c */
441 gchar *ext_strerror(int err);
442 gboolean logopen(void);
443 void logclose(void);
444 void vlogwrite(int pri, const char *fmt, va_list args);
445 void logwrite(int pri, const char *fmt, ...);
446 void debugf(const char *fmt, ...);
447 void vdebugf(const char *fmt, va_list args);
448 void maillog(const char *fmt, ...);
450 /* spool.c */
451 gboolean spool_read_data(message *msg);
452 message *msg_spool_read(gchar *uid);
453 gboolean spool_write(message *msg, gboolean do_writedata);
454 gboolean spool_lock(gchar *uid);
455 gboolean spool_unlock(gchar *uid);
456 gboolean spool_delete_all(message *msg);
458 /* queue.c */
459 GList *read_queue(void);
460 gboolean queue_run(void);
461 gboolean queue_run_online(void);
462 void queue_list(void);
463 gboolean queue_delete(gchar *uid);
465 /* online.c */
466 gchar *online_query();
468 /* permissions.c */
469 gboolean is_ingroup(uid_t uid, gid_t gid);
470 void set_euidgid(gint uid, gint gid, uid_t *old_uid, gid_t *old_gid);
471 void set_identity(uid_t old_uid, gchar *task_name);
473 /* rewrite.c */
474 gboolean set_address_header_domain(header *hdr, gchar *domain);
475 gboolean map_address_header(header *hdr, GList *table);
477 /* route.c */
478 msgout_perhost *create_msgout_perhost(gchar *host);
479 void destroy_msgout_perhost(msgout_perhost *mo_ph);
480 void rewrite_headers(msg_out *msgout, connect_route *route);
481 void split_rcpts(GList *rcpt_list, GList *localnets, GList **rl_local, GList **rl_localnet, GList **rl_others);
482 GList *local_rcpts(GList *rcpt_list);
483 GList *remote_rcpts(GList *rcpt_list);
484 gboolean route_strip_msgout(connect_route *route, msg_out *msgout);
485 msg_out *route_prepare_msgout(connect_route *route, msg_out *msgout);
486 GList *route_msgout_list(connect_route *route, GList *msgout_list);
487 gboolean route_sender_is_allowed(connect_route *route, address *ret_path);
488 void route_split_rcpts(connect_route *route, GList *rcpt_list, GList **p_rcpt_list, GList **p_non_rcpt_list);
490 /* tables.c */
491 table_pair *create_pair(gchar *key, gpointer value);
492 table_pair *create_pair_string(gchar *key, gpointer value);
493 table_pair *parse_table_pair(gchar *line, char delim);
494 gpointer *table_find_func(GList *table_list, gchar *key, int (*cmp_func) (const char *, const char *));
495 gpointer *table_find(GList *table_list, gchar *key);
496 gpointer *table_find_case(GList *table_list, gchar *key);
497 gpointer *table_find_fnmatch(GList *table_list, gchar *key);
498 GList *table_read(gchar *fname, gchar delim);
499 void destroy_table(GList *table);
501 /* timeival.c */
502 gint time_interval(gchar *str);
504 /* permissions.c */
505 gboolean is_privileged_user(uid_t uid);
507 /* other things */
509 #define foreach(list, node)\
510 for((node) = g_list_first(list);\
511 (node);\
512 (node) = g_list_next(node))
514 #ifdef ENABLE_DEBUG
515 #define DEBUG(level) if(level <= conf.debug_level)
516 #else
517 /* hopefully the compiler optmizes this away... */
518 #define DEBUG(level) if(0)
519 #endif
521 #define LOG_VERBOSE 0x100
523 #ifndef HAVE_GETLINE
524 #define getline(buf, size, file) getdelim(buf, size, '\n', file)
525 #endif
527 #ifndef HAVE_FDATASYNC
528 #define fdatasync(fd) fsync(fd)
529 #endif
531 #ifndef CONF_DIR
532 #define CONF_DIR "/etc/masqmail"
533 #endif
535 #define CONF_FILE CONF_DIR"/masqmail.conf"
537 #define PIDFILEDIR "/var/run/masqmail/"
539 #ifndef va_copy
540 #ifdef __va_copy
541 #define va_copy(ap1, ap2) __va_copy(ap1, ap2)
542 #else
543 #define va_copy(ap1, ap2) G_VA_COPY(ap1, ap2)
544 #endif
545 #endif
547 /* *BSD needs this: */
548 extern char **environ;