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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
|
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <signal.h>
#include <time.h>
static void data(int argc, char * * argv);
static void error(int argc, char * * argv);
static void helo(int argc, char * * argv);
static void help(int argc, char * * argv);
static void ok(int argc, char * * argv);
static void quit(int argc, char * * argv);
static void refuse(int argc, char * * argv);
static void send_file(int error_number, FILE * file, int argc, char * * argv);
struct command {
const char name[4];
void (*function)(int argc, char * * argv);
};
static const struct command commands[] = {
{ "data", data },
{ "debu", refuse },
{ "expn", refuse },
{ "helo", helo },
{ "help", help },
{ "mail", ok },
{ "noop", ok },
{ "rcpt", refuse },
{ "rset", ok },
{ "quit", quit },
{ "vrfy", refuse },
{ "", error }
};
static char hostname[100];
static char bouncerName[100], bouncerPhone[100];
static const char *
get_line(char * buffer, int length, FILE * stream)
{
int c;
int got_one = 0;
length--;
while ( length > 0 && (c = fgetc(stream)) != EOF ) {
if ( c == '\r' || c == '\n' ) {
if ( got_one )
break;
else
continue;
}
*buffer++ = c;
got_one = 1;
}
*buffer = '\0';
if ( got_one )
return buffer;
else
return 0;
}
static void
timeout(int signal_number)
{
/* Don't hold resources for over a minute just to bounce a message */
shutdown(0, 2);
shutdown(1, 2);
shutdown(2, 2);
_exit(0);
}
int
main(int argc, char * * argv)
{
char buffer[1024];
time_t t = time(0);
char * date = ctime(&t);
#ifdef LOGFILE
FILE * log;
#endif
FILE * bouncer;
signal(SIGALRM, timeout);
alarm(60);
#ifdef LOGFILE
log = fopen(LOGFILE, "w");
#endif
bouncer = fopen("/etc/smtp-refuser.conf", "r");
get_line(bouncerName, sizeof(bouncerName), bouncer);
get_line(bouncerPhone, sizeof(bouncerPhone), bouncer);
fclose(bouncer);
if ( gethostname(hostname, sizeof(hostname)) != 0 )
hostname[0] = '\0';
date[strlen(date) - 1] = '\0';
setlinebuf(stdout);
printf("220 %s smtp-refuser ready at %s.\r\n", hostname, date);
while ( get_line(buffer, sizeof(buffer), stdin) != NULL ) {
const struct command * c = commands;
#ifdef LOGFILE
if ( log )
fprintf(log, "%s\n", buffer);
#endif
while ( *(c->name) && strncasecmp(buffer, c->name, 4) != 0 )
c++;
(c->function)(argc, argv);
}
return 0;
}
static void
data(int argc, char * * argv)
{
fputs("503 No recepients are valid.\r\n", stdout);
}
static void
error(int argc, char * * argv)
{
fputs("500 Command unrecognized.\r\n", stdout);
}
static void
helo(int argc, char * * argv)
{
printf(
"250 %s ready. You are not welcome here.\r\n"
,hostname);
}
static void
help(int argc, char * * argv)
{
static const char help_text[] =
"250-This server exists only to refuse mail.\r\n"
"250-Your system has been directed to it instead of the normal mail\r\n"
"250-server because the system administrator has some reason to\r\n"
"250-refuse your messages. If you feel this is an error, please\r\n"
"250 contact the system administrator using other means than e-mail.\r\n";
fputs(help_text, stdout);
}
static void
ok(int argc, char * * argv)
{
fputs("250 ok. Any mail you send will be rejected.\r\n", stdout);
}
static void
quit(int argc, char * * argv)
{
printf(
"221 %s closing connection. Please don't bother us again.\r\n"
,hostname);
exit(0);
}
static void
send_file(int error_number, FILE * file, int argc, char * * argv)
{
int c;
int escape = 0;
int dollar = 0;
int new_line = 1;
while ( (c = getc(file)) != EOF ) {
if ( new_line ) {
printf("%d- ", error_number);
new_line = 0;
}
if ( !escape ) {
switch ( c ) {
case '\\':
escape = 1;
continue;
case '$':
dollar = 1;
continue;
case '\n':
printf("\r\n", error_number);
new_line = 1;
continue;
}
if ( dollar ) {
if ( c >= '1' && c <= '9' && argc > (c - '0') ) {
fputs(argv[c - '0'], stdout);
dollar = 0;
continue;
}
if ( c == 'c' || c == 'C' ) {
fputs(bouncerName, stdout);
dollar = 0;
continue;
}
if ( c == 'p' || c == 'P' ) {
fputs(bouncerPhone, stdout);
dollar = 0;
continue;
}
}
}
escape = 0;
dollar = 0;
putchar(c);
}
printf("%d \r\n", error_number);
}
static void
refuse(int argc, char * * argv)
{
FILE * file = NULL;
static const char text[] =
"550- All mail from your site is refused. If you feel this is an\r\n"
"550- error, please contact the system administrator using other\r\n"
"550 means than e-mail.\r\n";
if ( argc > 1 )
file = fopen(argv[1], "r");
if ( file == NULL ) {
fputs(text, stdout);
return;
}
argc--;
argv++;
send_file(550, file, argc, argv);
fclose(file);
}
|