Sync usage with man page.
[netbsd-mini2440.git] / external / bsd / bind / dist / contrib / sdb / dir / dirdb.c
blob7cb13e21abe181af71628ca83c2d932f6f792045
1 /* $NetBSD$ */
3 /*
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.
27 #include <config.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <sys/stat.h>
33 #include <sys/sysmacros.h>
35 #include <isc/mem.h>
36 #include <isc/print.h>
37 #include <isc/result.h>
38 #include <isc/util.h>
40 #include <dns/sdb.h>
42 #include <named/globals.h>
44 #include "dirdb.h"
46 static dns_sdbimplementation_t *dirdb = NULL;
48 #define CHECK(op) \
49 do { result = (op); \
50 if (result != ISC_R_SUCCESS) return (result); \
51 } while (0)
53 #define CHECKN(op) \
54 do { n = (op); \
55 if (n < 0) return (ISC_R_FAILURE); \
56 } while (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 static isc_result_t
66 dirdb_lookup(const char *zone, const char *name, void *dbdata,
67 dns_sdblookup_t *lookup)
69 char filename[255];
70 char filename2[255];
71 char buf[1024];
72 struct stat statbuf;
73 isc_result_t result;
74 int n;
76 UNUSED(zone);
77 UNUSED(dbdata);
79 if (strcmp(name, "@") == 0)
80 snprintf(filename, sizeof(filename), "%s", (char *)dbdata);
81 else
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));
101 buf[n] = 0;
102 CHECKN(snprintf(buf, sizeof(buf), "\"symlink\" \"%s\"",
103 filename2));
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"));
107 else {
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.
119 static isc_result_t
120 dirdb_authority(const char *zone, void *dbdata, dns_sdblookup_t *lookup) {
121 isc_result_t result;
123 UNUSED(zone);
124 UNUSED(dbdata);
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
138 * level directory.
140 static isc_result_t
141 dirdb_create(const char *zone, int argc, char **argv,
142 void *driverdata, void **dbdata)
144 UNUSED(zone);
145 UNUSED(driverdata);
147 if (argc < 1)
148 return (ISC_R_FAILURE);
149 *dbdata = isc_mem_strdup((isc_mem_t *)driverdata, argv[0]);
150 if (*dbdata == NULL)
151 return (ISC_R_NOMEMORY);
152 return (ISC_R_SUCCESS);
156 * The destroy() function frees the memory allocated by create().
158 static void
159 dirdb_destroy(const char *zone, void *driverdata, void **dbdata) {
160 UNUSED(zone);
161 UNUSED(driverdata);
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 = {
169 dirdb_lookup,
170 dirdb_authority,
171 NULL, /* allnodes */
172 dirdb_create,
173 dirdb_destroy
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().
181 isc_result_t
182 dirdb_init(void) {
183 unsigned int flags;
184 flags = DNS_SDBFLAG_RELATIVEOWNER | DNS_SDBFLAG_RELATIVERDATA |
185 DNS_SDBFLAG_THREADSAFE;
186 return (dns_sdb_register("dir", &dirdb_methods, ns_g_mctx, flags,
187 ns_g_mctx, &dirdb));
191 * Wrapper around dns_sdb_unregister().
193 void
194 dirdb_clear(void) {
195 if (dirdb != NULL)
196 dns_sdb_unregister(&dirdb);