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

Delete all corrupted account at once, search for user and fixes
author Christophe Lincoln <pankso@slitaz.org>
date Mon Feb 20 17:50:15 2017 +0100 (2017-02-20)
parents 03310414b391
children 93e083231066
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="/home/pankso/tazu/db/people"
16 authfile="/home/pankso/tazu/db/auth/people"
17 admin="/home/pankso/tazu/db/auth/admin"
19 #people="/var/lib/slitaz/people"
20 #authfile="/var/lib/slitaz/auth/people"
21 #admin="/var/lib/slitaz/auth/admin"
23 # Sanity check
24 for file in ${authfile} ${admin}; do
25 if ! [ -f "$file" ]; then
26 echo check $file
27 install -d -m 0700 -o www -g www $(dirname $file)
28 touch ${file} && chown www.www ${file} && chmod 0600 ${file}
29 fi
30 done
32 #
33 # Functions
34 #
36 usage() {
37 cat << EOT
39 $(boldify "Usage:") $(basename $0) [user|command] [--option]
41 Commands:
42 stats SliTaz users DB stats
43 list List all users accounts
44 last List last active users
45 check Check for corrupted accounts
47 Options:
48 --admin Make user admin
49 --edit Edit user account.conf
50 --search Search for users using patterns
51 --del Delete a user account (or all corrupted)
53 Examples:
54 tazu username --admin
55 tazu "user name" --search
57 EOT
58 }
60 no_account() {
61 echo "No user account for: $user"
62 }
64 md5crypt() {
65 echo -n "$1" | md5sum | awk '{print $1}'
66 }
68 # Delete a user (we may have corrupted accounts: check twice)
69 # Usage: deluser "username"
70 deluser() {
71 if [ -d "${people}/${1}" ] || grep -q "^$1:" ${authfile}; then
72 if [ -d "${people}/${1}" ]; then
73 echo -n "Deleting account: $(colorize 34 "$1")"
74 rm -rf "${people}/${1}" && status
75 fi
76 if grep -q "^$user:" ${authfile}; then
77 echo -n "Removing '$1' from authfile..."
78 sed -i "/^${1}:/"d ${authfile} && status
79 fi
80 else
81 no_account
82 fi
83 }
85 #
86 # Commands
87 #
89 case "$1" in
90 "") usage ;;
92 stats)
93 newline
94 boldify "SliTaz users stats"
95 separator
96 cat << EOT
97 People DB : $people
98 Authfie path : $authfile
99 Admin users : $admin
100 User accounts : $(ls $people | wc -l)
101 Authfile users : $(cat $authfile | wc -l)
102 Admin users : $(cat $admin | wc -l)
103 EOT
104 separator && newline ;;
106 last)
107 [ ! "$count" ] && count=15
108 newline
109 boldify "Last active users"
110 separator
111 find ${people} -name "last" | xargs ls -1t | head -n ${count} | while read last;
112 do
113 dir="$(dirname $last)"
114 echo -n "$(basename $dir)"
115 indent 26 "$(cat $last)"
116 done
117 separator && newline ;;
119 list)
120 # List all users
121 newline
122 boldify "SliTaz users list"
123 separator
124 for user in $(ls $people)
125 do
126 if ! [ -f "$people/$user/account.conf" ]; then
127 echo -n "$(colorize 31 "$user")"
128 indent 20 "CORRUPTED" && continue
129 fi
130 echo -n "$(colorize 34 "$user")"
131 indent 20 "${NAME}"
132 done
133 separator
134 echo "$(boldify "Users:") $(ls $people | wc -l)"
135 echo -n "$(boldify "Admin users:") "
136 for u in $(cat $admin); do
137 echo -n "$u "
138 done && newline
139 separator && newline ;;
141 check)
142 # Check accounts and auth file
143 tmp=/tmp/tazu_corrupted
144 newline
145 boldify "SliTaz accounts integrity"
146 separator
147 echo "$(colorize 33 "Checking users: account.conf")"
148 for user in $(ls $people)
149 do
150 if ! [ -f "$people/$user/account.conf" ]; then
151 echo -n "$(colorize 31 "$user")"
152 indent 26 "Missing account.conf"
153 else # Check empty VALUES
154 . "$people/$user/account.conf"
155 if [ -z "$NAME" ]; then
156 echo -n "$(colorize 31 "$user")"
157 indent 26 "Missing NAME"
158 fi
159 if [ -z "$MAIL" ]; then
160 echo -n $(colorize 31 "$user")
161 indent 26 "Missing MAIL"
162 fi
163 # Invalide mail
164 if ! echo "$MAIL" | grep -q "@"; then
165 echo -n $(colorize 31 "$user")
166 indent 26 "Invalid MAIL: $MAIL"
167 echo "$user" >> ${tmp}
168 fi
169 # account.conf but not in authfile ?
170 if ! grep -q "^${user}:" ${authfile}; then
171 echo -n $(colorize 31 "$user")
172 indent 26 "Missing in authfile"
173 echo "$user" >> ${tmp}
174 fi
175 unset NAME MAIL
176 fi
177 done
178 # Check authfile
179 echo "$(colorize 33 "Checking users in authfile...")"
180 IFS=":"
181 cat ${authfile} | while read user passwd;
182 do
183 if ! [ -d "$people/$user" ]; then
184 echo -n $(colorize 31 "$user")
185 indent 26 "Missing in DB"
186 echo "$user" >> ${tmp}
187 fi
188 done
189 unset IFS
190 separator
191 # Handle --del option
192 if [ "$del" ] && [ -f "$tmp" ]; then
193 boldify "Deleting accounts..."
194 cat $tmp | uniq | while read u;
195 do
196 deluser "$u"
197 done && separator
198 else
199 echo "To remove a single corrupted account you can use: tazu 'user' --del"
200 fi
201 newline && rm -f ${tmp} ;;
203 *)
204 # Handle general: --options
205 case " $@ " in
206 *\ --admin\ *)
207 # Admin user
208 if fgrep -q ${user} ${admin}; then
209 echo -n "User is already admin: " && colorize 34 "$user"
210 else
211 echo -n "Adding $user to admin users..."
212 echo "$user" >> ${admin} && status
213 fi ;;
215 *\ --edit\ *)
216 # Edit a user account
217 if [ -f "${people}/${user}/account.conf" ]; then
218 nano ${people}/${user}/account.conf
219 else
220 no_account
221 fi ;;
223 *\ --search\ *)
224 # Search for a user
225 newline
226 echo -n "Searching for: "; colorize 34 "$1"
227 separator
228 IFS=":"
229 grep -i "$1" ${people}/*/account.conf | while read path patterm;
230 do
231 . ${path}
232 if ! echo "$found" | grep -w -q "$USER"; then
233 found="$found $USER"
234 echo "$(colorize 34 $USER) $(indent 20 $NAME) $(indent 46 $MAIL)"
235 fi
236 done
237 unset IFS && separator && newline ;;
239 *\ --passwd\ *)
240 echo -n "New password for $1: "; read pass
241 echo "TODO" ;;
243 *\ --del\ *)
244 deluser "$user" ;;
246 *)
247 # Show user info
248 if [ -d "${people}/${user}" ]; then
249 newline
250 if fgrep -w -q "$user" ${admin}; then
251 echo "$(colorize 35 "Admin user:") $(colorize 34 "$user")"
252 else
253 echo "$(boldify "User:") $(colorize 34 "$user")"
254 fi
255 separator
256 cat $people/$user/account.conf | grep "="
257 separator
259 newline
260 else
261 no_account
262 fi ;;
263 esac ;;
264 esac
266 exit 0