summaryrefslogtreecommitdiff
path: root/src/core/privilege.c
blob: 254dbc6c2a71792b07601e298cf35c976e1b05f6 (plain)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#if defined(__linux__)
#define _DEFAULT_SOURCE
#endif

#include "hybbx/privilege.h"
#include "hybbx/config.h"
#include "hybbx/log.h"
#include "hybbx/types.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#if !defined(_WIN32)
#include <errno.h>
#include <grp.h>
#include <pwd.h>
#include <sys/types.h>
#include <unistd.h>
#endif

#if !defined(_WIN32)

static int parse_optional_uid(const char *raw, uid_t *out)
{
    char *end;
    unsigned long value;

    if (raw == NULL || raw[0] == '\0') {
        return 0;
    }
    errno = 0;
    value = strtoul(raw, &end, 10);
    if (end == raw || *end != '\0' || errno != 0) {
        return -1;
    }
    *out = (uid_t)value;
    return 1;
}

static int parse_optional_gid(const char *raw, gid_t *out)
{
    char *end;
    unsigned long value;

    if (raw == NULL || raw[0] == '\0') {
        return 0;
    }
    errno = 0;
    value = strtoul(raw, &end, 10);
    if (end == raw || *end != '\0' || errno != 0) {
        return -1;
    }
    *out = (gid_t)value;
    return 1;
}

static hybbx_result_t resolve_target(
    const hybbx_config_t *config,
    uid_t *uid_out,
    gid_t *gid_out,
    char name_buf[64],
    size_t name_buf_len)
{
    const char *user_name;
    const char *group_name;
    const char *uid_raw;
    const char *gid_raw;
    struct passwd *pw = NULL;
    struct group *gr = NULL;
    uid_t uid_override = 0;
    gid_t gid_override = 0;
    int have_uid = 0;
    int have_gid = 0;

    user_name = hybbx_config_get(config, "service", "user", NULL);
    group_name = hybbx_config_get(config, "service", "group", NULL);
    uid_raw = hybbx_config_get(config, "service", "uid", NULL);
    gid_raw = hybbx_config_get(config, "service", "gid", NULL);

    have_uid = parse_optional_uid(uid_raw, &uid_override);
    if (have_uid < 0) {
        hybbx_log_warn("[service] invalid uid=%s", uid_raw);
        return HYBBX_ERR_INVALID;
    }
    have_gid = parse_optional_gid(gid_raw, &gid_override);
    if (have_gid < 0) {
        hybbx_log_warn("[service] invalid gid=%s", gid_raw);
        return HYBBX_ERR_INVALID;
    }

    if (user_name != NULL && user_name[0] != '\0') {
        pw = getpwnam(user_name);
        if (pw == NULL) {
            hybbx_log_warn("[service] unknown user=%s", user_name);
            return HYBBX_ERR_INVALID;
        }
        *uid_out = pw->pw_uid;
        *gid_out = pw->pw_gid;
        if (name_buf_len > 0) {
            snprintf(name_buf, name_buf_len, "%s", pw->pw_name);
        }
    } else if (have_uid) {
        pw = getpwuid(uid_override);
        if (pw == NULL) {
            hybbx_log_warn("[service] unknown uid=%s", uid_raw);
            return HYBBX_ERR_INVALID;
        }
        *uid_out = pw->pw_uid;
        *gid_out = pw->pw_gid;
        if (name_buf_len > 0) {
            snprintf(name_buf, name_buf_len, "%s", pw->pw_name);
        }
    } else {
        return HYBBX_ERR_INVALID;
    }

    if (have_uid) {
        *uid_out = uid_override;
    }
    if (group_name != NULL && group_name[0] != '\0') {
        gr = getgrnam(group_name);
        if (gr == NULL) {
            hybbx_log_warn("[service] unknown group=%s", group_name);
            return HYBBX_ERR_INVALID;
        }
        *gid_out = gr->gr_gid;
    } else if (have_gid) {
        if (getgrgid(gid_override) == NULL) {
            hybbx_log_warn("[service] unknown gid=%s", gid_raw);
            return HYBBX_ERR_INVALID;
        }
        *gid_out = gid_override;
    }

    return HYBBX_OK;
}

static hybbx_result_t drop_to(uid_t uid, gid_t gid, const char *name)
{
    if (getuid() != 0) {
        hybbx_log_warn("[service] privilege drop requires root start (euid=0)");
        return HYBBX_ERR_DENIED;
    }

    if (name != NULL && name[0] != '\0') {
        if (initgroups(name, gid) != 0) {
            hybbx_log_warn("[service] initgroups(%s) failed: %s",
                            name, strerror(errno));
            return HYBBX_ERR_IO;
        }
    }

    if (setgid(gid) != 0) {
        hybbx_log_warn("[service] setgid(%u) failed: %s",
                        (unsigned)gid, strerror(errno));
        return HYBBX_ERR_IO;
    }

    if (setuid(uid) != 0) {
        hybbx_log_warn("[service] setuid(%u) failed: %s",
                        (unsigned)uid, strerror(errno));
        return HYBBX_ERR_IO;
    }

    if (setuid(0) == 0 || seteuid(0) == 0) {
        hybbx_log_warn("[service] privilege drop incomplete — still root");
        return HYBBX_ERR_IO;
    }

    hybbx_log_info("[service] dropped privileges to uid=%u gid=%u (%s)",
                   (unsigned)uid, (unsigned)gid,
                   name != NULL && name[0] != '\0' ? name : "?");
    return HYBBX_OK;
}

#endif /* !_WIN32 */

hybbx_result_t hybbx_privilege_apply_from_config(const hybbx_config_t *config)
{
#if defined(_WIN32)
    (void)config;
    return HYBBX_OK;
#else
    const char *user_name;
    uid_t target_uid = 0;
    gid_t target_gid = 0;
    char resolved_name[64];
    hybbx_result_t rc;

    if (config == NULL) {
        return HYBBX_ERR_INVALID;
    }

    resolved_name[0] = '\0';
    user_name = hybbx_config_get(config, "service", "user", NULL);

    if (geteuid() == 0) {
        if ((user_name == NULL || user_name[0] == '\0') &&
            hybbx_config_get(config, "service", "uid", NULL) == NULL) {
            hybbx_log_warn(
                "[service] refusing to run as root — set [service] user= "
                "(and optional group=)");
            return HYBBX_ERR_DENIED;
        }

        rc = resolve_target(config, &target_uid, &target_gid,
                            resolved_name, sizeof(resolved_name));
        if (rc != HYBBX_OK) {
            return rc;
        }

        return drop_to(target_uid, target_gid,
                       resolved_name[0] != '\0' ? resolved_name : user_name);
    }

    if (user_name != NULL && user_name[0] != '\0') {
        struct passwd *pw = getpwnam(user_name);

        if (pw == NULL) {
            hybbx_log_warn("[service] unknown configured user=%s", user_name);
            return HYBBX_OK;
        }
        if ((uid_t)geteuid() != pw->pw_uid) {
            hybbx_log_warn(
                "[service] running as uid=%u but [service] user=%s (uid=%u)",
                (unsigned)geteuid(), user_name, (unsigned)pw->pw_uid);
        }
    }

    return HYBBX_OK;
#endif
}
git clone -b <branch> https://cgit.mode42.com/<repo>.git
git clone -b <branch> git://cgit.mode42.com/<repo>.git

info@mode42.com