Article View: news.software.nntp
Article #30300(LONG) Stupid filter patch for INN 2.1
From: Fluffy
Date: Thu, 06 Aug 1998 00:00
Date: Thu, 06 Aug 1998 00:00
634 lines
22201 bytes
22201 bytes
This is something I have been playing with. It's purely a toy at the moment
and _not_ to be stuck into a production server. It just seems like a nifty
idea, and maybe someone else will want to play too, maybe make it work
better too and not be so ugly and stuff...
BTW, I've been trying this with traditional storage. I don't _think_ we
need to worry about storage manager differences here, but it's certainly
possible that I'm wrong.
What it all does:
1. innd gets a new IHOPE command. Operation and responses are identical to
IHAVE, except that we don't write to history and don't really accept the
article. The idea is to see if INN _would_ take the article if it was
being offered for real. This should probably be shored up a bit. Right
now it will accept an IHOPE from anyone who's got transfer permission.
It should probably be limited to the hosts where nnrpd is running
(probably just the localhost in most cases). Make sure you _don't_ run
configure with --with-tcl, because I haven't touched the TCL stuff and
it's probably gonna get broken here.
2. filter_innd.pl gets a new subroutine called filter_art_test(). In the
simplest implementation that sort of works, just add this to your perl
filter:
sub filter_art_test { return &filter_art }
In real life, you probably _don't_ want to make it that simple. The
reason is that while the article isn't really being offered, the usual
filter_art subroutine doesn't know this. That means you'll be racking up
MD5 and other counters just for testing an article. So, you'll either
want to rework filter_art to know when it's only a test, or even use an
entirely separate code block for each.
All data and functions that filter_art can use are also available to
filter_art_test.
3. nnrpd is horribly hacked to try the IHOPE command before real posting is
attempted. This part needs some work: the
no-filtering-for-moderated-groups bug is left intact, and IHOPE won't be
executed if innd is doing (or not doing) something that causes nnrpd to
spool the article.
Okay, so what's the point of all this? Lots of folks dislike the normal
nnrpd perl filter because it's big and doesn't remember anything between
connections. And, using filter_art isn't a possibility for moderated group
submissions because we don't want innd to keep those; they're supposed to
get mailed, right?. I think this takes care of those problems.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
So, the patches. First, the changes to innd
(art.c, innd.h, nc.c, perl.c):
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*** art.c.dist Thu Aug 6 07:04:29 1998
--- art.c Thu Aug 6 08:04:08 1998
***************
*** 2064,2071 ****
** This routine is the heart of it all. Take a full article, parse it,
** file or reject it, feed it to the other sites. Return the NNTP
** message to send back.
*/
! STRING ARTpost(CHANNEL *cp)
{
static BUFFER Files;
static BUFFER Header;
--- 2064,2075 ----
** This routine is the heart of it all. Take a full article, parse it,
** file or reject it, feed it to the other sites. Return the NNTP
** message to send back.
+ **
+ ** If testonly is TRUE, then we only pretend to submit the article.
+ ** That is, we run the sanity checks and can call a filter routine,
+ ** but we don't change history or keep the article.
*/
! STRING ARTpost(CHANNEL *cp, BOOL testonly)
{
static BUFFER Files;
static BUFFER Header;
***************
*** 2159,2168 ****
if (error != NULL) {
(void)sprintf(buff, "%d %s", NNTP_REJECTIT_VAL, error);
ARTlog(&Data, ART_REJECT, buff);
! if (innconf->remembertrash && Data.MessageID &&
! (Mode == OMrunning) && !HISremember(hash))
! syslog(L_ERROR, "%s cant write history %s %m",
! LogName, Data.MessageID);
ARTreject(error_code, cp, buff, article);
return buff;
}
--- 2163,2174 ----
if (error != NULL) {
(void)sprintf(buff, "%d %s", NNTP_REJECTIT_VAL, error);
ARTlog(&Data, ART_REJECT, buff);
! if (testonly == FALSE) {
! if (innconf->remembertrash && Data.MessageID &&
! (Mode == OMrunning) && !HISremember(hash))
! syslog(L_ERROR, "%s cant write history %s %m",
! LogName, Data.MessageID);
! }
ARTreject(error_code, cp, buff, article);
return buff;
}
***************
*** 2170,2185 ****
#if defined(DO_PERL)
pathForPerl = HeaderFindMem(article->Data, article->Used, "Path", 4) ;
TMRstart(TMR_PERL);
! perlrc = (char *)HandleArticle(Data.Body);
TMRstop(TMR_PERL);
if (perlrc != NULL) {
(void)sprintf(buff, "%d %s", NNTP_REJECTIT_VAL, perlrc);
syslog(L_NOTICE, "rejecting[perl] %s %s", HDR(_message_id), buff);
ARTlog(&Data, ART_REJECT, buff);
! if (innconf->remembertrash && Data.MessageID &&
! (Mode == OMrunning) && !HISremember(hash))
! syslog(L_ERROR, "%s cant write history %s %m",
! LogName, Data.MessageID);
ARTreject(REJECT_FILTER, cp, buff, article);
return buff;
}
--- 2176,2193 ----
#if defined(DO_PERL)
pathForPerl = HeaderFindMem(article->Data, article->Used, "Path", 4) ;
TMRstart(TMR_PERL);
! perlrc = (char *)HandleArticle(Data.Body, testonly);
TMRstop(TMR_PERL);
if (perlrc != NULL) {
(void)sprintf(buff, "%d %s", NNTP_REJECTIT_VAL, perlrc);
syslog(L_NOTICE, "rejecting[perl] %s %s", HDR(_message_id), buff);
ARTlog(&Data, ART_REJECT, buff);
! if (testonly == FALSE) {
! if (innconf->remembertrash && Data.MessageID &&
! (Mode == OMrunning) && !HISremember(hash))
! syslog(L_ERROR, "%s cant write history %s %m",
! LogName, Data.MessageID);
! }
ARTreject(REJECT_FILTER, cp, buff, article);
return buff;
}
***************
*** 2261,2270 ****
NNTP_REJECTIT_VAL,
MaxLength(distributions[0], distributions[0]));
ARTlog(&Data, ART_REJECT, buff);
! if (innconf->remembertrash && Data.MessageID &&
! (Mode == OMrunning) && !HISremember(hash))
! syslog(L_ERROR, "%s cant write history %s %m",
! LogName, Data.MessageID);
DISPOSE(distributions);
ARTreject(REJECT_DISTRIB, cp, buff, article);
return buff;
--- 2269,2280 ----
NNTP_REJECTIT_VAL,
MaxLength(distributions[0], distributions[0]));
ARTlog(&Data, ART_REJECT, buff);
! if (testonly == FALSE) {
! if (innconf->remembertrash && Data.MessageID &&
! (Mode == OMrunning) && !HISremember(hash))
! syslog(L_ERROR, "%s cant write history %s %m",
! LogName, Data.MessageID);
! }
DISPOSE(distributions);
ARTreject(REJECT_DISTRIB, cp, buff, article);
return buff;
***************
*** 2365,2374 ****
(void)sprintf(buff, "%d Unapproved for \"%s\"",
NNTP_REJECTIT_VAL, ngp->Name);
ARTlog(&Data, ART_REJECT, buff);
! if (innconf->remembertrash && Data.MessageID &&
! (Mode == OMrunning) && !HISremember(hash))
! syslog(L_ERROR, "%s cant write history %s %m",
! LogName, Data.MessageID);
if (distributions)
DISPOSE(distributions);
ARTreject(REJECT_UNAPP, cp, buff, article);
--- 2375,2386 ----
(void)sprintf(buff, "%d Unapproved for \"%s\"",
NNTP_REJECTIT_VAL, ngp->Name);
ARTlog(&Data, ART_REJECT, buff);
! if (testonly == FALSE) {
! if (innconf->remembertrash && Data.MessageID &&
! (Mode == OMrunning) && !HISremember(hash))
! syslog(L_ERROR, "%s cant write history %s %m",
! LogName, Data.MessageID);
! }
if (distributions)
DISPOSE(distributions);
ARTreject(REJECT_UNAPP, cp, buff, article);
***************
*** 2452,2461 ****
MaxLength(HDR(_newsgroups), HDR(_newsgroups)));
ARTlog(&Data, ART_REJECT, buff);
if (!innconf->wanttrash) {
! if (innconf->remembertrash && Data.MessageID &&
! (Mode == OMrunning) && !HISremember(hash))
! syslog(L_ERROR, "%s cant write history %s %m",
! LogName, Data.MessageID);
if (distributions)
DISPOSE(distributions);
ARTreject(REJECT_GROUP, cp, buff, article);
--- 2464,2475 ----
MaxLength(HDR(_newsgroups), HDR(_newsgroups)));
ARTlog(&Data, ART_REJECT, buff);
if (!innconf->wanttrash) {
! if (testonly == FALSE) {
! if (innconf->remembertrash && Data.MessageID &&
! (Mode == OMrunning) && !HISremember(hash))
! syslog(L_ERROR, "%s cant write history %s %m",
! LogName, Data.MessageID);
! }
if (distributions)
DISPOSE(distributions);
ARTreject(REJECT_GROUP, cp, buff, article);
***************
*** 2467,2476 ****
* DO_WANT_TRASH, then you want all trash except that which
* you explicitly excluded in your active file. */
if (!GroupMissing) {
! if (innconf->remembertrash && Data.MessageID &&
! (Mode == OMrunning) && !HISremember(hash))
! syslog(L_ERROR, "%s cant write history %s %m",
! LogName, Data.MessageID);
if (distributions)
DISPOSE(distributions);
ARTreject(REJECT_GROUP, cp, buff, article);
--- 2481,2492 ----
* DO_WANT_TRASH, then you want all trash except that which
* you explicitly excluded in your active file. */
if (!GroupMissing) {
! if (testonly == FALSE) {
! if (innconf->remembertrash && Data.MessageID &&
! (Mode == OMrunning) && !HISremember(hash))
! syslog(L_ERROR, "%s cant write history %s %m",
! LogName, Data.MessageID);
! }
if (distributions)
DISPOSE(distributions);
ARTreject(REJECT_GROUP, cp, buff, article);
***************
*** 2494,2499 ****
--- 2510,2522 ----
*ngptr = NULL;
j++;
+ /* If we're only testing, we can stop now. */
+ if (testonly == TRUE) {
+ if (distributions)
+ DISPOSE(distributions);
+ return NNTP_TOOKIT;
+ }
+
/* Allocate exactly enough space for the textual representation */
if (innconf->storageapi)
j = (sizeof(TOKEN) * 2) + 3;
*** innd.h.dist Thu Aug 6 08:29:30 1998
--- innd.h Thu Aug 6 08:31:42 1998
***************
*** 477,483 ****
extern BOOL ARTreadschema(void);
extern const char *ARTreadarticle(char *files);
extern char *ARTreadheader(char *files);
! extern STRING ARTpost(CHANNEL *cp);
extern void ARTcancel(const ARTDATA *Data, const char *MessageID, const BOOL Trusted);
extern void ARTclose(void);
extern void ARTsetup(void);
--- 477,483 ----
extern BOOL ARTreadschema(void);
extern const char *ARTreadarticle(char *files);
extern char *ARTreadheader(char *files);
! extern STRING ARTpost(CHANNEL *cp, BOOL testonly);
extern void ARTcancel(const ARTDATA *Data, const char *MessageID, const BOOL Trusted);
extern void ARTclose(void);
extern void ARTsetup(void);
*** nc.c.dist Thu Aug 6 06:41:34 1998
--- nc.c Thu Aug 6 08:32:37 1998
***************
*** 33,38 ****
--- 33,39 ----
static FUNCTYPE NChead();
static FUNCTYPE NChelp();
static FUNCTYPE NCihave();
+ static FUNCTYPE NCihope();
static FUNCTYPE NClist();
static FUNCTYPE NCmode();
static FUNCTYPE NCquit();
***************
*** 55,60 ****
--- 56,62 ----
{ "authinfo", NCauthinfo },
{ "help", NChelp },
{ "ihave", NCihave },
+ { "ihope", NCihope },
{ "check", NCcheck },
{ "takethis", NCtakethis },
{ "list", NClist },
***************
*** 81,86 ****
--- 83,89 ----
STATIC char NCdot[] = "." ;
STATIC char NCbadcommand[] = NNTP_BAD_COMMAND;
STATIC STRING NCgreeting;
+ STATIC BOOL NCtestonly;
/*
** Clear the WIP entry for the given channel
***************
*** 180,186 ****
** not running, drop the article or just pause and reschedule.
*/
STATIC void
! NCpostit(CHANNEL *cp)
{
STRING response;
--- 183,189 ----
** not running, drop the article or just pause and reschedule.
*/
STATIC void
! NCpostit(CHANNEL *cp, BOOL testonly)
{
STRING response;
***************
*** 194,200 ****
(POINTER)&Mode, NCpostit, (POINTER)NULL);
return;
case OMrunning:
! response = ARTpost(cp);
if (atoi(response) == NNTP_TOOKIT_VAL) {
cp->Received++;
if (cp->Sendid.Size > 3) { /* We be streaming */
--- 197,203 ----
(POINTER)&Mode, NCpostit, (POINTER)NULL);
return;
case OMrunning:
! response = ARTpost(cp, testonly);
if (atoi(response) == NNTP_TOOKIT_VAL) {
cp->Received++;
if (cp->Sendid.Size > 3) { /* We be streaming */
***************
*** 493,498 ****
--- 496,561 ----
}
}
+ /*
+ ** The "ihope" command. Pretend to offer an article,
+ ** for filtering purposes.
+ */
+ STATIC FUNCTYPE
+ NCihope(CHANNEL *cp)
+ {
+ char *p;
+ #if defined(DO_PERL)
+ char *perlrc;
+ int msglen;
+ #endif /* DO_PERL */
+
+ cp->Ihave++;
+ /* Snip off the Message-ID. */
+ for (p = cp->In.Data + STRLEN("ihope"); ISWHITE(*p); p++)
+ continue;
+ if (NCbadid(cp, p))
+ return;
+
+ if ((innconf->refusecybercancels) && (strncmp(p, "<cancel.", 8) == 0)) {
+ cp->Refused++;
+ cp->Ihave_Cybercan++;
+ NCwritereply(cp, NNTP_HAVEIT);
+ return;
+ }
+
+ #if defined(DO_PERL)
+ /* invoke a perl message filter on the message id */
+ if ((perlrc = (char *)HandleMessageID(p)) != NULL) {
+ cp->Refused++;
+ msglen = strlen(p) + 5; /* 3 digits + space + id + null */
+ if (cp->Sendid.Size < msglen) {
+ if (cp->Sendid.Size > 0) DISPOSE(cp->Sendid.Data);
+ if (msglen > MAXHEADERSIZE) cp->Sendid.Size = msglen;
+ else cp->Sendid.Size = MAXHEADERSIZE;
+ cp->Sendid.Data = NEW(char, cp->Sendid.Size);
+ }
+ (void)sprintf(cp->Sendid.Data, "%d %s", NNTP_ERR_GOTID_VAL, p);
+ NCwritereply(cp, cp->Sendid.Data);
+ return;
+ }
+ #endif
+
+ if (HIShavearticle(HashMessageID(p))) {
+ cp->Refused++;
+ cp->Ihave_Duplicate++;
+ NCwritereply(cp, NNTP_HAVEIT);
+ }
+ else if (WIPinprogress(p, cp, FALSE)) {
+ cp->Ihave_Deferred++;
+ NCwritereply(cp, NNTP_RESENDIT_LATER);
+ }
+ else {
+ cp->Ihave_SendIt++;
+ NCwritereply(cp, NNTP_SENDIT);
+ cp->State = CSgetarticle;
+ }
+ }
+
/*
** The "xbatch" command. Set the state appropriately.
*/
***************
*** 808,813 ****
--- 871,883 ----
break;
}
+ if (cp->State == CSgetcmd) {
+ if (caseEQn(bp->Data, "ihope", 5))
+ NCtestonly = TRUE;
+ else
+ NCtestonly = FALSE;
+ }
+
/* Loop through the command table. */
for (p = bp->Data, dp = NCcommands; dp < ENDOF(NCcommands); dp++)
if (caseEQn(p, dp->Name, dp->Size)) {
***************
*** 913,919 ****
}
if (!innconf->wireformat)
NCclean(bp);
! NCpostit(cp);
if (cp->State == CSwritegoodbye)
break;
cp->State = CSgetcmd;
--- 983,989 ----
}
if (!innconf->wireformat)
NCclean(bp);
! NCpostit(cp, NCtestonly);
if (cp->State == CSwritegoodbye)
break;
cp->State = CSgetcmd;
*** perl.c.dist Thu Aug 6 08:01:05 1998
--- perl.c Thu Aug 6 07:35:47 1998
***************
*** 39,46 ****
extern char *pathForPerl ;
char *
! HandleArticle(artBody)
char *artBody;
{
dSP;
ARTHEADER *hp;
--- 39,47 ----
extern char *pathForPerl ;
char *
! HandleArticle(artBody, testonly)
char *artBody;
+ BOOL testonly;
{
dSP;
ARTHEADER *hp;
***************
*** 76,82 ****
ENTER ;
SAVETMPS ;
! rc = perl_call_argv ("filter_art", G_EVAL|G_SCALAR,NULL);
SPAGAIN;
--- 77,88 ----
ENTER ;
SAVETMPS ;
! if (testonly == TRUE) {
! rc = perl_call_argv ("filter_art_test", G_EVAL|G_SCALAR,NULL);
! }
! else {
! rc = perl_call_argv ("filter_art", G_EVAL|G_SCALAR,NULL);
! }
SPAGAIN;
***************
*** 86,93 ****
if (SvTRUE(GvSV(errgv))) /* check $@ */
{
! syslog (L_ERROR,"Perl function filter_art died: %s",
SvPV(GvSV(errgv), na)) ;
POPs ;
PerlFilter (FALSE) ;
}
--- 92,105 ----
if (SvTRUE(GvSV(errgv))) /* check $@ */
{
! if (testonly == TRUE) {
! syslog (L_ERROR,"Perl function filter_art_test died: %s",
SvPV(GvSV(errgv), na)) ;
+ }
+ else {
+ syslog (L_ERROR,"Perl function filter_art died: %s",
+ SvPV(GvSV(errgv), na)) ;
+ }
POPs ;
PerlFilter (FALSE) ;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
And, for nnrpd (just post.c):
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*** post.c.dist Fri Jul 24 09:13:00 1998
--- post.c Thu Aug 6 10:10:55 1998
***************
*** 789,794 ****
--- 789,815 ----
return atoi(buff);
}
+ /*
+ ** Offer the article to the server FOR TESTING, return its reply.
+ */
+ STATIC int
+ OfferTest(buff, buffsize, FromServer, ToServer)
+ char *buff;
+ int buffsize;
+ FILE *FromServer;
+ FILE *ToServer;
+ {
+ static char CANTSEND[] = "Can't send %s to server, %s";
+
+ (void)fprintf(ToServer, "ihope %s\r\n", HDR(_messageid));
+ if (FLUSH_ERROR(ToServer)
+ || fgets(buff, buffsize, FromServer) == NULL) {
+ (void)sprintf(buff, CANTSEND, "IHOPE", strerror(errno));
+ return -1;
+ }
+ return atoi(buff);
+ }
+
/*
** Spool article to temp file.
***************
*** 981,987 ****
* they are all inside cases that are most likely never going to
* happen -- for example, if the server crashes. */
! /* Offer article to server. */
i = OfferArticle(buff, (int)sizeof buff, FromServer, ToServer);
if (i == NNTP_AUTH_NEEDED_VAL) {
/* Send authorization. */
--- 1002,1061 ----
* they are all inside cases that are most likely never going to
* happen -- for example, if the server crashes. */
! /* Offer article to server FOR TESTING. */
! i = OfferTest(buff, (int)sizeof buff, FromServer, ToServer);
! if (i == NNTP_AUTH_NEEDED_VAL) {
! /* Send authorization. */
! if (NNTPsendpassword(innconf->nnrpdposthost, FromServer, ToServer) < 0) {
! (void)sprintf(Error, "Can't authorize with %s",
! innconf->nnrpdposthost ? innconf->nnrpdposthost : "innd");
! return Spoolit(article,Error);
! }
! i = OfferTest(buff, (int)sizeof buff, FromServer, ToServer);
! }
!
! if (i != NNTP_SENDIT_VAL) {
! (void)strcpy(Error, "[pre-post] ");
! (void)strcat(Error, buff);
! SendQuit(FromServer, ToServer);
! return (i != NNTP_HAVEIT_VAL ? Spoolit(article, Error) : Error) ;
! }
! if (Tracing)
! syslog(L_TRACE, "%s test post starting", ClientHost);
!
! /* Write the headers and a blank line. */
! for (hp = Table; hp < ENDOF(Table); hp++)
! if (hp->Value)
! (void)fprintf(ToServer, "%s: %s\r\n", hp->Name, hp->Value);
! for (i = 0; i < OtherCount; i++)
! (void)fprintf(ToServer, "%s\r\n", OtherHeaders[i]);
! (void)fprintf(ToServer, "\r\n");
! if (FLUSH_ERROR(ToServer)) {
! (void)sprintf(Error, CANTSEND, "headers [pre-post]", strerror(errno));
! (void)fclose(FromServer);
! (void)fclose(ToServer);
! return Spoolit(article, Error);
! }
!
! /* Send the article, get the server's reply. */
! if (NNTPsendarticle(article, ToServer, TRUE) < 0
! || fgets(buff, sizeof buff, FromServer) == NULL) {
! (void)sprintf(Error, CANTSEND, "article [pre-post]", strerror(errno));
! (void)fclose(FromServer);
! (void)fclose(ToServer);
! return Spoolit(article, Error);
! }
!
! /* Did the server want the article? */
! if ((i = atoi(buff)) != NNTP_TOOKIT_VAL) {
! (void)strcpy(Error, "[pre-post] ");
! (void)strcat(Error, buff);
! SendQuit(FromServer, ToServer);
! return (i != NNTP_REJECTIT_VAL ? Spoolit(article, Error) : Error) ;
! }
!
!
! /* Offer article to server FOR REAL. */
i = OfferArticle(buff, (int)sizeof buff, FromServer, ToServer);
if (i == NNTP_AUTH_NEEDED_VAL) {
/* Send authorization. */
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Okay, I remembered all the affected files. I think.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Mw.,
F.
Technical Details
Message-ID:
<6qcf60$pbd$1@fluffy.meow.org>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.nntp.mbox.zip!not-for-mailShow Full Headers
Complete Article Headers
Message-ID: <6qcf60$pbd$1@fluffy.meow.org> Subject: (LONG) Stupid filter patch for INN 2.1 Date: Thu, 06 Aug 1998 00:00:00 +0000 From: Fluffy <fluffy@meow.org> Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.nntp.mbox.zip!not-for-mail X-Deja-AN: 378526178 X-Complaints-To: mom X-Trace: fluffy.meow.org 902414337 25966 127.0.0.1 (6 Aug 1998 14:38:57 GMT) Organization: Fluffy's Meow Server NNTP-Posting-Date: 6 Aug 1998 14:38:57 GMT Newsgroups: news.software.nntp