Compare commits

..

No commits in common. '33027db3b326f342c9524e6b40fc5f70678d8539' and 'dac63b29889e2b9f710c3d21fab3eb55ca3ce50f' have entirely different histories.

  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.2") set(VERSION "0.6.1")
project(${CNAME} C) project(${CNAME} C)

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

@ -1,4 +1,2 @@
[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,9 +1,3 @@
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,16 +14,13 @@
#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;
struct stat st;
FILE *fd; FILE *fd;
char path[]; char path[PATH_MAX];
struct stat st;
} f2b_file_t; } f2b_file_t;
struct _config { struct _config {
@ -31,39 +28,40 @@ struct _config {
f2b_file_t *files; f2b_file_t *files;
f2b_file_t *current; f2b_file_t *current;
int flags; int flags;
bool missingok; char path[256];
char path[];
}; };
#include "source.c" #include "source.c"
static bool static bool
file_open(const cfg_t *cfg, f2b_file_t *file) { file_open(f2b_file_t *file, const char *path) {
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');
if (stat(file->path, &st) != 0) strlcpy(buf, path ? path : file->path, sizeof(buf));
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(file->path, "r")) == NULL) { if ((fd = fopen(buf, "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) {
log_msg(cfg, error, "fseek error: %s -- %s", file->path, strerror(errno)); fclose(fd);
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);
@ -74,7 +72,11 @@ static void
file_close(f2b_file_t *file) { file_close(f2b_file_t *file) {
assert(file != NULL); assert(file != NULL);
FCLOSE(file->fd); if (file->fd == NULL)
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));
} }
@ -88,7 +90,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, "stat(): %s -- %s", file->path, strerror(errno)); log_msg(cfg, error, "file stat error: %s", strerror(errno));
return true; return true;
} }
@ -130,21 +132,15 @@ 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) {
cfg->logcb = &logcb_stub; strlcpy(cfg->path, init, sizeof(cfg->path));
strlcpy(cfg->path, init, ilen); cfg->flags |= MOD_IS_READY;
cfg->flags |= MOD_IS_READY; }
return cfg; return cfg;
} }
@ -154,10 +150,10 @@ config(cfg_t *cfg, const char *key, const char *value) {
assert(key != NULL); assert(key != NULL);
assert(value != NULL); assert(value != NULL);
if (strcmp(key, "missingok") == 0) { /* no options */
cfg->missingok = (strcmp(value, "yes") == 0) ? true : false; (void)(cfg); /* suppress warning for unused variable 'ip' */
return true; (void)(key); /* suppress warning for unused variable 'ip' */
} (void)(value); /* suppress warning for unused variable 'ip' */
return false; return false;
} }
@ -165,32 +161,25 @@ 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 (cfg->missingok) if (glob(cfg->path, GLOB_MARK | GLOB_NOESCAPE, NULL, &globbuf) != 0)
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++) {
size_t slen = strlen(globbuf.gl_pathv[i]) + 1; if ((file = calloc(1, sizeof(f2b_file_t))) == NULL)
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) {
strlcpy(file->path, globbuf.gl_pathv[i], slen); log_msg(cfg, error, "can't open file: %s -- %s", globbuf.gl_pathv[i], strerror(errno));
if (!file_open(cfg, file) && !cfg->missingok) { free(file);
FREE(file);
continue; continue;
} }
if (cfg->files) { if (cfg->files == NULL) {
file->next = cfg->files;
cfg->files = file; cfg->files = file;
} else { } else {
file->next = cfg->files;
cfg->files = file; cfg->files = file;
} }
} }
@ -208,7 +197,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;
@ -224,10 +213,13 @@ 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->fd != NULL && file_rotated(cfg, file)) if (file_rotated(cfg, file))
file_close(file); file_close(file);
if (file->fd == NULL && !file_open(cfg, file)) if (file->fd == NULL) {
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;
} }
@ -237,11 +229,14 @@ 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];
time_t mtime; char mtime[30];
const char *fmt = const char *fmt =
"- path: %s\n" "- path: %s\n"
" info: fd=%d inode=%d size=%ld pos=%ld tag=%08X readl=%lu mtime=%lu\n"; " mtime: %s\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);
@ -253,11 +248,13 @@ 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);
mtime = f->st.st_mtime; localtime_r(&f->st.st_mtime, &tm);
strftime(mtime, sizeof(mtime), "%Y-%m-%d %H:%M:%S", &tm);
} else { } else {
fd = -1, ino = -1, sz = 0, pos = -1, mtime = 0; fd = -1, ino = -1, sz = 0, pos = -1;
strlcpy(mtime, "-", sizeof(mtime));
} }
snprintf(tmp, sizeof(tmp), fmt, f->path, fd, ino, sz, pos, f->stag, f->lines, mtime); snprintf(tmp, sizeof(tmp), fmt, f->path, mtime, fd, ino, sz, pos, f->stag, f->lines);
strlcat(buf, tmp, bufsize); strlcat(buf, tmp, bufsize);
} }
@ -268,5 +265,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"
" info: tag=%08X sock=%ld accepts=%u\n"; " 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->stag, p->sock, p->accepts); snprintf(tmp, sizeof(tmp), fmt, p->host, p->port, p->accepts);
strlcat(buf, tmp, bufsize); strlcat(buf, tmp, bufsize);
} }

Loading…
Cancel
Save