dlz_filesystem_dynamic.c [plain text]
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/stat.h>
#include "dlz_minimal.h"
#include "dlz_list.h"
#include "dir.h"
typedef struct config_data {
char *basedir;
int basedirsize;
char *datadir;
int datadirsize;
char *xfrdir;
int xfrdirsize;
int splitcnt;
char separator;
char pathsep;
log_t *log;
dns_sdlz_putrr_t *putrr;
dns_sdlz_putnamedrr_t *putnamedrr;
dns_dlz_writeablezone_t *writeable_zone;
} config_data_t;
typedef struct dir_entry dir_entry_t;
struct dir_entry {
char dirpath[DIR_PATHMAX];
DLZ_LINK(dir_entry_t) link;
};
typedef DLZ_LIST(dir_entry_t) dlist_t;
static void
b9_add_helper(struct config_data *cd, const char *helper_name, void *ptr);
static isc_boolean_t
is_safe(const char *input) {
unsigned int i;
unsigned int len = strlen(input);
for (i = 0; i < len; i++) {
if (input[i] == '.') {
if (i == 0)
return (ISC_FALSE);
else if (input[i-1] == '.')
return (ISC_FALSE);
if (i == len)
return (ISC_FALSE);
continue;
}
if (input[i] == '-')
continue;
if (input[i] >= '0' && input[i] <= '9')
continue;
if (input[i] >= 'A' && input[i] <= 'Z')
continue;
if (input[i] >= 'a' && input[i] <= 'z')
continue;
if (input[i] == ':')
continue;
if (input[i] == '@')
continue;
return (ISC_FALSE);
}
return (ISC_TRUE);
}
static isc_result_t
create_path_helper(char *out, const char *in, config_data_t *cd) {
char *tmpString;
char *tmpPtr;
int i;
tmpString = strdup(in);
if (tmpString == NULL)
return (ISC_R_NOMEMORY);
while ((tmpPtr = strrchr(tmpString, '.')) != NULL) {
i = 0;
while (tmpPtr[i+1] != '\0') {
if (cd->splitcnt < 1)
strcat(out, (char *) &tmpPtr[i+1]);
else
strncat(out, (char *) &tmpPtr[i+1],
cd->splitcnt);
strncat(out, (char *) &cd->pathsep, 1);
if (cd->splitcnt == 0)
break;
if (strlen((char *) &tmpPtr[i+1]) <=
(unsigned int) cd->splitcnt)
break;
i += cd->splitcnt;
}
tmpPtr[0] = '\0';
}
i=0;
tmpPtr = tmpString;
while (tmpPtr[i] != '\0') {
if (cd->splitcnt < 1)
strcat(out, (char *) &tmpPtr[i]);
else
strncat(out, (char *) &tmpPtr[i], cd->splitcnt);
strncat(out, (char *) &cd->pathsep, 1);
if (cd->splitcnt == 0)
break;
if (strlen((char *) &tmpPtr[i]) <=
(unsigned int) cd->splitcnt)
break;
i += cd->splitcnt;
}
free(tmpString);
return (ISC_R_SUCCESS);
}
static isc_result_t
create_path(const char *zone, const char *host, const char *client,
config_data_t *cd, char **path)
{
char *tmpPath;
int pathsize;
int len;
isc_result_t result;
isc_boolean_t isroot = ISC_FALSE;
if (strcmp(zone, ".") == 0)
isroot = ISC_TRUE;
if (!isroot && !is_safe(zone))
return (ISC_R_FAILURE);
if (host != NULL && !is_safe(host))
return (ISC_R_FAILURE);
if (client != NULL && !is_safe(client))
return (ISC_R_FAILURE);
if (host != NULL)
len = strlen(zone) + strlen(host);
else if (client != NULL)
len = strlen(zone) + strlen(client);
else
len = strlen(zone);
pathsize = len + cd->basedirsize +
cd->datadirsize + cd->xfrdirsize + 4;
if (cd->splitcnt > 0)
pathsize += len/cd->splitcnt;
tmpPath = malloc(pathsize * sizeof(char));
if (tmpPath == NULL) {
cd->log(ISC_LOG_ERROR,
"Filesystem driver unable to "
"allocate memory in create_path().");
result = ISC_R_NOMEMORY;
goto cleanup_mem;
}
strcpy(tmpPath, cd->basedir);
if (!isroot) {
result = create_path_helper(tmpPath, zone, cd);
if (result != ISC_R_SUCCESS)
goto cleanup_mem;
}
if (client != NULL) {
strcat(tmpPath, cd->xfrdir);
strncat(tmpPath, (char *) &cd->pathsep, 1);
strcat(tmpPath, client);
} else
strcat(tmpPath, cd->datadir);
if (host != NULL) {
strncat(tmpPath, (char *) &cd->pathsep, 1);
result = create_path_helper(tmpPath, host, cd);
if (result != ISC_R_SUCCESS)
goto cleanup_mem;
}
*path = tmpPath;
result = ISC_R_SUCCESS;
cleanup_mem:
if (tmpPath != NULL && result != ISC_R_SUCCESS)
free(tmpPath);
return (result);
}
static isc_result_t
process_dir(dir_t *dir, void *passback, config_data_t *cd,
dlist_t *dir_list, unsigned int basedirlen)
{
char tmp[DIR_PATHMAX + DIR_NAMEMAX];
int astPos;
struct stat sb;
isc_result_t result = ISC_R_FAILURE;
char *endp;
char *type;
char *ttlStr;
char *data;
char host[DIR_NAMEMAX];
char *tmpString;
char *tmpPtr;
int ttl;
int i;
int len;
dir_entry_t *direntry;
isc_boolean_t foundHost;
tmp[0] = '\0';
host[0] = '\0';
foundHost = ISC_FALSE;
strcpy(tmp, dir->dirname);
astPos = strlen(dir->dirname) - 1;
if (dir_list != NULL) {
if (cd->splitcnt == 0) {
if (strlen(tmp) - 3 > basedirlen) {
tmp[astPos-1] = '\0';
tmpString = (char *) &tmp[basedirlen+1];
if (strcmp(tmpString, "-") == 0) {
strcpy(host, "*");
} else {
while ((tmpPtr = strrchr(tmpString,
cd->pathsep))
!= NULL)
{
if ((strlen(host) +
strlen(tmpPtr + 1) + 2)
> DIR_NAMEMAX)
continue;
strcat(host, tmpPtr + 1);
strcat(host, ".");
tmpPtr[0] = '\0';
}
if ((strlen(host) +
strlen(tmpString) + 1)
<= DIR_NAMEMAX)
strcat(host, tmpString);
}
foundHost = ISC_TRUE;
strcpy(tmp, dir->dirname);
}
} else {
while (dir_read(dir) == ISC_R_SUCCESS) {
if (strncasecmp(".host",
dir->entry.name, 5) == 0) {
if (strcmp((char *) &dir->entry.name[6],
"-") == 0)
strcpy(host, "*");
else {
strncpy(host,
(char *) &dir->entry.name[6],
sizeof(host) - 1);
host[255] = '\0';
}
foundHost = ISC_TRUE;
break;
}
}
dir_reset(dir);
}
}
while (dir_read(dir) == ISC_R_SUCCESS) {
cd->log(ISC_LOG_DEBUG(1),
"Filesystem driver Dir name:"
" '%s' Dir entry: '%s'\n",
dir->dirname, dir->entry.name);
if (dir->entry.name[0] == '.')
continue;
tmp[astPos] = '\0';
strcat(tmp, dir->entry.name);
if (stat(tmp, &sb) == 0 ) {
if ((sb.st_mode & S_IFDIR) != 0) {
if (dir_list != NULL) {
direntry = malloc(sizeof(dir_entry_t));
if (direntry == NULL)
return (ISC_R_NOMEMORY);
strcpy(direntry->dirpath, tmp);
DLZ_LINK_INIT(direntry, link);
DLZ_LIST_APPEND(*dir_list, direntry,
link);
result = ISC_R_SUCCESS;
}
continue;
} else if (dir_list != NULL &&
foundHost == ISC_FALSE) {
continue;
}
} else
continue;
type = dir->entry.name;
ttlStr = strchr(type, cd->separator);
if (ttlStr == NULL) {
cd->log(ISC_LOG_ERROR,
"Filesystem driver: "
"%s could not be parsed properly", tmp);
return (ISC_R_FAILURE);
}
ttlStr[0] = '\0';
ttlStr = (char *) &ttlStr[1];
data = strchr(ttlStr, cd->separator);
if (data == NULL) {
cd->log(ISC_LOG_ERROR,
"Filesystem driver: "
"%s could not be parsed properly", tmp);
return (ISC_R_FAILURE);
}
data[0] = '\0';
data = (char *) &data[1];
len = strlen(data);
for (i=0; i < len; i++) {
if (data[i] == cd->separator)
data[i] = ' ';
}
ttl = strtol(ttlStr, &endp, 10);
if (*endp != '\0' || ttl < 0)
cd->log(ISC_LOG_ERROR,
"Filesystem driver "
"ttl must be a postive number");
if (dir_list == NULL)
result = cd->putrr((dns_sdlzlookup_t *) passback,
type, ttl, data);
else
result = cd->putnamedrr((dns_sdlzallnodes_t *) passback,
(char *) host,
type, ttl, data);
if (result != ISC_R_SUCCESS)
return (result);
}
return (result);
}
isc_result_t
dlz_allowzonexfr(void *dbdata, const char *name, const char *client) {
isc_result_t result;
char *path;
struct stat sb;
config_data_t *cd;
path = NULL;
cd = (config_data_t *) dbdata;
if (create_path(name, NULL, client, cd, &path) != ISC_R_SUCCESS) {
return (ISC_R_NOTFOUND);
}
if (stat(path, &sb) != 0) {
result = ISC_R_NOTFOUND;
goto complete_AXFR;
}
if ((sb.st_mode & S_IFREG) != 0) {
result = ISC_R_SUCCESS;
goto complete_AXFR;
}
result = ISC_R_NOTFOUND;
complete_AXFR:
free(path);
return (result);
}
isc_result_t
dlz_allnodes(const char *zone, void *dbdata, dns_sdlzallnodes_t *allnodes) {
isc_result_t result;
dlist_t *dir_list;
config_data_t *cd = (config_data_t *) dbdata;
char *basepath;
unsigned int basepathlen;
struct stat sb;
dir_t dir;
dir_entry_t *dir_entry;
dir_entry_t *next_de;
basepath = NULL;
dir_list = NULL;
dir_list = malloc(sizeof(dlist_t));
if (dir_list == NULL) {
result = ISC_R_NOTFOUND;
goto complete_allnds;
}
DLZ_LIST_INIT(*dir_list);
if (create_path(zone, NULL, NULL, cd, &basepath) != ISC_R_SUCCESS) {
result = ISC_R_NOTFOUND;
goto complete_allnds;
}
basepathlen = strlen(basepath);
if (stat(basepath, &sb) != 0) {
result = ISC_R_NOTFOUND;
goto complete_allnds;
}
if ((sb.st_mode & S_IFDIR) == 0) {
result = ISC_R_NOTFOUND;
goto complete_allnds;
}
dir_init(&dir);
result = dir_open(&dir, basepath);
if (result != ISC_R_SUCCESS) {
cd->log(ISC_LOG_ERROR,
"Unable to open %s directory to read entries.",
basepath);
result = ISC_R_FAILURE;
goto complete_allnds;
}
result = process_dir(&dir, allnodes, cd, dir_list, basepathlen);
dir_close(&dir);
if (result != ISC_R_SUCCESS)
goto complete_allnds;
dir_entry = DLZ_LIST_HEAD(*dir_list);
while (dir_entry != NULL) {
result = dir_open(&dir, dir_entry->dirpath);
if (result != ISC_R_SUCCESS) {
cd->log(ISC_LOG_ERROR,
"Unable to open %s "
"directory to read entries.", basepath);
result = ISC_R_FAILURE;
goto complete_allnds;
}
result = process_dir(&dir, allnodes, cd, dir_list, basepathlen);
dir_close(&dir);
if (result != ISC_R_SUCCESS)
goto complete_allnds;
dir_entry = DLZ_LIST_NEXT(dir_entry, link);
}
complete_allnds:
if (dir_list != NULL) {
dir_entry = DLZ_LIST_HEAD(*dir_list);
while (dir_entry != NULL) {
next_de = DLZ_LIST_NEXT(dir_entry, link);
free(dir_entry);
dir_entry = next_de;
}
free(dir_list);
}
if (basepath != NULL)
free(basepath);
return (result);
}
#if DLZ_DLOPEN_VERSION < 3
isc_result_t
dlz_findzonedb(void *dbdata, const char *name)
#else
isc_result_t
dlz_findzonedb(void *dbdata, const char *name,
dns_clientinfomethods_t *methods,
dns_clientinfo_t *clientinfo)
#endif
{
isc_result_t result;
config_data_t *cd = (config_data_t *) dbdata;
char *path;
struct stat sb;
path = NULL;
#if DLZ_DLOPEN_VERSION >= 3
UNUSED(methods);
UNUSED(clientinfo);
#endif
if (create_path(name, NULL, NULL, cd, &path) != ISC_R_SUCCESS)
return (ISC_R_NOTFOUND);
cd->log(ISC_LOG_DEBUG(1),
"Filesystem driver Findzone() Checking for path: '%s'\n", path);
if (stat(path, &sb) != 0) {
result = ISC_R_NOTFOUND;
goto complete_FZ;
}
if ((sb.st_mode & S_IFDIR) != 0) {
result = ISC_R_SUCCESS;
goto complete_FZ;
}
result = ISC_R_NOTFOUND;
complete_FZ:
free(path);
return (result);
}
#if DLZ_DLOPEN_VERSION == 1
isc_result_t
dlz_lookup(const char *zone, const char *name,
void *dbdata, dns_sdlzlookup_t *lookup)
#else
isc_result_t
dlz_lookup(const char *zone, const char *name,
void *dbdata, dns_sdlzlookup_t *lookup,
dns_clientinfomethods_t *methods,
dns_clientinfo_t *clientinfo)
#endif
{
isc_result_t result = ISC_R_NOTFOUND;
config_data_t *cd = (config_data_t *) dbdata;
char *path;
struct stat sb;
dir_t dir;
path = NULL;
UNUSED(lookup);
#if DLZ_DLOPEN_VERSION >= 2
UNUSED(methods);
UNUSED(clientinfo);
#endif
if (strcmp(name, "*") == 0)
result = create_path(zone, "-", NULL, cd, &path);
else
result = create_path(zone, name, NULL, cd, &path);
if (result != ISC_R_SUCCESS)
return (ISC_R_NOTFOUND);
path[strlen(path)-1] = '\0';
cd->log(ISC_LOG_DEBUG(1),
"Filesystem driver lookup() Checking for path: '%s'\n", path);
if (stat(path, &sb) != 0) {
result = ISC_R_NOTFOUND;
goto complete_lkup;
}
if ((sb.st_mode & S_IFDIR) == 0) {
result = ISC_R_NOTFOUND;
goto complete_lkup;
}
dir_init(&dir);
result = dir_open(&dir, path);
if (result != ISC_R_SUCCESS) {
cd->log(ISC_LOG_ERROR,
"Unable to open %s directory to read entries.", path);
result = ISC_R_FAILURE;
goto complete_lkup;
}
result = process_dir(&dir, lookup, cd, NULL, 0);
dir_close(&dir);
complete_lkup:
free(path);
return (result);
}
isc_result_t
dlz_create(const char *dlzname, unsigned int argc, char *argv[],
void **dbdata, ...)
{
isc_result_t result = ISC_R_NOMEMORY;
config_data_t *cd;
char *endp;
int len;
char pathsep;
const char *helper_name;
va_list ap;
UNUSED(dlzname);
cd = calloc(1, sizeof(config_data_t));
if (cd == NULL)
goto no_mem;
memset(cd, 0, sizeof(config_data_t));
va_start(ap, dbdata);
while ((helper_name = va_arg(ap, const char*)) != NULL)
b9_add_helper(cd, helper_name, va_arg(ap, void*));
va_end(ap);
if (argc != 6) {
cd->log(ISC_LOG_ERROR,
"Filesystem driver requires "
"6 command line args.");
result = ISC_R_FAILURE;
goto free_cd;
}
if (strlen(argv[5]) > 1) {
cd->log(ISC_LOG_ERROR,
"Filesystem driver can only "
"accept a single character for separator.");
result = ISC_R_FAILURE;
goto free_cd;
}
len = strlen(argv[1]);
if (argv[1][len-1] != '\\' && argv[1][len-1] != '/') {
cd->log(ISC_LOG_ERROR,
"Base dir parameter for filesystem driver "
"should end with %s",
"either '/' or '\\' ");
result = ISC_R_FAILURE;
goto free_cd;
}
if (argv[1][len-1] == '\\')
pathsep = '\\';
else
pathsep = '/';
cd->pathsep = pathsep;
cd->basedir = strdup(argv[1]);
if (cd->basedir == NULL)
goto no_mem;
cd->basedirsize = strlen(cd->basedir);
cd->datadir = strdup(argv[2]);
if (cd->datadir == NULL)
goto no_mem;
cd->datadirsize = strlen(cd->datadir);
cd->xfrdir = strdup(argv[3]);
if (cd->xfrdir == NULL)
goto no_mem;
cd->xfrdirsize = strlen(cd->xfrdir);
cd->splitcnt = strtol(argv[4], &endp, 10);
if (*endp != '\0' || cd->splitcnt < 0)
cd->log(ISC_LOG_ERROR,
"Directory split count must be zero (0) "
"or a postive number");
cd->separator = *argv[5];
*dbdata = cd;
return (ISC_R_SUCCESS);
no_mem:
if (cd != NULL && cd->log != NULL)
cd->log(ISC_LOG_ERROR,
"filesystem_dynamic: Filesystem driver unable to "
"allocate memory for config data.");
free_cd:
if (cd != NULL)
dlz_destroy(cd);
return (result);
}
void
dlz_destroy(void *dbdata) {
config_data_t *cd;
cd = (config_data_t *) dbdata;
if (cd->basedir != NULL)
free(cd->basedir);
if (cd->datadir != NULL)
free(cd->datadir);
if (cd->xfrdir != NULL)
free(cd->xfrdir);
free(cd);
}
int
dlz_version(unsigned int *flags) {
UNUSED(flags);
return (DLZ_DLOPEN_VERSION);
}
static void
b9_add_helper(struct config_data *cd, const char *helper_name, void *ptr) {
if (strcmp(helper_name, "log") == 0)
cd->log = (log_t *)ptr;
if (strcmp(helper_name, "putrr") == 0)
cd->putrr = (dns_sdlz_putrr_t *)ptr;
if (strcmp(helper_name, "putnamedrr") == 0)
cd->putnamedrr = (dns_sdlz_putnamedrr_t *)ptr;
if (strcmp(helper_name, "writeable_zone") == 0)
cd->writeable_zone = (dns_dlz_writeablezone_t *)ptr;
}