-
Notifications
You must be signed in to change notification settings - Fork 2
/
chroot-tool
executable file
·178 lines (132 loc) · 4.79 KB
/
chroot-tool
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
#!/usr/bin/python
import os
import sys
import optparse
import ConfigParser
def usage():
print "Usage: chroot-tool [-c config_file] cmd"
print "Available commands:"
print " create Create the empty chroot"
print " install rpm1 [rpm2 rpm3 ...] Install a list of RPMs into chroot"
print " clear Clear a chroot of all mountpoints"
print " query rpm Query chroot's RPM database"
print " secure Chmod insecure dirs and binaries"
def create(args, cfg, opts):
root_dir = cfg.get("chroot", "root_dir")
yum_conf = cfg.get("chroot", "yum_conf")
if not os.path.exists(yum_conf):
print "Yum config does not exist: %s." % yum_conf
return 5
if not os.path.exists(root_dir):
os.makedirs(root_dir)
rpm_path = os.path.join(root_dir, "var", "lib", "rpm")
if os.path.exists(rpm_path):
print "Cowardly refusing to re-create an existing chroot %s" % root_dir
return 3
os.makedirs(rpm_path)
if os.system("rpm --root %s --initdb" % root_dir):
print "RPM database failed to initialize in chroot"
return 4
def install(args, cfg, opts):
if not args:
print "No RPMs specified to install!"
return 6
root_dir = cfg.get("chroot", "root_dir")
yum_conf = cfg.get("chroot", "yum_conf")
rpms = " ".join(args)
if os.system("yum -c %s --installroot %s -y install %s" % (yum_conf, root_dir, rpms)):
print "yum install failed"
return 7
def clear(args, cfg, opts):
if len(args) > 1:
print "Must provide only one chroot to clear"
return 14
if args:
root_dir = args[0]
else:
root_dir = cfg.get("chroot", "root_dir")
failed_dismounts = []
for line in open("/proc/self/mounts", "r"):
line = line.strip()
info = line.split()
if len(info) < 2:
print "Bad /proc/self/mounts line: %s" % line
mount_point = info[1]
if mount_point.startswith(root_dir):
print "Unmounting %s" % mount_point
if os.system("umount -l %s" % mount_point):
failed_dismounts.append(mount_point)
for line in open("/proc/self/mounts", "r"):
line = line.strip()
info = line.split()
if len(info) < 2:
print "Bad /proc/self/mounts line: %s" % line
mount_point = info[1]
if mount_point.startswith(root_dir):
if mount_point in failed_dismounts:
print "Failed to unmount %s" % mount_point
return 8
else:
print "Mysterious new mount %s" % mount_point
return 9
return 0
def query(args, cfg, opts):
root_dir = cfg.get("chroot", "root_dir")
if not os.path.exists(root_dir):
print "No chroot to query: %s" % root_dir
return 10
if len(args) != 1:
print "Invalid usage: must specify an RPM to query"
return 11
return os.WEXITSTATUS(os.system("rpm --root %s -q %s" % (root_dir, args[0])))
def secure(args, cfg, opts):
root_dir = cfg.get("chroot", "root_dir")
# Check for world-writable dirs
fd = os.popen("find %s -mount -type d -perm -1777" % root_dir)
bad_dirs = []
for line in fd.readlines():
bad_dirs.append(line.strip())
if fd.close():
print "Unable to find world-writable dirs"
return 12
for dir in bad_dirs:
st = os.stat(dir)
os.chmod(dir, st.st_mode & 00755)
# Check for setuid junk
fd = os.popen("find %s -mount -type f \( -perm -4000 -o -perm -2000 \)" % root_dir)
bad_files = []
for line in fd.readlines():
st
bad_files.append(line.strip())
if fd.close():
print "Unable to find setuid binaries"
return 13
for file in bad_files:
st = os.stat(file)
os.chmod(file, st.st_mode & 00777)
return 0
def main():
parser = optparse.OptionParser()
parser.add_option("-c", "--config", dest="config_file",
default="/etc/chroot-tool/tool.cfg", help="Configuration file for chroot-tool")
opts, args = parser.parse_args()
if len(args) == 0:
usage()
return 1
cfg = ConfigParser.ConfigParser()
if opts.config_file not in cfg.read(opts.config_file):
print "Unable to parse config file: %s" % opts.config_file
return 2
if args[0] == "create":
return create(args[1:], cfg, opts)
elif args[0] == "install":
return install(args[1:], cfg, opts)
elif args[0] == "clear":
return clear(args[1:], cfg, opts)
elif args[0] == "query":
return query(args[1:], cfg, opts)
elif args[0] == "secure":
return secure(args[1:], cfg, opts)
if __name__ == '__main__':
retval = main()
sys.exit(retval)