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
27 #include <sys/socket.h>
32 #include <netinet/in.h>
36 #include "configuration.h"
37 #include "inputline.h"
43 /** @brief Get a server response
44 * @param tag Server name
45 * @param in Input stream
46 * @return Response code 0-999 or -1 on error
48 static int getresponse(const char *tag
, FILE *in
) {
51 while(!inputline(tag
, in
, &line
, CRLF
)) {
52 if(line
[0] >= '0' && line
[0] <= '9'
53 && line
[1] >= '0' && line
[1] <= '9'
54 && line
[2] >= '0' && line
[2] <= '9') {
55 const int rc
= 10 * (10 * line
[0] + line
[1]) + line
[2] - 111 * '0';
56 if(rc
>= 400 && rc
<= 599)
57 error(0, "%s: %s", tag
, line
);
61 /* else go round for further response lines */
63 error(0, "%s: malformed response: %s", tag
, line
);
68 error(errno
, "%s: read error", tag
);
70 error(0, "%s: server closed connection", tag
);
74 /** @brief Send a command to the server
75 * @param tag Server name
76 * @param out stream to send commands to
77 * @param fmt Format string
78 * @return 0 on success, non-0 on error
80 static int sendcommand(const char *tag
, FILE *out
, const char *fmt
, ...) {
85 rc
= vfprintf(out
, fmt
, ap
);
88 rc
= fputs("\r\n", out
);
93 error(errno
, "%s: write error", tag
);
97 /** @brief Send a mail message
98 * @param tag Server name
99 * @param in Stream to read responses from
100 * @param out Stream to send commands to
101 * @param sender Sender address (can be "")
102 * @param pubsender Visible sender address (must not be "")
103 * @param recipient Recipient address
104 * @param subject Subject string
105 * @param encoding Body encoding
106 * @param content_type Content-type of body
107 * @param body Text of body (encoded, but \n for newline)
108 * @return 0 on success, non-0 on error
110 static int sendmailfp(const char *tag
, FILE *in
, FILE *out
,
112 const char *pubsender
,
113 const char *recipient
,
115 const char *encoding
,
116 const char *content_type
,
128 strftime(date
, sizeof date
, "%a, %d %b %Y %H:%M:%S +0000", &ut
);
129 gcry_create_nonce(idbuf
, sizeof idbuf
);
130 id
= mime_to_base64(idbuf
, sizeof idbuf
);
131 if((rc
= getresponse(tag
, in
)) / 100 != 2)
133 if(sendcommand(tag
, out
, "HELO %s", local_hostname()))
135 if((rc
= getresponse(tag
, in
)) / 100 != 2)
137 if(sendcommand(tag
, out
, "MAIL FROM:<%s>", sender
))
139 if((rc
= getresponse(tag
, in
)) / 100 != 2)
141 if(sendcommand(tag
, out
, "RCPT TO:<%s>", recipient
))
143 if((rc
= getresponse(tag
, in
)) / 100 != 2)
145 if(sendcommand(tag
, out
, "DATA", sender
))
147 if((rc
= getresponse(tag
, in
)) / 100 != 3)
149 if(fprintf(out
, "From: %s\r\n", pubsender
) < 0
150 || fprintf(out
, "To: %s\r\n", recipient
) < 0
151 || fprintf(out
, "Subject: %s\r\n", subject
) < 0
152 || fprintf(out
, "Message-ID: <%s@%s>\r\n", id
, local_hostname()) < 0
153 || fprintf(out
, "MIME-Version: 1.0\r\n") < 0
154 || fprintf(out
, "Content-Type: %s\r\n", content_type
) < 0
155 || fprintf(out
, "Content-Transfer-Encoding: %s\r\n", encoding
) < 0
156 || fprintf(out
, "Date: %s\r\n", date
) < 0
157 || fprintf(out
, "\r\n") < 0) {
159 error(errno
, "%s: write error", tag
);
162 for(ptr
= body
; *ptr
; ++ptr
) {
163 if(sol
&& *ptr
== '.')
164 if(fputc('.', out
) < 0)
167 if(fputc('\r', out
) < 0)
172 if(fputc(*ptr
, out
) < 0)
176 if(fputs("\r\n", out
) < 0)
178 if(fprintf(out
, ".\r\n") < 0
181 if((rc
= getresponse(tag
, in
)) / 100 != 2)
186 /** @brief Send a mail message
187 * @param sender Sender address (can be "")
188 * @param pubsender Visible sender address (must not be "")
189 * @param recipient Recipient address
190 * @param subject Subject string
191 * @param encoding Body encoding
192 * @param content_type Content-type of body
193 * @param body Text of body (encoded, but \n for newline)
194 * @return 0 on success, non-0 on error
196 * See mime_encode_text() for encoding of text bodies.
198 int sendmail(const char *sender
,
199 const char *pubsender
,
200 const char *recipient
,
202 const char *encoding
,
203 const char *content_type
,
212 static const struct addrinfo pref
= {
214 .ai_family
= PF_INET
,
215 .ai_socktype
= SOCK_STREAM
,
216 .ai_protocol
= IPPROTO_TCP
,
219 /* Find the SMTP server */
222 s
[0] = config
->smtp_server
;
223 s
[1] = (char *)"smtp";
224 if(!(ai
= get_address(&a
, &pref
, &tag
)))
226 fdin
= xsocket(ai
->ai_family
, ai
->ai_socktype
, ai
->ai_protocol
);
227 if(connect(fdin
, ai
->ai_addr
, ai
->ai_addrlen
) < 0) {
228 error(errno
, "error connecting to %s", tag
);
232 if((fdout
= dup(fdin
)) < 0)
233 fatal(errno
, "error calling dup2");
234 if(!(in
= fdopen(fdin
, "rb")))
235 fatal(errno
, "error calling fdopen");
236 if(!(out
= fdopen(fdout
, "wb")))
237 fatal(errno
, "error calling fdopen");
238 rc
= sendmailfp(tag
, in
, out
, sender
, pubsender
, recipient
, subject
,
239 encoding
, content_type
, body
);
245 /** @brief Start a subproces to send a mail message
246 * @param sender Sender address (can be "")
247 * @param pubsender Visible sender address (must not be "")
248 * @param recipient Recipient address
249 * @param subject Subject string
250 * @param encoding Body encoding
251 * @param content_type Content-type of body
252 * @param body Text of body (encoded, but \n for newline)
253 * @return Subprocess PID on success, -1 on error
255 pid_t
sendmail_subprocess(const char *sender
,
256 const char *pubsender
,
257 const char *recipient
,
259 const char *encoding
,
260 const char *content_type
,
264 if(!(pid
= fork())) {
266 if(sendmail(sender
, pubsender
, recipient
, subject
,
267 encoding
, content_type
, body
))
272 error(errno
, "error calling fork");