dd317e5b309b9e660cad5ee6a82a0ecbbb1e3613
[dragonfly.git] / libexec / dma / mail.c
1 /*
2  * Copyright (c) 2008 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Simon 'corecode' Schubert <corecode@fs.ei.tum.de>.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34
35 #include <errno.h>
36 #include <syslog.h>
37 #include <unistd.h>
38
39 #include "dma.h"
40
41 void
42 bounce(struct qitem *it, const char *reason)
43 {
44         struct queue bounceq;
45         char line[1000];
46         size_t pos;
47         int error;
48
49         /* Don't bounce bounced mails */
50         if (it->sender[0] == 0) {
51                 syslog(LOG_INFO, "can not bounce a bounce message, discarding");
52                 exit(1);
53         }
54
55         bzero(&bounceq, sizeof(bounceq));
56         LIST_INIT(&bounceq.queue);
57         bounceq.sender = "";
58         if (add_recp(&bounceq, it->sender, 1) != 0)
59                 goto fail;
60
61         if (newspoolf(&bounceq) != 0)
62                 goto fail;
63
64         syslog(LOG_ERR, "delivery failed, bouncing as %s", bounceq.id);
65         setlogident("%s", bounceq.id);
66
67         error = fprintf(bounceq.mailf,
68                 "Received: from MAILER-DAEMON\n"
69                 "\tid %s\n"
70                 "\tby %s (%s)\n"
71                 "\t%s\n"
72                 "X-Original-To: <%s>\n"
73                 "From: MAILER-DAEMON <>\n"
74                 "To: %s\n"
75                 "Subject: Mail delivery failed\n"
76                 "Message-Id: <%s@%s>\n"
77                 "Date: %s\n"
78                 "\n"
79                 "This is the %s at %s.\n"
80                 "\n"
81                 "There was an error delivering your mail to <%s>.\n"
82                 "\n"
83                 "%s\n"
84                 "\n"
85                 "%s\n"
86                 "\n",
87                 bounceq.id,
88                 hostname(), VERSION,
89                 rfc822date(),
90                 it->addr,
91                 it->sender,
92                 bounceq.id, hostname(),
93                 rfc822date(),
94                 VERSION, hostname(),
95                 it->addr,
96                 reason,
97                 config->features & FULLBOUNCE ?
98                     "Original message follows." :
99                     "Message headers follow.");
100         if (error < 0)
101                 goto fail;
102
103         if (fseek(it->mailf, it->hdrlen, SEEK_SET) != 0)
104                 goto fail;
105         if (config->features & FULLBOUNCE) {
106                 while ((pos = fread(line, 1, sizeof(line), it->mailf)) > 0) {
107                         if (fwrite(line, 1, pos, bounceq.mailf) != pos)
108                                 goto fail;
109                 }
110         } else {
111                 while (!feof(it->mailf)) {
112                         if (fgets(line, sizeof(line), it->mailf) == NULL)
113                                 break;
114                         if (line[0] == '\n')
115                                 break;
116                         if (fwrite(line, strlen(line), 1, bounceq.mailf) != 1)
117                                 goto fail;
118                 }
119         }
120
121         if (linkspool(&bounceq) != 0)
122                 goto fail;
123         /* bounce is safe */
124
125         delqueue(it);
126
127         run_queue(&bounceq);
128         /* NOTREACHED */
129
130 fail:
131         syslog(LOG_CRIT, "error creating bounce: %m");
132         delqueue(it);
133         exit(1);
134 }
135
136 int
137 readmail(struct queue *queue, int nodot)
138 {
139         char line[1000];        /* by RFC2822 */
140         size_t linelen;
141         size_t error;
142         int had_headers = 0;
143         int had_from = 0;
144         int had_messagid = 0;
145         int had_date = 0;
146
147         error = fprintf(queue->mailf,
148                 "Received: from %s (uid %d)\n"
149                 "\t(envelope-from %s)\n"
150                 "\tid %s\n"
151                 "\tby %s (%s)\n"
152                 "\t%s\n",
153                 username, getuid(),
154                 queue->sender,
155                 queue->id,
156                 hostname(), VERSION,
157                 rfc822date());
158         if ((ssize_t)error < 0)
159                 return (-1);
160
161         while (!feof(stdin)) {
162                 if (fgets(line, sizeof(line), stdin) == NULL)
163                         break;
164                 linelen = strlen(line);
165                 if (linelen == 0 || line[linelen - 1] != '\n') {
166                         errno = EINVAL;         /* XXX mark permanent errors */
167                         return (-1);
168                 }
169                 if (!had_headers) {
170                         if (strprefixcmp(line, "Date:") == 0)
171                                 had_date = 1;
172                         else if (strprefixcmp(line, "Message-Id:") == 0)
173                                 had_messagid = 1;
174                         else if (strprefixcmp(line, "From:") == 0)
175                                 had_from = 1;
176                 }
177                 if (strcmp(line, "\n") == 0 && !had_headers) {
178                         had_headers = 1;
179                         while (!had_date || !had_messagid || !had_from) {
180                                 if (!had_date) {
181                                         had_date = 1;
182                                         snprintf(line, sizeof(line), "Date: %s\n", rfc822date());
183                                 } else if (!had_messagid) {
184                                         /* XXX better msgid, assign earlier and log? */
185                                         had_messagid = 1;
186                                         snprintf(line, sizeof(line), "Message-Id: <%s@%s>\n",
187                                                  queue->id, hostname());
188                                 } else if (!had_from) {
189                                         had_from = 1;
190                                         snprintf(line, sizeof(line), "From: <%s>\n", queue->sender);
191                                 }
192                                 if (fwrite(line, strlen(line), 1, queue->mailf) != 1)
193                                         return (-1);
194                         }
195                         strcpy(line, "\n");
196                 }
197                 if (!nodot && linelen == 2 && line[0] == '.')
198                         break;
199                 if (fwrite(line, strlen(line), 1, queue->mailf) != 1)
200                         return (-1);
201         }
202
203         return (0);
204 }