-
Notifications
You must be signed in to change notification settings - Fork 8
/
sign.php
executable file
·209 lines (181 loc) · 4.86 KB
/
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
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
#!/usr/bin/env php
<?php
// This creates and signs a .tar.gz file with a valid private key on your keyring.
//
// Params: sign.php /location/of/module <keyfingerprint>
//
// If keyfingerprint is supplied, no sanity checking about the key is
// performed.
if (!isset($argv[1])) {
print $argv[0].": /location/of/module [--local] <keyfingerprint>\n";
print "Key fingerprint is optional\n";
exit(1);
}
$loc = $argv[1];
if (isset($argv[2]) && $argv[2] == "--local") {
if (posix_geteuid() !== 0) {
throw new \Exception("--local must be run as root. You are not root");
}
$local = true;
$keyindex = 3;
} else {
$local = false;
$keyindex = 2;
}
if (substr($loc, -1) == "/") {
// Strip off any trailing slash
$loc = substr($loc, 0, -1);
}
if (!is_dir($loc)) {
print "$loc is not a directory\n";
exit(1);
}
if (!file_exists($loc."/module.xml")) {
print "module.xml does not exists in $loc\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[$keyindex])) {
$key = getSigningKey($argv[$keyindex]);
} else {
$key = getSigningKey();
}
if (!$key) {
print "Wasn't able to find a valid key. Sorry\n";
exit(1);
}
if ($local) {
$ldir = "/etc/freepbx.secure";
print "Installing to local signing directory\n";
if (is_link($ldir)) {
throw new \Exception("Secure Directory ($ldir) is a link");
}
if (!file_exists($ldir)) {
// Make it
mkdir($ldir);
}
if (!is_dir($ldir)) {
throw new \Exception("Secure Directory ($ldir) is not a dir");
}
$xml = new SimpleXmlElement(file_get_contents($loc."/module.xml"));
$name = $xml->rawname;
$sig = "$ldir/$name.sig";
} else {
$sig = "$loc/module.sig";
}
print "Signing with $key\n";
print "\tGenerating file list...";
@unlink($sig);
$files = $gpg->getHashes($loc);
print "\n\tSigning $sig..";
$fh = popen("gpg --default-key $key --clearsign > $sig", "w");
fwrite($fh, ";################################################
;# FreePBX Module Signature File #
;################################################
;# Do not alter the contents of this file! If #
;# this file is tampered with, the module will #
;# fail validation and be marked as invalid! #
;################################################
");
fwrite($fh, "[config]\n");
fwrite($fh, "version=2\n");
fwrite($fh, "hash=sha256\n");
fwrite($fh, "signedwith=$key\n");
fwrite($fh, "signedby=sign.php\n");
fwrite($fh, "repo=manual\n");
if ($local) {
fwrite($fh, "type=local\n");
} else {
fwrite($fh, "type=public\n");
}
fwrite($fh, "[hashes]\n");
foreach ($files as $f => $h) {
// Don't try to validate yourself.
if ($f == "module.sig") {
continue;
}
fwrite($fh, "$f = $h\n");
}
fwrite ($fh, ";# End\n");
pclose($fh);
print "\nDone\n";
if ($local) {
print "Tagging module for local signing...";
$fh = popen("gpg --default-key $key --clearsign > $loc/module.sig", "w");
fwrite($fh, ";################################################
;# FreePBX Module Signature File #
;################################################
;# Do not alter the contents of this file! If #
;# this file is tampered with, the module will #
;# fail validation and be marked as invalid! #
;################################################
");
fwrite($fh, "[config]\n");
fwrite($fh, "version=2\n");
fwrite($fh, "hash=sha256\n");
fwrite($fh, "signedwith=$key\n");
fwrite($fh, "signedby=sign.php\n");
fwrite($fh, "repo=local\n");
fwrite($fh, "type=local\n");
fwrite($fh, "[hashes]\n");
fwrite($fh, "$name.sig = ".hash_file("sha256", $sig)."\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");
}