masqmail-0.2
view src/masqmail.h @ 3:8c55886cacd8
man pages will be maintained in troff now
author | meillo@marmaro.de |
---|---|
date | Fri, 26 Sep 2008 21:40:10 +0200 |
parents | |
children | 26e34ae9a3e3 |
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
49 struct _interface
50 {
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
59 #define ADDR_FLAG_NOEXPAND 0x80
61 typedef struct _address
62 {
63 gchar *address;
64 gchar *local_part;
65 gchar *domain;
66 gint flags;
67 GList *children;
68 struct _address *parent;
69 } address;
71 #define addr_mark_delivered(addr) { addr->flags |= ADDR_FLAG_DELIVERED; }
72 #define addr_unmark_delivered(addr) { addr->flags &= ~ADDR_FLAG_DELIVERED; }
73 #define addr_is_delivered(addr) ((addr->flags & ADDR_FLAG_DELIVERED) != 0 )
75 #define addr_mark_defered(addr) { addr->flags |= ADDR_FLAG_DEFERED; }
76 #define addr_unmark_defered(addr) { addr->flags &= ~ADDR_FLAG_DEFERED; }
77 #define addr_is_defered(addr) ((addr->flags & ADDR_FLAG_DEFERED) != 0 )
79 #define addr_mark_failed(addr) { addr->flags |= ADDR_FLAG_FAILED; }
80 #define addr_unmark_failed(addr) { addr->flags &= ~ADDR_FLAG_FAILED; }
81 #define addr_is_failed(addr) ((addr->flags & ADDR_FLAG_FAILED) != 0 )
83 typedef
84 struct _connect_route
85 {
86 gchar *name;
87 gchar *filename;
89 gchar *protocol;
91 gboolean is_local_net;
92 gboolean last_route;
94 GList *allowed_return_paths;
95 GList *not_allowed_return_paths;
96 GList *allowed_mail_locals;
97 GList *not_allowed_mail_locals;
98 GList *allowed_rcpt_domains;
99 GList *not_allowed_rcpt_domains;
101 interface *mail_host;
102 gchar *wrapper;
103 gboolean connect_error_fail;
105 gchar *helo_name;
106 gboolean do_correct_helo;
107 gboolean do_pipelining;
109 gchar *set_h_from_domain;
110 gchar *set_h_reply_to_domain;
111 gchar *set_return_path_domain;
113 GList *map_h_from_addresses;
114 GList *map_h_reply_to_addresses;
115 GList *map_h_mail_followup_to_addresses;
116 GList *map_return_path_addresses;
118 gboolean expand_h_sender_domain;
119 gboolean expand_h_sender_address;
121 GList *resolve_list;
123 gchar *auth_name;
124 gchar *auth_login;
125 gchar *auth_secret;
127 gchar *pop3_login;
129 gchar *pipe;
131 gboolean pipe_fromline;
132 gboolean pipe_fromhack;
133 } connect_route;
135 typedef struct _get_conf
136 {
137 gchar *protocol;
138 gchar *server_name;
139 guint server_port;
140 gchar *wrapper;
141 gchar *login_user;
142 gchar *login_pass;
143 address *address;
144 address *return_path;
145 gboolean do_keep;
146 gboolean do_uidl;
147 gboolean do_uidl_dele;
148 gint max_size;
149 gboolean max_size_delete;
150 gint max_count;
152 GList *resolve_list;
154 } get_conf;
156 typedef
157 struct _masqmail_conf
158 {
159 gint mail_uid;
160 gint mail_gid;
162 gint orig_uid;
163 gint orig_gid;
165 gboolean run_as_user;
167 gchar *mail_dir;
168 gchar *lock_dir;
169 gchar *spool_dir;
170 gchar *log_dir;
172 gint debug_level;
173 gboolean use_syslog;
174 guint log_max_pri;
176 gchar *host_name;
177 GList *local_hosts;
178 GList *local_addresses;
179 GList *not_local_addresses;
180 GList *local_nets;
181 GList *listen_addresses;
183 guint remote_port;
185 gboolean do_save_envelope_to;
187 gboolean defer_all;
188 gboolean do_relay;
190 GList *ident_trusted_nets;
192 gboolean do_queue;
194 gboolean do_verbose;
196 gchar *mbox_default;
197 GList *mbox_users;
198 GList *mda_users;
199 GList *maildir_users;
201 gchar *mda;
202 gboolean mda_fromline;
203 gboolean mda_fromhack;
205 gboolean pipe_fromline;
206 gboolean pipe_fromhack;
208 gchar *alias_file;
209 int (*alias_local_cmp)(const char *, const char *);
211 GList *local_net_routes;
212 GList *connect_routes; /* list of pairs which point to lists */
214 gchar *online_detect;
215 gchar *online_file;
216 gchar *online_pipe;
217 interface *mserver_iface;
219 GList *get_names;
220 GList *online_gets; /* list of pairs which point to lists */
222 gchar *errmsg_file;
223 gchar *warnmsg_file;
224 GList *warn_intervals;
225 gint max_defer_time;
227 gchar *log_user;
228 } masqmail_conf;
230 extern masqmail_conf conf;
232 typedef
233 struct _table_pair
234 {
235 gchar *key;
236 gpointer *value;
237 } table_pair;
240 typedef
241 enum _prot_id
242 {
243 PROT_LOCAL = 0,
244 PROT_BSMTP,
245 PROT_SMTP,
246 PROT_ESMTP,
247 PROT_POP3,
248 PROT_APOP,
249 PROT_NUM
250 }prot_id;
252 extern gchar *prot_names[];
254 typedef
255 enum _header_id
256 {
257 HEAD_FROM = 0,
258 HEAD_SENDER,
259 HEAD_TO,
260 HEAD_CC,
261 HEAD_BCC,
262 HEAD_DATE,
263 HEAD_MESSAGE_ID,
264 HEAD_REPLY_TO,
265 HEAD_SUBJECT,
266 HEAD_RETURN_PATH,
267 HEAD_ENVELOPE_TO,
268 HEAD_RECEIVED,
269 HEAD_NUM_IDS,
270 HEAD_STATUS,
271 HEAD_UNKNOWN = HEAD_NUM_IDS,
272 HEAD_NONE = -1,
273 }header_id;
275 typedef
276 struct _header_name
277 {
278 gchar *header;
279 header_id id;
280 }header_name;
282 typedef
283 struct _header
284 {
285 header_id id;
286 gchar *header;
287 gchar *value;
288 }header;
291 typedef
292 struct _message
293 {
294 gchar *uid;
296 gchar *received_host;
297 prot_id received_prot;
298 gchar *ident;
299 gint transfer_id; /* for multiple messages per transfer */
301 address *return_path;
302 GList *rcpt_list;
303 GList *non_rcpt_list;
305 GList *hdr_list;
306 GList *data_list;
308 gint data_size;
309 time_t received_time;
310 time_t warned_time;
312 gchar *full_sender_name;
313 }message;
315 typedef
316 struct _msg_out
317 {
318 message *msg;
320 address *return_path;
321 GList *rcpt_list;
323 GList *hdr_list;
324 GList *xtra_hdr_list;
325 }msg_out;
327 typedef
328 struct _msgout_perhost
329 {
330 gchar *host;
331 GList *msgout_list;
332 } msgout_perhost;
334 /* flags for accept() */
335 /*#define ACC_LOCAL 0x01 (we better use received_host == NULL) */
336 #define ACC_HEAD_FROM_RCPT 0x01 /* create To: Header from rcpt_list (cmd line) */
337 #define ACC_DEL_RCPTS 0x02 /* -t option, delete rcpts */
338 #define ACC_DEL_BCC 0x04 /* -t option, delete Bcc header */
339 #define ACC_RCPT_FROM_HEAD 0x08 /* -t option, get rcpts from headers */
340 #define ACC_NODOT_TERM 0x10 /* a dot on a line itself does not end
341 the message (-oi option) */
342 #define ACC_NO_RECVD_HDR 0x20 /* do not create a Received: header */
343 #define ACC_MAIL_FROM_HEAD 0x40 /* get return path from header */
344 #define ACC_NODOT_RELAX 0x80 /* do not be picky if message ist not terminated by a dot on a line */
345 #define ACC_SAVE_ENVELOPE_TO 0x0100 /* save an existent Envelope-to header as X-Orig-Envelope-to */
347 #define DLVR_LOCAL 0x01
348 #define DLVR_LAN 0x02
349 #define DLVR_ONLINE 0x04
350 #define DLVR_ALL (DLVR_LOCAL|DLVR_LAN|DLVR_ONLINE)
352 /* transport flags */
353 #define MSGSTR_FROMLINE 0x01
354 #define MSGSTR_FROMHACK 0x02
356 typedef
357 enum _accept_error
358 {
359 AERR_OK = 0,
360 AERR_TIMEOUT,
361 AERR_EOF,
362 AERR_OVERFLOW,
363 AERR_SYNTAX,
364 AERR_NOSPOOL,
365 AERR_NORCPT,
366 AERR_UNKNOWN
367 }accept_error;
369 #define BUF_LEN 1024
370 #define MAX_ADDRESS 256
371 #define MAX_DATALINE 4096
373 typedef
374 enum _smtp_cmd_id
375 {
376 SMTP_HELO = 0,
377 SMTP_EHLO,
378 SMTP_MAIL_FROM,
379 SMTP_RCPT_TO,
380 SMTP_DATA,
381 SMTP_QUIT,
382 SMTP_RSET,
383 SMTP_NOOP,
384 SMTP_HELP,
385 SMTP_NUM_IDS,
386 SMTP_EOF = -1,
387 SMTP_ERROR = -2,
388 } smtp_cmd_id;
390 typedef
391 struct _smtp_cmd
392 {
393 smtp_cmd_id id;
394 gchar *cmd;
395 } smtp_cmd;
397 typedef
398 struct _smtp_connection
399 {
400 gchar *remote_host;
402 prot_id prot;
403 gint next_id;
405 gboolean helo_seen;
406 gboolean from_seen;
407 gboolean rcpt_seen;
409 message *msg;
410 }smtp_connection;
412 /* alias.c*/
413 gboolean addr_is_local(address *addr);
414 GList *alias_expand(GList *alias_table, GList *rcpt_list, GList *non_rcpt_list);
416 /* child.c */
417 int child(const char *command);
419 /* conf.c */
420 void init_conf();
421 gboolean read_conf(gchar *filename);
422 connect_route *read_route(gchar *filename, gboolean is_local_net);
423 GList *read_route_list(GList *rf_list, gboolean is_local_net);
424 void destroy_route(connect_route *r);
425 void destroy_route_list(GList *list);
426 get_conf *read_get_conf(gchar *filename);
427 void destroy_get_conf(get_conf *gc);
428 connect_route *create_local_route();
430 /* expand.c */
431 GList *var_table_rcpt(GList *var_table, address *rcpt);
432 GList *var_table_msg(GList *var_table, message *msg);
433 GList *var_table_conf(GList *var_table);
434 gint expand(GList *var_list, gchar *format, gchar *result, gint result_len);
436 /* message.c */
437 message *create_message(void);
438 void destroy_message(message *msg);
439 void destroy_msg_list(GList *msg_list);
440 void msg_free_data(message *msg);
441 gint msg_calc_size(message *msg, gboolean is_smtp);
443 msg_out *create_msg_out(message *msg);
444 msg_out *clone_msg_out(msg_out *msgout_orig);
445 GList *create_msg_out_list(GList *msg_list);
446 void destroy_msg_out(msg_out *msgout);
447 void destroy_msg_out_list(GList *msgout_list);
449 /* address.c */
450 address *create_address(gchar *path, gboolean is_rfc821);
451 address *create_address_qualified(gchar *path, gboolean is_rfc821,
452 gchar *domain);
453 address *create_address_pipe(gchar *path);
454 void destroy_address(address *addr);
455 address *copy_modify_address(const address *orig, gchar *l_part, gchar *dom);
456 #define copy_address(addr) copy_modify_address(addr, NULL, NULL)
457 gboolean addr_isequal(address *addr1, address *addr2);
458 gboolean addr_isequal_parent(address *addr1, address *addr2);
459 address *addr_find_ancestor(address *addr);
460 gboolean addr_is_delivered_children(address *addr);
461 gboolean addr_is_finished_children(address *addr);
462 gchar *addr_string(address *addr);
463 gint addr_match(address *addr1, address *addr2);
465 /* accept.c */
466 accept_error accept_message(FILE *in, message *msg,
467 guint flags);
468 accept_error accept_message_prepare(message *msg, guint flags);
470 /* header.c */
471 gchar *rec_timestamp();
472 GList *find_header(GList *hdr_list, header_id id, gchar *hdr_str);
473 void header_unfold(header *hdr);
474 void header_fold(header *hdr);
475 header *create_header(header_id id, gchar *fmt, ...);
476 void destroy_header(header *hdr);
477 header *copy_header(header *hdr);
478 header *get_header(gchar *line);
480 /* smtp_in.c */
481 void smtp_in(FILE *in, FILE *out, gchar *remote_host, gchar *ident);
483 /* listen.c */
484 void listen_port(GList *addr_list, gint qival, char *argv[]);
486 /* parse.c */
487 gboolean split_address(const gchar *path, gchar **local_part, gchar **domain,
488 gboolean is_rfc821);
489 gboolean parse_address_rfc822(gchar *string,
490 gchar **local_begin, gchar **local_end,
491 gchar **domain_begin, gchar **domain_end,
492 gchar **address_end);
493 gboolean parse_address_rfc821(gchar *string,
494 gchar **local_begin, gchar **local_end,
495 gchar **domain_begin, gchar **domain_end,
496 gchar **address_end);
497 address *_create_address(gchar *string, gchar **end, gboolean is_rfc821);
498 address *create_address_rfc821(gchar *string, gchar **end);
499 address *create_address_rfc822(gchar *string, gchar **end);
500 GList *addr_list_append_rfc822(GList *addr_list, gchar *string, gchar *domain);
501 gboolean addr_isequal(address *addr1, address *addr2);
503 /* connect.c */
504 mxip_addr *connect_hostlist(int *psockfd, gchar *host, guint port,
505 GList *addr_list);
506 mxip_addr *connect_resolvelist(int *psockfd, gchar *host, guint port,
507 GList *res_funcs);
509 /* deliver.c */
510 void msg_rcptlist_local(GList *rcpt_list, GList **, GList **);
511 gboolean deliver_local(msg_out *msgout);
512 gboolean deliver_msglist_host(connect_route *route, GList *msg_list, gchar *host, GList *res_list);
513 gboolean deliver_route_msgout_list(connect_route *route, GList *msgout_list);
514 gboolean deliver_route_msg_list(connect_route *route, GList *msgout_list);
515 gboolean deliver_finish(msg_out *msgout);
516 gboolean deliver_finish_list(GList *msgout_list);
517 gboolean deliver_msg_list(GList *msg_list, guint flags);
518 gboolean deliver(message *msg);
520 /* fail_msg.c */
521 gboolean fail_msg(message *msg, gchar *template,
522 GList *failed_rcpts, gchar *err_fmt, va_list args);
523 gboolean warn_msg(message *msg, gchar *template,
524 GList *failed_rcpts, gchar *err_fmt, va_list args);
526 /* get.c */
527 gboolean get_from_file(gchar *fname);
528 gboolean get_from_name(gchar *name);
529 gboolean get_all(void);
530 void get_online(void);
531 void get_daemon(gint gival, char *argv[]);
532 gboolean pop_before_smtp(gchar *fname);
534 /* interface.c */
535 gboolean init_sockaddr(struct sockaddr_in *name, interface *iface);
536 int make_server_socket(interface *iface);
538 /* local.c */
539 gboolean append_file(message *msg, GList *hdr_list, gchar *user);
540 gboolean maildir_out(message *msg, GList *hdr_list, gchar *user, guint flags);
541 gboolean pipe_out(message *msg, GList *hdr_list, address *rcpt, gchar *cmd, guint flags);
543 /* log.c */
544 gchar *ext_strerror(int err);
545 gboolean logopen(void);
546 void logclose(void);
547 void vlogwrite(int pri, const char *fmt, va_list args);
548 void logwrite(int pri, const char *fmt, ...);
549 void debugf(const char *fmt, ...);
550 void vdebugf(const char *fmt, va_list args);
551 void maillog(const char *fmt, ...);
553 /* spool.c */
554 gboolean spool_read_data(message *msg);
555 gboolean spool_read_data(message *msg);
556 message *msg_spool_read(gchar *uid, gboolean do_readdata);
557 gboolean spool_write(message *msg, gboolean do_writedata);
558 gboolean spool_lock(gchar *uid);
559 gboolean spool_unlock(gchar *uid);
560 gboolean spool_delete_all(message *msg);
562 /* queue.c */
563 GList *read_queue(gboolean do_readdata);
564 gboolean queue_run(void);
565 gboolean queue_run_online(void);
566 void queue_list(void);
567 gboolean queue_delete(gchar *uid);
569 /* online.c */
570 gchar *detect_online();
571 void set_online_name(gchar *name);
573 /* permissions.c */
574 gboolean is_ingroup(uid_t uid, gid_t gid);
575 void set_euidgid(gint uid, gint gid, uid_t *old_uid, gid_t *old_gid);
576 void set_identity(uid_t old_uid, gchar *task_name);
578 /* rewrite.c */
579 gboolean set_address_header_domain(header *hdr, gchar *domain);
580 gboolean map_address_header(header *hdr, GList *table);
582 /* route.c */
583 msgout_perhost *create_msgout_perhost(gchar *host);
584 void destroy_msgout_perhost(msgout_perhost *mo_ph);
585 void rewrite_headers(msg_out *msgout, connect_route *route);
586 void rcptlist_with_one_of_hostlist(GList *rcpt_list, GList *host_list,
587 GList **, GList **);
588 void rcptlist_with_addr_is_local(GList *rcpt_list,
589 GList **p_rcpt_list, GList **p_non_rcpt_list);
590 gboolean route_strip_msgout(connect_route *route, msg_out *msgout);
591 msg_out *route_prepare_msgout(connect_route *route, msg_out *msgout);
592 GList *route_msgout_list(connect_route *route, GList *msgout_list);
593 gboolean route_is_allowed_return_path(connect_route *route, address *ret_path);
594 gboolean route_is_allowed_mail_local(connect_route *route, address *ret_path);
595 void msg_rcptlist_route(connect_route *route, GList *rcpt_list,
596 GList **p_rcpt_list, GList **p_non_rcpt_list);
598 /* tables.c */
599 table_pair *create_pair(gchar *key, gpointer value);
600 table_pair *create_pair_string(gchar *key, gpointer value);
601 table_pair *parse_table_pair(gchar *line, char delim);
602 gpointer *table_find_func(GList *table_list, gchar *key, int (*cmp_func)(const char *, const char *));
603 gpointer *table_find(GList *table_list, gchar *key);
604 gpointer *table_find_case(GList *table_list, gchar *key);
605 gpointer *table_find_fnmatch(GList *table_list, gchar *key);
606 GList *table_read(gchar *fname, gchar delim);
607 void destroy_table(GList *table);
609 /* timeival.c */
610 gint time_interval(gchar *str, gint *pos);
612 /* permissions.c */
613 gboolean is_privileged_user(uid_t uid);
615 /* other things */
617 #define foreach(list, node)\
618 for((node) = g_list_first(list);\
619 (node);\
620 (node) = g_list_next(node))
622 #ifdef ENABLE_DEBUG
623 #define DEBUG(level) if(level <= conf.debug_level)
624 #else
625 /* hopefully the compiler optmizes this away... */
626 #define DEBUG(level) if(0)
627 #endif
629 #define LOG_VERBOSE 0x100
631 #ifndef HAVE_GETLINE
632 #define getline(buf, size, file) getdelim(buf, size, '\n', file)
633 #endif
635 #ifndef HAVE_FDATASYNC
636 #define fdatasync(fd) fsync(fd)
637 #endif
639 #ifndef CONF_DIR
640 #define CONF_DIR "/etc/masqmail"
641 #endif
643 #define CONF_FILE CONF_DIR"/masqmail.conf"
645 #define PIDFILEDIR "/var/run/masqmail/"
647 #ifndef va_copy
648 #ifdef __va_copy
649 #define va_copy(ap1, ap2) __va_copy(ap1, ap2)
650 #else
651 #define va_copy(ap1, ap2) G_VA_COPY(ap1, ap2)
652 #endif
653 #endif
655 /* *BSD needs this: */
656 extern char **environ;