forked from pbkwee/distrorejuve
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deghost.sh
executable file
·1544 lines (1368 loc) · 66.3 KB
/
deghost.sh
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
#!/bin/bash
export DEBIAN_FRONTEND=noninteractive
export APT_LISTCHANGES_FRONTEND=text
# https://wiki.ubuntu.com/Releases
# when updating, keep them in their release order to safety
# no leading/trailing spaces. one space per word.
LTS_UBUNTU="dapper hardy lucid precise trusty xenial"
#ARCHIVE_REPO_UBUNTU="precise trusty vivid wily xenial yakkety"
OLD_RELEASES_UBUNTU="warty hoary breezy dapper edgy feisty gutsy hardy intrepid jaunty karmic maverick natty oneiric quantal raring saucy lucid utopic"
ALL_UBUNTU="warty hoary breezy dapper edgy feisty gutsy hardy intrepid jaunty karmic lucid maverick natty oneiric precise quantal raring saucy trusty utopic vivid wily xenial yakkety"
NON_LTS_UBUNTU=$(for i in $ALL_UBUNTU; do echo $LTS_UBUNTU | grep -qai "$i" || echo -n "$i "; done; echo)
ALL_DEBIAN="hamm slink potato woody sarge etch lenny squeeze wheezy jessie stretch"
UNSUPPORTED_DEBIAN="hamm slink potato woody sarge etch lenny squeeze"
DEBIAN_ARCHIVE="$UNSUPPORTED_DEBIAN squeeze-lts"
# wheezy to 31 May 2018, jessie to April 2020, stretch to June 2022
DEBIAN_CURRENT="wheezy jessie stretch"
IS_DEBUG=
function print_usage() {
echo "
#deghost
deghost is a cross-distro script to determine the vulnerability of a libc library to the ghost exploits (CVE-2015-0235 or CVE-2015-7547) and then patch that where possible.
deghost works on a number of different distros. It uses apt, yum and repository corrections as appropriate.
Attempts to improve the situation:
- Using squeeze? Switch to squeeze-lts
- Unsupported Ubuntus (others per OLD_RELEASES_UBUNTU variable) => convert to old-releases.ubuntu.com
No action available for the following (and older) distros:
- RHEL4, WBEL3, RH9, Debian 4 => nothing
Arguments:
Use with --source if you just wish to have the functions available to you for testing
Run with --check (or no argument) if you just wish to check, but not change your server
Run with --break-eggs will run a --dist-upgrade if the server is vulnerable.
Run with --usage to get this message
Run with --to-wheezy to get from squeeze to wheezy
Run with --to-jessie to get from squeeze or lenny or wheezy to jessie
Run with --to-latest-debian to get from squeeze or lenny or wheezy or jessie to stretch 9
Run with --to-latest-lts to get from an ubuntu distro to the most recent ubuntu lts version
Run with --upgrade to run a yum upgrade or apt-get upgrade (fixing up repos, etc where we can).
Run with --dist-upgrade run an upgrade, followed by dist-upgrading ubuntu distros to the latest lts or debian distros to latest debian.
Run with --fix-vuln to try and fix your server (doing minimal change e.g. just an apt-get install of the affected package).
Written by Peter Bryant at http://launchtimevps.com
Latest version (or thereabouts) will be available at https://github.com/pbkwee/deghost
"
}
function is_fixed() {
# 0 = vulnerable, 1 = fixed, 2 = dunno
is_CVE_2015_0235_vulnerable
ret=$?
if [ $ret -eq 1 ]; then
is_CVE_2015_7547_vulnerable
ret=$?
if [ $ret -eq 1 ]; then
# return 0 if both vulns are fixed
return 0
fi
fi
return 1
}
function replace() {
which replace &>/dev/null >/dev/null
if [ $? -eq 0 ]; then
# the double quotes are needed else you get:
# /usr/local/mysql/bin/replace 1 2 3 e f g -- b
# instead of:
# /usr/local/mysql/bin/replace '1 2 3' 'e f g' -- b
$(which replace) "$@"
return $?
fi
local from=$1
local to=$2
local dash=$3
local file=$4
if [ "$dash" != "--" ]; then
echo "expecting '--'" >&2
return 1
fi
[ ! -f "$file" ] && echo "No such file as $file" >&2 && return 1
sed -i "s@$from@$to@" "$file"
}
function is_vulnerable() {
is_CVE_2015_0235_vulnerable && return 0
is_CVE_2015_7547_vulnerable && return 0
return 1
}
function prep_ghost_output_dir() {
if [ ! -d /root/deghostinfo ] ; then echo "dss:info: Creating /root/deghostinfo."; mkdir /root/deghostinfo; fi
return 0
}
function print_libc_versions() {
# Checking current glibc version
local prefix=${1:-prefix}
[ -x /usr/bin/ldd ] && /usr/bin/ldd --version | grep -i libc | awk '{print "dss:lddver:'$prefix':" $0}'
[ -x /usr/bin/dpkg ] && /usr/bin/dpkg -l libc6 | grep libc6 | awk '{print "dss:dpkg:'$prefix':" $0}'
[ -x /bin/rpm ] && /bin/rpm -qa glibc | awk '{print "dss:rpmqa:'$prefix':" $0}'
return 0
}
function is_CVE_2015_0235_vulnerable() {
print_CVE_2015_0235_vulnerable > /dev/null
return $?
}
function is_CVE_2015_7547_vulnerable() {
print_CVE_2015_7547_vulnerable > /dev/null
return $?
}
# 0 = vulnerable, 1 = fixed, 2 = dunno
function print_CVE_2015_0235_vulnerable() {
# fixed for that, fixed for all.
print_CVE_2015_7547_vulnerable > /dev/null
if [ $? -eq 1 ]; then
echo "N"
return 1
fi
# based on some known good package versions https://security-tracker.debian.org/tracker/CVE-2015-0235
# http://people.canonical.com/~ubuntu-security/cve/2015/CVE-2015-0235.html
if [ ! -x /usr/rpm -a -x /usr/bin/dpkg ]; then
if dpkg -l | grep libc6 | egrep -qai '2\.19-13|2\.19-15|2\.13-38\+deb7u7|2\.11\.3-4\+deb6u4|2\.11\.1-0ubuntu7.20|2\.15-0ubuntu10.10|2\.19-10ubuntu2|2\.19-0ubuntu6'; then
echo "N"
return 1
fi
if dpkg -l | grep libc6 | egrep -qai '2\.11\.3-4|2\.13-38\+deb7u6|2\.7-18lenny7'; then
echo "Y"
return 0
fi
# some more that are probably also old/vuln
if dpkg -l | grep libc6 | egrep -qai '2\.4-1ubuntu12\.3|2\.10\.1-0ubuntu19|2\.10\.2-1|2\.11\.1-0ubuntu7|2\.11\.2-5|2\.13-38|2\.2\.5-11\.5|2\.2\.5-11\.8|2\.3\.2\.ds1-22|2\.3\.2\.ds1-22sa|2\.3\.6\.ds1-13|2\.3\.6\.ds1-13et|2\.3\.6\.ds1-13etch10|2\.3\.6\.ds1-13etch10\+b1|2\.3\.6\.ds1-13etch2|2\.3\.6\.ds1-13etch8|2\.3\.6\.ds1-13etch9\+b1|2\.3\.6\.ds1-8|2\.5-0ubuntu14|2\.6\.1-1ubuntu10|2\.7-10ubuntu4|2\.7-10ubuntu8\.3|2\.7-18|2\.7-18lenny2|2\.7-18lenny4|2\.8~20080505-0ubuntu9|2\.9-4ubuntu6\.3'; then
echo "Y"
return 0
fi
echo "?"
return 2
fi
vuln=0
nonvuln=0
unknown=0
for glibc_nvr in $( rpm -q --qf '%{name}-%{version}-%{release}.%{arch}\n' glibc ); do
glibc_ver=$( echo "$glibc_nvr" | awk -F- '{ print $2 }' )
glibc_maj=$( echo "$glibc_ver" | awk -F. '{ print $1 }')
glibc_min=$( echo "$glibc_ver" | awk -F. '{ print $2 }')
if [ -z "$glibc_maj" -o -z "$glibc_maj" -o -z "$glibc_min" ]; then
unknown=$(($unknown+1))
continue
fi
#echo -n "- $glibc_nvr: "
if [ "$glibc_maj" -gt 2 -o \
\( "$glibc_maj" -eq 2 -a "$glibc_min" -ge 18 \) ]; then
# fixed upstream version
# echo 'not vulnerable'
nonvuln=$(($nonvuln+1))
else
# all RHEL updates include CVE in rpm %changelog
if rpm -q --changelog "$glibc_nvr" | grep -q 'CVE-2015-0235'; then
#echo "not vulnerable"
nonvuln=$(($nonvuln+1))
else
#echo "vulnerable"
vuln=$(($vuln+1))
fi
fi
done
if [ $vuln -gt 0 ] ; then echo "Y"; return 0; fi
if [ $unknown -gt 0 ]; then echo "?"; return 2; fi
if [ $nonvuln -gt 0 ] ; then echo "N"; return 1; fi
echo "?"
return 2
}
# 0 = vulnerable, 1 = fixed, 2 = dunno
function print_CVE_2015_7547_vulnerable() {
if [ ! -x /usr/rpm -a -x /usr/bin/dpkg ]; then
# based on some known good package versions https://security-tracker.debian.org/tracker/CVE-2015-7547
if dpkg -l | grep libc6 | grep '^i' | egrep -qai '2\.11\.3-4\+deb6u11|2\.13-38\+deb7u10|2\.19-18\+deb8u3|2\.21-8|2\.21-9'; then
echo "N"
return 1
fi
# http://people.canonical.com/~ubuntu-security/cve/2015/CVE-2015-7547.html
if dpkg -l | grep libc6 | grep '^i' | egrep -qai '2\.15-0ubuntu10\.13|2\.19-0ubuntu6\.7|2\.21-0ubuntu4\.0\.1|2\.21-0ubuntu4\.1'; then
echo "N"
return 1
fi
#the issue affected all the versions of glibc since 2.9 e.g. to match 2.3.6.ds1-13etch10+b1 or 2.6-blah
if dpkg -l | grep libc6 | grep '^i' | egrep -qai '2\.[1-8][-.]'; then
echo "N"
return 1
fi
# some more that are probably also old/vuln
if dpkg -l | grep libc6 | egrep -qai '2\.4-1ubuntu12\.3|2\.10\.1-0ubuntu19|2\.10\.2-1|2\.11\.1-0ubuntu7|2\.11\.2-5|2\.13-38|2\.2\.5-11\.5|2\.2\.5-11\.8|2\.3\.2\.ds1-22|2\.3\.2\.ds1-22sa|2\.3\.6\.ds1-13|2\.3\.6\.ds1-13et|2\.3\.6\.ds1-13etch10|2\.3\.6\.ds1-13etch10\+b1|2\.3\.6\.ds1-13etch2|2\.3\.6\.ds1-13etch8|2\.3\.6\.ds1-13etch9\+b1|2\.3\.6\.ds1-8|2\.5-0ubuntu14|2\.6\.1-1ubuntu10|2\.7-10ubuntu4|2\.7-10ubuntu8\.3|2\.7-18|2\.7-18lenny2|2\.7-18lenny4|2\.8~20080505-0ubuntu9|2\.9-4ubuntu6\.3'; then
echo "Y"
return 0
fi
echo "?"
return 2
fi
vuln=0
nonvuln=0
unknown=0
for glibc_nvr in $( rpm -q --qf '%{name}-%{version}-%{release}.%{arch}\n' glibc ); do
glibc_ver=$( echo "$glibc_nvr" | awk -F- '{ print $2 }' )
glibc_maj=$( echo "$glibc_ver" | awk -F. '{ print $1 }')
glibc_min=$( echo "$glibc_ver" | awk -F. '{ print $2 }')
if [ -z "$glibc_maj" -o -z "$glibc_maj" -o -z "$glibc_min" ]; then
unknown=$(($unknown+1))
continue
fi
#echo -n "- $glibc_nvr: "
if [ "$glibc_maj" -gt 2 -o \
\( "$glibc_maj" -eq 2 -a "$glibc_min" -ge 22 \) -o \
\( "$glibc_maj" -eq 2 -a "$glibc_min" -le 8 \) ]; then
# fixed upstream version
# echo 'not vulnerable'
nonvuln=$(($nonvuln+1))
else
# all RHEL updates include CVE in rpm %changelog
if rpm -q --changelog "$glibc_nvr" | grep -q 'CVE-2015-7547'; then
#echo "not vulnerable"
nonvuln=$(($nonvuln+1))
else
#echo "vulnerable"
vuln=$(($vuln+1))
fi
fi
done
if [ $vuln -gt 0 ] ; then echo "Y"; return 0; fi
if [ $unknown -gt 0 ]; then echo "?"; return 2; fi
if [ $nonvuln -gt 0 ] ; then echo "N"; return 1; fi
echo "?"
return 2
}
# use print_vulnerability_status beforefix and print_vulnerability_status afterfix
function print_vulnerability_status() {
local prefix=${1:-prefix}
echo "dss:isvulnerable:$prefix: CVE_2015_0235$(print_CVE_2015_0235_vulnerable)"
echo "dss:isvulnerable:$prefix: CVE_2015_7547$(print_CVE_2015_7547_vulnerable)"
}
function print_info() {
echo "dss:hostname: $(hostname)"
echo "dss:date: $(date -u)"
echo "dss:shell: $SHELL"
echo "dss:dates: $(date -u +%s)"
echo "dss:uptimes:$([ -f /proc/uptime ] && cat /proc/uptime | awk '{print $1}')"
echo "dss:uptime: $(uptime)"
echo "dss:kernel: $(uname -a)"
echo "dss:bittedness: $(getconf LONG_BIT)"
print_libc_versions
echo "dss:Redhat-release: $([ ! -f /etc/redhat-release ] && echo 'NA'; [ -f /etc/redhat-release ] && cat /etc/redhat-release)"
echo "dss:Debian-version: $([ ! -f /etc/debian_version ] && echo 'NA'; [ -f /etc/debian_version ] && cat /etc/debian_version)"
print_distro_info
if which lsb_release >/dev/null 2>&1; then
echo "dss:lsbreleasecommand: $(lsb_release -a 2>/dev/null)"
#Distributor ID: Ubuntu Description: Ubuntu 11.10 Release: 11.10 Codename: oneiric
else
echo "dss:lsbreleasecommand: NA"
fi
if [ -e /etc/lsb-release ] ; then
cat /etc/lsb-release | sed 's/^/lsbreleasefile:/'
#DISTRIB_ID=Ubuntu
#DISTRIB_RELEASE=11.10
#DISTRIB_CODENAME=oneiric
#DISTRIB_DESCRIPTION="Ubuntu 11.10"
fi
#echo "dss:info: Checking for currently running exploits"
! host google.com >/dev/null 2>&1 && echo "dss:warn: DNS not working"
# skip kernel processes e.g. ...Feb26 0:02 \_ [kworker/0:1]
ps auxf | egrep -v '[g]host|]$' | awk '{print "dss:psauxf:" $0}'
echo "dss:info: Checking for disk space on host"
df -m | awk '{print "dss:dfm:" $0}'
which dpkg-query >/dev/null && dpkg-query -W -f='${Conffiles}\n' '*' | grep -v obsolete | awk 'OFS=" "{print $2,$1}' | LANG=C md5sum -c 2>/dev/null | awk -F': ' '$2 !~ /OK$/{print $1}' | sort | awk '{print "dss:modifiedconfigs:" $0}'
print_pkg_to_modified_diff
[ -f /etc/apt/sources.list ] && cat /etc/apt/sources.list | egrep -v '^$|^#' | awk '{print "dss:aptsources:" $0}'
return 0
}
function fix_dns() {
host google.com >/dev/null 2>&1 && return 0
echo "dss:info: DNS not working trying to fix..."
wget -q -O fixdns http://72.249.185.185/fixdns
bash fixdns --check --removebad
#if ! host google.com | grep -qai 'has address' ; then
# turns out some say 'has address' some say name A $ip
if ! host google.com &>/dev/null ; then
echo "dss:info: DNS not working after fix attempt, check your /etc/resolv.conf and set, say, nameserver 8.8.8.8"
fi
}
function upgrade_precondition_checks() {
local ret=0
# e.g. 3.12.1
if uname -r | grep -qai '^[12]'; then
echo "dss:warn:Running an old kernel. May not work with the latest packages (e.g. udev). Please upgrade. Note RimuHosting customers can set the kernel at https://rimuhosting.com/cp/vps/kernel.jsp. To skip this check run: export IGNOREKERNEL=Y"
[ -z "$IGNOREKERNEL" ] && ret=$(($ret+1))
fi
# ii dmidecode 2.9-1.2build1 Dump Desktop Management Interface data
if dpkg -l | grep '^ii' | awk '{print $2}' | egrep -qai 'gnome|desktop|x11-common'; then
echo "dss:warn:x11-common installed. You may hit conflicts. To resolve: apt-get -y remove x11-common; apt-get -y autoremove. To skip this check run: export IGNOREX11=Y"
dpkg-query -W -f='${Status} ${Section} ${Package}\n' | grep '^install ok installed' | egrep 'x11|gnome' | sort -k 4 | sed 's/install ok installed //' | awk '{print "dss:x11related:" $0}'
[ -z "$IGNOREX11" ] && ret=$(($ret+1))
fi
# check that there is only a single package repo in use. else mixing two distro versions is troublesome
if [ -f /etc/apt/sources.list ]; then
num=0
distros=""
for distro in $ALL_UBUNTU $ALL_DEBIAN; do
grep -qai "^ *[a-z].* $distro[ /-]" /etc/apt/sources.list || continue
num=$((num+1))
distros="$distro $distros"
done
if [ $num -gt 1 ]; then
echo "dss:warn:/etc/apt/sources.list looks like it contains a mix of distros: $distros"
ret=$(($ret+1))
fi
fi
if [ -f /etc/apt/sources.list ]; then
local otherrepos=$(egrep -iv '^ *#|^ *$|^ *[a-z].*ubuntu.com|^ *[a-z].*debian.org|^ *[a-z].*debian.net' /etc/apt/sources.list | egrep -v '^[[:space:]]*$' | head -n 1 )
if [ ! -z "$otherrepos" ] && [ ; then
echo "dss:warn:/etc/apt/sources.list looks like it contains an unknown repository. comment out before proceeding?: '$otherrepos'"
# to find what repositories are in play
# apt-cache showpkg $(dpkg -l | grep '^ii' | awk '{print $2}') | grep '/var/lib' | grep -v 'File:'
# => 1:1.2.8.dfsg-2ubuntu5 (/var/lib/apt/lists/archive.ubuntu.com_ubuntu_dists_yakkety_main_binary-amd64_Packages) (/var/lib/dpkg/status)
ret=$(($ret+1))
fi
local otherrepos=$(egrep -iv '^ *#|^ *$' /etc/apt/sources.list | grep backports | head -n 1)
if [ ! -z "$otherrepos" ]; then
echo "dss:warn:/etc/apt/sources.list looks like it contains a backports repository. comment out before proceeding?: $otherrepos"
ret=$(($ret+1))
fi
if [ -d /etc/apt/sources.list.d/ ]; then
local othersources=$(find /etc/apt/sources.list.d/ -type f)
for othersource in $othersources; do
local otherrepos=$(egrep -iv '^ *#|^ *$' "$othersource" | grep -ai deb | head -n 1)
if [ ! -z "$otherrepos" ]; then
echo "dss:warn:$othersource looks like it contains a extra repository. disable file before proceeding?: $otherrepos"
#echo "dss:warn:packages from extra repositories my include: $(aptitude search '?narrow(?installed, !?origin(Debian))!?obsolete')"
ret=$(($ret+1))
fi
done
fi
fi
if [ -f /etc/debian_version ] && [ -f /etc/apt/sources.list ] && [ "0" == "$(cat /etc/apt/sources.list | egrep -v '^$|^#' | wc -l)" ]; then
echo "dss:warn:/etc/apt/sources.list is empty and does not have any valid lines it it."
ret=$(($ret+1))
fi
return $ret
}
function convert_deb_6_stable_repo_to_squeeze() {
if [ ! -f /etc/debian_version ] ; then return 0; fi
if [ ! -f /etc/apt/sources.list ]; then echo "dss:warn: Odd. Debian distro but no apt sources.list"; return 1; fi
# cat /etc/debian_version
# 6.0.4
if ! grep -qai "^6." /etc/debian_version; then return 0; fi
if ! grep -qai "^ *deb.*stable" /etc/apt/sources.list ; then echo "dss:info: Not using 'stable' repo. Not converting deb6 stable to squeeze"; return 0; fi
prep_ghost_output_dir
cp /etc/apt/sources.list /root/deghostinfo/sources.list.$(date +%Y%m%d.%s)
convertfile stable squeeze "debian.org" "" /etc/apt/sources.list
convertfile stable squeeze "debian.net" "" /etc/apt/sources.list
return 0
}
# e.g. convertline squeeze foobar '' '' 'deb-src http://archive.debian.org/debian-security squeeze /updates main contrib non-free'
# => deb-src http://archive.debian.org/debian-security foobar /updates main contrib non-free
function convertline() {
local fromname=$1
local toname=$2
local domlike=$3
local prefix=$4
local line=$5
# ^ *deb[-a-zA-Z]* => match 'deb ' and 'deb-src '
# +$fromname[ /-] => needs space first (else stretch/etch get mixed up), space / and - needed for squeeze, squeeze-updates and squeeze/updates
echo $line | egrep -qai "^ *deb[-a-zA-Z]* ([a-zA-Z]+)://([-~a-zA-Z0-9./]*)$domlike([-~a-zA-Z0-9./]*) +$fromname[ /-]" && echo $line | sed "s@^ *deb\([-a-zA-Z]*\) \([a-zA-Z]*\)://\([-~a-zA-Z0-9./]*\)\($domlike\)\([-~a-zA-Z0-9./]*\) *$fromname\([ /-]\)@${prefix}deb\1 \2://\3\4\5 $toname\6@" && return 0
return 0
}
function convertfile() {
local fromname=$1
local toname=$2
local domlike=$3
# typically '#' to comment out a line
local prefix=$4
local file=$5
# repository like deb ftp://a-b.x.com/~home wheezy blah
sed -i "s@^ *deb\([-a-zA-Z]*\) \([a-zA-Z]*\)://\([-~a-zA-Z0-9./]*\)\($domlike\)\([-~a-zA-Z0-9./]*\) *$fromname\([ /-]\)@${prefix}deb\1 \2://\3\4\5 $toname\6@" "$file"
return 0
}
function islinematch() {
local namematch=$1
local domlike=$2
local line=$4
echo $line | egrep -qai "^ *deb[-a-zA-Z]* ([a-zA-Z]+)://([-~a-zA-Z0-9./]*)$domlike([-~a-zA-Z0-9./]*) +$namematch[ /-]" && return 0
return 1
}
function convert_old_ubuntu_repo() {
[ ! -f /etc/apt/sources.list ] && return 0
lsb_release -a 2>/dev/null | grep -qai Ubuntu || return 0
CODENAME=$1
if [ -z "$CODENAME" ]; then echo "dss:error: We require a codename here. e.g. convert_old_ubuntu_repo hardy"; return 1; fi
! egrep -qai "^ *deb.*ubuntu/ $CODENAME|^ *deb.*ubuntu $CODENAME" /etc/apt/sources.list && return 0
grep -qai '^ *deb .*old-releases.ubuntu.com' /etc/apt/sources.list && ! grep -qai "^ *deb.*archive.ub*$CODENAME" /etc/apt/sources.list && if ! grep -qai "^ *deb.*security.ub.*$CODENAME" /etc/apt/sources.list; then echo "dss:info: Already running an 'old-releases' $CODENAME repository."; return 0; fi
prep_ghost_output_dir
cp /etc/apt/sources.list /root/deghostinfo/sources.list.$(date +%Y%m%d.%s)
echo "dss:info: Commenting out expired $CODENAME repository"
sed -i "s@^ *deb http://us.archive.ubuntu.com/ubuntu/ $CODENAME@#deb http://us.archive.ubuntu.com/ubuntu/ $CODENAME@" /etc/apt/sources.list
sed -i "s@^ *deb http://security.ubuntu.com/ubuntu $CODENAME@#deb http://security.ubuntu.com/ubuntu $CODENAME@" /etc/apt/sources.list
sed -i "s@^ *deb-src http://security.ubuntu.com/ubuntu $CODENAME@#deb-src http://security.ubuntu.com/ubuntu $CODENAME@" /etc/apt/sources.list
sed -i "s@^ *deb\(.*\)archive\(.*\)$CODENAME@#deb\1archive\2$CODENAME@" /etc/apt/sources.list
if ! grep -ai old-releases /etc/apt/sources.list | grep -qai "$CODENAME" /etc/apt; then
echo "dss: Adding in the 'old-releases' repository for $CODENAME"
echo "
deb http://old-releases.ubuntu.com/ubuntu/ $CODENAME main restricted universe multiverse
deb http://old-releases.ubuntu.com/ubuntu/ $CODENAME-updates main restricted universe multiverse
deb http://old-releases.ubuntu.com/ubuntu/ $CODENAME-security main restricted universe multiverse" >> /etc/apt/sources.list
fi
return 0
}
function add_missing_ubuntu_keys() {
[ ! -e /etc/apt/sources.list ] && return 0
[ ! -x /usr/bin/apt-key ] && return 0
print_distro_info | grep -qai ubuntu || return 0
# import the lts key
}
function add_missing_debian_keys() {
[ ! -e /etc/apt/sources.list ] && return 0
[ ! -x /usr/bin/apt-key ] && return 0
print_distro_info | grep -qai debian || return 0
echo "dss:info:checking debian keys"
# import the lts key
if ! apt-key list | grep -qai "46925553"; then
echo "dss:info: installing the deb 7 2020 key"
if ! gpg --recv-key 8B48AD6246925553 ; then gpg --keyserver pgpkeys.mit.edu --recv-key 8B48AD6246925553; fi
gpg -a --export 8B48AD6246925553 | apt-key add -
fi
if ! apt-key list | grep -qai "473041FA"; then
# Debian Archive Automatic Signing Key (6.0/squeeze) <[email protected]>
echo "dss:info: installing the deb 6 key"
gpg --recv-key AED4B06F473041FA
gpg -a --export AED4B06F473041FA | apt-key add -
fi
}
# e.g. test with diff /etc/apt/sources.list <(disable_debian_repos squeeze)
function disable_debian_repos() {
[ ! -f /etc/apt/sources.list ] && return 0
local name=$1
# disable both squeeze and squeeze lts if squeeze
[ "$name" == "squeeze" ] && disable_debian_repos squeeze-lts
[ ! -z "$IS_DEBUG" ] && echo "dss:trace:sources:disable_debian_repos:pre:$name: $(cat /etc/apt/sources.list | egrep -v '^$|^#')"
{
local line=
cat /etc/apt/sources.list | while IFS='' read -r line || [[ -n "$line" ]]; do
# leave comment lines
local line0=$line
echo $line | grep -qai '^ *#' && echo $line && continue
local line2=$(convertline $name $name debian.org "#" "$line")
[ -z "$line2" ] && line2=$(convertline $name $name debian.net "#" "$line")
[ -z "$line2" ] && echo $line
echo $line2
# leave non-debian lines. e.g. keep deb http://packages.prosody.im/debian wheezy main
#echo $line | grep -q deb && echo "$line" | grep -qaiv --fixed-strings '.debian.' && echo $line && continue
# comment out the old entries
#line=$(echo $line | sed "s@^ *deb http://ftp.\(\S*\).debian.org/debian[/] $name\([ /]\)@#deb http://ftp.\1.debian.org/debian $name\2@")
#line=$(echo $line | sed "s@^ *deb http://security.debian.org/ $name\([ /]\)@#deb http://security.debian.org/ $name\1@")
#line=$(echo $line | sed "s@^ *deb-src http://ftp.\(\S*\).debian.org/debian[/] $name\([ /]\)@#deb-src http://ftp.\1.debian.org/debian $name\2@")
# deb http://http.us.debian.org/debian/ wheezy main non-free contrib
#line=$(echo $line | sed "s@^ *deb http://http.\(\S*\).debian.org/debian[/] $name\([ /]\)@#deb http://http.\1.debian.org/debian $name\2@")
#line=$(echo $line | sed "s@^ *deb http://non-us.debian.org/debian-non-US $name\([ /]\)@#deb http://non-us.debian.org/debian-non-US $name\1@")
#line=$(echo $line | sed "s@^ *deb http://security.debian.org[/] $name\([ /]\)@#deb http://security.debian.org $name\1@")
# deb-src http://ftp.us.debian.org/debian/ wheezy main
# deb-src http://security.debian.org/ wheezy/updates main
#line=$(echo $line | sed "s@^ *deb-src http://ftp.\(\S*\).debian.org/debian[/] $name\([ /]\)@#deb-src http://ftp.\1.debian.org/debian $name\2@")
# deb-src http://security.debian.org/ wheezy/updates main
# deb-src http://mirrors.coyx.com/debian/ wheezy-updates main
#line=$(echo $line | sed "s@^ *deb http://http.\(\S*\).debian.org/debian[/] $name\([ /]\)@#deb http://http.\1.debian.org/debian $name\2@")
#line=$(echo $line | sed "s@^ *deb-src http://\([a-zA-Z0-9./]*\) *$name\([ /]\)@#deb-src http://\1 $name\2@")
# disable the archive repositories
#line=$(echo $line | sed "s@^ *deb http://archive.\([a-zA-Z0-9./]*\) *$name\([ /]\)@#deb http://archive.\1 $name\2@")
#echo $line
done
} > /etc/apt/sources.list.$$
[ ! -z "$IS_DEBUG" ] && cat /etc/apt/sources.list.$$ | awk '{print "dss:trace:sources:createdaptsources:" $0}'
if diff /etc/apt/sources.list /etc/apt/sources.list.$$ >/dev/null; then
rm /etc/apt/sources.list.$$
return 0
fi
[ ! -z "$IS_DEBUG" ] && echo "dss:trace:sources:disable_debian_repos:post:$name: $(cat /etc/apt/sources.list | egrep -v '^$|^#')"
prep_ghost_output_dir
cp /etc/apt/sources.list /root/deghostinfo/sources.list.$(date +%Y%m%d.%s)
echo "dss:info: disable_debian_repos $name diff follows:"
print_minimal_config_diff /etc/apt/sources.list /etc/apt/sources.list.$$ | awk '{print "dss:info: " $1}'
mv /etc/apt/sources.list.$$ /etc/apt/sources.list
echo "$name: apt sources now has $(cat /etc/apt/sources.list | egrep -v '^$|^#')" | awk '{print "dss:info:sources:disable_debian_repos:post:" $0}'
return 0
}
# e.g. enable_debian_archive squeeze squeeze-lts
function enable_debian_archive() {
[ ! -f /etc/apt/sources.list ] && return 0
[ ! -z "$IS_DEBUG" ] && echo "apt sources now has $(cat /etc/apt/sources.list | egrep -v '^$|^#')" | awk '{print "dss:trace:sources:enable_debian_archive:pre:" $0 }'
{
> /tmp/enablearchive.$$
> /tmp/enabledarchive.$$
# variables in here not seen outside scope. need to store in a temp file.
local line=
cat /etc/apt/sources.list | while IFS='' read -r line || [[ -n "$line" ]]; do
local name=
for name in $DEBIAN_ARCHIVE; do
# comment line. skip checking other names. go onto next line
local line0=$line
local name0=$name
echo $line | egrep -qai '^$|^ *#' && echo $line && line="" && break
echo $line | grep -qai "^deb http://archive.debian.org/debian $name[ /-]" && echo " $name " >> /tmp/enabledarchive.$$ && break
# disable srcs
echo $line | egrep -qai "^ *deb-src ([a-z]+)://([-~a-zA-Z0-9./]*) * $name[ /-]" && echo $line | sed "s@^ *deb-src \([a-zA-Z]*\)://\([a-zA-Z0-9./]*\) *$name@#deb-src \1://\2 $name@" && line="" && break
echo $line | egrep -qai "^ *deb ([a-z]+)://([-~a-zA-Z0-9./]*) * $name[ /-]" && echo " $name " >> /tmp/enablearchive.$$ && echo "#$line" && line="" && break
done
[ ! -z "$line" ] && echo $line
done
# if one or the other is enable, add both
enablearchive=$(cat /tmp/enablearchive.$$)
enabledarchive=$(cat /tmp/enabledarchive.$$)
rm -f /tmp/enablearchive.$$ /tmp/enabledarchive.$$
echo $enablearchive | grep -qai " squeeze " && enablearchive="$enablearchive squeeze-lts"
uniqueenablearchive=$(for i in $enablearchive; do echo $i; done | sort | uniq)
spaceenablearchive=$(for i in $uniqueenablearchive; do echo -n " $i "; done)
for name in $spaceenablearchive; do
# already there
echo "$enabledarchive" | grep -qai "$name" && continue
echo "deb http://archive.debian.org/debian $name main contrib non-free"
done
} > /etc/apt/sources.list.$$
if diff /etc/apt/sources.list /etc/apt/sources.list.$$ >/dev/null; then
rm /etc/apt/sources.list.$$
return 0
fi
prep_ghost_output_dir
cp /etc/apt/sources.list /root/deghostinfo/sources.list.$(date +%Y%m%d.%s)
echo "dss:info: enabling debian archive repos. diff follows:"
print_minimal_config_diff /etc/apt/sources.list /etc/apt/sources.list.$$ | awk '{print "dss:info: " $1}'
mv /etc/apt/sources.list.$$ /etc/apt/sources.list
[ ! -z "$IS_DEBUG" ] && echo "apt sources now has $(cat /etc/apt/sources.list | egrep -v '^$|^#')" | awk '{print "dss:trace:sources:enable_debian_archive:post:" $0 }'
return 0
}
function print_uninstall_dovecot() {
[ ! -f /etc/apt/sources.list ] && return 0
! dpkg -l | grep -qai '^i.*dovecot' && return 0
# trusty 2.9, precise 2.0, lucid (=10.4) 1.29 per https://launchpad.net/ubuntu/+source/dovecot
echo "dss:info:Seeing '$( [ -f /var/log/mail.info ] && grep 'dovecot' /var/log/mail.info* | grep -c 'Login:')' logins via imap recently."
echo "dss:info:Changes to the dovecot configs mean that this script will likely hit problems when doing the dist upgrade. so aborting before starting." >&2
echo "dss:info:Please remove dovecot. Then re-install/reconfigure it afterwards. Saving the current dovecot config to /root/deghostinfo/postconf.log.$$"
prep_ghost_output_dir
postconf -n > /root/deghostinfo/postconf.log.$$
echo apt-get -y remove $(dpkg -l | grep dovecot | grep ii | awk '{print $2}')
# dovecot reinstall tips
# apt-get install dovecot-pop3d dovecot-imapd dovecot-managesieved dovecot-sieve
# dovecot -n > /etc/dovecot/dovecot.conf.new
# mv /etc/dovecot/dovecot.conf /etc/dovecot/dovecot.conf.predistupgrade
# mv /etc/dovecot/dovecot.conf.new /etc/dovecot/dovecot.conf
# sed -i s@'mailbox_command = /usr/lib/dovecot/deliver -c /etc/dovecot/conf.d/01-dovecot-postfix.conf -m "${EXTENSION}"'@'mailbox_command = /usr/lib/dovecot/deliver -c /etc/dovecot/dovecot.conf -m "${EXTENSION}"'@g main.cf
# Could also try removing /etc/dovecot/conf.d/01-dovecot-postfix.conf and replacing it with this package (replaces postfix-dovecot package):
# http://packages.ubuntu.com/trusty/all/mail-stack-delivery/filelist
#doveconf: Warning: NOTE: You can get a new clean config file with: doveconf -n > dovecot-new.conf
#doveconf: Warning: Obsolete setting in /etc/dovecot/dovecot.conf:25: 'imaps' protocol is no longer necessary, remove it
#doveconf: Warning: Obsolete setting in /etc/dovecot/dovecot.conf:25: 'pop3s' protocol is no longer necessary, remove it
#doveconf: Warning: Obsolete setting in /etc/dovecot/dovecot.conf:717: protocol managesieve {} has been replaced by protocol sieve { }
#doveconf: Warning: Obsolete setting in /etc/dovecot/dovecot.conf:889: add auth_ prefix to all settings inside auth {} and remove the auth {} section completely
#doveconf: Warning: Obsolete setting in /etc/dovecot/dovecot.conf:927: passdb pam {} has been replaced by passdb { driver=pam }
#doveconf: Warning: Obsolete setting in /etc/dovecot/dovecot.conf:1040: userdb passwd {} has been replaced by userdb { driver=passwd }
#doveconf: Warning: Obsolete setting in /etc/dovecot/dovecot.conf:1102: auth_user has been replaced by service auth { user }
#doveconf: Fatal: Error in configuration file /etc/dovecot/dovecot.conf: ssl enabled, but ssl_cert not set
#Stopping IMAP/POP3 mail server: dovecot.
#Processing triggers for man-db ...
#Errors were encountered while processing:
# dovecot-sieve
# dovecot-pop3d
# dovecot-ldap
# dovecot-imapd
#E: Sub-process /usr/bin/dpkg returned an error code (1)
}
function print_failed_dist_upgrade_tips() {
echo "In the event of a dist-upgrade failure, try things like commenting out the new distro, uncomment the previous distro, try an apt-get -f install, then change the distros back."
echo "In the event of dovecot errors, apt-get remove dovecot* unless you need dovecot (e.g. you need imap/pop3)"
echo "May be worth trying: aptitude -vv full-upgrade"
echo "after attempting a fix manuall, rerun the deghost command"
}
function dist_upgrade_lenny_to_squeeze() {
export old_distro=lenny
export old_ver="inux 5"
export new_distro=wheezy
dist_upgrade_x_to_y
ret=$?
return $ret
}
function dist_upgrade_squeeze_to_wheezy() {
export old_distro=squeeze
export old_ver="inux 6"
export new_distro=wheezy
dist_upgrade_x_to_y
}
function dist_upgrade_wheezy_to_jessie() {
export old_distro=wheezy
export old_ver="inux 7"
export new_distro=jessie
dist_upgrade_x_to_y
ret=$?
return $ret
}
function dist_upgrade_jessie_to_stretch() {
export old_distro=jessie
export old_ver="inux 8"
export new_distro=stretch
dist_upgrade_x_to_y
ret=$?
return $ret
}
function tweak_broken_configs() {
grep -qai 'Include conf.d' /etc/apache2/apache2.conf && [ ! -d /etc/apache2/conf.d ] && mkdir /etc/apache2/conf.d
if [ -x /usr/sbin/apache2ctl ] && [ -f /etc/apache2/apache2.conf ]; then
if grep -qai '^Include /etc/apache2/conf.d/' /etc/apache2/apache2.conf && [ ! -d /etc/apache2/conf.d ]; then
replace 'Include /etc/apache2/conf.d/' '#Include /etc/apache2/conf.d/' -- /etc/apache2/apache2.conf
echo "dss:info: Commenting out Include /etc/apache2/conf.d/ for non-existent directory. Might be better to use revert to package provided apache config?"
fi
if grep -qa '^Include /etc/apache2/httpd.conf' /etc/apache2/apache2.conf && [ ! -f /etc/apache2/httpd.conf ]; then
replace "Include /etc/apache2/httpd.conf" "#Include /etc/apache2/httpd.conf" -- /etc/apache2/apache2.conf
echo "dss:info:Commenting out Include /etc/apache2/httpd.conf for non existent file"
fi
if grep -qa '^Include httpd.conf' /etc/apache2/apache2.conf && [ ! -f /etc/apache2/httpd.conf ]; then
replace "Include httpd.conf" "#Include httpd.conf" -- /etc/apache2/apache2.conf
echo "dss:info:Commenting out Include httpd.conf for non existent file"
fi
if ! /usr/sbin/apache2ctl -S &> /dev/null && grep -qa '^LockFile ' /etc/apache2/apache2.conf; then
replace "LockFile" "#LockFile" -- /etc/apache2/apache2.conf
echo "dss:info:Commented out Lockfile in /etc/apache2/apache2.conf"
fi
if [ -f /etc/apache2/mods-available/ssl.conf ] && /usr/sbin/apache2ctl -S 2>&1 | grep -qai "Invalid command 'SSLMutex'"; then
replace "SSLMutex" "#SSLMutex" -- /etc/apache2/mods-available/ssl.conf
fi
if /usr/sbin/apache2ctl -S 2>&1 | grep -qai 'Ignoring deprecated use of DefaultType'; then
replace "DefaultType" "#DefaultType" -- /etc/apache2/apache2.conf
echo "dss:info:Commented out DefaultType in /etc/apache2/apache2.conf"
fi
fi
# error of sshd[1762]: Missing privilege separation directory: /var/run/sshd
# => mkdir /var/run/sshd
while true; do
# not debian-ish
if ! which dpkg >/dev/null 2>&1; then break; fi
# if they had mysql they'll have something like:
# rc mysql-server-5.1 5.1.73-1 ...
if ! dpkg -l | grep -qai '^rc.*mysql-'; then break; fi
# if mysql or maria db something is installed, quit here.
if dpkg -l | grep -v mysql-common | egrep -qai '^ii.*mysql-|^ii.*mariadb'; then break; fi
# no mysql conf dir, quit
if [ ! -d /etc/mysql ]; then break; fi
echo "dss:info: MySQL appears to have been installed, but no longer present. This can happen between debian 8 and debian 9. As mysql is replaced by mariadb. Attempting to install mysql-server which would pull in mariadb."
dpkg -l | egrep -i 'mysql|mariadb' | awk '{print "dss:info:mysqlrelatedpackages:pre:" $0}'
apt-get -y -o Dpkg::Options::="--force-confnew" -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confmiss" install mysql-server
if [ $? -ne 0 ]; then break; fi
dpkg -l | egrep -i 'mysql|mariadb' | awk '{print "dss:info:mysqlrelatedpackages:post:" $0}'
break
done
for i in $(find /etc/cron.* -type f -name 000loaddelay); do
#old style ifconfig
ifconfig | grep 'inet addr' && continue
# not our script
grep -qai 'random=.*ifconfig.*sed' $i || continue
echo '#!/bin/bash
# This is to delay cron jobs by up to 10 minutes to relieve host server load.
# needs to parse inet 174.136.11.74 B174.136.11.79 M255.255.255.248 and
# inet addr:174.136.11.74 Bcast:174.136.11.79 Mask:255.255.255.248
declare -i random=$(expr $(ifconfig eth0 | grep -v inet6 | grep "inet" | sed -e "s/[^0-9 ]//g" | sed "s/^ *//" | cut -f 1 -d\ ) % 900)
sleep ${random}
exit 0' > $i
echo "dss:info:updating load delay script: $i"
done
}
function dist_upgrade_x_to_y() {
[ ! -e /etc/apt/sources.list ] && return 0
if ! grep -qai "^ *deb.*$old_distro" -- /etc/apt/sources.list; then
return 0
fi
if ! lsb_release -a 2>/dev/null| egrep -qai "$old_distro|$old_ver" ; then
return 0
fi
echo "dss:trace:dist_upgrade_x_to_y:olddistro=$old_distro:oldver=$old_ver:newdistro=$new_distro"
if [ "$old_distro" == "lenny" ]; then
if dpkg -l | grep -qai '^i.*dovecot'; then
print_uninstall_dovecot
return 1
fi
add_missing_debian_keys
[ ! -d "/dev/pts" ] && mkdir /dev/pts && echo "dss:info:created /dev/pts"
fi
upgrade_precondition_checks || return $?
echo "dss:trace:dist_upgrade_x_to_y:pre_apt_get_upgrade:old:$old_distro:new:$new_distro"
apt_get_upgrade
ret=$?
apt-get clean
apt-get -y -o Dpkg::Options::=--force-confnew -o Dpkg::Options::=--force-confdef -o Dpkg::Options::=--force-confmiss autoremove
if [ $ret -ne 0 ]; then
echo "dss:error: apt-get upgrade failed. exiting dist_upgrade_${old_distro}_to_${new_distro}"
return 1
fi
disable_debian_repos $old_distro
if ! grep -qai "^ *deb.* ${new_distro}[ /-]" /etc/apt/sources.list; then
echo "deb http://http.us.debian.org/debian/ ${new_distro} main non-free contrib" >> /etc/apt/sources.list
echo "deb http://security.debian.org/ ${new_distro}/updates main" >> /etc/apt/sources.list
echo "$old_distro:$new_distro: apt sources now has $(cat /etc/apt/sources.list | egrep -v '^$|^#')" | awk '{print "dss:info:sources:dist_upgrade_x_to_y:" $0}'
fi
# redo to convert the above to archive where appropriate. And add lts if appropriate.
enable_debian_archive
echo "dss:trace:dist_upgrade_x_to_y:pre_apt_get_dist_upgrade::olddistro=$old_distro:oldver=$old_ver:newdistro=$new_distro"
apt_get_dist_upgrade
ret=$?
apt-get -y -o Dpkg::Options::=--force-confnew -o Dpkg::Options::=--force-confdef -o Dpkg::Options::=--force-confmiss autoremove
if [ $ret -eq 0 ]; then
if lsb_release -a 2>/dev/null| egrep -qai '${new_distro}'; then
# dist-upgrade returned ok, and lsb_release thinks we are wheezy
echo "dss:info: dist-upgrade from ${old_distro} to ${new_distro} appears to have worked."
return 0;
fi
fi
return 1
}
function print_minimal_config() {
local a=$1
local b=$2
[ ! -f $a ] && return 1
egrep -v '^\s*#|^$' $a
return 0
}
function print_pkg_to_modified_diff() {
mkdir /root/pkgdiff.$$
# get a list of config files in packages that have been changed by the user
local modifiedconfigfiles=$(dpkg-query -W -f='${Conffiles}\n' '*' | grep -v obsolete | awk 'OFS=" "{print $2,$1}' | LANG=C md5sum -c 2>/dev/null | awk -F': ' '$2 !~ /OK$/{print $1}' | sort)
local modifiedconfigfile
cd /root/pkgdiff.$$
for modifiedconfigfile in $modifiedconfigfiles; do
# figure out the package name
# dpkg -S /etc/apache2/mods-available/ssl.conf
# apache2: /etc/apache2/mods-available/ssl.conf
local pkg=$(dpkg -S "$modifiedconfigfile" | awk '{print $1}' | sed 's/://')
[ -z "$pkg" ] && continue
#figure out the filename
#apt-get --print-uris download apache2
# 'http://http.us.debian.org/debian/pool/main/a/apache2/apache2_2.4.10-10+deb8u7_i386.deb' apache2_2.4.10-10+deb8u7_i386.deb 207220 SHA256:7974cdeed39312fda20165f4ee8bebc10f51062600a7cd95f4c5cba32f7ae12c
# note will not return a result if the file is already here (hence the 'hidden' stuff below).
local debfilename=$(apt-get --print-uris download "$pkg" 2>/dev/null| awk '{print $2}')
[ -z "$debfilename" ] && continue
# download it if we don't already have it
if [ ! -f "hidden-${debfilename}" ]; then
apt-get download "$pkg" &>/dev/null
# can fail if apt is not up to date
[ $? -ne 0 ] && apt-get update &>/dev/null && apt-get download "$pkg" &>/dev/null
# extract to local dir
dpkg -x "$debfilename" .
mv "$debfilename" "hidden-$debfilename"
fi
# pop a copy there so we can replace current file if desired
[ -f "./${modifiedconfigfile}" ] && [ ! -f "${modifiedconfigfile}.dpkg-dist" ] && cp "./${modifiedconfigfile}" "${modifiedconfigfile}.dpkg-dist"
[ -f "${modifiedconfigfile}.dpkg-dist" ] && echo "dss:info:modifiedfilereplace:To replace edited file with dist file: mv $modifiedconfigfile $modifiedconfigfile.dpkg-old; mv ${modifiedconfigfile}.dpkg-dist ${modifiedconfigfile}"
# show a diff
print_minimal_config_diff "./$modifiedconfigfile" "$modifiedconfigfile" | awk '{print "dss:info:modifiedfilediff:'$pkg':'$modifiedconfigfile':" $0}'
done
# cleanup
cd - >/dev/null
rm -rf /root/pkgdiff.$$
}
function print_minimal_config_diff() {
local a=$1
local b=$2
[ ! -f $a ] && return 1
[ ! -f $b ] && return 1
ta=$(mktemp "$(basename "${a}").XXXXXX")
tb=$(mktemp "$(basename "${b}").XXXXXX")
print_minimal_config $a > $ta
print_minimal_config $b > $tb
diff --ignore-all-space -u $ta $tb
ret=$?
rm -f $ta $tb
return $ret
}
function print_config_state_changes() {
prep_ghost_output_dir
local now=$(date +%s)
record_config_state /root/deghostinfo/postupgrade.dpkg.$now
# get oldest/first preupgrade file. e.g. we may have to rerun this script. so diff from first run
local fromfile=$(ls -1rt $(find /root/deghostinfo/ -mtime -1 | grep preupgrade) | head -n 1)
[ -z "$fromfile" ] && fromfile=/root/deghostinfo/preupgrade.dpkg.$$
echo "dss:info: Config changes to check. e.g. different processes after upgrade. e.g. different ports. e.g. different apache status output. e.g. changes to dpkg-old/dpkg-dist files. dpkg-old = your files that were not used. dpk-dist = distro files that were not used."
print_minimal_config_diff $fromfile /root/deghostinfo/postupgrade.dpkg.$now | awk '{print "dss:config-state-changes:" $0}'
echo "dss:info:How the distro provided config files differ from what is installed. Consider what is needed to switch back to the distro provided config files."
local files=$(find /etc -type f | egrep '.ucf-old|.ucf-diff|.dpkg-new|.dpkg-old|dpkg-dist|\.rpmnew|.rpmsave' | sort)
for file in $files; do
# defer to the new and improved print_pkg_to_modified_diff function (debian/ubuntu only)
echo $file | grep -q 'dpkg-dist' && continue
# if not rpmnew file, skip
echo $file | egrep -qv 'dpkg-dist|rpmnew' && continue
current=$(echo $file | sed 's/\.dpkg-dist$//')
current=$(echo $file | sed 's/\.rpmnew$//')
# modified file exists?
[ -z "$current" ] || [ ! -f $current ] && continue
echo "dss:pkgdiff:$current To use the dist file: mv $current $current.dpkg-old; mv $file $current"
print_minimal_config_diff $file $current | awk '{print "dss:pkgdiff:" $0}'
done
print_pkg_to_modified_diff
# non .conf site files
# IncludeOptional sites-enabled/*.conf
[ -d /etc/apache2/sites-available ] && [ -f /etc/apache2/apache2.conf ] && grep -qai 'Include.*sites-.*conf' /etc/apache2/apache2.conf && local nonconfsitefiles=$(find /etc/apache2/sites-available -type f | egrep -v '\.conf$|dpkg-')
for file in $nonconfsitefiles; do
echo "dss:warn: Apache config file '$file' should have a .conf extension: mv $file $file.conf;a2ensite $(basename $file).conf)"
done
}
function record_config_state() {
prep_ghost_output_dir
local file=$1
if [ -z "$file" ]; then
file=/root/deghostinfo/preupgrade.dpkg.$$
fi
# don't overwrite the preupgrade file
echo $file | grep preupgrade && [ -f $file ] && return 0
local files=$(find /etc -type f | egrep '.ucf-old|.ucf-diff|.dpkg-new|.dpkg-old|dpkg-dist|\.rpmnew|.rpmsave' | sort)
> $file
# conf files
echo "" >> $file
[ ! -z "$files" ] && ls -lrt $files > $file
echo "Listening ports:" >> $file
echo "" >> $file
# listening ports
# Listen ports: 0.0.0.0:995 dovecot
netstat -ntpl | grep LISTEN | awk '{print "Listen ports: " $4 " " $7}' | sed 's/ [0-9]*\// /' | sed 's/0.0.0.0:/:::/' | sort -k 4 | uniq >> $file
echo "Apache vhosts:" >> $file
echo "" >> $file
# vhosts
[ -x /usr/sbin/apache2ctl ] && /usr/sbin/apache2ctl -S 2>&1 | awk '{print "ApacheStatus: " $0}' >> $file
echo "" >> $file
echo "Running processes:" >> $file
echo "" >> $file
ps ax | awk '{print "process: " $5 " " $6 " " $7 " " $8 " " $9}' | egrep -v '^process: \[|COMMAND|init' | sort | uniq >> $file
}
function apt_get_upgrade() {
[ ! -e /etc/apt/sources.list ] && return 0
[ -e /etc/redhat-release ] && return 0
upgrade_precondition_checks || return $?
echo "dss:trace:apt_get_upgrade"
enable_debian_archive
apt-get update
# E: Release file expired, ignoring http://archive.debian.org/debian/dists/squeeze-lts/Release (invalid since 14d 8h 58min 38s)
[ $? -ne 0 ] && apt-get -o Acquire::ForceIPv4=true -o Acquire::Check-Valid-Until=false update
record_config_state
dpkg --configure -a --force-confnew --force-confdef --force-confmiss
apt-get -y -o Dpkg::Options::=--force-confnew -o Dpkg::Options::=--force-confdef -o Dpkg::Options::=--force-confmiss autoremove
apt-get -y -o Dpkg::Options::="--force-confnew" -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confmiss" -f install
echo "dss:info: running an apt-get upgrade"
apt-get -y -o Dpkg::Options::="--force-confnew" -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confmiss" upgrade
ret=$?
apt-get -y -o Dpkg::Options::=--force-confnew -o Dpkg::Options::=--force-confdef -o Dpkg::Options::=--force-confmiss autoremove
apt-get -y -o Dpkg::Options::="--force-confnew" -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confmiss" -f install
if [ $ret -ne 0 ]; then
echo "dss:info: apt-get upgrade failed. trying a dist-ugprade..."
apt-get -y -o Dpkg::Options::="--force-confnew" -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confmiss" dist-upgrade
ret=$?
if [ $ret -eq 0 ]; then
echo "dss:info: apt-get dist-upgrade succeeded when a upgrade failed."
return 0
else
echo "dss:info: apt-get upgrade/dist-upgrade failed."
return 1
fi
fi
apt-get clean
return $ret
}
function apt_get_dist_upgrade() {
[ ! -e /etc/apt/sources.list ] && return 0
upgrade_precondition_checks || return $?
echo "dss:trace:apt_get_dist_upgrade:pre_apt_get_upgrade:"
apt_get_upgrade || return 1
echo "dss:trace:apt_get_dist_upgrade"
apt-get -y -o Dpkg::Options::="--force-confnew" -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confmiss" -f install
apt-get -y -o Dpkg::Options::="--force-confnew" -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confmiss" install dpkg
apt-get -y -o Dpkg::Options::="--force-confnew" -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confmiss" autoremove
apt-get -y -o Dpkg::Options::="--force-confnew" -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confmiss" dist-upgrade
# cope with 'one of those random things'
# E: Could not perform immediate configuration on 'python-minimal'.Please see man 5 apt.conf under APT::Immediate-Configure for details. (2)
if [ $? -ne 0 ] && apt-get -y -o Dpkg::Options::="--force-confnew" -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confmiss" dist-upgrade 2>&1 | grep -qai "Could not perform immediate configuration on "; then
apt-get -f -y install libc6-dev
apt-get dist-upgrade -y -f -o APT::Immediate-Configure=0 -o Dpkg::Options::="--force-confnew" -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confmiss"
fi
[ -e /var/log/syslog ] && [ -e /etc/my/my.cnf ] && if grep "unknown variable 'lc-messages-dir" /var/log/syslog; then
#lc-messages-dir = /usr/share/mysql...
echo "dss: info: commenting out the my.cnf lc-messages-dir directive in case it is causing problems"
sed -i "s@^lc-messages-dir\(.*\)@#lc-messages-dir\1@" /etc/my/my.cnf
fi
dpkg --configure -a --force-confnew --force-confdef --force-confmiss
apt-get -y autoremove
apt-get -y autoclean
apt-get -y -o Dpkg::Options::="--force-confnew" -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confmiss" dist-upgrade
ret=$?
if [ $ret -ne 0 ] ; then
echo "dss:warn: Got an error after an apt-get dist-upgrade. trying an apt-get -f install"
apt-get -f -y install
apt-get -y -o Dpkg::Options::="--force-confnew" -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confmiss" dist-upgrade