File: test_mod_ipv6calc.sh

package info (click to toggle)
ipv6calc 4.1.0-0.2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 8,676 kB
  • sloc: ansic: 101,567; sh: 6,796; perl: 3,867; xml: 1,475; makefile: 908
file content (445 lines) | stat: -rwxr-xr-x 11,753 bytes parent folder | download | duplicates (2)
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#!/usr/bin/env bash
#
# Project    : ipv6calc
# File       : test_mod_ipv6calc.sh
# Version    : $Id: 8df55b24aa8e28b4a7ecc5fe13179fae8cc998bd $
# Copyright  : 2015-2022 by Peter Bieringer <pb (at) bieringer.de>
#
# Test patterns for ipv6calc conversions

for BIN_PS in /bin/ps /usr/bin/ps; do
	if [ -x "$BIN_PS" ]; then
		break
	fi
done


create_apache_root_and_start() {
	echo "INFO  : create temporary base directory"
	dir_base=$(mktemp -d /tmp/mod_ipv6calc.XXXXX)
	if [ -z "$dir_base" ]; then
		echo "ERROR : can't create temporary directory"
		return 1
	fi
	echo "INFO  : temporary base directory created: $dir_base"

	for dir in $dir_base/conf $dir_base/var $dir_base/modules $dir_base/logs $dir_base/conf.d $dir_base/conf.modules.d; do
		echo "INFO  : create directory: $dir"
		mkdir $dir
		if [ $? -ne 0 ]; then
			echo "ERROR : can't create directory: $dir"
			return 1
		fi
		echo "INFO  : directory created: $dir"
	done

	for file in /etc/httpd/conf/httpd.conf /etc/httpd/conf/magic; do
		echo "INFO  : copy file: $file -> $dir_base/conf"
		cp -p $file $dir_base/conf/
		if [ $? -ne 0 ]; then
			echo "ERROR : can't copy file: $file"
			return 1
		fi
		echo "INFO  : file copied: $file -> $dir_base/conf"
	done

	echo "INFO  : define listen port 8080 in $dir_base/conf/httpd.conf"
	perl -pi -e 's/^Listen.*/Listen 8080/g' $dir_base/conf/httpd.conf
	if [ $? -ne 0 ]; then
		echo "ERROR : can't define listen port: $dir_base/conf/httpd.conf"
		return 1
	fi

	echo "INFO  : change log level to debug $dir_base/conf/httpd.conf"
	perl -pi -e 's/^LogLevel .*/LogLevel debug/g' $dir_base/conf/httpd.conf
	if [ $? -ne 0 ]; then
		echo "ERROR : can't define LogLevel: $dir_base/conf/httpd.conf"
		return 1
	fi

	echo "INFO  : specify pid file in $dir_base/conf/httpd.conf"
	echo "PidFile $dir_base/var/httpd.pid" >>$dir_base/conf/httpd.conf
	if [ $? -ne 0 ]; then
		echo "ERROR : can't specify pid file: $dir_base/conf/httpd.conf"
		return 1
	fi

	perl -pi -e 's/^ServerRoot.*$//g' $dir_base/conf/httpd.conf

	for file in 00-base.conf 00-mpm.conf 00-systemd.conf; do
		[ -e /etc/httpd/conf.modules.d/$file ] || continue
		cp /etc/httpd/conf.modules.d/$file $dir_base/conf.modules.d/
	done

	# disable auth modules
	perl -pi -e 's/^LoadModule authn.*$//g' $dir_base/conf.modules.d/00-base.conf
	perl -pi -e 's/^LoadModule auth_digest_module.*$//g' $dir_base/conf.modules.d/00-base.conf

	cp /etc/httpd/modules/* $dir_base/modules/

	# copy mod_ipv6calc
	cp .libs/mod_ipv6calc.so $dir_base/modules/
	cp ipv6calc.conf $dir_base/conf.d/

	## adjust ipv6calc.conf settings	
	# load module
	perl -pi -e 's/#(LoadModule.*)/$1/g' $dir_base/conf.d/ipv6calc.conf

	# enable module
	perl -pi -e 's/#(ipv6calcEnable).*/$1 on/g' $dir_base/conf.d/ipv6calc.conf

	# enable anonymized access logging
	perl -pi -e 's/#(CustomLog.*)/$1/g' $dir_base/conf.d/ipv6calc.conf

	if [ "$debug_module" = "1" ]; then
		# enable module debugging
		perl -pi -e 's/#(ipv6calcDebuglevel.*-1)/$1/g' $dir_base/conf.d/ipv6calc.conf
	fi

	if [ "$debug_library" = "1" ]; then
		# enable library debugging
		perl -pi -e 's/#(ipv6calcOption\s+debug).*/$1 -1/g' $dir_base/conf.d/ipv6calc.conf
	fi

	## disable databases by option
	[ "$disable_geoip2" = "1" ]      && perl -pi -e 's/#(ipv6calcOption\s+db-geoip2-disable\s+yes)$/$1/g' $dir_base/conf.d/ipv6calc.conf
	[ "$disable_ip2location" = "1" ] && perl -pi -e 's/#(ipv6calcOption\s+db-ip2location-disable\s+yes)$/$1/g' $dir_base/conf.d/ipv6calc.conf
	[ "$disable_dbip2" = "1" ]       && perl -pi -e 's/#(ipv6calcOption\s+db-dbip2-disable\s+yes)$/$1/g' $dir_base/conf.d/ipv6calc.conf
	[ "$disable_external" = "1" ]    && perl -pi -e 's/#(ipv6calcOption\s+db-external-disable\s+yes)$/$1/g' $dir_base/conf.d/ipv6calc.conf

	[ "$action_asn" = "0" ]    && perl -pi -e 's/(ipv6calcActionAsn\s+).*$/$1 off/g'         $dir_base/conf.d/ipv6calc.conf
	[ "$action_cc" = "0" ]     && perl -pi -e 's/(ipv6calcActionCountrycode\s+).*$/$1 off/g' $dir_base/conf.d/ipv6calc.conf
	[ "$action_reg" = "0" ]    && perl -pi -e 's/(ipv6calcActionRegistry\s+).*$/$1 off/g'    $dir_base/conf.d/ipv6calc.conf
	[ "$action_gid" = "0" ]    && perl -pi -e 's/(ipv6calcActionGeonameid\s+).*$/$1 off/g'   $dir_base/conf.d/ipv6calc.conf
	[ "$action_anon" = "0" ]   && perl -pi -e 's/(ipv6calcActionAnonymize\s+).*$/$1 off/g'   $dir_base/conf.d/ipv6calc.conf

	[ "$config_passive" = "1" ]   && perl -pi -e 's/(ipv6calcDefaultActive\s+).*$/$1 off/g'   $dir_base/conf.d/ipv6calc.conf
	[ "$config_passive" != "1" -a "$config_environment" = "1" ]   && echo "SetEnv ipv6calcPassive" >>$dir_base/conf.d/ipv6calc.conf
	[ "$config_passive" = "1" -a "$config_environment" = "1" ]   && echo "SetEnv ipv6calcActive" >>$dir_base/conf.d/ipv6calc.conf


	echo "INFO  : start httpd with ServerRoot $dir_base"
	/usr/sbin/httpd -X -e info -d $dir_base &
	if [ $? -eq 0 ]; then
		httpd_pid=$!
		echo "INFO  : httpd started in background with pid=$httpd_pid"
		sleep 1
		if $BIN_PS -p $httpd_pid --no-headers >/dev/null 2>&1; then
                        echo "INFO  : httpd started in background (wait 10 seconds for pid file now)"
                else
                        echo "ERROR : httpd did not start in background (pid missing: $httpd_pid)"
                        return 1
                fi
	fi

	limit=10
	i=0
	while [ ! -f $dir_base/var/httpd.pid ]; do
		i=$[ $i + 1 ]
		if [ $i -gt 10 ]; then
			break
		fi
		sleep 1
	done

	pid="$(cat "$dir_base/var/httpd.pid")"

	if [ -z "$pid" ]; then
		echo "ERROR : httpd PID file empty: $dir_base/var/httpd.pid"
		return 1
	fi

	if ! $BIN_PS -p $pid --no-headers >/dev/null 2>&1; then
		echo "ERROR : httpd started with PID but no longer running: $pid"
		return 1
	fi

	echo "INFO  : httpd started with PID: $pid"

	if [ -f $dir_base/logs/error_log ]; then
		grep ipv6calc $dir_base/logs/error_log
		lines_error_log=$(cat "$dir_base/logs/error_log" | wc -l)
	fi

	
	if [ "$debug_lsof" = "1" ]; then
		echo "INFO  : list of open files of pid: $pid"
		lsof -p $pid
	fi

	if [ "$debug_config" = "1" ]; then
		echo "INFO  : list effective module config"
		grep -v "^\s*#" $dir_base/conf.d/ipv6calc.conf | grep -v "^\s*$"
	fi

	echo "NOTICE: base directory is     : $dir_base"
	echo "NOTICE: ipv6calc module config: $dir_base/conf.d/ipv6calc.conf"
	echo "NOTICE: error log             : $dir_base/logs/error_log"
	echo "NOTICE: access log            : $dir_base/logs/access_log"
	echo "NOTICE: anonymized access log : $dir_base/logs/access_anon_log"
}

stop_apache() {
	if [ -z "$pid" -a -n "$dir_base" -a -f "$dir_base/var/httpd.pid" ]; then
		pid="$(cat "$dir_base/var/httpd.pid")"
	fi

	if [ -n "$pid" ]; then
		echo "NOTICE: kill started httpd with PID: $pid"
		kill $pid

		sleep 1
		if $BIN_PS -p $pid --no-headers; then
			echo "WARN  : process still running (kill now with -9)"
			kill -9 $pid
		fi
	else
		if ! $BIN_PS u -C httpd --no-headers | grep "/tmp/mod_ipv6calc"; then
			# nothing to do
			echo "INFO  : no related httpd process found to kill"
			return 0
		fi

		if [ ! -x /usr/bin/killall ]; then
			echo "ERROR : no httpd PID given, but no 'killall' available (provided by RPM: psmisc)"
			echo "ERROR : you have to kill process manually"
		else
			echo "WARN  : no httpd PID given, use killall"
			/usr/bin/killall httpd
		fi

		sleep 1
		if $BIN_PS u -C httpd --no-headers | grep "/tmp/mod_ipv6calc"; then
			echo "WARN  : process still running (kill now with -9)"
			/usr/bin/killall -9 httpd
		fi
	fi
}


exec_request() {
	dst="$1"

	if [ -n "$repeat" ]; then
		max=$[ $repeat + 1 ]
	else
		max=1
	fi

	count=0

	while [ $count -lt $max ]; do
		echo "NOTICE: test: $1"
		# curl-7.29.0-19.el7.x86_64 is broken, -g required
		curl -g -s "http://$1:8080/" >/dev/null
		if [ $? -ne 0 ]; then
			echo "ERROR : curl request to $1:8080 failed"
			return 1
		fi

		echo "INFO  : access log entry"
		tail -1 $dir_base/logs/access_log

		if [ -f "$dir_base/logs/access_anon_log" -a -s "$dir_base/logs/access_anon_log" ]; then
			echo "INFO  : anonymized access log entry"
			log_anon=$(tail -1 $dir_base/logs/access_anon_log)
			echo "$log_anon"
			if [ "$config_passive" = "1" -a "$config_environment" != "1" ]; then
				if echo "$log_anon" | grep -q "PbD"; then
					echo "INFO  : anonymized access log entry is containing 'PdD' (OK)"
				else
					echo "ERROR : anonymized access log entry is not containing 'PdD'"
					return 1
				fi
			elif [ "$config_passive" != "1" -a "$config_environment" = "1" ]; then
				if echo "$log_anon" | grep -q "PbE"; then
					echo "INFO  : anonymized access log entry is containing 'PbE' (OK)"
				else
					echo "ERROR : anonymized access log entry is not containing 'PbE'"
					return 1
				fi
			fi
		else
			echo "ERROR : anonymized access log missing or empty"
			return 1
		fi

		echo "INFO  : error log entry"
		tail -n +$[ $lines_error_log + 1 ] "$dir_base/logs/error_log" | grep "ipv6calc"
		# update number of lines
		lines_error_log=$(cat "$dir_base/logs/error_log" | wc -l)

		count=$[ $count + 1 ]	
	done || return 1

	return 0
}


run_test_requests() {
	echo "NOTICE: run test requests"

	if [ -n "$address" ]; then
		if echo "$address" | grep -q ":"; then
			# IPv6
			exec_request "[$address]"
		else
			# IPv4
			exec_request "$address"
		fi
	else
		for ipv4 in $(/sbin/ip -o addr show |grep -w inet | grep -w global | grep -vw deprecated | awk '{ print $4 }' | awk -F/ '{ print $1 }'); do
			exec_request "$ipv4" || return 1
		done

		# retrieve local IPv6 address
		for ipv6 in $(/sbin/ip -o addr show |grep -w inet6 | grep -w global | grep -vw deprecated | awk '{ print $4 }' | awk -F/ '{ print $1 }'); do
			exec_request "[$ipv6]" || return 1
		done
	fi

	sleep 2

	return 0
}

#### Help
help() {
	cat <<END
$(basename "$0") [<options>] [-S|-K|-W]
	-S	start
	-K	stop (kill)
	-W	run workflow

	-m	enable debug module
	-l	enable debug library
	-f	list open files after start
	-c	show effective module config options

	-g	disable GeoIP
	-G	disable GeoIP(MaxMindDB)
	-i	disable IP2Location
	-d	disable db-ip.com
	-D	disable db-ip.com(MaxMindDB)
	-e	disable external databases

	-A	disable action ASN
	-C	disable action CountryCode
	-R	disable action Registry
	-I	disable action GeonameID
	-N	disable action Anonymization

	-P	disable 'ipv6calcDefaultActive'
	-E	keep 'ipv6calcDefaultActive', but set environment

	-b <base directory

	-a <address>	disable autoretrievement of local IP, use given one instead
	-r		repeat (1x)
END
}

#### Options
while getopts "EPIDGrACRNca:fSKWb:mlgideh\?" opt; do
	case $opt in
	    b)
		if [ -d "$OPTARG" ]; then
			dir_base=$OPTARG
		else
			echo "ERROR : given base directory doesn't exist: $dir_base"
		fi
		;;
	    m)
		debug_module=1
		;;
	    l)
		debug_library=1
		;;
	    f)
		debug_lsof=1
		;;
	    c)
		debug_config=1
		;;
	    g)
		echo "NOTICE: support removed for GeoIP(legacy): $opt"
		;;
	    G)
		disable_geoip2=1
		;;
	    i)
		disable_ip2location=1
		;;
	    d)
		echo "NOTICE: support removed for db-ip.com(BerkeleyDB): $opt"
		;;
	    D)
		disable_dbip2=1
		;;
	    e)
		disable_external=1
		;;
	    S)
		action="start"
		;;
	    K)
		action="kill"
		;;
	    a)
		address="$OPTARG"
		;;
	    W)
		action="workflow"
		;;
	    A)
		action_asn="0"
		;;
	    C)
		action_cc="0"
		;;
	    R)
		action_reg="0"
		;;
	    I)
		action_gid="0"
		;;
	    N)
		action_anon="0"
		;;
	    P)
		config_passive="1"
		;;
	    E)
		config_environment="1"
		;;
	    r)
		repeat=1
		;;
	    h|\?)
		help
		exit 1
		;;
	    *)
		echo "ERROR : unrecognized option: $opt"
		;;
	esac
done

case $action in
    workflow)
	create_apache_root_and_start || exit 1
	run_test_requests || exit 1
	stop_apache || exit 1
	;;
    kill)
	stop_apache || exit 1
	;;
    start)
	create_apache_root_and_start || exit 1
	;;
    *)
	help
	exit 1
	;;
esac