Statistics
| Branch: | Tag: | Revision:

root / image_creator / os_type / unix.py @ 9297c398

History | View | Annotate | Download (1.7 kB)

1
#!/usr/bin/env python
2

    
3
import re
4
import sys
5

    
6
from image_creator.os_type import OSBase
7

    
8

    
9
class Unix(OSBase):
10

    
11
    sensitive_userdata = ['.bash_history']
12

    
13
    def get_metadata(self):
14
        meta = super(Unix, self).get_metadata()
15
        meta["USERS"] = " ".join(self.get_passworded_users())
16
        return meta
17

    
18
    def get_passworded_users(self):
19
        users = []
20
        regexp = re.compile('(\S+):((?:!\S+)|(?:[^!*]\S+)|):(?:\S*:){6}')
21

    
22
        for line in self.g.cat('/etc/shadow').splitlines():
23
            match = regexp.match(line)
24
            if not match:
25
                continue
26

    
27
            user, passwd = match.groups()
28
            if len(passwd) > 0 and passwd[0] == '!':
29
                print "Warning: Ignoring locked %s account." % user
30
            else:
31
                users.append(user)
32

    
33
        return users
34

    
35
    def data_cleanup(self):
36
        self.cleanup_userdata()
37
        self.cleanup_tmp()
38
        self.cleanup_log()
39
        self.cleanup_mail()
40

    
41
    def cleanup_tmp(self):
42
        self.foreach_file('/tmp', self.g.rm_rf, maxdepth=1)
43
        self.foreach_file('/var/tmp', self.g.rm_rf, maxdepth=1)
44

    
45
    def cleanup_log(self):
46
        self.foreach_file('/var/log', self.g.truncate, ftype='r')
47

    
48
    def cleanup_mail(self):
49
        self.foreach_file('var/spool/mail', self.g.rm_rf, maxdepth=1)
50
        self.foreach_file('var/mail', self.g.rm_rf, maxdepth=1)
51

    
52
    def cleanup_userdata(self):
53
        homedirs = ['/root'] + self.ls('/home/')
54

    
55
        for homedir in homedirs:
56
            for data in self.sensitive_userdata:
57
                fname = "%s/%s" % (homedir, data)
58
                if self.g.is_file(fname):
59
                    self.g.scrub_file(fname)
60

    
61
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :