summaryrefslogtreecommitdiff
path: root/src/journal
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2020-10-25 12:11:21 +0100
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2020-10-27 18:33:29 +0100
commitf1b823596f8a4dc6b1f76206b7473bac71e3840b (patch)
tree7d44f1758de5ce755bc79eedd32a3fd6bd70097c /src/journal
parentc9e9a57fce214b24d250921654b101ff433962e7 (diff)
downloadsystemd-f1b823596f8a4dc6b1f76206b7473bac71e3840b.tar.gz
journal,homectl: unify implementations of libqrencode loading and fss key printing
We had two of each: both homectl and journalctl had the whole dlopen() wrapper, and journalctl had two implementations (slightly different) of the code to print the fss:// pattern. print_qrcode() now returns -EOPNOTSUPP when compiled with qrcode support. Both callers ignore the return value, so this changes nothing. No functional change.
Diffstat (limited to 'src/journal')
-rw-r--r--src/journal/journal-qrcode.c97
-rw-r--r--src/journal/journal-qrcode.h9
-rw-r--r--src/journal/journalctl.c75
-rw-r--r--src/journal/meson.build5
4 files changed, 62 insertions, 124 deletions
diff --git a/src/journal/journal-qrcode.c b/src/journal/journal-qrcode.c
deleted file mode 100644
index e8a7655316..0000000000
--- a/src/journal/journal-qrcode.c
+++ /dev/null
@@ -1,97 +0,0 @@
-/* SPDX-License-Identifier: LGPL-2.1+ */
-
-#include <errno.h>
-#include <qrencode.h>
-#include <stdbool.h>
-#include <stdio.h>
-#include <stdlib.h>
-
-#include "alloc-util.h"
-#include "dlfcn-util.h"
-#include "fd-util.h"
-#include "fileio.h"
-#include "journal-qrcode.h"
-#include "locale-util.h"
-#include "macro.h"
-#include "qrcode-util.h"
-#include "terminal-util.h"
-
-int print_qr_code(
- FILE *output,
- const char *prefix_text,
- const void *seed,
- size_t seed_size,
- uint64_t start,
- uint64_t interval,
- const char *hn,
- sd_id128_t machine) {
-
- QRcode* (*sym_QRcode_encodeString)(const char *string, int version, QRecLevel level, QRencodeMode hint, int casesensitive);
- void (*sym_QRcode_free)(QRcode *qrcode);
- _cleanup_(dlclosep) void *dl = NULL;
- _cleanup_free_ char *url = NULL;
- _cleanup_fclose_ FILE *f = NULL;
- size_t url_size = 0;
- QRcode* qr;
- int r;
-
- assert(seed);
- assert(seed_size > 0);
-
- /* If this is not an UTF-8 system or ANSI colors aren't supported/disabled don't print any QR
- * codes */
- if (!is_locale_utf8() || !colors_enabled())
- return -EOPNOTSUPP;
-
- dl = dlopen("libqrencode.so.4", RTLD_LAZY);
- if (!dl)
- return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
- "QRCODE support is not installed: %s", dlerror());
-
- r = dlsym_many_and_warn(
- dl,
- LOG_DEBUG,
- &sym_QRcode_encodeString, "QRcode_encodeString",
- &sym_QRcode_free, "QRcode_free",
- NULL);
- if (r < 0)
- return r;
-
- f = open_memstream_unlocked(&url, &url_size);
- if (!f)
- return -ENOMEM;
-
- fputs("fss://", f);
-
- for (size_t i = 0; i < seed_size; i++) {
- if (i > 0 && i % 3 == 0)
- fputc('-', f);
- fprintf(f, "%02x", ((uint8_t*) seed)[i]);
- }
-
- fprintf(f, "/%"PRIx64"-%"PRIx64"?machine=" SD_ID128_FORMAT_STR,
- start,
- interval,
- SD_ID128_FORMAT_VAL(machine));
-
- if (hn)
- fprintf(f, ";hostname=%s", hn);
-
- r = fflush_and_check(f);
- if (r < 0)
- return r;
-
- f = safe_fclose(f);
-
- qr = sym_QRcode_encodeString(url, 0, QR_ECLEVEL_L, QR_MODE_8, 1);
- if (!qr)
- return -ENOMEM;
-
- if (prefix_text)
- fputs(prefix_text, output);
-
- write_qrcode(output, qr);
-
- sym_QRcode_free(qr);
- return 0;
-}
diff --git a/src/journal/journal-qrcode.h b/src/journal/journal-qrcode.h
deleted file mode 100644
index 24ae9d32ee..0000000000
--- a/src/journal/journal-qrcode.h
+++ /dev/null
@@ -1,9 +0,0 @@
-/* SPDX-License-Identifier: LGPL-2.1+ */
-#pragma once
-
-#include <inttypes.h>
-#include <stdio.h>
-
-#include "sd-id128.h"
-
-int print_qr_code(FILE *f, const char *prefix_text, const void *seed, size_t seed_size, uint64_t start, uint64_t interval, const char *hn, sd_id128_t machine);
diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c
index e43e9d1afa..4014b8aad2 100644
--- a/src/journal/journalctl.c
+++ b/src/journal/journalctl.c
@@ -43,7 +43,6 @@
#include "io-util.h"
#include "journal-def.h"
#include "journal-internal.h"
-#include "journal-qrcode.h"
#include "journal-util.h"
#include "journal-vacuum.h"
#include "journal-verify.h"
@@ -60,6 +59,7 @@
#include "path-util.h"
#include "pcre2-dlopen.h"
#include "pretty-print.h"
+#include "qrcode-util.h"
#include "random-util.h"
#include "rlimit-util.h"
#include "set.h"
@@ -1779,6 +1779,53 @@ static int add_syslog_identifier(sd_journal *j) {
return 0;
}
+static int format_journal_url(
+ const void *seed,
+ size_t seed_size,
+ uint64_t start,
+ uint64_t interval,
+ const char *hn,
+ sd_id128_t machine,
+ bool full,
+ char **ret_url) {
+ _cleanup_free_ char *url = NULL;
+ _cleanup_fclose_ FILE *f = NULL;
+ size_t url_size = 0;
+ int r;
+
+ assert(seed);
+ assert(seed_size > 0);
+
+ f = open_memstream_unlocked(&url, &url_size);
+ if (!f)
+ return -ENOMEM;
+
+ if (full)
+ fputs("fss://", f);
+
+ for (size_t i = 0; i < seed_size; i++) {
+ if (i > 0 && i % 3 == 0)
+ fputc('-', f);
+ fprintf(f, "%02x", ((uint8_t*) seed)[i]);
+ }
+
+ fprintf(f, "/%"PRIx64"-%"PRIx64, start, interval);
+
+ if (full) {
+ fprintf(f, "?machine=" SD_ID128_FORMAT_STR, SD_ID128_FORMAT_VAL(machine));
+ if (hn)
+ fprintf(f, ";hostname=%s", hn);
+ }
+
+ r = fflush_and_check(f);
+ if (r < 0)
+ return r;
+
+ f = safe_fclose(f);
+ *ret_url = TAKE_PTR(url);
+ return 0;
+}
+
static int setup_keys(void) {
#if HAVE_GCRYPT
size_t mpk_size, seed_size, state_size;
@@ -1893,7 +1940,11 @@ static int setup_keys(void) {
k = mfree(k);
- _cleanup_free_ char *hn = NULL;
+ _cleanup_free_ char *hn = NULL, *key = NULL;
+
+ r = format_journal_url(seed, seed_size, n, arg_interval, hn, machine, false, &key);
+ if (r < 0)
+ return r;
if (on_tty()) {
hn = gethostname_malloc();
@@ -1925,21 +1976,19 @@ static int setup_keys(void) {
fflush(stderr);
}
- for (size_t i = 0; i < seed_size; i++) {
- if (i > 0 && i % 3 == 0)
- putchar('-');
- printf("%02x", ((uint8_t*) seed)[i]);
- }
- printf("/%llx-%llx\n", (unsigned long long) n, (unsigned long long) arg_interval);
+ puts(key);
if (on_tty()) {
fprintf(stderr, "%s", ansi_normal());
#if HAVE_QRENCODE
- (void) print_qr_code(stderr,
- "\nTo transfer the verification key to your phone scan the QR code below:\n",
- seed, seed_size,
- n, arg_interval,
- hn, machine);
+ _cleanup_free_ char *url = NULL;
+ r = format_journal_url(seed, seed_size, n, arg_interval, hn, machine, true, &url);
+ if (r < 0)
+ return r;
+
+ (void) print_qrcode(stderr,
+ "To transfer the verification key to your phone scan the QR code below",
+ url);
#endif
}
diff --git a/src/journal/meson.build b/src/journal/meson.build
index 3a590bdc6c..215ba949e7 100644
--- a/src/journal/meson.build
+++ b/src/journal/meson.build
@@ -107,11 +107,6 @@ journalctl_sources = files('''
pcre2-dlopen.h
'''.split())
-if conf.get('HAVE_QRENCODE') == 1
- journalctl_sources += files('journal-qrcode.c',
- 'journal-qrcode.h')
-endif
-
install_data('journald.conf',
install_dir : pkgsysconfdir)