This file is indexed.

/usr/sbin/syslog-ng-debun is in syslog-ng-core 3.13.2-3.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

   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
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
#!/bin/sh
#!/bin/bash

### syslog-ng-debun: syslog debug bundle generator
### Written/Copyright: Gyorgy Pasztor <pasztor@linux.gyakg.u-szeged.hu>, (c) 2014-2016.
### Further enhancements: Janos Szigetvari - jszigetvari at gmail dot com, (c) 2016-2017.
### This software may be used and distributed according to the terms of GNU GPLv2
### http://www.gnu.org/licenses/gpl-2.0.html

version="0.3.8.20170926"

### Check for "local" variable support
if type local >/dev/null; then
	:
elif type bash >/dev/null; then
	#printf "There is no local, re-exec self in bash\n" >&2
	exec bash $0 "$@"
else
	printf "No local variable support on this system\n" >&2
	exit 1
fi

###
### Global defaults
### Do not overwrite them, parameters or distro / OS specific detections will do that if neccessary
###

argv_backup="${@}"
engage=0
os="none"
dist="none"
default_debug_params="-Fedv --enable-core"
default_ldebug_params="-Fev"
default_pcap_params="port 514 or port 601 or port 53"
extras=""
sngallpids=""
debugpid=none
debugtailpid=none
pcappid=none
tracepids=""
ipconfig="ip addr"
routeconfig () { netstat -nr ; }
netstatnlp () { netstat -nlp ; }
netstatlunp () { netstat -lunp ; }
netstatsu="netstat -su"
netstatpunt="netstat -punt"
binprefix=/opt/syslog-ng
workdir=/tmp
lsof="lsof -p"
pscmd="ps auxwwwwwf"
pseao="ps -eao"
dfk="df -k"
dfh="df -h"
dfi="df -i"
duks="du -ks"
grepq="fgrep -q"
lddcmd="ldd"
topcmd () { top -b -n 1 -c >"${1}"; }
opensslcmd="openssl"
sed_equivalent_cmd="sed -E"
mount=mount
varlimit=1000
myplimit () { echo "Plimit query is not supported on this platform" >&3 ; }
distpkgoffile () { echo "Package file search is not supported (yet) on this platform" >&3 ; }
distpkgstatus () { echo "Package status query is not (yet) supported on this platform" >&3 ; }
selftar="tar cf - ."
gzipcmd="gzip -9"
showdep="dpkg -S"
pcapifparm=-i
w=w
vmstat=vmstat
dmesg=dmesg
tcpdumpcmd="tcpdump"
tcpdumpopts="-p -s 1500 -w"
opensslmajor=0
getsyslogpids () { pidof syslog-ng ; }
os_hash_helper () { find . '!' \( -name debun.manifest -o -name syslog-ng.debun.txt \) -type f -print0 | xargs -0 md5sum ; }
dfk_parser () { tail -1 | while read FS ALL USED AVAIL UPERC MP; do if echo ${AVAIL} | ${grepq} '%'; then echo ${USED} ; else echo ${AVAIL} ; fi done ; }
trace="strace -s256 -ff -ttT -f"
initfile="/etc/init.d/syslog-ng"
service_stop="${initfile} stop"
service_start="${initfile} start"
service_status="${initfile} status"
#checkpid () { [ -d /proc/"$1" ]; }
#some old Unix versions did not have procfs, and kill -0 allows to check whether a PID exists
#for reference: http://pubs.opengroup.org/onlinepubs/009695399/functions/kill.html
checkpid () { [ -n "$1" ] && kill -0 "$1" 2>/dev/null ; }
#mywait () { jobs -p >${tmpdir}/sdn.jobs ; for i in `grep -v "\<$tailpid\>" ${tmpdir}/sdn.jobs` ; do wait $i ; done ; }
mywait () { jobs -p >${tmpdir}/sdn.jobs ; for i in `grep -v "^$tailpid\$" ${tmpdir}/sdn.jobs` ; do wait $i ; done ; }
is_available () { which "$1" >/dev/null 2>&1; }
distpkgoffile_cleanup () { : ; }

###
### Show Usage
###
debun_usage () {
cat <<END
Usage: syslog-ng-debun [OPTIONS]

General Options:
  -r		Run actual information gathering
  -h		Show this help page
  -R [dir]	Syslog-ng-PE's alternate install dir, instead of /opt/syslog-ng
  -W [dir]	Work dir, where debug bundle will be placed
  -l		"light" collect: Don't get data, which may disturb your sense about
		privacy, like process tree, fstab, etc. If you use with -d, then it
		will also enlighten that's params: $default_ldebug_params

Debug mode options:
  -d		Debug with params: $default_debug_params
		Warning! May increase disk io during the debug,
		and dumps huge amount of data!
  -D [params]	Debug with custom params
  -w [sec]	Wait [sec] seconds before start syslog's debug mode, and
		start realtime watching of it
  -t [sec]	Timeout for noninteractive debug

Packet capture options:
  -i [iface]	Capture packets on specified interface
  -p		Create packet capture with filter: $default_pcap_params
  -P [params]	Create packet capture with custom filter
  -t [sec]	Timeout for noninteractive debug

Syscall tracing options:
  -s		Trace syslog
  -t [sec]	Timeout for noninteractive debug
END
[ -n "$2" ] && printf "\nError: %s\n\n" "$2"
exit ${1:-0}
}

###
### Parsing optional parameters
###

while getopts "rhldD:pP:w:i:W:R:t:s" opt ; do
	case $opt in
		r)
			engage=1
			;;
		d)
			[ -n "$debug_params" ] && debun_usage 2
			debug_params="$default_debug_params"
			debug_mode=1
			;;
		D)
			[ -n "$debug_params" ] && debun_usage 2
			debug_params="$OPTARG"
			debug_mode=1
			;;
		i)
			[ -n "$pcap_iface" ] && debun_usage 2
			pcap_iface="$OPTARG"
			;;
		h)
			debun_usage
			;;
		l)
			privacy_mode=1
			;;
		p)
			[ -n "$pcap_params" ] && debun_usage 2
			pcap_params="$default_pcap_params"
			debug_mode=1
			;;
		P)
			[ -n "$pcap_params" ] && debun_usage 2
			pcap_params="$OPTARG"
			debug_mode=1
			;;
		R)
			binprefix="$OPTARG"
			;;
		t)
			timeout="$OPTARG"
			;;
		w)
			waitforit="$OPTARG"
			;;
		W)
			workdir="$OPTARG"
			;;
		s)
			tracing=1
			debug_mode=1
			;;
		*)
			debun_usage 2
			;;
	esac
done

if [ "${engage}" -eq 0 ]; then
	debun_usage
	exit 0
fi

# Parameter consistency checks
[ -n "$pcap_iface" ] && [ -z "$pcap_params" ] && debun_usage 2 "Pcap interface without packet caputre params (-p|-P args)"
[ -n "$waitforit" ] && [ -z "$debug_params" ] && debun_usage 2 "Waiting without debug mode run (-d|-D args)"
[ -n "$timeout" ] && [ -z "$debug_mode" ] && debun_usage 2 "Timeout without debug mode or packet capture"
[ -n "$privacy_mode" ] && [ "x$debug_params" = "x$default_debug_params" ] && debug_params="$default_ldebug_params"

syslogbin=${binprefix}/sbin/syslog-ng
vardir=${binprefix}/var
piddir=${vardir}/run
confdir=${binprefix}/etc
if [ -x "${binprefix}/libexec/syslog-ng" ]; then
	syslogrealbin=${binprefix}/libexec/syslog-ng
else
	syslogrealbin=${binprefix}/sbin/syslog-ng
fi

debun_init () {
	#Create temp dir, to place files into
	host=`uname -n`
	date=`date '+%Y-%m-%d_%H-%M'`
	tmpdir=${workdir}/syslog.debun.${host}.${date}.$$
	( umask 077 ; mkdir ${tmpdir} )
	[ -d "$tmpdir" ] || { printf "Could not create a temp directory\n" >&2 ; exit 1 ; }
	[ -z "$tmpdir" ] && { printf "Could not create a temp directory\n" >&2 ; exit 1 ; }

	# Start redirections

	#exec 3>&1 >${tmpdir}/syslog-ng.debun.txt 2>${tmpdir}/syslog-ng.debun.txt
	exec 3>&1 >${tmpdir}/syslog-ng.debun.txt 2>&1
	echo "Syslog-NG DEBUg buNdle generator"
	sync
	while [ ! -f ${tmpdir}/syslog-ng.debun.txt ] ; do sleep 1 ; done
	#nohup tail -f ${tmpdir}/syslog-ng.debun.txt >&3 &
	tail -f ${tmpdir}/syslog-ng.debun.txt >&3 &
	tailpid=$!
	#disown
}

debun_finish_debug () {
	if [ "${debugpid}" != "none" ]; then
		if checkpid ${debugpid} ; then
			kill -INT $debugpid
			checkpid ${debugpid} && sleep 1
			checkpid ${debugpid} && kill -9 $debugpid
			checkpid ${debugpid} && sleep 1
			checkpid ${debugpid} && echo "I gave up... debugger pid doesn't die"
		fi
		printf 'Debugpid: "%s"\n' "${debugpid}"
		( exec 3>&- ; $service_start ; )
	fi
	if checkpid ${debugtailpid} ; then
		kill $debugtailpid
	fi
	if checkpid ${pcappid} ; then
		kill -INT $pcappid
	fi
	if [ -n "$tracing" ]; then
		for i in ${tracepids} ; do
			checkpid $i && kill -INT $i
		done
		sleep 2
		for i in ${tracepids} ; do
			checkpid $i && kill -9 $i 2>/dev/null
		done
	fi
	mywait
}

debun_do_tarball () {
	cd ${tmpdir}
	touch ${tmpdir}.tgz
	chmod 600 ${tmpdir}.tgz
	${selftar} | ${gzipcmd} >${tmpdir}.tgz
	cd ..
	rm -r "${tmpdir}"
	printf "Terminating live message watcher: "
	sync
	kill ${tailpid}
	sync
	sleep 1
}

debun_generate_hashes () {
	cd ${tmpdir}
	os_hash_helper > debun.manifest
}

debun_final() {
	debun_finish_debug
	[ -n "$service_status" ] && $service_status >${tmpdir}/svc.post
	printf "\nGenerating hashes..."
	debun_generate_hashes
	printf " done.\nDebug Bundle generation: Done.\n"
	exec >&3 2>&1
	debun_do_tarball
	printf "\n\nYour debug bundle will be stored at %s.tgz\n" "$tmpdir"
}

add_extra () {
	extras="${extras}${extras:+ }$@"
}

###
###PROCESS HANDLING FUNCTIONS
###
getparent () {
	local self
	local parent
	local ret
	local tmpfile=${tmpdir}/getparent.$$.txt
	$pseao pid,ppid >$tmpfile
	# Default value, learned after getchilds()'s forkbomb case
	unset ret
	while read self parent ; do
		[ "$1" = "$self" ] || continue
		ret=$parent
	done < $tmpfile
	rm $tmpfile
	echo $ret
}

getchilds () {
	local childs
	local dummy
	local child
	local tmpfile=${tmpdir}/getchilds.$$.txt
	$pseao ppid,pid >$tmpfile
	# Need to initialize it with a default value, since dash allows to inherit the caller's value, even if it's a local variable
	unset childs
	while read dummy child ; do
		[ "$1" = "$dummy" ] || continue
		childs="${childs}${childs:+ }$child"
	done < $tmpfile
	rm $tmpfile
	echo ${childs}
}

getallchilds () {
	local childs
	local subchilds
	childs="`getchilds $1`"
	local i
	# Default value, learned after getchilds()'s forkbomb case
	unset subchilds
	for i in ${childs} ; do
		subchilds="${subchilds}${subchilds:+ }`getallchilds $i`"
	done
	echo ${childs} ${subchilds}
}

acquire_debun_info () {
	pwd > ${tmpdir}/debun.pwd
	echo "${0} ${argv_backup}" > ${tmpdir}/debun.argv
	echo "${version}" > ${tmpdir}/debun.version
	id > ${tmpdir}/debun.runas
}

acquire_system_info () {
	printf "System's full uname: "
	uname -a | tee "${tmpdir}/sys.uname"
	free >${tmpdir}/sys.free
	vmstat >${tmpdir}/sys.vmstat
	topcmd "${tmpdir}/sys.top"
	if is_available ${opensslcmd}; then
		${opensslcmd} version >${tmpdir}/sys.openssl.version
	fi
	if is_available java; then
		java -version >${tmpdir}/sys.java.version 2>&1
	fi
}

acquire_network_info () {
	printf "Getting network-interface information: "
	if $ipconfig >${tmpdir}/net.ip ; then
		printf "Success\n"
	else
		printf "Failed\n"
	fi
	printf "Getting network routes: "
	if routeconfig >${tmpdir}/net.route ; then
		printf "Success\n"
	else
		printf "Failed\n"
	fi
	printf "Getting DNS resolution-related information: "
	[ -f /etc/nsswitch.conf ] && cp /etc/nsswitch.conf ${tmpdir}/sys.nsswitch.conf
	[ -f /etc/resolv.conf ] && cp /etc/resolv.conf ${tmpdir}/sys.resolv.conf
	[ -f /etc/hosts ] && cp /etc/hosts ${tmpdir}/sys.hosts
	printf "Done\n"
}

acquire_system_process_info () {
	echo "List all processes"
	$pscmd >${tmpdir}/sys.ps
}

acquire_filesystem_info () {
	echo "Mount and disk free info collection"
	$dfk >${tmpdir}/sys.df_k
	$dfh >${tmpdir}/sys.df_h
	$dfi >${tmpdir}/sys.df_i 2>/dev/null
	$mount >${tmpdir}/sys.mount
}

acquire_system_other_info () {
	$w >${tmpdir}/sys.w
	$dmesg >${tmpdir}/sys.dmesg
	netstatnlp >${tmpdir}/sys.netstat.ltn
	$netstatpunt >${tmpdir}/sys.netstat.est
	netstatlunp >${tmpdir}/sys.netstat.lunp
	$netstatsu >${tmpdir}/sys.netstat.su
	[ -f /proc/net/udp ] && cp /proc/net/udp ${tmpdir}/sys.proc.net.udp
}

### Here comes the general info acquiring parts
acquire_general_info () {
	printf "\nStart general info collection\n"
	acquire_debun_info
	acquire_system_info
	[ -n "$privacy_mode" ] && return
	acquire_network_info
	acquire_system_process_info
	acquire_filesystem_info
	acquire_system_other_info
}

pki_is_certificate () {
	grep "BEGIN CERTIFICATE" "${1}" 2>/dev/null | ${grepq} -v "REQUEST"
}

pki_count_certificates () {
	grep -c "BEGIN CERTIFICATE"	"${1}" 2>/dev/null
}

pki_is_private_key () {
	${grepq} "PRIVATE KEY" "${1}" 2>/dev/null
}

pki_is_public_key () {
	${grepq} "PUBLIC KEY" "${1}" 2>/dev/null
}

pki_is_rsa () {
	${grepq} "RSA " "${1}" 2>/dev/null
}

pki_is_dsa () {
	${grepq} "DSA " "${1}" 2>/dev/null
}

pki_is_ecdsa () {
	${grepq} " EC " "${1}" 2>/dev/null
}

pki_is_encrypted () {
	if ${grepq} "ENCRYPTED" "${1}"; then
		return 0
	else
		local buffer_error
		buffer_error=$( ${opensslcmd} rsa -in "${1}" -noout -text 2>&1 >/dev/null )
		if echo "${buffer_error}" | ${grepq} "problems getting password"; then
			return 0
		else
			return 1
		fi
	fi
}

pki_parse_public_key () {
	oneline_cert=$( tr -d '\n' | tr -d ' ' )
	[ -z "${oneline_cert}" ] && printf "NO_PUBLIC_KEY_COULD_BE_EXTRACTED;" && return

	if echo "${oneline_cert}" | $grepq "UnabletoloadPublicKey"; then
		printf "OPENSSL_TOO_OLD|NO_ECDSA_SUPPORT_IN_OPENSSL;"
	else
		if echo "${oneline_cert}" | $grepq "PublicKey:X509v3extensions"; then
			printf "NO_PUBLIC_KEY_COULD_BE_EXTRACTED;"
		else
			echo "${oneline_cert}" | ${sed_equivalent_cmd} 's/^.*(Modulus|modulus|pub)(\([0-9]+bit\))?:([a-f0-9:]+).*$/Public Key=\3;/'
		fi
	fi
}

pki_guess_certificate () {
	local header fsize buffer_pubkey buffer_rest hashopts hashnum
	fsize=$( wc -c "${1}" | ${sed_equivalent_cmd} 's:^ *([0-9]+) .*$:\1:' )

	if [ ${opensslmajor} -gt 0 ]; then
			hashopts="-subject_hash -subject_hash_old"
			hashnum="2;"
	else
			hashopts="-hash"
			hashnum="1;"
	fi

	certcount=$( pki_count_certificates "${1}" )
	[ "${certcount}" -gt 1 ] && header="STACKED_CERTIFICATE(${certcount});${1};" || header="CERTIFICATE;${1};"
	buffer_pubkey=$( ${opensslcmd} x509 -in "${1}" -text -noout  2>/dev/null | pki_parse_public_key )
	buffer_rest=$( ${opensslcmd} x509 -in "${1}" -noout ${hashopts} -serial -dates -fingerprint -subject -issuer | tr '\n' ';' )
	printf "${header}${fsize};${buffer_pubkey}${hashnum}${buffer_rest}"
}

pki_guess_private_key () {
	local header fsize buffer_pubkey buffer_error
	fsize=$( wc -c "${1}" | ${sed_equivalent_cmd} 's:^ *([0-9]+) .*$:\1:' )

	if pki_is_encrypted "${1}"; then
		header="PRIVATE_ENCRYPTED;${1};"
	else
		if pki_is_rsa "${1}"; then
			header="PRIVATE_RSA;${1};"
			buffer_pubkey=$( ${opensslcmd} rsa -in "${1}" -noout -text 2>/dev/null | pki_parse_public_key )
		elif pki_is_dsa "${1}"; then
			header="PRIVATE_DSA;${1};"
			buffer_pubkey=$( ${opensslcmd} dsa -in "${1}" -noout -text 2>/dev/null | pki_parse_public_key )
		elif pki_is_ecdsa "${1}"; then
			header="PRIVATE_EC;${1};"
			buffer_error=$( ${opensslcmd} ec -in "${1}" -noout -text 2>&1 >/dev/null )
			if echo "${buffer_error}" | ${grepq} "'ec' is an invalid command"; then
				buffer_pubkey="NO_ECDSA_SUPPORT_IN_OPENSSL;"
			elif echo "${buffer_error}" | $grepq "unable to load"; then
				buffer_pubkey="OPENSSL_TOO_OLD;"
			else
				buffer_pubkey=$( ${opensslcmd} ec -in "${1}" -noout -text 2>/dev/null | pki_parse_public_key )
			fi
		else
			header="UNKNOWN_PRIVATE;${1};"
			buffer_pubkey=";"
		fi
	fi
	printf "${header}${fsize};${buffer_pubkey}"
}

pki_guess_public_key () {
	# Public keys do not have their type in the PEM header/footer

	local header fsize buffer_pubkey buffer_error
	header="PUBLIC;${1};"
	fsize=$( wc -c "${1}" | ${sed_equivalent_cmd} 's:^ *([0-9]+) .*$:\1:' )

	if [ ${opensslmajor} -gt 0 ]; then
		buffer_error=$( ${opensslcmd} pkey -pubin -in "${1}" -noout -text 2>&1 >/dev/null )
		if echo "${buffer_error}" | ${grepq} "unable to load"; then
			header="UNKNOWN_PUBLIC;${1};"
			buffer_pubkey="OPENSSL_TOO_OLD|NO_ECDSA_SUPPORT_IN_OPENSSL;"
		else
			buffer_pubkey=$( ${opensslcmd} pkey -pubin -in "${1}" -noout -text 2>/dev/null | pki_parse_public_key )
		fi
	else
		buffer_error=$( ${opensslcmd} rsa -pubin -in "${1}" -noout -text 2>&1 >/dev/null )
		if echo "${buffer_error}" | ${grepq} "expecting an rsa key"; then
			buffer_error=$( ${opensslcmd} dsa -pubin -in "${1}" -noout -text 2>&1 >/dev/null )
			if echo "${buffer_error}" | ${grepq} "expecting a dsa key"; then
				buffer_error=$( ${opensslcmd} ec -pubin -in "${1}" -noout -text 2>&1 >/dev/null )
				if echo "${buffer_error}" | ${grepq} "expecting a ec key"; then
					header="UNKNOWN_PUBLIC;${1};"
					buffer_pubkey=";"
				elif echo "${buffer_error}" | ${grepq} "'ec' is an invalid command"; then
					header="UNKNOWN_PUBLIC;${1};"
					buffer_pubkey="NO_ECDSA_SUPPORT_IN_OPENSSL;"
				else
					buffer_pubkey=$( ${opensslcmd} ec -pubin -in "${1}" -noout -text 2>/dev/null | pki_parse_public_key )
				fi
			else
				buffer_pubkey=$( ${opensslcmd} dsa -pubin -in "${1}" -noout -text 2>/dev/null | pki_parse_public_key )
			fi
		elif echo "${buffer_error}" | ${grepq} "unable to load"; then
			header="UNKNOWN_PUBLIC;${1};"
			buffer_pubkey="OPENSSL_TOO_OLD|NO_ECDSA_SUPPORT_IN_OPENSSL;"
		else
			buffer_pubkey=$( ${opensslcmd} rsa -pubin -in "${1}" -noout -text 2>/dev/null | pki_parse_public_key )
		fi
	fi
	printf "${header}${fsize};${buffer_pubkey}"
}

pki_other_file () {
	local fsize
	fsize=$( wc -c "${1}" | ${sed_equivalent_cmd} 's:^ *([0-9]+) .*$:\1:' )
	printf "OTHER_FILE;${1};${fsize};"
}

pki_process_file () {
	local output_buffer

	if pki_is_certificate "${1}"; then
		output_buffer=$( pki_guess_certificate "${1}" )
	else
		if pki_is_private_key "${1}"; then
			output_buffer=$( pki_guess_private_key "${1}"  )
		elif pki_is_public_key "${1}"; then
			output_buffer=$( pki_guess_public_key "${1}" )
		else
			output_buffer=$( pki_other_file "${1}" )
		fi
	fi
	echo "${output_buffer}"
}

acquire_syslog_pki_info () {
	printf "Gathering PKI information... "
	if is_available ${opensslcmd}; then
		local OPENSSL OPENSSLVER OPENSSLDAY OPENSSLMONTH OPENSSLYEAR REST
		read OPENSSL OPENSSLVER OPENSSLDAY OPENSSLMONTH OPENSSLYEAR REST <${tmpdir}/sys.openssl.version
		opensslmajor=${OPENSSLVER%%.*}
		cd "${confdir}"
		find . -name '*.0' -o -name '*.1' -o -name '*.key' -o -name '*.crt' -o -name '*.pem' >${tmpdir}/syslog.etc.pki.files 2>/dev/null
		while read FILE; do
			pki_process_file "${FILE}" >>${tmpdir}/syslog.etc.pki.info.csv
		done <${tmpdir}/syslog.etc.pki.files
		printf "done.\n"
	else
		printf "no openssl found in PATH.\n"
	fi
}

remove_passwords_from_file () {
	#
	# Double quotes are used because escaping single quotes within a single quoted string is ugly.
	# [ \t\r\n\v\f] class is used for matching whitespaces, because \s is unavailable.
	# The goal is matching substrings that look like this, to replace the password string, and preserve original formatting:
	# password ( 'password-string' ) -->  password ( '___PASSWORD_REMOVED___' )
	###
	${sed_equivalent_cmd} "s|password([ \t\r\n\v\f]*)\(([ \t\r\n\v\f]*)([\"']?)[^)\"']+([\"']?)([ \t\r\n\v\f]*)\)|password\1(\2\3___PASSWORD_REMOVED___\4\5)|g" <"${1}" >"${1}.bak" 2>/dev/null
	mv "${1}.bak" "${1}"
}

acquire_syslog_config () {
	echo "Copy configs from $confdir"
	cd "${confdir}"
	mkdir ${tmpdir}/config
	find . > ${tmpdir}/syslog.etc.files
	grep '\.key$' ${tmpdir}/syslog.etc.files > ${tmpdir}/syslog.etc.files.removed
	grep '\.jks$' ${tmpdir}/syslog.etc.files > ${tmpdir}/syslog.etc.files.removed
	grep -v '\.key$' ${tmpdir}/syslog.etc.files | grep -v '\.jks' | \
		while read FILE; do \
			if pki_is_private_key "${FILE}" 2>/dev/null; then
				echo "${FILE}" >> ${tmpdir}/syslog.etc.files.removed
			else
				echo "${FILE}"
			fi
		done | \
		cpio -pd ${tmpdir}/config

	find ${tmpdir}/config -name "*.conf*" | \
		while read FILE; do \
			remove_passwords_from_file "${FILE}"
		done
}

acquire_syslog_pids () {
	echo 'Old "getsyslogpids":' >${tmpdir}/syslog.pids
	getsyslogpids >>${tmpdir}/syslog.pids
	if [ -r "${piddir}/syslog-ng.pid" ]; then
		sngpid=`cat ${piddir}/syslog-ng.pid`
	else
		# Handle when fhs is "linux"-like
		sngpid=`cat ${piddir}/syslog-ng.pid`
	fi
	ppid=`getparent $sngpid`
	sngallpids="`getallchilds $sngpid`"
	echo "SVpid: $ppid SNGpid: $sngpid Chpids: ${sngallpids}" >>${tmpdir}/syslog.pids
	tail -1 ${tmpdir}/syslog.pids
	if [ -n "$ppid" ]; then
		sngallpids="$ppid $sngpid ${sngallpids}"
	else
		sngallpids="` getsyslogpids `"
	fi
	# drop out the unneeded white spaces, since that disturb the ps command
	sngallpids=`echo ${sngallpids}`
	if [ -n "${sngallpids}" ]; then
		printf 'ps -l -f -p "%s"\n' "${sngallpids}" >>${tmpdir}/syslog.pids
		ps -l -f -p "${sngallpids}" >>${tmpdir}/syslog.pids
	fi
	[ -n "$service_status" ] && $service_status >${tmpdir}/svc.pre
}

acquire_syslog_info () {
	ls -la "${binprefix}" > ${tmpdir}/syslog.lsl.install_dir 2>&1
	printf "Syslog-ng's exact version: "
	$syslogbin --version > ${tmpdir}/syslog.version
	head -1 ${tmpdir}/syslog.version
	${syslogbin}-ctl stats > ${tmpdir}/syslog.stats 2>&1
	echo ${syslogbin}-ctl stats
	${syslogbin}-ctl query get "*" > ${tmpdir}/syslog.query.all 2>/dev/null
	echo ${syslogbin}-ctl query get "*"
	if [ -x ${syslogbin}-query ]; then
		#if we reach this brach, then syslog.query.all - as generated above - will not contain any meaningful data
		${syslogbin}-query sum "*" > ${tmpdir}/syslog.query.all 2>/dev/null
		echo ${syslogbin}-query sum "*"
	fi
	if ${syslogbin}-ctl show-license-info > ${tmpdir}/syslog.license-usage 2>&1; then
		${syslogbin}-ctl show-license-info --json > ${tmpdir}/syslog.license-usage.json 2>/dev/null
	fi
	echo ${syslogbin}-ctl show-license-info
	for i in ${sngallpids} ; do
		is_available ${lsof%% *} && $lsof $i >${tmpdir}/syslog.$i.lsof 2>/dev/null || echo "No lsof in path." >${tmpdir}/syslog.$i.lsof
		myplimit $i >${tmpdir}/syslog.$i.limits
	done

	$syslogbin -s --preprocess-into "${tmpdir}/syslog.pp.conf"
	[ -f "${tmpdir}/syslog.pp.conf" ] && remove_passwords_from_file "${tmpdir}/syslog.pp.conf"
}

acquire_syslog_var () {
	ls -laR "${vardir}" >${tmpdir}/syslog.lslR.var 2>&1

	$duks "${vardir}/" >${tmpdir}/syslog.duks.var
	read vardu dir <${tmpdir}/syslog.duks.var
	mkdir ${tmpdir}/var
	cd "${vardir}"

	freek_tmp=$( ${dfk} ${tmpdir} | dfk_parser )
	#
	# low disk-space => only copy the persist file and the pid file
	###
	if [ "${freek_tmp}" -gt "${vardu}" ] ; then
		if [ "$vardu" -lt "$varlimit" ] ; then
			find . |grep -v run\\/syslog-ng.ctl$ | cpio -pd ${tmpdir}/var
		else
			printf "Size of ${vardir} is larger than $varlimit kilobytes.\n"
			printf "Do you really want to copy its contents? Type 'YES' with all capitals: "
			read ans
			if [ "$ans" = "YES" ]; then
				find . | grep -v "syslog-ng*\.ctl" | cpio -pd ${tmpdir}/var
			fi
		fi
	else
		printf "TOO LOW free disk space on the filesystem holding ${tmpdir}\n"
		printf "to create a full copy of ${vardir}!\nOnly copying most important files.\n"
		find . \( -name "*.persist" -o -name "*.pid" \) | grep -v "syslog-ng.*\.ctl" | cpio -pd ${tmpdir}/var
	fi
}

format_ldd_output () {
	while read x ; do
		#	libsyslog-ng-5.0.5.so => /opt/syslog-ng/lib/libsyslog-ng-5.0.5.so (0x00007f9b42990000)
		#/opt/syslog-ng/lib/libsyslog-ng-5.0.5.so (0x00007f9b42990000)
		x="/${x#*/}"
		#/opt/syslog-ng/lib/libsyslog-ng-6.0.2.so (0x00007f034c8c0000)
		#/opt/syslog-ng/lib/libsyslog-ng-6.0.2.so
		x="${x%% (*}"
		#AIX:
		#/opt/syslog-ng/lib/libsyslog-ng.a(libsyslog-ng-5.0.14.so)
		#/opt/syslog-ng/lib/libsyslog-ng.a
		x="${x%%(*}"
		[ "${x}" != "/ " ] && [ -f "${x}" ] && echo "$x"
	done
}


acquire_syslog_ldinfo () {
	$lddcmd $syslogrealbin |grep -v needs >${tmpdir}/syslog.ldd
	format_ldd_output <${tmpdir}/syslog.ldd >${tmpdir}/syslog.ldfiles
	for i in `cat ${tmpdir}/syslog.ldfiles ` ; do
		distpkgoffile $i >>${tmpdir}/syslog.ldpkg
	done
	distpkgoffile_cleanup
	sort <${tmpdir}/syslog.ldpkg | uniq >${tmpdir}/syslog.ldpkg.u
	mv ${tmpdir}/syslog.ldpkg.u ${tmpdir}/syslog.ldpkg
	for i in `cat ${tmpdir}/syslog.ldpkg ` ; do
		distpkgstatus $i >>${tmpdir}/syslog.ldinfos
	done
}

acquire_syslog_startup_method () {
	printf "Detecting init system: "
	if [ -d "/run/systemd/system" ]; then
		acquire_syslog_startup_systemdunit
	elif [ -d "/lib/svc/method" ]; then
		acquire_syslog_startup_smf
	else
		acquire_syslog_startup_initscript
	fi
	for i in /etc/default/syslog-ng* ; do
		[ -f "${i}" ] && cp "${i}" "${tmpdir}/sys.startup.default.${i##*/}"
	done
	for i in /etc/sysconfig/syslog-ng* ; do
		[ -f "${i}" ] && cp "${i}" "${tmpdir}/sys.startup.sysconfig.${i##*/}"
	done
}

acquire_syslog_startup_initscript () {
	if [ -n "${initfile}" ]; then
		printf "falling back to SystemV init style...\n"
		cp "${initfile}" "${tmpdir}/sys.startup.init.syslog-ng"
		chmod 0660 "${tmpdir}/sys.startup.init.syslog-ng"
	else
		printf "none.\n"
	fi
}

acquire_syslog_startup_systemdunit () {
	printf "systemd detected...\n"
	systemctl list-units --type=service --plain --no-legend --no-pager --all | grep syslog | grep -vE '(not-found|masked)' | tee "${tmpdir}/sys.startup.systemd-instances" | \
		while read SERVICE LOAD ACTIVE SUB DESCRIPTION ; do output_buffer=$( systemctl show -p FragmentPath ${SERVICE} ) ; echo "${output_buffer##FragmentPath=}" ; done | \
		while read UNITFILE; do SUFFIX=$( echo "${UNITFILE}" | tr / . ); cp "${UNITFILE}" "${tmpdir}/sys.startup.systemd-service${SUFFIX}" ; done
}

acquire_syslog_startup_smf () {
	printf "Solaris SMF detected...\n"
	cp "/lib/svc/method/syslog-ng" "${tmpdir}/sys.startup.svc-method.syslog-ng"
	chmod 0660 "${tmpdir}/sys.startup.svc-method.syslog-ng"
	cp "/var/svc/manifest/system/syslog-ng.xml" "${tmpdir}/sys.startup.svc-manifest.syslog-ng.xml"
	svcs -H system/syslog* >"${tmpdir}/sys.startup.svc-instances"
}

acquire_syslog_all () {
	printf "\nStart Syslog-specific info collection\n"
	acquire_syslog_config
	acquire_syslog_pki_info
	acquire_syslog_pids
	acquire_syslog_info
	acquire_syslog_var
	acquire_syslog_ldinfo
	acquire_syslog_startup_method

}

acquire_syslog_nprv () {
	printf "\nStart Syslog-specific info collection (light)\n"
	acquire_syslog_pids
	acquire_syslog_info
	acquire_syslog_ldinfo
	acquire_syslog_startup_method
}

fhs_set_linux () {
	confdir=/etc/syslog-ng
	vardir=/var/lib/syslog-ng
	piddir=/var/lib/syslog-ng
	syslogbin=/usr/sbin/syslog-ng
	syslogrealbin=/usr/sbin/syslog-ng
}

fhs_set_unix () {
	:
}

rpm_verify () {
	local found=0
	for pkg in "${@}"; do
		if rpm -q "${pkg}" ; then
			rpm -V "${pkg}" && echo "${pkg}: Package files are intact"
			((found+=1))
		fi
	done
	[ ${found} -eq 0 ] && return 1 || return 0
}

### Here comes the linux & distro specific parts

debun_extra_debian () {
	printf "\nDebian specific checks\n"
	printf "Check package files integrity\n"
	cd /
	md5sum --quiet -c /var/lib/dpkg/info/syslog-ng*.md5sums && printf "Package files are intact\n"
	printf "list syslog-related packages\n"
	dpkg -l |grep syslog > ${tmpdir}/deb.packages
}

debun_extra_redhat () {
	printf "\nRedhat specific checks\n"
	printf "Check package files integrity\n"
	rpm_verify syslog-ng-premium-edition syslog-ng-premium-edition-client syslog-ng-premium-edition-compact || \
		printf "No syslog-ng RPM packages have been found!\n"
	printf "list syslog-related packages\n"
	rpm -qa |grep syslog > ${tmpdir}/rpm.packages
}

debun_extra_slackware () {
	printf "\nSlackware Linux specific checks\n"
	printf "list syslog-related packages\n"
	find /var/log/packages -name "*sys*log*" | while read -r FILE; do echo "${FILE##*/}"; done > ${tmpdir}/pkg.packages
}

debun_extra_suse() {
	printf "\nSuSE specific checks\n"
	printf "Check package files integrity\n"
	rpm_verify syslog-ng-premium-edition syslog-ng-premium-edition-client syslog-ng-premium-edition-compact || \
		printf "No syslog-ng RPM packages have been found!\n"
	printf "list syslog-related packages\n"
	rpm -qa | grep syslog > ${tmpdir}/rpm.packages
	#on opensuse "ss utility, iproute2-ss071016" crashes when run with the -punt CLI options
	if is_available netstat; then
		#this info should only be collected if the user has not requested privacy mode
		#we value our customer's sense of privacy
		if [ -z "$privacy_mode" ]; then
			netstat -punt >${tmpdir}/sys.netstat.est.noss
		fi
	fi

}

debun_extra_genlinux () {
	if is_available getenforce; then
		getenforce >"${tmpdir}/sys.selinux"
		${pscmd}Z >"${tmpdir}/sys.ps.selinux"
		semodule -l >"${tmpdir}/sys.selinux.modules"
	else
		echo "No getenforce in path." >"${tmpdir}/sys.selinux"
	fi

	if is_available sysstat ; then
		sysstat -P ALL 1 5 >${tmpdir}/sys.sar.cpu
		sysstat -d 1 5 >${tmpdir}/sys.sar.disk
	elif is_available sar ; then
		sar -P ALL 1 5 >${tmpdir}/sys.sar.cpu
		sar -d 1 5 >${tmpdir}/sys.sar.disk
	fi
	if is_available top; then
		top -b -H -n 1 -c >${tmpdir}/sys.top.threads
	fi

	[ -n "$privacy_mode" ] && return
	if is_available dmidecode; then
		dmidecode >"${tmpdir}/sys.dmidecode"
	fi

	if is_available lspci; then
		lspci >"${tmpdir}/sys.lspci"
	else
		echo "No lspci in path." >"${tmpdir}/sys.lspci"
	fi
	sysctl -a >"${tmpdir}/sys.sysctl.all" 2>/dev/null
	cp /proc/cpuinfo "${tmpdir}/sys.cpuinfo"
}

debun_linux () {
	if [ "$dist" = "Debian" ]; then
		add_extra debun_extra_debian
	elif [ "$dist" = "Ubuntu" ]; then
		add_extra debun_extra_debian
	elif [ "$dist" = "CentOS" ]; then
		add_extra debun_extra_redhat
	elif [ "$dist" = "RedHatEnterpriseServer" ]; then
		add_extra debun_extra_redhat
	elif [ "$dist" = "RHEL" ]; then
		add_extra debun_extra_redhat
	elif [ "$dist" = "OracleServer" ]; then
		add_extra debun_extra_redhat
	elif [ "$dist" = "EnterpriseEnterpriseServer" ]; then
		add_extra debun_extra_redhat
	elif [ "$dist" = "SUSE LINUX" ]; then
		add_extra debun_extra_suse
	elif [ "$dist" = "Slackware" ]; then
		add_extra debun_extra_slackware
	else
		echo "Unknown Distro, perhaps unsupported"
	fi

	add_extra debun_extra_genlinux
}

debun_extra_gensolaris () {
	sysdef >${tmpdir}/sys.sysdef
	kstat >${tmpdir}/sys.kstat
	cp /etc/release ${tmpdir}/sys.release
	if is_available showrev ; then
		showrev >${tmpdir}/sys.showrev
	fi
	if is_available sar ; then
		sar -u 1 5 >${tmpdir}/sys.sar.cpu
		sar -d 1 5 >${tmpdir}/sys.sar.disk
	fi
	if is_available top; then
		top -b -t -n 1 -c >${tmpdir}/sys.top.threads
	fi
}

### Here comes solaris specific parts
debun_solaris () {
	add_extra debun_extra_gensolaris

	pkginfo | grep -i syslog > ${tmpdir}/pkg.packages
}

debun_extra_freebsd() {
	if is_available top; then
		top -b -d1 -H >${tmpdir}/sys.top.threads
	fi
}

debun_freebsd() {
	add_extra debun_extra_freebsd
}

debun_extra_hpux () {
	sysdef >${tmpdir}/sys.sysdef
	swlist >${tmpdir}/sys.swlist

	if is_available sar ; then
		sar -u 1 5 >${tmpdir}/sys.sar.cpu
		sar -d 1 5 >${tmpdir}/sys.sar.disk
	fi
}

debun_hpux () {
	add_extra debun_extra_hpux
}

debun_extra_aix () {
	alog -o -t console >${tmpdir}/sys.console-log

	if is_available sar ; then
		sar -u 1 5 >${tmpdir}/sys.sar.cpu 2>/dev/null
		sar -b 1 5 >${tmpdir}/sys.sar.disk 2>/dev/null1
	fi
}

debun_aix () {
	add_extra debun_extra_aix

	echo "Check package files integrity"
	rpm_verify syslog-ng-premium-edition syslog-ng-premium-edition-client syslog-ng-premium-edition-compact || \
		echo "No syslog-ng RPM packages have been found!"
	echo "list syslog-related packages"
	rpm -qa |grep syslog > ${tmpdir}/rpm.packages
}

detect_env_linux () {
	if is_available lsb_release ; then
		lsb_release -a | tee ${tmpdir}/lsb.all
		dist=`lsb_release -si`
	elif [ -r /etc/debian_version ]; then
		dist="Debian"
	elif [ -r /etc/redhat-release ]; then
		dist="RHEL"
	elif [ -r /etc/slackware-version ]; then
		dist="Slackware"
	elif [ -r /etc/SuSE-release ]; then
		dist="SUSE LINUX"
	fi
}

detect_env () {
	###
	### Detecting syslog-ng ver: ose or pe
	###

	echo "Start environment detection"
	if [ -x /opt/syslog-ng/bin/loggen ] ; then
		syslogfhs=unix
		echo "Unix-like FHS detected"
	elif [ -d /etc/syslog-ng/ ]; then
		syslogfhs=linux
		echo "Linux-type FHS detected"
	else
		syslogfhs=unknown
		confdir=/nonexistent
		echo "No syslog-ng detected"
	fi

	os=`uname -s`
	if [ "$os" = "Linux" ]; then
		detect_env_linux
	fi
}

setup_env_debian () {
	unset distpkgoffile
	unset distpkgstatus
	distpkgoffile () {
		local tmpfile=${tmpdir}/distpkgoffile.$$.tmp
		dpkg -S $1 >$tmpfile
		read x < $tmpfile
		rm $tmpfile
		echo "${x%: /*}"
	}
	distpkgstatus () {
		echo "@@@Package info for: ${1}"
		dpkg -s ${1}
		echo ""
	}
}

setup_env_redhat () {
	unset distpkgoffile
	unset distpkgstatus
	distpkgoffile () {
		local tmpfile=${tmpdir}/distpkgoffile.$$.tmp
		rpm -qf $1 >$tmpfile
		read x < $tmpfile
		rm $tmpfile
		echo "$x"
	}
	distpkgstatus () {
		echo "@@@Package info for: ${1}"
		rpm -qi ${1}
		echo ""
	}
}

setup_env_suse  () {
	unset distpkgoffile
	unset distpkgstatus
	distpkgoffile () {
		local tmpfile=${tmpdir}/distpkgoffile.$$.tmp
		rpm -qf $1 >$tmpfile
		read x < $tmpfile
		rm $tmpfile
		echo "$x"
	}
	distpkgstatus () {
		echo "@@@Package info for: ${1}"
		rpm -qi $1
		echo ""
	}
}

setup_env_slackware  () {
	initfile="/etc/rc.d/rc.syslog"
	service_start="${initfile} start"
	service_stop="${initfile} stop"

	if [ -f "/var/run/syslog-ng.pid" ]; then
		piddir="/var/run"
	fi

	unset service_status
	unset distpkgoffile
	unset distpkgstatus

	distpkgoffile () {
		local LINKTARGET
		local PKGLOGFILENAMES
		local SEARCHSTRING
		PKGLOGFILE=
		LINKTARGET=$( readlink -f "${1}" 2>/dev/null )
		SEARCHSTRING="${LINKTARGET##/}"
		PKGLOGFILENAMES=$( \
			( grep -r -m 1 -E "^${SEARCHSTRING}\$" /var/log/packages ; \
				[ "${SEARCHSTRING%%/*}" = "lib" ] && grep -r -m 1 -E "^lib/incoming/${SEARCHSTRING#*/}\$" /var/log/packages ; \
				[ "${SEARCHSTRING%%/*}" = "lib64" ] && grep -r -m 1 -E "^lib64/incoming/${SEARCHSTRING#*/}\$" /var/log/packages ) | \
			while read -r RESULT; do
				RESULT="${RESULT%%:*}"
				echo "${RESULT##*/}"
			done )
		if [ -n "${PKGLOGFILENAMES}" ]; then
			echo "${PKGLOGFILENAMES}"
		else
			echo "No installed package for '$1' found!" >&2
		fi
	}
	distpkgstatus () {
		local PIVOT
		local LASTLINE
		local PKGINFO
		echo "@@@Package info for: ${1}"
		PKGINFO=$( \
			( find /var/log/packages/ -name "${1}*" ) | \
			while read -r PKGLOGFILE; do
				PIVOT=$( fgrep -n "FILE LIST:" "${PKGLOGFILE}" )
				LASTLINE=$(( ${PIVOT%%:*} - 1 )) 2>/dev/null
				head -n ${LASTLINE:-16} "${PKGLOGFILE}"
				echo ""
			done )
		if [ -n "${PKGINFO}" ]; then
			echo "${PKGINFO}"
		else
			echo "The package '${1}' is not installed, or does not exist!" >&2
		fi
	}
}

setup_env_genlinux () {
	unset myplimit
	myplimit () { [ -f "/proc/$1/limits" ] && cat /proc/$1/limits ; }

	if is_available systemctl ; then
		service_stop="systemctl stop syslog-ng"
		service_start="systemctl start syslog-ng"
		service_status="systemctl status syslog-ng"
	fi

	# sed -E uses extended regexes, and normally works on Linux and BSD, where perl is not present by default.
	# -e and -E patterns use different syntax, but the great thing is that the -E format also works with perl.
	# However older GNU sed versions do not support the -E option, so we have no other choice but perl in
	# these cases.
	if echo "eee" | sed -E 's/eee/fff/g' >/dev/null 2>/dev/null ; then
		:
	else
		sed_equivalent_cmd="perl -p -e"
	fi
}

setup_env_linux () {
	if [ "$dist" = "Debian" ]; then
		setup_env_debian
	elif [ "$dist" = "Ubuntu" ]; then
		setup_env_debian
	elif [ "$dist" = "CentOS" ]; then
		setup_env_redhat
	elif [ "$dist" = "RedHatEnterpriseServer" ]; then
		setup_env_redhat
	elif [ "$dist" = "RHEL" ]; then
		setup_env_redhat
	elif [ "$dist" = "OracleServer" ]; then
		setup_env_redhat
	elif [ "$dist" = "EnterpriseEnterpriseServer" ]; then
		setup_env_redhat
	elif [ "$dist" = "SUSE LINUX" ]; then
		setup_env_suse
	elif [ "$dist" = "Slackware" ]; then
		setup_env_slackware
	else
		echo "Unknown Distro, perhaps unsupported"
	fi

	setup_env_genlinux
}

setup_env_solaris () {
	dfi="df -o i"
	lsof=pfiles
	ipconfig="ifconfig -a"
	pscmd="ps -eaf"
	tcpdumpcmd="snoop"
	tcpdumpopts="-P -q -o"
	pcapifparm="-d"
	trace="truss -r all -w all -u libc:: -f"
	netstatpunt="netstat -n"
	netstatsu="netstat -s"
	grepq="/usr/xpg4/bin/grep -q"
	sed_equivalent_cmd="perl -p -e"

	unset -f mypidof
	unset -f getsyslogpids
	unset -f netstatlunp
	unset -f netstatnlp
	unset -f myplimit
	unset -f topcmd
	unset -f free
	unset -f distpkgoffile
	unset -f distpkgstatus
	unset -f is_available
	unset -f os_hash_helper

	is_available () { which "$1" | $grepq "no $1 in" && return 1 || return 0 ; }
	mypidof () { $pseao pid,comm | while read pid bin ; do [ "$bin" = "$1" ] && echo $pid ; done ; }
	getsyslogpids () { mypidof "${syslogrealbin}" ; }
	netstatnlp () { netstat -na ; }
	netstatlunp () { netstat -P udp -na ; }
	myplimit () { plimit $1 ; }
	free () { prtconf | grep Mem ; printf Pagesize:\  ; pagesize -a ; }
	distpkgoffile () {
		FILE="$1"
		if [ -L "/lib/64" ]; then
			FILE=$( perl -sae '$libarch=readlink("/lib/64"); $filename =~ s/lib\/64/lib\/$libarch/; print "$filename\n";' -- -filename="$1" )
		fi
		pkgchk -l -p $FILE | \
		perl -ne 'if ( /^Referenced by the/ ) { $p=1; } elsif (/:/ or /^$/ ) { $p=0; } elsif ($p) { s/^\s+//; print ; } else { print "FAIL:".$_; }'
	}
	distpkgstatus () {
		echo "@@@Package info for: ${1}"
		pkginfo -l $1
		echo ""
	}

	if is_available top; then
		topcmd ()  { top -b -n 1 -c > "${1}" ; }
	else
		topcmd () { ( uptime ; echo ; echo "::memstat" | mdb -k ; sar -u 1 1 ; echo ; ps -eao user,pid,ppid,pcpu,pmem,vsz,rss,tty,s,stime,args | head -n 1; ps -eao user,pid,ppid,pcpu,pmem,vsz,rss,tty,s,stime,args | grep -v COMMAND | sort -rn +3 ) >"${1}" 2>/dev/null ; }
	fi

	if is_available md5sum; then
		os_hash_helper () {
			find . '!' \( -name debun.manifest -o -name syslog-ng.debun.txt \) -type f -print0 | xargs -0 md5sum ;
		}
	elif is_available digest; then
		os_hash_helper () {
			find . '!' \( -name debun.manifest -o -name syslog-ng.debun.txt \) -type f -exec digest -a md5 -v '{}' \; | sed -e 's:^md5 (\(.*\)) = \([a-z0-9]\{32\}\)$:\2  \1:'
		}
	else
		os_hash_helper () { : ; }
	fi

	if is_available svcadm ; then
		service_stop="svcadm disable system/syslog-ng:default"
		service_start="svcadm enable system/syslog-ng:default"
		service_status="svcs system/syslog-ng:default"
	fi

	if is_available ${opensslcmd}; then
		:
	else
		[ -x /usr/sfw/bin/openssl ] && opensslcmd="/usr/sfw/bin/openssl"
	fi
}

setup_env_freebsd () {
	netstatpunt="netstat -n"
	netstatsu="netstat -s"
	ipconfig="ifconfig -a"
	pseao="ps xao"
	trace="truss -a -d -f -s 256"
	initfile="/etc/rc.d/syslog-ng"
	service_stop="${initfile} stop"
	service_start="${initfile} start"
	service_status="${initfile} status"

	unset -f free
	unset -f netstatnlp
	unset -f netstatlunp
	unset -f mypidof
	unset -f topcmd
	unset -f getsyslogpids
	unset -f distpkgoffile
	unset -f distpkgstatus
	unset -f os_hash_helper

	free () { top -bt 0 ; }
	netstatnlp () { sockstat ; }
	netstatlunp () { netstat -na | grep -E "(Internet|Proto|udp)" ; }
	topcmd ()  { top -b -d1 > "${1}" ; }
	mypidof () { $pseao pid,comm | while read pid bin ; do [ "$bin" = "$1" ] && echo $pid ; done; }
	getsyslogpids () { mypidof syslog-ng ; }
	distpkgoffile () { : ; }
	distpkgstatus () { : ; }
	os_hash_helper () {
		find . '!' \( -name debun.manifest -o -name syslog-ng.debun.txt \) -type f -exec md5 '{}' \; | sed -e 's:^MD5 (\(.*\)) = \([a-z0-9]\{32\}\)$:\2  \1:'
	}
}

setup_env_hpux () {
	gzipcmd="/usr/contrib/bin/gzip -9"
	lddcmd="/usr/ccs/bin/ldd"
	trace="/usr/local/bin/tusc -p -l -u -f"
	netstatsu="netstat -s"
	netstatpunt="netstat -n"
	ipconfig="netstat -ni"
	pscmd="ps -eaf"
	dfh="df"
	initfile="/sbin/init.d/syslog-ng"
	service_stop="${initfile} stop"
	service_start="${initfile} start"
	service_status="${initfile} status"
	sed_equivalent_cmd="perl -p -e"

	unset -f free
	unset -f netstatnlp
	unset -f netstatlunp
	unset -f mypidof
	unset -f topcmd
	unset -f os_hash_helper
	unset -f getsyslogpids
	unset -f getparent
	unset -f getchilds
	unset -f is_available
	unset -f dfk_parser
	unset -f distpkgoffile
	unset -f distpkgstatus
	unset -f distpkgoffile_cleanup

	is_available () { which "$1" | $grepq "no $1 in" && return 1 || return 0 ; }
	free () { swapinfo -tam ; }
	netstatnlp () { netstat -na | grep -E "(Internet|Proto|LISTEN)" ; }
	netstatlunp () { netstat -na | grep -E "(Internet|Proto|udp)" ; }
	topcmd ()  { top -d 1 -f "${1}" ; }
	dfk_parser () { grep free | while read AVAIL REST_TEXT; do echo ${AVAIL}; done }
	os_hash_helper () {
		find . '!' \( -name debun.manifest -o -name syslog-ng.debun.txt \) -type f -exec md5sum '{}' \;
	}
	mypidof () { ps -e -f | while read uid pid ppid c stime tty time command extra ; do
		if [ "${stime%%:*}" = "${stime}" ]
		then
			[ "${extra%% *}" = "$1" ] && echo $pid
		else
			[ "${command%% *}" = "$1" ] && echo $pid
		fi ; done ; }
	getsyslogpids () { mypidof "${syslogrealbin}" ; }
	getparent () {
		local self
		local parent
		local ret
		local tmpfile=${tmpdir}/getparent.$$.txt
		ps -ef  >$tmpfile
		while read user pid ppid dummy ; do
			[ "$1" = "$pid" ] || continue
			ret=$ppid
		done < $tmpfile
		rm $tmpfile
		echo $ret
	}
	getchilds () {
		local childs
		local dummy
		local child
		local tmpfile=${tmpdir}/getchilds.$$.txt
		ps -ef >$tmpfile
		while read user pid ppid dummy ; do
			[ "$1" = "$ppid" ] || continue
			childs="${childs}${childs:+ }$pid"
		done < $tmpfile
		rm $tmpfile
		echo ${childs}
	}

	distpkgoffile () {
		local tmpfile=${tmpdir}/distpkgoffile.tmp
		if [ ! -f $tmpfile ]; then
			swlist -l file > "$tmpfile"
		fi
		grep $1 $tmpfile | cut -d : -f 1 | while read x; do
			echo "$x"
		done
	}
	distpkgstatus () {
		printf "@@@Package info for fileset/patch: %s\n" "$1"
		swlist -l fileset -a title -a description $1 | grep -v "^#" | grep -v "^\""
		printf "\n"
	}
	distpkgoffile_cleanup () {
		if [ -f "${tmpdir}/distpkgoffile.tmp" ]; then
			echo "Removing package list cache"
			rm "${tmpdir}/distpkgoffile.tmp"
		fi
	}
}

setup_env_aix () {
	ipconfig="ifconfig -a"
	pscmd="ps -eaf"
	dfh="df -k"
	netstatsu="netstat -s"
	netstatpunt="netstat -n"
	dmesg="alog -o -t boot"
	trace="truss -r all -w all -u libc:: -f"
	initfile=
	service_stop="/usr/bin/stopsrc -s syslog-ng"
	service_start="/usr/bin/startsrc -s syslog-ng"
	service_status="/usr/bin/lssrc -s syslog-ng"
	sed_equivalent_cmd="perl -p -e"

	unset -f initfile
	unset -f netstatnlp
	unset -f netstatlunp
	unset -f routeconfig
	unset -f free
	unset -f dfk_parser
	unset -f getsyslogpids
	unset -f mypidof
	unset -f topcmd
	unset -f format_ldd_output
	unset -f os_hash_helper

	format_ldd_output () { sed -e 's:^[^/]*\(.*\)$:\1:' -e 's:^\(.*\)(.*)$:\1:'; }
	netstatnlp () { netstat -na | grep -E "(Active|Proto|LISTEN)" ; }
	netstatlunp () { netstat -na | grep -E "(Internet|Proto|udp)" ; }
	dfk_parser () { tail -1 | while read FS ALL AVAIL UPERC IUPERC MP; do echo ${AVAIL}; done }
	routeconfig () { if netstat -nr 2>&1 | $grepq 'Permission error' ; then echo 'WPAR without its own routing table.' ; else netstat -nr ; fi ; }
	free () { svmon -G -O unit=KB ; }
	topcmd () { ( uptime ; svmon -G | head -n 3 ; sar -u 1 1 ; echo ; ps auxwww | head -n 1; ps auxwww | grep -v COMMAND | sort -rn +2 ) >"${1}" 2>/dev/null ; }
	mypidof () { ps -eaf | while read user pid ppid c stime tty time cmd extra; do
		if [ "${stime%%:*}" = "${stime}" ]
		then
			[ "${extra%% *}" = "$1" ] && echo $pid
		else
			[ "${cmd%% *}" = "$1" ] && echo $pid
		fi ; done ; }
	getsyslogpids () { mypidof "${syslogrealbin}" ; }

	unset distpkgoffile
	unset distpkgstatus
	distpkgoffile () {
		local tmpfile=${tmpdir}/distpkgoffile.$$.tmp
		rpm -qf $1 >$tmpfile
		read x < $tmpfile
		rm $tmpfile
		echo "$x"
	}
	distpkgstatus () {
		printf "@@@Package info for: %s\n" "$1"
		rpm -qi $1
		printf "\n"
	}
	os_hash_helper () {
		find . '!' \( -name debun.manifest -o -name syslog-ng.debun.txt \) -type f -exec csum -h MD5 '{}' \;
	}
}

setup_env_generic_pre () {
	:
}

setup_env_generic_post () {
	### Check if ss is available (should only be present on Linux)
	if is_available ss ; then
		unset -f routeconfig
		unset -f netstatnlp
		unset -f netstatlunp

		routeconfig () { ip route show ; }
		netstatnlp () { ss -nlp ; }
		netstatlunp () { ss -lunp ; }
		netstatpunt="ss -punt"
	fi
	if is_available netstat; then
		:
	else
		is_available nstat && netstatsu="nstat"
	fi
}

setup_env() {
	setup_env_generic_pre

	###
	### Decide OS (switch-like)
	###
	printf "\nOperating System Name: %s\n" "$os"
	if [ "$os" = "Linux" ]; then
		setup_env_linux
	elif [ "$os" = "SunOS" ]; then
		setup_env_solaris
	elif [ "$os" = "FreeBSD" ]; then
		setup_env_freebsd
	elif [ "$os" = "HP-UX" ]; then
		setup_env_hpux
	elif [ "$os" = "AIX" ]; then
		setup_env_aix
	else
		printf "Unkonwn or (yet) unhandled system\n"
	fi

	setup_env_generic_post
}

debun_run () {
	if [ "$os" = "Linux" ]; then
		debun_linux
	elif [ "$os" = "SunOS" ]; then
		debun_solaris
	elif [ "$os" = "FreeBSD" ]; then
		debun_freebsd
	elif [ "$os" = "HP-UX" ]; then
		debun_hpux
	elif [ "$os" = "AIX" ]; then
		debun_aix
	fi
}

run_specific_extras () {
	for i in ${extras}; do
		$i
	done
}

run_debug () {
	printf "\nStart Debug collection\n"
	if [ -n "${pcap_params}" ]; then
		if is_available $tcpdumpcmd ; then
			echo "Start packet dump in background with filters: ${pcap_params}"
			${tcpdumpcmd} ${tcpdumpopts} ${tmpdir}/debug.pcap ${pcap_iface:+$pcapifparm} ${pcap_iface} ${pcap_params} &
			pcappid=${!}
		else
			echo "tcpdump/snoop is not available" >&2
		fi
	fi
	if [ -n "${tracing}" ] && [ -z "${debug_params}" ]; then
		if is_available "${trace%% *}"; then
			for i in ${sngallpids}; do
				${trace} -o ${tmpdir}/trace.${i}.txt -p ${i} &
				tracepids="${tracepids}${tracepids:+ }${!}"
			done
		else
			echo "Tracing was requested but ${trace%% *} was not available!"
		fi
	fi
	if [ -n "${waitforit}" ]; then
		[ -n "${pcap_params}" ] && sleep 1
		echo "Waiting ${waitforit} secs before stop system's syslog-ng, and restart in debug mode."
		pad=''
		bs=''
		for i in `seq 1 ${#waitforit}`; do pad="${pad} " ; bs="\b${bs}" ; done
		printf "Start countdown: ${pad}" >&3
		for i in `seq ${waitforit} -1 1 `; do printf "${bs}${pad:${#i}}$i" >&3 ; sleep 1 ; done
		print "0\n">&3
		touch ${tmpdir}/syslog.debug
	fi
	if [ -n "${debug_params}" ]; then
		${service_stop}
		# We should implement a better waiting for the system service's shutdown, sleep 1 works for now
		sleep 1
		echo "Start syslog-ng debug with params: ${debug_params}"
		if [ -n "$tracing" ]; then
			if is_available "${trace%% *}"; then
				${trace} -o ${tmpdir}/trace.dbg.txt ${syslogbin} ${debug_params} >>${tmpdir}/syslog.debug 2>&1 &
				i=${!}
				tracepids="${i}"
				debugpid="`getchilds ${i}`"
				echo "Trace: ${i} Debug: ${debugpid}"
			else
				echo "Tracing was requested but ${trace%% *} was not available!"
				${syslogbin} ${debug_params} >>${tmpdir}/syslog.debug 2>&1 &
				debugpid=${!}
			fi
		else
			${syslogbin} ${debug_params} >>${tmpdir}/syslog.debug 2>&1 &
			debugpid=${!}
		fi
	fi
	[ -n "${timeout}" ] || echo "When you want to stop collecting data, press ENTER" >&3
	if [ -n "${waitforit}" ]; then
		sleep 1
		# Let's gave time the user, to read the message about stopping
		tail -f ${tmpdir}/syslog.debug >&3 &
		debugtailpid=${!}
		#disown
	fi
	if [ -n "${timeout}" ];
	then
		sleep "${timeout}"
	else
		read line
	fi
	}

###
### Main program tasks
###

debun_init
detect_env
setup_env
debun_run
[ "$syslogfhs" = "linux" ] && fhs_set_linux
[ "$syslogfhs" = "unix" ] && fhs_set_unix
run_specific_extras
acquire_general_info
if [ -n "$privacy_mode" ]; then
	acquire_syslog_nprv
else
	acquire_syslog_all
fi
[ -n "$debug_mode" ] && run_debug
debun_final