示例#1
0
class Lock:
    def __init__(self, _file, shared=False):
        lockfile = os.path.join(os.path.dirname(_file), ".%s" % os.path.basename(_file))
        try:
            self.lock = FileLock(_file)
            self.lock.lock(timeout=TIMEOUT, shared=shared)
        except IOError:
            fail(FAIL_TIMEOUT)
    
    def release(self):
        self.lock.unlock()
示例#2
0
class Lock:
    def __init__(self, _file, shared=False):
        lockfile = os.path.join(os.path.dirname(_file),
                                ".%s" % os.path.basename(_file))
        try:
            self.lock = FileLock(_file)
            self.lock.lock(timeout=TIMEOUT, shared=shared)
        except IOError:
            fail(FAIL_TIMEOUT)

    def release(self):
        self.lock.unlock()
示例#3
0
class grubParser:
    def __init__(self, _dir, write=False, timeout=-1):
        self.dir = _dir
        self.write = write
        self.grub_conf = os.path.join(_dir, "grub.conf")
        self.device_map = os.path.join(_dir, "device.map")
        self.device_remap = os.path.join(_dir, "device.remap")
        
        self.lock = FileLock("%s/.grub.lock" % _dir)
        try:
            self.lock.lock(write, timeout)
        except IOError:
            fail(FAIL_TIMEOUT)
        
        # Fail if grub is not installed
        if os.path.exists(self.grub_conf) and os.path.exists(self.device_map):
            self.config = grubConf()
            self.config.parseConf(self.grub_conf)
        else:
            self.fail(FAIL_NOGRUB)
        
        # Get device map
        mapped_devices = {}
        for line in file(self.device_map):
            try:
                grub_device, linux_device = line.split("\t")
            except ValueError:
                self.fail(FAIL_NOGRUB)
            mapped_devices[grub_device] = linux_device.strip()
        
        # Remap devices if necessary
        if not os.path.exists(self.device_remap):
            remapped_devices = {}
            for entry in self.config.entries:
                commands = entry.listCommands()
                if "root" in commands and "kernel" in commands:
                    grub_device = entry.getCommand("root").value
                    try:
                        grub_disk, grub_part, linux_disk, linux_part = parseGrubDevice(grub_device, remapped=False)
                    except TypeError:
                        self.fail(FAIL_NODEVICE % grub_device)
                    entry_version, entry_root = getKernelData(entry.getCommand("kernel"))
                    if entry_root:
                        if entry_root.startswith("LABEL="):
                            entry_root = getDeviceByLabel(entry_root.split("LABEL=")[1])
                        try:
                            linux_disk2, linux_part2, grub_disk2, grub_part2 = parseLinuxDevice(entry_root, remapped=False)
                        except:
                            self.fail(FAIL_NODEVICE % entry_root)
                        remapped_devices["(%s)" % grub_disk] = linux_disk2
            
            grub_devices = mapped_devices.keys()
            grub_devices.sort(key=lambda x: int(x[3:-1]))
            linux_devices = []
            for device in grub_devices:
                linux_devices.append(mapped_devices[device])
            if "(hd0)" in remapped_devices and remapped_devices["(hd0)"] != mapped_devices["(hd0)"]:
                # Need remapping
                linux_devices.remove(remapped_devices["(hd0)"])
                linux_devices.insert(0, remapped_devices["(hd0)"])
            list_map = []
            for i in xrange(len(grub_devices)):
                list_map.append("%s\t%s" % (grub_devices[i], linux_devices[i]))
            file(self.device_remap, "w").write("\n".join(list_map))
    
    def fail(self, msg):
        self.lock.unlock()
        fail(msg)
    
    def release(self, save=True):
        if save and self.write:
            self.config.write(self.grub_conf)
        self.lock.unlock()
示例#4
0
class grubParser:
    def __init__(self, _dir, write=False, timeout=-1):
        self.dir = _dir
        self.write = write
        self.grub_conf = os.path.join(_dir, "grub.conf")
        self.device_map = os.path.join(_dir, "device.map")
        self.device_remap = os.path.join(_dir, "device.remap")
        
        self.lock = FileLock("%s/.grub.lock" % _dir)
        try:
            self.lock.lock(write, timeout)
        except IOError:
            fail(FAIL_TIMEOUT)
        
        # Fail if grub is not installed
        if os.path.exists(self.grub_conf) and os.path.exists(self.device_map):
            self.config = grubConf()
            self.config.parseConf(self.grub_conf)
        else:
            self.fail(FAIL_NOGRUB)
        
        # Get device map
        mapped_devices = {}
        for line in file(self.device_map):
            try:
                grub_device, linux_device = line.split("\t")
            except ValueError:
                self.fail(FAIL_NOGRUB)
            mapped_devices[grub_device] = linux_device.strip()
        
        # Remap devices if necessary
        if not os.path.exists(self.device_remap):
            remapped_devices = {}
            for entry in self.config.entries:
                commands = entry.listCommands()
                if "root" in commands and "kernel" in commands:
                    grub_device = entry.getCommand("root").value
                    try:
                        grub_disk, grub_part, linux_disk, linux_part = parseGrubDevice(grub_device, remapped=False)
                    except TypeError:
                        self.fail(FAIL_NODEVICE % grub_device)
                    entry_version, entry_root = getKernelData(entry.getCommand("kernel"))
                    if entry_root:
                        if entry_root.startswith("LABEL="):
                            entry_root = getDeviceByLabel(entry_root.split("LABEL=")[1])
                        try:
                            linux_disk2, linux_part2, grub_disk2, grub_part2 = parseLinuxDevice(entry_root, remapped=False)
                        except:
                            self.fail(FAIL_NODEVICE % entry_root)
                        remapped_devices["(%s)" % grub_disk] = linux_disk2
            
            grub_devices = mapped_devices.keys()
            grub_devices.sort(key=lambda x: int(x[3:-1]))
            linux_devices = []
            for device in grub_devices:
                linux_devices.append(mapped_devices[device])
            if "(hd0)" in remapped_devices and remapped_devices["(hd0)"] != mapped_devices["(hd0)"]:
                # Need remapping
                linux_devices.remove(remapped_devices["(hd0)"])
                linux_devices.insert(0, remapped_devices["(hd0)"])
            list_map = []
            for i in xrange(len(grub_devices)):
                list_map.append("%s\t%s" % (grub_devices[i], linux_devices[i]))
            file(self.device_remap, "w").write("\n".join(list_map))
    
    def fail(self, msg):
        self.lock.unlock()
        fail(msg)
    
    def release(self, save=True):
        if save and self.write:
            self.config.write(self.grub_conf)
        self.lock.unlock()
示例#5
0
class Database:
    passwd_path = "/etc/passwd"
    shadow_path = "/etc/shadow"
    group_path = "/etc/group"
    lock_path = "/etc/.pwd.lock"

    def __init__(self, for_read=False):
        self.lock = FileLock(self.lock_path)
        self.lock.lock(shared=for_read)

        self.users = {}
        self.users_by_name = {}
        self.groups = {}
        self.groups_by_name = {}

        for line in file(self.passwd_path):
            if line != "" and line != "\n":
                parts = line.rstrip("\n").split(":")
                user = User()
                user.name = parts[0]
                user.uid = int(parts[2])
                user.gid = int(parts[3])
                user.realname = parts[4]
                user.homedir = parts[5]
                user.shell = parts[6]
                self.users[user.uid] = user
                self.users_by_name[user.name] = user

        for line in file(self.shadow_path):
            if line != "" and line != "\n":
                parts = line.rstrip("\n").split(":")
                if self.users_by_name.has_key(parts[0]):
                    user = self.users_by_name[parts[0]]
                    user.password = parts[1]
                    user.pwrest = parts[2:]

        for line in file(self.group_path):
            if line != "" and line != "\n":
                parts = line.rstrip("\n").split(":")
                group = Group()
                group.name = parts[0]
                group.gid = int(parts[2])
                group.members = parts[3].split(",")
                if "" in group.members:
                    group.members.remove("")
                self.groups[group.gid] = group
                self.groups_by_name[group.name] = group

    def sync(self):
        lines = []
        keys = self.users.keys()
        keys.sort()
        for uid in keys:
            user = self.users[uid]
            lines.append("%s:x:%d:%d:%s:%s:%s\n" %
                         (user.name, uid, user.gid, user.realname,
                          user.homedir, user.shell))
        f = file(self.passwd_path, "w")
        f.writelines(lines)
        f.close()

        lines = []
        keys = self.users.keys()
        keys.sort()
        for uid in keys:
            user = self.users[uid]
            if user.password:
                lines.append("%s:%s:%s\n" %
                             (user.name, user.password, ":".join(user.pwrest)))
        f = file(self.shadow_path, "w")
        f.writelines(lines)
        f.close()

        lines = []
        keys = self.groups.keys()
        keys.sort()
        for gid in keys:
            group = self.groups[gid]
            lines.append("%s:x:%s:%s\n" %
                         (group.name, gid, ",".join(group.members)))
        f = file(self.group_path, "w")
        f.writelines(lines)
        f.close()

    def set_groups(self, name, grouplist):
        for gid in self.groups.keys():
            g = self.groups[gid]
            if name in g.members:
                if not g.name in grouplist:
                    g.members.remove(name)
            else:
                if g.name in grouplist:
                    g.members.append(name)

    def next_uid(self):
        for i in range(uid_minimum, uid_maximum):
            if not self.users.has_key(i):
                return i

    def next_gid(self):
        for i in range(uid_minimum, uid_maximum):
            if not self.groups.has_key(i):
                return i
示例#6
0
class Database:
    passwd_path = "/etc/passwd"
    shadow_path = "/etc/shadow"
    group_path = "/etc/group"
    lock_path = "/etc/.pwd.lock"

    def __init__(self, for_read=False):
        self.lock = FileLock(self.lock_path)
        self.lock.lock(shared=for_read)

        self.users = {}
        self.users_by_name = {}
        self.groups = {}
        self.groups_by_name = {}

        for line in file(self.passwd_path):
            if line != "" and line != "\n":
                parts = line.rstrip("\n").split(":")
                user = User()
                user.name = parts[0]
                user.uid = int(parts[2])
                user.gid = int(parts[3])
                user.realname = parts[4]
                user.homedir = parts[5]
                user.shell = parts[6]
                self.users[user.uid] = user
                self.users_by_name[user.name] = user

        for line in file(self.shadow_path):
            if line != "" and line != "\n":
                parts = line.rstrip("\n").split(":")
                if self.users_by_name.has_key(parts[0]):
                    user = self.users_by_name[parts[0]]
                    user.password = parts[1]
                    user.pwrest = parts[2:]

        for line in file(self.group_path):
            if line != "" and line != "\n":
                parts = line.rstrip("\n").split(":")
                group = Group()
                group.name = parts[0]
                group.gid = int(parts[2])
                group.members = parts[3].split(",")
                if "" in group.members:
                    group.members.remove("")
                self.groups[group.gid] = group
                self.groups_by_name[group.name] = group

    def sync(self):
        lines = []
        keys = self.users.keys()
        keys.sort()
        for uid in keys:
            user = self.users[uid]
            lines.append("%s:x:%d:%d:%s:%s:%s\n" % (user.name, uid, user.gid, user.realname, user.homedir, user.shell))
        f = file(self.passwd_path, "w")
        f.writelines(lines)
        f.close()

        lines = []
        keys = self.users.keys()
        keys.sort()
        for uid in keys:
            user = self.users[uid]
            if user.password:
                lines.append("%s:%s:%s\n" % (user.name, user.password, ":".join(user.pwrest)))
        f = file(self.shadow_path, "w")
        f.writelines(lines)
        f.close()

        lines = []
        keys = self.groups.keys()
        keys.sort()
        for gid in keys:
            group = self.groups[gid]
            lines.append("%s:x:%s:%s\n" % (group.name, gid, ",".join(group.members)))
        f = file(self.group_path, "w")
        f.writelines(lines)
        f.close()

    def set_groups(self, name, grouplist):
        for gid in self.groups.keys():
            g = self.groups[gid]
            if name in g.members:
                if not g.name in grouplist:
                    g.members.remove(name)
            else:
                if g.name in grouplist:
                    g.members.append(name)

    def next_uid(self):
        for i in range(uid_minimum, uid_maximum):
            if not self.users.has_key(i):
                return i

    def next_gid(self):
        for i in range(uid_minimum, uid_maximum):
            if not self.groups.has_key(i):
                return i