2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2012-08-20 22:02:19 +02:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <qrencode.h>
|
2015-11-16 22:09:36 +01:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2012-08-20 22:02:19 +02:00
|
|
|
|
2020-06-11 12:30:16 +02:00
|
|
|
#include "alloc-util.h"
|
2020-06-11 13:16:53 +02:00
|
|
|
#include "dlfcn-util.h"
|
2020-06-11 12:30:16 +02:00
|
|
|
#include "fd-util.h"
|
2019-04-04 11:46:44 +02:00
|
|
|
#include "fileio.h"
|
2012-08-20 22:02:19 +02:00
|
|
|
#include "journal-qrcode.h"
|
2020-06-11 12:34:31 +02:00
|
|
|
#include "locale-util.h"
|
2017-05-13 17:44:51 +02:00
|
|
|
#include "macro.h"
|
2020-08-17 15:59:00 +02:00
|
|
|
#include "qrcode-util.h"
|
2020-06-11 12:34:31 +02:00
|
|
|
#include "terminal-util.h"
|
2012-08-20 22:02:19 +02:00
|
|
|
|
2012-08-20 22:11:38 +02:00
|
|
|
int print_qr_code(
|
|
|
|
FILE *output,
|
2020-06-11 12:34:31 +02:00
|
|
|
const char *prefix_text,
|
2012-08-20 22:11:38 +02:00
|
|
|
const void *seed,
|
|
|
|
size_t seed_size,
|
|
|
|
uint64_t start,
|
|
|
|
uint64_t interval,
|
|
|
|
const char *hn,
|
|
|
|
sd_id128_t machine) {
|
|
|
|
|
2020-06-11 13:16:53 +02:00
|
|
|
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;
|
2020-06-11 12:30:16 +02:00
|
|
|
_cleanup_free_ char *url = NULL;
|
|
|
|
_cleanup_fclose_ FILE *f = NULL;
|
2020-08-04 15:20:08 +02:00
|
|
|
size_t url_size = 0;
|
2020-06-11 12:30:16 +02:00
|
|
|
QRcode* qr;
|
|
|
|
int r;
|
2012-08-20 22:02:19 +02:00
|
|
|
|
|
|
|
assert(seed);
|
|
|
|
assert(seed_size > 0);
|
|
|
|
|
2020-06-11 12:34:31 +02:00
|
|
|
/* 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;
|
|
|
|
|
2020-06-11 13:16:53 +02:00
|
|
|
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;
|
|
|
|
|
2019-04-04 11:46:44 +02:00
|
|
|
f = open_memstream_unlocked(&url, &url_size);
|
2012-08-20 22:02:19 +02:00
|
|
|
if (!f)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2017-12-11 19:50:30 +01:00
|
|
|
fputs("fss://", f);
|
2012-08-20 22:02:19 +02:00
|
|
|
|
2020-08-04 15:20:08 +02:00
|
|
|
for (size_t i = 0; i < seed_size; i++) {
|
2012-08-20 22:02:19 +02:00
|
|
|
if (i > 0 && i % 3 == 0)
|
2017-12-11 19:50:30 +01:00
|
|
|
fputc('-', f);
|
2012-08-20 22:02:19 +02:00
|
|
|
fprintf(f, "%02x", ((uint8_t*) seed)[i]);
|
|
|
|
}
|
|
|
|
|
2013-06-06 00:44:16 +02:00
|
|
|
fprintf(f, "/%"PRIx64"-%"PRIx64"?machine=" SD_ID128_FORMAT_STR,
|
|
|
|
start,
|
|
|
|
interval,
|
2012-08-20 22:11:38 +02:00
|
|
|
SD_ID128_FORMAT_VAL(machine));
|
2012-08-20 22:02:19 +02:00
|
|
|
|
|
|
|
if (hn)
|
2012-08-20 22:11:38 +02:00
|
|
|
fprintf(f, ";hostname=%s", hn);
|
2012-08-20 22:02:19 +02:00
|
|
|
|
2020-06-11 12:30:16 +02:00
|
|
|
r = fflush_and_check(f);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2012-08-20 22:02:19 +02:00
|
|
|
|
2020-06-11 12:30:16 +02:00
|
|
|
f = safe_fclose(f);
|
2012-08-20 22:02:19 +02:00
|
|
|
|
2020-06-11 13:16:53 +02:00
|
|
|
qr = sym_QRcode_encodeString(url, 0, QR_ECLEVEL_L, QR_MODE_8, 1);
|
2012-08-20 22:02:19 +02:00
|
|
|
if (!qr)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2020-06-11 12:34:31 +02:00
|
|
|
if (prefix_text)
|
|
|
|
fputs(prefix_text, output);
|
|
|
|
|
2020-08-17 15:59:00 +02:00
|
|
|
write_qrcode(output, qr);
|
2012-08-20 22:02:19 +02:00
|
|
|
|
2020-06-11 13:16:53 +02:00
|
|
|
sym_QRcode_free(qr);
|
2012-08-20 22:02:19 +02:00
|
|
|
return 0;
|
|
|
|
}
|