From 03a32b6e1e364bccf6d181057ba876162685c44d Mon Sep 17 00:00:00 2001 From: Junichi Uekawa Date: Wed, 8 Oct 2008 04:08:56 +0000 Subject: [PATCH] add sanity checking in filenames and paths. Check files exist, and check paths are specified absolute. I am relying on paths starting from '/' when they are absolute. daemon() will chdir to /, and that means giving relative paths to this program will give unexpected (to the user) results. --- lsyncd.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/lsyncd.c b/lsyncd.c index 71fe59c..ea2ff47 100644 --- a/lsyncd.c +++ b/lsyncd.c @@ -800,6 +800,35 @@ bool scan_homes() return true; } +/** + * Utility function to check file exists. Print out error message and die. + * + * @param filename filename to check + */ +void check_file_exists(const char* filename) +{ + struct stat st; + if (-1==stat(filename, &st)) { + printlogf(LOG_ERROR, "File [%s] does not exist\n", filename); + exit (-1); + } +} + + +/** + * Utility function to check given path is absolute path. + * + * @param filename filename to check + */ +void check_absolute_path(const char* filename) +{ + if (filename[0] != '/') { + printlogf(LOG_ERROR, "Filename [%s] is not an absolute path\n", filename); + exit (-1); + } +} + + /** * Prints the help text and exits 0. * @@ -907,6 +936,7 @@ bool parse_options(int argc, char **argv) } } + if (optind + 2 != argc) { printf("Error: please specify SOURCE and TARGET (see --help)\n"); exit(-1); @@ -922,6 +952,15 @@ bool parse_options(int argc, char **argv) } printlogf(LOG_NORMAL, "syncing %s -> %s\n", option_source, option_target); + + /* sanity checking here */ + if (exclude_file) { + check_absolute_path(exclude_file); + check_file_exists(exclude_file); + } + if (pidfile) { + check_absolute_path(pidfile); + } return true; }