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 #define LMTP 0x400 /* Use LMTP instead of SMTP with the relay */ 74 75 #ifndef CONF_PATH 76 #error Please define CONF_PATH 77 #endif 78 79 #ifndef LIBEXEC_PATH 80 #error Please define LIBEXEC_PATH 81 #endif 82 83 #define SPOOL_FLUSHFILE "flush" 84 85 #ifndef DMA_ROOT_USER 86 #define DMA_ROOT_USER "mail" 87 #endif 88 #ifndef DMA_GROUP 89 #define DMA_GROUP "mail" 90 #endif 91 92 #ifndef MBOX_STRICT 93 #define MBOX_STRICT 0 94 #endif 95 96 97 struct stritem { 98 SLIST_ENTRY(stritem) next; 99 char *str; 100 }; 101 SLIST_HEAD(strlist, stritem); 102 103 struct alias { 104 LIST_ENTRY(alias) next; 105 char *alias; 106 struct strlist dests; 107 }; 108 LIST_HEAD(aliases, alias); 109 110 struct qitem { 111 LIST_ENTRY(qitem) next; 112 const char *sender; 113 char *addr; 114 char *queuefn; 115 char *mailfn; 116 char *queueid; 117 FILE *queuef; 118 FILE *mailf; 119 int remote; 120 }; 121 LIST_HEAD(queueh, qitem); 122 123 struct queue { 124 struct queueh queue; 125 char *id; 126 FILE *mailf; 127 char *tmpf; 128 const char *sender; 129 }; 130 131 struct config { 132 const char *smarthost; 133 int port; 134 const char *aliases; 135 const char *spooldir; 136 const char *authpath; 137 const char *certfile; 138 int features; 139 const char *mailname; 140 const char *masquerade_host; 141 const char *masquerade_user; 142 const unsigned char *fingerprint; 143 144 /* XXX does not belong into config */ 145 SSL *ssl; 146 }; 147 148 149 struct authuser { 150 SLIST_ENTRY(authuser) next; 151 char *login; 152 char *password; 153 char *host; 154 }; 155 SLIST_HEAD(authusers, authuser); 156 157 158 struct mx_hostentry { 159 char host[MAXDNAME]; 160 char addr[INET6_ADDRSTRLEN]; 161 int pref; 162 struct addrinfo ai; 163 struct sockaddr_storage sa; 164 }; 165 166 struct smtp_auth_mechanisms { 167 int cram_md5; 168 int login; 169 }; 170 171 struct smtp_features { 172 struct smtp_auth_mechanisms auth; 173 int starttls; 174 }; 175 176 /* global variables */ 177 extern struct aliases aliases; 178 extern struct config config; 179 extern struct strlist tmpfs; 180 extern struct authusers authusers; 181 extern char username[USERNAME_SIZE]; 182 extern uid_t useruid; 183 extern const char *logident_base; 184 185 extern char neterr[ERRMSG_SIZE]; 186 extern char errmsg[ERRMSG_SIZE]; 187 188 /* aliases_parse.y */ 189 int yyparse(void); 190 int yywrap(void); 191 int yylex(void); 192 extern FILE *yyin; 193 194 /* conf.c */ 195 void trim_line(char *); 196 void parse_conf(const char *); 197 void parse_authfile(const char *); 198 199 /* crypto.c */ 200 void hmac_md5(unsigned char *, int, unsigned char *, int, unsigned char *); 201 int smtp_auth_md5(int, char *, char *); 202 int smtp_init_crypto(int, int, struct smtp_features*); 203 204 /* dns.c */ 205 int dns_get_mx_list(const char *, int, struct mx_hostentry **, int); 206 207 /* net.c */ 208 char *ssl_errstr(void); 209 int read_remote(int, int, char *); 210 ssize_t send_remote_command(int, const char*, ...) __attribute__((__nonnull__(2), __format__ (__printf__, 2, 3))); 211 int perform_server_greeting(int, struct smtp_features*); 212 int deliver_remote(struct qitem *); 213 214 /* base64.c */ 215 int base64_encode(const void *, int, char **); 216 int base64_decode(const char *, void *); 217 218 /* dma.c */ 219 #define EXPAND_ADDR 1 220 #define EXPAND_WILDCARD 2 221 int add_recp(struct queue *, const char *, int); 222 void run_queue(struct queue *); 223 224 /* spool.c */ 225 int newspoolf(struct queue *); 226 int linkspool(struct queue *); 227 int load_queue(struct queue *); 228 void delqueue(struct qitem *); 229 int acquirespool(struct qitem *); 230 void dropspool(struct queue *, struct qitem *); 231 int flushqueue_since(unsigned int); 232 int flushqueue_signal(void); 233 234 /* local.c */ 235 int deliver_local(struct qitem *); 236 237 /* mail.c */ 238 void bounce(struct qitem *, const char *); 239 int readmail(struct queue *, int, int); 240 241 /* util.c */ 242 const char *hostname(void); 243 const char *systemhostname(void); 244 void setlogident(const char *, ...) __attribute__((__format__ (__printf__, 1, 2))); 245 void errlog(int, const char *, ...) __attribute__((__format__ (__printf__, 2, 3))); 246 void errlogx(int, const char *, ...) __attribute__((__format__ (__printf__, 2, 3))); 247 void set_username(void); 248 void deltmp(void); 249 int do_timeout(int, int); 250 int open_locked(const char *, int, ...); 251 char *rfc822date(void); 252 int strprefixcmp(const char *, const char *); 253 void init_random(void); 254 255 #endif 256