]> err.no Git - yubikey-server-c/commitdiff
Add initial postgresql support
authorTollef Fog Heen <tfheen@err.no>
Fri, 18 Sep 2009 16:35:50 +0000 (18:35 +0200)
committerTollef Fog Heen <tfheen@err.no>
Fri, 18 Sep 2009 16:35:50 +0000 (18:35 +0200)
configure.ac
schema.sql [new file with mode: 0644]
src/Makefile.am
src/main.c

index 9350bb2bc34790fb37ee31b2fa0ea893739a0e0e..09e9d8ed9935bf1e83b6089a2107fc46c92ce47c 100644 (file)
@@ -7,6 +7,22 @@ AC_CONFIG_MACRO_DIR([m4])
 AM_INIT_AUTOMAKE([-Wall -Werror foreign])
 AC_PROG_CC
 
+AC_MSG_CHECKING([for PostgreSQL libraries])
+
+AC_PATH_PROG([PG_CONFIG], [pg_config], [])
+if test "$PG_CONFIG" != "no"; then
+   POSTGRESQL_CFLAGS="-I`$PG_CONFIG --includedir`"
+   POSTGRESQL_LDFLAGS="-L`$PG_CONFIG --libdir` -lpq"
+   AC_SUBST([POSTGRESQL_CFLAGS])
+   AC_SUBST([POSTGRESQL_LDFLAGS])
+   AC_MSG_RESULT([yes])
+else
+   AC_MSG_ERROR([$PG_CONFIG is not installed - install it and postgresql headers?])
+fi
+
+
+PKG_CHECK_MODULES([libmicrohttpd], [libmicrohttpd])
+
 AC_CONFIG_FILES([
   Makefile
   src/Makefile
diff --git a/schema.sql b/schema.sql
new file mode 100644 (file)
index 0000000..43f6f7e
--- /dev/null
@@ -0,0 +1,14 @@
+
+-- --
+-- This should work with at least PostgreSQL and SQLite.  Maybe more --
+-- --
+
+CREATE TABLE yubikey (
+       yubikey_id int NOT NULL,
+       active boolean NOT NULL DEFAULT 'f',
+       secret_id varchar NOT NULL,
+       session_counter int,
+       session_use int
+);
+
+INSERT INTO yubikey (yubikey_id, active, secret_id, session_counter, session_use) VALUES (0, 't', 'secret', 0,0);
\ No newline at end of file
index 1ebe53336d6c7054a11dd9c8b4b68a7e911d5ebb..3d3f75db9c73b85d0c7886b99d5b70a8802c2ca6 100644 (file)
@@ -2,3 +2,6 @@
 sbin_PROGRAMS = yubikeyd
 
 yubikeyd_SOURCES = main.c
+
+yubikeyd_CFLAGS = @libmicrohttpd_CFLAGS@ @POSTGRESQL_CFLAGS@
+yubikeyd_LDADD = @libmicrohttpd_LIBS@ @POSTGRESQL_LDFLAGS@
\ No newline at end of file
index 99d9165c2717e0578d38f269d1b312712a6d69d5..affaf46aa38ff514645f2eb1d56277a9f26186df 100644 (file)
 
 #define PORT 8000
 
+#include <stdlib.h>
+#include <unistd.h>
+#include <stdint.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <string.h>
 #include <microhttpd.h>
+#include <libpq-fe.h>
 
-int ContentReaderCallback(void *data, uint64_t pos, char *buf, int max)
+PGconn     *db_conn;
+
+static int handle_request(void *data,
+                         struct MHD_Connection *conn,
+                         const char *url,
+                         const char *method,
+                         const char *version,
+                         const char *upload_data,
+                         size_t *upload_data_size,
+                         void **con_cls)
 {
-       return strncpy(buf, "This is a test" + pos, max);
+       static char *ret = "This is a test";
+       int r;
+       struct MHD_Response *response;
+       PGresult   *res;
+       const char *paramValues[1];
+       int i;
+
+       paramValues[0] = 0;
+       res = PQexecParams(db_conn,
+                          "SELECT secret_id FROM yubikey WHERE yubikey_id = $1",
+                          1,       /* one param */
+                          NULL,    /* let the backend deduce param type */
+                          paramValues,
+                          NULL,    /* don't need param lengths since text */
+                          NULL,    /* default to all text params */
+                          1);      /* ask for binary results */
+
+       if (PQresultStatus(res) != PGRES_TUPLES_OK)
+       {
+               fprintf(stderr, "SELECT failed: %s", PQerrorMessage(db_conn));
+               PQclear(res);
+               return MHD_NO;
+       }
+
+       for (i = 0; i < PQntuples(res); i++) {
+               const char *fullname;
+               
+               fullname = PQgetvalue(res, i, 0);
+               response = MHD_create_response_from_data(strlen(fullname), (void*)fullname,
+                                                        MHD_NO, MHD_NO);
+               r = MHD_queue_response(conn, MHD_HTTP_OK, response);
+               MHD_destroy_response(response);
+               fprintf(stderr, "%s %s\n", method, url);
+       }
+       PQclear(res);
+       return r;
 }
 
 int main(int argc, char **argv)
 {
        struct MHD_Daemon *d;
 
-       d = MHD_start_daemon (MHD_USE_DEBUG,
-                             PORT,
-                             NULL, /* Access policy handler */
-                             NULL, /* Data to access policy handler */
-                             ContentReaderCallback, /* default handler for all URIs */
+       db_conn = PQconnectdb("dbname=yubikey");
+       if (PQstatus(db_conn) != CONNECTION_OK) {
+               fprintf(stderr, "Connection to database failed: %s",
+                       PQerrorMessage(db_conn));
+               exit(1);
+       }
+       d = MHD_start_daemon(MHD_USE_DEBUG,
+                            PORT,
+                            NULL, /* Access policy handler */
+                            NULL, /* Data to access policy handler */
+                            handle_request, /* default handler for all URIs */
                              NULL, /* Data for default handler */
                              MHD_OPTION_END);
        if (d == NULL) {
@@ -47,6 +104,7 @@ int main(int argc, char **argv)
                }
        }
        MHD_stop_daemon (d);
+       PQfinish(db_conn);
        return 0;
 }