Friday, October 31, 2008

SMS Alerts on root logins

Last week I met Anuradha at Kandy and we talked about various stuff. At one time, Anuradha mentioned about this sms alerting method and how we can use that tools in our working environment. So I thought of trying it out (not in an operational server but in a testing server).

There are several ways to get an alert on your mobile phone when there has being a root login in your server. This is not for a critical server set up. There are other better alert systems for those kind of systems. But, for a small business server, home network this is a very cheep and cool solution.

Assumption:

Your server is a GNU/Linux (I used debian/Ubuntu distribution)

What you need :

  • A Mobile Phone with USB cable.
  • smstools package.
  • Your time.
Let's start.

Install smstools.

# apt-get install smstools

Plug in the phone with the USB cable. And get the device name.

# dmesg |grep usb

eg: You should see some thing like /dev/ttyACM0 or similar. If your phone is not detected then you have to troubleshoot that. ;-)

Now you can edit the configuration file and set up the device and baudrate.

# vim /etc/smsd.conf

#init =
device = /dev/ttyACM0
incoming = yes
#pin =
baudrate = 460800

Most of the time you don't have give modem init commands, cos modern phones have their own init commands.

Start the smstools

# /etc/init.d/smstools start

The location where your messages handled is;

$ /var/spool/sms/

There you have several directories.

checked - Messages cued for various checking, and ready to send after checking.
failed - Messages which being failed to send, and the reason for the failure is stated inside the sms
incoming - This is the place where the system stores incoming messages.
outgoing - This is where the message to be sent is stored as a text file.
sent - Messages which sent successfully.

To test your configuration, create a simple text file and put it in the outgoing directory. It should be sent in seconds to the mobile phone number specified in the text file.

Format of the message.

From : Foo
To : 9471XXXXXXX
This is the message, Testing...

If the number specified in the file gets the sms, you are ready to do the real thing. :-) Else you can always manually edit the /etc/smsd.conf or run dpkg-reconfigure smstools and set the values.

Now you want to be alerted via a sms when some one loged in as root to your server locally or remotely.

What I did was very simple. I created the text file in somewhere (/home/user/alert.txt).


From: Server1
To: xxxxxxxxx
A Root Login Detected.


Edit the bashrc file (expecting your default shell is bin/bash) and put the line showed below to get my job done.

# vim /root/.bashrc

And put the following at the end of the file.

cp /home/user/alert.txt /var/spool/sms/outgoing/


Done.!

What it does is each time some one loged in as root, the text file which I created will be copied to the smstool's outgoing directory and cued. smsd will send it to the phone number I specified in the text file. And a copy of the sent message will stored in the sent directory too. If you want you can clean it later (manually or by using cron with a rm /var/spool/sms/sent/alert.txt).

You have to keep a phone attached to the server and the phone should have credit to send out going messages!!:D

Hope this help.!




Wednesday, October 01, 2008

Word Children's & Elder's Day

Children are the future of the world. We must protect them and guide them to make the world a better place for every species. We must teach them good habits and develop a good mind set on them. If we don't guide them on the right track, future of this world is not going to be a very nice one.!

Elders are those who have made this world what it is now... (I'm NOT referring to the elders who made this world a living hell). They have sacrificed their lives to make ours a better one. So we have to protect our elder generation with love and care.