File: snort-pgsql.postinst

package info (click to toggle)
snort 2.3.3-11
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 22,512 kB
  • ctags: 11,344
  • sloc: ansic: 70,967; sh: 4,848; makefile: 748; perl: 478; sql: 212
file content (253 lines) | stat: -rw-r--r-- 8,005 bytes parent folder | download
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#!/bin/sh -e

CONFIG=/etc/snort/snort.debian.conf

. /usr/share/debconf/confmodule
test $DEBIAN_SCRIPT_DEBUG && set -v -x

# summary of how this script can be called:
#        * <postinst> `configure' <most-recently-configured-version>
#        * <old-postinst> `abort-upgrade' <new version>
#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
#          <new-version>
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see /usr/doc/packaging-manual/
#
# quoting from the policy:
#     Any necessary prompting should almost always be confined to the
#     post-installation script, and should be protected with a conditional
#     so that unnecessary prompting doesn't happen if a package's
#     installation fails and the `postinst' is called with `abort-upgrade',
#     `abort-remove' or `abort-deconfigure'.

case "$1" in
    install)
	;;
    upgrade)
	db_get snort-pgsql/startup || true
	if [ "$RET" = "manual" ]; then
		#db_fset snort-pgsql/please_restart_manually seen false
		db_beginblock
		db_input high snort-pgsql/please_restart_manually || true
		db_endblock
		db_go
		db_stop
	fi
	;;
    configure)
	# edit config file
	db_get snort-pgsql/startup || true; 		STARTUP=$RET
	db_get snort-pgsql/interface || true; 	INTERFACE="$RET"
	db_get snort-pgsql/address_range || true;	ADDRESS_RANGE="$RET"
	db_get snort-pgsql/disable_promiscuous || true; DISABLE_PROMISCUOUS=$RET
	db_get snort-pgsql/reverse_order || true;	REVERSE_ORDER=$RET
        db_get snort-pgsql/send_stats || true;        STATS_SEND="$RET"
	db_get snort-pgsql/stats_rcpt || true;	STATS_RCPT="$RET"
	db_get snort-pgsql/stats_treshold || true;	STATS_THRESHOLD="$RET"
	db_get snort-pgsql/options || true;		OPTIONS="$RET"

	test "$DISABLE_PROMISCUOUS" = "true" && OPTIONS="$OPTIONS -p"
	test "$REVERSE_ORDER" = "true" && OPTIONS="$OPTIONS -o"
	# Failsafe in case the values above are blank (jfs)
	[ -z "$STATS_RCPT" ] && STATS_RCPT=root
	[ -z "$STATS_THRESHOLD" ] && STATS_THRESHOLD=1
	#STATS_RCPT=`echo "$STATS_RCPT" | sed -e 's/@/\\\\@/g' -e 's/,/\\\\,/g'`

	cat <<EOF >$CONFIG
# This file is used for options that are changed by Debian to leave
# the original lib files untouched.
# You have to use "dpkg-reconfigure snort" to change them.

DEBIAN_SNORT_STARTUP="$STARTUP"
DEBIAN_SNORT_HOME_NET="$ADDRESS_RANGE"
DEBIAN_SNORT_OPTIONS="$OPTIONS"
DEBIAN_SNORT_INTERFACE="$INTERFACE"
DEBIAN_SNORT_SEND_STATS="$STATS_SEND"
DEBIAN_SNORT_STATS_RCPT="$STATS_RCPT"
DEBIAN_SNORT_STATS_THRESHOLD="$STATS_THRESHOLD"
EOF

	if [ -f /etc/snort/snort.conf ]; then
		# insert database config stuff in the configuration file,
		# or configure it for syslog-logging.
		db_get snort-pgsql/configure_db
		if  [ "$RET" = "true" ]; then
			db_get snort-pgsql/db_host || true; DB_HOST=$RET
			db_get snort-pgsql/db_database || true; DB_DATABASE=$RET
			db_get snort-pgsql/db_user || true; DB_USER=$RET
			db_get snort-pgsql/db_pass || true; DB_PASS=$RET

			# Here we put the database stuff in the config file.
			TEMPFILE=`mktemp`
			cat /etc/snort/snort.conf | while read LINE
			do
				if [ "$LINE" = "# (#DBSTART#)" ]
				then
					echo "# (#DBSTART#)" >> $TEMPFILE
					echo -n "output database: log, postgresql, " >> $TEMPFILE
					if [ $DB_USER ]
					then
						echo -n "user=$DB_USER " >> $TEMPFILE
					fi
					if [ $DB_PASS ]
					then
						echo -n "password=$DB_PASS " >> $TEMPFILE
					fi
					if [ $DB_DATABASE ]
					then
						echo -n "dbname=$DB_DATABASE " >> $TEMPFILE
					fi
					if [ $DB_HOST ]
					then
						echo -n "host=$DB_HOST " >> $TEMPFILE
					fi
					echo " " >> $TEMPFILE
					echo "# (#DBEND#)" >> $TEMPFILE
					break
				else
					echo $LINE >> $TEMPFILE
				fi
			done

			WRITE=0
			cat /etc/snort/snort.conf | while read LINE
			do
				if [ $WRITE -eq 1 ]
				then
					echo $LINE >> $TEMPFILE
				fi

				if [ "$LINE" = "# (#DBEND#)" ]
				then
					WRITE=1
				fi
			done
			mv -f $TEMPFILE /etc/snort/snort.conf
		fi

		# Ensure the config file is readable by root.root and mode 600
		if ! dpkg-statoverride --list /etc/snort/snort.conf >/dev/null
		then
			chown root:snort /etc/snort/snort.conf
			chmod 640 /etc/snort/snort.conf
		fi
	fi

	db_stop

	# Check for left-over files from woody packages.
	OLDCONF=/etc/snort/snort.rules.conf
	if [ -f $OLDCONF ]; then
		mv $OLDCONF $OLDCONF.OBSOLETE
	fi

	# Update the rc.d's
	update-rc.d snort defaults >/dev/null

	# in the case we reconfigure we have to restart and not just to start.
	if [ -x /usr/sbin/invoke-rc.d ]; then
		invoke-rc.d snort stop || true
	else
		/etc/init.d/snort stop || true
	fi
	;;
    abort-upgrade)
	;;
    *)
	echo "postinst called with unknown argument \`$1'" >&2
	exit 0
	;;
esac

if [ "$STARTUP" = "dialup" ]; then

	# Try to guess environments for all pppds we have no .env for...
	for PPPD_PID in $(pidof pppd ipppd); do
		# If we got an empty PID (however), we break here
		test "$PPPD_PID" || continue

		#
		# This is a lot of shell voodoo, let's try to figure it out:
		# 1. egrep:
		#    It greps for our current pppd PID in all pppd and ipppd
		#    pidfiles.
		#    It *should* return exactly one file name: the one with
		#    our current pppd PID in it; however, to be safe, we fence
		#    it with a head -1.
		# 2. basename $(egrep ...) .pid:
		#    It takes the file name from the egrep and strips off its
		#    path and the .pid suffix
		# 3. sed:
		#    Unfortunately the filenames of pppd and ipppd differ:
		#    pppd uses $INTERFACE.pid, while ipppd uses
		#    ipppd.$INTERFACE.pid.
		#    The .pid is already stripped off by basename, thus, we
		#    just strip off any "ipppd." prefix and end up in the
		#    plain interface name.
		#    Maybe pppd decides to change it's pidfile naming
		#    convention according to ipppd somewhere in the future,
		#    thus, we use '^i\?pppd\.' (sed eregex) and thus strip
		#    off all "ipppd." and all "pppd." prefixes. This doesn't
		#    harm anyways.
		#    Because of the pppd pidfile naming convention, our
		#    approach works always with ipppd and mostly with pppd:
		#    the latter only, if the user did not decide to rename
		#    his ppp interface to something else than ppp*
		#    (not possible currently, afaics).
		#
		PPP_IFACE=$(basename $(egrep -l "^[[:space:]]*$PPPD_PID[[:space:]]*\$" /var/run/ppp*.pid /var/run/ipppd.*.pid 2> /dev/null | head -1) .pid | sed -e 's/^i\?pppd\.//')

		#
		# If we got no interface from pidfiles (because there are no
		# pidfiles, for example), we assume the most common case:
		# one pppd with default route set.
		# This is ugly, but there is no other chance. Let's hope,
		# nobody ever manages multiple pppds without pidfiles for
		# them.
		#
		test "$PPP_IFACE" || PPP_IFACE=$(route -n |
			awk '/^0\.0\.0\.0 / { print $8 }')

		# If we couldn't discover an interface name, we break here
		test "$PPP_IFACE" || continue

		PPP_LOCAL=$(ifconfig $PPP_IFACE |
			awk '/inet addr:/ { gsub("addr:", ""); print $2 }')

		# If we couldn't discover a local IP, we break here
		test "$PPP_LOCAL" || continue

		ENVFILE=/var/run/snort_$PPP_IFACE.env

		# If we already have an .env for that interface, we break here
		test -e "$ENVFILE" && continue

		# Write .env for that interface
		echo "Creating missing $ENVFILE"
		echo "PPPD_PID=$PPPD_PID"    > "$ENVFILE"
		echo "PPP_IFACE=$PPP_IFACE" >> "$ENVFILE"
		echo "PPP_LOCAL=$PPP_LOCAL" >> "$ENVFILE"

		# If such a snort is still running, just kill it
		ps -ef | grep /usr/sbin/snort | grep "$PPP_LOCAL" |
			grep "$PPP_IFACE" | awk '{ print $2 }' |
			xargs --no-run-if-empty kill -s KILL >/dev/null
	done
fi

if [ "$STARTUP" = "boot" ] || [ "$STARTUP" = "dialup" ]; then
	if [ -x /usr/sbin/invoke-rc.d ]; then
		invoke-rc.d snort start || true
	else
		/etc/init.d/snort start || true
	fi
fi

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0