1 /* A little hacked up program that listens on a given port and allows a script
2 to play the part of a remote MTA for testing purposes. This scripted version is
3 hacked from my original interactive version. A further hack allows it to listen
4 on a Unix domain socket as an alternative to a TCP/IP port.
6 In an IPv6 world, listening happens on both an IPv6 and an IPv4 socket, always
7 on all interfaces, unless the option -noipv6 is given. */
9 /* ANSI C standard includes */
24 #include <sys/types.h>
26 #include <netinet/in_systm.h>
27 #include <netinet/in.h>
28 #include <netinet/ip.h>
30 #ifdef HAVE_NETINET_IP_VAR_H
31 #include <netinet/ip_var.h>
35 #include <arpa/inet.h>
37 #include <sys/resource.h>
38 #include <sys/socket.h>
50 # define S_ADDR_TYPE u_long
64 /*************************************************
65 * SIGALRM handler - crash out *
66 *************************************************/
69 sigalrm_handler(int sig)
71 sig = sig; /* Keep picky compilers happy */
72 printf("\nServer timed out\n");
77 /*************************************************
78 * Get textual IP address *
79 *************************************************/
81 /* This function is copied from Exim */
84 host_ntoa(const void *arg, char *buffer)
88 /* The new world. It is annoying that we have to fish out the address from
89 different places in the block, depending on what kind of address it is. It
90 is also a pain that inet_ntop() returns a const char *, whereas the IPv4
91 function inet_ntoa() returns just char *, and some picky compilers insist
92 on warning if one assigns a const char * to a char *. Hence the casts. */
96 int family = ((struct sockaddr *)arg)->sa_family;
97 if (family == AF_INET6)
99 struct sockaddr_in6 *sk = (struct sockaddr_in6 *)arg;
100 yield = (char *)inet_ntop(family, &(sk->sin6_addr), addr_buffer,
101 sizeof(addr_buffer));
105 struct sockaddr_in *sk = (struct sockaddr_in *)arg;
106 yield = (char *)inet_ntop(family, &(sk->sin_addr), addr_buffer,
107 sizeof(addr_buffer));
110 /* If the result is a mapped IPv4 address, show it in V4 format. */
112 if (strncmp(yield, "::ffff:", 7) == 0) yield += 7;
114 #else /* HAVE_IPV6 */
118 yield = inet_ntoa(((struct sockaddr_in *)arg)->sin_addr);
121 strcpy(buffer, yield);
126 /*************************************************
128 *************************************************/
130 #define v6n 0 /* IPv6 socket number */
131 #define v4n 1 /* IPv4 socket number */
132 #define udn 2 /* Unix domain socket number */
133 #define skn 2 /* Potential number of sockets */
135 int main(int argc, char **argv)
139 int listen_socket[3] = { -1, -1, -1 };
141 int dup_accept_socket;
142 int connection_count = 1;
155 char *sockname = NULL;
156 unsigned char buffer[10240];
158 struct sockaddr_un sockun; /* don't use "sun" */
159 struct sockaddr_un sockun_accepted;
160 int sockun_len = sizeof(sockun_accepted);
163 struct sockaddr_in6 sin6;
164 struct sockaddr_in6 accepted;
165 struct in6_addr anyaddr6 = IN6ADDR_ANY_INIT ;
167 struct sockaddr_in accepted;
170 /* Always need an IPv4 structure */
172 struct sockaddr_in sin4;
174 int len = sizeof(accepted);
177 /* Sort out the arguments */
179 while (na < argc && argv[na][0] == '-')
181 if (strcmp(argv[na], "-d") == 0) debug = 1;
182 else if (strcmp(argv[na], "-t") == 0) timeout = atoi(argv[++na]);
183 else if (strcmp(argv[na], "-noipv4") == 0) use_ipv4 = 0;
184 else if (strcmp(argv[na], "-noipv6") == 0) use_ipv6 = 0;
187 printf("server: unknown option %s\n", argv[na]);
193 if (!use_ipv4 && !use_ipv6)
195 printf("server: -noipv4 and -noipv6 cannot both be given\n");
201 printf("server: no port number or socket name given\n");
205 if (argv[na][0] == '/')
208 unlink(sockname); /* in case left lying around */
210 else port = atoi(argv[na]);
213 if (na < argc) connection_count = atoi(argv[na]);
218 if (port == 0) /* Unix domain */
220 if (debug) printf("Creating Unix domain socket\n");
221 listen_socket[udn] = socket(PF_UNIX, SOCK_STREAM, 0);
222 if (listen_socket[udn] < 0)
224 printf("Unix domain socket creation failed: %s\n", strerror(errno));
233 if (debug) printf("Creating IPv6 socket\n");
234 listen_socket[v6n] = socket(AF_INET6, SOCK_STREAM, 0);
235 if (listen_socket[v6n] < 0)
237 printf("IPv6 socket creation failed: %s\n", strerror(errno));
241 /* If this is an IPv6 wildcard socket, set IPV6_V6ONLY if that option is
245 if (setsockopt(listen_socket[v6n], IPPROTO_IPV6, IPV6_V6ONLY, (char *)(&on),
247 printf("Setting IPV6_V6ONLY on IPv6 wildcard "
248 "socket failed (%s): carrying on without it\n", strerror(errno));
249 #endif /* IPV6_V6ONLY */
251 #endif /* HAVE_IPV6 */
253 /* Create an IPv4 socket if required */
257 if (debug) printf("Creating IPv4 socket\n");
258 listen_socket[v4n] = socket(AF_INET, SOCK_STREAM, 0);
259 if (listen_socket[v4n] < 0)
261 printf("IPv4 socket creation failed: %s\n", strerror(errno));
268 /* Set SO_REUSEADDR on the IP sockets so that the program can be restarted
269 while a connection is being handled - this can happen as old connections lie
270 around for a bit while crashed processes are tidied away. Without this, a
271 connection will prevent reuse of the smtp port for listening. */
273 for (i = v6n; i <= v4n; i++)
275 if (listen_socket[i] >= 0 &&
276 setsockopt(listen_socket[i], SOL_SOCKET, SO_REUSEADDR, (char *)(&on),
279 printf("setting SO_REUSEADDR on socket failed: %s\n", strerror(errno));
285 /* Now bind the sockets to the required port or path. If a path, ensure
286 anyone can write to it. */
291 sockun.sun_family = AF_UNIX;
292 if (debug) printf("Binding Unix domain socket\n");
293 sprintf(sockun.sun_path, "%.*s", (int)(sizeof(sockun.sun_path)-1), sockname);
294 if (bind(listen_socket[udn], (struct sockaddr *)&sockun, sizeof(sockun)) < 0)
296 printf("Unix domain socket bind() failed: %s\n", strerror(errno));
299 (void)stat(sockname, &statbuf);
300 if (debug) printf("Setting Unix domain socket mode: %0x\n",
301 statbuf.st_mode | 0777);
302 if (chmod(sockname, statbuf.st_mode | 0777) < 0)
304 printf("Unix domain socket chmod() failed: %s\n", strerror(errno));
311 for (i = 0; i < skn; i++)
313 if (listen_socket[i] < 0) continue;
315 /* For an IPv6 listen, use an IPv6 socket */
320 memset(&sin6, 0, sizeof(sin6));
321 sin6.sin6_family = AF_INET6;
322 sin6.sin6_port = htons(port);
323 sin6.sin6_addr = anyaddr6;
324 if (bind(listen_socket[i], (struct sockaddr *)&sin6, sizeof(sin6)) < 0)
326 printf("IPv6 socket bind() failed: %s\n", strerror(errno));
333 /* For an IPv4 bind, use an IPv4 socket, even in an IPv6 world. If an IPv4
334 bind fails EADDRINUSE after IPv6 success, carry on, because it means the
335 IPv6 socket will handle IPv4 connections. */
338 memset(&sin4, 0, sizeof(sin4));
339 sin4.sin_family = AF_INET;
340 sin4.sin_addr.s_addr = (S_ADDR_TYPE)INADDR_ANY;
341 sin4.sin_port = htons(port);
342 if (bind(listen_socket[i], (struct sockaddr *)&sin4, sizeof(sin4)) < 0)
344 if (listen_socket[v6n] < 0 || errno != EADDRINUSE)
346 printf("IPv4 socket bind() failed: %s\n", strerror(errno));
351 close(listen_socket[i]);
352 listen_socket[i] = -1;
360 /* Start listening. If IPv4 fails EADDRINUSE after IPv6 succeeds, ignore the
361 error because it means that the IPv6 socket will handle IPv4 connections. Don't
362 output anything, because it will mess up the test output, which will be
363 different for systems that do this and those that don't. */
365 for (i = 0; i <= skn; i++)
367 if (listen_socket[i] >= 0 && listen(listen_socket[i], 5) < 0)
369 if (i != v4n || listen_socket[v6n] < 0 || errno != EADDRINUSE)
371 printf("listen() failed: %s\n", strerror(errno));
378 /* This program handles only a fixed number of connections, in sequence. Before
379 waiting for the first connection, read the standard input, which contains the
380 script of things to do. A line containing "++++" is treated as end of file.
381 This is so that the Perl driving script doesn't have to close the pipe -
382 because that would cause it to wait for this process, which it doesn't yet want
383 to do. The driving script adds the "++++" automatically - it doesn't actually
384 appear in the test script. */
386 while (fgets(CS buffer, sizeof(buffer), stdin) != NULL)
389 int n = (int)strlen(CS buffer);
390 while (n > 0 && isspace(buffer[n-1])) n--;
392 if (strcmp(CS buffer, "++++") == 0) break;
393 next = malloc(sizeof(line) + n);
395 strcpy(next->line, CS buffer);
396 if (last == NULL) script = last = next;
397 else last->next = next;
403 /* SIGALRM handler crashes out */
405 signal(SIGALRM, sigalrm_handler);
407 /* s points to the current place in the script */
411 for (count = 0; count < connection_count; count++)
416 printf("Listening on %s ... ", sockname);
418 accept_socket = accept(listen_socket[udn],
419 (struct sockaddr *)&sockun_accepted, &sockun_len);
426 fd_set select_listen;
428 printf("Listening on port %d ... ", port);
431 FD_ZERO(&select_listen);
432 for (i = 0; i < skn; i++)
434 if (listen_socket[i] >= 0) FD_SET(listen_socket[i], &select_listen);
435 if (listen_socket[i] > max_socket) max_socket = listen_socket[i];
438 lcount = select(max_socket + 1, &select_listen, NULL, NULL, NULL);
441 printf("Select failed\n");
447 for (i = 0; i < skn; i++)
449 if (listen_socket[i] > 0 && FD_ISSET(listen_socket[i], &select_listen))
451 accept_socket = accept(listen_socket[i],
452 (struct sockaddr *)&accepted, &len);
453 FD_CLR(listen_socket[i], &select_listen);
460 if (accept_socket < 0)
462 printf("accept() failed: %s\n", strerror(errno));
466 out = fdopen(accept_socket, "w");
468 dup_accept_socket = dup(accept_socket);
471 printf("\nConnection request from [%s]\n", host_ntoa(&accepted, CS buffer));
474 printf("\nConnection request\n");
476 /* Linux supports a feature for acquiring the peer's credentials, but it
477 appears to be Linux-specific. This code is untested and unused, just
478 saved here for reference. */
480 /**********--------------------
484 if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &cl)==0) {
485 printf("Peer's pid=%d, uid=%d, gid=%d\n",
486 cr.pid, cr.uid, cr.gid);
487 --------------*****************/
490 if (dup_accept_socket < 0)
492 printf("Couldn't dup socket descriptor\n");
493 printf("421 Connection refused: %s\n", strerror(errno));
494 fprintf(out, "421 Connection refused: %s\r\n", strerror(errno));
499 in = fdopen(dup_accept_socket, "r");
501 /* Loop for handling the conversation(s). For use in SMTP sessions, there are
502 default rules for determining input and output lines: the latter start with
503 digits. This means that the input looks like SMTP dialog. However, this
504 doesn't work for other tests (e.g. ident tests) so we have explicit '<' and
505 '>' flags for input and output as well as the defaults. */
507 for (; s != NULL; s = s->next)
511 /* Output lines either start with '>' or a digit. In the '>' case we can
512 fudge the sending of \r\n as required. Default is \r\n, ">>" send nothing,
513 ">CR>" sends \r only, and ">LF>" sends \n only. We can also force a
514 connection closedown by ">*eof". */
519 printf("%s\n", ss++);
521 if (strncmp(ss, "*eof", 4) == 0)
529 else if (strncmp(ss, "CR>", 3) == 0)
530 { end = "\r"; ss += 3; }
531 else if (strncmp(ss, "LF>", 3) == 0)
532 { end = "\n"; ss += 3; }
534 fprintf(out, "%s%s", ss, end);
537 else if (isdigit((unsigned char)ss[0]))
540 fprintf(out, "%s\r\n", ss);
543 /* If the script line starts with "*sleep" we just sleep for a while
544 before continuing. */
546 else if (strncmp(ss, "*sleep ", 7) == 0)
548 int sleepfor = atoi(ss+7);
554 /* Otherwise the script line is the start of an input line we are expecting
555 from the client, or "*eof" indicating we expect the client to close the
556 connection. Read command line or data lines; the latter are indicated
557 by the expected line being just ".". If the line starts with '<', that
558 doesn't form part of the expected input. (This allows for incoming data
559 starting with a digit.) */
564 int data = strcmp(ss, ".") == 0;
579 if (fgets(CS buffer+offset, sizeof(buffer)-offset, in) == NULL)
581 printf("%sxpected EOF read from client\n",
582 (strncmp(ss, "*eof", 4) == 0)? "E" : "Une");
587 n = (int)strlen(CS buffer);
588 while (n > 0 && isspace(buffer[n-1])) n--;
590 printf("%s\n", buffer);
591 if (!data || strcmp(CS buffer, ".") == 0) break;
594 if (strncmp(ss, CS buffer, (int)strlen(ss)) != 0)
596 printf("Comparison failed - bailing out\n");
597 printf("Expected: %s\n", ss);
608 if (s == NULL) printf("End of script\n");
610 if (sockname != NULL) unlink(sockname);
614 /* End of server.c */