|
|
@ -1,6 +1,8 @@ |
|
|
|
#include <assert.h> |
|
|
|
#include <assert.h> |
|
|
|
#include <string.h> |
|
|
|
#include <string.h> |
|
|
|
#include <stdlib.h> |
|
|
|
#include <stdlib.h> |
|
|
|
|
|
|
|
#include <sys/types.h> |
|
|
|
|
|
|
|
#include <sys/wait.h> |
|
|
|
#include <unistd.h> |
|
|
|
#include <unistd.h> |
|
|
|
|
|
|
|
|
|
|
|
#include "backend.h" |
|
|
|
#include "backend.h" |
|
|
@ -42,7 +44,7 @@ cmd_from_str(const char *str) { |
|
|
|
goto cleanup; |
|
|
|
goto cleanup; |
|
|
|
*cmd->argv = argv; |
|
|
|
*cmd->argv = argv; |
|
|
|
if (strcmp(token, HOST_TOKEN) == 0) |
|
|
|
if (strcmp(token, HOST_TOKEN) == 0) |
|
|
|
cmd->pos = cmd->argc; |
|
|
|
cmd->pos = cmd->argc + 1; |
|
|
|
cmd->argv[cmd->argc] = token; |
|
|
|
cmd->argv[cmd->argc] = token; |
|
|
|
cmd->argc++; |
|
|
|
cmd->argc++; |
|
|
|
} |
|
|
|
} |
|
|
@ -84,6 +86,33 @@ cmd_list_destroy(cmd_t *list) { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static bool |
|
|
|
|
|
|
|
cmd_list_exec(cmd_t *list, const char *ip) { |
|
|
|
|
|
|
|
int status = 0; |
|
|
|
|
|
|
|
pid_t pid; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (cmd_t *cmd = list; cmd != NULL; cmd = cmd->next) { |
|
|
|
|
|
|
|
pid = fork(); |
|
|
|
|
|
|
|
if (pid == 0) { |
|
|
|
|
|
|
|
/* child */ |
|
|
|
|
|
|
|
if (ip && cmd->pos) |
|
|
|
|
|
|
|
cmd->argv[cmd->pos - 1] = ip; |
|
|
|
|
|
|
|
execv(cmd->args, cmd->argv); |
|
|
|
|
|
|
|
} else if (pid > 0) { |
|
|
|
|
|
|
|
/* parent */ |
|
|
|
|
|
|
|
waitpid(pid, &status, 0); |
|
|
|
|
|
|
|
if (WIFEXITED(status) && WEXITSTATUS(status) == 0) |
|
|
|
|
|
|
|
continue; |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
/* parent, fork error */ |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* handlers */ |
|
|
|
/* handlers */ |
|
|
|
cfg_t * |
|
|
|
cfg_t * |
|
|
|
create(const char *id) { |
|
|
|
create(const char *id) { |
|
|
@ -136,6 +165,51 @@ ready(cfg_t *cfg) { |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool |
|
|
|
|
|
|
|
start(cfg_t *cfg) { |
|
|
|
|
|
|
|
assert(cfg != NULL); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!cfg->start) |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return cmd_list_exec(cfg->start, NULL); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool |
|
|
|
|
|
|
|
stop(cfg_t *cfg) { |
|
|
|
|
|
|
|
assert(cfg != NULL); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!cfg->stop) |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return cmd_list_exec(cfg->stop, NULL); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool |
|
|
|
|
|
|
|
ban(cfg_t *cfg, const char *ip) { |
|
|
|
|
|
|
|
assert(cfg != NULL && ip != NULL); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return cmd_list_exec(cfg->ban, ip); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool |
|
|
|
|
|
|
|
unban(cfg_t *cfg, const char *ip) { |
|
|
|
|
|
|
|
assert(cfg != NULL && ip != NULL); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return cmd_list_exec(cfg->unban, ip); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool |
|
|
|
|
|
|
|
exists(cfg_t *cfg, const char *ip) { |
|
|
|
|
|
|
|
assert(cfg != NULL && ip != NULL); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!cfg->exists) |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return cmd_list_exec(cfg->exists, ip); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool |
|
|
|
bool |
|
|
|
ping(cfg_t *cfg) { |
|
|
|
ping(cfg_t *cfg) { |
|
|
|
return cfg != NULL; |
|
|
|
return cfg != NULL; |
|
|
|