blob: d41e1c0a4146c5ba429380c139a563475de2b1bc [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 Tarreau92fb9832007-10-16 17:34:28 +020030#include <common/debug.h>
Willy Tarreaud740bab2007-10-28 11:14:07 +010031#include <common/errors.h>
Willy Tarreau92fb9832007-10-16 17:34:28 +020032#include <common/mini-clist.h>
33#include <common/standard.h>
34#include <common/time.h>
35#include <common/version.h>
36
Willy Tarreau92fb9832007-10-16 17:34:28 +020037#include <types/global.h>
Willy Tarreau92fb9832007-10-16 17:34:28 +020038
Willy Tarreau47f48c42014-05-09 22:57:47 +020039#include <proto/connection.h>
Willy Tarreau92fb9832007-10-16 17:34:28 +020040#include <proto/fd.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020041#include <proto/listener.h>
Willy Tarreau92fb9832007-10-16 17:34:28 +020042#include <proto/log.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020043#include <proto/protocol.h>
Willy Tarreau92fb9832007-10-16 17:34:28 +020044#include <proto/task.h>
45
Emeric Bruncf20bf12010-10-22 16:06:11 +020046static int uxst_bind_listener(struct listener *listener, char *errmsg, int errlen);
47static int uxst_bind_listeners(struct protocol *proto, char *errmsg, int errlen);
Willy Tarreaudabf2e22007-10-28 21:59:24 +010048static int uxst_unbind_listeners(struct protocol *proto);
Olivier Houchardfdcb0072019-05-06 18:32:29 +020049static int uxst_connect_server(struct connection *conn, int flags);
Willy Tarreau9d5be5c2017-09-15 07:55:51 +020050static void uxst_add_listener(struct listener *listener, int port);
Willy Tarreau31794892017-09-15 07:59:31 +020051static int uxst_pause_listener(struct listener *l);
52static int uxst_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir);
53static int uxst_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir);
Willy Tarreaudabf2e22007-10-28 21:59:24 +010054
55/* Note: must not be declared <const> as its list will be overwritten */
56static struct protocol proto_unix = {
57 .name = "unix_stream",
58 .sock_domain = PF_UNIX,
59 .sock_type = SOCK_STREAM,
60 .sock_prot = 0,
61 .sock_family = AF_UNIX,
62 .sock_addrlen = sizeof(struct sockaddr_un),
63 .l3_addrlen = sizeof(((struct sockaddr_un*)0)->sun_path),/* path len */
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020064 .accept = &listener_accept,
Willy Tarreau47f48c42014-05-09 22:57:47 +020065 .connect = &uxst_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020066 .bind = uxst_bind_listener,
Willy Tarreaudabf2e22007-10-28 21:59:24 +010067 .bind_all = uxst_bind_listeners,
68 .unbind_all = uxst_unbind_listeners,
69 .enable_all = enable_all_listeners,
70 .disable_all = disable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +020071 .get_src = uxst_get_src,
72 .get_dst = uxst_get_dst,
Willy Tarreaufd0e0082014-07-07 21:07:51 +020073 .pause = uxst_pause_listener,
Willy Tarreau9d5be5c2017-09-15 07:55:51 +020074 .add = uxst_add_listener,
Willy Tarreaudabf2e22007-10-28 21:59:24 +010075 .listeners = LIST_HEAD_INIT(proto_unix.listeners),
76 .nb_listeners = 0,
77};
78
Willy Tarreau0108d902018-11-25 19:14:37 +010079INITCALL1(STG_REGISTER, protocol_register, &proto_unix);
80
Willy Tarreaudabf2e22007-10-28 21:59:24 +010081/********************************
82 * 1) low-level socket functions
83 ********************************/
84
Willy Tarreau59b94792012-05-11 16:16:40 +020085/*
86 * Retrieves the source address for the socket <fd>, with <dir> indicating
87 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
88 * success, -1 in case of error. The socket's source address is stored in
89 * <sa> for <salen> bytes.
90 */
Willy Tarreau31794892017-09-15 07:59:31 +020091static int uxst_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
Willy Tarreau59b94792012-05-11 16:16:40 +020092{
93 if (dir)
94 return getsockname(fd, sa, &salen);
95 else
96 return getpeername(fd, sa, &salen);
97}
98
99
100/*
101 * Retrieves the original destination address for the socket <fd>, with <dir>
102 * indicating if we're a listener (=0) or an initiator (!=0). It returns 0 in
103 * case of success, -1 in case of error. The socket's source address is stored
104 * in <sa> for <salen> bytes.
105 */
Willy Tarreau31794892017-09-15 07:59:31 +0200106static int uxst_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
Willy Tarreau59b94792012-05-11 16:16:40 +0200107{
108 if (dir)
109 return getpeername(fd, sa, &salen);
110 else
111 return getsockname(fd, sa, &salen);
112}
113
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100114
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100115/********************************
116 * 2) listener-oriented functions
117 ********************************/
118
119
Olivier Houchardf886e342017-04-05 22:24:59 +0200120static int uxst_find_compatible_fd(struct listener *l)
121{
122 struct xfer_sock_list *xfer_sock = xfer_sock_list;
123 int ret = -1;
124
125 while (xfer_sock) {
126 struct sockaddr_un *un1 = (void *)&l->addr;
127 struct sockaddr_un *un2 = (void *)&xfer_sock->addr;
128
129 /*
130 * The bound socket's path as returned by getsockaddr
131 * will be the temporary name <sockname>.XXXXX.tmp,
132 * so we can't just compare the two names
133 */
134 if (xfer_sock->addr.ss_family == AF_UNIX &&
135 strncmp(un1->sun_path, un2->sun_path,
136 strlen(un1->sun_path)) == 0) {
137 char *after_sockname = un2->sun_path +
138 strlen(un1->sun_path);
139 /* Make a reasonnable effort to check that
140 * it is indeed a haproxy-generated temporary
141 * name, it's not perfect, but probably good enough.
142 */
143 if (after_sockname[0] == '.') {
144 after_sockname++;
145 while (after_sockname[0] >= '0' &&
146 after_sockname[0] <= '9')
147 after_sockname++;
148 if (!strcmp(after_sockname, ".tmp"))
149 break;
Olivier Houchardb4dd15b2018-06-06 18:34:34 +0200150 /* abns sockets sun_path starts with a \0 */
151 } else if (un1->sun_path[0] == 0
152 && un2->sun_path[0] == 0
153 && !memcmp(&un1->sun_path[1], &un2->sun_path[1],
154 sizeof(un1->sun_path) - 1))
155 break;
Olivier Houchardf886e342017-04-05 22:24:59 +0200156 }
157 xfer_sock = xfer_sock->next;
158 }
159 if (xfer_sock != NULL) {
160 ret = xfer_sock->fd;
161 if (xfer_sock == xfer_sock_list)
162 xfer_sock_list = xfer_sock->next;
163 if (xfer_sock->prev)
164 xfer_sock->prev->next = xfer_sock->next;
165 if (xfer_sock->next)
Olivier Houchardec9516a2018-03-08 18:25:49 +0100166 xfer_sock->next->prev = xfer_sock->prev;
Olivier Houchardf886e342017-04-05 22:24:59 +0200167 free(xfer_sock);
168 }
169 return ret;
170
171}
172
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100173/* This function creates a UNIX socket associated to the listener. It changes
174 * the state from ASSIGNED to LISTEN. The socket is NOT enabled for polling.
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100175 * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL. It
176 * may return a warning or an error message in <errmsg> if the message is at
177 * most <errlen> bytes long (including '\0'). Note that <errmsg> may be NULL if
178 * <errlen> is also zero.
Willy Tarreau92fb9832007-10-16 17:34:28 +0200179 */
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100180static int uxst_bind_listener(struct listener *listener, char *errmsg, int errlen)
Willy Tarreau92fb9832007-10-16 17:34:28 +0200181{
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100182 int fd;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200183 char tempname[MAXPATHLEN];
184 char backname[MAXPATHLEN];
185 struct sockaddr_un addr;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100186 const char *msg = NULL;
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100187 const char *path;
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100188 int maxpathlen;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100189 int ext, ready;
190 socklen_t ready_len;
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200191 int err;
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100192 int ret;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200193
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200194 err = ERR_NONE;
195
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100196 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100197 if (errlen)
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100198 *errmsg = 0;
199
200 if (listener->state != LI_ASSIGNED)
201 return ERR_NONE; /* already bound */
202
Olivier Houchardf886e342017-04-05 22:24:59 +0200203 if (listener->fd == -1)
204 listener->fd = uxst_find_compatible_fd(listener);
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100205 path = ((struct sockaddr_un *)&listener->addr)->sun_path;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200206
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100207 maxpathlen = MIN(MAXPATHLEN, sizeof(addr.sun_path));
208
Willy Tarreau40aa0702013-03-10 23:51:38 +0100209 /* if the listener already has an fd assigned, then we were offered the
210 * fd by an external process (most likely the parent), and we don't want
211 * to create a new socket. However we still want to set a few flags on
212 * the socket.
213 */
214 fd = listener->fd;
215 ext = (fd >= 0);
216 if (ext)
217 goto fd_ready;
218
Willy Tarreauccfccef2014-05-10 01:49:15 +0200219 if (path[0]) {
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100220 ret = snprintf(tempname, maxpathlen, "%s.%d.tmp", path, pid);
221 if (ret < 0 || ret >= maxpathlen) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200222 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100223 msg = "name too long for UNIX socket (limit usually 97)";
Willy Tarreauccfccef2014-05-10 01:49:15 +0200224 goto err_return;
225 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200226
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100227 ret = snprintf(backname, maxpathlen, "%s.%d.bak", path, pid);
228 if (ret < 0 || ret >= maxpathlen) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200229 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100230 msg = "name too long for UNIX socket (limit usually 97)";
Willy Tarreauccfccef2014-05-10 01:49:15 +0200231 goto err_return;
232 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200233
Willy Tarreauccfccef2014-05-10 01:49:15 +0200234 /* 2. clean existing orphaned entries */
235 if (unlink(tempname) < 0 && errno != ENOENT) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200236 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200237 msg = "error when trying to unlink previous UNIX socket";
238 goto err_return;
239 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200240
Willy Tarreauccfccef2014-05-10 01:49:15 +0200241 if (unlink(backname) < 0 && errno != ENOENT) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200242 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200243 msg = "error when trying to unlink previous UNIX socket";
244 goto err_return;
245 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200246
Willy Tarreauccfccef2014-05-10 01:49:15 +0200247 /* 3. backup existing socket */
248 if (link(path, backname) < 0 && errno != ENOENT) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200249 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200250 msg = "error when trying to preserve previous UNIX socket";
251 goto err_return;
252 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200253
Willy Tarreau719e07c2019-12-11 16:29:10 +0100254 strncpy(addr.sun_path, tempname, sizeof(addr.sun_path) - 1);
Willy Tarreauccfccef2014-05-10 01:49:15 +0200255 addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200256 }
Willy Tarreauccfccef2014-05-10 01:49:15 +0200257 else {
258 /* first char is zero, it's an abstract socket whose address
259 * is defined by all the bytes past this zero.
260 */
261 memcpy(addr.sun_path, path, sizeof(addr.sun_path));
262 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200263 addr.sun_family = AF_UNIX;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200264
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100265 fd = socket(PF_UNIX, SOCK_STREAM, 0);
266 if (fd < 0) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200267 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100268 msg = "cannot create UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200269 goto err_unlink_back;
270 }
271
Willy Tarreau40aa0702013-03-10 23:51:38 +0100272 fd_ready:
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100273 if (fd >= global.maxsock) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200274 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100275 msg = "socket(): not enough free sockets, raise -n argument";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200276 goto err_unlink_temp;
277 }
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100278
279 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200280 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100281 msg = "cannot make UNIX socket non-blocking";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200282 goto err_unlink_temp;
283 }
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100284
Willy Tarreau40aa0702013-03-10 23:51:38 +0100285 if (!ext && bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
Willy Tarreau92fb9832007-10-16 17:34:28 +0200286 /* note that bind() creates the socket <tempname> on the file system */
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200287 if (errno == EADDRINUSE) {
288 /* the old process might still own it, let's retry */
289 err |= ERR_RETRYABLE | ERR_ALERT;
290 msg = "cannot listen to socket";
291 }
292 else {
293 err |= ERR_FATAL | ERR_ALERT;
294 msg = "cannot bind UNIX socket";
295 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200296 goto err_unlink_temp;
297 }
298
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100299 /* <uid> and <gid> different of -1 will be used to change the socket owner.
300 * If <mode> is not 0, it will be used to restrict access to the socket.
301 * While it is known not to be portable on every OS, it's still useful
Willy Tarreauccfccef2014-05-10 01:49:15 +0200302 * where it works. We also don't change permissions on abstract sockets.
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100303 */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200304 if (!ext && path[0] &&
Willy Tarreau40aa0702013-03-10 23:51:38 +0100305 (((listener->bind_conf->ux.uid != -1 || listener->bind_conf->ux.gid != -1) &&
306 (chown(tempname, listener->bind_conf->ux.uid, listener->bind_conf->ux.gid) == -1)) ||
307 (listener->bind_conf->ux.mode != 0 && chmod(tempname, listener->bind_conf->ux.mode) == -1))) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200308 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100309 msg = "cannot change UNIX socket ownership";
Willy Tarreaue6ad2b12007-10-18 12:45:54 +0200310 goto err_unlink_temp;
311 }
312
Willy Tarreau40aa0702013-03-10 23:51:38 +0100313 ready = 0;
314 ready_len = sizeof(ready);
315 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1)
316 ready = 0;
317
318 if (!(ext && ready) && /* only listen if not already done by external process */
Willy Tarreaue2711c72019-02-27 15:39:41 +0100319 listen(fd, listener_backlog(listener)) < 0) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200320 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100321 msg = "cannot listen to UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200322 goto err_unlink_temp;
323 }
324
Willy Tarreauccfccef2014-05-10 01:49:15 +0200325 /* Point of no return: we are ready, we'll switch the sockets. We don't
Willy Tarreau92fb9832007-10-16 17:34:28 +0200326 * fear loosing the socket <path> because we have a copy of it in
Willy Tarreauccfccef2014-05-10 01:49:15 +0200327 * backname. Abstract sockets are not renamed.
Willy Tarreau92fb9832007-10-16 17:34:28 +0200328 */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200329 if (!ext && path[0] && rename(tempname, path) < 0) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200330 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100331 msg = "cannot switch final and temporary UNIX sockets";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200332 goto err_rename;
333 }
334
Willy Tarreau68986ab2017-06-16 10:34:20 +0200335 /* Cleanup: only unlink if we didn't inherit the fd from the parent */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200336 if (!ext && path[0])
Willy Tarreau40aa0702013-03-10 23:51:38 +0100337 unlink(backname);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200338
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100339 /* the socket is now listening */
340 listener->fd = fd;
341 listener->state = LI_LISTEN;
342
Willy Tarreaua9786b62018-01-25 07:22:13 +0100343 fd_insert(fd, listener, listener->proto->accept,
Willy Tarreau0948a782020-02-12 10:15:34 +0100344 thread_mask(listener->bind_conf->bind_thread) & all_threads_mask);
Willy Tarreaua9786b62018-01-25 07:22:13 +0100345
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200346 return err;
347
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100348 err_rename:
349 ret = rename(backname, path);
350 if (ret < 0 && errno == ENOENT)
351 unlink(path);
352 err_unlink_temp:
Jan Seda7319b642014-06-26 20:44:05 +0200353 if (!ext && path[0])
Willy Tarreau40aa0702013-03-10 23:51:38 +0100354 unlink(tempname);
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100355 close(fd);
356 err_unlink_back:
Jan Seda7319b642014-06-26 20:44:05 +0200357 if (!ext && path[0])
Willy Tarreau40aa0702013-03-10 23:51:38 +0100358 unlink(backname);
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100359 err_return:
Willy Tarreau40aa0702013-03-10 23:51:38 +0100360 if (msg && errlen) {
361 if (!ext)
362 snprintf(errmsg, errlen, "%s [%s]", msg, path);
363 else
364 snprintf(errmsg, errlen, "%s [fd %d]", msg, fd);
365 }
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200366 return err;
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100367}
368
369/* This function closes the UNIX sockets for the specified listener.
370 * The listener enters the LI_ASSIGNED state. It always returns ERR_NONE.
371 */
372static int uxst_unbind_listener(struct listener *listener)
373{
Willy Tarreaube58c382011-07-24 18:28:10 +0200374 if (listener->state > LI_ASSIGNED) {
375 unbind_listener(listener);
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100376 }
377 return ERR_NONE;
378}
379
Willy Tarreau32282382017-09-15 07:44:44 +0200380/* Add <listener> to the list of unix stream listeners (port is ignored). The
381 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
382 * The number of listeners for the protocol is updated.
Willy Tarreaudaacf362019-07-24 16:45:02 +0200383 *
384 * Must be called with proto_lock held.
385 *
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100386 */
Willy Tarreau9d5be5c2017-09-15 07:55:51 +0200387static void uxst_add_listener(struct listener *listener, int port)
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100388{
389 if (listener->state != LI_INIT)
390 return;
391 listener->state = LI_ASSIGNED;
392 listener->proto = &proto_unix;
393 LIST_ADDQ(&proto_unix.listeners, &listener->proto_list);
394 proto_unix.nb_listeners++;
395}
396
Willy Tarreaufd0e0082014-07-07 21:07:51 +0200397/* Pause a listener. Returns < 0 in case of failure, 0 if the listener
398 * was totally stopped, or > 0 if correctly paused. Nothing is done for
399 * plain unix sockets since currently it's the new process which handles
400 * the renaming. Abstract sockets are completely unbound.
401 */
Willy Tarreau31794892017-09-15 07:59:31 +0200402static int uxst_pause_listener(struct listener *l)
Willy Tarreaufd0e0082014-07-07 21:07:51 +0200403{
404 if (((struct sockaddr_un *)&l->addr)->sun_path[0])
405 return 1;
406
Christopher Faulet510c0d62018-03-16 10:04:47 +0100407 /* Listener's lock already held. Call lockless version of
408 * unbind_listener. */
409 do_unbind_listener(l, 1);
Willy Tarreaufd0e0082014-07-07 21:07:51 +0200410 return 0;
411}
412
Willy Tarreau47f48c42014-05-09 22:57:47 +0200413
414/*
415 * This function initiates a UNIX connection establishment to the target assigned
Willy Tarreauca9f5a92019-07-17 16:40:37 +0200416 * to connection <conn> using (si->{target,dst}). The source address is ignored
Willy Tarreau47f48c42014-05-09 22:57:47 +0200417 * and will be selected by the system. conn->target may point either to a valid
418 * server or to a backend, depending on conn->target. Only OBJ_TYPE_PROXY and
419 * OBJ_TYPE_SERVER are supported. The <data> parameter is a boolean indicating
420 * whether there are data waiting for being sent or not, in order to adjust data
421 * write polling and on some platforms. The <delack> argument is ignored.
422 *
423 * Note that a pending send_proxy message accounts for data.
424 *
425 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +0200426 * - SF_ERR_NONE if everything's OK
427 * - SF_ERR_SRVTO if there are no more servers
428 * - SF_ERR_SRVCL if the connection was refused by the server
429 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
430 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
431 * - SF_ERR_INTERNAL for any other purely internal errors
Tim Düsterhus4896c442016-11-29 02:15:19 +0100432 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau47f48c42014-05-09 22:57:47 +0200433 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200434 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
Willy Tarreau47f48c42014-05-09 22:57:47 +0200435 * it's invalid and the caller has nothing to do.
436 */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200437static int uxst_connect_server(struct connection *conn, int flags)
Willy Tarreau47f48c42014-05-09 22:57:47 +0200438{
439 int fd;
440 struct server *srv;
441 struct proxy *be;
442
Willy Tarreau47f48c42014-05-09 22:57:47 +0200443 switch (obj_type(conn->target)) {
444 case OBJ_TYPE_PROXY:
445 be = objt_proxy(conn->target);
446 srv = NULL;
447 break;
448 case OBJ_TYPE_SERVER:
449 srv = objt_server(conn->target);
450 be = srv->proxy;
451 break;
452 default:
453 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200454 return SF_ERR_INTERNAL;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200455 }
456
Willy Tarreau585744b2017-08-24 14:31:19 +0200457 if ((fd = conn->handle.fd = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
Willy Tarreau47f48c42014-05-09 22:57:47 +0200458 qfprintf(stderr, "Cannot get a server socket.\n");
459
460 if (errno == ENFILE) {
461 conn->err_code = CO_ER_SYS_FDLIM;
462 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100463 "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n",
464 be->id, global.maxsock);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200465 }
466 else if (errno == EMFILE) {
467 conn->err_code = CO_ER_PROC_FDLIM;
468 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100469 "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n",
470 be->id, global.maxsock);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200471 }
472 else if (errno == ENOBUFS || errno == ENOMEM) {
473 conn->err_code = CO_ER_SYS_MEMLIM;
474 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100475 "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n",
476 be->id, global.maxsock);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200477 }
478 else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
479 conn->err_code = CO_ER_NOPROTO;
480 }
481 else
482 conn->err_code = CO_ER_SOCK_ERR;
483
484 /* this is a resource error */
485 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200486 return SF_ERR_RESOURCE;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200487 }
488
489 if (fd >= global.maxsock) {
490 /* do not log anything there, it's a normal condition when this option
491 * is used to serialize connections to a server !
492 */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100493 ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
Willy Tarreau47f48c42014-05-09 22:57:47 +0200494 close(fd);
495 conn->err_code = CO_ER_CONF_FDLIM;
496 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200497 return SF_ERR_PRXCOND; /* it is a configuration limit */
Willy Tarreau47f48c42014-05-09 22:57:47 +0200498 }
499
500 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
501 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
502 close(fd);
503 conn->err_code = CO_ER_SOCK_ERR;
504 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200505 return SF_ERR_INTERNAL;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200506 }
507
William Lallemandc03eb012018-11-27 12:02:37 +0100508 if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) {
509 ha_alert("Cannot set CLOEXEC on client socket.\n");
510 close(fd);
511 conn->err_code = CO_ER_SOCK_ERR;
512 conn->flags |= CO_FL_ERROR;
513 return SF_ERR_INTERNAL;
514 }
515
Willy Tarreau47f48c42014-05-09 22:57:47 +0200516 /* if a send_proxy is there, there are data */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200517 if (conn->send_proxy_ofs)
518 flags |= CONNECT_HAS_DATA;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200519
520 if (global.tune.server_sndbuf)
521 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
522
523 if (global.tune.server_rcvbuf)
524 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
525
Willy Tarreauca9f5a92019-07-17 16:40:37 +0200526 if (connect(fd, (struct sockaddr *)conn->dst, get_addr_len(conn->dst)) == -1) {
Willy Tarreau94841792017-01-25 14:27:38 +0100527 if (errno == EINPROGRESS || errno == EALREADY) {
Willy Tarreau7bb21532014-05-10 09:48:28 +0200528 conn->flags |= CO_FL_WAIT_L4_CONN;
529 }
Willy Tarreau94841792017-01-25 14:27:38 +0100530 else if (errno == EISCONN) {
531 conn->flags &= ~CO_FL_WAIT_L4_CONN;
532 }
Willy Tarreau7bb21532014-05-10 09:48:28 +0200533 else if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau47f48c42014-05-09 22:57:47 +0200534 char *msg;
535 if (errno == EAGAIN || errno == EADDRNOTAVAIL) {
Lukas Tribus9f256d42016-01-26 20:33:14 +0100536 msg = "can't connect to destination unix socket, check backlog size on the server";
Willy Tarreau47f48c42014-05-09 22:57:47 +0200537 conn->err_code = CO_ER_FREE_PORTS;
538 }
539 else {
540 msg = "local address already in use";
541 conn->err_code = CO_ER_ADDR_INUSE;
542 }
543
544 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
545 close(fd);
546 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
547 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200548 return SF_ERR_RESOURCE;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200549 }
550 else if (errno == ETIMEDOUT) {
551 close(fd);
552 conn->err_code = CO_ER_SOCK_ERR;
553 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200554 return SF_ERR_SRVTO;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200555 }
556 else { // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
557 close(fd);
558 conn->err_code = CO_ER_SOCK_ERR;
559 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200560 return SF_ERR_SRVCL;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200561 }
562 }
Willy Tarreau7bb21532014-05-10 09:48:28 +0200563 else {
564 /* connect() already succeeded, which is quite usual for unix
Willy Tarreau94841792017-01-25 14:27:38 +0100565 * sockets. Let's avoid a second connect() probe to complete it.
Willy Tarreau7bb21532014-05-10 09:48:28 +0200566 */
567 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau7bb21532014-05-10 09:48:28 +0200568 }
Willy Tarreau47f48c42014-05-09 22:57:47 +0200569
570 conn->flags |= CO_FL_ADDR_TO_SET;
571
572 /* Prepare to send a few handshakes related to the on-wire protocol. */
573 if (conn->send_proxy_ofs)
574 conn->flags |= CO_FL_SEND_PROXY;
575
576 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreau7bb21532014-05-10 09:48:28 +0200577 fdtab[fd].linger_risk = 0; /* no need to disable lingering */
Willy Tarreau47f48c42014-05-09 22:57:47 +0200578
Willy Tarreau4c69cff2020-03-04 16:38:00 +0100579 if (conn->flags & CO_FL_WAIT_L4_CONN) {
580 fd_want_send(fd);
581 fd_cant_send(fd);
582 }
Willy Tarreauccf3f6d2019-09-05 17:05:05 +0200583
Willy Tarreau47f48c42014-05-09 22:57:47 +0200584 if (conn_xprt_init(conn) < 0) {
Willy Tarreau8c829012017-10-05 18:02:11 +0200585 conn_full_close(conn);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200586 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200587 return SF_ERR_RESOURCE;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200588 }
589
Willy Tarreaue7dff022015-04-03 01:14:29 +0200590 return SF_ERR_NONE; /* connection is OK */
Willy Tarreau47f48c42014-05-09 22:57:47 +0200591}
592
593
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100594/********************************
595 * 3) protocol-oriented functions
596 ********************************/
597
598
Willy Tarreau92fb9832007-10-16 17:34:28 +0200599/* This function creates all UNIX sockets bound to the protocol entry <proto>.
600 * It is intended to be used as the protocol's bind_all() function.
601 * The sockets will be registered but not added to any fd_set, in order not to
602 * loose them across the fork(). A call to uxst_enable_listeners() is needed
603 * to complete initialization.
604 *
Willy Tarreaudaacf362019-07-24 16:45:02 +0200605 * Must be called with proto_lock held.
606 *
Willy Tarreau92fb9832007-10-16 17:34:28 +0200607 * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL.
608 */
Emeric Bruncf20bf12010-10-22 16:06:11 +0200609static int uxst_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreau92fb9832007-10-16 17:34:28 +0200610{
611 struct listener *listener;
612 int err = ERR_NONE;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200613
614 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200615 err |= uxst_bind_listener(listener, errmsg, errlen);
616 if (err & ERR_ABORT)
617 break;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200618 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200619 return err;
620}
621
Willy Tarreau92fb9832007-10-16 17:34:28 +0200622
623/* This function stops all listening UNIX sockets bound to the protocol
624 * <proto>. It does not detaches them from the protocol.
625 * It always returns ERR_NONE.
Willy Tarreaudaacf362019-07-24 16:45:02 +0200626 *
627 * Must be called with proto_lock held.
628 *
Willy Tarreau92fb9832007-10-16 17:34:28 +0200629 */
630static int uxst_unbind_listeners(struct protocol *proto)
631{
632 struct listener *listener;
633
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100634 list_for_each_entry(listener, &proto->listeners, proto_list)
635 uxst_unbind_listener(listener);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200636 return ERR_NONE;
637}
638
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200639/* parse the "mode" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200640static 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 +0200641{
Willy Tarreaua1a247b2017-10-04 14:43:44 +0200642 char *endptr;
643
644 conf->ux.mode = strtol(args[cur_arg + 1], &endptr, 8);
645
646 if (!*args[cur_arg + 1] || *endptr) {
647 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 +0200648 return ERR_ALERT | ERR_FATAL;
649 }
650
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200651 return 0;
652}
653
654/* parse the "gid" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200655static 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 +0200656{
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200657 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200658 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200659 return ERR_ALERT | ERR_FATAL;
660 }
661
Willy Tarreau290e63a2012-09-20 18:07:14 +0200662 conf->ux.gid = atol(args[cur_arg + 1]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200663 return 0;
664}
665
666/* parse the "group" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200667static 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 +0200668{
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200669 struct group *group;
670
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 group name", args[cur_arg]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200673 return ERR_ALERT | ERR_FATAL;
674 }
675
676 group = getgrnam(args[cur_arg + 1]);
677 if (!group) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200678 memprintf(err, "'%s' : unknown group name '%s'", args[cur_arg], args[cur_arg + 1]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200679 return ERR_ALERT | ERR_FATAL;
680 }
681
Willy Tarreau290e63a2012-09-20 18:07:14 +0200682 conf->ux.gid = group->gr_gid;
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200683 return 0;
684}
685
686/* parse the "uid" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200687static 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 +0200688{
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200689 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200690 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200691 return ERR_ALERT | ERR_FATAL;
692 }
693
Willy Tarreau290e63a2012-09-20 18:07:14 +0200694 conf->ux.uid = atol(args[cur_arg + 1]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200695 return 0;
696}
697
698/* parse the "user" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200699static 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 +0200700{
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200701 struct passwd *user;
702
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 user name", args[cur_arg]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200705 return ERR_ALERT | ERR_FATAL;
706 }
707
708 user = getpwnam(args[cur_arg + 1]);
709 if (!user) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200710 memprintf(err, "'%s' : unknown user name '%s'", args[cur_arg], args[cur_arg + 1]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200711 return ERR_ALERT | ERR_FATAL;
712 }
713
Willy Tarreau290e63a2012-09-20 18:07:14 +0200714 conf->ux.uid = user->pw_uid;
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200715 return 0;
716}
717
718/* Note: must not be declared <const> as its list will be overwritten.
719 * Please take care of keeping this list alphabetically sorted, doing so helps
720 * all code contributors.
721 * Optional keywords are also declared with a NULL ->parse() function so that
722 * the config parser can report an appropriate error when a known keyword was
723 * not enabled.
724 */
Willy Tarreau51fb7652012-09-18 18:24:39 +0200725static struct bind_kw_list bind_kws = { "UNIX", { }, {
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200726 { "gid", bind_parse_gid, 1 }, /* set the socket's gid */
727 { "group", bind_parse_group, 1 }, /* set the socket's gid from the group name */
728 { "mode", bind_parse_mode, 1 }, /* set the socket's mode (eg: 0644)*/
729 { "uid", bind_parse_uid, 1 }, /* set the socket's uid */
730 { "user", bind_parse_user, 1 }, /* set the socket's uid from the user name */
731 { NULL, NULL, 0 },
732}};
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100733
Willy Tarreau0108d902018-11-25 19:14:37 +0100734INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200735
736/*
737 * Local variables:
738 * c-indent-level: 8
739 * c-basic-offset: 8
740 * End:
741 */