From ca2637e4ddd64a85d7e0300b27b3765906265d7f Mon Sep 17 00:00:00 2001 From: "Alex Xu (Hello71)" Date: Wed, 8 Aug 2018 17:19:34 -0400 Subject: Fix stuff. --- .gitignore | 14 ++++++++------ Makefile.in | 26 ++++++++++++++++---------- README | 18 +++++++++++++++--- autogen.sh | 1 + configure.ac | 28 ++++++++++++++++++++++------ random-seed.c | 36 +++++++++++------------------------- util.c | 4 +++- 7 files changed, 76 insertions(+), 51 deletions(-) diff --git a/.gitignore b/.gitignore index 0734561..069ca76 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,11 @@ -/aclocal.m4 -/autom4te.cache -/config.h.in -/config.log -/config.status -/configure +aclocal.m4 +autom4te.cache +config.h +config.h.in +config.h.in~ +config.log +config.status +configure /Makefile /random-seed diff --git a/Makefile.in b/Makefile.in index 45593d3..ae356c6 100644 --- a/Makefile.in +++ b/Makefile.in @@ -1,19 +1,25 @@ # @configure_input@ +VPATH = @abs_srcdir@ +abs_srcdir = @abs_srcdir@ + +prefix = @prefix@ +exec_prefix = @exec_prefix@ datarootdir = @datarootdir@ sbindir = @sbindir@ sysconfdir = @sysconfdir@ localstatedir = @localstatedir@ mandir = @mandir@ -CPPFLAGS = @CPPFLAGS@ -DSYSCONFDIR='"$(sysconfdir)"' -DLOCALSTATEDIR='"$(localstatedir)"' -CFLAGS = -Wall -Wextra -pedantic @SYSTEMD_CFLAGS@ @CFLAGS@ -MD -MP -UNDEBUG -include @abs_top_builddir@/config.h +systemdsystemunitdir = @systemdsystemunitdir@ + +CPPFLAGS = @CPPFLAGS@ +CFLAGS = -Wall -Wextra -pedantic @CFLAGS@ -MD -MP -UNDEBUG -include @abs_top_builddir@/config.h LDFLAGS = @LDFLAGS@ -LDLIBS = @SYSTEMD_LIBS@ -SRC ::= random-seed.c sha2.c util.c -OBJ ::= $(SRC:.c=.o) -DEP ::= $(SRC:.c=.d) -TEST_FILE ::= random-seed.test +SRC := random-seed.c sha2.c util.c +OBJ := $(SRC:.c=.o) +DEP := $(SRC:.c=.d) +TEST_FILE := random-seed.test all: random-seed @@ -21,9 +27,9 @@ random-seed: $(OBJ) install: random-seed install -D -m755 random-seed $(sbindir)/random-seed - install -D -m644 random-seed.8 $(mandir)/man8/random-seed.8 -ifneq (@systemdsystemunitdir@,) - install -D -m644 random-seed.service @systemdsystemunitdir@/random-seed.service + install -D -m644 $(abs_srcdir)/random-seed.8 $(mandir)/man8/random-seed.8 +ifneq ($(systemdsystemunitdir),) + install -D -m644 $(abs_srcdir)/random-seed.service $(systemdsystemunitdir)/random-seed.service endif test: $(TEST_FILE) diff --git a/README b/README index fbb9b56..6c27edb 100644 --- a/README +++ b/README @@ -6,8 +6,20 @@ ID have not changed between a save and load. If these identifiers do not match, random-seed will still load the random seed, but will not credit the entropy. -It is my understanding that other operating systems are either not commonly imaged (e.g. BSDs) or have official tools for system image preparation (e.g. sysprep for Windows). Therefore, random-seed is Linux specific. However, it should be reasonably easy to port by simply adjusting the paths and changing getrandom to /dev/random. +It is my understanding that other operating systems are either not commonly +imaged (e.g. BSDs) or have official tools for system image preparation (e.g. +sysprep for Windows). Therefore, random-seed is Linux specific. However, it +should be reasonably easy to port by simply adjusting the paths and changing +getrandom to /dev/random. -random-seed requires GNU make to compile. +random-seed requires the following to compile: -random-seed requires Linux 3.11 or higher supporting the getrandom(2) system call. +- a gcc-compatible compiler (clang is fine) +- GNU make (BSD make is not fine) +- Bourne-like sh (autoconf compatible, i.e. probably all of them) + +If compiling from git, autoconf is also required. random-seed does not use +automake, gettext, or libtool. + +random-seed requires Linux 3.11 or higher supporting the getrandom(2) system +call. diff --git a/autogen.sh b/autogen.sh index 81f2919..afcd93c 100755 --- a/autogen.sh +++ b/autogen.sh @@ -5,4 +5,5 @@ set -e autoheader & aclocal autoconf + wait diff --git a/configure.ac b/configure.ac index 386fa5e..a624294 100644 --- a/configure.ac +++ b/configure.ac @@ -3,22 +3,38 @@ AC_LANG(C) AC_CONFIG_HEADERS(config.h) AC_PROG_CC +AC_PROG_CC_C99 +AS_IF([test "$ac_cv_prog_cc_c99" = no], + [AC_MSG_ERROR([a C99 compatible compiler is required])]) + CPPFLAGS="$CPPFLAGS -D_GNU_SOURCE -D_DEFAULT_SOURCE -D_POSIX_C_SOURCE=200809L" -CFLAGS="$CFLAGS -std=c99" -PKG_HAVE_DEFINE_WITH_MODULES(SYSTEMD, libsystemd, , , [use libsystemd to get machine id]) -AS_IF([test "$with_systemd" = yes], - [PKG_CHECK_VAR(systemdsystemunitdir, systemd, systemdsystemunitdir)]) +PKG_CHECK_VAR(systemdsystemunitdir, systemd, systemdsystemunitdir) AC_ARG_ENABLE(debug, - AC_HELP_STRING(--enable-debug, [enable debug output [default=no]]), + AC_HELP_STRING(--enable-debug, [enable debug output [no]]), [enable_debug=$enableval], [enable_debug=no] - ) +) AS_CASE([$enable_debug], [yes], [AC_DEFINE(DEBUG, [], [enable debug output])], [no], [], [AC_MSG_ERROR([invalid argument to --enable-debug])] ) +AC_ARG_WITH([machine-id], + AC_HELP_STRING(--with-machine-id, [manually specify machine-id location [/etc/machine-id, /var/lib/dbus/machine-id]]), + [machine_id=$withval] +) + +AS_IF([test -n "$machine_id"], + [ + AS_CASE($machine_id, + [/*], [], + [AC_MSG_ERROR([relative machine id path is invalid])] + ) + AC_DEFINE_UNQUOTED(MACHINE_ID_PATH, "$machine_id", [machine id location]) + ] +) + AC_OUTPUT(Makefile) diff --git a/random-seed.c b/random-seed.c index bc0952f..f33bf02 100644 --- a/random-seed.c +++ b/random-seed.c @@ -65,37 +65,24 @@ static inline void usage() { } static bool get_machine_id(unsigned char **machine_id) { -#ifdef HAVE_SYSTEMD - sd_id128_t raw_machine_id; - int r = sd_id128_get_machine(&raw_machine_id); - if (r) { - fprintf(stderr, "error getting machine id: %s\n", strerror(-r)); - *machine_id = NULL; - return false; - } - *machine_id = malloc(33); - /* convert to string to be compatible with non-hex machine IDs */ - sd_id128_to_string(raw_machine_id, (char *)*machine_id); - /* match /etc/machine-id format */ - (*machine_id)[32] = '\n'; - - return true; +#ifdef MACHINE_ID_PATH + FILE *machine_id_file = fopen(MACHINE_ID_PATH, "r"); #else - const char *etc_machine_id = SYSCONFDIR "/machine-id"; - const char *var_lib_dbus_machine_id = LOCALSTATEDIR "/lib/dbus/machine-id"; + const char *etc_machine_id = "/etc/machine-id"; + const char *var_lib_dbus_machine_id = "/var/lib/dbus/machine-id"; FILE *machine_id_file = fopen(etc_machine_id, "r"); if (!machine_id_file) { if (errno != ENOENT) fprintf(stderr, "error opening %s: %s, trying %s\n", etc_machine_id, strerror(errno), var_lib_dbus_machine_id); machine_id_file = fopen(var_lib_dbus_machine_id, "r"); - if (!machine_id_file) { - fprintf(stderr, "couldn't open any machine-id file, last error: " - "%s; tried: %s, %s\n", strerror(errno), etc_machine_id, - var_lib_dbus_machine_id); - *machine_id = NULL; - return false; - } + } +#endif + + if (!machine_id_file) { + perror("couldn't open any machine-id file, last error"); + *machine_id = NULL; + return false; } size_t machine_id_len = 0; @@ -106,7 +93,6 @@ static bool get_machine_id(unsigned char **machine_id) { } return true; -#endif } static bool get_machine_id_hash(const unsigned char salt[static SALT_LEN], unsigned char machine_id_digest[static HASH_LEN]) { diff --git a/util.c b/util.c index 7dd299a..f0d1fda 100644 --- a/util.c +++ b/util.c @@ -63,7 +63,7 @@ void hash(const unsigned char salt[static SALT_LEN], unsigned char *out, const v #endif sha256_ctx ctx; sha256_init(&ctx); - //sha256_update(&ctx, salt, SALT_LEN); + sha256_update(&ctx, salt, SALT_LEN); sha256_update(&ctx, in, (unsigned int)size); sha256_final(&ctx, out); } @@ -73,6 +73,8 @@ void print_hash(const unsigned char digest[static HASH_LEN]) { char hash[HASH_LEN*2+1]; mem2hex(hash, digest, HASH_LEN); fprintf(stderr, "hash: %s\n", hash); +#else + (void)digest; #endif } -- cgit v1.2.3-54-g00ecf