1/*
2 * Copyright (C) 2004-2007, 2009, 2010  Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2001  Internet Software Consortium.
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
16 */
17
18/* $Id: tkeyconf.c,v 1.33 2010/12/20 23:47:20 tbox Exp $ */
19
20/*! \file */
21
22#include <config.h>
23
24#include <isc/buffer.h>
25#include <isc/string.h>		/* Required for HP/UX (and others?) */
26#include <isc/mem.h>
27
28#include <isccfg/cfg.h>
29
30#include <dns/fixedname.h>
31#include <dns/keyvalues.h>
32#include <dns/name.h>
33#include <dns/tkey.h>
34
35#include <dst/gssapi.h>
36
37#include <named/tkeyconf.h>
38
39#define RETERR(x) do { \
40	result = (x); \
41	if (result != ISC_R_SUCCESS) \
42		goto failure; \
43	} while (0)
44
45#include<named/log.h>
46#define LOG(msg) \
47	isc_log_write(ns_g_lctx, \
48	NS_LOGCATEGORY_GENERAL, \
49	NS_LOGMODULE_SERVER, \
50	ISC_LOG_ERROR, \
51	"%s", msg)
52
53isc_result_t
54ns_tkeyctx_fromconfig(const cfg_obj_t *options, isc_mem_t *mctx,
55		      isc_entropy_t *ectx, dns_tkeyctx_t **tctxp)
56{
57	isc_result_t result;
58	dns_tkeyctx_t *tctx = NULL;
59	const char *s;
60	isc_uint32_t n;
61	dns_fixedname_t fname;
62	dns_name_t *name;
63	isc_buffer_t b;
64	const cfg_obj_t *obj;
65	int type;
66
67	result = dns_tkeyctx_create(mctx, ectx, &tctx);
68	if (result != ISC_R_SUCCESS)
69		return (result);
70
71	obj = NULL;
72	result = cfg_map_get(options, "tkey-dhkey", &obj);
73	if (result == ISC_R_SUCCESS) {
74		s = cfg_obj_asstring(cfg_tuple_get(obj, "name"));
75		n = cfg_obj_asuint32(cfg_tuple_get(obj, "keyid"));
76		isc_buffer_init(&b, s, strlen(s));
77		isc_buffer_add(&b, strlen(s));
78		dns_fixedname_init(&fname);
79		name = dns_fixedname_name(&fname);
80		RETERR(dns_name_fromtext(name, &b, dns_rootname, 0, NULL));
81		type = DST_TYPE_PUBLIC|DST_TYPE_PRIVATE|DST_TYPE_KEY;
82		RETERR(dst_key_fromfile(name, (dns_keytag_t) n, DNS_KEYALG_DH,
83					type, NULL, mctx, &tctx->dhkey));
84	}
85
86	obj = NULL;
87	result = cfg_map_get(options, "tkey-domain", &obj);
88	if (result == ISC_R_SUCCESS) {
89		s = cfg_obj_asstring(obj);
90		isc_buffer_init(&b, s, strlen(s));
91		isc_buffer_add(&b, strlen(s));
92		dns_fixedname_init(&fname);
93		name = dns_fixedname_name(&fname);
94		RETERR(dns_name_fromtext(name, &b, dns_rootname, 0, NULL));
95		tctx->domain = isc_mem_get(mctx, sizeof(dns_name_t));
96		if (tctx->domain == NULL) {
97			result = ISC_R_NOMEMORY;
98			goto failure;
99		}
100		dns_name_init(tctx->domain, NULL);
101		RETERR(dns_name_dup(name, mctx, tctx->domain));
102	}
103
104	obj = NULL;
105	result = cfg_map_get(options, "tkey-gssapi-credential", &obj);
106	if (result == ISC_R_SUCCESS) {
107		s = cfg_obj_asstring(obj);
108
109		isc_buffer_init(&b, s, strlen(s));
110		isc_buffer_add(&b, strlen(s));
111		dns_fixedname_init(&fname);
112		name = dns_fixedname_name(&fname);
113		RETERR(dns_name_fromtext(name, &b, dns_rootname, 0, NULL));
114		RETERR(dst_gssapi_acquirecred(name, ISC_FALSE, &tctx->gsscred));
115	}
116
117	obj = NULL;
118	result = cfg_map_get(options, "tkey-gssapi-keytab", &obj);
119	if (result == ISC_R_SUCCESS) {
120		s = cfg_obj_asstring(obj);
121		tctx->gssapi_keytab = isc_mem_strdup(mctx, s);
122		if (tctx->gssapi_keytab == NULL) {
123			result = ISC_R_NOMEMORY;
124			goto failure;
125		}
126	}
127
128
129	*tctxp = tctx;
130	return (ISC_R_SUCCESS);
131
132 failure:
133	dns_tkeyctx_destroy(&tctx);
134	return (result);
135}
136
137