slitaz-dev-tools view tazu/tazu @ rev 267

tazu: many improvments to handle SCN/Bugs users accounts
author Christophe Lincoln <pankso@slitaz.org>
date Mon Feb 20 15:34:43 2017 +0100 (2017-02-20)
parents b8d41d8263f2
children 03310414b391
line source
1 #!/bin/sh
2 #
3 # TazU - SliTaz Users account utility
4 #
5 # This tool is used to mange SliTaz users accounts on bugs.slitaz.org
6 # and scn.slitaz.org. It can also be used to admin TinyCM users DB.
7 #
8 # Copyright 2017 (C) SliTaz GNU/Linux - BSD License
9 # Author: Christophe Lincoln <pankso@slitaz.org>
10 #
11 . /lib/libtaz.sh
12 check_root
13 user="$1"
15 people="/var/lib/slitaz/people"
16 authfile="/var/lib/slitaz/auth/people"
17 admin="/var/lib/slitaz/auth/admin"
19 # Sanity check
20 for file in ${authfile} ${admin}; do
21 if ! [ -f "$file" ]; then
22 echo check $file
23 install -d -m 0700 -o www -g www $(dirname $file)
24 touch ${file} && chown www.www ${file} && chmod 0600 ${file}
25 fi
26 done
28 #
29 # Functions
30 #
32 usage() {
33 cat << EOT
35 Usage: $(basename $0) [user|command] [--option]
37 Commands:
38 stats Count all users
39 list List all users
40 check Check accounts integrity
42 Options:
43 --admin Make user admin
44 --edit Edit user account.conf
45 --del Delete a user account (or all corrupted)
47 Examples:
48 tazu username --admin
49 tazu "user name" --del
51 EOT
52 }
54 no_account() {
55 echo "No user account for: $user"
56 }
58 # Delete a user (we may have corrupted accounts: check twice)
59 # Usage: deluser "username"
60 deluser() {
61 if [ -d "${people}/${1}" ] || grep -q "^$1:" ${authfile}; then
62 if [ -d "${people}/${1}" ]; then
63 echo -n "Deleting account: $(colorize 34 "$1")"
64 rm -rf "${people}/${1}" && status
65 fi
66 if grep -q "^$user:" ${authfile}; then
67 echo -n "Removing '$1' from authfile..."
68 sed -i "/^${1}:/"d ${authfile} && status
69 fi
70 else
71 no_account
72 fi
73 }
75 #
76 # Commands
77 #
79 case "$1" in
80 "") usage ;;
82 stats)
83 newline
84 boldify "SliTaz users stats"
85 separator
86 cat << EOT
87 People DB : $people
88 Authfie path : $authfile
89 Admin users : $admin
90 User accounts : $(ls $people | wc -l)
91 Authfile users : $(cat $authfile | wc -l)
92 Admin users : $(cat $admin | wc -l)
93 EOT
94 separator && newline ;;
96 last)
97 find ${people} -name "last" | xargs ls -1t | head -n 10 | while read last;
98 do
99 dir="$(dirname $last)"
100 echo -n "$(basename $dir)"
101 indent 26 "$(cat $last)"
102 done ;;
104 list)
105 # List all users
106 newline
107 boldify "SliTaz users list"
108 separator
109 for user in $(ls $people)
110 do
111 if ! [ -f "$people/$user/account.conf" ]; then
112 echo -n "$(colorize 31 "$user")"
113 indent 26 "CORRUPTED" && continue
114 fi
115 . $people/$user/account.conf
116 echo -n "$(colorize 34 "$user")"
117 indent 26 "${NAME}"
118 done
119 separator && newline ;;
121 check)
122 # Check accounts and auth file
123 newline
124 boldify "SliTaz accounts integrity"
125 separator
126 echo "$(colorize 33 "Checking users: account.conf")"
127 for user in $(ls $people)
128 do
129 if ! [ -f "$people/$user/account.conf" ]; then
130 echo -n "$(colorize 30 "$user")"
131 indent 26 "Missing account.conf"
132 else # check empty VALUES
133 . "$people/$user/account.conf"
134 if [ -z "$NAME" ]; then
135 echo -n "$(colorize 30 "$user")"
136 indent 26 "Missing NAME"
137 fi
138 if [ -z "$MAIL" ]; then
139 echo -n $(colorize 30 "$user")
140 indent 26 "Missing MAIL"
141 fi
142 # account.conf but not in authfile ?
143 if ! grep -q "^${user}:" ${authfile}; then
144 echo -n $(colorize 31 "$user")
145 indent 26 "Missing in authfile"
146 fi
147 unset NAME MAIL
148 fi
149 done
150 # Check authfile
151 echo "$(colorize 33 "Checking users in authfile...")"
152 IFS=":"
153 cat ${authfile} | while read user passwd;
154 do
155 if ! [ -d "$people/$user" ]; then
156 echo -n $(colorize 30 "$user")
157 indent 26 "Missing in DB"
158 [ "$del" ] && deluser "$user"
159 fi
160 done
161 unset IFS
162 separator
163 echo "To remove a single corrupted account you can use: tazu 'user' --del" && newline ;;
165 *)
166 # Handle general: --options
167 case " $@ " in
168 *\ --admin\ *)
169 # Admin user
170 if fgrep -q ${user} ${admin}; then
171 echo -n "User is already admin: " && colorize 34 "$user"
172 else
173 echo -n "Adding $user to admin users..."
174 echo "$user" >> ${admin} && status
175 fi ;;
177 *\ --edit\ *)
178 # Edit a user account
179 if [ -f "${people}/${user}/account.conf" ]; then
180 nano ${people}/${user}/account.conf
181 else
182 no_account
183 fi ;;
185 *\ --del\ *)
186 deluser "$user" ;;
188 *)
189 # Show user info
190 if [ -d "${people}/${user}" ]; then
191 newline
192 if grep -q "^$user$" ${admin}; then
193 echo "$(colorize 35 "Admin user:") $(colorize 34 "$user")"
194 else
195 echo "$(boldify "User:") $(colorize 34 "$user")"
196 fi
197 separator
198 cat $people/$user/account.conf | grep "="
199 separator
201 newline
202 else
203 no_account
204 fi ;;
205 esac ;;
206 esac
208 exit 0