Category: Perl & Python

Simple LDAP Caching Unix Daemon

Simple LDAP Caching Unix Daemon

Introduction-

I wrote this simple LDAP Caching unix daemon 2 yrs back when we faced lot of issue with Integrating Apache with LDAP authentication
using Apache ldap auth module. We were able to configure it properly however we faced slowness issue.

Basically We wanted to use Nagios (Check_mk Multisite) with LDAP authentication. So we wrote this unix daemon.
As of now this is very simple (no theading/forking and it is blocking) However it is working perfectly without any issue (for Nagios web interface authentication and few other web based tools, around 300+ users).

Download Perl files-

* ldapcached.pl
http://www.indiangnu.org/wp-content/uploads/2013/ldapcached-pl.txt

* ldapcached-client.pl
http://www.indiangnu.org/wp-content/uploads/2013/ldapcached-client-pl.txt

* Custom Apache Handler – to use this daemon for basic authentication
http://www.indiangnu.org/wp-content/uploads/2013/MyHandler-pm.txt

root@arunb:~# cat /etc/init.d/ldapcached-initd.pl
#!/usr/bin/perl
use strict;
use warnings;
use Proc::Daemon;

no warnings ‘uninitialized’;
if ( $ARGV[0] =~ m/start/ ) {
Proc::Daemon::Init;
my $continue = 1;
$SIG{TERM} = sub { $continue = 0 };
while ($continue) { eval { `/usr/local/ldapcached.pl –daemon`;};}

} else { print ” * Usage: $0 {start}”;}
#end
print “\n”;
root@arunb:~#

Step 1] Run Daemon – (edit LDAP related varibles)

Copy ldapcached.pl perl file at following location on your system /usr/local/ldapcached.pl
OR Change path in “ldapcached-initd.pl” startup file

* Start process-

root@:~# /etc/init.d/ldapcached-initd.pl start
root@:~#

* Make sure only one ldapcached process running?

root@:~# ps aux | grep ldap | grep -v grep
root 19441 0.0 0.0 6212 1408 ? S 15:03 0:00 /usr/bin/perl /etc/init.d/ldapcached-initd.pl start
root 19442 0.0 0.2 8656 4916 ? S 15:03 0:00 ldapcached
root@:~#

Step 2] How to test-

root@:~$ perl ldapcached-client.pl –client ‘my-ldap-user’ ‘my-ldap-pass’
Failed
root@:~$

root@:~$ perl ldapcached-client.pl –client ‘my-ldap-user’ ‘my-ldap-pass’
Pass
root@:~$

Step 3] How I should use/integrate in Application-

Say I want to use this ldapcached unix daemon for Apache/Basic Authentication –

NOTE- Make sure to copy MyHandler.pm module in Perl module directory or Check Apache error log for any error.

ScriptAlias /nagios/cgi-bin “/usr/lib64/nagios/cgi”

<Directory “/usr/lib64/nagios/cgi”>
Order allow,deny
Allow from all
AuthType Basic
AuthName “Nagios GUI”
PerlAuthenHandler Apache::MyHandler
Require valid-user
</Directory>

Thank you,
Arun

Firefox yslow and Showslow for Website testing and automation

Firefox yslow and Showslow for Website testing and automation

Introduction-

Showslow is web based interface to collect yslow data. We can use yslow+showslow to get website loadtime and various other details provided by yslow.

This howto explains how to automate website loadtime/yslow testing using Firefox and Perl (Mechanize::Firefox, Firefox::Application module)

* Please download perl scripts and Strawberry perl for Windows-

URL-

http://www.indiangnu.org/wp-content/uploads/2012/firefox-cli-for-yslow-url-pl.txt

http://www.indiangnu.org/wp-content/uploads/2012/firefox-cli-for-yslow-url-from-csv-pl.txt

http://www.indiangnu.org/wp-content/uploads/2012/yslow-firefox-sh.txt

* Reguirement-

1) Firefox -MozRepl extension to connect to firefox using socket -firebug and yslow extension/addon

2) Perl -WWW::Mechanize::Firefox module -Firefox::Application module *

1] How to create Firefox profile-

root@arunb:/home/arunb/firefox-yslow# /var/firefox-4.0.1/firefox/firefox -CreateProfile yslow /home/arunb/firefox-yslow/yslow_firefox_profile Success: created profile ‘yslow’ at ‘/root/.mozilla/firefox/rjel2s26.yslow/prefs.js’ root@arunb:/home/arunb/firefox-yslow#

NOTE- On Window create profile with ‘firefox -p’ command

* 2] Showslow Setup-

Download showslow and put in Documentroot directory of apache/web server. Then Create DB,Add DB details in config file and type ‘make’ in source directory mysql> create database showslow; mysql> grant all privileges on showslow.* to ‘showslow’@localhost identified by ‘mypass’;

* 3] Now Configure Firefox to submit "yslow" results to "showslow"

Yslow 2.x extensions.yslow.beaconUrl = http://mywebserver/showslow/beacon/yslow/ extensions.yslow.beaconInfo = grade extensions.yslow.optinBeacon = true extensions.yslow.autorun = true

IMP- Please enable following setting in firefox. extensions.firebug.allPagesActivation", "on"

* 4] Perl and Firefox/Mechanize Module –

root@arunb:/home/arunb/firefox-yslow# ./firefox-cli-for-yslow-url.pl http://www.glam.com

For more info – http://www.showslow.org/Tools_configuration#ShowSlow_Installation_and_configuration http://getfirebug.com/wiki/index.php/Firebug_Preferences

Thank You,
Arun Bagul

perl script to check Bind dns zone files

perl script to check Bind dns zone files

Introduction –

dns-zone-verify.pl is perl script used to check dns zone file syntax! This script verifies both Forward and Reverse Zone.
Internally perl script is using named-checkzone and named-checkconf. Bind dns commands to check zone file syntax.
Script is reading named.conf file to get all zone

* Download – Please download perl script from following locations…

http://www.indiangnu.org/wp-content/uploads/2012/dns-zone-verify-pl.txt

* How to run/use –

root@localhost~# perl ./dns-zone-verify.pl

* Usage: ./dns-zone-verify.pl { –verify }

root@localhost~# perl ./dns-zone-verify.pl –verify

zone myzone_internal.file/IN: loaded serial 2007013101
OK

zone myzone_external.file/IN: loaded serial 2012100527
OK
….
root@localhost~#

Thank you,
Arun Bagul

Parse FTP server log and send mail for every uploaded files

Parse FTP server log and send mail for every uploaded files

Introduction –

One of my friend was looking for script to parse FTP server log after every 10mins and send mail for every file uploaded by user
with Username, Uploaded File Name, Date and Size.  Please prefer the follow perl script to this task….

Download ftp file from here – http://www.indiangnu.org/wp-content/uploads/2012/ftp_logparser-pl.txt

1] Script –

arunb@mylaptop:~$ cat perl-prog/ftp_logparser.pl

#!/usr/bin/perl

use strict;
use warnings;

no warnings 'uninitialized';

my $argument = $#ARGV + 1 ;
my $mylog_file = $ARGV[0];

if ( -f $mylog_file ) {
  print "\nChecking FTP log file for Upload files ~ $mylog_file";
  open(FILE, "<$mylog_file") or die("\nFile not found : $! ");

  print "\nContent of file is....\n";
  while() {
	chomp($_);
	## Fri Jul  6 14:15:25 2012 [pid 27841] [vivek] OK UPLOAD: Client "172.30.1.76"," /squid-2 7-Template-v23.docx", 594488 bytes
	#print "\nLine=> $_";
	if ($_ !~ /^\s*$/) {
	  #print "\nLine=> $_";
	  if ($_ =~ m/(.*)\s+\[pid.*\]\s+\[(.*)\]\s+OK\s+UPLOAD:\s+Client\s+"(\d+.\d+.\d+.\d+)","(.*)",\s+(\d+)\sbytes.*/) {
	     my ($mydate, $myser,$ipaddr, $file_upload, $file_size) = ($1,$2,$3,$4,$5);
	     print "\n'$mydate' - '$myser' - '$ipaddr' - '$file_upload' - '$file_size'";
	     ## send mail
	     my $CONTACTEMAIL = "abc\@xyz.com";
	     open(MAIL, "|/usr/sbin/sendmail -t");
	     print MAIL "To: $CONTACTEMAIL\n";
	     print MAIL "From: ftp\@abc.com\n";
	     print MAIL "Subject: File Uploaded- $file_upload";
	     #print MAIL "\nContent-type: text/html\n\n";
	     print MAIL "\nContent-type: text/plain\n\n";
	     print MAIL "\nFile Name: $file_upload";
	     print MAIL "\nFile Size: $file_size bytes";
	     print MAIL "\nUser: $myser";
	     print MAIL "\nIPaddr: $ipaddr";
	     print MAIL "\nDate: $mydate";
	     close(MAIL);
	  }
	}
  }
  close(FILE);

  print "\nDone";
} else {
   print "\n * Usage: $0  \n";
}

#end
print "\n";
arunb@mylaptop:~$

2] How to Use –

arunb@mylaptop:~$ ./perl-prog/ftp_logparser.pl

* Usage: ./perl-prog/ftp_logparser.pl

arunb@mylaptop:~$ ./perl-prog/ftp_logparser.pl /var/log/ftp.log

Thank you,
Arun Bagul

Perl CGI – Session and Cookie howto

Perl CGI – Session and Cookie howto

Introduction –

Almost  2 year back (today also!)  I struggled a lot for implementing session and cookie in Perl CGI application.  So thought to share my work with you all. I wanted to do it in my way…

Assumption, your web server ie Apache is enabled to run CGI scripts

CGI directory location – /var/application/www/cgi-bin/
Htdocs location – /var/application/www/
Perl Module direcotry – /var/application/module/

Step 1] Write Auth.pm Perl module –

Please simply copy following Auth.pm perl module for authentication using Session and Cookies…

[root@arun ~]# cat /var/application/module/Auth.pm

package Auth;

### Subroutine to authenticate user
sub  User
{
my ($ref_page) = (@_);
### Session information
my $sid = $ref_page->cookie("APP_SID") || undef;
my $session = CGI::Session->load(undef,$sid);
if ( $session->is_expired ) { print $ref_page->redirect(-location => '../arun.html');}
elsif ( $session->is_empty) { print $ref_page->redirect(-location => '../arun.html');}
else { print $ref_page->header();}
# don't forget to create dir '/var/tmp'
# with proper ownership/permission
#$session = new CGI::Session(undef, $sid, {Directory=>'/var/tmp'});
#################################################
return($session->param('login_user'));
}

1;
[root@arun ~]#

Step 2] authe_me.pl –

authe_me.pl file is used to set cookies and verify username/password. You may use MySQL DB to store username and password.  In this case you have to this file…

[root@arun ~]# cat /var/application/www/cgi-bin/auth_me.pl
#!/usr/bin/perl

sub BEGIN
{
unshift (@INC, '/var/application/module/');
}

use strict;
use warnings;
use CGI qw(:standard);
use CGI::Session;
use Auth; ## our module

### Header
########################
my $page = CGI->new();
##print $page->header();

##########
if ( $ENV{REQUEST_METHOD} eq "POST" )
{
my %form;
my $session_dir="/var/tmp";
 my ($admin_user,$admin_password) = ("admin","arun123");

foreach my $key (param()) { $form{$key} = param($key);}
##
if (($form{username}) && ($form{password}))
{

### Session Details ###
CGI::Session->name("APP_SID");
## Create new session
my $session = new CGI::Session(undef, undef, {Directory=>$session_dir});
 ## Set cookies
my $cookie = $page->cookie(-name=>$session->name(),-value=>$session->id(),-expires=>'+2h',-path=>'/');
## Store data in session variable and save it
$session->param('login_user',$form{username}); # OR
##$session->param(-name=>'login_user',-value=>$form{username});
$session->save_param($page, ["login_user"]);

## Session and Cookie expiration time is SAME.
$session->expire("+2h");
#### Session Details end ####

## if login successful redirect to main.pl else login page
if (($form{username} eq $admin_user) and ($form{password} eq $admin_password))
{ print $page->redirect(-location => 'main.pl',-cookie=>$cookie);}
else { print $page->redirect(-location => '../arun.html'); }
############################
} else { print $page->redirect(-location => '../arun.html'); }
}

[root@arun ~]#

Step 3] Create Login Page –

[root@arun ~]# cat /var/application/www/arun.html
<html>
<title>Arun Login Page</title>

<!-- Form start -->
<table align='center' border='1'>
<form method="POST" action="cgi-bin/auth_me.pl">
<tr>
<td><label>Login</label></td>
<td><input name="username" type="text"></td>
</tr>
<tr>
<td><label>Password</label></td>
<td><input name="password" type="password"><br/></td>
</tr>
<tr>
<td><input value="Submit" type="submit"></td>
</tr>

</form>
</table>

</html>

[root@arun ~]#

Step 4] Create main page where Session and Cookie authentication verified – main.pl

[root@arun ~]# cat /var/application/www/cgi-bin/main.pl
#!/usr/bin/perl

sub BEGIN
{
unshift (@INC, '/var/application/module/');
}

use strict;
use warnings;
use CGI qw(:standard);
use CGI::Session;
use Auth;

### Header
my $page = CGI->new();
## check authentication
my $login_name=Auth::User($page);
###
print $page->start_html( -title=>'Arun Main Page');

print "<h3>This is Main Page</h3></br>";
print "<br>Login Name - $login_name";

#end
[root@arun ~]#

Step 5] Please access login page and try http://your_ipaddr/arun.html

Thank you,
Arun