ssfs view ssfs-server @ rev 35
Create a list of system files when gen-vdisk
author | Christophe Lincoln <pankso@slitaz.org> |
---|---|
date | Sun Jun 12 13:38:25 2011 +0200 (2011-06-12) |
parents | c7205f3db649 |
children | e3160ccd3ffb |
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 # Handle Ssfs virtual disk.
115 umount_vdisk() {
116 if mount | fgrep -q $root; then
117 loop=$(mount | fgrep $root | awk '{print $1}')
118 gettext "Unmounting Ssfs vdisk:"; echo " $vdisk"
119 umount $root && sleep 1
120 gettext "Detaching loop device:"; echo " $loop"
121 losetup -d $loop
122 else
123 gettext "Ssfs vdisk is not mounted:"; echo " $vdisk"
124 fi
125 }
127 mount_vdisk() {
128 if ! mount | fgrep -q $root; then
129 [ -d "$root" ] || mkdir -p $root
130 gettext "Mounting virtual disk:"
131 mount -o loop -t ext3 $vdisk $root
132 else
133 gettext "Ssfs vdisk is already mounted:"
134 fi
135 echo " $vdisk $root"
136 }
138 #
139 # Commands
140 #
142 case "$1" in
143 users)
144 gettext -e "\nChecking:"; echo " /etc/passwd"
145 fgrep "Ssfs User" /etc/passwd | while read line
146 do
147 login=$(echo $line | cut -d ":" -f 1)
148 home="$root/home/$login"
149 usage=$(du -sm $home | awk '{print $1}')
150 config=$SSFS_USERS/$login.conf
151 . $config || gettext -e "WARNING: No config file\n"
152 user_info
153 done
154 users=$(ls $SSFS_USERS | wc -l)
155 gettext "Users:"; echo -e " $users\n" ;;
156 adduser)
157 # Add a Ssfs user to the system with $HOME in chroot.
158 [ -z "$login" ] && gettext -e "Missing user login name.\n" && exit 0
159 [ -z "$id" ] && gettext -e "Missing user id.\n" && exit 0
160 [ -z "$pass" ] && gettext -e "Missing user password.\n" && exit 0
161 user_paths
163 # We need chroot command allowed for users to chroot them on SSH
164 # login. Ssfs users have /bin/ssfs-sh as SHell.
165 grep -q ^chroot /etc/busybox.conf ||
166 echo 'chroot = ssx root.root' >> /etc/busybox.conf
168 gettext -e "\nChecking:"; echo " /etc/passwd"
169 if grep ^$login: /etc/passwd; then
170 gettext -e "Exiting, user already exists:"
171 echo -e " $login\n" && exit 0
172 fi
174 gettext "Creating user: $login..."
175 echo -e "$pass\n$pass" | \
176 adduser -h "$home" -g "Ssfs User" -u $id \
177 -s /bin/ssfs-sh $login >/dev/null
178 status
180 # Add user to chroot /etc/passwd
181 gettext "Checking vdisk chroot:"; echo " $root/etc/passwd"
182 if ! grep -q ^$login: $root/etc/passwd; then
183 echo "$login:x:$id:$id:Ssfs User:/home/$login:/bin/sh" >> \
184 $root/etc/passwd
185 fi
187 # We don't want any files from /etc/skel.
188 gettext "Cleaning home and creating: Sync/..."
189 rm -rf $home && mkdir -p $home/Sync $home/.ssh && status
190 gettext "Changing mode on user home: 0700..."
191 chown -R $login.$login $home
192 chmod 0700 $home && status
194 # Create a custom config per user in SSFS_USERS.
195 [ ! -d "$SSFS_USERS" ] && mkdir -p $SSFS_USERS
196 user_config ;;
197 deluser)
198 [ -z "$login" ] && gettext -e "Missing user login name.\n" && exit 0
199 user_paths
200 gettext -e "\nDeleting user:"; echo -n " $login..."
201 sed -i /^$login:/d $root/etc/passwd
202 deluser $login || status && status
203 gettext "Removing all files in:"; echo -n " $home..."
204 rm -rf $home && status
205 gettext "Removing user config:"; echo -n " $login.conf..."
206 rm -rf $config && status
207 echo "" ;;
208 chroot)
209 gettext -e "\nChanging root to:"; echo -e " $root\n"
210 chroot $root
211 gettext -e "\nBack to the host system:"
212 echo -e " $(hostname)\n" ;;
213 gen-vdisk)
214 # Generate a virtual disk with a minimal chroot for Ssfs users home.
215 rootfs=$share/rootfs
216 if [ -d "$root/bin" ]; then
217 gettext "A chroot already exists in:"; echo " $root"
218 exit 0
219 fi
220 if [ ! -f "$rootfs/etc/busybox.conf" ]; then
221 gettext "Missing package ssfs-busybox"; echo
222 exit 0
223 fi
224 echo ""
225 gettext "Creating Sshs vdisk minimal chroot"; echo
226 separator
227 echo "Chroot path: $root"
229 # Create vdisk if missing.
230 if [ ! -f "$vdisk" ]; then
231 gettext "Creating virtual disk:"; echo " $vdisk ${size}Gb"
232 dd if=/dev/zero of=$vdisk bs=1G count=$size
233 chmod 0600 $vdisk && du -sh $vdisk
234 gettext "Creating ext3 filesystem..."
235 mkfs.ext3 -q -T ext3 -L "Ssfs" -F $vdisk
236 status
237 mount_vdisk
238 fi
240 # Create a radically minimal chroot with all libs in /lib.
241 gettext "Creating base files..."
242 mkdir -p $root && cd $root
243 for d in etc lib home root
244 do
245 mkdir -p $d
246 done && status
248 # /etc files.
249 cp -f /etc/slitaz-release $root/etc
250 if [ ! -f "$root/etc/passwd" ]; then
251 echo "root:x:0:0:root:/root:/bin/sh" > $root/etc/passwd
252 echo "root::13525:0:99999:7:::" > $root/etc/shadow
253 echo "root:x:0:" > $root/etc/group
254 echo "root:*::" > $root/etc/gshadow
255 fi
257 # /dev nodes.
258 #mknod -m 666 $root/dev/null c 1 3
260 # Ssfs Busybox package install files in $cache and allow easy vdisk
261 # upgrade folowing SliTaz repo.
262 gettext "Installing Ssfs Busybox..."
263 cp -a $rootfs/* $root
264 status
266 gettext "Setting files permissions..."
267 chmod 0640 $root/etc/*shadow
268 chmod 0700 $root/root
269 chmod 4755 $root/bin/busybox
270 chmod 0600 $root/etc/busybox.conf
271 status
273 # Glib minimal libs, use host lib since package should be installed
274 # from same repo.
275 gettext "Installing Glibc libraries..."
276 for l in ld-*.*so* libc-*.*so libc.so.* libnss_files*
277 do
278 cp -a /lib/$l* $root/lib
279 done && status
281 # Ssfs chroot SHell
282 gettext "Installing Ssfs SHell..."
283 install -m 0755 /bin/ssfs-sh $root/bin
284 status
286 # List of all system files.
287 gettext "Creating the list of files... "
288 cd $root && rm -f $state/vdisk.files
289 for d in bin etc lib sbin
290 do
291 find ./$d | sed s'/^.//' >> $state/vdisk.files
292 done
293 cat $state/vdisk.files | wc -l
295 separator
296 size=$(du -sh $root | awk '{print $1}')
297 gettext "Vdisk used space:"; echo -e " $size\n" ;;
298 mount-vdisk)
299 mount_vdisk ;;
300 umount-vdisk)
301 umount_vdisk ;;
302 check-vdisk)
303 # Check vdisk with e2fsck.
304 echo ""
305 gettext -e "Checking Ssfs virtual disk\n"
306 separator
307 gettext "Virtual disk : "; du -sh $vdisk
308 gettext "Filesystem usage : "; du -sh $root
309 gettext "Remounting vdisk read/only before e2fsck -p..."
310 mount -o remount,loop,ro $vdisk $root && status
311 e2fsck -p $vdisk
312 gettext "Remounting vdisk read/write..."
313 mount -o remount,loop,rw $vdisk $root && status
314 separator && echo "" ;;
315 clean-vdisk)
316 # clean up the vdisk storage chroot.
317 if [ ! -d "$root/bin" ] || [ ! -d "$root/lib" ]; then
318 gettext -e "No chroot found in:"; echo " $root"
319 exit 0
320 fi
321 gettext -e "\nCleaning virtual disk\n"
322 separator
323 echo "Chroot path: $root"
324 cd $root
325 for dir in *
326 do
327 size=$(du -sh $dir | awk '{print $1}')
328 case "$dir" in
329 etc|home|root|lost*)
330 gettext "Skipping:"; echo " $dir $size *" ;;
331 *)
332 gettext "Removing:"; echo " $dir $size"
333 rm -rf $dir ;;
334 esac
335 done && separator && echo "" ;;
336 *)
337 help ;;
338 esac
339 exit 0