4 * Copyright (C) 2004, 2007 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.12 2007/06/19 23:47:07 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); \
55 if (n < 0) return (ISC_R_FAILURE); \
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.
66 dirdb_lookup(const char *zone
, const char *name
, void *dbdata
,
67 dns_sdblookup_t
*lookup
)
79 if (strcmp(name
, "@") == 0)
80 snprintf(filename
, sizeof(filename
), "%s", (char *)dbdata
);
82 snprintf(filename
, sizeof(filename
), "%s/%s",
83 (char *)dbdata
, name
);
84 CHECKN(lstat(filename
, &statbuf
));
86 if (S_ISDIR(statbuf
.st_mode
))
87 CHECK(dns_sdb_putrr(lookup
, "txt", 3600, "dir"));
88 else if (S_ISCHR(statbuf
.st_mode
) || S_ISBLK(statbuf
.st_mode
)) {
89 CHECKN(snprintf(buf
, sizeof(buf
),
90 "\"%sdev\" \"major %d\" \"minor %d\"",
91 S_ISCHR(statbuf
.st_mode
) ? "chr" : "blk",
92 major(statbuf
.st_rdev
),
93 minor(statbuf
.st_rdev
)));
94 CHECK(dns_sdb_putrr(lookup
, "txt", 3600, buf
));
95 } else if (S_ISFIFO(statbuf
.st_mode
))
96 CHECK(dns_sdb_putrr(lookup
, "txt", 3600, "pipe"));
97 else if (S_ISSOCK(statbuf
.st_mode
))
98 CHECK(dns_sdb_putrr(lookup
, "txt", 3600, "socket"));
99 else if (S_ISLNK(statbuf
.st_mode
)) {
100 CHECKN(readlink(filename
, filename2
, sizeof(filename2
) - 1));
102 CHECKN(snprintf(buf
, sizeof(buf
), "\"symlink\" \"%s\"",
104 CHECK(dns_sdb_putrr(lookup
, "txt", 3600, buf
));
105 } else if (!S_ISREG(statbuf
.st_mode
))
106 CHECK(dns_sdb_putrr(lookup
, "txt", 3600, "unknown"));
108 CHECKN(snprintf(buf
, sizeof(buf
), "\"file\" \"size = %u\"",
109 (unsigned int)statbuf
.st_size
));
110 CHECK(dns_sdb_putrr(lookup
, "txt", 3600, buf
));
113 return (ISC_R_SUCCESS
);
117 * lookup () does not return SOA or NS records, so authority() must be defined.
120 dirdb_authority(const char *zone
, void *dbdata
, dns_sdblookup_t
*lookup
) {
126 result
= dns_sdb_putsoa(lookup
, "ns", "hostmaster", 0);
127 INSIST(result
== ISC_R_SUCCESS
);
128 result
= dns_sdb_putrr(lookup
, "ns", 86400, "ns1");
129 INSIST(result
== ISC_R_SUCCESS
);
130 result
= dns_sdb_putrr(lookup
, "ns", 86400, "ns2");
131 INSIST(result
== ISC_R_SUCCESS
);
132 return (ISC_R_SUCCESS
);
136 * Each database stores the top-level directory as the dbdata opaque
137 * object. The create() function allocates it. argv[0] holds the top
141 dirdb_create(const char *zone
, int argc
, char **argv
,
142 void *driverdata
, void **dbdata
)
148 return (ISC_R_FAILURE
);
149 *dbdata
= isc_mem_strdup((isc_mem_t
*)driverdata
, argv
[0]);
151 return (ISC_R_NOMEMORY
);
152 return (ISC_R_SUCCESS
);
156 * The destroy() function frees the memory allocated by create().
159 dirdb_destroy(const char *zone
, void *driverdata
, void **dbdata
) {
162 isc_mem_free((isc_mem_t
*)driverdata
, *dbdata
);
166 * This zone does not support zone transfer, so allnodes() is NULL.
168 static dns_sdbmethods_t dirdb_methods
= {
177 * Wrapper around dns_sdb_register(). Note that the first ns_g_mctx is
178 * being passed as the "driverdata" parameter, so that will it will be
179 * passed to create() and destroy().
184 flags
= DNS_SDBFLAG_RELATIVEOWNER
| DNS_SDBFLAG_RELATIVERDATA
|
185 DNS_SDBFLAG_THREADSAFE
;
186 return (dns_sdb_register("dir", &dirdb_methods
, ns_g_mctx
, flags
,
191 * Wrapper around dns_sdb_unregister().
196 dns_sdb_unregister(&dirdb
);