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

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