aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Common/assertProcessUserMatchesDataOwner.cpp
blob: f2557a4aaaf0ee0f039aa2cdd16412752637ff19 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <Common/assertProcessUserMatchesDataOwner.h>
#include <Common/logger_useful.h>
#include <Common/Exception.h>
#include <sys/stat.h>
#include <unistd.h>
#include <pwd.h>


namespace DB
{

namespace ErrorCodes
{
    extern const int FAILED_TO_GETPWUID;
    extern const int MISMATCHING_USERS_FOR_PROCESS_AND_DATA;
}

namespace
{
    std::string getUserName(uid_t user_id)
    {
        /// Try to convert user id into user name.
        auto buffer_size = sysconf(_SC_GETPW_R_SIZE_MAX);
        if (buffer_size <= 0)
            buffer_size = 1024;
        std::string buffer;
        buffer.reserve(buffer_size);

        struct passwd passwd_entry;
        struct passwd * result = nullptr;
        const auto error = getpwuid_r(user_id, &passwd_entry, buffer.data(), buffer_size, &result);

        if (error)
            throwFromErrno("Failed to find user name for " + std::to_string(user_id), ErrorCodes::FAILED_TO_GETPWUID, error);
        else if (result)
            return result->pw_name;
        return std::to_string(user_id);
    }
}

void assertProcessUserMatchesDataOwner(const std::string & path, std::function<void(const std::string &)> on_warning)
{
    /// Check that the process user id matches the owner of the data.
    const auto effective_user_id = geteuid();
    struct stat statbuf;
    if (stat(path.c_str(), &statbuf) == 0 && effective_user_id != statbuf.st_uid)
    {
        const auto effective_user = getUserName(effective_user_id);
        const auto data_owner = getUserName(statbuf.st_uid);
        std::string message = fmt::format(
            "Effective user of the process ({}) does not match the owner of the data ({}).",
            effective_user, data_owner);

        if (effective_user_id == 0)
        {
            message += fmt::format(" Run under 'sudo -u {}'.", data_owner);
            throw Exception(ErrorCodes::MISMATCHING_USERS_FOR_PROCESS_AND_DATA, "{}", message);
        }
        else
        {
            on_warning(message);
        }
    }
}

}