/* * logtool - a logfile parsing/monitoring/manipulation utility * * Copyright (C) Y2K (2000) A.L.Lambert * * 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, 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. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ /* Yee ole includes (I put this all in one file for my sanity) */ #include "includes.h" #include <gdbm.h> GDBM_FILE dbf; datum key, val; time_t curtime, oldtime; int lt_gdbm_query(char *ip_addr) { char line[LSIZE]; char value[LSIZE]; char tstamp[LSIZE]; /* open our dbf file (if we can */ dbf = gdbm_open(DNSDB_FILE, 0, GDBM_READER, 0666, 0); /* double check we were able to open it */ if(dbf == NULL) { /* no error reporting here - could get ugly if user is * trying to use with a corrupted database :) */ /* fprintf(stderr, "open: %s\n", gdbm_strerror(errno)); perror("open"); */ return -1; } /* set the values for key before we query */ key.dptr = ip_addr; key.dsize = strlen(key.dptr); /* do the query */ val = gdbm_fetch(dbf, key); /* we're done with our dbf - close it now */ gdbm_close(dbf); /* if we got somethign back from the query */ if(val.dsize != 0 && val.dptr != NULL) { strcpy(line, val.dptr); if(val.dsize > 0) free(val.dptr); /* val.dptr[val.dsize] = '\0'; */ /* null terminate string */ /* split up val.dptr into it's components */ sscanf(line, "%s %s", value, tstamp); curtime = time(NULL); /* figure out what current time is */ /* convert this to a timestamp and not an ASCII string */ oldtime = atol(tstamp); /* * if it's too old, tell calling process we didn't find it. * Calling process should then do a fresh lookup, and call lt_gdbm_write() * below to re-write it into the database with a fresh timestamp. */ if((curtime - oldtime) > DNS_CACHE_TIME) { return FALSE; } strcpy(lt_host, value); /* copy it into our main variable */ /* free val.dptr lest we suck up memory with each call to this function :) */ return TRUE; /* tell calling function we got something */ } return FALSE; } void lt_gdbm_write(char *ip_addr, char *h_name) { /* open our database in write mode - WRCREAT means create it if it doesn't exist, * otherwise just open it in write mode */ int oumask; /* double-check our umask doesn't mutz up the world writeable file here */ oumask = umask(0000); dbf = gdbm_open (DNSDB_FILE, 0, GDBM_WRCREAT | GDBM_FAST, 0666, 0); umask(oumask); /* restore the old umask, whatever it was */ if(dbf == NULL) { return; } /* make sure we got a timestamp on the end of our value to store */ /* setup our data pointers */ key.dptr = ip_addr; key.dsize = strlen(key.dptr); val.dptr = h_name; val.dsize = strlen(val.dptr); /* dump them into the database in GDBM_REPLACE mode */ gdbm_store(dbf, key, val, GDBM_REPLACE); /* make double sure we're sync'd to disk */ gdbm_sync(dbf); /* close our database */ gdbm_close(dbf); return; }