#!/usr/bin/env python2.7 # # Tool to find all pax flags from ebuilds. # __author__ = "Jens Kasten" __copyright__ = "Copyright 2013, RSBAC" __license__ = "GPL" __version__ = "0.1" __maintainer__ = "Jens Kasten" __email__ = "igraltist@rsbac.org" __status__ = "Testing" import sys import os import re # global list PAX_MARKING = [] # exclude list for searching directories EXCLUDE_DIRS = ["rsbac.dat", "metadata", "eclass", "distfiles"] # search pattern for pax marking and pax flags and paths PATTERN = re.compile(r'(?Ppaxctl|pax-mark) -(?P[pmsrx]+|[PMSRX]+) "\${.*}"(?P.+)') def _create_rsbac_pax(pax_file, pax_flags): """Add the created string to list.""" temp = "attr_set_file_dir FILE %s pax_flags %s" % (pax_file, pax_flags) PAX_MARKING.append(temp) def _multiple_files(files): """Split the bash shortcut into multiple full path names. e.g /usr/bin/wine{,-preloader} """ file_list = [] temp = files.replace("}", "").split("{") # add the base file to list file_list.append(temp[0]) # split into list and append it to file_list for file_ in temp[1].split(","): # exclude empty string if len(file_) > 0: # concat and append to list file_list.append(temp[0] + file_) if len(file_list) == 0: raise Exception("wrong bash shortcut: %s" % files) return file_list def _find_pax_marking(line): """Search for pattern in given line.""" match = PATTERN.search(line) if match is not None: marker = match.group("marker") pax_flags = match.group("flags") bin_path = match.group("path").split(" ")[0] print "*" * 80 print line.strip() print "marker: %s; flags: %s; path: %s" % (marker, pax_flags, bin_path) # check for bash shortcuts if re.search("{", bin_path): for file_ in _multiple_files(bin_path): _create_rsbac_pax(file_, pax_flags) else: _create_rsbac_pax(bin_path, pax_flags) def get_pax_marking(file_name): """Return pax marking flags as list or None.""" try: with open(file_name) as fd: for line in fd.readlines(): if len(line) > 0: _find_pax_marking(line) if len(PAX_MARKING) > 0: return PAX_MARKING except IOError, error: print error def get_all_ebuilds(): ebuild_list = [] for apps in os.listdir("/usr/portage"): apps_dir = os.path.join("/usr/portage", apps) if apps in EXCLUDE_DIRS: continue if os.path.isfile(apps_dir): continue # iter over first sub directory e.g sys-apps for app in os.listdir(apps_dir): app_dir = os.path.join(apps_dir, app) if os.path.isdir(app_dir): for ebuild in os.listdir(app_dir): ebuild_path = os.path.join(app_dir, ebuild) if ebuild.endswith(".ebuild"): ebuild_list.append(ebuild_path) return ebuild_list if __name__ == "__main__": import argparse # parse commandline parser = argparse.ArgumentParser() parser.add_argument("-f", "--FILE", type=str, help="File to find pax marking flags.") args = parser.parse_args() # switch for single file if args.FILE is None: for ebuild in get_all_ebuilds(): get_pax_marking(ebuild) else: get_pax_marking(args.FILE) #print PAX_MARKING