summaryrefslogtreecommitdiffstats
path: root/debian/samhain.init
blob: 858d5698840a1bb32669bfc95085821975041185 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#! /bin/sh
# 
# Init.d file for Samhain, based on the example init.d file written by
# Miquel van Smoorenburg and modified for Debian GNU/Linux by Ian Murdock
#
### BEGIN INIT INFO
# Provides:          samhain
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
### END INIT INFO
#

prefix="/usr"
exec_prefix="${prefix}"

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=${exec_prefix}/sbin/samhain
NAME=samhain
DESC="file integrity checker"
PIDFILE=/var/run/${NAME}/${NAME}.pid

test -x $DAEMON || exit 0

. /lib/lsb/init-functions

set -e

# Check if a daemon is running
running()
{
# Check with pidfile first, if available
    if [ -r "$PIDFILE" ] ; then
	    pid=`cat $PIDFILE`
# No pid, probably no daemon present
	    if [ -n "$pid" ] ; then
		    pidofproc -p $PIDFILE $DAEMON
		    return $?
	    fi
    fi
# Try to find the daemon by name
    pidof $DAEMON >/dev/null 
    return $?
}

# Initialize 
init_db()
{
# Initialize the database only if does not exist yet, abort if
# it cannot be created
     [  -f /var/lib/samhain/samhain_file ] && return
     log_progress_msg "Creating integrity database (this can take some minutes)."
     samhain -t init >/var/log/samhain/samhain-init.log 2>&1
     if [  ! -f /var/lib/samhain/samhain_file ] ; then
        log_failure_msg "Database could not be created. Review /var/log/samhain/samhain-init.log"
        log_end_msg 1
        exit 1
    fi
    log_progress_msg "Database created."
}



case "$1" in
  start)
        [ ! -e /var/run/${NAME} ] && mkdir -p /var/run/${NAME}
        log_begin_msg "Starting $DESC: $NAME"
        init_db
        start-stop-daemon --start --quiet --oknodo --pidfile $PIDFILE --exec $DAEMON
        log_end_msg $?
        ;;
  stop)
        log_begin_msg "Stopping $DESC: $NAME"
        start-stop-daemon --stop --quiet --retry 5 --oknodo --pidfile $PIDFILE  --name  $NAME 
        log_end_msg $?
        ;;
  reload)
  	log_begin_msg "Reloading $DESC configuration files: $NAME"
	if running ; then
		start-stop-daemon --stop --signal 1 --quiet --exec $DAEMON
		log_end_msg $?
	else
            	log_daemon_msg " ERROR: $DAEMON is not running."
		log_end_msg 1
	fi

  	;;
  restart|force-reload)
        log_begin_msg "Restarting $DESC: $NAME"
	if running; then
	        start-stop-daemon --stop --quiet --retry 5 --oknodo --pidfile $PIDFILE  --name  $NAME 
	        for i in 1 2 3 ; do
	            if ! running; then break ; fi
	            sleep 1
	        done
	fi
        if  ! running  ; then
            start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON
            log_end_msg $?
        else
            log_daemon_msg " ERROR: $DAEMON did not die in the expected time, will not restart/force-reload"
            log_end_msg 1
        fi
        ;;
   status)
	if [ -e $PIDFILE ] ; then
	   	status_of_proc -p $PIDFILE $DAEMON $NAME && exit 0 || exit $?
	else
	   	status_of_proc $DAEMON $NAME && exit 0 || exit $?
	fi
        ;;
  *)
        N=/etc/init.d/${0##*/}
        echo "Usage: $N {start|stop|restart|reload|force-reload|status}" >&2
        exit 1
        ;;
esac

exit 0