2021-11-11 16:26:27 +00:00
|
|
|
#define _POSIX_C_SOURCE 200809L
|
2021-04-07 18:10:43 +00:00
|
|
|
#include "util/token.h"
|
|
|
|
#include "wlr/util/log.h"
|
|
|
|
|
2021-11-11 16:26:27 +00:00
|
|
|
#include <fcntl.h>
|
2021-04-07 18:10:43 +00:00
|
|
|
#include <inttypes.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2021-11-11 16:26:27 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
2021-04-07 18:10:43 +00:00
|
|
|
|
2023-10-06 14:08:28 +00:00
|
|
|
bool generate_token(char out[static TOKEN_SIZE]) {
|
2021-04-07 18:10:43 +00:00
|
|
|
static FILE *urandom = NULL;
|
|
|
|
uint64_t data[2];
|
|
|
|
|
|
|
|
if (!urandom) {
|
2021-11-11 16:26:27 +00:00
|
|
|
int fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC);
|
|
|
|
if (fd < 0) {
|
2021-04-07 18:10:43 +00:00
|
|
|
wlr_log_errno(WLR_ERROR, "Failed to open random device");
|
|
|
|
return false;
|
|
|
|
}
|
2021-11-11 16:26:27 +00:00
|
|
|
if (!(urandom = fdopen(fd, "r"))) {
|
|
|
|
wlr_log_errno(WLR_ERROR, "fdopen failed");
|
|
|
|
close(fd);
|
|
|
|
return false;
|
|
|
|
}
|
2021-04-07 18:10:43 +00:00
|
|
|
}
|
|
|
|
if (fread(data, sizeof(data), 1, urandom) != 1) {
|
|
|
|
wlr_log_errno(WLR_ERROR, "Failed to read from random device");
|
|
|
|
return false;
|
|
|
|
}
|
2023-10-06 14:08:28 +00:00
|
|
|
if (snprintf(out, TOKEN_SIZE, "%016" PRIx64 "%016" PRIx64, data[0], data[1]) != TOKEN_SIZE - 1) {
|
2021-04-07 18:10:43 +00:00
|
|
|
wlr_log_errno(WLR_ERROR, "Failed to format hex string token");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|