From 467560daa6b0b9e1df17729f07399c136effc211 Mon Sep 17 00:00:00 2001 From: Tollef Fog Heen Date: Fri, 18 Sep 2009 18:35:50 +0200 Subject: [PATCH] Add initial postgresql support --- configure.ac | 16 +++++++++++ schema.sql | 14 ++++++++++ src/Makefile.am | 3 +++ src/main.c | 72 ++++++++++++++++++++++++++++++++++++++++++++----- 4 files changed, 98 insertions(+), 7 deletions(-) create mode 100644 schema.sql diff --git a/configure.ac b/configure.ac index 9350bb2..09e9d8e 100644 --- a/configure.ac +++ b/configure.ac @@ -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 index 0000000..43f6f7e --- /dev/null +++ b/schema.sql @@ -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 diff --git a/src/Makefile.am b/src/Makefile.am index 1ebe533..3d3f75d 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -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 diff --git a/src/main.c b/src/main.c index 99d9165..affaf46 100644 --- a/src/main.c +++ b/src/main.c @@ -18,22 +18,79 @@ #define PORT 8000 +#include +#include +#include +#include +#include +#include #include +#include -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; } -- 2.39.5