aboutsummaryrefslogtreecommitdiffstats
path: root/share/Perms.py
blob: 60d52f782d06a99c9a874de661be6ad70707b6bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/python -O
#---------------------------------------------------------------
# Project         : Mandrake Linux
# Module          : msec
# File            : Perms.py
# Version         : $Id$
# Author          : Frederic Lepied
# Created On      : Fri Dec  7 23:33:49 2001
# Purpose         : fix permissions and owner/group of files
#                   and directories.
#---------------------------------------------------------------

import glob
import re
import string
import os
import stat
import pwd
import grp
import Config
import sys
from Log import *
import gettext

try:
    cat = gettext.Catalog('msec')
    _ = cat.gettext
except IOError:
    _ = str

comment_regex = re.compile('^\s*#|^\s*$')

user = {}
group = {}
userid = {}
groupid = {}

def get_user_id(name):
    try:
        return user[name]
    except KeyError:
        try:
            user[name] = pwd.getpwnam(name)[2]
        except KeyError:
            error(_('user name %s not found') % name)
            user[name] = -1
    return user[name]

def get_user_name(id):
    try:
        return userid[id]
    except KeyError:
        try:
            userid[id] = pwd.getpwuid(id)[0]
        except KeyError:
            error(_('user name not found for id %d') % id)
            userid[id] = str(id)
    return userid[id]

def get_group_id(name):
    try:
        return group[name]
    except KeyError:
        try:
            group[name] = grp.getgrnam(name)[2]
        except KeyError:
            error(_('group name %s not found') % name)
            group[name] = -1
    return group[name]

def get_group_name(id):
    try:
        return groupid[id]
    except KeyError:
        try:
            groupid[id] = grp.getgrgid(id)[0]
        except KeyError:
            error(_('group name not found for id %d') % id)
            groupid[id] = str(id)
    return groupid[id]

assoc = {}

def fix_perms(path):
    try:
        file = open(path, 'r')
    except IOError:
        return
    root = Config.get_config('root', '')
    lineno = 0
    for line in file.readlines():
        lineno = lineno + 1
        
        if comment_regex.search(line):
            continue

        fields = re.split('\s*', line)
        mode_str = fields[2]
        
        if mode_str == 'current':
            newmode = -1
        else:
            newmode = int(mode_str, 8)
        
        if fields[1] == 'current':
            user = group = -1
            user_str = group_str = ''
        else:
            (user_str, group_str) = string.split(fields[1], '.')
            user = get_user_id(user_str)
            group = get_group_id(group_str)
        
        if len(fields) == 4:
            for f in glob.glob(fields[0]):
                try:
                    full = os.stat(f)
                except OSError:
                    continue
                mode = stat.S_IMODE(full[stat.ST_MODE])
                newperm = newmode
                if stat.S_ISDIR(full[stat.ST_MODE]):
                    if newperm & 0400:
                        newperm = newperm | 0100
                    if newperm & 0040:
                        newperm = newperm | 0010
                    if newperm & 0004:
                        newperm = newperm | 0001
                uid = full[stat.ST_UID]
                gid = full[stat.ST_GID]
                if f != '/' and f[-1] == '/':
                    f = f[:-1]
                if f[-2:] == '/.':
                    f = f[:-2]
                assoc[f] = (mode, uid, gid, newperm, user, group, user_str, group_str)
        else:
            error(_('invalid syntax in %s line %d') % (path, lineno))
    file.close()

def act():
    for f in assoc.keys():
        (mode, uid, gid, newperm, user, group, user_str, group_str) = assoc[f]
        #print f, (mode, uid, gid, newperm, user, group)
        if newperm != -1 and mode != newperm:
            log(_('changed mode of %s from %o to %o') % (f, mode, newperm))
            try:
                os.chmod(f, newperm)
            except:
                error('chmod %s %o: %s' % (f, newperm, sys.exc_value[0]))
        if user != -1 and user != uid:
            log(_('changed owner of %s from %s to %s') % (f, get_user_name(uid), user_str))
            try:
                os.chown(f, user, -1)
            except:
                error('chown %s %s: %s' % (f, user, sys.exc_value[0]))
        if group != -1 and group != gid:
            log(_('changed group of %s from %s to %s') % (f, get_group_name(gid), group_str))
            try:
                os.chown(f, -1, group)
            except:
                error('chgrp %s %s: %s' % (f, group, sys.exc_value[0]))

def chmod(f, newperm):
    try:
        full = os.stat(f)
    except OSError:
        return 0
    mode = stat.S_IMODE(full[stat.ST_MODE])
    if stat.S_ISDIR(full[stat.ST_MODE]):
        if newperm & 0400:
            newperm = newperm | 0100
        if newperm & 0040:
            newperm = newperm | 0010
        if newperm & 0004:
            newperm = newperm | 0001
    if mode != newperm:
        log(_('changed mode of %s from %o to %o') % (f, mode, newperm))
        try:
            os.chmod(f, newperm)
        except:
            error('chmod %s %o: %s' % (f, newperm, sys.exc_value[0]))
    return 1

if __name__ == '__main__':
    import sys

    initlog('msec')
    
    for p in sys.argv[1:]:
        fix_perms(p)

    act()
    
# Perms.py ends here