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 EHLO_RESPONSE_SIZE BUF_SIZE
55#define MIN_RETRY	300		/* 5 minutes */
56#define MAX_RETRY	(3*60*60)	/* retry at least every 3 hours */
57#define MAX_TIMEOUT	(5*24*60*60)	/* give up after 5 days */
58#define SLEEP_TIMEOUT	30		/* check for queue flush every 30 seconds */
59#ifndef PATH_MAX
60#define PATH_MAX	1024		/* Max path len */
61#endif
62#define	SMTP_PORT	25		/* Default SMTP port */
63#define CON_TIMEOUT	(5*60)		/* Connection timeout per RFC5321 */
64
65#define STARTTLS	0x002		/* StartTLS support */
66#define SECURETRANSFER	0x004		/* SSL/TLS in general */
67#define NOSSL		0x008		/* Do not use SSL */
68#define DEFER		0x010		/* Defer mails */
69#define INSECURE	0x020		/* Allow plain login w/o encryption */
70#define FULLBOUNCE	0x040		/* Bounce the full message */
71#define TLS_OPP		0x080		/* Opportunistic STARTTLS */
72#define NULLCLIENT	0x100		/* Nullclient support */
73
74#ifndef CONF_PATH
75#error Please define CONF_PATH
76#endif
77
78#ifndef LIBEXEC_PATH
79#error Please define LIBEXEC_PATH
80#endif
81
82#define SPOOL_FLUSHFILE	"flush"
83
84#ifndef DMA_ROOT_USER
85#define DMA_ROOT_USER	"mail"
86#endif
87#ifndef DMA_GROUP
88#define DMA_GROUP	"mail"
89#endif
90
91#ifndef MBOX_STRICT
92#define MBOX_STRICT	0
93#endif
94
95
96struct stritem {
97	SLIST_ENTRY(stritem) next;
98	char *str;
99};
100SLIST_HEAD(strlist, stritem);
101
102struct alias {
103	LIST_ENTRY(alias) next;
104	char *alias;
105	struct strlist dests;
106};
107LIST_HEAD(aliases, alias);
108
109struct qitem {
110	LIST_ENTRY(qitem) next;
111	const char *sender;
112	char *addr;
113	char *queuefn;
114	char *mailfn;
115	char *queueid;
116	FILE *queuef;
117	FILE *mailf;
118	int remote;
119};
120LIST_HEAD(queueh, qitem);
121
122struct queue {
123	struct queueh queue;
124	char *id;
125	FILE *mailf;
126	char *tmpf;
127	const char *sender;
128};
129
130struct config {
131	const char *smarthost;
132	int port;
133	const char *aliases;
134	const char *spooldir;
135	const char *authpath;
136	const char *certfile;
137	int features;
138	const char *mailname;
139	const char *masquerade_host;
140	const char *masquerade_user;
141	const unsigned char *fingerprint;
142
143	/* XXX does not belong into config */
144	SSL *ssl;
145};
146
147
148struct authuser {
149	SLIST_ENTRY(authuser) next;
150	char *login;
151	char *password;
152	char *host;
153};
154SLIST_HEAD(authusers, authuser);
155
156
157struct mx_hostentry {
158	char		host[MAXDNAME];
159	char		addr[INET6_ADDRSTRLEN];
160	int		pref;
161	struct addrinfo	ai;
162	struct sockaddr_storage	sa;
163};
164
165struct smtp_auth_mechanisms {
166	int cram_md5;
167	int login;
168};
169
170struct smtp_features {
171	struct smtp_auth_mechanisms auth;
172	int starttls;
173};
174
175/* global variables */
176extern struct aliases aliases;
177extern struct config config;
178extern struct strlist tmpfs;
179extern struct authusers authusers;
180extern char username[USERNAME_SIZE];
181extern uid_t useruid;
182extern const char *logident_base;
183
184extern char neterr[ERRMSG_SIZE];
185extern char errmsg[ERRMSG_SIZE];
186
187/* aliases_parse.y */
188int yyparse(void);
189int yywrap(void);
190int yylex(void);
191extern FILE *yyin;
192
193/* conf.c */
194void trim_line(char *);
195void parse_conf(const char *);
196void parse_authfile(const char *);
197
198/* crypto.c */
199void hmac_md5(unsigned char *, int, unsigned char *, int, unsigned char *);
200int smtp_auth_md5(int, char *, char *);
201int smtp_init_crypto(int, int, struct smtp_features*);
202
203/* dns.c */
204int dns_get_mx_list(const char *, int, struct mx_hostentry **, int);
205
206/* net.c */
207char *ssl_errstr(void);
208int read_remote(int, int, char *);
209ssize_t send_remote_command(int, const char*, ...)  __attribute__((__nonnull__(2), __format__ (__printf__, 2, 3)));
210int perform_server_greeting(int, struct smtp_features*);
211int deliver_remote(struct qitem *);
212
213/* base64.c */
214int base64_encode(const void *, int, char **);
215int base64_decode(const char *, void *);
216
217/* dma.c */
218#define EXPAND_ADDR	1
219#define EXPAND_WILDCARD	2
220int add_recp(struct queue *, const char *, int);
221void run_queue(struct queue *);
222
223/* spool.c */
224int newspoolf(struct queue *);
225int linkspool(struct queue *);
226int load_queue(struct queue *);
227void delqueue(struct qitem *);
228int acquirespool(struct qitem *);
229void dropspool(struct queue *, struct qitem *);
230int flushqueue_since(unsigned int);
231int flushqueue_signal(void);
232
233/* local.c */
234int deliver_local(struct qitem *);
235
236/* mail.c */
237void bounce(struct qitem *, const char *);
238int readmail(struct queue *, int, int);
239
240/* util.c */
241const char *hostname(void);
242const char *systemhostname(void);
243void setlogident(const char *, ...) __attribute__((__format__ (__printf__, 1, 2)));
244void errlog(int, const char *, ...) __attribute__((__format__ (__printf__, 2, 3)));
245void errlogx(int, const char *, ...) __attribute__((__format__ (__printf__, 2, 3)));
246void set_username(void);
247void deltmp(void);
248int do_timeout(int, int);
249int open_locked(const char *, int, ...);
250char *rfc822date(void);
251int strprefixcmp(const char *, const char *);
252void init_random(void);
253
254#endif
255