Subversion Repositories ALCASAR

Rev

Rev 2488 | 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 2564 2018-07-07 21:03:32Z 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
 
2564 lucas.echa 27
	grep -q "${username}:${REALM}:" "$passwdfile" && existing_user=1 || existing_user=0
2488 lucas.echa 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
2564 lucas.echa 141
 
142
		if ! echo $ALL_PROFILS | grep -qw $profil
143
		then
144
			if [ $Lang == "fr" ]
145
			then
146
				echo "Le profil indiqué n'existe pas"
147
			else
148
				echo "The given profile doesn't exist"
149
			fi
150
			exit 1
151
		fi
152
 
1353 richard 153
		if [ $Lang == "fr" ]
154
		then
155
			echo -n "Entrez le nom du compte à créer (profil '$profil') : "
156
		else
2454 tom.houday 157
			echo "Enter the name of the account to create (profile '$profil') : "
1353 richard 158
		fi
1 root 159
		read account
160
		# on teste s'il n'existe pas déjà
161
		for i in $ALL_PROFILS
162
		do
163
			tmp_account=`cat $DIR_KEY/key_only_$i | cut -d':' -f1`
164
			for j in $tmp_account
165
				do
166
				if [ "$j" = "$account" ]
1353 richard 167
					then if [ $Lang == "fr" ]
168
						then
169
							echo "Ce compte existe déjà"
170
						else
171
							echo "This account already exists"
172
						fi
2564 lucas.echa 173
					exit 1
1 root 174
				fi
175
				done
176
		done
2488 lucas.echa 177
		htdigest $DIR_KEY/key_only_$profil "$account"
1 root 178
		concat
179
		list
180
		;;
1769 richard 181
	--del|-d)
1 root 182
		# suppression d'un compte
183
		list
1353 richard 184
		if [ $Lang == "fr" ]
185
		then
186
			echo -n "entrez le nom du compte à supprimer : "
187
		else
188
			echo -n "enter the name of the account to remove : "
189
		fi
1 root 190
		read account
191
		for i in $ALL_PROFILS
192
			do
193
			$SED "/^$account:/d" $DIR_KEY/key_only_$i
194
			done
195
		concat
196
		list
197
		;;
1769 richard 198
	--pass|-p)
1 root 199
		# changement du mot de passe d'un compte
200
		list
1353 richard 201
		if [ $Lang == "fr" ]
202
		then
203
			echo "Changement de mot de passe"
204
			echo -n "Entrez le nom du compte : "
205
		else
206
			echo "Password change"
207
			echo -n "Enter the name of the account : "
208
		fi
1 root 209
		read account
210
		for i in $ALL_PROFILS
211
		do
212
			tmp_account=`cat $DIR_KEY/key_only_$i | cut -d':' -f1`
213
			for j in $tmp_account
214
				do
215
				if [ "$j" = "$account" ]
2488 lucas.echa 216
				then
217
					htdigest $DIR_KEY/key_only_$i "$account"
1 root 218
				fi
219
				done
220
		done
221
		concat
222
		;;
1769 richard 223
	--list|-l)
2488 lucas.echa 224
		# liste des comptes par profil
1 root 225
		list
226
		;;
227
	*)
1353 richard 228
		if [ $Lang == "fr" ]
229
		then
230
			echo "Argument inconnu :$1";
231
		else
232
			echo "Unknown argument : $i";
233
		fi
1 root 234
		echo "$usage"
235
		exit 1
236
		;;
237
esac