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
--- /dev/null
+
+-- --
+-- 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
#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) {
}
}
MHD_stop_daemon (d);
+ PQfinish(db_conn);
return 0;
}