Posted by sgj700 on Jun 16, 2008 in
bash and perl
Perl script to display files in a folder (and delete them) via HTML
Place this code into an “index.cgi” file, and open that file in a browser:
#!/usr/bin/perl
$dir = “<absolute path to the directory you’d like to see contents of>”;
$folder = “http://<your web server path to that directory>”;
use CGI; # load the CGI.pm module
my $GET = new CGI; # create a new object
my @VAL = $GET->param; #get all form field names
my $filename = $GET->param($_[0]);
&PrintHead; #print the header
if($GET->param(‘delfile’)) {
print “<hr><form>Are you sure you want to delete the <b>”.$GET->param(‘delfile’).”</b> file?
<input type=submit value=Delete name=”.$GET->param(‘delfile’).”>
<input type=submit name=cancel value=Cancel>
<input type=hidden name=del_confirm value=”.$GET->param(‘delfile’).”>
</form>
<hr>
<br>”;
}
if($GET->param(‘del_confirm’) and !$GET->param(‘cancel’)) {
if (unlink($GET->param(‘del_confirm’)) == 0) {
}
print “<hr><form>The <b>”.$GET->param(‘del_confirm’).”</b> file has been deleted.”;
}
print qq~
<html>
~;
print qq~
<table align=”center” width=”500″>
<tr><td><font face=”Arial” size=”2″><b>Current files in folder</b></td></tr>
<tr>
<td valign=”top”>
<font face=”Arial” size=”2″>
~;
opendir(DIR, “$dir”);
@current = readdir(DIR);
closedir(DIR);
foreach(@current){
unless($_ eq ‘.’ || $_ eq ‘..’ || $_ eq ‘.htaccess’ || $_ =~ ‘index.cgi’ || -d qq~$dir/$_~){
push(@currentfiles, $_);
}
}
@currentfiles = sort { uc($a) cmp uc($b) } @currentfiles;
for($aa = 0; $aa <= int($#currentfiles / 2); $aa++){
print qq~
<font color=”#ff0000″><b>•</b>
<a href=”$folder/$currentfiles[$aa]” target=”_blank”>$currentfiles[$aa]</a><a href=”index.cgi?delfile=$currentfiles[$aa]“> (Delete)</a></font><br>
~;
}
print qq~</font></td><td valign=”top”><font face=”Arial” size=”2″>~;
for($bb = $aa; $bb < @currentfiles; $bb++){
print qq~
<font color=”#ff0000″><b>•</b>
<a href=”$folder/$currentfiles[$bb]” target=”_blank”>$currentfiles[$bb]</a><a href=”index.cgi?delfile=$currentfiles[$bb]“> (Delete)</a></font><br>
~;
}
print qq~
</font></td>
</tr>
</table>
</html>~;
&PrintFoot; #print the footer
sub PrintHead {
print qq~Content-type: text/html\n\n~;
print qq~
<html>
<title>Peds Uploads</title>
<body bgcolor=”#ffffff”>
~;
}
####################################################################
#
#####################################################################
#
sub PrintFoot {
print qq~
</body>
</html>
~;
Posted by sgj700 on May 25, 2008 in
Uncategorized
PROBLEM
Recently we found ourselves in a situation where an Exchange 2003 email distribution list won’t work for our “faculty@” and “all@” lists because:
- non-Active Directory-account-holding people outside of our Exchange organization needed to contact the list on occasion
- Our Exchange Users (doctors) would occasionally need to get an email through to colleagues ASAP, OFF-HOURS, and waiting for their email to be moderated wouldn’t be an option
- We had a large number of doctors in the distribution list
- We didn’t want to risk having spammers send junk to everyone on the list
- Filtering the list server-side via Ninja isn’t supported on Exchange email distribution lists
- Filtering the list on the gateway via spamassassin isn’t going to work well for lists
SOLUTION
- set up mailman on the gateway to handle the lists
- auto-accept emails from anyone internal to the Exchange organization
- auto-maintain the mailman lists membership based upon Active Directory group membership
- hold for moderation emails from anyone else
———————————————————————————–
1) set up mailman on the gateway to handle the lists
See mailman documentation for more details. The gist:
Mailman receives emails bound for “faculty@lists.<your domain>”. It checks the “approved senders” list here: /etc/postfix/access and immediately distributes emails sent from those people. Anyone else gets moderated first (usually spam, which the sysadmin is notified of and either approves or deletes at his/her leisure).
2) auto-accept emails from anyone internal to the Exchange organization
Use a perl script to pull the email addresses of everyone in an Active Directory group. The script is below… plug in your Active Directory settings (domain info, OU path info, authentication account username and password).
———————————————————————————–
You’ll be calling this script by passing in the name of your Active Directory group:
perl /scripts/getadsmtp_postfix_senders.pl Faculty-members
(where “Faculty-members” is a group in Active Directory that is located here: OU=Distribution_Lists,OU=All_Users,DC=mydomain,DC=example,DC=com )
———————————————————————————–
———————————————————
##!/usr/bin/perl -T -w
# getadsmtp_postfix_senders.pl
# This script will pull all users’ SMTP addresses from your Active Directory
# (including primary and secondary email addresses) and list them in the
# format “user@example.com OK” which Postfix uses with relay_recipient_maps.
# Be sure to double-check the path to perl above.
# This requires Net::LDAP to be installed. To install Net::LDAP, at a shell
# type “perl -MCPAN -e shell” and then “install Net::LDAP”
use Net::LDAP;
use Net::LDAP::Control::Paged;
use Net::LDAP::Constant ( “LDAP_CONTROL_PAGED” );
# Enter the path/file for the output
$VALID = “/etc/postfix/sender_ldap_paste/$ARGV[0]“;
# Enter the FQDN of your Active Directory domain controllers below
$dc1=”<dc1.mydomain.example.com>”;
$dc2=”<dc2.mydomain.example.com>”;
### works for showing all users
$hqbase=”dc=mydomain,dc=example,dc=com”;
$user=”cn=ldap user,ou=Application_Accounts,ou=All_Users,dc=mydomain,dc=example,dc=com”;
$passwd=”abcd1234″;
# Connecting to Active Directory domain controllers
$noldapserver=0;
$ldap = Net::LDAP->new($dc1) or
$noldapserver=1;
if ($noldapserver == 1) {
$ldap = Net::LDAP->new($dc2) or
die “Error connecting to specified domain controllers $@ \n”;
}
$mesg = $ldap->bind ( dn => $user,
password =>$passwd);
if ( $mesg->code()) {
die (“error:”, $mesg->code(),”\n”,”error name: “,$mesg->error_name(),
“\n”, “error text: “,$mesg->error_text(),”\n”);
}
# How many LDAP query results to grab for each paged round
# Set to under 1000 for Active Directory
$page = Net::LDAP::Control::Paged->new( size => 990 );
@args = ( base => $hqbase,
filter => “(memberOf=CN=$ARGV[0],OU=Distribution_Lists,OU=All_Users,DC=mydomain,DC=example,DC=com)”,
control => [ $page ],
#attrs => “proxyAddresses”,
attrs => “mail”,
);
my $cookie;
while(1) {
# Perform search
my $mesg = $ldap->search( @args );
# Filtering results for proxyAddresses attributes
foreach my $entry ( $mesg->entries ) {
my $name = $entry->get_value( “cn” );
# LDAP Attributes are multi-valued, so we have to print each one.
foreach my $mail ( $entry->get_value( “mail” ) ) {
push(@valid, $mail.”\tOK\n”);
}
}
# Only continue on LDAP_SUCCESS
$mesg->code and last;
# Get cookie from paged control
my($resp) = $mesg->control( LDAP_CONTROL_PAGED ) or last;
$cookie = $resp->cookie or last;
# Set cookie in paged control
$page->cookie($cookie);
}
if ($cookie) {
# We had an abnormal exit, so let the server know we do not want any more
$page->cookie($cookie);
$page->size(0);
$ldap->search( @args );
# Also would be a good idea to die unhappily and inform OP at this point
die(“LDAP query unsuccessful”);
}
# Only write the file once the query is successful
open VALID, “>$VALID” or die “CANNOT OPEN $VALID $!”;
print VALID @valid;
### print to screen
#print @valid;
close VALID;
———————————————————
Auto-maintain mailman’s “approved senders list” dyanmically from Active Directory by running this bash script via cron (you can do it nightly or hourly, but the script does restart postfix, which may or may not be OK do do during the day depending on your environment)
#!/bin/bash
rm /etc/postfix/sender_ldap_paste/Faculty-members_old
mv /etc/postfix/sender_ldap_paste/Faculty-members /etc/postfix/sender_ldap_paste/Faculty-members_old
perl /scripts/getadsmtp_postfix_senders.pl Faculty-members
rm /etc/postfix/access
cp /etc/postfix/sender_ldap_paste/Faculty-members /etc/postfix/access
postmap /etc/postfix/access
service postfix restart
———————————————————
3) auto-maintain the mailman lists membership based upon Active Directory group membership
Use that same perl script to create a separate membership file, then use mailman’s “sync_members” command to update the mailman list’s members
rm /var/lib/mailman/ad_membership/Faculty-members_old
mv /var/lib/mailman/ad_membership/Faculty-members /var/lib/mailman/ad_membership/Faculty-members_old
perl /scripts/getadsmtp_default.pl Faculty-members
/usr/lib/mailman/bin/sync_members -f /var/lib/mailman/ad_membership/Faculty-members faculty
Posted by sgj700 on May 25, 2008 in
Uncategorized
#!/bin/bash
#release_n_emails_from_hold.sh
ls /var/spool/postfix/hold/ > /tmp/hold
COUNTER=0
RELEASE_MAX=10 #modify me as needed
RELEASED_N=0
for l in `cat /tmp/hold` ; do
if [ $COUNTER -lt $RELEASE_MAX ]; then
echo Unholding $l
let RELEASED_N=RELEASED_N+1
fi
let COUNTER=COUNTER+1
done
echo There were $COUNTER emails in the hold que. We released $RELEASED_N
Tags: postfix bash script release emails
Posted by sgj700 on May 25, 2008 in
Uncategorized
#!/usr/bin/perl
use strict;
use warnings;
my $high = 200;
my $string = `postqueue -p|grep ” Requests.”`;
my $length_string = length $string;
my $start_string = index($string, ‘ in ‘)+4;
my $end_string = index($string, ‘ Requests.’);
my $fragment = substr $string, $start_string, ($end_string-$start_string);
if ($fragment > $high) {
print “que of $fragment above threshold of $high.\n”;
system(‘postqueue -p’);
system(’service MailScanner restart’);
} else {
print “que of $fragment is fine.\n”;
}
#exec(‘postqueue -p|grep ” Requests.”‘);
#system(‘postqueue -p’);
Tags: perl mailscanner script email