Debian Codegraph @

database/eurephiadb-driver_template.c

raw
1 /* eurephia-template.c -- Main driver for eurephia authentication plugin for OpenVPN
2 *
3 *
4 * // THIS IS A TEMPLATE FOR A DATABASE DRIVER - IT WILL NOT COMPILE //
5 *
6 * Go through the code and you will find examples for SQL queries and
7 * hints how to implement the database specific code
8 *
9 * This template is based on the sqlite/eurephia-sqlite.c
10 *
11 *
12 *
13 * GPLv2 only - Copyright (C) 2008 David Sommerseth <[email protected]>
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; version 2
18 * of the License.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 *
29 */
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <dlfcn.h>
35
36 #ifndef
37 #define DRIVER_MODE
38 #endif
39 #include <eurephiadb_driver.h>
40 #include <eurephia_nullsafe.h>
41 #include <eurephia_log.h>
42 #include <eurephia_values.h>
43 #include <eurephiadb_session.h>
44 #include <passwd.h>
45
46
47 #define DRIVERVERSION "1.0"
48 #ifndef DRIVERAPIVERSION
49 # define DRIVERAPIVERSION 1 // Set this value to 2 to support admin utilities
50 #endif
51
52
53 // Mapping table - mapping attempt types from .... to database table fields
54 typedef struct {
55 char *colname;
56 char *allow_cfg;
57 char *descr;
58 } eDBattempt_types_t;
59
60
61 static const eDBattempt_types_t eDBattempt_types[] = {
62 {NULL, NULL, NULL},
63 {"remoteip\0", "allow_ipaddr_attempts\0", "IP Address\0"},
64 {"digest\0", "allow_cert_attempts\0", "Certificate\0"},
65 {"username\0", "allow_username_attempts\0", "Username\0"},
66 {NULL, NULL, NULL}
67 };
68
69 /*
70 * Driver info
71 */
72
73 const char *eDB_DriverVersion() {
74 /* WORK TO DO --- UPDATE INFO */
75 return "eurephiadb-template (v"DRIVERVERSION") David Sommerseth 2008 (C) GPLv2";
76 }
77
78
79 int eDB_DriverAPIVersion() {
80 return DRIVERAPIVERSION;
81 }
82
83
84 /*
85 * local functions
86 */
87
88
89 // Function for simplifying update of openvpn_blacklist
90 void update_attempts(eurephiaCTX *ctx, const char *blid) {
91
92 if( blid != NULL ) {
93 /* WORK TO DO -- DO SQL:
94 "UPDATE openvpn_blacklist "
95 " SET last_accessed = CURRENT_TIMESTAMP WHERE blid = %s", blid
96 */
97 if( /* SQL COMMAND FAILED */ ) {
98 eurephia_log(ctx, LOG_CRITICAL, 0,
99 "Could not update openvpn_blacklist.last_accessed for blid=%s", blid);
100 }
101 /* FREE SQL RESULT */
102 }
103 }
104
105 /*
106 * Public driver functions
107 */
108
109 // Connect to the database ... connection is stored in the eurephiaCTX context
110 int eDBconnect(eurephiaCTX *ctx, const int argc, const char **argv)
111 {
112 eDBconn *dbc = NULL;
113 int rc;
114
115 /* WORK TO DO -- Parse arguments
116 *
117 * use what ever suitable approach to parse arguments sent to the database module.
118 *
119 * The arguments being recieved is everything after the '--' (double dash)
120 * in the 'plugin' configuration line for OpenVPN.
121 *
122 * plugin eurephia-auth.so "-i my_db_module.so -L 3 -- myparam1 myparam2 myparam3"
123 *
124 * In this example the following parameters will be available here:
125 *
126 * argc = 3
127 * argv[0] = myparam1
128 * argv[1] = myparam2
129 * argv[2] = myparam3
130 *
131 * You are free to use whatever parameter syntax you would like to use.
132 */
133
134 DEBUG(ctx, 20, "Function call: eDBconnect(ctx, %i, '...')", argc, dbame);
135
136 // Connect to the database
137 dbc = (eDBconn *) malloc_nullsafe(ctx, sizeof(eDBconn)+2);
138 if( !dbc ) {
139 return 0;
140 }
141 dbc->dbname = strdup(argv[1]);
142
143 eurephia_log(ctx, LOG_INFO, 1, "Opening database '%s'", dbc->dbname);
144
145 /* WORK TO DO -- Open a database connection, and save the handle in dbc->handle */
146
147 if( /* IF CONNECTION FAILED */ ) {
148 eurephia_log(ctx, LOG_PANIC, 0, "Could not open database '%s'", dbc->dbname);
149 free_nullsafe(dbc->dbname);
150 free_nullsafe(dbc);
151 return 0;
152 }
153
154 dbc->config = NULL;
155 ctx->dbc = dbc;
156
157 // Load configuration parameters into memory
158 eurephia_log(ctx, LOG_INFO, 1, "Reading config from database (openvpn_config)");
159
160 /* WORK TO DO - DO SQL:
161 "SELECT datakey, dataval FROM openvpn_config"
162 */
163 if( /* IF WE GOT SOME RECORDS */ ) {
164 int i = 0;
165 eurephiaVALUES *cfg = NULL;
166
167 cfg = eCreate_value_space(ctx, 11);
168 if( cfg == NULL ) {
169 eurephia_log(ctx, LOG_FATAL, 0, "Could not allocate memory for config variables");
170 /* FREE SQL RESULT */
171 return 0;
172 }
173
174 for( /* LOOP THROUGH ALL RECORDS, AND ADD THEM TO THE cfg VARIABLE */ ) {
175 eAdd_value(ctx, cfg,
176 /* GET datakey FROM SQL RESULT */,
177 /* GET dataval FROM SQL RESULT */);
178 }
179 /* FREE SQL RESULT */
180 ctx->dbc->config = cfg;
181 }
182 return 1;
183 }
184
185 // Disconnect from the database
186 void eDBdisconnect(eurephiaCTX *ctx)
187 {
188 eDBconn *dbc = NULL;
189
190 DEBUG(ctx, 20, "Function call: eDBdisconnect(ctx)");
191
192 if( ctx->dbc == NULL ) {
193 eurephia_log(ctx, LOG_WARNING, 0, "Database not open, cannot close database.");
194 return;
195 }
196
197 dbc = ctx->dbc;
198 eurephia_log(ctx, LOG_INFO, 1, "Closing database '%s'", dbc->dbname);
199
200 /* WORK TO DO: Close database connection using dbc->dbhandle */
201
202
203 free_nullsafe(dbc->dbname);
204 dbc->dbhandle = NULL;
205
206 // Free up config memory
207 eFree_values(ctx, dbc->config);
208 free_nullsafe(dbc);
209 ctx->dbc = NULL;
210 }
211
212
213 // Authenticate certificate ... returns certid (certificate ID) on success,
214 // 0 if not found or -1 if certificate is blacklisted
215 int eDBauth_TLS(eurephiaCTX *ctx, const char *org, const char *cname, const char *email,
216 const char *digest, const char *depth)
217 {
218 int certid = 0;
219 char *blid = NULL;
220
221 DEBUG(ctx, 20, "Function call: eDBauth_TLS(ctx, '%s', '%s', '%s', '%s', %s)",
222 org, cname, email, digest, depth);
223
224 // Check if certificate is valid, and not too many attempts has been tried with the given certificate
225 /* WORK TO DO -- DO SQL:
226 "SELECT cert.certid, blid "
227 " FROM openvpn_certificates cert"
228 " LEFT JOIN openvpn_blacklist bl USING(digest)"
229 " WHERE organisation='%q' AND common_name='%q' "
230 " AND email='%q' AND depth='%q' AND cert.digest='%q'%c",
231 org, cname, email, depth, digest
232 */
233
234 if( /* IF WE GOT A RESULT */ ) {
235 certid = atoi_nullsafe(/* GET cert.certid FROM SQL RESULT */);
236 blid = atoi_nullsafe(/* GET blid FROM SQL RESULT */);
237 /* FREE SQL RESULT */
238
239 // Check if the certificate is blacklisted or not. blid != NULL when blacklisted
240 if( blid != NULL ) {
241 // If the certificate or IP is blacklisted, update status and deny access.
242 eurephia_log(ctx, LOG_WARNING, 0,
243 "Attempt with BLACKLISTED certificate (certid %i)", certid);
244 update_attempts(ctx, blid);
245 certid = -1;
246 }
247 free_nullsafe(blid);
248 } else {
249 eurephia_log(ctx, LOG_FATAL, 0, "Could not look up certificate information");
250 }
251
252 DEBUG(ctx, 20, "Result function call: eDBauth_TLS(ctx, '%s', '%s', '%s', '%s', %s) - %i",
253 org, cname, email, digest, depth, certid);
254
255 return certid;
256 }
257
258 // Authenticate user, using username, password and certid as authentication parameters
259 // returns -1 if authentication fails. Returns 0 if user account is not found.
260 int eDBauth_user(eurephiaCTX *ctx, const int certid, const char *username, const char *passwd)
261 {
262 char *crpwd = NULL, *activated = NULL, *deactivated = NULL, *blid_uname = NULL, *blid_cert;
263 int uicid = 0, uid = 0, pwdok = 0;
264
265 DEBUG(ctx, 20, "Function call: eDBauth_user(ctx, %i, '%s','xxxxxxxx')", certid, username);
266
267
268 // Generate SHA512 hash of password, used for password auth
269 crpwd = passwdhash(passwd);
270
271 /* WORK TO DO -- DO SQL:
272 "SELECT uicid, ou.uid, activated, deactivated, bl1.blid, bl2.blid, "
273 " (password = '%s') AS pwdok"
274 " FROM openvpn_users ou"
275 " JOIN openvpn_usercerts uc USING(uid) "
276 " LEFT JOIN openvpn_blacklist bl1 ON( ou.username = bl1.username) "
277 " LEFT JOIN (SELECT blid, certid "
278 " FROM openvpn_certificates "
279 " JOIN openvpn_blacklist USING(digest)) bl2 ON(uc.certid = bl2.certid)"
280 " WHERE uc.certid = '%i' AND ou.username = '%q'",
281 crpwd, certid, username
282 */
283 free_nullsafe(crpwd);
284 if( /* IF NO RESULT WAS RETURNED */ ) {
285 eurephia_log(ctx, LOG_FATAL, 0,
286 "Could not look up user in database (certid %i, username '%s'", certid, username);
287 return 0;
288 }
289
290 if( /* CHECK THAT WE ONLY GOT 1 RECORD*/ ) {
291 uid = atoi_nullsafe(/* RETRIEVE uid FROM SQL*/);
292 activated = ;/* GET activated FIELD FROM SQL RESULT */
293 deactivated = ;/* GET deactivated FIELD FROM SQL RESULT */
294 blid_uname = ;/* GET bl1.blid FIELD FROM SQL RESULT */
295 blid_cert = ;/* GET bl2.blid FIELD FROM SQL RESULT */
296 pwdok = atoi_nullsafe(/* GET pwdok FIELD FROM SQL RESULT */);
297
298 if( blid_uname != NULL ) {
299 eurephia_log(ctx, LOG_WARNING, 0,
300 "User account is BLACKLISTED (uid: %i, %s)",
301 uid, username);
302 uicid = -1;
303 } else if( blid_cert != NULL ) {
304 eurephia_log(ctx, LOG_WARNING, 0,
305 "User account linked with a BLACKLISTED certificate "
306 "(uid: %i, %s) - certid: %s",
307 uid, username, certid);
308 uicid = -1;
309 } else if( activated == NULL ) {
310 eurephia_log(ctx, LOG_WARNING, 0,
311 "User account is not activated (uid: %i, %s)",
312 uid, username);
313 uicid = -1;
314 } else if( deactivated != NULL ) {
315 eurephia_log(ctx, LOG_WARNING, 0,
316 "User account is deactivated (uid: %i, %s)",
317 uid, username);
318 uicid = -1;
319 } else if( pwdok != 1 ) {
320 eurephia_log(ctx, LOG_WARNING, 0,
321 "Authentication failed for user '%s'. Wrong password.",
322 username);
323 uicid = -1;
324
325 } else {
326 uicid = atoi_nullsafe(/*GET uicid FIELD FROM SQL RESULT */);
327 }
328 } else {
329 eurephia_log(ctx, LOG_WARNING, 0, "Authentication failed for user '%s'. "
330 "Could not find user or user-certificate link.", username);
331 uicid = 0;
332 }
333 /* FREE SQL RESULT */
334
335 DEBUG(ctx, 20, "Result function call: eDBauth_user(ctx, %i, '%s','xxxxxxxx') - %i",
336 certid, username, uicid);
337
338 return uicid;
339 }
340
341 // Retrieve the user id from openvpn_usercerts, based on certid and username
342 int eDBget_uid(eurephiaCTX *ctx, const int certid, const char *username)
343 {
344 int ret = 0;
345
346 DEBUG(ctx, 20, "Function call: eDBget_uid(ctx, %i, '%s')", certid, username);
347
348 /* WORK TO DO -- DO SQL:
349 "SELECT uid "
350 " FROM openvpn_usercerts "
351 " JOIN openvpn_users USING (uid) "
352 " WHERE certid = '%i' AND username = '%q'",
353 certid, username
354 */
355 if( /* IF NO RESULT FOUND */ || /* OR WE GOT MORE THAN 1 RECORD */ ) {
356 eurephia_log(ctx, LOG_FATAL, 0, "Could not lookup userid for user '%s'", username);
357 ret = -1;
358 } else {
359 ret = atoi_nullsafe(/* GET uid FIELD FROM SQL RESULT */);
360 }
361 /* FREE SQL RESULT */
362
363 return ret;
364 }
365
366
367 // If function returns true(1) this control marks it as blacklisted
368 int eDBblacklist_check(eurephiaCTX *ctx, const int type, const char *val)
369 {
370 int atpexceed = -1, blacklisted = 0;
371 char *atpid = NULL, *blid = NULL;
372
373 DEBUG(ctx, 20, "Function call: eDBblacklist_check(ctx, '%s', '%s')",
374 eDBattempt_types[type].descr, val);
375
376 /* WORK TO DO -- DO SQL:
377 "SELECT blid FROM openvpn_blacklist WHERE %s = '%q'",
378 eDBattempt_types[type].colname, val
379 */
380 if( /* IF RECORDS WAS FOUND */ ) {
381 blid = strdup_nullsafe(/* GET blid FIELD FROM SQL RESULT */);
382 /* FREE SQL RESULT */
383
384 if( blid != NULL ) { // If we found a blacklisted record
385 eurephia_log(ctx, LOG_WARNING, 0, "Attempt from blacklisted %s: %s",
386 eDBattempt_types[type].descr, val);
387 blacklisted = 1; // [type] is blacklisted
388 }
389 // Update attempt information
390 update_attempts(ctx, blid);
391 } else {
392 eurephia_log(ctx, LOG_FATAL, 0, "Querying openvpn_blacklist for blacklisted %s failed",
393 eDBattempt_types[type].descr);
394 }
395
396 if( blacklisted == 0 ) {
397 // Check if this [type] has been attempted earlier - if it has reaced the maximum
398 // attempt limit, blacklist it
399
400 /* WORK TO DO -- DO SQL:
401 "SELECT atpid, attempts >= %q FROM openvpn_attempts WHERE %s = '%q'",
402 eGet_value(ctx->dbc->config, eDBattempt_types[type].allow_cfg),
403 eDBattempt_types[type].colname, val
404 */
405 if( /* IF WE FOUND RECORDS */ ) {
406 atpid = strdup_nullsafe(/* GET atpid FROM SQL RESULT */);
407 atpexceed = atoi_nullsafe(/* GET "attempts >= %q" FROM SQL RESULT */);
408 /* FREE SQL RESULT */
409
410 // If [type] has reached attempt limit and it is not black listed, black list it
411 if( (atpexceed > 0) && (blid == NULL) ) {
412 eurephia_log(ctx, LOG_WARNING, 0,
413 "%s got BLACKLISTED due to too many failed attempts: %s",
414 eDBattempt_types[type].descr, val);
415 /* WORK TO DO -- DO SQL
416 "INSERT INTO openvpn_blacklist (%s) VALUES ('%q')",
417 eDBattempt_types[type].colname, val
418 */
419 if( /* IF SQL QUERY FAILED */ ) {
420 eurephia_log(ctx, LOG_CRITICAL, 0,
421 "Could not blacklist %s (%s)",
422 eDBattempt_types[type].descr, val);
423 }
424 /* FREE SQL RESULT */
425 blacklisted = 1; // [type] is blacklisted
426 }
427 free_nullsafe(atpid);
428 } else {
429 eurephia_log(ctx, LOG_CRITICAL, 0,
430 "Querying openvpn_attempts for blacklisted %s failed",
431 eDBattempt_types[type].descr);
432 }
433 free_nullsafe(atpr);
434 }
435 free_nullsafe(blid);
436
437 DEBUG(ctx, 20, "Result - function call: eDBblacklist_check(ctx, '%s', '%s') - %i",
438 eDBattempt_types[type].descr, val, blacklisted);
439
440 return blacklisted;
441 }
442
443 // Register a failed attempt of authentication or IP address has been tried to many times
444 void eDBregister_attempt(eurephiaCTX *ctx, int type, int mode, const char *value)
445 {
446 char *id = NULL, *atmpt_block = NULL, *blid = NULL;
447 int attempts = 0;
448
449 DEBUG(ctx, 20, "Function call: eDBregister_attempt(ctx, %s, %s, '%s')",
450 eDBattempt_types[type].colname,
451 (mode == ATTEMPT_RESET ? "ATTEMPT_RESET" : "ATTEMPT_REGISTER"),
452 value);
453
454 //
455 // openvpn_attempts
456 //
457 /* WORK TO DO -- DO SQL
458 "SELECT atpid, attempts > %s, blid, attempts "
459 " FROM openvpn_attempts "
460 " LEFT JOIN openvpn_blacklist USING(%s)"
461 " WHERE %s = '%q'",
462 eGet_value(ctx->dbc->config, eDBattempt_types[type].allow_cfg),
463 eDBattempt_types[type].colname,
464 eDBattempt_types[type].colname, value
465 */
466 if( /* IF SQL QUERY FAILED */ ) {
467 eurephia_log(ctx, LOG_FATAL, 0, "Could not look up atpid in openvpn_attempts");
468 return;
469 }
470
471 attempts = atoi_nullsafe(/* GET attempts FROM SQL RESULT */);
472 // If we are asked to reset the attempt counter and we do not find any attempts, exit here
473 if( (mode == ATTEMPT_RESET) && (/* IF NO RECORDS WHERE FOUND OR attempts == 0 */) ) {
474 /* FREE SQL RESULT */
475 return;
476 }
477 id = strdup_nullsafe(/* GET atpid FROM SQL RESULT */);
478 atmpt_block = strdup_nullsafe(/* GET "attempts > %s" FROM SQL RESULT */);
479 blid = strdup_nullsafe(/* GET blid FROM SQL RESULT */);
480
481 /* FREE SQL RESULT */
482
483
484 if( (id == NULL) && (mode == ATTEMPT_REGISTER) ) {
485 // Only insert record when we are in registering mode
486
487 /* WORK TO DO -- DO SQL:
488 "INSERT INTO openvpn_attempts (%s, attempts) VALUES ('%q', 1)",
489 eDBattempt_types[type].colname, value);
490
491 Result check comes later ...
492 */
493 } else if( id != NULL ) {
494 // if a attempt record exists, update it according to mode
495 switch( mode ) {
496 case ATTEMPT_RESET:
497 /* WORK TO DO -- DO SQL:
498 "UPDATE openvpn_attempts "
499 " SET attempts = 0 "
500 " WHERE atpid = '%q'",
501 id
502 Result check comes later ...
503 */
504 break;
505 default:
506 /* WORK TO DO -- DO SQL:
507 "UPDATE openvpn_attempts "
508 " SET last_attempt = CURRENT_TIMESTAMP, attempts = attempts + 1"
509 " WHERE atpid = '%q'",
510 id
511 Result check comes later ...
512 */
513 break;
514 }
515 }
516 if( /* IF SQL QUERY FAILED */ ) {
517 eurephia_log(ctx, LOG_CRITICAL, 0,
518 "Could not update openvpn_attempts for %s = %s",
519 eDBattempt_types[type].colname, value);
520 }
521 /* FREE SQL RESULT */
522
523 // If attempts have exceeded attempt limit, blacklist it immediately if not already registered
524 if( (mode == ATTEMPT_REGISTER)
525 && (blid == NULL) && (atmpt_block != NULL) && (atoi_nullsafe(atmpt_block) > 0) ) {
526 eurephia_log(ctx, LOG_WARNING, 0, "Blacklisting %s due to too many attempts: %s",
527 eDBattempt_types[type].descr, value);
528
529 /* WORK TO DO -- DO SQL:
530 "INSERT INTO openvpn_blacklist (%s) VALUES ('%q')",
531 eDBattempt_types[type].colname, value
532 */
533 if( /* IF SQL FAILED */ ) {
534 eurephia_log(ctx, LOG_CRITICAL, 0,
535 "Could not blacklist %s: %s",
536 eDBattempt_types[type].descr, value);
537 }
538 /* FREE SQL RESULT */
539 }
540 free_nullsafe(id);
541 free_nullsafe(atmpt_block);
542 free_nullsafe(blid);
543 }
544
545
546 // Register a successful authentication
547 int eDBregister_login(eurephiaCTX *ctx, eurephiaSESSION *skey, const int certid, const int uid,
548 const char *proto, const char *remipaddr, const char *remport,
549 const char *vpnipaddr, const char *vpnipmask)
550 {
551 DEBUG(ctx, 20, "Function call: eDBregister_login(ctx, '%s', %i, %i, '%s','%s','%s','%s','%s')",
552 skey->sessionkey, certid, uid, proto, remipaddr, remport, vpnipaddr, vpnipmask);
553
554 if( skey->sessionstatus != SESSION_NEW ) {
555 eurephia_log(ctx, LOG_ERROR, 5, "Not a new session, will not register it again");
556 return 1;
557 }
558
559 /* WORK TO DO -- DO SQL:
560 "INSERT INTO openvpn_lastlog (uid, certid, "
561 " protocol, remotehost, remoteport,"
562 " vpnipaddr, vpnipmask,"
563 " sessionstatus, sessionkey, login) "
564 "VALUES (%i, %i, '%q','%q','%q','%q','%q', 1,'%q', CURRENT_TIMESTAMP)",
565 uid, certid, proto, remipaddr, remport, vpnipaddr, vpnipmask, skey->sessionkey
566 */
567 if( /* IF SQL QUERY FAILED */ ) {
568 eurephia_log(ctx, LOG_FATAL, 0, "Could not insert new session into openvpn_lastlog");
569 return 0;
570 }
571 /* FREE SQL RESULT */
572 skey->sessionstatus = SESSION_REGISTERED;
573 return 1;
574 }
575
576 // Register the MAC address of the VPN adapter of the user.
577 int eDBregister_vpnmacaddr(eurephiaCTX *ctx, eurephiaSESSION *session, const char *macaddr)
578 {
579
580 DEBUG(ctx, 20, "Function call: eDBregister_vpnmacaddr(ctx, '%s', '%s')",
581 session->sessionkey, macaddr);
582
583 if( macaddr == NULL ) {
584 eurephia_log(ctx, LOG_FATAL, 0, "No MAC address was given to save");
585 return 0;
586 }
587
588 // Register MAC address into history table
589 /* WORK TO DO -- DO SQL:
590 "INSERT INTO openvpn_macaddr_history (sessionkey, macaddr) VALUES ('%q','%q')",
591 session->sessionkey, macaddr
592 */
593 if( /* IF SQL QUERY FAILED */ ) {
594 eurephia_log(ctx, LOG_FATAL, 0, "Failed to log new MAC address for session");
595 return 0;
596 }
597 /* FREE SQL RESULT */
598
599 // Update lastlog to reflect last used MAC address for the session
600 /* WORK TO DO -- DO SQL:
601 "UPDATE openvpn_lastlog SET sessionstatus = 2, macaddr = '%q' "
602 " WHERE sessionkey = '%q' AND sessionstatus = 1", macaddr, session->sessionkey);
603 */
604 if( /* IF SQL QUERY FAILED */ ) {
605 eurephia_log(ctx, LOG_FATAL, 0, "Could not update lastlog with new MAC address for session");
606 return 0;
607 }
608 /* FREE SQL RESULT */
609
610 // Save the MAC address in the session values register - needed for the destroy session
611 if( eDBset_session_value(ctx, session, "macaddr", macaddr) == 0 ) {
612 eurephia_log(ctx, LOG_FATAL, 0, "Could not save MAC address into session variables");
613 return 0;
614 }
615
616 return 1;
617 }
618
619
620 // Register the user as logged out
621 int eDBregister_logout(eurephiaCTX *ctx, eurephiaSESSION *skey,
622 const char *bytes_sent, const char *bytes_received, const char *duration)
623 {
624 DEBUG(ctx, 20, "Function call: eDBregister_logout(ctx, '%s', %s, %s)",
625 skey->sessionkey, bytes_sent, bytes_received);
626
627 /* WORK TO DO -- DO SQL:
628 "UPDATE openvpn_lastlog "
629 " SET sessionstatus = 3, logout = CURRENT_TIMESTAMP, "
630 " bytes_sent = '%i', bytes_received = '%i', session_duration = '%i' "
631 " WHERE sessionkey = '%q' AND sessionstatus = 2",
632 atoi_nullsafe(bytes_sent), atoi_nullsafe(bytes_received),
633 atoi_nullsafe(duration), skey->sessionke
634 */
635 if( /* IF SQL QUERY FAILED */ ) {
636 eurephia_log(ctx, LOG_FATAL, 0,
637 "Could not update lastlog with logout information (%s)",
638 skey->sessionkey);
639 return 0;
640 }
641 /* FREE SQL RESULT */
642 skey->sessionstatus = SESSION_LOGGEDOUT;
643 return 1;
644 }
645
646
647 // Retrieve a session key from openvpn_sessionkeys if it is a current session. Session seed is used
648 // as criteria
649 char *eDBget_sessionkey_seed(eurephiaCTX *ctx, const char *sessionseed) {
650 char *skey = NULL;
651
652 DEBUG(ctx, 20, "eDBget_sessionkey(ctx, '%s')", sessionseed);
653
654 if( sessionseed == NULL ) {
655 eurephia_log(ctx, LOG_FATAL, 1,
656 "eDBget_sessionkey: No session seed given - cannot locate sessionkey");
657 return NULL;
658 }
659
660 /* WORK TO DO -- DO SQL:
661 "SELECT sessionkey "
662 " FROM openvpn_sessionkeys "
663 " JOIN openvpn_lastlog USING (sessionkey)"
664 " WHERE sessionstatus IN (1,2)"
665 " AND sessionseed = '%q'",
666 sessionseed
667 */
668 if( /* IF SQL QUERY FAILED */ ) {
669 eurephia_log(ctx, LOG_FATAL, 0,
670 "Could not retrieve sessionkey from openvpn_sessionkeys (%s)",
671 sessionseed);
672 return NULL;
673 }
674 if( /* CHECK THAT WE ONLY GOT 1 RECORD */ ) {
675 skey = strdup_nullsafe(/* GET sessionkey FIELD FROM SQL RESULT */);
676 } else {
677 skey = NULL;
678 }
679 /* FREE SQL RESULT */
680 return skey;
681 }
682
683 char *eDBget_sessionkey_macaddr(eurephiaCTX *ctx, const char *macaddr) {
684 char *skey = NULL;
685
686 // Find sessionkey from MAC address
687 /* WORK TO DO -- DO SQL:
688 "SELECT sessionkey "
689 " FROM openvpn_sessions "
690 " JOIN openvpn_lastlog USING (sessionkey)"
691 " WHERE sessionstatus = 3 "
692 " AND datakey = 'macaddr' "
693 " AND dataval = '%q'",
694 macaddr
695 */
696 if( /* IF SQL QUERY FAILED */ ) {
697 eurephia_log(ctx, LOG_FATAL, 0,
698 "Could not remove session from database (MAC addr: %s)", macaddr);
699 return 0;
700 }
701 skey = strdup_nullsafe(/* GET sessionkey FROM SQL RESULT */);
702 /* FREE SQL RESULT */
703
704 return skey;
705 }
706
707
708 // Function returns true(1) if session key is unique
709 int eDBcheck_sessionkey_uniqueness(eurephiaCTX *ctx, const char *seskey) {
710 int uniq = 0;
711
712 DEBUG(ctx, 20, "eDBcheck_sessionkey_uniqueness(ctx, '%s')", seskey);
713 if( seskey == NULL ) {
714 eurephia_log(ctx, LOG_FATAL, 1,
715 "eDBcheck_sessionkey_uniqness: Invalid session key given");
716 return 0;
717 }
718
719 /* WORK TO DO -- DO SQL:
720 "SELECT count(sessionkey) = 0 FROM openvpn_lastlog WHERE sessionkey = '%q'",
721 seskey
722 */
723 if( /* IF SQL QUERY FAILED */ ) {
724 eurephia_log(ctx, LOG_FATAL, 0,
725 "eDBcheck_sessionkey_uniqness: Could not check uniqueness of sessionkey");
726 return 0;
727 }
728 uniq = atoi_nullsafe(/* GET "count(sessionkey) == 0" (as integer bool - 1 == true) FROM SQL RESULT */);
729 /* FREE SQL RESULT */
730
731 return uniq;
732 }
733
734 // register a link between a short-term session seed and a long-term session key
735 int eDBregister_sessionkey(eurephiaCTX *ctx, const char *seed, const char *seskey) {
736
737 DEBUG(ctx, 20, "eDBregister_sessionkey(ctx, '%s', '%s')", seed, seskey);
738 if( (seed == NULL) || (seskey == NULL) ) {
739 eurephia_log(ctx, LOG_FATAL, 1,
740 "eDBregister_sessionkey: Invalid session seed or session key given");
741 return 0;
742 }
743
744 /* WORK TO DO -- DO SQL:
745 "INSERT INTO openvpn_sessionkeys (sessionseed, sessionkey) VALUES('%q','%q')",
746 seed, seskey
747 */
748 if( /* IF SQL QUERY FAILED */ ) {
749 eurephia_log(ctx, LOG_FATAL, 0,
750 "eDBregister_sessionkey: Error registering sessionkey into openvpn_sessionkeys");
751 return 0;
752 }
753 /* FREE SQL RESULT */
754 return 1;
755 }
756
757 // remove a session seed/session key link from openvpn_sessionkeys
758 int eDBremove_sessionkey(eurephiaCTX *ctx, const char *seskey) {
759
760 DEBUG(ctx, 20, "eDBremove_sessionkey(ctx, '%s')", seskey);
761 if( seskey == NULL ) {
762 eurephia_log(ctx, LOG_FATAL, 1,
763 "eDBremove_sessionkey: Invalid session key given");
764 return 0;
765 }
766
767 /* WORK TO DO -- DO SQL:
768 "DELETE FROM openvpn_sessionkeys WHERE sessionkey = '%q'",
769 seskey
770 */
771 if( /* IF SQL QUERY FAILED */ ) {
772 eurephia_log(ctx, LOG_FATAL, 0,
773 "eDBremove_sessionkey: Error removing sessionkey from openvpn_sessionkeys");
774 return 0;
775 }
776 /* FREE SQL RESULT */
777 return 1;
778 }
779
780 // Load session values stored in the database into a eurephiaVALUES struct (session values)
781 eurephiaVALUES *eDBload_sessiondata(eurephiaCTX *ctx, const char *sesskey) {
782 eurephiaVALUES *sessvals = NULL;
783 int i;
784
785 if( (ctx == NULL) || (sesskey == NULL) ) {
786 return NULL;
787 }
788
789 DEBUG(ctx, 20, "Function call: eDBload_sessiondata(ctx, '%s')", sesskey);
790
791 sessvals = eCreate_value_space(ctx, 10);
792
793 /* WORK TO DO -- DO SQL:
794 "SELECT datakey, dataval FROM openvpn_sessions WHERE sessionkey = '%q'",
795 sesskey
796 */
797 if( /* IF WE GOT RECORDS IN THE QUERY */ ) {
798 for( /* LOOP THROUGH ALL RECORDS */ ) {
799 eAdd_value(ctx, sessvals,
800 /* GET datakey FROM SQL RESULT */,
801 /* GET dataval FROM SQL RESULT */);
802 }
803 /* FREE SQL RESULT */
804 } else {
805 eurephia_log(ctx, LOG_CRITICAL, 0,
806 "Could not load session values for session '%s'", sesskey);
807
808 }
809 return sessvals;
810 }
811
812
813 // Store a new, update or delete a sessionvalue in the database
814 int eDBstore_session_value(eurephiaCTX *ctx, eurephiaSESSION *session, int mode, const char *key, const char *val)
815 {
816
817 if( session == NULL ) {
818 DEBUG(ctx, 20,
819 "Function call failed to eDBstore_session_value(ctx, ...): Non-existing session key");
820 return 0;
821 }
822
823 DEBUG(ctx, 20, "Function call: eDBstore_session_value(ctx, '%s', %i, '%s', '%s')",
824 session->sessionkey, mode, key, val);
825
826 switch( mode ) {
827 case SESSVAL_NEW:
828 /* WORK TO DO -- DO SQL:
829 "INSERT INTO openvpn_sessions (sessionkey, datakey, dataval) "
830 "VALUES ('%q','%q','%q')",
831 session->sessionkey, key, val
832 */
833 if( /* IF SQL QUERY FAILED */ ) {
834 eurephia_log(ctx, LOG_FATAL, 0,
835 "Could not register new session variable into database: [%s] %s = %s",
836 session->sessionkey, key, val);
837 return 0;
838 }
839 break;
840
841 case SESSVAL_UPDATE:
842 /* WORK TO DO -- DO SQL:
843 "UPDATE openvpn_sessions SET dataval = '%q' "
844 " WHERE sessionkey = '%q' AND datakey = '%q'",
845 val, session->sessionkey, key
846 */
847 if( /* IF SQL QUERY FAILED */ ) {
848 eurephia_log(ctx, LOG_FATAL, 0,
849 "Could not update session variable: [%s] %s = %s ",
850 session->sessionkey, key, val);
851 return 0;
852 }
853 break;
854
855 case SESSVAL_DELETE:
856 /* WORK TO DO -- DO SQL:
857 "DELETE FROM openvpn_sessions WHERE sessionkey = '%q' AND datakey = '%q'",
858 session->sessionkey, key
859 */
860 if( /* IF SQL QUERY FAILED */ ) {
861 eurephia_log(ctx, LOG_FATAL, 0,
862 "Could not delete session variable: [%s] %s",
863 session->sessionkey, key);
864 return 0;
865 }
866 break;
867
868 default:
869 eurephia_log(ctx, LOG_FATAL, 0,
870 "Unknown eDBstore_session_value mode '%i'", mode);
871 return 0;
872 }
873 /* FREE SQL RESULT */
874 return 1;
875 }
876
877
878 // Delete session information from openvpn_sessions and update openvpn_lastlog with status
879 int eDBdestroy_session(eurephiaCTX *ctx, eurephiaSESSION *session) {
880
881 DEBUG(ctx, 20, "Function call: eDBdestroy_session(ctx, '%s')", session->sessionkey);
882
883 if( (session == NULL) || (session->sessionkey == NULL) ) {
884 eurephia_log(ctx, LOG_WARNING, 1, "No active session given to be destroyed");
885 return 1;
886 }
887
888 // Update session status
889 /* WORK TO DO -- SQL
890 "UPDATE openvpn_lastlog "
891 " SET sessionstatus = 4, session_deleted = CURRENT_TIMESTAMP "
892 " WHERE sessionkey = '%q' AND sessionstatus = 3",
893 session->sessionkey
894 */
895 if( /* IF SQL QUERY FAILED */ ) {
896 eurephia_log(ctx, LOG_FATAL, 0,
897 "Could not update session status in lastlog (%s))", session->sessionkey);
898 return 0;
899 }
900 /* FREE SQL RESULT */
901
902 // Delete session variables
903 /* WORK TO DO -- DO SQL:
904 "DELETE FROM openvpn_sessions WHERE sessionkey = '%q'",
905 session->sessionkey
906 */
907 if( /* IF SQL QUERY FAILED */ ) {
908 eurephia_log(ctx, LOG_FATAL, 0,
909 "Could not delete session variables (%s))", session->sessionkey);
910 return 0;
911 }
912 /* FREE SQL RESULT */
913
914 // Remove the sessionkey from openvpn_sessions
915 if( eDBremove_sessionkey(ctx, session->sessionkey) == 0 ) {
916 return 0;
917 }
918 return 1;
919 }
920
921
922 char *eDBget_firewall_profile(eurephiaCTX *ctx, eurephiaSESSION *session)
923 {
924 char *ret = NULL;
925
926 DEBUG(ctx, 20, "Function call: eDBget_firewall_profile(ctx, {session}'%s')",
927 session->sessionkey);
928
929 /* WORK TO DO -- DO SQL:
930 "SELECT fw_profile "
931 " FROM openvpn_lastlog "
932 " JOIN openvpn_usercerts USING(certid, uid)"
933 " JOIN openvpn_accesses USING(accessprofile)"
934 " WHERE sessionkey = '%q'",
935 session->sessionkey
936 */
937 if( /* IF SQL QUERY FAILED */ ) {
938 eurephia_log(ctx, LOG_FATAL, 0,
939 "Could not retrieve firewall profile for session '%s'",
940 session->sessionkey);
941 return NULL;
942 }
943 ret = strdup_nullsafe(/* GET fw_profile FROM SQL RESULT */);
944 /* FREE SQL RESULT */
945 return ret;
946 }
947
948 eurephiaVALUES *eDBget_blacklisted_ip(eurephiaCTX *ctx) {
949 eurephiaVALUES *ret = NULL;
950
951 DEBUG(ctx, 20, "Function call: eDBget_blacklisted_ip(ctx)");
952
953 /* WORK TO DO -- DO SQL:
954 SELECT remoteip FROM openvpn_blacklist WHERE remoteip IS NOT NULL
955 */
956 if( /* IF SQL QUERY FAILED */ ) {
957 eurephia_log(ctx, LOG_FATAL, 0,
958 "Could not retrieve blacklisted IP addresses from the database");
959 return NULL;
960 }
961 ret = eCreate_value_space(ctx, 21);
962 for( /* LOOP THROUGH ALL RECORDS */ ) {
963 eAdd_value(ctx, ret, NULL, /* GET remoteip FROM SQL RESULT */);
964 }
965 /* FREE SQL RESULT */
966
967 return ret;
968 }
969
970
971 #if DRIVERAPIVERSION > 1
972 /*
973 * API Version 2 functions
974 *
975 */
976
977 #endif