/* * lmtp2nntp.c * * The lmtp2nntp program reads mail as a LMTP server and posts it to one or * more newsgroups using NNTP. It delivers the message immediately or fails. * * The OSSP Project, Cable & Wireless Deutschland GmbH * Thomas Lotterer, * */ #include #include #include #include #include #include /* third party */ #include "str.h" #include "argz.h" /* own headers */ #include "lmtp.h" #include "nntp.h" #include "sock.h" #ifndef FALSE #define FALSE (1 != 1) #endif #ifndef TRUE #define TRUE (!FALSE) #endif #define ERR_EXECUTION -1 #define ERR_DELIVERY -2 #define MESSAGE_MAXLEN 8*1024*1024 #define STDSTRLEN 128 extern void lmtp_debug_dumplmtp(lmtp_t *lmtp); static ssize_t trace_read(int d, void *buf, size_t nbytes); static ssize_t trace_write(int d, const void *buf, size_t nbytes); static lmtp_rc_t lmtp_cb_lhlo (lmtp_t *lmtp, lmtp_io_t *io, lmtp_req_t *req, void *ctx); static lmtp_rc_t lmtp_cb_mail (lmtp_t *lmtp, lmtp_io_t *io, lmtp_req_t *req, void *ctx); static lmtp_rc_t lmtp_cb_rcpt (lmtp_t *lmtp, lmtp_io_t *io, lmtp_req_t *req, void *ctx); static lmtp_rc_t lmtp_cb_data (lmtp_t *lmtp, lmtp_io_t *io, lmtp_req_t *req, void *ctx); static lmtp_rc_t lmtp_cb_noop (lmtp_t *lmtp, lmtp_io_t *io, lmtp_req_t *req, void *ctx); static lmtp_rc_t lmtp_cb_rset (lmtp_t *lmtp, lmtp_io_t *io, lmtp_req_t *req, void *ctx); static lmtp_rc_t lmtp_cb_quit (lmtp_t *lmtp, lmtp_io_t *io, lmtp_req_t *req, void *ctx); static int helo_rfc0821domain(char *msg, char **domain); static int helo_rfc1035domain(char *msg, char **domain); struct session { int lhlo_seen; char *lhlo_domain; }; static void resetsession(struct session *session); struct message { char *rfc822message; char *mail_from; char *azRcpt; size_t asRcpt; }; static void resetmessage(struct message *message); typedef struct { int option_verbose; int option_tracing; int option_deliverymode; int option_groupmode; char *azArggroups; size_t asArggroups; struct session session; struct message message; } lmtp2nntp_t; enum { DELIVERYMODE_ONCE, DELIVERYMODE_MANY, DELIVERYMODE_FULL }; enum { GROUPMODE_ARG, GROUPMODE_ENVELOPE, GROUPMODE_HEADER }; /* * tracing */ ssize_t trace_read(int d, void *buf, size_t nbytes) { ssize_t rc; int tracefile; rc = read(d, buf, nbytes); if ((tracefile = open("/tmp/t", O_CREAT|O_WRONLY|O_APPEND, 0664)) != -1) { write(tracefile, buf, rc); close(tracefile); } return rc; } ssize_t trace_write(int d, const void *buf, size_t nbytes) { ssize_t rc; int tracefile; rc = write(d, buf, nbytes); if ((tracefile = open("/tmp/t", O_CREAT|O_WRONLY|O_APPEND, 0664)) != -1) { write(tracefile, buf, rc); close(tracefile); } return rc; } /* * print usage information */ static void usage(char *command) { fprintf(stderr, "USAGE: %s [-p protocol] [-l logtarget] " "[-h host[:port]] [-m mode] [-t] [-v] newsgroup [newsgroup ...]\n", command); return; } int main(int argc, char **argv) { //FIXME int rc = 0; lmtp_t *lmtp; lmtp_io_t lmtp_io; lmtp2nntp_t *ctx; int i; /* general purpose scratch int, index ... */ char *progname; #if 0 /* begin NNTP posting test */ { int s; nntp_t *nntp; if ((s = sock_create(argv[1])) == -1) { fprintf(stderr, "fuck socket\n"); exit(1); } nntp = nntp_create(s, s, NULL); nntp_post(nntp, "..."); nntp_destroy(nntp); sock_destroy(s); exit(0); } #endif progname = argv[0]; /* create application context */ if ((ctx = (lmtp2nntp_t *)malloc(sizeof(lmtp2nntp_t))) == NULL) exit(ERR_EXECUTION); ctx->option_verbose = FALSE; ctx->option_tracing = FALSE; ctx->option_deliverymode = DELIVERYMODE_MANY; ctx->option_groupmode = GROUPMODE_ARG; ctx->azArggroups = NULL; ctx->asArggroups = 0; resetsession(&ctx->session); resetmessage(&ctx->message); { char buf[1000]; int bufused = 0; int tracefile; for (i=0; ioption_deliverymode = DELIVERYMODE_ONCE; else if (strcasecmp(optarg, "many") == 0) ctx->option_deliverymode = DELIVERYMODE_MANY; else if (strcasecmp(optarg, "full") == 0) ctx->option_deliverymode = DELIVERYMODE_FULL; else { fprintf(stderr, "%s:Error: Invalid mode \"%s\" to option -d\n", progname, optarg); exit(ERR_EXECUTION); } break; case 'g': /* -g groupmode */ if (strcasecmp(optarg, "arg") == 0) ctx->option_groupmode = GROUPMODE_ARG; else if (strcasecmp(optarg, "envelope") == 0) ctx->option_groupmode = GROUPMODE_ENVELOPE; else if (strcasecmp(optarg, "header") == 0) ctx->option_groupmode = GROUPMODE_HEADER; else { fprintf(stderr, "%s:Error: Invalid mode \"%s\" to option -g\n", progname, optarg); exit(ERR_EXECUTION); } break; case 't': // -t (tracing) ctx->option_tracing = TRUE; break; case 'v': // -v (verbose) ctx->option_verbose = TRUE; break; case '?': default: usage(progname); exit(ERR_EXECUTION); } } /* remaining arguments are groups */ for (i = optind; i < argc; i++) argz_add(&ctx->azArggroups, &ctx->asArggroups, argv[i]); /* initialize LMTP context */ lmtp_io.read = trace_read; lmtp_io.write = trace_write; if ((lmtp = lmtp_create(STDIN_FILENO, STDOUT_FILENO, &lmtp_io)) == NULL) { fprintf(stderr, "%s:Error: Unable to initialize LMTP library\n", progname); exit(ERR_EXECUTION); } lmtp_register(lmtp, "LHLO", lmtp_cb_lhlo, ctx, NULL, NULL); lmtp_register(lmtp, "MAIL", lmtp_cb_mail, ctx, NULL, NULL); lmtp_register(lmtp, "RCPT", lmtp_cb_rcpt, ctx, NULL, NULL); lmtp_register(lmtp, "DATA", lmtp_cb_data, ctx, NULL, NULL); lmtp_register(lmtp, "NOOP", lmtp_cb_noop, ctx, NULL, NULL); lmtp_register(lmtp, "RSET", lmtp_cb_rset, ctx, NULL, NULL); lmtp_register(lmtp, "QUIT", lmtp_cb_quit, ctx, NULL, NULL); /* loop for LMTP protocol */ lmtp_loop(lmtp); return 0; } static void resetsession(struct session *session) { session->lhlo_seen = FALSE; // fprintf(stderr, "DEBUG: session->lhlo_domain=***%s***\n", session->lhlo_domain); if (session->lhlo_domain != NULL) free(session->lhlo_domain); //FIXME what about non-graceful aborts? session->lhlo_domain = NULL; return; } static void resetmessage(struct message *message) { // fprintf(stderr, "DEBUG: message->mail_from=***%s***\n", message->mail_from); if (message->mail_from != NULL) free(message->mail_from); //FIXME what about non-graceful aborts? message->mail_from = NULL; // fprintf(stderr, "DEBUG: message->azRcpt=***%s***\n", message->azRcpt); if (message->azRcpt != NULL) free(message->azRcpt); //FIXME what about non-graceful aborts? message->azRcpt = NULL; message->asRcpt = 0; // fprintf(stderr, "DEBUG: message->rfc822message=***%s***\n", message->rfc822message); if (message->rfc822message != NULL) free(message->rfc822message); //FIXME what about non-graceful aborts? message->rfc822message = NULL; return; } static lmtp_rc_t lmtp_cb_lhlo(lmtp_t *lmtp, lmtp_io_t *io, lmtp_req_t *req, void *_ctx) { /* * RFC821 [excerpt] 4.1. SMTP COMMANDS * 4.1.1. COMMAND SEMANTICS, HELO * This command and an OK reply to it confirm that both the sender-SMTP * and the receiver-SMTP are in the initial state, that is, there is no * transaction in progress and all state tables and buffers are cleared. * * The first command in a session must be the HELO command. The HELO * command may be used later in a session as well. If the HELO command * argument is not acceptable a 501 failure reply must be returned and * the receiver-SMTP must stay in the same state. * * If the transaction beginning command argument is not acceptable a 501 * failure reply must be returned and the receiver-SMTP must stay in the * same state. If the commands in a transaction are out of order a 503 * failure reply must be returned and the receiver-SMTP must stay in the * same state. * * HELO */ lmtp2nntp_t *ctx = (lmtp2nntp_t *)_ctx; lmtp_rc_t rc = LMTP_OK; lmtp_res_t res; char str[STDSTRLEN]; if (ctx->session.lhlo_seen == TRUE) { res.statuscode = "503"; res.dsncode = "5.0.0"; res.statusmsg = "Duplicate LHLO."; } else { if ( helo_rfc0821domain(req->msg, &ctx->session.lhlo_domain) || helo_rfc1035domain(req->msg, &ctx->session.lhlo_domain)) { ctx->session.lhlo_seen = TRUE; res.statuscode = "250"; res.dsncode = NULL; /* DSN not used for greeting */ str_format(str, sizeof(str), "FIXME.dev.de.cw.net" /* RFC2821 4.1.1.1 */ " Hello %s, pleased to meet you.\n" "ENHANCEDSTATUSCODES\n" /* RFC2034 */ "DSN\n" /* RFC1894 */ "PIPELINING\n" /* RFC1854 */ "8BITMIME", /* RFC1652 */ ctx->session.lhlo_domain); res.statusmsg = str; } else { res.statuscode = "501"; res.dsncode = "5.0.0"; res.statusmsg = "Please identify yourself. Domain must match RFC0821/RFC1035."; } } lmtp_response(lmtp, &res); return rc; } static int helo_rfc0821domain(char *msg, char **domain) { int rc; rc = str_parse(msg, "^.+ (" /* ## ## The mega Perl regular expression below is generated ## with the following Perl program. This is only possible ## because the given grammar is Chomsky-3 (right or left ## linear grammar, but noth both). ## # BNF grammar for according to RFC 821: # ::= one, two, or three digits representing a decimal integer value in the range 0 through 255 # ::= any one of the 52 alphabetic characters A through Z in upper case and a through z in lower case # ::= any one of the ten digits 0 through 9 # ::= | | "-" # ::= | # ::= | # ::= "." "." "." # ::= | # ::= # ::= | "#" | "[" "]" # ::= | "." # # corresponding Perl regular expression ($domain) $snum = "(?:[0-9]|[0-9]{2}|[0-1][0-9]{2}|2[0-4][0-9]|25[0-5])"; $d = "[0-9]"; $a = "[A-Za-z]"; $let_dig_hyp = "(?:$a|$d|-)"; $let_dig = "(?:$a|$d)"; $ldh_str = "${let_dig_hyp}+"; $dotnum = "$snum\\.$snum\\.$snum\\.$snum"; $number = "$d+"; $name = "$a$ldh_str$let_dig"; $element = "(?:$name|#$number|\\[$dotnum\\])"; $domain = "(?:$element\.)*$element"; # # translate into C string block suitable for passing to the Perl # Compatible Regular Expressions (PCRE) based string library Str. my $cregex = $domain; $cregex =~ s|\\|\\\\|sg; $cregex =~ s|(.{70})|"$1"\n|sg; $cregex =~ s|\n([^\n]+)$|\n"$1"|s; #FIXME this fails when last #FIXME line matches linelength exacly print "$cregex\n"; */ "(?:(?:[A-Za-z](?:[A-Za-z]|[0-9]|-)+(?:[A-Za-z]|[0-9])|#[0-9]+|\\[(?:[0" "-9]|[0-9]{2}|[0-1][0-9]{2}|2[0-4][0-9]|25[0-5])\\.(?:[0-9]|[0-9]{2}|[0" "-1][0-9]{2}|2[0-4][0-9]|25[0-5])\\.(?:[0-9]|[0-9]{2}|[0-1][0-9]{2}|2[0" "-4][0-9]|25[0-5])\\.(?:[0-9]|[0-9]{2}|[0-1][0-9]{2}|2[0-4][0-9]|25[0-5" "])\\]).)*(?:[A-Za-z](?:[A-Za-z]|[0-9]|-)+(?:[A-Za-z]|[0-9])|#[0-9]+|\\" "[(?:[0-9]|[0-9]{2}|[0-1][0-9]{2}|2[0-4][0-9]|25[0-5])\\.(?:[0-9]|[0-9]" "{2}|[0-1][0-9]{2}|2[0-4][0-9]|25[0-5])\\.(?:[0-9]|[0-9]{2}|[0-1][0-9]{" "2}|2[0-4][0-9]|25[0-5])\\.(?:[0-9]|[0-9]{2}|[0-1][0-9]{2}|2[0-4][0-9]|" "25[0-5])\\])" ")$", domain); return rc; } static int helo_rfc1035domain(char *msg, char **domain) { int rc; rc = str_parse(msg, "^.+ (" /* ## ## The mega Perl regular expression below is generated ## with the following Perl program. This is only possible ## because the given grammar is Chomsky-3 (right or left ## linear grammar, but noth both). ## # BNF grammar for according to RFC1035: # ::= any one of the 52 alphabetic characters A through Z in upper case and a through z in lower case # ::= any one of the ten digits 0 through 9 # ::= | # ::= | "-" # ::= | #