ssfs view ssfs-server @ rev 23

README: small addition and fix to ssfs-server
author Christophe Lincoln <pankso@slitaz.org>
date Sun Jun 12 04:40:40 2011 +0200 (2011-06-12)
parents 2ab2f1cbd203
children 8727a2a80b10
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 cache=/var/cache/ssfs
11 [ -f "/etc/ssfs/$app.conf" ] && . /etc/ssfs/$app.conf
12 [ -f "./data/$app.conf" ] && . ./data/$app.conf
14 # Be sure we're root.
15 [ $(id -u) != 0 ] && gettext "You must be root to run:" && \
16 echo " $app" && exit 0
18 # Parse cmdline options.
19 for opt in $@
20 do
21 case "$opt" in
22 --login=*)
23 login=${opt#--login=} ;;
24 --id=*)
25 id=${opt#--id=} ;;
26 --pass=*)
27 pass=${opt#--pass=} ;;
28 --root=*)
29 root=${opt#--root=} ;;
30 --vdisk=*)
31 vdisk=${opt#--vdisk=} ;;
32 --size=*)
33 size=${opt#--size=} ;;
34 *)
35 continue ;;
36 esac
37 done
39 [ "$root" ] || root=${SSFS_CHROOT}
40 [ "$vdisk" ] || vdisk=${SSFS_VDISK}
41 [ "$size" ] || size=${SSFS_SIZE}
43 #
44 # Functions
45 #
47 # Built-in help usage.
48 help() {
49 cat << EOT
51 $(echo -e "\033[1m$(gettext "Usage:")\033[0m") $app [command] [--option=]
53 $(echo -e "\033[1m$(gettext "Commands:")\033[0m")
54 help $(gettext "Display this short usage.")
55 users $(gettext "List user accounts and stats.")
56 adduser $(gettext "Add a user to the system with \$HOME in chroot.")
57 deluser $(gettext "Delete a user and remove \$HOME files.")
58 chroot $(gettext "Chroot to Ssfs storage root.")
59 gen-vdisk $(gettext "Create a vdisk with chroot for files storage.")
60 clean-vdisk $(gettext "Clean the vdisk but skip home and root.")
61 check-vdisk $(gettext "Check vdisk filesystem with e2fsck.")
62 mount-vdisk $(gettext "Mount ssfs virtual disk.")
63 umount-vdisk $(gettext "Unmount the vdisk and free loop device.")
65 $(echo -e "\033[1m$(gettext "Options:")\033[0m")
66 --login= $(gettext "Login name for add or del an user.")
67 --id= $(gettext "User id for adduser command.")
68 --pass= $(gettext "User password for adduser.")
69 --root= $(gettext "The path to the Ssfs vdisk chroot.")
70 --vdisk= $(gettext "Set the Ssfs vdisk path and name.")
71 --size= $(gettext "Set the ext3 vdisk size in Gb.")
73 EOT
74 }
76 status() {
77 [ $? = 0 ] && echo " OK"
78 [ $? = 1 ] && echo -e " ERROR\n" && exit 1
79 }
81 separator() {
82 echo "================================================================================"
83 }
85 # We have custom config when adding user to handle quota and user info.
86 user_paths() {
87 config=$SSFS_USERS/$login.conf
88 home=$root/./home/$login
89 }
91 user_info() {
92 cat << EOT
94 $(gettext "User login :") $login
95 $(gettext "User quota :") $QUOTA
96 $(gettext "Home usage :") $usage
98 EOT
99 }
101 user_config() {
102 gettext "Creating Ssfs user configuration file..."
103 cat > $config << EOT
104 # Ssfs user configuration file.
106 LOGIN="$login"
107 QUOTA="$DEFAULT_QUOTA"
108 EOT
109 chmod 0600 $config && status
110 echo ""
111 }
113 # Handle Ssfs virtual disk.
114 umount_vdisk() {
115 if mount | fgrep -q $root; then
116 loop=$(mount | fgrep $root | awk '{print $1}')
117 gettext "Unmounting Ssfs vdisk:"; echo " $vdisk"
118 umount $root && sleep 1
119 gettext "Detaching loop device:"; echo " $loop"
120 losetup -d $loop
121 else
122 gettext "Ssfs vdisk is not mounted:"; echo " $vdisk"
123 fi
124 }
126 mount_vdisk() {
127 if ! mount | fgrep -q $root; then
128 [ -d "$root" ] || mkdir -p $root
129 gettext "Mounting virtual disk:"
130 mount -o loop -t ext3 $vdisk $root
131 else
132 gettext "Ssfs vdisk is already mounted:"
133 fi
134 echo " $vdisk $root"
135 }
137 #
138 # Commands
139 #
141 case "$1" in
142 users)
143 gettext -e "\nChecking:"; echo " /etc/passwd"
144 fgrep "Ssfs User" /etc/passwd | while read line
145 do
146 login=$(echo $line | cut -d ":" -f 1)
147 home="$root/home/$login"
148 usage=$(du -sm $home | awk '{print $1}')
149 config=$SSFS_USERS/$login.conf
150 . $config || gettext -e "WARNING: No config file\n"
151 user_info
152 done
153 users=$(ls $SSFS_USERS | wc -l)
154 gettext "Users:"; echo -e " $users\n" ;;
155 adduser)
156 # Add a Ssfs user to the system with $HOME in chroot.
157 [ -z "$login" ] && gettext -e "Missing user login name.\n" && exit 0
158 [ -z "$id" ] && gettext -e "Missing user id.\n" && exit 0
159 [ -z "$pass" ] && gettext -e "Missing user password.\n" && exit 0
160 user_paths
162 gettext -e "\nChecking:"; echo " /etc/passwd"
163 if grep ^$login: /etc/passwd; then
164 gettext -e "Exiting, user already exists:"
165 echo -e " $login\n" && exit 0
166 fi
167 gettext "Creating user: $login..."
168 echo -e "$pass\n$pass" | \
169 adduser -h "$home" -g "Ssfs User" -u $id $login >/dev/null
170 status
172 # We don't want any files from /etc/skel.
173 gettext "Cleaning home and creating: Sync/..."
174 rm -rf $home && mkdir -p $home/Sync $home/.ssh && status
175 gettext "Changing mode on user home: 0700..."
176 chown -R $login.$login $home
177 chmod 0700 $home && status
179 # Create a custom config per user in SSFS_USERS.
180 [ ! -d "$SSFS_USERS" ] && mkdir -p $SSFS_USERS
181 user_config ;;
182 deluser)
183 [ -z "$login" ] && gettext -e "Missing user login name.\n" && exit 0
184 user_paths
185 gettext -e "\nDeleting user:"; echo -n " $login..."
186 deluser $login || status && status
187 gettext "Removing all files in:"; echo -n " $home..."
188 rm -rf $home && status
189 gettext "Removing user config:"; echo -n " $login.conf..."
190 rm -rf $config && status
191 echo "" ;;
192 chroot)
193 gettext -e "\nChanging root to:"; echo -e " $root\n"
194 chroot $root
195 gettext -e "\nBack to the host system:"
196 echo -e " $(hostname)\n" ;;
197 gen-vdisk)
198 # Generated a virtual disk with a minimal chroot for Ssfs users home.
199 if [ -d "$root/bin" ]; then
200 gettext -e "A chroot already exists in:"; echo " $root"
201 exit 0
202 fi
203 echo ""
204 gettext "Creating chroot in:"; echo " $root"
205 separator
207 # Create vdisk if missing.
208 if [ ! -f "$vdisk" ]; then
209 gettext "Creating virtual disk:"; echo " $vdisk ${size}Gb"
210 dd if=/dev/zero of=$vdisk bs=1G count=$size
211 chmod 0600 $vdisk && du -sh $vdisk
212 gettext "Creating ext3 filesystem..."
213 mkfs.ext3 -q -T ext3 -L "Ssfs" -F $vdisk
214 status
215 mount_vdisk
216 fi
218 # Create a radicaly minimal chroot with all libs in /lib.
219 gettext "Creating base files..."
220 mkdir -p $root && cd $root
221 for d in etc tmp lib usr home root
222 do
223 mkdir -p $d
224 done && status
225 cp -a /etc/slitaz-release $root/etc
226 #cp -a /etc/nsswitch.conf $root/etc
227 echo "root:x:0:0:root:/root:/bin/sh" > etc/passwd
228 echo "root::13525:0:99999:7:::" > etc/shadow
229 echo "root:x:0:" > etc/group
230 echo "root:*::" > etc/gshadow
232 gettext "Setting files permissions..."
233 chmod 640 etc/shadow etc/gshadow
234 chmod 0700 root && chmod 1777 tmp
235 status
237 # Busybox without deps (get && extract). No system comands are allowed
238 # in /etc/busybox.conf to restrict SSHed users.
239 gettext "Installing Busybox..."
240 cd $root/tmp
241 tazpkg get busybox >/dev/null
242 tazpkg extract busybox-* >/dev/null
243 rm -rf fs && mv -f busybox-*/fs . && rm -rf busybox-*
244 cp -a fs/bin fs/sbin $root
245 cp -a fs/usr/bin fs/usr/sbin $root/usr
246 rm -rf fs
247 status
248 gettext "Creatin restrictive Busybox config file..."
249 echo '# /etc/busybox.conf: Ssfs Busybox configuration.' \
250 > $root/etc/busybox.conf
251 echo -e "\nsu = ---" >> $root/etc/busybox.conf
252 chmod 0600 $root/etc/busybox.conf
253 status
255 # Glib minimal libs, use host lib since package should be installed
256 # from same repo.
257 gettext "Installing Glibc libraries..."
258 for l in ld-*.*so* libc-*.*so libc.so.* libnss_files*
259 do
260 cp -a /lib/$l* $root/lib
261 done && status
262 size=$(du -sh $root | awk '{print $1}')
263 separator
264 gettext "Vdisk used space:"; echo -e " $size\n" ;;
265 mount-vdisk)
266 mount_vdisk ;;
267 umount-vdisk)
268 umount_vdisk ;;
269 check-vdisk)
270 # Check vdisk with e2fsck.
271 echo ""
272 gettext -e "Checking Ssfs virtual disk\n"
273 separator
274 gettext "Virtual disk : "; du -sh $vdisk
275 gettext "Filesystem usage : "; du -sh $root
276 gettext "Remounting vdisk read/only before e2fsck -p..."
277 mount -o remount,loop,ro $vdisk $root && status
278 e2fsck -p $vdisk
279 gettext "Remounting vdisk read/write..."
280 mount -o remount,loop,rw $vdisk $root && status
281 separator && echo "" ;;
282 clean-vdisk)
283 # clean up the vdisk storage chroot.
284 if [ ! -d "$root/bin" ] || [ ! -d "$root/usr" ]; then
285 gettext -e "No chroot found in:"; echo " $root"
286 exit 0
287 fi
288 gettext -e "\nCleaning virtual disk\n"
289 separator
290 gettext "Changing directory to:"; echo " $root"
291 cd $root
292 for dir in *
293 do
294 size=$(du -sh $dir | awk '{print $1}')
295 case "$dir" in
296 home|root|lost*)
297 gettext "Skipping:"; echo " $dir $size *" ;;
298 *)
299 gettext "Removing:"; echo " $dir $size"
300 rm -rf $dir ;;
301 esac
302 done && separator && echo "" ;;
303 *)
304 help ;;
305 esac
306 exit 0