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

tazu: small cosmetic fixes
author Christophe Lincoln <pankso@slitaz.org>
date Mon Feb 20 15:52:09 2017 +0100 (2017-02-20)
parents edb58659d191
children 12bce7d6f274
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 accounts integrity
43 Options:
44 --admin Make user admin
45 --edit Edit user account.conf
46 --del Delete a user account (or all corrupted)
48 Examples:
49 tazu username --admin
50 tazu "user name" --del
52 EOT
53 }
55 no_account() {
56 echo "No user account for: $user"
57 }
59 # Delete a user (we may have corrupted accounts: check twice)
60 # Usage: deluser "username"
61 deluser() {
62 if [ -d "${people}/${1}" ] || grep -q "^$1:" ${authfile}; then
63 if [ -d "${people}/${1}" ]; then
64 echo -n "Deleting account: $(colorize 34 "$1")"
65 rm -rf "${people}/${1}" && status
66 fi
67 if grep -q "^$user:" ${authfile}; then
68 echo -n "Removing '$1' from authfile..."
69 sed -i "/^${1}:/"d ${authfile} && status
70 fi
71 else
72 no_account
73 fi
74 }
76 #
77 # Commands
78 #
80 case "$1" in
81 "") usage ;;
83 stats)
84 newline
85 boldify "SliTaz users stats"
86 separator
87 cat << EOT
88 People DB : $people
89 Authfie path : $authfile
90 Admin users : $admin
91 User accounts : $(ls $people | wc -l)
92 Authfile users : $(cat $authfile | wc -l)
93 Admin users : $(cat $admin | wc -l)
94 EOT
95 separator && newline ;;
97 last)
98 [ ! "$count" ] && count=15
99 newline
100 boldify "Last active users"
101 separator
102 find ${people} -name "last" | xargs ls -1t | head -n ${count} | while read last;
103 do
104 dir="$(dirname $last)"
105 echo -n "$(basename $dir)"
106 indent 26 "$(cat $last)"
107 done
108 separator && newline ;;
110 list)
111 # List all users
112 newline
113 boldify "SliTaz users list"
114 separator
115 for user in $(ls $people)
116 do
117 if ! [ -f "$people/$user/account.conf" ]; then
118 echo -n "$(colorize 31 "$user")"
119 indent 26 "CORRUPTED" && continue
120 fi
121 . $people/$user/account.conf
122 echo -n "$(colorize 34 "$user")"
123 indent 26 "${NAME}"
124 done
125 separator && newline ;;
127 check)
128 # Check accounts and auth file
129 newline
130 boldify "SliTaz accounts integrity"
131 separator
132 echo "$(colorize 33 "Checking users: account.conf")"
133 for user in $(ls $people)
134 do
135 if ! [ -f "$people/$user/account.conf" ]; then
136 echo -n "$(colorize 30 "$user")"
137 indent 26 "Missing account.conf"
138 else # check empty VALUES
139 . "$people/$user/account.conf"
140 if [ -z "$NAME" ]; then
141 echo -n "$(colorize 30 "$user")"
142 indent 26 "Missing NAME"
143 fi
144 if [ -z "$MAIL" ]; then
145 echo -n $(colorize 30 "$user")
146 indent 26 "Missing MAIL"
147 fi
148 # account.conf but not in authfile ?
149 if ! grep -q "^${user}:" ${authfile}; then
150 echo -n $(colorize 31 "$user")
151 indent 26 "Missing in authfile"
152 fi
153 unset NAME MAIL
154 fi
155 done
156 # Check authfile
157 echo "$(colorize 33 "Checking users in authfile...")"
158 IFS=":"
159 cat ${authfile} | while read user passwd;
160 do
161 if ! [ -d "$people/$user" ]; then
162 echo -n $(colorize 30 "$user")
163 indent 26 "Missing in DB"
164 [ "$del" ] && deluser "$user"
165 fi
166 done
167 unset IFS
168 separator
169 echo "To remove a single corrupted account you can use: tazu 'user' --del" && newline ;;
171 *)
172 # Handle general: --options
173 case " $@ " in
174 *\ --admin\ *)
175 # Admin user
176 if fgrep -q ${user} ${admin}; then
177 echo -n "User is already admin: " && colorize 34 "$user"
178 else
179 echo -n "Adding $user to admin users..."
180 echo "$user" >> ${admin} && status
181 fi ;;
183 *\ --edit\ *)
184 # Edit a user account
185 if [ -f "${people}/${user}/account.conf" ]; then
186 nano ${people}/${user}/account.conf
187 else
188 no_account
189 fi ;;
191 *\ --del\ *)
192 deluser "$user" ;;
194 *)
195 # Show user info
196 if [ -d "${people}/${user}" ]; then
197 newline
198 if grep -q "^$user$" ${admin}; then
199 echo "$(colorize 35 "Admin user:") $(colorize 34 "$user")"
200 else
201 echo "$(boldify "User:") $(colorize 34 "$user")"
202 fi
203 separator
204 cat $people/$user/account.conf | grep "="
205 separator
207 newline
208 else
209 no_account
210 fi ;;
211 esac ;;
212 esac
214 exit 0