1 /* $NetBSD: dirdb.c,v 1.4 2014/12/10 04:37:57 christos Exp $ */
4 * Copyright (C) 2004, 2007, 2011, 2014 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 2000, 2001 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
20 /* Id: dirdb.c,v 1.14 2011/10/11 23:46:45 tbox Exp */
23 * A simple database driver that returns basic information about
24 * files and directories in the Unix file system as DNS data.
33 #include <sys/sysmacros.h>
36 #include <isc/print.h>
37 #include <isc/result.h>
42 #include <named/globals.h>
46 static dns_sdbimplementation_t
*dirdb
= NULL
;
50 if (result != ISC_R_SUCCESS) return (result); \
51 } while (/*CONSTCOND*/0)
55 if (n < 0) return (ISC_R_FAILURE); \
56 } while (/*CONSTCOND*/0)
60 * This database operates on relative names.
62 * Any name will be interpreted as a pathname offset from the directory
63 * specified in the configuration file.
65 #ifdef DNS_CLIENTINFO_VERSION
67 dirdb_lookup(const char *zone
, const char *name
, void *dbdata
,
68 dns_sdblookup_t
*lookup
, dns_clientinfomethods_t
*methods
,
69 dns_clientinfo_t
*clientinfo
)
72 dirdb_lookup(const char *zone
, const char *name
, void *dbdata
,
73 dns_sdblookup_t
*lookup
)
74 #endif /* DNS_CLIENTINFO_VERSION */
85 #ifdef DNS_CLIENTINFO_VERSION
88 #endif /* DNS_CLIENTINFO_VERSION */
90 if (strcmp(name
, "@") == 0)
91 snprintf(filename
, sizeof(filename
), "%s", (char *)dbdata
);
93 snprintf(filename
, sizeof(filename
), "%s/%s",
94 (char *)dbdata
, name
);
95 CHECKN(lstat(filename
, &statbuf
));
97 if (S_ISDIR(statbuf
.st_mode
))
98 CHECK(dns_sdb_putrr(lookup
, "txt", 3600, "dir"));
99 else if (S_ISCHR(statbuf
.st_mode
) || S_ISBLK(statbuf
.st_mode
)) {
100 CHECKN(snprintf(buf
, sizeof(buf
),
101 "\"%sdev\" \"major %d\" \"minor %d\"",
102 S_ISCHR(statbuf
.st_mode
) ? "chr" : "blk",
103 major(statbuf
.st_rdev
),
104 minor(statbuf
.st_rdev
)));
105 CHECK(dns_sdb_putrr(lookup
, "txt", 3600, buf
));
106 } else if (S_ISFIFO(statbuf
.st_mode
))
107 CHECK(dns_sdb_putrr(lookup
, "txt", 3600, "pipe"));
108 else if (S_ISSOCK(statbuf
.st_mode
))
109 CHECK(dns_sdb_putrr(lookup
, "txt", 3600, "socket"));
110 else if (S_ISLNK(statbuf
.st_mode
)) {
111 CHECKN(readlink(filename
, filename2
, sizeof(filename2
) - 1));
113 CHECKN(snprintf(buf
, sizeof(buf
), "\"symlink\" \"%s\"",
115 CHECK(dns_sdb_putrr(lookup
, "txt", 3600, buf
));
116 } else if (!S_ISREG(statbuf
.st_mode
))
117 CHECK(dns_sdb_putrr(lookup
, "txt", 3600, "unknown"));
119 CHECKN(snprintf(buf
, sizeof(buf
), "\"file\" \"size = %u\"",
120 (unsigned int)statbuf
.st_size
));
121 CHECK(dns_sdb_putrr(lookup
, "txt", 3600, buf
));
124 return (ISC_R_SUCCESS
);
128 * lookup () does not return SOA or NS records, so authority() must be defined.
131 dirdb_authority(const char *zone
, void *dbdata
, dns_sdblookup_t
*lookup
) {
137 result
= dns_sdb_putsoa(lookup
, "ns", "hostmaster", 0);
138 INSIST(result
== ISC_R_SUCCESS
);
139 result
= dns_sdb_putrr(lookup
, "ns", 86400, "ns1");
140 INSIST(result
== ISC_R_SUCCESS
);
141 result
= dns_sdb_putrr(lookup
, "ns", 86400, "ns2");
142 INSIST(result
== ISC_R_SUCCESS
);
143 return (ISC_R_SUCCESS
);
147 * Each database stores the top-level directory as the dbdata opaque
148 * object. The create() function allocates it. argv[0] holds the top
152 dirdb_create(const char *zone
, int argc
, char **argv
,
153 void *driverdata
, void **dbdata
)
159 return (ISC_R_FAILURE
);
160 *dbdata
= isc_mem_strdup((isc_mem_t
*)driverdata
, argv
[0]);
162 return (ISC_R_NOMEMORY
);
163 return (ISC_R_SUCCESS
);
167 * The destroy() function frees the memory allocated by create().
170 dirdb_destroy(const char *zone
, void *driverdata
, void **dbdata
) {
173 isc_mem_free((isc_mem_t
*)driverdata
, *dbdata
);
177 * This zone does not support zone transfer, so allnodes() is NULL.
179 static dns_sdbmethods_t dirdb_methods
= {
189 * Wrapper around dns_sdb_register(). Note that the first ns_g_mctx is
190 * being passed as the "driverdata" parameter, so that will it will be
191 * passed to create() and destroy().
196 flags
= DNS_SDBFLAG_RELATIVEOWNER
| DNS_SDBFLAG_RELATIVERDATA
|
197 DNS_SDBFLAG_THREADSAFE
;
198 return (dns_sdb_register("dir", &dirdb_methods
, ns_g_mctx
, flags
,
203 * Wrapper around dns_sdb_unregister().
208 dns_sdb_unregister(&dirdb
);