-
Notifications
You must be signed in to change notification settings - Fork 0
/
chroot_setup_script_MOP.sh
320 lines (267 loc) · 8.54 KB
/
chroot_setup_script_MOP.sh
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
316
317
318
319
320
#!/QOpenSys/usr/bin/ksh
## CHANGED VERSION WITH 2 ARGS - BENOIT MAROLLEAU
##
##############################################################################
### Example script to setup chroot environment on IBM i
###
### Version 20160503
###
### IBM grants you a nonexclusive copyright license to use all programming
### code examples from which you can generate similar function tailored to
### your own specific needs.
###
### SUBJECT TO ANY STATUTORY WARRANTIES WHICH CANNOT BE EXCLUDED, IBM,
### ITS PROGRAM DEVELOPERS AND SUPPLIERS MAKE NO WARRANTIES OR CONDITIONS
### EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO, THE IMPLIED
### WARRANTIES OR CONDITIONS OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
### PURPOSE, AND NON-INFRINGEMENT, REGARDING THE PROGRAM OR TECHNICAL
### SUPPORT, IF ANY.
###
### UNDER NO CIRCUMSTANCES IS IBM, ITS PROGRAM DEVELOPERS OR SUPPLIERS
### LIABLE FOR ANY OF THE FOLLOWING, EVEN IF INFORMED OF THEIR POSSIBILITY:
###
### LOSS OF, OR DAMAGE TO, DATA;
### DIRECT, SPECIAL, INCIDENTAL, OR INDIRECT DAMAGES, OR FOR ANY ECONOMIC
### CONSEQUENTIAL DAMAGES; OR
### LOST PROFITS, BUSINESS, REVENUE, GOODWILL, OR ANTICIPATED SAVINGS.
### SOME JURISDICTIONS DO NOT ALLOW THE EXCLUSION OR LIMITATION OF DIRECT,
### INCIDENTAL, OR CONSEQUENTIAL DAMAGES, SO SOME OR ALL OF THE ABOVE
### LIMITATIONS OR EXCLUSIONS MAY NOT APPLY TO YOU.
###
##############################################################################
V=`uname -v`
R=`uname -r`
case $V$R in
61)
USERDATA=/QOpenSys/QIBM/UserData/SC1/OpenSSH/openssh-3.8.1p1
PRODDATA=/QOpenSys/QIBM/ProdData/SC1/OpenSSH/openssh-3.8.1p1
;;
71)
USERDATA=/QOpenSys/QIBM/UserData/SC1/OpenSSH/openssh-4.7p1
PRODDATA=/QOpenSys/QIBM/ProdData/SC1/OpenSSH/openssh-4.7p1
;;
*)
if [ $V$R -lt 72 ]
then
echo "Unsupported version $V.$R"
exit 1
fi
USERDATA=/QOpenSys/QIBM/UserData/SC1/OpenSSH
PRODDATA=/QOpenSys/QIBM/ProdData/SC1/OpenSSH
;;
esac
# directory that will be created and used as the chroot root "/" directory
if [ -z "$CHROOTDIR" ]
then
#CHROOTDIR=$USERDATA/chroot
CHROOTDIR=$2
fi
if [ -z "$CHROOTLOCALE" ]
then
CHROOTLOCALE='*C'
fi
FIRST=`echo "$CHROOTLOCALE" | /QOpenSys/usr/bin/cut -c 1`
if [ "$FIRST" != '*' -a $V -lt 7 ]
then
echo "chroot user locale must be *C (default), *POSIX, or *NONE on IBM i 6.1 and below"
exit 1
fi
# log file
LOGFILE=chroot_config.log
# (optional) userid to change home directory to trigger ssh chroot access
USER=$1
function log
{
echo "$@"
echo "$@" >> $LOGFILE
}
function log_only
{
echo "$@" >> $LOGFILE
}
function log_cmd
{
log_only "$@"
prog=`shift`
$prog "$@" >> $LOGFILE 2>&1
}
function show_progress
{
echo ".\c"
}
log_only
log_only "$(date)"
log_only
log_only "Starting configuration of chroot environment. chroot-dir = $CHROOTDIR"
log_only
# ensure that we can handle user names longer than 8 characters
export PASE_USRGRP_LIMITED=N
# Step 1 - verify that the chroot directory does not yet exist
log "########## STEP 1 ##########"
log "Verify that the chroot-dir does not yet exist"
if [ -e $CHROOTDIR ]
then
log "Error: chroot-dir $CHROOTDIR already exists"
exit 1
fi
# Step 2 - verify that the userid to be changed exists
log
log "########## STEP 2 ##########"
log "If specified, verify that the user profile to be changed to ssh chroot access exists"
if [ -n "$USER" ]
then
# translate USER to all lowercase
USER=`echo $USER | /QOpenSys/usr/bin/tr '[A-Z]' '[a-z]'`
log_cmd /QOpenSys/usr/bin/id $USER
if [ $? != 0 ]
then
log "Error: chroot user profile $USER does not exist"
exit 2
fi
fi
# Step 3 - create chroot directories
log
log "########## STEP 3 ##########"
log "Making necessary directories in chroot-dir"
log_only "/home, /home/<user>, /dev, /dev/pts, /usr/bin/X11, /usr/sbin, /usr/lib, $PRODDATA/libexec and $PRODDATA/etc will be created"
log_only
log_cmd /QOpenSys/usr/bin/mkdir -p $CHROOTDIR
for i in /home /dev/pts /usr/bin/X11 /usr/sbin /usr/lib $PRODDATA/libexec $PRODDATA/etc
do
log_cmd /QOpenSys/usr/bin/mkdir -p $CHROOTDIR/$i
show_progress
done
if [ -n "$USER" ]
then
log_cmd /QOpenSys/usr/bin/mkdir -p $CHROOTDIR/home/$USER
show_progress
fi
echo
# Step 4 - set directory permissions
log
log "########## STEP 4 ##########"
log "Make sure that the permissions on all the files created inside the chrooted directory"
log "are same as the one for the original directories"
log_only
log_cmd /QOpenSys/usr/bin/chmod -R 0755 $CHROOTDIR
# Step 5 - create QOpenSys/usr symlink
log
log "########## STEP 5 ##########"
log "Create QOpenSys/usr symlink inside the chroot-dir directory"
log_only
log_cmd /QOpenSys/usr/bin/ln -s ../usr $CHROOTDIR/QOpenSys/usr
# Step 6 - copy binaries and libraries
log
log "########## STEP 6 ##########"
log "Copying example binaries and libraries to the chroot environment"
log_only
log_only "This script is an example script only!"
log_only "Thus, only a few binaries and the necessary libraries are copied to the chroot environment."
log_only "If you need additional binaries you will have to copy them manually to the chroot directories or adapt this script. "
log_only "Remember to check for any requisite libaries with the dump command and copy them to the chroot environment as well."
log_only
log_only "The following binaries and libraries have been copied to the chroot directory:"
log_only
for i in execerror sh bsh ksh cd pwd ls mkdir rmdir rm cp cat xauth scp ssh sftp
do
log_cmd /QOpenSys/usr/bin/cp -p `which $i` $CHROOTDIR/`which $i`
log_cmd /QOpenSys/usr/bin/ls -l $CHROOTDIR/`which $i`
log_only
show_progress
done
for i in /usr/lib/libc.a /usr/lib/libpthreads.a /usr/lib/libiconv.a /usr/lib/libcrypt.a /usr/lib/libcrypto.a /usr/lib/libcrypto.so.1.1 /usr/lib/libX11.a /usr/lib/libXext.a /usr/lib/libIM.a /usr/lib/libICE.a /usr/lib/libXi.a /usr/lib/libSM.a /usr/lib/libgaimisc.a /usr/lib/libgair4.a /usr/lib/libC.a /usr/lib/libz.a
do
log_cmd /QOpenSys/usr/bin/cp -p $i $CHROOTDIR/$i
log_cmd /QOpenSys/usr/bin/ls -l $CHROOTDIR/$i
log_only
show_progress
done
log_cmd /QOpenSys/usr/bin/cp -p $PRODDATA/etc/sshd_config $CHROOTDIR$PRODDATA/etc/sshd_config
log_cmd /QOpenSys/usr/bin/ls -al $CHROOTDIR$PRODDATA/etc/sshd_config
log_only
show_progress
log_cmd /QOpenSys/usr/bin/cp -p $PRODDATA/libexec/sftp-server $CHROOTDIR$PRODDATA/libexec/sftp-server
log_cmd /QOpenSys/usr/bin/ls -al $CHROOTDIR$PRODDATA/libexec/sftp-server
log_only
show_progress
echo
# Step 7 - create necessary devices
log
log "########## STEP 7 ##########"
log "Necessary devices will be created in the chroot environment"
log_only
log_cmd /QOpenSys/usr/sbin/mknod $CHROOTDIR/dev/tty c 32945 0
show_progress
log_cmd /QOpenSys/usr/sbin/mknod $CHROOTDIR/dev/null c 32769 1
show_progress
log_cmd /QOpenSys/usr/sbin/mknod $CHROOTDIR/dev/zero c 32769 2
show_progress
log_cmd /QOpenSys/usr/sbin/mknod $CHROOTDIR/dev/urandom c 32954 1
show_progress
i=0
while [ $i -lt 10 ]
do
log_cmd /QOpenSys/usr/sbin/mknod $CHROOTDIR/dev/pts/$i c 32947 $i
show_progress
((i=i+1))
done
for i in zero null tty urandom
do
log_cmd /QOpenSys/usr/bin/chmod 0666 $CHROOTDIR/dev/$i
show_progress
done
log_cmd /QOpenSys/usr/bin/chmod 0666 $CHROOTDIR/dev/pts/*
log_only
log_only "The following devices have been configured within your chroot environment:"
log_only
for i in zero null tty urandom
do
log_cmd /QOpenSys/usr/bin/ls -al $CHROOTDIR/dev/$i
log_only
show_progress
done
i=0
while [ $i -lt 10 ]
do
log_cmd /QOpenSys/usr/bin/ls -al $CHROOTDIR/dev/pts/$i
log_only
show_progress
((i=i+1))
done
echo
# Step 8 - change ownership
log
log "########## STEP 8 ##########"
log "Change ownership of files in chroot environment:"
log_only
log_cmd /QOpenSys/usr/bin/chown -Rh qsys:0 $CHROOTDIR
if [ -n "$USER" ]
then
log_cmd /QOpenSys/usr/bin/chown $USER $CHROOTDIR/home/$USER
fi
# Step 9 - change home directory of chroot user to enable chroot at ssh connection
log
log "########## STEP 9 ##########"
log "If specified, change user profile home directory to enable chroot ssh connection"
if [ -n "$USER" ]
then
log_only
log_cmd /QOpenSys/usr/bin/system "CHGUSRPRF USRPRF($USER) HOMEDIR('$CHROOTDIR/./home/$USER')"
fi
# Step 10 - set chroot user profile to use C locale
log
log "########## STEP 10 ##########"
log "If specified, change user profile locale to enable chroot ssh connection"
if [ -n "$USER" -a $V -lt 7 ]
then
log_only
log_cmd /QOpenSys/usr/bin/system "CHGUSRPRF USRPRF($USER) LOCALE('$CHROOTLOCALE')"
fi
log
log "########## FINISH ##########"
log
log "Configuration of the following chroot environment has completed:"
log_only
log_cmd /QOpenSys/usr/bin/find $CHROOTDIR -ls
log
echo "A log file was created for reference if needed: $LOGFILE"