Merge branch 'vendor/FILE'
[dragonfly.git] / contrib / bind / bin / named / tkeyconf.c
1 /*
2  * Copyright (C) 2004-2007  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.29 2007/06/19 23:46:59 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
53 isc_result_t
54 ns_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,
81                                          ISC_FALSE, NULL));
82                 type = DST_TYPE_PUBLIC|DST_TYPE_PRIVATE|DST_TYPE_KEY;
83                 RETERR(dst_key_fromfile(name, (dns_keytag_t) n, DNS_KEYALG_DH,
84                                         type, NULL, mctx, &tctx->dhkey));
85         }
86
87         obj = NULL;
88         result = cfg_map_get(options, "tkey-domain", &obj);
89         if (result == ISC_R_SUCCESS) {
90                 s = cfg_obj_asstring(obj);
91                 isc_buffer_init(&b, s, strlen(s));
92                 isc_buffer_add(&b, strlen(s));
93                 dns_fixedname_init(&fname);
94                 name = dns_fixedname_name(&fname);
95                 RETERR(dns_name_fromtext(name, &b, dns_rootname, ISC_FALSE,
96                                          NULL));
97                 tctx->domain = isc_mem_get(mctx, sizeof(dns_name_t));
98                 if (tctx->domain == NULL) {
99                         result = ISC_R_NOMEMORY;
100                         goto failure;
101                 }
102                 dns_name_init(tctx->domain, NULL);
103                 RETERR(dns_name_dup(name, mctx, tctx->domain));
104         }
105
106         obj = NULL;
107         result = cfg_map_get(options, "tkey-gssapi-credential", &obj);
108         if (result == ISC_R_SUCCESS) {
109                 s = cfg_obj_asstring(obj);
110
111                 isc_buffer_init(&b, s, strlen(s));
112                 isc_buffer_add(&b, strlen(s));
113                 dns_fixedname_init(&fname);
114                 name = dns_fixedname_name(&fname);
115                 RETERR(dns_name_fromtext(name, &b, dns_rootname, ISC_FALSE,
116                                          NULL));
117                 RETERR(dst_gssapi_acquirecred(name, ISC_FALSE,
118                                               &tctx->gsscred));
119         }
120
121         *tctxp = tctx;
122         return (ISC_R_SUCCESS);
123
124  failure:
125         dns_tkeyctx_destroy(&tctx);
126         return (result);
127 }
128