blob: 2a4610f68a8014962eb203596a6ce49751b03894 [file] [log] [blame]
Willy Tarreau92fb9832007-10-16 17:34:28 +02001/*
2 * UNIX SOCK_STREAM protocol layer (uxst)
3 *
Willy Tarreaueb472682010-05-28 18:46:57 +02004 * Copyright 2000-2010 Willy Tarreau <w@1wt.eu>
Willy Tarreau92fb9832007-10-16 17:34:28 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
Willy Tarreaud0a895d2012-09-18 17:40:35 +020016#include <pwd.h>
17#include <grp.h>
Willy Tarreau92fb9832007-10-16 17:34:28 +020018#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21#include <syslog.h>
22#include <time.h>
23
Willy Tarreau92fb9832007-10-16 17:34:28 +020024#include <sys/socket.h>
25#include <sys/stat.h>
26#include <sys/types.h>
27#include <sys/un.h>
28
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020029#include <haproxy/api.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020030#include <haproxy/connection.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020031#include <haproxy/errors.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020032#include <haproxy/fd.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020033#include <haproxy/global.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020034#include <haproxy/list.h>
Willy Tarreau213e9902020-06-04 14:58:24 +020035#include <haproxy/listener.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020036#include <haproxy/log.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020037#include <haproxy/protocol.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020038#include <haproxy/time.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020039#include <haproxy/tools.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020040#include <haproxy/version.h>
Willy Tarreau92fb9832007-10-16 17:34:28 +020041
Willy Tarreau92fb9832007-10-16 17:34:28 +020042
Emeric Bruncf20bf12010-10-22 16:06:11 +020043static int uxst_bind_listener(struct listener *listener, char *errmsg, int errlen);
44static int uxst_bind_listeners(struct protocol *proto, char *errmsg, int errlen);
Willy Tarreaudabf2e22007-10-28 21:59:24 +010045static int uxst_unbind_listeners(struct protocol *proto);
Olivier Houchardfdcb0072019-05-06 18:32:29 +020046static int uxst_connect_server(struct connection *conn, int flags);
Willy Tarreau9d5be5c2017-09-15 07:55:51 +020047static void uxst_add_listener(struct listener *listener, int port);
Willy Tarreau31794892017-09-15 07:59:31 +020048static int uxst_pause_listener(struct listener *l);
49static int uxst_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir);
50static int uxst_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir);
Willy Tarreaudabf2e22007-10-28 21:59:24 +010051
52/* Note: must not be declared <const> as its list will be overwritten */
53static struct protocol proto_unix = {
54 .name = "unix_stream",
55 .sock_domain = PF_UNIX,
56 .sock_type = SOCK_STREAM,
57 .sock_prot = 0,
58 .sock_family = AF_UNIX,
59 .sock_addrlen = sizeof(struct sockaddr_un),
60 .l3_addrlen = sizeof(((struct sockaddr_un*)0)->sun_path),/* path len */
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020061 .accept = &listener_accept,
Willy Tarreau47f48c42014-05-09 22:57:47 +020062 .connect = &uxst_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020063 .bind = uxst_bind_listener,
Willy Tarreaudabf2e22007-10-28 21:59:24 +010064 .bind_all = uxst_bind_listeners,
65 .unbind_all = uxst_unbind_listeners,
66 .enable_all = enable_all_listeners,
67 .disable_all = disable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +020068 .get_src = uxst_get_src,
69 .get_dst = uxst_get_dst,
Willy Tarreaufd0e0082014-07-07 21:07:51 +020070 .pause = uxst_pause_listener,
Willy Tarreau9d5be5c2017-09-15 07:55:51 +020071 .add = uxst_add_listener,
Willy Tarreaudabf2e22007-10-28 21:59:24 +010072 .listeners = LIST_HEAD_INIT(proto_unix.listeners),
73 .nb_listeners = 0,
74};
75
Willy Tarreau0108d902018-11-25 19:14:37 +010076INITCALL1(STG_REGISTER, protocol_register, &proto_unix);
77
Willy Tarreaudabf2e22007-10-28 21:59:24 +010078/********************************
79 * 1) low-level socket functions
80 ********************************/
81
Willy Tarreau59b94792012-05-11 16:16:40 +020082/*
83 * Retrieves the source address for the socket <fd>, with <dir> indicating
84 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
85 * success, -1 in case of error. The socket's source address is stored in
86 * <sa> for <salen> bytes.
87 */
Willy Tarreau31794892017-09-15 07:59:31 +020088static int uxst_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
Willy Tarreau59b94792012-05-11 16:16:40 +020089{
90 if (dir)
91 return getsockname(fd, sa, &salen);
92 else
93 return getpeername(fd, sa, &salen);
94}
95
96
97/*
98 * Retrieves the original destination address for the socket <fd>, with <dir>
99 * indicating if we're a listener (=0) or an initiator (!=0). It returns 0 in
100 * case of success, -1 in case of error. The socket's source address is stored
101 * in <sa> for <salen> bytes.
102 */
Willy Tarreau31794892017-09-15 07:59:31 +0200103static int uxst_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
Willy Tarreau59b94792012-05-11 16:16:40 +0200104{
105 if (dir)
106 return getpeername(fd, sa, &salen);
107 else
108 return getsockname(fd, sa, &salen);
109}
110
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100111
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100112/********************************
113 * 2) listener-oriented functions
114 ********************************/
115
116
Olivier Houchardf886e342017-04-05 22:24:59 +0200117static int uxst_find_compatible_fd(struct listener *l)
118{
119 struct xfer_sock_list *xfer_sock = xfer_sock_list;
120 int ret = -1;
121
122 while (xfer_sock) {
123 struct sockaddr_un *un1 = (void *)&l->addr;
124 struct sockaddr_un *un2 = (void *)&xfer_sock->addr;
125
126 /*
127 * The bound socket's path as returned by getsockaddr
128 * will be the temporary name <sockname>.XXXXX.tmp,
129 * so we can't just compare the two names
130 */
131 if (xfer_sock->addr.ss_family == AF_UNIX &&
132 strncmp(un1->sun_path, un2->sun_path,
133 strlen(un1->sun_path)) == 0) {
134 char *after_sockname = un2->sun_path +
135 strlen(un1->sun_path);
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500136 /* Make a reasonable effort to check that
Olivier Houchardf886e342017-04-05 22:24:59 +0200137 * it is indeed a haproxy-generated temporary
138 * name, it's not perfect, but probably good enough.
139 */
140 if (after_sockname[0] == '.') {
141 after_sockname++;
142 while (after_sockname[0] >= '0' &&
143 after_sockname[0] <= '9')
144 after_sockname++;
145 if (!strcmp(after_sockname, ".tmp"))
146 break;
Olivier Houchardb4dd15b2018-06-06 18:34:34 +0200147 /* abns sockets sun_path starts with a \0 */
148 } else if (un1->sun_path[0] == 0
149 && un2->sun_path[0] == 0
150 && !memcmp(&un1->sun_path[1], &un2->sun_path[1],
151 sizeof(un1->sun_path) - 1))
152 break;
Olivier Houchardf886e342017-04-05 22:24:59 +0200153 }
154 xfer_sock = xfer_sock->next;
155 }
156 if (xfer_sock != NULL) {
157 ret = xfer_sock->fd;
158 if (xfer_sock == xfer_sock_list)
159 xfer_sock_list = xfer_sock->next;
160 if (xfer_sock->prev)
161 xfer_sock->prev->next = xfer_sock->next;
162 if (xfer_sock->next)
Olivier Houchardec9516a2018-03-08 18:25:49 +0100163 xfer_sock->next->prev = xfer_sock->prev;
Olivier Houchardf886e342017-04-05 22:24:59 +0200164 free(xfer_sock);
165 }
166 return ret;
167
168}
169
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100170/* This function creates a UNIX socket associated to the listener. It changes
171 * the state from ASSIGNED to LISTEN. The socket is NOT enabled for polling.
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100172 * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL. It
173 * may return a warning or an error message in <errmsg> if the message is at
174 * most <errlen> bytes long (including '\0'). Note that <errmsg> may be NULL if
175 * <errlen> is also zero.
Willy Tarreau92fb9832007-10-16 17:34:28 +0200176 */
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100177static int uxst_bind_listener(struct listener *listener, char *errmsg, int errlen)
Willy Tarreau92fb9832007-10-16 17:34:28 +0200178{
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100179 int fd;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200180 char tempname[MAXPATHLEN];
181 char backname[MAXPATHLEN];
182 struct sockaddr_un addr;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100183 const char *msg = NULL;
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100184 const char *path;
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100185 int maxpathlen;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100186 int ext, ready;
187 socklen_t ready_len;
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200188 int err;
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100189 int ret;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200190
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200191 err = ERR_NONE;
192
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100193 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100194 if (errlen)
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100195 *errmsg = 0;
196
197 if (listener->state != LI_ASSIGNED)
198 return ERR_NONE; /* already bound */
199
Olivier Houchardf886e342017-04-05 22:24:59 +0200200 if (listener->fd == -1)
201 listener->fd = uxst_find_compatible_fd(listener);
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100202 path = ((struct sockaddr_un *)&listener->addr)->sun_path;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200203
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100204 maxpathlen = MIN(MAXPATHLEN, sizeof(addr.sun_path));
205
Willy Tarreau40aa0702013-03-10 23:51:38 +0100206 /* if the listener already has an fd assigned, then we were offered the
207 * fd by an external process (most likely the parent), and we don't want
208 * to create a new socket. However we still want to set a few flags on
209 * the socket.
210 */
211 fd = listener->fd;
212 ext = (fd >= 0);
213 if (ext)
214 goto fd_ready;
215
Willy Tarreauccfccef2014-05-10 01:49:15 +0200216 if (path[0]) {
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100217 ret = snprintf(tempname, maxpathlen, "%s.%d.tmp", path, pid);
Willy Tarreauf28d5c92020-06-12 15:58:19 +0200218 if (ret < 0 || ret >= sizeof(addr.sun_path)) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200219 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100220 msg = "name too long for UNIX socket (limit usually 97)";
Willy Tarreauccfccef2014-05-10 01:49:15 +0200221 goto err_return;
222 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200223
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100224 ret = snprintf(backname, maxpathlen, "%s.%d.bak", path, pid);
225 if (ret < 0 || ret >= maxpathlen) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200226 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100227 msg = "name too long for UNIX socket (limit usually 97)";
Willy Tarreauccfccef2014-05-10 01:49:15 +0200228 goto err_return;
229 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200230
Willy Tarreauccfccef2014-05-10 01:49:15 +0200231 /* 2. clean existing orphaned entries */
232 if (unlink(tempname) < 0 && errno != ENOENT) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200233 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200234 msg = "error when trying to unlink previous UNIX socket";
235 goto err_return;
236 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200237
Willy Tarreauccfccef2014-05-10 01:49:15 +0200238 if (unlink(backname) < 0 && errno != ENOENT) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200239 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200240 msg = "error when trying to unlink previous UNIX socket";
241 goto err_return;
242 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200243
Willy Tarreauccfccef2014-05-10 01:49:15 +0200244 /* 3. backup existing socket */
245 if (link(path, backname) < 0 && errno != ENOENT) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200246 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200247 msg = "error when trying to preserve previous UNIX socket";
248 goto err_return;
249 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200250
Willy Tarreauf28d5c92020-06-12 15:58:19 +0200251 /* Note: this test is redundant with the snprintf one above and
252 * will never trigger, it's just added as the only way to shut
253 * gcc's painfully dumb warning about possibly truncated output
254 * during strncpy(). Don't move it above or smart gcc will not
255 * see it!
256 */
257 if (strlen(tempname) >= sizeof(addr.sun_path)) {
258 err |= ERR_FATAL | ERR_ALERT;
259 msg = "name too long for UNIX socket (limit usually 97)";
260 goto err_return;
261 }
262
Willy Tarreau719e07c2019-12-11 16:29:10 +0100263 strncpy(addr.sun_path, tempname, sizeof(addr.sun_path) - 1);
Willy Tarreauccfccef2014-05-10 01:49:15 +0200264 addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200265 }
Willy Tarreauccfccef2014-05-10 01:49:15 +0200266 else {
267 /* first char is zero, it's an abstract socket whose address
268 * is defined by all the bytes past this zero.
269 */
270 memcpy(addr.sun_path, path, sizeof(addr.sun_path));
271 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200272 addr.sun_family = AF_UNIX;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200273
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100274 fd = socket(PF_UNIX, SOCK_STREAM, 0);
275 if (fd < 0) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200276 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100277 msg = "cannot create UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200278 goto err_unlink_back;
279 }
280
Willy Tarreau40aa0702013-03-10 23:51:38 +0100281 fd_ready:
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100282 if (fd >= global.maxsock) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200283 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100284 msg = "socket(): not enough free sockets, raise -n argument";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200285 goto err_unlink_temp;
286 }
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100287
288 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200289 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100290 msg = "cannot make UNIX socket non-blocking";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200291 goto err_unlink_temp;
292 }
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100293
Willy Tarreau40aa0702013-03-10 23:51:38 +0100294 if (!ext && bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
Willy Tarreau92fb9832007-10-16 17:34:28 +0200295 /* note that bind() creates the socket <tempname> on the file system */
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200296 if (errno == EADDRINUSE) {
297 /* the old process might still own it, let's retry */
298 err |= ERR_RETRYABLE | ERR_ALERT;
299 msg = "cannot listen to socket";
300 }
301 else {
302 err |= ERR_FATAL | ERR_ALERT;
303 msg = "cannot bind UNIX socket";
304 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200305 goto err_unlink_temp;
306 }
307
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100308 /* <uid> and <gid> different of -1 will be used to change the socket owner.
309 * If <mode> is not 0, it will be used to restrict access to the socket.
310 * While it is known not to be portable on every OS, it's still useful
Willy Tarreauccfccef2014-05-10 01:49:15 +0200311 * where it works. We also don't change permissions on abstract sockets.
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100312 */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200313 if (!ext && path[0] &&
Willy Tarreau40aa0702013-03-10 23:51:38 +0100314 (((listener->bind_conf->ux.uid != -1 || listener->bind_conf->ux.gid != -1) &&
315 (chown(tempname, listener->bind_conf->ux.uid, listener->bind_conf->ux.gid) == -1)) ||
316 (listener->bind_conf->ux.mode != 0 && chmod(tempname, listener->bind_conf->ux.mode) == -1))) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200317 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100318 msg = "cannot change UNIX socket ownership";
Willy Tarreaue6ad2b12007-10-18 12:45:54 +0200319 goto err_unlink_temp;
320 }
321
Willy Tarreau40aa0702013-03-10 23:51:38 +0100322 ready = 0;
323 ready_len = sizeof(ready);
324 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1)
325 ready = 0;
326
327 if (!(ext && ready) && /* only listen if not already done by external process */
Willy Tarreaue2711c72019-02-27 15:39:41 +0100328 listen(fd, listener_backlog(listener)) < 0) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200329 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100330 msg = "cannot listen to UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200331 goto err_unlink_temp;
332 }
333
Willy Tarreauccfccef2014-05-10 01:49:15 +0200334 /* Point of no return: we are ready, we'll switch the sockets. We don't
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500335 * fear losing the socket <path> because we have a copy of it in
Willy Tarreauccfccef2014-05-10 01:49:15 +0200336 * backname. Abstract sockets are not renamed.
Willy Tarreau92fb9832007-10-16 17:34:28 +0200337 */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200338 if (!ext && path[0] && rename(tempname, path) < 0) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200339 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100340 msg = "cannot switch final and temporary UNIX sockets";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200341 goto err_rename;
342 }
343
Willy Tarreau68986ab2017-06-16 10:34:20 +0200344 /* Cleanup: only unlink if we didn't inherit the fd from the parent */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200345 if (!ext && path[0])
Willy Tarreau40aa0702013-03-10 23:51:38 +0100346 unlink(backname);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200347
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100348 /* the socket is now listening */
349 listener->fd = fd;
350 listener->state = LI_LISTEN;
351
Willy Tarreaua9786b62018-01-25 07:22:13 +0100352 fd_insert(fd, listener, listener->proto->accept,
Willy Tarreau0948a782020-02-12 10:15:34 +0100353 thread_mask(listener->bind_conf->bind_thread) & all_threads_mask);
Willy Tarreaua9786b62018-01-25 07:22:13 +0100354
Willy Tarreaubb1caff2020-08-19 10:00:57 +0200355 /* for now, all regularly bound UNIX listeners are exportable */
356 if (!(listener->options & LI_O_INHERITED))
357 fdtab[fd].exported = 1;
358
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200359 return err;
360
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100361 err_rename:
362 ret = rename(backname, path);
363 if (ret < 0 && errno == ENOENT)
364 unlink(path);
365 err_unlink_temp:
Jan Seda7319b642014-06-26 20:44:05 +0200366 if (!ext && path[0])
Willy Tarreau40aa0702013-03-10 23:51:38 +0100367 unlink(tempname);
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100368 close(fd);
369 err_unlink_back:
Jan Seda7319b642014-06-26 20:44:05 +0200370 if (!ext && path[0])
Willy Tarreau40aa0702013-03-10 23:51:38 +0100371 unlink(backname);
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100372 err_return:
Willy Tarreau40aa0702013-03-10 23:51:38 +0100373 if (msg && errlen) {
374 if (!ext)
375 snprintf(errmsg, errlen, "%s [%s]", msg, path);
376 else
377 snprintf(errmsg, errlen, "%s [fd %d]", msg, fd);
378 }
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200379 return err;
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100380}
381
382/* This function closes the UNIX sockets for the specified listener.
383 * The listener enters the LI_ASSIGNED state. It always returns ERR_NONE.
384 */
385static int uxst_unbind_listener(struct listener *listener)
386{
Willy Tarreaube58c382011-07-24 18:28:10 +0200387 if (listener->state > LI_ASSIGNED) {
388 unbind_listener(listener);
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100389 }
390 return ERR_NONE;
391}
392
Willy Tarreau32282382017-09-15 07:44:44 +0200393/* Add <listener> to the list of unix stream listeners (port is ignored). The
394 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
395 * The number of listeners for the protocol is updated.
Willy Tarreaudaacf362019-07-24 16:45:02 +0200396 *
397 * Must be called with proto_lock held.
398 *
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100399 */
Willy Tarreau9d5be5c2017-09-15 07:55:51 +0200400static void uxst_add_listener(struct listener *listener, int port)
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100401{
402 if (listener->state != LI_INIT)
403 return;
404 listener->state = LI_ASSIGNED;
405 listener->proto = &proto_unix;
406 LIST_ADDQ(&proto_unix.listeners, &listener->proto_list);
407 proto_unix.nb_listeners++;
408}
409
Willy Tarreaufd0e0082014-07-07 21:07:51 +0200410/* Pause a listener. Returns < 0 in case of failure, 0 if the listener
411 * was totally stopped, or > 0 if correctly paused. Nothing is done for
412 * plain unix sockets since currently it's the new process which handles
413 * the renaming. Abstract sockets are completely unbound.
414 */
Willy Tarreau31794892017-09-15 07:59:31 +0200415static int uxst_pause_listener(struct listener *l)
Willy Tarreaufd0e0082014-07-07 21:07:51 +0200416{
417 if (((struct sockaddr_un *)&l->addr)->sun_path[0])
418 return 1;
419
Christopher Faulet510c0d62018-03-16 10:04:47 +0100420 /* Listener's lock already held. Call lockless version of
421 * unbind_listener. */
422 do_unbind_listener(l, 1);
Willy Tarreaufd0e0082014-07-07 21:07:51 +0200423 return 0;
424}
425
Willy Tarreau47f48c42014-05-09 22:57:47 +0200426
427/*
428 * This function initiates a UNIX connection establishment to the target assigned
Willy Tarreauca9f5a92019-07-17 16:40:37 +0200429 * to connection <conn> using (si->{target,dst}). The source address is ignored
Willy Tarreau47f48c42014-05-09 22:57:47 +0200430 * and will be selected by the system. conn->target may point either to a valid
431 * server or to a backend, depending on conn->target. Only OBJ_TYPE_PROXY and
432 * OBJ_TYPE_SERVER are supported. The <data> parameter is a boolean indicating
433 * whether there are data waiting for being sent or not, in order to adjust data
434 * write polling and on some platforms. The <delack> argument is ignored.
435 *
436 * Note that a pending send_proxy message accounts for data.
437 *
438 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +0200439 * - SF_ERR_NONE if everything's OK
440 * - SF_ERR_SRVTO if there are no more servers
441 * - SF_ERR_SRVCL if the connection was refused by the server
442 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
443 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
444 * - SF_ERR_INTERNAL for any other purely internal errors
Tim Düsterhus4896c442016-11-29 02:15:19 +0100445 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau47f48c42014-05-09 22:57:47 +0200446 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200447 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
Willy Tarreau47f48c42014-05-09 22:57:47 +0200448 * it's invalid and the caller has nothing to do.
449 */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200450static int uxst_connect_server(struct connection *conn, int flags)
Willy Tarreau47f48c42014-05-09 22:57:47 +0200451{
452 int fd;
453 struct server *srv;
454 struct proxy *be;
455
Willy Tarreau47f48c42014-05-09 22:57:47 +0200456 switch (obj_type(conn->target)) {
457 case OBJ_TYPE_PROXY:
458 be = objt_proxy(conn->target);
459 srv = NULL;
460 break;
461 case OBJ_TYPE_SERVER:
462 srv = objt_server(conn->target);
463 be = srv->proxy;
464 break;
465 default:
466 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200467 return SF_ERR_INTERNAL;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200468 }
469
Willy Tarreau585744b2017-08-24 14:31:19 +0200470 if ((fd = conn->handle.fd = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
Willy Tarreau47f48c42014-05-09 22:57:47 +0200471 qfprintf(stderr, "Cannot get a server socket.\n");
472
473 if (errno == ENFILE) {
474 conn->err_code = CO_ER_SYS_FDLIM;
475 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100476 "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n",
477 be->id, global.maxsock);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200478 }
479 else if (errno == EMFILE) {
480 conn->err_code = CO_ER_PROC_FDLIM;
481 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100482 "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n",
483 be->id, global.maxsock);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200484 }
485 else if (errno == ENOBUFS || errno == ENOMEM) {
486 conn->err_code = CO_ER_SYS_MEMLIM;
487 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100488 "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n",
489 be->id, global.maxsock);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200490 }
491 else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
492 conn->err_code = CO_ER_NOPROTO;
493 }
494 else
495 conn->err_code = CO_ER_SOCK_ERR;
496
497 /* this is a resource error */
498 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200499 return SF_ERR_RESOURCE;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200500 }
501
502 if (fd >= global.maxsock) {
503 /* do not log anything there, it's a normal condition when this option
504 * is used to serialize connections to a server !
505 */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100506 ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
Willy Tarreau47f48c42014-05-09 22:57:47 +0200507 close(fd);
508 conn->err_code = CO_ER_CONF_FDLIM;
509 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200510 return SF_ERR_PRXCOND; /* it is a configuration limit */
Willy Tarreau47f48c42014-05-09 22:57:47 +0200511 }
512
513 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
514 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
515 close(fd);
516 conn->err_code = CO_ER_SOCK_ERR;
517 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200518 return SF_ERR_INTERNAL;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200519 }
520
William Lallemandc03eb012018-11-27 12:02:37 +0100521 if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) {
522 ha_alert("Cannot set CLOEXEC on client socket.\n");
523 close(fd);
524 conn->err_code = CO_ER_SOCK_ERR;
525 conn->flags |= CO_FL_ERROR;
526 return SF_ERR_INTERNAL;
527 }
528
Willy Tarreau47f48c42014-05-09 22:57:47 +0200529 /* if a send_proxy is there, there are data */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200530 if (conn->send_proxy_ofs)
531 flags |= CONNECT_HAS_DATA;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200532
533 if (global.tune.server_sndbuf)
534 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
535
536 if (global.tune.server_rcvbuf)
537 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
538
Willy Tarreauca9f5a92019-07-17 16:40:37 +0200539 if (connect(fd, (struct sockaddr *)conn->dst, get_addr_len(conn->dst)) == -1) {
Willy Tarreau94841792017-01-25 14:27:38 +0100540 if (errno == EINPROGRESS || errno == EALREADY) {
Willy Tarreau7bb21532014-05-10 09:48:28 +0200541 conn->flags |= CO_FL_WAIT_L4_CONN;
542 }
Willy Tarreau94841792017-01-25 14:27:38 +0100543 else if (errno == EISCONN) {
544 conn->flags &= ~CO_FL_WAIT_L4_CONN;
545 }
Willy Tarreau7bb21532014-05-10 09:48:28 +0200546 else if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau47f48c42014-05-09 22:57:47 +0200547 char *msg;
548 if (errno == EAGAIN || errno == EADDRNOTAVAIL) {
Lukas Tribus9f256d42016-01-26 20:33:14 +0100549 msg = "can't connect to destination unix socket, check backlog size on the server";
Willy Tarreau47f48c42014-05-09 22:57:47 +0200550 conn->err_code = CO_ER_FREE_PORTS;
551 }
552 else {
553 msg = "local address already in use";
554 conn->err_code = CO_ER_ADDR_INUSE;
555 }
556
557 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
558 close(fd);
559 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
560 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200561 return SF_ERR_RESOURCE;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200562 }
563 else if (errno == ETIMEDOUT) {
564 close(fd);
565 conn->err_code = CO_ER_SOCK_ERR;
566 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200567 return SF_ERR_SRVTO;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200568 }
569 else { // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
570 close(fd);
571 conn->err_code = CO_ER_SOCK_ERR;
572 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200573 return SF_ERR_SRVCL;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200574 }
575 }
Willy Tarreau7bb21532014-05-10 09:48:28 +0200576 else {
577 /* connect() already succeeded, which is quite usual for unix
Willy Tarreau94841792017-01-25 14:27:38 +0100578 * sockets. Let's avoid a second connect() probe to complete it.
Willy Tarreau7bb21532014-05-10 09:48:28 +0200579 */
580 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau7bb21532014-05-10 09:48:28 +0200581 }
Willy Tarreau47f48c42014-05-09 22:57:47 +0200582
583 conn->flags |= CO_FL_ADDR_TO_SET;
584
585 /* Prepare to send a few handshakes related to the on-wire protocol. */
586 if (conn->send_proxy_ofs)
587 conn->flags |= CO_FL_SEND_PROXY;
588
589 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreau7bb21532014-05-10 09:48:28 +0200590 fdtab[fd].linger_risk = 0; /* no need to disable lingering */
Willy Tarreau47f48c42014-05-09 22:57:47 +0200591
Willy Tarreau4c69cff2020-03-04 16:38:00 +0100592 if (conn->flags & CO_FL_WAIT_L4_CONN) {
593 fd_want_send(fd);
594 fd_cant_send(fd);
Willy Tarreau8dbd1a22020-07-31 08:59:09 +0200595 fd_cant_recv(fd);
Willy Tarreau4c69cff2020-03-04 16:38:00 +0100596 }
Willy Tarreauccf3f6d2019-09-05 17:05:05 +0200597
Willy Tarreau47f48c42014-05-09 22:57:47 +0200598 if (conn_xprt_init(conn) < 0) {
Willy Tarreau8c829012017-10-05 18:02:11 +0200599 conn_full_close(conn);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200600 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200601 return SF_ERR_RESOURCE;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200602 }
603
Willy Tarreaue7dff022015-04-03 01:14:29 +0200604 return SF_ERR_NONE; /* connection is OK */
Willy Tarreau47f48c42014-05-09 22:57:47 +0200605}
606
607
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100608/********************************
609 * 3) protocol-oriented functions
610 ********************************/
611
612
Willy Tarreau92fb9832007-10-16 17:34:28 +0200613/* This function creates all UNIX sockets bound to the protocol entry <proto>.
614 * It is intended to be used as the protocol's bind_all() function.
615 * The sockets will be registered but not added to any fd_set, in order not to
616 * loose them across the fork(). A call to uxst_enable_listeners() is needed
617 * to complete initialization.
618 *
Willy Tarreaudaacf362019-07-24 16:45:02 +0200619 * Must be called with proto_lock held.
620 *
Willy Tarreau92fb9832007-10-16 17:34:28 +0200621 * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL.
622 */
Emeric Bruncf20bf12010-10-22 16:06:11 +0200623static int uxst_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreau92fb9832007-10-16 17:34:28 +0200624{
625 struct listener *listener;
626 int err = ERR_NONE;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200627
628 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200629 err |= uxst_bind_listener(listener, errmsg, errlen);
630 if (err & ERR_ABORT)
631 break;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200632 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200633 return err;
634}
635
Willy Tarreau92fb9832007-10-16 17:34:28 +0200636
637/* This function stops all listening UNIX sockets bound to the protocol
638 * <proto>. It does not detaches them from the protocol.
639 * It always returns ERR_NONE.
Willy Tarreaudaacf362019-07-24 16:45:02 +0200640 *
641 * Must be called with proto_lock held.
642 *
Willy Tarreau92fb9832007-10-16 17:34:28 +0200643 */
644static int uxst_unbind_listeners(struct protocol *proto)
645{
646 struct listener *listener;
647
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100648 list_for_each_entry(listener, &proto->listeners, proto_list)
649 uxst_unbind_listener(listener);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200650 return ERR_NONE;
651}
652
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200653/* parse the "mode" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200654static int bind_parse_mode(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200655{
Willy Tarreaua1a247b2017-10-04 14:43:44 +0200656 char *endptr;
657
658 conf->ux.mode = strtol(args[cur_arg + 1], &endptr, 8);
659
660 if (!*args[cur_arg + 1] || *endptr) {
661 memprintf(err, "'%s' : missing or invalid mode '%s' (octal integer expected)", args[cur_arg], args[cur_arg + 1]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200662 return ERR_ALERT | ERR_FATAL;
663 }
664
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200665 return 0;
666}
667
668/* parse the "gid" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200669static int bind_parse_gid(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200670{
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200671 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200672 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200673 return ERR_ALERT | ERR_FATAL;
674 }
675
Willy Tarreau290e63a2012-09-20 18:07:14 +0200676 conf->ux.gid = atol(args[cur_arg + 1]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200677 return 0;
678}
679
680/* parse the "group" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200681static int bind_parse_group(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200682{
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200683 struct group *group;
684
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200685 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200686 memprintf(err, "'%s' : missing group name", args[cur_arg]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200687 return ERR_ALERT | ERR_FATAL;
688 }
689
690 group = getgrnam(args[cur_arg + 1]);
691 if (!group) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200692 memprintf(err, "'%s' : unknown group name '%s'", args[cur_arg], args[cur_arg + 1]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200693 return ERR_ALERT | ERR_FATAL;
694 }
695
Willy Tarreau290e63a2012-09-20 18:07:14 +0200696 conf->ux.gid = group->gr_gid;
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200697 return 0;
698}
699
700/* parse the "uid" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200701static int bind_parse_uid(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200702{
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200703 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200704 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200705 return ERR_ALERT | ERR_FATAL;
706 }
707
Willy Tarreau290e63a2012-09-20 18:07:14 +0200708 conf->ux.uid = atol(args[cur_arg + 1]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200709 return 0;
710}
711
712/* parse the "user" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200713static int bind_parse_user(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200714{
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200715 struct passwd *user;
716
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200717 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200718 memprintf(err, "'%s' : missing user name", args[cur_arg]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200719 return ERR_ALERT | ERR_FATAL;
720 }
721
722 user = getpwnam(args[cur_arg + 1]);
723 if (!user) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200724 memprintf(err, "'%s' : unknown user name '%s'", args[cur_arg], args[cur_arg + 1]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200725 return ERR_ALERT | ERR_FATAL;
726 }
727
Willy Tarreau290e63a2012-09-20 18:07:14 +0200728 conf->ux.uid = user->pw_uid;
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200729 return 0;
730}
731
732/* Note: must not be declared <const> as its list will be overwritten.
733 * Please take care of keeping this list alphabetically sorted, doing so helps
734 * all code contributors.
735 * Optional keywords are also declared with a NULL ->parse() function so that
736 * the config parser can report an appropriate error when a known keyword was
737 * not enabled.
738 */
Willy Tarreau51fb7652012-09-18 18:24:39 +0200739static struct bind_kw_list bind_kws = { "UNIX", { }, {
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200740 { "gid", bind_parse_gid, 1 }, /* set the socket's gid */
741 { "group", bind_parse_group, 1 }, /* set the socket's gid from the group name */
742 { "mode", bind_parse_mode, 1 }, /* set the socket's mode (eg: 0644)*/
743 { "uid", bind_parse_uid, 1 }, /* set the socket's uid */
744 { "user", bind_parse_user, 1 }, /* set the socket's uid from the user name */
745 { NULL, NULL, 0 },
746}};
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100747
Willy Tarreau0108d902018-11-25 19:14:37 +0100748INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200749
750/*
751 * Local variables:
752 * c-indent-level: 8
753 * c-basic-offset: 8
754 * End:
755 */