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. 111
      src/sources/files.c
  5. 4
      src/sources/portknock.c

2
CMakeLists.txt

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

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

@ -1,4 +1,2 @@
[source:files]
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
* bugfix version

111
src/sources/files.c

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

Loading…
Cancel
Save