Revision a9c9d939
b/image_creator/os_type/unix.py | ||
---|---|---|
70 | 70 |
|
71 | 71 |
return users |
72 | 72 |
|
73 |
@exclude_task |
|
74 |
def data_cleanup_user_accounts(self, print_header=True): |
|
75 |
"""Remove all user account with id more than 1000""" |
|
76 |
|
|
77 |
if print_header: |
|
78 |
output('Removing all user accounts with id greater than 1000') |
|
79 |
|
|
80 |
# Remove users from /etc/passwd |
|
81 |
passwd = [] |
|
82 |
removed_users = {} |
|
83 |
for line in self.g.cat('/etc/passwd').splitlines(): |
|
84 |
fields = line.split(':') |
|
85 |
if int(fields[2]) > 1000: |
|
86 |
removed_users[fields[0]] = fields |
|
87 |
else: |
|
88 |
passwd.append(':'.join(fields)) |
|
89 |
|
|
90 |
self.g.write('/etc/passwd', '\n'.join(passwd) + '\n') |
|
91 |
|
|
92 |
# Remove the corresponding /etc/shadow entries |
|
93 |
shadow = [] |
|
94 |
for line in self.g.cat('/etc/shadow').splitlines(): |
|
95 |
fields = line.split(':') |
|
96 |
if fields[0] not in removed_users: |
|
97 |
shadow.append(':'.join(fields)) |
|
98 |
|
|
99 |
self.g.write('/etc/shadow', "\n".join(shadow) + '\n') |
|
100 |
|
|
101 |
# Remove the corresponding /etc/group entries |
|
102 |
group = [] |
|
103 |
for line in self.g.cat('/etc/group').splitlines(): |
|
104 |
fields = line.split(':') |
|
105 |
# Remove groups tha have the same name as the removed users |
|
106 |
if fields[0] not in removed_users: |
|
107 |
group.append(':'.join(fields)) |
|
108 |
|
|
109 |
self.g.write('/etc/group', '\n'.join(group) + '\n') |
|
110 |
|
|
111 |
# Remove home directories |
|
112 |
for home in [field[5] for field in removed_users.values()]: |
|
113 |
if self.g.is_dir(home) and home.startswith('/home/'): |
|
114 |
self.g.rm_rf(home) |
|
115 |
|
|
73 | 116 |
def data_cleanup_passwords(self, print_header=True): |
74 | 117 |
"""Remove all passwords and lock all user accounts""" |
75 | 118 |
|
... | ... | |
85 | 128 |
|
86 | 129 |
shadow.append(":".join(fields)) |
87 | 130 |
|
88 |
self.g.write('/etc/shadow', "\n".join(shadow)) |
|
131 |
self.g.write('/etc/shadow', "\n".join(shadow) + '\n')
|
|
89 | 132 |
|
90 | 133 |
def data_cleanup_cache(self, print_header=True): |
91 | 134 |
"""Remove all regular files under /var/cache""" |
Also available in: Unified diff