bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2007-2018 Dovecot authors, see the included COPYING file */
128ab2c52a29068be87e12ab5aebbb8fdc933adfTimo Sirainen
128ab2c52a29068be87e12ab5aebbb8fdc933adfTimo Sirainen#include "lib.h"
128ab2c52a29068be87e12ab5aebbb8fdc933adfTimo Sirainen#include "str.h"
128ab2c52a29068be87e12ab5aebbb8fdc933adfTimo Sirainen#include "hex-binary.h"
128ab2c52a29068be87e12ab5aebbb8fdc933adfTimo Sirainen#include "randgen.h"
128ab2c52a29068be87e12ab5aebbb8fdc933adfTimo Sirainen#include "hostpid.h"
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen#include "eacces-error.h"
128ab2c52a29068be87e12ab5aebbb8fdc933adfTimo Sirainen#include "safe-mkstemp.h"
128ab2c52a29068be87e12ab5aebbb8fdc933adfTimo Sirainen
128ab2c52a29068be87e12ab5aebbb8fdc933adfTimo Sirainen#include <unistd.h>
128ab2c52a29068be87e12ab5aebbb8fdc933adfTimo Sirainen#include <fcntl.h>
128ab2c52a29068be87e12ab5aebbb8fdc933adfTimo Sirainen#include <sys/stat.h>
128ab2c52a29068be87e12ab5aebbb8fdc933adfTimo Sirainen
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Sirainenstatic int ATTR_NULL(5)
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainensafe_mkstemp_full(string_t *prefix, mode_t mode, uid_t uid, gid_t gid,
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen const char *gid_origin)
128ab2c52a29068be87e12ab5aebbb8fdc933adfTimo Sirainen{
128ab2c52a29068be87e12ab5aebbb8fdc933adfTimo Sirainen size_t prefix_len;
128ab2c52a29068be87e12ab5aebbb8fdc933adfTimo Sirainen struct stat st;
128ab2c52a29068be87e12ab5aebbb8fdc933adfTimo Sirainen unsigned char randbuf[8];
4ded3d18fa391ae5908f9834f1390cf55e8c99d5Timo Sirainen mode_t old_umask;
128ab2c52a29068be87e12ab5aebbb8fdc933adfTimo Sirainen int fd;
128ab2c52a29068be87e12ab5aebbb8fdc933adfTimo Sirainen
128ab2c52a29068be87e12ab5aebbb8fdc933adfTimo Sirainen prefix_len = str_len(prefix);
128ab2c52a29068be87e12ab5aebbb8fdc933adfTimo Sirainen for (;;) {
128ab2c52a29068be87e12ab5aebbb8fdc933adfTimo Sirainen do {
2a628a8c90839439baff5b45116f89f2b3cd9e37Aki Tuomi random_fill(randbuf, sizeof(randbuf));
128ab2c52a29068be87e12ab5aebbb8fdc933adfTimo Sirainen str_truncate(prefix, prefix_len);
128ab2c52a29068be87e12ab5aebbb8fdc933adfTimo Sirainen str_append(prefix,
128ab2c52a29068be87e12ab5aebbb8fdc933adfTimo Sirainen binary_to_hex(randbuf, sizeof(randbuf)));
128ab2c52a29068be87e12ab5aebbb8fdc933adfTimo Sirainen } while (lstat(str_c(prefix), &st) == 0);
128ab2c52a29068be87e12ab5aebbb8fdc933adfTimo Sirainen
128ab2c52a29068be87e12ab5aebbb8fdc933adfTimo Sirainen if (errno != ENOENT) {
128ab2c52a29068be87e12ab5aebbb8fdc933adfTimo Sirainen i_error("stat(%s) failed: %m", str_c(prefix));
3f442e48e5708f2f80cc937cd722c56a4fbd1c70Timo Sirainen str_truncate(prefix, prefix_len);
128ab2c52a29068be87e12ab5aebbb8fdc933adfTimo Sirainen return -1;
128ab2c52a29068be87e12ab5aebbb8fdc933adfTimo Sirainen }
128ab2c52a29068be87e12ab5aebbb8fdc933adfTimo Sirainen
4ded3d18fa391ae5908f9834f1390cf55e8c99d5Timo Sirainen old_umask = umask(0666 ^ mode);
4ded3d18fa391ae5908f9834f1390cf55e8c99d5Timo Sirainen fd = open(str_c(prefix), O_RDWR | O_EXCL | O_CREAT, 0666);
4ded3d18fa391ae5908f9834f1390cf55e8c99d5Timo Sirainen umask(old_umask);
128ab2c52a29068be87e12ab5aebbb8fdc933adfTimo Sirainen if (fd != -1)
128ab2c52a29068be87e12ab5aebbb8fdc933adfTimo Sirainen break;
128ab2c52a29068be87e12ab5aebbb8fdc933adfTimo Sirainen
128ab2c52a29068be87e12ab5aebbb8fdc933adfTimo Sirainen if (errno != EEXIST) {
ba153863e55d75a7c2f28c9c010a905b8887b62bTimo Sirainen if (errno != ENOENT && errno != EACCES)
b3a069922c8150a1cb14ec7683444f60dee98b55Timo Sirainen i_error("open(%s) failed: %m", str_c(prefix));
3f442e48e5708f2f80cc937cd722c56a4fbd1c70Timo Sirainen str_truncate(prefix, prefix_len);
128ab2c52a29068be87e12ab5aebbb8fdc933adfTimo Sirainen return -1;
128ab2c52a29068be87e12ab5aebbb8fdc933adfTimo Sirainen }
128ab2c52a29068be87e12ab5aebbb8fdc933adfTimo Sirainen }
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen if (uid == (uid_t)-1 && gid == (gid_t)-1)
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen return fd;
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen if (fchown(fd, uid, gid) < 0) {
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen if (errno == EPERM) {
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen i_error("%s", eperm_error_get_chgrp("fchown",
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen str_c(prefix), gid, gid_origin));
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen } else {
7b9f0c6aba07358e5520dc60c058126a4cae4056Timo Sirainen i_error("fchown(%s, %ld, %ld) failed: %m",
7b9f0c6aba07358e5520dc60c058126a4cae4056Timo Sirainen str_c(prefix),
7b9f0c6aba07358e5520dc60c058126a4cae4056Timo Sirainen uid == (uid_t)-1 ? -1L : (long)uid,
7b9f0c6aba07358e5520dc60c058126a4cae4056Timo Sirainen gid == (gid_t)-1 ? -1L : (long)gid);
128ab2c52a29068be87e12ab5aebbb8fdc933adfTimo Sirainen }
4307c886579381dbb1897ea1388ae6978c96f560Timo Sirainen i_close_fd(&fd);
46b823ac3bce2c0f9f0fc73911e48d3a77b04fbeTimo Sirainen i_unlink(str_c(prefix));
033e315cc9396332a09b98bee7824b8e6641c2fcTimo Sirainen str_truncate(prefix, prefix_len);
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen return -1;
128ab2c52a29068be87e12ab5aebbb8fdc933adfTimo Sirainen }
128ab2c52a29068be87e12ab5aebbb8fdc933adfTimo Sirainen return fd;
128ab2c52a29068be87e12ab5aebbb8fdc933adfTimo Sirainen}
128ab2c52a29068be87e12ab5aebbb8fdc933adfTimo Sirainen
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainenint safe_mkstemp(string_t *prefix, mode_t mode, uid_t uid, gid_t gid)
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen{
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen return safe_mkstemp_full(prefix, mode, uid, gid, NULL);
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen}
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainenint safe_mkstemp_group(string_t *prefix, mode_t mode,
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen gid_t gid, const char *gid_origin)
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen{
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen return safe_mkstemp_full(prefix, mode, (uid_t)-1, gid, gid_origin);
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen}
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen
128ab2c52a29068be87e12ab5aebbb8fdc933adfTimo Sirainenint safe_mkstemp_hostpid(string_t *prefix, mode_t mode, uid_t uid, gid_t gid)
128ab2c52a29068be87e12ab5aebbb8fdc933adfTimo Sirainen{
033e315cc9396332a09b98bee7824b8e6641c2fcTimo Sirainen size_t orig_prefix_len = str_len(prefix);
033e315cc9396332a09b98bee7824b8e6641c2fcTimo Sirainen int fd;
033e315cc9396332a09b98bee7824b8e6641c2fcTimo Sirainen
128ab2c52a29068be87e12ab5aebbb8fdc933adfTimo Sirainen str_printfa(prefix, "%s.%s.", my_hostname, my_pid);
033e315cc9396332a09b98bee7824b8e6641c2fcTimo Sirainen if ((fd = safe_mkstemp(prefix, mode, uid, gid)) == -1)
033e315cc9396332a09b98bee7824b8e6641c2fcTimo Sirainen str_truncate(prefix, orig_prefix_len);
033e315cc9396332a09b98bee7824b8e6641c2fcTimo Sirainen return fd;
128ab2c52a29068be87e12ab5aebbb8fdc933adfTimo Sirainen}
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainenint safe_mkstemp_hostpid_group(string_t *prefix, mode_t mode,
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen gid_t gid, const char *gid_origin)
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen{
033e315cc9396332a09b98bee7824b8e6641c2fcTimo Sirainen size_t orig_prefix_len = str_len(prefix);
033e315cc9396332a09b98bee7824b8e6641c2fcTimo Sirainen int fd;
033e315cc9396332a09b98bee7824b8e6641c2fcTimo Sirainen
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen str_printfa(prefix, "%s.%s.", my_hostname, my_pid);
033e315cc9396332a09b98bee7824b8e6641c2fcTimo Sirainen if ((fd = safe_mkstemp_group(prefix, mode, gid, gid_origin)) == -1)
033e315cc9396332a09b98bee7824b8e6641c2fcTimo Sirainen str_truncate(prefix, orig_prefix_len);
033e315cc9396332a09b98bee7824b8e6641c2fcTimo Sirainen return fd;
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen}