Subversion Repositories ALCASAR

Rev

Rev 2454 | Rev 2564 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log

Rev Author Line No. Line
2223 tom.houday 1
#!/bin/bash
63 franck 2
# $Id: alcasar-profil.sh 2488 2018-02-25 14:53:54Z lucas.echard $
3
 
672 richard 4
# alcasar-profil.sh
5
# by Richard REY
6
# This script is distributed under the Gnu General Public License (GPL)
7
 
8
# Gestion des comptes liés aux profiles
9
# Manage the profil logins
10
 
2488 lucas.echa 11
DIR_BIN="/usr/local/bin" # scripts directory
1 root 12
ADM_PROFIL="admin"
13
PROFILS="backup manager"
14
ALL_PROFILS=`echo $ADM_PROFIL $PROFILS`
434 richard 15
DIR_KEY="/usr/local/etc/digest"
1 root 16
SED="/bin/sed -i"
1353 richard 17
Lang=`echo $LANG|cut -c 1-2`
2488 lucas.echa 18
REALM="ALCASAR Control Center (ACC)"
518 stephane 19
 
2488 lucas.echa 20
# génère le htdigest
21
function htdigest () {
22
	passwdfile="$1"
23
	username="$2"
24
 
25
	[ -f "$passwdfile" ] || touch "$passwdfile"
26
 
27
	[ $(grep -c "${username}:${REALM}:" "$passwdfile") ] && existing_user=0 || existing_user=1
28
 
29
	if [ $existing_user -eq 1 ]; then
30
		echo "Changing password for user $username in realm $REALM"
31
	else
32
		echo "Adding user $username in realm $REALM"
33
	fi
34
 
35
	equal=0
36
 
37
	while [ $equal -eq 0 ]; do
38
		echo -n "New password: "
39
		read -s pass_1
40
		echo
41
		echo -n "Confirm the new password: "
42
		read -s pass_2
43
		echo
44
 
45
		if [ "$pass_1" != "$pass_2" ]; then
46
			echo -e "\nThe passwords don't match.\n"
47
		else
48
			equal=1
49
		fi
50
	done
51
 
52
	digest="${username}:${REALM}:"
53
	digest+=$(echo -n "${username}:${REALM}:${pass_1}" | md5sum | cut -d" " -f1)
54
 
55
	if [ $existing_user -eq 0 ]; then
56
		echo "$digest" >> "$passwdfile"
57
	else 
58
		sed -i "s/${username}:${REALM}:.*/${digest}/" "$passwdfile"
59
	fi
60
}
61
 
1 root 62
# liste les comptes de chaque profile
63
function list () {
64
	for i in $ALL_PROFILS
65
	do
1369 richard 66
		if [ $Lang == "fr" ]
2454 tom.houday 67
			then
1369 richard 68
			echo -n "Comptes liés au profil '$i' : "
1353 richard 69
 
1369 richard 70
		else
2454 tom.houday 71
			echo -n "accounts linked with profile '$i' : "
1369 richard 72
		fi
73
		account_list=`cat $DIR_KEY/key_only_$i | cut -d':' -f1|sort`
74
		for account in $account_list
75
		do
2454 tom.houday 76
			echo -n "$account "
1369 richard 77
		done
78
	echo
1 root 79
	done
80
}
27 franck 81
# ajoute les comptes du profil "admin" aux autres profils
316 richard 82
# crée le fichier de clés contenant tous les compte (pour l'accès au centre de gestion)
1 root 83
function concat () {
509 richard 84
	> $DIR_KEY/key_all
1 root 85
	for i in $PROFILS
86
	do
87
		cp -f $DIR_KEY/key_only_$ADM_PROFIL $DIR_KEY/key_$i
88
		cat $DIR_KEY/key_only_$i >> $DIR_KEY/key_$i
316 richard 89
		cat $DIR_KEY/key_only_$i >> $DIR_KEY/key_all
1 root 90
	done
91
	cp -f $DIR_KEY/key_only_$ADM_PROFIL $DIR_KEY/key_$ADM_PROFIL
316 richard 92
	cat $DIR_KEY/key_only_$ADM_PROFIL >> $DIR_KEY/key_all
1 root 93
	chown -R root:apache $DIR_KEY
94
	chmod 640 $DIR_KEY/key_*
95
}
96
 
2488 lucas.echa 97
usage="Usage: alcasar-profil.sh [-l|--list] [-a|--add [profil]] [-d|--del] [-p|--pass]"
1 root 98
nb_args=$#
2488 lucas.echa 99
arg1=$1
100
arg2=$2
1 root 101
 
102
# on met en place la structure minimale
103
if [ ! -e $DIR_KEY/key_$ADM_PROFIL ]
104
then
105
	touch $DIR_KEY/key_$ADM_PROFIL
106
fi
107
cp -f $DIR_KEY/key_$ADM_PROFIL $DIR_KEY/key_only_$ADM_PROFIL
108
for i in $PROFILS
109
do
110
	if [ ! -e $DIR_KEY/key_only_$i ]
316 richard 111
	then
1 root 112
		touch $DIR_KEY/key_only_$i
113
	fi
114
done
115
concat
116
if [ $nb_args -eq 0 ]
117
then
118
	echo $usage
119
	exit 0
120
fi
2488 lucas.echa 121
case $arg1 in
1 root 122
	-\? | -h* | --h*)
123
		echo "$usage"
124
		exit 0
125
		;;
2454 tom.houday 126
	--add|-a)
2488 lucas.echa 127
		if [ $nb_args -eq 1 ]
2454 tom.houday 128
		then
2488 lucas.echa 129
			# ajout d'un compte
130
			list
131
			if [ $Lang == "fr" ]
132
			then
133
				echo -n "Choisissez un profil ($ALL_PROFILS) : "
134
			else
135
				echo -n "Select a profile ($ALL_PROFILS) : "
136
			fi
137
			read profil
1353 richard 138
		else
2488 lucas.echa 139
			profil="$2"
1353 richard 140
		fi
141
		if [ $Lang == "fr" ]
142
		then
143
			echo -n "Entrez le nom du compte à créer (profil '$profil') : "
144
		else
2454 tom.houday 145
			echo "Enter the name of the account to create (profile '$profil') : "
1353 richard 146
		fi
1 root 147
		read account
148
		# on teste s'il n'existe pas déjà
149
		for i in $ALL_PROFILS
150
		do
151
			tmp_account=`cat $DIR_KEY/key_only_$i | cut -d':' -f1`
152
			for j in $tmp_account
153
				do
154
				if [ "$j" = "$account" ]
1353 richard 155
					then if [ $Lang == "fr" ]
156
						then
157
							echo "Ce compte existe déjà"
158
						else
159
							echo "This account already exists"
160
						fi
1 root 161
					exit 0
162
				fi
163
				done
164
		done
2488 lucas.echa 165
		htdigest $DIR_KEY/key_only_$profil "$account"
1 root 166
		concat
167
		list
168
		;;
1769 richard 169
	--del|-d)
1 root 170
		# suppression d'un compte
171
		list
1353 richard 172
		if [ $Lang == "fr" ]
173
		then
174
			echo -n "entrez le nom du compte à supprimer : "
175
		else
176
			echo -n "enter the name of the account to remove : "
177
		fi
1 root 178
		read account
179
		for i in $ALL_PROFILS
180
			do
181
			$SED "/^$account:/d" $DIR_KEY/key_only_$i
182
			done
183
		concat
184
		list
185
		;;
1769 richard 186
	--pass|-p)
1 root 187
		# changement du mot de passe d'un compte
188
		list
1353 richard 189
		if [ $Lang == "fr" ]
190
		then
191
			echo "Changement de mot de passe"
192
			echo -n "Entrez le nom du compte : "
193
		else
194
			echo "Password change"
195
			echo -n "Enter the name of the account : "
196
		fi
1 root 197
		read account
198
		for i in $ALL_PROFILS
199
		do
200
			tmp_account=`cat $DIR_KEY/key_only_$i | cut -d':' -f1`
201
			for j in $tmp_account
202
				do
203
				if [ "$j" = "$account" ]
2488 lucas.echa 204
				then
205
					htdigest $DIR_KEY/key_only_$i "$account"
1 root 206
				fi
207
				done
208
		done
209
		concat
210
		;;
1769 richard 211
	--list|-l)
2488 lucas.echa 212
		# liste des comptes par profil
1 root 213
		list
214
		;;
215
	*)
1353 richard 216
		if [ $Lang == "fr" ]
217
		then
218
			echo "Argument inconnu :$1";
219
		else
220
			echo "Unknown argument : $i";
221
		fi
1 root 222
		echo "$usage"
223
		exit 1
224
		;;
225
esac