+ 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 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')
+
+ # 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()]:
+ if self.image.g.is_dir(home) and home.startswith('/home/'):
+ self.image.g.rm_rf(home)
+
+ @sysprep('Cleaning up password & locking all user accounts')
+ def cleanup_passwords(self):
+ """Remove all passwords and lock all user accounts"""
+
+ shadow = []
+
+ for line in self.image.g.cat('/etc/shadow').splitlines():
+ fields = line.split(':')
+ if fields[1] not in ('*', '!'):
+ fields[1] = '!'
+
+ shadow.append(":".join(fields))
+
+ self.image.g.write('/etc/shadow', "\n".join(shadow) + '\n')
+
+ @sysprep('Fixing acpid powerdown action')
+ def fix_acpid(self):
+ """Replace acpid powerdown action scripts to immediately shutdown the
+ system without checking if a GUI is running.
+ """
+
+ powerbtn_action = '#!/bin/sh\n\nPATH=/sbin:/bin:/usr/bin\n' \
+ 'shutdown -h now "Power button pressed"\n'
+
+ events_dir = '/etc/acpi/events'
+ if not self.image.g.is_dir(events_dir):
+ self.out.warn("No acpid event directory found")
+ return