Mercurial > dovecot > core-2.2
view src/lib/safe-mkdir.c @ 1741:9df02b1533b3 HEAD
Removed most of the license comments from src/lib/*.c. It's just fine to
keep them in a single COPYING.MIT file. Changed a few other comments as well.
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Wed, 27 Aug 2003 00:18:16 +0300 |
parents | 500ec0ec3b85 |
children | 05427d8112ee |
line wrap: on
line source
/* Copyright (c) 2002-2003 Timo Sirainen */ #include "lib.h" #include "safe-mkdir.h" #include <sys/stat.h> #include <unistd.h> #include <fcntl.h> int safe_mkdir(const char *dir, mode_t mode, uid_t uid, gid_t gid) { struct stat st; int fd, ret = 1; if (lstat(dir, &st) < 0) { if (errno != ENOENT) i_fatal("lstat() failed for %s: %m", dir); if (mkdir(dir, mode) < 0) i_fatal("Can't create directory %s: %m", dir); } else { /* already exists. */ ret = 2; } /* use fchown() and fchmod() just to make sure we aren't following symbolic links. */ fd = open(dir, O_RDONLY); if (fd == -1) i_fatal("open() failed for %s: %m", dir); if (fstat(fd, &st) < 0) i_fatal("fstat() failed for %s: %m", dir); if (!S_ISDIR(st.st_mode) || S_ISLNK(st.st_mode)) i_fatal("Not a directory %s", dir); if (st.st_uid != uid || st.st_gid != gid) { if (fchown(fd, uid, gid) < 0) i_fatal("fchown() failed for %s: %m", dir); ret = 0; } if ((st.st_mode & 07777) != mode) { if (fchmod(fd, mode) < 0) i_fatal("chmod() failed for %s: %m", dir); ret = 0; } if (close(fd) < 0) i_fatal("close() failed for %s: %m", dir); /* make sure we succeeded in everything. chown() and chmod() are racy: user owned 0777 file - change either and the user can still change it back. */ if (lstat(dir, &st) < 0) i_fatal("lstat() check failed for %s: %m", dir); if (!S_ISDIR(st.st_mode) || S_ISLNK(st.st_mode)) i_fatal("Not a directory %s", dir); if ((st.st_mode & 07777) != mode) { i_fatal("safe_mkdir() failed: %s (%o) is still not mode %o", dir, (int)st.st_mode, (int)mode); } if (st.st_uid != uid || st.st_gid != gid) { i_fatal("safe_mkdir() failed: %s (%s, %s) " "is still not owned by %s.%s", dir, dec2str(st.st_uid), dec2str(st.st_gid), dec2str(uid), dec2str(gid)); } return ret; }