localzone.h revision 269257
10SN/A/*
20SN/A * services/localzone.h - local zones authority service.
30SN/A *
40SN/A * Copyright (c) 2007, NLnet Labs. All rights reserved.
50SN/A *
61113Sjoehw * This software is open source.
71113Sjoehw *
81113Sjoehw * Redistribution and use in source and binary forms, with or without
91113Sjoehw * modification, are permitted provided that the following conditions
101113Sjoehw * are met:
111113Sjoehw *
120SN/A * Redistributions of source code must retain the above copyright notice,
131113Sjoehw * this list of conditions and the following disclaimer.
140SN/A *
150SN/A * Redistributions in binary form must reproduce the above copyright notice,
160SN/A * this list of conditions and the following disclaimer in the documentation
170SN/A * and/or other materials provided with the distribution.
180SN/A *
190SN/A * Neither the name of the NLNET LABS nor the names of its contributors may
200SN/A * be used to endorse or promote products derived from this software without
210SN/A * specific prior written permission.
220SN/A *
230SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
240SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
250SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
260SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
270SN/A * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
280SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
290SN/A * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
300SN/A * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
310SN/A * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
320SN/A * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
330SN/A * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
340SN/A */
350SN/A
360SN/A/**
370SN/A * \file
380SN/A *
390SN/A * This file contains functions to enable local zone authority service.
400SN/A */
410SN/A
420SN/A#ifndef SERVICES_LOCALZONE_H
430SN/A#define SERVICES_LOCALZONE_H
440SN/A#include "util/rbtree.h"
450SN/A#include "util/locks.h"
460SN/Astruct ub_packed_rrset_key;
470SN/Astruct regional;
480SN/Astruct config_file;
490SN/Astruct edns_data;
500SN/Astruct query_info;
510SN/Astruct sldns_buffer;
520SN/A
530SN/A/**
540SN/A * Local zone type
550SN/A * This type determines processing for queries that did not match
560SN/A * local-data directly.
570SN/A */
580SN/Aenum localzone_type {
590SN/A	/** drop query */
600SN/A	local_zone_deny = 0,
610SN/A	/** answer with error */
620SN/A	local_zone_refuse,
630SN/A	/** answer nxdomain or nodata */
640SN/A	local_zone_static,
650SN/A	/** resolve normally */
660SN/A	local_zone_transparent,
670SN/A	/** do not block types at localdata names */
680SN/A	local_zone_typetransparent,
690SN/A	/** answer with data at zone apex */
700SN/A	local_zone_redirect,
710SN/A	/** remove default AS112 blocking contents for zone
720SN/A	 * nodefault is used in config not during service. */
730SN/A	local_zone_nodefault
740SN/A};
750SN/A
760SN/A/**
770SN/A * Authoritative local zones storage, shared.
780SN/A */
790SN/Astruct local_zones {
800SN/A	/** lock on the localzone tree */
810SN/A	lock_rw_t lock;
820SN/A	/** rbtree of struct local_zone */
830SN/A	rbtree_t ztree;
840SN/A};
850SN/A
860SN/A/**
870SN/A * Local zone. A locally served authoritative zone.
880SN/A */
890SN/Astruct local_zone {
900SN/A	/** rbtree node, key is name and class */
910SN/A	rbnode_t node;
920SN/A	/** parent zone, if any. */
930SN/A	struct local_zone* parent;
940SN/A
950SN/A	/** zone name, in uncompressed wireformat */
960SN/A	uint8_t* name;
970SN/A	/** length of zone name */
980SN/A	size_t namelen;
990SN/A	/** number of labels in zone name */
1000SN/A	int namelabs;
1010SN/A	/** the class of this zone.
1020SN/A	 * uses 'dclass' to not conflict with c++ keyword class. */
1030SN/A	uint16_t dclass;
1040SN/A
1050SN/A	/** lock on the data in the structure
1060SN/A	 * For the node, parent, name, namelen, namelabs, dclass, you
1070SN/A	 * need to also hold the zones_tree lock to change them (or to
1080SN/A	 * delete this zone) */
1090SN/A	lock_rw_t lock;
1100SN/A
1110SN/A	/** how to process zone */
1120SN/A	enum localzone_type type;
1130SN/A
1140SN/A	/** in this region the zone's data is allocated.
1150SN/A	 * the struct local_zone itself is malloced. */
1160SN/A	struct regional* region;
1170SN/A	/** local data for this zone
1180SN/A	 * rbtree of struct local_data */
1190SN/A	rbtree_t data;
1200SN/A	/** if data contains zone apex SOA data, this is a ptr to it. */
1210SN/A	struct ub_packed_rrset_key* soa;
1220SN/A};
1230SN/A
1240SN/A/**
1250SN/A * Local data. One domain name, and the RRs to go with it.
1260SN/A */
1270SN/Astruct local_data {
1280SN/A	/** rbtree node, key is name only */
1290SN/A	rbnode_t node;
1300SN/A	/** domain name */
1310SN/A	uint8_t* name;
1320SN/A	/** length of name */
1330SN/A	size_t namelen;
1340SN/A	/** number of labels in name */
1350SN/A	int namelabs;
1360SN/A	/** the data rrsets, with different types, linked list.
1370SN/A	 * If this list is NULL, the node is an empty non-terminal. */
1380SN/A	struct local_rrset* rrsets;
1390SN/A};
1400SN/A
1410SN/A/**
1420SN/A * A local data RRset
1430SN/A */
1440SN/Astruct local_rrset {
1450SN/A	/** next in list */
1460SN/A	struct local_rrset* next;
1470SN/A	/** RRset data item */
1480SN/A	struct ub_packed_rrset_key* rrset;
1490SN/A};
1500SN/A
1510SN/A/**
1520SN/A * Create local zones storage
1530SN/A * @return new struct or NULL on error.
1540SN/A */
1550SN/Astruct local_zones* local_zones_create(void);
1560SN/A
1570SN/A/**
1580SN/A * Delete local zones storage
1590SN/A * @param zones: to delete.
1600SN/A */
1610SN/Avoid local_zones_delete(struct local_zones* zones);
1620SN/A
1630SN/A/**
1640SN/A * Apply config settings; setup the local authoritative data.
1650SN/A * Takes care of locking.
1660SN/A * @param zones: is set up.
1670SN/A * @param cfg: config data.
1680SN/A * @return false on error.
1690SN/A */
1700SN/Aint local_zones_apply_cfg(struct local_zones* zones, struct config_file* cfg);
1710SN/A
1720SN/A/**
1730SN/A * Compare two local_zone entries in rbtree. Sort hierarchical but not
1740SN/A * canonical
1750SN/A * @param z1: zone 1
1760SN/A * @param z2: zone 2
1770SN/A * @return: -1, 0, +1 comparison value.
1780SN/A */
1790SN/Aint local_zone_cmp(const void* z1, const void* z2);
1800SN/A
1810SN/A/**
1820SN/A * Compare two local_data entries in rbtree. Sort canonical.
1830SN/A * @param d1: data 1
1840SN/A * @param d2: data 2
1850SN/A * @return: -1, 0, +1 comparison value.
1860SN/A */
1870SN/Aint local_data_cmp(const void* d1, const void* d2);
1880SN/A
1890SN/A/**
1900SN/A * Delete one zone
1910SN/A * @param z: to delete.
1920SN/A */
1930SN/Avoid local_zone_delete(struct local_zone* z);
1940SN/A
1950SN/A/**
1960SN/A * Lookup zone that contains the given name, class.
1970SN/A * User must lock the tree or result zone.
1980SN/A * @param zones: the zones tree
1990SN/A * @param name: dname to lookup
2000SN/A * @param len: length of name.
2010SN/A * @param labs: labelcount of name.
2020SN/A * @param dclass: class to lookup.
2030SN/A * @return closest local_zone or NULL if no covering zone is found.
2040SN/A */
2050SN/Astruct local_zone* local_zones_lookup(struct local_zones* zones,
2060SN/A	uint8_t* name, size_t len, int labs, uint16_t dclass);
2070SN/A
2080SN/A/**
2090SN/A * Debug helper. Print all zones
2100SN/A * Takes care of locking.
2110SN/A * @param zones: the zones tree
2120SN/A */
2130SN/Avoid local_zones_print(struct local_zones* zones);
2140SN/A
2150SN/A/**
2160SN/A * Answer authoritatively for local zones.
2170SN/A * Takes care of locking.
2180SN/A * @param zones: the stored zones (shared, read only).
2190SN/A * @param qinfo: query info (parsed).
2200SN/A * @param edns: edns info (parsed).
2210SN/A * @param buf: buffer with query ID and flags, also for reply.
2220SN/A * @param temp: temporary storage region.
2230SN/A * @return true if answer is in buffer. false if query is not answered
2240SN/A * by authority data. If the reply should be dropped altogether, the return
2250SN/A * value is true, but the buffer is cleared (empty).
2260SN/A */
2270SN/Aint local_zones_answer(struct local_zones* zones, struct query_info* qinfo,
2280SN/A	struct edns_data* edns, struct sldns_buffer* buf, struct regional* temp);
2290SN/A
2300SN/A/**
2310SN/A * Parse the string into localzone type.
2320SN/A *
2330SN/A * @param str: string to parse
2340SN/A * @param t: local zone type returned here.
2350SN/A * @return 0 on parse error.
2360SN/A */
2370SN/Aint local_zone_str2type(const char* str, enum localzone_type* t);
2380SN/A
2390SN/A/**
2400SN/A * Print localzone type to a string.  Pointer to a constant string.
2410SN/A *
2420SN/A * @param t: local zone type.
2430SN/A * @return constant string that describes type.
2440SN/A */
2450SN/Aconst char* local_zone_type2str(enum localzone_type t);
2460SN/A
2470SN/A/**
2480SN/A * Find zone that with exactly given name, class.
2490SN/A * User must lock the tree or result zone.
2500SN/A * @param zones: the zones tree
2510SN/A * @param name: dname to lookup
2520SN/A * @param len: length of name.
2530SN/A * @param labs: labelcount of name.
2540SN/A * @param dclass: class to lookup.
2550SN/A * @return the exact local_zone or NULL.
2560SN/A */
2570SN/Astruct local_zone* local_zones_find(struct local_zones* zones,
2580SN/A	uint8_t* name, size_t len, int labs, uint16_t dclass);
2590SN/A
2600SN/A/**
2610SN/A * Add a new zone. Caller must hold the zones lock.
2620SN/A * Adjusts the other zones as well (parent pointers) after insertion.
2630SN/A * The zone must NOT exist (returns NULL and logs error).
2640SN/A * @param zones: the zones tree
2650SN/A * @param name: dname to add
2660SN/A * @param len: length of name.
2670SN/A * @param labs: labelcount of name.
2680SN/A * @param dclass: class to add.
2690SN/A * @param tp: type.
2700SN/A * @return local_zone or NULL on error, caller must printout memory error.
2710SN/A */
2720SN/Astruct local_zone* local_zones_add_zone(struct local_zones* zones,
2730SN/A	uint8_t* name, size_t len, int labs, uint16_t dclass,
2740SN/A	enum localzone_type tp);
2750SN/A
2760SN/A/**
2770SN/A * Delete a zone. Caller must hold the zones lock.
2780SN/A * Adjusts the other zones as well (parent pointers) after insertion.
2790SN/A * @param zones: the zones tree
2800SN/A * @param zone: the zone to delete from tree. Also deletes zone from memory.
2810SN/A */
2820SN/Avoid local_zones_del_zone(struct local_zones* zones, struct local_zone* zone);
2830SN/A
2840SN/A/**
2850SN/A * Add RR data into the localzone data.
2860SN/A * Looks up the zone, if no covering zone, a transparent zone with the
2870SN/A * name of the RR is created.
2880SN/A * @param zones: the zones tree. Not locked by caller.
2890SN/A * @param rr: string with on RR.
2900SN/A * @return false on failure.
2910SN/A */
2920SN/Aint local_zones_add_RR(struct local_zones* zones, const char* rr);
2930SN/A
2940SN/A/**
2950SN/A * Remove data from domain name in the tree.
2960SN/A * All types are removed. No effect if zone or name does not exist.
2970SN/A * @param zones: zones tree.
2980SN/A * @param name: dname to remove
2990SN/A * @param len: length of name.
3000SN/A * @param labs: labelcount of name.
3010SN/A * @param dclass: class to remove.
3020SN/A */
3030SN/Avoid local_zones_del_data(struct local_zones* zones,
3040SN/A	uint8_t* name, size_t len, int labs, uint16_t dclass);
3050SN/A
3060SN/A
3070SN/A/**
3080SN/A * Form wireformat from text format domain name.
3090SN/A * @param str: the domain name in text "www.example.com"
3100SN/A * @param res: resulting wireformat is stored here with malloc.
3110SN/A * @param len: length of resulting wireformat.
3120SN/A * @param labs: number of labels in resulting wireformat.
3130SN/A * @return false on error, syntax or memory. Also logged.
3140SN/A */
3150SN/Aint parse_dname(const char* str, uint8_t** res, size_t* len, int* labs);
3160SN/A
3170SN/A#endif /* SERVICES_LOCALZONE_H */
3180SN/A