Merge branch 'develop'
[snf-image-creator] / image_creator / os_type / linux.py
index fd45152..4724336 100644 (file)
@@ -1,3 +1,5 @@
+# -*- coding: utf-8 -*-
+#
 # Copyright 2012 GRNET S.A. All rights reserved.
 #
 # Redistribution and use in source and binary forms, with or
@@ -31,6 +33,8 @@
 # interpreted as representing official policies, either expressed
 # or implied, of GRNET S.A.
 
+"""This module hosts OS-specific code for Linux"""
+
 from image_creator.os_type.unix import Unix, sysprep
 
 import re
@@ -38,22 +42,87 @@ import time
 
 
 class Linux(Unix):
+    """OS class for Linux"""
     def __init__(self, rootdev, ghandler, output):
         super(Linux, self).__init__(rootdev, ghandler, output)
         self._uuid = dict()
         self._persistent = re.compile('/dev/[hsv]d[a-z][1-9]*')
 
-    def is_persistent(self, dev):
-        return not self._persistent.match(dev)
+    @sysprep(enabled=False)
+    def remove_user_accounts(self, print_header=True):
+        """Remove all user accounts with id greater than 1000"""
 
-    def get_uuid(self, dev):
-        if dev in self._uuid:
-            return self._uuid[dev]
+        if print_header:
+            self.out.output("Removing all user accounts with id greater than "
+                            "1000")
 
-        uuid = self.g.vfs_uuid(dev)
-        assert len(uuid)
-        self._uuid[dev] = uuid
-        return uuid
+        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.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.g.write('/etc/passwd', '\n'.join(passwd) + '\n')
+
+        # Remove the corresponding /etc/shadow entries
+        shadow = []
+        for line in self.g.cat('/etc/shadow').splitlines():
+            fields = line.split(':')
+            if fields[0] not in removed_users:
+                shadow.append(':'.join(fields))
+
+        self.g.write('/etc/shadow', "\n".join(shadow) + '\n')
+
+        # Remove the corresponding /etc/group entries
+        group = []
+        for line in self.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.g.write('/etc/group', '\n'.join(group) + '\n')
+
+        # Remove home directories
+        for home in [field[5] for field in removed_users.values()]:
+            if self.g.is_dir(home) and home.startswith('/home/'):
+                self.g.rm_rf(home)
+
+    @sysprep()
+    def cleanup_passwords(self, print_header=True):
+        """Remove all passwords and lock all user accounts"""
+
+        if print_header:
+            self.out.output("Cleaning up passwords & locking all user "
+                            "accounts")
+
+        shadow = []
+
+        for line in self.g.cat('/etc/shadow').splitlines():
+            fields = line.split(':')
+            if fields[1] not in ('*', '!'):
+                fields[1] = '!'
+
+            shadow.append(":".join(fields))
+
+        self.g.write('/etc/shadow', "\n".join(shadow) + '\n')
 
     @sysprep()
     def fix_acpid(self, print_header=True):
@@ -74,21 +143,21 @@ class Linux(Unix):
 
         event_exp = re.compile('event=(.+)', re.I)
         action_exp = re.compile('action=(.+)', re.I)
-        for f in self.g.readdir(events_dir):
-            if f['ftyp'] != 'r':
+        for events_file in self.g.readdir(events_dir):
+            if events_file['ftyp'] != 'r':
                 continue
 
-            fullpath = "%s/%s" % (events_dir, f['name'])
+            fullpath = "%s/%s" % (events_dir, events_file['name'])
             event = ""
             action = ""
             for line in self.g.cat(fullpath).splitlines():
-                m = event_exp.match(line)
-                if m:
-                    event = m.group(1)
+                match = event_exp.match(line)
+                if match:
+                    event = match.group(1)
                     continue
-                m = action_exp.match(line)
-                if m:
-                    action = m.group(1)
+                match = action_exp.match(line)
+                if match:
+                    action = match.group(1)
                     continue
 
             if event.strip() in ("button[ /]power", "button/power.*"):
@@ -156,12 +225,12 @@ class Linux(Unix):
     @sysprep()
     def use_persistent_block_device_names(self, print_header=True):
         """Scan fstab & grub configuration files and replace all non-persistent
-        device appearences with UUIDs.
+        device references with UUIDs.
         """
 
         if print_header:
             self.out.output("Replacing fstab & grub non-persistent device "
-                            "appearences")
+                            "references")
 
         # convert all devices in fstab to persistent
         persistent_root = self._persistent_fstab()
@@ -170,6 +239,9 @@ class Linux(Unix):
         self._persistent_grub1(persistent_root)
 
     def _persistent_grub1(self, new_root):
+        """Replaces non-persistent device name occurencies with persistent
+        ones in GRUB1 configuration files.
+        """
         if self.g.is_file('/boot/grub/menu.lst'):
             grub1 = '/boot/grub/menu.lst'
         elif self.g.is_file('/etc/grub.conf'):
@@ -182,7 +254,7 @@ class Linux(Unix):
             roots = self.g.aug_match('/files%s/title[*]/kernel/root' % grub1)
             for root in roots:
                 dev = self.g.aug_get(root)
-                if not self.is_persistent(dev):
+                if not self._is_persistent(dev):
                     # This is not always correct. Grub may contain root entries
                     # for other systems, but we only support 1 OS per hard
                     # disk, so this shouldn't harm.
@@ -192,6 +264,9 @@ class Linux(Unix):
             self.g.aug_close()
 
     def _persistent_fstab(self):
+        """Replaces non-persistent device name occurencies in /etc/fstab with
+        persistent ones.
+        """
         mpoints = self.g.mountpoints()
         if len(mpoints) == 0:
             pass  # TODO: error handling
@@ -216,6 +291,9 @@ class Linux(Unix):
         return root_dev
 
     def _convert_fstab_line(self, line, devices):
+        """Replace non-persistent device names in an fstab line to their UUID
+        equivalent
+        """
         orig = line
         line = line.split('#')[0].strip()
         if len(line) == 0:
@@ -229,9 +307,9 @@ class Linux(Unix):
         dev = entry[0]
         mpoint = entry[1]
 
-        if not self.is_persistent(dev):
+        if not self._is_persistent(dev):
             if mpoint in devices:
-                dev = "UUID=%s" % self.get_uuid(devices[mpoint])
+                dev = "UUID=%s" % self._get_uuid(devices[mpoint])
                 entry[0] = dev
             else:
                 # comment out the entry
@@ -240,4 +318,46 @@ class Linux(Unix):
 
         return orig, dev, mpoint
 
+    def _do_collect_metadata(self):
+        """Collect metadata about the OS"""
+        super(Linux, self)._do_collect_metadata()
+        self.meta["USERS"] = " ".join(self._get_passworded_users())
+
+        # Delete the USERS metadata if empty
+        if not len(self.meta['USERS']):
+            self.out.warn("No passworded users found!")
+            del self.meta['USERS']
+
+    def _get_passworded_users(self):
+        """Returns a list of non-locked user accounts"""
+        users = []
+        regexp = re.compile('(\S+):((?:!\S+)|(?:[^!*]\S+)|):(?:\S*:){6}')
+
+        for line in self.g.cat('/etc/shadow').splitlines():
+            match = regexp.match(line)
+            if not match:
+                continue
+
+            user, passwd = match.groups()
+            if len(passwd) > 0 and passwd[0] == '!':
+                self.out.warn("Ignoring locked %s account." % user)
+            else:
+                users.append(user)
+
+        return users
+
+    def _is_persistent(self, dev):
+        """Checks if a device name is persistent."""
+        return not self._persistent.match(dev)
+
+    def _get_uuid(self, dev):
+        """Returns the UUID corresponding to a device"""
+        if dev in self._uuid:
+            return self._uuid[dev]
+
+        uuid = self.g.vfs_uuid(dev)
+        assert len(uuid)
+        self._uuid[dev] = uuid
+        return uuid
+
 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :