changeset 19:168009f3e66f

argh, sendmail is a pos. some major fudge to store keys as strings
author Simon Horman <horms@verge.net.au>
date Thu, 16 May 2002 16:48:16 +0900
parents 6edc123bfc48
children bf731fa9c758
files pbs/Makefile.am pbs/Makefile.in pbs/pbs.c pbs/pbs_record.c pbs/pbs_record.h pbs/pbs_record_db.c pbs/pbs_record_db.h
diffstat 7 files changed, 248 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/pbs/Makefile.am	Thu May 16 11:44:16 2002 +0900
+++ b/pbs/Makefile.am	Thu May 16 16:48:16 2002 +0900
@@ -35,7 +35,8 @@
 pbs_db.c pbs_db.h \
 pbs_log.h \
 pbs_option.c pbs_option.h \
-pbs_record.c pbs_record.h 
+pbs_record.c pbs_record.h \
+pbs_record_db.c pbs_record_db.h 
 
 #INCLUDES= 
 
--- a/pbs/Makefile.in	Thu May 16 11:44:16 2002 +0900
+++ b/pbs/Makefile.in	Thu May 16 16:48:16 2002 +0900
@@ -1,4 +1,4 @@
-# Makefile.in generated automatically by automake 1.4 from Makefile.am
+# Makefile.in generated automatically by automake 1.4-p4 from Makefile.am
 
 # Copyright (C) 1994, 1995-8, 1999 Free Software Foundation, Inc.
 # This Makefile.in is free software; the Free Software Foundation
@@ -99,7 +99,7 @@
 
 EXTRA_DIST = perdition-pbs.8
 
-perdition_pbs_SOURCES =  pbs.c pbs_db.c pbs_db.h pbs_log.h pbs_option.c pbs_option.h pbs_record.c pbs_record.h 
+perdition_pbs_SOURCES =  pbs.c pbs_db.c pbs_db.h pbs_log.h pbs_option.c pbs_option.h pbs_record.c pbs_record.h pbs_record_db.c pbs_record_db.h 
 
 
 #INCLUDES= 
@@ -119,7 +119,8 @@
 CPPFLAGS = @CPPFLAGS@
 LDFLAGS = @LDFLAGS@
 LIBS = @LIBS@
-perdition_pbs_OBJECTS =  pbs.o pbs_db.o pbs_option.o pbs_record.o
+perdition_pbs_OBJECTS =  pbs.o pbs_db.o pbs_option.o pbs_record.o \
+pbs_record_db.o
 perdition_pbs_LDADD = $(LDADD)
 perdition_pbs_DEPENDENCIES = 
 perdition_pbs_LDFLAGS = 
@@ -136,10 +137,10 @@
 
 DISTFILES = $(DIST_COMMON) $(SOURCES) $(HEADERS) $(TEXINFOS) $(EXTRA_DIST)
 
-TAR = gtar
+TAR = tar
 GZIP_ENV = --best
 DEP_FILES =  .deps/pbs.P .deps/pbs_db.P .deps/pbs_option.P \
-.deps/pbs_record.P
+.deps/pbs_record.P .deps/pbs_record_db.P
 SOURCES = $(perdition_pbs_SOURCES)
 OBJECTS = $(perdition_pbs_OBJECTS)
 
--- a/pbs/pbs.c	Thu May 16 11:44:16 2002 +0900
+++ b/pbs/pbs.c	Thu May 16 16:48:16 2002 +0900
@@ -42,7 +42,7 @@
 #include <vanessa_socket.h>
 
 #include "pbs_log.h"
-#include "pbs_db.h"
+#include "pbs_record_db.h"
 #include "pbs_option.h"
 #include "pbs_record.h"
 
@@ -525,8 +525,8 @@
 	}
 
 	expire = time(NULL) + timeout;
-	if(pbs_db_put(db, ip, strlen(ip)+1, &expire, sizeof(expire)) < 0) {
-		PBS_DEBUG("pbs_db_put");
+	if(pbs_record_db_put(db, ip, prefix, expire, ip) < 0) {
+		PBS_DEBUG("pbs_record_store");
 		status = -1;
 	}
 
@@ -646,10 +646,7 @@
 		time_t timeout, int quiet) {
 	size_t width;
 	time_t expire;
-	size_t buf_len = 0;
 	const char **k;
-	const char *k_fixed;
-	char *buf = NULL;
 
 	if(key == NULL) {
 		PBS_DEBUG("no key");
@@ -663,17 +660,10 @@
 		return(-1);
 	}
 
+	expire = time(NULL) + timeout;
 	for(k = key ; *k != NULL; k++) {
-
-		k_fixed = pbs_record_fix_key(*k, prefix, &buf, &buf_len);
-		if(k_fixed == NULL) {
-			PBS_DEBUG("pbs_record_fix_key");
-			return(-1);
-		}
-		expire = time(NULL) + timeout;
-		if(pbs_db_put(db, (char *)k_fixed, strlen(k_fixed)+1,
-					&expire, sizeof(expire)) < 0) {
-			PBS_DEBUG("pbs_db_put");
+		if(pbs_record_db_put(db, *k, prefix, expire, *k) < 0) {
+			PBS_DEBUG("pbs_record_store");
 			return(-1);
 		}
 		if(!quiet) {
@@ -681,10 +671,6 @@
 		}
 	}
 
-	if(buf != NULL) {
-		free(buf);
-	}
-
 	return(0);
 }
 
--- a/pbs/pbs_record.c	Thu May 16 11:44:16 2002 +0900
+++ b/pbs/pbs_record.c	Thu May 16 16:48:16 2002 +0900
@@ -110,12 +110,35 @@
 }
 
 
+char *pbs_record_prefix_key(const char *key, const char *prefix) {
+	size_t prefix_len;
+	size_t key_len;
+	size_t new_key_len;
+	char *new_key;
+
+
+	prefix_len = strlen(prefix);
+	key_len = strlen(key);
+	new_key_len = key_len + prefix_len + 1;
+
+	new_key = (char *)malloc(new_key_len);
+	if(new_key == NULL) {
+		PBS_DEBUG_ERRNO("realloc");
+		return(NULL);
+	}
+	memset(new_key, 0, new_key_len);
+	memcpy(new_key, prefix, prefix_len);
+	memcpy(new_key+prefix_len, key, key_len);
+	
+	return(new_key);
+}
+
 const char *pbs_record_fix_key(const char *key, const char *prefix,
 		char **buf, size_t *buf_len) {
 	size_t prefix_len;
 	size_t k_len;
 
-	prefix_len = strlen(prefix); /* :( */
+	prefix_len = strlen(prefix);
 
 	if(strncmp(key, prefix, prefix_len) == 0) {
 		return(key);
@@ -125,7 +148,7 @@
 	if(*buf_len < prefix_len +k_len + 1) {
 		*buf_len = prefix_len + k_len + 1;
 		*buf = realloc(*buf, *buf_len);
-		if(buf == NULL) {
+		if(*buf == NULL) {
 			PBS_DEBUG_ERRNO("realloc");
 			return(NULL);
 		}
--- a/pbs/pbs_record.h	Thu May 16 11:44:16 2002 +0900
+++ b/pbs/pbs_record.h	Thu May 16 16:48:16 2002 +0900
@@ -43,6 +43,7 @@
 int pbs_record_title(size_t width);
 int pbs_record_top(size_t width);
 size_t pbs_key_width(const char *prefix);
+char *pbs_record_prefix_key(const char *key, const char *prefix);
 const char *pbs_record_fix_key(const char *key, const char *prefix,
 		                char **buf, size_t *buf_len);
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pbs/pbs_record_db.c	Thu May 16 16:48:16 2002 +0900
@@ -0,0 +1,168 @@
+/**********************************************************************
+ * pbs_record_db.c                                             May 2002
+ * Horms                                             horms@vergenet.net
+ *
+ * Perdition PBS
+ * Pop Before SMTP Tools
+ * Copyright (C) 2002 Horms
+ * 
+ * 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.
+ * 
+ * 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., 59 Temple Place, Suite 330, Boston, MA
+ * 02111-1307  USA
+ *
+ **********************************************************************/
+
+#include <stdlib.h>
+
+#include "pbs_db.h"
+#include "pbs_record.h"
+#include "pbs_log.h"
+
+#define PBS_TIME_PREFIX "TIME:"
+
+int pbs_record_db_put(pbs_db_t *db, const char *ip, const char *prefix, 
+		const time_t expire, const char *status) {
+	const char *ip_fixed;
+	char *time_ip = NULL;
+	size_t buf_len = 0;
+	char *buf = NULL;
+	int exit_status = -1;
+
+	ip_fixed = pbs_record_fix_key(ip, prefix, &buf, &buf_len);
+	if(ip_fixed == NULL) {
+		PBS_DEBUG("pbs_record_fix_key");
+		goto leave;
+	}
+
+	time_ip = pbs_record_prefix_key(ip_fixed, PBS_TIME_PREFIX);
+	if(time_ip == NULL) {
+		PBS_DEBUG("pbs_record_prefix_key");
+		goto leave;
+	}
+
+	if(pbs_db_put(db, (char *)ip_fixed, strlen(ip_fixed)+1,
+				(char *)status, strlen(status)+1) < 0) {
+		PBS_DEBUG("pbs_db_put");
+		goto leave;
+	}
+
+	if(pbs_db_put(db, (void *)time_ip, strlen(time_ip)+1,
+				(void *)&expire, sizeof(expire)) < 0) {
+		PBS_DEBUG("pbs_db_put");
+		pbs_db_del(db, (char *)ip_fixed, strlen(ip_fixed)+1);
+		goto leave;
+	}
+
+	exit_status = 0;
+leave:
+	if(buf != NULL) {
+		free(buf);
+	}
+	if(time_ip != NULL) {
+		free(time_ip);
+	}
+
+	return(exit_status);
+}
+
+
+int pbs_record_db_get(pbs_db_t *db, const char *ip, const char *prefix, 
+		time_t *expire, char **status, size_t *status_len) {
+	const char *ip_fixed;
+	char *time_ip = NULL;
+	size_t buf_len = 0;
+	size_t dummy;
+	char *buf = NULL;
+	int exit_status = -1;
+
+	ip_fixed = pbs_record_fix_key(ip, prefix, &buf, &buf_len);
+	if(ip_fixed == NULL) {
+		PBS_DEBUG("pbs_record_fix_key");
+		goto leave;
+	}
+
+	time_ip = pbs_record_prefix_key(ip_fixed, PBS_TIME_PREFIX);
+	if(time_ip == NULL) {
+		PBS_DEBUG("pbs_record_prefix_key");
+		goto leave;
+	}
+
+	if(pbs_db_get(db, (void *)ip_fixed, strlen(ip_fixed)+1,
+				(void **)status, status_len) < 0) {
+		PBS_DEBUG("pbs_db_get");
+		goto leave;
+	}
+	if(*status_len > 0) {
+		*status_len=*status_len-1;
+	}
+
+	if(pbs_db_get(db, (void *)time_ip, strlen(time_ip)+1,
+				(void **)expire, &dummy) < 0) {
+		PBS_DEBUG("pbs_db_get");
+		goto leave;
+	}
+
+	exit_status = 0;
+leave:
+	if(buf != NULL) {
+		free(buf);
+	}
+	if(time_ip != NULL) {
+		free(time_ip);
+	}
+
+	return(exit_status);
+}
+
+
+int pbs_record_db_del(pbs_db_t *db, const char *ip, const char *prefix) {
+	const char *ip_fixed;
+	char *time_ip = NULL;
+	size_t buf_len = 0;
+	char *buf = NULL;
+	int exit_status = -1;
+
+	ip_fixed = pbs_record_fix_key(ip, prefix, &buf, &buf_len);
+	if(ip_fixed == NULL) {
+		PBS_DEBUG("pbs_record_fix_key");
+		goto leave;
+	}
+
+	time_ip = pbs_record_prefix_key(ip_fixed, PBS_TIME_PREFIX);
+	if(time_ip == NULL) {
+		PBS_DEBUG("pbs_record_prefix_key");
+		goto leave;
+	}
+
+	if(pbs_db_del(db, (char *)ip_fixed, strlen(ip_fixed)+1) < 0) {
+		PBS_DEBUG("pbs_db_get");
+		goto leave;
+	}
+
+	if(pbs_db_del(db, (char *)time_ip, strlen(time_ip)+1) < 0) {
+		PBS_DEBUG("pbs_db_get");
+		goto leave;
+	}
+
+	exit_status = 0;
+leave:
+	if(buf != NULL) {
+		free(buf);
+	}
+	if(time_ip != NULL) {
+		free(time_ip);
+	}
+
+	return(exit_status);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pbs/pbs_record_db.h	Thu May 16 16:48:16 2002 +0900
@@ -0,0 +1,40 @@
+/**********************************************************************
+ * pbs_record_db.c                                             May 2002
+ * Horms                                             horms@vergenet.net
+ *
+ * Perdition PBS
+ * Pop Before SMTP Tools
+ * Copyright (C) 2002 Horms
+ * 
+ * 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.
+ * 
+ * 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., 59 Temple Place, Suite 330, Boston, MA
+ * 02111-1307  USA
+ *
+ **********************************************************************/
+
+#ifndef _PBS_RECORD_DB_H
+#define _PBS_RECORD_DB_H
+
+#include "pbs_db.h"
+
+int pbs_record_db_put(pbs_db_t *db, const char *ip, const char *prefix, 
+		const time_t expire, const char *status);
+
+
+int pbs_record_db_get(pbs_db_t *db, const char *ip, const char *prefix, 
+		time_t *expire, char **status, size_t *status_len);
+
+int pbs_record_db_del(pbs_db_t *db, const char *ip, const char *prefix);
+
+#endif /* _PBS_RECORD_DB_H */