Home | History | Annotate | Line # | Download | only in ipsecmod
      1      1.1  christos /*
      2      1.1  christos  * ipsecmod/ipsecmod.c - facilitate opportunistic IPsec module
      3      1.1  christos  *
      4      1.1  christos  * Copyright (c) 2017, NLnet Labs. All rights reserved.
      5      1.1  christos  *
      6      1.1  christos  * This software is open source.
      7      1.1  christos  *
      8      1.1  christos  * Redistribution and use in source and binary forms, with or without
      9      1.1  christos  * modification, are permitted provided that the following conditions
     10      1.1  christos  * are met:
     11      1.1  christos  *
     12      1.1  christos  * Redistributions of source code must retain the above copyright notice,
     13      1.1  christos  * this list of conditions and the following disclaimer.
     14      1.1  christos  *
     15      1.1  christos  * Redistributions in binary form must reproduce the above copyright notice,
     16      1.1  christos  * this list of conditions and the following disclaimer in the documentation
     17      1.1  christos  * and/or other materials provided with the distribution.
     18      1.1  christos  *
     19      1.1  christos  * Neither the name of the NLNET LABS nor the names of its contributors may
     20      1.1  christos  * be used to endorse or promote products derived from this software without
     21      1.1  christos  * specific prior written permission.
     22      1.1  christos  *
     23      1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     24      1.1  christos  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     25      1.1  christos  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     26      1.1  christos  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     27      1.1  christos  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     28      1.1  christos  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
     29      1.1  christos  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     30      1.1  christos  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     31      1.1  christos  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     32      1.1  christos  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     33      1.1  christos  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     34      1.1  christos  */
     35      1.1  christos 
     36      1.1  christos /**
     37      1.1  christos  * \file
     38      1.1  christos  *
     39      1.1  christos  * This file contains a module that facilitates opportunistic IPsec. It does so
     40  1.1.1.4  christos  * by also querying for the IPSECKEY for A/AAAA queries and calling a
     41      1.1  christos  * configurable hook (eg. signaling an IKE daemon) before replying.
     42      1.1  christos  */
     43      1.1  christos 
     44      1.1  christos #include "config.h"
     45      1.1  christos #ifdef USE_IPSECMOD
     46      1.1  christos #include "ipsecmod/ipsecmod.h"
     47      1.1  christos #include "ipsecmod/ipsecmod-whitelist.h"
     48      1.1  christos #include "util/fptr_wlist.h"
     49      1.1  christos #include "util/regional.h"
     50      1.1  christos #include "util/net_help.h"
     51      1.1  christos #include "util/config_file.h"
     52      1.1  christos #include "services/cache/dns.h"
     53      1.1  christos #include "sldns/wire2str.h"
     54      1.1  christos 
     55      1.1  christos /** Apply configuration to ipsecmod module 'global' state. */
     56      1.1  christos static int
     57      1.1  christos ipsecmod_apply_cfg(struct ipsecmod_env* ipsecmod_env, struct config_file* cfg)
     58      1.1  christos {
     59      1.1  christos 	if(!cfg->ipsecmod_hook || (cfg->ipsecmod_hook && !cfg->ipsecmod_hook[0])) {
     60      1.1  christos 		log_err("ipsecmod: missing ipsecmod-hook.");
     61      1.1  christos 		return 0;
     62      1.1  christos 	}
     63      1.1  christos 	if(cfg->ipsecmod_whitelist &&
     64      1.1  christos 		!ipsecmod_whitelist_apply_cfg(ipsecmod_env, cfg))
     65      1.1  christos 		return 0;
     66      1.1  christos 	return 1;
     67      1.1  christos }
     68      1.1  christos 
     69      1.1  christos int
     70      1.1  christos ipsecmod_init(struct module_env* env, int id)
     71      1.1  christos {
     72      1.1  christos 	struct ipsecmod_env* ipsecmod_env = (struct ipsecmod_env*)calloc(1,
     73      1.1  christos 		sizeof(struct ipsecmod_env));
     74      1.1  christos 	if(!ipsecmod_env) {
     75      1.1  christos 		log_err("malloc failure");
     76      1.1  christos 		return 0;
     77      1.1  christos 	}
     78      1.1  christos 	env->modinfo[id] = (void*)ipsecmod_env;
     79      1.1  christos 	ipsecmod_env->whitelist = NULL;
     80      1.1  christos 	if(!ipsecmod_apply_cfg(ipsecmod_env, env->cfg)) {
     81      1.1  christos 		log_err("ipsecmod: could not apply configuration settings.");
     82      1.1  christos 		return 0;
     83      1.1  christos 	}
     84      1.1  christos 	return 1;
     85      1.1  christos }
     86      1.1  christos 
     87      1.1  christos void
     88      1.1  christos ipsecmod_deinit(struct module_env* env, int id)
     89      1.1  christos {
     90      1.1  christos 	struct ipsecmod_env* ipsecmod_env;
     91      1.1  christos 	if(!env || !env->modinfo[id])
     92      1.1  christos 		return;
     93      1.1  christos 	ipsecmod_env = (struct ipsecmod_env*)env->modinfo[id];
     94      1.1  christos 	/* Free contents. */
     95      1.1  christos 	ipsecmod_whitelist_delete(ipsecmod_env->whitelist);
     96      1.1  christos 	free(ipsecmod_env);
     97      1.1  christos 	env->modinfo[id] = NULL;
     98      1.1  christos }
     99      1.1  christos 
    100      1.1  christos /** New query for ipsecmod. */
    101      1.1  christos static int
    102      1.1  christos ipsecmod_new(struct module_qstate* qstate, int id)
    103      1.1  christos {
    104      1.1  christos 	struct ipsecmod_qstate* iq = (struct ipsecmod_qstate*)regional_alloc(
    105      1.1  christos 		qstate->region, sizeof(struct ipsecmod_qstate));
    106      1.1  christos 	qstate->minfo[id] = iq;
    107      1.1  christos 	if(!iq)
    108      1.1  christos 		return 0;
    109      1.1  christos 	/* Initialise it. */
    110  1.1.1.3  christos 	memset(iq, 0, sizeof(*iq));
    111      1.1  christos 	iq->enabled = qstate->env->cfg->ipsecmod_enabled;
    112      1.1  christos 	iq->is_whitelisted = ipsecmod_domain_is_whitelisted(
    113      1.1  christos 		(struct ipsecmod_env*)qstate->env->modinfo[id], qstate->qinfo.qname,
    114      1.1  christos 		qstate->qinfo.qname_len, qstate->qinfo.qclass);
    115      1.1  christos 	return 1;
    116      1.1  christos }
    117      1.1  christos 
    118      1.1  christos /**
    119      1.1  christos  * Exit module with an error status.
    120      1.1  christos  * @param qstate: query state
    121      1.1  christos  * @param id: module id.
    122      1.1  christos  */
    123      1.1  christos static void
    124      1.1  christos ipsecmod_error(struct module_qstate* qstate, int id)
    125      1.1  christos {
    126      1.1  christos 	qstate->ext_state[id] = module_error;
    127      1.1  christos 	qstate->return_rcode = LDNS_RCODE_SERVFAIL;
    128      1.1  christos }
    129      1.1  christos 
    130      1.1  christos /**
    131      1.1  christos  * Generate a request for the IPSECKEY.
    132      1.1  christos  *
    133      1.1  christos  * @param qstate: query state that is the parent.
    134      1.1  christos  * @param id: module id.
    135      1.1  christos  * @param name: what name to query for.
    136      1.1  christos  * @param namelen: length of name.
    137      1.1  christos  * @param qtype: query type.
    138      1.1  christos  * @param qclass: query class.
    139      1.1  christos  * @param flags: additional flags, such as the CD bit (BIT_CD), or 0.
    140      1.1  christos  * @return false on alloc failure.
    141      1.1  christos  */
    142      1.1  christos static int
    143      1.1  christos generate_request(struct module_qstate* qstate, int id, uint8_t* name,
    144      1.1  christos 	size_t namelen, uint16_t qtype, uint16_t qclass, uint16_t flags)
    145      1.1  christos {
    146      1.1  christos 	struct module_qstate* newq;
    147      1.1  christos 	struct query_info ask;
    148      1.1  christos 	ask.qname = name;
    149      1.1  christos 	ask.qname_len = namelen;
    150      1.1  christos 	ask.qtype = qtype;
    151      1.1  christos 	ask.qclass = qclass;
    152      1.1  christos 	ask.local_alias = NULL;
    153      1.1  christos 	log_query_info(VERB_ALGO, "ipsecmod: generate request", &ask);
    154  1.1.1.4  christos 
    155  1.1.1.4  christos 	/* Explicitly check for cycle before trying to attach. Will result in
    156  1.1.1.4  christos 	 * cleaner error message. The attach_sub code also checks for cycle but the
    157  1.1.1.4  christos 	 * message will be out of memory in both cases then. */
    158  1.1.1.4  christos 	fptr_ok(fptr_whitelist_modenv_detect_cycle(qstate->env->detect_cycle));
    159  1.1.1.4  christos 	if((*qstate->env->detect_cycle)(qstate, &ask,
    160  1.1.1.4  christos 		(uint16_t)(BIT_RD|flags), 0, 0)) {
    161  1.1.1.4  christos 		verbose(VERB_ALGO, "Could not generate request: cycle detected");
    162  1.1.1.4  christos 		return 0;
    163  1.1.1.4  christos 	}
    164  1.1.1.4  christos 
    165      1.1  christos 	fptr_ok(fptr_whitelist_modenv_attach_sub(qstate->env->attach_sub));
    166      1.1  christos 	if(!(*qstate->env->attach_sub)(qstate, &ask,
    167      1.1  christos 		(uint16_t)(BIT_RD|flags), 0, 0, &newq)){
    168      1.1  christos 		log_err("Could not generate request: out of memory");
    169      1.1  christos 		return 0;
    170      1.1  christos 	}
    171      1.1  christos 	qstate->ext_state[id] = module_wait_subquery;
    172      1.1  christos 	return 1;
    173      1.1  christos }
    174      1.1  christos 
    175      1.1  christos /**
    176  1.1.1.3  christos  * Check if the string passed is a valid domain name with safe characters to
    177  1.1.1.3  christos  * pass to a shell.
    178  1.1.1.3  christos  * This will only allow:
    179  1.1.1.3  christos  *  - digits
    180  1.1.1.3  christos  *  - alphas
    181  1.1.1.3  christos  *  - hyphen (not at the start)
    182  1.1.1.3  christos  *  - dot (not at the start, or the only character)
    183  1.1.1.3  christos  *  - underscore
    184  1.1.1.3  christos  * @param s: pointer to the string.
    185  1.1.1.3  christos  * @param slen: string's length.
    186  1.1.1.3  christos  * @return true if s only contains safe characters; false otherwise.
    187  1.1.1.3  christos  */
    188  1.1.1.3  christos static int
    189  1.1.1.3  christos domainname_has_safe_characters(char* s, size_t slen) {
    190  1.1.1.3  christos 	size_t i;
    191  1.1.1.3  christos 	for(i = 0; i < slen; i++) {
    192  1.1.1.3  christos 		if(s[i] == '\0') return 1;
    193  1.1.1.3  christos 		if((s[i] == '-' && i != 0)
    194  1.1.1.3  christos 			|| (s[i] == '.' && (i != 0 || s[1] == '\0'))
    195  1.1.1.3  christos 			|| (s[i] == '_') || (s[i] >= '0' && s[i] <= '9')
    196  1.1.1.3  christos 			|| (s[i] >= 'A' && s[i] <= 'Z')
    197  1.1.1.3  christos 			|| (s[i] >= 'a' && s[i] <= 'z')) {
    198  1.1.1.3  christos 			continue;
    199  1.1.1.3  christos 		}
    200  1.1.1.3  christos 		return 0;
    201  1.1.1.3  christos 	}
    202  1.1.1.3  christos 	return 1;
    203  1.1.1.3  christos }
    204  1.1.1.3  christos 
    205  1.1.1.3  christos /**
    206  1.1.1.3  christos  * Check if the stringified IPSECKEY RDATA contains safe characters to pass to
    207  1.1.1.3  christos  * a shell.
    208  1.1.1.3  christos  * This is only relevant for checking the gateway when the gateway type is 3
    209  1.1.1.3  christos  * (domainname).
    210  1.1.1.3  christos  * @param s: pointer to the string.
    211  1.1.1.3  christos  * @param slen: string's length.
    212  1.1.1.3  christos  * @return true if s contains only safe characters; false otherwise.
    213  1.1.1.3  christos  */
    214  1.1.1.3  christos static int
    215  1.1.1.3  christos ipseckey_has_safe_characters(char* s, size_t slen) {
    216  1.1.1.3  christos 	int precedence, gateway_type, algorithm;
    217  1.1.1.3  christos 	char* gateway;
    218  1.1.1.3  christos 	gateway = (char*)calloc(slen, sizeof(char));
    219  1.1.1.3  christos 	if(!gateway) {
    220  1.1.1.3  christos 		log_err("ipsecmod: out of memory when calling the hook");
    221  1.1.1.3  christos 		return 0;
    222  1.1.1.3  christos 	}
    223  1.1.1.3  christos 	if(sscanf(s, "%d %d %d %s ",
    224  1.1.1.3  christos 			&precedence, &gateway_type, &algorithm, gateway) != 4) {
    225  1.1.1.3  christos 		free(gateway);
    226  1.1.1.3  christos 		return 0;
    227  1.1.1.3  christos 	}
    228  1.1.1.3  christos 	if(gateway_type != 3) {
    229  1.1.1.3  christos 		free(gateway);
    230  1.1.1.3  christos 		return 1;
    231  1.1.1.3  christos 	}
    232  1.1.1.3  christos 	if(domainname_has_safe_characters(gateway, slen)) {
    233  1.1.1.3  christos 		free(gateway);
    234  1.1.1.3  christos 		return 1;
    235  1.1.1.3  christos 	}
    236  1.1.1.3  christos 	free(gateway);
    237  1.1.1.3  christos 	return 0;
    238  1.1.1.3  christos }
    239  1.1.1.3  christos 
    240  1.1.1.3  christos /**
    241      1.1  christos  *  Prepare the data and call the hook.
    242      1.1  christos  *
    243      1.1  christos  *  @param qstate: query state.
    244      1.1  christos  *  @param iq: ipsecmod qstate.
    245      1.1  christos  *  @param ie: ipsecmod environment.
    246      1.1  christos  *  @return true on success, false otherwise.
    247      1.1  christos  */
    248      1.1  christos static int
    249      1.1  christos call_hook(struct module_qstate* qstate, struct ipsecmod_qstate* iq,
    250      1.1  christos 	struct ipsecmod_env* ATTR_UNUSED(ie))
    251      1.1  christos {
    252      1.1  christos 	size_t slen, tempdata_len, tempstring_len, i;
    253      1.1  christos 	char str[65535], *s, *tempstring;
    254  1.1.1.3  christos 	int w = 0, w_temp, qtype;
    255      1.1  christos 	struct ub_packed_rrset_key* rrset_key;
    256      1.1  christos 	struct packed_rrset_data* rrset_data;
    257      1.1  christos 	uint8_t *tempdata;
    258      1.1  christos 
    259      1.1  christos 	/* Check if a shell is available */
    260      1.1  christos 	if(system(NULL) == 0) {
    261      1.1  christos 		log_err("ipsecmod: no shell available for ipsecmod-hook");
    262      1.1  christos 		return 0;
    263      1.1  christos 	}
    264      1.1  christos 
    265      1.1  christos 	/* Zero the buffer. */
    266      1.1  christos 	s = str;
    267      1.1  christos 	slen = sizeof(str);
    268      1.1  christos 	memset(s, 0, slen);
    269      1.1  christos 
    270      1.1  christos 	/* Copy the hook into the buffer. */
    271  1.1.1.3  christos 	w += sldns_str_print(&s, &slen, "%s", qstate->env->cfg->ipsecmod_hook);
    272      1.1  christos 	/* Put space into the buffer. */
    273  1.1.1.3  christos 	w += sldns_str_print(&s, &slen, " ");
    274      1.1  christos 	/* Copy the qname into the buffer. */
    275      1.1  christos 	tempstring = sldns_wire2str_dname(qstate->qinfo.qname,
    276      1.1  christos 		qstate->qinfo.qname_len);
    277      1.1  christos 	if(!tempstring) {
    278      1.1  christos 		log_err("ipsecmod: out of memory when calling the hook");
    279      1.1  christos 		return 0;
    280      1.1  christos 	}
    281  1.1.1.3  christos 	if(!domainname_has_safe_characters(tempstring, strlen(tempstring))) {
    282  1.1.1.3  christos 		log_err("ipsecmod: qname has unsafe characters");
    283  1.1.1.3  christos 		free(tempstring);
    284  1.1.1.3  christos 		return 0;
    285  1.1.1.3  christos 	}
    286  1.1.1.3  christos 	w += sldns_str_print(&s, &slen, "\"%s\"", tempstring);
    287      1.1  christos 	free(tempstring);
    288      1.1  christos 	/* Put space into the buffer. */
    289  1.1.1.3  christos 	w += sldns_str_print(&s, &slen, " ");
    290      1.1  christos 	/* Copy the IPSECKEY TTL into the buffer. */
    291      1.1  christos 	rrset_data = (struct packed_rrset_data*)iq->ipseckey_rrset->entry.data;
    292  1.1.1.3  christos 	w += sldns_str_print(&s, &slen, "\"%ld\"", (long)rrset_data->ttl);
    293      1.1  christos 	/* Put space into the buffer. */
    294  1.1.1.3  christos 	w += sldns_str_print(&s, &slen, " ");
    295      1.1  christos 	rrset_key = reply_find_answer_rrset(&qstate->return_msg->qinfo,
    296      1.1  christos 		qstate->return_msg->rep);
    297  1.1.1.3  christos 	/* Double check that the records are indeed A/AAAA.
    298  1.1.1.3  christos 	 * This should never happen as this function is only executed for A/AAAA
    299  1.1.1.3  christos 	 * queries but make sure we don't pass anything other than A/AAAA to the
    300  1.1.1.3  christos 	 * shell. */
    301  1.1.1.3  christos 	qtype = ntohs(rrset_key->rk.type);
    302  1.1.1.3  christos 	if(qtype != LDNS_RR_TYPE_AAAA && qtype != LDNS_RR_TYPE_A) {
    303  1.1.1.3  christos 		log_err("ipsecmod: Answer is not of A or AAAA type");
    304  1.1.1.3  christos 		return 0;
    305  1.1.1.3  christos 	}
    306      1.1  christos 	rrset_data = (struct packed_rrset_data*)rrset_key->entry.data;
    307  1.1.1.3  christos 	/* Copy the A/AAAA record(s) into the buffer. Start and end this section
    308  1.1.1.3  christos 	 * with a double quote. */
    309  1.1.1.3  christos 	w += sldns_str_print(&s, &slen, "\"");
    310      1.1  christos 	for(i=0; i<rrset_data->count; i++) {
    311      1.1  christos 		if(i > 0) {
    312      1.1  christos 			/* Put space into the buffer. */
    313  1.1.1.3  christos 			w += sldns_str_print(&s, &slen, " ");
    314      1.1  christos 		}
    315      1.1  christos 		/* Ignore the first two bytes, they are the rr_data len. */
    316  1.1.1.3  christos 		w_temp = sldns_wire2str_rdata_buf(rrset_data->rr_data[i] + 2,
    317      1.1  christos 			rrset_data->rr_len[i] - 2, s, slen, qstate->qinfo.qtype);
    318  1.1.1.3  christos 		if(w_temp < 0) {
    319      1.1  christos 			/* Error in printout. */
    320  1.1.1.3  christos 			log_err("ipsecmod: Error in printing IP address");
    321  1.1.1.3  christos 			return 0;
    322  1.1.1.3  christos 		} else if((size_t)w_temp >= slen) {
    323      1.1  christos 			s = NULL; /* We do not want str to point outside of buffer. */
    324      1.1  christos 			slen = 0;
    325  1.1.1.3  christos 			log_err("ipsecmod: shell command too long");
    326  1.1.1.3  christos 			return 0;
    327      1.1  christos 		} else {
    328  1.1.1.3  christos 			s += w_temp;
    329  1.1.1.3  christos 			slen -= w_temp;
    330  1.1.1.3  christos 			w += w_temp;
    331      1.1  christos 		}
    332      1.1  christos 	}
    333  1.1.1.3  christos 	w += sldns_str_print(&s, &slen, "\"");
    334      1.1  christos 	/* Put space into the buffer. */
    335  1.1.1.3  christos 	w += sldns_str_print(&s, &slen, " ");
    336      1.1  christos 	/* Copy the IPSECKEY record(s) into the buffer. Start and end this section
    337      1.1  christos 	 * with a double quote. */
    338  1.1.1.3  christos 	w += sldns_str_print(&s, &slen, "\"");
    339      1.1  christos 	rrset_data = (struct packed_rrset_data*)iq->ipseckey_rrset->entry.data;
    340      1.1  christos 	for(i=0; i<rrset_data->count; i++) {
    341      1.1  christos 		if(i > 0) {
    342      1.1  christos 			/* Put space into the buffer. */
    343  1.1.1.3  christos 			w += sldns_str_print(&s, &slen, " ");
    344      1.1  christos 		}
    345      1.1  christos 		/* Ignore the first two bytes, they are the rr_data len. */
    346      1.1  christos 		tempdata = rrset_data->rr_data[i] + 2;
    347      1.1  christos 		tempdata_len = rrset_data->rr_len[i] - 2;
    348      1.1  christos 		/* Save the buffer pointers. */
    349      1.1  christos 		tempstring = s; tempstring_len = slen;
    350  1.1.1.3  christos 		w_temp = sldns_wire2str_ipseckey_scan(&tempdata, &tempdata_len, &s,
    351  1.1.1.3  christos 			&slen, NULL, 0, NULL);
    352      1.1  christos 		/* There was an error when parsing the IPSECKEY; reset the buffer
    353      1.1  christos 		 * pointers to their previous values. */
    354  1.1.1.3  christos 		if(w_temp == -1) {
    355      1.1  christos 			s = tempstring; slen = tempstring_len;
    356  1.1.1.3  christos 		} else if(w_temp > 0) {
    357  1.1.1.3  christos 			if(!ipseckey_has_safe_characters(
    358  1.1.1.3  christos 					tempstring, tempstring_len - slen)) {
    359  1.1.1.3  christos 				log_err("ipsecmod: ipseckey has unsafe characters");
    360  1.1.1.3  christos 				return 0;
    361  1.1.1.3  christos 			}
    362  1.1.1.3  christos 			w += w_temp;
    363      1.1  christos 		}
    364      1.1  christos 	}
    365  1.1.1.3  christos 	w += sldns_str_print(&s, &slen, "\"");
    366  1.1.1.3  christos 	if(w >= (int)sizeof(str)) {
    367  1.1.1.3  christos 		log_err("ipsecmod: shell command too long");
    368  1.1.1.3  christos 		return 0;
    369  1.1.1.3  christos 	}
    370  1.1.1.3  christos 	verbose(VERB_ALGO, "ipsecmod: shell command: '%s'", str);
    371      1.1  christos 	/* ipsecmod-hook should return 0 on success. */
    372      1.1  christos 	if(system(str) != 0)
    373      1.1  christos 		return 0;
    374      1.1  christos 	return 1;
    375      1.1  christos }
    376      1.1  christos 
    377      1.1  christos /**
    378      1.1  christos  * Handle an ipsecmod module event with a query
    379      1.1  christos  * @param qstate: query state (from the mesh), passed between modules.
    380      1.1  christos  * 	contains qstate->env module environment with global caches and so on.
    381      1.1  christos  * @param iq: query state specific for this module.  per-query.
    382      1.1  christos  * @param ie: environment specific for this module.  global.
    383      1.1  christos  * @param id: module id.
    384      1.1  christos  */
    385      1.1  christos static void
    386      1.1  christos ipsecmod_handle_query(struct module_qstate* qstate,
    387      1.1  christos 	struct ipsecmod_qstate* iq, struct ipsecmod_env* ie, int id)
    388      1.1  christos {
    389      1.1  christos 	struct ub_packed_rrset_key* rrset_key;
    390      1.1  christos 	struct packed_rrset_data* rrset_data;
    391      1.1  christos 	size_t i;
    392      1.1  christos 	/* Pass to next module if we are not enabled and whitelisted. */
    393      1.1  christos 	if(!(iq->enabled && iq->is_whitelisted)) {
    394      1.1  christos 		qstate->ext_state[id] = module_wait_module;
    395      1.1  christos 		return;
    396      1.1  christos 	}
    397      1.1  christos 	/* New query, check if the query is for an A/AAAA record and disable
    398      1.1  christos 	 * caching for other modules. */
    399      1.1  christos 	if(!iq->ipseckey_done) {
    400      1.1  christos 		if(qstate->qinfo.qtype == LDNS_RR_TYPE_A ||
    401      1.1  christos 			qstate->qinfo.qtype == LDNS_RR_TYPE_AAAA) {
    402      1.1  christos 			char type[16];
    403      1.1  christos 			sldns_wire2str_type_buf(qstate->qinfo.qtype, type,
    404      1.1  christos 				sizeof(type));
    405      1.1  christos 			verbose(VERB_ALGO, "ipsecmod: query for %s; engaging",
    406      1.1  christos 				type);
    407      1.1  christos 			qstate->no_cache_store = 1;
    408      1.1  christos 		}
    409      1.1  christos 		/* Pass request to next module. */
    410      1.1  christos 		qstate->ext_state[id] = module_wait_module;
    411      1.1  christos 		return;
    412      1.1  christos 	}
    413      1.1  christos 	/* IPSECKEY subquery is finished. */
    414      1.1  christos 	/* We have an IPSECKEY answer. */
    415      1.1  christos 	if(iq->ipseckey_rrset) {
    416      1.1  christos 		rrset_data = (struct packed_rrset_data*)iq->ipseckey_rrset->entry.data;
    417      1.1  christos 		if(rrset_data) {
    418      1.1  christos 			/* If bogus return SERVFAIL. */
    419      1.1  christos 			if(!qstate->env->cfg->ipsecmod_ignore_bogus &&
    420      1.1  christos 				rrset_data->security == sec_status_bogus) {
    421      1.1  christos 				log_err("ipsecmod: bogus IPSECKEY");
    422  1.1.1.4  christos 				errinf(qstate, "ipsecmod: bogus IPSECKEY");
    423      1.1  christos 				ipsecmod_error(qstate, id);
    424      1.1  christos 				return;
    425      1.1  christos 			}
    426      1.1  christos 			/* We have a valid IPSECKEY reply, call hook. */
    427      1.1  christos 			if(!call_hook(qstate, iq, ie) &&
    428      1.1  christos 				qstate->env->cfg->ipsecmod_strict) {
    429      1.1  christos 				log_err("ipsecmod: ipsecmod-hook failed");
    430  1.1.1.4  christos 				errinf(qstate, "ipsecmod: ipsecmod-hook failed");
    431      1.1  christos 				ipsecmod_error(qstate, id);
    432      1.1  christos 				return;
    433      1.1  christos 			}
    434      1.1  christos 			/* Make sure the A/AAAA's TTL is equal/less than the
    435      1.1  christos 			 * ipsecmod_max_ttl. */
    436      1.1  christos 			rrset_key = reply_find_answer_rrset(&qstate->return_msg->qinfo,
    437      1.1  christos 				qstate->return_msg->rep);
    438      1.1  christos 			rrset_data = (struct packed_rrset_data*)rrset_key->entry.data;
    439      1.1  christos 			if(rrset_data->ttl > (time_t)qstate->env->cfg->ipsecmod_max_ttl) {
    440      1.1  christos 				/* Update TTL for rrset to fixed value. */
    441      1.1  christos 				rrset_data->ttl = qstate->env->cfg->ipsecmod_max_ttl;
    442      1.1  christos 				for(i=0; i<rrset_data->count+rrset_data->rrsig_count; i++)
    443      1.1  christos 					rrset_data->rr_ttl[i] = qstate->env->cfg->ipsecmod_max_ttl;
    444      1.1  christos 				/* Also update reply_info's TTL */
    445      1.1  christos 				if(qstate->return_msg->rep->ttl > (time_t)qstate->env->cfg->ipsecmod_max_ttl) {
    446      1.1  christos 					qstate->return_msg->rep->ttl =
    447      1.1  christos 						qstate->env->cfg->ipsecmod_max_ttl;
    448      1.1  christos 					qstate->return_msg->rep->prefetch_ttl = PREFETCH_TTL_CALC(
    449      1.1  christos 						qstate->return_msg->rep->ttl);
    450  1.1.1.2  christos 					qstate->return_msg->rep->serve_expired_ttl = qstate->return_msg->rep->ttl +
    451  1.1.1.2  christos 						qstate->env->cfg->serve_expired_ttl;
    452      1.1  christos 				}
    453      1.1  christos 			}
    454      1.1  christos 		}
    455      1.1  christos 	}
    456      1.1  christos 	/* Store A/AAAA in cache. */
    457      1.1  christos 	if(!dns_cache_store(qstate->env, &qstate->qinfo,
    458      1.1  christos 		qstate->return_msg->rep, 0, qstate->prefetch_leeway,
    459  1.1.1.5  christos 		0, qstate->region, qstate->query_flags, qstate->qstarttime,
    460  1.1.1.5  christos 		qstate->is_valrec)) {
    461      1.1  christos 		log_err("ipsecmod: out of memory caching record");
    462      1.1  christos 	}
    463      1.1  christos 	qstate->ext_state[id] = module_finished;
    464      1.1  christos }
    465      1.1  christos 
    466      1.1  christos /**
    467      1.1  christos  * Handle an ipsecmod module event with a response from the iterator.
    468      1.1  christos  * @param qstate: query state (from the mesh), passed between modules.
    469      1.1  christos  * 	contains qstate->env module environment with global caches and so on.
    470      1.1  christos  * @param iq: query state specific for this module.  per-query.
    471      1.1  christos  * @param ie: environment specific for this module.  global.
    472      1.1  christos  * @param id: module id.
    473      1.1  christos  */
    474      1.1  christos static void
    475      1.1  christos ipsecmod_handle_response(struct module_qstate* qstate,
    476      1.1  christos 	struct ipsecmod_qstate* ATTR_UNUSED(iq),
    477      1.1  christos 	struct ipsecmod_env* ATTR_UNUSED(ie), int id)
    478      1.1  christos {
    479      1.1  christos 	/* Pass to previous module if we are not enabled and whitelisted. */
    480      1.1  christos 	if(!(iq->enabled && iq->is_whitelisted)) {
    481      1.1  christos 		qstate->ext_state[id] = module_finished;
    482      1.1  christos 		return;
    483      1.1  christos 	}
    484      1.1  christos 	/* check if the response is for an A/AAAA query. */
    485      1.1  christos 	if((qstate->qinfo.qtype == LDNS_RR_TYPE_A ||
    486      1.1  christos 		qstate->qinfo.qtype == LDNS_RR_TYPE_AAAA) &&
    487      1.1  christos 		/* check that we had an answer for the A/AAAA query. */
    488      1.1  christos 		qstate->return_msg &&
    489      1.1  christos 		reply_find_answer_rrset(&qstate->return_msg->qinfo,
    490      1.1  christos 		qstate->return_msg->rep) &&
    491      1.1  christos 		/* check that another module didn't SERVFAIL. */
    492      1.1  christos 		qstate->return_rcode == LDNS_RCODE_NOERROR) {
    493      1.1  christos 		char type[16];
    494      1.1  christos 		sldns_wire2str_type_buf(qstate->qinfo.qtype, type,
    495      1.1  christos 			sizeof(type));
    496      1.1  christos 		verbose(VERB_ALGO, "ipsecmod: response for %s; generating IPSECKEY "
    497      1.1  christos 			"subquery", type);
    498      1.1  christos 		/* generate an IPSECKEY query. */
    499      1.1  christos 		if(!generate_request(qstate, id, qstate->qinfo.qname,
    500      1.1  christos 			qstate->qinfo.qname_len, LDNS_RR_TYPE_IPSECKEY,
    501      1.1  christos 			qstate->qinfo.qclass, 0)) {
    502      1.1  christos 			log_err("ipsecmod: could not generate subquery.");
    503  1.1.1.4  christos 			errinf(qstate, "ipsecmod: could not generate subquery.");
    504      1.1  christos 			ipsecmod_error(qstate, id);
    505      1.1  christos 		}
    506      1.1  christos 		return;
    507      1.1  christos 	}
    508      1.1  christos 	/* we are done with the query. */
    509      1.1  christos 	qstate->ext_state[id] = module_finished;
    510      1.1  christos }
    511      1.1  christos 
    512      1.1  christos void
    513      1.1  christos ipsecmod_operate(struct module_qstate* qstate, enum module_ev event, int id,
    514      1.1  christos 	struct outbound_entry* outbound)
    515      1.1  christos {
    516      1.1  christos 	struct ipsecmod_env* ie = (struct ipsecmod_env*)qstate->env->modinfo[id];
    517      1.1  christos 	struct ipsecmod_qstate* iq = (struct ipsecmod_qstate*)qstate->minfo[id];
    518      1.1  christos 	verbose(VERB_QUERY, "ipsecmod[module %d] operate: extstate:%s event:%s",
    519      1.1  christos 		id, strextstate(qstate->ext_state[id]), strmodulevent(event));
    520      1.1  christos 	if(iq) log_query_info(VERB_QUERY, "ipsecmod operate: query",
    521      1.1  christos 		&qstate->qinfo);
    522      1.1  christos 
    523      1.1  christos 	/* create ipsecmod_qstate. */
    524      1.1  christos 	if((event == module_event_new || event == module_event_pass) &&
    525      1.1  christos 		iq == NULL) {
    526      1.1  christos 		if(!ipsecmod_new(qstate, id)) {
    527  1.1.1.4  christos 			errinf(qstate, "ipsecmod: could not ipsecmod_new");
    528      1.1  christos 			ipsecmod_error(qstate, id);
    529      1.1  christos 			return;
    530      1.1  christos 		}
    531      1.1  christos 		iq = (struct ipsecmod_qstate*)qstate->minfo[id];
    532      1.1  christos 	}
    533      1.1  christos 	if(iq && (event == module_event_pass || event == module_event_new)) {
    534      1.1  christos 		ipsecmod_handle_query(qstate, iq, ie, id);
    535      1.1  christos 		return;
    536      1.1  christos 	}
    537      1.1  christos 	if(iq && (event == module_event_moddone)) {
    538      1.1  christos 		ipsecmod_handle_response(qstate, iq, ie, id);
    539      1.1  christos 		return;
    540      1.1  christos 	}
    541      1.1  christos 	if(iq && outbound) {
    542      1.1  christos 		/* cachedb does not need to process responses at this time
    543      1.1  christos 		 * ignore it.
    544      1.1  christos 		cachedb_process_response(qstate, iq, ie, id, outbound, event);
    545      1.1  christos 		*/
    546      1.1  christos 		return;
    547      1.1  christos 	}
    548      1.1  christos 	if(event == module_event_error) {
    549      1.1  christos 		verbose(VERB_ALGO, "got called with event error, giving up");
    550  1.1.1.4  christos 		errinf(qstate, "ipsecmod: got called with event error");
    551      1.1  christos 		ipsecmod_error(qstate, id);
    552      1.1  christos 		return;
    553      1.1  christos 	}
    554      1.1  christos 	if(!iq && (event == module_event_moddone)) {
    555      1.1  christos 		/* during priming, module done but we never started. */
    556      1.1  christos 		qstate->ext_state[id] = module_finished;
    557      1.1  christos 		return;
    558      1.1  christos 	}
    559      1.1  christos 
    560      1.1  christos 	log_err("ipsecmod: bad event %s", strmodulevent(event));
    561  1.1.1.4  christos 	errinf(qstate, "ipsecmod: operate got bad event");
    562      1.1  christos 	ipsecmod_error(qstate, id);
    563      1.1  christos 	return;
    564      1.1  christos }
    565      1.1  christos 
    566      1.1  christos void
    567      1.1  christos ipsecmod_inform_super(struct module_qstate* qstate, int id,
    568      1.1  christos 	struct module_qstate* super)
    569      1.1  christos {
    570      1.1  christos 	struct ipsecmod_qstate* siq;
    571      1.1  christos 	log_query_info(VERB_ALGO, "ipsecmod: inform_super, sub is",
    572      1.1  christos 		&qstate->qinfo);
    573      1.1  christos 	log_query_info(VERB_ALGO, "super is", &super->qinfo);
    574      1.1  christos 	siq = (struct ipsecmod_qstate*)super->minfo[id];
    575      1.1  christos 	if(!siq) {
    576      1.1  christos 		verbose(VERB_ALGO, "super has no ipsecmod state");
    577      1.1  christos 		return;
    578      1.1  christos 	}
    579      1.1  christos 
    580      1.1  christos 	if(qstate->return_msg) {
    581      1.1  christos 		struct ub_packed_rrset_key* rrset_key = reply_find_answer_rrset(
    582      1.1  christos 			&qstate->return_msg->qinfo, qstate->return_msg->rep);
    583      1.1  christos 		if(rrset_key) {
    584      1.1  christos 			/* We have an answer. */
    585      1.1  christos 			/* Copy to super's region. */
    586      1.1  christos 			rrset_key = packed_rrset_copy_region(rrset_key, super->region, 0);
    587      1.1  christos 			siq->ipseckey_rrset = rrset_key;
    588      1.1  christos 			if(!rrset_key) {
    589      1.1  christos 				log_err("ipsecmod: out of memory.");
    590      1.1  christos 			}
    591      1.1  christos 		}
    592      1.1  christos 	}
    593      1.1  christos 	/* Notify super to proceed. */
    594      1.1  christos 	siq->ipseckey_done = 1;
    595      1.1  christos }
    596      1.1  christos 
    597      1.1  christos void
    598      1.1  christos ipsecmod_clear(struct module_qstate* qstate, int id)
    599      1.1  christos {
    600      1.1  christos 	if(!qstate)
    601      1.1  christos 		return;
    602      1.1  christos 	qstate->minfo[id] = NULL;
    603      1.1  christos }
    604      1.1  christos 
    605      1.1  christos size_t
    606      1.1  christos ipsecmod_get_mem(struct module_env* env, int id)
    607      1.1  christos {
    608      1.1  christos 	struct ipsecmod_env* ie = (struct ipsecmod_env*)env->modinfo[id];
    609      1.1  christos 	if(!ie)
    610      1.1  christos 		return 0;
    611      1.1  christos 	return sizeof(*ie) + ipsecmod_whitelist_get_mem(ie->whitelist);
    612      1.1  christos }
    613      1.1  christos 
    614      1.1  christos /**
    615      1.1  christos  * The ipsecmod function block
    616      1.1  christos  */
    617      1.1  christos static struct module_func_block ipsecmod_block = {
    618      1.1  christos 	"ipsecmod",
    619  1.1.1.5  christos 	NULL, NULL, &ipsecmod_init, &ipsecmod_deinit, &ipsecmod_operate,
    620      1.1  christos 	&ipsecmod_inform_super, &ipsecmod_clear, &ipsecmod_get_mem
    621      1.1  christos };
    622      1.1  christos 
    623      1.1  christos struct module_func_block*
    624      1.1  christos ipsecmod_get_funcblock(void)
    625      1.1  christos {
    626      1.1  christos 	return &ipsecmod_block;
    627      1.1  christos }
    628      1.1  christos #endif /* USE_IPSECMOD */
    629