ssfs view ssfs-server @ rev 49

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