From 54378a355c31bf2e05fa90100ea3df6fc3ad56a1 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Mon, 8 Jan 2018 13:30:44 +0100 Subject: initial commit --- .gitignore | 7 +++++++ Makefile | 48 ++++++++++++++++++++++++++++++++++++++++++++ README.md | 41 ++++++++++++++++++++++++++++++++++++++ udp514-journal.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++ udp514-journal.h | 33 ++++++++++++++++++++++++++++++ udp514-journal.service | 15 ++++++++++++++ 6 files changed, 198 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 README.md create mode 100644 udp514-journal.c create mode 100644 udp514-journal.h create mode 100644 udp514-journal.service diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b1df16f --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +*~ +*.o +udp514-journal +README.html +version.h +udp514-journal-*.tar.xz +udp514-journal-*.tar.xz.asc diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e81a83c --- /dev/null +++ b/Makefile @@ -0,0 +1,48 @@ +# udp514-journal - forward syslog from network (udp/514) to journal + +# commands +CC := gcc +MD := markdown +INSTALL := install +RM := rm +CP := cp + +# flags +CFLAGS += -std=c11 -O2 -fPIC -Wall -Werror +CFLAGS += $(shell pkg-config --cflags --libs libsystemd 2>/dev/null) +LDFLAGS += -Wl,-z,now -Wl,-z,relro -pie + +# this is just a fallback in case you do not use git but downloaded +# a release tarball... +VERSION := 0.0.1 + +all: udp514-journal README.html + +version.h: $(wildcard .git/HEAD .git/index .git/refs/tags/*) Makefile + printf "#ifndef VERSION\n#define VERSION \"%s\"\n#endif\n" $(shell git describe --long 2>/dev/null || echo ${VERSION}) > $@ + +udp514-journal: udp514-journal.c udp514-journal.h version.h + $(CC) $(CFLAGS) $(LDFLAGS) -o udp514-journal udp514-journal.c + +README.html: README.md + $(MD) README.md > README.html + +install: install-bin install-doc + +install-bin: udp514-journal + $(INSTALL) -D -m0755 udp514-journal $(DESTDIR)/usr/bin/udp514-journal + $(INSTALL) -D -m0644 udp514-journal.service $(DESTDIR)/usr/lib/systemd/system/udp514-journal.service + +install-doc: README.html + $(INSTALL) -D -m0644 README.md $(DESTDIR)/usr/share/doc/udp514-journal/README.md + $(INSTALL) -D -m0644 README.html $(DESTDIR)/usr/share/doc/udp514-journal/README.html + +clean: + $(RM) -f *.o *~ udp514-journal README.html version.h + +distclean: + $(RM) -f *.o *~ udp514-journal README.html version.h config.h + +release: + git archive --format=tar.xz --prefix=udp514-journal-$(VERSION)/ $(VERSION) > udp514-journal-$(VERSION).tar.xz + gpg -ab udp514-journal-$(VERSION).tar.xz diff --git a/README.md b/README.md new file mode 100644 index 0000000..25fa3ee --- /dev/null +++ b/README.md @@ -0,0 +1,41 @@ +udp514-journal +============== + +forward syslog from network (udp/514) to systemd-journald + +Requirements +------------ + +To compile and run `udp514-journal` you need: + +* [systemd](https://www.github.com/systemd/systemd) +* [markdown](http://daringfireball.net/projects/markdown/) (HTML documentation) + +Build and install +----------------- + +Building and installing is very easy. Just run: + +> make + +followed by: + +> make install + +This will place an executable at `/usr/bin/udp514-journal`, +documentation can be found in `/usr/share/doc/udp514-journal/`. +Additionally a systemd unit file is installed to +`/usr/lib/systemd/system/udp514-journal.service`. + +Usage +----- + +Just run `udp514-journal` or start a systemd unit with +`systemctl start udp514-journal`. + +Make sure UDP port 514 is not blocked in your firewall. + +### Upstream + +URL: [GitHub.com](https://github.com/eworm-de/udp514-journal) +Mirror: [eworm.de](https://git.eworm.de/cgit.cgi/udp514-journal/) diff --git a/udp514-journal.c b/udp514-journal.c new file mode 100644 index 0000000..005bb0d --- /dev/null +++ b/udp514-journal.c @@ -0,0 +1,54 @@ +/* + * (C) 2018 by Christian Hesse + * + * This software may be used and distributed according to the terms + * of the GNU General Public License, incorporated herein by reference. + */ + +#include "udp514-journal.h" + +int main(int argc, char **argv) { + int s, rc, n; + socklen_t len; + struct sockaddr_in cliAddr, servAddr; + char buffer[BUFFER_SIZE]; + const int y = 1; + + /* Socket erzeugen */ + if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { + perror("could not open socket"); + return EXIT_FAILURE; + } + + /* Lokalen Server Port bind(en) */ + servAddr.sin_family = AF_INET; + servAddr.sin_addr.s_addr = htonl(INADDR_ANY); + servAddr.sin_port = htons(LOCAL_SERVER_PORT); + setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &y, sizeof(int)); + if ((rc = bind(s, (struct sockaddr *) &servAddr, sizeof(servAddr))) < 0) { + perror("could not bind on port " LOCAL_SERVER_PORT_STR); + return EXIT_FAILURE; + } + + /* tell systemd we are ready to go... */ + sd_notify(0, "READY=1\nSTATUS=Listening for syslog input..."); + + /* server loop */ + while (1) { + memset(buffer, 0, BUFFER_SIZE); + len = sizeof(cliAddr); + n = recvfrom(s, buffer, BUFFER_SIZE, 0, + (struct sockaddr *) &cliAddr, &len); + if (n < 0) { + perror("could not receive data"); + continue; + } + + /* send to systemd-journald */ + sd_journal_send("MESSAGE=%s: %s", inet_ntoa(cliAddr.sin_addr), buffer, + "IP_ADDRESS=%s", inet_ntoa(cliAddr.sin_addr), + NULL); + } + + return EXIT_SUCCESS; +} diff --git a/udp514-journal.h b/udp514-journal.h new file mode 100644 index 0000000..2767e1c --- /dev/null +++ b/udp514-journal.h @@ -0,0 +1,33 @@ +/* + * (C) 2018 by Christian Hesse + * + * This software may be used and distributed according to the terms + * of the GNU General Public License, incorporated herein by reference. + */ + +#ifndef _UDP514_JOURNAL_H +#define _UDP514_JOURNAL_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#define STR_(x) #x +#define STR(x) STR_(x) + +#define LOCAL_SERVER_PORT 514 +#define LOCAL_SERVER_PORT_STR STR(LOCAL_SERVER_PORT) +#define BUFFER_SIZE 1024 + +#endif /* _UDP514_JOURNAL_H */ diff --git a/udp514-journal.service b/udp514-journal.service new file mode 100644 index 0000000..5ae11f8 --- /dev/null +++ b/udp514-journal.service @@ -0,0 +1,15 @@ +[Unit] +Description=Forward syslog from network (udp/514) to journal +Requires=systemd-journald.socket +After=network.target + +[Service] +Type=notify +ExecStart=/usr/bin/udp514-journal +ProtectSystem=full +ProtectHome=on +PrivateDevices=on +NoNewPrivileges=on + +[Install] +WantedBy=multi-user.target -- cgit v1.2.3-54-g00ecf