-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdate-kali-menu
executable file
·219 lines (193 loc) · 5.82 KB
/
update-kali-menu
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
#!/usr/bin/perl
use warnings;
use strict;
use Dpkg::ErrorHandling;
use Dpkg::IPC;
use File::FcntlLock;
use File::Copy;
use File::Path qw(make_path);
use constant {
INCOMING_APPDIR => "/usr/share/kali-menu/applications",
OUTGOING_APPDIR => "/usr/share/applications",
DPKG_LOCKFILE => "/var/lib/dpkg/lock",
KALI_MENU_LOCKFILE => "/var/lock/kali-menu",
};
our %pkg2desktop;
our %desktop2pkg;
our %installed;
make_path(OUTGOING_APPDIR, { mode => 0755 });
my $lock = get_lock(); # Can stop here if another instance is already running
parse_kali_desktop_files();
my $arg = shift @ARGV || "";
wait_dpkg() if (defined $ENV{'DPKG_RUNNING_VERSION'} or $arg eq "wait_dpkg");
# Scan installed packages to identify desktop files to install
foreach my $pkg (list_installed_packages()) {
$installed{$pkg} = 1;
next unless exists $pkg2desktop{$pkg};
foreach my $desktop (@{$pkg2desktop{$pkg}}) {
my $target = OUTGOING_APPDIR . "/$desktop";
my $source = INCOMING_APPDIR . "/$desktop";
take_control_of($target);
copy($source, $target);
}
}
# Scan installed desktop files to remove the entries that are no longer
# relevant
FILE: foreach my $desktop (list_installed_desktop_files()) {
my $ref_file = INCOMING_APPDIR . "/$desktop";
my $out_file = OUTGOING_APPDIR . "/$desktop";
next unless is_managed($out_file);
# Drop unknown files
unless (-e $ref_file and exists $desktop2pkg{$desktop}) {
release_desktop_file($out_file);
next;
}
# Ensure the corresponding package is still installed
foreach my $pkg (@{$desktop2pkg{$desktop}}) {
next FILE if $installed{$pkg};
}
release_desktop_file($out_file);
}
$lock->truncate(0);
$lock->close();
### HELPER FUNCTIONS
sub release_desktop_file {
my ($file) = @_;
my $infos = get_dpkg_infos($file);
unlink($file) or syserr("can't remove %s", $file);
remove_diversion($file) if $infos->{'diverted'};
}
sub take_control_of {
my ($target) = @_;
return if not -e $target; # No conflicting file, go ahead
return if is_managed($target); # Already managed, nothing else to do
my $infos = get_dpkg_infos($target);
if ($infos->{'known'} and not $infos->{'diverted'}) {
divert($target);
}
}
sub get_dpkg_infos {
my ($target) = @_;
my $pipe;
my $pid = spawn(exec => [ 'dpkg-query', '-S', $target ],
to_pipe => \$pipe, error_to_file => "/dev/null",
env => { LC_ALL => 'C' });
my $res = {
diverted => 0,
known => 0,
};
while (<$pipe>) {
if (/^(diversion by|local diversion)/) {
$res->{'diverted'} = 1;
next;
}
$res->{'known'} = 1 if /: \Q$target\E/;
}
wait_child($pid, cmdline => "dpkg-query -S $target", nocheck => 1);
return $res;
}
sub divert {
my ($target) = @_;
spawn(exec => [ "dpkg-divert", "--local", "--rename", "--divert",
"$target.disabled-by-kali-menu", "--add", $target ],
wait_child => 1, to_file => "/dev/null");
}
sub remove_diversion {
my ($target) = @_;
spawn(exec => [ "dpkg-divert", "--local", "--rename", "--divert",
"$target.disabled-by-kali-menu", "--remove", $target ],
wait_child => 1, to_file => "/dev/null");
}
sub is_managed {
my ($target) = @_;
my $res = 0;
open(my $fh, "<", $target) or syserr("can't open %s", $target);
if (grep { /^X-Kali-Package=/i } <$fh>) {
$res = 1;
}
close($fh);
return $res;
}
sub list_installed_packages {
my (@list, $pipe);
my $pid = spawn(exec => [ 'dpkg-query', '-f', '${Package} ${Status}\n', '-W' ],
to_pipe => \$pipe);
while (<$pipe>) {
my ($pkg, $want, $ok, $status) = split;
if ($status ne "not-installed" and $status ne "config-files") {
push @list, $pkg;
}
}
wait_child($pid, cmdline => "dpkg-query");
return @list;
}
sub list_installed_desktop_files {
opendir(my $dh, OUTGOING_APPDIR) or syserr("can't opendir %s", OUTGOING_APPDIR);
my @files = grep(/\.desktop$/, readdir($dh));
closedir($dh);
return @files;
}
sub parse_kali_desktop_files {
opendir(my $dh, INCOMING_APPDIR) or syserr("can't opendir %s", INCOMING_APPDIR);
foreach my $file (readdir $dh) {
next unless ($file =~ /\.desktop$/);
my $path = INCOMING_APPDIR . "/$file";
open(my $fh, "<", $path) or syserr("can't open %s", $path);
my $found = 0;
while (<$fh>) {
if (/X-Kali-Package=\s*(.*)$/i) {
$found = 1;
foreach my $pkg (split(/\s+|\s*,\s*/, $1)) {
$pkg2desktop{$pkg} = [] unless exists $pkg2desktop{$pkg};
$desktop2pkg{$file} = [] unless exists $desktop2pkg{$file};
push @{$pkg2desktop{$pkg}}, $file;
push @{$desktop2pkg{$file}}, $pkg;
}
}
}
close($fh);
unless ($found) {
warning("%s is missing the X-Kali-Package header", $path);
}
}
closedir($dh);
}
sub get_lock {
my $lockfile = KALI_MENU_LOCKFILE;
system("mkdir -p /run/lock /var/lock; touch $lockfile") unless -e $lockfile;
open my $fh, '+<', $lockfile or syserr("Can't open %s", $lockfile);
my $fs = new File::FcntlLock l_type => F_WRLCK;
if (not $fs->lock($fh, F_SETLK)) {
exit 0; # Failed to lock, another instance is already running
}
$fh->truncate(0);
$fh->print("$$\n");
$fh->flush();
$fh->sync();
return $fh;
}
sub wait_dpkg {
return unless -e DPKG_LOCKFILE;
open my $fh, '<', DPKG_LOCKFILE or syserr("Can't open %s", DPKG_LOCKFILE);
my $fs = new File::FcntlLock l_type => F_WRLCK;
# Wait until dpkg releases the lock and doesn't grab it back in the
# next 5 seconds
my $count = 0;
while ($count < 2) {
if (can_lock($fh, $fs)) {
$count++;
} else {
$count = 0;
}
sleep(5);
}
close $fh;
}
sub can_lock {
my ($fh, $fs) = @_;
$fs->l_type(F_WRLCK);
if (not $fs->lock($fh, F_GETLK)) {
error("Failed to analyze lock on %s: %s", DPKG_LOCKFILE, $fs->error());
}
return $fs->l_type == F_UNLCK;
}