Code Coverage: vinyl-cache/vmod/vmod_unix.c
 
82217aa4fc7094b3610340078afaf78cd95a8ec9
 
0
/*-
1
 * Copyright 2018 UPLEX - Nils Goroll Systemoptimierung
2
 * All rights reserved.
3
 *
4
 * Authors: Geoffrey Simmons <geoffrey.simmons@uplex.de>
5
 *
6
 * SPDX-License-Identifier: BSD-2-Clause
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions are met:
10
 * 1. Redistributions of source code must retain the above copyright notice,
11
 *    this list of conditions and the following disclaimer.
12
 * 2. Redistributions in binary form must reproduce the above copyright notice,
13
 *    this list of conditions and the following disclaimer in the documentation
14
 *    and/or other materials provided with the distribution.
15
 *
16
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
17
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
 * DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
20
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 *
27
 */
28
29
#include "config.h"
30
31
#include <pwd.h>
32
#include <grp.h>
33
#include <string.h>
34
35
#include "cache/cache.h"
36
#include "vcl.h"
37
#include "common/heritage.h"
38
39
#include "vmod_unix_cred_compat.h"
40
#include "vcc_unix_if.h"
41
42
#define FAIL(ctx, msg) \
43 0
        VRT_fail((ctx), "vmod unix failure: " msg)
44
45
#define ERR(ctx, msg) \
46 8
        VSLb((ctx)->vsl, SLT_VCL_Error, "vmod unix error: " msg)
47
48
#define VERR(ctx, fmt, ...) \
49 0
        VSLb((ctx)->vsl, SLT_VCL_Error, "vmod unix error: " fmt, __VA_ARGS__)
50
51
#define ERRNOTUDS(ctx) \
52 8
        ERR((ctx), "not listening on a Unix domain socket")
53
54
#define FAIL_SUPPORT(ctx) \
55 0
        FAIL((ctx), "not supported on this platform")
56
57
#define ERRNOCREDS(ctx) \
58 0
        VERR((ctx), "could not read peer credentials: %s", strerror(errno))
59
60
#define ERRNOMEM(ctx) \
61 0
        ERR((ctx), "out of space")
62
63
static struct sess *
64
get_sp(VRT_CTX)
65 16
{
66 16
        struct sess *sp;
67
68 16
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
69
70 16
        if (ctx->req) {
71 8
                CHECK_OBJ(ctx->req, REQ_MAGIC);
72 8
                sp = ctx->req->sp;
73 8
        } else {
74 8
                CHECK_OBJ_NOTNULL(ctx->bo, BUSYOBJ_MAGIC);
75 8
                sp = ctx->bo->sp;
76 8
        }
77 16
        CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
78 16
        CHECK_OBJ_NOTNULL(sp->listen_sock, LISTEN_SOCK_MAGIC);
79 16
        return (sp);
80 16
}
81
82
#define NUM_FUNC(func)                                  \
83
VCL_INT                                                 \
84 16
vmod_##func(VRT_CTX)                                    \
85 16
{                                                       \
86 16
        struct sess *sp;                                \
87 16
        uid_t uid;                                      \
88 16
        gid_t gid;                                      \
89 16
        int ret;                                        \
90 16
                                                        \
91 16
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);          \
92 16
        AZ(ctx->method & VCL_MET_TASK_H);       \
93 16
                                                        \
94 16
        sp = get_sp(ctx);                               \
95 16
        if (!sp->listen_sock->uds) {                    \
96 8
                ERRNOTUDS(ctx);                         \
97 8
                return (-1);                            \
98 8
        }                                               \
99 16
                                                        \
100 16
        ret = get_ids(sp->fd, &uid, &gid);              \
101 8
        if (ret == 0)                                   \
102 8
                return (func);                          \
103 8
                                                        \
104 8
        if (ret == NOT_SUPPORTED)                       \
105 0
                FAIL_SUPPORT(ctx);                      \
106 0
        else if (ret == CREDS_FAIL)                     \
107 0
                ERRNOCREDS(ctx);                        \
108 0
        return (-1);                                    \
109 8
}
110
111
NUM_FUNC(uid)
112
NUM_FUNC(gid)
113
114
#define NAME_FUNC(func, type, get, id, fld)                     \
115
VCL_STRING                                                      \
116 8
vmod_##func(VRT_CTX)                                            \
117 8
{                                                               \
118 8
        struct type *s;                                         \
119 8
        VCL_INT ret;                                            \
120 8
        VCL_STRING name;                                        \
121 8
                                                                \
122 8
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);                  \
123 8
        ret = vmod_##id(ctx);                                   \
124 8
        if (ret == -1)                                          \
125 8
                return (NULL);                                  \
126 8
                                                                \
127 8
        errno = 0;                                              \
128 4
        s = get((id##_t) ret);                                  \
129 4
        if (s == NULL) {                                        \
130 0
                ERRNOCREDS(ctx);                                \
131 0
                return (NULL);                                  \
132 0
        }                                                       \
133 4
        if ((name = WS_Copy(ctx->ws, s->fld, -1)) == NULL) {    \
134 0
                ERRNOMEM(ctx);                                  \
135 0
                return (NULL);                                  \
136 0
        }                                                       \
137 4
        return (name);                                          \
138 4
}
139
140 4
NAME_FUNC(user, passwd, getpwuid, uid, pw_name)
141
NAME_FUNC(group, group, getgrgid, gid, gr_name)