From 70f54cfd11d2d3f14690fd4962043965b18319fb Mon Sep 17 00:00:00 2001 From: kakwa Date: Sat, 4 Nov 2017 13:11:33 +0100 Subject: [PATCH 01/21] fix civetweb bundling + version bump + changelog --- CMakeLists.txt | 17 +++++++++++++++-- ChangeLog.rst | 8 ++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f5c4826..1dc4ac9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required (VERSION 2.6) project (uts-server) include(ExternalProject) -set(VERSION 0.1.8) +set(VERSION 0.1.9) option(DEBUG "compile with debug symbol" OFF) option(BUNDLE_CIVETWEB "bundle civetweb with uts-server" OFF) @@ -13,11 +13,19 @@ option(CIVETWEB_CUST_ARGS "Custom args for civetweb (if civetweb is bundled)" "" option(LINK_PTHREAD "link pthread" OFF) IF("$ENV{CIVETWEB_GITURL}" STREQUAL "") - set(CIVETWEB_GITURL "https://github.com/kakwa/civetweb") + set(CIVETWEB_GITURL "https://github.com/civetweb/civetweb") ELSE() set(CIVETWEB_GITURL "$ENV{CIVETWEB_GITURL}") ENDIF() +IF("$ENV{CIVETWEB_GITTAG}" STREQUAL "") + set(CIVETWEB_GITTAG "v1.10") +ELSE() + set(CIVETWEB_GITTAG "$ENV{CIVETWEB_GITTAG}") +ENDIF() + + + IF(STATIC) set(CMAKE_FIND_LIBRARY_SUFFIXES ".a") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static") @@ -92,9 +100,13 @@ if(BUNDLE_CIVETWEB) MESSAGE(STATUS "Building Civetweb From: ${CIVETWEB_GITURL}") ExternalProject_Add( civetweb GIT_REPOSITORY ${CIVETWEB_GITURL} + GIT_TAG ${CIVETWEB_GITTAG} PATCH_COMMAND sed -i ${SED_FREEBSD} s/__DATE__/"110973"/ src/main.c src/civetweb.c INSTALL_DIR /usr/local CMAKE_ARGS .. -DCMAKE_INSTALL_PREFIX=/usr + -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} + -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER} + -DCMAKE_C_FLAGS="-Wno-incompatible-pointer-types" -DCIVETWEB_ENABLE_CXX=OFF -DBUILD_SHARED_LIBS=OFF -DCIVETWEB_DISABLE_CGI=ON @@ -105,6 +117,7 @@ if(BUNDLE_CIVETWEB) -DCIVETWEB_SSL_OPENSSL_API_1_1=${OPENSSL_API_1_1} -DOPENSSL_USE_STATIC_LIBS=${OPENSSL_USE_STATIC_LIBS} -DCIVETWEB_ENABLE_SSL_DYNAMIC_LOADING=OFF + -DCIVETWEB_ALLOW_WARNINGS=ON ${CIVETWEB_CUST_ARGS} INSTALL_COMMAND make install DESTDIR=${CMAKE_CURRENT_SOURCE_DIR}/vendor/ ) diff --git a/ChangeLog.rst b/ChangeLog.rst index 422cf14..6dc2b8f 100644 --- a/ChangeLog.rst +++ b/ChangeLog.rst @@ -1,6 +1,14 @@ Changelogs ========== +0.1.9 +----- + +* [fix ] point to upstream civetweb (forked civetweb now removed) +* [fix ] disable some compiler warnings to allow civetweb compilation +* [impr] add possibility to specify which tag used for civetweb bundling +* [impr] use same compiler for uts-server and civetweb when bundling + 0.1.8 ----- From 3de83303e1bb6e062f70a95352457de15fcf96a0 Mon Sep 17 00:00:00 2001 From: kakwa Date: Sat, 4 Nov 2017 14:10:41 +0100 Subject: [PATCH 02/21] use openssl dynamic loading for civetweb --- CMakeLists.txt | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8f2bef5..812ad5b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -42,6 +42,10 @@ IF(STATIC) set(CIVETWEB_USE_STATIC_LIBS ON) ENDIF(STATIC) +IF(BUNDLE_CIVETWEB) + set(DL_LIBRARIES 'dl') +ENDIF(BUNDLE_CIVETWEB) + IF(LINK_DL) set(DL_LIBRARIES 'dl') ENDIF(LINK_DL) @@ -114,7 +118,6 @@ if(BUNDLE_CIVETWEB) CMAKE_ARGS .. -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER} - -DCMAKE_C_FLAGS="-Wno-incompatible-pointer-types" -DCIVETWEB_ENABLE_CXX=OFF -DBUILD_SHARED_LIBS=OFF -DCIVETWEB_DISABLE_CGI=ON @@ -124,7 +127,7 @@ if(BUNDLE_CIVETWEB) -DBUILD_TESTING=OFF -DCIVETWEB_SSL_OPENSSL_API_1_1=${OPENSSL_API_1_1} -DOPENSSL_USE_STATIC_LIBS=${OPENSSL_USE_STATIC_LIBS} - -DCIVETWEB_ENABLE_SSL_DYNAMIC_LOADING=OFF + -DCIVETWEB_ENABLE_SSL_DYNAMIC_LOADING=ON -DCIVETWEB_ALLOW_WARNINGS=ON ${CIVETWEB_CUST_ARGS} INSTALL_COMMAND make install DESTDIR=${CMAKE_CURRENT_SOURCE_DIR}/vendor/ From 03f9bfed56b14820bfda225bdc0e1b6bffec4d20 Mon Sep 17 00:00:00 2001 From: kakwa Date: Sat, 4 Nov 2017 14:22:25 +0100 Subject: [PATCH 03/21] only link dl on Linux --- CMakeLists.txt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 812ad5b..cf6f798 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,7 +43,9 @@ IF(STATIC) ENDIF(STATIC) IF(BUNDLE_CIVETWEB) - set(DL_LIBRARIES 'dl') + IF(${CMAKE_SYSTEM_NAME} MATCHES "Linux") + set(DL_LIBRARIES 'dl') + ENDIF(${CMAKE_SYSTEM_NAME} MATCHES "Linux") ENDIF(BUNDLE_CIVETWEB) IF(LINK_DL) From 5c180efee49f879c5fe33db72a4b56110930ec9c Mon Sep 17 00:00:00 2001 From: kakwa Date: Sat, 4 Nov 2017 14:28:41 +0100 Subject: [PATCH 04/21] updating changelog --- ChangeLog.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ChangeLog.rst b/ChangeLog.rst index 66ebae7..20eda51 100644 --- a/ChangeLog.rst +++ b/ChangeLog.rst @@ -5,7 +5,7 @@ Changelogs ----- * [fix ] point to upstream civetweb (forked civetweb now removed) -* [fix ] disable some compiler warnings to allow civetweb compilation +* [fix ] using dynamic openssl loading for civetweb when bundling * [impr] add possibility to specify which tag used for civetweb bundling * [impr] use same compiler for uts-server and civetweb when bundling From 2a9aa85ec6d3f63b8e541a8e692c3a6569239520 Mon Sep 17 00:00:00 2001 From: Carpentier Pierre-Francois Date: Wed, 6 Dec 2017 21:08:54 +0100 Subject: [PATCH 05/21] Set theme jekyll-theme-hacker --- _config.yml | 1 + 1 file changed, 1 insertion(+) create mode 100644 _config.yml diff --git a/_config.yml b/_config.yml new file mode 100644 index 0000000..fc24e7a --- /dev/null +++ b/_config.yml @@ -0,0 +1 @@ +theme: jekyll-theme-hacker \ No newline at end of file From 20b747565fa020e85af68b7e1b8d9182f4cb87f2 Mon Sep 17 00:00:00 2001 From: Carpentier Pierre-Francois Date: Tue, 12 Mar 2019 19:01:14 +0100 Subject: [PATCH 06/21] Update uts-server.conf --- goodies/rhel/uts-server.conf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/goodies/rhel/uts-server.conf b/goodies/rhel/uts-server.conf index 5e145cc..29c6133 100644 --- a/goodies/rhel/uts-server.conf +++ b/goodies/rhel/uts-server.conf @@ -1 +1 @@ -d /run/ldapcherryd 0755 ldapcherry ldapcherry - +d /run/ldapcherryd 0755 uts-server uts-server - From 3e15083a31387652853be4f9263a820a36a70f69 Mon Sep 17 00:00:00 2001 From: Carpentier Pierre-Francois Date: Tue, 12 Mar 2019 19:01:28 +0100 Subject: [PATCH 07/21] Update uts-server.conf --- goodies/rhel/uts-server.conf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/goodies/rhel/uts-server.conf b/goodies/rhel/uts-server.conf index 29c6133..751814e 100644 --- a/goodies/rhel/uts-server.conf +++ b/goodies/rhel/uts-server.conf @@ -1 +1 @@ -d /run/ldapcherryd 0755 uts-server uts-server - +d /run/uts-server 0755 uts-server uts-server - From d034fc727a262ea058c3716dba955927a2a3d72d Mon Sep 17 00:00:00 2001 From: kakwa Date: Thu, 14 Mar 2019 11:52:24 +0100 Subject: [PATCH 08/21] update civetweb version when bundling --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index cf6f798..f001f15 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,7 +27,7 @@ ELSE() ENDIF() IF("$ENV{CIVETWEB_GITTAG}" STREQUAL "") - set(CIVETWEB_GITTAG "v1.10") + set(CIVETWEB_GITTAG "v1.11") ELSE() set(CIVETWEB_GITTAG "$ENV{CIVETWEB_GITTAG}") ENDIF() From 1b9739969443c78639cf6ee67bb804ad8bd9ff27 Mon Sep 17 00:00:00 2001 From: kakwa Date: Wed, 10 Apr 2019 22:47:38 +0200 Subject: [PATCH 09/21] better handling for logging to stdout * disable stdout buffering when logging to stdout * add 'log_to_syslog' parameter in configuration file to enable/disable logging to syslog * add 'log_to_stdout' parameter in configuration file to enable/disable logging to stdout slight clean-up and reformatting also (thanks to clang-format new version) --- conf/uts-server.cnf | 6 ++++++ inc/context.h | 11 +++++++++-- src/lib/http.c | 20 +++++++++++++------- src/lib/rfc3161.c | 5 +++-- src/lib/utils.c | 25 ++++++++++++++++++++----- 5 files changed, 51 insertions(+), 16 deletions(-) diff --git a/conf/uts-server.cnf b/conf/uts-server.cnf index 6dff509..d3dae19 100644 --- a/conf/uts-server.cnf +++ b/conf/uts-server.cnf @@ -103,6 +103,12 @@ tcp_nodelay = 0 # Loglevel (debug, info, notice, warn, err, emerg, crit) log_level = info +# Enable logging to syslog (default: yes) +log_to_syslog = yes + +# Enable logging to stdout (default: no) +#log_to_stdout = no + # TSA configuration parameters. [ tsa ] diff --git a/inc/context.h b/inc/context.h index e4f4dcf..ff88fa1 100644 --- a/inc/context.h +++ b/inc/context.h @@ -9,8 +9,9 @@ #define HTTP_OPTIONS 1 #define LOGLEVEL_OPTIONS 2 -#define TSA_OPTIONS 3 -#define PATH_HTTP_OPTIONS 4 +#define LOGHANDLER_OPTIONS 3 +#define TSA_OPTIONS 4 +#define PATH_HTTP_OPTIONS 5 #define MAIN_CONF_SECTION "main" #define RFC3161_OPTIONS_LEN \ @@ -26,6 +27,10 @@ typedef struct { uint64_t query_counter; // flag for debugging bool stdout_dbg; + // flag for logging to stdout + bool stdout_logging; + // flag for logging to stdout + bool syslog_logging; // log level int loglevel; // number of threads @@ -64,6 +69,8 @@ static struct rfc3161_option rfc3161_options[] = { {"access_control_allow_origin", HTTP_OPTIONS, "*"}, {"tcp_nodelay", HTTP_OPTIONS, "0"}, {"log_level", LOGLEVEL_OPTIONS, "info"}, + {"log_to_syslog", LOGHANDLER_OPTIONS, "yes"}, + {"log_to_stdout", LOGHANDLER_OPTIONS, "no"}, {"ssl_certificate", PATH_HTTP_OPTIONS, NULL}, {"ssl_ca_path", PATH_HTTP_OPTIONS, NULL}, {"ssl_ca_file", PATH_HTTP_OPTIONS, NULL}, diff --git a/src/lib/http.c b/src/lib/http.c index c85bcfa..9ab3c18 100644 --- a/src/lib/http.c +++ b/src/lib/http.c @@ -92,9 +92,10 @@ void log_request(const struct mg_request_info *request_info, char *request_id, } } - uts_logger(context, LOG_INFO, "Request[%s], remote_addr[%s] ssl[%d] " - "uri[%s] http_resp_code[%d] duration[%d us] " - "user-agent[%s] content-type[%s]", + uts_logger(context, LOG_INFO, + "Request[%s], remote_addr[%s] ssl[%d] " + "uri[%s] http_resp_code[%d] duration[%d us] " + "user-agent[%s] content-type[%s]", request_id, null_undef(request_info->remote_addr), request_info->is_ssl, null_undef(request_info->local_uri), response_code, timer, null_undef(user_agent), @@ -178,10 +179,11 @@ int rfc3161_handler(struct mg_connection *conn, void *context) { // respond according to create_response return code switch (resp_code) { case 200: - mg_printf(conn, "HTTP/1.1 200 OK\r\n" - "Content-Type: application/timestamp-reply\r\n" - "Content-Length: %d\r\n" - "\r\n", + mg_printf(conn, + "HTTP/1.1 200 OK\r\n" + "Content-Type: application/timestamp-reply\r\n" + "Content-Length: %d\r\n" + "\r\n", (int)content_length); mg_write(conn, content, content_length); log_hex(ct, LOG_DEBUG, "response hexdump content", content, @@ -241,6 +243,10 @@ int http_server_start(char *conffile, char *conf_wd, bool stdout_dbg) { if (!set_params(ct, conffile, conf_wd)) return EXIT_FAILURE; + // Disable stdout buffering if logging to stdout + if (ct->stdout_logging || ct->stdout_dbg) + setbuf(stdout, NULL); + // Prepare callbacks structure. We have only one callback, the rest are // NULL. memset(&callbacks, 0, sizeof(callbacks)); diff --git a/src/lib/rfc3161.c b/src/lib/rfc3161.c index d923c8f..e8b6865 100644 --- a/src/lib/rfc3161.c +++ b/src/lib/rfc3161.c @@ -319,8 +319,9 @@ end: ret = 200; break; case TS_STATUS_GRANTED_WITH_MODS: - uts_logger(ct, LOG_NOTICE, "timestamp request granted with " - "modification", + uts_logger(ct, LOG_NOTICE, + "timestamp request granted with " + "modification", *serial_id); ret = 200; break; diff --git a/src/lib/utils.c b/src/lib/utils.c index 4adb262..90e06cb 100644 --- a/src/lib/utils.c +++ b/src/lib/utils.c @@ -8,7 +8,6 @@ #include #include #include -#include #include #include #include @@ -162,7 +161,7 @@ void uts_logger(rfc3161_context *ct, int priority, char *fmt, ...) { fclose(stream); // if in debugging mode, also log to stdout - if (ct->stdout_dbg) { + if (ct->stdout_logging || ct->stdout_dbg) { switch (priority) { case LOG_EMERG: printf("LOG_EMER : %s\n", out); @@ -197,7 +196,8 @@ void uts_logger(rfc3161_context *ct, int priority, char *fmt, ...) { ; } } - syslog(priority, "%s", out); + if (ct->syslog_logging) + syslog(priority, "%s", out); free(out); } @@ -301,6 +301,21 @@ int set_params(rfc3161_context *ct, char *conf_file, char *conf_wd) { } break; ; + case LOGHANDLER_OPTIONS: + if (strcmp(name, "log_to_syslog") == 0) { + if (strcmp(value, "yes")) + ct->syslog_logging = 0; + else + ct->syslog_logging = 1; + } + if (strcmp(name, "log_to_stdout") == 0) { + if (strcmp(value, "yes")) + ct->stdout_logging = 0; + else + ct->stdout_logging = 1; + } + break; + ; } } // parse the options to get the civetweb options and a few other things @@ -318,8 +333,8 @@ int set_params(rfc3161_context *ct, char *conf_file, char *conf_wd) { uts_logger(ct, LOG_DEBUG, "configuration param['%s'] = '%s'", name, null_undef(value)); switch (type) { - // if it's an http (civetweb) option, put it in the http_options buffer - // like civetweb is expected it. + // if it's an http (civetweb) option, put it in the http_options + // buffer like civetweb is expected it. case HTTP_OPTIONS: if (value != NULL) { ct->http_options[http_counter] = name; From 0e0b11310dc855bed53af9d09f7669c1667b76fe Mon Sep 17 00:00:00 2001 From: kakwa Date: Wed, 10 Apr 2019 23:27:36 +0200 Subject: [PATCH 10/21] add new configuration file parameters in test conf --- tests/cfg/uts-server-ssl.cnf | 6 ++++++ tests/cfg/uts-server.cnf | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/tests/cfg/uts-server-ssl.cnf b/tests/cfg/uts-server-ssl.cnf index 9c145cb..448f158 100644 --- a/tests/cfg/uts-server-ssl.cnf +++ b/tests/cfg/uts-server-ssl.cnf @@ -78,6 +78,12 @@ tcp_nodelay = 0 # debug, info, notice, warn, err, emerg, crit log_level = info +# Enable logging to syslog (default: yes) +log_to_syslog = no + +# Enable logging to stdout (default: no) +#log_to_stdout = yes + #################################################################### [ tsa ] diff --git a/tests/cfg/uts-server.cnf b/tests/cfg/uts-server.cnf index df9451a..7522b78 100644 --- a/tests/cfg/uts-server.cnf +++ b/tests/cfg/uts-server.cnf @@ -78,6 +78,12 @@ tcp_nodelay = 0 # debug, info, notice, warn, err, emerg, crit log_level = info +# Enable logging to syslog (default: yes) +log_to_syslog = no + +# Enable logging to stdout (default: no) +#log_to_stdout = yes + #################################################################### [ tsa ] From 8fefcbaf6b400b9972990e66af758744c972711a Mon Sep 17 00:00:00 2001 From: kakwa Date: Thu, 11 Apr 2019 00:08:52 +0200 Subject: [PATCH 11/21] set signal handler also when running in foreground --- inc/utils.h | 1 + src/cmd/uts-server.c | 2 ++ src/lib/utils.c | 20 ++++++++++++-------- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/inc/utils.h b/inc/utils.h index 8c45696..a18e6bb 100644 --- a/inc/utils.h +++ b/inc/utils.h @@ -7,6 +7,7 @@ typedef struct _code { static void signal_handler_general(int sig_num); static void signal_handler_up(int sig_num); +void set_sig_handler(); void skeleton_daemon(); int init_pid(char *pidfile_path); int write_pid(char *pidfile_path); diff --git a/src/cmd/uts-server.c b/src/cmd/uts-server.c index 7d5db70..7e75c24 100644 --- a/src/cmd/uts-server.c +++ b/src/cmd/uts-server.c @@ -101,6 +101,8 @@ int main(int argc, char **argv) { if (args.daemonize) skeleton_daemon(); + else + set_sig_handler(); syslog(LOG_NOTICE, "uts-server daemon starting with conf '%s' from working dir '%s'", diff --git a/src/lib/utils.c b/src/lib/utils.c index 90e06cb..2657894 100644 --- a/src/lib/utils.c +++ b/src/lib/utils.c @@ -67,6 +67,17 @@ int write_pid(char *pidfile_path) { return 1; } +void set_sig_handler() { + g_uts_sig_up = 0; + g_uts_sig = 0; + /* Catch, ignore and handle signals */ + // TODO: Implement a working signal handler */ + signal(SIGTERM, signal_handler_general); + signal(SIGINT, signal_handler_general); + signal(SIGHUP, signal_handler_up); + signal(SIGCHLD, SIG_IGN); +} + void skeleton_daemon() { pid_t pid; @@ -85,14 +96,7 @@ void skeleton_daemon() { if (setsid() < 0) exit(EXIT_FAILURE); - g_uts_sig_up = 0; - g_uts_sig = 0; - /* Catch, ignore and handle signals */ - // TODO: Implement a working signal handler */ - signal(SIGTERM, signal_handler_general); - signal(SIGINT, signal_handler_general); - signal(SIGHUP, signal_handler_up); - signal(SIGCHLD, SIG_IGN); + set_sig_handler(); /* Fork off for the second time*/ pid = fork(); From cab605597b603103be426ae14ce2becfa13bf4fb Mon Sep 17 00:00:00 2001 From: kakwa Date: Thu, 11 Apr 2019 01:02:46 +0200 Subject: [PATCH 12/21] sleep unit tests (hopefully --- tests/external_test.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/external_test.sh b/tests/external_test.sh index 2d77617..0f273be 100755 --- a/tests/external_test.sh +++ b/tests/external_test.sh @@ -32,6 +32,8 @@ kill `cat ./uts-server.pid` sed "s/2020/$PORT/" tests/cfg/uts-server-ssl.cnf >$CFG +sleep 1 + $TO ./uts-server -c $CFG -D -p ./uts-server.pid & sleep 1 From 68ffb0f7e827abbaf3306491a6034034455c41cb Mon Sep 17 00:00:00 2001 From: kakwa Date: Thu, 11 Apr 2019 01:10:45 +0200 Subject: [PATCH 13/21] disable more testing when bundleling hopefully, this should fix OpenBSD and FreeBSD CI --- CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index f001f15..3fd55e9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -127,6 +127,7 @@ if(BUNDLE_CIVETWEB) -DCIVETWEB_ENABLE_IPV6=ON -DCIVETWEB_DISABLE_CACHING=ON -DBUILD_TESTING=OFF + -DCIVETWEB_BUILD_TESTING=OFF -DCIVETWEB_SSL_OPENSSL_API_1_1=${OPENSSL_API_1_1} -DOPENSSL_USE_STATIC_LIBS=${OPENSSL_USE_STATIC_LIBS} -DCIVETWEB_ENABLE_SSL_DYNAMIC_LOADING=ON From bd0a32221c250a91b5b551f97e386a0d9cbaf957 Mon Sep 17 00:00:00 2001 From: kakwa Date: Fri, 12 Apr 2019 10:48:55 +0200 Subject: [PATCH 14/21] creating a nicer landing page + serve the ca file * nicer landing page with a few instructions, and download link for the ca, and even some fancy CSS * add a download link for the CA file --- goodies/index.html | 96 ++++++++++++++++++++++++++++++++++++++ inc/context.h | 5 ++ inc/http.h | 114 +++++++++++++++++++++++++++++++++++++++++++++ inc/rfc3161.h | 4 -- src/lib/http.c | 56 +++++++++++++--------- src/lib/utils.c | 4 ++ 6 files changed, 252 insertions(+), 27 deletions(-) create mode 100644 goodies/index.html diff --git a/goodies/index.html b/goodies/index.html new file mode 100644 index 0000000..fef0ab8 --- /dev/null +++ b/goodies/index.html @@ -0,0 +1,96 @@ + + + + + + + + + +
+ uts-server, a simple RFC 3161 timestamp server +
+
+ For timestamping a file with OpenSSL and curl, run the following commands + (setting the $UTS_SERVER_URL, $FILE and $FILE_TIMESTAMP variables): +
+ openssl ts -query -data "$FILE" -out "ts_req.ts";
+ curl "$UTS_SERVER_URL" -H "Content-Type: application/timestamp-query" \
+ -f -g --data-binary "@ts_req.ts" -o "$FILE_TIMESTAMP" +
+ For verifying the timestamp with OpenSSL, download the CA, and run the following command: +
+ openssl ts -verify -in "$FILE_TIMESTAMP" -data "$FILE" -CAfile ca.pem +
+ +
+ + + diff --git a/inc/context.h b/inc/context.h index ff88fa1..aee62c0 100644 --- a/inc/context.h +++ b/inc/context.h @@ -12,7 +12,11 @@ #define LOGHANDLER_OPTIONS 3 #define TSA_OPTIONS 4 #define PATH_HTTP_OPTIONS 5 + +/* name of the configuration file section */ #define MAIN_CONF_SECTION "main" +#define OID_SECTION "oids" +#define TSA_SECTION "tsa" #define RFC3161_OPTIONS_LEN \ sizeof(rfc3161_options) / sizeof(struct rfc3161_option) @@ -42,6 +46,7 @@ typedef struct { // just to track for freeing later CONF *conf; char *cust_conf[20]; + char *ca_file; } rfc3161_context; // definition of structure to describe diff --git a/inc/http.h b/inc/http.h index e89e902..4221f35 100644 --- a/inc/http.h +++ b/inc/http.h @@ -6,3 +6,117 @@ struct tuser_data { }; int http_server_start(char *conffile, char *conf_wd, bool stdout_dbg); + +#define STATIC_PAGE \ + "HTTP/1.1 200 OK\r\n" \ + "Content-Type: text/html\r\n" \ + "Content-Length: 2509\r\n" \ + "\r\n" \ + "" \ + "" \ + " " \ + " " \ + " " \ + " " \ + "" \ + "" \ + "" \ + "
" \ + " uts-server, a simple RFC 3161 timestamp server" \ + "
" \ + "
" \ + " For timestamping a file with OpenSSL and curl, run the following " \ + "commands" \ + " (setting the $UTS_SERVER_URL, $FILE and $FILE_TIMESTAMP variables):" \ + "
" \ + " openssl ts -query -data \"$FILE\" -out " \ + "\"ts_req.ts\";
" \ + " curl \"$UTS_SERVER_URL\" -H " \ + "\"Content-Type: application/timestamp-query\" \\
" \ + " -f -g --data-binary \"@ts_req.ts\" -o \"$FILE_TIMESTAMP\"" \ + "
" \ + " For verifying the timestamp with OpenSSL, download the CA, and run " \ + "the following command:" \ + "
" \ + " openssl ts -verify -in \"$FILE_TIMESTAMP\" -data \"$FILE\" -CAfile ca.pem" \ + "
" \ + "
" \ + " " \ + "
" \ + "
" \ + "
" \ + "
" \ + " uts-server" \ + " • © 2019 • Pierre-François Carpentier • Released under the MIT " \ + "License" \ + "
" \ + "
" \ + "" \ + "" diff --git a/inc/rfc3161.h b/inc/rfc3161.h index 0f88494..93f6984 100644 --- a/inc/rfc3161.h +++ b/inc/rfc3161.h @@ -39,10 +39,6 @@ #endif #endif -/* Name of config entry that defines the OID file. */ -#define OID_SECTION "oids" -#define TSA_SECTION "tsa" - // number of char we get to log for the serial #define SERIAL_ID_SIZE 8 diff --git a/src/lib/http.c b/src/lib/http.c index 9ab3c18..88786bf 100644 --- a/src/lib/http.c +++ b/src/lib/http.c @@ -102,22 +102,6 @@ void log_request(const struct mg_request_info *request_info, char *request_id, null_undef(content_type)); } -// This function will be called by civetweb on every new request. -static int begin_request_handler(struct mg_connection *conn) { - const struct mg_request_info *request_info = mg_get_request_info(conn); - - mg_printf(conn, - "HTTP/1.1 200 OK\r\n" - "Content-Type: text/plain\r\n" - "Content-Length: 46\r\n" // Always set Content-Length - "\r\n" - "uts-server, a simple RFC 3161 timestamp server"); - - // Returning non-zero tells civetweb that our function has replied to - // the client, and civetweb should not send client any more data. - return 1; -} - int rfc3161_handler(struct mg_connection *conn, void *context) { // some timer stuff clock_t start = clock(), diff; @@ -208,11 +192,7 @@ int rfc3161_handler(struct mg_connection *conn, void *context) { } else { // default reply if we don't have a time-stamp request resp_code = 200; - mg_printf(conn, "HTTP/1.1 200 OK\r\n" - "Content-Type: text/plain\r\n" - "Content-Length: 46\r\n" - "\r\n" - "uts-server, a simple RFC 3161 timestamp server"); + mg_printf(conn, STATIC_PAGE); } // initialize a serial_id if not created by create_response if (serial_id == NULL) { @@ -231,6 +211,37 @@ int rfc3161_handler(struct mg_connection *conn, void *context) { return 1; } +int ca_serve_handler(struct mg_connection *conn, void *context) { + /* In this handler, we ignore the req_info and send the file "filename". */ + const struct mg_request_info *request_info = mg_get_request_info(conn); + clock_t start = clock(), diff; + rfc3161_context *ct = (rfc3161_context *)context; + const char *filename = ct->ca_file; + if (strlen(filename) == 0){ + uts_logger(context, LOG_NOTICE, "'certs' param in '[ tsa ]' section not filed"); + mg_send_http_error(conn, 404, "CA file not available"); + diff = clock() - start; + log_request(request_info, "CA_DL ", ct, 404, + (diff * 1000000 / CLOCKS_PER_SEC)); + return 1; + } + if (access(filename, F_OK) != -1) { + mg_send_file(conn, filename); + const struct mg_response_info *ri = mg_get_response_info(conn); + diff = clock() - start; + log_request(request_info, "CA_DL ", ct, 200, + (diff * 1000000 / CLOCKS_PER_SEC)); + + } else { + uts_logger(context, LOG_NOTICE, "CA file '%s' not available", filename); + mg_send_http_error(conn, 404, "CA file not available"); + diff = clock() - start; + log_request(request_info, "CA_DL ", ct, 404, + (diff * 1000000 / CLOCKS_PER_SEC)); + } + return 1; +} + int http_server_start(char *conffile, char *conf_wd, bool stdout_dbg) { struct mg_context *ctx; struct mg_callbacks callbacks; @@ -250,7 +261,6 @@ int http_server_start(char *conffile, char *conf_wd, bool stdout_dbg) { // Prepare callbacks structure. We have only one callback, the rest are // NULL. memset(&callbacks, 0, sizeof(callbacks)); - memset(&user_data, 0, sizeof(user_data)); callbacks.log_message = &log_civetweb; @@ -258,12 +268,12 @@ int http_server_start(char *conffile, char *conf_wd, bool stdout_dbg) { ctx = mg_start(&callbacks, &user_data, ct->http_options); if (ctx != NULL) { mg_set_request_handler(ctx, "/", rfc3161_handler, (void *)ct); + mg_set_request_handler(ctx, "/ca.pem", ca_serve_handler, (void *)ct); // Wait until some signals are received while (g_uts_sig == 0) { sleep(1); } - // getchar(); } else { uts_logger(ct, LOG_ERR, "Failed to start uts-server: %s", ((user_data.first_message == NULL) diff --git a/src/lib/utils.c b/src/lib/utils.c index 2657894..d7c5c93 100644 --- a/src/lib/utils.c +++ b/src/lib/utils.c @@ -387,6 +387,9 @@ int set_params(rfc3161_context *ct, char *conf_file, char *conf_wd) { break; } } + + ct->ca_file = calloc(PATH_MAX, sizeof(char)); + realpath(NCONF_get_string(ct->conf, TSA_SECTION, "certs"), ct->ca_file); // like any good daemon, return to '/' once the configuration is loaded chdir("/"); return ret; @@ -405,6 +408,7 @@ void free_uts_context(rfc3161_context *ct) { free(ct->cust_conf[i]); } free(ct->ts_ctx_pool); + free(ct->ca_file); NCONF_free(ct->conf); free(ct); } From dd19915c91e753d660b780d0f6161f76be2968c3 Mon Sep 17 00:00:00 2001 From: Pierre-Francois Carpentier Date: Fri, 12 Apr 2019 14:26:21 +0200 Subject: [PATCH 15/21] dl for the signer cert + fix example + css tweaks * add a DL button + serve the signer certificate file (the one used to timestamp) * fix the verification instruction (add -untrusted tsa_cert.pem) * few CSS tweaks to improve page layout --- goodies/index.html | 43 ++++++++++++++++++++--------------- inc/context.h | 1 + inc/http.h | 56 ++++++++++++++++++++++++++-------------------- src/lib/http.c | 42 +++++++++++++++++++++++++++++++--- src/lib/utils.c | 5 +++++ 5 files changed, 102 insertions(+), 45 deletions(-) diff --git a/goodies/index.html b/goodies/index.html index fef0ab8..d80fa52 100644 --- a/goodies/index.html +++ b/goodies/index.html @@ -1,7 +1,7 @@ - + uts-server @@ -71,20 +75,23 @@ body { uts-server, a simple RFC 3161 timestamp server
- For timestamping a file with OpenSSL and curl, run the following commands - (setting the $UTS_SERVER_URL, $FILE and $FILE_TIMESTAMP variables): -
- openssl ts -query -data "$FILE" -out "ts_req.ts";
- curl "$UTS_SERVER_URL" -H "Content-Type: application/timestamp-query" \
- -f -g --data-binary "@ts_req.ts" -o "$FILE_TIMESTAMP" -
- For verifying the timestamp with OpenSSL, download the CA, and run the following command: -
- openssl ts -verify -in "$FILE_TIMESTAMP" -data "$FILE" -CAfile ca.pem -
- + For timestamping a file with OpenSSL and curl, run the following commands + (setting the $UTS_SERVER_URL, $FILE and $FILE_TIMESTAMP variables): +
+ openssl ts -query -data "$FILE" -out "ts_req.ts";
+ curl "$UTS_SERVER_URL" \
+      -H "Content-Type: application/timestamp-query" \
+      -f -g --data-binary "@ts_req.ts" -o "$FILE_TIMESTAMP" +
+ For verifying the timestamp with OpenSSL, download the CA and the signer cert, and run the following command: +
+ openssl ts -verify -in "$FILE_TIMESTAMP" \
+      -data "$FILE" -CAfile ca.pem -untrusted tsa_cert.pem +
+