From ceaee7d23c9650ccb4d88e3d07c42a3443b75839 Mon Sep 17 00:00:00 2001 From: netblue30 Date: Wed, 4 Jan 2017 08:34:16 -0500 Subject: 3 new Python scripts in contrib section --- contrib/fix_private-bin.py | 68 +++++++++++++++++++++++++++++ contrib/fix_private-bin_for_symlinked_sh.py | 68 ----------------------------- contrib/fjclip.py | 35 +++++++++++++++ contrib/fjdisplay.py | 43 ++++++++++++++++++ contrib/fjresize.py | 25 +++++++++++ 5 files changed, 171 insertions(+), 68 deletions(-) create mode 100755 contrib/fix_private-bin.py delete mode 100644 contrib/fix_private-bin_for_symlinked_sh.py create mode 100755 contrib/fjclip.py create mode 100755 contrib/fjdisplay.py create mode 100755 contrib/fjresize.py (limited to 'contrib') diff --git a/contrib/fix_private-bin.py b/contrib/fix_private-bin.py new file mode 100755 index 000000000..705e46e46 --- /dev/null +++ b/contrib/fix_private-bin.py @@ -0,0 +1,68 @@ +#!/usr/bin/python3 + +import sys, os, glob, re + +privRx=re.compile("^(?:#\s*)?private-bin") + +def fixSymlinkedBins(files, replMap): + rxs=dict() + for (old,new) in replMap.items(): + rxs[old]=re.compile("\\b"+old+"\\b") + rxs[new]=re.compile("\\b"+new+"\\b") + print(rxs) + + for filename in files: + lines=None + with open(filename,"r") as file: + lines=file.readlines() + + shouldUpdate=False + for (i,line) in enumerate(lines): + if privRx.search(line): + for (old,new) in replMap.items(): + if rxs[old].search(line) and not rxs[new].search(line): + lines[i]=rxs[old].sub(old+","+new, line) + shouldUpdate=True + print(lines[i]) + + if shouldUpdate: + with open(filename,"w") as file: + file.writelines(lines) + pass + +def createListOfBinaries(files): + s=set() + for filename in files: + lines=None + with open(filename,"r") as file: + for line in file: + if privRx.search(line): + bins=line.split(",") + bins[0]=bins[0].split(" ")[-1] + bins = [n.strip() for n in bins] + s=s|set(bins) + return s + +def createSymlinkTable(binDirs, binariesSet): + m=dict() + for sh in binariesSet: + for bD in binDirs: + p=bD+os.path.sep+sh + if os.path.exists(p): + if os.path.islink(p): + m[sh]=os.readlink(p) + else: + pass + break + return m + + +sh="sh" +binDirs=["/bin","/usr/bin","/usr/sbin","/usr/local/bin","/usr/local/sbin"] +profilesPath="." +files=glob.glob(profilesPath+os.path.sep+"*.profile") + +bins=createListOfBinaries(files) +stbl=createSymlinkTable(binDirs,bins) +print(stbl) +fixSymlinkedBins(files,{a[0]:a[1] for a in stbl.items() if a[0].find("/") < 0 and a[1].find("/")<0}) diff --git a/contrib/fix_private-bin_for_symlinked_sh.py b/contrib/fix_private-bin_for_symlinked_sh.py deleted file mode 100644 index 705e46e46..000000000 --- a/contrib/fix_private-bin_for_symlinked_sh.py +++ /dev/null @@ -1,68 +0,0 @@ -#!/usr/bin/python3 - -import sys, os, glob, re - -privRx=re.compile("^(?:#\s*)?private-bin") - -def fixSymlinkedBins(files, replMap): - rxs=dict() - for (old,new) in replMap.items(): - rxs[old]=re.compile("\\b"+old+"\\b") - rxs[new]=re.compile("\\b"+new+"\\b") - print(rxs) - - for filename in files: - lines=None - with open(filename,"r") as file: - lines=file.readlines() - - shouldUpdate=False - for (i,line) in enumerate(lines): - if privRx.search(line): - for (old,new) in replMap.items(): - if rxs[old].search(line) and not rxs[new].search(line): - lines[i]=rxs[old].sub(old+","+new, line) - shouldUpdate=True - print(lines[i]) - - if shouldUpdate: - with open(filename,"w") as file: - file.writelines(lines) - pass - -def createListOfBinaries(files): - s=set() - for filename in files: - lines=None - with open(filename,"r") as file: - for line in file: - if privRx.search(line): - bins=line.split(",") - bins[0]=bins[0].split(" ")[-1] - bins = [n.strip() for n in bins] - s=s|set(bins) - return s - -def createSymlinkTable(binDirs, binariesSet): - m=dict() - for sh in binariesSet: - for bD in binDirs: - p=bD+os.path.sep+sh - if os.path.exists(p): - if os.path.islink(p): - m[sh]=os.readlink(p) - else: - pass - break - return m - - -sh="sh" -binDirs=["/bin","/usr/bin","/usr/sbin","/usr/local/bin","/usr/local/sbin"] -profilesPath="." -files=glob.glob(profilesPath+os.path.sep+"*.profile") - -bins=createListOfBinaries(files) -stbl=createSymlinkTable(binDirs,bins) -print(stbl) -fixSymlinkedBins(files,{a[0]:a[1] for a in stbl.items() if a[0].find("/") < 0 and a[1].find("/")<0}) diff --git a/contrib/fjclip.py b/contrib/fjclip.py new file mode 100755 index 000000000..cd12cd289 --- /dev/null +++ b/contrib/fjclip.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python + +import re +import sys +import subprocess +import fjdisplay + +usage = """fjclip.py src dest. src or dest can be named firejails or - for stdin or stdout. +firemon --x11 to see available running x11 firejails. firejail names can be shortened +to least ambiguous. for example 'work-libreoffice' can be shortened to 'work' if no +other firejails name starts with 'work'. +warning: browsers are dangerous. clipboards from browsers are dangerous. see +https://github.com/dxa4481/Pastejacking +fjclip.py strips whitespace from both +ends, but does nothing else to protect you. use a simple gui text editor like +gedit if you want to see what your pasting.""" + +if len(sys.argv) != 3 or sys.argv == '-h' or sys.argv == '--help': + print(usage) + exit(1) + +if sys.argv[1] == '-': + clipin_raw = sys.stdin.read() +else: + display = fjdisplay.getdisplay(sys.argv[1]) + clipin_raw = subprocess.check_output(['xsel','-b','--display',display]) + +clipin = clipin_raw.strip() + +if sys.argv[2] == '-': + print(clipin) +else: + display = fjdisplay.getdisplay(sys.argv[2]) + clipout = subprocess.Popen(['xsel','-b','-i','--display',display],stdin=subprocess.PIPE) + clipout.communicate(clipin) \ No newline at end of file diff --git a/contrib/fjdisplay.py b/contrib/fjdisplay.py new file mode 100755 index 000000000..0e0ef01ec --- /dev/null +++ b/contrib/fjdisplay.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python + +import re +import sys +import subprocess + +usage = """fjdisplay.py name-of-firejail +returns the display in the form of ':NNN' +""" + +def getfirejails(): + output = subprocess.check_output(['firemon','--x11']) + firejails = {} + name = '' + for line in output.split('\n'): + namematch = re.search('--name=(\w+\S*)',line) + if namematch: + name = namematch.group(1) + displaymatch = re.search('DISPLAY (:\d+)',line) + if displaymatch: + firejails[name] = displaymatch.group(1) + return firejails + +def getdisplay(name): + firejails = getfirejails() + fjlist = '\n'.join(firejails.keys()) + namere = re.compile('^'+name+'.*', re.MULTILINE) + matchingjails = namere.findall(fjlist) + if len(matchingjails) == 1: + return firejails[matchingjails[0]] + if len(matchingjails) == 0: + raise NameError("firejail {} does not exist".format(name)) + else: + raise NameError("ambiguous firejail name") + +if __name__ == '__main__': + if '-h' in sys.argv or '--help' in sys.argv or len(sys.argv) > 2: + print(usage) + exit() + if len(sys.argv) == 1: + print(getfirejails()) + if len(sys.argv) == 2: + print (getdisplay(sys.argv[1])) \ No newline at end of file diff --git a/contrib/fjresize.py b/contrib/fjresize.py new file mode 100755 index 000000000..52b289159 --- /dev/null +++ b/contrib/fjresize.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python + +import sys +import fjdisplay +import subprocess + +usage = """usage: fjresize.py firejail-name displaysize +resize firejail xephyr windows. +fjdisplay.py with no other arguments will list running named firejails with displays. +fjresize.py with only a firejail name will list valid resolutions. +names can be shortend as long its unambiguous. +note: you may need to move the xephyr window for the resize to take effect +example: + fjresize.py browser 1280x800 +""" + + +if len(sys.argv) == 2: + out = subprocess.check_output(['xrandr','--display',fjdisplay.getdisplay(sys.argv[1])]) + print(out) +elif len(sys.argv) == 3: + out = subprocess.check_output(['xrandr','--display',fjdisplay.getdisplay(sys.argv[1]),'--output','default','--mode',sys.argv[2]]) + print(out) +else: + print(usage) \ No newline at end of file -- cgit v1.2.3-54-g00ecf