From 8b469e9a2fd4d3211c25e054e1f192f270ccf65c Mon Sep 17 00:00:00 2001
From: Charles Duffy <charles@threatgrid.com>
Date: Thu, 12 Jun 2014 21:20:47 -0500
Subject: [PATCH] Allow systemd-journal-remote to be built without microhttpd
 support

---
 Makefile.am                  |  6 ++++-
 src/journal/journal-remote.c | 61 +++++++++++++++++++++++++++++++++++++++-----
 2 files changed, 60 insertions(+), 7 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index 685066f..abaad68 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -3233,7 +3233,6 @@ systemd_cat_SOURCES = \
 systemd_cat_LDADD = \
 	libsystemd-journal-core.la
 
-if HAVE_MICROHTTPD
 rootlibexec_PROGRAMS += \
 	systemd-journal-remote
 
@@ -3248,6 +3247,7 @@ systemd_journal_remote_LDADD = \
 	libsystemd-internal.la \
 	libsystemd-journal-core.la
 
+if HAVE_MICROHTTPD
 systemd_journal_remote_SOURCES += \
 	src/journal/microhttpd-util.h \
 	src/journal/microhttpd-util.c
@@ -3263,6 +3263,10 @@ if HAVE_GNUTLS
 systemd_journal_remote_LDADD += \
 	$(GNUTLS_LIBS)
 endif
+
+else
+systemd_journal_remote_CFLAGS = \
+	$(AM_CFLAGS)
 endif
 
 # using _CFLAGS = in the conditional below would suppress AM_CFLAGS
diff --git a/src/journal/journal-remote.c b/src/journal/journal-remote.c
index 915f234..5c1555d 100644
--- a/src/journal/journal-remote.c
+++ b/src/journal/journal-remote.c
@@ -42,11 +42,13 @@
 #include "macro.h"
 #include "strv.h"
 #include "fileio.h"
-#include "microhttpd-util.h"
 
+#ifdef HAVE_MICROHTTPD
+#include "microhttpd-util.h"
 #ifdef HAVE_GNUTLS
 #include <gnutls/gnutls.h>
 #endif
+#endif
 
 #include "journal-remote-parse.h"
 #include "journal-remote-write.h"
@@ -64,9 +66,11 @@ static int arg_compress = true;
 static int arg_seal = false;
 static int http_socket = -1, https_socket = -1;
 
+#ifdef HAVE_MICROHTTPD
 static char *key_pem = NULL;
 static char *cert_pem = NULL;
 static char *trust_pem = NULL;
+#endif
 
 /**********************************************************************
  **********************************************************************
@@ -247,10 +251,12 @@ static int dispatch_raw_connection_event(sd_event_source *event,
                                          int fd,
                                          uint32_t revents,
                                          void *userdata);
+#if defined(HAVE_MICROHTTPD) && defined(HAVE_GNUTLS)
 static int dispatch_http_event(sd_event_source *event,
                                int fd,
                                uint32_t revents,
                                void *userdata);
+#endif
 
 static int get_source_for_fd(RemoteServer *s, int fd, RemoteSource **source) {
         assert(fd >= 0);
@@ -361,6 +367,7 @@ static int setup_raw_socket(RemoteServer *s, const char *address) {
  **********************************************************************
  **********************************************************************/
 
+#ifdef HAVE_MICROHTTPD
 static RemoteSource *request_meta(void **connection_cls) {
         RemoteSource *source;
 
@@ -378,11 +385,14 @@ static RemoteSource *request_meta(void **connection_cls) {
         *connection_cls = source;
         return source;
 }
+#endif
 
+#ifdef HAVE_MICROHTTPD
 static void request_meta_free(void *cls,
-                              struct MHD_Connection *connection,
                               void **connection_cls,
-                              enum MHD_RequestTerminationCode toe) {
+                              struct MHD_Connection *connection,
+                              enum MHD_RequestTerminationCode toe
+) {
         RemoteSource *s;
 
         assert(connection_cls);
@@ -392,7 +402,9 @@ static void request_meta_free(void *cls,
         source_free(s);
         *connection_cls = NULL;
 }
+#endif
 
+#ifdef HAVE_MICROHTTPD
 static int process_http_upload(
                 struct MHD_Connection *connection,
                 const char *upload_data,
@@ -446,7 +458,9 @@ static int process_http_upload(
 
         return mhd_respond(connection, MHD_HTTP_ACCEPTED, "OK.\n");
 };
+#endif
 
+#ifdef HAVE_MICROHTTPD
 static int request_handler(
                 void *cls,
                 struct MHD_Connection *connection,
@@ -497,7 +511,9 @@ static int request_handler(
                 return respond_oom(connection);
         return MHD_YES;
 }
+#endif
 
+#ifdef HAVE_MICROHTTPD
 static int setup_microhttpd_server(RemoteServer *s, int fd, bool https) {
         struct MHD_OptionItem opts[] = {
                 { MHD_OPTION_NOTIFY_COMPLETED, (intptr_t) request_meta_free},
@@ -602,7 +618,9 @@ error:
         free(d);
         return r;
 }
+#endif
 
+#ifdef HAVE_MICROHTTPD
 static int setup_microhttpd_socket(RemoteServer *s,
                                    const char *address,
                                    bool https) {
@@ -614,7 +632,9 @@ static int setup_microhttpd_socket(RemoteServer *s,
 
         return setup_microhttpd_server(s, fd, https);
 }
+#endif
 
+#ifdef HAVE_MICROHTTPD
 static int dispatch_http_event(sd_event_source *event,
                                int fd,
                                uint32_t revents,
@@ -635,6 +655,7 @@ static int dispatch_http_event(sd_event_source *event,
 
         return 1; /* work to do */
 }
+#endif
 
 /**********************************************************************
  **********************************************************************
@@ -719,11 +740,13 @@ static int remoteserver_init(RemoteServer *s) {
                 if (sd_is_socket(fd, AF_UNSPEC, 0, false)) {
                         log_info("Received a listening socket (fd:%d)", fd);
 
+#ifdef HAVE_MICROHTTPD
                         if (fd == http_socket)
                                 r = setup_microhttpd_server(s, fd, false);
                         else if (fd == https_socket)
                                 r = setup_microhttpd_server(s, fd, true);
                         else
+#endif
                                 r = add_raw_socket(s, fd);
                 } else if (sd_is_socket(fd, AF_UNSPEC, 0, true)) {
                         log_info("Received a connection socket (fd:%d)", fd);
@@ -779,6 +802,7 @@ static int remoteserver_init(RemoteServer *s) {
                 output_name = arg_listen_raw;
         }
 
+#ifdef HAVE_MICROHTTPD
         if (arg_listen_http) {
                 r = setup_microhttpd_socket(s, arg_listen_http, false);
                 if (r < 0)
@@ -794,6 +818,12 @@ static int remoteserver_init(RemoteServer *s) {
 
                 output_name = arg_listen_https;
         }
+#else
+        if (arg_listen_http || arg_listen_https) {
+            log_error("HTTP requested but not compiled");
+            return -EINVAL;
+        }
+#endif
 
         STRV_FOREACH(file, arg_files) {
                 if (streq(*file, "-")) {
@@ -833,6 +863,7 @@ static int remoteserver_init(RemoteServer *s) {
         return r;
 }
 
+#ifdef HAVE_MICROHTTPD
 static int server_destroy(RemoteServer *s) {
         int r;
         size_t i;
@@ -863,6 +894,7 @@ static int server_destroy(RemoteServer *s) {
 
         return r;
 }
+#endif
 
 /**********************************************************************
  **********************************************************************
@@ -964,8 +996,10 @@ static int help(void) {
                "  --url=URL            Read events from systemd-journal-gatewayd at URL\n"
                "  --getter=COMMAND     Read events from the output of COMMAND\n"
                "  --listen-raw=ADDR    Listen for connections at ADDR\n"
+#ifdef HAVE_MICROHTTPD
                "  --listen-http=ADDR   Listen for HTTP connections at ADDR\n"
                "  --listen-https=ADDR  Listen for HTTPS connections at ADDR\n"
+#endif
                "  -o --output=FILE|DIR Write output to FILE or DIR/external-*.journal\n"
                "  --[no-]compress      Use XZ-compression in the output journal (default: yes)\n"
                "  --[no-]seal          Use Event sealing in the output journal (default: no)\n"
@@ -1096,6 +1130,7 @@ static int parse_argv(int argc, char *argv[]) {
                         break;
 
                 case ARG_KEY:
+#if defined(HAVE_MICROHTTPD) && defined(HAVE_GNUTLS)
                         if (key_pem) {
                                 log_error("Key file specified twice");
                                 return -EINVAL;
@@ -1107,8 +1142,13 @@ static int parse_argv(int argc, char *argv[]) {
                         }
                         assert(key_pem);
                         break;
+#else
+                        log_error("Option --key is not available.");
+                        return -EINVAL;
+#endif
 
                 case ARG_CERT:
+#if defined(HAVE_MICROHTTPD) && defined(HAVE_GNUTLS)
                         if (cert_pem) {
                                 log_error("Certificate file specified twice");
                                 return -EINVAL;
@@ -1120,9 +1160,13 @@ static int parse_argv(int argc, char *argv[]) {
                         }
                         assert(cert_pem);
                         break;
+#else
+                        log_error("Option --cert is not available.");
+                        return -EINVAL;
+#endif
 
                 case ARG_TRUST:
-#ifdef HAVE_GNUTLS
+#if defined(HAVE_MICROHTTPD) && defined(HAVE_GNUTLS)
                         if (trust_pem) {
                                 log_error("CA certificate file specified twice");
                                 return -EINVAL;
@@ -1136,6 +1180,7 @@ static int parse_argv(int argc, char *argv[]) {
                         break;
 #else
                         log_error("Option --trust is not available.");
+                        return -EINVAL;
 #endif
 
                 case 'o':
@@ -1168,10 +1213,12 @@ static int parse_argv(int argc, char *argv[]) {
                         return -EINVAL;
                 }
 
+#if defined(HAVE_MICROHTTPD) && defined(HAVE_GNUTLS)
         if (arg_listen_https && !(key_pem && cert_pem)) {
                 log_error("Options --key and --cert must be used when https sources are specified");
                 return -EINVAL;
         }
+#endif
 
         if (optind < argc)
                 arg_files = argv + optind;
@@ -1183,7 +1230,7 @@ static int setup_gnutls_logger(void) {
         if (!arg_listen_http && !arg_listen_https)
                 return 0;
 
-#ifdef HAVE_GNUTLS
+#if defined(HAVE_MICROHTTPD) && defined(HAVE_GNUTLS)
         gnutls_global_set_log_function(log_func_gnutls);
         gnutls_global_set_log_level(GNUTLS_LOG_LEVEL);
 #endif
@@ -1193,7 +1240,7 @@ static int setup_gnutls_logger(void) {
 
 int main(int argc, char **argv) {
         RemoteServer s = {};
-        int r, r2;
+        int r, r2 = 0;
 
         log_set_max_level(LOG_DEBUG);
         log_show_color(true);
@@ -1231,7 +1278,9 @@ int main(int argc, char **argv) {
         }
 
         log_info("Finishing after writing %" PRIu64 " entries", s.writer.seqnum);
+#ifdef HAVE_MICROHTTPD
         r2 = server_destroy(&s);
+#endif
 
         sd_notify(false, "STATUS=Shutting down...");
 
-- 
2.0.0

