Revision 121f3bc0 image_creator/os_type/freebsd.py

b/image_creator/os_type/freebsd.py
1
# -*- coding: utf-8 -*-
2
#
1 3
# Copyright 2012 GRNET S.A. All rights reserved.
2 4
#
3 5
# Redistribution and use in source and binary forms, with or
......
31 33
# interpreted as representing official policies, either expressed
32 34
# or implied, of GRNET S.A.
33 35

  
36
"""This module hosts OS-specific code for FreeBSD."""
37

  
34 38
from image_creator.os_type.unix import Unix, sysprep
35 39

  
36 40
import re
......
41 45
    def __init__(self, rootdev, ghandler, output):
42 46
        super(Freebsd, self).__init__(rootdev, ghandler, output)
43 47

  
44
    def _do_collect_metadata(self):
48
    @sysprep()
49
    def cleanup_password(self, print_header=True):
50
        """Remove all passwords and lock all user accounts"""
51

  
52
        if print_header:
53
            self.out.output("Cleaning up passwords & locking all user "
54
                            "accounts")
55

  
56
        master_passwd = []
57

  
58
        for line in self.g.cat('/etc/master.passwd').splitlines():
59

  
60
            # Check for empty or comment lines
61
            if len(line.split('#')[0]) == 0:
62
                master_passwd.append(line)
63
                continue
64

  
65
            fields = line.split(':')
66
            if fields[1] not in ('*', '!'):
67
                fields[1] = '!'
68

  
69
            master_passwd.append(":".join(fields))
70

  
71
        self.g.write('/etc/master.passwd', "\n".join(master_passwd) + '\n')
72

  
73
        # Make sure no one can login on the system
74
        self.g.rm_rf('/etc/spwd.db')
45 75

  
76
    def _do_collect_metadata(self):
77
        """Collect metadata about the OS"""
46 78
        super(Freebsd, self)._do_collect_metadata()
47 79
        self.meta["USERS"] = " ".join(self._get_passworded_users())
48 80

  
......
56 88
            del self.meta['USERS']
57 89

  
58 90
    def _get_passworded_users(self):
91
        """Returns a list of non-locked user accounts"""
59 92
        users = []
60 93
        regexp = re.compile(
61 94
            '^([^:]+):((?:![^:]+)|(?:[^!*][^:]+)|):(?:[^:]*:){7}(?:[^:]*)'
......
80 113

  
81 114
        critical_mpoints = ('/', '/etc', '/root', '/home', '/var')
82 115

  
83
        # libguestfs can't handle correct freebsd partitions on GUID
84
        # Partition Table. We have to do the translation to linux
85
        # device names ourselves
116
        # libguestfs can't handle correct freebsd partitions on a GUID
117
        # Partition Table. We have to do the translation to linux device names
118
        # ourselves
86 119
        guid_device = re.compile('^/dev/((?:ada)|(?:vtbd))(\d+)p(\d+)$')
87 120

  
88 121
        mopts = "ufstype=ufs2,%s" % ('ro' if readonly else 'rw')
89 122
        for mp, dev in self._mountpoints():
90 123
            match = guid_device.match(dev)
91 124
            if match:
92
                m2 = int(match.group(2))
93
                m3 = int(match.group(3))
94
                dev = '/dev/sd%c%d' % (chr(ord('a') + m2), m3)
125
                group2 = int(match.group(2))
126
                group3 = int(match.group(3))
127
                dev = '/dev/sd%c%d' % (chr(ord('a') + group2), group3)
95 128
            try:
96 129
                self.g.mount_vfs(mopts, 'ufs', dev, mp)
97 130
            except RuntimeError as msg:
......
103 136

  
104 137
        return True
105 138

  
106
    @sysprep()
107
    def cleanup_password(self, print_header=True):
108
        """Remove all passwords and lock all user accounts"""
109

  
110
        if print_header:
111
            self.out.output("Cleaning up passwords & locking all user "
112
                            "accounts")
113

  
114
        master_passwd = []
115

  
116
        for line in self.g.cat('/etc/master.passwd').splitlines():
117

  
118
            # Check for empty or comment lines
119
            if len(line.split('#')[0]) == 0:
120
                master_passwd.append(line)
121
                continue
122

  
123
            fields = line.split(':')
124
            if fields[1] not in ('*', '!'):
125
                fields[1] = '!'
126

  
127
            master_passwd.append(":".join(fields))
128

  
129
        self.g.write('/etc/master.passwd', "\n".join(master_passwd) + '\n')
130

  
131
        # Make sure no one can login on the system
132
        self.g.rm_rf('/etc/spwd.db')
133

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

Also available in: Unified diff