]> git.notmuchmail.org Git - notmuch/blob - test/smtp-dummy.c
test: allow specifying tests to run with NOTMUCH_TESTS env var
[notmuch] / test / smtp-dummy.c
1 /* smtp-dummy - Dummy SMTP server that delivers mail to the given file
2  *
3  * Copyright © 2010 Carl Worth
4  *
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.
9  *
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.
14  *
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/ .
17  *
18  * Authors: Carl Worth <cworth@cworth.org>
19  */
20
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
24  * completes.
25  *
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.
30  *
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
33  * have been warned.
34  */
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <errno.h>
40 #include <netinet/ip.h>
41 #include <netdb.h>
42 #include <unistd.h>
43
44 #define STRNCMP_LITERAL(var, literal) \
45     strncmp ((var), (literal), sizeof (literal) - 1)
46
47 static void
48 receive_data_to_file (FILE *peer, FILE *output)
49 {
50         char *line = NULL;
51         size_t line_size;
52         ssize_t line_len;
53
54         while ((line_len = getline (&line, &line_size, peer)) != -1) {
55                 if (STRNCMP_LITERAL (line, ".\r\n") == 0)
56                         break;
57                 if (line_len < 2)
58                         continue;
59                 if (line[line_len-1] == '\n' && line[line_len-2] == '\r') {
60                         line[line_len-2] = '\n';
61                         line[line_len-1] = '\0';
62                 }
63                 fprintf (output, "%s",
64                          line[0] == '.' ? line + 1 : line);
65         }
66
67         free (line);
68 }
69
70 static int
71 process_command (FILE *peer, FILE *output, const char *command)
72 {
73         if (STRNCMP_LITERAL (command, "EHLO ") == 0) {
74                 fprintf (peer, "502\r\n");
75                 fflush (peer);
76         } else if (STRNCMP_LITERAL (command, "HELO ") == 0) {
77                 fprintf (peer, "250 localhost\r\n");
78                 fflush (peer);
79         } else if (STRNCMP_LITERAL (command, "MAIL FROM:") == 0 ||
80                    STRNCMP_LITERAL (command, "RCPT TO:") == 0) {
81                 fprintf (peer, "250 OK\r\n");
82                 fflush (peer);
83         } else if (STRNCMP_LITERAL (command, "DATA") == 0) {
84                 fprintf (peer, "354 End data with <CR><LF>.<CR><LF>\r\n");
85                 fflush (peer);
86                 receive_data_to_file (peer, output);
87                 fprintf (peer, "250 OK\r\n");
88                 fflush (peer);
89         } else if (STRNCMP_LITERAL (command, "QUIT") == 0) {
90                 fprintf (peer, "221 BYE\r\n");
91                 fflush (peer);
92                 return 1;
93         } else {
94                 fprintf (stderr, "Unknown command: %s\n", command);
95         }
96         return 0;
97 }
98
99 static void
100 do_smtp_to_file (FILE *peer, FILE *output)
101 {
102         char buf[4096];
103         ssize_t bytes;
104         char greeting[] = "220 localhost smtp-dummy\r\n";
105         char *line = NULL;
106         size_t line_size;
107         ssize_t line_len;
108
109         fprintf (peer, "220 localhost smtp-dummy\r\n");
110         fflush (peer);
111
112         while ((line_len = getline (&line, &line_size, peer)) != -1) {
113                 if (process_command (peer, output, line))
114                         break;
115         }
116
117         free (line);
118 }
119
120 int
121 main (int argc, char *argv[])
122 {
123         char *output_filename;
124         FILE *peer_file, *output;
125         int sock, peer, err;
126         struct sockaddr_in addr, peer_addr;
127         struct hostent *hostinfo;
128         socklen_t peer_addr_len;
129         int reuse;
130
131         if (argc != 2) {
132                 fprintf (stderr, "Usage: %s <output-file>\n", argv[0]);
133                 exit (1);
134         }
135
136         output_filename = argv[1];
137         output = fopen (output_filename, "w");
138         if (output == NULL) {
139                 fprintf (stderr, "Failed to open %s for writing: %s\n",
140                          output_filename, strerror (errno));
141                 exit (1);
142         }
143
144         sock = socket (AF_INET, SOCK_STREAM, 0);
145         if (sock == -1) {
146                 fprintf (stderr, "Error: socket() failed: %s\n",
147                          strerror (errno));
148                 exit (1);
149         }
150
151         reuse = 1;
152         err = setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof (reuse));
153         if (err) {
154                 fprintf (stderr, "Error: setsockopt() failed: %s\n",
155                          strerror (errno));
156                 exit (1);
157         }
158
159         hostinfo = gethostbyname ("localhost");
160         if (hostinfo == NULL) {
161                 fprintf (stderr, "Unknown host: localhost\n");
162                 exit (1);
163         }
164
165         addr.sin_family = AF_INET;
166         addr.sin_port = htons (25025);
167         addr.sin_addr = *(struct in_addr *) hostinfo->h_addr;
168         err = bind (sock, (struct sockaddr *) &addr, sizeof(addr));
169         if (err) {
170                 fprintf (stderr, "Error: bind() failed: %s\n",
171                          strerror (errno));
172                 close (sock);
173                 exit (1);
174         }
175
176         err = listen (sock, 1);
177         if (err) {
178                 fprintf (stderr, "Error: listen() failed: %s\n",
179                          strerror (errno));
180                 close (sock);
181                 exit (1);
182         }
183
184         peer_addr_len = sizeof (peer_addr);
185         peer = accept (sock, (struct sockaddr *) &peer_addr, &peer_addr_len);
186         if (peer == -1) {
187                 fprintf (stderr, "Error: accept() failed: %s\n",
188                          strerror (errno));
189                 exit (1);
190         }
191
192         peer_file = fdopen (peer, "w+");
193         if (peer_file == NULL) {
194                 fprintf (stderr, "Error: fdopen() failed: %s\n",
195                          strerror (errno));
196                 return;
197         }
198
199         do_smtp_to_file (peer_file, output);
200
201         fclose (output);
202         fclose (peer_file);
203         close (sock);
204
205         return 0;
206 }