slish view slish @ rev 8

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