chiark / gitweb /
truncation: adjust limit, and fix handling so we can do that in one place
[modbot-ulm.git] / stump / bin / submission.pl
1 #
2 # this script accepts submissions that come to the robomoderator by
3 # email. It make s a decision whether the submission deserves
4 # rejection, automatic approval, or is suspicious and should be
5 # forwarded to human moderators for review.
6 #
7 # A decision is essentially a choice of the program that will be
8 # fed with preprocessed article.
9 #
10 # Also note that this script fixes common problems and mistakes in
11 # newsreaders, newsservers, and users. Even though we have no
12 # obligation to fix these problems, people get really disappointed
13 # if we outright reject their bogus messages, because these
14 # people often have no control over how the posts get delivered to us.
15 #
16 # This script supports notion of blacklisting and list of
17 # preapproved persons. As the names imply, we reject all submissions
18 # from blacklisted posters and automatically approve all messages submitted
19 # by preapproved posters (provided that their posts meet other criteria
20 # imposed by the robomoderator.
21 #
22 # For an automatic rejection, it gives a main "reason" for rejection
23 #
24 #Currently supported list of reasons: 
25 #
26 #       - crosspost
27 #       - abuse
28 #       - harassing
29 #       - offtopic
30
31 # get the directory where robomod is residing
32 $MNG_ROOT = $ENV{'MNG_ROOT'} || die "Root dir for moderation not specified";
33
34 # common library
35 require "$MNG_ROOT/bin/robomod.pl";
36
37 # max allowed number of newsgroups in crossposts
38 # change it if you want, but 5 is really good.
39 $maxNewsgroups = $ENV{'MAX_CROSSPOSTS'} || 5; 
40
41 # should we ALWAYS require preapproved posters to sign their submissions
42 # with PGP? Turn it on in the `etc/modenv' if you suffer from numerous
43 # forgeries in the names of preapproved posters.
44 $PGPCheckPreapproved = $ENV{ "WHITELIST_MUST_SIGN" } eq "YES";
45
46 # So, what newsgroup I am moderating?
47 $Newsgroup = $ENV{'NEWSGROUP'};
48
49 # as the name implies. ATTENTION: $TMP must be mode 700!!!
50 $TmpFile = "$ENV{'TMP'}/submission.$$";
51
52 # how do we treat suspicious articles?
53 $Command_Suspicious = "formail -a \"Newsgroups: $Newsgroup\" " .
54                       "| stump.pl suspicious.pl";
55
56 # approved
57 $Command_Approve = "processApproved robomod";
58 # rejected
59 $Command_Reject  = "processRejected robomod";
60
61 # location of blacklist
62 $badGuys = "bad.guys.list";
63
64 # location of preapproved list
65 $goodGuys = "good.guys.list";
66
67 # words that trigger robomod to mark messages suspicious, even 
68 # when the message comes from a preapproved person.
69 $badWords = "bad.words.list";
70
71 # list of people who want all their submissions to be signed
72 $PGPMustList = "pgp.must.list";
73
74 # set PMUSER and ROBOMOD to Internal. Will be used by `suspicious' script.
75 $ENV{'PMUSER'} = $ENV{'PMUSER_INTERNAL'};
76 $ENV{'ROBOMOD'} = $ENV{'ROBOMOD_INTERNAL'};
77
78
79 ######################################################################
80 # Filter rules
81 # checks if all is OK with newsgroups.
82 # what's not OK: 
83 #   1. Megacrossposts
84 #   2. Crossposts to other moderated groups
85 #   3. Control messages (currently)
86 #
87 sub checkNewsgroups {
88
89   # We have not implemented Control: yet...
90   if( $Control ) {
91 print STDERR "CONTROL message - rejected\n";
92     return "$Command_Reject crosspost You posted a Control message which " .
93            "is not allowed.";
94   }
95
96   if( $#newsgroups >= $maxNewsgroups ) {
97 print STDERR "Too many newsgroups\n";
98     return "$Command_Reject crosspost Too many newsgroups, " .
99            "$maxNewsgroups is maximum.";
100   }
101
102   local( $good ) = 0;
103
104   for( $i = 0; $i <= $#newsgroups; $i++ ) {
105
106     if( $newsgroups[$i] eq $Newsgroup ) {
107       $good = 1;
108       next;
109     }
110
111     if( $NewsgroupsDB{$newsgroups[$i]} eq 'm' && 
112         $newsgroups[$i] ne $Newsgroup) {
113 print STDERR "posting to ANOTHER moderated newsgroups\n";
114       return "$Command_Reject crosspost You crossposted to another " .
115              "moderated newsgroup.";
116     }
117
118   }
119
120   if( !$good ) { # Some fool forgot to list the moderated newsgroup
121                  # in the Newsgroups
122     $Newsgroups .= ",$Newsgroup";
123     if( $#newsgroups + 1 >= $maxNewsgroups ) {
124 print STDERR "Too many newsgroups\n";
125       return "$Command_Reject crosspost Too many newsgroups, FIVE is maximum.";
126     }
127   }
128
129   return 0;
130 }
131
132 ###################################################################### checkAck
133 # checks if poster needs acknowledgment of receipt
134 #
135 sub checkAck {
136   #ULM mods want to disable this, so just return "no"
137   $needAck = "no";
138 }
139
140 ################################################################### checkPGP
141 # checks PGP sig IF REQUIRED
142 #
143 # we can reject a post if
144 #
145 #   1. A post must be signed accordinng to rules OR
146 #   2. A post is signed but verification fails.
147 #
148 # Note that we set From: to the user ID in the PGP signature
149 # if a signature is present. It allows for identification of trolls
150 # and for preventing subtle forgeries.
151 #
152 sub checkPGP {
153
154   local( $FromSig ) = `verifySignature < $TmpFile`; chop( $FromSig );
155   local( $good ) = $? == 0;
156
157 print STDERR "FromSig = $FromSig, good = $good\n" if $FromSig;
158
159   if( !$good ) {
160     return "$Command_Reject signature Your PGP signature does NOT match, or is not in our keyring";
161   }
162
163   if( &nameIsInListRegexp( $From, $PGPMustList ) ||
164       ($PGPCheckPreapproved && &nameIsInListExactly($From, $goodGuys) ) ) {
165     if( $FromSig eq "" ) {
166       return "$Command_Reject signature You are REQUIRED to sign your posts.";
167     } 
168   }
169
170   if( $FromSig ) {
171     $X_Origin = $From;
172     $From = "From: $FromSig";
173     $ReplyTo = $From;
174   }
175
176   # else nothing to do
177   return 0;
178 }
179
180 ################################################################ checkCharter
181 # checks charter calling conforms_charter
182 #
183 sub checkCharter {
184   open( VERIFY, "|conforms_charter" ) or die $!;
185   print VERIFY $Body or die $!;
186   close( VERIFY );
187
188   return $? == 0;
189 }
190
191 ################################################################### Filter
192 # contains all filtering rules. calls subroutines above.
193 sub Filter {
194
195
196   local( $response );
197
198   @newsgroups = split( /,/, $Newsgroups );
199
200   return "Command_Reject charter We do not allow any control and " .
201          "cancel messages. contact newsgroup administrator" 
202     if( $Control );
203
204   if( $response = &checkNewsgroups() ) {
205       return $response;
206   }
207
208   if( $paranoid_pgp ) {
209     if( $response = &checkPGP() ) {
210         return $response;
211     }
212   }
213
214   if( &nameIsInListRegexp( $From, $badGuys ) ) {
215     return "$Command_Reject blocklist";
216   }
217
218   # note that if even a preapproved person uses "BAD words" (that is
219   # words from a special list), his/her message will be marked
220   # "suspicious" and will be forwarded to a humen mod for review.
221   # As an example of a bad word may be "MAKE MONEY FAST - IT REALLY WORKS!!!"
222   #
223   if( $badWord = &nameIsInListRegexp( $Body, $badWords ) ) {
224 print STDERR "BAD WORD $badWord FOUND!!!\n";
225     return $Command_Suspicious; # messages from approved guys MAY be 
226                          # suspicious if they write about
227                          # homosexual forgers
228   }
229
230   # checking for charter-specific restrictions
231   if( !&checkCharter || ($Encoding =~ "base64") ) {
232     return "$Command_Reject charter you sent a " .
233            "binary encoded file which is not allowed.";
234   }
235
236   # Checking preapproved list
237   if( &nameIsInListExactly( $From, $goodGuys ) ) {
238   local( $from ) = $From; $from =~ s/^From: //i;
239 print STDERR "$from is a PREAPPROVED person\n";
240     return $Command_Approve;
241   }
242
243   # Here I may put some more rules...
244
245   return $Command_Suspicious;
246 }
247
248 ######################################################################
249 # set defaults
250 sub setDefaults {
251   if( !$Newsgroups ) {
252     $Newsgroups = $ENV{ "NEWSGROUP" } || die "No default newsgroup";
253   }
254 }
255
256 ################################################################# ignoreHeader
257 # some of the header fields present in emails must be ignored.
258 #
259 sub ignoreHeader {
260   local( $header ) = pop( @_ );
261
262 #  return 1 if( $header =~ /^Control:/i );
263   return 1 if( $header =~ /^Expires:/i );
264   return 1 if( $header =~ /^Supersedes:/i );
265   return 1 if( $header =~ /^Precedence:/i );
266   return 1 if( $header =~ /^Apparently-To:/i );
267   return 1 if( $header =~ /^Expires:/i );
268   return 1 if( $header =~ /^Distribution:/i );
269   return 1 if( $header =~ /^Path:/i );
270   return 1 if( $header =~ /^NNTP-Posting-Host:/i );
271   return 1 if( $header =~ /^Xref:/i );
272   return 1 if( $header =~ /^Status:/i );
273   return 1 if( $header =~ /^Lines:/i );
274   return 1 if( $header =~ /^Apparently-To:/i );
275   return 1 if( $header =~ /^Cc:/i );
276   return 1 if( $header =~ /^Sender:/i );
277   return 1 if( $header =~ /^In-Reply-To:/i );
278   return 1 if( $header =~ /^Originator:/i );
279   return 1 if( $header =~ /^X-Trace:/i );
280   return 1 if( $header =~ /^X-Complaints-To:/i );
281   return 1 if( $header =~ /^NNTP-Posting-Date:/i );
282
283   return 0;
284 }
285
286
287 ######################################################################
288 # Getting data
289
290 # reads message, sets variables describing header fields
291 #
292 # it also tries to "fix" the problem with old newsservers (B-News I think)
293 # when they try to "wrap" a submission in one more layer of meaningless
294 # headers. It is recognized by STUPID presense of TWO identical To: 
295 # fields.
296 #
297
298 sub readMessage {
299
300 #open IWJL, ">>/home/webstump/t.log";
301 #print IWJL "=========== SUBMISSION READMESSAGE\n";
302
303   open( TMPFILE, "> $TmpFile" ) or die $!;
304
305   $IsBody = 0;
306
307   my @unfolded;
308   my $readahead = '';
309
310   our $warnings=0;
311   my $warning = sub {
312     sprintf "X-STUMP-Warning-%d: %s\n", $warnings++, $_[0];
313   };
314
315 #open TTY, ">/home/webstump/t";
316   for (;;) {
317 #print TTY "=| $IsBody | $readahead ...\n";
318     if (!defined $readahead) {
319       # we got EOF earlier;
320       last;
321     }
322     if (length $readahead) {
323       $_ = $readahead;
324       $readahead = '';
325     } else {
326       $_ = <>;
327       last unless defined;
328     }
329     if (!$IsBody) {
330       # right now there is no readahead, since we just consumed it into $_
331       if ($_ !~ m/^\r?\n$/) { # blank line ? no...
332         $readahead = <>;
333         if (defined $readahead && $readahead =~ m/^[ \t]/) {
334           # this is a continuation, keep stashing
335           $readahead = $_.$readahead;
336           next;
337         }
338         # OK, $readahead is perhaps:
339         #   - undef: we got eof
340         #   - empty line: signalling end of (these) headers
341         #   - start of next header
342         # In these cases, keep that in $readahead for now,
343         # and process the previous header, which is in $_.
344         # But, first, a wrinkle ...
345         if (!m/^(?:References):/i) {
346           push @unfolded, (m/^[^:]+:/ ? $& : '????')
347             if s/\n(?=.)//g;
348           my $maxlen = 510;
349           if (length $_ > $maxlen+1) { # $maxlen plus one \n
350             chomp;
351             $_ = substr($_, 0, $maxlen);
352             $_ .= "\n";
353             $readahead = $_;
354             m/^[0-9a-z-]+/i;
355             $_ = $warning->("Next header ($&) truncated!");
356           }
357         }
358       } else {
359         # $_ is empty line at end of headers
360         # (and, there is no $readahead)
361         if (@unfolded) {
362           # insert this warning into the right set of headers
363           $readahead = $_;
364           $_ = $warning->("Unfolded headers @unfolded");
365           @unfolded = ();
366         }
367       }
368       # Now we have in $_ either a complete header, unfolded,
369       # or the empty line at the end of headers
370     } 
371 #print TTY "=> $IsBody | $readahead | $_ ...\n";
372
373     $Body .= $_;
374
375     if( !$IsBody && &ignoreHeader( $_ ) ) {
376       next;
377     }
378
379     print TMPFILE or die $!;
380   
381     chop;
382   
383     if( /^$/ ) {
384       if( !$Subject && $From =~ /news\@/) {
385         $BadNewsserver = 1;
386       }
387
388       if( $BadNewsserver ) { # just ignore the outer layer of headers
389         $To = 0;
390       } else {
391         $IsBody = 1;
392       }
393     }
394   
395     if( !$IsBody ) {
396   
397       if( /^Newsgroups: / ) { # set Newsgroups, remove spaces
398         $Newsgroups = $_;
399         $Newsgroups =~ s/^Newsgroups: //i;
400         $Newsgroups =~ s/ //g; # some fools put spaces in list of newsgroups
401       } elsif( /^Subject: / ) {
402         $Subject = $_;
403       } elsif( /^From: / ) {
404         $From = $_;
405       } elsif( /^To: / ) {
406         if( $To && ($To eq $_)) { 
407           # Old & crappy news servers that wrap submissions with one more
408           # layer of headers. For them, I simply ignore the outer
409           # headers. These (at least I think) submissions may be
410           # recognized by TWO idiotic To: header fields.
411 print STDERR "BAD NEWSSERVER\n";
412           $BadNewsserver = 1;
413         }
414         $To = $_;
415       } elsif( /^Path: / ) {
416         $Path = $_;
417       } elsif( /^Keywords: / ) {
418         $Keywords = $_;
419       } elsif( /^Summary: / ) {
420         $Summary = $_;
421       } elsif( /^Control: / ) {
422         $Control = $_;
423       } elsif( /^Message-ID: / ) {
424         $Message_ID = $_;
425       } elsif ( /^Content-Transfer-Encoding: / ) {
426         $Encoding = $_;
427         $Encoding =~ s/^Content-Transfer-Encoding: //;
428       }
429   
430     }
431   }
432 use IO::Handle;
433 #  print IWJL "SbRmE $!\n";
434   die "read message $! !" if STDIN->error;
435
436   close( TMPFILE );
437 }
438
439 ###################################################################### work
440 # all main work is done here
441
442 ######################################################################
443 # read the thing
444 &readMessage();
445
446 if( !$Newsgroups ) {
447   $Newsgroups = $Newsgroup;
448 }
449
450 ######################################################################
451 # process acks
452 &checkAck;
453 $Command_Suspicious .= " $needAck";
454
455 ######################################################################
456 # set defaults
457 &setDefaults();
458
459 ######################################################################
460 # Check
461
462 $command = &Filter;
463
464 ######################################################################
465 # process
466 print STDERR "command = $command\n";
467
468 #open IWJL, ">>/home/webstump/t.log";
469 #print IWJL "=========== SUBMISSION MAIN\n";
470
471 open( COMMAND, "| $command" ) or die $!;
472 open( TMPFILE, "$TmpFile" ) || die "cant open tmpfile";
473
474   $IsBody = 0;
475
476   while( <TMPFILE> ) {
477
478     if( $BadNewsserver && !(/^$/) ) {
479       next;
480     }
481
482     if( $BadNewsserver && /^$/ ) {
483       $BadNewsserver = 0;
484       next;
485     }
486
487     if( /^$/ ) {
488       $IsBody = 1;
489     }
490
491     if( /^From / ) {
492       print COMMAND or die $!;
493       print COMMAND "X-Origin: $X_Origin, $_" or die $! if $X_Origin;
494       print STDERR "Subject =`$Subject'\n";
495       print COMMAND "Subject: No subject given\n" or die $! if !$Subject;
496       # nothing
497     } elsif( /^From: / && !$IsBody) {
498       next if $FromWasUsed;
499
500       $FromWasUsed = 1; # note that some crappy remailers have several
501                         # "From: " fields. We really do NOT want two
502                         # "From: " to go to headers!
503
504       if( $From ) {
505         print COMMAND "$From\n" or die $!;
506         $From = "";
507       } else {
508         print COMMAND or die $!;
509       }
510     } elsif( /^Newsgroups: / && !$IsBody ) {
511       print COMMAND "Newsgroups: $Newsgroups\n" or die $!;
512     } else {
513       print COMMAND or die $!;
514     }
515   }
516
517 close( TMPFILE ) or die $!;
518 close( COMMAND ) or die "$? $!";
519
520 ################################################################## Archiving
521 # archive
522
523 #open( COMMAND, "| procmail -f- $MNG_ROOT/etc/procmail/save-incoming" );
524 #print COMMAND $Body;
525 #close( COMMAND );
526
527 unlink( $TmpFile );