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

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