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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
|
#!/usr/bin/python -O
#---------------------------------------------------------------
# Project : Mandrakelinux
# 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]
# Build a regexp that matches all the non local filesystems
REGEXP_START = '^('
REGEXP_END = ')'
def build_non_localfs_regexp():
# Allow to avoid this feature
if Config.get_config('all-local-files', '0') == '1':
return None
try:
file = open('/proc/mounts', 'r')
except IOError:
error(_('Unable to check /proc/mounts. Assuming all file systems are local.'))
return None
non_localfs = Config.get_config('non-local-fstypes', None)
if non_localfs:
non_localfs = string.split(non_localfs)
else:
non_localfs = ('nfs', 'codafs', 'smbfs')
regexp = None
for line in file.readlines():
fields = string.split(line)
if fields[2] in non_localfs:
if regexp:
regexp = regexp + '|' + fields[1]
else:
regexp = REGEXP_START + fields[1]
file.close()
if not regexp:
return None
else:
return re.compile(regexp + REGEXP_END)
# resolv symlink
def get_sylink_name(path):
try:
p = os.readlink(path)
except OSError:
return path
if p and p[0] != '/':
p = os.path.dirname(path) + '/' + p
p = os.path.abspath(p)
if p == path:
return path
else:
return get_sylink_name(p)
# put the new perm/group/owner in the assoc variable according to the
# content of the path file.
assoc = {}
def fix_perms(path, _interactive):
try:
file = open(path, 'r')
except IOError:
return
root = Config.get_config('root', '')
fs_regexp = build_non_localfs_regexp()
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':
perm = -1
else:
perm = int(mode_str, 8)
if fields[1] == 'current':
user = group = -1
user_str = group_str = ''
else:
(user_str, group_str) = string.split(fields[1], '.')
if user_str != '':
user = get_user_id(user_str)
else:
user = -1
if group_str != '':
group = get_group_id(group_str)
else:
group = -1
fieldcount = len(fields)
if fieldcount == 5:
if fields[3] == 'force':
mandatory = 1
fieldcount = 4
else:
mandatory = 0
if fieldcount == 4:
for f in glob.glob(fields[0]):
newperm = perm
if fs_regexp and fs_regexp.search(f):
_interactive and log(_('Non local file: "%s". Nothing changed.') % fields[0])
continue
try:
full = os.lstat(f)
except OSError:
continue
if stat.S_ISLNK(full[stat.ST_MODE]):
f = get_sylink_name(f)
try:
full = os.stat(f)
except OSError:
continue
mode = stat.S_IMODE(full[stat.ST_MODE])
if newperm != -1 and 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, mandatory)
else:
error(_('invalid syntax in %s line %d') % (path, lineno))
file.close()
# commit the changes to the files
def act(change):
for f in assoc.keys():
(mode, uid, gid, newperm, user, group, user_str, group_str, mandatory) = assoc[f]
# if we don't change the security level, try not to lower the security
# if the user has changed it manually
if not change and not mandatory:
newperm = newperm & mode
if newperm != -1 and mode != newperm:
try:
os.chmod(f, newperm)
log(_('changed mode of %s from %o to %o') % (f, mode, newperm))
except:
error('chmod %s %o: %s' % (f, newperm, str(sys.exc_value)))
if user != -1 and user != uid:
try:
os.chown(f, user, -1)
log(_('changed owner of %s from %s to %s') % (f, get_user_name(uid), user_str))
except:
error('chown %s %s: %s' % (f, user, str(sys.exc_value)))
if group != -1 and group != gid:
try:
os.chown(f, -1, group)
log(_('changed group of %s from %s to %s') % (f, get_group_name(gid), group_str))
except:
error('chgrp %s %s: %s' % (f, group, str(sys.exc_value)))
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, str(sys.exc_value)))
return 1
if __name__ == '__main__':
import getopt
_interactive = sys.stdin.isatty()
change = 0
# process the options
try:
(opt, args) = getopt.getopt(sys.argv[1:], 'co:',
['change', 'option'])
except getopt.error:
error(_('Invalid option. Use %s (-o var=<val>...) ([0-5])') % sys.argv[0])
sys.exit(1)
for o in opt:
if o[0] == '-o' or o[0] == '--option':
pair = string.split(o[1], '=')
if len(pair) != 2:
error(_('Invalid option format %s %s: use -o var=<val>') % (o[0], o[1]))
sys.exit(1)
else:
Config.set_config(pair[0], pair[1])
elif o[0] == '-c' or o[0] == '--change':
change = 1
# initlog must be done after processing the option because we can change
# the way to report log with options...
if _interactive:
import syslog
initlog('msec', syslog.LOG_LOCAL1)
else:
initlog('msec')
_interactive and log(_('Fixing owners and permissions of files and directories'))
# process the files
for p in args:
_interactive and log(_('Reading data from %s') % p)
fix_perms(p, _interactive)
# do the modifications
act(change)
# Perms.py ends here
|