Start work on evars
authorJeroen van der Heijden <jeroen@transceptor.technology>
Thu, 9 Apr 2020 13:49:03 +0000 (15:49 +0200)
committerJeroen van der Heijden <jeroen@transceptor.technology>
Thu, 9 Apr 2020 13:49:03 +0000 (15:49 +0200)
include/siri/evars.h [new file with mode: 0644]
main.c
src/siri/cfg/cfg.c
src/siri/db/auth.c
src/siri/evars.c [new file with mode: 0644]
src/siri/net/tcp.c

diff --git a/include/siri/evars.h b/include/siri/evars.h
new file mode 100644 (file)
index 0000000..147974d
--- /dev/null
@@ -0,0 +1,11 @@
+/*
+ * siri/evars.h
+ */
+#ifndef SIRI_EVARS_H_
+#define SIRI_EVARS_H_
+
+#include <siri/siri.h>
+
+void siri_evars_parse(siri_t * siri);
+
+#endif  /* SIRI_EVARS_H_ */
diff --git a/main.c b/main.c
index 819a4f14cae8732ba17a72893afd70b27c926786..0ee8e7468d221d8b47b28f6e8239355c5cd10994 100644 (file)
--- a/main.c
+++ b/main.c
@@ -15,6 +15,7 @@
 #include <siri/help/help.h>
 #include <siri/siri.h>
 #include <siri/version.h>
+#include <siri/evars.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <time.h>
@@ -56,6 +57,8 @@ int main(int argc, char * argv[])
     /* read siridb main application configuration */
     siri_cfg_init(&siri);
 
+    siri_evars_parse(&siri);
+
     /* start SiriDB. (this start the event loop etc.) */
     if (siri_start() && !siri_err)
     {
index 9a6c8b47a5df3ab0a7c62c652d6b344b26016944..8bb840b5886ec4132ea6f420dc0d8d6a5bffbc46 100644 (file)
@@ -633,8 +633,10 @@ static void SIRI_CFG_read_address_port(
                 siri.args->config,
                 cfgparser_errmsg(rc));
         exit(EXIT_FAILURE);
+        return;
     }
-    else if (option->tp != CFGPARSER_TP_STRING)
+
+    if (option->tp != CFGPARSER_TP_STRING)
     {
         log_critical(
                 "Error reading '%s' in '%s': %s. ",
@@ -642,80 +644,78 @@ static void SIRI_CFG_read_address_port(
                 siri.args->config,
                 "error: expecting a string value");
         exit(EXIT_FAILURE);
+        return;
     }
-    else
-    {
-        if (*option->val->string == '[')
-        {
-            /* an IPv6 address... */
-            for (port = address = option->val->string + 1; *port; port++)
-            {
-                if (*port == ']')
-                {
-                    *port = 0;
-                    port++;
-                    break;
-                }
-            }
-        }
-        else
-        {
-            port = address = option->val->string;
-        }
 
-        for (; *port; port++)
+    if (*option->val->string == '[')
+    {
+        /* an IPv6 address... */
+        for (port = address = option->val->string + 1; *port; port++)
         {
-            if (*port == ':')
+            if (*port == ']')
             {
                 *port = 0;
                 port++;
                 break;
             }
         }
+    }
+    else
+    {
+        port = address = option->val->string;
+    }
+
+    for (; *port; port++)
+    {
+        if (*port == ':')
+        {
+            *port = 0;
+            port++;
+            break;
+        }
+    }
+
+    if (    !strlen(address) ||
+            strlen(address) >= SIRI_CFG_MAX_LEN_ADDRESS ||
+            !xstr_is_int(port) ||
+            strcpy(address_pt, address) == NULL ||
+            xstr_replace_str(
+                    address_pt,
+                    "%HOSTNAME",
+                    hostname,
+                    SIRI_CFG_MAX_LEN_ADDRESS))
+    {
+        log_critical(
+                "Error reading '%s' in '%s': "
+                "error: got an unexpected value '%s:%s'.",
+                option_name,
+                siri.args->config,
+                address,
+                port);
+        exit(EXIT_FAILURE);
+    }
+    else
+    {
+        test_port = atoi(port);
 
-        if (    !strlen(address) ||
-                strlen(address) >= SIRI_CFG_MAX_LEN_ADDRESS ||
-                !xstr_is_int(port) ||
-                strcpy(address_pt, address) == NULL ||
-                xstr_replace_str(
-                        address_pt,
-                        "%HOSTNAME",
-                        hostname,
-                        SIRI_CFG_MAX_LEN_ADDRESS))
+        if (test_port < 1 || test_port > 65535)
         {
             log_critical(
                     "Error reading '%s' in '%s': "
-                    "error: got an unexpected value '%s:%s'.",
+                    "error: port should be between 1 and 65535, got '%d'.",
                     option_name,
                     siri.args->config,
-                    address,
-                    port);
+                    test_port);
             exit(EXIT_FAILURE);
         }
         else
         {
-            test_port = atoi(port);
-
-            if (test_port < 1 || test_port > 65535)
-            {
-                log_critical(
-                        "Error reading '%s' in '%s': "
-                        "error: port should be between 1 and 65535, got '%d'.",
-                        option_name,
-                        siri.args->config,
-                        test_port);
-                exit(EXIT_FAILURE);
-            }
-            else
-            {
-                *port_pt = (uint16_t) test_port;
-            }
-
-            log_debug("Read '%s' from config: %s:%d",
-                    option_name,
-                    address_pt,
-                    *port_pt);
+            *port_pt = (uint16_t) test_port;
         }
 
+        log_debug("Read '%s' from config: %s:%d",
+                option_name,
+                address_pt,
+                *port_pt);
     }
 }
index 7144815c4597e35fb1db8eb0745e7c5ff7dbcb00..d0b4a3bbb4baf2b246f2dda4d54e004de6e06503 100644 (file)
@@ -42,7 +42,18 @@ cproto_server_t siridb_auth_user_request(
             username,
             password)) == NULL)
     {
-        log_warning("User authentication request failed: invalid credentials");
+        if (strcmp(username, "sa") == 0)
+        {
+            log_warning(
+                    "User authentication request failed: "
+                    "invalid credentials for user `sa`, "
+                    "did you mean to use the default database user `iris`?");
+        }
+        else
+        {
+            log_warning(
+                    "User authentication request failed: invalid credentials");
+        }
         return CPROTO_ERR_AUTH_CREDENTIALS;
     }
 
diff --git a/src/siri/evars.c b/src/siri/evars.c
new file mode 100644 (file)
index 0000000..480939f
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ * siri/evars.c
+ */
+
+#include <siri/evars.h>
+
+static void evars__u8(const char * evar, uint8_t * u8)
+{
+    char * u8str = getenv(evar);
+    if (!u8str)
+        return;
+
+    *u8 = (uint8_t) strtoul(u8str, NULL, 10);
+}
+
+static void evars__u16(const char * evar, uint16_t * u16)
+{
+    char * u16str = getenv(evar);
+    if (!u16str)
+        return;
+
+    *u16 = (uint16_t) strtoul(u16str, NULL, 10);
+}
+
+static void evars__sizet(const char * evar, size_t * sz)
+{
+    char * sizetstr = getenv(evar);
+    if (!sizetstr)
+        return;
+
+    *sz = (size_t) strtoull(sizetstr, NULL, 10);
+}
+
+static void evars__double(const char * evar, double * d)
+{
+    char * doublestr = getenv(evar);
+    if (!doublestr)
+        return;
+
+    *d = strtod(doublestr, NULL);
+}
+
+static void evars__str(const char * evar, char ** straddr)
+{
+    char * str = getenv(evar);
+    if (!str || !(str = strdup(str)))
+        return;
+
+    free(*straddr);
+    *straddr = str;
+}
+
+
+void siri_evars_parse(siri_t * siri)
+{
+
+}
index dd3c5fe6d23dcf3e4d03b41013dafc5f189a7a3b..2cdcc070de58574499a9a301897e59d7c744d93b 100644 (file)
@@ -85,6 +85,83 @@ failed:
 }
 
 
+int sirinet_extract_addr_port(
+        const char * s,
+        char * addr,
+        uint16_t * port)
+{
+    if (*option->val->string == '[')
+    {
+        /* an IPv6 address... */
+        for (port = address = option->val->string + 1; *port; port++)
+        {
+            if (*port == ']')
+            {
+                *port = 0;
+                port++;
+                break;
+            }
+        }
+    }
+    else
+    {
+        port = address = option->val->string;
+    }
+
+    for (; *port; port++)
+    {
+        if (*port == ':')
+        {
+            *port = 0;
+            port++;
+            break;
+        }
+    }
+
+    if (    !strlen(address) ||
+            strlen(address) >= SIRI_CFG_MAX_LEN_ADDRESS ||
+            !xstr_is_int(port) ||
+            strcpy(address_pt, address) == NULL ||
+            xstr_replace_str(
+                    address_pt,
+                    "%HOSTNAME",
+                    hostname,
+                    SIRI_CFG_MAX_LEN_ADDRESS))
+    {
+        log_critical(
+                "Error reading '%s' in '%s': "
+                "error: got an unexpected value '%s:%s'.",
+                option_name,
+                siri.args->config,
+                address,
+                port);
+        exit(EXIT_FAILURE);
+    }
+    else
+    {
+        test_port = atoi(port);
+
+        if (test_port < 1 || test_port > 65535)
+        {
+            log_critical(
+                    "Error reading '%s' in '%s': "
+                    "error: port should be between 1 and 65535, got '%d'.",
+                    option_name,
+                    siri.args->config,
+                    test_port);
+            exit(EXIT_FAILURE);
+        }
+        else
+        {
+            *port_pt = (uint16_t) test_port;
+        }
+
+        log_debug("Read '%s' from config: %s:%d",
+                option_name,
+                address_pt,
+                *port_pt);
+    }
+}