ufpsutil/ufpsutil.py

69 lines
2.5 KiB
Python

from libufps import ufps
import libufps
from inspect import getmembers, isfunction
import os
import argparse
import create
import restore
parser = argparse.ArgumentParser(
prog='ufpsutil',
description=' Unix File Permission Supplicant Utility - A simple utility that indexes directories and records their Unix permission and ownership data.'
)
parser.add_argument('path', help = "Path to create index of or restore index to")
parser.add_argument("-c", "--create", help = "Create index", action = "store_true")
parser.add_argument("-r", "--restore", help = "Restore permissions from index", action = "store_true")
parser.add_argument("-i", "--index", help = "Index file to read from/write to (default is index.ufps)", default='index.ufps')
parser.add_argument("--i-know-what-im-doing", help = "Force the utility to run without root", action = "store_true")
args = parser.parse_args()
# Just make argument variables a bit more human readable.
basedir = args.path
indexFile = args.index
modeCreate = args.create
modeRestore = args.restore
forceWithoutRoot = args.i_know_what_im_doing
# Check that both -c and -r arent specified.
if modeCreate and modeRestore:
exit("[Fatal] Both --create and --restore specified. That doesn't make sense!")
# That said, at least -c or -r must be specified.
if not modeCreate and not modeRestore:
exit("[Fatal] I'm not sure what you want me to do. Must specify --create or --restore.")
# Check if user is running as root.
uid = os.geteuid()
if uid != 0:
# We make a special case for when the user is forcing the program to proceed.
# Otherwise, the program doesn't run without root.
if forceWithoutRoot == True:
print("[Warning] Running without root. This may cause issues when encountering files owned by other users!")
else:
exit("[Fatal] Root privileges are usually required for this utility to do it's job. If you know what you're doing, try running with --i-know-what-im-doing")
ufps = ufps.ufps(basedir)
if modeCreate:
create.createIndex(ufps, indexFile)
if modeRestore:
restore.restoreIndex(ufps, indexFile)
#basedir = "tests/testfiles/"
#basedirbad = "tests/testfiles_bad/"
#ufps = ufps.ufps(basedir)
#print("Regular dir: " + ufps.getInformationRecursive("."))
#print("Regular file: " + ufps.getInformationRecursive("0644"))
#print("Linked dir: " + ufps.getInformationRecursive("linktests/link"))
#print("Linked file: " + ufps.getInformationRecursive("linktests/linkfile"))
#index = ufps.createIndex("")
#file = open("index.ufps", "w")
#file.write(index)
#file.close()