2 * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2001-2003 Internet Software Consortium.
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.
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.
18 /* $Id: builtin.c,v 1.4.106.4 2004/03/08 04:04:18 marka Exp $ */
21 * The built-in "version", "hostname", "id" and "authors" databases.
29 #include <isc/print.h>
30 #include <isc/result.h>
34 #include <dns/result.h>
36 #include <named/builtin.h>
37 #include <named/globals.h>
38 #include <named/server.h>
41 typedef struct builtin builtin_t
;
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
);
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.
55 isc_result_t (*do_lookup
)(dns_sdblookup_t
*lookup
);
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
};
63 static dns_sdbimplementation_t
*builtin_impl
;
66 builtin_lookup(const char *zone
, const char *name
, void *dbdata
,
67 dns_sdblookup_t
*lookup
)
69 builtin_t
*b
= (builtin_t
*) dbdata
;
73 if (strcmp(name
, "@") == 0)
74 return (b
->do_lookup(lookup
));
76 return (ISC_R_NOTFOUND
);
80 put_txt(dns_sdblookup_t
*lookup
, const char *text
) {
81 unsigned char buf
[256];
82 unsigned int len
= strlen(text
);
84 len
= 255; /* Silently truncate */
86 memcpy(&buf
[1], text
, len
);
87 return (dns_sdb_putrdata(lookup
, dns_rdatatype_txt
, 0, buf
, len
+ 1));
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
);
96 return (put_txt(lookup
, ns_g_server
->version
));
98 return (put_txt(lookup
, ns_g_version
));
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
);
108 return (put_txt(lookup
, ns_g_server
->hostname
));
111 isc_result_t result
= ns_os_gethostname(buf
, sizeof(buf
));
112 if (result
!= ISC_R_SUCCESS
)
114 return (put_txt(lookup
, buf
));
119 do_authors_lookup(dns_sdblookup_t
*lookup
) {
122 static const char *authors
[] = {
127 "Andreas Gustafsson",
139 * If a version string is specified, disable the authors.bind zone.
141 if (ns_g_server
->version_set
)
142 return (ISC_R_SUCCESS
);
144 for (p
= authors
; *p
!= NULL
; p
++) {
145 result
= put_txt(lookup
, *p
);
146 if (result
!= ISC_R_SUCCESS
)
149 return (ISC_R_SUCCESS
);
153 do_id_lookup(dns_sdblookup_t
*lookup
) {
155 if (ns_g_server
->server_usehostname
) {
157 isc_result_t result
= ns_os_gethostname(buf
, sizeof(buf
));
158 if (result
!= ISC_R_SUCCESS
)
160 return (put_txt(lookup
, buf
));
163 if (ns_g_server
->server_id
== NULL
)
164 return (ISC_R_SUCCESS
);
166 return (put_txt(lookup
, ns_g_server
->server_id
));
170 builtin_authority(const char *zone
, void *dbdata
, dns_sdblookup_t
*lookup
) {
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
);
183 return (ISC_R_SUCCESS
);
187 builtin_create(const char *zone
, int argc
, char **argv
,
188 void *driverdata
, void **dbdata
)
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
;
203 return (ISC_R_NOTIMPLEMENTED
);
204 return (ISC_R_SUCCESS
);
207 static dns_sdbmethods_t builtin_methods
= {
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
)
222 return (ISC_R_SUCCESS
);
226 ns_builtin_deinit(void) {
227 dns_sdb_unregister(&builtin_impl
);