validateattachment 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/bin/rc
  2. # validateattachment mboxfile
  3. rfork en
  4. upastmp=/mail/tmp
  5. #
  6. # exit status matching:
  7. #
  8. # *discard* - is really bad, refuse the message
  9. # *accept* - is really good, leave attachment alone
  10. # * - rewrite attachment to have .suspect extension
  11. #
  12. if(! ~ $#* 1){
  13. echo usage: validateattachment mboxfile >[1=2]
  14. exit usage
  15. }
  16. echo validating >[1=2]
  17. fn save {
  18. d=`{date -n}
  19. cp body $upastmp/$d.$1
  20. cp raw $upastmp/$d.$1.raw
  21. whatis x >$upastmp/$d.$1.file
  22. }
  23. fn sigexit {
  24. rm -f $upastmp/$d.$1^('' .raw .file)
  25. }
  26. upas/fs -f $1
  27. cd /mail/fs/mbox/1
  28. x=`{file body | sed s/body://}
  29. x=$"x
  30. switch($x){
  31. case *Ascii* *text* *'c program'* *'rc executable'*
  32. save accept
  33. exit accept
  34. case *'zip archive'*
  35. # >[2=1] because sometimes we get zip files we can't parse
  36. # but the errors look like
  37. # unzip: reading data for philw.doc.scr failed: ...
  38. # so we can still catch these.
  39. if(unzip -tsf body >[2=1] | grep -si ' |\.(scr|exe|pif|bat|com)$'){
  40. echo executables inside zip file!
  41. exit discard
  42. }
  43. case jpeg 'PNG image' bmp 'GIF image' *'plan 9 image'*
  44. save accept
  45. exit accept
  46. case *Microsoft* *Office*
  47. save wrap
  48. exit wrap
  49. case *MSDOS*
  50. # no executables
  51. echo $x
  52. exit discard
  53. }
  54. save wrap
  55. exit wrap