From 534e9010ebd8e0613e4a8f8924188e7174b98dc3 Mon Sep 17 00:00:00 2001 From: Tollef Fog Heen Date: Mon, 12 Oct 2009 12:59:01 +0200 Subject: [PATCH] use syslog rather than printf for logging --- configure.ac | 2 +- src/main.c | 32 +++++++++++++++++++++----------- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/configure.ac b/configure.ac index 29155ee..5cda9d0 100644 --- a/configure.ac +++ b/configure.ac @@ -23,7 +23,7 @@ fi PKG_CHECK_MODULES([libmicrohttpd], [libmicrohttpd]) AC_CHECK_LIB([yubikey], [yubikey_modhex_decode],[], AC_MSG_ERROR([libyubikey is not installed or not new enough])) - +AM_CONFIG_HEADER(src/config.h) AC_CONFIG_FILES([ Makefile src/Makefile diff --git a/src/main.c b/src/main.c index c52d24f..9e02fe9 100644 --- a/src/main.c +++ b/src/main.c @@ -33,7 +33,9 @@ #include #include #include +#include #include "util.h" +#include "config.h" #ifdef UNUSED #elif defined(__GNUC__) @@ -185,14 +187,15 @@ static int get_shared_secret(const char *id, char **shared_secret, 1); /* ask for binary results */ if (PQresultStatus(res) != PGRES_TUPLES_OK) { - fprintf(stderr, "SELECT failed: %s", PQerrorMessage(db_conn)); + syslog(LOG_ERR, "Failed to get shared secret for id=%s: %s", + id, PQerrorMessage(db_conn)); r = -1; goto free_mem; /* XXX Return error object */ } if (PQntuples(res) == 0) { + syslog(LOG_INFO, "No such id: %s", id); /* XXX Better handling */ - fprintf(stderr, "No such secrets: %s\n", id); r = -1; goto free_mem; } @@ -264,7 +267,8 @@ static int set_data_for_uid(char *uid, struct ykc_stats *stats) 1); /* ask for binary results */ if (PQresultStatus(res) != PGRES_COMMAND_OK) { - fprintf(stderr, "UPDATE failed: %s\n", PQerrorMessage(db_conn)); + syslog(LOG_ERR, "UPDATE for %s failed: %s", uid, + PQerrorMessage(db_conn)); r = -1; goto free_mem; } @@ -296,7 +300,8 @@ static int get_data_for_uid(char *uid, struct ykc_stats *stats) if (PQresultStatus(res) != PGRES_TUPLES_OK) { - fprintf(stderr, "SELECT failed: %s", PQerrorMessage(db_conn)); + syslog(LOG_ERR, "Failed to get shared secret for uid=%s: %s", + uid, PQerrorMessage(db_conn)); r = -1; goto free_mem; } @@ -356,8 +361,8 @@ static int handle_request(void * UNUSED(data), id = MHD_lookup_connection_value(conn, MHD_GET_ARGUMENT_KIND, "id"); otp = MHD_lookup_connection_value(conn, MHD_GET_ARGUMENT_KIND, "otp"); h = MHD_lookup_connection_value(conn, MHD_GET_ARGUMENT_KIND, "h"); - fprintf(stderr, "got params: url=%s id=%s otp=%s, h=%s\n", url, id, - otp, h); + syslog(LOG_DEBUG, "Got new connection with parameters: " + "url=%s id=%s otp=%s, h=%s\n", url, id, otp, h); /* Do query to grab shared secret, we need this later anyway */ if (get_shared_secret(id, &shared_secret, &shared_secret_len) < 0) { @@ -421,7 +426,8 @@ static int handle_request(void * UNUSED(data), signature = sign_request(shared_secret, shared_secret_len, NULL, status, timestamp); send_response(conn, signature, status, NULL, timestamp); - fprintf(stderr, "replay\n"); + syslog(LOG_NOTICE, "Replay attempt for otp=%s, id=%s, uid=%s", + otp, id, uid); goto free_mem; } @@ -434,7 +440,8 @@ static int handle_request(void * UNUSED(data), stats.session_use = token.use; set_data_for_uid(uid, &stats); /* Generate response, sign it */ - fprintf(stderr, "ok request\n"); + syslog(LOG_INFO, "OK request for otp=%s, id=%s, uid=%s", + otp, id, uid); status = "OK"; signature = sign_request(shared_secret, shared_secret_len, NULL, status, timestamp); @@ -455,11 +462,14 @@ int main(int UNUSED(argc), char ** UNUSED(argv)) { struct MHD_Daemon *d; + openlog("yubikeyd", LOG_PID, LOG_AUTHPRIV); + syslog(LOG_NOTICE, "yubikeyd version %s starting up", VERSION); + /* XXX connect args should go in config file */ db_conn = PQconnectdb("dbname=yubikey port=5433"); if (PQstatus(db_conn) != CONNECTION_OK) { - fprintf(stderr, "Connection to database failed: %s", - PQerrorMessage(db_conn)); + syslog(LOG_ERR, "connection to database failed: %s", + PQerrorMessage(db_conn)); exit(1); } d = MHD_start_daemon(MHD_USE_DEBUG, @@ -470,7 +480,7 @@ int main(int UNUSED(argc), char ** UNUSED(argv)) NULL, /* Data for default handler */ MHD_OPTION_END); if (d == NULL) { - fprintf(stderr, "Could not start daemon\n"); + syslog(LOG_ERR, "could not start daemon, unsure why\n"); exit(1); } while (1) { -- 2.39.5