#!/bin/bash

# Scripts to search unlogged user
# Copyright (C) 2026 FUSS Project <info@fuss.bz.it>
# Authors: Simone Piccardi <piccardi@truelite.it>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program or from the site that you downloaded it
# from; if not, write to the Free Software Foundation, Inc., 59 Temple
# Place, Suite 330, Boston, MA  02111-1307   USA
#

#
# exclude following users from search
# give a space separated list
#
EXCLUDED_USERS="admin nobody"


#
# searching for something in ~/.client-logins.log, it should be empty
# when an user is unlogged. Rhere are false positives if client script
# 
for i in $(getent.ldap passwd|tr -d " "); do
    USR=$(echo $i|cut -d: -f1)
    if ! echo $EXCLUDED_USERS | egrep "\b$USR\b" > /dev/null; then
	HOM=$(echo $i|cut -d: -f6)
	if [ -s $HOM/.client-logins.log ]; then
	    echo User: $USR logged, $(cat $HOM/.client-logins.log)
	fi
    fi
done

#
# searching for logins in the latest freerarius log, to see if somebody has
# logged on wifi today; we can't see whether they are still logged in, however
#
for i in $(getent.ldap passwd|tr -d " "); do
    USR=$(echo $i|cut -d: -f1)
    if ! echo $EXCLUDED_USERS | egrep "\b$USR\b" > /dev/null; then
        LATEST=$(grep -h $USR /var/log/freeradius/radius.log | grep "Login OK" | head -n 1 | cut -c 1-24)
        if [ "x$LATEST" != "x" ] ; then
            echo "User: $USR logged in to WiFi on $LATEST"
        fi
    fi
done
