conf: create directory for Unix domain command socket

Try to create the directory where will be the Unix domain command socket
bound to allow starting with empty /var/run. Check the permissions and
owner/group in case the directory already existed. It MUST NOT be
accessible by others as permissions on Unix domain sockets are ignored
on some systems (e.g. Solaris).
This commit is contained in:
Miroslav Lichvar
2015-08-11 17:41:02 +02:00
parent 6d42dd8603
commit f1ed08abf0
3 changed files with 76 additions and 0 deletions

51
util.c
View File

@@ -897,6 +897,27 @@ UTI_SetQuitSignalsHandler(void (*handler)(int))
/* ================================================== */
char *
UTI_PathToDir(const char *path)
{
char *dir, *slash;
slash = strrchr(path, '/');
if (!slash)
return Strdup(".");
if (slash == path)
return Strdup("/");
dir = Malloc(slash - path + 1);
snprintf(dir, slash - path + 1, "%s", path);
return dir;
}
/* ================================================== */
static int
create_dir(char *p, mode_t mode, uid_t uid, gid_t gid)
{
@@ -986,3 +1007,33 @@ UTI_CreateDirAndParents(const char *path, mode_t mode, uid_t uid, gid_t gid)
Free(p);
return 1;
}
/* ================================================== */
int
UTI_CheckDirPermissions(const char *path, mode_t perm, uid_t uid, gid_t gid)
{
struct stat buf;
if (stat(path, &buf)) {
LOG(LOGS_ERR, LOGF_Util, "Could not access %s : %s", path, strerror(errno));
return 0;
}
if (!S_ISDIR(buf.st_mode)) {
LOG(LOGS_ERR, LOGF_Util, "%s is not directory", path);
return 0;
}
if ((buf.st_mode & 0777) & ~perm) {
LOG(LOGS_ERR, LOGF_Util, "Wrong permissions on %s", path);
return 0;
}
if (buf.st_uid != uid || buf.st_gid != gid) {
LOG(LOGS_ERR, LOGF_Util, "Wrong owner/group of %s", path);
return 0;
}
return 1;
}