slish rev 1
Add slish command line utility
author | Christophe Lincoln <pankso@slitaz.org> |
---|---|
date | Wed Jan 22 18:35:00 2014 +0100 (2014-01-22) |
parents | 51f598a23d7d |
children | 103734990d08 |
files | slish |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/slish Wed Jan 22 18:35:00 2014 +0100 1.3 @@ -0,0 +1,281 @@ 1.4 +#!/bin/sh 1.5 +# 1.6 +# SliSH - The SliTaz SHell on demand. No gettext this is a pure adim 1.7 +# mainly developpee for slish.in but who can be used by other projects. 1.8 +# 1.9 +# Copyright (C) 2014 SliTaz GNU/Linux - BSD License 1.10 +# Author: Christophe Lincoln <pankso@slitaz.org> 1.11 +# 1.12 +export LANG=en LC_ALL=en 1.13 +. /lib/libtaz.sh 1.14 + 1.15 +[ "$root" ] || root="/home/slish/chroot" 1.16 +people="$(dirname $root)/people" 1.17 +data="/usr/share/slish" 1.18 +logs="$(dirname $root)/logs" 1.19 +cache="$(dirname $root)/cache" 1.20 +activity="$logs/activity.log" 1.21 +queue="${cache}/signup-queue" 1.22 +domain="slish.in" 1.23 + 1.24 +# Basic chroot packages 1.25 +chrootpkgs="glibc-base slitaz-base-files ncursesw nano ytree busybox-slish 1.26 +tcc rhapsody" 1.27 + 1.28 +# 1.29 +# Functions 1.30 +# 1.31 + 1.32 +usage() { 1.33 + cat << EOT 1.34 + 1.35 +$(boldify "Usage:") $(basename $0) [command] [--option] 1.36 + 1.37 +$(boldify "Commands:") 1.38 + info Display paths, configs and some stats 1.39 + setup Setup SliSH server and users chroot 1.40 + gen-chroot Generate a new default or user chroot 1.41 + clean-chroot Clean the chroot but skip home and root 1.42 + adduser Add a user to the server with \$HOME in chroot 1.43 + deluser Delete a SliSH user from server and chroot 1.44 + 1.45 +$(boldify "Options:") 1.46 + --root= Set the path to the SliSH or user chroot 1.47 + --clean Clean the chroot before gen-chroot 1.48 + 1.49 +EOT 1.50 +} 1.51 + 1.52 +# Setup SliSH server 1.53 +setup() { 1.54 + # Allow users to use the chroot command 1.55 + if ! grep -q "^chroot =" /etc/busybox.conf; then 1.56 + echo "Allowing all users to use: chroot" 1.57 + echo 'chroot = ssx root.root' >> /etc/busybox.conf 1.58 + fi 1.59 + # Gen a chroot if not yet done 1.60 + if [ ! -d "$root" ]; then 1.61 + echo "Creating a chroot environment..." 1.62 + gen_chroot 1.63 + fi 1.64 + # Also used by the CGI web interface 1.65 + for dir in ${people} ${cache} ${logs}; do 1.66 + echo "Setting up the $(basename $dir) directory..." 1.67 + mkdir -p ${dir} && chown www.www ${dir} 1.68 + done 1.69 + # Activity log must be writtable by users 1.70 + touch ${activity} && chmod 0666 ${activity} 1.71 + echo "All done!" 1.72 +} 1.73 + 1.74 +# Gen a user config file 1.75 +user_config() { 1.76 + echo -n "Creating SliSH account configuration..." 1.77 + mkdir -p ${people}/${user} 1.78 + cat > ${people}/${user}/account.conf << EOT 1.79 +# SliSH account configuration 1.80 + 1.81 +NAME="$name" 1.82 +USER="$user" 1.83 +MAIL="$mail" 1.84 + 1.85 +ULIMIT="-d 4096 -m 4096 -l 32 -p 5 -v 16384" 1.86 +QUOTA="" 1.87 + 1.88 +EOT 1.89 + chmod 0600 ${people}/${user}/account.conf 1.90 + chown ${user}.${user} ${people}/${user}/account.conf 1.91 + status 1.92 +} 1.93 + 1.94 +# Mail body. 1.95 +user_mail() { 1.96 + cat << EOT 1.97 +From: SliSH <shell@${domain}> 1.98 +To: $mail 1.99 +Date: $(date '+%a, %d %b %Y %H:%M:%S %z') 1.100 +Subject: SliSH - Account created 1.101 +Content-Type: text/plain; charset=utf-8 1.102 +Content-Transfer-Encoding: 8bit 1.103 + 1.104 +Hi, 1.105 + 1.106 +Your custom SliTaz GNU/Linux SHell is ready to use! You can login with: 1.107 + 1.108 +$ ssh ${user}@${domain} 1.109 + 1.110 +Visit http://slish.in and http://www.slitaz.org for the latest news about 1.111 +both projects. 1.112 + 1.113 +Happy SliTaz :-) 1.114 + 1.115 +--- 1.116 +Sent by the SliSH Mailer 1.117 + 1.118 +EOT 1.119 +} 1.120 + 1.121 +# Add a new SliSH user 1.122 +add_user() { 1.123 + home="$root/home/$user" 1.124 + shell="/usr/bin/slish" 1.125 + 1.126 + if grep -q ^${user}: /etc/passwd; then 1.127 + newline 1.128 + echo -n "User already exists: "; colorize 31 "$user" 1.129 + newline && exit 0 1.130 + fi 1.131 + newline 1.132 + echo -n "$(boldify 'Creating user:') "; colorize 32 "$user" 1.133 + separator 1.134 + echo -e "$pass\n$pass" | adduser -h "$home" -g "SliSH User" \ 1.135 + -s ${shell} ${user} >/dev/null 1.136 + 1.137 + # Add user to chroot /etc/passwd 1.138 + if ! grep -q ^${user}: ${root}/etc/passwd; then 1.139 + echo -n "Adding $user to: $root" 1.140 + grep "^$user:" /etc/passwd >> ${root}/etc/passwd 1.141 + grep "^$user:" /etc/group >> ${root}/etc/group 1.142 + sed -i s"!$root!!" ${root}/etc/passwd 1.143 + status 1.144 + fi 1.145 + 1.146 + # We don't want any files from /etc/skel. 1.147 + echo -n "Cleaning home and creating: ~/.ssh" 1.148 + rm -rf ${home} && mkdir -p ${home}/.ssh 1.149 + status 1.150 + 1.151 + # Let a web server access an eventual ~/Public dir 1.152 + echo -n "Changing mode on user home..." 1.153 + chown -R ${user}.${user} ${home} 1.154 + chown ${user}.www ${home} 1.155 + chmod 0750 ${home} 1.156 + chmod 0700 ${home}/.ssh 1.157 + status 1.158 + user_config 1.159 + # Send mail to notify user account creation 1.160 + if [ -x /usr/sbin/sendmail ]; then 1.161 + echo -n "Sending mail to: $mail" 1.162 + user_mail | /usr/sbin/sendmail -f "shell@${domain}" "$mail" 1.163 + status 1.164 + fi 1.165 + separator && newline 1.166 +} 1.167 + 1.168 +# Delete a SliSH user 1.169 +del_user() { 1.170 + home="$root/home/$user" 1.171 + if [ ! -d "$home" ] || [ ! "$user" ]; then 1.172 + newline 1.173 + echo "Missing --user= name option or invalid user name" 1.174 + newline && exit 0 1.175 + fi 1.176 + newline 1.177 + echo "$(boldify 'Deleting user:') $(colorize 32 "$user")" 1.178 + separator 1.179 + echo -n "Removing user account from: $(hostname) server" 1.180 + deluser "$user"; status 1.181 + sed -i "/^$user:/"d ${root}/etc/passwd 1.182 + sed -i "/^$user:/"d ${root}/etc/group 1.183 + echo -n "Removing all files in : $home" 1.184 + rm -rf ${home} ; status 1.185 + echo -n "Removing user config : $people/$user" 1.186 + rm -rf "${people}/${user}" ; status 1.187 + separator && newline 1.188 +} 1.189 + 1.190 +# Create a minimal chroot environment 1.191 +gen_chroot() { 1.192 + [ "$clean" ] && clean_chroot 1.193 + if [ -d "$root/bin" ]; then 1.194 + echo "A chroot already exist: Use -cc command or --clean option" 1.195 + exit 1 1.196 + fi 1.197 + [ "$clean" ] || newline 1.198 + boldify "Creating chroot in: $root" 1.199 + separator 1.200 + mkdir -p ${root} 1.201 + for pkg in ${chrootpkgs} 1.202 + do 1.203 + echo -n "Installing: $pkg" 1.204 + tazpkg -gi ${pkg} --root=${root} >/dev/null 1.205 + status 1.206 + done 1.207 + echo -n "Installing: /bin/slish.sh" 1.208 + install -m 0755 ${data}/slish.sh ${root}/bin 1.209 + cp -a /etc/resolv.conf ${root}/etc 1.210 + status 1.211 + separator && newline 1.212 +} 1.213 + 1.214 +# Clean up a chroot environment 1.215 +clean_chroot() { 1.216 + if [ ! -d "$root/bin" ]; then 1.217 + echo "No chroot found in: $root" && exit 0 1.218 + fi 1.219 + newline 1.220 + boldify "Cleaning: $root" 1.221 + separator 1.222 + cd ${root} 1.223 + for dir in * 1.224 + do 1.225 + size=$(du -sh $dir | awk '{print $1}') 1.226 + case "$dir" in 1.227 + etc|home|root|lost*) continue ;; 1.228 + *) 1.229 + echo -n "Removing: $dir $size" 1.230 + rm -rf ${dir} ; status ;; 1.231 + esac 1.232 + done && separator && newline 1.233 +} 1.234 + 1.235 +# 1.236 +# Handle commands 1.237 +# 1.238 + 1.239 +case "$1" in 1.240 + -i|info) 1.241 + check_root 1.242 + echo -n "Chroot size : " && du -sh ${root} 1.243 + echo -n "Users count : " && ls -1 ${people} | wc -l ;; 1.244 + setup) 1.245 + check_root 1.246 + setup ;; 1.247 + adduser) 1.248 + check_root 1.249 + add_user ;; 1.250 + deluser) 1.251 + check_root 1.252 + del_user ;; 1.253 + -gc|gen-chroot) 1.254 + check_root 1.255 + gen_chroot ;; 1.256 + -cc|clean-chroot) 1.257 + check_root 1.258 + clean_chroot ;; 1.259 + -c|chroot) 1.260 + echo "Chrooting to: $root" 1.261 + chroot ${root} /bin/sh 1.262 + echo "Exiting from: $root" ;; 1.263 + -cq|check-queue) 1.264 + # Check online registration queue 1.265 + for user in $(ls ${queue}) 1.266 + do 1.267 + . ${queue}/${user}/account.conf 1.268 + pass=$(cat ${queue}/${user}/passwd | base64 -d) 1.269 + add_user 1.270 + rm -rf ${queue}/${user} 1.271 + done ;; 1.272 + *) 1.273 + # /usr/bin/slish is be exectue on login to chroot the user 1.274 + if [ -d "$root/home/$USER" ]; then 1.275 + . ${people}/"$USER"/account.conf 1.276 + log "Chrooting user: $USER" 1.277 + ulimit $(echo "$ULIMIT") 1.278 + exec chroot $root /bin/slish.sh "$@" 1.279 + else 1.280 + usage 1.281 + fi ;; 1.282 +esac 1.283 + 1.284 +exit 0