unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
blob aa82fa156a93d6b0a8550ce1a7cbac8e59c45a34 5351 bytes (raw)
name: test/smtp-dummy.c 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
 
/* smtp-dummy - Dummy SMTP server that delivers mail to the given file
 *
 * Copyright © 2010 Carl Worth
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see http://www.gnu.org/licenses/ .
 *
 * Authors: Carl Worth <cworth@cworth.org>
 */

/* This (non-compliant) SMTP server listens on localhost, port 25025
 * and delivers a mail received to the given filename, (specified as a
 * command-line argument). It exists after the first client connection
 * completes.
 *
 * It implements very little of the SMTP protocol, even less than
 * specified as the minimum implementation in the SMTP RFC, (not
 * implementing RSET, NOOP, nor VRFY). And it doesn't do any
 * error-checking on the input.
 *
 * That is to say, if you use this program, you will very likely find
 * cases where it doesn't do everything your SMTP client expects. You
 * have been warned.
 */

#define _GNU_SOURCE /* for getline */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>

#define STRNCMP_LITERAL(var, literal) \
    strncmp ((var), (literal), sizeof (literal) - 1)

static void
receive_data_to_file (FILE *peer, FILE *output)
{
	char *line = NULL;
	size_t line_size;
	ssize_t line_len;

	while ((line_len = getline (&line, &line_size, peer)) != -1) {
		if (STRNCMP_LITERAL (line, ".\r\n") == 0)
			break;
		if (line_len < 2)
			continue;
		if (line[line_len-1] == '\n' && line[line_len-2] == '\r') {
			line[line_len-2] = '\n';
			line[line_len-1] = '\0';
		}
		fprintf (output, "%s",
			 line[0] == '.' ? line + 1 : line);
	}

	free (line);
}

static int
process_command (FILE *peer, FILE *output, const char *command)
{
	if (STRNCMP_LITERAL (command, "EHLO ") == 0) {
		fprintf (peer, "502 not implemented\r\n");
		fflush (peer);
	} else if (STRNCMP_LITERAL (command, "HELO ") == 0) {
		fprintf (peer, "250 localhost\r\n");
		fflush (peer);
	} else if (STRNCMP_LITERAL (command, "MAIL FROM:") == 0 ||
		   STRNCMP_LITERAL (command, "RCPT TO:") == 0) {
		fprintf (peer, "250 OK\r\n");
		fflush (peer);
	} else if (STRNCMP_LITERAL (command, "DATA") == 0) {
		fprintf (peer, "354 End data with <CR><LF>.<CR><LF>\r\n");
		fflush (peer);
		receive_data_to_file (peer, output);
		fprintf (peer, "250 OK\r\n");
		fflush (peer);
	} else if (STRNCMP_LITERAL (command, "QUIT") == 0) {
		fprintf (peer, "221 BYE\r\n");
		fflush (peer);
		return 1;
	} else {
		fprintf (stderr, "Unknown command: %s\n", command);
	}
	return 0;
}

static void
do_smtp_to_file (FILE *peer, FILE *output)
{
	char *line = NULL;
	size_t line_size;
	ssize_t line_len;

	fprintf (peer, "220 localhost smtp-dummy\r\n");
	fflush (peer);

	while ((line_len = getline (&line, &line_size, peer)) != -1) {
		if (process_command (peer, output, line))
			break;
	}

	free (line);
}

int
main (int argc, char *argv[])
{
	char *output_filename;
	FILE *peer_file, *output;
	int sock, peer, err;
	struct sockaddr_in addr, peer_addr;
	struct hostent *hostinfo;
	socklen_t peer_addr_len;
	int reuse;

	if (argc != 2) {
		fprintf (stderr, "Usage: %s <output-file>\n", argv[0]);
		return 1;
	}

	output_filename = argv[1];
	output = fopen (output_filename, "w");
	if (output == NULL) {
		fprintf (stderr, "Failed to open %s for writing: %s\n",
			 output_filename, strerror (errno));
		return 1;
	}

	sock = socket (AF_INET, SOCK_STREAM, 0);
	if (sock == -1) {
		fprintf (stderr, "Error: socket() failed: %s\n",
			 strerror (errno));
		return 1;
	}

	reuse = 1;
	err = setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof (reuse));
	if (err) {
		fprintf (stderr, "Error: setsockopt() failed: %s\n",
			 strerror (errno));
		return 1;
	}

	hostinfo = gethostbyname ("localhost");
	if (hostinfo == NULL) {
		fprintf (stderr, "Unknown host: localhost\n");
		return 1;
	}

	memset (&addr, 0, sizeof (addr));
	addr.sin_family = AF_INET;
	addr.sin_port = htons (25025);
	addr.sin_addr = *(struct in_addr *) hostinfo->h_addr;
	err = bind (sock, (struct sockaddr *) &addr, sizeof(addr));
	if (err) {
		fprintf (stderr, "Error: bind() failed: %s\n",
			 strerror (errno));
		close (sock);
		return 1;
	}

	err = listen (sock, 1);
	if (err) {
		fprintf (stderr, "Error: listen() failed: %s\n",
			 strerror (errno));
		close (sock);
		return 1;
	}

	peer_addr_len = sizeof (peer_addr);
	peer = accept (sock, (struct sockaddr *) &peer_addr, &peer_addr_len);
	if (peer == -1) {
		fprintf (stderr, "Error: accept() failed: %s\n",
			 strerror (errno));
		return 1;
	}

	peer_file = fdopen (peer, "w+");
	if (peer_file == NULL) {
		fprintf (stderr, "Error: fdopen() failed: %s\n",
			 strerror (errno));
		return 1;
	}

	do_smtp_to_file (peer_file, output);

	fclose (output);
	fclose (peer_file);
	close (sock);

	return 0;
}

debug log:

solving aa82fa1 ...
found aa82fa1 in https://yhetil.org/notmuch/1337953411-21000-4-git-send-email-pioto@pioto.org/
found 3801a5e in https://yhetil.org/notmuch.git/
preparing index
index prepared:
100644 3801a5e066c7c8fe1804ca3306e250fb0eb962f7	test/smtp-dummy.c

applying [1/1] https://yhetil.org/notmuch/1337953411-21000-4-git-send-email-pioto@pioto.org/
diff --git a/test/smtp-dummy.c b/test/smtp-dummy.c
index 3801a5e..aa82fa1 100644

Checking patch test/smtp-dummy.c...
Applied patch test/smtp-dummy.c cleanly.

index at:
100644 aa82fa156a93d6b0a8550ce1a7cbac8e59c45a34	test/smtp-dummy.c

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).