Commit bae88e1c authored by Libor Peltan's avatar Libor Peltan

libdnssec: deleted unused kasp_dir_*

parent b54dc864
......@@ -88,11 +88,6 @@ src/dnssec/lib/dnssec/random.h
src/dnssec/lib/dnssec/sign.h
src/dnssec/lib/dnssec/tsig.h
src/dnssec/lib/error.c
src/dnssec/lib/kasp/dir/escape.c
src/dnssec/lib/kasp/dir/escape.h
src/dnssec/lib/kasp/dir/file.c
src/dnssec/lib/kasp/dir/file.h
src/dnssec/lib/kasp/internal.h
src/dnssec/lib/kasp/policy.c
src/dnssec/lib/key/algorithm.c
src/dnssec/lib/key/algorithm.h
......
......@@ -73,11 +73,6 @@ libdnssec_la_SOURCES = \
lib/binary.c \
lib/crypto.c \
lib/error.c \
lib/kasp/dir/escape.c \
lib/kasp/dir/escape.h \
lib/kasp/dir/file.c \
lib/kasp/dir/file.h \
lib/kasp/internal.h \
lib/kasp/policy.c \
lib/key/algorithm.c \
lib/key/algorithm.h \
......
/* Copyright (C) 2014 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include "error.h"
#include "kasp/dir/escape.h"
#include "wire.h"
/*!
* Convert ASCII upper case character to lowercase one.
*/
static char ascii_tolower(char chr)
{
if ('A' <= chr && chr <= 'Z') {
return chr - 'A' + 'a';
} else {
return chr;
}
}
/*!
* Check if char is safe and should be written without escaping.
*/
static bool is_safe(char chr)
{
return ('a' <= chr && chr <= 'z') ||
('0' <= chr && chr <= '9') ||
(chr == '.' || chr == '-' || chr == '_');
}
/*!
* Write one safe byte.
*/
static int write_safe(wire_ctx_t *dest, char chr)
{
if (wire_available(dest) < 1) {
return DNSSEC_ENOMEM;
}
wire_write_u8(dest, chr);
return DNSSEC_EOK;
}
/*!
* Escape one unsafe byte.
*/
static int write_unsafe(wire_ctx_t *dest, char chr)
{
if (wire_available(dest) < 4) {
return DNSSEC_ENOMEM;
}
char buffer[5] = { 0 };
int written = snprintf(buffer, sizeof(buffer), "\\x%02x", (unsigned char)chr);
if (written != 4) {
return DNSSEC_ERROR;
}
wire_write(dest, (uint8_t *)buffer, written);
return DNSSEC_EOK;
}
/*!
* Read one safe byte during unescaping.
*/
static int read_safe(wire_ctx_t *dest, char chr)
{
if (!is_safe(chr)) {
return DNSSEC_MALFORMED_DATA;
}
return write_safe(dest, chr);
}
/*!
* Unescape one unsafe byte.
*
* Initial slash is already processed.
*/
static int read_unsafe(wire_ctx_t *dest, wire_ctx_t *src)
{
if (wire_available(dest) < 1 || wire_available(src) < 3) {
return DNSSEC_ENOMEM;
}
char buffer[3] = { 0 };
wire_read(src, (uint8_t *)buffer, sizeof(buffer));
unsigned value = 0;
int read = sscanf(buffer, "x%02x", &value);
if (read != 1 || value == 0) {
return DNSSEC_MALFORMED_DATA;
}
assert(value <= UINT8_MAX);
wire_write_u8(dest, value);
return DNSSEC_EOK;
}
/*!
* Filtering function type signature.
*/
typedef int (*filter_cb)(wire_ctx_t *, wire_ctx_t *);
/*!
* Escaping filter.
*/
static int filter_escape(wire_ctx_t *src, wire_ctx_t *dest)
{
char chr = wire_read_u8(src);
chr = ascii_tolower(chr);
if (is_safe(chr)) {
return write_safe(dest, chr);
} else {
return write_unsafe(dest, chr);
}
}
/*!
* Unescaping filter.
*/
static int filter_unescape(wire_ctx_t *src, wire_ctx_t *dest)
{
char chr = wire_read_u8(src);
if (chr != '\\') {
return read_safe(dest, chr);
} else {
return read_unsafe(dest, src);
}
}
/*!
* Convert one buffer to another using filtering function.
*/
static int filter_ctx(wire_ctx_t *src, wire_ctx_t *dest, filter_cb filter)
{
while (wire_available(src) > 0) {
int result = filter(src, dest);
if (result != DNSSEC_EOK) {
return result;
}
}
return DNSSEC_EOK;
}
/*!
* Create string from wire context.
*/
static int ctx_to_str(wire_ctx_t *src, char **result)
{
size_t len = wire_tell(src);
char *str = malloc(len + 1);
if (!str) {
return DNSSEC_ENOMEM;
}
memcpy(str, src->wire, len);
str[len] = '\0';
*result = str;
return DNSSEC_EOK;
}
/*!
* Convert string to another using a filtering function.
*/
static int filter_str(const char *input, char **output, filter_cb filter)
{
assert(input);
assert(output);
assert(filter);
uint8_t buffer[256] = { 0 };
wire_ctx_t in_ctx = wire_init((uint8_t *)input, strlen(input));
wire_ctx_t out_ctx = wire_init(buffer, sizeof(buffer));
int r = filter_ctx(&in_ctx, &out_ctx, filter);
if (r != DNSSEC_EOK) {
return r;
}
return ctx_to_str(&out_ctx, output);
}
/* -- internal API --------------------------------------------------------- */
int escape_entity_name(const char *name, char **escaped)
{
return filter_str(name, escaped, filter_escape);
}
int unescape_entity_name(const char *escaped, char **name)
{
return filter_str(escaped, name, filter_unescape);
}
/* Copyright (C) 2014 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
/*!
* Escape entity name to be safely included into a file name.
*
* A-Z letters are converted to lower case.
*
* ASCII letters (a-z), numbers (0-9), dot (.), dash (-), and underscore (_)
* are preserved.
*
* Other characters are written as a sequence '\x..', where .. is hex
* representation of the char.
*
* \param[in] name Entity name to be encoded.
* \param[out] escaped Allocated escaped zone name.
*
* \return Error code, DNSSEC_EOK if successful.
*/
int escape_entity_name(const char *name, char **escaped);
/*!
* Reverse function of \ref escape_entity_name.
*/
int unescape_entity_name(const char *escaped, char **name);
/* Copyright (C) 2014 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include "error.h"
#include "kasp/dir/escape.h"
#include "shared.h"
static const char *SUFFIX = ".json";
/* -- internal API --------------------------------------------------------- */
/*!
* Get zone configuration file name.
*/
char *file_from_entity(const char *dir, const char *type, const char *name)
{
assert(dir);
assert(type);
assert(name);
// escape entity name
_cleanup_free_ char *escaped = NULL;
int r = escape_entity_name(name, &escaped);
if (r != DNSSEC_EOK) {
return NULL;
}
// build full path
char *config = NULL;
int result = asprintf(&config, "%s/%s_%s%s", dir, type, escaped, SUFFIX);
if (result == -1) {
return NULL;
}
return config;
}
/*!
* Get a configuration entity name from a file name.
*/
char *file_to_entity(const char *type, const char *basename)
{
assert(type);
assert(basename);
// basename components
size_t basename_len = strlen(basename);
size_t prefix_len = strlen(type) + 1;
size_t suffix_len = strlen(SUFFIX);
if (basename_len < prefix_len + suffix_len) {
return NULL;
}
size_t name_len = basename_len - suffix_len - prefix_len;
const char *basename_prefix = basename;
const char *basename_name = basename_prefix + prefix_len;
const char *basename_suffix = basename_name + name_len;
// prefix and suffix match
char prefix[prefix_len + 1];
if (snprintf(prefix, sizeof(prefix), "%s_", type) != prefix_len) {
return NULL;
}
if (memcmp(basename_prefix, prefix, prefix_len) != 0 ||
memcmp(basename_suffix, SUFFIX, suffix_len) != 0
) {
return NULL;
}
// unescape zone name
_cleanup_free_ char *escaped = strndup(basename_name, name_len);
if (!escaped) {
return NULL;
}
char *name = NULL;
int r = unescape_entity_name(escaped, &name);
if (r != DNSSEC_EOK) {
free(name);
return NULL;
}
return name;
}
/* Copyright (C) 2014 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
/*!
* Get a file name from a configuration entity name.
*
* \param dir Destination directory.
* \param type Entity type.
* \param name Entity name.
*
* \return File name '<dir>/<type>_<escaped-name>.json'
*/
char *file_from_entity(const char *dir, const char *type, const char *name);
/*!
* Get a configuration entity name from a file name.
*
* \param type Entity type.
* \param basename Base name of the configuration file.
*
* \return Unescaped entity name from a basename.
*/
char *file_to_entity(const char *type, const char *basename);
/* Copyright (C) 2014 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "kasp.h"
/*!
* Free content of the keystore structure.
*/
void kasp_keystore_cleanup(dnssec_kasp_keystore_t *keystore);
......@@ -5,8 +5,6 @@
# test binaries
/binary
/crypto
/kasp_dir_escape
/kasp_dir_file
/kasp_policy
/key
/key_algorithm
......
......@@ -20,8 +20,6 @@ EXTRA_DIST = sample_keys.h
check_PROGRAMS = \
binary \
crypto \
kasp_dir_escape \
kasp_dir_file \
kasp_policy \
key \
key_algorithm \
......
/* Copyright (C) 2014 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <string.h>
#include <tap/basic.h>
#include "error.h"
#include "kasp/dir/escape.h"
static void test_function(int (*function)(const char *, char **),
const char *function_name,
const char *input, const char *expected)
{
char *output = NULL;
int result = function(input, &output);
if (result == DNSSEC_EOK) {
ok(output && expected && strcmp(output, expected) == 0,
"%s on '%s' succeeds", function_name, input);
} else {
ok(output == NULL && expected == NULL,
"%s on '%s' fails", function_name, input);
}
free(output);
}
static void test_escape(const char *input, const char *expected)
{
test_function(escape_entity_name, "escape", input, expected);
}
static void test_unescape(const char *input, const char *expected)
{
test_function(unescape_entity_name, "unescape", input, expected);
}
int main(int argc, char *argv[])
{
plan_lazy();
// escape safe strings
test_escape("", "");
test_escape("abc-xyz_012-789.com", "abc-xyz_012-789.com");
test_escape("EXAMple.com", "example.com");
test_escape("_.-AZaz09", "_.-azaz09");
// escape unsafe strings
test_escape("abc\\def", "abc\\x5cdef");
test_escape("xyz/jkl", "xyz\\x2fjkl");
test_escape("aaa?bbb#ccc*ddd", "aaa\\x3fbbb\\x23ccc\\x2addd");
test_escape("!$%&()", "\\x21\\x24\\x25\\x26\\x28\\x29");
// test safe unescape
test_unescape("", "");
test_unescape("abc-xyz_012-789.com", "abc-xyz_012-789.com");
test_unescape("examPLE.net", NULL);
test_unescape("@", NULL);
// test unsafe unescape
test_unescape("opq\\x40rst", "opq@rst");
test_unescape("_\\x40-42\\x2eaz", "_@-42.az");
test_unescape("\\x3f\\x2a\\x28\\x3d\\x22\\x27\\x5c\\x2f", "?*(=\"\'\\/");
test_unescape("new\\x0aline", "new\nline");
// invalid unsafe unescape
test_unescape("a\\", NULL);
test_unescape("a\\z", NULL);
test_unescape("a\\x", NULL);
test_unescape("a\\x00b", NULL);
test_unescape("a\\xgg", NULL);
// unicode escape
test_escape("křemílek.cz", "k\\xc5\\x99em\\xc3\\xadlek.cz");
test_escape("www.vochomůrka", "www.vochom\\xc5\\xafrka");
// unicode unescape
test_unescape("br\\xc4\\x8d\\xc3\\xa1ln\\xc3\\xadk.eu", "brčálník.eu");
test_unescape("\\xc4\\x8dty\\xc5\\x99i.\\xc5\\xa1neci", "čtyři.šneci");
return 0;
}
/* Copyright (C) 2014 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <tap/basic.h>
#include "kasp/dir/file.h"
struct test {
const char *dir;
const char *type;
const char *name;
const char *expected;
};
static const struct test TESTS[] = {
{ "kasp", "zone", "example.com", "kasp/zone_example.com.json" },
{ "zo/ne", "zone", "test@zone", "zo/ne/zone_test\\x40zone.json" },
{ "kasp", "policy", "default", "kasp/policy_default.json" },
{ NULL }
};
int main(int argc, char *argv[])
{
plan_lazy();
for (const struct test *t = TESTS; t->dir; t++) {
char *file = file_from_entity(t->dir, t->type, t->name);
is_string(t->expected, file, "file_from_entity(%s, %s, %s)",
t->dir, t->type, t->name);
free(file);
const char *basename = rindex(t->expected, '/');
assert(basename);
basename += 1;
char *entity = file_to_entity(t->type, basename);
is_string(t->name, entity, "file_to_entity(%s, %s)",
t->type, basename);
free(entity);
}
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment