aboutsummaryrefslogtreecommitdiff
path: root/libbb
diff options
context:
space:
mode:
authorBjørn Mork <bjorn@mork.no>2017-11-05 15:42:31 +0100
committerBjørn Mork <bjorn@mork.no>2017-11-05 15:42:31 +0100
commitaf6d5f438bf18c92730a8ae273efa54cd11f2906 (patch)
tree6d8e15a1766ecb006e10a54a031b93a32d908736 /libbb
parentbba3d8db9a578585a25dd7289b8480e3530f7526 (diff)
ripe-atlas-fw: imported version 47804780
Signed-off-by: Bjørn Mork <bjorn@mork.no>
Diffstat (limited to 'libbb')
-rw-r--r--libbb/Kbuild1
-rw-r--r--libbb/atlas_name_macro.c87
-rw-r--r--libbb/atlas_probe.c6
3 files changed, 93 insertions, 1 deletions
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<sizeof(random_buf); i++)
+ {
+ c= random_buf[i];
+
+ out[0]= hex_chars[(c >> 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;