#!/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 all'inizio di gennaio, consente di prolungare la scadenza della password almeno fino al 15 di GENNAIO
# 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 gennaio.

EXCLUDED_USERS="admin nobody"

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

# 15 gennaio dell'anno corrente in secondi
YEAR=$(date +%Y)
JAN15=$(date -d "15 JANUARY $year" +%s)

# Verifica se il 15 gennaio è nel futuro
if (( JAN15 > 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=$((JAN15 / 86400 - $SHALASTCH))
	        # Verifica se la password scadrebbe prima del 15 gennaio ed in caso sposta la scadenza     
	        if [ $DIFF -gt 90 ]; then
	            echo $i $DIFF
	            SHALASTCH_NEW=$((JAN15 / 86400 - 90))
	            echo $SHALASTCH_NEW
	            /usr/share/fuss-server/scripts/chage.py -d $SHALASTCH_NEW $i
	        fi
            fi
        fi
    done
fi



