X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fshared%2Futil.c;h=969ef2bb90a759409178ff109c29bc2c11a740fa;hb=0b9cc004a4dd20b32459615dd1ab98ba27202095;hp=08c0c2be131c2531c6168f1c1f8d62d9d1ab8c7e;hpb=565d91fdf198b88f7c2d72c67cfc6c30341a3596;p=elogind.git diff --git a/src/shared/util.c b/src/shared/util.c index 08c0c2be1..969ef2bb9 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -77,10 +77,6 @@ char **saved_argv = NULL; static volatile unsigned cached_columns = 0; static volatile unsigned cached_lines = 0; -bool is_efiboot(void) { - return access("/sys/firmware/efi", F_OK) >= 0; -} - size_t page_size(void) { static __thread size_t pgsz = 0; long r; @@ -561,9 +557,9 @@ int fchmod_umask(int fd, mode_t m) { } int write_one_line_file_atomic(const char *fn, const char *line) { - FILE *f; + _cleanup_fclose_ FILE *f = NULL; + _cleanup_free_ char *p = NULL; int r; - char *p; assert(fn); assert(line); @@ -585,12 +581,9 @@ int write_one_line_file_atomic(const char *fn, const char *line) { fflush(f); - if (ferror(f)) { - if (errno != 0) - r = -errno; - else - r = -EIO; - } else { + if (ferror(f)) + r = errno ? -errno : -EIO; + else { if (rename(p, fn) < 0) r = -errno; else @@ -601,9 +594,6 @@ finish: if (r < 0) unlink(p); - fclose(f); - free(p); - return r; } @@ -775,20 +765,19 @@ fail: return r; } -int load_env_file( - const char *fname, - char ***rl) { +int load_env_file(const char *fname, + char ***rl) { - FILE *f; + FILE _cleanup_fclose_ *f; char *b; - char *c = NULL; - char **m = NULL; - int r; + char _cleanup_free_ *c = NULL; + char _cleanup_strv_free_ **m = NULL; assert(fname); assert(rl); - if (!(f = fopen(fname, "re"))) + f = fopen(fname, "re"); + if (!f) return -errno; while (!feof(f)) { @@ -796,24 +785,19 @@ int load_env_file( char **t; if (!fgets(l, sizeof(l), f)) { - if(!feof(f)) { - r = -errno; - goto finish; - } + if (!feof(f)) + return -errno; else if (!c) break; - } cs = endswith(l, "\\\n"); if (cs) { - *cs = '\0'; b = strappend(c, l); - if (!b) { - r = log_oom(); - goto finish; - } + if (!b) + return log_oom(); + free(c); c = b; *l = '\0'; @@ -822,10 +806,9 @@ int load_env_file( if (c) { b = strappend(c, l); - if (!b) { - r = log_oom(); - goto finish; - } + if (!b) + return log_oom(); + free(c); c = b; } @@ -838,39 +821,27 @@ int load_env_file( if (strchr(COMMENTS, *p)) continue; - if (!(u = normalize_env_assignment(p))) { - r = log_oom(); - goto finish; - } + u = normalize_env_assignment(p); + if (!u) + return log_oom(); + free(c); c = NULL; t = strv_append(m, u); free(u); - if (!t) { - r = log_oom(); - goto finish; - } + if (!t) + return log_oom(); strv_free(m); m = t; } - r = 0; - *rl = m; m = NULL; -finish: - if (f) - fclose(f); - - free(c); - - strv_free(m); - - return r; + return 0; } int write_env_file(const char *fname, char **l) { @@ -5582,6 +5553,11 @@ void fclosep(FILE **f) { fclose(*f); } +void pclosep(FILE **f) { + if (*f) + pclose(*f); +} + void closep(int *fd) { if (*fd >= 0) close_nointr_nofail(*fd); @@ -5632,6 +5608,27 @@ bool string_is_safe(const char *p) { return true; } +bool path_is_safe(const char *p) { + + if (isempty(p)) + return false; + + if (streq(p, "..") || startswith(p, "../") || endswith(p, "/..") || strstr(p, "/../")) + return false; + + if (strlen(p) > PATH_MAX) + return false; + + /* The following two checks are not really dangerous, but hey, they still are confusing */ + if (streq(p, ".") || startswith(p, "./") || endswith(p, "/.") || strstr(p, "/./")) + return false; + + if (strstr(p, "//")) + return false; + + return true; +} + /* hey glibc, APIs with callbacks without a user pointer are so useless */ void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size, int (*compar) (const void *, const void *, void *), void *arg) {