2010-08-02 20:27:37 +02:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
2011-02-01 15:34:40 +01:00
|
|
|
# Copyright (c) 2010-2011, Benjamin Drung <bdrung@ubuntu.com>
|
2008-08-11 20:06:35 +02:00
|
|
|
#
|
2010-08-02 20:27:37 +02:00
|
|
|
# Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
# purpose with or without fee is hereby granted, provided that the above
|
|
|
|
# copyright notice and this permission notice appear in all copies.
|
2007-07-09 16:17:24 +01:00
|
|
|
#
|
2010-08-02 20:27:37 +02:00
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
|
|
|
import optparse
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
2010-12-22 01:28:00 +01:00
|
|
|
from ubuntutools.logger import Logger
|
|
|
|
|
2011-02-12 22:37:16 +01:00
|
|
|
try:
|
|
|
|
import magic
|
|
|
|
except ImportError:
|
|
|
|
Logger.error("Please install 'python-magic' in order to use this utility.")
|
|
|
|
sys.exit(1)
|
|
|
|
|
2010-12-22 22:43:21 +01:00
|
|
|
DEFAULT_WHITELISTED_MIMETYPES = [
|
2010-12-22 01:28:00 +01:00
|
|
|
"application/vnd.font-fontforge-sfd", # font source: fontforge
|
|
|
|
"application/x-elc",
|
|
|
|
"application/x-empty",
|
|
|
|
"application/x-font-otf", # font object and source
|
|
|
|
"application/x-font-ttf", # font object and source
|
|
|
|
"application/x-font-woff", # font object and source
|
|
|
|
"application/x-symlink",
|
|
|
|
"application/xml",
|
|
|
|
"audio/x-wav",
|
|
|
|
"font/otf", # font object and source
|
|
|
|
"font/ttf", # font object and source
|
|
|
|
"image/gif",
|
|
|
|
"image/jpeg",
|
|
|
|
"image/png",
|
|
|
|
"image/svg+xml",
|
2011-02-01 15:34:40 +01:00
|
|
|
"image/vnd.adobe.photoshop",
|
2010-12-22 01:28:00 +01:00
|
|
|
"image/x-icns",
|
|
|
|
"image/x-ico",
|
|
|
|
"image/x-ms-bmp",
|
|
|
|
"image/x-portable-pixmap",
|
|
|
|
"message/rfc822",
|
|
|
|
"text/html",
|
|
|
|
"text/plain",
|
|
|
|
"text/rtf",
|
|
|
|
"text/troff",
|
|
|
|
"text/x-asm",
|
|
|
|
"text/x-c",
|
|
|
|
"text/x-c++",
|
|
|
|
"text/x-diff",
|
|
|
|
"text/x-fortran",
|
|
|
|
"text/x-java",
|
|
|
|
"text/x-lisp",
|
|
|
|
"text/x-m4",
|
|
|
|
"text/x-makefile",
|
|
|
|
"text/x-msdos-batch",
|
|
|
|
"text/x-pascal",
|
|
|
|
"text/x-perl",
|
|
|
|
"text/x-php",
|
|
|
|
"text/x-po",
|
2010-12-26 20:32:48 +02:00
|
|
|
"text/x-python",
|
2010-12-22 01:28:00 +01:00
|
|
|
"text/x-shellscript",
|
|
|
|
"text/x-tex",
|
|
|
|
"text/x-texinfo",
|
2010-08-02 20:27:37 +02:00
|
|
|
]
|
|
|
|
|
2010-12-22 22:43:21 +01:00
|
|
|
DEFAULT_WHITELISTED_EXTENSIONS = [
|
|
|
|
".fea", # font source format: Adobe Font Development Kit for OpenType
|
2010-12-22 01:28:00 +01:00
|
|
|
".fog", # font source format: Fontographer
|
|
|
|
".g2n", # font source format: fontforge
|
|
|
|
".gdh", # font source format: Graphite (headers)
|
|
|
|
".gdl", # font source format: Graphite
|
|
|
|
".glyph", # font source format: cross-toolkit UFO
|
2011-02-01 15:15:01 +01:00
|
|
|
".icns", # Apple Icon Image format
|
|
|
|
".java", # Java source files
|
2010-12-22 01:28:00 +01:00
|
|
|
".plate", # font source format: Spiro
|
|
|
|
".rsa",
|
|
|
|
".sfd", # font source format: fontforge
|
|
|
|
".sfdir", # font source format: fontforge
|
|
|
|
".ttx", # font source format: fonttools
|
|
|
|
".ufo", # font source format: cross-toolkit UFO
|
|
|
|
".vfb" # font source format: FontLab
|
|
|
|
".vtp", # font source format: OpenType (VOLT)
|
|
|
|
".xgf", # font source format: Xgridfit
|
2010-08-02 20:27:37 +02:00
|
|
|
]
|
|
|
|
|
2010-12-27 15:20:49 +01:00
|
|
|
def suspicious_source(whitelisted_mimetypes, whitelisted_extensions, directory,
|
|
|
|
verbose=False):
|
|
|
|
magic_cookie = magic.open(magic.MAGIC_MIME_TYPE)
|
|
|
|
magic_cookie.load()
|
2010-08-02 20:27:37 +02:00
|
|
|
|
2010-12-22 01:28:00 +01:00
|
|
|
for root, dirs, files in os.walk(directory):
|
|
|
|
for f in files:
|
2010-12-27 15:20:49 +01:00
|
|
|
mimetype = magic_cookie.file(os.path.join(root, f))
|
2010-12-22 01:28:00 +01:00
|
|
|
if mimetype not in whitelisted_mimetypes:
|
2010-12-27 15:20:49 +01:00
|
|
|
if not [x for x in whitelisted_extensions
|
|
|
|
if f.lower().endswith(x)]:
|
2010-12-22 01:28:00 +01:00
|
|
|
if verbose:
|
|
|
|
print "%s (%s)" % (os.path.join(root, f), mimetype)
|
|
|
|
else:
|
|
|
|
print os.path.join(root, f)
|
2010-12-22 22:43:21 +01:00
|
|
|
for vcs_dir in (".bzr", "CVS", ".git", ".svn"):
|
|
|
|
if vcs_dir in dirs:
|
|
|
|
dirs.remove(vcs_dir)
|
2010-08-02 20:27:37 +02:00
|
|
|
|
2010-12-27 15:20:49 +01:00
|
|
|
def main():
|
2010-12-22 01:28:00 +01:00
|
|
|
script_name = os.path.basename(sys.argv[0])
|
|
|
|
usage = "%s [options]" % (script_name)
|
|
|
|
epilog = "See %s(1) for more info." % (script_name)
|
|
|
|
parser = optparse.OptionParser(usage=usage, epilog=epilog)
|
2010-08-02 20:27:37 +02:00
|
|
|
|
2010-12-22 01:28:00 +01:00
|
|
|
parser.add_option("-v", "--verbose", help="print more information",
|
|
|
|
dest="verbose", action="store_true", default=False)
|
|
|
|
parser.add_option("-d", "--directory",
|
|
|
|
help="check the files in the specified directory",
|
|
|
|
dest="directory", default=".")
|
|
|
|
parser.add_option("-m", "--mimetype", metavar="MIMETYPE",
|
|
|
|
help="Add MIMETYPE to list of whitelisted mimetypes.",
|
|
|
|
dest="whitelisted_mimetypes", action="append",
|
2010-12-22 22:43:21 +01:00
|
|
|
default=DEFAULT_WHITELISTED_MIMETYPES)
|
2010-12-22 01:28:00 +01:00
|
|
|
parser.add_option("-e", "--extension", metavar="EXTENSION",
|
|
|
|
help="Add EXTENSION to list of whitelisted extensions.",
|
|
|
|
dest="whitelisted_extensions", action="append",
|
2010-12-22 22:43:21 +01:00
|
|
|
default=DEFAULT_WHITELISTED_EXTENSIONS)
|
2010-08-02 20:27:37 +02:00
|
|
|
|
2010-12-22 01:28:00 +01:00
|
|
|
(options, args) = parser.parse_args()
|
2010-08-02 20:27:37 +02:00
|
|
|
|
2010-12-22 01:28:00 +01:00
|
|
|
if len(args) != 0:
|
|
|
|
Logger.error("This script does not take any additional parameters.")
|
|
|
|
sys.exit(1)
|
2010-08-02 20:27:37 +02:00
|
|
|
|
2010-12-22 22:43:21 +01:00
|
|
|
whitelisted_extensions = [x.lower() for x in options.whitelisted_extensions]
|
2010-12-27 15:20:49 +01:00
|
|
|
suspicious_source(options.whitelisted_mimetypes, whitelisted_extensions,
|
|
|
|
options.directory, options.verbose)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|