blob: df813a199f40a07b99f9a96b9a3ba82926e6dc0f [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
29#include <common/compat.h>
30#include <common/config.h>
31#include <common/debug.h>
Willy Tarreaud740bab2007-10-28 11:14:07 +010032#include <common/errors.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010033#include <common/initcall.h>
Willy Tarreau92fb9832007-10-16 17:34:28 +020034#include <common/mini-clist.h>
35#include <common/standard.h>
36#include <common/time.h>
37#include <common/version.h>
38
Willy Tarreau92fb9832007-10-16 17:34:28 +020039#include <types/global.h>
Willy Tarreau92fb9832007-10-16 17:34:28 +020040
Willy Tarreau47f48c42014-05-09 22:57:47 +020041#include <proto/connection.h>
Willy Tarreau92fb9832007-10-16 17:34:28 +020042#include <proto/fd.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020043#include <proto/listener.h>
Willy Tarreau92fb9832007-10-16 17:34:28 +020044#include <proto/log.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020045#include <proto/protocol.h>
Willy Tarreau92fb9832007-10-16 17:34:28 +020046#include <proto/task.h>
47
Emeric Bruncf20bf12010-10-22 16:06:11 +020048static int uxst_bind_listener(struct listener *listener, char *errmsg, int errlen);
49static int uxst_bind_listeners(struct protocol *proto, char *errmsg, int errlen);
Willy Tarreaudabf2e22007-10-28 21:59:24 +010050static int uxst_unbind_listeners(struct protocol *proto);
Olivier Houchardfdcb0072019-05-06 18:32:29 +020051static int uxst_connect_server(struct connection *conn, int flags);
Willy Tarreau9d5be5c2017-09-15 07:55:51 +020052static void uxst_add_listener(struct listener *listener, int port);
Willy Tarreau31794892017-09-15 07:59:31 +020053static int uxst_pause_listener(struct listener *l);
54static int uxst_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir);
55static int uxst_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir);
Willy Tarreaudabf2e22007-10-28 21:59:24 +010056
57/* Note: must not be declared <const> as its list will be overwritten */
58static struct protocol proto_unix = {
59 .name = "unix_stream",
60 .sock_domain = PF_UNIX,
61 .sock_type = SOCK_STREAM,
62 .sock_prot = 0,
63 .sock_family = AF_UNIX,
64 .sock_addrlen = sizeof(struct sockaddr_un),
65 .l3_addrlen = sizeof(((struct sockaddr_un*)0)->sun_path),/* path len */
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020066 .accept = &listener_accept,
Willy Tarreau47f48c42014-05-09 22:57:47 +020067 .connect = &uxst_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020068 .bind = uxst_bind_listener,
Willy Tarreaudabf2e22007-10-28 21:59:24 +010069 .bind_all = uxst_bind_listeners,
70 .unbind_all = uxst_unbind_listeners,
71 .enable_all = enable_all_listeners,
72 .disable_all = disable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +020073 .get_src = uxst_get_src,
74 .get_dst = uxst_get_dst,
Willy Tarreaufd0e0082014-07-07 21:07:51 +020075 .pause = uxst_pause_listener,
Willy Tarreau9d5be5c2017-09-15 07:55:51 +020076 .add = uxst_add_listener,
Willy Tarreaudabf2e22007-10-28 21:59:24 +010077 .listeners = LIST_HEAD_INIT(proto_unix.listeners),
78 .nb_listeners = 0,
79};
80
Willy Tarreau0108d902018-11-25 19:14:37 +010081INITCALL1(STG_REGISTER, protocol_register, &proto_unix);
82
Willy Tarreaudabf2e22007-10-28 21:59:24 +010083/********************************
84 * 1) low-level socket functions
85 ********************************/
86
Willy Tarreau59b94792012-05-11 16:16:40 +020087/*
88 * Retrieves the source address for the socket <fd>, with <dir> indicating
89 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
90 * success, -1 in case of error. The socket's source address is stored in
91 * <sa> for <salen> bytes.
92 */
Willy Tarreau31794892017-09-15 07:59:31 +020093static int uxst_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
Willy Tarreau59b94792012-05-11 16:16:40 +020094{
95 if (dir)
96 return getsockname(fd, sa, &salen);
97 else
98 return getpeername(fd, sa, &salen);
99}
100
101
102/*
103 * Retrieves the original destination address for the socket <fd>, with <dir>
104 * indicating if we're a listener (=0) or an initiator (!=0). It returns 0 in
105 * case of success, -1 in case of error. The socket's source address is stored
106 * in <sa> for <salen> bytes.
107 */
Willy Tarreau31794892017-09-15 07:59:31 +0200108static int uxst_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
Willy Tarreau59b94792012-05-11 16:16:40 +0200109{
110 if (dir)
111 return getpeername(fd, sa, &salen);
112 else
113 return getsockname(fd, sa, &salen);
114}
115
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100116
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100117/********************************
118 * 2) listener-oriented functions
119 ********************************/
120
121
Olivier Houchardf886e342017-04-05 22:24:59 +0200122static int uxst_find_compatible_fd(struct listener *l)
123{
124 struct xfer_sock_list *xfer_sock = xfer_sock_list;
125 int ret = -1;
126
127 while (xfer_sock) {
128 struct sockaddr_un *un1 = (void *)&l->addr;
129 struct sockaddr_un *un2 = (void *)&xfer_sock->addr;
130
131 /*
132 * The bound socket's path as returned by getsockaddr
133 * will be the temporary name <sockname>.XXXXX.tmp,
134 * so we can't just compare the two names
135 */
136 if (xfer_sock->addr.ss_family == AF_UNIX &&
137 strncmp(un1->sun_path, un2->sun_path,
138 strlen(un1->sun_path)) == 0) {
139 char *after_sockname = un2->sun_path +
140 strlen(un1->sun_path);
141 /* Make a reasonnable effort to check that
142 * it is indeed a haproxy-generated temporary
143 * name, it's not perfect, but probably good enough.
144 */
145 if (after_sockname[0] == '.') {
146 after_sockname++;
147 while (after_sockname[0] >= '0' &&
148 after_sockname[0] <= '9')
149 after_sockname++;
150 if (!strcmp(after_sockname, ".tmp"))
151 break;
Olivier Houchardb4dd15b2018-06-06 18:34:34 +0200152 /* abns sockets sun_path starts with a \0 */
153 } else if (un1->sun_path[0] == 0
154 && un2->sun_path[0] == 0
155 && !memcmp(&un1->sun_path[1], &un2->sun_path[1],
156 sizeof(un1->sun_path) - 1))
157 break;
Olivier Houchardf886e342017-04-05 22:24:59 +0200158 }
159 xfer_sock = xfer_sock->next;
160 }
161 if (xfer_sock != NULL) {
162 ret = xfer_sock->fd;
163 if (xfer_sock == xfer_sock_list)
164 xfer_sock_list = xfer_sock->next;
165 if (xfer_sock->prev)
166 xfer_sock->prev->next = xfer_sock->next;
167 if (xfer_sock->next)
Olivier Houchardec9516a2018-03-08 18:25:49 +0100168 xfer_sock->next->prev = xfer_sock->prev;
Olivier Houchardf886e342017-04-05 22:24:59 +0200169 free(xfer_sock);
170 }
171 return ret;
172
173}
174
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100175/* This function creates a UNIX socket associated to the listener. It changes
176 * the state from ASSIGNED to LISTEN. The socket is NOT enabled for polling.
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100177 * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL. It
178 * may return a warning or an error message in <errmsg> if the message is at
179 * most <errlen> bytes long (including '\0'). Note that <errmsg> may be NULL if
180 * <errlen> is also zero.
Willy Tarreau92fb9832007-10-16 17:34:28 +0200181 */
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100182static int uxst_bind_listener(struct listener *listener, char *errmsg, int errlen)
Willy Tarreau92fb9832007-10-16 17:34:28 +0200183{
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100184 int fd;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200185 char tempname[MAXPATHLEN];
186 char backname[MAXPATHLEN];
187 struct sockaddr_un addr;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100188 const char *msg = NULL;
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100189 const char *path;
Willy Tarreau5d1c9482020-02-11 06:43:37 +0100190 int maxpathlen;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100191 int ext, ready;
192 socklen_t ready_len;
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200193 int err;
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100194 int ret;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200195
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200196 err = ERR_NONE;
197
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100198 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100199 if (errlen)
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100200 *errmsg = 0;
201
202 if (listener->state != LI_ASSIGNED)
203 return ERR_NONE; /* already bound */
204
Olivier Houchardf886e342017-04-05 22:24:59 +0200205 if (listener->fd == -1)
206 listener->fd = uxst_find_compatible_fd(listener);
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100207 path = ((struct sockaddr_un *)&listener->addr)->sun_path;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200208
Willy Tarreau5d1c9482020-02-11 06:43:37 +0100209 maxpathlen = MIN(MAXPATHLEN, sizeof(addr.sun_path));
210
Willy Tarreau40aa0702013-03-10 23:51:38 +0100211 /* if the listener already has an fd assigned, then we were offered the
212 * fd by an external process (most likely the parent), and we don't want
213 * to create a new socket. However we still want to set a few flags on
214 * the socket.
215 */
216 fd = listener->fd;
217 ext = (fd >= 0);
218 if (ext)
219 goto fd_ready;
220
Willy Tarreauccfccef2014-05-10 01:49:15 +0200221 if (path[0]) {
Willy Tarreau5d1c9482020-02-11 06:43:37 +0100222 ret = snprintf(tempname, maxpathlen, "%s.%d.tmp", path, pid);
223 if (ret < 0 || ret >= maxpathlen) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200224 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreau5d1c9482020-02-11 06:43:37 +0100225 msg = "name too long for UNIX socket (limit usually 97)";
Willy Tarreauccfccef2014-05-10 01:49:15 +0200226 goto err_return;
227 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200228
Willy Tarreau5d1c9482020-02-11 06:43:37 +0100229 ret = snprintf(backname, maxpathlen, "%s.%d.bak", path, pid);
230 if (ret < 0 || ret >= maxpathlen) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200231 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreau5d1c9482020-02-11 06:43:37 +0100232 msg = "name too long for UNIX socket (limit usually 97)";
Willy Tarreauccfccef2014-05-10 01:49:15 +0200233 goto err_return;
234 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200235
Willy Tarreauccfccef2014-05-10 01:49:15 +0200236 /* 2. clean existing orphaned entries */
237 if (unlink(tempname) < 0 && errno != ENOENT) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200238 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200239 msg = "error when trying to unlink previous UNIX socket";
240 goto err_return;
241 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200242
Willy Tarreauccfccef2014-05-10 01:49:15 +0200243 if (unlink(backname) < 0 && errno != ENOENT) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200244 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200245 msg = "error when trying to unlink previous UNIX socket";
246 goto err_return;
247 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200248
Willy Tarreauccfccef2014-05-10 01:49:15 +0200249 /* 3. backup existing socket */
250 if (link(path, backname) < 0 && errno != ENOENT) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200251 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200252 msg = "error when trying to preserve previous UNIX socket";
253 goto err_return;
254 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200255
Willy Tarreaud668dcf2019-12-11 16:29:10 +0100256 strncpy(addr.sun_path, tempname, sizeof(addr.sun_path) - 1);
Willy Tarreauccfccef2014-05-10 01:49:15 +0200257 addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200258 }
Willy Tarreauccfccef2014-05-10 01:49:15 +0200259 else {
260 /* first char is zero, it's an abstract socket whose address
261 * is defined by all the bytes past this zero.
262 */
263 memcpy(addr.sun_path, path, sizeof(addr.sun_path));
264 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200265 addr.sun_family = AF_UNIX;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200266
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100267 fd = socket(PF_UNIX, SOCK_STREAM, 0);
268 if (fd < 0) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200269 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100270 msg = "cannot create UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200271 goto err_unlink_back;
272 }
273
Willy Tarreau40aa0702013-03-10 23:51:38 +0100274 fd_ready:
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100275 if (fd >= global.maxsock) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200276 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100277 msg = "socket(): not enough free sockets, raise -n argument";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200278 goto err_unlink_temp;
279 }
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100280
281 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200282 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100283 msg = "cannot make UNIX socket non-blocking";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200284 goto err_unlink_temp;
285 }
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100286
Willy Tarreau40aa0702013-03-10 23:51:38 +0100287 if (!ext && bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
Willy Tarreau92fb9832007-10-16 17:34:28 +0200288 /* note that bind() creates the socket <tempname> on the file system */
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200289 if (errno == EADDRINUSE) {
290 /* the old process might still own it, let's retry */
291 err |= ERR_RETRYABLE | ERR_ALERT;
292 msg = "cannot listen to socket";
293 }
294 else {
295 err |= ERR_FATAL | ERR_ALERT;
296 msg = "cannot bind UNIX socket";
297 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200298 goto err_unlink_temp;
299 }
300
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100301 /* <uid> and <gid> different of -1 will be used to change the socket owner.
302 * If <mode> is not 0, it will be used to restrict access to the socket.
303 * While it is known not to be portable on every OS, it's still useful
Willy Tarreauccfccef2014-05-10 01:49:15 +0200304 * where it works. We also don't change permissions on abstract sockets.
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100305 */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200306 if (!ext && path[0] &&
Willy Tarreau40aa0702013-03-10 23:51:38 +0100307 (((listener->bind_conf->ux.uid != -1 || listener->bind_conf->ux.gid != -1) &&
308 (chown(tempname, listener->bind_conf->ux.uid, listener->bind_conf->ux.gid) == -1)) ||
309 (listener->bind_conf->ux.mode != 0 && chmod(tempname, listener->bind_conf->ux.mode) == -1))) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200310 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100311 msg = "cannot change UNIX socket ownership";
Willy Tarreaue6ad2b12007-10-18 12:45:54 +0200312 goto err_unlink_temp;
313 }
314
Willy Tarreau40aa0702013-03-10 23:51:38 +0100315 ready = 0;
316 ready_len = sizeof(ready);
317 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1)
318 ready = 0;
319
320 if (!(ext && ready) && /* only listen if not already done by external process */
Willy Tarreaue2711c72019-02-27 15:39:41 +0100321 listen(fd, listener_backlog(listener)) < 0) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200322 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100323 msg = "cannot listen to UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200324 goto err_unlink_temp;
325 }
326
Willy Tarreauccfccef2014-05-10 01:49:15 +0200327 /* Point of no return: we are ready, we'll switch the sockets. We don't
Willy Tarreau92fb9832007-10-16 17:34:28 +0200328 * fear loosing the socket <path> because we have a copy of it in
Willy Tarreauccfccef2014-05-10 01:49:15 +0200329 * backname. Abstract sockets are not renamed.
Willy Tarreau92fb9832007-10-16 17:34:28 +0200330 */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200331 if (!ext && path[0] && rename(tempname, path) < 0) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200332 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100333 msg = "cannot switch final and temporary UNIX sockets";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200334 goto err_rename;
335 }
336
Willy Tarreau68986ab2017-06-16 10:34:20 +0200337 /* Cleanup: only unlink if we didn't inherit the fd from the parent */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200338 if (!ext && path[0])
Willy Tarreau40aa0702013-03-10 23:51:38 +0100339 unlink(backname);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200340
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100341 /* the socket is now listening */
342 listener->fd = fd;
343 listener->state = LI_LISTEN;
344
Willy Tarreaua9786b62018-01-25 07:22:13 +0100345 fd_insert(fd, listener, listener->proto->accept,
Willy Tarreau3f1199d2020-02-12 10:15:34 +0100346 thread_mask(listener->bind_conf->bind_thread) & all_threads_mask);
Willy Tarreaua9786b62018-01-25 07:22:13 +0100347
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200348 return err;
349
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100350 err_rename:
351 ret = rename(backname, path);
352 if (ret < 0 && errno == ENOENT)
353 unlink(path);
354 err_unlink_temp:
Jan Seda7319b642014-06-26 20:44:05 +0200355 if (!ext && path[0])
Willy Tarreau40aa0702013-03-10 23:51:38 +0100356 unlink(tempname);
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100357 close(fd);
358 err_unlink_back:
Jan Seda7319b642014-06-26 20:44:05 +0200359 if (!ext && path[0])
Willy Tarreau40aa0702013-03-10 23:51:38 +0100360 unlink(backname);
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100361 err_return:
Willy Tarreau40aa0702013-03-10 23:51:38 +0100362 if (msg && errlen) {
363 if (!ext)
364 snprintf(errmsg, errlen, "%s [%s]", msg, path);
365 else
366 snprintf(errmsg, errlen, "%s [fd %d]", msg, fd);
367 }
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200368 return err;
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100369}
370
371/* This function closes the UNIX sockets for the specified listener.
372 * The listener enters the LI_ASSIGNED state. It always returns ERR_NONE.
373 */
374static int uxst_unbind_listener(struct listener *listener)
375{
Willy Tarreaube58c382011-07-24 18:28:10 +0200376 if (listener->state > LI_ASSIGNED) {
377 unbind_listener(listener);
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100378 }
379 return ERR_NONE;
380}
381
Willy Tarreau32282382017-09-15 07:44:44 +0200382/* Add <listener> to the list of unix stream listeners (port is ignored). The
383 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
384 * The number of listeners for the protocol is updated.
Willy Tarreau937604b2019-07-24 16:45:02 +0200385 *
386 * Must be called with proto_lock held.
387 *
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100388 */
Willy Tarreau9d5be5c2017-09-15 07:55:51 +0200389static void uxst_add_listener(struct listener *listener, int port)
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100390{
391 if (listener->state != LI_INIT)
392 return;
393 listener->state = LI_ASSIGNED;
394 listener->proto = &proto_unix;
395 LIST_ADDQ(&proto_unix.listeners, &listener->proto_list);
396 proto_unix.nb_listeners++;
397}
398
Willy Tarreaufd0e0082014-07-07 21:07:51 +0200399/* Pause a listener. Returns < 0 in case of failure, 0 if the listener
400 * was totally stopped, or > 0 if correctly paused. Nothing is done for
401 * plain unix sockets since currently it's the new process which handles
402 * the renaming. Abstract sockets are completely unbound.
403 */
Willy Tarreau31794892017-09-15 07:59:31 +0200404static int uxst_pause_listener(struct listener *l)
Willy Tarreaufd0e0082014-07-07 21:07:51 +0200405{
406 if (((struct sockaddr_un *)&l->addr)->sun_path[0])
407 return 1;
408
Christopher Faulet510c0d62018-03-16 10:04:47 +0100409 /* Listener's lock already held. Call lockless version of
410 * unbind_listener. */
411 do_unbind_listener(l, 1);
Willy Tarreaufd0e0082014-07-07 21:07:51 +0200412 return 0;
413}
414
Willy Tarreau47f48c42014-05-09 22:57:47 +0200415
416/*
417 * This function initiates a UNIX connection establishment to the target assigned
418 * to connection <conn> using (si->{target,addr.to}). The source address is ignored
419 * and will be selected by the system. conn->target may point either to a valid
420 * server or to a backend, depending on conn->target. Only OBJ_TYPE_PROXY and
421 * OBJ_TYPE_SERVER are supported. The <data> parameter is a boolean indicating
422 * whether there are data waiting for being sent or not, in order to adjust data
423 * write polling and on some platforms. The <delack> argument is ignored.
424 *
425 * Note that a pending send_proxy message accounts for data.
426 *
427 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +0200428 * - SF_ERR_NONE if everything's OK
429 * - SF_ERR_SRVTO if there are no more servers
430 * - SF_ERR_SRVCL if the connection was refused by the server
431 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
432 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
433 * - SF_ERR_INTERNAL for any other purely internal errors
Tim Düsterhus4896c442016-11-29 02:15:19 +0100434 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau47f48c42014-05-09 22:57:47 +0200435 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200436 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
Willy Tarreau47f48c42014-05-09 22:57:47 +0200437 * it's invalid and the caller has nothing to do.
438 */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200439static int uxst_connect_server(struct connection *conn, int flags)
Willy Tarreau47f48c42014-05-09 22:57:47 +0200440{
441 int fd;
442 struct server *srv;
443 struct proxy *be;
444
Willy Tarreau47f48c42014-05-09 22:57:47 +0200445 switch (obj_type(conn->target)) {
446 case OBJ_TYPE_PROXY:
447 be = objt_proxy(conn->target);
448 srv = NULL;
449 break;
450 case OBJ_TYPE_SERVER:
451 srv = objt_server(conn->target);
452 be = srv->proxy;
453 break;
454 default:
455 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200456 return SF_ERR_INTERNAL;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200457 }
458
Willy Tarreau585744b2017-08-24 14:31:19 +0200459 if ((fd = conn->handle.fd = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
Willy Tarreau47f48c42014-05-09 22:57:47 +0200460 qfprintf(stderr, "Cannot get a server socket.\n");
461
462 if (errno == ENFILE) {
463 conn->err_code = CO_ER_SYS_FDLIM;
464 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100465 "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n",
466 be->id, global.maxsock);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200467 }
468 else if (errno == EMFILE) {
469 conn->err_code = CO_ER_PROC_FDLIM;
470 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100471 "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n",
472 be->id, global.maxsock);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200473 }
474 else if (errno == ENOBUFS || errno == ENOMEM) {
475 conn->err_code = CO_ER_SYS_MEMLIM;
476 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100477 "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n",
478 be->id, global.maxsock);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200479 }
480 else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
481 conn->err_code = CO_ER_NOPROTO;
482 }
483 else
484 conn->err_code = CO_ER_SOCK_ERR;
485
486 /* this is a resource error */
487 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200488 return SF_ERR_RESOURCE;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200489 }
490
491 if (fd >= global.maxsock) {
492 /* do not log anything there, it's a normal condition when this option
493 * is used to serialize connections to a server !
494 */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100495 ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
Willy Tarreau47f48c42014-05-09 22:57:47 +0200496 close(fd);
497 conn->err_code = CO_ER_CONF_FDLIM;
498 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200499 return SF_ERR_PRXCOND; /* it is a configuration limit */
Willy Tarreau47f48c42014-05-09 22:57:47 +0200500 }
501
502 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
503 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
504 close(fd);
505 conn->err_code = CO_ER_SOCK_ERR;
506 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200507 return SF_ERR_INTERNAL;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200508 }
509
William Lallemandc03eb012018-11-27 12:02:37 +0100510 if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) {
511 ha_alert("Cannot set CLOEXEC on client socket.\n");
512 close(fd);
513 conn->err_code = CO_ER_SOCK_ERR;
514 conn->flags |= CO_FL_ERROR;
515 return SF_ERR_INTERNAL;
516 }
517
Willy Tarreau47f48c42014-05-09 22:57:47 +0200518 /* if a send_proxy is there, there are data */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200519 if (conn->send_proxy_ofs)
520 flags |= CONNECT_HAS_DATA;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200521
522 if (global.tune.server_sndbuf)
523 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
524
525 if (global.tune.server_rcvbuf)
526 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
527
Willy Tarreau7bb21532014-05-10 09:48:28 +0200528 if (connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) == -1) {
Willy Tarreau94841792017-01-25 14:27:38 +0100529 if (errno == EINPROGRESS || errno == EALREADY) {
Willy Tarreau7bb21532014-05-10 09:48:28 +0200530 conn->flags |= CO_FL_WAIT_L4_CONN;
531 }
Willy Tarreau94841792017-01-25 14:27:38 +0100532 else if (errno == EISCONN) {
533 conn->flags &= ~CO_FL_WAIT_L4_CONN;
534 }
Willy Tarreau7bb21532014-05-10 09:48:28 +0200535 else if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau47f48c42014-05-09 22:57:47 +0200536 char *msg;
537 if (errno == EAGAIN || errno == EADDRNOTAVAIL) {
Lukas Tribus9f256d42016-01-26 20:33:14 +0100538 msg = "can't connect to destination unix socket, check backlog size on the server";
Willy Tarreau47f48c42014-05-09 22:57:47 +0200539 conn->err_code = CO_ER_FREE_PORTS;
540 }
541 else {
542 msg = "local address already in use";
543 conn->err_code = CO_ER_ADDR_INUSE;
544 }
545
546 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
547 close(fd);
548 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
549 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200550 return SF_ERR_RESOURCE;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200551 }
552 else if (errno == ETIMEDOUT) {
553 close(fd);
554 conn->err_code = CO_ER_SOCK_ERR;
555 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200556 return SF_ERR_SRVTO;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200557 }
558 else { // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
559 close(fd);
560 conn->err_code = CO_ER_SOCK_ERR;
561 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200562 return SF_ERR_SRVCL;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200563 }
564 }
Willy Tarreau7bb21532014-05-10 09:48:28 +0200565 else {
566 /* connect() already succeeded, which is quite usual for unix
Willy Tarreau94841792017-01-25 14:27:38 +0100567 * sockets. Let's avoid a second connect() probe to complete it.
Willy Tarreau7bb21532014-05-10 09:48:28 +0200568 */
569 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau7bb21532014-05-10 09:48:28 +0200570 }
Willy Tarreau47f48c42014-05-09 22:57:47 +0200571
572 conn->flags |= CO_FL_ADDR_TO_SET;
573
574 /* Prepare to send a few handshakes related to the on-wire protocol. */
575 if (conn->send_proxy_ofs)
576 conn->flags |= CO_FL_SEND_PROXY;
577
578 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreau7bb21532014-05-10 09:48:28 +0200579 fdtab[fd].linger_risk = 0; /* no need to disable lingering */
Willy Tarreau47f48c42014-05-09 22:57:47 +0200580
581 if (conn_xprt_init(conn) < 0) {
Willy Tarreau8c829012017-10-05 18:02:11 +0200582 conn_full_close(conn);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200583 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200584 return SF_ERR_RESOURCE;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200585 }
586
Olivier Houchard03abf2d2019-05-28 10:12:02 +0200587 conn_xprt_want_send(conn); /* for connect status, proxy protocol or SSL */
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 Tarreau937604b2019-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 Tarreau937604b2019-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 */