#!/bin/bash
#======================================================================
# Copyright (c) 1997 Daniele Giacomini daniele@pluto.linux.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 2 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
# General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#======================================================================
# ftpguestadd
#
# Permette di aggiungere un nuovo utente che potr accedere
# esclusivamente attraverso FTP e verr qualificato come ``guest''.
#======================================================================

#======================================================================
# Variabili.
#======================================================================

    #------------------------------------------------------------------
    # Il punto di partenza delle directory personali.
    #------------------------------------------------------------------
    DIRECTORY_PERSONALI=/home
    #------------------------------------------------------------------
    # Il punto di partenza dell'FTP anonimo.
    #------------------------------------------------------------------
    FTP_ANONIMO=/home/ftp

#======================================================================
# Funzioni.
#======================================================================

    #------------------------------------------------------------------
    # Visualizza la sintassi corretta per l'utilizzo di questo script.
    #------------------------------------------------------------------
    function sintassi () {
	echo ""
	echo "ftpguestadd <nome-utente>"
	echo ""
	echo "Il nome pu avere al massimo 8 caratteri."
    }
    #------------------------------------------------------------------
    # Spiega cosa fare in caso di errore.
    #------------------------------------------------------------------
    function errore () {
	echo "Qualcosa  andato storto."
	echo "Probabilmente  il caso di cancellare la directory \
$DIRECTORY_PERSONALI/$1 e tutto il suo contenuto, oltre a eliminare \
l'utente $1 sia dal file /etc/passwd che da /etc/group."
	echo "Se si utilizzano le password shadow  bene utilizzare \
gli strumenti che appositi per fare questo."
    }

#======================================================================
# Inizio.
#======================================================================

    #------------------------------------------------------------------
    # Verifica la quantit di argomenti.
    #------------------------------------------------------------------
    if [ $# != 1 ]
    then
	sintassi
        exit 1
    fi
    #------------------------------------------------------------------
    # Verifica che l'utente sia root.
    #------------------------------------------------------------------
    if [ $UID != 0 ]
    then
        echo \
"Questo script pu essere utilizzato solo dall'utente root."
        exit 1
    fi
    #------------------------------------------------------------------
    # Crea l'utente in modo normale.
    #------------------------------------------------------------------
    if adduser $1 > /dev/null
    then
	echo "1 adduser $1"
    else
	echo "! adduser non ha funzionato; forse l'utente $1 esiste \
gi?"
	exit 1
    fi
    #------------------------------------------------------------------
    # Gli cambia la shell.
    #------------------------------------------------------------------
    if chsh -s "/bin/false" $1 > /dev/null
    then
	echo "2 chsh -s /bin/false $1"
    else
	echo "! chsh non ha funzionato"
	errore
	exit 1
    fi
    #------------------------------------------------------------------
    # Cancella la directory personale dell'utente appena creato.
    #------------------------------------------------------------------
    if rm -r $DIRECTORY_PERSONALI/$1 > /dev/null
    then
	echo "3 rm -r --force $DIRECTORY_PERSONALI/$1"
    else
	echo "! la cancellazione della directory \
$DIRECTORY_PERSONALI/$1 non ha funzionato"
	errore
	exit 1
    fi
    #------------------------------------------------------------------
    # Ricrea la directory personale, che adesso apparterr a root.
    #------------------------------------------------------------------
    if mkdir $DIRECTORY_PERSONALI/$1 > /dev/null
    then
	echo "4 mkdir $DIRECTORY_PERSONALI/$1"
    else
	echo "! la creazione della directory \
$DIRECTORY_PERSONALI/$1 non ha funzionato"
	errore
	exit 1
    fi
    #------------------------------------------------------------------
    # Riproduce le directory dell'FTP anonimo.
    #------------------------------------------------------------------
    if cp -dpRl $FTP_ANONIMO/bin $DIRECTORY_PERSONALI/$1 > /dev/null
    then
	echo "5 cp -dpRl $FTP_ANONIMO/bin $DIRECTORY_PERSONALI/$1"
    else
	echo "! la copia della directory $FTP_ANONIMO/bin \
non ha funzionato"
	errore
	exit 1
    fi
    if cp -dpRl $FTP_ANONIMO/etc $DIRECTORY_PERSONALI/$1 > /dev/null
    then
	echo "6 cp -dpRl $FTP_ANONIMO/etc $DIRECTORY_PERSONALI/$1"
    else
	echo "! la copia della directory $FTP_ANONIMO/etc \
non ha funzionato"
	errore
	exit 1
    fi
    if cp -dpRl $FTP_ANONIMO/lib $DIRECTORY_PERSONALI/$1 > /dev/null
    then
	echo "7 cp -dpRl $FTP_ANONIMO/lib $DIRECTORY_PERSONALI/$1"
    else
	echo "! la copia della directory $FTP_ANONIMO/lib \
non ha funzionato"
	errore
	exit 1
    fi
    if cp -l $FTP_ANONIMO/welcome.msg $DIRECTORY_PERSONALI/$1 > /dev/null
    then
	echo "8 cp -l $FTP_ANONIMO/welcome.msg $DIRECTORY_PERSONALI/$1"
    else
	echo "! la copia del file $FTP_ANONIMO/welcome.msg \
non ha funzionato"
	errore
	exit 1
    fi
    #------------------------------------------------------------------
    # Sistema altre cose nella directory personale dell'utente.
    #------------------------------------------------------------------
    if cd $DIRECTORY_PERSONALI/$1 > /dev/null
    then
	echo "9 cd $DIRECTORY_PERSONALI/$1"
    else
	echo "! \"cd $DIRECTORY_PERSONALI/$1\" non ha funzionato"
	errore
	exit 1
    fi
    if mkdir public_html > /dev/null
    then
	echo "10 mkdir public_html"
    else
	echo "! \"mkdir public_html\" non ha funzionato"
	errore
	exit 1
    fi
    if chown $1. public_html > /dev/null
    then
	echo "11 chown $1. public_html"
    else
	echo "! \"chown $1. public_html\" non ha funzionato"
	errore
	exit 1
    fi
    if ln -s public_html pub > /dev/null
    then
	echo "12 ln -s public_html pub"
    else
	echo "! \"ln -s public_html pub\" non ha funzionato"
	errore
	exit 1
    fi
    if ln -s public_html html > /dev/null
    then
	echo "12 ln -s public_html html"
    else
	echo "! \"ln -s public_html html\" non ha funzionato"
	errore
	exit 1
    fi
    #------------------------------------------------------------------
    # Permette di inserire la password per l'utente.
    #------------------------------------------------------------------
    passwd $1

    #------------------------------------------------------------------
    # Promemoria.
    #------------------------------------------------------------------
    echo "L'aggiunta dell'utente per l'accesso esclusivo con FTP  \
stata completata."
    echo "E' importante ricordare di aggiungere tale utente \
al gruppo degli utenti FTP guest, altrimenti quando $1 acceder al \
sistema con il suo client FTP, potr percorre l'intero filesystem."
    echo "Se l'inserimento della password  fallito, si pu usare \
il programma passwd in modo autonomo."

#======================================================================
# Fine.
#======================================================================
