bmf

bmf (Bayesian Mail Filter) 0.9.4 fork + patches
git clone git://git.codemadness.org/bmf
Log | Files | Refs | README | LICENSE

commit da144ef21a75e5a1f78c1faf2d76d93c68f6180f
parent 8c0e2cad22ac8e72666e90b8069cb0b082e38429
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date:   Sat, 27 Oct 2018 19:56:26 +0200

fix unveil(2) permissions and path name + misc code fixes

- check strdup call
- remove dbh_t abstraction
- remove unneccesary casts.

Diffstat:
bmf.c | 12++++++++----
dbg.c | 3++-
dbh.c | 22++++++++++------------
dbh.h | 9+--------
lex.c | 5++---
vec.c | 3++-
6 files changed, 25 insertions(+), 29 deletions(-)

diff --git a/bmf.c b/bmf.c @@ -75,12 +75,12 @@ version(void) int main(int argc, char **argv) { - char *dbname = NULL; + char *dbname = ""; bool_t rdonly; runmode_t mode = mode_normal; mbox_t mboxtype = detect; bool_t do_passthru = false; - dbh_t *pdb; + dbhtext_t *pdb; dbt_t *pblist, *pglist, *ptable; vec_t mlist; stats_t stats; @@ -110,7 +110,10 @@ main(int argc, char **argv) break; /* NOTREACHED */ case 'd': free(dbname); - dbname = strdup(optarg); + if (!(dbname = strdup(optarg))) { + perror("strdup()"); + exit(2); + } break; case 'h': usage(); @@ -151,7 +154,8 @@ main(int argc, char **argv) } stats.extrema = (discrim_t *) malloc(stats.keepers * sizeof(discrim_t)); - pdb = dbtext_db_open(dbname); + /* create directory if it doesn't exist yet, when dbname is NULL or empty use $HOME/.bmf */ + pdb = dbtext_db_open(dbname, rdonly); if (pdb == NULL) { fprintf(stderr, "%s: cannot open database\n", argv[0]); exit(2); diff --git a/dbg.c b/dbg.c @@ -9,9 +9,10 @@ * dbg.c: debug functions for bmf. */ +#include <stdarg.h> + #include "config.h" #include "dbg.h" -#include <stdarg.h> uint g_verbose = 0; diff --git a/dbh.c b/dbh.c @@ -48,8 +48,8 @@ db_getnewcount(veciter_t * piter) return count; } -dbh_t * -dbtext_db_open(cpchar dbname) +dbhtext_t * +dbtext_db_open(cpchar dbname, bool_t rdonly) { dbhtext_t *pthis = NULL; uint dirlen; @@ -64,7 +64,7 @@ dbtext_db_open(cpchar dbname) pthis->close = dbtext_db_close; pthis->opentable = dbtext_db_opentable; - if (dbname != NULL && *dbname != '\0') { + if (dbname[0]) { dirlen = strlen(dbname); if ((pthis->dir = strdup(dbname)) == NULL) { perror("strdup()"); @@ -77,7 +77,7 @@ dbtext_db_open(cpchar dbname) if (phome == NULL || *phome == '\0') { phome = "."; } - dirlen = strlen(phome) + 5 + 1; + dirlen = strlen(phome) + sizeof("/.bmf"); if ((pthis->dir = malloc(dirlen)) == NULL) goto bail; @@ -96,15 +96,14 @@ dbtext_db_open(cpchar dbname) } /* unveil(2), TODO: rework later */ - /* TODO: permission depending on mode */ char listpath[PATH_MAX]; - snprintf(listpath, sizeof(listpath), "%s/%s", pthis->dir, "goodlist"); - if (unveil(listpath, "rw") == -1) { + snprintf(listpath, sizeof(listpath), "%s/%s", pthis->dir, "goodlist.txt"); + if (unveil(listpath, rdonly ? "rc" : "rwc") == -1) { perror("unveil()"); exit(2); } - snprintf(listpath, sizeof(listpath), "%s/%s", pthis->dir, "spamlist"); - if (unveil(listpath, "rw") == -1) { + snprintf(listpath, sizeof(listpath), "%s/%s", pthis->dir, "spamlist.txt"); + if (unveil(listpath, rdonly ? "rc" : "rwc") == -1) { perror("unveil()"); exit(2); } @@ -113,7 +112,7 @@ dbtext_db_open(cpchar dbname) exit(2); } - return (dbh_t *)pthis; + return pthis; bail: if (pthis) { @@ -222,8 +221,7 @@ dbtext_db_opentable(dbhtext_t * pthis, cpchar table, bool_t rdonly) if (st.st_size == 0) { return (dbt_t *) ptable; } - ptable->pbuf = (char *) malloc(st.st_size); - if (ptable->pbuf == NULL) { + if ((ptable->pbuf = malloc(st.st_size)) == NULL) { perror("malloc()"); goto bail_uc; } diff --git a/dbh.h b/dbh.h @@ -29,13 +29,6 @@ struct _dbt { uint(*getcount) (dbt_t *, str_t *); }; -/* database instance */ -typedef struct _dbh dbh_t; -struct _dbh { - bool_t(*close) (dbh_t *); - dbt_t *(*opentable) (dbh_t *, cpchar, bool_t); -}; - typedef struct _dbttext dbttext_t; struct _dbttext { @@ -64,7 +57,7 @@ struct _dbhtext uint db_getnewcount(veciter_t * piter); -dbh_t* dbtext_db_open(cpchar dbname); +dbhtext_t* dbtext_db_open(cpchar dbname, bool_t rdonly); bool_t dbtext_db_close( dbhtext_t* pthis ); dbt_t* dbtext_db_opentable( dbhtext_t* pthis, cpchar table, bool_t rdonly ); diff --git a/lex.c b/lex.c @@ -482,10 +482,9 @@ lex_load(lex_t * pthis, int fd) ssize_t nread; nalloc = IOBUFSIZE; - pthis->pbuf = (char *) malloc(IOBUFSIZE); - if (pthis->pbuf == NULL) { + if ((pthis->pbuf = malloc(IOBUFSIZE)) == NULL) return false; - } + while ((nread = read(fd, pthis->pbuf + pthis->buflen, nalloc - pthis->buflen)) > 0) { pthis->buflen += nread; if (pthis->buflen == nalloc) { diff --git a/vec.c b/vec.c @@ -28,7 +28,8 @@ vec_create(vec_t * pthis) { pthis->nalloc = VEC_INITIAL_SIZE; pthis->nitems = 0; - pthis->pitems = (str_t *) malloc(VEC_INITIAL_SIZE * sizeof(str_t)); + /* TODO: check malloc() */ + pthis->pitems = malloc(VEC_INITIAL_SIZE * sizeof(str_t)); } void