ssfs view ssfs-server @ rev 62

No /sbin in chroot anymore with ssfs-busybox
author Christophe Lincoln <pankso@slitaz.org>
date Sun Jun 12 23:13:14 2011 +0200 (2011-06-12)
parents e136f9af3729
children 291d7b317507
line source
1 #!/bin/sh
2 #
3 # SliTaz Secure File Storage server side tool.
4 #
5 # Copyright (C) SliTaz GNU/Linux - BSD License
6 # Author: Christophe Lincoln <pankso@slitaz.org>
7 #
9 app=$(basename $0)
10 [ -f "/etc/ssfs/$app.conf" ] && . /etc/ssfs/$app.conf
11 [ -f "./data/$app.conf" ] && . ./data/$app.conf
12 state=/var/lib/ssfs
13 share=/usr/share/ssfs
15 # Be sure we're root.
16 [ $(id -u) != 0 ] && gettext "You must be root to run:" && \
17 echo " $app" && exit 0
19 # Parse cmdline options.
20 for opt in $@
21 do
22 case "$opt" in
23 --login=*)
24 login=${opt#--login=} ;;
25 --id=*)
26 id=${opt#--id=} ;;
27 --pass=*)
28 pass=${opt#--pass=} ;;
29 --root=*)
30 root=${opt#--root=} ;;
31 --vdisk=*)
32 vdisk=${opt#--vdisk=} ;;
33 --size=*)
34 size=${opt#--size=} ;;
35 *)
36 continue ;;
37 esac
38 done
40 [ "$root" ] || root=${SSFS_CHROOT}
41 [ "$vdisk" ] || vdisk=${SSFS_VDISK}
42 [ "$size" ] || size=${SSFS_SIZE}
44 #
45 # Functions
46 #
48 # Built-in help usage.
49 help() {
50 cat << EOT
52 $(echo -e "\033[1m$(gettext "Usage:")\033[0m") $app [command] [--option=]
54 $(echo -e "\033[1m$(gettext "Commands:")\033[0m")
55 help $(gettext "Display this short help usage.")
56 users $(gettext "List user accounts and stats.")
57 adduser $(gettext "Add a user to the system with \$HOME in chroot.")
58 deluser $(gettext "Delete a user and remove \$HOME files.")
59 chroot $(gettext "Chroot to Ssfs storage root.")
60 gen-vdisk $(gettext "Create a vdisk with chroot for files storage.")
61 clean-vdisk $(gettext "Clean the vdisk but skip home and root.")
62 check-vdisk $(gettext "Check the vdisk filesystem with e2fsck.")
63 mount-vdisk $(gettext "Mount a ssfs virtual disk.")
64 umount-vdisk $(gettext "Unmount the vdisk and free loop device.")
65 note $(gettext "Write a public note for users.")
67 $(echo -e "\033[1m$(gettext "Options:")\033[0m")
68 --login= $(gettext "Login name to add or del an user.")
69 --id= $(gettext "User id for adduser command.")
70 --pass= $(gettext "User password for adduser.")
71 --root= $(gettext "The path to the Ssfs vdisk chroot.")
72 --vdisk= $(gettext "Set the Ssfs vdisk path and name.")
73 --size= $(gettext "Set the ext3 vdisk size in Gb.")
75 EOT
76 }
78 status() {
79 [ $? = 0 ] && echo " OK"
80 [ $? = 1 ] && echo -e " ERROR\n" && exit 1
81 }
83 separator() {
84 echo "================================================================================"
85 }
87 # We have custom config when adding user to handle quota and user info.
88 user_paths() {
89 config=$SSFS_USERS/$login.conf
90 home=$root/./home/$login
91 }
93 user_info() {
94 cat << EOT
96 $(gettext "User login :") $login
97 $(gettext "User quota :") $QUOTA
98 $(gettext "Home usage :") $usage
100 EOT
101 }
103 user_config() {
104 gettext "Creating Ssfs user configuration file..."
105 cat > $config << EOT
106 # Ssfs user configuration file.
108 LOGIN="$login"
109 QUOTA="$DEFAULT_QUOTA"
110 EOT
111 chmod 0600 $config && status
112 echo ""
113 }
115 vdisk_config() {
116 cat > $root/etc/vdisk.conf << EOT
117 # /etc/vdisk.conf: Ssfs virtual auto-generated config file.
119 VDATE="$date"
120 VSIZE="$size"
121 FILES="$files"
122 EOT
123 }
125 # Handle Ssfs virtual disk.
126 umount_vdisk() {
127 if mount | fgrep -q $root; then
128 loop=$(mount | fgrep $root | awk '{print $1}')
129 gettext "Unmounting Ssfs vdisk:"; echo " $vdisk"
130 umount $root && sleep 1
131 gettext "Detaching loop device:"; echo " $loop"
132 losetup -d $loop
133 else
134 gettext "Ssfs vdisk is not mounted:"; echo " $vdisk"
135 fi
136 }
138 mount_vdisk() {
139 if ! mount | fgrep -q $root; then
140 [ -d "$root" ] || mkdir -p $root
141 gettext "Mounting virtual disk:"
142 mount -o loop -t ext3 $vdisk $root
143 else
144 gettext "Ssfs vdisk is already mounted:"
145 fi
146 echo " $vdisk $root"
147 }
149 #
150 # Commands
151 #
153 case "$1" in
154 users)
155 gettext -e "\nChecking:"; echo " /etc/passwd"
156 fgrep "Ssfs User" /etc/passwd | while read line
157 do
158 login=$(echo $line | cut -d ":" -f 1)
159 home="$root/home/$login"
160 usage=$(du -sm $home | awk '{print $1}')
161 config=$SSFS_USERS/$login.conf
162 . $config || gettext -e "WARNING: No config file\n"
163 user_info
164 done
165 users=$(ls $SSFS_USERS | wc -l)
166 gettext "Users:"; echo -e " $users\n" ;;
167 adduser)
168 # Add a Ssfs user to the system with $HOME in chroot.
169 [ -z "$login" ] && gettext -e "Missing user login name.\n" && exit 0
170 [ -z "$id" ] && gettext -e "Missing user id.\n" && exit 0
171 [ -z "$pass" ] && gettext -e "Missing user password.\n" && exit 0
172 user_paths
174 # We need chroot command allowed for users to chroot them on SSH
175 # login. Ssfs users have /bin/ssfs-sh as SHell.
176 grep -q ^chroot /etc/busybox.conf ||
177 echo 'chroot = ssx root.root' >> /etc/busybox.conf
179 gettext -e "\nChecking:"; echo " /etc/passwd"
180 if grep ^$login: /etc/passwd; then
181 gettext -e "Exiting, user already exists:"
182 echo -e " $login\n" && exit 0
183 fi
185 gettext "Creating user: $login..."
186 echo -e "$pass\n$pass" | \
187 adduser -h "$home" -g "Ssfs User" -u $id \
188 -s /bin/ssfs-sh $login >/dev/null
189 status
191 # Add user to chroot /etc/passwd
192 gettext "Checking vdisk chroot:"; echo " $root/etc/passwd"
193 if ! grep -q ^$login: $root/etc/passwd; then
194 echo "$login:x:$id:$id:Ssfs User:/home/$login:/bin/sh" >> \
195 $root/etc/passwd
196 fi
198 # We don't want any files from /etc/skel.
199 gettext "Cleaning home and creating: Sync/..."
200 rm -rf $home && mkdir -p $home/Sync $home/.ssh && status
201 gettext "Changing mode on user home: 0700..."
202 chown -R $login.$login $home
203 chmod 0700 $home && status
205 # Create a custom config per user in SSFS_USERS.
206 [ ! -d "$SSFS_USERS" ] && mkdir -p $SSFS_USERS
207 user_config ;;
208 deluser)
209 [ -z "$login" ] && gettext -e "Missing user login name.\n" && exit 0
210 user_paths
211 gettext -e "\nDeleting user:"; echo -n " $login..."
212 sed -i /^$login:/d $root/etc/passwd
213 deluser $login || status && status
214 gettext "Removing all files in:"; echo -n " $home..."
215 rm -rf $home && status
216 gettext "Removing user config:"; echo -n " $login.conf..."
217 rm -rf $config && status
218 echo "" ;;
219 chroot)
220 gettext -e "\nChanging root to:"; echo -e " $root\n"
221 chroot $root
222 gettext -e "\nBack to the host system:"
223 echo -e " $(hostname)\n" ;;
224 note)
225 # Admin notes for users and displayed on the web interface.
226 note="$2"
227 date=$(date "+%Y-%m-%d %H:%M")
228 if [ "$note" ]; then
229 gettext "Adding note to:"; echo " $state/notes"
230 echo "$date : $note" >> $state/notes
231 fi ;;
232 gen-vdisk)
233 # Generate a virtual disk with a minimal chroot for Ssfs users home.
234 rootfs=$share/rootfs
235 if [ -d "$root/bin" ]; then
236 gettext "A chroot already exists in:"; echo " $root"
237 exit 0
238 fi
239 if [ ! -f "$rootfs/etc/busybox.conf" ]; then
240 gettext "Missing package ssfs-busybox"; echo
241 exit 0
242 fi
243 echo ""
244 gettext "Creating Sshs vdisk minimal chroot"; echo
245 separator
246 echo "Chroot path: $root"
248 # Create vdisk if missing.
249 if [ ! -f "$vdisk" ]; then
250 gettext "Creating virtual disk:"; echo " $vdisk ${size}Gb"
251 dd if=/dev/zero of=$vdisk bs=1G count=$size
252 chmod 0600 $vdisk && du -sh $vdisk
253 gettext "Creating ext3 filesystem..."
254 mkfs.ext3 -q -T ext3 -L "Ssfs" -F $vdisk
255 status
256 mount_vdisk
257 fi
259 # Create a radically minimal chroot with all libs in /lib.
260 gettext "Creating base files..."
261 mkdir -p $root && cd $root
262 for d in etc lib home root
263 do
264 mkdir -p $d
265 done && status
267 # /etc files.
268 cp -f /etc/slitaz-release $root/etc
269 if [ ! -f "$root/etc/passwd" ]; then
270 echo "root:x:0:0:root:/root:/bin/sh" > $root/etc/passwd
271 echo "root::13525:0:99999:7:::" > $root/etc/shadow
272 echo "root:x:0:" > $root/etc/group
273 echo "root:*::" > $root/etc/gshadow
274 fi
276 # /dev nodes.
277 #mknod -m 666 $root/dev/null c 1 3
279 # Ssfs Busybox package install files in $cache and allow easy vdisk
280 # upgrade following SliTaz repo.
281 gettext "Installing Ssfs Busybox..."
282 cp -a $rootfs/* $root
283 status
285 gettext "Setting files permissions..."
286 chmod 0640 $root/etc/*shadow
287 chmod 0700 $root/root
288 chmod 4755 $root/bin/busybox
289 chmod 0600 $root/etc/busybox.conf
290 status
292 # Glib minimal libs, use host lib since package should be installed
293 # from same repo. ? libnss_compat*
294 gettext "Installing Glibc libraries..."
295 for l in ld-*.*so* libc-*.*so libc.so.* libnss_files*
296 do
297 cp -a /lib/$l* $root/lib
298 done && status
300 # Ssfs chroot SHell and declare vdisk config.
301 gettext "Installing Ssfs SHell and utility..."
302 install -m 0755 /bin/ssfs-sh $root/bin
303 install -m 0755 $share/ssfs-env $root/bin
304 touch $root/etc/vdisk.conf
305 status
307 # List of all system files.
308 gettext "Creating the list of files... "
309 cd $root && rm -f $state/vdisk.files
310 for d in bin etc lib
311 do
312 find ./$d | sed s'/^.//' >> $state/vdisk.files
313 done
314 files=$(cat $state/vdisk.files | wc -l)
315 echo "$files"
317 # Create chroot /etc/vdisk.conf
318 size=$(du -sh $vdisk | awk '{print $1}')
319 used=$(du -sh $root | awk '{print $1}')
320 date=$(date '+%Y-%m-%d %H:%M')
321 vdisk_config
322 separator
323 gettext "Vdisk used space:"; echo -e " $used - $date\n" ;;
324 mount-vdisk)
325 mount_vdisk ;;
326 umount-vdisk)
327 umount_vdisk ;;
328 check-vdisk)
329 # Check vdisk with e2fsck.
330 echo ""
331 gettext -e "Checking Ssfs virtual disk\n"
332 separator
333 gettext "Virtual disk : "; du -sh $vdisk
334 gettext "Filesystem usage : "; du -sh $root
335 gettext "Remounting vdisk read/only before e2fsck -p..."
336 mount -o remount,loop,ro $vdisk $root && status
337 e2fsck -p $vdisk
338 gettext "Remounting vdisk read/write..."
339 mount -o remount,loop,rw $vdisk $root && status
340 separator && echo "" ;;
341 clean-vdisk)
342 # clean up the vdisk storage chroot.
343 if [ ! -d "$root/bin" ] || [ ! -d "$root/lib" ]; then
344 gettext -e "No chroot found in:"; echo " $root"
345 exit 0
346 fi
347 gettext -e "\nCleaning virtual disk\n"
348 separator
349 echo "Chroot path: $root"
350 cd $root
351 for dir in *
352 do
353 size=$(du -sh $dir | awk '{print $1}')
354 case "$dir" in
355 etc|home|root|lost*)
356 gettext "Skipping:"; echo " $dir $size *" ;;
357 *)
358 gettext "Removing:"; echo " $dir $size"
359 rm -rf $dir ;;
360 esac
361 done && separator && echo "" ;;
362 *)
363 help ;;
364 esac
365 exit 0