1 /* smtp-dummy - Dummy SMTP server that delivers mail to the given file
3 * Copyright © 2010 Carl Worth
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/ .
18 * Authors: Carl Worth <cworth@cworth.org>
21 /* This (non-compliant) SMTP server listens on localhost, port 25025
22 * and delivers a mail received to the given filename, (specified as a
23 * command-line argument). It exists after the first client connection
26 * It implements very little of the SMTP protocol, even less than
27 * specified as the minimum implementation in the SMTP RFC, (not
28 * implementing RSET, NOOP, nor VRFY). And it doesn't do any
29 * error-checking on the input.
31 * That is to say, if you use this program, you will very likely find
32 * cases where it doesn't do everything your SMTP client expects. You
40 #include <netinet/ip.h>
44 #define STRNCMP_LITERAL(var, literal) \
45 strncmp ((var), (literal), sizeof (literal) - 1)
48 receive_data_to_file (FILE *peer, FILE *output)
54 while ((line_len = getline (&line, &line_size, peer)) != -1) {
55 if (STRNCMP_LITERAL (line, ".\r\n") == 0)
59 if (line[line_len-1] == '\n' && line[line_len-2] == '\r') {
60 line[line_len-2] = '\n';
61 line[line_len-1] = '\0';
63 fprintf (output, "%s",
64 line[0] == '.' ? line + 1 : line);
71 process_command (FILE *peer, FILE *output, const char *command)
73 if (STRNCMP_LITERAL (command, "EHLO ") == 0) {
74 fprintf (peer, "502 not implemented\r\n");
76 } else if (STRNCMP_LITERAL (command, "HELO ") == 0) {
77 fprintf (peer, "250 localhost\r\n");
79 } else if (STRNCMP_LITERAL (command, "MAIL FROM:") == 0 ||
80 STRNCMP_LITERAL (command, "RCPT TO:") == 0) {
81 fprintf (peer, "250 OK\r\n");
83 } else if (STRNCMP_LITERAL (command, "DATA") == 0) {
84 fprintf (peer, "354 End data with <CR><LF>.<CR><LF>\r\n");
86 receive_data_to_file (peer, output);
87 fprintf (peer, "250 OK\r\n");
89 } else if (STRNCMP_LITERAL (command, "QUIT") == 0) {
90 fprintf (peer, "221 BYE\r\n");
94 fprintf (stderr, "Unknown command: %s\n", command);
100 do_smtp_to_file (FILE *peer, FILE *output)
106 fprintf (peer, "220 localhost smtp-dummy\r\n");
109 while ((line_len = getline (&line, &line_size, peer)) != -1) {
110 if (process_command (peer, output, line))
118 main (int argc, char *argv[])
120 char *output_filename;
121 FILE *peer_file, *output;
123 struct sockaddr_in addr, peer_addr;
124 struct hostent *hostinfo;
125 socklen_t peer_addr_len;
129 fprintf (stderr, "Usage: %s <output-file>\n", argv[0]);
133 output_filename = argv[1];
134 output = fopen (output_filename, "w");
135 if (output == NULL) {
136 fprintf (stderr, "Failed to open %s for writing: %s\n",
137 output_filename, strerror (errno));
141 sock = socket (AF_INET, SOCK_STREAM, 0);
143 fprintf (stderr, "Error: socket() failed: %s\n",
149 err = setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof (reuse));
151 fprintf (stderr, "Error: setsockopt() failed: %s\n",
156 hostinfo = gethostbyname ("localhost");
157 if (hostinfo == NULL) {
158 fprintf (stderr, "Unknown host: localhost\n");
162 memset (&addr, 0, sizeof (addr));
163 addr.sin_family = AF_INET;
164 addr.sin_port = htons (25025);
165 addr.sin_addr = *(struct in_addr *) hostinfo->h_addr;
166 err = bind (sock, (struct sockaddr *) &addr, sizeof(addr));
168 fprintf (stderr, "Error: bind() failed: %s\n",
174 err = listen (sock, 1);
176 fprintf (stderr, "Error: listen() failed: %s\n",
182 peer_addr_len = sizeof (peer_addr);
183 peer = accept (sock, (struct sockaddr *) &peer_addr, &peer_addr_len);
185 fprintf (stderr, "Error: accept() failed: %s\n",
190 peer_file = fdopen (peer, "w+");
191 if (peer_file == NULL) {
192 fprintf (stderr, "Error: fdopen() failed: %s\n",
197 do_smtp_to_file (peer_file, output);