masqmail
view src/masqmail.h @ 310:f10a56dc7481
reworked online_detect to the simpler online_query
Only pipe is supported now. Use
online_query="/bin/cat /path/to/file"
instead of
online_detect=file
online_file=/path/to/file
and
online_query="/path/to/some/script foo"
instead of
online_detect=pipe
online_pipe="/path/to/some/script foo"
See man page masqmail.conf(5) and admin/config-transition.
author | meillo@marmaro.de |
---|---|
date | Sun, 24 Apr 2011 19:14:38 +0200 |
parents | 3e3c280ca5b2 |
children | e230bcd0f1c6 |
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 gchar *protocol;
86 gboolean is_local_net;
87 gboolean last_route;
89 GList *allowed_return_paths;
90 GList *not_allowed_return_paths;
91 GList *allowed_mail_locals;
92 GList *not_allowed_mail_locals;
93 GList *allowed_rcpt_domains;
94 GList *not_allowed_rcpt_domains;
96 interface *mail_host;
97 gchar *wrapper;
98 gboolean connect_error_fail;
100 gchar *helo_name;
101 gboolean do_correct_helo;
102 gboolean instant_helo;
103 gboolean do_pipelining;
105 gchar *set_h_from_domain;
106 gchar *set_h_reply_to_domain;
107 gchar *set_return_path_domain;
109 GList *map_h_from_addresses;
110 GList *map_h_reply_to_addresses;
111 GList *map_h_mail_followup_to_addresses;
112 GList *map_return_path_addresses;
114 gboolean expand_h_sender_domain;
115 gboolean expand_h_sender_address;
117 GList *resolve_list;
119 gchar *auth_name;
120 gchar *auth_login;
121 gchar *auth_secret;
123 gchar *pipe;
125 gboolean pipe_fromline;
126 gboolean pipe_fromhack;
127 } connect_route;
129 typedef struct _masqmail_conf {
130 gint mail_uid;
131 gint mail_gid;
133 gint orig_uid;
134 gint orig_gid;
136 gboolean run_as_user;
138 gchar *mail_dir;
139 gchar *lock_dir;
140 gchar *spool_dir;
141 gchar *log_dir;
143 gint debug_level;
144 gboolean use_syslog;
145 guint log_max_pri;
147 gchar *host_name;
148 GList *local_hosts;
149 GList *local_addresses;
150 GList *not_local_addresses;
151 GList *local_nets;
152 GList *listen_addresses;
154 /* ANSI C defines unsigned long to be at least 32bit
155 i.e. ca. 4GB max; that should be enough. */
156 gulong max_msg_size;
158 gboolean do_save_envelope_to;
160 gboolean defer_all;
161 gboolean do_relay;
163 GList *ident_trusted_nets;
165 gboolean do_queue;
167 gboolean do_verbose;
169 gchar *mbox_default;
170 GList *mbox_users;
171 GList *mda_users;
173 gchar *mda;
174 gboolean mda_fromline;
175 gboolean mda_fromhack;
177 gboolean pipe_fromline;
178 gboolean pipe_fromhack;
180 gchar *alias_file;
181 int (*localpartcmp) (const char *, const char *);
183 GList *local_net_routes;
184 GList *connect_routes; /* list of pairs which point to lists */
186 gchar *online_query;
188 gchar *errmsg_file;
189 gchar *warnmsg_file;
190 GList *warn_intervals;
191 gint max_defer_time;
193 gchar *log_user;
194 } masqmail_conf;
196 extern masqmail_conf conf;
198 typedef struct _table_pair {
199 gchar *key;
200 gpointer *value;
201 } table_pair;
204 /* must match the contents of prot_names[] in accept.c */
205 typedef enum _prot_id {
206 PROT_LOCAL = 0,
207 PROT_SMTP,
208 PROT_ESMTP,
209 PROT_NUM
210 } prot_id;
212 extern gchar *prot_names[];
214 typedef enum _header_id {
215 HEAD_FROM = 0,
216 HEAD_SENDER,
217 HEAD_TO,
218 HEAD_CC,
219 HEAD_BCC,
220 HEAD_DATE,
221 HEAD_MESSAGE_ID,
222 HEAD_REPLY_TO,
223 HEAD_SUBJECT,
224 HEAD_RETURN_PATH,
225 HEAD_ENVELOPE_TO,
226 HEAD_RECEIVED,
227 HEAD_NUM_IDS,
228 HEAD_STATUS,
229 HEAD_UNKNOWN = HEAD_NUM_IDS,
230 HEAD_NONE = -1,
231 } header_id;
233 typedef struct _header_name {
234 gchar *header;
235 header_id id;
236 } header_name;
238 typedef struct _header {
239 header_id id;
240 gchar *header;
241 gchar *value;
242 } header;
245 typedef struct _message {
246 gchar *uid;
248 gchar *received_host;
249 prot_id received_prot;
250 gchar *ident;
251 gint transfer_id; /* for multiple messages per transfer */
253 address *return_path;
254 GList *rcpt_list;
255 GList *non_rcpt_list;
257 GList *hdr_list;
258 GList *data_list;
260 gint data_size;
261 time_t received_time;
262 time_t warned_time;
264 gchar *full_sender_name;
265 } message;
267 typedef struct _msg_out {
268 message *msg;
270 address *return_path;
271 GList *rcpt_list;
273 GList *hdr_list;
274 GList *xtra_hdr_list;
275 } msg_out;
277 typedef struct _msgout_perhost {
278 gchar *host;
279 GList *msgout_list;
280 } msgout_perhost;
282 /* flags for accept() */
283 #define ACC_RCPT_FROM_HEAD 0x08 /* -t option, get rcpts from headers */
284 #define ACC_DOT_IGNORE 0x10 /* a dot on a line itself does not end the message (-oi option) */
285 #define ACC_MAIL_FROM_HEAD 0x40 /* get return path from header */
286 #define ACC_NODOT_RELAX 0x80 /* do not be picky if message ist not terminated by a dot on a line */
287 #define ACC_SAVE_ENVELOPE_TO 0x0100 /* save an existent Envelope-to header as X-Orig-Envelope-to */
289 #define DLVR_LOCAL 0x01
290 #define DLVR_LAN 0x02
291 #define DLVR_ONLINE 0x04
292 #define DLVR_ALL (DLVR_LOCAL|DLVR_LAN|DLVR_ONLINE)
294 /* transport flags */
295 #define MSGSTR_FROMLINE 0x01
296 #define MSGSTR_FROMHACK 0x02
298 typedef enum _accept_error {
299 AERR_OK = 0,
300 AERR_TIMEOUT,
301 AERR_EOF,
302 AERR_OVERFLOW,
303 AERR_SYNTAX,
304 AERR_NOSPOOL,
305 AERR_NORCPT,
306 AERR_SIZE, /* max msg size exeeded (SMTP SIZE) */
307 AERR_UNKNOWN
308 } accept_error;
310 #define BUF_LEN 1024
311 #define MAX_ADDRESS 256
312 #define MAX_DATALINE 4096
314 typedef enum _smtp_cmd_id {
315 SMTP_HELO = 0,
316 SMTP_EHLO,
317 SMTP_MAIL_FROM,
318 SMTP_RCPT_TO,
319 SMTP_DATA,
320 SMTP_QUIT,
321 SMTP_RSET,
322 SMTP_NOOP,
323 SMTP_HELP,
324 SMTP_NUM_IDS,
325 SMTP_EOF = -1,
326 SMTP_ERROR = -2,
327 } smtp_cmd_id;
329 typedef struct _smtp_cmd {
330 smtp_cmd_id id;
331 gchar *cmd;
332 } smtp_cmd;
334 typedef struct _smtp_connection {
335 gchar *remote_host;
337 prot_id prot;
338 gint next_id;
340 gboolean helo_seen;
341 gboolean from_seen;
342 gboolean rcpt_seen;
344 message *msg;
345 } smtp_connection;
347 /* alias.c*/
348 gboolean addr_is_local(address * addr);
349 GList *alias_expand(GList * alias_table, GList * rcpt_list, GList * non_rcpt_list);
351 /* child.c */
352 int child(const char *command);
354 /* conf.c */
355 void init_conf();
356 gboolean read_conf(gchar * filename);
357 connect_route *read_route(gchar * filename, gboolean is_local_net);
358 GList *read_route_list(GList * rf_list, gboolean is_local_net);
359 void destroy_route(connect_route * r);
360 void destroy_route_list(GList * list);
361 connect_route *create_local_route();
363 /* expand.c */
364 GList *var_table_rcpt(GList * var_table, address * rcpt);
365 GList *var_table_msg(GList * var_table, message * msg);
366 GList *var_table_conf(GList * var_table);
367 gint expand(GList * var_list, gchar * format, gchar * result, gint result_len);
369 /* message.c */
370 message *create_message(void);
371 void destroy_message(message * msg);
372 void destroy_msg_list(GList * msg_list);
373 void msg_free_data(message * msg);
374 gint msg_calc_size(message * msg, gboolean is_smtp);
376 msg_out *create_msg_out(message * msg);
377 msg_out *clone_msg_out(msg_out * msgout_orig);
378 GList *create_msg_out_list(GList * msg_list);
379 void destroy_msg_out(msg_out * msgout);
380 void destroy_msg_out_list(GList * msgout_list);
382 /* address.c */
383 address *create_address(gchar * path, gboolean is_rfc821);
384 address *create_address_qualified(gchar * path, gboolean is_rfc821, gchar * domain);
385 address *create_address_pipe(gchar * path);
386 void destroy_address(address * addr);
387 address *copy_modify_address(const address * orig, gchar * l_part, gchar * dom);
388 #define copy_address(addr) copy_modify_address(addr, NULL, NULL)
389 gboolean addr_isequal(address * addr1, address * addr2, int (*cmpfunc) (const char*, const char*));
390 gboolean addr_isequal_parent(address * addr1, address * addr2, int (*cmpfunc) (const char*, const char*));
391 address *addr_find_ancestor(address * addr);
392 gboolean addr_is_delivered_children(address * addr);
393 gboolean addr_is_finished_children(address * addr);
394 gchar *addr_string(address * addr);
395 gint addr_match(address * addr1, address * addr2);
397 /* accept.c */
398 accept_error accept_message(FILE * in, message * msg, guint flags);
399 accept_error accept_message_prepare(message * msg, guint flags);
401 /* header.c */
402 gchar *rec_timestamp();
403 GList *find_header(GList * hdr_list, header_id id, gchar * hdr_str);
404 void header_unfold(header * hdr);
405 void header_fold(header * hdr, unsigned int maxlen);
406 header *create_header(header_id id, gchar * fmt, ...);
407 void destroy_header(header * hdr);
408 header *copy_header(header * hdr);
409 header *get_header(gchar * line);
411 /* smtp_in.c */
412 void smtp_in(FILE * in, FILE * out, gchar * remote_host, gchar * ident);
414 /* listen.c */
415 void listen_port(GList * addr_list, gint qival, char *argv[]);
417 /* parse.c */
418 gboolean split_address(const gchar * path, gchar ** local_part, gchar ** domain, gboolean is_rfc821);
419 gboolean parse_address_rfc822(gchar * string, gchar ** local_begin, gchar ** local_end, gchar ** domain_begin, gchar ** domain_end, gchar ** address_end);
420 gboolean parse_address_rfc821(gchar * string, gchar ** local_begin, gchar ** local_end, gchar ** domain_begin, gchar ** domain_end, gchar ** address_end);
421 address *_create_address(gchar * string, gchar ** end, gboolean is_rfc821);
422 address *create_address_rfc821(gchar * string, gchar ** end);
423 address *create_address_rfc822(gchar * string, gchar ** end);
424 GList *addr_list_append_rfc822(GList * addr_list, gchar * string, gchar * domain);
426 /* connect.c */
427 mxip_addr *connect_hostlist(int *psockfd, gchar * host, guint port, GList * addr_list);
428 mxip_addr *connect_resolvelist(int *psockfd, gchar * host, guint port, GList * res_funcs);
430 /* deliver.c */
431 void msg_rcptlist_local(GList * rcpt_list, GList **, GList **);
432 gboolean deliver_local(msg_out * msgout);
433 gboolean deliver_msglist_host(connect_route * route, GList * msg_list, gchar * host, GList * res_list);
434 gboolean deliver_route_msgout_list(connect_route * route, GList * msgout_list);
435 gboolean deliver_route_msg_list(connect_route * route, GList * msgout_list);
436 gboolean deliver_finish(msg_out * msgout);
437 gboolean deliver_finish_list(GList * msgout_list);
438 gboolean deliver_msg_list(GList * msg_list, guint flags);
439 gboolean deliver(message * msg);
441 /* fail_msg.c */
442 gboolean fail_msg(message * msg, gchar * template, GList * failed_rcpts, gchar * err_fmt, va_list args);
443 gboolean warn_msg(message * msg, gchar * template, GList * failed_rcpts, gchar * err_fmt, va_list args);
445 /* interface.c */
446 gboolean init_sockaddr(struct sockaddr_in *name, interface * iface);
447 int make_server_socket(interface * iface);
449 /* local.c */
450 gboolean append_file(message * msg, GList * hdr_list, gchar * user);
451 gboolean pipe_out(message * msg, GList * hdr_list, address * rcpt, gchar * cmd, guint flags);
453 /* log.c */
454 gchar *ext_strerror(int err);
455 gboolean logopen(void);
456 void logclose(void);
457 void vlogwrite(int pri, const char *fmt, va_list args);
458 void logwrite(int pri, const char *fmt, ...);
459 void debugf(const char *fmt, ...);
460 void vdebugf(const char *fmt, va_list args);
461 void maillog(const char *fmt, ...);
463 /* spool.c */
464 gboolean spool_read_data(message * msg);
465 message *msg_spool_read(gchar * uid, gboolean do_readdata);
466 gboolean spool_write(message * msg, gboolean do_writedata);
467 gboolean spool_lock(gchar * uid);
468 gboolean spool_unlock(gchar * uid);
469 gboolean spool_delete_all(message * msg);
471 /* queue.c */
472 GList *read_queue(gboolean do_readdata);
473 gboolean queue_run(void);
474 gboolean queue_run_online(void);
475 void queue_list(void);
476 gboolean queue_delete(gchar * uid);
478 /* online.c */
479 gchar *online_query();
481 /* permissions.c */
482 gboolean is_ingroup(uid_t uid, gid_t gid);
483 void set_euidgid(gint uid, gint gid, uid_t * old_uid, gid_t * old_gid);
484 void set_identity(uid_t old_uid, gchar * task_name);
486 /* rewrite.c */
487 gboolean set_address_header_domain(header * hdr, gchar * domain);
488 gboolean map_address_header(header * hdr, GList * table);
490 /* route.c */
491 msgout_perhost *create_msgout_perhost(gchar * host);
492 void destroy_msgout_perhost(msgout_perhost * mo_ph);
493 void rewrite_headers(msg_out * msgout, connect_route * route);
494 void split_rcpts(GList* rcpt_list, GList* localnets, GList** rl_local, GList** rl_localnet, GList** rl_others);
495 gboolean route_strip_msgout(connect_route * route, msg_out * msgout);
496 msg_out *route_prepare_msgout(connect_route * route, msg_out * msgout);
497 GList *route_msgout_list(connect_route * route, GList * msgout_list);
498 gboolean route_is_allowed_return_path(connect_route * route, address * ret_path);
499 gboolean route_is_allowed_mail_local(connect_route * route, address * ret_path);
500 void msg_rcptlist_route(connect_route * route, GList * rcpt_list, GList ** p_rcpt_list, GList ** p_non_rcpt_list);
502 /* tables.c */
503 table_pair *create_pair(gchar * key, gpointer value);
504 table_pair *create_pair_string(gchar * key, gpointer value);
505 table_pair *parse_table_pair(gchar * line, char delim);
506 gpointer *table_find_func(GList * table_list, gchar * key, int (*cmp_func) (const char *, const char *));
507 gpointer *table_find(GList * table_list, gchar * key);
508 gpointer *table_find_case(GList * table_list, gchar * key);
509 gpointer *table_find_fnmatch(GList * table_list, gchar * key);
510 GList *table_read(gchar * fname, gchar delim);
511 void destroy_table(GList * table);
513 /* timeival.c */
514 gint time_interval(gchar * str);
516 /* permissions.c */
517 gboolean is_privileged_user(uid_t uid);
519 /* other things */
521 #define foreach(list, node)\
522 for((node) = g_list_first(list);\
523 (node);\
524 (node) = g_list_next(node))
526 #ifdef ENABLE_DEBUG
527 #define DEBUG(level) if(level <= conf.debug_level)
528 #else
529 /* hopefully the compiler optmizes this away... */
530 #define DEBUG(level) if(0)
531 #endif
533 #define LOG_VERBOSE 0x100
535 #ifndef HAVE_GETLINE
536 #define getline(buf, size, file) getdelim(buf, size, '\n', file)
537 #endif
539 #ifndef HAVE_FDATASYNC
540 #define fdatasync(fd) fsync(fd)
541 #endif
543 #ifndef CONF_DIR
544 #define CONF_DIR "/etc/masqmail"
545 #endif
547 #define CONF_FILE CONF_DIR"/masqmail.conf"
549 #define PIDFILEDIR "/var/run/masqmail/"
551 #ifndef va_copy
552 #ifdef __va_copy
553 #define va_copy(ap1, ap2) __va_copy(ap1, ap2)
554 #else
555 #define va_copy(ap1, ap2) G_VA_COPY(ap1, ap2)
556 #endif
557 #endif
559 /* *BSD needs this: */
560 extern char **environ;