🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: news.software.nntp
Article #27910

Proof of Concept (was cancelling from the perl filter)

#27910
From: Silas Dunsmore
Date: Thu, 30 Apr 1998 00:00
270 lines
9647 bytes
In article <37pvi0e6dt.fsf_-_@cascade.cs.ubc.ca>,
Brian Edmonds  <bedmonds@cs.ubc.ca> wrote:
>
>I can see in my head trivially how to implement this in my filter,
>except for the part about nuking.  At present all I can imagine is some
>hack involving logging msgids to a file and having a cron job deal with
>it externally.
>
>What I'd really love is some way to do callbacks from the filter to innd
>so I can just call whatever C function does the cancelling and feed it
>the msgids to zap.

I had extreme insomnia tonight, so I gutted a local canceller I'd written
a few months ago, and inserted the pieces into cleanfeed.

It uses the "ICC" interface to INN.  This is basically a socket that
innd watches for commands on.  It's actually the mechanism that ctlinnd
itself uses to talk to innd.

I've appended diffs against a stock cleanfeed-0.95.5a.

Be warned!  This is rough code.  It has hardcoded stuff all through it, 
and it's not thouroughly tested.  Don't run it on a production machine!

On the other hand, it ran for over an hour with no major problems.
It was slow, because of the extra debugging logging I inserted, but
it was functional.

Somebody take this and make it better, please.

Me for bed.

-- 
Later, eh?    Silas

CUT TO:        The Mercator map, with animation.

Narrator(voice over): The so-called Norway rat actually originated in Southeast
               Asia.

A dot appears in Cambodia.




=== This is a patch against cleanfeed-0.95.5a/cleanfeed
--- cleanfeed	Fri Apr  3 13:40:23 1998
+++ cleanfeed.work	Thu Apr 30 02:27:51 1998
@@ -505,14 +505,29 @@
             # MD5 checksums
 	    if (defined $md5hash) {
 		push @md5history, $md5hash;
-		$md5history{$md5hash}++;
+		##$md5history{$md5hash}++;
+
+                # this has been changed to keep track of the actual
+                # message-ids involved.
+                $md5history{$md5hash} .= $hdr{'Message-ID'};
+
+ICCnote("Added ${hdr{'Message-ID'}} to $md5hash");
 	    }
 
 	    # if post appears more than high limit, save for
 	    # continual rejection, outside of history window
-	    if ((defined $md5hash) and ($md5history{$md5hash} > $config{'md5maxmultiposts'})) {
+	    ##if ((defined $md5hash) and ($md5history{$md5hash} > $config{'md5maxmultiposts'})) {
+	    if ((defined $md5hash) and
+                    (( $md5history{$md5hash} =~ tr/<// )
+                     > $config{'md5maxmultiposts'})) {
 	        md5savehist ($md5hash);
 	        $empreturn = "New EMP detected (md5)";
+
+ICCnote("Scanning $md5hash for saved IDs");
+                while ( $md5history{$md5hash} =~ /(<[^>]+>)/g ) {
+                    cancel_mid( $1 );
+                }
+
 	    } elsif ((defined $hash2) and ($history{$hash2} > $config{'maxmultiposts'})) {
 		savehist ($hash2);
 		$empreturn = "New EMP detected (ph/l)";
@@ -531,7 +546,26 @@
 	    while (scalar @md5history > $config{'MD5History'}) {
 		$tmp_hist = shift @md5history;
 		next unless (exists $md5history{$tmp_hist});
-		delete $md5history{$tmp_hist} if ($md5history{$tmp_hist}-- < 2);
+		##delete $md5history{$tmp_hist} if ($md5history{$tmp_hist}-- < 2);
+
+ICCnote("Cleaning MD5 $tmp_hist");
+ICCnote("Old value is ${md5history{$tmp_hist}}");
+
+                # chop out the first message-id
+                ( $md5history{$tmp_hist} ) = 
+                        ( $md5history{$tmp_hist} =~ /^<[^>]+>(.*)$/ );
+
+ICCnote("New value is ${md5history{$tmp_hist}}");
+
+                # delete the entry if there's nothing left
+                delete $md5history{$tmp_hist} if $md5history{$tmp_hist} eq '';
+
+                # delete the entry if there's only one message-id left
+                delete $md5history{$tmp_hist} if 
+                       $md5history{$tmp_hist} =~ /^<[^>]+>$/;
+
+ICCnote("Deleted entry for $tmp_hist") if not exists $md5history{$tmp_hist};
+
 	    }
 
 	    return reject ($empreturn) unless ($empreturn eq "");
@@ -754,3 +788,158 @@
     close FILE;
 }
 
+
+
+# from here on down is routines modified from a standalone local canceller
+require 5.003;
+use integer;
+use strict 'subs', 'refs';
+
+use Socket;
+use Fcntl;
+
+
+$ICCdir = "/var/run/news";
+$ICCservername = "$ICCdir/control";             # belongs to INND
+$ICCclientname = "$ICCdir/cleanfeed-out";       # belongs to us
+
+$ICClogfile = "/tmp/cleanfeed-ICC-logfile";     # logging for debugging
+
+sub ICCerror(@) {
+    $ICCokay = 0;                       # remember that we have had a problem.
+
+    return if not defined $ICClogfile;
+
+    # turn autoflush on, since something abnormal has occured.
+    select( (select(ICCLOG), $| = 1)[0] );
+    
+    # log the error message.
+    print ICCLOG @_, "\n";
+
+}
+
+
+sub ICCnote(@) {
+    return if not defined $ICClogfile;
+
+    # log the note
+    print ICCLOG @_, "\n";
+}
+
+
+# this ought to be rewritten to handle the named-pipe variant of the
+# interface as well as the unix datagram socket variant.
+#
+# it should also be rewritten to gracefully handle multiple calls to
+# the setup and/or close routines.
+sub setup_cancel_socket() {
+
+    open( ICCLOG, ">>$ICClogfile" ) or undef $ICClogfile
+        if defined $ICClogfile;
+
+    $ICCokay = 1;                       # assume okay until something fails.
+
+    ICCnote( "Opening socket at GMT ", scalar gmtime() );
+
+    $ICCserver = pack_sockaddr_un( $ICCservername );
+
+    $ICCclient = pack_sockaddr_un( $ICCclientname );
+
+    socket( ICCSOCKET, AF_UNIX, SOCK_DGRAM, 0 ) or 
+        ICCerror( "failed to create local socket $ICCclientname" )
+            if $ICCokay;
+
+    bind( ICCSOCKET, $ICCclient ) or
+        ICCerror( "failed to bind local socket $ICCclientname" )
+            if $ICCokay;
+
+    # fcntl() returns "0 but true" for 0, isn't that convenient?
+    fcntl( ICCSOCKET, F_SETFL, O_NONBLOCK ) or
+        ICCerror( "failed to set local socket to nonblocking" )
+            if $ICCokay;
+
+    ICCnote( "Opened the socket, everything okay so far" ) if $ICCokay;
+}
+
+
+sub close_cancel_socket() {
+    ICCnote( "Closing socket at GMT ", scalar gmtime() );
+
+    close( ICCSOCKET ) or
+        ICCerror( "Problem closing socket's file descriptor!" );
+
+    unlink( $ICCclientname ) or
+        ICCerror( "Problem removing socket's control file!" );
+
+    close( ICCLOG ) if defined $ICClogfile;
+}
+
+
+# Warning!  This routine should only be used to cancel Message-IDs that
+# are KNOWN TO EXIST in the history file.  See the ncmspcancel.c source
+# for details on a workaround for a bug involving cancels of never-seen
+# articles.
+#
+# This routine is currently coded asynchronously -- it may not get a
+# response from the server, or it may get several.  Further, there isn't
+# a mapping between commands and responses.
+#
+# Note that most of the error-handling code has not been tested.
+#
+# Note the HORRIBLE hardcoded ctlinnd/ICC interface!
+# This was a quick hack.  It would be much better to convert inndcomm.h
+# to a .ph file and use the symbolic names.  Probably.
+sub cancel_mid($) {
+    my($mid) = @_;
+    my($msg, $rsp);
+
+    ICCnote( "ICC not okay, skipping cancel of $mid" ) if not $ICCokay;
+    return if not $ICCokay;
+
+    ICCnote( "Issuing local cancel for $mid" );
+
+    # \x01 is the seperator between commands & parameters.
+    # \x00 is the terminator for the command.
+    # c is the constant for cancels.
+    # $mid is the Message-ID to cancel.
+    # $ICCclientname is the socket that we want responses sent to.
+    $msg = $ICCclientname . "\x01" . "c" . "\x01" . $mid . "\x00";
+
+    # ana* is a pack command-string: asciichar, networkshort, asciichars.
+    # a is the protocol version.
+    # length of the (entire) message.
+    # the message itself.
+    $msg = pack( 'ana*', 'a', length($msg) + 3, $msg );        
+
+    send( ICCSOCKET, $msg, 0, $ICCserver ) or
+        ICCerror( "Couldn't send() to $ICCservername !" );
+
+    # this could be used to check if something's out of control.
+    $ICCpending++;
+
+    # paranoia -- ensure that the variable exists with valid data
+    $rsp = '';
+
+    # are there any responses waiting yet?
+    # the socket is non-blocking & asynchronous, so a response might
+    # not be here yet... or there could be several!
+    while( recv( ICCSOCKET, $rsp, 4096, 0) ) {
+        # ought to check for problems here...
+        if( $rsp ne pack( 'ana*', 'a', 7, "0 Ok" ) ) {
+            ICCnote( "recv(): unexpected INND ICC response: $rsp" );
+        }
+        $ICCpending--;
+    }
+
+}
+
+
+# at startup
+setup_cancel_socket();
+
+# at shutdown -- does this work?
+sub filter_end () {
+    close_cancel_socket();
+}
+
+# close/open using the before/after reload hooks as well?

Technical Details
Message-ID: <6i9ggc$aok$1@warez.psyber.com>
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
References: <ErJo4q.50J@ferret.ocunix.on.ca> <8rr11.14$n5.2561968@gazette.loc1.tandem.com> <6i5kmh$emd@bcarh8ab.bnr.ca> <37pvi0e6dt.fsf_-_@cascade.cs.ubc.ca>

Show Full Headers
Complete Article Headers
Message-ID: <6i9ggc$aok$1@warez.psyber.com>
Subject: Proof of Concept (was cancelling from the perl filter)
Date: Thu, 30 Apr 1998 00:00:00 +0000
From: silasd@user.psyber.com (Silas Dunsmore)
References: <ErJo4q.50J@ferret.ocunix.on.ca> <8rr11.14$n5.2561968@gazette.loc1.tandem.com> <6i5kmh$emd@bcarh8ab.bnr.ca> <37pvi0e6dt.fsf_-_@cascade.cs.ubc.ca>
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: 349014568
Organization: Psyberware Internet Access
Newsgroups: news.software.nntp