From: David Lawrence Ramsey Date: Sun, 5 Jun 2005 19:33:27 +0000 (+0000) Subject: various umask-related cleanups X-Git-Tag: v1.3.8~208 X-Git-Url: https://git.wh0rd.org/?a=commitdiff_plain;h=6fdc81ae0477575ef34945b5dbaa637b9abb8d40;p=nano.git various umask-related cleanups git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@2595 35c25a1d-7b9e-4130-9fde-d3aeb78583b8 --- diff --git a/src/files.c b/src/files.c index 555baded..289264df 100644 --- a/src/files.c +++ b/src/files.c @@ -1139,17 +1139,18 @@ char *check_writable_directory(const char *path) char *safe_tempfile(FILE **f) { char *full_tempdir = NULL; - const char *TMPDIR_env; - int filedesc; + const char *tmpdir_env; + int fd; + mode_t original_umask = 0; assert(f != NULL); /* If $TMPDIR is set and non-empty, set tempdir to it, run it * through get_full_path(), and save the result in full_tempdir. * Otherwise, leave full_tempdir set to NULL. */ - TMPDIR_env = getenv("TMPDIR"); - if (TMPDIR_env != NULL && TMPDIR_env[0] != '\0') - full_tempdir = check_writable_directory(TMPDIR_env); + tmpdir_env = getenv("TMPDIR"); + if (tmpdir_env != NULL && tmpdir_env[0] != '\0') + full_tempdir = check_writable_directory(tmpdir_env); /* If $TMPDIR is unset, empty, or not a writable directory, and * full_tempdir is NULL, try P_tmpdir instead. */ @@ -1162,15 +1163,21 @@ char *safe_tempfile(FILE **f) full_tempdir = charealloc(full_tempdir, strlen(full_tempdir) + 12); strcat(full_tempdir, "nano.XXXXXX"); - filedesc = mkstemp(full_tempdir); - if (filedesc != -1) - *f = fdopen(filedesc, "r+b"); + original_umask = umask(0); + umask(S_IRWXG | S_IRWXO); + + fd = mkstemp(full_tempdir); + + if (fd != -1) + *f = fdopen(fd, "r+b"); else { free(full_tempdir); full_tempdir = NULL; } + umask(original_umask); + return full_tempdir; } @@ -1516,13 +1523,13 @@ int write_file(const char *name, FILE *f_open, bool tmp, int append, if (f_open == NULL) { original_umask = umask(0); - umask(original_umask); /* If we create a temp file, we don't let anyone else access it. - * We create a temp file if tmp is TRUE or if we're - * prepending. */ - if (tmp || append == 2) + * We create a temp file if tmp is TRUE. */ + if (tmp) umask(S_IRWXG | S_IRWXO); + else + umask(original_umask); } /* If we're prepending, copy the file to a temp file. */