Compare commits

..

6 Commits

  1. 2
      CMakeLists.txt
  2. 2
      configs/conf-available/05-source-files.conf
  3. 6
      debian/changelog
  4. 113
      src/sources/files.c
  5. 4
      src/sources/portknock.c

2
CMakeLists.txt

@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.4) cmake_minimum_required(VERSION 3.4)
set(CNAME "f2b") set(CNAME "f2b")
set(VERSION "0.6.1") set(VERSION "0.6.2")
project(${CNAME} C) project(${CNAME} C)

2
configs/conf-available/05-source-files.conf

@ -1,2 +1,4 @@
[source:files] [source:files]
load = source_files.so load = source_files.so
; allow source start if no files matched?
missingok = no

6
debian/changelog vendored

@ -1,3 +1,9 @@
f2b (0.6.2-1) unstable; urgency=medium
* bugfix version
-- Alex 'AdUser' Z <ad_user@runbox.com> Mon, 26 Aug 2024 11:17:01 +1000
f2b (0.6.1-1) unstable; urgency=medium f2b (0.6.1-1) unstable; urgency=medium
* bugfix version * bugfix version

113
src/sources/files.c

@ -14,13 +14,16 @@
#define MODNAME "files" #define MODNAME "files"
#define FREE(x) { free(x); x = NULL; }
#define FCLOSE(x) { if (x) fclose(x); x = NULL; }
typedef struct f2b_file_t { typedef struct f2b_file_t {
struct f2b_file_t *next; struct f2b_file_t *next;
uint32_t lines; uint32_t lines;
uint32_t stag; uint32_t stag;
FILE *fd;
char path[PATH_MAX];
struct stat st; struct stat st;
FILE *fd;
char path[];
} f2b_file_t; } f2b_file_t;
struct _config { struct _config {
@ -28,40 +31,39 @@ struct _config {
f2b_file_t *files; f2b_file_t *files;
f2b_file_t *current; f2b_file_t *current;
int flags; int flags;
char path[256]; bool missingok;
char path[];
}; };
#include "source.c" #include "source.c"
static bool static bool
file_open(f2b_file_t *file, const char *path) { file_open(const cfg_t *cfg, f2b_file_t *file) {
FILE *fd; FILE *fd;
struct stat st; struct stat st;
char buf[PATH_MAX] = "";
assert(file != NULL); assert(file != NULL);
assert(path != NULL || file->path[0] != '\0');
strlcpy(buf, path ? path : file->path, sizeof(buf)); if (stat(file->path, &st) != 0)
memset(file, 0x0, sizeof(f2b_file_t));
if (stat(buf, &st) != 0)
return false; return false;
if (!(S_ISREG(st.st_mode) || S_ISFIFO(st.st_mode))) if (!(S_ISREG(st.st_mode) || S_ISFIFO(st.st_mode))) {
log_msg(cfg, error, "not a regular file/fifo: %s", file->path);
return false; return false;
}
if ((fd = fopen(buf, "r")) == NULL) if ((fd = fopen(file->path, "r")) == NULL) {
log_msg(cfg, error, "can't open file: %s -- %s", file->path, strerror(errno));
return false; return false;
}
if (S_ISREG(st.st_mode) && fseek(fd, 0, SEEK_END) < 0) { if (S_ISREG(st.st_mode) && fseek(fd, 0, SEEK_END) < 0) {
fclose(fd); log_msg(cfg, error, "fseek error: %s -- %s", file->path, strerror(errno));
FCLOSE(fd);
return false; return false;
} }
memcpy(&file->st, &st, sizeof(st)); memcpy(&file->st, &st, sizeof(st));
strlcpy(file->path, buf, sizeof(file->path));
file->fd = fd; file->fd = fd;
file->stag = fnv_32a_str(file->path, FNV1_32A_INIT); file->stag = fnv_32a_str(file->path, FNV1_32A_INIT);
@ -72,11 +74,7 @@ static void
file_close(f2b_file_t *file) { file_close(f2b_file_t *file) {
assert(file != NULL); assert(file != NULL);
if (file->fd == NULL) FCLOSE(file->fd);
return;
fclose(file->fd);
file->fd = NULL;
file->lines = 0; file->lines = 0;
memset(&file->st, 0, sizeof(struct stat)); memset(&file->st, 0, sizeof(struct stat));
} }
@ -90,7 +88,7 @@ file_rotated(const cfg_t *cfg, f2b_file_t *file) {
return true; return true;
if (stat(file->path, &st) != 0) { if (stat(file->path, &st) != 0) {
log_msg(cfg, error, "file stat error: %s", strerror(errno)); log_msg(cfg, error, "stat(): %s -- %s", file->path, strerror(errno));
return true; return true;
} }
@ -132,15 +130,21 @@ file_getline(const cfg_t *cfg, f2b_file_t *file, char *buf, size_t bufsize) {
cfg_t * cfg_t *
create(const char *init) { create(const char *init) {
cfg_t *cfg = NULL; cfg_t *cfg = NULL;
if ((cfg = calloc(1, sizeof(cfg_t))) == NULL)
if (init == NULL) return NULL;
size_t ilen = strlen(init) + 1;
if (ilen <= 1) return NULL;
if ((cfg = calloc(1, sizeof(cfg_t) + ilen)) == NULL)
return NULL; return NULL;
cfg->logcb = &logcb_stub;
cfg->flags |= MOD_TYPE_SOURCE; cfg->flags |= MOD_TYPE_SOURCE;
cfg->flags |= MOD_NEED_FILTER; cfg->flags |= MOD_NEED_FILTER;
if (init != NULL && strlen(init) > 0) {
strlcpy(cfg->path, init, sizeof(cfg->path)); cfg->logcb = &logcb_stub;
cfg->flags |= MOD_IS_READY; strlcpy(cfg->path, init, ilen);
} cfg->flags |= MOD_IS_READY;
return cfg; return cfg;
} }
@ -150,10 +154,10 @@ config(cfg_t *cfg, const char *key, const char *value) {
assert(key != NULL); assert(key != NULL);
assert(value != NULL); assert(value != NULL);
/* no options */ if (strcmp(key, "missingok") == 0) {
(void)(cfg); /* suppress warning for unused variable 'ip' */ cfg->missingok = (strcmp(value, "yes") == 0) ? true : false;
(void)(key); /* suppress warning for unused variable 'ip' */ return true;
(void)(value); /* suppress warning for unused variable 'ip' */ }
return false; return false;
} }
@ -161,25 +165,32 @@ config(cfg_t *cfg, const char *key, const char *value) {
bool bool
start(cfg_t *cfg) { start(cfg_t *cfg) {
f2b_file_t *file = NULL; f2b_file_t *file = NULL;
int glob_flags = GLOB_MARK | GLOB_NOESCAPE;
glob_t globbuf; glob_t globbuf;
assert(cfg != NULL); assert(cfg != NULL);
if (glob(cfg->path, GLOB_MARK | GLOB_NOESCAPE, NULL, &globbuf) != 0) if (cfg->missingok)
glob_flags |= GLOB_NOCHECK; /* allow no matches */
if (glob(cfg->path, glob_flags, NULL, &globbuf) != 0)
return NULL; return NULL;
for (size_t i = 0; i < globbuf.gl_pathc; i++) { for (size_t i = 0; i < globbuf.gl_pathc; i++) {
if ((file = calloc(1, sizeof(f2b_file_t))) == NULL) size_t slen = strlen(globbuf.gl_pathv[i]) + 1;
if ((file = calloc(1, sizeof(f2b_file_t) + slen)) == NULL) {
log_msg(cfg, error, "can't allocate memory: %s", strerror(errno));
continue; continue;
if (file_open(file, globbuf.gl_pathv[i]) == false) { }
log_msg(cfg, error, "can't open file: %s -- %s", globbuf.gl_pathv[i], strerror(errno)); strlcpy(file->path, globbuf.gl_pathv[i], slen);
free(file); if (!file_open(cfg, file) && !cfg->missingok) {
FREE(file);
continue; continue;
} }
if (cfg->files == NULL) { if (cfg->files) {
file->next = cfg->files;
cfg->files = file; cfg->files = file;
} else { } else {
file->next = cfg->files;
cfg->files = file; cfg->files = file;
} }
} }
@ -197,7 +208,7 @@ stop(cfg_t *cfg) {
for (; cfg->files != NULL; cfg->files = next) { for (; cfg->files != NULL; cfg->files = next) {
next = cfg->files->next; next = cfg->files->next;
file_close(cfg->files); file_close(cfg->files);
free(cfg->files); FREE(cfg->files);
} }
return true; return true;
@ -213,13 +224,10 @@ next(cfg_t *cfg, char *buf, size_t bufsize, bool reset) {
cfg->current = cfg->files; cfg->current = cfg->files;
for (f2b_file_t *file = cfg->current; file != NULL; file = file->next) { for (f2b_file_t *file = cfg->current; file != NULL; file = file->next) {
if (file_rotated(cfg, file)) if (file->fd != NULL && file_rotated(cfg, file))
file_close(file); file_close(file);
if (file->fd == NULL) { if (file->fd == NULL && !file_open(cfg, file))
if (!file_open(file, NULL))
log_msg(cfg, error, "can't open file: %s", file->path);
continue; continue;
}
if (file_getline(cfg, file, buf, bufsize)) if (file_getline(cfg, file, buf, bufsize))
return file->stag; return file->stag;
} }
@ -229,14 +237,11 @@ next(cfg_t *cfg, char *buf, size_t bufsize, bool reset) {
bool bool
stats(cfg_t *cfg, char *buf, size_t bufsize) { stats(cfg_t *cfg, char *buf, size_t bufsize) {
struct tm tm;
char tmp[PATH_MAX + 512]; char tmp[PATH_MAX + 512];
char mtime[30]; time_t mtime;
const char *fmt = const char *fmt =
"- path: %s\n" "- path: %s\n"
" mtime: %s\n" " info: fd=%d inode=%d size=%ld pos=%ld tag=%08X readl=%lu mtime=%lu\n";
" file: fd=%d inode=%d size=%ld pos=%ld tag=%08X\n"
" read: %lu lines\n";
int fd, ino; off_t sz; long pos; int fd, ino; off_t sz; long pos;
assert(cfg != NULL); assert(cfg != NULL);
assert(buf != NULL); assert(buf != NULL);
@ -248,13 +253,11 @@ stats(cfg_t *cfg, char *buf, size_t bufsize) {
for (f2b_file_t *f = cfg->files; f != NULL; f = f->next) { for (f2b_file_t *f = cfg->files; f != NULL; f = f->next) {
if (f->fd) { if (f->fd) {
fd = fileno(f->fd), ino = f->st.st_ino, sz = f->st.st_size, pos = ftell(f->fd); fd = fileno(f->fd), ino = f->st.st_ino, sz = f->st.st_size, pos = ftell(f->fd);
localtime_r(&f->st.st_mtime, &tm); mtime = f->st.st_mtime;
strftime(mtime, sizeof(mtime), "%Y-%m-%d %H:%M:%S", &tm);
} else { } else {
fd = -1, ino = -1, sz = 0, pos = -1; fd = -1, ino = -1, sz = 0, pos = -1, mtime = 0;
strlcpy(mtime, "-", sizeof(mtime));
} }
snprintf(tmp, sizeof(tmp), fmt, f->path, mtime, fd, ino, sz, pos, f->stag, f->lines); snprintf(tmp, sizeof(tmp), fmt, f->path, fd, ino, sz, pos, f->stag, f->lines, mtime);
strlcat(buf, tmp, bufsize); strlcat(buf, tmp, bufsize);
} }
@ -265,5 +268,5 @@ void
destroy(cfg_t *cfg) { destroy(cfg_t *cfg) {
assert(cfg != NULL); assert(cfg != NULL);
free(cfg); FREE(cfg);
} }

4
src/sources/portknock.c

@ -208,14 +208,14 @@ stats(cfg_t *cfg, char *buf, size_t bufsize) {
char tmp[256]; char tmp[256];
const char *fmt = const char *fmt =
"- listen: %s:%s\n" "- listen: %s:%s\n"
" accepts: %u\n"; " info: tag=%08X sock=%ld accepts=%u\n";
assert(cfg != NULL); assert(cfg != NULL);
if (buf == NULL || bufsize == 0) if (buf == NULL || bufsize == 0)
return false; return false;
for (f2b_port_t *p = cfg->ports; p != NULL; p = p->next) { for (f2b_port_t *p = cfg->ports; p != NULL; p = p->next) {
snprintf(tmp, sizeof(tmp), fmt, p->host, p->port, p->accepts); snprintf(tmp, sizeof(tmp), fmt, p->host, p->port, p->stag, p->sock, p->accepts);
strlcat(buf, tmp, bufsize); strlcat(buf, tmp, bufsize);
} }

Loading…
Cancel
Save