Exemplo n.º 1
0
bdb_lookup(const char *zone, const char *name, void *dbdata,
	   dns_sdblookup_t *l)
#endif /* DNS_CLIENTINFO_VERSION */
{
	int ret;
	char *type, *rdata;
	dns_ttl_t ttl;
	isc_consttextregion_t ttltext;
	DBC *c;
	DBT key, data;

	UNUSED(zone);
#ifdef DNS_CLIENTINFO_VERSION
	UNUSED(methods);
	UNUSED(clientinfo);
#endif /* DNS_CLIENTINFO_VERSION */

	if ((ret = ((DB *)dbdata)->cursor((DB *)dbdata, NULL, &c, 0)) != 0) {
		isc_log_iwrite(dns_lctx, DNS_LOGCATEGORY_DATABASE,
			       DNS_LOGMODULE_SDB, ISC_LOG_ERROR,
			       isc_msgcat, ISC_MSGSET_GENERAL,
			       ISC_MSG_FAILED, "DB->cursor: %s",
			       db_strerror(ret));
		return ISC_R_FAILURE;
	}

	memset(&key, 0, sizeof(DBT));
	memset(&data, 0, sizeof(DBT));

	(const char *)key.data = name;
	key.size = strlen(name);

	ret = c->c_get(c, &key, &data, DB_SET);
	while (ret == 0) {
		((char *)key.data)[key.size] = 0;
		((char *)data.data)[data.size] = 0;
		ttltext.base = strtok((char *)data.data, " ");
		ttltext.length = strlen(ttltext.base);
		dns_ttl_fromtext((isc_textregion_t *)&ttltext, &ttl);
		type = strtok(NULL, " ");
		rdata = type + strlen(type) + 1;

		if (dns_sdb_putrr(l, type, ttl, rdata) != ISC_R_SUCCESS) {
			isc_log_iwrite(dns_lctx,
				       DNS_LOGCATEGORY_DATABASE,
				       DNS_LOGMODULE_SDB, ISC_LOG_ERROR,
				       isc_msgcat, ISC_MSGSET_GENERAL,
				       ISC_MSG_FAILED, "dns_sdb_putrr");
			return ISC_R_FAILURE;
		}
		ret = c->c_get(c, &key, &data, DB_NEXT_DUP);
	}

	c->c_close(c);
	return ISC_R_SUCCESS;
}
Exemplo n.º 2
0
static isc_result_t
bdb_allnodes(const char *zone, void *dbdata, dns_sdballnodes_t *n)
{
	int ret;
	char *type, *rdata;
	dns_ttl_t ttl;
	isc_consttextregion_t ttltext;
	DBC *c;
	DBT key, data;

	UNUSED(zone);

	if ((ret = ((DB *)dbdata)->cursor((DB *)dbdata, NULL, &c, 0)) != 0) {
		isc_log_iwrite(dns_lctx, DNS_LOGCATEGORY_DATABASE,
			       DNS_LOGMODULE_SDB, ISC_LOG_ERROR,
			       isc_msgcat, ISC_MSGSET_GENERAL,
			       ISC_MSG_FAILED, "DB->cursor: %s",
			       db_strerror(ret));
		return ISC_R_FAILURE;
	}

	memset(&key, 0, sizeof(DBT));
	memset(&data, 0, sizeof(DBT));

	while (c->c_get(c, &key, &data, DB_NEXT) == 0) {
		((char *)key.data)[key.size] = 0;
		((char *)data.data)[data.size] = 0;
		ttltext.base = strtok((char *)data.data, " ");
		ttltext.length = strlen(ttltext.base);
		dns_ttl_fromtext((isc_textregion_t *)&ttltext, &ttl);
		type = strtok(NULL, " ");
		rdata = type + strlen(type) + 1;

		if (dns_sdb_putnamedrr(n, key.data, type, ttl, rdata) !=
		    ISC_R_SUCCESS) {
			isc_log_iwrite(dns_lctx,
				       DNS_LOGCATEGORY_DATABASE,
				       DNS_LOGMODULE_SDB, ISC_LOG_ERROR,
				       isc_msgcat, ISC_MSGSET_GENERAL,
				       ISC_MSG_FAILED, "dns_sdb_putnamedrr");
			return ISC_R_FAILURE;
		}

	}

	c->c_close(c);
	return ISC_R_SUCCESS;
}
Exemplo n.º 3
0
/*%
 * Reads a public key from disk
 */
isc_result_t
dst_key_read_public(const char *filename, int type,
		    isc_mem_t *mctx, dst_key_t **keyp)
{
	u_char rdatabuf[DST_KEY_MAXSIZE];
	isc_buffer_t b;
	dns_fixedname_t name;
	isc_lex_t *lex = NULL;
	isc_token_t token;
	isc_result_t ret;
	dns_rdata_t rdata = DNS_RDATA_INIT;
	unsigned int opt = ISC_LEXOPT_DNSMULTILINE;
	dns_rdataclass_t rdclass = dns_rdataclass_in;
	isc_lexspecials_t specials;
	isc_uint32_t ttl = 0;
	isc_result_t result;
	dns_rdatatype_t keytype;

	/*
	 * Open the file and read its formatted contents
	 * File format:
	 *    domain.name [ttl] [class] [KEY|DNSKEY] <flags> <protocol> <algorithm> <key>
	 */

	/* 1500 should be large enough for any key */
	ret = isc_lex_create(mctx, 1500, &lex);
	if (ret != ISC_R_SUCCESS)
		goto cleanup;

	memset(specials, 0, sizeof(specials));
	specials['('] = 1;
	specials[')'] = 1;
	specials['"'] = 1;
	isc_lex_setspecials(lex, specials);
	isc_lex_setcomments(lex, ISC_LEXCOMMENT_DNSMASTERFILE);

	ret = isc_lex_openfile(lex, filename);
	if (ret != ISC_R_SUCCESS)
		goto cleanup;

#define NEXTTOKEN(lex, opt, token) { \
	ret = isc_lex_gettoken(lex, opt, token); \
	if (ret != ISC_R_SUCCESS) \
		goto cleanup; \
	}

#define BADTOKEN() { \
	ret = ISC_R_UNEXPECTEDTOKEN; \
	goto cleanup; \
	}

	/* Read the domain name */
	NEXTTOKEN(lex, opt, &token);
	if (token.type != isc_tokentype_string)
		BADTOKEN();

	/*
	 * We don't support "@" in .key files.
	 */
	if (!strcmp(DST_AS_STR(token), "@"))
		BADTOKEN();

	dns_fixedname_init(&name);
	isc_buffer_init(&b, DST_AS_STR(token), strlen(DST_AS_STR(token)));
	isc_buffer_add(&b, strlen(DST_AS_STR(token)));
	ret = dns_name_fromtext(dns_fixedname_name(&name), &b, dns_rootname,
				0, NULL);
	if (ret != ISC_R_SUCCESS)
		goto cleanup;

	/* Read the next word: either TTL, class, or 'KEY' */
	NEXTTOKEN(lex, opt, &token);

	if (token.type != isc_tokentype_string)
		BADTOKEN();

	/* If it's a TTL, read the next one */
	result = dns_ttl_fromtext(&token.value.as_textregion, &ttl);
	if (result == ISC_R_SUCCESS)
		NEXTTOKEN(lex, opt, &token);

	if (token.type != isc_tokentype_string)
		BADTOKEN();

	ret = dns_rdataclass_fromtext(&rdclass, &token.value.as_textregion);
	if (ret == ISC_R_SUCCESS)
		NEXTTOKEN(lex, opt, &token);

	if (token.type != isc_tokentype_string)
		BADTOKEN();

	if (strcasecmp(DST_AS_STR(token), "DNSKEY") == 0)
		keytype = dns_rdatatype_dnskey;
	else if (strcasecmp(DST_AS_STR(token), "KEY") == 0)
		keytype = dns_rdatatype_key; /*%< SIG(0), TKEY */
	else
		BADTOKEN();

	if (((type & DST_TYPE_KEY) != 0 && keytype != dns_rdatatype_key) ||
	    ((type & DST_TYPE_KEY) == 0 && keytype != dns_rdatatype_dnskey)) {
		ret = DST_R_BADKEYTYPE;
		goto cleanup;
	}

	isc_buffer_init(&b, rdatabuf, sizeof(rdatabuf));
	ret = dns_rdata_fromtext(&rdata, rdclass, keytype, lex, NULL,
				 ISC_FALSE, mctx, &b, NULL);
	if (ret != ISC_R_SUCCESS)
		goto cleanup;

	ret = dst_key_fromdns(dns_fixedname_name(&name), rdclass, &b, mctx,
			      keyp);
	if (ret != ISC_R_SUCCESS)
		goto cleanup;

	dst_key_setttl(*keyp, ttl);

 cleanup:
	if (lex != NULL)
		isc_lex_destroy(&lex);
	return (ret);
}