2 * This file is part of DisOrder
3 * Copyright (C) 2007-2008 Richard Kettlewell
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include <sys/socket.h>
28 #include <netinet/in.h>
32 #include "configuration.h"
33 #include "inputline.h"
40 /** @brief Get a server response
41 * @param tag Server name
42 * @param in Input stream
43 * @return Response code 0-999 or -1 on error
45 static int getresponse(const char *tag, FILE *in) {
48 while(!inputline(tag, in, &line, CRLF)) {
49 if(line[0] >= '0' && line[0] <= '9'
50 && line[1] >= '0' && line[1] <= '9'
51 && line[2] >= '0' && line[2] <= '9') {
52 const int rc = 10 * (10 * line[0] + line[1]) + line[2] - 111 * '0';
53 if(rc >= 400 && rc <= 599)
54 error(0, "%s: %s", tag, line);
58 /* else go round for further response lines */
60 error(0, "%s: malformed response: %s", tag, line);
65 error(errno, "%s: read error", tag);
67 error(0, "%s: server closed connection", tag);
71 /** @brief Send a command to the server
72 * @param tag Server name
73 * @param out stream to send commands to
74 * @param fmt Format string
75 * @return 0 on success, non-0 on error
77 static int sendcommand(const char *tag, FILE *out, const char *fmt, ...) {
82 rc = vfprintf(out, fmt, ap);
85 rc = fputs("\r\n", out);
90 error(errno, "%s: write error", tag);
94 /** @brief Send a mail message
95 * @param tag Server name
96 * @param in Stream to read responses from
97 * @param out Stream to send commands to
98 * @param sender Sender address (can be "")
99 * @param pubsender Visible sender address (must not be "")
100 * @param recipient Recipient address
101 * @param subject Subject string
102 * @param encoding Body encoding
103 * @param content_type Content-type of body
104 * @param body Text of body (encoded, but \n for newline)
105 * @return 0 on success, non-0 on error
107 static int sendmailfp(const char *tag, FILE *in, FILE *out,
109 const char *pubsender,
110 const char *recipient,
112 const char *encoding,
113 const char *content_type,
125 strftime(date, sizeof date, "%a, %d %b %Y %H:%M:%S +0000", &ut);
126 gcry_create_nonce(idbuf, sizeof idbuf);
127 id = mime_to_base64(idbuf, sizeof idbuf);
128 if((rc = getresponse(tag, in)) / 100 != 2)
130 if(sendcommand(tag, out, "HELO %s", local_hostname()))
132 if((rc = getresponse(tag, in)) / 100 != 2)
134 if(sendcommand(tag, out, "MAIL FROM:<%s>", sender))
136 if((rc = getresponse(tag, in)) / 100 != 2)
138 if(sendcommand(tag, out, "RCPT TO:<%s>", recipient))
140 if((rc = getresponse(tag, in)) / 100 != 2)
142 if(sendcommand(tag, out, "DATA", sender))
144 if((rc = getresponse(tag, in)) / 100 != 3)
146 if(fprintf(out, "From: %s\r\n", pubsender) < 0
147 || fprintf(out, "To: %s\r\n", recipient) < 0
148 || fprintf(out, "Subject: %s\r\n", subject) < 0
149 || fprintf(out, "Message-ID: <%s@%s>\r\n", id, local_hostname()) < 0
150 || fprintf(out, "MIME-Version: 1.0\r\n") < 0
151 || fprintf(out, "Content-Type: %s\r\n", content_type) < 0
152 || fprintf(out, "Content-Transfer-Encoding: %s\r\n", encoding) < 0
153 || fprintf(out, "Date: %s\r\n", date) < 0
154 || fprintf(out, "\r\n") < 0) {
156 error(errno, "%s: write error", tag);
159 for(ptr = body; *ptr; ++ptr) {
160 if(sol && *ptr == '.')
161 if(fputc('.', out) < 0)
164 if(fputc('\r', out) < 0)
169 if(fputc(*ptr, out) < 0)
173 if(fputs("\r\n", out) < 0)
175 if(fprintf(out, ".\r\n") < 0
178 if((rc = getresponse(tag, in)) / 100 != 2)
183 /** @brief Send a mail message
184 * @param sender Sender address (can be "")
185 * @param pubsender Visible sender address (must not be "")
186 * @param recipient Recipient address
187 * @param subject Subject string
188 * @param encoding Body encoding
189 * @param content_type Content-type of body
190 * @param body Text of body (encoded, but \n for newline)
191 * @return 0 on success, non-0 on error
193 * See mime_encode_text() for encoding of text bodies.
195 int sendmail(const char *sender,
196 const char *pubsender,
197 const char *recipient,
199 const char *encoding,
200 const char *content_type,
210 static const struct addrinfo pref = {
212 .ai_family = PF_INET,
213 .ai_socktype = SOCK_STREAM,
214 .ai_protocol = IPPROTO_TCP,
217 /* Find the SMTP server */
218 if(config->sendmail && config->sendmail[0]) {
219 int inpipe[2], outpipe[2];
221 /* If it's a path name, use -bs mode. Exim, Postfix and Sendmail all claim
222 * to support this. */
223 xpipe(inpipe); /* sendmail's stdin */
224 xpipe(outpipe); /* sendmail's stdout */
225 if(!(pid = xfork())) {
227 signal(SIGPIPE, SIG_DFL);
231 xdup2(outpipe[1], 1);
232 execlp(config->sendmail,
233 config->sendmail, "-bs", (char *)0);
234 fatal(errno, "executing %s", config->sendmail);
238 fdin = outpipe[0]; /* read from sendmail's stdout */
239 fdout = inpipe[1]; /* write to sendmail's stdin */
240 tag = config->sendmail;
244 s[0] = config->smtp_server;
245 s[1] = (char *)"smtp";
246 if(!(ai = get_address(&a, &pref, &tag)))
248 fdin = xsocket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
249 if(connect(fdin, ai->ai_addr, ai->ai_addrlen) < 0) {
250 error(errno, "error connecting to %s", tag);
254 if((fdout = dup(fdin)) < 0)
255 fatal(errno, "error calling dup2");
257 if(!(in = fdopen(fdin, "rb")))
258 fatal(errno, "error calling fdopen");
259 if(!(out = fdopen(fdout, "wb")))
260 fatal(errno, "error calling fdopen");
261 rc = sendmailfp(tag, in, out, sender, pubsender, recipient, subject,
262 encoding, content_type, body);
268 while(waitpid(pid, &w, 0) < 0 && errno == EINTR)
271 fatal(errno, "error calling waitpid");
273 info("warning: %s -bs: %s", config->sendmail, wstat(w));
274 /* Not fatal - we determine success/failure from the SMTP conversation.
275 * Some MTAs exit nonzero if you don't QUIT, which is just stupidly
281 /** @brief Start a subproces to send a mail message
282 * @param sender Sender address (can be "")
283 * @param pubsender Visible sender address (must not be "")
284 * @param recipient Recipient address
285 * @param subject Subject string
286 * @param encoding Body encoding
287 * @param content_type Content-type of body
288 * @param body Text of body (encoded, but \n for newline)
289 * @return Subprocess PID on success, -1 on error
291 pid_t sendmail_subprocess(const char *sender,
292 const char *pubsender,
293 const char *recipient,
295 const char *encoding,
296 const char *content_type,
300 if(!(pid = fork())) {
302 if(sendmail(sender, pubsender, recipient, subject,
303 encoding, content_type, body))
308 error(errno, "error calling fork");