From af6d5f438bf18c92730a8ae273efa54cd11f2906 Mon Sep 17 00:00:00 2001 From: Bjørn Mork Date: Sun, 5 Nov 2017 15:42:31 +0100 Subject: ripe-atlas-fw: imported version 4780 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Bjørn Mork --- libbb/Kbuild | 1 + libbb/atlas_name_macro.c | 87 ++++++++++++++++++++++++++++++++++++++++++++++++ libbb/atlas_probe.c | 6 +++- 3 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 libbb/atlas_name_macro.c (limited to 'libbb') diff --git a/libbb/Kbuild b/libbb/Kbuild index 9a17725..280d40d 100644 --- a/libbb/Kbuild +++ b/libbb/Kbuild @@ -12,6 +12,7 @@ lib-y += atlas_bb64.o lib-y += atlas_probe.o lib-y += atlas_timesync.o lib-y += atlas_check_addr.o +lib-y += atlas_name_macro.o lib-y += bb_askpass.o lib-y += bb_basename.o lib-y += bb_do_delay.o diff --git a/libbb/atlas_name_macro.c b/libbb/atlas_name_macro.c new file mode 100644 index 0000000..0674fd0 --- /dev/null +++ b/libbb/atlas_name_macro.c @@ -0,0 +1,87 @@ +#include "libbb.h" + +#define URANDOM_DEV "/dev/urandom" + +static char hex_chars[]= "0123456789abcdef"; + +char *atlas_name_macro(char *str) +{ + unsigned char c; + int i, fd; + size_t len; + char *p, *in, *out; + char buf[256]; + unsigned char random_buf[8]; + + p= strchr(str, '$'); + if (p == NULL) + return strdup(str); + + in= str; + out= buf; + + while (*in) + { + p= strchr(in, '$'); + if (p == NULL) + { + strlcpy(out, in, buf+sizeof(buf)-out); + break; + } + if (p != in) + { + len= p-in; + + if (len+1 > buf+sizeof(buf)-out) + return NULL; + memcpy(out, in, len); + out[len]= '\0'; + + out += len; + } + + switch(p[1]) + { + case 'p': + snprintf(out, buf+sizeof(buf)-out, "%d", + get_probe_id()); + break; + case 't': + snprintf(out, buf+sizeof(buf)-out, "%ld", + (long)time(NULL)); + break; + case 'r': + /* We need to hex digits per byte in random_buf */ + if (sizeof(random_buf)*2+1 > buf+sizeof(buf)-out) + return NULL; + + fd= open(URANDOM_DEV, O_RDONLY); + + /* Best effort, just ignore errors */ + if (fd != -1) + { + read(fd, random_buf, sizeof(random_buf)); + close(fd); + } + + for (i= 0; i> 4) & 0xf]; + out[1]= hex_chars[c & 0xf]; + out += 2; + } + + out[0]= '\0'; + break; + + default: + return NULL; + } + in= p+2; + out += strlen(out); + } + + return strdup(buf); +} diff --git a/libbb/atlas_probe.c b/libbb/atlas_probe.c index a3264fb..8a5c8f0 100644 --- a/libbb/atlas_probe.c +++ b/libbb/atlas_probe.c @@ -6,13 +6,17 @@ #include "libbb.h" int get_probe_id(void) { - int probe_id; + static int probe_id= -1; + size_t len; char *check; const char *key; FILE *fp; char buf[80]; + if (probe_id > 0) + return probe_id; /* Assume probe ID never changes */ + fp= fopen("/home/atlas/status/reg_init_reply.txt", "r"); if (!fp) return -1; -- cgit v1.2.3