Monitor passworded users in FreeBSD
authorNikos Skalkotos <skalkoto@grnet.gr>
Tue, 21 May 2013 11:35:20 +0000 (14:35 +0300)
committerNikos Skalkotos <skalkoto@grnet.gr>
Tue, 21 May 2013 11:35:20 +0000 (14:35 +0300)
image_creator/os_type/freebsd.py

index 5feb9fb..b4eef28 100644 (file)
 
 from image_creator.os_type.unix import Unix
 
 
 from image_creator.os_type.unix import Unix
 
+import re
+
 
 class Freebsd(Unix):
     """OS class for FreeBSD Unix-like os"""
 
 class Freebsd(Unix):
     """OS class for FreeBSD Unix-like os"""
-    pass
+    def __init__(self, rootdev, ghandler, output):
+        super(Freebsd, self).__init__(rootdev, ghandler, output)
+
+        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):
+        users = []
+        regexp = re.compile(
+            '^([^:]+):((?:![^:]+)|(?:[^!*][^:]+)|):(?:[^:]*:){7}(?:[^:]*)'
+        )
+
+        for line in self.g.cat('/etc/master.passwd').splitlines():
+            line = line.split('#')[0]
+            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
 
 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
 
 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :