find DDOS attack on apache and blocking dos attack

netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n

tail -n 10000 yourweblog.log|cut -f 1 -d ' '|sort|uniq -c|sort -nr|more
Take a look at the top IP addresses. If any stand out from the others, those would be the ones to firewall.
netstat -n|grep :80|cut -c 45-|cut -f 1 -d ':'|sort|uniq -c|sort -nr|more
This will look at the currently active connections to see if there are any IPs connecting to port 80. You might need to alter the cut -c 45- as the IP address may not start at column 45. If someone was doing a UDP flood to your webserver, this would pick it up as well.
On the off chance that neither of these show any IPs that are excessively out of the norm, you would need to assume that you have a botnet attacking you and would need to look for particular patterns in the logs to see what they are doing. A common attack against wordpress sites is:
GET /index.php? HTTP/1.0
If you look through the access logs for your website, you might be able to do something like:
cut -f 2 -d '"' yourweblog.log|cut -f 2 -d ' '|sort|uniq -c|sort -nr|more
which would show you the most commonly hit URLs. You might find that they are hitting a particular script rather than loading the entire site.
cut -f 4 -d '"' yourweblog.log|sort|uniq -c|sort -nr|more
would allow you to see common UserAgents. It is possible that they are using a single UserAgent in their attack.
The trick is to find something in common with the attack traffic that doesn't exist in your normal traffic and then filter that through iptables, mod_rewrite or upstream with your webhost. If you are getting hit with Slowloris, Apache 2.2.15 now has the reqtimeout module which allows you to configure some settings to better protect against Slowloris.
more on :-

http://linoxide.com/firewall/how-to-ddos-in-linux/
http://www.opensourceforu.com/2011/04/securing-apache-part-8-dos-ddos-attacks/

Postfix ip rotation bash style

[root@mx12 postfix]# pwd
/etc/postfix

[root@mx12 postfix]# touch current.txt &&  vim  bal.cf

#!/bin/bash
ips=("5.9.240.232" "5.9.219.160" "5.9.219.166" "5.9.219.167")
length=${#ips[@]}
pos=`cat current.txt`
oldip=${ips[$pos - 1]}
if [ "$length" -eq "$pos" ] ; then
    echo "limit reached"
    pos=1
else
    echo "increment!"
    (( pos++ ))
fi

echo "$pos" > current.txt

newip=${ips[$pos - 1]}
echo "prev: $oldip"
echo "new: $newip"
echo "=============="
sed -ie "s/smtp_bind_address=$oldip/smtp_bind_address=$newip/g" /etc/postfix/main.cf > /dev/null
service MailScanner restart


[root@mx12 postfix]# cat current.txt
1

put the script bal.cf in crontab to run every  5 or 10 or 15 so...........


RAID setup in rhel 6

This document attempts to provide step-by-step procedures that can be followed to:
• Set up software RAID in RHEL 6
• Migrate existing storage from Non-RAID to Software RAID
• Resize RAID volumes
• Recover from a broken RAID
This document uses RAID-1 as an example while working with Software RAID. The procedure can however be applied to other RAID types as applicable. Please consult the mdadm(8) man page for details on exact options for various RAID types.

We used a Dell PowerEdgeTM R510 server with a Dell PERC H200 storage controller on the system for this procedure. All the storage volumes were exported directly to the OS without using any controller hardware RAID features.

Note: Ensure that all data backed up before performing any of these procedures

Setting up Software RAID in RHEL 6
Setup during Installation
The RHEL 6 installer (anaconda) has functionality that enables the OS to be installed on a software RAID partition. This section describes the steps to install RHEL 6 on a RAID-1 partition.
1. Start the RHEL 6 installer and follow the on-screen installation instructions and select the “Custom” layout for installation.
2. Create a minimum of two partitions to create RAID-1 device type and set the File System Type as software raid.
3. Create a RAID-1 device from RAID members created in Step2, select the filesystem and RAID-1 level
Here is the minimum number of software RAID partitions required for each RAID level:
• RAID 0,1,10 – 2 partitions
• RAID 4,5 – 3 partitions
• RAID 6 – 4 partitions
4. After creating all the necessary partitions (/boot, /, swap, etc.) on RAID-1 volume, proceed with the installation.
5. Once the installation is completed, the OS will boot successfully from the partitions on the RAID volume.
Note: Ensure that the boot-loader is installed on the first disk and not on the RAID device.
Installing boot-loader on the RAID device may result in failure to boot the OS after installation.

Setup after Installation
Software RAID volumes can be created on a running system post install as well. Ensure that the partition/s on which the OS is installed are not used for creating software RAID partitions, failure to do that may results in re-installing OS on the system.
Following section describes steps to create RAID-1 partition on the system.
1. Create the raid-1 md device using the mdadm command with /dev/sdj1 and /dev/sdl1. sdj1 and sdl1 are un-used partitions on this system

Note: "--metadata=0.90" is only used when you create /boot partition, because CentOS 6.x (6.4) still does not support boot from default metadata 1.2

[root@Dell-PowerEdge-R510 ~]# mdadm --create /dev/md9 --level=1 --raid-disks=2 /dev/sdj1 /dev/sdl1 --metadata=0.90
mdadm: array /dev/md9 started.
2. Create ext4 filesystem layout on the md device and add array details mdadm –-detail –scan to /etc/mdadm.conf file. Mount the device /dev/md9 on the system to use it.

[root@Dell-PowerEdge-R510 ~]# mkfs.ext4 /dev/md9

[root@Dell-PowerEdge-R510 ~]# mdadm --detail --scan >> /etc/mdadm.conf

[root@Dell-PowerEdge-R510 ~]# mount /dev/md9 /data/
3. Add a new entry in /etc/fstab file to auto mount the md raid partition, whenever system reboots.

[root@Dell-PowerEdge-R510 ~]# cat /etc/fstab
/dev/md9 /data ext4 defaults 1 1

Migration of Storage from Non-RAID to RAID Configurations
It is possible to migrate to software raid, the “/” partition without having to re-install the operating system if you installed RHEL 6 OS without software raid volumes. This section explains how migration of storage from non-raid to raid configuration can be achieved.

At a high level, here is how it can be achieved:
• Prepare the new storage volume
• Update fstab and grub configuration to boot from newly created storage volume
• Sync the data from the old partitions to the new storage volume
• Install the boot loader on new storage volume
• Add the old partition volume to the md raid-1 set


Prepare the new storage volume
1. Create the partition layout on the /dev/sdj volume similar to /dev/sdh.

[root@Dell-PowerEdge-R510 ~]# sfdisk -d /dev/sdh | sfdisk --force /dev/sdj
Device     Boot      Start         End   #sectors  Id System
/dev/sdj1  *         2048  20482047  20480000 83 Linux
/dev/sdj2      20482048  24578047    4096000 82 Linux swap / Solaris
Warning: partition 1 does not end at a cylinder boundary
Successfully wrote the new partition table

2. Set the partition id of /dev/sdj1 to Linux RAID

[root@Dell-PowerEdge-R510 ~]# sfdisk -c /dev/sdj 1 fd
Done

Create RAID-1 using mdadm utility:
1. Create the raid-1 md device using the mdadm command with /dev/sdj1. Mark the first volume as “missing”, which will be sdh volume, and it will be added later in the steps. Since sdh has OS installed, we have to add this to raid array after copying the contents from sdj to sdh drive.

Note: "--metadata=0.90" is only used when you create /boot partition, because CentOS 6.x (6.4) still does not support boot from default metadata 1.2

[root@Dell-PowerEdge-R510 ~]# mdadm --create /dev/md9 --level=1 --raid-disks=2 missing /dev/sdj1 --metadata=0.90
mdadm: array /dev/md9 started.
2. Create ext4 filesystem layout on the md device and add the array details mdadm –-detail –scan to /etc/mdadm.conf file.

[root@Dell-PowerEdge-R510 ~]# mkfs.ext4 /dev/md9

[root@Dell-PowerEdge-R510 ~]# mdadm --detail --scan >> /etc/mdadm.conf
Update fstab and grub configuration to boot from newly created storage volume:
1. Modify the /etc/fstab and /boot/grub/menu.lst with md device.

[root@Dell-PowerEdge-R510 ~]# blkid | grep –i md9
/dev/md9: UUID="016db049-6802-4369-bf66-bd48aad15395" TYPE="ext4"

[root@Dell-PowerEdge-R510 ~]# cat /etc/fstab
#UUID=38b56dff-c6d2-434f-bb48-25efb97f3a58 / 1 1 ext4 defaults
UUID=016db049-6802-4369-bf66-bd48aad15395 / 1 1 ext4 defaults

[root@Dell-PowerEdge-R510 ~]# cat /boot/grub/menu.lst
2. Add sdj to the device map entry to install the grub on the sdj device.

[root@Dell-PowerEdge-R510 ~]# cat /boot/grub/device.map
# this device map was generated by anaconda
(hd0) /dev/sdh
(hd1) /dev/sdj

Sync the data from the old partitions to the new storage volume:
1. Since we are trying to replicate the contents from currently running partition. It is recommended that you execute the following steps in run level1
2. Mount the array volume and copy the contents from sdh1 to md9.

[root@Dell-PowerEdge-R510 ~]# mount /dev/md9 //

[root@Dell-PowerEdge-R510 ~]# rsync -aqxP / //

Install the boot loader on new storage volume:
1. Install the boot loader on the sdj device.

[root@Dell-PowerEdge-R510 ~]# grub-install /dev/sdj
Installation finished. No error reported.
This is the contents of the device map /boot/grub/device.map.
Check if this is correct or not. If any of the lines is incorrect, fix it and re-run the script `grub-install'.
# this device map was generated by anaconda
(hd0) /dev/sdh
(hd1) /dev/sdj

2. Reboot the system and verify the system has booted with the md device using the mount command.

Add the old partition volume to the md raid-1 set:
1. Change the partition of sdh1 and add the sdh1 device to md9 array and allow the resync to complete from sdh1 to sdj1.

[root@Dell-PowerEdge-R510 ~]# sfdisk -c /dev/sdh 1 fd
Done

[root@Dell-PowerEdge-R510 ~]# mdadm --add /dev/md9 /dev/sdh1
mdadm: added /dev/sdh1

[root@Dell-PowerEdge-R510 ~]# watch cat /proc/mdstat
2. Reboot the system to verify the md migration completed successfully.
3. Run the cat /proc /mdstat command to check the status of the running array.

Resizing an existing RAID Partition
The Linux software-RAID solution allows us to resize (increase or decrease) the RAID partition size. Following steps explain how to increase the size of existing software RAID partition (data and OS partitions).

Here is what is required:
• Prepare partitions of the new size desired
• Replace both RAID members with newly created partitions by breaking existing RAID
• Resize the RAID array
• If dealing with OS partitions, Prepare the new RAID volume to be bootable

Prepare partitions of the new size desired:
1. Initially create the md9 raid-1 level with a size approximately 100 GB comprised of both the sdh1 and sdj1 volumes. The md-0 array RAID set will be increased to approximately 200 GB by using the sdl1 and sdm1 volumes.
2. Create the new RAID partition of increased size on sdl and sdm. In this example, we created a new RAID partition of approximately 200GB in size on the sdl volume.

[root@Dell-PowerEdge-R510 ~]# sfdisk -c /dev/sdl 1 fd
Done

[root@Dell-PowerEdge-R510 ~]# fdisk -l /dev/sdl
Disk /dev/sdl: 1000.2 GB, 1000204886016 bytes
255 heads, 63 sectors/track, 121601 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x4c0a9054
Device     Boot Start     End      Blocks       Id System
/dev/sdl1           1      25000  200812468+ fd Linux raid autodetect

Replace both RAID members with newly created partitions by breaking existing RAID:
1. Set the sdj1 volume to faulty and remove the volume from the RAID set.

[root@Dell-PowerEdge-R510 ~]# mdadm /dev/md9 –-fail /dev/sdj1 –remove /dev/sdj1
mdadm: set /dev/sdj1 faulty in /dev/md9
mdadm: hot removed /dev/sdj1 from /dev/md9

2. Add the new partition to the RAID set and allow the resync to complete on the new partition added to the RAID set. Run cat /proc/mdstat to show the status of resynchronization.
[root@Dell-PowerEdge-R510 ~]# mdadm --add /dev/md9 /dev/sdl1
mdadm: added /dev/sdl1
[root@Dell-PowerEdge-R510 ~]# cat /proc/mdstat
Personalities : [RAID-1]
md9 : active RAID-1 sdl1[2] sdh1[0]
102399928 blocks super 1.0 [2/2] [UU]
bitmap: 1/1 pages [4KB], 65536KB chunk
unused devices:

3. Repeat the above steps adding the /dev/sdm1 partition. Remove the /dev/sdh1 partition from the RAID set. Allow the resynchronization to complete on the /dev/sdm1 partition.
[root@Dell-PowerEdge-R510 ~]# cat /proc/mdstat
Personalities : [RAID-1]
md9 : active RAID-1 sdm1[3] sdl1[2]
102399928 blocks super 1.0 [2/1] [_U]
[>....................]
recovery =
finish=20.7min speed=81894K/sec
bitmap: 1/1 pages [4KB], 65536KB chunk
0.3%
(409472/102399928)
unused devices:

Resize the RAID array:
1. Set the /dev/md9 partition size to use the new volume partition size and allow the resync to complete.
[root@Dell-PowerEdge-R510 ~]# mdadm --grow /dev/md9 --size=max
mdadm: Cann[ot set device size for /dev/md9: Device or resource busy
Bitmap must be removed before size can be changed
[root@Dell-PowerEdge-R510 ~]# mdadm --grow /dev/md9 --bitmap none
[root@Dell-PowerEdge-R510 ~]# mdadm --grow /dev/md9 --size=max
mdadm: component size of /dev/md9 has been set to 200812396K

2. Resize the file system of the /dev/md9 partition to increase the file system size. Now the df –h command shows the increased md RAID set.
[root@Dell-PowerEdge-R510 ~]# resize2fs /dev/md9
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/md9 is mounted on /; on-line resizing required old desc_blocks = 7, new_desc_blocks = 12
Performing an on-line resize of /dev/md9 to 50203099 (4k) blocks.
The filesystem on /dev/md9 is now 50203099 blocks long.
[root@Dell-PowerEdge-R510 ~]# df -h
Filesystem  Size Used Avail Use% Mounted on
/dev/md9 189G 1.4G 178G     1% /
tmpfs       1.9G     0 1.9G       0% /dev/shm

If dealing with OS partitions, prepare the new RAID volume to be bootable:

Note: Following steps are required to boot the Linux system, if you are replacing the drive from RAID array which has boot loader and file system already installed.
Before resizing the RAID partition, ensure that the md-raid set has two active drives, sdh1 and sdj1 and that grub is installed in /dev/sdh. After the resizing operation, the md-raid set has been replaced with sdl1 and sdm1 volumes and boot loader has been installed on sdl volume.

1. Add the sdl and sdm entries in device.map file.
[root@Dell-PowerEdge-R510 ~]# cat /boot/grub/device.map
# this device map was generated by anaconda
(hd0) /dev/sdh
(hd1) /dev/sdj
(hd2) /dev/sdl
(hd3) /dev/sdm

2. Install the grub boot loader on the /dev/sdl volume and then remove the older drives from the system.
[root@Dell-PowerEdge-R510 ~]# grub-install /dev/sdl
Installation finished. No error reported.
This is the contents of the device map /boot/grub/device.map. Check if this is correct or not. If any of the lines is incorrect, fix it and re-run the script `grub-install'.
# this device map was generated by anaconda
(hd0) /dev/sdh
(hd1) /dev/sdj
(hd2) /dev/sdl
(hd3) /dev/sdm

3. Reboot the system and enter the storage controller BIOS configuration.
4. Change the boot drive to new drive where the boot loader is now installed.
5. Save the configuration and restart the system to boot from new RAID set partition.

Recovery from a Broken RAID
In case of RAID failures on a system running Linux Software RAID (md) solution (for example, media failure or disk driver failure), it is possible to recover the system by any of these methods
• replacing the faulty disk
• adding a new disk
• using the spare disk

In Linux software raid, recovery is achieved through “failover” mechanisms. Failover mechanism ensures data protection by providing additional drives (spares) and can be automatic or manual.

Automatic Failover
Linux md-raid solution has an intelligent monitor mechanism to detect hardware failure in RAID arrays.
If any disk in the RAID array fails, the monitors sets the failed drive to faulty and starts using one of the available spare drives for regeneration. To check the status of the RAID array, look at  /proc/mdstat file.
To replicate a raid failure to check how automatic failover scenario works, follow the steps.
1. Create a RAID-1 setup with three raid partition. Minimum two raid partitions are required to create RAID-1 device and third raid partition is used as a spare disk, will be used as a replacement if one of the active RAID partition fails.
2. Simply pull out one of the disks which are active in the raid array and check the status of the array using the cat /proc/mdstat command, will show the removed drive as faulty and use the spare drive as replacement for date re-generation.

Manual Failover
Faulty drives in the raid array can be replaced manually. Following steps discuss how to manually replace the faulty drive from the RAID-1 array:
1. Raid-1 setup is created with sdh1 and sdj1 partitions. Set the sdh1 drive to faulty.
[root@DELL-PowerEdge-R510 ~]# mdadm -f /dev/md9 /dev/sdh1
mdadm: set /dev/sdh1 faulty in /dev/md9
2. Remove the faulty drive sdh1 from the array.
[root@DELL-PowerEdge-R510 ~]# mdadm -r /dev/md9 /dev/sdh1
mdadm: hot removed /dev/sdh1 from /dev/md9
3. Replace the faulty drive with adding a new one to the array.
[root@DELL-PowerEdge-R510 ~]# mdadm --add /dev/md9 /dev/sdl1
Device Added
4. Look at status of the RAID array by executing the cat /proc/mdstat command, showing /dev/sdl1 added to the RAID array. Also check that the resynchronization is complete.
[root@DELL-PowerEdge-R510 ~]# cat /proc/mdstat
Personalities : [RAID-1]
md9 : active RAID-1 sdl1[2] sdj1[0]
40958908 blocks super 1.1 [2/2] [UU]
bitmap: 1/1 pages [4KB], 65536KB chunk

Adding a Spare Disk to the Array
Spare disks provide additional protection to a raid configuration. If a disk fails in a raid array, the spare disk automatically replaces the failed drive; also the raid can be rebuilt automatically in the background. Spare drives can be added to the RAID array during the time of creation of the array or later.

Adding spare disk during raid array creation
[root@Dell-PowerEdge-R510 ~]# mdadm --create /dev/md9 --level=1 --raid-disks=2 /dev/sdj1 /dev/sdl1 --metadata=0.90 –-spare-devices=1 /dev/sdm1
mdadm: array /dev/md9 started.

Adding spare disk to an existing array.
[root@DELL-PowerEdge-R510 ~]# mdadm --add /dev/md9 /dev/sdl1
mdadm: added /dev/sdl1
[root@DELL-Poweredge-R510 ~]# cat /proc/mdstat
Personalities : [RAID-1]
md8 : active RAID-1 sdl1[2](S) sdh1[0] sdj1[1] sdl

References
• http://docs.redhat.com/docs/en-US/Red_Hat_Enterprise_Linux/index.html
• http://www.spinics.net/lists/raid/
• https://raid.wiki.kernel.org/index.php/Linux_Raid
-------------------------------------------------------------------------------------------

Stopping resync-a:
echo "idle" > /sys/block/md0/md/sync_action

Rename:

Last time I did this it was from a later distribution --- either a RHEL-4 or FC-5 rescue CD, I cannot recall which at this point.  I did it precisely for the reason you give, to move an existing /dev/md0 to another RHEL-4 box.  But it wasn't hard, using mdadm.  Knowing the devices belonging to the array (sda5 and sdb5 in this case), I just did:

# mdadm --stop /dev/md0
# mdadm -A /dev/md6 -m0 --update=super-minor /dev/sda5 /dev/sdb5

which stops the array as /dev/md0 and then reassembles it as /dev/md6.
The reassembly looks for devices which have an existing minor number of 0, not 6 (-m0), and then updates the minors in the superblocks to the new number.

I believe the same options are present in taroon's mdadm.

mdadm ---create --level=1 --raid-devices=2 /dev/sdh1 /dev/sdg1

(Repair filesystem) Writing not allowed. Solution:
mount -w -o remount /

Linux console scroll : Shift + PgDn ili Shift + PgUp

Creating Boot RAID1:
[root@kancelarija yum]# mdadm --create /dev/md9 --level=1 --raid-disks=2 /dev/sdd1 /dev/sde1 --metadata=0.90

Creating RAID10 far,2:
[root@kancelarija yum]# mdadm --create /dev/md9 --level=10 --layout=f2 --raid-disks=2 /dev/sdd9 /dev/sde9

6.3. Activate RAID Devices
The installation media doesn't include a default mdadm.conf file, so it must be created. An easy method is...

mdadm --examine --scan > /etc/mdadm.conf

Next, activate all discovered RAID devices with...

mdadm --assemble --scan

cat /proc/sys/dev/raid/speed_limit_max
200000
cat /proc/sys/dev/raid/speed_limit_min
1000

50MB/s:
echo 50000 >/proc/sys/dev/raid/speed_limit_min

vsftpd ftp file server setup

yum install vsftpd ftp -y

chkconfig vsftpd on
sestatus              # to check selinux status
[root@web vsftpd]#  getsebool -a | grep ftp  
allow_ftpd_anon_write --> off
allow_ftpd_full_access --> off
allow_ftpd_use_cifs --> off
allow_ftpd_use_nfs --> off
ftp_home_dir --> off
ftpd_connect_db --> off
ftpd_use_passive_mode --> off
httpd_enable_ftp_server --> off
tftp_anon_write --> off

you need to enable ftp_home_dir => on

# setsebool -P ftp_home_dir=1   

vim /etc/vsftpd/vsftpd.conf

anonymous_enable=NO
chroot_local_user=YES
chroot_list_enable=NO
chroot_list_file=/etc/vsftpd/chroot_list 
 
listen=YES
#listen_ipv6=YES  this will disable ipv6 listening
#current version doesnt support the vsftp listening on ipv4 and ipv6 simultaneously.
 
And lets configure vsftpd to be able to chroot(commonly referred to as 
jailing or jail) users to their home directories for security and 
privacy:
 
 
  • Also, lock down vsftpd to a predictable port range. (By default, vsftpd runs in passive mode and will choose a port between 1024-65535). Add the following:
pasv_min_port=3000
pasv_max_port=3050

  • Create the chroot_list file so you do not get an error when restarting:
sudo touch /etc/vsftpd/chroot_list

For RHEL6

iptables -I INPUT -m tcp -p tcp --dport 21 -j ACCEPT
iptables -I INPUT 1 -p tcp --dport 3000:3050 -j ACCEPT
 
For RHEL5 
 
iptables -I RH-Firewall-1-INPUT 1 -p tcp --dport 3000:3050 -j ACCEPT
iptables -I RH-Firewall-1-INPUT -m tcp -p tcp --dport 21 -j ACCEPT 


Setup user for ftp with no login

# useradd test
    # passwd test
 
usermod -s /sbin/nologin test
 
 
Making the softlink work !!!!!!
 
we create a symbolic link inside user’s folder to point to the music folder:
 
ln -s /tmp/harddisk/music /tmp/harddisk/ftp_pvt/user/music 


It seems like everything is okay. As long as the permissions for the folder music grants access for the right users

Instead of copying whole content use mount

  mkdir /tmp/harddisk/ftp_pvt/user/music



 
 
Mount the folder you want user to access using the bind option  
 
  mount --bind /tmp/harddisk/music /tmp/harddisk/ftp_pvt/user/music
 

 Consider "/realdata/dir   /virtual/dir  none default,bind 0 0" in 
/etc/fstab to make the "mount -B" available on reboot as well (perhaps 
less obscure than "/etc/rc.local"). Note: don't bother looking into hard
 links, since they will not link directories, and will not file systems.

postfix change port 25

Bydefault the SMTP protocol runs at port no. 25.
You can change the port no. to any port no. other than below 2048.
0 to 2048 are IANA authorized known port no.but we have some other port no. also which are bydefault used by manyapplication for. eg. port no. 8080 for tomcat.
To know more about port number click this wikipedia URL .
Here we are using Postfix as our MTA.
To change the smtp port no. in postfix,edit the master.cf file and change the given below value
vi /etc/postfix/master.cf
smtp inet n – n – – smtpd
change smtp word to your desired port no. Here I am changing it to 9267
9267 inet n – n – – smtpd
Now restart the postfix
/etc/init.d/postfix restart
Check the port no. status with netstat command
[root@localhost postfix]# netstat -tanp|grep 9267
tcp 0 0 0.0.0.0:9267 0.0.0.0:* LISTEN 9788/master
[root@localhost postfix]#

postfix part 7 - Setting up Mailwatch for Mailscanner/postfix



MailWatch for MailScanner is a open source or free web-based front-end to Mailscanner that use to displays the inbound/outbound mail queue, spam, viruses and blocked content on each page header. MailWatch has been written in PHP, MySQL and JpGraph. By using Mailwatch, it allows you to delete, release or run sa-learn across any messages that was quarantined. This post describes how to setup MailWatch on CentOS 6.3 and assumed that you already installed MailScanner and postfix.

1. Install required php and mysql :
[root@mx1 ~]# yum install httpdphpmysql-server mysqlphp-gdphp-mysql -y

2. Modify the php.ini :
[root@mx1 ~]# vim /etc/php.ini

PHP should have the following set in php.ini
short_open_tag = On
safe_mode = Off
register_globals = Off
magic_quotes_gpc = On
magic_quotes_runtime = Off
session.auto_start = 0

3. Download the latest version from mailwatch.sourceforge.net :
[root@mx1 ~]# wget http://sourceforge.net/projects/mailwatch/files/mailwatch/1.1.5.1/mailwatch-1.1.5.1.tar.gz

4. Extract downloaded mailwatchpackage :
[root@mx1 ~]# tar xzvf mailwatch-1.1.5.1.tar.gz

5. Start mysqlservice :
[root@mx1 ~]# servicemysqld start

6. Enter extracted mailwatch-x.xdirectory :
[root@mx1 mailwatch-1.1.5.1]# ls
CHANGELOG   fix_quarantine_permissions  LICENSEmailscanner               Remote_DB.txt  upgrade.php  USER_FILTERS
create.sql  INSTALLluserMailScanner_perl_scripts  tools          UPGRADING
[root@mx1 mailwatch-1.1.5.1]# mysql -p <create.sql

7. Create the database and import create.sql. The following commands below should be run as the ‘root’ :
[root@mx1 mailwatch-1.1.5.1]# mysql -p <create.sql
Enter password:

8. Login to mysql as a root :
[root@mx1 mailwatch-1.1.5.1]# mysql -u root -p
Enter password:


9. Proceed to create a MySQL user and then set password and configure MailScanner for SQL logging :
mysql> GRANT ALL ON mailscanner.* TO mailwatch@localhost IDENTIFIED BY 'password';
Query OK, 0 rows affected (0.00 sec)

mysql> GRANT FILE ON *.* TO mailwatch@localhost IDENTIFIED BY 'password';
Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mailscanner        |
| mysql              |
| test               |
+--------------------+
4 rows in set (0.00 sec)


10. Edit MailWatch.pm and change the database values accordingly :
a. Enter to the perl_scripts’sfolder :
[root@mx1 mailwatch-1.1.5.1]# cdMailScanner_perl_scripts
[root@mx1 MailScanner_perl_scripts]# pwd
/root/mailwatch-1.1.5.1/MailScanner_perl_scripts
b. Edit MailWatch.pm :
[root@mx1 MailScanner_perl_scripts]# vim MailWatch.pm
c.Modify this as necessary for your configuration
my($db_name) = 'mailscanner';
my($db_host) = 'localhost';
my($db_user) = 'mailwatch';
my($db_pass) = 'password';
11. Move MailWatch.pm to /usr/lib/MailScanner/MailScanner/CustomFunctions :
[root@mx1 MailScanner_perl_scripts]# cp MailWatch.pm /usr/lib/MailScanner/MailScanner/CustomFunctions


12. Create a MailWatch web user. This user will able to monitor the mailscanner through MailWatch web interface.
[root@mx1 ~]# mysqlmailscanner -u mailwatch -p
Enter password:

mysql>INSERT INTO users SET username = 'admin', password = md5('password'), fullname = 'MAilwatch Administrator', type ='A';
 
Query OK, 1 row affected (0.00 sec)

13. Install &Configure MailWatch :
a. Enter to mailwatch directory :
[root@mx1 ~]# cd mailwatch-1.1.5.1
b. Move mailscanner folder for web root folder. For apache, default root folder is /var/www/html :
[root@mx1 mailwatch-1.1.5.1]# mvmailscanner /var/www/html/
c. Enter to mailscanner folder in web root directory :
[root@mx1 ~]# cd /var/www/html/mailscanner

d. Verify and check the permissions of /var/www/html/mailscanner/images and /var/www/html/images/cache. The permissions and ownerships should be as below :
[root@mx1 mailscanner]# chownroot:apache images
[root@mx1 mailscanner]# chmodug+rwx images
[root@mx1 mailscanner]# chownroot:apache images/cache
[root@mx1 mailscanner]# chmodug+rwx images/cache
14. Create conf.php by copying conf.php.example. Edit the values to any details as you created earlier :
a. Copy the conf.php :
[root@mx1 mailscanner]# cpconf.php.exampleconf.php
b. Open conf.php :
[root@mx1 mailscanner]# vimconf.php
c. Edit the details as below :
define('DB_TYPE', 'mysql');
define('DB_USER', 'mailwatch');
define('DB_PASS', 'password');
define('DB_HOST', 'localhost');
define('DB_NAME', 'mailscanner');
15. Before setup the mailscanner, make sure it’s Stopped. Run 3 to 4 times to ensure the mailscanner completely stop. The status will FAILED if mailscanner completely stop.
[root@mx1 ~]# serviceMailScanner stop
Shutting down MailScanner daemons:
MailScanner:                                      [  OK  ]
incoming postfix:                                 [  OK  ]
outgoing postfix:                                 [  OK  ]
[root@mx1 ~]# serviceMailScanner stop
Shutting down MailScanner daemons:
MailScanner:                                      [  OK  ]
incoming postfix:                                 [  OK  ]
outgoing postfix:                                 [  OK  ]
[root@mx1 ~]# serviceMailScanner stop
Shutting down MailScanner daemons:
MailScanner:                                      [  OK  ]
incoming postfix:                                 [  OK  ]
outgoing postfix:                                 [  OK  ]
[root@mx1 ~]# serviceMailScanner stop
Shutting down MailScanner daemons:
MailScanner:                                      [  OK  ]
incoming postfix:                                 [  OK  ]
outgoing postfix:                                 [  OK  ]
[root@mx1 ~]# serviceMailScanner stop
Shutting down MailScanner daemons:
MailScanner:                                      [FAILED]
incoming postfix:                                 [  OK  ]
outgoing postfix:                                 [  OK  ]
16. Edit /etc/MailScanner/MailScanner.conf
[root@mx1 ~]# vim /etc/MailScanner/MailScanner.conf
Make sure that the following options are set:
 Always Looked Up Last = &MailWatchLogging
 Detailed Spam Report = yes
 Quarantine Whole Message = yes
 Quarantine Whole Message As Queue Files = no
 Include Scores InSpamAssassin Report = yes
 Quarantine User = root
 Quarantine Group = apache (this should be the same group as your web server)
 Quarantine Permissions = 0660
17. Start MailScanner :
[root@mx1 ~]# serviceMailScanner start
Starting MailScanner daemons:
incoming postfix:                                 [  OK  ]
outgoing postfix:                                 [  OK  ]
MailScanner:                                      [  OK  ]
18. Restart httpdservice :
[root@mx1 ~]# servicehttpd restart
19. Make sure all the related service such as httpd, mysql and MailScanner configured to start at boot :
[root@mx1 ~]# chkconfigmysqld on
[root@mx1 ~]# chkconfighttpd on
[root@mx1 ~]# chkconfigMailScanner on
[root@mx1 ~]# chkconfigspamassassin on
[root@mx1 ~]# chkconfig postfix on
20. Check the mail log.
[root@mx1 ~]# tail -f /var/log/maillog
Mar  5 22:14:42 mx1 MailScanner[1718]: Using locktype = flock
Mar  5 22:14:43 mx1 MailScanner[1697]: Requeue: C297BA1C3F.AA850 to E7373A1DD6
Mar  5 22:14:43 mx1 postfix/qmgr[1687]: E7373A1DD6: from=<root@mx1.ehowstuff.local>, size=609, nrcpt=1 (queue active)
Mar  5 22:14:43 mx1 MailScanner[1697]: Uninfected: Delivered 1 messages
Mar  5 22:14:43 mx1 MailScanner[1697]: Deleted 1 messages from processing-database
Mar  5 22:14:43 mx1 MailScanner[1697]: Logging message C297BA1C3F.AA850 to SQL
Mar  5 22:14:43 mx1 postfix/local[1768]: E7373A1DD6: to=<root@mx1.ehowstuff.local>, orig_to=, relay=local, delay=166894, delays=166894/0.13/0/0.02, dsn=2.0.0, status=sent (delivered to mailbox)
Mar  5 22:14:43 mx1 postfix/qmgr[1687]: E7373A1DD6: removed
Mar  5 22:56:25 mx1 postfix/postfix-script[1869]: fatal: the Postfix mail system is already running
Mar  5 23:01:08 mx1 update.bad.phishing.sites: Delaying cron job up to 600 seconds

21. Login to mailwatch web interface :



5.9.240.232/mailscanner
 
                                 Solution   if you get error !!!  :-                   
Please verify read permissions on /var/spool/postfix/hold and /var/spool/postfix/incoming
Do-check permissions

sudo chown postfix:apache /var/spool/postfix/incoming
sudo chown postfix:apache /var/spool/postfix/hold
sudo chmod 740 /var/spool/postfix/incoming
sudo chmod 740 /var/spool/postfix/hold


cd /var/spool/MailScanner/

chown -R postfix.clam *
chmod -R 750 *

# ls -ltrh

total 12K
drwxr-x---  2 postfix  clam   4.0K Jul 16 06:11 spamassassin
drwxr-x--- 14 postfix  apache 4.0K Aug  5 01:58 quarantine
drwxr-x---  9 postfix  clam   4.0K Aug  5 01:59 incoming


For ubuntu 14.04LTS
# ls -l /var/spool/MailScanner
total 16
drwxrwx---  2 postfix clamav   4096 Apr  6 16:24 archive
drwxrwx--- 29 postfix clamav   4096 Jun  1 12:32 incoming
drwxr-x---  4 postfix www-data 4096 May 28 16:33 quarantine
drwxr-x---  2 postfix clamav   4096 Apr 12 01:39 spamassassin



http://www.netmium.com.au/category/how-tos/