masqmail
view src/masqmail.h @ 222:8cddc65765bd
added support for STARTTLS wrappers
added the route config option `instant_helo' which
causes masqmail, as SMTP client, not to wait for the
server's 220 greeting. Instead if says EHLO right at
once. You'll need this for STARTTLS wrappers that
usually eat the greeting line.
author | meillo@marmaro.de |
---|---|
date | Fri, 23 Jul 2010 10:57:53 +0200 |
parents | 4fd237550525 |
children | 996b53a50f55 |
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 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 (*alias_local_cmp) (const char *, const char *);
183 GList *local_net_routes;
184 GList *connect_routes; /* list of pairs which point to lists */
186 gchar *online_detect;
187 gchar *online_file;
188 gchar *online_pipe;
190 gchar *errmsg_file;
191 gchar *warnmsg_file;
192 GList *warn_intervals;
193 gint max_defer_time;
195 gchar *log_user;
196 } masqmail_conf;
198 extern masqmail_conf conf;
200 typedef struct _table_pair {
201 gchar *key;
202 gpointer *value;
203 } table_pair;
206 typedef enum _prot_id {
207 PROT_LOCAL = 0,
208 PROT_BSMTP,
209 PROT_SMTP,
210 PROT_ESMTP,
211 PROT_NUM
212 } prot_id;
214 extern gchar *prot_names[];
216 typedef enum _header_id {
217 HEAD_FROM = 0,
218 HEAD_SENDER,
219 HEAD_TO,
220 HEAD_CC,
221 HEAD_BCC,
222 HEAD_DATE,
223 HEAD_MESSAGE_ID,
224 HEAD_REPLY_TO,
225 HEAD_SUBJECT,
226 HEAD_RETURN_PATH,
227 HEAD_ENVELOPE_TO,
228 HEAD_RECEIVED,
229 HEAD_NUM_IDS,
230 HEAD_STATUS,
231 HEAD_UNKNOWN = HEAD_NUM_IDS,
232 HEAD_NONE = -1,
233 } header_id;
235 typedef struct _header_name {
236 gchar *header;
237 header_id id;
238 } header_name;
240 typedef struct _header {
241 header_id id;
242 gchar *header;
243 gchar *value;
244 } header;
247 typedef struct _message {
248 gchar *uid;
250 gchar *received_host;
251 prot_id received_prot;
252 gchar *ident;
253 gint transfer_id; /* for multiple messages per transfer */
255 address *return_path;
256 GList *rcpt_list;
257 GList *non_rcpt_list;
259 GList *hdr_list;
260 GList *data_list;
262 gint data_size;
263 time_t received_time;
264 time_t warned_time;
266 gchar *full_sender_name;
267 } message;
269 typedef struct _msg_out {
270 message *msg;
272 address *return_path;
273 GList *rcpt_list;
275 GList *hdr_list;
276 GList *xtra_hdr_list;
277 } msg_out;
279 typedef struct _msgout_perhost {
280 gchar *host;
281 GList *msgout_list;
282 } msgout_perhost;
284 /* flags for accept() */
285 #define ACC_DEL_RCPTS 0x02 /* -t option, delete rcpts that were given as cmd args */
286 #define ACC_RCPT_FROM_HEAD 0x08 /* -t option, get rcpts from headers */
287 #define ACC_DOT_IGNORE 0x10 /* a dot on a line itself does not end the message (-oi option) */
288 #define ACC_MAIL_FROM_HEAD 0x40 /* get return path from header */
289 #define ACC_NODOT_RELAX 0x80 /* do not be picky if message ist not terminated by a dot on a line */
290 #define ACC_SAVE_ENVELOPE_TO 0x0100 /* save an existent Envelope-to header as X-Orig-Envelope-to */
292 #define DLVR_LOCAL 0x01
293 #define DLVR_LAN 0x02
294 #define DLVR_ONLINE 0x04
295 #define DLVR_ALL (DLVR_LOCAL|DLVR_LAN|DLVR_ONLINE)
297 /* transport flags */
298 #define MSGSTR_FROMLINE 0x01
299 #define MSGSTR_FROMHACK 0x02
301 typedef enum _accept_error {
302 AERR_OK = 0,
303 AERR_TIMEOUT,
304 AERR_EOF,
305 AERR_OVERFLOW,
306 AERR_SYNTAX,
307 AERR_NOSPOOL,
308 AERR_NORCPT,
309 AERR_SIZE, /* max msg size exeeded (SMTP SIZE) */
310 AERR_UNKNOWN
311 } accept_error;
313 #define BUF_LEN 1024
314 #define MAX_ADDRESS 256
315 #define MAX_DATALINE 4096
317 typedef enum _smtp_cmd_id {
318 SMTP_HELO = 0,
319 SMTP_EHLO,
320 SMTP_MAIL_FROM,
321 SMTP_RCPT_TO,
322 SMTP_DATA,
323 SMTP_QUIT,
324 SMTP_RSET,
325 SMTP_NOOP,
326 SMTP_HELP,
327 SMTP_NUM_IDS,
328 SMTP_EOF = -1,
329 SMTP_ERROR = -2,
330 } smtp_cmd_id;
332 typedef struct _smtp_cmd {
333 smtp_cmd_id id;
334 gchar *cmd;
335 } smtp_cmd;
337 typedef struct _smtp_connection {
338 gchar *remote_host;
340 prot_id prot;
341 gint next_id;
343 gboolean helo_seen;
344 gboolean from_seen;
345 gboolean rcpt_seen;
347 message *msg;
348 } smtp_connection;
350 /* alias.c*/
351 gboolean addr_is_local(address * addr);
352 GList *alias_expand(GList * alias_table, GList * rcpt_list, GList * non_rcpt_list);
354 /* child.c */
355 int child(const char *command);
357 /* conf.c */
358 void init_conf();
359 gboolean read_conf(gchar * filename);
360 connect_route *read_route(gchar * filename, gboolean is_local_net);
361 GList *read_route_list(GList * rf_list, gboolean is_local_net);
362 void destroy_route(connect_route * r);
363 void destroy_route_list(GList * list);
364 connect_route *create_local_route();
366 /* expand.c */
367 GList *var_table_rcpt(GList * var_table, address * rcpt);
368 GList *var_table_msg(GList * var_table, message * msg);
369 GList *var_table_conf(GList * var_table);
370 gint expand(GList * var_list, gchar * format, gchar * result, gint result_len);
372 /* message.c */
373 message *create_message(void);
374 void destroy_message(message * msg);
375 void destroy_msg_list(GList * msg_list);
376 void msg_free_data(message * msg);
377 gint msg_calc_size(message * msg, gboolean is_smtp);
379 msg_out *create_msg_out(message * msg);
380 msg_out *clone_msg_out(msg_out * msgout_orig);
381 GList *create_msg_out_list(GList * msg_list);
382 void destroy_msg_out(msg_out * msgout);
383 void destroy_msg_out_list(GList * msgout_list);
385 /* address.c */
386 address *create_address(gchar * path, gboolean is_rfc821);
387 address *create_address_qualified(gchar * path, gboolean is_rfc821, gchar * domain);
388 address *create_address_pipe(gchar * path);
389 void destroy_address(address * addr);
390 address *copy_modify_address(const address * orig, gchar * l_part, gchar * dom);
391 #define copy_address(addr) copy_modify_address(addr, NULL, NULL)
392 gboolean addr_isequal(address * addr1, address * addr2);
393 gboolean addr_isequal_parent(address * addr1, address * addr2);
394 address *addr_find_ancestor(address * addr);
395 gboolean addr_is_delivered_children(address * addr);
396 gboolean addr_is_finished_children(address * addr);
397 gchar *addr_string(address * addr);
398 gint addr_match(address * addr1, address * addr2);
400 /* accept.c */
401 accept_error accept_message(FILE * in, message * msg, guint flags);
402 accept_error accept_message_prepare(message * msg, guint flags);
404 /* header.c */
405 gchar *rec_timestamp();
406 GList *find_header(GList * hdr_list, header_id id, gchar * hdr_str);
407 void header_unfold(header * hdr);
408 void header_fold(header * hdr);
409 header *create_header(header_id id, gchar * fmt, ...);
410 void destroy_header(header * hdr);
411 header *copy_header(header * hdr);
412 header *get_header(gchar * line);
414 /* smtp_in.c */
415 void smtp_in(FILE * in, FILE * out, gchar * remote_host, gchar * ident);
417 /* listen.c */
418 void listen_port(GList * addr_list, gint qival, char *argv[]);
420 /* parse.c */
421 gboolean split_address(const gchar * path, gchar ** local_part, gchar ** domain, gboolean is_rfc821);
422 gboolean parse_address_rfc822(gchar * string, gchar ** local_begin, gchar ** local_end, gchar ** domain_begin, gchar ** domain_end, gchar ** address_end);
423 gboolean parse_address_rfc821(gchar * string, gchar ** local_begin, gchar ** local_end, gchar ** domain_begin, gchar ** domain_end, gchar ** address_end);
424 address *_create_address(gchar * string, gchar ** end, gboolean is_rfc821);
425 address *create_address_rfc821(gchar * string, gchar ** end);
426 address *create_address_rfc822(gchar * string, gchar ** end);
427 GList *addr_list_append_rfc822(GList * addr_list, gchar * string, gchar * domain);
428 gboolean addr_isequal(address * addr1, address * addr2);
430 /* connect.c */
431 mxip_addr *connect_hostlist(int *psockfd, gchar * host, guint port, GList * addr_list);
432 mxip_addr *connect_resolvelist(int *psockfd, gchar * host, guint port, GList * res_funcs);
434 /* deliver.c */
435 void msg_rcptlist_local(GList * rcpt_list, GList **, GList **);
436 gboolean deliver_local(msg_out * msgout);
437 gboolean deliver_msglist_host(connect_route * route, GList * msg_list, gchar * host, GList * res_list);
438 gboolean deliver_route_msgout_list(connect_route * route, GList * msgout_list);
439 gboolean deliver_route_msg_list(connect_route * route, GList * msgout_list);
440 gboolean deliver_finish(msg_out * msgout);
441 gboolean deliver_finish_list(GList * msgout_list);
442 gboolean deliver_msg_list(GList * msg_list, guint flags);
443 gboolean deliver(message * msg);
445 /* fail_msg.c */
446 gboolean fail_msg(message * msg, gchar * template, GList * failed_rcpts, gchar * err_fmt, va_list args);
447 gboolean warn_msg(message * msg, gchar * template, GList * failed_rcpts, gchar * err_fmt, va_list args);
449 /* interface.c */
450 gboolean init_sockaddr(struct sockaddr_in *name, interface * iface);
451 int make_server_socket(interface * iface);
453 /* local.c */
454 gboolean append_file(message * msg, GList * hdr_list, gchar * user);
455 gboolean pipe_out(message * msg, GList * hdr_list, address * rcpt, gchar * cmd, guint flags);
457 /* log.c */
458 gchar *ext_strerror(int err);
459 gboolean logopen(void);
460 void logclose(void);
461 void vlogwrite(int pri, const char *fmt, va_list args);
462 void logwrite(int pri, const char *fmt, ...);
463 void debugf(const char *fmt, ...);
464 void vdebugf(const char *fmt, va_list args);
465 void maillog(const char *fmt, ...);
467 /* spool.c */
468 gboolean spool_read_data(message * msg);
469 gboolean spool_read_data(message * msg);
470 message *msg_spool_read(gchar * uid, gboolean do_readdata);
471 gboolean spool_write(message * msg, gboolean do_writedata);
472 gboolean spool_lock(gchar * uid);
473 gboolean spool_unlock(gchar * uid);
474 gboolean spool_delete_all(message * msg);
476 /* queue.c */
477 GList *read_queue(gboolean do_readdata);
478 gboolean queue_run(void);
479 gboolean queue_run_online(void);
480 void queue_list(void);
481 gboolean queue_delete(gchar * uid);
483 /* online.c */
484 gchar *detect_online();
485 void set_online_name(gchar * name);
487 /* permissions.c */
488 gboolean is_ingroup(uid_t uid, gid_t gid);
489 void set_euidgid(gint uid, gint gid, uid_t * old_uid, gid_t * old_gid);
490 void set_identity(uid_t old_uid, gchar * task_name);
492 /* rewrite.c */
493 gboolean set_address_header_domain(header * hdr, gchar * domain);
494 gboolean map_address_header(header * hdr, GList * table);
496 /* route.c */
497 msgout_perhost *create_msgout_perhost(gchar * host);
498 void destroy_msgout_perhost(msgout_perhost * mo_ph);
499 void rewrite_headers(msg_out * msgout, connect_route * route);
500 void rcptlist_with_one_of_hostlist(GList * rcpt_list, GList * host_list, GList **, GList **);
501 void rcptlist_with_addr_is_local(GList * rcpt_list, GList ** p_rcpt_list, GList ** p_non_rcpt_list);
502 gboolean route_strip_msgout(connect_route * route, msg_out * msgout);
503 msg_out *route_prepare_msgout(connect_route * route, msg_out * msgout);
504 GList *route_msgout_list(connect_route * route, GList * msgout_list);
505 gboolean route_is_allowed_return_path(connect_route * route, address * ret_path);
506 gboolean route_is_allowed_mail_local(connect_route * route, address * ret_path);
507 void msg_rcptlist_route(connect_route * route, GList * rcpt_list, GList ** p_rcpt_list, GList ** p_non_rcpt_list);
509 /* tables.c */
510 table_pair *create_pair(gchar * key, gpointer value);
511 table_pair *create_pair_string(gchar * key, gpointer value);
512 table_pair *parse_table_pair(gchar * line, char delim);
513 gpointer *table_find_func(GList * table_list, gchar * key, int (*cmp_func) (const char *, const char *));
514 gpointer *table_find(GList * table_list, gchar * key);
515 gpointer *table_find_case(GList * table_list, gchar * key);
516 gpointer *table_find_fnmatch(GList * table_list, gchar * key);
517 GList *table_read(gchar * fname, gchar delim);
518 void destroy_table(GList * table);
520 /* timeival.c */
521 gint time_interval(gchar * str, gint * pos);
523 /* permissions.c */
524 gboolean is_privileged_user(uid_t uid);
526 /* other things */
528 #define foreach(list, node)\
529 for((node) = g_list_first(list);\
530 (node);\
531 (node) = g_list_next(node))
533 #ifdef ENABLE_DEBUG
534 #define DEBUG(level) if(level <= conf.debug_level)
535 #else
536 /* hopefully the compiler optmizes this away... */
537 #define DEBUG(level) if(0)
538 #endif
540 #define LOG_VERBOSE 0x100
542 #ifndef HAVE_GETLINE
543 #define getline(buf, size, file) getdelim(buf, size, '\n', file)
544 #endif
546 #ifndef HAVE_FDATASYNC
547 #define fdatasync(fd) fsync(fd)
548 #endif
550 #ifndef CONF_DIR
551 #define CONF_DIR "/etc/masqmail"
552 #endif
554 #define CONF_FILE CONF_DIR"/masqmail.conf"
556 #define PIDFILEDIR "/var/run/masqmail/"
558 #ifndef va_copy
559 #ifdef __va_copy
560 #define va_copy(ap1, ap2) __va_copy(ap1, ap2)
561 #else
562 #define va_copy(ap1, ap2) G_VA_COPY(ap1, ap2)
563 #endif
564 #endif
566 /* *BSD needs this: */
567 extern char **environ;