Subversion Repositories ALCASAR

Rev

Rev 2564 | 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 2606 2018-08-20 00:11:49Z tom.houdayer $
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
	while [ $equal -eq 0 ]; do
37
		echo -n "New password: "
38
		read -s pass_1
39
		echo
40
		echo -n "Confirm the new password: "
41
		read -s pass_2
42
		echo
43
 
2606 tom.houday 44
		if [ -n "$pass_1" ] && [ "$pass_1" != "$pass_2" ]; then
2488 lucas.echa 45
			echo -e "\nThe passwords don't match.\n"
46
		else
47
			equal=1
48
		fi
49
	done
50
 
51
	digest="${username}:${REALM}:"
52
	digest+=$(echo -n "${username}:${REALM}:${pass_1}" | md5sum | cut -d" " -f1)
53
 
54
	if [ $existing_user -eq 0 ]; then
55
		echo "$digest" >> "$passwdfile"
56
	else 
57
		sed -i "s/${username}:${REALM}:.*/${digest}/" "$passwdfile"
58
	fi
59
}
60
 
1 root 61
# liste les comptes de chaque profile
62
function list () {
63
	for i in $ALL_PROFILS
64
	do
1369 richard 65
		if [ $Lang == "fr" ]
2454 tom.houday 66
			then
1369 richard 67
			echo -n "Comptes liés au profil '$i' : "
1353 richard 68
 
1369 richard 69
		else
2454 tom.houday 70
			echo -n "accounts linked with profile '$i' : "
1369 richard 71
		fi
72
		account_list=`cat $DIR_KEY/key_only_$i | cut -d':' -f1|sort`
73
		for account in $account_list
74
		do
2454 tom.houday 75
			echo -n "$account "
1369 richard 76
		done
77
	echo
1 root 78
	done
79
}
27 franck 80
# ajoute les comptes du profil "admin" aux autres profils
316 richard 81
# crée le fichier de clés contenant tous les compte (pour l'accès au centre de gestion)
1 root 82
function concat () {
509 richard 83
	> $DIR_KEY/key_all
1 root 84
	for i in $PROFILS
85
	do
86
		cp -f $DIR_KEY/key_only_$ADM_PROFIL $DIR_KEY/key_$i
87
		cat $DIR_KEY/key_only_$i >> $DIR_KEY/key_$i
316 richard 88
		cat $DIR_KEY/key_only_$i >> $DIR_KEY/key_all
1 root 89
	done
90
	cp -f $DIR_KEY/key_only_$ADM_PROFIL $DIR_KEY/key_$ADM_PROFIL
316 richard 91
	cat $DIR_KEY/key_only_$ADM_PROFIL >> $DIR_KEY/key_all
1 root 92
	chown -R root:apache $DIR_KEY
93
	chmod 640 $DIR_KEY/key_*
94
}
95
 
2488 lucas.echa 96
usage="Usage: alcasar-profil.sh [-l|--list] [-a|--add [profil]] [-d|--del] [-p|--pass]"
1 root 97
nb_args=$#
2488 lucas.echa 98
arg1=$1
99
arg2=$2
1 root 100
 
101
# on met en place la structure minimale
102
if [ ! -e $DIR_KEY/key_$ADM_PROFIL ]
103
then
104
	touch $DIR_KEY/key_$ADM_PROFIL
105
fi
106
cp -f $DIR_KEY/key_$ADM_PROFIL $DIR_KEY/key_only_$ADM_PROFIL
107
for i in $PROFILS
108
do
109
	if [ ! -e $DIR_KEY/key_only_$i ]
316 richard 110
	then
1 root 111
		touch $DIR_KEY/key_only_$i
112
	fi
113
done
114
concat
115
if [ $nb_args -eq 0 ]
116
then
117
	echo $usage
118
	exit 0
119
fi
2488 lucas.echa 120
case $arg1 in
1 root 121
	-\? | -h* | --h*)
122
		echo "$usage"
123
		exit 0
124
		;;
2454 tom.houday 125
	--add|-a)
2488 lucas.echa 126
		if [ $nb_args -eq 1 ]
2454 tom.houday 127
		then
2488 lucas.echa 128
			# ajout d'un compte
129
			list
130
			if [ $Lang == "fr" ]
131
			then
132
				echo -n "Choisissez un profil ($ALL_PROFILS) : "
133
			else
134
				echo -n "Select a profile ($ALL_PROFILS) : "
135
			fi
2606 tom.houday 136
			profil=''
137
			while [ -z "$profil" ]; do
138
				read profil
139
			done
1353 richard 140
		else
2488 lucas.echa 141
			profil="$2"
1353 richard 142
		fi
2564 lucas.echa 143
 
144
		if ! echo $ALL_PROFILS | grep -qw $profil
145
		then
146
			if [ $Lang == "fr" ]
147
			then
148
				echo "Le profil indiqué n'existe pas"
149
			else
150
				echo "The given profile doesn't exist"
151
			fi
152
			exit 1
153
		fi
154
 
1353 richard 155
		if [ $Lang == "fr" ]
156
		then
157
			echo -n "Entrez le nom du compte à créer (profil '$profil') : "
158
		else
2454 tom.houday 159
			echo "Enter the name of the account to create (profile '$profil') : "
1353 richard 160
		fi
2606 tom.houday 161
		account=''
162
		while [ -z "$account" ]; do
163
			read account
164
		done
165
		# TODO : add check
1 root 166
		# on teste s'il n'existe pas déjà
167
		for i in $ALL_PROFILS
168
		do
169
			tmp_account=`cat $DIR_KEY/key_only_$i | cut -d':' -f1`
170
			for j in $tmp_account
171
				do
172
				if [ "$j" = "$account" ]
1353 richard 173
					then if [ $Lang == "fr" ]
174
						then
175
							echo "Ce compte existe déjà"
176
						else
177
							echo "This account already exists"
178
						fi
2564 lucas.echa 179
					exit 1
1 root 180
				fi
181
				done
182
		done
2488 lucas.echa 183
		htdigest $DIR_KEY/key_only_$profil "$account"
1 root 184
		concat
185
		list
186
		;;
1769 richard 187
	--del|-d)
1 root 188
		# suppression d'un compte
189
		list
1353 richard 190
		if [ $Lang == "fr" ]
191
		then
192
			echo -n "entrez le nom du compte à supprimer : "
193
		else
194
			echo -n "enter the name of the account to remove : "
195
		fi
2606 tom.houday 196
		account=''
197
		while [ -z "$account" ]; do
198
			read account
199
		done
200
		for i in $ALL_PROFILS; do
1 root 201
			$SED "/^$account:/d" $DIR_KEY/key_only_$i
2606 tom.houday 202
		done
1 root 203
		concat
204
		list
205
		;;
1769 richard 206
	--pass|-p)
1 root 207
		# changement du mot de passe d'un compte
208
		list
1353 richard 209
		if [ $Lang == "fr" ]
210
		then
211
			echo "Changement de mot de passe"
212
			echo -n "Entrez le nom du compte : "
213
		else
214
			echo "Password change"
215
			echo -n "Enter the name of the account : "
216
		fi
1 root 217
		read account
218
		for i in $ALL_PROFILS
219
		do
220
			tmp_account=`cat $DIR_KEY/key_only_$i | cut -d':' -f1`
221
			for j in $tmp_account
222
				do
223
				if [ "$j" = "$account" ]
2488 lucas.echa 224
				then
225
					htdigest $DIR_KEY/key_only_$i "$account"
1 root 226
				fi
227
				done
228
		done
229
		concat
230
		;;
1769 richard 231
	--list|-l)
2488 lucas.echa 232
		# liste des comptes par profil
1 root 233
		list
234
		;;
235
	*)
1353 richard 236
		if [ $Lang == "fr" ]
237
		then
238
			echo "Argument inconnu :$1";
239
		else
240
			echo "Unknown argument : $i";
241
		fi
1 root 242
		echo "$usage"
243
		exit 1
244
		;;
245
esac