[Mimedefang] Code to remove winmail.dat files and replace them with the attachments.

Matthew Schumacher matt.s at aptalaska.net
Tue Mar 15 10:56:14 EST 2005


List,

I just wrote this code to solve my winmail.dat problem (yes, I know it 
should be disabled in the client, but my users insist that they need 
it), but I wonder if I could get some peer review before I toss it into 
the wild.  Anyone else interested in this code and care to take a few 
minutes to do some testing or comment?  Once it's done I'll post it to 
the wiki.

The attached file is a patch for mimedefang-filter that comes with MD 2.51.

Thanks,

schu
-------------- next part --------------
--- mimedefang-filter.default	2005-03-02 22:41:15.000000000 -0900
+++ mimedefang-filter.tnef	2005-03-15 06:54:17.000000000 -0900
@@ -208,6 +208,18 @@
 	}
     }
 
+    # if this is a application/ms-tnef message then extract and delete it
+    if (lc($type) eq "application/ms-tnef") {
+      @tnef_files = extract_tnef( $entity );
+      my $tnef_file_qty = @tnef_files;
+      if( $tnef_file_qty > 0 ){
+        md_syslog( 'info', "ytnef extracted some files so we are now dropping the winmail.dat mime part" );
+      } else {
+        md_syslog( 'info', "ytnef didn't find any attachments, but we are dropping the winmail.dat mime part anyway" );
+      }
+      return action_drop();
+    }
+
     return action_accept();
 }
 
@@ -290,6 +302,18 @@
     # No sense doing any extra work
     return if message_rejected();
 
+    # add the tnef files to the message
+    foreach my $fname ( @tnef_files ){
+      local($/) = undef;
+      open(FILE, "$fname");
+      my $file = <FILE>;
+      close(FILE);
+      $file =~ s:^.*[\\/]::;
+      action_add_part($entity, "application/octet-stream", "base64", $file, $fname, "attachment");
+      md_syslog('info', "Added file $fname to message because it was extracted from the winmail.dat");
+    }
+
+
     # Spam checks if SpamAssassin is installed
     if ($Features{"SpamAssassin"}) {
 	if (-s "./INPUTMSG" < 100*1024) {
@@ -346,6 +370,44 @@
     # action_rebuild();
 }
 
+#***********************************************************************
+# %PROCEDURE: extract_tnef
+# %ARGUMENTS:
+#  entity -- a Mime::Entity object (see MIME-tools documentation for details)
+# %RETURNS:
+#  A array of filenames extracted from the tnef part
+# %DESCRIPTION:
+#  This function extracts the attachments from a tnef file (winmail.dat)
+#  then provides a array of the files so that they can be attached later.
+#***********************************************************************
+sub extract_tnef( $ ) {
+
+  my ( $entity ) = @_;
+  my ( $body ) = $entity->bodyhandle;
+  my @tnef_files = ();
+
+  if( ! defined( $body ) ){
+    return @tnef_files;
+  }
+
+  # Get filename
+  my ( $path ) = $body->path;
+  if( ! defined( $path ) ){
+    return @tnef_files;
+  }
+
+  @new_files = `/usr/local/bin/ytnef -f Work $path`;
+
+  foreach my $file ( @new_files ){
+    chomp( $file );
+    push( @tnef_files, $file );
+    md_syslog( 'info', "Found file $file in winmail.dat attachment" );
+  }
+
+  return @tnef_files;
+
+}
+
 # DO NOT delete the next line, or Perl will complain.
 1;
 


More information about the MIMEDefang mailing list