Commit 788ea15b authored by Nikos Skalkotos's avatar Nikos Skalkotos

Always check if a file exists before opening it

It is better to create warnings that RuntimeError exceptions.
parent 130e9f64
......@@ -29,6 +29,11 @@ class Bsd(Unix):
def _cleanup_password(self):
"""Remove all passwords and lock all user accounts"""
if not self.image.g.is_file('/etc/master.passwd'):
self.out.warn(
"File: `/etc/master.passwd' is missing. Nothing to do!")
return
master_passwd = []
for line in self.image.g.cat('/etc/master.passwd').splitlines():
......
......@@ -72,49 +72,54 @@ class Linux(Unix):
def _remove_user_accounts(self):
"""Remove all user accounts with id greater than 1000"""
if 'USERS' not in self.meta:
return
# Remove users from /etc/passwd
passwd = []
removed_users = {}
metadata_users = self.meta['USERS'].split()
for line in self.image.g.cat('/etc/passwd').splitlines():
fields = line.split(':')
if int(fields[2]) > 1000:
removed_users[fields[0]] = fields
# remove it from the USERS metadata too
if fields[0] in metadata_users:
metadata_users.remove(fields[0])
else:
passwd.append(':'.join(fields))
self.meta['USERS'] = " ".join(metadata_users)
# Delete the USERS metadata if empty
if not len(self.meta['USERS']):
del self.meta['USERS']
self.image.g.write('/etc/passwd', '\n'.join(passwd) + '\n')
# Remove users from /etc/passwd
if self.image.g.is_file('/etc/passwd'):
passwd = []
metadata_users = self.meta['USERS'].split()
for line in self.image.g.cat('/etc/passwd').splitlines():
fields = line.split(':')
if int(fields[2]) > 1000:
removed_users[fields[0]] = fields
# remove it from the USERS metadata too
if fields[0] in metadata_users:
metadata_users.remove(fields[0])
else:
passwd.append(':'.join(fields))
# Remove the corresponding /etc/shadow entries
shadow = []
for line in self.image.g.cat('/etc/shadow').splitlines():
fields = line.split(':')
if fields[0] not in removed_users:
shadow.append(':'.join(fields))
self.meta['USERS'] = " ".join(metadata_users)
self.image.g.write('/etc/shadow', "\n".join(shadow) + '\n')
# Delete the USERS metadata if empty
if not len(self.meta['USERS']):
del self.meta['USERS']
# Remove the corresponding /etc/group entries
group = []
for line in self.image.g.cat('/etc/group').splitlines():
fields = line.split(':')
# Remove groups tha have the same name as the removed users
if fields[0] not in removed_users:
group.append(':'.join(fields))
self.image.g.write('/etc/passwd', '\n'.join(passwd) + '\n')
else:
self.out.warn("File: `/etc/passwd' is missing. "
"No users were deleted")
return
self.image.g.write('/etc/group', '\n'.join(group) + '\n')
if self.image.g.is_file('/etc/shadow'):
# Remove the corresponding /etc/shadow entries
shadow = []
for line in self.image.g.cat('/etc/shadow').splitlines():
fields = line.split(':')
if fields[0] not in removed_users:
shadow.append(':'.join(fields))
self.image.g.write('/etc/shadow', "\n".join(shadow) + '\n')
else:
self.out.warn("File: `/etc/shadow' is missing.")
if self.image.g.is_file('/etc/group'):
# Remove the corresponding /etc/group entries
group = []
for line in self.image.g.cat('/etc/group').splitlines():
fields = line.split(':')
# Remove groups tha have the same name as the removed users
if fields[0] not in removed_users:
group.append(':'.join(fields))
self.image.g.write('/etc/group', '\n'.join(group) + '\n')
# Remove home directories
for home in [field[5] for field in removed_users.values()]:
......@@ -216,6 +221,10 @@ class Linux(Unix):
going to shrink the image you should probably disable this.
"""
if not self.image.g.is_file('/etc/fstab'):
self.out.warn("File: `/etc/fstab' is missing. No entry removed!")
return
new_fstab = ""
fstab = self.image.g.cat('/etc/fstab')
for line in fstab.splitlines():
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment