File: fakesmtp.c

package info (click to toggle)
nmh 1.8-4
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 7,860 kB
  • sloc: ansic: 50,445; sh: 22,697; makefile: 1,138; lex: 740; perl: 509; yacc: 265
file content (195 lines) | stat: -rw-r--r-- 3,933 bytes parent folder | download | duplicates (3)
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
/* fakesmtp - A fake SMTP server used by the nmh test suite
 *
 * This code is Copyright (c) 2012, by the authors of nmh.  See the
 * COPYRIGHT file in the root directory of the nmh distribution for
 * complete copyright information.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>

#include "server.h"

#ifndef min
# define min(a,b) ((a) < (b) ? (a) : (b))
#endif

#define PIDFILE "/tmp/fakesmtp.pid"

#define LINESIZE 1024

enum {
	/* Processing top-level SMTP commands (e.g. EHLO, DATA). */
	SMTP_TOP,

	/* Processing payload of a DATA command. */
	SMTP_DATA,

	/* Looking for the blank line required by XOAUTH2 after 334 response. */
	SMTP_XOAUTH_ERR
};

static int getsmtp(int, char *);

int
main(int argc, char *argv[])
{
	int rc, conn, smtp_state;
	FILE *f;
	const char *xoauth = getenv("XOAUTH");
	const char *smtputf8 = getenv("SMTPUTF8");

	if (argc != 3) {
		fprintf(stderr, "Usage: %s output-filename port\n", argv[0]);
		exit(1);
	}

	if (!(f = fopen(argv[1], "w"))) {
		fprintf(stderr, "Unable to open output file \"%s\": %s\n",
			argv[1], strerror(errno));
		exit(1);
	}

	conn = serve(PIDFILE, argv[2]);

	/*
	 * Pretend to be an SMTP server.
	 */

	putcrlf(conn, "220 Not really an ESMTP server");
	smtp_state = SMTP_TOP;

	for (;;) {
		char line[LINESIZE];

		rc = getsmtp(conn, line);

		if (rc == -1)
			break;	/* EOF */

                fputs(line, f);
                putc('\n', f);

		switch (smtp_state) {
		case SMTP_DATA:
			if (strcmp(line, ".") == 0) {
				smtp_state = SMTP_TOP;
				putcrlf(conn, "250 Thanks for the info!");
			}
			continue;
		case SMTP_XOAUTH_ERR:
			smtp_state = SMTP_TOP;
			putcrlf(conn, "535 Not no way, not no how!");
			continue;
		}

		/*
		 * Most commands we ignore and send the same response to.
		 */

		if (strcmp(line, "QUIT") == 0) {
			fclose(f);
			f = NULL;
			putcrlf(conn, "221 Later alligator!");
			close(conn);
			break;
		}
		if (strcmp(line, "DATA") == 0) {
			putcrlf(conn, "354 Go ahead");
			smtp_state = SMTP_DATA;
			continue;
		}
		if (strncmp(line, "EHLO", 4) == 0) {
			putcrlf(conn, "250-ready");
			if (smtputf8 != NULL) {
				putcrlf(conn, "250-8BITMIME");
				putcrlf(conn, "250-SMTPUTF8");
			}
			if (xoauth != NULL) {
				putcrlf(conn, "250-AUTH XOAUTH2");
			}
			putcrlf(conn, "250 I'll buy that for a dollar!");
			continue;
		}
		if (xoauth != NULL) {
			/* XOAUTH2 support enabled; handle AUTH (and EHLO above). */
			if (strncmp(line, "AUTH", 4) == 0) {
				if (strncmp(line, "AUTH XOAUTH2", 12) == 0
				    && strstr(line, xoauth) != NULL) {
					putcrlf(conn, "235 OK come in");
					continue;
				}
				putcrlf(conn, "334 base64-json-err");
				smtp_state = SMTP_XOAUTH_ERR;
				continue;
			}
		}
		putcrlf(conn, "250 I'll buy that for a dollar!");
	}

	if (f)
		fclose(f);

	exit(0);
}

/*
 * Read a line (up to the \r\n)
 */

static int
getsmtp(int socket, char *data)
{
	int cc;
	static unsigned int bytesinbuf = 0;
	static char buffer[LINESIZE * 2], *p;

	for (;;) {
		/*
		 * Find our \r\n
		 */

		if (bytesinbuf > 0 && (p = strchr(buffer, '\r')) &&
							*(p + 1) == '\n') {
			*p = '\0';
			cc = strlen(buffer);
			memcpy(data, buffer, min(cc + 1, LINESIZE));
			data[LINESIZE - 1] = '\0';

			/*
			 * Shuffle leftover bytes back to the beginning
			 */

			bytesinbuf -= cc + 2;	/* Don't forget \r\n */
			if (bytesinbuf > 0) {
				memmove(buffer, buffer + cc + 2, bytesinbuf);
			}
			return cc;
		}

		if (bytesinbuf >= sizeof(buffer)) {
			fprintf(stderr, "Buffer overflow in getsmtp()!\n");
			exit(1);
		}

		memset(buffer + bytesinbuf, 0, sizeof(buffer) - bytesinbuf);
		cc = read(socket, buffer + bytesinbuf,
			  sizeof(buffer) - bytesinbuf);

		if (cc < 0) {
			fprintf(stderr, "Read failed: %s\n", strerror(errno));
			exit(1);
		}

		if (cc == 0)
			return -1;

		bytesinbuf += cc;
	}
}