Add variable to set configuration directory

The IMAPFILTER_HOME is checked in order to set a different than the
default $HOME/.imapfilter configuration directory.
This commit is contained in:
Lefteris Chatzimparmpas 2011-10-30 13:31:04 +01:00
parent ed27fa41ee
commit a47605cb61
5 changed files with 52 additions and 71 deletions

View File

@ -9,7 +9,6 @@
#include "imapfilter.h"
#include "session.h"
#include "pathnames.h"
#include <openssl/x509.h>
#include <openssl/ssl.h>
@ -81,9 +80,8 @@ fail:
int
check_cert(X509 *pcert, unsigned char *pmd, unsigned int *pmdlen)
{
int n, r;
int r;
FILE *fd;
char b;
char *certf;
X509 *cert;
unsigned char md[EVP_MAX_MD_SIZE];
@ -92,24 +90,13 @@ check_cert(X509 *pcert, unsigned char *pmd, unsigned int *pmdlen)
r = 0;
cert = NULL;
n = snprintf(&b, 1, "%s/%s", env.home, PATHNAME_CERTS);
if (env.pathmax != -1 && n > env.pathmax)
fatal(ERROR_PATHNAME,
"pathname limit %ld exceeded: %d\n", env.pathmax, n);
certf = (char *)xmalloc((n + 1) * sizeof(char));
snprintf(certf, n + 1, "%s/%s", env.home, PATHNAME_CERTS);
certf = get_filepath("certificates");
if (!exists_file(certf)) {
xfree(certf);
return 0;
}
fd = fopen(certf, "r");
xfree(certf);
if (fd == NULL)
return -1;
@ -166,9 +153,8 @@ print_cert(X509 *cert, unsigned char *md, unsigned int *mdlen)
int
write_cert(X509 *cert)
{
int n;
FILE *fd;
char b, c, buf[64];
char c, buf[64];
char *certf;
do {
@ -184,21 +170,10 @@ write_cert(X509 *cert)
else if (c == 't')
return 0;
n = snprintf(&b, 1, "%s/%s", env.home, PATHNAME_CERTS);
if (env.pathmax != -1 && n > env.pathmax)
fatal(ERROR_PATHNAME,
"pathname limit %ld exceeded: %d\n", env.pathmax, n);
certf = (char *)xmalloc((n + 1) * sizeof(char));
snprintf(certf, n + 1, "%s/%s", env.home, PATHNAME_CERTS);
certf = get_filepath("certificates");
create_file(certf, S_IRUSR | S_IWUSR);
fd = fopen(certf, "a");
xfree(certf);
if (fd == NULL)
return -1;

View File

@ -17,30 +17,37 @@ extern environment env;
/*
* Create imapfilter's home directory.
*/
int
void
create_homedir(void)
{
int n;
char b;
char *hd;
char *h, *i;
n = snprintf(&b, 1, "%s/%s", env.home, PATHNAME_HOME);
h = getenv("HOME");
i = getenv("IMAPFILTER_HOME");
if (i == NULL)
n = strlen(h ? h : "") + strlen(h ? "/" : "") +
strlen(".imapfilter");
else
n = strlen(i);
if (env.pathmax != -1 && n > env.pathmax)
fatal(ERROR_PATHNAME,
"pathname limit %ld exceeded: %d\n", env.pathmax, n);
hd = (char *)xmalloc((n + 1) * sizeof(char));
snprintf(hd, n + 1, "%s/%s", env.home, PATHNAME_HOME);
env.home = (char *)xmalloc((n + 1) * sizeof(char));
if (i == NULL)
snprintf(env.home, n + 1, "%s%s%s", h ? h : "", h ? "/" : "",
".imapfilter");
else
snprintf(env.home, n + 1, "%s", i);
if (!exists_dir(hd)) {
if (mkdir(hd, S_IRUSR | S_IWUSR | S_IXUSR))
error("could not create directory %s; %s\n", hd,
if (!exists_dir(env.home)) {
if (mkdir(env.home, S_IRUSR | S_IWUSR | S_IXUSR))
error("could not create directory %s; %s\n", env.home,
strerror(errno));
}
xfree(hd);
return 0;
}
@ -130,3 +137,24 @@ get_pathmax(void)
return 0;
}
/*
* Get the path of a file inside the configuration directory.
*/
char *
get_filepath(char *fname)
{
int n;
char *fp;
n = strlen(env.home) + strlen("/") + strlen(fname);
if (env.pathmax != -1 && n > env.pathmax)
fatal(ERROR_PATHNAME,
"pathname limit %ld exceeded: %d\n", env.pathmax, n);
fp = (char *)xmalloc((n + 1) * sizeof(char));
snprintf(fp, n + 1, "%s/%s", env.home, fname);
return fp;
}

View File

@ -50,7 +50,7 @@ main(int argc, char *argv[])
opts.oneline = NULL;
opts.debug = NULL;
env.home = getenv("HOME");
env.home = NULL;
env.pathmax = -1;
while ((c = getopt(argc, argv, "Vc:d:e:il:v?")) != -1) {
@ -99,21 +99,8 @@ main(int argc, char *argv[])
buffer_init(&obuf, OUTPUT_BUF);
buffer_init(&nbuf, NAMESPACE_BUF);
if (opts.config == NULL) {
int n;
char b;
n = snprintf(&b, 1, "%s/%s", env.home, PATHNAME_CONFIG);
if (env.pathmax != -1 && n > env.pathmax)
fatal(ERROR_PATHNAME,
"pathname limit %ld exceeded: %d\n", env.pathmax,
n);
opts.config = (char *)xmalloc((n + 1) * sizeof(char));
snprintf(opts.config, n + 1, "%s/%s", env.home,
PATHNAME_CONFIG);
}
if (opts.config == NULL)
opts.config = get_filepath("config.lua");
regexp_compile(responses);
@ -151,6 +138,8 @@ main(int argc, char *argv[])
buffer_free(&obuf);
buffer_free(&nbuf);
xfree(env.home);
close_log();
close_debug();

View File

@ -73,7 +73,7 @@ typedef struct options {
/* Environment variables. */
typedef struct environment {
char *home; /* User's home directory. */
char *home; /* Program's home directory. */
long pathmax; /* Maximum pathname. */
} environment;
@ -92,11 +92,12 @@ int get_cert(session *ssn);
LUALIB_API int luaopen_ifcore(lua_State *lua);
/* file.c */
int create_homedir(void);
void create_homedir(void);
int exists_file(char *fname);
int exists_dir(char *fname);
int create_file(char *fname, mode_t mode);
int get_pathmax(void);
char *get_filepath(char *fname);
/* imap.c */
int imap_continuation(session *ssn, const char *cont, size_t len);

View File

@ -2,12 +2,6 @@
#define PATHNAMES_H
/* Program's home directory. */
#define PATHNAME_HOME ".imapfilter"
/* Program's configuration file. */
#define PATHNAME_CONFIG PATHNAME_HOME "/config.lua"
/* Lua imapfilter set functions file. */
#define PATHNAME_COMMON MAKEFILE_SHAREDIR "/common.lua"
@ -35,11 +29,5 @@
/* Lua imapfilter old interface functions file. */
#define PATHNAME_DEPRECATED MAKEFILE_SHAREDIR "/deprecated.lua"
/* SSL/TLS certificates file. */
#define PATHNAME_CERTS PATHNAME_HOME "/certificates"
/* Debug temporary file template. */
#define PATHNAME_DEBUG PATHNAME_HOME "/debug.XXXXXX"
#endif /* PATHNAMES_H */