[exim-dev] sqlite lookup support.

Top Page
Delete this message
Reply to this message
Author: David Woodhouse
Date:  
To: exim-dev
Subject: [exim-dev] sqlite lookup support.
I really shouldn't be using lsearch and ${run sh -c "echo blah >> dbfile"}
for my greylisting. Here's sqlite3 support.

--- exim-4.52/src/lookups/sqlite.c~    2005-07-04 15:22:00.000000000 +0100
+++ exim-4.52/src/lookups/sqlite.c    2005-07-04 15:16:24.000000000 +0100
@@ -0,0 +1,103 @@
+/* $Cambridge$ */
+
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2005 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+#include "../exim.h"
+#include "lf_functions.h"
+#include "sqlite.h"
+
+#ifndef LOOKUP_SQLITE
+static void dummy(int x) { dummy(x-1); }
+#else
+#include <sqlite3.h>
+
+/*************************************************
+*              Open entry point                  *
+*************************************************/
+
+/* See local README for interface description. */
+
+void *
+sqlite_open(uschar *filename, uschar **errmsg)
+{
+sqlite3 *db = NULL;
+int ret;
+
+ret = sqlite3_open((char *)filename, &db);
+if (ret) {
+  *errmsg = (void *)sqlite3_errmsg(db);
+  debug_printf("Error opening database: %s\n", *errmsg);
+}
+
+return db;
+}
+
+
+/*************************************************
+*               Find entry point                 *
+*************************************************/
+
+/* See local README for interface description. */
+
+struct strbuf {
+  uschar *string;
+  int size;
+  int len;
+};
+
+static int sqlite_callback(void *arg, int argc, char **argv, char **azColName)
+{
+struct strbuf *res = arg;
+int i;
+
+/* For second and subsequent results, insert \n */
+if (res->string)
+  res->string = string_cat(res->string, &res->size, &res->len, US"\n", 1);
+
+if (argc > 1)
+  {
+  /* For multiple fields, include the field name too */
+  for (i=0; i<argc-1; i++)
+    res->string = string_append(res->string, &res->size, &res->len, 4,
+                azColName[i], US"=", argv[i]?argv[i]:"<NULL>", " ");
+  res->string = string_append(res->string, &res->size, &res->len, 3,
+                  azColName[i], US"=", argv[i]?argv[i]:"<NULL>");
+  }
+else
+  res->string = string_append(res->string, &res->size, &res->len, 1, argv[0]?argv[0]:"<NULL>");
+
+return 0;
+}
+
+int
+sqlite_find(void *handle, uschar *filename, uschar *query, int length,
+  uschar **result, uschar **errmsg, BOOL *do_cache)
+{
+int ret;
+struct strbuf res = { NULL, 0, 0 };
+
+ret = sqlite3_exec(handle, (char *)query, sqlite_callback, &res, (char **)errmsg);
+if (ret != SQLITE_OK)
+  {
+  debug_printf("sqlite3_exec failed: %s\n", *errmsg);
+  return FAIL;
+  }
+if (!res.string)
+  *do_cache = FALSE;
+
+*result = res.string;
+return OK;
+}
+
+void sqlite_close(void *handle)
+{
+sqlite3_close(handle);
+}
+#endif /* LOOKUP_SQLITE */
+
+/* End of lookups/sqlite.c */
--- exim-4.52/src/lookups/sqlite.h~    2005-07-04 15:22:02.000000000 +0100
+++ exim-4.52/src/lookups/sqlite.h    2005-07-04 15:12:12.000000000 +0100
@@ -0,0 +1,17 @@
+/* $Cambridge$ */
+
+/*************************************************
+*     Exim - an Internet mail transport agent    *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2005 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+/* Header for the sqlite lookup */
+
+extern void *sqlite_open(uschar *, uschar **);
+extern int   sqlite_find(void *, uschar *, uschar *, int, uschar **, uschar **,
+               BOOL *);
+extern void  sqlite_close(void *);
+
+/* End of lookups/sqlite.h */
--- exim-4.52/src/lookups/Makefile~    2005-07-01 12:09:15.000000000 +0100
+++ exim-4.52/src/lookups/Makefile    2005-07-04 15:15:03.000000000 +0100
@@ -6,8 +6,8 @@
 # defined, dummy modules get compiled.


 OBJ = cdb.o dbmdb.o dnsdb.o dsearch.o ibase.o ldap.o lsearch.o mysql.o nis.o \
-      nisplus.o oracle.o passwd.o pgsql.o spf.o testdb.o whoson.o lf_check_file.o \
-      lf_quote.o
+      nisplus.o oracle.o passwd.o pgsql.o spf.o sqlite.o testdb.o whoson.o \
+      lf_check_file.o lf_quote.o


 lookups.a:       $(OBJ)
          @/bin/rm -f lookups.a
@@ -37,6 +37,7 @@ oracle.o:        $(HDRS) oracle.c    ora
 passwd.o:        $(HDRS) passwd.c    passwd.h
 pgsql.o:         $(HDRS) pgsql.c     pgsql.h
 spf.o:           $(HDRS) spf.c       spf.h
+sqlite.o:        $(HDRS) sqlite.c    sqlite.h
 testdb.o:        $(HDRS) testdb.c    testdb.h
 whoson.o:        $(HDRS) whoson.c    whoson.h


--- exim-4.52/src/EDITME~    2005-07-01 16:29:09.000000000 +0100
+++ exim-4.52/src/EDITME    2005-07-04 15:14:17.000000000 +0100
@@ -266,6 +266,7 @@ LOOKUP_LSEARCH=yes
 # LOOKUP_ORACLE=yes
 # LOOKUP_PASSWD=yes
 # LOOKUP_PGSQL=yes
+# LOOKUP_SQLITE=yes
 # LOOKUP_WHOSON=yes


 # These two settings are obsolete; all three lookups are compiled when
--- exim-4.52/src/drtables.c~    2005-07-01 12:09:15.000000000 +0100
+++ exim-4.52/src/drtables.c    2005-07-04 15:13:57.000000000 +0100
@@ -93,6 +93,10 @@ be NULL for methods that don't need them
 #include "lookups/spf.h"
 #endif


+#ifdef LOOKUP_SQLITE
+#include "lookups/sqlite.h"
+#endif
+
#ifdef LOOKUP_TESTDB
#include "lookups/testdb.h"
#endif
@@ -458,6 +462,23 @@ Shares many functions with lsearch. */
#endif
},

+/* sqlite lookup */
+
+  {
+  US"sqlite",                    /* lookup name */
+  lookup_absfile,                /* not query style */
+#ifdef LOOKUP_SQLITE
+  sqlite_open,                   /* open function */
+  NULL,                          /* no check function */
+  sqlite_find,                   /* find function */
+  sqlite_close,                  /* close function */
+  NULL,                          /* no tidy function */
+  NULL                           /* no quoting function */
+#else
+  NULL, NULL, NULL, NULL, NULL, NULL /* lookup not present */
+#endif
+  },
+
 /* Testdb lookup is for testing Exim, not useful for normal running.
 For that reason, we omit the entry entirely when not building it into
 the binary, so that attempts to use it give "unknown lookup type" instead
--- exim-4.52/scripts/MakeLinks~    2005-07-01 12:09:15.000000000 +0100
+++ exim-4.52/scripts/MakeLinks    2005-07-04 15:27:16.000000000 +0100
@@ -84,6 +84,8 @@ ln -s ../../src/lookups/pgsql.h         
 ln -s ../../src/lookups/pgsql.c          pgsql.c
 ln -s ../../src/lookups/spf.h            spf.h
 ln -s ../../src/lookups/spf.c            spf.c
+ln -s ../../src/lookups/sqlite.h         sqlite.h
+ln -s ../../src/lookups/sqlite.c         sqlite.c
 ln -s ../../src/lookups/testdb.h         testdb.h
 ln -s ../../src/lookups/testdb.c         testdb.c
 ln -s ../../src/lookups/whoson.h         whoson.h
--- exim-4.52/src/exim.c~    2005-07-01 12:09:15.000000000 +0100
+++ exim-4.52/src/exim.c    2005-07-04 15:27:39.000000000 +0100
@@ -918,6 +918,9 @@ fprintf(f, "Lookups:");
 #ifdef LOOKUP_PGSQL
   fprintf(f, " pgsql");
 #endif
+#ifdef LOOKUP_SQLITE
+  fprintf(f, " sqlite");
+#endif
 #ifdef LOOKUP_TESTDB
   fprintf(f, " testdb");
 #endif
--- exim-4.52/src/config.h.defaults~    2005-07-04 15:25:25.000000000 +0100
+++ exim-4.52/src/config.h.defaults    2005-07-04 15:29:06.000000000 +0100
@@ -80,6 +80,7 @@ in config.h unless some value is defined
 #define LOOKUP_ORACLE
 #define LOOKUP_PASSWD
 #define LOOKUP_PGSQL
+#define LOOKUP_SQLITE
 #define LOOKUP_TESTDB
 #define LOOKUP_WHOSON
 #define LOOKUP_WILDLSEARCH



--
dwmw2