]> git.notmuchmail.org Git - notmuch/blob - test/smtp-dummy.c
Merge branch 'release'
[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 https://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 <sys/types.h>
41 #include <sys/socket.h>
42 #include <netinet/in.h>
43 #include <netdb.h>
44 #include <unistd.h>
45
46 #define STRNCMP_LITERAL(var, literal) \
47     strncmp ((var), (literal), sizeof (literal) - 1)
48
49 static void
50 receive_data_to_file (FILE *peer, FILE *output)
51 {
52     char *line = NULL;
53     size_t line_size;
54     ssize_t line_len;
55
56     while ((line_len = getline (&line, &line_size, peer)) != -1) {
57         if (STRNCMP_LITERAL (line, ".\r\n") == 0)
58             break;
59         if (line_len < 2)
60             continue;
61         if (line[line_len - 1] == '\n' && line[line_len - 2] == '\r') {
62             line[line_len - 2] = '\n';
63             line[line_len - 1] = '\0';
64         }
65         fprintf (output, "%s",
66                  line[0] == '.' ? line + 1 : line);
67     }
68
69     free (line);
70 }
71
72 static int
73 process_command (FILE *peer, FILE *output, const char *command)
74 {
75     if (STRNCMP_LITERAL (command, "EHLO ") == 0) {
76         fprintf (peer, "502 not implemented\r\n");
77         fflush (peer);
78     } else if (STRNCMP_LITERAL (command, "HELO ") == 0) {
79         fprintf (peer, "250 localhost\r\n");
80         fflush (peer);
81     } else if (STRNCMP_LITERAL (command, "MAIL FROM:") == 0 ||
82                STRNCMP_LITERAL (command, "RCPT TO:") == 0) {
83         fprintf (peer, "250 OK\r\n");
84         fflush (peer);
85     } else if (STRNCMP_LITERAL (command, "DATA") == 0) {
86         fprintf (peer, "354 End data with <CR><LF>.<CR><LF>\r\n");
87         fflush (peer);
88         receive_data_to_file (peer, output);
89         fprintf (peer, "250 OK\r\n");
90         fflush (peer);
91     } else if (STRNCMP_LITERAL (command, "QUIT") == 0) {
92         fprintf (peer, "221 BYE\r\n");
93         fflush (peer);
94         return 1;
95     } else {
96         fprintf (stderr, "Unknown command: %s\n", command);
97     }
98     return 0;
99 }
100
101 static void
102 do_smtp_to_file (FILE *peer, FILE *output)
103 {
104     char *line = NULL;
105     size_t line_size;
106     ssize_t line_len;
107
108     fprintf (peer, "220 localhost smtp-dummy\r\n");
109     fflush (peer);
110
111     while ((line_len = getline (&line, &line_size, peer)) != -1) {
112         if (process_command (peer, output, line))
113             break;
114     }
115
116     free (line);
117 }
118
119 int
120 main (int argc, char *argv[])
121 {
122     const char *progname;
123     char *output_filename;
124     FILE *peer_file = NULL, *output = NULL;
125     int sock = -1, peer, err;
126     struct sockaddr_in addr, peer_addr;
127     struct hostent *hostinfo;
128     socklen_t peer_addr_len;
129     int reuse;
130     int background;
131     int ret = 0;
132     socklen_t addrlen;
133
134     progname = argv[0];
135
136     background = 0;
137     for (; argc >= 2; argc--, argv++) {
138         if (argv[1][0] != '-')
139             break;
140         if (strcmp (argv[1], "--") == 0) {
141             argc--;
142             argv++;
143             break;
144         }
145         if (strcmp (argv[1], "--background") == 0) {
146             background = 1;
147             continue;
148         }
149         fprintf (stderr, "%s: unregognized option '%s'\n",
150                  progname, argv[1]);
151         return 1;
152     }
153
154     if (argc != 2) {
155         fprintf (stderr,
156                  "Usage: %s [--background] <output-file>\n", progname);
157         return 1;
158     }
159
160     output_filename = argv[1];
161     output = fopen (output_filename, "w");
162     if (output == NULL) {
163         fprintf (stderr, "Failed to open %s for writing: %s\n",
164                  output_filename, strerror (errno));
165         ret = 1;
166         goto DONE;
167     }
168
169     sock = socket (AF_INET, SOCK_STREAM, 0);
170     if (sock == -1) {
171         fprintf (stderr, "Error: socket() failed: %s\n",
172                  strerror (errno));
173         ret = 1;
174         goto DONE;
175     }
176
177     reuse = 1;
178     err = setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof (reuse));
179     if (err) {
180         fprintf (stderr, "Error: setsockopt() failed: %s\n",
181                  strerror (errno));
182         ret = 1;
183         goto DONE;
184     }
185
186     hostinfo = gethostbyname ("localhost");
187     if (hostinfo == NULL) {
188         fprintf (stderr, "Unknown host: localhost\n");
189         ret = 1;
190         goto DONE;
191     }
192
193     memset (&addr, 0, sizeof (addr));
194     addr.sin_family = AF_INET;
195     addr.sin_port = 0;
196     addr.sin_addr = *(struct in_addr *) hostinfo->h_addr;
197     err = bind (sock, (struct sockaddr *) &addr, sizeof (addr));
198     if (err) {
199         fprintf (stderr, "Error: bind() failed: %s\n",
200                  strerror (errno));
201         close (sock);
202         ret = 1;
203         goto DONE;
204     }
205
206     addrlen = sizeof (addr);
207     err = getsockname (sock, (struct sockaddr *) &addr, &addrlen);
208     if (err) {
209         fprintf (stderr, "Error: getsockname() failed: %s\n",
210                  strerror (errno));
211         close (sock);
212         ret = 1;
213         goto DONE;
214     }
215
216     printf ("smtp_dummy_port='%d'\n", ntohs (addr.sin_port));
217
218     err = listen (sock, 1);
219     if (err) {
220         fprintf (stderr, "Error: listen() failed: %s\n",
221                  strerror (errno));
222         close (sock);
223         ret = 1;
224         goto DONE;
225     }
226
227     if (background) {
228         int pid = fork ();
229         if (pid > 0) {
230             printf ("smtp_dummy_pid='%d'\n", pid);
231             fflush (stdout);
232             close (sock);
233             ret = 0;
234             goto DONE;
235         }
236         if (pid < 0) {
237             fprintf (stderr, "Error: fork() failed: %s\n",
238                      strerror (errno));
239             close (sock);
240             ret = 1;
241             goto DONE;
242         }
243         /* Reached if pid == 0 (the child process). */
244         /* Close stdout so that the one interested in pid value will
245          * also get EOF. */
246         close (STDOUT_FILENO);
247         /* dup2() will re-reserve fd of stdout (1) (opportunistically),
248          * in case fd of stderr (2) is open. If that was not open we
249          * don't care fd of stdout (1) either. */
250         dup2 (STDERR_FILENO, STDOUT_FILENO);
251
252         /* This process is now out of reach of shell's job control.
253          * To resolve the rare but possible condition where this
254          * "daemon" is started but never connected this process will
255          * (only) have 30 seconds to exist. */
256         alarm (30);
257     }
258
259     peer_addr_len = sizeof (peer_addr);
260     peer = accept (sock, (struct sockaddr *) &peer_addr, &peer_addr_len);
261     if (peer == -1) {
262         fprintf (stderr, "Error: accept() failed: %s\n",
263                  strerror (errno));
264         ret = 1;
265         goto DONE;
266     }
267
268     peer_file = fdopen (peer, "w+");
269     if (peer_file == NULL) {
270         fprintf (stderr, "Error: fdopen() failed: %s\n",
271                  strerror (errno));
272         ret = 1;
273         goto DONE;
274     }
275
276     do_smtp_to_file (peer_file, output);
277
278  DONE:
279     if (output)
280         fclose (output);
281     if (peer_file)
282         fclose (peer_file);
283     if (sock >= 0)
284         close (sock);
285
286     return ret;
287 }