Salut à toutes et à tous,
Merci pour vos réponses, j'ai un peu fouiné à partir de ce que vous m'avez apporté plus des recherches supplémentaires sur internet pour obtenir non seulement un résultat satisfaisant mais qui test automatiquement un jeu de valeur (bonne et mauvaises) dans différentes situations d'échecs notamment que je souhaite éviter (par exemple, je veux explicitement qu'une entrée ne commence ni ne finisse par un underscore, c'est personnel et totalement arbitraire).
Petite explications avant de continuer, sinon c'est juste en dessous: ce script dont vous n'avez qu'un bout a pour but de créer un utilisateur virtual pour vsftpd selon mes règles (longueurs et correspondances de patern grace aux expressions régulières), avec un mot de passe (la prochaine étape) hashé et copié dans un fichier avec le nom d'utilisateur, la création de la racine de l'utilisateur et l'application des bons droits etc et surtout le respect de ma configuration vsftpd.
Ci-dessous le code qui test automatiquement les entrées possibles, ce qui ne sera pas en 'prod':
echo -e "\e[1;93mNew FTP user account \e[0m\e[93m(range length for username is 4 to 16 characters):\e[0m"
echo -e "\e[93mOnly [\e[32ma\e[93m-\e[32mz\e[93m], [\e[32m0\e[93m-\e[32m9\e[93m] and \e[32munderscore \e[93m(\"\e[32m_\e[93m\") characters are allowed.\e[0m\n"
echo -e "\e[1;93mAllowed scheme for username are:\e[0m"
echo -e "\e[32ma\e[93m-\e[32mz\e[93m, \e[32ma\e[93m-\e[32mz_a\e[93m-\e[32mz\e[93m, \e[32ma\e[93m-\e[32mz_0\e[93m-\e[32m9\e[93m, \e[32ma\e[93m-\e[32mz0\e[93m-\e[32m9\e[93m, \e[32ma\e[93m-\e[32mz0\e[93m-\e[32m9a\e[93m-\e[32mz\e[93m, \e[32m0\e[93m-\e[32m9a\e[93m-\e[32mz\e[93m, \e[32m0\e[93m-\e[32m9_a\e[93m-\e[32mz\e[93m, \e[32m0\e[93m-\e[32m9a\e[93m-\e[32mz0\e[93m-\e[32m9\e[0m\n"
echo -e ">>>\e[93m New FTP user account name:\e[0m"
### TESTING LOOP ###
fake_ftp_username_input=("abcd" "ab_cd" "ab_12" "ab12" "ab12cd" "" "12ab" "12_ab" "12ab34" "ab" "abc_" "_abc" "123_" "_123" "ab12_" "_ab12" "____" "123")
for (( i=0; $i<${#fake_ftp_username_input[@]}; i++));
do
ftp_username=${fake_ftp_username_input[$i]}
# Check if user input length is allowed
ok=0
while [[ $ok = 0 ]]
do
if [[ ${#ftp_username} -lt 4 || ${#ftp_username} -gt 16 ]]; then
echo -e "\e[31mFTP username length for '$ftp_username' should be between 4 and 16 characters. Exiting!\e[0m"
ok=1
else
echo -e "\e[1;32mLength for FTP username '$ftp_username' is OK.\e[0m\n"
ok=1
next_test=1
fi
done
if [[ $next_test = 1 ]]; then
# Check if user input for username is allowed in pattern matching
regex='^[[:alnum:]]+(_?[[:alnum:]]+)*$'
if [[ "$ftp_username" =~ $regex ]]; then
echo -e "\e[32mFTP username '\e[93m$ftp_username\e[32m' is matching the following: $regex\e[0m\n"
echo -e "\n\n\n"
else
echo -e "\e[31mFTP username '\e[93m$ftp_username\e[31m' is not matching the following: $regex\e[0m\n"
echo -e "\e[93mAllowed scheme for username are:\e[0m"
echo -e "\e[32ma\e[93m-\e[32mz\e[93m, \e[32ma\e[93m-\e[32mz_a\e[93m-\e[32mz\e[93m, \e[32ma\e[93m-\e[32mz_0\e[93m-\e[32m9\e[93m, \e[32ma\e[93m-\e[32mz0\e[93m-\e[32m9\e[93m, \e[32ma\e[93m-\e[32mz0\e[93m-\e[32m9a\e[93m-\e[32mz\e[93m, \e[32m0\e[93m-\e[32m9a\e[93m-\e[32mz\e[93m, \e[32m0\e[93m-\e[32m9_a\e[93m-\e[32mz\e[93m, \e[32m0\e[93m-\e[32m9a\e[93m-\e[32mz0\e[93m-\e[32m9\e[0m\n"
echo -e "\n\n\n"
fi
fi
done
### END OF TESTING LOOP ###
Ci-dessous cela donne donc systématiquement (et sans les couleurs, je vous invite à tester c'est safe car ça ne fait que des sorties sur votre terminal, point):
New FTP user account (range length for username is 4 to 16 characters):
Only [a-z], [0-9] and underscore ("_") characters are allowed.
Allowed scheme for username are:
a-z, a-z_a-z, a-z_0-9, a-z0-9, a-z0-9a-z, 0-9a-z, 0-9_a-z, 0-9a-z0-9
>>> New FTP user account name:
Length for FTP username 'abcd' is OK.
FTP username 'abcd' is matching the following: ^[[:alnum:]]+(_?[[:alnum:]]+)*$
Length for FTP username 'ab_cd' is OK.
FTP username 'ab_cd' is matching the following: ^[[:alnum:]]+(_?[[:alnum:]]+)*$
Length for FTP username 'ab_12' is OK.
FTP username 'ab_12' is matching the following: ^[[:alnum:]]+(_?[[:alnum:]]+)*$
Length for FTP username 'ab12' is OK.
FTP username 'ab12' is matching the following: ^[[:alnum:]]+(_?[[:alnum:]]+)*$
Length for FTP username 'ab12cd' is OK.
FTP username 'ab12cd' is matching the following: ^[[:alnum:]]+(_?[[:alnum:]]+)*$
FTP username length for '' should be between 4 and 16 characters. Exiting!
FTP username '' is not matching the following: ^[[:alnum:]]+(_?[[:alnum:]]+)*$
Allowed scheme for username are:
a-z, a-z_a-z, a-z_0-9, a-z0-9, a-z0-9a-z, 0-9a-z, 0-9_a-z, 0-9a-z0-9
Length for FTP username '12ab' is OK.
FTP username '12ab' is matching the following: ^[[:alnum:]]+(_?[[:alnum:]]+)*$
Length for FTP username '12_ab' is OK.
FTP username '12_ab' is matching the following: ^[[:alnum:]]+(_?[[:alnum:]]+)*$
Length for FTP username '12ab34' is OK.
FTP username '12ab34' is matching the following: ^[[:alnum:]]+(_?[[:alnum:]]+)*$
FTP username length for 'ab' should be between 4 and 16 characters. Exiting!
FTP username 'ab' is matching the following: ^[[:alnum:]]+(_?[[:alnum:]]+)*$
Length for FTP username 'abc_' is OK.
FTP username 'abc_' is not matching the following: ^[[:alnum:]]+(_?[[:alnum:]]+)*$
Allowed scheme for username are:
a-z, a-z_a-z, a-z_0-9, a-z0-9, a-z0-9a-z, 0-9a-z, 0-9_a-z, 0-9a-z0-9
Length for FTP username '_abc' is OK.
FTP username '_abc' is not matching the following: ^[[:alnum:]]+(_?[[:alnum:]]+)*$
Allowed scheme for username are:
a-z, a-z_a-z, a-z_0-9, a-z0-9, a-z0-9a-z, 0-9a-z, 0-9_a-z, 0-9a-z0-9
Length for FTP username '123_' is OK.
FTP username '123_' is not matching the following: ^[[:alnum:]]+(_?[[:alnum:]]+)*$
Allowed scheme for username are:
a-z, a-z_a-z, a-z_0-9, a-z0-9, a-z0-9a-z, 0-9a-z, 0-9_a-z, 0-9a-z0-9
Length for FTP username '_123' is OK.
FTP username '_123' is not matching the following: ^[[:alnum:]]+(_?[[:alnum:]]+)*$
Allowed scheme for username are:
a-z, a-z_a-z, a-z_0-9, a-z0-9, a-z0-9a-z, 0-9a-z, 0-9_a-z, 0-9a-z0-9
Length for FTP username 'ab12_' is OK.
FTP username 'ab12_' is not matching the following: ^[[:alnum:]]+(_?[[:alnum:]]+)*$
Allowed scheme for username are:
a-z, a-z_a-z, a-z_0-9, a-z0-9, a-z0-9a-z, 0-9a-z, 0-9_a-z, 0-9a-z0-9
Length for FTP username '_ab12' is OK.
FTP username '_ab12' is not matching the following: ^[[:alnum:]]+(_?[[:alnum:]]+)*$
Allowed scheme for username are:
a-z, a-z_a-z, a-z_0-9, a-z0-9, a-z0-9a-z, 0-9a-z, 0-9_a-z, 0-9a-z0-9
Length for FTP username '____' is OK.
FTP username '____' is not matching the following: ^[[:alnum:]]+(_?[[:alnum:]]+)*$
Allowed scheme for username are:
a-z, a-z_a-z, a-z_0-9, a-z0-9, a-z0-9a-z, 0-9a-z, 0-9_a-z, 0-9a-z0-9
FTP username length for '123' should be between 4 and 16 characters. Exiting!
FTP username '123' is matching the following: ^[[:alnum:]]+(_?[[:alnum:]]+)*$
Maintenant le bout de code qui sera réellement utilisé pour mon script, sans test automatique donc, je vous invite aussi à tester car la seule différence c'est qu'il est à l'utilisateur de taper le nom:
Le code:
echo -e ">>>\e[93m New FTP user account name:\e[0m"
read ftp_username
# Check if user input length is allowed
ok=0
while [[ $ok = 0 ]]
do
if [[ ${#ftp_username} -lt 4 || ${#ftp_username} -gt 16 ]]; then
echo -e "\e[31mFTP username length for '$ftp_username' should be between 4 and 16 characters. Exiting!\e[0m"
ok=1
else
echo -e "\e[1;32mLength for FTP username '$ftp_username' is OK.\e[0m\n"
ok=1
fi
done
# Check if user input for username is allowed in pattern matching
regex='^[[:alnum:]]+(_?[[:alnum:]]+)*$'
if [[ "$ftp_username" =~ $regex ]]; then
echo -e "\e[32mFTP username '\e[93m$ftp_username\e[32m' is matching the following: $regex\e[0m\n"
else
echo -e "\e[31mFTP username '\e[93m$ftp_username\e[31m' is not matching the following: $regex\e[0m\n"
echo -e "\e[93mAllowed scheme for username are:\e[0m"
echo -e "\e[32ma\e[93m-\e[32mz\e[93m, \e[32ma\e[93m-\e[32mz_a\e[93m-\e[32mz\e[93m, \e[32ma\e[93m-\e[32mz_0\e[93m-\e[32m9\e[93m, \e[32ma\e[93m-\e[32mz0\e[93m-\e[32m9\e[93m, \e[32ma\e[93m-\e[32mz0\e[93m-\e[32m9a\e[93m-\e[32mz\e[93m, \e[32m0\e[93m-\e[32m9a\e[93m-\e[32mz\e[93m, \e[32m0\e[93m-\e[32m9_a\e[93m-\e[32mz\e[93m, \e[32m0\e[93m-\e[32m9a\e[93m-\e[32mz0\e[93m-\e[32m9\e[0m\n"
fi
La sortie:
$ bash add_virtual_ftp_user.sh
New FTP user account (range length for username is 4 to 16 characters):
Only [a-z], [0-9] and underscore ("_") characters are allowed.
Allowed scheme for username are:
a-z, a-z_a-z, a-z_0-9, a-z0-9, a-z0-9a-z, 0-9a-z, 0-9_a-z, 0-9a-z0-9
>>> New FTP user account name:
abcd
Length for FTP username 'abcd' is OK.
FTP username 'abcd' is matching the following: ^[[:alnum:]]+(_?[[:alnum:]]+)*$
$ bash add_virtual_ftp_user.sh
New FTP user account (range length for username is 4 to 16 characters):
Only [a-z], [0-9] and underscore ("_") characters are allowed.
Allowed scheme for username are:
a-z, a-z_a-z, a-z_0-9, a-z0-9, a-z0-9a-z, 0-9a-z, 0-9_a-z, 0-9a-z0-9
>>> New FTP user account name:
abc
FTP username length for 'abc' should be between 4 and 16 characters. Exiting!
FTP username 'abc' is matching the following: ^[[:alnum:]]+(_?[[:alnum:]]+)*$
$ bash add_virtual_ftp_user.sh
New FTP user account (range length for username is 4 to 16 characters):
Only [a-z], [0-9] and underscore ("_") characters are allowed.
Allowed scheme for username are:
a-z, a-z_a-z, a-z_0-9, a-z0-9, a-z0-9a-z, 0-9a-z, 0-9_a-z, 0-9a-z0-9
>>> New FTP user account name:
abc_
Length for FTP username 'abc_' is OK.
FTP username 'abc_' is not matching the following: ^[[:alnum:]]+(_?[[:alnum:]]+)*$
Allowed scheme for username are:
a-z, a-z_a-z, a-z_0-9, a-z0-9, a-z0-9a-z, 0-9a-z, 0-9_a-z, 0-9a-z0-9
$ bash add_virtual_ftp_user.sh
New FTP user account (range length for username is 4 to 16 characters):
Only [a-z], [0-9] and underscore ("_") characters are allowed.
Allowed scheme for username are:
a-z, a-z_a-z, a-z_0-9, a-z0-9, a-z0-9a-z, 0-9a-z, 0-9_a-z, 0-9a-z0-9
>>> New FTP user account name:
12ab_4
Length for FTP username '12ab_4' is OK.
FTP username '12ab_4' is matching the following: ^[[:alnum:]]+(_?[[:alnum:]]+)*$
$ bash add_virtual_ftp_user.sh
New FTP user account (range length for username is 4 to 16 characters):
Only [a-z], [0-9] and underscore ("_") characters are allowed.
Allowed scheme for username are:
a-z, a-z_a-z, a-z_0-9, a-z0-9, a-z0-9a-z, 0-9a-z, 0-9_a-z, 0-9a-z0-9
>>> New FTP user account name:
abccccccccccccccccccccccccccccccccccccccc
FTP username length for 'abccccccccccccccccccccccccccccccccccccccc' should be between 4 and 16 characters. Exiting!
FTP username 'abccccccccccccccccccccccccccccccccccccccc' is matching the following: ^[[:alnum:]]+(_?[[:alnum:]]+)*$
$ bash add_virtual_ftp_user.sh
New FTP user account (range length for username is 4 to 16 characters):
Only [a-z], [0-9] and underscore ("_") characters are allowed.
Allowed scheme for username are:
a-z, a-z_a-z, a-z_0-9, a-z0-9, a-z0-9a-z, 0-9a-z, 0-9_a-z, 0-9a-z0-9
>>> New FTP user account name:
ab
FTP username length for 'ab' should be between 4 and 16 characters. Exiting!
FTP username 'ab' is matching the following: ^[[:alnum:]]+(_?[[:alnum:]]+)*$
Je sais que ce code est basique et qu'il est largement améliorable, mais en l'état ce n'est pas le but. J'ai déjà 3 scripts pour:
- Ajouter un utilisateur
- Supprimer un utilisateur
- Modifier le mot de passe d'un utilisateur
Mais étant le seul à m'en servir, me faisant un peu confiance à moi-même, je suivait mes propres règles mais désormais j'aimerai juste améliorer ce système en forçant certains paramètres. L'amélioration viendra en temps et en heure.
Encore merci à toutes et à tous .
Dernière modification par BarbeRousseLibre (10-11-2018 17:01:50)