#!/bin/bash
# This file is part of the FUSS Utility project.
# Copyright (C) 2022-2025 The FUSS Project <info@fuss.bz.it>
# Author: Claudio Cavalli <ccavalli@fuss.bz.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, either version 3 of the
# License, or (at your option) any later version.
#
# 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

# Questo script, lanciato possibilmente prima della ripresa delle attività scolastiche,
# consente di prolungare la scadenza della password almeno fino al 15 di settembre
# Viene calcolato per ciascun docente se la password scadrà prima di quella data.
# Se è così, il termine viene prolungato dei giorni sufficienti per non far scadere la password prima del 15 di settembre.

EXCLUDED_USERS="admin nobody"

# Data di oggi in secondi dal 1970
NOW=$(date +%s)

# 15 settembre dell'anno corrente in secondi
YEAR=$(date +%Y)
SEPT15=$(date -d "15 September $year" +%s)

# Verifica se il 15 settembre è nel futuro
if (( SEPT15 > NOW )); then
    for i in $(getent.ldap passwd |tr -d " "|cut -d: -f1); do
        if ! echo $EXCLUDED_USERS | egrep "\b$i\b" > /dev/null; then
            MAXSHADOW=$(smbldap-usershow $i | grep shadowMax |cut -d" " -f2)
            # Verifica se l'utente ha un MAXSHADOW di 90 gg, ovvero è un docente o equivalente
            if [ "$MAXSHADOW" -eq "90" ]; then                	
	        SHALASTCH=$(smbldap-usershow $i | grep shadowLastChange |cut -d" " -f2)
	        DIFF=$((SEPT15 / 86400 - $SHALASTCH))
                # Verifica se la password scadrebbe prima del 15 settembre ed in caso sposta la scadenza
	        if [ $DIFF -gt 90 ]; then
	            echo  $i $DIFF
	            SHALASTCH_NEW=$((SEPT15 / 86400 - 90))
	            /usr/share/fuss-server/scripts/chage.py -d $SHALASTCH_NEW $i
	        fi
            fi
        fi
    done
fi



