#!/bin/bash

##########################################################################
# A lancer en sudo sur la station Ubuntu à auditer: 			 #
# 	sudo ./audit_linux.sh						 #
# Ne pas tenir compte des erreurs affichees                              #
# Les infos seront envoyees à la base mysql d'Antigua                    #
#									 #
# ATTENTION: le Hostname de la station Ubuntu auditee doit etre de la    #
# forme "AAAAAAA[Numero de station]" ou bien (si elle est deja           #
# enregistree) identique à celui de la base mysql (attention à la casse) # 
# afin que la station soit correctement identifiee sur Antigua sinon une #
# nouvelle station sera creee dans la base de donnees                    #
##########################################################################


if [ $(id -u) != "0" ]; then
  echo "You must use root privs to run this script."
  exit 0
fi

HOSTNAME=`hostname`
DATE="`date +%d/%m/%Y``date +%H:%M:%S`"
ReportFile=$HOSTNAME.txt
audit_date="`date +%Y%m%d``date +%H%M%S`"


#Network detection
pcieth=`lspci -vm | grep -A2 "Ethernet controller"; lspci -vm | grep -A2 "Network controller"`
for i in `ifconfig -a | grep eth | cut -d" " -f1`
do 
  name=`echo "$pcieth" | grep -w "Device:" | cut -d: -f2 | cut -c2-`
  manufacturer=`echo "$pcieth" | grep -w "Vendor:" | cut -d: -f2 | cut -c2-`
  ip=`ifconfig $i | grep -w inet | cut -d":" -f2 | cut -d" " -f1`
  mac=`ifconfig $i | grep -w HWaddr | cut -d" " -f11`
  type="Network Adapter"
done

echo "network^^^$mac^^^$name^^^$type^^^$manufacturer^^^" >> $ReportFile
# 
# System
sys_model=`hal-get-property --udi /org/freedesktop/Hal/devices/computer --key smbios.system.product`
chassis_type=`hal-get-property --udi /org/freedesktop/Hal/devices/computer --key smbios.chassis.type`
sys_serial=`hal-get-property --udi /org/freedesktop/Hal/devices/computer --key smbios.system.serial`
sys_manufacturer=`hal-get-property --udi /org/freedesktop/Hal/devices/computer --key smbios.system.manufacturer`

#Operating System
name=`uname -s`
version=`uname -r`

if [ "$name" = "Linux" ]
        then if test -f /etc/redhat-release; then
                distribution="RedHat"
                release=`cat /etc/redhat-release`
            elif test -f /etc/redhat-version; then
                distribution="RedHat"
                release=`cat /etc/redhat-version`
            elif test -f /etc/fedora-release; then
                distribution="Fedora"
                release=`cat /etc/fedora-release`
            elif test -f /etc/mandrake-release; then
                distribution="Mandrake"
                release=`cat /etc/mandrake-release`
            elif test -f /etc/SuSE-release; then
                distribution="Novell SuSE"
                release=`cat /etc/SuSE-release`
            elif test -f /etc/issue; then
                distribution="Ubuntu"
                release=`cat /etc/issue | cut -d'\\' -f1`
            elif test -f /etc/debian_version; then
                distribution="Debian"
                release=`cat /etc/debian_version`
            elif test -f /etc/debian-version; then
                distribution="Debian"
                release=`cat /etc/debian-version`
            elif test -f /etc/arch-release; then
                distribution="Arch"
                release=`cat /etc/arch-release`
            elif test -f /etc/gentoo-release; then
                distribution="Gentoo"
                release=`cat /etc/gentoo-release`
            elif test -f /etc/slackware-release; then
                distribution="Slackware"
                release=`cat /etc/slackware-release`
            elif test -f /etc/slackware-version; then
                distribution="Slackware"
                release=`cat /etc/slackware-version`
            elif test -f /etc/yellowdog-release; then
                distribution="Yellow dog"
                release=`cat /etc/yellowdog-release`
            else distribution="unknown"
                release="unknown"
            fi
fi

mount_points=`cat /etc/mtab | cut -d ' ' -f1,2`
for i in `echo $mount_points | cut -d" " -f2`
do
if [ "$i" = "/" ]
then
mount_point=`echo $mount_points | cut -d" " -f1`
fi
done
mount_point=`grep ' / ' /etc/mtab |cut -d " " -f1`

if [ "$release" = "Ubuntu 6.06 LTS \n \l" ]
then
release="Ubuntu 6.06"
fi

# Total memory size
RAMsizekb=`cat /proc/meminfo | grep MemTotal |cut -d: -f2 | cut -dk -f1`
RAMsizekb=`expr $RAMsizekb / 1`
RAMsize=`expr $RAMsizekb / 1024`

echo "system^^^$ip^^^ ^^^$sys_model^^^$chassis_type^^^$sys_serial^^^$sys_manufacturer^^^Linux Workstation^^^$distribution ($release)^^^ ^^^$version^^^ ^^^$RAMsize^^^" >> $ReportFile


# Processor
cpu_device_id=`cat /proc/cpuinfo | grep "processor" | cut -d: -f2 | cut -c2-`
for i in $cpu_device_id; do
  count=` expr $i + 1`
  cpu_name=`cat /proc/cpuinfo | grep "model name" | cut -d: -f2 | cut -c2- | tr "\n" "^" | cut -d^ -f$count`
  cpu_freq=`cat /proc/cpuinfo | grep "cpu MHz" | cut -d: -f2 | cut -c2- | cut -d. -f1 | tr "\n" "^" | cut -d^ -f$count`
  cpu_manufacturer=`cat /proc/cpuinfo | grep "vendor_id" | cut -d: -f2 | cut -c2- | tr "\n" "^" | cut -d^ -f$count`
  echo "processor^^^$cpu_freq^^^CPU$i^^^ ^^^cpu_manufacturer^^^$cpu_name^^^ ^^^" >> $ReportFile
done

# Memory
mem_device_id=`lshw | sed -n -e /bank/,/clock/p | grep "bank" | cut -d: -f2 | cut -c1-`
if [ -n "$mem_device_id" ]
then
  for i in $mem_device_id; do
    count=` expr $i + 1`
    mem_slot=`lshw | sed -n -e /bank/,/clock/p | grep "slot" | cut -d: -f2 | cut -c2- | tr "\n" "^" | cut -d^ -f$count`
    mem_size=`lshw | sed -n -e /bank/,/clock/p | grep "size" | cut -d: -f2 | cut -c2- | cut -dM -f1 | tr "\n" "^" | cut -d^ -f$count`
    mem_clock=`lshw | sed -n -e /bank/,/clock/p | grep "clock" | cut -d: -f2 | cut -c2- | cut -dM -f1 | tr "\n" "^" | cut -d^ -f$count`
    if [ -n "$mem_size" ]; then
      echo "memory^^^$mem_slot^^^$mem_size^^^$mem_clock^^^" >> $ReportFile
    fi
  done
fi

# Multimedia
pcilist=`lspci -vm`
perif=`lspci -vm | grep "[[:digit:]]:[[:digit:]]" | cut -f2`
for i in $perif; do
  type=`echo "$pcilist" | grep -w $i -A 4 | grep -w "Class:" | cut -d":" -f2 | cut -f2`
  name=`echo "$pcilist" | grep -w $i -A 4 | grep -v "[[:digit:]]:[[:digit:]]" | grep -w "Device:" | cut -d":" -f2 | cut -f2`
  name=`echo ${name//\'/-}`
  manufacturer=`echo "$pcilist" | grep -w $i -A 4 | grep -w "Vendor:" | cut -d":" -f2 | cut -f2`
  device_id=`echo $i`
  
  # Graphic Card
  if [ "$type" = "VGA compatible controller" ]
  then
    sss=`echo "video^^^ ^^^$manufacturer - $name^^^0^^^0^^^0^^^0^^^0^^^VGA Linux $device_id^^^"`
    echo "$sss" >> $ReportFile
  fi

  #Sound Card
  if [ "$type" = "Multimedia audio controller" ]
  then
    echo "sound^^^$manufacturer - $name^^^" >> $ReportFile
  fi
done

# Software
packages="apt azureus bash build-essential cdparanoia cdrdao cdrecord cpp cron cupsys cvs dbus dhcp3-client diff dpkg epiphany-browser esound evolution firefox flashplugin-nonfree foomatic-db g++ gaim gcc gdm gedit gimp gnome-about gnucash gnumeric gtk+ httpd inkscape iptables k3b kdebase koffice libgnome2-0 linux-image-386 metacity mozilla-browser mysql-admin mysql-query-browser mysql-server-4.1 nautilus openoffice.org openssh-client openssh-server perl php4 php5 postfix postgresql python python2.4 rdesktop rhythmbox samba-common sendmail smbclient subversion sun-j2re1.5 swf-player synaptic thunderbird tsclient udev vim vlc vnc-common webmin xfce xmms xserver-xorg"
for name in $packages; do
  version=`dpkg --list | grep "  $name " |tail -n1|awk '{print $3}' 2> /dev/null`
  if [ "$version" ] 
  then
    echo "software^^^$name^^^$version^^^ ^^^ ^^^" >> $ReportFile
  fi
done

# Floppy
product=`lshal -s | grep storage | grep -i floppy`
if [ -n "$product" ]
then
  echo "floppy^^^Lecteur de disquettes^^^" >> $ReportFile
fi

# Hard Disks
devices=`lshal -s`
for i in $devices
do
  device=`echo $i | grep storage`
  storage=`echo $device | cut -d"_" -f1`
  if [ "$storage" = "storage" ]
  then
    udi="/org/freedesktop/Hal/devices/$device"
    category=`hal-get-property --udi $udi --key info.category`
    vendor=`hal-get-property --udi $udi --key info.vendor`
    product=`hal-get-property --udi $udi --key info.product`
    bus=`hal-get-property --udi $udi --key storage.bus`
    mount=`hal-get-property --udi $udi --key block.device`
    product_dvd=`echo $product | grep -i DVD`
    product_cd=`echo $product | grep -i CD`
    if [ "$category" = "storage" ]
    then
      if [ "$product_dvd" = "$product" -o "$product_cd" = "$product" ]
      then
        # Item is a DVD or CD drive
        echo "optical^^^$product^^^$mount^^^^^^" >> $ReportFile

      else
        # Item is a hard drive
        mount_end=`echo $mount | cut -d"/" -f3`
        # size=`dmesg | grep -w $mount_end: | grep MB | cut -d"(" -f2 | cut -d" " -f1 | uniq`
        size=`fdisk -l $mount | grep Disque | cut -d" " -f3 | cut -d"." -f1`
        size_type=`fdisk -l $mount | grep Disque | cut -d" " -f4`
        if [ "$size_type" = "Go," ]
        then
          let "size = $size * 1024"
        fi
        echo "harddrive^^^$mount^^^$bus^^^$vendor^^^$product^^^$count^^^$size^^^^^^" >> $ReportFile
        count=0
      fi
    else
      if [ "$bus" = "usb" ]
      then
        echo "usb^^^$category^^^$product^^^$vendor^^^^^^" >> $ReportFile
      fi
    fi
  fi
done

#Partitions
for j in `df -l -T -x tmpfs |awk '{print $1}'`
do
  part_capt=`df -l -T -x tmpfs | grep $j | awk '{print $7}'`
  if [ "$part_capt" = "/" ]; then
    part_boot="True"
  else
    part_boot="False"
  fi
  part_perc=`df -l -T -x tmpfs | grep $j |awk '{print $6}'`
  part_name=`df -l -T -x tmpfs | grep $j |awk '{print $1}'`
  part_form=`df -l -T -x tmpfs | grep $j |awk '{print $2}'`
  part_size=`df -l -T -x tmpfs | grep $j |awk '{print $3}'`
  part_size=`expr $part_size / 1`
  part_size=`expr $part_size / 1024`
  part_aval=`df -l -T -x tmpfs | grep $j |awk '{print $5}'`
  part_aval=`expr $part_aval / 1`
  part_aval=`expr $part_aval / 1024`
  if [ "$part_form" != "Type" ]; then
    echo "partition^^^$part_boot^^^$part_boot^^^ ^^^ ^^^$part_perc^^^$part_boot^^^$part_name^^^$part_form^^^$part_aval^^^$part_size^^^$part_name^^^" >> $ReportFile
  fi
  # Missing - DeviceID
  #         - Disk Index
  #         - File System
done

# Users
users=`cat /etc/passwd | cut -d":" -f1`
for i in $users
do
  username=`cat /etc/passwd | grep $i | cut -d":" -f5 | tr -s ',' | tr ',' ' '`
  user_id=`cat /etc/passwd | grep $i | cut -d":" -f3`
#  echo "l_user^^^^^^^^^$username^^^$i^^^^^^^^^^^^$user_id^^^" >> $ReportFile
  printf "l_user^^^^^^^^^%s^^^%s^^^^^^^^^^^^%s^^^\n" $username $i $user_id >> $ReportFile
done

# The end - submit to Open-AudIT
audit_result=`cat $ReportFile`
uuid=`echo ${HOSTNAME:(-7)}`
if [ -z "$uuid" ]; then
  uuid=$HOSTNAME
fi
wget --post-data="submit=submit&add=$audit_result&uuid=$uuid&systemname=$HOSTNAME" http://antigua/openmairie_parc/wmi/traitementwmi.php
rm "$ReportFile"
rm "traitementwmi.php"

