There is an existing function for recursing through directories. So use
that instead. source-directory only recurses through a single directory,
so we set a maximum depth of 1.

ifupdown only selects files that match the regex '^[a-zA-Z0-9_-]+$'[1].
Comply so that we don't include invalid files.

[1] 
https://manpages.debian.org/stretch/ifupdown/interfaces.5.en.html#INCLUDING_OTHER_FILES

Signed-off-by: Brandon Maier <[email protected]>
---
 networking/ifupdown.c | 47 ++++++++++++++++++++++++++++++++-----------
 1 file changed, 35 insertions(+), 12 deletions(-)

diff --git a/networking/ifupdown.c b/networking/ifupdown.c
index 41cc86df7..ef6b185fc 100644
--- a/networking/ifupdown.c
+++ b/networking/ifupdown.c
@@ -159,6 +159,8 @@
 /* Forward declaration */
 struct interface_defn_t;
 
+static struct interfaces_file_t *read_interfaces(const char *filename, struct 
interfaces_file_t *defn);
+
 typedef int execfn(char *command);
 
 struct method_t {
@@ -857,6 +859,31 @@ static const struct method_t *get_method(const struct 
address_family_t *af, char
        return NULL;
 }
 
+static int FAST_FUNC read_interfaces_action(const char *fileName,
+               struct stat *statbuf UNUSED_PARAM, void* userData, int depth)
+{
+       const char *p;
+
+       if (depth == 0)
+               return SKIP;
+
+       for (p = bb_basename(fileName); *p; p++)
+               if (!(isalnum(*p) || *p == '_' || *p == '-'))
+                       return SKIP;
+
+       read_interfaces(fileName, userData);
+
+       return TRUE;
+}
+
+static int FAST_FUNC max_depth_one(const char *fileName UNUSED_PARAM,
+               struct stat *statbuf UNUSED_PARAM, void* userData UNUSED_PARAM, 
int depth)
+{
+       if (depth > 0)
+               return SKIP;
+       return TRUE;
+}
+
 static struct interfaces_file_t *read_interfaces(const char *filename, struct 
interfaces_file_t *defn)
 {
        /* Let's try to be compatible.
@@ -1019,20 +1046,16 @@ static struct interfaces_file_t *read_interfaces(const 
char *filename, struct in
                        read_interfaces(next_word(&rest_of_line), defn);
                } else if (is_prefixed_with(first_word, "source-dir")) {
                        const char *dirpath;
-                       DIR *dir;
-                       struct dirent *entry;
 
                        dirpath = next_word(&rest_of_line);
-                       dir = xopendir(dirpath);
-                       while ((entry = readdir(dir)) != NULL) {
-                               char *path;
-                               if (entry->d_name[0] == '.')
-                                       continue;
-                               path = concat_path_file(dirpath, entry->d_name);
-                               read_interfaces(path, defn);
-                               free(path);
-                       }
-                       closedir(dir);
+                       debug_noise("%s %s\n", first_word, dirpath);
+                       recursive_action(dirpath,
+                                       
ACTION_RECURSE|ACTION_FOLLOWLINKS|ACTION_QUIET,
+                                       read_interfaces_action, /* file action 
*/
+                                       max_depth_one, /* dir action */
+                                       defn, /* user data */
+                                       0 /* depth */
+                               );
                } else {
                        switch (currently_processing) {
                        case IFACE:
-- 
2.19.0

_______________________________________________
busybox mailing list
[email protected]
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to