slitaz-dev-tools 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 | 8a6bda7892ad |
children | 03310414b391 |
files | tazu/Makefile tazu/tazu |
line diff
1.1 --- a/tazu/Makefile Sat Jan 21 11:03:10 2017 +0200 1.2 +++ b/tazu/Makefile Mon Feb 20 15:34:43 2017 +0100 1.3 @@ -7,7 +7,7 @@ 1.4 all: 1.5 1.6 install: 1.7 - install -m 0755 tazu $(DESTDIR)$(PREFIX)/bin 1.8 + install -m 0755 tazu $(DESTDIR)$(PREFIX)/sbin 1.9 1.10 uninstall: 1.11 - rm -rf $(DESTDIR)$(PREFIX)/bin/tazu 1.12 + rm -rf $(DESTDIR)$(PREFIX)/sbin/tazu
2.1 --- a/tazu/tazu Sat Jan 21 11:03:10 2017 +0200 2.2 +++ b/tazu/tazu Mon Feb 20 15:34:43 2017 +0100 2.3 @@ -5,15 +5,25 @@ 2.4 # This tool is used to mange SliTaz users accounts on bugs.slitaz.org 2.5 # and scn.slitaz.org. It can also be used to admin TinyCM users DB. 2.6 # 2.7 -# Copyright 2016 (C) SliTaz GNU/Linux - BSD License 2.8 +# Copyright 2017 (C) SliTaz GNU/Linux - BSD License 2.9 # Author: Christophe Lincoln <pankso@slitaz.org> 2.10 # 2.11 . /lib/libtaz.sh 2.12 check_root 2.13 +user="$1" 2.14 2.15 +people="/var/lib/slitaz/people" 2.16 authfile="/var/lib/slitaz/auth/people" 2.17 -people="/var/lib/slitaz/people" 2.18 -user="$1" 2.19 +admin="/var/lib/slitaz/auth/admin" 2.20 + 2.21 +# Sanity check 2.22 +for file in ${authfile} ${admin}; do 2.23 + if ! [ -f "$file" ]; then 2.24 + echo check $file 2.25 + install -d -m 0700 -o www -g www $(dirname $file) 2.26 + touch ${file} && chown www.www ${file} && chmod 0600 ${file} 2.27 + fi 2.28 +done 2.29 2.30 # 2.31 # Functions 2.32 @@ -25,14 +35,18 @@ 2.33 Usage: $(basename $0) [user|command] [--option] 2.34 2.35 Commands: 2.36 - count Count all users 2.37 - list List all users 2.38 - check Check accounts integrity 2.39 + stats Count all users 2.40 + list List all users 2.41 + check Check accounts integrity 2.42 2.43 Options: 2.44 - --admin Make user admin 2.45 - --edit Edit user account.conf 2.46 - --del Delete a user account 2.47 + --admin Make user admin 2.48 + --edit Edit user account.conf 2.49 + --del Delete a user account (or all corrupted) 2.50 + 2.51 +Examples: 2.52 + tazu username --admin 2.53 + tazu "user name" --del 2.54 2.55 EOT 2.56 } 2.57 @@ -41,11 +55,21 @@ 2.58 echo "No user account for: $user" 2.59 } 2.60 2.61 +# Delete a user (we may have corrupted accounts: check twice) 2.62 +# Usage: deluser "username" 2.63 deluser() { 2.64 - if [ -d "${people}/${user}" ]; then 2.65 - rm -rf "${people}/${user}" 2.66 + if [ -d "${people}/${1}" ] || grep -q "^$1:" ${authfile}; then 2.67 + if [ -d "${people}/${1}" ]; then 2.68 + echo -n "Deleting account: $(colorize 34 "$1")" 2.69 + rm -rf "${people}/${1}" && status 2.70 + fi 2.71 + if grep -q "^$user:" ${authfile}; then 2.72 + echo -n "Removing '$1' from authfile..." 2.73 + sed -i "/^${1}:/"d ${authfile} && status 2.74 + fi 2.75 + else 2.76 + no_account 2.77 fi 2.78 - sed -i "/^${user}:/"d $authfile 2.79 } 2.80 2.81 # 2.82 @@ -54,9 +78,29 @@ 2.83 2.84 case "$1" in 2.85 "") usage ;; 2.86 - count) 2.87 - echo -n "Users: " 2.88 - colorize 34 "$(ls $people | wc -l)" ;; 2.89 + 2.90 + stats) 2.91 + newline 2.92 + boldify "SliTaz users stats" 2.93 + separator 2.94 + cat << EOT 2.95 +People DB : $people 2.96 +Authfie path : $authfile 2.97 +Admin users : $admin 2.98 +User accounts : $(ls $people | wc -l) 2.99 +Authfile users : $(cat $authfile | wc -l) 2.100 +Admin users : $(cat $admin | wc -l) 2.101 +EOT 2.102 + separator && newline ;; 2.103 + 2.104 + last) 2.105 + find ${people} -name "last" | xargs ls -1t | head -n 10 | while read last; 2.106 + do 2.107 + dir="$(dirname $last)" 2.108 + echo -n "$(basename $dir)" 2.109 + indent 26 "$(cat $last)" 2.110 + done ;; 2.111 + 2.112 list) 2.113 # List all users 2.114 newline 2.115 @@ -66,62 +110,68 @@ 2.116 do 2.117 if ! [ -f "$people/$user/account.conf" ]; then 2.118 echo -n "$(colorize 31 "$user")" 2.119 - echo -e "\\033[16GCORRUPTED" && continue 2.120 + indent 26 "CORRUPTED" && continue 2.121 fi 2.122 . $people/$user/account.conf 2.123 echo -n "$(colorize 34 "$user")" 2.124 - echo -e "\\033[16G${NAME}" 2.125 + indent 26 "${NAME}" 2.126 done 2.127 separator && newline ;; 2.128 + 2.129 check) 2.130 # Check accounts and auth file 2.131 newline 2.132 boldify "SliTaz accounts integrity" 2.133 separator 2.134 - echo "$(colorize 33 "Checking account.conf files...")" 2.135 + echo "$(colorize 33 "Checking users: account.conf")" 2.136 for user in $(ls $people) 2.137 do 2.138 if ! [ -f "$people/$user/account.conf" ]; then 2.139 echo -n "$(colorize 30 "$user")" 2.140 - echo -e "\\033[16GMissing account.conf" 2.141 + indent 26 "Missing account.conf" 2.142 else # check empty VALUES 2.143 . "$people/$user/account.conf" 2.144 if [ -z "$NAME" ]; then 2.145 echo -n "$(colorize 30 "$user")" 2.146 - echo -e "\\033[16GMissing NAME" 2.147 + indent 26 "Missing NAME" 2.148 fi 2.149 if [ -z "$MAIL" ]; then 2.150 - echo -n "$(colorize 30 "$user")" 2.151 - echo -e "\\033[16GMissing MAIL" 2.152 + echo -n $(colorize 30 "$user") 2.153 + indent 26 "Missing MAIL" 2.154 fi 2.155 - if [ -z "$KEY" ]; then 2.156 - echo -n "$(colorize 30 "$user")" 2.157 - echo -e "\\033[16GMissing KEY" 2.158 + # account.conf but not in authfile ? 2.159 + if ! grep -q "^${user}:" ${authfile}; then 2.160 + echo -n $(colorize 31 "$user") 2.161 + indent 26 "Missing in authfile" 2.162 fi 2.163 - unset NAME MAIL KEY 2.164 + unset NAME MAIL 2.165 fi 2.166 done 2.167 - echo "$(colorize 33 "Checking auth file...")" 2.168 - for user in $(cat $authfile | cut -d : -f 1) 2.169 + # Check authfile 2.170 + echo "$(colorize 33 "Checking users in authfile...")" 2.171 + IFS=":" 2.172 + cat ${authfile} | while read user passwd; 2.173 do 2.174 if ! [ -d "$people/$user" ]; then 2.175 - echo -n "$(colorize 30 "$user")" 2.176 - echo -e "\\033[16GMissing in DB" 2.177 + echo -n $(colorize 30 "$user") 2.178 + indent 26 "Missing in DB" 2.179 + [ "$del" ] && deluser "$user" 2.180 fi 2.181 done 2.182 - separator 2.183 - echo "Use 'tazu user --del' to remove a corrupted account" && newline ;; 2.184 + unset IFS 2.185 + separator 2.186 + echo "To remove a single corrupted account you can use: tazu 'user' --del" && newline ;; 2.187 + 2.188 *) 2.189 # Handle general: --options 2.190 case " $@ " in 2.191 *\ --admin\ *) 2.192 # Admin user 2.193 - if fgrep -q ADMIN_USER= ${people}/${user}/account.conf; then 2.194 + if fgrep -q ${user} ${admin}; then 2.195 echo -n "User is already admin: " && colorize 34 "$user" 2.196 else 2.197 echo -n "Adding $user to admin users..." 2.198 - echo 'ADMIN_USER="yes"' >> ${people}/${user}/account.conf 2.199 - status 2.200 + echo "$user" >> ${admin} && status 2.201 fi ;; 2.202 2.203 *\ --edit\ *) 2.204 @@ -133,22 +183,22 @@ 2.205 fi ;; 2.206 2.207 *\ --del\ *) 2.208 - # Delete a user 2.209 - if [ -d "${people}/${user}" ]; then 2.210 - echo -n "Deleting user: $(colorize 34 "$user")" 2.211 - deluser && status 2.212 - else 2.213 - no_account 2.214 - fi ;; 2.215 + deluser "$user" ;; 2.216 2.217 *) 2.218 # Show user info 2.219 - if [ -d "${people}/${user}" ]; then 2.220 + if [ -d "${people}/${user}" ]; then 2.221 newline 2.222 - echo "$(boldify "User:") $(colorize 34 "$user")" 2.223 + if grep -q "^$user$" ${admin}; then 2.224 + echo "$(colorize 35 "Admin user:") $(colorize 34 "$user")" 2.225 + else 2.226 + echo "$(boldify "User:") $(colorize 34 "$user")" 2.227 + fi 2.228 separator 2.229 cat $people/$user/account.conf | grep "=" 2.230 - separator && newline 2.231 + separator 2.232 + 2.233 + newline 2.234 else 2.235 no_account 2.236 fi ;;