blob: 69253732b20e79a5a80d227ef38a01803d489ca2 [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 Tarreau8d366972020-05-27 16:10:29 +020030#include <haproxy/errors.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020031#include <haproxy/global.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020032#include <haproxy/list.h>
Willy Tarreau213e9902020-06-04 14:58:24 +020033#include <haproxy/listener.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020034#include <haproxy/tools.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020035#include <haproxy/time.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020036#include <haproxy/version.h>
Willy Tarreau92fb9832007-10-16 17:34:28 +020037
Willy Tarreau47f48c42014-05-09 22:57:47 +020038#include <proto/connection.h>
Willy Tarreau0f6ffd62020-06-03 19:33:00 +020039#include <haproxy/fd.h>
Willy Tarreau92fb9832007-10-16 17:34:28 +020040#include <proto/log.h>
Willy Tarreau2dd7c352020-06-03 15:26:55 +020041#include <haproxy/protocol.h>
Willy Tarreau92fb9832007-10-16 17:34:28 +020042#include <proto/task.h>
43
Emeric Bruncf20bf12010-10-22 16:06:11 +020044static int uxst_bind_listener(struct listener *listener, char *errmsg, int errlen);
45static int uxst_bind_listeners(struct protocol *proto, char *errmsg, int errlen);
Willy Tarreaudabf2e22007-10-28 21:59:24 +010046static int uxst_unbind_listeners(struct protocol *proto);
Olivier Houchardfdcb0072019-05-06 18:32:29 +020047static int uxst_connect_server(struct connection *conn, int flags);
Willy Tarreau9d5be5c2017-09-15 07:55:51 +020048static void uxst_add_listener(struct listener *listener, int port);
Willy Tarreau31794892017-09-15 07:59:31 +020049static int uxst_pause_listener(struct listener *l);
50static int uxst_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir);
51static int uxst_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir);
Willy Tarreaudabf2e22007-10-28 21:59:24 +010052
53/* Note: must not be declared <const> as its list will be overwritten */
54static struct protocol proto_unix = {
55 .name = "unix_stream",
56 .sock_domain = PF_UNIX,
57 .sock_type = SOCK_STREAM,
58 .sock_prot = 0,
59 .sock_family = AF_UNIX,
60 .sock_addrlen = sizeof(struct sockaddr_un),
61 .l3_addrlen = sizeof(((struct sockaddr_un*)0)->sun_path),/* path len */
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020062 .accept = &listener_accept,
Willy Tarreau47f48c42014-05-09 22:57:47 +020063 .connect = &uxst_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020064 .bind = uxst_bind_listener,
Willy Tarreaudabf2e22007-10-28 21:59:24 +010065 .bind_all = uxst_bind_listeners,
66 .unbind_all = uxst_unbind_listeners,
67 .enable_all = enable_all_listeners,
68 .disable_all = disable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +020069 .get_src = uxst_get_src,
70 .get_dst = uxst_get_dst,
Willy Tarreaufd0e0082014-07-07 21:07:51 +020071 .pause = uxst_pause_listener,
Willy Tarreau9d5be5c2017-09-15 07:55:51 +020072 .add = uxst_add_listener,
Willy Tarreaudabf2e22007-10-28 21:59:24 +010073 .listeners = LIST_HEAD_INIT(proto_unix.listeners),
74 .nb_listeners = 0,
75};
76
Willy Tarreau0108d902018-11-25 19:14:37 +010077INITCALL1(STG_REGISTER, protocol_register, &proto_unix);
78
Willy Tarreaudabf2e22007-10-28 21:59:24 +010079/********************************
80 * 1) low-level socket functions
81 ********************************/
82
Willy Tarreau59b94792012-05-11 16:16:40 +020083/*
84 * Retrieves the source address for the socket <fd>, with <dir> indicating
85 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
86 * success, -1 in case of error. The socket's source address is stored in
87 * <sa> for <salen> bytes.
88 */
Willy Tarreau31794892017-09-15 07:59:31 +020089static int uxst_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
Willy Tarreau59b94792012-05-11 16:16:40 +020090{
91 if (dir)
92 return getsockname(fd, sa, &salen);
93 else
94 return getpeername(fd, sa, &salen);
95}
96
97
98/*
99 * Retrieves the original destination address for the socket <fd>, with <dir>
100 * indicating if we're a listener (=0) or an initiator (!=0). It returns 0 in
101 * case of success, -1 in case of error. The socket's source address is stored
102 * in <sa> for <salen> bytes.
103 */
Willy Tarreau31794892017-09-15 07:59:31 +0200104static int uxst_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
Willy Tarreau59b94792012-05-11 16:16:40 +0200105{
106 if (dir)
107 return getpeername(fd, sa, &salen);
108 else
109 return getsockname(fd, sa, &salen);
110}
111
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100112
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100113/********************************
114 * 2) listener-oriented functions
115 ********************************/
116
117
Olivier Houchardf886e342017-04-05 22:24:59 +0200118static int uxst_find_compatible_fd(struct listener *l)
119{
120 struct xfer_sock_list *xfer_sock = xfer_sock_list;
121 int ret = -1;
122
123 while (xfer_sock) {
124 struct sockaddr_un *un1 = (void *)&l->addr;
125 struct sockaddr_un *un2 = (void *)&xfer_sock->addr;
126
127 /*
128 * The bound socket's path as returned by getsockaddr
129 * will be the temporary name <sockname>.XXXXX.tmp,
130 * so we can't just compare the two names
131 */
132 if (xfer_sock->addr.ss_family == AF_UNIX &&
133 strncmp(un1->sun_path, un2->sun_path,
134 strlen(un1->sun_path)) == 0) {
135 char *after_sockname = un2->sun_path +
136 strlen(un1->sun_path);
137 /* Make a reasonnable effort to check that
138 * it is indeed a haproxy-generated temporary
139 * name, it's not perfect, but probably good enough.
140 */
141 if (after_sockname[0] == '.') {
142 after_sockname++;
143 while (after_sockname[0] >= '0' &&
144 after_sockname[0] <= '9')
145 after_sockname++;
146 if (!strcmp(after_sockname, ".tmp"))
147 break;
Olivier Houchardb4dd15b2018-06-06 18:34:34 +0200148 /* abns sockets sun_path starts with a \0 */
149 } else if (un1->sun_path[0] == 0
150 && un2->sun_path[0] == 0
151 && !memcmp(&un1->sun_path[1], &un2->sun_path[1],
152 sizeof(un1->sun_path) - 1))
153 break;
Olivier Houchardf886e342017-04-05 22:24:59 +0200154 }
155 xfer_sock = xfer_sock->next;
156 }
157 if (xfer_sock != NULL) {
158 ret = xfer_sock->fd;
159 if (xfer_sock == xfer_sock_list)
160 xfer_sock_list = xfer_sock->next;
161 if (xfer_sock->prev)
162 xfer_sock->prev->next = xfer_sock->next;
163 if (xfer_sock->next)
Olivier Houchardec9516a2018-03-08 18:25:49 +0100164 xfer_sock->next->prev = xfer_sock->prev;
Olivier Houchardf886e342017-04-05 22:24:59 +0200165 free(xfer_sock);
166 }
167 return ret;
168
169}
170
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100171/* This function creates a UNIX socket associated to the listener. It changes
172 * the state from ASSIGNED to LISTEN. The socket is NOT enabled for polling.
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100173 * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL. It
174 * may return a warning or an error message in <errmsg> if the message is at
175 * most <errlen> bytes long (including '\0'). Note that <errmsg> may be NULL if
176 * <errlen> is also zero.
Willy Tarreau92fb9832007-10-16 17:34:28 +0200177 */
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100178static int uxst_bind_listener(struct listener *listener, char *errmsg, int errlen)
Willy Tarreau92fb9832007-10-16 17:34:28 +0200179{
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100180 int fd;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200181 char tempname[MAXPATHLEN];
182 char backname[MAXPATHLEN];
183 struct sockaddr_un addr;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100184 const char *msg = NULL;
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100185 const char *path;
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100186 int maxpathlen;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100187 int ext, ready;
188 socklen_t ready_len;
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200189 int err;
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100190 int ret;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200191
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200192 err = ERR_NONE;
193
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100194 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100195 if (errlen)
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100196 *errmsg = 0;
197
198 if (listener->state != LI_ASSIGNED)
199 return ERR_NONE; /* already bound */
200
Olivier Houchardf886e342017-04-05 22:24:59 +0200201 if (listener->fd == -1)
202 listener->fd = uxst_find_compatible_fd(listener);
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100203 path = ((struct sockaddr_un *)&listener->addr)->sun_path;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200204
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100205 maxpathlen = MIN(MAXPATHLEN, sizeof(addr.sun_path));
206
Willy Tarreau40aa0702013-03-10 23:51:38 +0100207 /* if the listener already has an fd assigned, then we were offered the
208 * fd by an external process (most likely the parent), and we don't want
209 * to create a new socket. However we still want to set a few flags on
210 * the socket.
211 */
212 fd = listener->fd;
213 ext = (fd >= 0);
214 if (ext)
215 goto fd_ready;
216
Willy Tarreauccfccef2014-05-10 01:49:15 +0200217 if (path[0]) {
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100218 ret = snprintf(tempname, maxpathlen, "%s.%d.tmp", path, pid);
219 if (ret < 0 || ret >= maxpathlen) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200220 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100221 msg = "name too long for UNIX socket (limit usually 97)";
Willy Tarreauccfccef2014-05-10 01:49:15 +0200222 goto err_return;
223 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200224
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100225 ret = snprintf(backname, maxpathlen, "%s.%d.bak", path, pid);
226 if (ret < 0 || ret >= maxpathlen) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200227 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100228 msg = "name too long for UNIX socket (limit usually 97)";
Willy Tarreauccfccef2014-05-10 01:49:15 +0200229 goto err_return;
230 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200231
Willy Tarreauccfccef2014-05-10 01:49:15 +0200232 /* 2. clean existing orphaned entries */
233 if (unlink(tempname) < 0 && errno != ENOENT) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200234 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200235 msg = "error when trying to unlink previous UNIX socket";
236 goto err_return;
237 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200238
Willy Tarreauccfccef2014-05-10 01:49:15 +0200239 if (unlink(backname) < 0 && errno != ENOENT) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200240 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200241 msg = "error when trying to unlink previous UNIX socket";
242 goto err_return;
243 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200244
Willy Tarreauccfccef2014-05-10 01:49:15 +0200245 /* 3. backup existing socket */
246 if (link(path, backname) < 0 && errno != ENOENT) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200247 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200248 msg = "error when trying to preserve previous UNIX socket";
249 goto err_return;
250 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200251
Willy Tarreau719e07c2019-12-11 16:29:10 +0100252 strncpy(addr.sun_path, tempname, sizeof(addr.sun_path) - 1);
Willy Tarreauccfccef2014-05-10 01:49:15 +0200253 addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200254 }
Willy Tarreauccfccef2014-05-10 01:49:15 +0200255 else {
256 /* first char is zero, it's an abstract socket whose address
257 * is defined by all the bytes past this zero.
258 */
259 memcpy(addr.sun_path, path, sizeof(addr.sun_path));
260 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200261 addr.sun_family = AF_UNIX;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200262
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100263 fd = socket(PF_UNIX, SOCK_STREAM, 0);
264 if (fd < 0) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200265 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100266 msg = "cannot create UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200267 goto err_unlink_back;
268 }
269
Willy Tarreau40aa0702013-03-10 23:51:38 +0100270 fd_ready:
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100271 if (fd >= global.maxsock) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200272 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100273 msg = "socket(): not enough free sockets, raise -n argument";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200274 goto err_unlink_temp;
275 }
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100276
277 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200278 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100279 msg = "cannot make UNIX socket non-blocking";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200280 goto err_unlink_temp;
281 }
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100282
Willy Tarreau40aa0702013-03-10 23:51:38 +0100283 if (!ext && bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
Willy Tarreau92fb9832007-10-16 17:34:28 +0200284 /* note that bind() creates the socket <tempname> on the file system */
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200285 if (errno == EADDRINUSE) {
286 /* the old process might still own it, let's retry */
287 err |= ERR_RETRYABLE | ERR_ALERT;
288 msg = "cannot listen to socket";
289 }
290 else {
291 err |= ERR_FATAL | ERR_ALERT;
292 msg = "cannot bind UNIX socket";
293 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200294 goto err_unlink_temp;
295 }
296
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100297 /* <uid> and <gid> different of -1 will be used to change the socket owner.
298 * If <mode> is not 0, it will be used to restrict access to the socket.
299 * While it is known not to be portable on every OS, it's still useful
Willy Tarreauccfccef2014-05-10 01:49:15 +0200300 * where it works. We also don't change permissions on abstract sockets.
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100301 */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200302 if (!ext && path[0] &&
Willy Tarreau40aa0702013-03-10 23:51:38 +0100303 (((listener->bind_conf->ux.uid != -1 || listener->bind_conf->ux.gid != -1) &&
304 (chown(tempname, listener->bind_conf->ux.uid, listener->bind_conf->ux.gid) == -1)) ||
305 (listener->bind_conf->ux.mode != 0 && chmod(tempname, listener->bind_conf->ux.mode) == -1))) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200306 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100307 msg = "cannot change UNIX socket ownership";
Willy Tarreaue6ad2b12007-10-18 12:45:54 +0200308 goto err_unlink_temp;
309 }
310
Willy Tarreau40aa0702013-03-10 23:51:38 +0100311 ready = 0;
312 ready_len = sizeof(ready);
313 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1)
314 ready = 0;
315
316 if (!(ext && ready) && /* only listen if not already done by external process */
Willy Tarreaue2711c72019-02-27 15:39:41 +0100317 listen(fd, listener_backlog(listener)) < 0) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200318 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100319 msg = "cannot listen to UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200320 goto err_unlink_temp;
321 }
322
Willy Tarreauccfccef2014-05-10 01:49:15 +0200323 /* Point of no return: we are ready, we'll switch the sockets. We don't
Willy Tarreau92fb9832007-10-16 17:34:28 +0200324 * fear loosing the socket <path> because we have a copy of it in
Willy Tarreauccfccef2014-05-10 01:49:15 +0200325 * backname. Abstract sockets are not renamed.
Willy Tarreau92fb9832007-10-16 17:34:28 +0200326 */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200327 if (!ext && path[0] && rename(tempname, path) < 0) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200328 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100329 msg = "cannot switch final and temporary UNIX sockets";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200330 goto err_rename;
331 }
332
Willy Tarreau68986ab2017-06-16 10:34:20 +0200333 /* Cleanup: only unlink if we didn't inherit the fd from the parent */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200334 if (!ext && path[0])
Willy Tarreau40aa0702013-03-10 23:51:38 +0100335 unlink(backname);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200336
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100337 /* the socket is now listening */
338 listener->fd = fd;
339 listener->state = LI_LISTEN;
340
Willy Tarreaua9786b62018-01-25 07:22:13 +0100341 fd_insert(fd, listener, listener->proto->accept,
Willy Tarreau0948a782020-02-12 10:15:34 +0100342 thread_mask(listener->bind_conf->bind_thread) & all_threads_mask);
Willy Tarreaua9786b62018-01-25 07:22:13 +0100343
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200344 return err;
345
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100346 err_rename:
347 ret = rename(backname, path);
348 if (ret < 0 && errno == ENOENT)
349 unlink(path);
350 err_unlink_temp:
Jan Seda7319b642014-06-26 20:44:05 +0200351 if (!ext && path[0])
Willy Tarreau40aa0702013-03-10 23:51:38 +0100352 unlink(tempname);
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100353 close(fd);
354 err_unlink_back:
Jan Seda7319b642014-06-26 20:44:05 +0200355 if (!ext && path[0])
Willy Tarreau40aa0702013-03-10 23:51:38 +0100356 unlink(backname);
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100357 err_return:
Willy Tarreau40aa0702013-03-10 23:51:38 +0100358 if (msg && errlen) {
359 if (!ext)
360 snprintf(errmsg, errlen, "%s [%s]", msg, path);
361 else
362 snprintf(errmsg, errlen, "%s [fd %d]", msg, fd);
363 }
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200364 return err;
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100365}
366
367/* This function closes the UNIX sockets for the specified listener.
368 * The listener enters the LI_ASSIGNED state. It always returns ERR_NONE.
369 */
370static int uxst_unbind_listener(struct listener *listener)
371{
Willy Tarreaube58c382011-07-24 18:28:10 +0200372 if (listener->state > LI_ASSIGNED) {
373 unbind_listener(listener);
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100374 }
375 return ERR_NONE;
376}
377
Willy Tarreau32282382017-09-15 07:44:44 +0200378/* Add <listener> to the list of unix stream listeners (port is ignored). The
379 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
380 * The number of listeners for the protocol is updated.
Willy Tarreaudaacf362019-07-24 16:45:02 +0200381 *
382 * Must be called with proto_lock held.
383 *
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100384 */
Willy Tarreau9d5be5c2017-09-15 07:55:51 +0200385static void uxst_add_listener(struct listener *listener, int port)
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100386{
387 if (listener->state != LI_INIT)
388 return;
389 listener->state = LI_ASSIGNED;
390 listener->proto = &proto_unix;
391 LIST_ADDQ(&proto_unix.listeners, &listener->proto_list);
392 proto_unix.nb_listeners++;
393}
394
Willy Tarreaufd0e0082014-07-07 21:07:51 +0200395/* Pause a listener. Returns < 0 in case of failure, 0 if the listener
396 * was totally stopped, or > 0 if correctly paused. Nothing is done for
397 * plain unix sockets since currently it's the new process which handles
398 * the renaming. Abstract sockets are completely unbound.
399 */
Willy Tarreau31794892017-09-15 07:59:31 +0200400static int uxst_pause_listener(struct listener *l)
Willy Tarreaufd0e0082014-07-07 21:07:51 +0200401{
402 if (((struct sockaddr_un *)&l->addr)->sun_path[0])
403 return 1;
404
Christopher Faulet510c0d62018-03-16 10:04:47 +0100405 /* Listener's lock already held. Call lockless version of
406 * unbind_listener. */
407 do_unbind_listener(l, 1);
Willy Tarreaufd0e0082014-07-07 21:07:51 +0200408 return 0;
409}
410
Willy Tarreau47f48c42014-05-09 22:57:47 +0200411
412/*
413 * This function initiates a UNIX connection establishment to the target assigned
Willy Tarreauca9f5a92019-07-17 16:40:37 +0200414 * to connection <conn> using (si->{target,dst}). The source address is ignored
Willy Tarreau47f48c42014-05-09 22:57:47 +0200415 * and will be selected by the system. conn->target may point either to a valid
416 * server or to a backend, depending on conn->target. Only OBJ_TYPE_PROXY and
417 * OBJ_TYPE_SERVER are supported. The <data> parameter is a boolean indicating
418 * whether there are data waiting for being sent or not, in order to adjust data
419 * write polling and on some platforms. The <delack> argument is ignored.
420 *
421 * Note that a pending send_proxy message accounts for data.
422 *
423 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +0200424 * - SF_ERR_NONE if everything's OK
425 * - SF_ERR_SRVTO if there are no more servers
426 * - SF_ERR_SRVCL if the connection was refused by the server
427 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
428 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
429 * - SF_ERR_INTERNAL for any other purely internal errors
Tim Düsterhus4896c442016-11-29 02:15:19 +0100430 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau47f48c42014-05-09 22:57:47 +0200431 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200432 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
Willy Tarreau47f48c42014-05-09 22:57:47 +0200433 * it's invalid and the caller has nothing to do.
434 */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200435static int uxst_connect_server(struct connection *conn, int flags)
Willy Tarreau47f48c42014-05-09 22:57:47 +0200436{
437 int fd;
438 struct server *srv;
439 struct proxy *be;
440
Willy Tarreau47f48c42014-05-09 22:57:47 +0200441 switch (obj_type(conn->target)) {
442 case OBJ_TYPE_PROXY:
443 be = objt_proxy(conn->target);
444 srv = NULL;
445 break;
446 case OBJ_TYPE_SERVER:
447 srv = objt_server(conn->target);
448 be = srv->proxy;
449 break;
450 default:
451 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200452 return SF_ERR_INTERNAL;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200453 }
454
Willy Tarreau585744b2017-08-24 14:31:19 +0200455 if ((fd = conn->handle.fd = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
Willy Tarreau47f48c42014-05-09 22:57:47 +0200456 qfprintf(stderr, "Cannot get a server socket.\n");
457
458 if (errno == ENFILE) {
459 conn->err_code = CO_ER_SYS_FDLIM;
460 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100461 "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n",
462 be->id, global.maxsock);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200463 }
464 else if (errno == EMFILE) {
465 conn->err_code = CO_ER_PROC_FDLIM;
466 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100467 "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n",
468 be->id, global.maxsock);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200469 }
470 else if (errno == ENOBUFS || errno == ENOMEM) {
471 conn->err_code = CO_ER_SYS_MEMLIM;
472 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100473 "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n",
474 be->id, global.maxsock);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200475 }
476 else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
477 conn->err_code = CO_ER_NOPROTO;
478 }
479 else
480 conn->err_code = CO_ER_SOCK_ERR;
481
482 /* this is a resource error */
483 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200484 return SF_ERR_RESOURCE;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200485 }
486
487 if (fd >= global.maxsock) {
488 /* do not log anything there, it's a normal condition when this option
489 * is used to serialize connections to a server !
490 */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100491 ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
Willy Tarreau47f48c42014-05-09 22:57:47 +0200492 close(fd);
493 conn->err_code = CO_ER_CONF_FDLIM;
494 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200495 return SF_ERR_PRXCOND; /* it is a configuration limit */
Willy Tarreau47f48c42014-05-09 22:57:47 +0200496 }
497
498 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
499 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
500 close(fd);
501 conn->err_code = CO_ER_SOCK_ERR;
502 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200503 return SF_ERR_INTERNAL;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200504 }
505
William Lallemandc03eb012018-11-27 12:02:37 +0100506 if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) {
507 ha_alert("Cannot set CLOEXEC on client socket.\n");
508 close(fd);
509 conn->err_code = CO_ER_SOCK_ERR;
510 conn->flags |= CO_FL_ERROR;
511 return SF_ERR_INTERNAL;
512 }
513
Willy Tarreau47f48c42014-05-09 22:57:47 +0200514 /* if a send_proxy is there, there are data */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200515 if (conn->send_proxy_ofs)
516 flags |= CONNECT_HAS_DATA;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200517
518 if (global.tune.server_sndbuf)
519 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
520
521 if (global.tune.server_rcvbuf)
522 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
523
Willy Tarreauca9f5a92019-07-17 16:40:37 +0200524 if (connect(fd, (struct sockaddr *)conn->dst, get_addr_len(conn->dst)) == -1) {
Willy Tarreau94841792017-01-25 14:27:38 +0100525 if (errno == EINPROGRESS || errno == EALREADY) {
Willy Tarreau7bb21532014-05-10 09:48:28 +0200526 conn->flags |= CO_FL_WAIT_L4_CONN;
527 }
Willy Tarreau94841792017-01-25 14:27:38 +0100528 else if (errno == EISCONN) {
529 conn->flags &= ~CO_FL_WAIT_L4_CONN;
530 }
Willy Tarreau7bb21532014-05-10 09:48:28 +0200531 else if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau47f48c42014-05-09 22:57:47 +0200532 char *msg;
533 if (errno == EAGAIN || errno == EADDRNOTAVAIL) {
Lukas Tribus9f256d42016-01-26 20:33:14 +0100534 msg = "can't connect to destination unix socket, check backlog size on the server";
Willy Tarreau47f48c42014-05-09 22:57:47 +0200535 conn->err_code = CO_ER_FREE_PORTS;
536 }
537 else {
538 msg = "local address already in use";
539 conn->err_code = CO_ER_ADDR_INUSE;
540 }
541
542 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
543 close(fd);
544 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
545 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200546 return SF_ERR_RESOURCE;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200547 }
548 else if (errno == ETIMEDOUT) {
549 close(fd);
550 conn->err_code = CO_ER_SOCK_ERR;
551 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200552 return SF_ERR_SRVTO;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200553 }
554 else { // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
555 close(fd);
556 conn->err_code = CO_ER_SOCK_ERR;
557 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200558 return SF_ERR_SRVCL;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200559 }
560 }
Willy Tarreau7bb21532014-05-10 09:48:28 +0200561 else {
562 /* connect() already succeeded, which is quite usual for unix
Willy Tarreau94841792017-01-25 14:27:38 +0100563 * sockets. Let's avoid a second connect() probe to complete it.
Willy Tarreau7bb21532014-05-10 09:48:28 +0200564 */
565 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau7bb21532014-05-10 09:48:28 +0200566 }
Willy Tarreau47f48c42014-05-09 22:57:47 +0200567
568 conn->flags |= CO_FL_ADDR_TO_SET;
569
570 /* Prepare to send a few handshakes related to the on-wire protocol. */
571 if (conn->send_proxy_ofs)
572 conn->flags |= CO_FL_SEND_PROXY;
573
574 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreau7bb21532014-05-10 09:48:28 +0200575 fdtab[fd].linger_risk = 0; /* no need to disable lingering */
Willy Tarreau47f48c42014-05-09 22:57:47 +0200576
Willy Tarreau4c69cff2020-03-04 16:38:00 +0100577 if (conn->flags & CO_FL_WAIT_L4_CONN) {
578 fd_want_send(fd);
579 fd_cant_send(fd);
580 }
Willy Tarreauccf3f6d2019-09-05 17:05:05 +0200581
Willy Tarreau47f48c42014-05-09 22:57:47 +0200582 if (conn_xprt_init(conn) < 0) {
Willy Tarreau8c829012017-10-05 18:02:11 +0200583 conn_full_close(conn);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200584 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200585 return SF_ERR_RESOURCE;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200586 }
587
Willy Tarreaue7dff022015-04-03 01:14:29 +0200588 return SF_ERR_NONE; /* connection is OK */
Willy Tarreau47f48c42014-05-09 22:57:47 +0200589}
590
591
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100592/********************************
593 * 3) protocol-oriented functions
594 ********************************/
595
596
Willy Tarreau92fb9832007-10-16 17:34:28 +0200597/* This function creates all UNIX sockets bound to the protocol entry <proto>.
598 * It is intended to be used as the protocol's bind_all() function.
599 * The sockets will be registered but not added to any fd_set, in order not to
600 * loose them across the fork(). A call to uxst_enable_listeners() is needed
601 * to complete initialization.
602 *
Willy Tarreaudaacf362019-07-24 16:45:02 +0200603 * Must be called with proto_lock held.
604 *
Willy Tarreau92fb9832007-10-16 17:34:28 +0200605 * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL.
606 */
Emeric Bruncf20bf12010-10-22 16:06:11 +0200607static int uxst_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreau92fb9832007-10-16 17:34:28 +0200608{
609 struct listener *listener;
610 int err = ERR_NONE;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200611
612 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200613 err |= uxst_bind_listener(listener, errmsg, errlen);
614 if (err & ERR_ABORT)
615 break;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200616 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200617 return err;
618}
619
Willy Tarreau92fb9832007-10-16 17:34:28 +0200620
621/* This function stops all listening UNIX sockets bound to the protocol
622 * <proto>. It does not detaches them from the protocol.
623 * It always returns ERR_NONE.
Willy Tarreaudaacf362019-07-24 16:45:02 +0200624 *
625 * Must be called with proto_lock held.
626 *
Willy Tarreau92fb9832007-10-16 17:34:28 +0200627 */
628static int uxst_unbind_listeners(struct protocol *proto)
629{
630 struct listener *listener;
631
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100632 list_for_each_entry(listener, &proto->listeners, proto_list)
633 uxst_unbind_listener(listener);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200634 return ERR_NONE;
635}
636
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200637/* parse the "mode" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200638static 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 +0200639{
Willy Tarreaua1a247b2017-10-04 14:43:44 +0200640 char *endptr;
641
642 conf->ux.mode = strtol(args[cur_arg + 1], &endptr, 8);
643
644 if (!*args[cur_arg + 1] || *endptr) {
645 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 +0200646 return ERR_ALERT | ERR_FATAL;
647 }
648
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200649 return 0;
650}
651
652/* parse the "gid" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200653static 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 +0200654{
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200655 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200656 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200657 return ERR_ALERT | ERR_FATAL;
658 }
659
Willy Tarreau290e63a2012-09-20 18:07:14 +0200660 conf->ux.gid = atol(args[cur_arg + 1]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200661 return 0;
662}
663
664/* parse the "group" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200665static 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 +0200666{
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200667 struct group *group;
668
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200669 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200670 memprintf(err, "'%s' : missing group name", args[cur_arg]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200671 return ERR_ALERT | ERR_FATAL;
672 }
673
674 group = getgrnam(args[cur_arg + 1]);
675 if (!group) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200676 memprintf(err, "'%s' : unknown group name '%s'", args[cur_arg], args[cur_arg + 1]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200677 return ERR_ALERT | ERR_FATAL;
678 }
679
Willy Tarreau290e63a2012-09-20 18:07:14 +0200680 conf->ux.gid = group->gr_gid;
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200681 return 0;
682}
683
684/* parse the "uid" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200685static 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 +0200686{
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200687 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200688 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200689 return ERR_ALERT | ERR_FATAL;
690 }
691
Willy Tarreau290e63a2012-09-20 18:07:14 +0200692 conf->ux.uid = atol(args[cur_arg + 1]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200693 return 0;
694}
695
696/* parse the "user" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200697static 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 +0200698{
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200699 struct passwd *user;
700
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200701 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200702 memprintf(err, "'%s' : missing user name", args[cur_arg]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200703 return ERR_ALERT | ERR_FATAL;
704 }
705
706 user = getpwnam(args[cur_arg + 1]);
707 if (!user) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200708 memprintf(err, "'%s' : unknown user name '%s'", args[cur_arg], args[cur_arg + 1]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200709 return ERR_ALERT | ERR_FATAL;
710 }
711
Willy Tarreau290e63a2012-09-20 18:07:14 +0200712 conf->ux.uid = user->pw_uid;
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200713 return 0;
714}
715
716/* Note: must not be declared <const> as its list will be overwritten.
717 * Please take care of keeping this list alphabetically sorted, doing so helps
718 * all code contributors.
719 * Optional keywords are also declared with a NULL ->parse() function so that
720 * the config parser can report an appropriate error when a known keyword was
721 * not enabled.
722 */
Willy Tarreau51fb7652012-09-18 18:24:39 +0200723static struct bind_kw_list bind_kws = { "UNIX", { }, {
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200724 { "gid", bind_parse_gid, 1 }, /* set the socket's gid */
725 { "group", bind_parse_group, 1 }, /* set the socket's gid from the group name */
726 { "mode", bind_parse_mode, 1 }, /* set the socket's mode (eg: 0644)*/
727 { "uid", bind_parse_uid, 1 }, /* set the socket's uid */
728 { "user", bind_parse_user, 1 }, /* set the socket's uid from the user name */
729 { NULL, NULL, 0 },
730}};
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100731
Willy Tarreau0108d902018-11-25 19:14:37 +0100732INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200733
734/*
735 * Local variables:
736 * c-indent-level: 8
737 * c-basic-offset: 8
738 * End:
739 */