1 /* A little hacked up program that allows a script to play the part of a remote
2 SMTP/LMTP server on stdin/stdout for testing purposes. Hacked from the more
3 complicated version that does it over a socket. */
6 /* ANSI C standard includes */
26 /*************************************************
27 * SIGALRM handler - crash out *
28 *************************************************/
31 sigalrm_handler(int sig)
33 sig = sig; /* Keep picky compilers happy */
34 fprintf(log, "Server timed out\n");
40 /*************************************************
42 *************************************************/
44 int main(int argc, char **argv)
49 unsigned char sbuffer[1024];
50 unsigned char ibuffer[1024];
54 fprintf(stdout, "500 Script and log file required\n");
58 /* Get the script and log open */
60 script = fopen(argv[1], "r");
63 fprintf(stdout, "500 Failed to open script %s: %s\r\n", argv[1],
69 if (logfile[0] == '+')
75 log = fopen(logfile, logmode);
78 fprintf(stdout, "500 Failed to open log %s: %s\r\n", logfile,
83 /* SIGALRM handler crashes out */
85 signal(SIGALRM, sigalrm_handler);
87 /* Read the script, and do what it says. */
89 while (fgets(sbuffer, sizeof(sbuffer), script) != NULL)
91 int n = (int)strlen(sbuffer);
92 while (n > 0 && isspace(sbuffer[n-1])) n--;
95 /* If the script line starts with a digit, it is a response line which
98 if (isdigit(sbuffer[0]))
100 fprintf(log, "%s\n", sbuffer);
102 fprintf(stdout, "%s\r\n", sbuffer);
106 /* If the script line starts with "*sleep" we just sleep for a while
107 before continuing. Do not write this to the log, as it may not get
108 written at the right place in a log that's being shared. */
110 else if (strncmp(sbuffer, "*sleep ", 7) == 0)
112 sleep(atoi(sbuffer+7));
115 /* Otherwise the script line is the start of an input line we are expecting
116 from the client, or "*eof" indicating we expect the client to close the
117 connection. Read command line or data lines; the latter are indicated
118 by the expected line being just ".". */
122 int data = strcmp(sbuffer, ".") == 0;
124 fprintf(log, "%s\n", sbuffer);
127 /* Loop for multiple data lines */
133 if (fgets(ibuffer, sizeof(ibuffer), stdin) == NULL)
135 fprintf(log, "%sxpected EOF read from client\n",
136 (strncmp(sbuffer, "*eof", 4) == 0)? "E" : "Une");
140 n = (int)strlen(ibuffer);
141 while (n > 0 && isspace(ibuffer[n-1])) n--;
143 fprintf(log, "<<< %s\n", ibuffer);
144 if (!data || strcmp(ibuffer, ".") == 0) break;
147 /* Check received what was expected */
149 if (strncmp(sbuffer, ibuffer, (int)strlen(sbuffer)) != 0)
151 fprintf(log, "Comparison failed - bailing out\n");
157 /* This could appear in the wrong place in a shared log, so forgo it. */
158 /* fprintf(log, "End of script\n"); */
167 /* End of mtpscript.c */