masqmail

view src/masqmail.h @ 227:cab46cefa4ce

renamed contrib/ to admin/ because the contents are for system admins and possibly for advanced users too
author meillo@marmaro.de
date Fri, 23 Jul 2010 11:49:44 +0200
parents 8cddc65765bd
children 5f9f3a65032e
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
58 #define ADDR_FLAG_NOEXPAND 0x80
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 gchar *protocol;
87 gboolean is_local_net;
88 gboolean last_route;
90 GList *allowed_return_paths;
91 GList *not_allowed_return_paths;
92 GList *allowed_mail_locals;
93 GList *not_allowed_mail_locals;
94 GList *allowed_rcpt_domains;
95 GList *not_allowed_rcpt_domains;
97 interface *mail_host;
98 gchar *wrapper;
99 gboolean connect_error_fail;
101 gchar *helo_name;
102 gboolean do_correct_helo;
103 gboolean instant_helo;
104 gboolean do_pipelining;
106 gchar *set_h_from_domain;
107 gchar *set_h_reply_to_domain;
108 gchar *set_return_path_domain;
110 GList *map_h_from_addresses;
111 GList *map_h_reply_to_addresses;
112 GList *map_h_mail_followup_to_addresses;
113 GList *map_return_path_addresses;
115 gboolean expand_h_sender_domain;
116 gboolean expand_h_sender_address;
118 GList *resolve_list;
120 gchar *auth_name;
121 gchar *auth_login;
122 gchar *auth_secret;
124 gchar *pipe;
126 gboolean pipe_fromline;
127 gboolean pipe_fromhack;
128 } connect_route;
130 typedef struct _masqmail_conf {
131 gint mail_uid;
132 gint mail_gid;
134 gint orig_uid;
135 gint orig_gid;
137 gboolean run_as_user;
139 gchar *mail_dir;
140 gchar *lock_dir;
141 gchar *spool_dir;
142 gchar *log_dir;
144 gint debug_level;
145 gboolean use_syslog;
146 guint log_max_pri;
148 gchar *host_name;
149 GList *local_hosts;
150 GList *local_addresses;
151 GList *not_local_addresses;
152 GList *local_nets;
153 GList *listen_addresses;
155 /* ANSI C defines unsigned long to be at least 32bit
156 i.e. ca. 4GB max; that should be enough. */
157 gulong max_msg_size;
159 gboolean do_save_envelope_to;
161 gboolean defer_all;
162 gboolean do_relay;
164 GList *ident_trusted_nets;
166 gboolean do_queue;
168 gboolean do_verbose;
170 gchar *mbox_default;
171 GList *mbox_users;
172 GList *mda_users;
174 gchar *mda;
175 gboolean mda_fromline;
176 gboolean mda_fromhack;
178 gboolean pipe_fromline;
179 gboolean pipe_fromhack;
181 gchar *alias_file;
182 int (*alias_local_cmp) (const char *, const char *);
184 GList *local_net_routes;
185 GList *connect_routes; /* list of pairs which point to lists */
187 gchar *online_detect;
188 gchar *online_file;
189 gchar *online_pipe;
191 gchar *errmsg_file;
192 gchar *warnmsg_file;
193 GList *warn_intervals;
194 gint max_defer_time;
196 gchar *log_user;
197 } masqmail_conf;
199 extern masqmail_conf conf;
201 typedef struct _table_pair {
202 gchar *key;
203 gpointer *value;
204 } table_pair;
207 typedef enum _prot_id {
208 PROT_LOCAL = 0,
209 PROT_BSMTP,
210 PROT_SMTP,
211 PROT_ESMTP,
212 PROT_NUM
213 } prot_id;
215 extern gchar *prot_names[];
217 typedef enum _header_id {
218 HEAD_FROM = 0,
219 HEAD_SENDER,
220 HEAD_TO,
221 HEAD_CC,
222 HEAD_BCC,
223 HEAD_DATE,
224 HEAD_MESSAGE_ID,
225 HEAD_REPLY_TO,
226 HEAD_SUBJECT,
227 HEAD_RETURN_PATH,
228 HEAD_ENVELOPE_TO,
229 HEAD_RECEIVED,
230 HEAD_NUM_IDS,
231 HEAD_STATUS,
232 HEAD_UNKNOWN = HEAD_NUM_IDS,
233 HEAD_NONE = -1,
234 } header_id;
236 typedef struct _header_name {
237 gchar *header;
238 header_id id;
239 } header_name;
241 typedef struct _header {
242 header_id id;
243 gchar *header;
244 gchar *value;
245 } header;
248 typedef struct _message {
249 gchar *uid;
251 gchar *received_host;
252 prot_id received_prot;
253 gchar *ident;
254 gint transfer_id; /* for multiple messages per transfer */
256 address *return_path;
257 GList *rcpt_list;
258 GList *non_rcpt_list;
260 GList *hdr_list;
261 GList *data_list;
263 gint data_size;
264 time_t received_time;
265 time_t warned_time;
267 gchar *full_sender_name;
268 } message;
270 typedef struct _msg_out {
271 message *msg;
273 address *return_path;
274 GList *rcpt_list;
276 GList *hdr_list;
277 GList *xtra_hdr_list;
278 } msg_out;
280 typedef struct _msgout_perhost {
281 gchar *host;
282 GList *msgout_list;
283 } msgout_perhost;
285 /* flags for accept() */
286 #define ACC_DEL_RCPTS 0x02 /* -t option, delete rcpts that were given as cmd args */
287 #define ACC_RCPT_FROM_HEAD 0x08 /* -t option, get rcpts from headers */
288 #define ACC_DOT_IGNORE 0x10 /* a dot on a line itself does not end the message (-oi option) */
289 #define ACC_MAIL_FROM_HEAD 0x40 /* get return path from header */
290 #define ACC_NODOT_RELAX 0x80 /* do not be picky if message ist not terminated by a dot on a line */
291 #define ACC_SAVE_ENVELOPE_TO 0x0100 /* save an existent Envelope-to header as X-Orig-Envelope-to */
293 #define DLVR_LOCAL 0x01
294 #define DLVR_LAN 0x02
295 #define DLVR_ONLINE 0x04
296 #define DLVR_ALL (DLVR_LOCAL|DLVR_LAN|DLVR_ONLINE)
298 /* transport flags */
299 #define MSGSTR_FROMLINE 0x01
300 #define MSGSTR_FROMHACK 0x02
302 typedef enum _accept_error {
303 AERR_OK = 0,
304 AERR_TIMEOUT,
305 AERR_EOF,
306 AERR_OVERFLOW,
307 AERR_SYNTAX,
308 AERR_NOSPOOL,
309 AERR_NORCPT,
310 AERR_SIZE, /* max msg size exeeded (SMTP SIZE) */
311 AERR_UNKNOWN
312 } accept_error;
314 #define BUF_LEN 1024
315 #define MAX_ADDRESS 256
316 #define MAX_DATALINE 4096
318 typedef enum _smtp_cmd_id {
319 SMTP_HELO = 0,
320 SMTP_EHLO,
321 SMTP_MAIL_FROM,
322 SMTP_RCPT_TO,
323 SMTP_DATA,
324 SMTP_QUIT,
325 SMTP_RSET,
326 SMTP_NOOP,
327 SMTP_HELP,
328 SMTP_NUM_IDS,
329 SMTP_EOF = -1,
330 SMTP_ERROR = -2,
331 } smtp_cmd_id;
333 typedef struct _smtp_cmd {
334 smtp_cmd_id id;
335 gchar *cmd;
336 } smtp_cmd;
338 typedef struct _smtp_connection {
339 gchar *remote_host;
341 prot_id prot;
342 gint next_id;
344 gboolean helo_seen;
345 gboolean from_seen;
346 gboolean rcpt_seen;
348 message *msg;
349 } smtp_connection;
351 /* alias.c*/
352 gboolean addr_is_local(address * addr);
353 GList *alias_expand(GList * alias_table, GList * rcpt_list, GList * non_rcpt_list);
355 /* child.c */
356 int child(const char *command);
358 /* conf.c */
359 void init_conf();
360 gboolean read_conf(gchar * filename);
361 connect_route *read_route(gchar * filename, gboolean is_local_net);
362 GList *read_route_list(GList * rf_list, gboolean is_local_net);
363 void destroy_route(connect_route * r);
364 void destroy_route_list(GList * list);
365 connect_route *create_local_route();
367 /* expand.c */
368 GList *var_table_rcpt(GList * var_table, address * rcpt);
369 GList *var_table_msg(GList * var_table, message * msg);
370 GList *var_table_conf(GList * var_table);
371 gint expand(GList * var_list, gchar * format, gchar * result, gint result_len);
373 /* message.c */
374 message *create_message(void);
375 void destroy_message(message * msg);
376 void destroy_msg_list(GList * msg_list);
377 void msg_free_data(message * msg);
378 gint msg_calc_size(message * msg, gboolean is_smtp);
380 msg_out *create_msg_out(message * msg);
381 msg_out *clone_msg_out(msg_out * msgout_orig);
382 GList *create_msg_out_list(GList * msg_list);
383 void destroy_msg_out(msg_out * msgout);
384 void destroy_msg_out_list(GList * msgout_list);
386 /* address.c */
387 address *create_address(gchar * path, gboolean is_rfc821);
388 address *create_address_qualified(gchar * path, gboolean is_rfc821, gchar * domain);
389 address *create_address_pipe(gchar * path);
390 void destroy_address(address * addr);
391 address *copy_modify_address(const address * orig, gchar * l_part, gchar * dom);
392 #define copy_address(addr) copy_modify_address(addr, NULL, NULL)
393 gboolean addr_isequal(address * addr1, address * addr2);
394 gboolean addr_isequal_parent(address * addr1, address * addr2);
395 address *addr_find_ancestor(address * addr);
396 gboolean addr_is_delivered_children(address * addr);
397 gboolean addr_is_finished_children(address * addr);
398 gchar *addr_string(address * addr);
399 gint addr_match(address * addr1, address * addr2);
401 /* accept.c */
402 accept_error accept_message(FILE * in, message * msg, guint flags);
403 accept_error accept_message_prepare(message * msg, guint flags);
405 /* header.c */
406 gchar *rec_timestamp();
407 GList *find_header(GList * hdr_list, header_id id, gchar * hdr_str);
408 void header_unfold(header * hdr);
409 void header_fold(header * hdr);
410 header *create_header(header_id id, gchar * fmt, ...);
411 void destroy_header(header * hdr);
412 header *copy_header(header * hdr);
413 header *get_header(gchar * line);
415 /* smtp_in.c */
416 void smtp_in(FILE * in, FILE * out, gchar * remote_host, gchar * ident);
418 /* listen.c */
419 void listen_port(GList * addr_list, gint qival, char *argv[]);
421 /* parse.c */
422 gboolean split_address(const gchar * path, gchar ** local_part, gchar ** domain, gboolean is_rfc821);
423 gboolean parse_address_rfc822(gchar * string, gchar ** local_begin, gchar ** local_end, gchar ** domain_begin, gchar ** domain_end, gchar ** address_end);
424 gboolean parse_address_rfc821(gchar * string, gchar ** local_begin, gchar ** local_end, gchar ** domain_begin, gchar ** domain_end, gchar ** address_end);
425 address *_create_address(gchar * string, gchar ** end, gboolean is_rfc821);
426 address *create_address_rfc821(gchar * string, gchar ** end);
427 address *create_address_rfc822(gchar * string, gchar ** end);
428 GList *addr_list_append_rfc822(GList * addr_list, gchar * string, gchar * domain);
429 gboolean addr_isequal(address * addr1, address * addr2);
431 /* connect.c */
432 mxip_addr *connect_hostlist(int *psockfd, gchar * host, guint port, GList * addr_list);
433 mxip_addr *connect_resolvelist(int *psockfd, gchar * host, guint port, GList * res_funcs);
435 /* deliver.c */
436 void msg_rcptlist_local(GList * rcpt_list, GList **, GList **);
437 gboolean deliver_local(msg_out * msgout);
438 gboolean deliver_msglist_host(connect_route * route, GList * msg_list, gchar * host, GList * res_list);
439 gboolean deliver_route_msgout_list(connect_route * route, GList * msgout_list);
440 gboolean deliver_route_msg_list(connect_route * route, GList * msgout_list);
441 gboolean deliver_finish(msg_out * msgout);
442 gboolean deliver_finish_list(GList * msgout_list);
443 gboolean deliver_msg_list(GList * msg_list, guint flags);
444 gboolean deliver(message * msg);
446 /* fail_msg.c */
447 gboolean fail_msg(message * msg, gchar * template, GList * failed_rcpts, gchar * err_fmt, va_list args);
448 gboolean warn_msg(message * msg, gchar * template, GList * failed_rcpts, gchar * err_fmt, va_list args);
450 /* interface.c */
451 gboolean init_sockaddr(struct sockaddr_in *name, interface * iface);
452 int make_server_socket(interface * iface);
454 /* local.c */
455 gboolean append_file(message * msg, GList * hdr_list, gchar * user);
456 gboolean pipe_out(message * msg, GList * hdr_list, address * rcpt, gchar * cmd, guint flags);
458 /* log.c */
459 gchar *ext_strerror(int err);
460 gboolean logopen(void);
461 void logclose(void);
462 void vlogwrite(int pri, const char *fmt, va_list args);
463 void logwrite(int pri, const char *fmt, ...);
464 void debugf(const char *fmt, ...);
465 void vdebugf(const char *fmt, va_list args);
466 void maillog(const char *fmt, ...);
468 /* spool.c */
469 gboolean spool_read_data(message * msg);
470 gboolean spool_read_data(message * msg);
471 message *msg_spool_read(gchar * uid, gboolean do_readdata);
472 gboolean spool_write(message * msg, gboolean do_writedata);
473 gboolean spool_lock(gchar * uid);
474 gboolean spool_unlock(gchar * uid);
475 gboolean spool_delete_all(message * msg);
477 /* queue.c */
478 GList *read_queue(gboolean do_readdata);
479 gboolean queue_run(void);
480 gboolean queue_run_online(void);
481 void queue_list(void);
482 gboolean queue_delete(gchar * uid);
484 /* online.c */
485 gchar *detect_online();
486 void set_online_name(gchar * name);
488 /* permissions.c */
489 gboolean is_ingroup(uid_t uid, gid_t gid);
490 void set_euidgid(gint uid, gint gid, uid_t * old_uid, gid_t * old_gid);
491 void set_identity(uid_t old_uid, gchar * task_name);
493 /* rewrite.c */
494 gboolean set_address_header_domain(header * hdr, gchar * domain);
495 gboolean map_address_header(header * hdr, GList * table);
497 /* route.c */
498 msgout_perhost *create_msgout_perhost(gchar * host);
499 void destroy_msgout_perhost(msgout_perhost * mo_ph);
500 void rewrite_headers(msg_out * msgout, connect_route * route);
501 void rcptlist_with_one_of_hostlist(GList * rcpt_list, GList * host_list, GList **, GList **);
502 void rcptlist_with_addr_is_local(GList * rcpt_list, GList ** p_rcpt_list, GList ** p_non_rcpt_list);
503 gboolean route_strip_msgout(connect_route * route, msg_out * msgout);
504 msg_out *route_prepare_msgout(connect_route * route, msg_out * msgout);
505 GList *route_msgout_list(connect_route * route, GList * msgout_list);
506 gboolean route_is_allowed_return_path(connect_route * route, address * ret_path);
507 gboolean route_is_allowed_mail_local(connect_route * route, address * ret_path);
508 void msg_rcptlist_route(connect_route * route, GList * rcpt_list, GList ** p_rcpt_list, GList ** p_non_rcpt_list);
510 /* tables.c */
511 table_pair *create_pair(gchar * key, gpointer value);
512 table_pair *create_pair_string(gchar * key, gpointer value);
513 table_pair *parse_table_pair(gchar * line, char delim);
514 gpointer *table_find_func(GList * table_list, gchar * key, int (*cmp_func) (const char *, const char *));
515 gpointer *table_find(GList * table_list, gchar * key);
516 gpointer *table_find_case(GList * table_list, gchar * key);
517 gpointer *table_find_fnmatch(GList * table_list, gchar * key);
518 GList *table_read(gchar * fname, gchar delim);
519 void destroy_table(GList * table);
521 /* timeival.c */
522 gint time_interval(gchar * str, gint * pos);
524 /* permissions.c */
525 gboolean is_privileged_user(uid_t uid);
527 /* other things */
529 #define foreach(list, node)\
530 for((node) = g_list_first(list);\
531 (node);\
532 (node) = g_list_next(node))
534 #ifdef ENABLE_DEBUG
535 #define DEBUG(level) if(level <= conf.debug_level)
536 #else
537 /* hopefully the compiler optmizes this away... */
538 #define DEBUG(level) if(0)
539 #endif
541 #define LOG_VERBOSE 0x100
543 #ifndef HAVE_GETLINE
544 #define getline(buf, size, file) getdelim(buf, size, '\n', file)
545 #endif
547 #ifndef HAVE_FDATASYNC
548 #define fdatasync(fd) fsync(fd)
549 #endif
551 #ifndef CONF_DIR
552 #define CONF_DIR "/etc/masqmail"
553 #endif
555 #define CONF_FILE CONF_DIR"/masqmail.conf"
557 #define PIDFILEDIR "/var/run/masqmail/"
559 #ifndef va_copy
560 #ifdef __va_copy
561 #define va_copy(ap1, ap2) __va_copy(ap1, ap2)
562 #else
563 #define va_copy(ap1, ap2) G_VA_COPY(ap1, ap2)
564 #endif
565 #endif
567 /* *BSD needs this: */
568 extern char **environ;