Merge from vendor branch LESS:
[dragonfly.git] / contrib / bind-9.3 / bin / named / builtin.c
1 /*
2  * Copyright (C) 2004  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 2001-2003  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and 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: builtin.c,v 1.4.106.4 2004/03/08 04:04:18 marka Exp $ */
19
20 /*
21  * The built-in "version", "hostname", "id" and "authors" databases.
22  */
23
24 #include <config.h>
25
26 #include <string.h>
27 #include <stdio.h>
28
29 #include <isc/print.h>
30 #include <isc/result.h>
31 #include <isc/util.h>
32
33 #include <dns/sdb.h>
34 #include <dns/result.h>
35
36 #include <named/builtin.h>
37 #include <named/globals.h>
38 #include <named/server.h>
39 #include <named/os.h>
40
41 typedef struct builtin builtin_t;
42
43 static isc_result_t do_version_lookup(dns_sdblookup_t *lookup);
44 static isc_result_t do_hostname_lookup(dns_sdblookup_t *lookup);
45 static isc_result_t do_authors_lookup(dns_sdblookup_t *lookup);
46 static isc_result_t do_id_lookup(dns_sdblookup_t *lookup);
47
48 /*
49  * We can't use function pointers as the db_data directly
50  * because ANSI C does not guarantee that function pointers
51  * can safely be cast to void pointers and back.
52  */
53
54 struct builtin {
55         isc_result_t (*do_lookup)(dns_sdblookup_t *lookup);
56 };
57
58 static builtin_t version_builtin = { do_version_lookup };
59 static builtin_t hostname_builtin = { do_hostname_lookup };
60 static builtin_t authors_builtin = { do_authors_lookup };
61 static builtin_t id_builtin = { do_id_lookup };
62
63 static dns_sdbimplementation_t *builtin_impl;
64
65 static isc_result_t
66 builtin_lookup(const char *zone, const char *name, void *dbdata,
67                dns_sdblookup_t *lookup)
68 {
69         builtin_t *b = (builtin_t *) dbdata;
70
71         UNUSED(zone);
72
73         if (strcmp(name, "@") == 0)
74                 return (b->do_lookup(lookup));
75         else
76                 return (ISC_R_NOTFOUND);
77 }
78
79 static isc_result_t
80 put_txt(dns_sdblookup_t *lookup, const char *text) {
81         unsigned char buf[256];
82         unsigned int len = strlen(text);
83         if (len > 255)
84                 len = 255; /* Silently truncate */
85         buf[0] = len;
86         memcpy(&buf[1], text, len);
87         return (dns_sdb_putrdata(lookup, dns_rdatatype_txt, 0, buf, len + 1));
88 }
89
90 static isc_result_t
91 do_version_lookup(dns_sdblookup_t *lookup) {
92         if (ns_g_server->version_set) { 
93                 if (ns_g_server->version == NULL)
94                         return (ISC_R_SUCCESS);
95                 else
96                         return (put_txt(lookup, ns_g_server->version));
97         } else {
98                 return (put_txt(lookup, ns_g_version));
99         }
100 }
101
102 static isc_result_t
103 do_hostname_lookup(dns_sdblookup_t *lookup) {
104         if (ns_g_server->hostname_set) {
105                 if (ns_g_server->hostname == NULL)
106                         return (ISC_R_SUCCESS);
107                 else
108                         return (put_txt(lookup, ns_g_server->hostname));
109         } else {
110                 char buf[256];
111                 isc_result_t result = ns_os_gethostname(buf, sizeof(buf));
112                 if (result != ISC_R_SUCCESS)
113                         return (result);
114                 return (put_txt(lookup, buf));
115         }
116 }
117
118 static isc_result_t
119 do_authors_lookup(dns_sdblookup_t *lookup) {
120         isc_result_t result;
121         const char **p;
122         static const char *authors[] = {
123                 "Mark Andrews",
124                 "James Brister",
125                 "Ben Cottrell",
126                 "Michael Graff",
127                 "Andreas Gustafsson",
128                 "Bob Halley",
129                 "David Lawrence",
130                 "Danny Mayer",
131                 "Damien Neil",
132                 "Matt Nelson",
133                 "Michael Sawyer",
134                 "Brian Wellington",
135                 NULL
136         };
137
138         /*
139          * If a version string is specified, disable the authors.bind zone.
140          */
141         if (ns_g_server->version_set)
142                 return (ISC_R_SUCCESS);
143
144         for (p = authors; *p != NULL; p++) {
145                 result = put_txt(lookup, *p);
146                 if (result != ISC_R_SUCCESS)
147                         return (result);
148         }
149         return (ISC_R_SUCCESS);
150 }
151
152 static isc_result_t
153 do_id_lookup(dns_sdblookup_t *lookup) {
154
155         if (ns_g_server->server_usehostname) {
156                 char buf[256];
157                 isc_result_t result = ns_os_gethostname(buf, sizeof(buf));
158                 if (result != ISC_R_SUCCESS)
159                         return (result);
160                 return (put_txt(lookup, buf));
161         }
162
163         if (ns_g_server->server_id == NULL)
164                 return (ISC_R_SUCCESS);
165         else
166                 return (put_txt(lookup, ns_g_server->server_id));
167 }
168
169 static isc_result_t
170 builtin_authority(const char *zone, void *dbdata, dns_sdblookup_t *lookup) {
171         isc_result_t result;
172
173         UNUSED(zone);
174         UNUSED(dbdata);
175
176         result = dns_sdb_putsoa(lookup, "@", "hostmaster", 0);
177         if (result != ISC_R_SUCCESS)
178                 return (ISC_R_FAILURE);
179         result = dns_sdb_putrr(lookup, "ns", 0, "@");
180         if (result != ISC_R_SUCCESS)
181                 return (ISC_R_FAILURE);
182
183         return (ISC_R_SUCCESS);
184 }
185
186 static isc_result_t
187 builtin_create(const char *zone, int argc, char **argv,
188                void *driverdata, void **dbdata)
189 {
190         UNUSED(zone);
191         UNUSED(driverdata);
192         if (argc != 1)
193                 return (DNS_R_SYNTAX);
194         if (strcmp(argv[0], "version") == 0)
195                 *dbdata = &version_builtin;
196         else if (strcmp(argv[0], "hostname") == 0)
197                 *dbdata = &hostname_builtin;
198         else if (strcmp(argv[0], "authors") == 0)
199                 *dbdata = &authors_builtin;
200         else if (strcmp(argv[0], "id") == 0)
201                 *dbdata = &id_builtin;
202         else
203                 return (ISC_R_NOTIMPLEMENTED);
204         return (ISC_R_SUCCESS);
205 }
206
207 static dns_sdbmethods_t builtin_methods = {
208         builtin_lookup,
209         builtin_authority,
210         NULL,           /* allnodes */
211         builtin_create,
212         NULL            /* destroy */
213 };
214
215 isc_result_t
216 ns_builtin_init(void) {
217         RUNTIME_CHECK(dns_sdb_register("_builtin", &builtin_methods, NULL,
218                                        DNS_SDBFLAG_RELATIVEOWNER |
219                                        DNS_SDBFLAG_RELATIVERDATA,
220                                        ns_g_mctx, &builtin_impl)
221                       == ISC_R_SUCCESS);
222         return (ISC_R_SUCCESS);
223 }
224
225 void
226 ns_builtin_deinit(void) {
227         dns_sdb_unregister(&builtin_impl);
228 }