| 1 |
/* eurephiafw.c -- Firewall interface loader for the eurephia module |
| 2 |
* |
| 3 |
* GPLv2 only - Copyright (C) 2008 - 2012 |
| 4 |
* David Sommerseth <[email protected]> |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU General Public License |
| 8 |
* as published by the Free Software Foundation; version 2 |
| 9 |
* of the License. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this program; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
/** |
| 23 |
* @file eurephiafw.c |
| 24 |
* @author David Sommerseth <[email protected]> |
| 25 |
* @date 2008-08-10 |
| 26 |
* |
| 27 |
* @brief Takes care of loading the configured firewall driver and provides a |
| 28 |
* generic API for updating the firewall rules. |
| 29 |
* |
| 30 |
*/ |
| 31 |
|
| 32 |
#include <stdio.h> |
| 33 |
#include <stdlib.h> |
| 34 |
#include <string.h> |
| 35 |
#include <dlfcn.h> |
| 36 |
#include <errno.h> |
| 37 |
#include <unistd.h> |
| 38 |
#include <sys/mman.h> |
| 39 |
#include <sys/wait.h> |
| 40 |
#include <time.h> |
| 41 |
#include <errno.h> |
| 42 |
#include <assert.h> |
| 43 |
|
| 44 |
#define EUREPHIA_FWINTF |
| 45 |
#include <eurephiafw_struct.h> |
| 46 |
#include <eurephia_context.h> |
| 47 |
#include "eurephia_log.h" |
| 48 |
#include "eurephiafw.h" |
| 49 |
#include "eurephiafw_intf.h" |
| 50 |
#include "eurephia_getsym.h" |
| 51 |
#include "eurephia_nullsafe.h" |
| 52 |
#include "eurephia_values.h" |
| 53 |
#include "eurephiafw_helpers.h" |
| 54 |
#include "eurephiadb_driver.h" |
| 55 |
|
| 56 |
|
| 57 |
/** |
| 58 |
* Unloads the firewall driver |
| 59 |
* |
| 60 |
* @param ctx eurephiaCTX |
| 61 |
* |
| 62 |
* @return Returns 1 on success, otherwise 0. |
| 63 |
*/ |
| 64 |
int eFW_unload(eurephiaCTX *ctx) { |
| 65 |
if( ctx == NULL ) { |
| 66 |
return 1; |
| 67 |
} |
| 68 |
|
| 69 |
if( ctx->eurephia_fw_intf != NULL ) { |
| 70 |
eurephia_log(ctx, LOG_INFO, 3, "Unloading eurephia firewall interface"); |
| 71 |
dlclose(ctx->eurephia_fw_intf); |
| 72 |
ctx->eurephia_fw_intf = NULL; |
| 73 |
return 0; |
| 74 |
} |
| 75 |
return 1; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
/** |
| 80 |
* Loads the given firewall driver/interface |
| 81 |
* |
| 82 |
* @param ctx eurephiaCTX |
| 83 |
* @param intf full path to the firewall interface |
| 84 |
* |
| 85 |
* @return Returns 1 on success, otherwise 0. |
| 86 |
*/ |
| 87 |
int eFW_load(eurephiaCTX *ctx, const char *intf) { |
| 88 |
if( (intf == NULL) || (strlen(intf) == 0) ) { |
| 89 |
eurephia_log(ctx, LOG_FATAL, 0, "No valid eurephia firewall interface indicated"); |
| 90 |
return 0; |
| 91 |
} |
| 92 |
eurephia_log(ctx, LOG_INFO, 2, "Loading eurephia firewall interface: %s", intf); |
| 93 |
|
| 94 |
ctx->eurephia_fw_intf = dlopen(intf, RTLD_NOW); |
| 95 |
if( ctx->eurephia_fw_intf == NULL ) { |
| 96 |
eurephia_log(ctx, LOG_FATAL, 0, "Could not open the eurephia firewall interface (%s)", intf); |
| 97 |
eurephia_log(ctx, LOG_FATAL, 1, "dlopen error: %s", dlerror()); |
| 98 |
return 0; |
| 99 |
} |
| 100 |
|
| 101 |
// Mandatory functions |
| 102 |
eFWinterfaceVersion = eGetSym(ctx, ctx->eurephia_fw_intf, "eFWinterfaceVersion"); |
| 103 |
eFWinterfaceAPIversion = eGetSym(ctx, ctx->eurephia_fw_intf, "eFWinterfaceAPIversion"); |
| 104 |
|
| 105 |
eurephia_log(ctx, LOG_INFO, 1, "Firewall interface loaded: %s (API version %i)", |
| 106 |
eFWinterfaceVersion(), eFWinterfaceAPIversion()); |
| 107 |
|
| 108 |
if( eFWinterfaceAPIversion() < 2 ) { |
| 109 |
eurephia_log(ctx, LOG_FATAL, 0, "The firewall interface must use API version 2 at minimum."); |
| 110 |
return 0; |
| 111 |
} |
| 112 |
|
| 113 |
// Configure firewall interface functions |
| 114 |
switch( eFWinterfaceAPIversion() ) { |
| 115 |
default: |
| 116 |
eurephia_log(ctx, LOG_WARNING, 0, |
| 117 |
"eurephia Firewall interface API is newer than what the running eurephia version is " |
| 118 |
"familiar with. Please consider to upgrade eurephia to take advantage of newer " |
| 119 |
"features in the firewall driver."); |
| 120 |
|
| 121 |
case 2: |
| 122 |
eFW_RunFirewall = eGetSym(ctx, ctx->eurephia_fw_intf, "eFW_RunFirewall"); |
| 123 |
break; |
| 124 |
|
| 125 |
} |
| 126 |
|
| 127 |
if( ctx->fatal_error > 0 ) { |
| 128 |
eurephia_log(ctx, LOG_FATAL, 0, "eurephia Firewall interface is not correctly initialised. " |
| 129 |
"eurephia authentication will not be available"); |
| 130 |
eFW_unload(ctx); |
| 131 |
return 0; |
| 132 |
} |
| 133 |
return 1; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
/** |
| 138 |
* daemonize the firewall thread if "daemon" environment variable is set. |
| 139 |
* preserves stderr access after being daemonized, but |
| 140 |
* only if "daemon_log_direct" environment variable is set. |
| 141 |
* |
| 142 |
* @param ctx eurephiaCTX - Used for error logging only |
| 143 |
* @param logdir Set to 1 if logging should be redirected |
| 144 |
*/ |
| 145 |
static void efw_daemonize(eurephiaCTX *ctx, const int logredir) |
| 146 |
{ |
| 147 |
int fd = -1; |
| 148 |
if( logredir ) { |
| 149 |
fd = dup (2); |
| 150 |
} |
| 151 |
if( daemon(0, 0) < 0 ) { |
| 152 |
eurephia_log(ctx, LOG_WARNING, 0, "efw_daemonize() failed"); |
| 153 |
} else if( fd >= 3 ) { |
| 154 |
dup2(fd, 2); |
| 155 |
close(fd); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
/** |
| 161 |
* Starts the firewall thread. It is started as a separate process, to make sure it will run with |
| 162 |
* root privileges. |
| 163 |
* |
| 164 |
* @param ctx eurephiaCTX |
| 165 |
*/ |
| 166 |
void eFW_StartFirewall(eurephiaCTX *ctx, const int daemon, const int logredir) { |
| 167 |
struct mq_attr mqattr; |
| 168 |
eurephiaCTX *shadowctx = NULL; |
| 169 |
eFWupdateRequest updreq; |
| 170 |
char *fwdest = NULL; |
| 171 |
unsigned int prio; |
| 172 |
int mqres = 0; |
| 173 |
|
| 174 |
ctx->fwcfg = (eurephiaFWINTF *) malloc_nullsafe(ctx, sizeof(eurephiaFWINTF)+2); |
| 175 |
|
| 176 |
// Create a fake eurephia context, just for logging |
| 177 |
shadowctx = (eurephiaCTX *) malloc_nullsafe(ctx, sizeof(eurephiaCTX)+2); |
| 178 |
assert( shadowctx != NULL ); |
| 179 |
if( mlock(shadowctx, sizeof(eurephiaCTX)+2) < 0 ) { |
| 180 |
eurephia_log(ctx, LOG_CRITICAL, 0, "Could not mlock() firewall context: %s", |
| 181 |
strerror(errno)); |
| 182 |
}; |
| 183 |
shadowctx->context_type = ECTX_NO_PRIVILEGES; |
| 184 |
shadowctx->log = ctx->log; |
| 185 |
(*ctx->fwcfg).thrdata.ctx = shadowctx; |
| 186 |
|
| 187 |
(*ctx->fwcfg).thrdata.fw_command = strdup_nullsafe(eGet_value(ctx->dbc->config, "firewall_command")); |
| 188 |
if( (*ctx->fwcfg).thrdata.fw_command == NULL) { |
| 189 |
eurephia_log(ctx, LOG_PANIC, 0, "Could not find firewall_command in configuration. " |
| 190 |
"Firewall updates will not be available."); |
| 191 |
return; |
| 192 |
} else { |
| 193 |
eurephia_log(ctx, LOG_INFO, 1, "Using %s to update the firewall rules.", |
| 194 |
(*ctx->fwcfg).thrdata.fw_command ); |
| 195 |
} |
| 196 |
|
| 197 |
fwdest = eGet_value(ctx->dbc->config, "firewall_destination"); |
| 198 |
if( fwdest == NULL ) { |
| 199 |
eurephia_log(ctx, LOG_PANIC, 0, "Could not find firewall_destination in configuration. " |
| 200 |
"Firewall updates will not be available."); |
| 201 |
return; |
| 202 |
} else { |
| 203 |
eurephia_log(ctx, LOG_INFO, 1, "Using '%s' as firewall rule for VPN accesses", fwdest); |
| 204 |
} |
| 205 |
|
| 206 |
ctx->fwcfg->fwblacklist = eGet_value(ctx->dbc->config, "firewall_blacklist_destination"); |
| 207 |
if( ctx->fwcfg->fwblacklist != NULL ) { |
| 208 |
eurephia_log(ctx, LOG_INFO, 1, |
| 209 |
"Blacklisted IP addresses will also be blocked in '%s'", |
| 210 |
ctx->fwcfg->fwblacklist); |
| 211 |
|
| 212 |
// Create value space for blacklisted IP addresses |
| 213 |
ctx->fwcfg->blacklisted = eCreate_value_space(ctx, 20); |
| 214 |
|
| 215 |
// Setup where to send the blacklisted IP addresses - default is to drop them. |
| 216 |
ctx->fwcfg->fwblacklist_sendto = eGet_value(ctx->dbc->config, "firewall_blacklist_send_to"); |
| 217 |
if( ctx->fwcfg->fwblacklist_sendto == NULL ) { |
| 218 |
eurephia_log(ctx, LOG_INFO, 2,"Blacklisted IP addresses will be dropped immediately"); |
| 219 |
} else { |
| 220 |
eurephia_log(ctx, LOG_INFO, 2,"Blacklisted IP addresses will be sent to '%s'", |
| 221 |
ctx->fwcfg->fwblacklist_sendto); |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
eurephia_log(ctx, LOG_INFO, 3, "Starting eurephia firewall interface"); |
| 226 |
|
| 227 |
// Setup semaphores we need |
| 228 |
if( efwSetupSemaphores(ctx, &(*ctx->fwcfg).thrdata) == 0 ) { |
| 229 |
free_nullsafe(ctx, ctx->fwcfg->thrdata.fw_command); |
| 230 |
return; |
| 231 |
}; |
| 232 |
|
| 233 |
// Setup a message queue |
| 234 |
if( efwSetupMessageQueue(ctx, &(*ctx->fwcfg).thrdata) == 0 ) { |
| 235 |
free_nullsafe(ctx, ctx->fwcfg); |
| 236 |
return; |
| 237 |
} |
| 238 |
|
| 239 |
// Make sure that these variables are not available in the child |
| 240 |
madvise(ctx, sizeof(eurephiaCTX), MADV_DONTFORK); |
| 241 |
|
| 242 |
// Start a new process (should run with root permissions) - which will do the firewall work |
| 243 |
if( (ctx->fwcfg->fwproc_pid = fork()) < 0 ) { |
| 244 |
eurephia_log(ctx, LOG_PANIC, 0, |
| 245 |
"Could not fork out a child process for the firewall interface (%s)", |
| 246 |
strerror(errno)); |
| 247 |
return; |
| 248 |
} |
| 249 |
switch( ctx->fwcfg->fwproc_pid ) { |
| 250 |
case 0: // Child process |
| 251 |
if( daemon ) { |
| 252 |
efw_daemonize(ctx, logredir); |
| 253 |
} |
| 254 |
eDBdisconnect(ctx); |
| 255 |
eFW_RunFirewall(&(*ctx->fwcfg).thrdata); |
| 256 |
exit(-1); // If our child process exits abnormally. |
| 257 |
|
| 258 |
default: // Main process |
| 259 |
eurephia_log(ctx, LOG_INFO, 2, "Firewall updater process started (pid %i)", |
| 260 |
ctx->fwcfg->fwproc_pid); |
| 261 |
} |
| 262 |
|
| 263 |
// Flush the message queue for old messages |
| 264 |
if( mq_getattr((*ctx->fwcfg).thrdata.msgq, &mqattr) == 0 ) { |
| 265 |
long i; |
| 266 |
char buf[1026]; |
| 267 |
|
| 268 |
memset(&buf, 0, 1026); |
| 269 |
if( mqattr.mq_curmsgs > 0 ) { |
| 270 |
for( i = 0; i < mqattr.mq_curmsgs; i++ ) { |
| 271 |
if( mq_receive((*ctx->fwcfg).thrdata.msgq, &buf[0], 1024, &prio) == -1 ) { |
| 272 |
eurephia_log(ctx, LOG_CRITICAL, 0, |
| 273 |
"Error while emptying messages from queue: %s", |
| 274 |
strerror(errno)); |
| 275 |
} else { |
| 276 |
DEBUG(ctx, 28, "Removed message on queue: %s", buf); |
| 277 |
} |
| 278 |
} |
| 279 |
} |
| 280 |
eurephia_log(ctx, LOG_INFO, 3, "Message queue for firewall updates is ready"); |
| 281 |
} else { |
| 282 |
eurephia_log(ctx, LOG_FATAL, 0, "Could not retrieve message queue attributes (%s)", |
| 283 |
strerror(errno)); |
| 284 |
} |
| 285 |
|
| 286 |
// Indicate for the FW module that we are ready |
| 287 |
sem_post(ctx->fwcfg->thrdata.semp_master); |
| 288 |
|
| 289 |
// Waiting for the FW module to get ready |
| 290 |
DEBUG(ctx, 28, "eFW master is ready, waiting for the eFW worker to get ready"); |
| 291 |
sem_wait(ctx->fwcfg->thrdata.semp_worker); |
| 292 |
eurephia_log(ctx, LOG_INFO, 2, "eFW interface initialised."); |
| 293 |
|
| 294 |
// Initialise the chain |
| 295 |
memset(&updreq, 0, sizeof(eFWupdateRequest)); |
| 296 |
updreq.mode = fwINITIALISE; |
| 297 |
strncpy(updreq.rule_destination, fwdest, 64); |
| 298 |
mqres = mq_send((*ctx->fwcfg).thrdata.msgq, |
| 299 |
(const char *)&updreq, EFW_MSG_SIZE, 1); |
| 300 |
if( mqres < 0 ) { |
| 301 |
eurephia_log(ctx, LOG_ERROR, 0, |
| 302 |
"Could not request firewall initialisation of the %s chain: (%i) %s", |
| 303 |
fwdest, mqres, strerror(errno)); |
| 304 |
}; |
| 305 |
|
| 306 |
if( ctx->fwcfg->fwblacklist != NULL ) { |
| 307 |
eurephiaVALUES *blacklisted = NULL, *p = NULL; |
| 308 |
eFWupdateRequest updreq; |
| 309 |
|
| 310 |
// Flushing firewall blacklist chain |
| 311 |
memset(&updreq, 0, sizeof(eFWupdateRequest)); |
| 312 |
updreq.mode = fwFLUSH; |
| 313 |
strncpy(updreq.rule_destination, ctx->fwcfg->fwblacklist, 64); |
| 314 |
errno = 0; |
| 315 |
mqres = mq_send((*ctx->fwcfg).thrdata.msgq, |
| 316 |
(const char *)&updreq, EFW_MSG_SIZE, 1); |
| 317 |
if( mqres < 0 ) { |
| 318 |
eurephia_log(ctx, LOG_ERROR, 0, "Could not request flushing of the %s chain: (%i) %s", |
| 319 |
ctx->fwcfg->fwblacklist, mqres, strerror(errno)); |
| 320 |
}; |
| 321 |
|
| 322 |
// Registering already blacklisted IP addresses into the proper firewall chain |
| 323 |
memset(&updreq, 0, sizeof(eFWupdateRequest)); |
| 324 |
updreq.mode = fwBLACKLIST; |
| 325 |
blacklisted = eDBget_blacklisted_ip(ctx); |
| 326 |
strncpy(updreq.rule_destination, ctx->fwcfg->fwblacklist, 64); |
| 327 |
strncpy(updreq.goto_destination, |
| 328 |
ctx->fwcfg->fwblacklist_sendto ? ctx->fwcfg->fwblacklist_sendto : "\0", 64); |
| 329 |
p = blacklisted; |
| 330 |
while( p != NULL ) { |
| 331 |
if( p->val != NULL ) { |
| 332 |
memset(&updreq.ipaddress, 0, 36); |
| 333 |
strncpy(updreq.ipaddress, p->val, 34); |
| 334 |
eFW_UpdateFirewall(ctx, &updreq); |
| 335 |
} |
| 336 |
p = p->next; |
| 337 |
} |
| 338 |
eFree_values(ctx, blacklisted); |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
/** |
| 344 |
* Stops the firewall update process. |
| 345 |
* |
| 346 |
* @param ctx eurephiaCTX |
| 347 |
*/ |
| 348 |
void eFW_StopFirewall(eurephiaCTX *ctx) { |
| 349 |
char *fwdest = NULL; |
| 350 |
eFWupdateRequest updreq; |
| 351 |
struct timespec tsp; |
| 352 |
int mqres = 0; |
| 353 |
|
| 354 |
if( ctx->fwcfg == NULL ) { |
| 355 |
return; |
| 356 |
} |
| 357 |
|
| 358 |
eurephia_log(ctx, LOG_INFO, 2, "Stopping eurephia firewall interface"); |
| 359 |
|
| 360 |
// Flush the firewall chain before shutting down, to make sure |
| 361 |
// we don't unintentionally some accesses open |
| 362 |
fwdest = eGet_value(ctx->dbc->config, "firewall_destination"); |
| 363 |
if( fwdest != NULL ) { |
| 364 |
memset(&updreq, 0, sizeof(eFWupdateRequest)); |
| 365 |
updreq.mode = fwFLUSH; |
| 366 |
strncpy(updreq.rule_destination, fwdest, 64); |
| 367 |
|
| 368 |
errno = 0; |
| 369 |
mqres = mq_send((*ctx->fwcfg).thrdata.msgq, |
| 370 |
(const char *)&updreq, EFW_MSG_SIZE, 1); |
| 371 |
if( mqres < 0 ) { |
| 372 |
eurephia_log(ctx, LOG_CRITICAL, 0, |
| 373 |
"Could not request firewall flushing of the %s chain: %s", |
| 374 |
fwdest, strerror(errno)); |
| 375 |
}; |
| 376 |
} else { |
| 377 |
eurephia_log(ctx, LOG_CRITICAL, 0, "firewall_destination not set in config. Will not flush " |
| 378 |
"firewall before shutting down the firewall interface."); |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
// Send shutdown message to the firewall module process |
| 383 |
memset(&updreq, 0, sizeof(eFWupdateRequest)); |
| 384 |
updreq.mode = fwSHUTDOWN; |
| 385 |
errno = 0; |
| 386 |
mqres = mq_send((*ctx->fwcfg).thrdata.msgq, |
| 387 |
(const char *)&updreq, EFW_MSG_SIZE, 1); |
| 388 |
if( mqres < 0 ) { |
| 389 |
eurephia_log(ctx, LOG_PANIC, 0, |
| 390 |
"Could not initiate shutdown on eFW module: %s", strerror(errno)); |
| 391 |
kill(ctx->fwcfg->fwproc_pid, SIGABRT); |
| 392 |
} |
| 393 |
|
| 394 |
// |
| 395 |
// Wait for the firewall module process to finish |
| 396 |
// |
| 397 |
|
| 398 |
// prepare a timeout - 30 seconder's should be enough |
| 399 |
if( clock_gettime(CLOCK_REALTIME, &tsp) == -1 ) { |
| 400 |
eurephia_log(ctx, LOG_FATAL, 0, "Could not prepare timeout for firewall shutdown: %s", |
| 401 |
strerror(errno)); |
| 402 |
sleep(3); |
| 403 |
kill(ctx->fwcfg->fwproc_pid, SIGABRT); |
| 404 |
} |
| 405 |
tsp.tv_sec += 30; |
| 406 |
|
| 407 |
// Wait for shutdown accepted signal |
| 408 |
if( sem_timedwait(ctx->fwcfg->thrdata.semp_worker, &tsp) == -1 ) { |
| 409 |
eurephia_log(ctx, LOG_PANIC, 0, "Failed to wait for eFW module process to quit: %s", |
| 410 |
strerror(errno)); |
| 411 |
sleep(3); |
| 412 |
kill(ctx->fwcfg->fwproc_pid, SIGABRT); |
| 413 |
} |
| 414 |
|
| 415 |
// Send acknowledge back |
| 416 |
sem_post(ctx->fwcfg->thrdata.semp_master); |
| 417 |
|
| 418 |
// Clean up and exit |
| 419 |
munlock(ctx->fwcfg->thrdata.ctx, sizeof(eurephiaCTX)+2); |
| 420 |
free_nullsafe(ctx, ctx->fwcfg->thrdata.ctx); |
| 421 |
free_nullsafe(ctx, ctx->fwcfg->fwblacklist_sendto); |
| 422 |
eFree_values(ctx, ctx->fwcfg->blacklisted); |
| 423 |
free_nullsafe(ctx, (*ctx->fwcfg).thrdata.fw_command); |
| 424 |
free_nullsafe(ctx, ctx->fwcfg); |
| 425 |
eurephia_log(ctx, LOG_INFO, 2, "eurephia firewall interface is stopped"); |
| 426 |
} |
| 427 |
|
| 428 |
|
| 429 |
/** |
| 430 |
* Requests an update of the firewall rules |
| 431 |
* |
| 432 |
* @param ctx eurephiaCTX |
| 433 |
* @param request eFWupdateRequest struct with the needed firewall information |
| 434 |
* |
| 435 |
* @return Returns 1 on success, otherwise 0. |
| 436 |
*/ |
| 437 |
int eFW_UpdateFirewall(eurephiaCTX *ctx, eFWupdateRequest *request ) { |
| 438 |
char *blchk = NULL; |
| 439 |
unsigned int prio = 0; |
| 440 |
int mqres = 0; |
| 441 |
|
| 442 |
if( (*ctx->fwcfg).thrdata.fw_command == NULL ) { |
| 443 |
eurephia_log(ctx, LOG_FATAL, 0, "Function call: eFW_UpdateFirewall() -- " |
| 444 |
"firewall_command is not configured. Firewall rules was not updated."); |
| 445 |
return 0; |
| 446 |
} |
| 447 |
|
| 448 |
if( (request == NULL) ) { |
| 449 |
eurephia_log(ctx, LOG_FATAL, 0, "eFW_UpdateFirewall: Invalid update request"); |
| 450 |
return 0; |
| 451 |
} |
| 452 |
|
| 453 |
switch( request->mode ) { |
| 454 |
case fwADD: |
| 455 |
prio = 11; |
| 456 |
case fwDELETE: |
| 457 |
if( strlen_nullsafe(request->rule_destination) < 1 ) { |
| 458 |
eurephia_log(ctx, LOG_FATAL, 0, |
| 459 |
"eFW_UpdateFirewall: No firewall destination defined"); |
| 460 |
return 0; |
| 461 |
} |
| 462 |
|
| 463 |
if( (strlen_nullsafe(request->macaddress) < 1) |
| 464 |
&& (strlen_nullsafe(request->ipaddress) < 1) ) { |
| 465 |
eurephia_log(ctx, LOG_ERROR, 0, |
| 466 |
"eFW_UpdateFirewall: No MAC address nor IP address was given received"); |
| 467 |
return 1; |
| 468 |
} |
| 469 |
if( prio == 0 ) { |
| 470 |
prio = 12; // It's a delete request, it's not as important as other messages |
| 471 |
} |
| 472 |
eurephia_log(ctx, LOG_INFO, 3, |
| 473 |
"Function call: eFW_UpdateFirewall(ctx, %s, '%.18s', '%s', '%s')", |
| 474 |
(request->mode == fwADD ? "ADD" : "DELETE"), |
| 475 |
(ctx->tuntype == tuntype_TAP ? request->macaddress : request->ipaddress), |
| 476 |
request->rule_destination, |
| 477 |
request->goto_destination); |
| 478 |
errno = 0; |
| 479 |
mqres = mq_send((*ctx->fwcfg).thrdata.msgq, |
| 480 |
(const char *)request, EFW_MSG_SIZE, prio); |
| 481 |
if( mqres < 0 ) { |
| 482 |
eurephia_log(ctx, LOG_CRITICAL, 0, |
| 483 |
"eFW_updateFirewall: Failed to send update request - %s", |
| 484 |
strerror(errno)); |
| 485 |
return 0; |
| 486 |
} |
| 487 |
return 1; |
| 488 |
|
| 489 |
case fwBLACKLIST: |
| 490 |
if( strlen_nullsafe(request->ipaddress) < 1) { |
| 491 |
eurephia_log(ctx, LOG_ERROR, 0, |
| 492 |
"eFW_UpdateFirewall: No IP address given for blacklist"); |
| 493 |
return 1; |
| 494 |
} |
| 495 |
eurephia_log(ctx, LOG_INFO, 3, |
| 496 |
"Function call: eFW_UpdateFirewall(ctx, %s, '%.34s','%s', NULL)", |
| 497 |
"BLACKLIST", request->ipaddress, request->rule_destination); |
| 498 |
|
| 499 |
// Check if IP address is already registered as blacklisted |
| 500 |
if( (blchk = eGet_value(ctx->fwcfg->blacklisted, request->ipaddress)) == NULL ) { |
| 501 |
// Setup rule destinations |
| 502 |
strncpy(request->rule_destination, ctx->fwcfg->fwblacklist, 64); |
| 503 |
if( ctx->fwcfg->fwblacklist_sendto != NULL ) { |
| 504 |
strncpy(request->goto_destination, ctx->fwcfg->fwblacklist_sendto, 64); |
| 505 |
} else { |
| 506 |
strncpy(request->goto_destination, "DROP\0", 6); |
| 507 |
} |
| 508 |
|
| 509 |
errno = 0; |
| 510 |
mqres = mq_send((*ctx->fwcfg).thrdata.msgq, |
| 511 |
(const char *)request, EFW_MSG_SIZE, prio); |
| 512 |
if( mqres < 0 ) { |
| 513 |
eurephia_log(ctx, LOG_CRITICAL, 0, |
| 514 |
"eFW_updateFirewall: Failed to send update request - %s", |
| 515 |
strerror(errno)); |
| 516 |
return 0; |
| 517 |
} |
| 518 |
eAdd_value(ctx, ctx->fwcfg->blacklisted, |
| 519 |
request->ipaddress, request->rule_destination); |
| 520 |
} else { |
| 521 |
eurephia_log(ctx, LOG_INFO, 5, "IP address already blacklisted in '%s'", blchk); |
| 522 |
} |
| 523 |
return 1; |
| 524 |
|
| 525 |
case fwFLUSH: |
| 526 |
case fwINITIALISE: |
| 527 |
eurephia_log(ctx, LOG_ERROR, 5, "eFW_UpdateFirewall: Unsupported update request"); |
| 528 |
return 0; |
| 529 |
|
| 530 |
default: |
| 531 |
eurephia_log(ctx, LOG_CRITICAL, 0, |
| 532 |
"Function call: eFW_UpdateFirewall(ctx, %s, '%s') - UNKNOWN MODE", |
| 533 |
"(unknown)", request->macaddress); |
| 534 |
return 0; |
| 535 |
} |
| 536 |
} |