ssfs rev 4

Add ssfs-server the server side tool
author Christophe Lincoln <pankso@slitaz.org>
date Sat Jun 11 08:42:29 2011 +0200 (2011-06-11)
parents 62f5949eac52
children 57a2a9a2bcdb
files data/ssfs-server.conf ssfs-server
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/data/ssfs-server.conf	Sat Jun 11 08:42:29 2011 +0200
     1.3 @@ -0,0 +1,11 @@
     1.4 +# Ssfs server side tool configuration file.
     1.5 +#
     1.6 +
     1.7 +# Path to Ssfs storage root.
     1.8 +SSFS_CHROOT="/home/ssfs"
     1.9 +
    1.10 +# User configuration file path.
    1.11 +SSFS_USERS="/etc/ssfs/users"
    1.12 +
    1.13 +# Default quota peer user in Mb.
    1.14 +DEFAULT_QUOTA="250"
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/ssfs-server	Sat Jun 11 08:42:29 2011 +0200
     2.3 @@ -0,0 +1,196 @@
     2.4 +#!/bin/sh
     2.5 +#
     2.6 +# SliTaz Secure File Storage server side tool.
     2.7 +#
     2.8 +# Copyright (C) SliTaz GNU/Linux - BSD License
     2.9 +# Author: Christophe Lincoln <pankso@slitaz.org>
    2.10 +#
    2.11 +
    2.12 +app=$(basename $0)
    2.13 +[ -f "/etc/ssfs/$app.conf" ] && . /etc/ssfs/$app.conf
    2.14 +[ -f "./data/$app.conf" ] && . ./data/$app.conf
    2.15 +
    2.16 +# Be sure we're root.
    2.17 +[ $(id -u) != 0 ] && gettext "You must be root to run:" && \
    2.18 +	echo " $app" && exit 0
    2.19 +
    2.20 +# Parse cmdline options.
    2.21 +for opt in $@
    2.22 +do
    2.23 +	case "$opt" in
    2.24 +		--login=*)
    2.25 +			login=${opt#--login=} ;;
    2.26 +		--id=*)
    2.27 +			id=${opt#--id=} ;;
    2.28 +		--pass=*)
    2.29 +			pass=${opt#--pass=} ;;
    2.30 +		--root=*)
    2.31 +			root=${opt#--root=} ;;
    2.32 +		*)
    2.33 +			continue ;;
    2.34 +	esac
    2.35 +done
    2.36 +
    2.37 +[ "$root" ] || root=${SSFS_CHROOT}
    2.38 +
    2.39 +#
    2.40 +# Functions
    2.41 +#
    2.42 +
    2.43 +# Built-in help usage.
    2.44 +help() {
    2.45 +	cat << EOT
    2.46 +
    2.47 +$(echo -e "\033[1m$(gettext "Usage:")\033[0m") $app [command] [--option=]
    2.48 +
    2.49 +$(echo -e "\033[1m$(gettext "Commands:")\033[0m")
    2.50 +  help          $(gettext "Display this short usage.")
    2.51 +  users         $(gettext "List users account and stats.")
    2.52 +  adduser       $(gettext "Add a user to the system with \$HOME in chroot.")
    2.53 +  deluser       $(gettext "Delete a user and remove \$HOME files.")
    2.54 +  chroot        $(gettext "Chroot to Ssfs storage root.")
    2.55 +  gen-chroot    $(gettext "Create a chroot for users files storage.")
    2.56 +  clean-chroot  $(gettext "Clean the chroot but skip home/ and root/.")
    2.57 +
    2.58 +$(echo -e "\033[1m$(gettext "Options:")\033[0m")
    2.59 +  --login=      $(gettext "Login name for add or del an user.")
    2.60 +  --id=         $(gettext "User id for adduser command.")
    2.61 +  --pass=       $(gettext "User password for adduser.")
    2.62 +  --root=       $(gettext "The path to the Ssfs chroot.")
    2.63 +
    2.64 +EOT
    2.65 +}
    2.66 +
    2.67 +status() {
    2.68 +	[ $? = 0 ] && echo " OK" 
    2.69 +	[ $? = 1 ] && echo -e " ERROR\n" && exit 1
    2.70 +}
    2.71 +
    2.72 +user_paths() {
    2.73 +	config=$SSFS_USERS/$login.conf
    2.74 +	home=$root/./home/$login
    2.75 +}
    2.76 +
    2.77 +user_info() {
    2.78 +	cat << EOT
    2.79 +
    2.80 +$(gettext "User login :") $login
    2.81 +$(gettext "User quota :") $QUOTA
    2.82 +$(gettext "Home usage :") $usage
    2.83 +
    2.84 +EOT
    2.85 +}
    2.86 +
    2.87 +# Create a custom config when adding user to handle quota and user info.
    2.88 +user_config() {
    2.89 +	gettext "Creating Ssfs user configuration file..."
    2.90 +	cat > $config << EOT
    2.91 +# Ssfs user configuration file.
    2.92 +
    2.93 +LOGIN="$login"
    2.94 +QUOTA="$DEFAULT_QUOTA"
    2.95 +PUBLIC="no"
    2.96 +EOT
    2.97 +	chmod 0600 $config && status
    2.98 +	echo ""
    2.99 +}
   2.100 +
   2.101 +#
   2.102 +# Commands
   2.103 +#
   2.104 +
   2.105 +case "$1" in
   2.106 +	users)
   2.107 +		gettext -e "\nChecking:"; echo " /etc/passwd"
   2.108 +		fgrep "Ssfs User" /etc/passwd | while read line
   2.109 +		do
   2.110 +			login=$(echo $line | cut -d ":" -f 1)
   2.111 +			home="$root/home/$login"
   2.112 +			usage=$(du -sm $home | awk '{print $1}')
   2.113 +			config=$SSFS_USERS/$login.conf
   2.114 +			. $config || gettext -e "WARNING: No config file\n"
   2.115 +			user_info
   2.116 +		done 
   2.117 +		users=$(ls $SSFS_USERS | wc -l)
   2.118 +		gettext "Users:"; echo -e " $users\n" ;;
   2.119 +	adduser)
   2.120 +		# Add a Ssfs user to the system with $HOME in chroot.
   2.121 +		[ -z "$login" ] && gettext -e "Missing user login name.\n" && exit 0
   2.122 +		[ -z "$id" ] && gettext -e "Missing user id.\n" && exit 0
   2.123 +		[ -z "$pass" ] && gettext -e "Missing user password.\n" && exit 0
   2.124 +		user_paths
   2.125 +		
   2.126 +		gettext -e "\nChecking:"; echo " /etc/passwd"
   2.127 +		if grep ^$login: /etc/passwd; then
   2.128 +			gettext -e "Exiting user already exist:"
   2.129 +			echo -e " $login\n" && exit 0
   2.130 +		fi
   2.131 +		gettext "Creating user: $login..."
   2.132 +		echo -e "$pass\n$pass" | \
   2.133 +			adduser -h "$home" -g "Ssfs User" -u $id $login >/dev/null
   2.134 +		status
   2.135 +		
   2.136 +		# We dont want any files from /etc/skel.
   2.137 +		gettext "Cleaning home and creating: Sync/..."
   2.138 +		rm -rf $home && mkdir -p $home/Sync && status
   2.139 +		gettext "Changing mode on user home: 0700..."
   2.140 +		chown -R $login.$login $home
   2.141 +		chmod 0700 $home && status
   2.142 +
   2.143 +		# Create a custom config peer user in SSFS_USERS.
   2.144 +		[ ! -d "$SSFS_USERS" ] && mkdir -p $SSFS_USERS
   2.145 +		user_config ;;
   2.146 +	deluser)
   2.147 +		[ -z "$login" ] && gettext -e "Missing user login name.\n" && exit 0
   2.148 +		user_paths
   2.149 +		gettext -e "\nDeleting user:"; echo -n " $login..."
   2.150 +		deluser $login || status && status
   2.151 +		gettext "Removing all files in:"; echo -n " $home..."
   2.152 +		rm -rf $home && status
   2.153 +		gettext "Removing user config:"; echo -n " $login.conf..."
   2.154 +		rm -rf $config && status
   2.155 +		echo "" ;;
   2.156 +	chroot)
   2.157 +		gettext -e "\nChanging root to:"; echo -e " $root\n"
   2.158 +		chroot $root
   2.159 +		gettext -e "\nBack to the host system:"
   2.160 +		echo -e " $(hostname)\n" ;;
   2.161 +	gen-chroot)
   2.162 +		# Generated a minimal chroot for Ssfs users home.
   2.163 +		if [ -d "$root/bin" ]; then
   2.164 +			gettext -e "A chroot already exist in:"; echo " $root"
   2.165 +			exit 0
   2.166 +		fi
   2.167 +		gettext -e "\nCreating chroot in:"; echo " $root"
   2.168 +		gettext "Installing SliTaz base files..."
   2.169 +		yes | tazpkg get-install slitaz-base-files --root=$root >/dev/null
   2.170 +		status
   2.171 +		gettext "Installing Busybox..."
   2.172 +		yes | tazpkg get-install busybox --root=$root >/dev/null
   2.173 +		status
   2.174 +		gettext "Cleaning Ssfs chroot..."
   2.175 +		rm -f $root/init
   2.176 +		status && echo "" ;;
   2.177 +	clean-chroot)
   2.178 +		# clean up the storage chroot.
   2.179 +		if [ ! -d "$root/bin" ] || [ ! -d "$root/usr" ]; then
   2.180 +			gettext -e "No chroot found in:"; echo " $root"
   2.181 +			exit 0
   2.182 +		fi
   2.183 +		gettext -e "\nChanging directory to:"; echo " $root"
   2.184 +		cd $root
   2.185 +		for dir in *
   2.186 +		do
   2.187 +			size=$(du -sh $dir | awk '{print $1}')
   2.188 +			case "$dir" in
   2.189 +				home|root)
   2.190 +					gettext "Skipping:"; echo " $dir $size *" ;;
   2.191 +				*)
   2.192 +					gettext "Removing:"; echo " $dir $size"
   2.193 +					rm -rf $dir ;;
   2.194 +			esac
   2.195 +		done && echo "" ;;
   2.196 +	*)
   2.197 +		help ;;
   2.198 +esac
   2.199 +exit 0