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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
|
/* eurephia-sqlite.c -- Main driver for eurephia authentication plugin for OpenVPN
* This is the SQLite database driver
*
* GPLv2 - Copyright (C) 2008 David Sommerseth <dazo@users.sourceforge.net>
*
* 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; version 2
* of the License.
*
* 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>
#include <assert.h>
#include <sqlite3.h>
#include <eurephiadb_driver.h>
#include <eurephia_nullsafe.h>
#include <eurephia_log.h>
#include <eurephia_values.h>
#include <eurephiadb_session_common.h>
#include <eurephiadb_session_struct.h>
#include <passwd.h>
#include "sqlite.h"
#ifdef MEMWATCH
#include <memwatch.h>
#endif
#define DRIVERVERSION "1.1"
#ifndef DRIVERAPIVERSION
# define DRIVERAPIVERSION 2
#endif
// Mapping table - mapping attempt types from .... to sqlite table fields
typedef struct {
char *colname;
char *allow_cfg;
char *descr;
char *default_value;
} eDBattempt_types_t;
static const eDBattempt_types_t eDBattempt_types[] = {
{NULL, NULL, NULL},
{"remoteip\0", "allow_ipaddr_attempts\0", "IP Address\0", "10\0"},
{"digest\0", "allow_cert_attempts\0", "Certificate\0", "5\0"},
{"username\0", "allow_username_attempts\0", "Username\0", "5\0"},
{NULL, NULL, NULL}
};
/*
* Driver info
*/
const char *eDB_DriverVersion() {
return "eurephiadb-sqlite (v"DRIVERVERSION") David Sommerseth 2008 (C) GPLv2";
}
int eDB_DriverAPIVersion() {
return DRIVERAPIVERSION;
}
/*
* local functions
*/
// Function for simplifying update of openvpn_blacklist
void update_attempts(eurephiaCTX *ctx, const char *blid) {
dbresult *res = NULL;
if( blid != NULL ) {
res = sqlite_query(ctx,
"UPDATE openvpn_blacklist "
" SET last_accessed = CURRENT_TIMESTAMP WHERE blid = %q", blid);
if( res == NULL ) {
eurephia_log(ctx, LOG_CRITICAL, 0,
"Could not update openvpn_blacklist.last_accessed for blid=%s", blid);
}
sqlite_free_results(res);
}
}
/*
* Public driver functions
*/
// Connect to the database ... connection is stored in the eurephiaCTX context
int eDBconnect(eurephiaCTX *ctx, const int argc, const char **argv)
{
eDBconn *dbc = NULL;
dbresult *res = NULL;
int rc;
DEBUG(ctx, 20, "Function call: eDBconnect(ctx, %i, '%s')", argc, argv[0]);
if( (argc != 1) || (argv[0] == NULL) || (strlen(argv[0]) < 1) ) {
eurephia_log(ctx, LOG_PANIC, 0, "Wrong parameters to eurephiadb-sqlite. Cannot open database.");
return 0;
}
// Connect to the database
dbc = (eDBconn *) malloc(sizeof(eDBconn)+2);
memset(dbc, 1, sizeof(eDBconn)+2);
dbc->dbname = strdup(argv[0]);
eurephia_log(ctx, LOG_INFO, 1, "Opening database '%s'", dbc->dbname);
rc = sqlite3_open(argv[0], (void *) &dbc->dbhandle);
if( rc ) {
eurephia_log(ctx, LOG_PANIC, 0, "Could not open database '%s'", dbc->dbname);
free_nullsafe(dbc->dbname);
free_nullsafe(dbc);
return 0;
}
dbc->config = NULL;
ctx->dbc = dbc;
// Load configuration parameters into memory
eurephia_log(ctx, LOG_INFO, 1, "Reading config from database (openvpn_config)");
res = sqlite_query(ctx, "SELECT datakey, dataval FROM openvpn_config");
if( res != NULL ) {
int i = 0;
eurephiaVALUES *cfg = NULL;
cfg = eCreate_value_space(ctx, 11);
if( cfg == NULL ) {
eurephia_log(ctx, LOG_FATAL, 0, "Could not allocate memory for config variables");
sqlite_free_results(res);
return 0;
}
for( i = 0; i < sqlite_get_numtuples(res); i++ ) {
eAdd_value(ctx, cfg, sqlite_get_value(res, i, 0), sqlite_get_value(res, i, 1));
}
sqlite_free_results(res);
ctx->dbc->config = cfg;
}
return 1;
}
// Disconnect from the database
void eDBdisconnect(eurephiaCTX *ctx)
{
eDBconn *dbc = NULL;
DEBUG(ctx, 20, "Function call: eDBdisconnect(ctx)");
if( ctx->dbc == NULL ) {
eurephia_log(ctx, LOG_WARNING, 0, "Database not open, cannot close database.");
return;
}
dbc = ctx->dbc;
eurephia_log(ctx, LOG_INFO, 1, "Closing database '%s'", dbc->dbname);
// Close database connection
sqlite3_close((sqlite3 *) dbc->dbhandle);
free_nullsafe(dbc->dbname);
dbc->dbhandle = NULL;
// Free up config memory
eFree_values(ctx, dbc->config);
free_nullsafe(dbc);
ctx->dbc = NULL;
}
// Authenticate certificate ... returns certid (certificate ID) on success,
// 0 if not found or -1 if certificate is blacklisted
int eDBauth_TLS(eurephiaCTX *ctx, const char *org, const char *cname, const char *email,
const char *digest, const char *depth)
{
dbresult *res = NULL;
int certid = 0;
char *blid = NULL;
DEBUG(ctx, 20, "Function call: eDBauth_TLS(ctx, '%s', '%s', '%s', '%s', %s)",
org, cname, email, digest, depth);
// Check if certificate is valid, and not too many attempts has been tried with the given certificate
res = sqlite_query(ctx,
"SELECT cert.certid, blid "
" FROM openvpn_certificates cert"
" LEFT JOIN openvpn_blacklist bl USING(digest)"
" WHERE organisation='%q' AND common_name='%q' "
" AND email='%q' AND depth='%q' AND cert.digest='%q'%c",
org, cname, email, depth, digest, 0);
if( res != NULL ) {
certid = atoi_nullsafe(sqlite_get_value(res, 0, 0));
blid = strdup_nullsafe(sqlite_get_value(res, 0, 1));
sqlite_free_results(res);
// Check if the certificate is blacklisted or not. blid != NULL when blacklisted
if( blid != NULL ) {
// If the certificate or IP is blacklisted, update status and deny access.
eurephia_log(ctx, LOG_WARNING, 0,
"Attempt with BLACKLISTED certificate (certid %i)", certid);
update_attempts(ctx, blid);
certid = -1;
}
free_nullsafe(blid);
} else {
eurephia_log(ctx, LOG_FATAL, 0, "Could not look up certificate information");
}
DEBUG(ctx, 20, "Result function call: eDBauth_TLS(ctx, '%s', '%s', '%s', '%s', %s) - %i",
org, cname, email, digest, depth, certid);
return certid;
}
// Authenticate user, using username, password and certid as authentication parameters
// returns -1 if authentication fails. Returns 0 if user account is not found.
int eDBauth_user(eurephiaCTX *ctx, const int certid, const char *username, const char *passwd)
{
dbresult *res = NULL;
char *crpwd = NULL, *activated = NULL, *deactivated = NULL, *blid_uname = NULL, *blid_cert;
int uicid = 0, uid = 0, pwdok = 0;
DEBUG(ctx, 20, "Function call: eDBauth_user(ctx, %i, '%s','xxxxxxxx')", certid, username);
// Generate SHA512 hash of password, used for password auth
crpwd = passwdhash(passwd);
res = sqlite_query(ctx,
"SELECT uicid, ou.uid, activated, deactivated, bl1.blid, bl2.blid, "
" (password = '%s') AS pwdok"
" FROM openvpn_users ou"
" JOIN openvpn_usercerts uc USING(uid) "
" LEFT JOIN openvpn_blacklist bl1 ON( ou.username = bl1.username) "
" LEFT JOIN (SELECT blid, certid "
" FROM openvpn_certificates "
" JOIN openvpn_blacklist USING(digest)) bl2 ON(uc.certid = bl2.certid)"
" WHERE uc.certid = '%i' AND ou.username = '%q'",
crpwd, certid, username);
memset(crpwd, 0, strlen_nullsafe(crpwd));
free_nullsafe(crpwd);
if( res == NULL ) {
eurephia_log(ctx, LOG_FATAL, 0,
"Could not lookup user in database (certid %i, username '%s'", certid, username);
return 0;
}
if( sqlite_get_numtuples(res) == 1 ) {
uid = atoi_nullsafe(sqlite_get_value(res, 0, 1));
activated = sqlite_get_value(res, 0, 2);
deactivated = sqlite_get_value(res, 0, 3);
blid_uname = sqlite_get_value(res, 0, 4);
blid_cert = sqlite_get_value(res, 0, 5);
pwdok = atoi_nullsafe(sqlite_get_value(res, 0, 6));
if( blid_uname != NULL ) {
eurephia_log(ctx, LOG_WARNING, 0, "User account is BLACKLISTED (uid: %i, %s)",
uid, username);
uicid = -1;
} else if( blid_cert != NULL ) {
eurephia_log(ctx, LOG_WARNING, 0,
"User account linked with a BLACKLISTED certificate "
"(uid: %i, %s) - certid: %s",
uid, username, certid);
uicid = -1;
} else if( activated == NULL ) {
eurephia_log(ctx, LOG_WARNING, 0, "User account is not activated (uid: %i, %s)",
uid, username);
uicid = -1;
} else if( deactivated != NULL ) {
eurephia_log(ctx, LOG_WARNING, 0, "User account is deactivated (uid: %i, %s)",
uid, username);
uicid = -1;
} else if( pwdok != 1 ) {
eurephia_log(ctx, LOG_WARNING, 0,"Authentication failed for user '%s'. Wrong password.",
username);
uicid = -1;
} else {
uicid = atoi_nullsafe(sqlite_get_value(res, 0, 0));
}
} else {
eurephia_log(ctx, LOG_WARNING, 0, "Authentication failed for user '%s'. "
"Could not find user or user-certificate link.", username);
uicid = 0;
}
sqlite_free_results(res);
DEBUG(ctx, 20, "Result function call: eDBauth_user(ctx, %i, '%s','xxxxxxxx') - %i",
certid, username, uicid);
return uicid;
}
// Retrieve the user id from openvpn_usercerts, based on certid and username
int eDBget_uid(eurephiaCTX *ctx, const int certid, const char *username)
{
dbresult *res = NULL;
int ret = 0;
DEBUG(ctx, 20, "Function call: eDBget_uid(ctx, %i, '%s')", certid, username);
res = sqlite_query(ctx,
"SELECT uid "
" FROM openvpn_usercerts "
" JOIN openvpn_users USING (uid) "
" WHERE certid = '%i' AND username = '%q'",
certid, username);
if( (res == NULL) || (sqlite_get_numtuples(res) != 1) ) {
eurephia_log(ctx, LOG_FATAL, 0, "Could not lookup userid for user '%s'", username);
ret = -1;
} else {
ret = atoi_nullsafe(sqlite_get_value(res, 0, 0));
}
sqlite_free_results(res);
return ret;
}
// If function returns true(1) this control marks it as blacklisted
int eDBblacklist_check(eurephiaCTX *ctx, const int type, const char *val)
{
dbresult *blr = NULL, *atpr = NULL;
int atpexceed = -1, blacklisted = 0;
char *atpid = NULL, *blid = NULL;
DEBUG(ctx, 20, "Function call: eDBblacklist_check(ctx, '%s', '%s')",
eDBattempt_types[type].descr, val);
blr = sqlite_query(ctx, "SELECT blid FROM openvpn_blacklist WHERE %s = '%q'",
eDBattempt_types[type].colname, val);
if( blr != NULL ) {
blid = strdup_nullsafe(sqlite_get_value(blr, 0, 0));
sqlite_free_results(blr);
blr = NULL;
if( blid != NULL ) {
eurephia_log(ctx, LOG_WARNING, 0, "Attempt from blacklisted %s: %s",
eDBattempt_types[type].descr, val);
blacklisted = 1; // [type] is blacklisted
}
// Update attempt information
update_attempts(ctx, blid);
} else {
eurephia_log(ctx, LOG_FATAL, 0, "Quering openvpn_blacklist for blacklisted %s failed",
eDBattempt_types[type].descr);
}
if( blacklisted == 0 ) {
// Check if this [type] has been attempted earlier - if it has reaced the maximum
// attempt limit, blacklist it
atpr = sqlite_query(ctx,
"SELECT atpid, attempts >= %q FROM openvpn_attempts WHERE %s = '%q'",
defaultValue(eGet_value(ctx->dbc->config, eDBattempt_types[type].allow_cfg),
eDBattempt_types[type].default_value),
eDBattempt_types[type].colname, val);
if( atpr != NULL ) {
atpid = strdup_nullsafe(sqlite_get_value(atpr, 0, 0));
atpexceed = atoi_nullsafe(sqlite_get_value(atpr, 0, 1));
sqlite_free_results(atpr);
atpr = NULL;
// If [type] has reached attempt limit and it is not black listed, black list it
if( (atpexceed > 0) && (blid == NULL) ) {
eurephia_log(ctx, LOG_WARNING, 0,
"%s got BLACKLISTED due to too many failed attempts: %s",
eDBattempt_types[type].descr, val);
blr = sqlite_query(ctx,
"INSERT INTO openvpn_blacklist (%s) VALUES ('%q')",
eDBattempt_types[type].colname, val);
if( blr == NULL ) {
eurephia_log(ctx, LOG_CRITICAL, 0,
"Could not blacklist %s (%s)",
eDBattempt_types[type].descr, val);
}
sqlite_free_results(blr);
blacklisted = 1; // [type] is blacklisted
}
free_nullsafe(atpid);
} else {
eurephia_log(ctx, LOG_CRITICAL, 0, "Quering openvpn_attempts for blacklisted %s failed",
eDBattempt_types[type].descr);
}
free_nullsafe(atpr);
}
free_nullsafe(blid);
DEBUG(ctx, 20, "Result - function call: eDBblacklist_check(ctx, '%s', '%s') - %i",
eDBattempt_types[type].descr, val, blacklisted);
return blacklisted;
}
// Register a failed attempt of authentication or IP address has been tried to many times
void eDBregister_attempt(eurephiaCTX *ctx, int type, int mode, const char *value) {
dbresult *res;
char *id = NULL, *atmpt_block = NULL, *blid = NULL;
int attempts = 0;
DEBUG(ctx, 20, "Function call: eDBregister_attempt(ctx, %s, %s, '%s')",
eDBattempt_types[type].colname,
(mode == ATTEMPT_RESET ? "ATTEMPT_RESET" : "ATTEMPT_REGISTER"),
value);
//
// openvpn_attempts
//
res = sqlite_query(ctx,
"SELECT atpid, attempts > %s, blid, attempts "
" FROM openvpn_attempts "
" LEFT JOIN openvpn_blacklist USING(%s)"
" WHERE %s = '%q'",
defaultValue(eGet_value(ctx->dbc->config, eDBattempt_types[type].allow_cfg),
eDBattempt_types[type].default_value),
eDBattempt_types[type].colname,
eDBattempt_types[type].colname, value);
if( res == NULL ) {
eurephia_log(ctx, LOG_FATAL, 0, "Could not look up atpid in openvpn_attempts");
return;
}
attempts = atoi_nullsafe(sqlite_get_value(res, 0, 3));
// If we are asked to reset the attempt counter and we do not find any attempts, exit here
if( (mode == ATTEMPT_RESET) && ((sqlite_get_numtuples(res) == 0) || (attempts == 0))) {
sqlite_free_results(res);
return;
}
id = strdup_nullsafe(sqlite_get_value(res, 0, 0));
atmpt_block = strdup_nullsafe(sqlite_get_value(res, 0, 1));
blid = strdup_nullsafe(sqlite_get_value(res, 0, 2));
sqlite_free_results(res);
if( (id == NULL) && (mode == ATTEMPT_REGISTER) ) {
// Only insert record when we are in registering mode
res = sqlite_query(ctx, "INSERT INTO openvpn_attempts (%s, attempts) VALUES ('%q', 1)",
eDBattempt_types[type].colname, value);
} else if( id != NULL ){
// if a attempt record exists, update it according to mode
switch( mode ) {
case ATTEMPT_RESET:
res = sqlite_query(ctx,
"UPDATE openvpn_attempts "
" SET attempts = 0 "
" WHERE atpid = '%q'", id);
break;
default:
res = sqlite_query(ctx,
"UPDATE openvpn_attempts "
" SET last_attempt = CURRENT_TIMESTAMP, attempts = attempts + 1"
" WHERE atpid = '%q'", id);
break;
}
}
if( res == NULL ) {
eurephia_log(ctx, LOG_CRITICAL, 0,
"Could not update openvpn_attempts for %s = %s",
eDBattempt_types[type].colname, value);
}
sqlite_free_results(res);
// If attempts have exceeded attempt limit, blacklist it immediately if not already registered
if( (mode == ATTEMPT_REGISTER)
&& (blid == NULL) && (atmpt_block != NULL) && (atoi_nullsafe(atmpt_block) > 0) ) {
eurephia_log(ctx, LOG_WARNING, 0, "Blacklisting %s due to too many attempts: %s",
eDBattempt_types[type].descr, value);
res = sqlite_query(ctx, "INSERT INTO openvpn_blacklist (%s) VALUES ('%q')",
eDBattempt_types[type].colname, value);
if( res == NULL ) {
eurephia_log(ctx, LOG_CRITICAL, 0,
"Could not blacklist %s: %s", eDBattempt_types[type].descr, value);
}
sqlite_free_results(res);
}
free_nullsafe(id);
free_nullsafe(atmpt_block);
free_nullsafe(blid);
}
// Register a successful authentication
int eDBregister_login(eurephiaCTX *ctx, eurephiaSESSION *skey, const int certid, const int uid,
const char *proto, const char *remipaddr, const char *remport,
const char *vpnipaddr, const char *vpnipmask)
{
dbresult *res = NULL;
DEBUG(ctx, 20, "Function call: eDBregister_login(ctx, '%s', %i, %i, '%s','%s','%s','%s','%s')",
skey->sessionkey, certid, uid, proto, remipaddr, remport, vpnipaddr, vpnipmask);
if( skey->sessionstatus != SESSION_NEW ) {
eurephia_log(ctx, LOG_ERROR, 5, "Not a new session, will not register it again");
return 1;
}
res = sqlite_query(ctx,
"INSERT INTO openvpn_lastlog (uid, certid, "
" protocol, remotehost, remoteport,"
" vpnipaddr, vpnipmask,"
" sessionstatus, sessionkey, login) "
"VALUES (%i, %i, '%q','%q','%q','%q','%q', 1,'%q', CURRENT_TIMESTAMP)",
uid, certid, proto, remipaddr, remport, vpnipaddr, vpnipmask, skey->sessionkey);
if( res == NULL ) {
eurephia_log(ctx, LOG_FATAL, 0, "Could not insert new session into openvpn_lastlog");
return 0;
}
sqlite_free_results(res);
skey->sessionstatus = SESSION_REGISTERED;
return 1;
}
// Register the MAC address of the VPN adapter of the user.
int eDBregister_vpnmacaddr(eurephiaCTX *ctx, eurephiaSESSION *session, const char *macaddr)
{
dbresult *res = NULL;
DEBUG(ctx, 20, "Function call: eDBregister_vpnmacaddr(ctx, '%s', '%s')",
session->sessionkey, macaddr);
if( macaddr == NULL ) {
eurephia_log(ctx, LOG_FATAL, 0, "No MAC address was given to save");
return 0;
}
// Register MAC address into history table
res = sqlite_query(ctx, "INSERT INTO openvpn_macaddr_history (sessionkey, macaddr) VALUES ('%q','%q')",
session->sessionkey, macaddr);
if( res == NULL ) {
eurephia_log(ctx, LOG_FATAL, 0, "Failed to log new MAC address for session");
return 0;
}
sqlite_free_results(res);
// Update lastlog to reflect last used MAC address for the session
res = sqlite_query(ctx,
"UPDATE openvpn_lastlog SET sessionstatus = 2, macaddr = '%q' "
" WHERE sessionkey = '%q' AND sessionstatus = 1", macaddr, session->sessionkey);
if( res == NULL ) {
eurephia_log(ctx, LOG_FATAL, 0, "Could not update lastlog with new MAC address for session");
return 0;
}
sqlite_free_results(res);
// Save the MAC address in the session values register - needed for the destroy session
if( eDBset_session_value(ctx, session, "macaddr", macaddr) == 0 ) {
eurephia_log(ctx, LOG_FATAL, 0, "Could not save MAC address into session variables");
return 0;
}
return 1;
}
// Register the user as logged out
int eDBregister_logout(eurephiaCTX *ctx, eurephiaSESSION *skey,
const char *bytes_sent, const char *bytes_received, const char *duration)
{
dbresult *res = NULL;
DEBUG(ctx, 20, "Function call: eDBregister_logout(ctx, '%s', %s, %s)",
skey->sessionkey, bytes_sent, bytes_received);
res = sqlite_query(ctx,
"UPDATE openvpn_lastlog "
" SET sessionstatus = 3, logout = CURRENT_TIMESTAMP, "
" bytes_sent = '%i', bytes_received = '%i', session_duration = '%i' "
" WHERE sessionkey = '%q' AND sessionstatus = 2",
atoi_nullsafe(bytes_sent), atoi_nullsafe(bytes_received),
atoi_nullsafe(duration), skey->sessionkey);
if( res == NULL ) {
eurephia_log(ctx, LOG_FATAL, 0, "Could not update lastlog with logout information (%s)",
skey->sessionkey);
return 0;
}
sqlite_free_results(res);
skey->sessionstatus = SESSION_LOGGEDOUT;
return 1;
}
// Retrieve a session key from openvpn_sessionkeys if it is a current session. Session seed is used
// as criteria
char *eDBget_sessionkey_seed(eurephiaCTX *ctx, const char *sessionseed) {
dbresult *res = NULL;
char *skey = NULL;
DEBUG(ctx, 20, "eDBget_sessionkey(ctx, '%s')", sessionseed);
if( sessionseed == NULL ) {
eurephia_log(ctx, LOG_FATAL, 1,
"eDBget_sessionkey: No session seed given - cannot locate sessionkey");
return NULL;
}
res = sqlite_query(ctx,
"SELECT sessionkey "
" FROM openvpn_sessionkeys "
" JOIN openvpn_lastlog USING (sessionkey)"
" WHERE sessionstatus IN (1,2)"
" AND sessionseed = '%q'",
sessionseed);
if( res == NULL ) {
eurephia_log(ctx, LOG_FATAL, 0,"Could not retrieve sessionkey from openvpn_sessionkeys (%s)",
sessionseed);
return NULL;
}
if( sqlite_get_numtuples(res) == 1 ) {
skey = strdup_nullsafe(sqlite_get_value(res, 0, 0));
} else {
skey = NULL;
}
sqlite_free_results(res);
return skey;
}
char *eDBget_sessionkey_macaddr(eurephiaCTX *ctx, const char *macaddr) {
dbresult *res = NULL;
char *skey = NULL;
// Find sessionkey from MAC address
res = sqlite_query(ctx,
"SELECT sessionkey "
" FROM openvpn_sessions "
" JOIN openvpn_lastlog USING (sessionkey)"
" WHERE sessionstatus = 3 "
" AND datakey = 'macaddr'"
" AND dataval = '%q'", macaddr);
if( res == NULL ) {
eurephia_log(ctx, LOG_FATAL, 0,
"Could not remove session from database (MAC addr: %s)", macaddr);
return 0;
}
skey = strdup_nullsafe(sqlite_get_value(res, 0, 0));
sqlite_free_results(res);
return skey;
}
// Function returns true(1) if session key is unique
int eDBcheck_sessionkey_uniqueness(eurephiaCTX *ctx, const char *seskey) {
dbresult *res;
int uniq = 0;
DEBUG(ctx, 20, "eDBcheck_sessionkey_uniqueness(ctx, '%s')", seskey);
if( seskey == NULL ) {
eurephia_log(ctx, LOG_FATAL, 1,
"eDBcheck_sessionkey_uniqness: Invalid session key given");
return 0;
}
switch( ctx->context_type ) {
case ECTX_NO_PRIVILEGES:
return 0;
break;
case ECTX_ADMIN_CONSOLE:
case ECTX_ADMIN_WEB:
res = sqlite_query(ctx,
"SELECT count(sessionkey) = 0 "
"FROM eurephia_adminlog WHERE sessionkey = '%q'", seskey);
break;
case ECTX_PLUGIN_AUTH:
default:
res = sqlite_query(ctx,
"SELECT count(sessionkey) = 0 "
"FROM openvpn_lastlog WHERE sessionkey = '%q'", seskey);
break;
}
if( res == NULL ) {
eurephia_log(ctx, LOG_FATAL, 0,
"eDBcheck_sessionkey_uniqness: Could not check uniqueness of sessionkey");
return 0;
}
uniq = atoi_nullsafe(sqlite_get_value(res, 0, 0));
sqlite_free_results(res);
return uniq;
}
// register a link between a short-term session seed and a long-term session key
int eDBregister_sessionkey(eurephiaCTX *ctx, const char *seed, const char *seskey) {
dbresult *res;
DEBUG(ctx, 20, "eDBregister_sessionkey(ctx, '%s', '%s')", seed, seskey);
if( (seed == NULL) || (seskey == NULL) ) {
eurephia_log(ctx, LOG_FATAL, 1,
"eDBregister_sessionkey: Invalid session seed or session key given");
return 0;
}
res = sqlite_query(ctx,
"INSERT INTO openvpn_sessionkeys (sessionseed, sessionkey) VALUES('%q','%q')",
seed, seskey);
if( res == NULL ) {
eurephia_log(ctx, LOG_FATAL, 0,
"eDBregister_sessionkey: Error registering sessionkey into openvpn_sessionkeys");
return 0;
}
sqlite_free_results(res);
return 1;
}
// remove a session seed/session key link from openvpn_sessionkeys
int eDBremove_sessionkey(eurephiaCTX *ctx, const char *seskey) {
dbresult *res;
DEBUG(ctx, 20, "eDBremove_sessionkey(ctx, '%s')", seskey);
if( seskey == NULL ) {
eurephia_log(ctx, LOG_FATAL, 1,
"eDBremove_sessionkey: Invalid session key given");
return 0;
}
res = sqlite_query(ctx, "DELETE FROM openvpn_sessionkeys WHERE sessionkey = '%q'", seskey);
if( res == NULL ) {
eurephia_log(ctx, LOG_FATAL, 0,
"eDBremove_sessionkey: Error removing sessionkey from openvpn_sessionkeys");
return 0;
}
sqlite_free_results(res);
return 1;
}
// Load session values stored in the database into a eurephiaVALUES struct (session values)
eurephiaVALUES *eDBload_sessiondata(eurephiaCTX *ctx, const char *sesskey) {
dbresult *res = NULL;
eurephiaVALUES *sessvals = NULL;
int i;
if( (ctx == NULL) || (sesskey == NULL) ) {
return NULL;
}
DEBUG(ctx, 20, "Function call: eDBload_sessiondata(ctx, '%s')", sesskey);
sessvals = eCreate_value_space(ctx, 10);
res = sqlite_query(ctx, "SELECT datakey, dataval FROM openvpn_sessions WHERE sessionkey = '%q'",
sesskey);
if( (res != NULL) || (sqlite_get_numtuples(res) > 0) ) {
for( i = 0; i < sqlite_get_numtuples(res); i++ ) {
eAdd_value(ctx, sessvals,
sqlite_get_value(res, i, 0),
sqlite_get_value(res, i, 1));
}
} else {
eurephia_log(ctx, LOG_CRITICAL, 0,
"Could not load session values for session '%s'", sesskey);
}
sqlite_free_results(res);
return sessvals;
}
// Store a new, update or delete a sessionvalue in the database
int eDBstore_session_value(eurephiaCTX *ctx, eurephiaSESSION *session, int mode, const char *key, const char *val)
{
dbresult *res = NULL;
if( session == NULL ) {
DEBUG(ctx, 20,
"Function call failed to eDBstore_session_value(ctx, ...): Non-existing session key");
return 0;
}
DEBUG(ctx, 20, "Function call: eDBstore_session_value(ctx, '%s', %i, '%s', '%s')",
session->sessionkey, mode, key, val);
switch( mode ) {
case SESSVAL_NEW:
res = sqlite_query(ctx,
"INSERT INTO openvpn_sessions (sessionkey, datakey, dataval) "
"VALUES ('%q','%q','%q')", session->sessionkey, key, val);
if( res == NULL ) {
eurephia_log(ctx, LOG_FATAL, 0,
"Could not register new session variable into database: [%s] %s = %s",
session->sessionkey, key, val);
return 0;
}
break;
case SESSVAL_UPDATE:
res = sqlite_query(ctx,
"UPDATE openvpn_sessions SET dataval = '%q' "
" WHERE sessionkey = '%q' AND datakey = '%q'",
val, session->sessionkey, key);
if( res == NULL ) {
eurephia_log(ctx, LOG_FATAL, 0, "Could not update session variable: [%s] %s = %s ",
session->sessionkey, key, val);
return 0;
}
break;
case SESSVAL_DELETE:
res = sqlite_query(ctx,
"DELETE FROM openvpn_sessions "
" WHERE sessionkey = '%q' AND datakey = '%q'",
session->sessionkey, key);
if( res == NULL ) {
eurephia_log(ctx, LOG_FATAL, 0, "Could not delete session variable: [%s] %s",
session->sessionkey, key);
return 0;
}
break;
default:
eurephia_log(ctx, LOG_FATAL, 0, "Unknown eDBstore_session_value mode '%i'", mode);
return 0;
}
sqlite_free_results(res);
return 1;
}
// Delete session information from openvpn_sessions and update openvpn_lastlog with status
int eDBdestroy_session(eurephiaCTX *ctx, eurephiaSESSION *session) {
dbresult *res = NULL;
DEBUG(ctx, 20, "Function call: eDBdestroy_session(ctx, '%s')", session->sessionkey);
if( (session == NULL) || (session->sessionkey == NULL) ) {
eurephia_log(ctx, LOG_WARNING, 1, "No active session given to be destroyed");
return 1;
}
// Update session status
res = sqlite_query(ctx,
"UPDATE openvpn_lastlog "
" SET sessionstatus = 4, session_deleted = CURRENT_TIMESTAMP "
" WHERE sessionkey = '%q' AND sessionstatus = 3", session->sessionkey);
if( res == NULL ) {
eurephia_log(ctx, LOG_FATAL, 0,
"Could not update session status in lastlog (%s))", session->sessionkey);
return 0;
}
sqlite_free_results(res);
// Delete session variables
res = sqlite_query(ctx, "DELETE FROM openvpn_sessions WHERE sessionkey = '%q'", session->sessionkey);
if( res == NULL ) {
eurephia_log(ctx, LOG_FATAL, 0,
"Could not delete session variables (%s))", session->sessionkey);
return 0;
}
sqlite_free_results(res);
// Remove the sessionkey from openvpn_sessions
if( eDBremove_sessionkey(ctx, session->sessionkey) == 0 ) {
return 0;
}
return 1;
}
char *eDBget_firewall_profile(eurephiaCTX *ctx, eurephiaSESSION *session)
{
char *ret = NULL;
dbresult *res = NULL;
DEBUG(ctx, 20, "Function call: eDBget_firewall_profile(ctx, {session}'%s')",
session->sessionkey);
res = sqlite_query(ctx,
"SELECT fw_profile "
" FROM openvpn_lastlog "
" JOIN openvpn_usercerts USING(certid, uid)"
" JOIN openvpn_accesses USING(accessprofile)"
" WHERE sessionkey = '%q'", session->sessionkey);
if( res == NULL ) {
eurephia_log(ctx, LOG_FATAL, 0, "Could not retrieve firewall profile for session '%s'",
session->sessionkey);
return NULL;
}
ret = strdup_nullsafe(sqlite_get_value(res, 0, 0));
sqlite_free_results(res);
return ret;
}
eurephiaVALUES *eDBget_blacklisted_ip(eurephiaCTX *ctx) {
eurephiaVALUES *ret = NULL;
dbresult *res = NULL;
int i = 0;
char *ip = NULL;
DEBUG(ctx, 20, "Function call: eDBget_blacklisted_ip(ctx)");
res = sqlite_query(ctx, "SELECT remoteip FROM openvpn_blacklist WHERE remoteip IS NOT NULL");
if( res == NULL ) {
eurephia_log(ctx, LOG_FATAL, 0,
"Could not retrieve blacklisted IP addresses from the database");
return NULL;
}
ret = eCreate_value_space(ctx, 21);
for( i = 0; i < sqlite_get_numtuples(res); i++ ) {
if( (ip = sqlite_get_value(res, i, 0)) != NULL ) {
eAdd_value(ctx, ret, NULL, ip);
}
}
sqlite_free_results(res);
return ret;
}
#if DRIVERAPIVERSION > 1
/*
* API Version 2 functions
*
*/
// Authenticate admin user against user database
int eDBadminAuth(eurephiaCTX *ctx, const char *req_access, const char *uname, const char *pwd) {
dbresult *res = NULL;
char *crpwd = NULL;
char *activated = NULL, *deactivated = NULL, *blid = NULL;
int uid = -1, pwok = 0;
char interface, *access = NULL;
assert(ctx != NULL);
switch( ctx->context_type ) {
case ECTX_ADMIN_CONSOLE:
interface = 'C';
break;
case ECTX_ADMIN_WEB:
interface = 'W';
break;
default:
eurephia_log(ctx, LOG_ERROR, 0, "Wrong eurephia context type (0x%04x)", ctx->context_type);
return 0;
}
if( (strlen_nullsafe(uname) < 4) || (strlen_nullsafe(pwd) < 4) ) {
eurephia_log(ctx, LOG_WARNING, 0, "User name and/or password is either null or less than 4 bytes");
return 0;
}
//
// Authenticate user and password
//
crpwd = passwdhash(pwd);
assert(crpwd != NULL);
res = sqlite_query(ctx,
"SELECT activated, deactivated, bl.blid, "
" (password = '%q') AS pwok, uid "
" FROM openvpn_users ou"
" LEFT JOIN openvpn_blacklist bl USING (username)"
" WHERE ou.username = '%q'",
crpwd, uname);
memset(crpwd, 0, strlen_nullsafe(crpwd));
free_nullsafe(crpwd);
if( res == NULL ) {
eurephia_log(ctx, LOG_FATAL, 0, "Could not authenticate user against the database");
return 0;
}
if( sqlite_get_numtuples(res) == 1 ) {
activated = sqlite_get_value(res, 0, 0);
deactivated = sqlite_get_value(res, 0, 1);
blid = sqlite_get_value(res, 0, 2);
pwok = atoi_nullsafe(sqlite_get_value(res, 0, 3));
uid = atoi_nullsafe(sqlite_get_value(res, 0, 4));
sqlite_free_results(res);
if( blid != NULL ) {
eurephia_log(ctx, LOG_WARNING, 0,
"Your user account is BLACKLISTED. You have no access.");
sqlite_free_results(res);
return 0;
}
if( activated == NULL ) {
eurephia_log(ctx, LOG_WARNING, 0, "Your user account is not yet activated.");
sqlite_free_results(res);
return 0;
}
if( deactivated != NULL ) {
eurephia_log(ctx, LOG_WARNING, 0, "Your user account is deactivated.");
sqlite_free_results(res);
return 0;
}
if( pwok != 1 ) {
eurephia_log(ctx, LOG_WARNING, 0, "Authentication failed,");
sqlite_free_results(res);
return 0;
}
// Check if access level is granted
// (SQLite do not handle advanced joins so well, so we need to
// do this check with an extra query)
res = sqlite_query(ctx,
"SELECT (count(*) = 1) AS access "
" FROM eurephia_adminaccess"
" WHERE uid = '%i' AND interface = '%c' AND access = '%q'",
uid, interface, req_access);
if( res == NULL ) {
eurephia_log(ctx, LOG_FATAL, 0, "Could not check access level");
return 0;
}
access = atoi_nullsafe(sqlite_get_value(res, 0, 0));
sqlite_free_results(res);
if( access == 0 ) {
eurephia_log(ctx, LOG_WARNING, 0, "Your account is lacking privileges for this operation");
return 0;
}
} else {
eurephia_log(ctx, LOG_WARNING, 0, "Authentication failed. No unique records found.");
sqlite_free_results(res);
return 0;
}
// If we reach this place, authentication was successful. Return users uid
return uid;
}
int eDBadminValidateSession(eurephiaCTX *ctx, const char *sesskey, const char *req_access) {
dbresult *res = NULL;
int valid = 0, access = 0, expire_time = 0;
char interface;
assert( (ctx != NULL) && (sesskey != NULL) );
switch( ctx->context_type ) {
case ECTX_ADMIN_CONSOLE:
interface = 'C';
break;
case ECTX_ADMIN_WEB:
interface = 'W';
break;
default:
eurephia_log(ctx, LOG_ERROR, 0, "Wrong eurephia context type (0x%04x)", ctx->context_type);
return 0;
}
// Check if the session is still valid (not expired) and that this session are allowed to access
// the requested access level.
expire_time = (60 * atoi_nullsafe(defaultValue(eGet_value(ctx->dbc->config, "eurephiadmin_autologout"),
"10")
)
);
res = sqlite_query(ctx,
"SELECT (strftime('%%s',CURRENT_TIMESTAMP)-strftime('%%s',last_action)) > %i AS exp,"
" (access IS NOT NULL) AS access"
" FROM eurephia_adminlog"
" LEFT JOIN eurephia_adminaccess USING(uid,interface)"
" WHERE status IN (1,2)"
" AND sessionkey = '%q'"
" AND access = '%q'",
expire_time, sesskey, req_access);
if( (res == NULL) ) {
eurephia_log(ctx, LOG_FATAL, 0, "Could not validate session");
return 0;
}
valid = (atoi_nullsafe(sqlite_get_value(res, 0, 0)) == 0);
access = (atoi_nullsafe(sqlite_get_value(res, 0, 1)) == 1);
sqlite_free_results(res);
// If still valid, update last_action
if( valid && access ) {
res = sqlite_query(ctx,
"UPDATE eurephia_adminlog"
" SET last_action = CURRENT_TIMESTAMP, status = 2"
" WHERE sessionkey = '%q'", sesskey);
if( res == NULL ) {
eurephia_log(ctx, LOG_ERROR, 0, "Could not register session activity");
}
sqlite_free_results(res);
} else {
// If not valid, register session as auto-logged out
res = sqlite_query(ctx,
"UPDATE eurephia_adminlog"
" SET logout = CURRENT_TIMESTAMP, status = %i"
" WHERE sessionkey = '%q'",
(access ? 4 : 5), sesskey);
if( res == NULL ) {
eurephia_log(ctx, LOG_ERROR, 0, "Could not register old session as logged out");
}
sqlite_free_results(res);
// Delete session variables
res = sqlite_query(ctx, "DELETE FROM openvpn_sessions WHERE sessionkey = '%q'",
sesskey);
if( res == NULL ) {
eurephia_log(ctx, LOG_ERROR, 0,
"Could not delete session variables (%s))", sesskey);
return 0;
}
sqlite_free_results(res);
if( !access ) {
eurephia_log(ctx, LOG_WARNING, 0, "Your user account is lacking privileges");
}
}
return (valid && access);
}
int eDBadminRegisterLogin(eurephiaCTX *ctx, eurephiaSESSION *session) {
dbresult *res = NULL;
char interface;
int uid;
assert((ctx != NULL) && (session != NULL));
switch( ctx->context_type ) {
case ECTX_ADMIN_CONSOLE:
interface = 'C'; break;
case ECTX_ADMIN_WEB:
interface = 'W'; break;
default:
eurephia_log(ctx, LOG_ERROR, 0, "Wrong eurephia context type (0x%04x)", ctx->context_type);
return 0;
}
// Register login into eurephia_adminlog ... uid, login, interface, sessionkey
uid = atoi_nullsafe(eGet_value(session->sessvals, "uid"));
res = sqlite_query(ctx,
"INSERT INTO eurephia_adminlog "
" (uid, interface, status, login, last_action, sessionkey) "
"VALUES ('%i','%c',1,CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, '%q')",
uid, interface, session->sessionkey);
if( !res ) {
eurephia_log(ctx, LOG_FATAL, 0, "Could not manage to register the session in the database");
return 0;
}
sqlite_free_results(res);
return 1;
}
int eDBadminLogout(eurephiaCTX *ctx, const char *sessionkey) {
dbresult *res = NULL;
assert((ctx != NULL) && (sessionkey != NULL));
// Update session as logged out
res = sqlite_query(ctx,
"UPDATE eurephia_adminlog "
" SET logout = CURRENT_TIMESTAMP, status = 3"
" WHERE sessionkey = '%q'",
sessionkey);
if( !res ) {
eurephia_log(ctx, LOG_FATAL, 0, "Could not manage to register the session as logged out");
return 0;
}
sqlite_free_results(res);
// Delete session variables
res = sqlite_query(ctx, "DELETE FROM openvpn_sessions WHERE sessionkey = '%q'", sessionkey);
if( res == NULL ) {
eurephia_log(ctx, LOG_ERROR, 0,
"Could not delete session variables (%s))", sessionkey);
return 0;
}
sqlite_free_results(res);
return 1;
}
int eDBadminConfigSet(eurephiaCTX *ctx, const char *key, const char *val) {
dbresult *res = NULL;
int found = 0;
assert((ctx != NULL) && (ctx->dbc != NULL));
res = sqlite_query(ctx, "SELECT count(*) FROM openvpn_config WHERE datakey = '%q'", key);
if( !res ) {
eurephia_log(ctx, LOG_ERROR, 0, "Could not query configuration table");
return 0;
}
found = atoi_nullsafe(sqlite_get_value(res, 0, 0));
sqlite_free_results(res);
if( found == 0 ) {
res = sqlite_query(ctx,
"INSERT INTO openvpn_config (datakey, dataval) VALUES ('%q','%q')",
key, val);
} else {
res = sqlite_query(ctx, "UPDATE openvpn_config SET dataval = '%q' WHERE datakey = '%q'",
val, key);
}
if( res == NULL ) {
eurephia_log(ctx, LOG_ERROR, 0, "Could not register configuration entry (%s = '%s'", key, val);
return 0;
}
sqlite_free_results(res);
eAdd_value(ctx, ctx->dbc->config, key, val);
return 1;
}
int eDBadminConfigDelete(eurephiaCTX *ctx, const char *key) {
dbresult *res = NULL;
assert((ctx != NULL) && (ctx->dbc != NULL));
res = sqlite_query(ctx, "DELETE FROM openvpn_config WHERE datakey = '%q'", key);
if( !res ) {
eurephia_log(ctx, LOG_ERROR, 0, "Could delete config configuration entry (%s)", key);
return 0;
}
sqlite_free_results(res);
return 1;
}
eurephiaUSERLIST *eDBgetUserList(eurephiaCTX *ctx, const int sortkey) {
return NULL;
}
eurephiaUSERINFO *eDBgetUserInfo(eurephiaCTX *ctx, eurephiaUSERINFO *srchkey) {
return NULL;
}
int eDBaddUser(eurephiaCTX *ctx, eurephiaUSERINFO *usrinf) {
return 0;
}
int eDBupdateUser(eurephiaCTX *ctx, const int uid, eurephiaUSERINFO *usrinf) {
return 0;
}
int eDBdeleteUser(eurephiaCTX *ctx, const int uid, eurephiaUSERINFO *usrinf) {
return 0;
}
eurephiaCERTLIST *eDBgetCertificateList(eurephiaCTX *ctx, const int sortkey) {
return NULL;
}
eurephiaCERTINFO *eDBgetCertificateInfo(eurephiaCTX *ctx, eurephiaCERTINFO *srchkey) {
return NULL;
}
int eDBaddCertificate(eurephiaCTX *ctx, eurephiaCERTINFO *crtinf) {
return 0;
}
int eDBdeleteCertificate(eurephiaCTX *ctx, const int uid, eurephiaCERTINFO *crtinf) {
return 0;
}
eurephiaLOGLIST *eDBgetLastlog(eurephiaCTX *ctx, eurephiaUSERINFO *usersrch, eurephiaCERTINFO *certsrch) {
return NULL;
};
eurephiaLOGLIST *eDBgetAttemptsLog(eurephiaCTX *ctx, eurephiaUSERINFO *usersrch, eurephiaCERTINFO *certsrch) {
return NULL;
};
#endif
|