ssfs view ssfs-server @ rev 66

Add some doc and few small fixes
author Christophe Lincoln <pankso@slitaz.org>
date Mon Jun 13 18:30:46 2011 +0200 (2011-06-13)
parents 09b765c4e000
children 3a2f682ce267
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 up-vdisk $(gettext "Update a vdisk filesystem.")
64 mount-vdisk $(gettext "Mount a ssfs virtual disk.")
65 umount-vdisk $(gettext "Unmount the vdisk and free loop device.")
66 note $(gettext "Write a public note for users.")
68 $(echo -e "\033[1m$(gettext "Options:")\033[0m")
69 --login= $(gettext "Login name to add or del an user.")
70 --id= $(gettext "User id for adduser command.")
71 --pass= $(gettext "User password for adduser.")
72 --root= $(gettext "The path to the Ssfs vdisk chroot.")
73 --vdisk= $(gettext "Set the Ssfs vdisk path and name.")
74 --size= $(gettext "Set the ext3 vdisk size in Gb.")
76 EOT
77 }
79 status() {
80 [ $? = 0 ] && echo " OK"
81 [ $? = 1 ] && echo -e " ERROR\n" && exit 1
82 }
84 separator() {
85 echo "================================================================================"
86 }
88 # We have custom config when adding user to handle quota and user info.
89 user_paths() {
90 config=$SSFS_USERS/$login.conf
91 home=$root/./home/$login
92 }
94 user_info() {
95 cat << EOT
97 $(gettext "User login :") $login
98 $(gettext "User quota :") $QUOTA
99 $(gettext "Home usage :") $usage
101 EOT
102 }
104 user_config() {
105 gettext "Creating Ssfs user configuration file..."
106 cat > $config << EOT
107 # Ssfs user configuration file.
109 LOGIN="$login"
110 QUOTA="$DEFAULT_QUOTA"
111 EOT
112 chmod 0600 $config && status
113 echo ""
114 }
116 vdisk_config() {
117 cat > $root/etc/vdisk.conf << EOT
118 # /etc/vdisk.conf: Ssfs virtual disk auto-generated config file.
120 VDATE="$date"
121 VSIZE="$size"
122 FILES="$files"
123 EOT
124 }
126 # Handle Ssfs virtual disk.
127 umount_vdisk() {
128 if mount | fgrep -q $root; then
129 loop=$(mount | fgrep $root | awk '{print $1}')
130 gettext "Unmounting Ssfs vdisk:"; echo " $vdisk"
131 umount $root && sleep 1
132 gettext "Detaching loop device:"; echo " $loop"
133 losetup -d $loop
134 else
135 gettext "Ssfs vdisk is not mounted:"; echo " $vdisk"
136 fi
137 }
139 mount_vdisk() {
140 if ! mount | fgrep -q $root; then
141 [ -d "$root" ] || mkdir -p $root
142 gettext "Mounting virtual disk:"
143 mount -o loop -t ext3 $vdisk $root
144 else
145 gettext "Ssfs vdisk is already mounted:"
146 fi
147 echo " $vdisk $root"
148 }
150 #
151 # Commands
152 #
154 case "$1" in
155 users)
156 gettext -e "\nChecking:"; echo " /etc/passwd"
157 fgrep "Ssfs User" /etc/passwd | while read line
158 do
159 login=$(echo $line | cut -d ":" -f 1)
160 home="$root/home/$login"
161 usage=$(du -sm $home | awk '{print $1}')
162 config=$SSFS_USERS/$login.conf
163 . $config || gettext -e "WARNING: No config file\n"
164 user_info
165 done
166 users=$(ls $SSFS_USERS | wc -l)
167 gettext "Users:"; echo -e " $users\n" ;;
168 adduser)
169 # Add a Ssfs user to the system with $HOME in chroot.
170 [ -z "$login" ] && gettext -e "Missing user login name.\n" && exit 0
171 [ -z "$id" ] && gettext -e "Missing user id.\n" && exit 0
172 [ -z "$pass" ] && gettext -e "Missing user password.\n" && exit 0
173 user_paths
175 # We need chroot command allowed for users to chroot them on SSH
176 # login. Ssfs users have /bin/ssfs-sh as SHell.
177 grep -q ^chroot /etc/busybox.conf ||
178 echo 'chroot = ssx root.root' >> /etc/busybox.conf
180 gettext -e "\nChecking:"; echo " /etc/passwd"
181 if grep ^$login: /etc/passwd; then
182 gettext -e "Exiting, user already exists:"
183 echo -e " $login\n" && exit 0
184 fi
186 gettext "Creating user: $login..."
187 echo -e "$pass\n$pass" | \
188 adduser -h "$home" -g "Ssfs User" -u $id \
189 -s /bin/ssfs-sh $login >/dev/null
190 status
192 # Add user to chroot /etc/passwd
193 gettext "Checking vdisk chroot:"; echo " $root/etc/passwd"
194 if ! grep -q ^$login: $root/etc/passwd; then
195 echo "$login:x:$id:$id:Ssfs User:/home/$login:/bin/sh" >> \
196 $root/etc/passwd
197 echo "$login:x:$id:" >> $root/etc/group
198 fi
200 # We don't want any files from /etc/skel.
201 gettext "Cleaning home and creating: Sync/..."
202 rm -rf $home && mkdir -p $home/Sync $home/.ssh && status
203 gettext "Changing mode on user home: 0700..."
204 chown -R $login.$login $home
205 chmod 0700 $home && status
207 # Create a custom config per user in SSFS_USERS.
208 [ ! -d "$SSFS_USERS" ] && mkdir -p $SSFS_USERS
209 user_config ;;
210 deluser)
211 [ -z "$login" ] && gettext -e "Missing user login name.\n" && exit 0
212 user_paths
213 gettext -e "\nDeleting user:"; echo -n " $login..."
214 sed -i /^$login:/d $root/etc/passwd
215 sed -i /^$login:/d $root/etc/group
216 deluser $login || status && status
217 gettext "Removing all files in:"; echo -n " $home..."
218 rm -rf $home && status
219 gettext "Removing user config:"; echo -n " $login.conf..."
220 rm -rf $config && status
221 echo "" ;;
222 chroot)
223 gettext -e "\nChanging root to:"; echo -e " $root\n"
224 chroot $root
225 gettext -e "\nBack to the host system:"
226 echo -e " $(hostname)\n" ;;
227 note)
228 # Admin notes for users and displayed on the web interface.
229 note="$2"
230 date=$(date "+%Y-%m-%d %H:%M")
231 if [ "$note" ]; then
232 gettext "Adding note to:"; echo " $state/notes"
233 echo "$date : $note" >> $state/notes
234 fi ;;
235 gen-vdisk)
236 # Generate a virtual disk with a minimal chroot for Ssfs users home.
237 rootfs=$share/rootfs
238 if [ -d "$root/bin" ]; then
239 gettext "A chroot already exists in:"; echo " $root"
240 exit 0
241 fi
242 if [ ! -f "$rootfs/etc/busybox.conf" ]; then
243 gettext "Missing package ssfs-busybox"; echo
244 exit 0
245 fi
246 echo ""
247 gettext "Creating Sshs vdisk minimal chroot"; echo
248 separator
249 echo "Chroot path: $root"
251 # Create vdisk if missing.
252 if [ ! -f "$vdisk" ]; then
253 gettext "Creating virtual disk:"; echo " $vdisk ${size}Gb"
254 dd if=/dev/zero of=$vdisk bs=1G count=$size
255 chmod 0600 $vdisk && du -sh $vdisk
256 gettext "Creating ext3 filesystem..."
257 mkfs.ext3 -q -T ext3 -L "Ssfs" -F $vdisk
258 status
259 mount_vdisk
260 fi
262 # Create a radically minimal chroot with all libs in /lib.
263 gettext "Creating base files..."
264 mkdir -p $root && cd $root
265 for d in etc lib home root
266 do
267 mkdir -p $d
268 done && status
270 # /etc files.
271 cp -f /etc/slitaz-release $root/etc
272 if [ ! -f "$root/etc/passwd" ]; then
273 echo "root:x:0:0:root:/root:/bin/sh" > $root/etc/passwd
274 echo "root:x:0:" > $root/etc/group
275 fi
277 # Ssfs Busybox package install files in $share and allow easy vdisk
278 # upgrade following SliTaz repo.
279 gettext "Installing Ssfs root filesystem..."
280 cp -a $rootfs/* $root
281 status
283 gettext "Setting files permissions..."
284 chmod 0700 $root/root
285 chmod 4755 $root/bin/busybox
286 chmod 0600 $root/etc/busybox.conf
287 status
289 # Glib minimal libs, use host lib since package should be installed
290 # from same repo. ? libnss_compat*
291 gettext "Installing Glibc libraries..."
292 for l in ld-*.*so* libc-*.*so libc.so.* libnss_files*
293 do
294 cp -a /lib/$l* $root/lib
295 done && status
297 # Ssfs chroot SHell and declare vdisk config.
298 gettext "Installing Ssfs SHell..."
299 install -m 0755 /bin/ssfs-sh $root/bin
300 touch $root/etc/vdisk.conf
301 status
303 # List of all system files.
304 gettext "Creating the list of files... "
305 cd $root && rm -f $state/vdisk.files
306 for d in bin etc lib
307 do
308 find ./$d | sed s'/^.//' >> $state/vdisk.files
309 done
310 files=$(cat $state/vdisk.files | wc -l)
311 echo "$files"
313 # Create chroot /etc/vdisk.conf
314 size=$(du -sh $vdisk | awk '{print $1}')
315 used=$(du -sh $root | awk '{print $1}')
316 date=$(date '+%Y-%m-%d %H:%M')
317 vdisk_config
318 separator
319 gettext "Vdisk used space:"; echo -e " $used - $date\n" ;;
320 mount-vdisk)
321 mount_vdisk ;;
322 umount-vdisk)
323 umount_vdisk ;;
324 check-vdisk)
325 # Check vdisk with e2fsck.
326 echo ""
327 gettext -e "Checking Ssfs virtual disk\n"
328 separator
329 gettext "Virtual disk : "; du -sh $vdisk
330 gettext "Filesystem usage : "; du -sh $root
331 gettext "Remounting vdisk read/only before e2fsck -p..."
332 mount -o remount,loop,ro $vdisk $root && status
333 e2fsck -p $vdisk
334 gettext "Remounting vdisk read/write..."
335 mount -o remount,loop,rw $vdisk $root && status
336 separator && echo "" ;;
337 up-vdisk)
338 $0 clean-vdisk
339 $0 gen-vdisk ;;
340 clean-vdisk)
341 # clean up the vdisk storage chroot.
342 if [ ! -d "$root/bin" ] || [ ! -d "$root/lib" ]; then
343 gettext -e "No chroot found in:"; echo " $root"
344 exit 0
345 fi
346 gettext -e "\nCleaning virtual disk\n"
347 separator
348 echo "Chroot path: $root"
349 cd $root
350 for dir in *
351 do
352 size=$(du -sh $dir | awk '{print $1}')
353 case "$dir" in
354 etc|home|root|lost*)
355 gettext "Skipping:"; echo " $dir $size *" ;;
356 *)
357 gettext "Removing:"; echo " $dir $size"
358 rm -rf $dir ;;
359 esac
360 done && separator && echo "" ;;
361 *)
362 help ;;
363 esac
364 exit 0