ssfs view ssfs-server @ rev 13

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