X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=src%2Fvlib%2Funix%2Futil.c;h=312cc9b5a0aa0eef28d921a50c6c89b3be692591;hb=9a244bb5e4a21835cac51ba7f35095b9c24547e6;hp=93aeb99c5d90b37c49379ef411a193aa33a5ebe5;hpb=215961829c4ae5f738ffcd01a8d1afcab13bd0e2;p=vpp.git diff --git a/src/vlib/unix/util.c b/src/vlib/unix/util.c index 93aeb99c5d9..312cc9b5a0a 100644 --- a/src/vlib/unix/util.c +++ b/src/vlib/unix/util.c @@ -257,6 +257,55 @@ done: return error; } +clib_error_t * +vlib_unix_validate_runtime_file (unix_main_t * um, + const char *path, u8 ** full_path) +{ + u8 *fp = 0; + char *last_slash = 0; + + if (path[0] == '\0') + { + return clib_error_return (0, "path is an empty string"); + } + else if (strncmp (path, "../", 3) == 0 || strstr (path, "/../")) + { + return clib_error_return (0, "'..' not allowed in runtime path"); + } + else if (path[0] == '/') + { + /* Absolute path. Has to start with runtime directory */ + if (strncmp ((char *) um->runtime_dir, path, + strlen ((char *) um->runtime_dir))) + { + return clib_error_return (0, + "file %s is not in runtime directory %s", + path, um->runtime_dir); + } + fp = format (0, "%s%c", path, '\0'); + } + else + { + /* Relative path, just append to runtime */ + fp = format (0, "%s/%s%c", um->runtime_dir, path, '\0'); + } + + /* We don't want to create a directory out of the last file */ + if ((last_slash = strrchr ((char *) fp, '/')) != NULL) + *last_slash = '\0'; + + clib_error_t *error = vlib_unix_recursive_mkdir ((char *) fp); + + if (last_slash != NULL) + *last_slash = '/'; + + if (error) + vec_free (fp); + + *full_path = fp; + return error; +} + /* * fd.io coding-style-patch-verification: ON *