Skip to content
Snippets Groups Projects
Commit 763207d9 authored by Sven Mäder's avatar Sven Mäder :speech_balloon:
Browse files

Add home owner and permission check

parent 214b0ac8
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env python3
import os
import sys
import pwd
import grp
import stat
owner = []
permission = []
class Home(object):
"""
Holds info about a home directory
"""
def __init__(self, name, path, st):
self.name = name
self.path = path
self.st = st
@property
def uid(self):
return self.st.st_uid
@property
def gid(self):
return self.st.st_gid
@property
def uname(self):
return pwd.getpwuid(self.uid).pw_name
@property
def gname(self):
return grp.getgrgid(self.gid).gr_name
@property
def filemode(self):
return stat.filemode(self.st.st_mode)
def __str__(self):
return ' '.join([self.filemode, self.uname, self.gname, self.path])
def check_homes(top):
for f in os.listdir(top):
pathname = os.path.join(top, f)
st = os.stat(pathname)
home = Home(f, pathname, st)
if bad_owner(home):
owner.append(home)
if bad_permission(home):
permission.append(home)
def bad_owner(home):
if home.name == home.uname and home.name == home.gname:
return False
return True
def bad_permission(home):
# d---------
if home.st.st_mode == stat.S_IFDIR:
return False
# drwx------
elif home.st.st_mode == stat.S_IFDIR | stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR:
return False
return True
def list_homes(homes):
for home in homes:
print(home)
if __name__ == '__main__':
arg_count = len(sys.argv) - 1
for i, arg in enumerate(sys.argv):
if i == 0:
continue
check_homes(arg)
if owner:
print('bad owner or group:')
print('-------------------')
list_homes(owner)
print()
if permission:
print('bad permission:')
print('---------------')
list_homes(permission)
print()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment