Subversion Repositories ALCASAR

Rev

Rev 2631 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log

Rev 2631 Rev 2875
Line 1... Line 1...
1
#!/bin/bash
1
#!/bin/bash
2
# $Id: alcasar-sms.sh 2631 2018-09-12 21:16:42Z rexy $
2
# $Id: alcasar-sms.sh 2875 2020-11-01 10:50:57Z rexy $
3
 
3
 
4
# alcasar-sms.sh
4
# alcasar-sms.sh
5
# by Nicolas Aubry & Rexy
5
# by Nicolas Aubry & Rexy
6
# This script is distributed under the Gnu General Public License (GPL)
6
# This script is distributed under the Gnu General Public License (GPL)
7
 
7
 
8
# This script manages the 'gammu-smsd' service when a GSM adapter is detected
8
# This script manages the 'gammu-smsd' service when a GSM adapter is detected
9
# Ce script gère le service 'gammu-smsd' quand un adaptateur GSM est détecté
9
# Ce script gère le service 'gammu-smsd' quand un adaptateur GSM est détecté
10
 
10
 
11
CONF_FILE='/usr/local/etc/alcasar.conf'
11
CONF_FILE='/usr/local/etc/alcasar.conf'
12
 
12
 
13
#########################################
-
 
14
#######	VARIABLES ########
13
#######	VARIABLES ########
15
nb_essais=3
14
nb_essais=3
16
time_account=1
15
time_account=1
17
time_ban=2
16
time_ban=2
18
#########################################
-
 
19
####### IDs DB ##########################
17
####### IDs DB ##########################
20
PASSWD_FILE="/root/ALCASAR-passwords.txt"
18
PASSWD_FILE="/root/ALCASAR-passwords.txt"
21
u_db=$(grep '^db_user='     $PASSWD_FILE | cut -d'=' -f2-)
19
u_db=$(grep '^db_user='     $PASSWD_FILE | cut -d'=' -f2-)
22
p_db=$(grep '^db_password=' $PASSWD_FILE | cut -d'=' -f2-)
20
p_db=$(grep '^db_password=' $PASSWD_FILE | cut -d'=' -f2-)
23
#########################################
21
#########################################
24
#########################################
-
 
25
 
22
 
26
rad="radcheck"
23
rad="radcheck"
27
radgp="radusergroup"
24
radgp="radusergroup"
28
radgpck="radgroupcheck"
25
radgpck="radgroupcheck"
29
radinf="userinfo"
26
radinf="userinfo"
30
 
-
 
31
sms_p="SMS_ban_perm"
27
sms_p="SMS_ban_perm"
32
sms_t="SMS_ban_temp"
28
sms_t="SMS_ban_temp"
33
 
-
 
34
inb="inbox"
29
inb="inbox"
35
SMS_c="SMS_country"
30
SMS_c="SMS_country"
36
 
-
 
37
config="/etc/gammu_smsd_conf"
31
config="/etc/gammu_smsd_conf"
38
config2="/etc/gammurc"
32
config2="/etc/gammurc"
39
logfile="/var/log/gammu-smsd/gammu-smsd.log"
33
logfile="/var/log/gammu-smsd/gammu-smsd.log"
40
script="/usr/local/bin/alcasar-sms.sh"
34
script="/usr/local/bin/alcasar-sms.sh"
41
separator="########## START ##########"
35
separator="########## START ##########"
42
end="%%%%%%%%%% STOP %%%%%%%%%%"
36
end="%%%%%%%%%% STOP %%%%%%%%%%"
43
 
37
 
44
 
-
 
45
usage="Usage: alcasar-gammu.sh
38
usage="Usage: alcasar-gammu.sh
46
Start Gammu-smsd   : --start
39
Start Gammu-smsd   : --start
47
Stop Gammu-smsd    : --stop
40
Stop Gammu-smsd    : --stop
48
Process on new sms : --new_sms"
41
Process on new sms : --new_sms"
49
 
42
 
50
 
-
 
51
nb_args=$#
43
nb_args=$#
52
args=$1
44
args=$1
53
 
45
 
54
 
-
 
55
# Functions
46
# Functions
56
function mode_huawei() {
47
function mode_huawei() {
57
	couple=$(lsusb | grep -i huawei | cut -d ' ' -f6)
48
	couple=$(lsusb | grep -i huawei | cut -d ' ' -f6)
58
	vendor=$(echo $couple | cut -d ':' -f1)
49
	vendor=$(echo $couple | cut -d ':' -f1)
59
	product=$(echo $couple | cut -d ':' -f2)
50
	product=$(echo $couple | cut -d ':' -f2)
60
 
-
 
61
	echo "******** Modeswitch *************" >> $logfile
51
	echo "******** Modeswitch *************" >> $logfile
62
	echo $vendor >> $logfile
52
	echo $vendor >> $logfile
63
	echo $product >> $logfile
53
	echo $product >> $logfile
64
 
-
 
65
	/usr/sbin/usb_modeswitch -I -H -v 0x$vendor -p 0x$product -V 0x$vendor -P 0x$product -W -n >> $logfile
54
	/usr/sbin/usb_modeswitch -I -H -v 0x$vendor -p 0x$product -V 0x$vendor -P 0x$product -W -n >> $logfile
66
}
-
 
-
 
55
} # end function mode_huawei
67
 
56
 
68
function start_gammu() {
57
function start_gammu() {
69
	# Check phone number is set
-
 
70
	if [ -z "$(grep '^SMS_NUM=' $CONF_FILE | cut -d'=' -f2-)" ]; then
-
 
71
		echo 'The phone number is not set.'
-
 
72
		# exit 2
-
 
73
	fi
-
 
74
 
-
 
75
	#On truncate la table phones (informations signal / IMEI / batterie / sms recu et envoyé)
58
	# Truncate phones table (informations signal/IMEI/battery/sent et reveived sms)
76
	mysql --user=$u_db --password=$p_db --database=gammu -Bs -e 'TRUNCATE phones;'
59
	mysql --user=$u_db --password=$p_db --database=gammu -Bs -e 'TRUNCATE phones;'
77
 
-
 
78
 
-
 
79
	#On verifie que le groupe sms est créé
60
	# Verify the sms group is created
80
	if [ $(mysql --user=$u_db --password=$p_db --database=radius -Bs -e "SELECT COUNT(*) FROM $radgp WHERE username='sms' AND groupname='sms';") -eq 0 ]; then
61
	if [ $(mysql --user=$u_db --password=$p_db --database=radius -Bs -e "SELECT COUNT(*) FROM $radgp WHERE username='sms' AND groupname='sms';") -eq 0 ]; then
81
		sql_add_gp="INSERT INTO $radgp (username,groupname) VALUES ('sms','sms'); INSERT INTO $radgpck (groupname,attribute,op,value) VALUES ('sms','Simultaneous-Use',':=',1);"
62
		sql_add_gp="INSERT INTO $radgp (username,groupname) VALUES ('sms','sms'); INSERT INTO $radgpck (groupname,attribute,op,value) VALUES ('sms','Simultaneous-Use',':=',1);"
82
		mysql --user=$u_db --password=$p_db --database=radius -Bs -e "$sql_add_gp"
63
		mysql --user=$u_db --password=$p_db --database=radius -Bs -e "$sql_add_gp"
83
	fi
64
	fi
84
 
-
 
85
	#Start gammu
65
	# Start gammu
86
	echo $separator >> $logfile
66
	echo $separator >> $logfile
87
	/usr/bin/systemctl -q start gammu-smsd.service
67
	/usr/bin/systemctl -q start gammu-smsd.service
88
	/usr/bin/systemctl -q enable gammu-smsd.service
68
	/usr/bin/systemctl -q enable gammu-smsd.service
89
} # end function start_gammu
69
} # end function start_gammu
90
 
70
 
91
function stop_gammu() {
71
function stop_gammu() {
92
	#Stop gammu
-
 
93
	/usr/bin/systemctl -q stop gammu-smsd.service
72
	/usr/bin/systemctl -q stop gammu-smsd.service
94
	/usr/bin/systemctl -q disable gammu-smsd.service
73
	/usr/bin/systemctl -q disable gammu-smsd.service
95
	sleep 10
-
 
96
	echo $end >> $logfile
74
	echo $end >> $logfile
97
} # end function stop_gammu
75
} # end function stop_gammu
98
 
76
 
99
function unlock() {
77
function unlock() {
100
	#Suppression du numero dans la table SMS_ban_perm
78
	# Remove phone number in SMS_ban_perm table
101
		sql_remove_ban_perm="connect gammu; DELETE FROM $sms_p"
79
		sql_remove_ban_perm="connect gammu; DELETE FROM $sms_p"
102
		mysql --user=$u_db --password=$p_db -B -se "$sql_remove_ban_perm WHERE SenderNumber=$1;"
80
		mysql --user=$u_db --password=$p_db -B -se "$sql_remove_ban_perm WHERE SenderNumber=$1;"
103
 
-
 
104
	# Ajout au groupe sms
81
	# Add sms group
105
		sql_remove_gp="connect radius; DELETE FROM $radgp WHERE username='$1';"
82
		sql_remove_gp="connect radius; DELETE FROM $radgp WHERE username='$1';"
106
		mysql --user=$u_db --password=$p_db -B -se "$sql_remove_gp"
83
		mysql --user=$u_db --password=$p_db -B -se "$sql_remove_gp"
107
 
-
 
108
	# Suppression du compte dans Radcheck
84
	# Remove account in Radcheck table
109
		sql_remove_compte="connect radius; DELETE FROM $rad WHERE username='$1';"
85
		sql_remove_compte="connect radius; DELETE FROM $rad WHERE username='$1';"
110
		mysql --user=$u_db --password=$p_db -B -se "$sql_remove_compte"
86
		mysql --user=$u_db --password=$p_db -B -se "$sql_remove_compte"
111
} # end function unlock
87
} # end function unlock
112
 
88
 
113
function change_country() {
89
function change_country() {
114
	# recupère le status du pays
-
 
115
		sql_status_country="connect gammu; SELECT status FROM SMS_country WHERE name='$1'"
90
		sql_status_country="connect gammu; SELECT status FROM SMS_country WHERE name='$1'"
116
		stat=$(mysql --user=$u_db --password=$p_db -B -se "$sql_status_country")
91
		stat=$(mysql --user=$u_db --password=$p_db -B -se "$sql_status_country")
117
 
-
 
118
		if [ $stat -eq 0 ]
92
		if [ $stat -eq 0 ]
119
		then
93
		then
120
			sql_change_country="connect gammu; UPDATE $SMS_c SET status=1 WHERE name='$1'"
94
			sql_change_country="connect gammu; UPDATE $SMS_c SET status=1 WHERE name='$1'"
121
			mysql --user=$u_db --password=$p_db -B -se "$sql_change_country"
95
			mysql --user=$u_db --password=$p_db -B -se "$sql_change_country"
122
		else
96
		else
123
			sql_change_country="connect gammu; UPDATE $SMS_c SET status=0 WHERE name='$1'"
97
			sql_change_country="connect gammu; UPDATE $SMS_c SET status=0 WHERE name='$1'"
124
			mysql --user=$u_db --password=$p_db -B -se "$sql_change_country"
98
			mysql --user=$u_db --password=$p_db -B -se "$sql_change_country"
125
		fi
99
		fi
126
 
-
 
127
 
-
 
128
} # end change_country
100
} # end change_country
129
 
101
 
130
function supp_acc_rad() {
102
function supp_acc_rad() {
131
	# Suppression du compte dans Radcheck
103
	# Remove account in Radcheck table
132
		sql_del_compte="connect radius; DELETE FROM $rad WHERE username='$1';"
104
		sql_del_compte="connect radius; DELETE FROM $rad WHERE username='$1';"
133
		mysql --user=$u_db --password=$p_db -B -se "$sql_del_compte"
105
		mysql --user=$u_db --password=$p_db -B -se "$sql_del_compte"
134
} # end function supp_acc_rad()
106
} # end function supp_acc_rad()
135
 
107
 
136
function add_acc_rad() {
108
function add_acc_rad() {
137
	# Ajout table RadCheck : creation du compte
109
	# Add accoubt in RadCheck table
138
		sql_add_pass="connect radius; INSERT INTO $rad (username,attribute,op,value) VALUES ('$1','Crypt-Password',':=','$2');"
110
		sql_add_pass="connect radius; INSERT INTO $rad (username,attribute,op,value) VALUES ('$1','Crypt-Password',':=','$2');"
139
		sql_add_expe="connect radius; INSERT INTO $rad (username,attribute,op,value) VALUES ('$1','Expiration',':=','$3');"
111
		sql_add_expe="connect radius; INSERT INTO $rad (username,attribute,op,value) VALUES ('$1','Expiration',':=','$3');"
140
 
-
 
141
		mysql --user=$u_db --password=$p_db -B -se "$sql_add_pass"
112
		mysql --user=$u_db --password=$p_db -B -se "$sql_add_pass"
142
		mysql --user=$u_db --password=$p_db -B -se "$sql_add_expe"
113
		mysql --user=$u_db --password=$p_db -B -se "$sql_add_expe"
143
 
-
 
144
	# Ajout au groupe sms
114
	# Add this account to sms group
145
		sql_add_gp="connect radius; INSERT INTO $radgp (username,groupname) VALUES ('$1','sms');"
115
		sql_add_gp="connect radius; INSERT INTO $radgp (username,groupname) VALUES ('$1','sms');"
146
		mysql --user=$u_db --password=$p_db -B -se "$sql_add_gp"
116
		mysql --user=$u_db --password=$p_db -B -se "$sql_add_gp"
147
} # end function add_acc_rad()
117
} # end function add_acc_rad()
148
 
118
 
149
function supp_num_temp() {
119
function supp_num_temp() {
150
	# Suppression du numéro dans table SMS_ban_temp
120
	# Remove phone number in SMS_ban_temp table
151
		sql_remove_ban_temp="connect gammu; DELETE FROM $sms_t"
121
		sql_remove_ban_temp="connect gammu; DELETE FROM $sms_t"
152
		mysql --user=$u_db --password=$p_db -B -se "$sql_remove_ban_temp WHERE SenderNumber=$1;"
122
		mysql --user=$u_db --password=$p_db -B -se "$sql_remove_ban_temp WHERE SenderNumber=$1;"
153
} # end function supp_num_temp()
123
} # end function supp_num_temp()
154
 
124
 
155
function add_num_perm() {
125
function add_num_perm() {
156
	# Ajout du numero table SMS_ban_perm, 0 : creation du compte
126
	# Add phone number in SMS_ban_perm table
157
		sql_add_ban_perm="connect gammu; INSERT INTO $sms_p (SenderNumber,Perm,Expiration) VALUES ('$1',0,'$2');"
127
		sql_add_ban_perm="connect gammu; INSERT INTO $sms_p (SenderNumber,Perm,Expiration) VALUES ('$1',0,'$2');"
158
		mysql --user=$u_db --password=$p_db -B -se "$sql_add_ban_perm"
128
		mysql --user=$u_db --password=$p_db -B -se "$sql_add_ban_perm"
159
} # end function add_num_perm()
129
} # end function add_num_perm()
160
 
130
 
161
function supp_num_perm() {
131
function supp_num_perm() {
162
	#Suppression du numero dans la table SMS_ban_perm
132
	# Remove phone number in SMS_ban_perm table
163
		sql_remove_ban_perm="connect gammu; DELETE FROM $sms_p"
133
		sql_remove_ban_perm="connect gammu; DELETE FROM $sms_p"
164
		mysql --user=$u_db --password=$p_db -B -se "$sql_remove_ban_perm WHERE SenderNumber=$1;"
134
		mysql --user=$u_db --password=$p_db -B -se "$sql_remove_ban_perm WHERE SenderNumber=$1;"
165
} # end function add_num_perm()
135
} # end function add_num_perm()
166
 
136
 
167
function new_sms() {
137
function new_sms() {
168
	# Check Inbox table, manage Ban temp and perm, create account
138
	# Check Inbox table, manage Ban temp and perm, create account
169
		export salt='$5$passwd$'
139
		export salt='$5$passwd$'
170
 
-
 
171
		sql_select_inbox="connect gammu; SELECT ID, SenderNumber, TextDecoded FROM $inb;"
140
		sql_select_inbox="connect gammu; SELECT ID, SenderNumber, TextDecoded FROM $inb;"
172
		sql_delete_inbox="connect gammu; DELETE FROM $inb"
141
		sql_delete_inbox="connect gammu; DELETE FROM $inb"
173
 
-
 
174
		mysql --user=$u_db --password=$p_db -B -se "$sql_select_inbox" | while read result;
142
		mysql --user=$u_db --password=$p_db -B -se "$sql_select_inbox" | while read result;
175
		do
143
		do
176
		# On recupère le nombre de mots (resultat)
144
		# Retrieve the number of words (result)
177
			nb=$(echo $result | wc -w)
145
			nb=$(echo $result | wc -w)
178
 
-
 
179
		# On récupère le numéro de l'ID
146
		# Retrive the ID
180
				id=$(echo $result | cut -d ' ' -f1)
147
			id=$(echo $result | cut -d ' ' -f1)
181
 
-
 
182
		numero=$(echo $result | cut -d ' ' -f2)
148
			numero=$(echo $result | cut -d ' ' -f2)
183
 
-
 
184
		if [[ $numero =~ ^\+ ]]
149
			if [[ $numero =~ ^\+ ]]
185
		then
150
			then
186
 
-
 
187
			# On vérifie si le pays est bloqué
151
			# Check if country is blocked
188
				sql_select_countries="connect gammu; SELECT id FROM $SMS_c WHERE status=1"
152
				sql_select_countries="connect gammu; SELECT id FROM $SMS_c WHERE status=1"
189
				mysql --user=$u_db --password=$p_db -B -se "$sql_select_countries" | while read result_c;
153
				mysql --user=$u_db --password=$p_db -B -se "$sql_select_countries" | while read result_c;
190
				do
154
				do
191
 
-
 
192
				if [[ $numero =~ ^"$result_c" ]]
155
					if [[ $numero =~ ^"$result_c" ]]
193
				then
-
 
194
 
-
 
195
				numero=$(echo $numero | cut -d '+' -f2)
-
 
196
 
-
 
197
				# On vérifie que le numéro n'est pas Ban Perm
-
 
198
					sql_ban_perm="connect gammu; SELECT * FROM $sms_p WHERE SenderNumber=$numero"
-
 
199
					result_bp=$(mysql --user=$u_db --password=$p_db -B -se "$sql_ban_perm")
-
 
200
 
-
 
201
					if test -z "$result_bp"
-
 
202
					then
156
					then
203
						# Test sur le nombre de mots (resultat)
157
						numero=$(echo $numero | cut -d '+' -f2)
204
						if [ $nb -eq 2 ]	# Si 2 mots : le mot de passe est manquant
158
				# Check if GSM number is nabbed
205
						then
-
 
206
							# On incrémente de 1 dans la table des bans temp // NO PASSWORD
-
 
207
							sql_add_temp="connect gammu; INSERT INTO $sms_t(SenderNumber) VALUES ('$numero');"
159
						sql_ban_perm="connect gammu; SELECT * FROM $sms_p WHERE SenderNumber=$numero"
208
							mysql --user=$u_db --password=$p_db -B -se "$sql_add_temp"
160
						result_bp=$(mysql --user=$u_db --password=$p_db -B -se "$sql_ban_perm")
209
 
-
 
210
						elif [ $nb -eq 3 ]	# Si 3 mots : id + mot de passe + numero
161
						if test -z "$result_bp"
211
						then
162
						then
212
							export pass=$(echo $result | cut -d ' ' -f3)
-
 
213
							pass_salt=$(perl -e'print crypt($ARGV[0],$ARGV[1])' $pass $salt)
-
 
214
 
-
 
215
							export LC_TIME="en_US.UTF-8"
-
 
216
							expir=$(date '+%d %B %Y' -d "$time_account days")
-
 
217
 
-
 
218
							supp_acc_rad "$numero"
-
 
219
							add_acc_rad "$numero" "$pass_salt" "$expir"
-
 
220
							supp_num_temp "$numero"
-
 
221
							add_num_perm "$numero" "$expir"
-
 
222
 
-
 
223
						else
-
 
224
						# Autrement, le mot de passe est trop grand ( > un mot )
-
 
225
							# On incrémente d'un 1 dans la table des bans temp
-
 
226
								sql_add_temp="connect gammu; INSERT INTO $sms_t(SenderNumber) VALUES ('$numero');"
-
 
227
								mysql --user=$u_db --password=$p_db -B -se "$sql_add_temp"
-
 
228
						fi
-
 
229
 
-
 
230
						# On gère les bans temp en ban perm
-
 
231
							sql_select_temp="connect gammu; SELECT ID FROM $sms_t WHERE SenderNumber='$numero'"
-
 
232
							r_select_temp=$(mysql --user=$u_db --password=$p_db -B -se "$sql_select_temp")
-
 
233
							nb_ban_t=$(echo $r_select_temp| wc -w)
-
 
234
 
-
 
235
						if [ $nb_ban_t -ge $nb_essais ]
-
 
236
						then
-
 
237
							supp_num_temp "$numero"
-
 
238
 
-
 
239
								export LC_TIME="en_US.UTF-8"
-
 
240
								expir_f=$(date '+%d %B %Y' -d "$time_ban days")
-
 
241
 
-
 
242
							# Ajout du numero table SMS_ban_perm, 1 : flood
-
 
243
								sql_add_ban_perm="connect gammu; INSERT INTO $sms_p (SenderNumber,Perm,Expiration) VALUES ('$numero',1,'$expir_f');"
-
 
244
								mysql --user=$u_db --password=$p_db -B -se "$sql_add_ban_perm"
-
 
245
						fi
-
 
246
					else
-
 
247
						date_expiration=$(echo $result_bp | cut -d ' ' -f2,3,4)
-
 
248
						perm=$(echo $result_bp | cut -d ' ' -f5)
-
 
249
 
-
 
250
						export LC_TIME="en_US.UTF-8"
-
 
251
						date_script=$(date '+%d %B %Y' -d "now")
-
 
252
 
-
 
253
					# On converti les deux dates en secondes, pour les comparer.
-
 
254
						d_exp=$(date --date "$date_expiration" +%s)
-
 
255
						d_scr=$(date --date "$date_script" +%s)
-
 
256
 
-
 
257
						if test $d_scr -ge $d_exp		# Si le ban à expiré
-
 
258
						then
-
 
259
 
-
 
260
							# Test sur le nombre de mots (resultat)
163
						# Test the number of word (result)
261
							if [ $nb -eq 2 ]	# Si 2 mots : le mot de passe est manquant
164
							if [ $nb -eq 2 ] # if only 2 words : lack of password
262
							then
165
							then
263
							# On incrémente de 1 dans la table des bans temp
166
							# Add "1" in bans_temp table // NO PASSWORD
264
								sql_add_temp="connect gammu; INSERT INTO $sms_t(SenderNumber) VALUES ('$numero');"
167
								sql_add_temp="connect gammu; INSERT INTO $sms_t(SenderNumber) VALUES ('$numero');"
265
								mysql --user=$u_db --password=$p_db -B -se "$sql_add_temp"
168
								mysql --user=$u_db --password=$p_db -B -se "$sql_add_temp"
266
 
-
 
267
							elif [ $nb -eq 3 ]	# Si 3 mots : id + mot de passe + numero
169
							elif [ $nb -eq 3 ] # if 3 words (id + password + phone numbere)
268
							then
170
							then
269
								date_expiration=$(echo $result_bp | cut -d ' ' -f2,3,4)
-
 
270
								perm=$(echo $result_bp | cut -d ' ' -f5)
-
 
271
 
-
 
272
								date_script=$(date '+%d %B %Y' -d "now")
-
 
273
 
-
 
274
							# On converti les deux dates en secondes, pour les comparer.
-
 
275
								d_exp=$(date --date "$date_expiration" +%s)
-
 
276
								d_scr=$(date --date "$date_script" +%s)
-
 
277
 
-
 
278
								export pass=$(echo $result | cut -d ' ' -f3)
171
								export pass=$(echo $result | cut -d ' ' -f3)
279
								pass_salt=$(perl -e'print crypt($ARGV[0],$ARGV[1])' $pass $salt)
172
								pass_salt=$(perl -e'print crypt($ARGV[0],$ARGV[1])' $pass $salt)
280
 
-
 
281
								export LC_TIME="en_US.UTF-8"
173
								export LC_TIME="en_US.UTF-8"
282
								expir=$(date '+%d %B %Y' -d "$time_account days")
174
								expir=$(date '+%d %B %Y' -d "$time_account days")
283
 
-
 
284
 
-
 
285
								supp_acc_rad "$numero"
175
								supp_acc_rad "$numero"
286
								add_acc_rad "$numero" "$pass_salt" "$expir"
176
								add_acc_rad "$numero" "$pass_salt" "$expir"
287
								supp_num_temp "$numero"
177
								supp_num_temp "$numero"
288
								supp_num_perm "$numero"
-
 
289
								add_num_perm "$numero" "$expir"
178
								add_num_perm "$numero" "$expir"
-
 
179
							else # more then 3 words --> Add "1" in ban_temp table
-
 
180
								sql_add_temp="connect gammu; INSERT INTO $sms_t(SenderNumber) VALUES ('$numero');"
-
 
181
								mysql --user=$u_db --password=$p_db -B -se "$sql_add_temp"
290
 
182
							fi
-
 
183
							# manage ban perm
-
 
184
							sql_select_temp="connect gammu; SELECT ID FROM $sms_t WHERE SenderNumber='$numero'"
-
 
185
							r_select_temp=$(mysql --user=$u_db --password=$p_db -B -se "$sql_select_temp")
-
 
186
							nb_ban_t=$(echo $r_select_temp| wc -w)
-
 
187
							if [ $nb_ban_t -ge $nb_essais ]
-
 
188
							then
-
 
189
								supp_num_temp "$numero"
-
 
190
								export LC_TIME="en_US.UTF-8"
-
 
191
								expir_f=$(date '+%d %B %Y' -d "$time_ban days")
-
 
192
							# Add "1" in SMS_ban_perm table : flood
-
 
193
								sql_add_ban_perm="connect gammu; INSERT INTO $sms_p (SenderNumber,Perm,Expiration) VALUES ('$numero',1,'$expir_f');"
-
 
194
								mysql --user=$u_db --password=$p_db -B -se "$sql_add_ban_perm"
-
 
195
							fi
291
							else
196
						else
-
 
197
							date_expiration=$(echo $result_bp | cut -d ' ' -f2,3,4)
-
 
198
							perm=$(echo $result_bp | cut -d ' ' -f5)
-
 
199
							export LC_TIME="en_US.UTF-8"
-
 
200
							date_script=$(date '+%d %B %Y' -d "now")
292
							# Autrement, le mot de passe est trop grand ( > un mot )
201
							# convert in seconds in order to be able to compare
-
 
202
							d_exp=$(date --date "$date_expiration" +%s)
-
 
203
							d_scr=$(date --date "$date_script" +%s)
-
 
204
							if test $d_scr -ge $d_exp		# Si le ban à expiré
-
 
205
							then
-
 
206
							# Test the number of words (result)
-
 
207
								if [ $nb -eq 2 ]	# Si 2 mots : le mot de passe est manquant
-
 
208
								then
-
 
209
								# Add "1" in ban temp table
-
 
210
								sql_add_temp="connect gammu; INSERT INTO $sms_t(SenderNumber) VALUES ('$numero');"
-
 
211
								mysql --user=$u_db --password=$p_db -B -se "$sql_add_temp"
-
 
212
								elif [ $nb -eq 3 ]	# Si 3 mots : id + mot de passe + numero
-
 
213
									then
-
 
214
									date_expiration=$(echo $result_bp | cut -d ' ' -f2,3,4)
-
 
215
									perm=$(echo $result_bp | cut -d ' ' -f5)
-
 
216
									date_script=$(date '+%d %B %Y' -d "now")
293
								# On incrémente d'un 1 dans la table des bans temp
217
									# convert in seconds in order to be able to compare
-
 
218
									d_exp=$(date --date "$date_expiration" +%s)
-
 
219
									d_scr=$(date --date "$date_script" +%s)
-
 
220
									export pass=$(echo $result | cut -d ' ' -f3)
-
 
221
									pass_salt=$(perl -e'print crypt($ARGV[0],$ARGV[1])' $pass $salt)
-
 
222
									export LC_TIME="en_US.UTF-8"
-
 
223
									expir=$(date '+%d %B %Y' -d "$time_account days")
-
 
224
									supp_acc_rad "$numero"
-
 
225
									add_acc_rad "$numero" "$pass_salt" "$expir"
-
 
226
									supp_num_temp "$numero"
-
 
227
									supp_num_perm "$numero"
-
 
228
									add_num_perm "$numero" "$expir"
-
 
229
								else
-
 
230
								# number of words to big (> 3)
-
 
231
								# Add "1" in bans temp table
294
									sql_add_temp="connect gammu; INSERT INTO $sms_t(SenderNumber) VALUES ('$numero');"
232
									sql_add_temp="connect gammu; INSERT INTO $sms_t(SenderNumber) VALUES ('$numero');"
295
									mysql --user=$u_db --password=$p_db -B -se "$sql_add_temp"
233
									mysql --user=$u_db --password=$p_db -B -se "$sql_add_temp"
296
									echo "Mot de passe incorrect, ajout du numero en ban temporaire"
234
									echo "Mot de passe incorrect, ajout du numero en ban temporaire"
297
							fi
235
								fi
298
 
-
 
299
							# On gère les bans temp en ban perm
236
							# manage bans_temp & ban_perm
300
								sql_select_temp="connect gammu; SELECT ID FROM $sms_t WHERE SenderNumber='$numero'"
237
								sql_select_temp="connect gammu; SELECT ID FROM $sms_t WHERE SenderNumber='$numero'"
301
								r_select_temp=$(mysql --user=$u_db --password=$p_db -B -se "$sql_select_temp")
238
								r_select_temp=$(mysql --user=$u_db --password=$p_db -B -se "$sql_select_temp")
302
								nb_ban_t=$(echo $r_select_temp| wc -w)
239
								nb_ban_t=$(echo $r_select_temp| wc -w)
303
 
-
 
304
							if [ $nb_ban_t -ge $nb_essais ]
240
								if [ $nb_ban_t -ge $nb_essais ]
305
							then
241
								then
306
								supp_num_perm "$numero"
242
									supp_num_perm "$numero"
307
								supp_num_temp "$numero"
243
									supp_num_temp "$numero"
308
 
-
 
309
									export LC_TIME="en_US.UTF-8"
244
									export LC_TIME="en_US.UTF-8"
310
									expir_f=$(date '+%d %B %Y' -d "$time_ban days")
245
									expir_f=$(date '+%d %B %Y' -d "$time_ban days")
311
 
-
 
312
								# Ajout du numero table SMS_ban_perm, 1 : flood
246
									# Add phne number in ban_perm : flood
313
									sql_add_ban_perm="connect gammu; INSERT INTO $sms_p (SenderNumber,Perm,Expiration) VALUES ('$numero',1,'$expir_f');"							mysql --user=$u_db --password=$p_db -B -se "$sql_add_ban_perm"
247
									sql_add_ban_perm="connect gammu; INSERT INTO $sms_p (SenderNumber,Perm,Expiration) VALUES ('$numero',1,'$expir_f');"
-
 
248
									mysql --user=$u_db --password=$p_db -B -se "$sql_add_ban_perm"
-
 
249
								fi
-
 
250
							else
-
 
251
									echo "Le ban de $numero est encore valide"
314
							fi
252
							fi
315
						else
-
 
316
							echo "Le ban de $numero est encore valide"
-
 
317
						fi
-
 
318
						break
253
						break
319
					fi
254
						fi
320
				#else
255
					#else
321
					#echo "Pays bloqué"
256
					#echo "Pays bloqué"
322
				fi
257
					fi
323
			done
258
				done
324
		else
259
			else
325
			echo "Numero non autorisé (ex: 36665)"
260
				echo "Numero non autorisé (ex: 36665)"
326
		fi
261
			fi
327
				# On supprime la ligne d'ID=$id dans inbox
262
			# On supprime la ligne d'ID=$id dans inbox
328
				mysql --user=$u_db --password=$p_db -B -e "$sql_delete_inbox WHERE ID=$id;"
263
			mysql --user=$u_db --password=$p_db -B -e "$sql_delete_inbox WHERE ID=$id;"
329
		done
264
		done
330
} # end function new_sms
265
} # end function new_sms
331
 
266
 
332
 
267
 
333
# CORE
268
# CORE
Line 335... Line 270...
335
	-h | --help)
270
	-h | --help)
336
		echo "$usage"
271
		echo "$usage"
337
		exit 0
272
		exit 0
338
		;;
273
		;;
339
	--start)
274
	--start)
-
 
275
		failed="0"
-
 
276
		comports=`ls -l /dev/ttyUSB* 2>/dev/null | wc -l`
-
 
277
		if [ $comports == "0" ]
-
 
278
		then
-
 
279
			echo "No GSM modem found."
-
 
280
			failed="1"
-
 
281
		fi	
-
 
282
		if [ -z "$(grep '^SMS_NUM=' $CONF_FILE | cut -d'=' -f2-)" ]; then
-
 
283
			echo 'The phone number is not set.'
-
 
284
			failed="1"
-
 
285
		fi
-
 
286
		if [ $failed == "1" ]
-
 
287
		then
-
 
288
			sed -i "s/^SMS=.*/SMS=off/" $CONF_FILE
-
 
289
			exit 0
-
 
290
		fi
340
		gammu_pid=`/usr/sbin/pidof gammu-smsd|wc -l`
291
		gammu_pid=`/usr/sbin/pidof gammu-smsd|wc -l`
341
		if [ $gammu_pid != "0" ]
292
		if [ $gammu_pid != "0" ]
342
			then
293
		then
343
			echo "gammu is already started"
294
			echo "Gammu is already started"
344
		else
295
		else
345
			start_gammu
296
			start_gammu
-
 
297
			sleep 1
-
 
298
			is_active=`systemctl is-active gammu-smsd`
-
 
299
			if [ $is_active == "active" ]
-
 
300
			then
-
 
301
				sed -i "s/^SMS=.*/SMS=on/" $CONF_FILE
-
 
302
			else
-
 
303
				sed -i "s/^SMS=.*/SMS=off/" $CONF_FILE
-
 
304
			fi
346
		fi
305
		fi
347
		sed -i "s/^SMS=.*/SMS=on/" $CONF_FILE
-
 
348
		exit 0
306
		exit 0
349
		;;
307
		;;
350
	--stop)
308
	--stop)
351
		gammu_pid=`/usr/sbin/pidof gammu-smsd|wc -l`
309
		gammu_pid=`/usr/sbin/pidof gammu-smsd|wc -l`
352
		if [ $gammu_pid != "0" ]
310
		if [ $gammu_pid != "0" ]
353
			then
311
		then
354
			stop_gammu
312
			stop_gammu
355
		else
313
		else
356
			echo "gammu is already stopped"
314
			echo "Gammu is already stopped"
357
		fi
315
		fi
358
		sed -i "s/^SMS=.*/SMS=off/" $CONF_FILE
316
		sed -i "s/^SMS=.*/SMS=off/" $CONF_FILE
359
		exit 0
317
		exit 0
360
		;;
318
		;;
361
	--pidof)
319
	--pidof)