-
Notifications
You must be signed in to change notification settings - Fork 8
/
system-hook-sign.php
executable file
·146 lines (123 loc) · 3.32 KB
/
system-hook-sign.php
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
#!/usr/bin/env php
<?php
// This creates and signs a system module (which must be stored in
// /usr/local/asterisk/HOOKNAME and /usr/local/asterisk/HOOKNAME.sig
//
// Params: sign-system.php /location/of/file <keyfingerprint>
//
// If keyfingerprint is supplied, no sanity checking about the key is
// performed.
if (!isset($argv[1])) {
print $argv[0].": /location/of/file <keyfingerprint>\n";
print "Key fingerprint is optional\n";
exit(1);
}
$loc = $argv[1];
if (substr($loc, -1) == "/") {
// Strip off any trailing slash
$loc = substr($loc, 0, -1);
}
if (is_dir($loc)) {
print "$loc is a directory. This only signs single files\n";
exit(1);
}
if (!file_exists($loc)) {
print "Can't locate file to sign\n";
exit(1);
}
// Things are looking good, initalize freepbx
include __DIR__."/libraries/GPG.class.php";
$gpg = new GPG();
// Make sure we have the FreePBX key
exec('gpg --list-key B53D215A755231A3', $output, $retcode);
if ($retcode != 0) {
recvKey('B53D215A755231A3');
}
// Now, figure out which key we want to use to sign this
// package with
if (isset($argv[2])) {
$key = getSigningKey($argv[2]);
} else {
$key = getSigningKey();
}
if (!$key) {
print "Wasn't able to find a valid key. Sorry\n";
exit(1);
}
print "Signing $loc with $key\n";
$sigfile = "$loc.sig";
if (file_exists($sigfile)) {
unlink($sigfile);
}
$hash = hash_file('sha256', $loc);
$outfile = basename($loc);
print "\n\tSigning $sigfile (as /usr/local/asterisk/$outfile)";
$fh = popen("gpg --default-key $key --clearsign > $sigfile", "w");
fwrite($fh, ";################################################
;# FreePBX SYSTEM Signature File #
;################################################
;# Do not alter the contents of this file! If #
;# this file is tampered with, the application #
;# will fail validation and WILL NOT BE RUN! #
;################################################
");
fwrite($fh, "[config]\n");
fwrite($fh, "type=system\n");
fwrite($fh, "version=1\n");
fwrite($fh, "hash=sha256\n");
fwrite($fh, "signedwith=$key\n");
fwrite($fh, "signedby=sign-system.php\n");
fwrite($fh, "repo=manual\n");
fwrite($fh, "[hashes]\n");
fwrite($fh, "/usr/local/asterisk/$outfile = $hash\n");
fwrite($fh, ";# End\n");
pclose($fh);
print "\nDone\n";
function getSigningKey($key = false) {
// Figure out what our valid signing key is.
if ($key) {
// TODO: Add sanity check here?
return $key;
}
// Ask GPG for a list of our known private keys.
$keys = listPrivateKeys();
foreach ($keys as $k) {
if (validateKey($k)) {
return $k;
}
}
// Wasn't able to find a key. Boo.
return false;
}
function listPrivateKeys() {
exec('gpg --with-colons --list-secret-keys', $output, $retvar);
if ($retvar != 0) {
print "Error running gpg --list-secret-keys ($retvar) - ".join("\n",$output)."\n";
exit($retvar);
}
$retarr = array();
foreach ($output as $l) {
if (preg_match('/^sec::\d+:\d+:([0-9A-F]+):/', $l, $out)) {
$retarr[] = $out[1];
}
}
return $retarr;
}
function validateKey($key) {
$trusted = 'B53D215A755231A3';
// Ask GPG for valid signatures
exec("gpg --with-colons --check-sigs $key", $output, $retvar);
foreach ($output as $l) {
$tmparr = explode(':', $l);
if ($tmparr[0] != 'sig') {
continue;
}
if ($tmparr[4] == $trusted) {
return true;
}
}
return false;
}
function recvKey($key) {
exec("gpg --recv-key $key");
}