| | varnish-cache/bin/varnishd/acceptor/mgt_acceptor_uds.c |
| 0 |
|
/*- |
| 1 |
|
* Copyright (c) 2006 Verdens Gang AS |
| 2 |
|
* Copyright (c) 2006-2023 Varnish Software AS |
| 3 |
|
* All rights reserved. |
| 4 |
|
* |
| 5 |
|
* Author: Poul-Henning Kamp <phk@phk.freebsd.dk> |
| 6 |
|
* Author: Asad Sajjad Ahmed <asadsa@varnish-software.com> |
| 7 |
|
* |
| 8 |
|
* SPDX-License-Identifier: BSD-2-Clause |
| 9 |
|
* |
| 10 |
|
* Redistribution and use in source and binary forms, with or without |
| 11 |
|
* modification, are permitted provided that the following conditions |
| 12 |
|
* are met: |
| 13 |
|
* 1. Redistributions of source code must retain the above copyright |
| 14 |
|
* notice, this list of conditions and the following disclaimer. |
| 15 |
|
* 2. Redistributions in binary form must reproduce the above copyright |
| 16 |
|
* notice, this list of conditions and the following disclaimer in the |
| 17 |
|
* documentation and/or other materials provided with the distribution. |
| 18 |
|
* |
| 19 |
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 20 |
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 21 |
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 22 |
|
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE |
| 23 |
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 24 |
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 25 |
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 26 |
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 27 |
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 28 |
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 29 |
|
* SUCH DAMAGE. |
| 30 |
|
* |
| 31 |
|
* Acceptor socket management |
| 32 |
|
*/ |
| 33 |
|
|
| 34 |
|
#include "config.h" |
| 35 |
|
|
| 36 |
|
#include <sys/types.h> |
| 37 |
|
#include <sys/socket.h> |
| 38 |
|
#include <sys/un.h> |
| 39 |
|
#include <sys/stat.h> |
| 40 |
|
#include <stdio.h> |
| 41 |
|
#include <stdlib.h> |
| 42 |
|
#include <string.h> |
| 43 |
|
#include <unistd.h> |
| 44 |
|
#include <pwd.h> |
| 45 |
|
#include <grp.h> |
| 46 |
|
|
| 47 |
|
#include "mgt/mgt.h" |
| 48 |
|
#include "common/heritage.h" |
| 49 |
|
|
| 50 |
|
#include "acceptor/cache_acceptor.h" |
| 51 |
|
#include "acceptor/acceptor_uds.h" |
| 52 |
|
#include "acceptor/mgt_acceptor.h" |
| 53 |
|
|
| 54 |
|
#include "vsa.h" |
| 55 |
|
#include "vus.h" |
| 56 |
|
|
| 57 |
|
int |
| 58 |
41240 |
vca_uds_config(void) |
| 59 |
|
{ |
| 60 |
|
|
| 61 |
41240 |
return (0); |
| 62 |
|
} |
| 63 |
|
|
| 64 |
|
static int |
| 65 |
2520 |
vca_vus_bind(void *priv, const struct sockaddr_un *uds) |
| 66 |
|
{ |
| 67 |
2520 |
return (VUS_bind(uds, priv)); |
| 68 |
|
} |
| 69 |
|
|
| 70 |
|
static int |
| 71 |
2520 |
vca_uds_opensocket(struct listen_sock *ls) |
| 72 |
|
{ |
| 73 |
|
int fail; |
| 74 |
|
const char *err; |
| 75 |
|
|
| 76 |
2520 |
CHECK_OBJ_NOTNULL(ls, LISTEN_SOCK_MAGIC); |
| 77 |
|
|
| 78 |
2520 |
if (ls->sock > 0) { |
| 79 |
1240 |
MCH_Fd_Inherit(ls->sock, NULL); |
| 80 |
1240 |
closefd(&ls->sock); |
| 81 |
1240 |
} |
| 82 |
|
|
| 83 |
2520 |
ls->sock = VUS_resolver(ls->endpoint, vca_vus_bind, NULL, &err); |
| 84 |
2520 |
fail = errno; |
| 85 |
|
|
| 86 |
2520 |
if (ls->sock < 0) { |
| 87 |
0 |
AN(fail); |
| 88 |
0 |
return (fail); |
| 89 |
|
} |
| 90 |
|
|
| 91 |
2520 |
if (ls->perms != NULL) { |
| 92 |
160 |
CHECK_OBJ(ls->perms, UDS_PERMS_MAGIC); |
| 93 |
160 |
assert(ls->uds); |
| 94 |
160 |
errno = 0; |
| 95 |
160 |
if (ls->perms->mode != 0 && |
| 96 |
160 |
chmod(ls->endpoint, ls->perms->mode) != 0) |
| 97 |
0 |
return (errno); |
| 98 |
160 |
if (chown(ls->endpoint, ls->perms->uid, ls->perms->gid) != 0) |
| 99 |
0 |
return (errno); |
| 100 |
160 |
} |
| 101 |
|
|
| 102 |
2520 |
MCH_Fd_Inherit(ls->sock, "sock"); |
| 103 |
2520 |
return (0); |
| 104 |
2520 |
} |
| 105 |
|
|
| 106 |
|
static int v_matchproto_(vus_resolved_f) |
| 107 |
1320 |
vca_uds_open_cb(void *priv, const struct sockaddr_un *uds) |
| 108 |
|
{ |
| 109 |
|
struct listen_arg *la; |
| 110 |
|
struct listen_sock *ls; |
| 111 |
|
int fail; |
| 112 |
|
|
| 113 |
1320 |
CAST_OBJ_NOTNULL(la, priv, LISTEN_ARG_MAGIC); |
| 114 |
1320 |
(void) uds; |
| 115 |
|
|
| 116 |
1360 |
VTAILQ_FOREACH(ls, &UDS_acceptor.socks, vcalist) { |
| 117 |
80 |
CHECK_OBJ_NOTNULL(ls, LISTEN_SOCK_MAGIC); |
| 118 |
|
|
| 119 |
80 |
if (strcmp(ls->endpoint, la->endpoint) == 0) |
| 120 |
40 |
ARGV_ERR("-a arguments %s and %s have same address\n", |
| 121 |
|
ls->endpoint, la->endpoint); |
| 122 |
40 |
} |
| 123 |
|
|
| 124 |
1280 |
ALLOC_OBJ(ls, LISTEN_SOCK_MAGIC); |
| 125 |
1280 |
AN(ls); |
| 126 |
|
|
| 127 |
1280 |
ls->sock = -1; |
| 128 |
1280 |
ls->vca = &UDS_acceptor; |
| 129 |
|
|
| 130 |
1280 |
ls->addr = VSA_Clone(bogo_ip); |
| 131 |
1280 |
AN(ls->addr); |
| 132 |
|
|
| 133 |
1280 |
REPLACE(ls->endpoint, la->endpoint); |
| 134 |
1280 |
ls->name = la->name; |
| 135 |
1280 |
ls->transport = la->transport; |
| 136 |
1280 |
ls->perms = la->perms; |
| 137 |
1280 |
ls->uds = 1; |
| 138 |
|
|
| 139 |
1280 |
VJ_master(JAIL_MASTER_PRIVPORT); |
| 140 |
1280 |
fail = vca_uds_opensocket(ls); |
| 141 |
1280 |
VJ_master(JAIL_MASTER_LOW); |
| 142 |
|
|
| 143 |
1280 |
if (fail) { |
| 144 |
0 |
VSA_free(&ls->addr); |
| 145 |
0 |
free(ls->endpoint); |
| 146 |
0 |
FREE_OBJ(ls); |
| 147 |
0 |
if (fail != EAFNOSUPPORT) |
| 148 |
0 |
ARGV_ERR("Could not get socket %s: %s\n", |
| 149 |
|
la->endpoint, VAS_errtxt(fail)); |
| 150 |
0 |
return (0); |
| 151 |
|
} |
| 152 |
|
|
| 153 |
1280 |
VTAILQ_INSERT_TAIL(&la->socks, ls, arglist); |
| 154 |
1280 |
VTAILQ_INSERT_TAIL(&heritage.socks, ls, list); |
| 155 |
1280 |
VTAILQ_INSERT_TAIL(&UDS_acceptor.socks, ls, vcalist); |
| 156 |
|
|
| 157 |
1280 |
return (0); |
| 158 |
1280 |
} |
| 159 |
|
|
| 160 |
|
int |
| 161 |
2040 |
vca_uds_open(char **av, struct listen_arg *la, const char **err) |
| 162 |
|
{ |
| 163 |
|
|
| 164 |
2040 |
const struct transport *xp = NULL; |
| 165 |
2040 |
struct passwd *pwd = NULL; |
| 166 |
2040 |
struct group *grp = NULL; |
| 167 |
|
struct uds_perms *perms; |
| 168 |
2040 |
mode_t mode = 0; |
| 169 |
|
|
| 170 |
2040 |
CHECK_OBJ_NOTNULL(la, LISTEN_ARG_MAGIC); |
| 171 |
2040 |
AN(av); |
| 172 |
2040 |
AN(err); |
| 173 |
|
|
| 174 |
2040 |
if (*la->endpoint != '/' && strchr(la->endpoint, '/') != NULL) |
| 175 |
0 |
ARGV_ERR("Unix domain socket addresses must be" |
| 176 |
|
" absolute paths in -a (%s)\n", la->endpoint); |
| 177 |
|
|
| 178 |
2040 |
heritage.min_vcl_version = vmax(heritage.min_vcl_version, 41U); |
| 179 |
|
|
| 180 |
2320 |
for (int i = 0; av[i] != NULL; i++) { |
| 181 |
|
const char *val; |
| 182 |
|
|
| 183 |
960 |
if (strchr(av[i], '=') == NULL) { |
| 184 |
120 |
if (xp != NULL) |
| 185 |
0 |
ARGV_ERR("Too many protocol sub-args" |
| 186 |
|
" in -a (%s)\n", av[i]); |
| 187 |
120 |
xp = XPORT_Find(av[i]); |
| 188 |
120 |
if (xp == NULL) |
| 189 |
40 |
ARGV_ERR("Unknown protocol '%s'\n", av[i]); |
| 190 |
80 |
continue; |
| 191 |
|
} |
| 192 |
840 |
if (la->endpoint[0] != '/') |
| 193 |
40 |
ARGV_ERR("Invalid sub-arg %s" |
| 194 |
|
" in -a\n", av[i]); |
| 195 |
|
|
| 196 |
800 |
val = keyval(av[i], "user="); |
| 197 |
800 |
if (val != NULL && pwd != NULL) |
| 198 |
0 |
ARGV_ERR("Too many user sub-args in -a (%s)\n", av[i]); |
| 199 |
800 |
if (val != NULL) { |
| 200 |
40 |
pwd = getpwnam(val); |
| 201 |
40 |
if (pwd == NULL) |
| 202 |
0 |
ARGV_ERR("Unknown user %s in -a\n", val); |
| 203 |
40 |
continue; |
| 204 |
|
} |
| 205 |
|
|
| 206 |
760 |
val = keyval(av[i], "group="); |
| 207 |
760 |
if (val != NULL && grp != NULL) |
| 208 |
0 |
ARGV_ERR("Too many group sub-args in -a (%s)\n", av[i]); |
| 209 |
760 |
if (val != NULL) { |
| 210 |
40 |
grp = getgrnam(val); |
| 211 |
40 |
if (grp == NULL) |
| 212 |
0 |
ARGV_ERR("Unknown group %s in -a\n", val); |
| 213 |
40 |
continue; |
| 214 |
|
} |
| 215 |
|
|
| 216 |
720 |
val = keyval(av[i], "mode="); |
| 217 |
720 |
if (val != NULL && mode != 0) |
| 218 |
40 |
ARGV_ERR("Too many mode sub-args in -a (%s)\n", av[i]); |
| 219 |
680 |
if (val != NULL && *val == '\0') |
| 220 |
40 |
ARGV_ERR("Empty mode sub-arg in -a\n"); |
| 221 |
640 |
if (val != NULL) { |
| 222 |
|
long m; |
| 223 |
|
char *p; |
| 224 |
|
|
| 225 |
440 |
errno = 0; |
| 226 |
440 |
m = strtol(val, &p, 8); |
| 227 |
440 |
if (*p != '\0') |
| 228 |
120 |
ARGV_ERR("Invalid mode sub-arg %s in -a\n", |
| 229 |
|
val); |
| 230 |
320 |
if (errno) |
| 231 |
80 |
ARGV_ERR("Cannot parse mode sub-arg %s in -a: " |
| 232 |
|
"%s\n", val, VAS_errtxt(errno)); |
| 233 |
240 |
if (m <= 0 || m > 0777) |
| 234 |
120 |
ARGV_ERR("Mode sub-arg %s out of range in -a\n", |
| 235 |
|
val); |
| 236 |
120 |
mode = (mode_t) m; |
| 237 |
120 |
continue; |
| 238 |
|
} |
| 239 |
|
|
| 240 |
200 |
ARGV_ERR("Invalid sub-arg %s in -a\n", av[i]); |
| 241 |
0 |
} |
| 242 |
|
|
| 243 |
1360 |
if (xp == NULL) |
| 244 |
1280 |
xp = XPORT_Find("http"); |
| 245 |
|
|
| 246 |
1360 |
AN(xp); |
| 247 |
1360 |
la->transport = xp; |
| 248 |
|
|
| 249 |
1360 |
if (pwd != NULL || grp != NULL || mode != 0) { |
| 250 |
80 |
ALLOC_OBJ(perms, UDS_PERMS_MAGIC); |
| 251 |
80 |
AN(perms); |
| 252 |
80 |
if (pwd != NULL) |
| 253 |
40 |
perms->uid = pwd->pw_uid; |
| 254 |
|
else |
| 255 |
40 |
perms->uid = (uid_t) -1; |
| 256 |
80 |
if (grp != NULL) |
| 257 |
40 |
perms->gid = grp->gr_gid; |
| 258 |
|
else |
| 259 |
40 |
perms->gid = (gid_t) -1; |
| 260 |
80 |
perms->mode = mode; |
| 261 |
80 |
la->perms = perms; |
| 262 |
80 |
} |
| 263 |
|
|
| 264 |
1360 |
return (VUS_resolver(la->endpoint, vca_uds_open_cb, la, err)); |
| 265 |
|
} |
| 266 |
|
|
| 267 |
|
int |
| 268 |
39080 |
vca_uds_reopen(void) |
| 269 |
|
{ |
| 270 |
|
struct listen_sock *ls; |
| 271 |
39080 |
int err, fail = 0; |
| 272 |
|
|
| 273 |
40320 |
VTAILQ_FOREACH(ls, &UDS_acceptor.socks, vcalist) { |
| 274 |
1240 |
CHECK_OBJ_NOTNULL(ls, LISTEN_SOCK_MAGIC); |
| 275 |
|
|
| 276 |
1240 |
VJ_master(JAIL_MASTER_PRIVPORT); |
| 277 |
1240 |
err = vca_uds_opensocket(ls); |
| 278 |
1240 |
VJ_master(JAIL_MASTER_LOW); |
| 279 |
|
|
| 280 |
1240 |
if (err == 0) |
| 281 |
1240 |
continue; |
| 282 |
|
|
| 283 |
0 |
fail = vmax(fail, err); |
| 284 |
0 |
MGT_Complain(C_ERR, "Could not reopen listen socket %s: %s", |
| 285 |
0 |
ls->endpoint, VAS_errtxt(err)); |
| 286 |
0 |
} |
| 287 |
|
|
| 288 |
39080 |
return (fail); |
| 289 |
|
} |