/* * * dumpcache-expire v0.2 * * quick and dirty hacked tool to expire an entry in djbdns dnscache's cache * by setting expiration time to 0. * needs the dumpcache-patch from http://www.efge.org/djbdns/ * * http://plonk.de/sw/djb/dumpcache-expire.c * (C) 2003 Jakob Hirsch (dumpcache@plonk.de) * * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * [http://www.fsf.org/licenses/gpl.txt] * */ #include #include #include #include #include #include #include #include #include void name(char * p, char * hostname); int main(int argc, char *argv[]) { /* the path to your dnscache dumpfile */ const char dumpcache[] = "/service/dnscache/root/dump/dumpcache"; /* expiration time (seconds since 1970-1-1) */ const char expiration[8] = "\000\000\000\000\000\000\000\000"; int fd; char *start, *end, *p; int *key_len, *data_len; char hostname[255]; /* hostnames should not be longer than that... */ struct stat st; time_t mtime; int i, count; if (argc<2) { printf("Usage: dumpcache-expire [records...]\n"); exit(-1); } /* wait for dnscache to dump */ stat(dumpcache, &st); mtime = st.st_mtime; system("svc -a /service/dnscache"); count = 0; while (mtime == st.st_mtime) { if (count++ > 10) { printf("timeout waiting for dnscache dump\n"); exit(-1); } usleep(100000); stat(dumpcache, &st); } fd = open(dumpcache, O_RDWR); if (fd == -1) { printf("open %s: %s\n", dumpcache, strerror(errno)); exit(-1); } fstat(fd, &st); start = p = mmap(0, st.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); if (p == MAP_FAILED) { printf("mmap: %s\n", strerror(errno)); exit(-1); } end = p + st.st_size; count = 0; while (p < end) { key_len = (int*) p; data_len = (int*) p+1; name(p+18, hostname); for (i=1; i < argc; i++) if (strcasecmp(hostname, argv[i]) == 0) { memcpy(p+8, expiration, 8); count++; } p += *data_len + *key_len + 16; } munmap(start, st.st_size); close(fd); if (count) system("svc -h /service/dnscache"); printf("%i records expired\n", count); exit(count); } /* build the hostname (no bounds checking) */ void name(char *p, char *hostname) { while (*p != 0) { memcpy(hostname, p+1, *p); hostname[*p] = '.'; hostname += (*p) + 1; p += (*p) + 1; } *(hostname-1) = 0; }