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 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * 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, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
25 #include <sys/socket.h>
30 #include <netinet/in.h>
34 #include "configuration.h"
35 #include "inputline.h"
41 /** @brief Get a server response
42 * @param tag Server name
43 * @param in Input stream
44 * @return Response code 0-999 or -1 on error
46 static int getresponse(const char *tag, FILE *in) {
49 while(!inputline(tag, in, &line, CRLF)) {
50 if(line[0] >= '0' && line[0] <= '9'
51 && line[1] >= '0' && line[1] <= '9'
52 && line[2] >= '0' && line[2] <= '9') {
53 const int rc = 10 * (10 * line[0] + line[1]) + line[2] - 111 * '0';
54 if(rc >= 400 && rc <= 599)
55 error(0, "%s: %s", tag, line);
59 /* else go round for further response lines */
61 error(0, "%s: malformed response: %s", tag, line);
66 error(errno, "%s: read error", tag);
68 error(0, "%s: server closed connection", tag);
72 /** @brief Send a command to the server
73 * @param tag Server name
74 * @param out stream to send commands to
75 * @param fmt Format string
76 * @return 0 on success, non-0 on error
78 static int sendcommand(const char *tag, FILE *out, const char *fmt, ...) {
83 rc = vfprintf(out, fmt, ap);
86 rc = fputs("\r\n", out);
91 error(errno, "%s: write error", tag);
95 /** @brief Send a mail message
96 * @param tag Server name
97 * @param in Stream to read responses from
98 * @param out Stream to send commands to
99 * @param sender Sender address (can be "")
100 * @param pubsender Visible sender address (must not be "")
101 * @param recipient Recipient address
102 * @param subject Subject string
103 * @param encoding Body encoding
104 * @param content_type Content-type of body
105 * @param body Text of body (encoded, but \n for newline)
106 * @return 0 on success, non-0 on error
108 static int sendmailfp(const char *tag, FILE *in, FILE *out,
110 const char *pubsender,
111 const char *recipient,
113 const char *encoding,
114 const char *content_type,
126 strftime(date, sizeof date, "%a, %d %b %Y %H:%M:%S +0000", &ut);
127 gcry_create_nonce(idbuf, sizeof idbuf);
128 id = mime_to_base64(idbuf, sizeof idbuf);
129 if((rc = getresponse(tag, in)) / 100 != 2)
131 if(sendcommand(tag, out, "HELO %s", local_hostname()))
133 if((rc = getresponse(tag, in)) / 100 != 2)
135 if(sendcommand(tag, out, "MAIL FROM:<%s>", sender))
137 if((rc = getresponse(tag, in)) / 100 != 2)
139 if(sendcommand(tag, out, "RCPT TO:<%s>", recipient))
141 if((rc = getresponse(tag, in)) / 100 != 2)
143 if(sendcommand(tag, out, "DATA", sender))
145 if((rc = getresponse(tag, in)) / 100 != 3)
147 if(fprintf(out, "From: %s\r\n", pubsender) < 0
148 || fprintf(out, "To: %s\r\n", recipient) < 0
149 || fprintf(out, "Subject: %s\r\n", subject) < 0
150 || fprintf(out, "Message-ID: <%s@%s>\r\n", id, local_hostname()) < 0
151 || fprintf(out, "MIME-Version: 1.0\r\n") < 0
152 || fprintf(out, "Content-Type: %s\r\n", content_type) < 0
153 || fprintf(out, "Content-Transfer-Encoding: %s\r\n", encoding) < 0
154 || fprintf(out, "Date: %s\r\n", date) < 0
155 || fprintf(out, "\r\n") < 0) {
157 error(errno, "%s: write error", tag);
160 for(ptr = body; *ptr; ++ptr) {
161 if(sol && *ptr == '.')
162 if(fputc('.', out) < 0)
165 if(fputc('\r', out) < 0)
170 if(fputc(*ptr, out) < 0)
174 if(fputs("\r\n", out) < 0)
176 if(fprintf(out, ".\r\n") < 0
179 if((rc = getresponse(tag, in)) / 100 != 2)
184 /** @brief Send a mail message
185 * @param sender Sender address (can be "")
186 * @param pubsender Visible sender address (must not be "")
187 * @param recipient Recipient address
188 * @param subject Subject string
189 * @param encoding Body encoding
190 * @param content_type Content-type of body
191 * @param body Text of body (encoded, but \n for newline)
192 * @return 0 on success, non-0 on error
194 * See mime_encode_text() for encoding of text bodies.
196 int sendmail(const char *sender,
197 const char *pubsender,
198 const char *recipient,
200 const char *encoding,
201 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 */
220 s[0] = config->smtp_server;
221 s[1] = (char *)"smtp";
222 if(!(ai = get_address(&a, &pref, &tag)))
224 fdin = xsocket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
225 if(connect(fdin, ai->ai_addr, ai->ai_addrlen) < 0) {
226 error(errno, "error connecting to %s", tag);
230 if((fdout = dup(fdin)) < 0)
231 fatal(errno, "error calling dup2");
232 if(!(in = fdopen(fdin, "rb")))
233 fatal(errno, "error calling fdopen");
234 if(!(out = fdopen(fdout, "wb")))
235 fatal(errno, "error calling fdopen");
236 rc = sendmailfp(tag, in, out, sender, pubsender, recipient, subject,
237 encoding, content_type, body);
243 /** @brief Start a subproces to send a mail message
244 * @param sender Sender address (can be "")
245 * @param pubsender Visible sender address (must not be "")
246 * @param recipient Recipient address
247 * @param subject Subject string
248 * @param encoding Body encoding
249 * @param content_type Content-type of body
250 * @param body Text of body (encoded, but \n for newline)
251 * @return Subprocess PID on success, -1 on error
253 pid_t sendmail_subprocess(const char *sender,
254 const char *pubsender,
255 const char *recipient,
257 const char *encoding,
258 const char *content_type,
262 if(!(pid = fork())) {
264 if(sendmail(sender, pubsender, recipient, subject,
265 encoding, content_type, body))
270 error(errno, "error calling fork");