1/*
2 * Copyright (c) 2008-2014, Simon Schubert <2@0x2c.org>.
3 * Copyright (c) 2008 The DragonFly Project.  All rights reserved.
4 *
5 * This code is derived from software contributed to The DragonFly Project
6 * by Simon Schubert <2@0x2c.org> and
7 * Matthias Schmidt <matthias@dragonflybsd.org>.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in
17 *    the documentation and/or other materials provided with the
18 *    distribution.
19 * 3. Neither the name of The DragonFly Project nor the names of its
20 *    contributors may be used to endorse or promote products derived
21 *    from this software without specific, prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
27 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#ifndef DMA_H
38#define DMA_H
39
40#include <sys/types.h>
41#include <sys/queue.h>
42#include <sys/socket.h>
43#include <arpa/nameser.h>
44#include <arpa/inet.h>
45#include <openssl/ssl.h>
46#include <netdb.h>
47#include <sysexits.h>
48
49#define VERSION	"DragonFly Mail Agent " DMA_VERSION
50
51#define BUF_SIZE	2048
52#define ERRMSG_SIZE	1024
53#define USERNAME_SIZE	50
54#define MIN_RETRY	300		/* 5 minutes */
55#define MAX_RETRY	(3*60*60)	/* retry at least every 3 hours */
56#define MAX_TIMEOUT	(5*24*60*60)	/* give up after 5 days */
57#define SLEEP_TIMEOUT	30		/* check for queue flush every 30 seconds */
58#ifndef PATH_MAX
59#define PATH_MAX	1024		/* Max path len */
60#endif
61#define	SMTP_PORT	25		/* Default SMTP port */
62#define CON_TIMEOUT	(5*60)		/* Connection timeout per RFC5321 */
63
64#define STARTTLS	0x002		/* StartTLS support */
65#define SECURETRANS	0x004		/* SSL/TLS in general */
66#define NOSSL		0x008		/* Do not use SSL */
67#define DEFER		0x010		/* Defer mails */
68#define INSECURE	0x020		/* Allow plain login w/o encryption */
69#define FULLBOUNCE	0x040		/* Bounce the full message */
70#define TLS_OPP		0x080		/* Opportunistic STARTTLS */
71#define NULLCLIENT	0x100		/* Nullclient support */
72
73#ifndef CONF_PATH
74#error Please define CONF_PATH
75#endif
76
77#ifndef LIBEXEC_PATH
78#error Please define LIBEXEC_PATH
79#endif
80
81#define SPOOL_FLUSHFILE	"flush"
82
83#ifndef DMA_ROOT_USER
84#define DMA_ROOT_USER	"mail"
85#endif
86#ifndef DMA_GROUP
87#define DMA_GROUP	"mail"
88#endif
89
90#ifndef MBOX_STRICT
91#define MBOX_STRICT	0
92#endif
93
94
95struct stritem {
96	SLIST_ENTRY(stritem) next;
97	char *str;
98};
99SLIST_HEAD(strlist, stritem);
100
101struct alias {
102	LIST_ENTRY(alias) next;
103	char *alias;
104	struct strlist dests;
105};
106LIST_HEAD(aliases, alias);
107
108struct qitem {
109	LIST_ENTRY(qitem) next;
110	const char *sender;
111	char *addr;
112	char *queuefn;
113	char *mailfn;
114	char *queueid;
115	FILE *queuef;
116	FILE *mailf;
117	int remote;
118};
119LIST_HEAD(queueh, qitem);
120
121struct queue {
122	struct queueh queue;
123	char *id;
124	FILE *mailf;
125	char *tmpf;
126	const char *sender;
127};
128
129struct config {
130	const char *smarthost;
131	int port;
132	const char *aliases;
133	const char *spooldir;
134	const char *authpath;
135	const char *certfile;
136	int features;
137	const char *mailname;
138	const char *masquerade_host;
139	const char *masquerade_user;
140
141	/* XXX does not belong into config */
142	SSL *ssl;
143};
144
145
146struct authuser {
147	SLIST_ENTRY(authuser) next;
148	char *login;
149	char *password;
150	char *host;
151};
152SLIST_HEAD(authusers, authuser);
153
154
155struct mx_hostentry {
156	char		host[MAXDNAME];
157	char		addr[INET6_ADDRSTRLEN];
158	int		pref;
159	struct addrinfo	ai;
160	struct sockaddr_storage	sa;
161};
162
163
164/* global variables */
165extern struct aliases aliases;
166extern struct config config;
167extern struct strlist tmpfs;
168extern struct authusers authusers;
169extern char username[USERNAME_SIZE];
170extern uid_t useruid;
171extern const char *logident_base;
172
173extern char neterr[ERRMSG_SIZE];
174extern char errmsg[ERRMSG_SIZE];
175
176/* aliases_parse.y */
177int yyparse(void);
178int yywrap(void);
179int yylex(void);
180extern FILE *yyin;
181
182/* conf.c */
183void trim_line(char *);
184void parse_conf(const char *);
185void parse_authfile(const char *);
186
187/* crypto.c */
188void hmac_md5(unsigned char *, int, unsigned char *, int, unsigned char *);
189int smtp_auth_md5(int, char *, char *);
190int smtp_init_crypto(int, int);
191
192/* dns.c */
193int dns_get_mx_list(const char *, int, struct mx_hostentry **, int);
194
195/* net.c */
196char *ssl_errstr(void);
197int read_remote(int, int, char *);
198ssize_t send_remote_command(int, const char*, ...)  __attribute__((__nonnull__(2), __format__ (__printf__, 2, 3)));
199int deliver_remote(struct qitem *);
200
201/* base64.c */
202int base64_encode(const void *, int, char **);
203int base64_decode(const char *, void *);
204
205/* dma.c */
206#define EXPAND_ADDR	1
207#define EXPAND_WILDCARD	2
208int add_recp(struct queue *, const char *, int);
209void run_queue(struct queue *);
210
211/* spool.c */
212int newspoolf(struct queue *);
213int linkspool(struct queue *);
214int load_queue(struct queue *);
215void delqueue(struct qitem *);
216int acquirespool(struct qitem *);
217void dropspool(struct queue *, struct qitem *);
218int flushqueue_since(unsigned int);
219int flushqueue_signal(void);
220
221/* local.c */
222int deliver_local(struct qitem *);
223
224/* mail.c */
225void bounce(struct qitem *, const char *);
226int readmail(struct queue *, int, int);
227
228/* util.c */
229const char *hostname(void);
230void setlogident(const char *, ...) __attribute__((__format__ (__printf__, 1, 2)));
231void errlog(int, const char *, ...) __attribute__((__format__ (__printf__, 2, 3)));
232void errlogx(int, const char *, ...) __attribute__((__format__ (__printf__, 2, 3)));
233void set_username(void);
234void deltmp(void);
235int do_timeout(int, int);
236int open_locked(const char *, int, ...);
237char *rfc822date(void);
238int strprefixcmp(const char *, const char *);
239void init_random(void);
240
241#endif
242