forked from NationalSecurityAgency/ghidra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ip.gradle
323 lines (291 loc) · 12.5 KB
/
ip.gradle
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
/*********************************************************************************
* ip.gradle
*
* This file defines the gradle tasks for generating the LICENSE.txt in each module
* which lists all the 3rd party files in this module and their licenses.
*
* The task also verifies that the license for each of the 3rd party files is allowed
* based on what license files exist in the root/licenses directory.
*
* It also reads the Module.manifest file where license information is recorded for
* jar dependencies and build artifacts. These are added to the LICENSE.txt file. Also,
* the jar dependencies (as defined in the build.gradle file) are examined to make
* sure they are defined in the Module.manifest file.
*********************************************************************************/
/*********************************************************************************
* Defines the main ip task for each subproject
*********************************************************************************/
subprojects {
ext.isPureEclipseProject = false;
ext.excludeFromBuild = false;
task ip {
doLast {
if (isPureEclipseProject || excludeFromBuild) {
// If the build.gradle file exists only to establish an
// eclipse project or is a support project, don't verify IP.
return;
}
// scans all the files in the module, reads ip from header, verifies ip, and creates mapping
def ipToFileMap = getIpForModule(project)
// reads the ip info from the Module.manifest file and verifies each ip
def moduleManifestIpMap = getModuleManifestIp(project)
// gets the external libs from gradle and verifies they are accounted for in the Module.manifest file
checkExternalLibsInMap(moduleManifestIpMap, project)
// adds the ip info from the Module.manifest file to the map generated from scanning the module files.
addModuleManifestIp(ipToFileMap, moduleManifestIpMap)
// writes the LICENSE.txt file for the module
writeLicenseInfo(project, ipToFileMap)
}
}
}
/*********************************************************************************
* Addes the ip information from the Module.manifest file into the ipToFileMap
*********************************************************************************/
def addModuleManifestIp(Map<String, List<String>> ipToFileMap, Map<String, String> moduleManifestIpMap) {
for (path in moduleManifestIpMap.keySet()) {
String ip = moduleManifestIpMap.get(path)
addToMap(ipToFileMap, ip, path)
}
}
/*********************************************************************************
* Reads the ip info in the Module.manifest file and creates a mapping of path to ip
*********************************************************************************/
def Map<String, String> getModuleManifestIp(Project project) {
Map<String, String> map = new HashMap<String, String>();
File moduleManifest = new File(project.projectDir, "Module.manifest")
if (!moduleManifest.exists()) {
return map
}
def allowedIP = getAllowedIP(project)
def lines = moduleManifest.readLines();
String key = "MODULE FILE LICENSE:"
for(line in lines) {
if (line.startsWith(key)) {
String s = line.substring(key.length()).trim()
int index = s.indexOf(' ')
String path = s.substring(0, index).trim()
String ip = s.substring(index+1).trim()
def ipString = ip.replace(' ','_')
assert allowedIP.contains(ipString) : "Encountered Non-allowed IP: "+ip+ " for Module.manifest entry: "+path
map.put(path, ip)
}
}
return map
}
/**********************************************************************************
* Gets the gradle dependences and makes sure each external lib is accounted for in
* the map from the Module.manifest file
*********************************************************************************/
def checkExternalLibsInMap(Map<String, String> map, Project project) {
List<String> libs = getExternalDependencies(project)
libs.each { lib ->
String libName = new File(lib).getName() // get just the filename without the path
String relativePath = "lib/"+libName;
assert map.containsKey(relativePath) : "No License specified for external library: "+relativePath+ " in module "+project.projectDir
}
}
/*********************************************************************************
* Examines all the files in the module, reads their ip from the header, verifies
* that the ip is allowed, and adds an entry to a mapping of the ip to a list of
* files with that ip
*********************************************************************************/
def Map<String, List<String>> getIpForModule(Project p) {
Map<String, List<String>> map = new HashMap<String, List<String>>()
File certificationFile = new File(p.projectDir, "certification.manifest")
if (!certificationFile.exists()) {
return map;
}
def allowedIP = getAllowedIP(p)
FileTree tree = p.fileTree(".") {
exclude "bin/**";
exclude "**/build/**";
exclude "certification.manifest"
exclude "certification.local.manifest"
exclude ".project"
exclude ".classpath"
exclude "Module.manifest"
exclude "build.gradle"
exclude "**/Misc/Tips.htm"
exclude "**/*.sla"
exclude "**/data/build.xml" // language build file (generated for dev only)
exclude "**/.gradle/**"
exclude "**/.settings/**"
exclude "**/.vs/**"
exclude "**/*.vcxproj.user"
}
tree.each { file ->
String ip = getIp(p.projectDir, file)
assert ip != null : "No IP found for "+file.path+ " in module: "+p.projectDir
String ipString = ip.replace(' ','_')
assert allowedIP.contains(ipString) : "Found non-allowed IP: "+ip+" for file "+file.path+" in module: "+p.projectDir
addToMap(map, ip, getRelativePath(p.projectDir, file))
}
return map;
}
/*********************************************************************************
* Returns the relative path of a file in the module
*********************************************************************************/
def String getRelativePath(File projectDir, File file) {
return file.getPath().substring(projectDir.getPath().length()+1)
}
/*********************************************************************************
* adds a path and its ip to the mapping of ip to list of files
*********************************************************************************/
def addToMap(Map<String, List<String>> map, String ip, String path) {
List<String> list = map.get(ip);
if (list == null) {
list = new ArrayList<String>();
map.put(ip, list);
}
list.add(path)
}
/*********************************************************************************
* checks if a file supports a C style header based on its extension.
*********************************************************************************/
def isSourceFile(File file) {
String filename = file.getName().toLowerCase();
return filename.endsWith(".java") ||
filename.endsWith(".c") ||
filename.endsWith(".groovy") ||
filename.endsWith(".cpp") ||
filename.endsWith(".cc") ||
filename.endsWith(".h") ||
filename.endsWith(".y") ||
filename.endsWith(".l") ||
filename.endsWith(".hh") ||
filename.endsWith(".css") ||
filename.endsWith(".jj");
}
/*********************************************************************************
* Gets the ip for a file in the module from its header (or certification.manifest
*********************************************************************************/
def getIp(File projectDir, File file) {
if (isSourceFile(file)) {
return getIpForSourceFile(file);
}
return getIpForNonSourceFile(projectDir, file);
}
/*********************************************************************************
* Gets the ip from a file that has a certification header
*********************************************************************************/
def getIpForSourceFile(File file) {
String ip =null
String line;
file.withReader { reader ->
while((line = reader.readLine()) != null) {
if (line.startsWith(" * IP:")) {
ip = line.substring(7).trim();
break;
}
}
}
return ip;
}
/*********************************************************************************
* Gets the ip for a file that does not have a header, but has an entry in the
* certication.manifest
*********************************************************************************/
def getIpForNonSourceFile(File projectDir, File file) {
String ip = null
File manifest = new File(projectDir, "certification.manifest");
def lines = manifest.readLines()
lines.each {line ->
line = line.trim();
def parts = line.split("\\|");
if (parts.length > 2 && file.toString().replace(File.separator, "/").endsWith(parts[0])) {
ip = parts[2];
}
}
return ip;
}
/*********************************************************************************
* Writes the license information to the LICENSE.txt file for the module
*********************************************************************************/
def writeLicenseInfo(Project project, Map<String, List<String>> map) {
if (map.isEmpty()) {
return;
}
File buildDir = new File(project.projectDir, "build")
buildDir.mkdir();
File licenseFile = new File(buildDir,"LICENSE.txt");
def buf = new StringBuffer();
addLicenseProlog(project, buf)
map.keySet().each { ip ->
reportLicenseFiles(buf, ip, map.get(ip))
}
licenseFile.text = buf.toString()
}
/*********************************************************************************
* Writes the files for a single ip
*********************************************************************************/
def reportLicenseFiles(StringBuffer buf, String ip, List<String> filepaths) {
if (ip.equals("GHIDRA") || ip.equals("LICENSE")) {
return;
}
buf.append(ip+":\n\n")
filepaths.each { path ->
buf.append("\t")
buf.append(path)
buf.append("\n")
}
buf.append("\n")
}
/*********************************************************************************
* Generates the text for the prolog (non-changing) part of the LICENSE.txt file
*********************************************************************************/
def addLicenseProlog(Project project, StringBuffer buf) {
if (project.projectDir.toString().contains(File.separator + "GPL" + File.separator)) {
buf.append("The program in this module is released under the GPL 3 license. \n")
buf.append("The files used to create this program include both public domain\n")
buf.append("files created by the Ghidra team and 3rd party files with \n")
buf.append("the GPL 3 or GPL 3 compatible license. ")
buf.append("The license files for each of license used can be found in the\n")
buf.append("<installation root>/GPL/licenses.\n\n")
buf.append("\nThe 3rd party files in this module are as follows:\n\n\n")
}
else {
buf.append("Ghidra software is released under the Apache 2.0 license. In addition, \n")
buf.append("there are numerous 3rd party components that each have their \n")
buf.append("own license. The license file for each of these licenses can be found\n")
buf.append("in the licenses directory in the installation root directory.\n")
buf.append("\nThe 3rd party files in this module are as follows:\n\n\n")
}
}
/*********************************************************************************
* Examines the <root>/licenses directory to discover what licenses are allowed
*********************************************************************************/
def Set<String> getAllowedIP(Project p) {
Set<String> set = new HashSet<String>()
def projectPath = p.projectDir.path.replace(File.separator, "/");
if (projectPath.contains("/GPL/")) {
set.add("GPL_3")
set.add("GPL_3_Linking_Permitted")
set.add("GPL_2_With_Classpath_Exception")
set.add("Public_Domain")
set.add("LGPL_3.0")
set.add("LGPL_2.1")
}
else {
File root = p.rootProject.file("..")
root.listFiles().each { f ->
File licenseDir = new File(f, "licenses")
File[] files = licenseDir.listFiles()
files.each { file ->
set.add(getIpName(file.getName()))
}
}
}
set.add("GHIDRA")
set.add("LICENSE")
set.add("Copyright_Distribution_Permitted")
return set;
}
/*********************************************************************************
* converts a file name to an ip name that can be compared to info from headers.
*********************************************************************************/
def String getIpName(String filename) {
if (filename.endsWith(".txt")) filename = filename.substring(0, filename.length()-4)
if (filename.endsWith(".htm")) filename = filename.substring(0, filename.length()-4)
if (filename.endsWith(".html")) filename = filename.substring(0, filename.length()-5)
return filename
}