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 */
30 /*************************************************
31 * SIGALRM handler - crash out *
32 *************************************************/
35 sigalrm_handler(int sig)
37 sig = sig; /* Keep picky compilers happy */
38 fprintf(log, "Server timed out\n");
44 /*************************************************
46 *************************************************/
48 int main(int argc, char **argv)
53 unsigned char sbuffer[1024];
54 unsigned char ibuffer[1024];
58 fprintf(stdout, "500 Script and log file required\n");
62 /* Get the script and log open */
64 script = fopen(argv[1], "r");
67 fprintf(stdout, "500 Failed to open script %s: %s\r\n", argv[1],
73 if (logfile[0] == '+')
79 log = fopen(logfile, logmode);
82 fprintf(stdout, "500 Failed to open log %s: %s\r\n", logfile,
87 /* SIGALRM handler crashes out */
89 signal(SIGALRM, sigalrm_handler);
91 /* Read the script, and do what it says. */
93 while (fgets(CS sbuffer, sizeof(sbuffer), script) != NULL)
95 int n = (int)strlen(CS sbuffer);
96 while (n > 0 && isspace(sbuffer[n-1])) n--;
99 /* If the script line starts with a digit, it is a response line which
102 if (isdigit(sbuffer[0]))
104 fprintf(log, "%s\n", sbuffer);
106 fprintf(stdout, "%s\r\n", sbuffer);
110 /* If the script line starts with "*sleep" we just sleep for a while
111 before continuing. Do not write this to the log, as it may not get
112 written at the right place in a log that's being shared. */
114 else if (strncmp(CS sbuffer, "*sleep ", 7) == 0)
116 sleep(atoi(CS sbuffer+7));
119 /* Otherwise the script line is the start of an input line we are expecting
120 from the client, or "*eof" indicating we expect the client to close the
121 connection. Read command line or data lines; the latter are indicated
122 by the expected line being just ".". */
126 int data = strcmp(CS sbuffer, ".") == 0;
128 fprintf(log, "%s\n", sbuffer);
131 /* Loop for multiple data lines */
137 if (fgets(CS ibuffer, sizeof(ibuffer), stdin) == NULL)
139 fprintf(log, "%sxpected EOF read from client\n",
140 (strncmp(CS sbuffer, "*eof", 4) == 0)? "E" : "Une");
144 n = (int)strlen(CS ibuffer);
145 while (n > 0 && isspace(ibuffer[n-1])) n--;
147 fprintf(log, "<<< %s\n", ibuffer);
148 if (!data || strcmp(CS ibuffer, ".") == 0) break;
151 /* Check received what was expected */
153 if (strncmp(CS sbuffer, CS ibuffer, (int)strlen(CS sbuffer)) != 0)
155 fprintf(log, "Comparison failed - bailing out\n");
161 /* This could appear in the wrong place in a shared log, so forgo it. */
162 /* fprintf(log, "End of script\n"); */
171 /* End of mtpscript.c */