slish view slish @ rev 5

Tiny edits
author Paul Issott <paul@slitaz.org>
date Wed Jan 22 21:21:29 2014 +0000 (2014-01-22)
parents e2f77a3185ab
children e9a2fa5a68d9
line source
1 #!/bin/sh
2 #
3 # SliSH - The SliTaz SHell on demand. No gettext this is a pure admin
4 # mainly developed for slish.in but which can be used by other projects.
5 #
6 # Copyright (C) 2014 SliTaz GNU/Linux - BSD License
7 # Author: Christophe Lincoln <pankso@slitaz.org>
8 #
9 export LANG=en LC_ALL=en
10 . /lib/libtaz.sh
12 [ "$root" ] || root="/home/slish/chroot"
13 people="$(dirname $root)/people"
14 data="/usr/share/slish"
15 logs="$(dirname $root)/logs"
16 cache="$(dirname $root)/cache"
17 activity="$logs/activity.log"
18 queue="${cache}/signup-queue"
19 domain="slish.in"
21 # Basic chroot packages
22 chrootpkgs="glibc-base slitaz-base-files ncursesw nano ytree busybox-slish
23 tcc rhapsody"
25 #
26 # Functions
27 #
29 usage() {
30 cat << EOT
32 $(boldify "Usage:") $(basename $0) [command] [--option]
34 $(boldify "Commands:")
35 info Display paths, configs and some stats
36 setup Setup SliSH server and user chroot
37 gen-chroot Generate a new default or user chroot
38 clean-chroot Clean the chroot but skip home and root
39 adduser Add a user to the server with \$HOME in chroot
40 deluser Delete a SliSH user from server and chroot
42 $(boldify "Options:")
43 --root= Set the path to the SliSH or user chroot
44 --clean Clean the chroot before gen-chroot
46 EOT
47 }
49 # Setup SliSH server
50 setup() {
51 # Allow users to use the chroot command
52 if ! grep -q "^chroot =" /etc/busybox.conf; then
53 echo "Allowing all users to use: chroot"
54 echo 'chroot = ssx root.root' >> /etc/busybox.conf
55 fi
56 # Gen a chroot if not yet done
57 if [ ! -d "$root" ]; then
58 echo "Creating a chroot environment..."
59 gen_chroot
60 fi
61 # Also used by the CGI web interface
62 for dir in ${people} ${cache} ${logs}; do
63 echo "Setting up the $(basename $dir) directory..."
64 mkdir -p ${dir} && chown www.www ${dir}
65 done
66 # Activity log must be writable by users
67 touch ${activity} && chmod 0666 ${activity}
68 echo "All done!"
69 }
71 # Gen a user config file
72 user_config() {
73 echo -n "Creating SliSH account configuration..."
74 mkdir -p ${people}/${user}
75 cat > ${people}/${user}/account.conf << EOT
76 # SliSH account configuration
78 NAME="$name"
79 USER="$user"
80 MAIL="$mail"
82 ULIMIT="-d 4096 -m 4096 -l 32 -p 5 -v 16384"
83 QUOTA=""
85 EOT
86 chmod 0600 ${people}/${user}/account.conf
87 chown ${user}.${user} ${people}/${user}/account.conf
88 status
89 }
91 # Mail body.
92 user_mail() {
93 cat << EOT
94 From: SliSH <shell@${domain}>
95 To: $mail
96 Date: $(date '+%a, %d %b %Y %H:%M:%S %z')
97 Subject: SliSH - Account created
98 Content-Type: text/plain; charset=utf-8
99 Content-Transfer-Encoding: 8bit
101 Hi,
103 Your custom SliTaz GNU/Linux SHell is ready to use! You can login with:
105 $ ssh ${user}@${domain}
107 Visit http://slish.in and http://www.slitaz.org for the latest news about
108 both projects.
110 Happy SliTaz :-)
112 ---
113 Sent by the SliSH Mailer
115 EOT
116 }
118 # Add a new SliSH user
119 add_user() {
120 home="$root/home/$user"
121 shell="/usr/bin/slish"
123 if grep -q ^${user}: /etc/passwd; then
124 newline
125 echo -n "User already exists: "; colorize 31 "$user"
126 newline && exit 0
127 fi
128 newline
129 echo -n "$(boldify 'Creating user:') "; colorize 32 "$user"
130 separator
131 echo -e "$pass\n$pass" | adduser -h "$home" -g "SliSH User" \
132 -s ${shell} ${user} >/dev/null
134 # Add user to chroot /etc/passwd
135 if ! grep -q ^${user}: ${root}/etc/passwd; then
136 echo -n "Adding $user to: $root"
137 grep "^$user:" /etc/passwd >> ${root}/etc/passwd
138 grep "^$user:" /etc/group >> ${root}/etc/group
139 sed -i s"!$root!!" ${root}/etc/passwd
140 status
141 fi
143 # We don't want any files from /etc/skel.
144 echo -n "Cleaning home and creating: ~/.ssh"
145 rm -rf ${home} && mkdir -p ${home}/.ssh
146 status
148 # Let a web server access an eventual ~/Public dir
149 echo -n "Changing mode on user home..."
150 chown -R ${user}.${user} ${home}
151 chown ${user}.www ${home}
152 chmod 0750 ${home}
153 chmod 0700 ${home}/.ssh
154 status
155 user_config
156 # Send mail to notify user account creation
157 if [ -x /usr/sbin/sendmail ]; then
158 echo -n "Sending mail to: $mail"
159 user_mail | /usr/sbin/sendmail -f "shell@${domain}" "$mail"
160 status
161 fi
162 separator && newline
163 }
165 # Delete a SliSH user
166 del_user() {
167 home="$root/home/$user"
168 if [ ! -d "$home" ] || [ ! "$user" ]; then
169 newline
170 echo "Missing --user= name option or invalid user name"
171 newline && exit 0
172 fi
173 newline
174 echo "$(boldify 'Deleting user:') $(colorize 32 "$user")"
175 separator
176 echo -n "Removing user account from: $(hostname) server"
177 deluser "$user"; status
178 sed -i "/^$user:/"d ${root}/etc/passwd
179 sed -i "/^$user:/"d ${root}/etc/group
180 echo -n "Removing all files in : $home"
181 rm -rf ${home} ; status
182 echo -n "Removing user config : $people/$user"
183 rm -rf "${people}/${user}" ; status
184 separator && newline
185 }
187 # Create a minimal chroot environment
188 gen_chroot() {
189 [ "$clean" ] && clean_chroot
190 if [ -d "$root/bin" ]; then
191 echo "A chroot already exists: Use -cc command or --clean option"
192 exit 1
193 fi
194 [ "$clean" ] || newline
195 boldify "Creating chroot in: $root"
196 separator
197 mkdir -p ${root}
198 for pkg in ${chrootpkgs}
199 do
200 echo -n "Installing: $pkg"
201 tazpkg -gi ${pkg} --root=${root} >/dev/null
202 status
203 done
204 echo -n "Installing: /bin/slish.sh"
205 install -m 0755 ${data}/slish.sh ${root}/bin
206 cp -a /etc/resolv.conf ${root}/etc
207 status
208 separator && newline
209 }
211 # Clean up a chroot environment
212 clean_chroot() {
213 if [ ! -d "$root/bin" ]; then
214 echo "No chroot found in: $root" && exit 0
215 fi
216 newline
217 boldify "Cleaning: $root"
218 separator
219 cd ${root}
220 for dir in *
221 do
222 size=$(du -sh $dir | awk '{print $1}')
223 case "$dir" in
224 etc|home|root|lost*) continue ;;
225 *)
226 echo -n "Removing: $dir $size"
227 rm -rf ${dir} ; status ;;
228 esac
229 done && separator && newline
230 }
232 #
233 # Handle commands
234 #
236 case "$1" in
237 -i|info)
238 check_root
239 echo -n "Chroot size : " && du -sh ${root}
240 echo -n "Users count : " && ls -1 ${people} | wc -l ;;
241 setup)
242 check_root
243 setup ;;
244 adduser)
245 check_root
246 add_user ;;
247 deluser)
248 check_root
249 del_user ;;
250 -gc|gen-chroot)
251 check_root
252 gen_chroot ;;
253 -cc|clean-chroot)
254 check_root
255 clean_chroot ;;
256 -c|chroot)
257 echo "Chrooting to: $root"
258 chroot ${root} /bin/sh
259 echo "Exiting from: $root" ;;
260 -cq|check-queue)
261 # Check online registration queue
262 for user in $(ls ${queue})
263 do
264 . ${queue}/${user}/account.conf
265 pass=$(cat ${queue}/${user}/passwd | base64 -d)
266 add_user
267 rm -rf ${queue}/${user}
268 done ;;
269 *)
270 # /usr/bin/slish is be executed on login to chroot the user
271 if [ -d "$root/home/$USER" ]; then
272 . ${people}/"$USER"/account.conf
273 log "Chrooting user: $USER"
274 ulimit $(echo "$ULIMIT")
275 exec chroot $root /bin/slish.sh "$@"
276 else
277 usage
278 fi ;;
279 esac
281 exit 0