Rename airpdcap to dot11decrypt.
[metze/wireshark/wip.git] / tools / checklicenses.py
1 #!/usr/bin/env python
2 # Copyright (c) 2013 The Chromium Authors. All rights reserved.
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
6 # met:
7 #
8 #    * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 #    * Redistributions in binary form must reproduce the above
11 # copyright notice, this list of conditions and the following disclaimer
12 # in the documentation and/or other materials provided with the
13 # distribution.
14 #    * Neither the name of Google Inc. nor the names of its
15 # contributors may be used to endorse or promote products derived from
16 # this software without specific prior written permission.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 """Makes sure that all files contain proper licensing information."""
31
32
33 import optparse
34 import os.path
35 import subprocess
36 import sys
37
38
39 def PrintUsage():
40   print("""Usage: python checklicenses.py [--root <root>] [tocheck]
41   --root   Specifies the repository root. This defaults to ".." relative
42            to the script file. This will be correct given the normal location
43            of the script in "<root>/tools".
44
45   --ignore-suppressions  Ignores path-specific license whitelist. Useful when
46                          trying to remove a suppression/whitelist entry.
47
48   tocheck  Specifies the directory, relative to root, to check. This defaults
49            to "." so it checks everything.
50
51 Examples:
52   python checklicenses.py
53   python checklicenses.py --root ~/chromium/src third_party""")
54
55
56 WHITELISTED_LICENSES = [
57     'BSD',
58     'BSD (2 clause)',
59     'BSD (2 clause) GPL (v2 or later)',
60     'BSD (3 clause)',
61     'GPL (v2 or later)',
62     'GPL (v3 or later) (with Bison parser exception)',
63     'ISC',
64     'ISC GPL (v2 or later)',
65     'LGPL (v2 or later)',
66     'LGPL (v2.1 or later)',
67     'MIT/X11 (BSD like)',
68     'Public domain',
69     'Public domain GPL (v2 or later)',
70     'Public domain MIT/X11 (BSD like)',
71     'zlib/libpng',
72     'zlib/libpng GPL (v2 or later)',
73 ]
74
75
76 PATH_SPECIFIC_WHITELISTED_LICENSES = {
77     'caputils/airpcap.h': [
78         'BSD-3-Clause',
79     ],
80     'wsutil/strnatcmp.c': [
81         'Zlib',
82     ],
83     'wsutil/strnatcmp.h': [
84         'Zlib',
85     ],
86     'dtds': [
87         'UNKNOWN',
88     ],
89     'diameter/dictionary.dtd': [
90         'UNKNOWN',
91     ],
92     'wimaxasncp/dictionary.dtd': [
93         'UNKNOWN',
94     ],
95     'doc/': [
96         'UNKNOWN',
97     ],
98     'docbook/custom_layer_chm.xsl': [
99         'UNKNOWN',
100     ],
101     'docbook/custom_layer_single_html.xsl': [
102         'UNKNOWN',
103     ],
104     'docbook/ws.css' : [
105         'UNKNOWN'
106     ],
107     'fix': [
108         'UNKNOWN',
109     ],
110     'wsutil/g711.c': [
111         'UNKNOWN',
112     ],
113     'packaging/macosx': [
114         'UNKNOWN',
115     ],
116     'epan/except.c': [
117         'UNKNOWN',
118     ],
119     'epan/except.h': [
120         'UNKNOWN',
121     ],
122     'cmake/TestFileOffsetBits.c': [
123         'UNKNOWN',
124     ],
125     'cmake/TestWindowsFSeek.c': [
126         'UNKNOWN',
127     ],
128     # Generated header files by lex/yacc/whatever
129     'epan/dtd_grammar.h': [
130         'UNKNOWN',
131     ],
132     'epan/dfilter/grammar.h': [
133         'UNKNOWN',
134     ],
135     'epan/dfilter/grammar.c': [
136         'UNKNOWN',
137     ],
138     'epan/dissectors/packet-dtn.c': [
139         'GPL (v2 or later) GPL (v2 or later)' # TODO: make licensecheck handle this better
140     ],
141     'epan/dissectors/packet-ieee80211-radiotap-iter.': [ # Using ISC license only
142          'ISC GPL (v2)'
143     ],
144     'epan/dissectors/packet-ppi.c': [ # Using BSD (3 clause) license
145         'BSD (3 clause) GPL (v2)'
146     ],
147     'plugins/mate/mate_grammar.h': [
148         'UNKNOWN',
149     ],
150     'version.h': [
151         'UNKNOWN',
152     ],
153     # Special IDL license that appears to be compatible as far as I (not a
154     # lawyer) can tell. See
155     # https://www.wireshark.org/lists/wireshark-dev/201310/msg00234.html
156     'epan/dissectors/pidl/idl_types.h': [
157         'UNKNOWN',
158     ],
159     # Written by Ronnie Sahlberg and correctly licensed, but cannot include
160     # a license header despite the file extension as they need to be
161     # parsed by the pidl tool
162     'epan/dissectors/pidl/mapi/request.cnf.c': [
163         'UNKNOWN',
164     ],
165     'epan/dissectors/pidl/mapi/response.cnf.c': [
166         'UNKNOWN',
167     ],
168     # The following tools are under incompatible licenses (mostly GPLv3 or
169     # GPLv3+), but this is OK since they are not actually linked into Wireshark
170     'tools/pidl': [
171         'UNKNOWN',
172     ],
173     'tools/lemon': [
174         'UNKNOWN',
175     ],
176     'tools/licensecheck.pl': [
177         'GPL (v2)'
178     ],
179     # Generated files for GTK pixbuf binary bundling
180     'ui/gtk/wireshark-gresources.h': [
181         'UNKNOWN',
182     ],
183     'ui/gtk/wireshark-gresources.c': [
184         'UNKNOWN',
185     ],
186     # The airpcap code is using BSD (3 clause)
187     'epan/crypt/dot11decrypt_interop.h': [
188         'BSD (3 clause) GPL (v2)'
189     ],
190     'epan/crypt/dot11decrypt_tkip.c': [
191         'BSD (3 clause) GPL (v2)'
192     ],
193     'epan/crypt/dot11decrypt_ws.h': [
194         'BSD (3 clause) GPL (v2)'
195     ],
196     'epan/crypt/wep-wpadefs.h': [
197         'BSD (3 clause) GPL (v2)'
198     ],
199     'epan/crypt/dot11decrypt_system.h': [
200         'BSD (3 clause) GPL (v2)'
201     ],
202     'epan/crypt/dot11decrypt_user.h': [
203         'BSD (3 clause) GPL (v2)'
204     ],
205     'epan/crypt/dot11decrypt_ccmp.c': [
206         'BSD (3 clause) GPL (v2)'
207     ],
208     'epan/crypt/dot11decrypt_int.h': [
209         'BSD (3 clause) GPL (v2)'
210     ],
211     'epan/crypt/dot11decrypt.c': [
212         'BSD (3 clause) GPL (v2)'
213     ],
214     'epan/crypt/dot11decrypt_debug.h': [
215         'BSD (3 clause) GPL (v2)'
216     ],
217     'wsutil/dot11decrypt_wep.c': [
218         'BSD (3 clause) GPL (v2)'
219     ],
220 }
221
222 def check_licenses(options, args):
223   # Figure out which directory we have to check.
224   if len(args) == 0:
225     # No directory to check specified, use the repository root.
226     start_dir = options.base_directory
227   elif len(args) == 1:
228     # Directory specified. Start here. It's supposed to be relative to the
229     # base directory.
230     start_dir = os.path.abspath(os.path.join(options.base_directory, args[0]))
231   else:
232     # More than one argument, we don't handle this.
233     PrintUsage()
234     return 1
235
236   print("Using base directory: %s" % options.base_directory)
237   print("Checking: %s" % start_dir)
238   print("")
239
240   licensecheck_path = os.path.abspath(os.path.join(options.base_directory,
241                                                     'tools',
242                                                     'licensecheck.pl'))
243
244   licensecheck = subprocess.Popen([licensecheck_path,
245                                    '-l', '150',
246                                    '-r', start_dir],
247                                   stdout=subprocess.PIPE,
248                                   stderr=subprocess.PIPE)
249   stdout, stderr = licensecheck.communicate()
250   if sys.version_info[0] >= 3:
251       stdout = stdout.decode('utf-8')
252       stderr = stderr.decode('utf-8')
253   if options.verbose:
254     print('----------- licensecheck stdout -----------')
255     print(stdout)
256     print('--------- end licensecheck stdout ---------')
257   if licensecheck.returncode != 0 or stderr:
258     print('----------- licensecheck stderr -----------')
259     print(stderr)
260     print('--------- end licensecheck stderr ---------')
261     print("\nFAILED\n")
262     return 1
263
264   success = True
265   exit_status = 0
266   for line in stdout.splitlines():
267     filename, license = line.split(':', 1)
268     filename = os.path.relpath(filename.strip(), options.base_directory)
269
270     # All files in the build output directory are generated one way or another.
271     # There's no need to check them.
272     if filename.startswith('out/') or filename.startswith('sconsbuild/'):
273       continue
274
275     # For now we're just interested in the license.
276     license = license.replace('*No copyright*', '').strip()
277
278     # Skip generated files.
279     if 'GENERATED FILE' in license:
280       continue
281
282     if license in WHITELISTED_LICENSES:
283       continue
284
285     if not options.ignore_suppressions:
286       found_path_specific = False
287       for prefix in PATH_SPECIFIC_WHITELISTED_LICENSES:
288         if (filename.startswith(prefix) and
289             license in PATH_SPECIFIC_WHITELISTED_LICENSES[prefix]):
290           found_path_specific = True
291           break
292       if found_path_specific:
293         continue
294
295     reason = "'%s' has non-whitelisted license '%s'" % (filename, license)
296     success = False
297     print(reason)
298     exit_status = 1
299
300   if success:
301     print("\nSUCCESS\n")
302     return 0
303   else:
304     print("\nFAILED\n")
305     return exit_status
306
307
308 def main():
309   default_root = os.path.abspath(
310       os.path.join(os.path.dirname(__file__), '..'))
311   option_parser = optparse.OptionParser()
312   option_parser.add_option('--root', default=default_root,
313                            dest='base_directory',
314                            help='Specifies the repository root. This defaults '
315                            'to "../.." relative to the script file, which '
316                            'will normally be the repository root.')
317   option_parser.add_option('-v', '--verbose', action='store_true',
318                            default=False, help='Print debug logging')
319   option_parser.add_option('--ignore-suppressions',
320                            action='store_true',
321                            default=False,
322                            help='Ignore path-specific license whitelist.')
323   options, args = option_parser.parse_args()
324   return check_licenses(options, args)
325
326
327 if '__main__' == __name__:
328   sys.exit(main())