Monday, March 29, 2010

Adding HTTP Authentication to Tomcat Container

I used the following method to secure the entire application. We use .htaccess based authentication setup in apache. I wanted a same sort of setup in tomcat instance.

Initially I added a user in the $catalina_home/conf/tomcat-users.xml







Then I add the security constraint in the web.xml of the application. In my case it was the default application ROOT. Hence I added the following entries in $catalina_home/webapps/ROOT/WEB-INF/web.xml





Entire Application
/*



agroup





BASIC
Authenticate yourself





The role that is required to log in to the Manager Application

agroup




Hence all the users under the security role "agroup" will be able to access the applicationn.

Hope this helps some one somewhere..

Thursday, March 18, 2010

Bash Script to send email using smtp authentiation and bash file descriptors

Sometimes we require bash script to perform some operation and use a remote mail server to send mails. In this instance we require smtp authentication for the remote server to allow this operation.

The script was copied from the link http://linuxshellaccount.blogspot.com/2008/04/shell-script-to-send-mail-using-bash.html which describes networking with the help of file descriptors.

I just added a smtp authentication part.

Please ensure that others do not have access to your bash history for security reasons.

Usage

sh mail.sh report@yourserver.com youremail@gmail.com yourserver.com yourmailserver.com filecontainingmessage email@yourmailserver.com youremailpassword





SCRIPT CONTENTS:


mail.sh




#!/bin/bash

#
# mail.sh
#
# 2008 - Mike Golvach - eggi@comcast.net
# 2010 - Rayber
#
# Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
#

if [ $# -ne 7 ]
then
echo "Usage: $0 FromAdress ToAdress Domain MailServer MailTextFile AuthEmail AuthPass"
exit 1
fi

from=$1
to=$2
domain=$3
mailserver=$4
mailtext=$5
authemail=`echo $6|openssl enc -base64|awk 'sub("..$", "")'`
authpass=`echo $7|openssl enc -base64|awk 'sub("..$", "")'`

if [ ! -f $mailtext ]
then
echo "Cannot find your mail text file. Exiting..."
exit 1
fi

exec 9<>/dev/tcp/$mailserver/25
echo "HELO $domain" >&9
read -r temp <&9
echo "$temp"
echo "auth login" >&9
read -r temp <&9
echo "$authemail" >&9
read -r temp <&9
echo "$authpass" >&9
read -r temp <&9
echo "Mail From: $from" >&9
read -r temp <&9
echo "$temp"
echo "Rcpt To: $to" >&9
read -r temp <&9
echo "$temp"
echo "Data" >&9
read -r temp <&9
echo "$temp"
cat $mailtext >&9
echo "." >&9
read -r temp <&9
echo "$temp"
echo "quit" >&9
read -r temp <&9
echo "$temp"
9>&-
9<&-
echo "All Done Sending Email. See above for errors"
exit 0



When you run the script you should get a output similar to the following output.

235 2.7.0 Authentication successful
250 2.1.0 Ok
250 2.1.5 Ok
354 End data with .
250 2.0.0 Ok

Tuesday, March 9, 2010

Remove or Disable svn authentication data

When you issue a svn co statement the authentication gets cached in the home directory of the user.

svn co http://192.168.1.13/repo
Authentication realm: Subversion Repository
Username: test
Password for 'test':

The authentication information gets stored in a cache /root/.subversion/auth/svn.simple directory.


o remove cached data go to “.subversion/auth/svn.simple” folder and delete the particular file.
There will be key (K)-value (V) pairs. “username” and “svn:realmstring” together can identify the user.
So use for example:

grep servername ./*

to find the right file.

Disable caching by opening “config” file in “.subversion” folder and setting the values of “store-passwords” and “store-auth-creds” to “no” or use --no-auth-cache as command line argument.


/root/.subversion/config

Wednesday, February 3, 2010

Mysql Database size calculation in MB

These are some small queries to find the total disk space used by the mysql database.

1. Get the Size of all the tables as well are databases.

SELECT TABLE_SCHEMA AS 'Database', TABLE_NAME AS 'Table', CONCAT(ROUND(((DATA_LENGTH + INDEX_LENGTH - DATA_FREE) / 1024 / 1024),2)," Mb") AS Size FROM INFORMATION_SCHEMA.TABLES;


2. Get the size of specific tables in the database.

SELECT TABLE_SCHEMA AS 'Database', TABLE_NAME AS 'Table', CONCAT(ROUND(((DATA_LENGTH + INDEX_LENGTH - DATA_FREE) / 1024 / 1024),2)," MB") AS Size FROM INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA like 'testdb';

Where testdb is the database for which you want to know the details.

3. Get the size of the entire DB

select CONCAT(sum(ROUND(((DATA_LENGTH + INDEX_LENGTH - DATA_FREE) / 1024 / 1024),2))," MB") AS Size FROM INFORMATION_SCHEMA.TABLES where table_schema like 'testdb';



Another query to display the above result would be

4. display the disk size of all the databases

SELECT table_schema "Data Base Name", sum( data_length + index_length ) / 1024 / 1024 "Data Base Size in MB"
FROM information_schema.TABLES GROUP BY table_schema ;








The 3'rd query would not give you a correct result for large database in AMAZON RDS at the time of writing. I had to use the 4'th query to obtain a approximate value.

Monday, January 11, 2010

Monitor Memcached status

It is quite a important task to manage and monitor the memcache caching system. There are quite a lot of options to carry out this operation.

1. Using Telnet Command Line interface

List of supported commands are available at

http://code.sixapart.com/svn/memcached/trunk/server/doc/protocol.txt



telnet localhost 11211



List of keys used

stats items


STAT items:5:number 2
STAT items:5:age 47
STAT items:5:evicted 0
STAT items:5:evicted_nonzero 0
STAT items:5:evicted_time 0
STAT items:5:outofmemory 0
STAT items:5:tailrepairs 0
END



2. Using memcached-tool

The memcached tool is available in the source directory .

/usr/src/memcached-1.4.4/scripts/memcached-tool

Stats

/usr/src/memcached-1.4.4/scripts/memcached-tool 127.0.0.1:11211 stats

Show slab information

/usr/src/memcached-1.4.4/scripts/memcached-tool 127.0.0.1:11211 display

Show keys and values

/usr/src/memcached-1.4.4/scripts/memcached-tool 127.0.0.1:11211 dump


3. Using the damemtop

A flexible 'top' like utility for viewing memcached clusters. You can use it to manage a interactive cluster status.

The utility is available in the source directory. /usr/src/memcached-1.4.4/scripts/damemtop


It require you to install the following perl modules

cpan AnyEvent Term::ReadKey YAML


Now you need the /etc/damemtop.yaml file with the following contents.


delay: 3
mode: t
top_mode:
sort_column: "hostname"
sort_order: "asc"
columns:
- hostname
- all_version
- all_fill_rate
- hit_rate
- evictions
- bytes_written
- "2:get_hits"
servers:
- 127.0.01:11211


In a distributed environment you can add all the servers in the cluster to the file and issue the following command.


/usr/src/memcached-1.4.4/scripts/damemtop


That would give you a ideal monitoring setup..

Sunday, January 10, 2010

Install Memcached caching sytem in centos from source

memcached is a high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.

It is mainly used in MYSQL + PHP or MYSQL + JAVA applications which are quite database intensive.

Install LIBEVENT


yum -y install libevent libevent-devel


Download Source

wget http://memcached.googlecode.com/files/memcached-1.4.4.tar.gz
tar zxf memcached-1.4.4.tar.gz
cd memcached-1.4.4
./configure && make && make install



Test the installation


memcached -m 256 -u nobody -vv
slab class 1: chunk size 80 perslab 13107
slab class 2: chunk size 104 perslab 10082
<29 send buffer was 110592, now 268435456
<29 server listening (udp)
<29 server listening (udp)



Start the Daemon


memcached -d -m 256 -u nobody -p 11211 -l 192.168.1.33


The above command will start the memcached daemon in port 11211

Ensure that it is working.

netstat -plan|grep 11211
tcp 0 0 192.168.1.33:11211 0.0.0.0:* LISTEN 7049/memcached




Thats it.. Enjoy the power of caching system..

Friday, January 8, 2010

Check Ubuntu 8.04 server security using OpenVAS

Install openvas server from source in ubunutu 8.04


apt-get install build-essential libgtk2.0-dev libglib2.0-dev libssl-dev htmldoc libgnutls-dev libpcap0.8-dev bison libgpgme11-dev libsmbclient-dev snmp pnscan


add the following line to /etc/apt/sources.list


deb http://ftp.de.debian.org/debian sid main


Install the server


apt-get install openvas-server openvas-server-dev openvas-plugins-dfsg openvas-plugins-base libopenvas2 libopenvas2-dev libopenvasnasl2 libopenvasnasl2-dev


Add your user


openvas-adduser


Update the feeds


openvas-nvt-sync



start the server


/etc/init.d/openvas-server start


Install the client

It would ask you to upgrade php etc and hence I compiled the agent from source. The trunk is incompatible with the openvas-server provided by debian repository.


svn co https://svn.wald.intevation.org/svn/openvas/branches/openvas-client-2-0/ openvas-client



cd openvas-client

./configure && make && make install


Running the agent in batch mode:

You can run the client to produce beautiful html with graphs using the following command line.

create a file named target with the host you want to check

cat >target
localhost
ctrl+c


OpenVAS-Client -q localhost 9390 ovasuser mypasswd target localhost-result -T html_graph -x


The run would take around 15 minutes and you will get the results in the localhost-result directory. View the HTML files using browser.