cleanup, move parse_tuple to libuci, add some input validation
authorFelix Fietkau <nbd@openwrt.org>
Tue, 29 Jan 2008 17:55:31 +0000 (18:55 +0100)
committerFelix Fietkau <nbd@openwrt.org>
Tue, 29 Jan 2008 17:55:31 +0000 (18:55 +0100)
cli.c
file.c
uci.h
util.c

diff --git a/cli.c b/cli.c
index 1fa1cb1..410d7e8 100644 (file)
--- a/cli.c
+++ b/cli.c
@@ -112,37 +112,6 @@ static int uci_do_export(int argc, char **argv)
        return 0;
 }
 
        return 0;
 }
 
-static void parse_tuple(char *str, char **package, char **section, char **option, char **value)
-{
-       char *last = NULL;
-
-       *package = strtok(str, ".");
-       if (!*package)
-               goto done;
-
-       last = *package;
-       *section = strtok(NULL, ".");
-       if (!*section)
-               goto done;
-
-       last = *section;
-       *option = strtok(NULL, ".");
-       if (!*option)
-               goto done;
-
-       last = *option;
-done:
-       if (!value)
-               return;
-
-       last = strtok(last, "=");
-       if (!last)
-               return;
-
-       *value = last + strlen(last) + 1;
-}
-
-
 static int uci_do_get(int argc, char **argv)
 {
        char *package = NULL;
 static int uci_do_get(int argc, char **argv)
 {
        char *package = NULL;
@@ -155,8 +124,7 @@ static int uci_do_get(int argc, char **argv)
        if (argc != 2)
                return 255;
 
        if (argc != 2)
                return 255;
 
-       parse_tuple(argv[1], &package, &section, &option, NULL);
-       if (!package)
+       if (uci_parse_tuple(ctx, argv[1], &package, &section, &option, NULL) != UCI_OK)
                return 1;
 
        if (uci_load(ctx, package, &p) != UCI_OK) {
                return 1;
 
        if (uci_load(ctx, package, &p) != UCI_OK) {
@@ -196,8 +164,7 @@ static int uci_do_set(int argc, char **argv)
        if (argc != 2)
                return 255;
 
        if (argc != 2)
                return 255;
 
-       parse_tuple(argv[1], &package, &section, &option, &value);
-       if (!package)
+       if (uci_parse_tuple(ctx, argv[1], &package, &section, &option, &value) != UCI_OK)
                return 1;
 
        if (uci_load(ctx, package, &p) != UCI_OK) {
                return 1;
 
        if (uci_load(ctx, package, &p) != UCI_OK) {
diff --git a/file.c b/file.c
index 9a2e353..a78c5a7 100644 (file)
--- a/file.c
+++ b/file.c
@@ -470,15 +470,14 @@ int uci_export(struct uci_context *ctx, FILE *stream, struct uci_package *packag
        UCI_HANDLE_ERR(ctx);
        UCI_ASSERT(ctx, stream != NULL);
 
        UCI_HANDLE_ERR(ctx);
        UCI_ASSERT(ctx, stream != NULL);
 
-       if (package) {
+       if (package)
                uci_export_package(package, stream, header);
                uci_export_package(package, stream, header);
-               goto done;
+       else {
+               uci_foreach_element(&ctx->root, e) {
+                       uci_export_package(uci_to_package(e), stream, header);
+               }
        }
 
        }
 
-       uci_foreach_element(&ctx->root, e) {
-               uci_export_package(uci_to_package(e), stream, header);
-       }
-done:
        return 0;
 }
 
        return 0;
 }
 
diff --git a/uci.h b/uci.h
index c7251ab..0cb352d 100644 (file)
--- a/uci.h
+++ b/uci.h
@@ -60,10 +60,24 @@ struct uci_package;
 struct uci_section;
 struct uci_option;
 struct uci_history;
 struct uci_section;
 struct uci_option;
 struct uci_history;
+struct uci_context;
 struct uci_parse_context;
 
 
 /**
 struct uci_parse_context;
 
 
 /**
+ * uci_parse_tuple: Parse an uci tuple
+ * @ctx: uci context
+ * @str: input string
+ * @package: output package pointer
+ * @section: output section pointer
+ * @option: output option pointer
+ * @value: output value pointer
+ *
+ * format: <package>[.<section>[.<option>]][=<value>]
+ */
+extern int uci_parse_tuple(struct uci_context *ctx, char *str, char **package, char **section, char **option, char **value);
+
+/**
  * uci_alloc_context: Allocate a new uci context
  */
 extern struct uci_context *uci_alloc_context(void);
  * uci_alloc_context: Allocate a new uci context
  */
 extern struct uci_context *uci_alloc_context(void);
diff --git a/util.c b/util.c
index c9be293..85e87ab 100644 (file)
--- a/util.c
+++ b/util.c
@@ -16,6 +16,8 @@
  * This file contains wrappers to standard functions, which
  * throw exceptions upon failure.
  */
  * This file contains wrappers to standard functions, which
  * throw exceptions upon failure.
  */
+#include <stdbool.h>
+#include <ctype.h>
 
 static void *uci_malloc(struct uci_context *ctx, size_t size)
 {
 
 static void *uci_malloc(struct uci_context *ctx, size_t size)
 {
@@ -49,4 +51,65 @@ static char *uci_strdup(struct uci_context *ctx, const char *str)
        return ptr;
 }
 
        return ptr;
 }
 
+static bool validate_name(char *str)
+{
+       if (!*str)
+               return false;
+
+       while (*str) {
+               if (!isalnum(*str) && (*str != '_'))
+                       return false;
+               str++;
+       }
+       return true;
+}
+
+int uci_parse_tuple(struct uci_context *ctx, char *str, char **package, char **section, char **option, char **value)
+{
+       char *last = NULL;
+
+       UCI_HANDLE_ERR(ctx);
+       UCI_ASSERT(ctx, str && package && section && option);
+
+       *package = strtok(str, ".");
+       if (!*package || !validate_name(*package))
+               goto error;
+
+       last = *package;
+       *section = strtok(NULL, ".");
+       if (!*section)
+               goto lastval;
+
+       last = *section;
+       *option = strtok(NULL, ".");
+       if (!*option)
+               goto lastval;
+
+       last = *option;
+
+lastval:
+       last = strchr(last, '=');
+       if (last) {
+               if (!value)
+                       goto error;
+
+               *last = 0;
+               last++;
+               if (!*last)
+                       goto error;
+       }
+
+       if (*section && !validate_name(*section))
+               goto error;
+       if (*option && !validate_name(*option))
+               goto error;
+
+       goto done;
+
+error:
+       UCI_THROW(ctx, UCI_ERR_PARSE);
+
+done:
+       return 0;
+}