masqmail-0.2

view src/masqmail.h @ 107:9104234a56a5

never create headers from recipients because it could disclose private information
author meillo@marmaro.de
date Tue, 29 Jun 2010 10:45:33 +0200
parents 1f0d63713a1c
children db52c64acaa2
line source
1 /* MasqMail
2 Copyright (C) 1999-2001 Oliver Kurth
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18 #include <config.h>
20 #include <stdio.h>
21 #include <stdarg.h>
22 #include <errno.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <ctype.h>
26 #include <unistd.h>
27 #include <pwd.h>
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <time.h>
32 #include <sys/time.h>
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
35 #include <netdb.h>
36 #include <syslog.h>
37 #include <signal.h>
38 #include <fcntl.h>
40 #include <glib.h>
42 #ifdef ENABLE_IDENT
43 #include "libident/ident.h"
44 #endif
46 #include "lookup.h"
48 typedef struct _interface {
49 gchar *address;
50 gint port;
51 } interface;
53 #define ADDR_FLAG_DELIVERED 0x01
54 #define ADDR_FLAG_DEFERED 0x02
55 #define ADDR_FLAG_FAILED 0x04
56 #define ADDR_FLAG_LAST_ROUTE 0x40
57 #define ADDR_FLAG_NOEXPAND 0x80
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 do_pipelining;
104 gchar *set_h_from_domain;
105 gchar *set_h_reply_to_domain;
106 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;
113 gboolean expand_h_sender_domain;
114 gboolean expand_h_sender_address;
116 GList *resolve_list;
118 gchar *auth_name;
119 gchar *auth_login;
120 gchar *auth_secret;
122 gchar *pop3_login;
124 gchar *pipe;
126 gboolean pipe_fromline;
127 gboolean pipe_fromhack;
128 } connect_route;
130 typedef struct _get_conf {
131 gchar *protocol;
132 gchar *server_name;
133 guint server_port;
134 gchar *wrapper;
135 gchar *login_user;
136 gchar *login_pass;
137 address *address;
138 address *return_path;
139 gboolean do_keep;
140 gboolean do_uidl;
141 gboolean do_uidl_dele;
142 gint max_size;
143 gboolean max_size_delete;
144 gint max_count;
146 GList *resolve_list;
148 } get_conf;
150 typedef struct _masqmail_conf {
151 gint mail_uid;
152 gint mail_gid;
154 gint orig_uid;
155 gint orig_gid;
157 gboolean run_as_user;
159 gchar *mail_dir;
160 gchar *lock_dir;
161 gchar *spool_dir;
162 gchar *log_dir;
164 gint debug_level;
165 gboolean use_syslog;
166 guint log_max_pri;
168 gchar *host_name;
169 GList *local_hosts;
170 GList *local_addresses;
171 GList *not_local_addresses;
172 GList *local_nets;
173 GList *listen_addresses;
175 guint remote_port;
177 gboolean do_save_envelope_to;
179 gboolean defer_all;
180 gboolean do_relay;
182 GList *ident_trusted_nets;
184 gboolean do_queue;
186 gboolean do_verbose;
188 gchar *mbox_default;
189 GList *mbox_users;
190 GList *mda_users;
191 GList *maildir_users;
193 gchar *mda;
194 gboolean mda_fromline;
195 gboolean mda_fromhack;
197 gboolean pipe_fromline;
198 gboolean pipe_fromhack;
200 gchar *alias_file;
201 int (*alias_local_cmp) (const char *, const char *);
203 GList *local_net_routes;
204 GList *connect_routes; /* list of pairs which point to lists */
206 gchar *online_detect;
207 gchar *online_file;
208 gchar *online_pipe;
209 interface *mserver_iface;
211 GList *get_names;
212 GList *online_gets; /* list of pairs which point to lists */
214 gchar *errmsg_file;
215 gchar *warnmsg_file;
216 GList *warn_intervals;
217 gint max_defer_time;
219 gchar *log_user;
220 } masqmail_conf;
222 extern masqmail_conf conf;
224 typedef struct _table_pair {
225 gchar *key;
226 gpointer *value;
227 } table_pair;
230 typedef enum _prot_id {
231 PROT_LOCAL = 0,
232 PROT_BSMTP,
233 PROT_SMTP,
234 PROT_ESMTP,
235 PROT_POP3,
236 PROT_APOP,
237 PROT_NUM
238 } prot_id;
240 extern gchar *prot_names[];
242 typedef enum _header_id {
243 HEAD_FROM = 0,
244 HEAD_SENDER,
245 HEAD_TO,
246 HEAD_CC,
247 HEAD_BCC,
248 HEAD_DATE,
249 HEAD_MESSAGE_ID,
250 HEAD_REPLY_TO,
251 HEAD_SUBJECT,
252 HEAD_RETURN_PATH,
253 HEAD_ENVELOPE_TO,
254 HEAD_RECEIVED,
255 HEAD_NUM_IDS,
256 HEAD_STATUS,
257 HEAD_UNKNOWN = HEAD_NUM_IDS,
258 HEAD_NONE = -1,
259 } header_id;
261 typedef struct _header_name {
262 gchar *header;
263 header_id id;
264 } header_name;
266 typedef struct _header {
267 header_id id;
268 gchar *header;
269 gchar *value;
270 } header;
273 typedef struct _message {
274 gchar *uid;
276 gchar *received_host;
277 prot_id received_prot;
278 gchar *ident;
279 gint transfer_id; /* for multiple messages per transfer */
281 address *return_path;
282 GList *rcpt_list;
283 GList *non_rcpt_list;
285 GList *hdr_list;
286 GList *data_list;
288 gint data_size;
289 time_t received_time;
290 time_t warned_time;
292 gchar *full_sender_name;
293 } message;
295 typedef struct _msg_out {
296 message *msg;
298 address *return_path;
299 GList *rcpt_list;
301 GList *hdr_list;
302 GList *xtra_hdr_list;
303 } msg_out;
305 typedef struct _msgout_perhost {
306 gchar *host;
307 GList *msgout_list;
308 } msgout_perhost;
310 /* flags for accept() */
311 /*#define ACC_LOCAL 0x01 (we better use received_host == NULL) */
312 #define ACC_DEL_RCPTS 0x02 /* -t option, delete rcpts */
313 #define ACC_RCPT_FROM_HEAD 0x08 /* -t option, get rcpts from headers */
314 #define ACC_NODOT_TERM 0x10 /* a dot on a line itself does not end the message (-oi option) */
315 #define ACC_MAIL_FROM_HEAD 0x40 /* get return path from header */
316 #define ACC_NODOT_RELAX 0x80 /* do not be picky if message ist not terminated by a dot on a line */
317 #define ACC_SAVE_ENVELOPE_TO 0x0100 /* save an existent Envelope-to header as X-Orig-Envelope-to */
319 #define DLVR_LOCAL 0x01
320 #define DLVR_LAN 0x02
321 #define DLVR_ONLINE 0x04
322 #define DLVR_ALL (DLVR_LOCAL|DLVR_LAN|DLVR_ONLINE)
324 /* transport flags */
325 #define MSGSTR_FROMLINE 0x01
326 #define MSGSTR_FROMHACK 0x02
328 typedef enum _accept_error {
329 AERR_OK = 0,
330 AERR_TIMEOUT,
331 AERR_EOF,
332 AERR_OVERFLOW,
333 AERR_SYNTAX,
334 AERR_NOSPOOL,
335 AERR_NORCPT,
336 AERR_UNKNOWN
337 } accept_error;
339 #define BUF_LEN 1024
340 #define MAX_ADDRESS 256
341 #define MAX_DATALINE 4096
343 typedef enum _smtp_cmd_id {
344 SMTP_HELO = 0,
345 SMTP_EHLO,
346 SMTP_MAIL_FROM,
347 SMTP_RCPT_TO,
348 SMTP_DATA,
349 SMTP_QUIT,
350 SMTP_RSET,
351 SMTP_NOOP,
352 SMTP_HELP,
353 SMTP_NUM_IDS,
354 SMTP_EOF = -1,
355 SMTP_ERROR = -2,
356 } smtp_cmd_id;
358 typedef struct _smtp_cmd {
359 smtp_cmd_id id;
360 gchar *cmd;
361 } smtp_cmd;
363 typedef struct _smtp_connection {
364 gchar *remote_host;
366 prot_id prot;
367 gint next_id;
369 gboolean helo_seen;
370 gboolean from_seen;
371 gboolean rcpt_seen;
373 message *msg;
374 } smtp_connection;
376 /* alias.c*/
377 gboolean addr_is_local(address * addr);
378 GList *alias_expand(GList * alias_table, GList * rcpt_list, GList * non_rcpt_list);
380 /* child.c */
381 int child(const char *command);
383 /* conf.c */
384 void init_conf();
385 gboolean read_conf(gchar * filename);
386 connect_route *read_route(gchar * filename, gboolean is_local_net);
387 GList *read_route_list(GList * rf_list, gboolean is_local_net);
388 void destroy_route(connect_route * r);
389 void destroy_route_list(GList * list);
390 get_conf *read_get_conf(gchar * filename);
391 void destroy_get_conf(get_conf * gc);
392 connect_route *create_local_route();
394 /* expand.c */
395 GList *var_table_rcpt(GList * var_table, address * rcpt);
396 GList *var_table_msg(GList * var_table, message * msg);
397 GList *var_table_conf(GList * var_table);
398 gint expand(GList * var_list, gchar * format, gchar * result, gint result_len);
400 /* message.c */
401 message *create_message(void);
402 void destroy_message(message * msg);
403 void destroy_msg_list(GList * msg_list);
404 void msg_free_data(message * msg);
405 gint msg_calc_size(message * msg, gboolean is_smtp);
407 msg_out *create_msg_out(message * msg);
408 msg_out *clone_msg_out(msg_out * msgout_orig);
409 GList *create_msg_out_list(GList * msg_list);
410 void destroy_msg_out(msg_out * msgout);
411 void destroy_msg_out_list(GList * msgout_list);
413 /* address.c */
414 address *create_address(gchar * path, gboolean is_rfc821);
415 address *create_address_qualified(gchar * path, gboolean is_rfc821, gchar * domain);
416 address *create_address_pipe(gchar * path);
417 void destroy_address(address * addr);
418 address *copy_modify_address(const address * orig, gchar * l_part, gchar * dom);
419 #define copy_address(addr) copy_modify_address(addr, NULL, NULL)
420 gboolean addr_isequal(address * addr1, address * addr2);
421 gboolean addr_isequal_parent(address * addr1, address * addr2);
422 address *addr_find_ancestor(address * addr);
423 gboolean addr_is_delivered_children(address * addr);
424 gboolean addr_is_finished_children(address * addr);
425 gchar *addr_string(address * addr);
426 gint addr_match(address * addr1, address * addr2);
428 /* accept.c */
429 accept_error accept_message(FILE * in, message * msg, guint flags);
430 accept_error accept_message_prepare(message * msg, guint flags);
432 /* header.c */
433 gchar *rec_timestamp();
434 GList *find_header(GList * hdr_list, header_id id, gchar * hdr_str);
435 void header_unfold(header * hdr);
436 void header_fold(header * hdr);
437 header *create_header(header_id id, gchar * fmt, ...);
438 void destroy_header(header * hdr);
439 header *copy_header(header * hdr);
440 header *get_header(gchar * line);
442 /* smtp_in.c */
443 void smtp_in(FILE * in, FILE * out, gchar * remote_host, gchar * ident);
445 /* listen.c */
446 void listen_port(GList * addr_list, gint qival, char *argv[]);
448 /* parse.c */
449 gboolean split_address(const gchar * path, gchar ** local_part, gchar ** domain, gboolean is_rfc821);
450 gboolean parse_address_rfc822(gchar * string, gchar ** local_begin, gchar ** local_end, gchar ** domain_begin, gchar ** domain_end, gchar ** address_end);
451 gboolean parse_address_rfc821(gchar * string, gchar ** local_begin, gchar ** local_end, gchar ** domain_begin, gchar ** domain_end, gchar ** address_end);
452 address *_create_address(gchar * string, gchar ** end, gboolean is_rfc821);
453 address *create_address_rfc821(gchar * string, gchar ** end);
454 address *create_address_rfc822(gchar * string, gchar ** end);
455 GList *addr_list_append_rfc822(GList * addr_list, gchar * string, gchar * domain);
456 gboolean addr_isequal(address * addr1, address * addr2);
458 /* connect.c */
459 mxip_addr *connect_hostlist(int *psockfd, gchar * host, guint port, GList * addr_list);
460 mxip_addr *connect_resolvelist(int *psockfd, gchar * host, guint port, GList * res_funcs);
462 /* deliver.c */
463 void msg_rcptlist_local(GList * rcpt_list, GList **, GList **);
464 gboolean deliver_local(msg_out * msgout);
465 gboolean deliver_msglist_host(connect_route * route, GList * msg_list, gchar * host, GList * res_list);
466 gboolean deliver_route_msgout_list(connect_route * route, GList * msgout_list);
467 gboolean deliver_route_msg_list(connect_route * route, GList * msgout_list);
468 gboolean deliver_finish(msg_out * msgout);
469 gboolean deliver_finish_list(GList * msgout_list);
470 gboolean deliver_msg_list(GList * msg_list, guint flags);
471 gboolean deliver(message * msg);
473 /* fail_msg.c */
474 gboolean fail_msg(message * msg, gchar * template, GList * failed_rcpts, gchar * err_fmt, va_list args);
475 gboolean warn_msg(message * msg, gchar * template, GList * failed_rcpts, gchar * err_fmt, va_list args);
477 /* get.c */
478 gboolean get_from_file(gchar * fname);
479 gboolean get_from_name(gchar * name);
480 gboolean get_all(void);
481 void get_online(void);
482 void get_daemon(gint gival, char *argv[]);
483 gboolean pop_before_smtp(gchar * fname);
485 /* interface.c */
486 gboolean init_sockaddr(struct sockaddr_in *name, interface * iface);
487 int make_server_socket(interface * iface);
489 /* local.c */
490 gboolean append_file(message * msg, GList * hdr_list, gchar * user);
491 gboolean maildir_out(message * msg, GList * hdr_list, gchar * user, guint flags);
492 gboolean pipe_out(message * msg, GList * hdr_list, address * rcpt, gchar * cmd, guint flags);
494 /* log.c */
495 gchar *ext_strerror(int err);
496 gboolean logopen(void);
497 void logclose(void);
498 void vlogwrite(int pri, const char *fmt, va_list args);
499 void logwrite(int pri, const char *fmt, ...);
500 void debugf(const char *fmt, ...);
501 void vdebugf(const char *fmt, va_list args);
502 void maillog(const char *fmt, ...);
504 /* spool.c */
505 gboolean spool_read_data(message * msg);
506 gboolean spool_read_data(message * msg);
507 message *msg_spool_read(gchar * uid, gboolean do_readdata);
508 gboolean spool_write(message * msg, gboolean do_writedata);
509 gboolean spool_lock(gchar * uid);
510 gboolean spool_unlock(gchar * uid);
511 gboolean spool_delete_all(message * msg);
513 /* queue.c */
514 GList *read_queue(gboolean do_readdata);
515 gboolean queue_run(void);
516 gboolean queue_run_online(void);
517 void queue_list(void);
518 gboolean queue_delete(gchar * uid);
520 /* online.c */
521 gchar *detect_online();
522 void set_online_name(gchar * name);
524 /* permissions.c */
525 gboolean is_ingroup(uid_t uid, gid_t gid);
526 void set_euidgid(gint uid, gint gid, uid_t * old_uid, gid_t * old_gid);
527 void set_identity(uid_t old_uid, gchar * task_name);
529 /* rewrite.c */
530 gboolean set_address_header_domain(header * hdr, gchar * domain);
531 gboolean map_address_header(header * hdr, GList * table);
533 /* route.c */
534 msgout_perhost *create_msgout_perhost(gchar * host);
535 void destroy_msgout_perhost(msgout_perhost * mo_ph);
536 void rewrite_headers(msg_out * msgout, connect_route * route);
537 void rcptlist_with_one_of_hostlist(GList * rcpt_list, GList * host_list, GList **, GList **);
538 void rcptlist_with_addr_is_local(GList * rcpt_list, GList ** p_rcpt_list, GList ** p_non_rcpt_list);
539 gboolean route_strip_msgout(connect_route * route, msg_out * msgout);
540 msg_out *route_prepare_msgout(connect_route * route, msg_out * msgout);
541 GList *route_msgout_list(connect_route * route, GList * msgout_list);
542 gboolean route_is_allowed_return_path(connect_route * route, address * ret_path);
543 gboolean route_is_allowed_mail_local(connect_route * route, address * ret_path);
544 void msg_rcptlist_route(connect_route * route, GList * rcpt_list, GList ** p_rcpt_list, GList ** p_non_rcpt_list);
546 /* tables.c */
547 table_pair *create_pair(gchar * key, gpointer value);
548 table_pair *create_pair_string(gchar * key, gpointer value);
549 table_pair *parse_table_pair(gchar * line, char delim);
550 gpointer *table_find_func(GList * table_list, gchar * key, int (*cmp_func) (const char *, const char *));
551 gpointer *table_find(GList * table_list, gchar * key);
552 gpointer *table_find_case(GList * table_list, gchar * key);
553 gpointer *table_find_fnmatch(GList * table_list, gchar * key);
554 GList *table_read(gchar * fname, gchar delim);
555 void destroy_table(GList * table);
557 /* timeival.c */
558 gint time_interval(gchar * str, gint * pos);
560 /* permissions.c */
561 gboolean is_privileged_user(uid_t uid);
563 /* other things */
565 #define foreach(list, node)\
566 for((node) = g_list_first(list);\
567 (node);\
568 (node) = g_list_next(node))
570 #ifdef ENABLE_DEBUG
571 #define DEBUG(level) if(level <= conf.debug_level)
572 #else
573 /* hopefully the compiler optmizes this away... */
574 #define DEBUG(level) if(0)
575 #endif
577 #define LOG_VERBOSE 0x100
579 #ifndef HAVE_GETLINE
580 #define getline(buf, size, file) getdelim(buf, size, '\n', file)
581 #endif
583 #ifndef HAVE_FDATASYNC
584 #define fdatasync(fd) fsync(fd)
585 #endif
587 #ifndef CONF_DIR
588 #define CONF_DIR "/etc/masqmail"
589 #endif
591 #define CONF_FILE CONF_DIR"/masqmail.conf"
593 #define PIDFILEDIR "/var/run/masqmail/"
595 #ifndef va_copy
596 #ifdef __va_copy
597 #define va_copy(ap1, ap2) __va_copy(ap1, ap2)
598 #else
599 #define va_copy(ap1, ap2) G_VA_COPY(ap1, ap2)
600 #endif
601 #endif
603 /* *BSD needs this: */
604 extern char **environ;