tvalidateattachment - plan9port - [fork] Plan 9 from user space
 (HTM) git clone git://src.adamsgaard.dk/plan9port
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       tvalidateattachment (1724B)
       ---
            1 #!/usr/local/plan9/bin/rc
            2 rfork n
            3 
            4 . 9.rc
            5 
            6 # exit status matching:
            7 #
            8 #        $discard - is really bad, refuse the message 
            9 #        $accept - is really good, leave attachment alone
           10 #        anything else - rewrite attachment to have .suspect extension
           11 # 
           12 
           13 # magic exit statuses known to vf
           14 accept=10
           15 discard=13
           16 
           17 wrap=123        # anything but 10, 13
           18 
           19 if(! ~ $#* 1){
           20         echo usage: validateattachment mboxfile >[1=2]
           21         exit usage
           22 }
           23 
           24 # some idiot virus is sending around attachments marked as .zip
           25 # that are completely bogus and just say %TS_ZIP_ATTACH%
           26 # as the base64 encoding of the zip file.  gmail rejects all zip
           27 # attachments when we forward them, so nip this one here.
           28 if(grep -s '^%TS_ZIP_ATTACH%$' $1 && ~ `{wc -l <$1} 1 2 3 4 5 6 7 8 9 10){
           29         echo bogus zip file!
           30         exit $discard
           31 }
           32 
           33 upas/unvf < $1 >$1.unvf
           34 file=$1.unvf
           35 fn sigexit { rm $file }
           36 
           37 fn save {
           38 #        d=`{date -n}
           39 #        cp $file /n/other/upas/tmp/$d.$1
           40 #        cp raw /n/other/upas/tmp/$d.$1.raw
           41 #        whatis x >/n/other/upas/tmp/$d.$1.file
           42 }
           43 
           44 x=`{file <$file | sed s/stdin://}
           45 x=$"x
           46 switch($x){
           47 case *Ascii* *text* *'c program'* *'rc executable'* 
           48         save accept
           49         exit $accept
           50 
           51 case *'zip archive'*
           52         
           53         # >[2=1] because sometimes we get zip files we can't parse
           54         # but the errors look like
           55         # unzip: reading data for philw.doc.scr failed: ...
           56         # so we can still catch these.
           57         if(! unzip -tsf $file >[2=1] >/dev/null){
           58                 echo corrupt zip file!
           59                 exit $discard
           60         }
           61         if(unzip -tsf $file >[2=1] | grep -si '      |\.(scr|exe|pif|bat|com)$'){
           62                 echo executables inside zip file!
           63                 exit $discard
           64         }
           65 
           66 case jpeg 'PNG image' bmp 'GIF image' *'plan 9 image'
           67         save accept
           68         exit $accept
           69 
           70 case *Microsoft* *Office*
           71         save wrap
           72         exit $wrap
           73 
           74 case *MSDOS*
           75         # no executables
           76         echo $x
           77         exit $discard
           78 }
           79 
           80 save wrap
           81 exit $wrap
           82 
           83