forked from kb1isz/linDmrMaster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqlite.c
402 lines (357 loc) · 16.1 KB
/
sqlite.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*
* Linux DMR Master server
Copyright (C) 2014 Wim Hofman
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "master_server.h"
sqlite3 *openDatabase(){
int rc;
sqlite3 *db;
rc = sqlite3_open("Master_Server.sql", &db);
if (rc){
syslog(LOG_NOTICE,"Cant't open database: %s",sqlite3_errmsg(db));
sqlite3_close(db);
return NULL;
}
return db;
}
void closeDatabase(sqlite3 *db){
if (sqlite3_close(db) != SQLITE_OK) syslog(LOG_NOTICE,"Failed to close SQLITE database");
}
bool isFieldExisting(sqlite3 *db, char *table, char *field){
sqlite3_stmt *stmt;
char SQLQUERY[200];
syslog(LOG_NOTICE,"Database, checking column '%s' in table '%s'",field,table);
sprintf(SQLQUERY,"PRAGMA table_info(%s)",table);
if (sqlite3_prepare_v2(db,SQLQUERY,-1,&stmt,0) == 0){
while (sqlite3_step(stmt) == SQLITE_ROW){
if (strcmp(sqlite3_column_text(stmt,1),field) == 0){
sqlite3_finalize(stmt);
return true;
}
}
}
else{
syslog(LOG_NOTICE,"Database error: %s",sqlite3_errmsg(db));
sqlite3_finalize(stmt);
return false;
}
sqlite3_finalize(stmt);
syslog(LOG_NOTICE,"field %s doesn't exist",field);
return false;
}
bool isTableExisting(sqlite3 *db, char *table){
sqlite3_stmt *stmt;
char SQLQUERY[200];
syslog(LOG_NOTICE,"Database, checking table '%s'",table);
sprintf(SQLQUERY,"SELECT count(*) FROM sqlite_master WHERE type='table' AND name = '%s'",table);
if (sqlite3_prepare_v2(db,SQLQUERY,-1,&stmt,0) == 0){
if (sqlite3_step(stmt) == SQLITE_ROW){
if(sqlite3_column_int(stmt,0) == 0){
syslog(LOG_NOTICE,"table %s doesn't exist",table);
sqlite3_finalize(stmt);
return false;
}
}
}
else{
syslog(LOG_NOTICE,"Database error: %s",sqlite3_errmsg(db));
sqlite3_finalize(stmt);
return false;
}
sqlite3_finalize(stmt);
return true;
}
int initDatabase(sqlite3 *db){
char SQLQUERY[1000];
int r;
sqlite3_stmt *stmt;
srand(time(NULL));
do{
r = rand() % 99999;
} while (r < 10000);
if (!isTableExisting(db,"master")){
sprintf(SQLQUERY,"CREATE TABLE master (repTS1 VARCHAR(100) default '',repTS2 VARCHAR(100) default '',sMasterTS1 VARCHAR(100) default '',sMasterTS2 VARCHAR(100) default '', timeBase INTEGER default 60, servicePort int default 50000, rdacPort int default 50002,dmrPort int default 50001, baseDmrPort int default 50100, baseRdacPort int default 50200, maxRepeaters int default 20, echoId int default 9990, rrsGpsId int default 500, aprsUrl VARCHAR(100) default '', aprsPort VARCHAR(7) default '8080', echoSlot integer default 1, masterDmrId integer default %i)",r);
if (sqlite3_exec(db,SQLQUERY,NULL,NULL,NULL) == 0){
sprintf(SQLQUERY,"INSERT INTO master (repTS1) VALUES ('')");
if (sqlite3_exec(db,SQLQUERY,NULL,NULL,NULL) == 0){
syslog(LOG_NOTICE,"Table master created");
}
else{
syslog(LOG_NOTICE,"Database error: %s",sqlite3_errmsg(db));
return 0;
}
}
else{
syslog(LOG_NOTICE,"Database error: %s",sqlite3_errmsg(db));
return 0;
}
}
if (!isTableExisting(db,"sMaster")){
sprintf(SQLQUERY,"CREATE TABLE sMaster (ownName VARCHAR(100) default '',ownCountryCode VARCHAR(5) default '',ownRegion VARCHAR(2) default '',sMasterIp VARCHAR(100) default '', sMasterPort VARCHAR(5) default '62010', priorityTGTS1 integer default 0, priorityTGTS2 integer default 0, priorityTimeout integer default 10)");
if (sqlite3_exec(db,SQLQUERY,NULL,NULL,NULL) == 0){
sprintf(SQLQUERY,"INSERT INTO sMaster (ownName) VALUES ('')");
if (sqlite3_exec(db,SQLQUERY,NULL,NULL,NULL) == 0){
syslog(LOG_NOTICE,"Table sMaster created");
}
else{
syslog(LOG_NOTICE,"Database error: %s",sqlite3_errmsg(db));
return 0;
}
}
else{
syslog(LOG_NOTICE,"Database error: %s",sqlite3_errmsg(db));
return 0;
}
}
if (!isTableExisting(db,"repeaters")){
sprintf(SQLQUERY,"CREATE TABLE repeaters (repeaterId INTEGER default 0 ,callsign VARCHAR(10) default '',txFreq VARCHAR(10) default '',shift VARCHAR(7) default '', hardware VARCHAR(11) default '', firmware VARCHAR(12) default '', mode VARCHAR(4) default '', currentAddress INTEGER default 0, timeStamp varchar(20) default '1970-1-1 00:00:00', ipAddress VARCHAR(50) default '',language VARCHAR(50) default 'english', geoLocation VARCHAR(20) default '', aprsPass VARCHAR(5) default '0000', aprsBeacon VARCHAR(100) default 'DMR repeater', aprsPHG VARCHHAR(7) default '', currentReflector integer default 0, autoReflector integer default 0, userName default '', upDated integer default 0, lastRdacUpdate varchar(20) default '1970-1-1 00:00:00', online integer default 0, intlRefAllow integer default 1, reflectorTimeout integer default 1800,autoConnectTimer integer default 600)");
if (sqlite3_exec(db,SQLQUERY,NULL,NULL,NULL) == 0){
syslog(LOG_NOTICE,"Table repeater created");
}
else{
syslog(LOG_NOTICE,"Database error: %s",sqlite3_errmsg(db));
return 0;
}
}
if (!isTableExisting(db,"callsigns")){
sprintf(SQLQUERY,"CREATE TABLE callsigns (radioId INTEGER default 0 PRIMARY KEY, callsign VARCHAR(32) default '', name VARCHAR(32) default '', aprsSuffix VARCHAR(3) default '-9', aprsBeacon VARCHAR(100) default 'DMR terminal', aprsSymbol INTEGER default 62, hasSendAprs INTEGER default 0, messageStore INTEGER default 1, email VARCHAR(100) default '', login VARCHAR(50) default '', password VARCHAR(50) default '', lastAprsTime INTEGER default 0, madeChange INTEGER default 0, city VARCHAR(32) default '', state VARCHAR(32) default '', country VARCHAR(32) default '', radio VARCHAR(32) default '', homeRepeaterId VARCHAR(32) default '', remarks VARCHAR(32) default '')");
if (sqlite3_exec(db,SQLQUERY,NULL,NULL,NULL) == 0){
syslog(LOG_NOTICE,"Table callsigns created");
}
else{
syslog(LOG_NOTICE,"Database error: %s",sqlite3_errmsg(db));
return 0;
}
}
if (!isTableExisting(db,"rrs")){
sprintf(SQLQUERY,"CREATE TABLE rrs (radioId INTEGER default 0 PRIMARY KEY, callsign VARCHAR(32) default '',name VARCHAR(32) default '', registerTime VARCHAR(20) default '1970-01-01 00:00:00', onRepeater VARCHAR(32) default '', unixTime long)");
if(sqlite3_exec(db,SQLQUERY,NULL,NULL,NULL) == 0){
syslog(LOG_NOTICE,"Table rrs created");
}
else{
syslog(LOG_NOTICE,"Database error: %s",sqlite3_errmsg(db));
return 0;
}
}
if (!isTableExisting(db,"traffic")){
sprintf(SQLQUERY,"CREATE TABLE traffic (senderId int default 0 PRIMARY KEY,senderCallsign varchar(32) default '',targetId int default 0,targetCallsign varchar(32) default '',channel int default 0,serviceType varchar(15) default 'Voice',callType varchar(15) default 'Group',timeStamp int default 0, onRepeater varchar(32) default '', senderName varchar(32) default '')");
if(sqlite3_exec(db,SQLQUERY,NULL,NULL,NULL) == 0){
syslog(LOG_NOTICE,"Table traffic created");
}
else{
syslog(LOG_NOTICE,"Database error: %s",sqlite3_errmsg(db));
return 0;
}
}
if (!isTableExisting(db,"voiceTraffic")){
sprintf(SQLQUERY,"CREATE TABLE voiceTraffic (senderId int default 0 PRIMARY KEY,senderCallsign varchar(32) default '',targetId int default 0,targetCallsign varchar(32) default '',channel int default 0,serviceType varchar(15) default 'Voice',callType varchar(15) default 'Group',timeStamp int default 0, onRepeater varchar(32) default '', senderName varchar(32) default '')");
if(sqlite3_exec(db,SQLQUERY,NULL,NULL,NULL) == 0){
syslog(LOG_NOTICE,"Table voiceTraffic created");
}
else{
syslog(LOG_NOTICE,"Database error: %s",sqlite3_errmsg(db));
return 0;
}
}
if (!isTableExisting(db,"localReflectors")){
sprintf(SQLQUERY,"CREATE TABLE localReflectors(id int primary key,name varchar(50),type int default 0)");
if(sqlite3_exec(db,SQLQUERY,NULL,NULL,NULL) == 0){
syslog(LOG_NOTICE,"Table localReflectors created");
}
else{
syslog(LOG_NOTICE,"Database error: %s",sqlite3_errmsg(db));
return 0;
}
}
if (!isTableExisting(db,"access")){
sprintf(SQLQUERY,"CREATE TABLE access(userName varchar(20) default '' PRIMARY KEY, password varchar(20) default '', role varchar(30) default 'log');");
if(sqlite3_exec(db,SQLQUERY,NULL,NULL,NULL) == 0){
syslog(LOG_NOTICE,"Table access created");
}
else{
syslog(LOG_NOTICE,"Database error: %s",sqlite3_errmsg(db));
return 0;
}
sprintf(SQLQUERY,"INSERT INTO access (userName,password,role) VALUES ('admin','admin','admin')");
if(sqlite3_exec(db,SQLQUERY,NULL,NULL,NULL) == 0){
syslog(LOG_NOTICE,"Default admin user created");
}
else{
syslog(LOG_NOTICE,"Database error: %s",sqlite3_errmsg(db));
return 0;
}
}
//check for new fields added later in development
if (!isFieldExisting(db,"localReflectors","type")){
sprintf(SQLQUERY,"alter table localReflectors add type integer default 0");
if (sqlite3_exec(db,SQLQUERY,0,0,0) == 0){
syslog(LOG_NOTICE,"field type in localReflectors created");
}
else{
syslog(LOG_NOTICE,"Database error: %s",sqlite3_errmsg(db));
return 0;
}
}
if (!isFieldExisting(db,"repeaters","intlRefAllow")){
sprintf(SQLQUERY,"alter table repeaters add intlRefAllow integer default 1");
if (sqlite3_exec(db,SQLQUERY,0,0,0) == 0){
syslog(LOG_NOTICE,"field intlRefAllow in repeaters created");
}
else{
syslog(LOG_NOTICE,"Database error: %s",sqlite3_errmsg(db));
return 0;
}
}
if (!isFieldExisting(db,"repeaters","reflectorTimeout")){
sprintf(SQLQUERY,"alter table repeaters add reflectorTimeout integer default 1800");
if (sqlite3_exec(db,SQLQUERY,0,0,0) == 0){
syslog(LOG_NOTICE,"field reflectorTimeout in repeaters created");
}
else{
syslog(LOG_NOTICE,"Database error: %s",sqlite3_errmsg(db));
return 0;
}
}
if (!isFieldExisting(db,"repeaters","autoConnectTimer")){
sprintf(SQLQUERY,"alter table repeaters add autoConnectTimer integer default 600");
if (sqlite3_exec(db,SQLQUERY,0,0,0) == 0){
syslog(LOG_NOTICE,"field autoConnectTimer in repeaters created");
}
else{
syslog(LOG_NOTICE,"Database error: %s",sqlite3_errmsg(db));
return 0;
}
}
if (!isFieldExisting(db,"master","masterDmrId")){
sprintf(SQLQUERY,"alter table master add masterDmrId integer default %i",r);
if (sqlite3_exec(db,SQLQUERY,0,0,0) == 0){
syslog(LOG_NOTICE,"field masterDmrId in master created");
}
else{
syslog(LOG_NOTICE,"Database error: %s",sqlite3_errmsg(db));
return 0;
}
}
if (!isFieldExisting(db,"master","debug")){
sprintf(SQLQUERY,"alter table master add debug integer default 0");
if (sqlite3_exec(db,SQLQUERY,0,0,0) == 0){
syslog(LOG_NOTICE,"field debug in master created");
}
else{
syslog(LOG_NOTICE,"Database error: %s",sqlite3_errmsg(db));
return 0;
}
}
if (!isFieldExisting(db,"master","dynamicTS1")){
sprintf(SQLQUERY,"alter table master add dynamicTS1 varchar(100) default ''");
if (sqlite3_exec(db,SQLQUERY,0,0,0) == 0){
syslog(LOG_NOTICE,"field dynamicTS1 in master created");
}
else{
syslog(LOG_NOTICE,"Database error: %s",sqlite3_errmsg(db));
return 0;
}
}
if (!isFieldExisting(db,"master","dynamicTS2")){
sprintf(SQLQUERY,"alter table master add dynamicTS2 varchar(100) default ''");
if (sqlite3_exec(db,SQLQUERY,0,0,0) == 0){
syslog(LOG_NOTICE,"field dynamicTS2 in master created");
}
else{
syslog(LOG_NOTICE,"Database error: %s",sqlite3_errmsg(db));
return 0;
}
}
if (!isFieldExisting(db,"sMaster","priorityTGTS1")){
sprintf(SQLQUERY,"alter table sMaster add priorityTGTS1 integer default 0");
if (sqlite3_exec(db,SQLQUERY,0,0,0) == 0){
syslog(LOG_NOTICE,"field priorityTGTS1 in sMaster created");
}
else{
syslog(LOG_NOTICE,"Database error: %s",sqlite3_errmsg(db));
return 0;
}
}
if (!isFieldExisting(db,"sMaster","priorityTGTS2")){
sprintf(SQLQUERY,"alter table sMaster add priorityTGTS2 integer default 0");
if (sqlite3_exec(db,SQLQUERY,0,0,0) == 0){
syslog(LOG_NOTICE,"field priorityTGTS2 in sMaster created");
}
else{
syslog(LOG_NOTICE,"Database error: %s",sqlite3_errmsg(db));
return 0;
}
}
if (!isFieldExisting(db,"sMaster","priorityTimeout")){
sprintf(SQLQUERY,"alter table sMaster add priorityTimeout integer default 10");
if (sqlite3_exec(db,SQLQUERY,0,0,0) == 0){
syslog(LOG_NOTICE,"field priorityTimeout in sMaster created");
}
else{
syslog(LOG_NOTICE,"Database error: %s",sqlite3_errmsg(db));
return 0;
}
}
if (!isFieldExisting(db,"sMaster","eMail")){
sprintf(SQLQUERY,"alter table sMaster add eMail varchar(100) default 'dmrMaster@test.be'");
if (sqlite3_exec(db,SQLQUERY,0,0,0) == 0){
syslog(LOG_NOTICE,"field eMail in sMaster created");
}
else{
syslog(LOG_NOTICE,"Database error: %s",sqlite3_errmsg(db));
return 0;
}
}
if (!isFieldExisting(db,"repeaters","codecRepeater")){
sprintf(SQLQUERY,"alter table repeaters add codecRepeater integer default 0");
if (sqlite3_exec(db,SQLQUERY,0,0,0) == 0){
syslog(LOG_NOTICE,"field codecRepeater in repeaters created");
}
else{
syslog(LOG_NOTICE,"Database error: %s",sqlite3_errmsg(db));
return 0;
}
}
//Fix for random master ID max 5 digits
bool misMatch = false;
sprintf(SQLQUERY,"SELECT masterDmrId FROM master");
if (sqlite3_prepare_v2(db,SQLQUERY,-1,&stmt,0) == 0){
if (sqlite3_step(stmt) == SQLITE_ROW){
if (sqlite3_column_int(stmt,0)<10000 || sqlite3_column_int(stmt,0)>99999){
misMatch = true;
}
sqlite3_finalize(stmt);
}
}
if (misMatch){
syslog(LOG_NOTICE,"Correcting random masterID");
sprintf(SQLQUERY,"update master set masterDmrId = %i",r);
if (sqlite3_exec(db,SQLQUERY,0,0,0) == 0){
syslog(LOG_NOTICE,"Successfully changed masterId");
}
else{
syslog(LOG_NOTICE,"Database error: %s",sqlite3_errmsg(db));
return 0;
}
}
//Clean database
sprintf(SQLQUERY,"update repeaters set currentReflector = 0");
if (sqlite3_exec(db,SQLQUERY,0,0,0) == 0){
syslog(LOG_NOTICE,"Reflector status cleaned");
}
else{
syslog(LOG_NOTICE,"Database error: %s",sqlite3_errmsg(db));
}
return 1;
}