slish view slish @ rev 6

Bunch of improvments and new cmds
author Christophe Lincoln <pankso@slitaz.org>
date Thu Jan 23 22:51:29 2014 +0100 (2014-01-23)
parents e147172174de
children 65ad158a371b
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 # Default 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
41 list-queue List users in the signup queue
42 chroot Chroot to SliSH users default chroot
44 $(boldify "Options:")
45 --root= Set the path to the SliSH or user chroot
46 --clean Clean the chroot before gen-chroot
47 --queued Add all users from the signup queue
49 EOT
50 }
52 # Setup SliSH server
53 setup() {
54 # Allow users to use the chroot command
55 if ! grep -q "^chroot =" /etc/busybox.conf; then
56 echo "Allowing all users to use: chroot"
57 echo 'chroot = ssx root.root' >> /etc/busybox.conf
58 fi
59 # Gen a chroot if not yet done
60 if [ ! -d "$root" ]; then
61 echo "Creating a chroot environment..."
62 gen_chroot
63 fi
64 # Also used by the CGI web interface
65 for dir in ${people} ${cache} ${logs}; do
66 echo "Setting up the $(basename $dir) directory..."
67 mkdir -p ${dir} && chown www.www ${dir}
68 done
69 # Activity log must be writable by users
70 touch ${activity} && chmod 0666 ${activity}
71 echo "All done!"
72 }
74 # Show user configs
75 show_queued_user() {
76 . ${queue}/${user}/account.conf
77 newline
78 separator
79 cat << EOT
80 User : $user
81 Name : $name
82 Mail : $mail
83 EOT
84 separator
85 }
87 # Gen a user config file
88 user_config() {
89 echo -n "Creating SliSH account configuration..."
90 mkdir -p ${people}/${user}
91 cat > ${people}/${user}/account.conf << EOT
92 # SliSH account configuration
94 NAME="$name"
95 USER="$user"
96 MAIL="$mail"
98 ULIMIT="-d 4096 -m 4096 -l 32 -p 5 -v 16384"
99 QUOTA="50"
101 EOT
102 chmod 0600 ${people}/${user}/account.conf
103 chown ${user}.${user} ${people}/${user}/account.conf
104 status
105 }
107 # Mail body.
108 user_mail() {
109 cat << EOT
110 From: SliSH <shell@${domain}>
111 To: $mail
112 Date: $(date '+%a, %d %b %Y %H:%M:%S %z')
113 Subject: SliSH - Account created
114 Content-Type: text/plain; charset=utf-8
115 Content-Transfer-Encoding: 8bit
117 Hi,
119 Your custom SliTaz GNU/Linux SHell is ready to use! You can login with:
121 $ ssh ${user}@${domain}
123 Visit http://slish.in and http://www.slitaz.org for the latest news about
124 both projects.
126 Happy SliTaz :-)
128 ---
129 Sent by the SliSH Mailer
131 EOT
132 }
134 # Add a new SliSH user
135 add_user() {
136 home="$root/home/$user"
137 shell="/usr/bin/slish"
139 if grep -q ^${user}: /etc/passwd; then
140 newline
141 echo -n "User already exists: "; colorize 31 "$user"
142 rm -rf ${queue}/${user}
143 newline && exit 1
144 fi
146 newline
147 echo -n "$(boldify 'Creating user:') "; colorize 32 "$user"
148 separator
149 echo -e "$pass\n$pass" | adduser -h "$home" -g "SliSH User" \
150 -s ${shell} ${user} >/dev/null
152 # Add user to chroot /etc/passwd
153 if ! grep -q ^${user}: ${root}/etc/passwd; then
154 echo -n "Adding $user to: $root"
155 grep "^$user:" /etc/passwd >> ${root}/etc/passwd
156 grep "^$user:" /etc/group >> ${root}/etc/group
157 sed -i s"!$root!!" ${root}/etc/passwd
158 status
159 fi
161 # We don't want any files from /etc/skel.
162 echo -n "Cleaning home and creating: ~/.ssh"
163 rm -rf ${home} && mkdir -p ${home}/.ssh
164 status
166 # Let a web server access an eventual ~/Public dir
167 echo -n "Changing mode on user home..."
168 chown -R ${user}.${user} ${home}
169 chown ${user}.www ${home}
170 chmod 0750 ${home}
171 chmod 0700 ${home}/.ssh
172 status
173 user_config
175 # Send mail to notify user account creation
176 if [ -x /usr/sbin/sendmail ]; then
177 echo -n "Sending mail to: $mail"
178 user_mail | /usr/sbin/sendmail -f "shell@${domain}" "$mail"
179 status
180 fi
181 separator && newline
182 }
184 # Add all users from the signup queue
185 add_queued_user() {
186 for user in $(ls ${queue})
187 do
188 . ${queue}/${user}/account.conf
189 pass=$(cat ${queue}/${user}/passwd | base64 -d)
190 add_user
191 rm -rf ${queue}/${user}
192 done
193 }
195 # Delete a SliSH user
196 del_user() {
197 home="$root/home/$user"
198 if [ ! -d "$home" ] || [ ! "$user" ]; then
199 newline
200 echo "Missing --user= name option or invalid user name"
201 newline && exit 0
202 fi
203 newline
204 echo "$(boldify 'Deleting user:') $(colorize 32 "$user")"
205 separator
206 echo -n "Removing user account from: $(hostname) server"
207 deluser "$user"; status
208 sed -i "/^$user:/"d ${root}/etc/passwd
209 sed -i "/^$user:/"d ${root}/etc/group
210 echo -n "Removing all files in : $home"
211 rm -rf ${home} ; status
212 echo -n "Removing user config : $people/$user"
213 rm -rf "${people}/${user}" ; status
214 separator && newline
215 }
217 # Create a minimal chroot environment
218 gen_chroot() {
219 [ "$clean" ] && clean_chroot
220 if [ -d "$root/bin" ]; then
221 echo "A chroot already exists: Use -cc command or --clean option"
222 exit 1
223 fi
224 [ "$clean" ] || newline
225 boldify "Creating chroot in: $root"
226 separator
227 mkdir -p ${root}
228 for pkg in ${chrootpkgs}
229 do
230 echo -n "Installing: $pkg"
231 tazpkg -gi ${pkg} --root=${root} >/dev/null
232 status
233 done
234 echo -n "Installing: /bin/slish.sh"
235 install -m 0755 ${data}/slish.sh ${root}/bin
236 cp -a /etc/resolv.conf ${root}/etc
237 status
238 separator && newline
239 }
241 # Clean up a chroot environment
242 clean_chroot() {
243 if [ ! -d "$root/bin" ]; then
244 echo "No chroot found in: $root" && exit 0
245 fi
246 newline
247 boldify "Cleaning: $root"
248 separator
249 cd ${root}
250 for dir in *
251 do
252 size=$(du -sh $dir | awk '{print $1}')
253 case "$dir" in
254 etc|home|root|lost*) continue ;;
255 *)
256 echo -n "Removing: $dir $size"
257 rm -rf ${dir} ; status ;;
258 esac
259 done && separator && newline
260 }
262 #
263 # Handle commands
264 #
266 case "$1" in
267 info)
268 check_root
269 newline
270 boldify "Info"
271 separator
272 echo -n "Chroot size : " && du -sh ${root}
273 echo -n "Users accounts : " && ls -1 ${people} | wc -l
274 echo -n "Signup queue : " && ls -1 ${queue} | wc -l
275 separator && newline ;;
276 last)
277 check_root
278 newline
279 boldify "Last users"
280 separator
281 tac ${activity} | head -n 20
282 separator && newline ;;
283 setup)
284 check_root
285 setup ;;
286 adduser)
287 # We can adduser from cmdline or from the signup queue
288 check_root
289 if [ "$from-queu" ]; then
290 add_queued_user
291 else
292 add_user
293 fi ;;
294 deluser)
295 check_root
296 del_user ;;
297 -gc|gen-chroot)
298 check_root
299 gen_chroot ;;
300 -cc|clean-chroot)
301 check_root
302 clean_chroot ;;
303 -c|chroot)
304 echo "Chrooting to: $root"
305 chroot ${root} /bin/sh
306 echo "Exiting from: $root" ;;
307 -lq|list-queue)
308 # Check online signup queue but do nothing
309 for user in $(ls ${queued})
310 do
311 show_queued_user
312 done
313 echo "" ;;
314 *)
315 # /usr/bin/slish is executed on login to chroot the user
316 if [ -d "$root/home/$USER" ]; then
317 . ${people}/"$USER"/account.conf
318 log "Chrooting user: $USER"
319 ulimit $(echo "$ULIMIT")
320 exec chroot ${root} /bin/slish.sh "$@"
321 else
322 usage
323 fi ;;
324 esac
326 exit 0