Create a secure temp path instead of a predictable one (Thanks Han)

With this commit, i3 will now use either $XDG_RUNTIME_DIR/i3 (XDG_RUNTIME_DIR
is only writable by the user, so this is not a problem) or a secure temporary
location in /tmp, following the pattern /tmp/i3-<user>.XXXXXX
This commit is contained in:
Michael Stapelberg
2011-12-18 17:53:21 +00:00
parent 7eb2ca405e
commit c21172a6f6
5 changed files with 51 additions and 23 deletions

View File

@ -234,25 +234,36 @@ static char **append_argument(char **original, char *argument) {
*
*/
char *get_process_filename(const char *prefix) {
char *dir = getenv("XDG_RUNTIME_DIR");
/* dir stores the directory path for this and all subsequent calls so that
* we only create a temporary directory once per i3 instance. */
static char *dir = NULL;
if (dir == NULL) {
struct passwd *pw = getpwuid(getuid());
const char *username = pw ? pw->pw_name : "unknown";
sasprintf(&dir, "/tmp/i3-%s", username);
} else {
char *tmp;
sasprintf(&tmp, "%s/i3", dir);
dir = tmp;
}
if (!path_exists(dir)) {
if (mkdir(dir, 0700) == -1) {
perror("mkdir()");
return NULL;
/* Check if XDG_RUNTIME_DIR is set. If so, we use XDG_RUNTIME_DIR/i3 */
if ((dir = getenv("XDG_RUNTIME_DIR"))) {
char *tmp;
sasprintf(&tmp, "%s/i3", dir);
dir = tmp;
if (!path_exists(dir)) {
if (mkdir(dir, 0700) == -1) {
perror("mkdir()");
return NULL;
}
}
} else {
/* If not, we create a (secure) temp directory using the template
* /tmp/i3-<user>.XXXXXX */
struct passwd *pw = getpwuid(getuid());
const char *username = pw ? pw->pw_name : "unknown";
sasprintf(&dir, "/tmp/i3-%s.XXXXXX", username);
/* mkdtemp modifies dir */
if (mkdtemp(dir) == NULL) {
perror("mkdtemp()");
return NULL;
}
}
}
char *filename;
sasprintf(&filename, "%s/%s.%d", dir, prefix, getpid());
free(dir);
return filename;
}