blob: f0a38e7f6c8602661f1c145dbfbf4e6727c081aa [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 Tarreau853b2972020-05-27 18:01:47 +020031#include <haproxy/list.h>
Willy Tarreau213e9902020-06-04 14:58:24 +020032#include <haproxy/listener.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020033#include <haproxy/tools.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020034#include <haproxy/time.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020035#include <haproxy/version.h>
Willy Tarreau92fb9832007-10-16 17:34:28 +020036
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 Tarreau0f6ffd62020-06-03 19:33:00 +020040#include <haproxy/fd.h>
Willy Tarreau92fb9832007-10-16 17:34:28 +020041#include <proto/log.h>
Willy Tarreau2dd7c352020-06-03 15:26:55 +020042#include <haproxy/protocol.h>
Willy Tarreau92fb9832007-10-16 17:34:28 +020043#include <proto/task.h>
44
Emeric Bruncf20bf12010-10-22 16:06:11 +020045static int uxst_bind_listener(struct listener *listener, char *errmsg, int errlen);
46static int uxst_bind_listeners(struct protocol *proto, char *errmsg, int errlen);
Willy Tarreaudabf2e22007-10-28 21:59:24 +010047static int uxst_unbind_listeners(struct protocol *proto);
Olivier Houchardfdcb0072019-05-06 18:32:29 +020048static int uxst_connect_server(struct connection *conn, int flags);
Willy Tarreau9d5be5c2017-09-15 07:55:51 +020049static void uxst_add_listener(struct listener *listener, int port);
Willy Tarreau31794892017-09-15 07:59:31 +020050static int uxst_pause_listener(struct listener *l);
51static int uxst_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir);
52static int uxst_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir);
Willy Tarreaudabf2e22007-10-28 21:59:24 +010053
54/* Note: must not be declared <const> as its list will be overwritten */
55static struct protocol proto_unix = {
56 .name = "unix_stream",
57 .sock_domain = PF_UNIX,
58 .sock_type = SOCK_STREAM,
59 .sock_prot = 0,
60 .sock_family = AF_UNIX,
61 .sock_addrlen = sizeof(struct sockaddr_un),
62 .l3_addrlen = sizeof(((struct sockaddr_un*)0)->sun_path),/* path len */
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020063 .accept = &listener_accept,
Willy Tarreau47f48c42014-05-09 22:57:47 +020064 .connect = &uxst_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020065 .bind = uxst_bind_listener,
Willy Tarreaudabf2e22007-10-28 21:59:24 +010066 .bind_all = uxst_bind_listeners,
67 .unbind_all = uxst_unbind_listeners,
68 .enable_all = enable_all_listeners,
69 .disable_all = disable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +020070 .get_src = uxst_get_src,
71 .get_dst = uxst_get_dst,
Willy Tarreaufd0e0082014-07-07 21:07:51 +020072 .pause = uxst_pause_listener,
Willy Tarreau9d5be5c2017-09-15 07:55:51 +020073 .add = uxst_add_listener,
Willy Tarreaudabf2e22007-10-28 21:59:24 +010074 .listeners = LIST_HEAD_INIT(proto_unix.listeners),
75 .nb_listeners = 0,
76};
77
Willy Tarreau0108d902018-11-25 19:14:37 +010078INITCALL1(STG_REGISTER, protocol_register, &proto_unix);
79
Willy Tarreaudabf2e22007-10-28 21:59:24 +010080/********************************
81 * 1) low-level socket functions
82 ********************************/
83
Willy Tarreau59b94792012-05-11 16:16:40 +020084/*
85 * Retrieves the source address for the socket <fd>, with <dir> indicating
86 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
87 * success, -1 in case of error. The socket's source address is stored in
88 * <sa> for <salen> bytes.
89 */
Willy Tarreau31794892017-09-15 07:59:31 +020090static int uxst_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
Willy Tarreau59b94792012-05-11 16:16:40 +020091{
92 if (dir)
93 return getsockname(fd, sa, &salen);
94 else
95 return getpeername(fd, sa, &salen);
96}
97
98
99/*
100 * Retrieves the original destination address for the socket <fd>, with <dir>
101 * indicating if we're a listener (=0) or an initiator (!=0). It returns 0 in
102 * case of success, -1 in case of error. The socket's source address is stored
103 * in <sa> for <salen> bytes.
104 */
Willy Tarreau31794892017-09-15 07:59:31 +0200105static int uxst_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
Willy Tarreau59b94792012-05-11 16:16:40 +0200106{
107 if (dir)
108 return getpeername(fd, sa, &salen);
109 else
110 return getsockname(fd, sa, &salen);
111}
112
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100113
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100114/********************************
115 * 2) listener-oriented functions
116 ********************************/
117
118
Olivier Houchardf886e342017-04-05 22:24:59 +0200119static int uxst_find_compatible_fd(struct listener *l)
120{
121 struct xfer_sock_list *xfer_sock = xfer_sock_list;
122 int ret = -1;
123
124 while (xfer_sock) {
125 struct sockaddr_un *un1 = (void *)&l->addr;
126 struct sockaddr_un *un2 = (void *)&xfer_sock->addr;
127
128 /*
129 * The bound socket's path as returned by getsockaddr
130 * will be the temporary name <sockname>.XXXXX.tmp,
131 * so we can't just compare the two names
132 */
133 if (xfer_sock->addr.ss_family == AF_UNIX &&
134 strncmp(un1->sun_path, un2->sun_path,
135 strlen(un1->sun_path)) == 0) {
136 char *after_sockname = un2->sun_path +
137 strlen(un1->sun_path);
138 /* Make a reasonnable effort to check that
139 * it is indeed a haproxy-generated temporary
140 * name, it's not perfect, but probably good enough.
141 */
142 if (after_sockname[0] == '.') {
143 after_sockname++;
144 while (after_sockname[0] >= '0' &&
145 after_sockname[0] <= '9')
146 after_sockname++;
147 if (!strcmp(after_sockname, ".tmp"))
148 break;
Olivier Houchardb4dd15b2018-06-06 18:34:34 +0200149 /* abns sockets sun_path starts with a \0 */
150 } else if (un1->sun_path[0] == 0
151 && un2->sun_path[0] == 0
152 && !memcmp(&un1->sun_path[1], &un2->sun_path[1],
153 sizeof(un1->sun_path) - 1))
154 break;
Olivier Houchardf886e342017-04-05 22:24:59 +0200155 }
156 xfer_sock = xfer_sock->next;
157 }
158 if (xfer_sock != NULL) {
159 ret = xfer_sock->fd;
160 if (xfer_sock == xfer_sock_list)
161 xfer_sock_list = xfer_sock->next;
162 if (xfer_sock->prev)
163 xfer_sock->prev->next = xfer_sock->next;
164 if (xfer_sock->next)
Olivier Houchardec9516a2018-03-08 18:25:49 +0100165 xfer_sock->next->prev = xfer_sock->prev;
Olivier Houchardf886e342017-04-05 22:24:59 +0200166 free(xfer_sock);
167 }
168 return ret;
169
170}
171
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100172/* This function creates a UNIX socket associated to the listener. It changes
173 * the state from ASSIGNED to LISTEN. The socket is NOT enabled for polling.
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100174 * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL. It
175 * may return a warning or an error message in <errmsg> if the message is at
176 * most <errlen> bytes long (including '\0'). Note that <errmsg> may be NULL if
177 * <errlen> is also zero.
Willy Tarreau92fb9832007-10-16 17:34:28 +0200178 */
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100179static int uxst_bind_listener(struct listener *listener, char *errmsg, int errlen)
Willy Tarreau92fb9832007-10-16 17:34:28 +0200180{
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100181 int fd;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200182 char tempname[MAXPATHLEN];
183 char backname[MAXPATHLEN];
184 struct sockaddr_un addr;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100185 const char *msg = NULL;
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100186 const char *path;
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100187 int maxpathlen;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100188 int ext, ready;
189 socklen_t ready_len;
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200190 int err;
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100191 int ret;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200192
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200193 err = ERR_NONE;
194
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100195 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100196 if (errlen)
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100197 *errmsg = 0;
198
199 if (listener->state != LI_ASSIGNED)
200 return ERR_NONE; /* already bound */
201
Olivier Houchardf886e342017-04-05 22:24:59 +0200202 if (listener->fd == -1)
203 listener->fd = uxst_find_compatible_fd(listener);
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100204 path = ((struct sockaddr_un *)&listener->addr)->sun_path;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200205
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100206 maxpathlen = MIN(MAXPATHLEN, sizeof(addr.sun_path));
207
Willy Tarreau40aa0702013-03-10 23:51:38 +0100208 /* if the listener already has an fd assigned, then we were offered the
209 * fd by an external process (most likely the parent), and we don't want
210 * to create a new socket. However we still want to set a few flags on
211 * the socket.
212 */
213 fd = listener->fd;
214 ext = (fd >= 0);
215 if (ext)
216 goto fd_ready;
217
Willy Tarreauccfccef2014-05-10 01:49:15 +0200218 if (path[0]) {
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100219 ret = snprintf(tempname, maxpathlen, "%s.%d.tmp", path, pid);
220 if (ret < 0 || ret >= maxpathlen) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200221 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100222 msg = "name too long for UNIX socket (limit usually 97)";
Willy Tarreauccfccef2014-05-10 01:49:15 +0200223 goto err_return;
224 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200225
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100226 ret = snprintf(backname, maxpathlen, "%s.%d.bak", path, pid);
227 if (ret < 0 || ret >= maxpathlen) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200228 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100229 msg = "name too long for UNIX socket (limit usually 97)";
Willy Tarreauccfccef2014-05-10 01:49:15 +0200230 goto err_return;
231 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200232
Willy Tarreauccfccef2014-05-10 01:49:15 +0200233 /* 2. clean existing orphaned entries */
234 if (unlink(tempname) < 0 && errno != ENOENT) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200235 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200236 msg = "error when trying to unlink previous UNIX socket";
237 goto err_return;
238 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200239
Willy Tarreauccfccef2014-05-10 01:49:15 +0200240 if (unlink(backname) < 0 && errno != ENOENT) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200241 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200242 msg = "error when trying to unlink previous UNIX socket";
243 goto err_return;
244 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200245
Willy Tarreauccfccef2014-05-10 01:49:15 +0200246 /* 3. backup existing socket */
247 if (link(path, backname) < 0 && errno != ENOENT) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200248 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200249 msg = "error when trying to preserve previous UNIX socket";
250 goto err_return;
251 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200252
Willy Tarreau719e07c2019-12-11 16:29:10 +0100253 strncpy(addr.sun_path, tempname, sizeof(addr.sun_path) - 1);
Willy Tarreauccfccef2014-05-10 01:49:15 +0200254 addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200255 }
Willy Tarreauccfccef2014-05-10 01:49:15 +0200256 else {
257 /* first char is zero, it's an abstract socket whose address
258 * is defined by all the bytes past this zero.
259 */
260 memcpy(addr.sun_path, path, sizeof(addr.sun_path));
261 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200262 addr.sun_family = AF_UNIX;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200263
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100264 fd = socket(PF_UNIX, SOCK_STREAM, 0);
265 if (fd < 0) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200266 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100267 msg = "cannot create UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200268 goto err_unlink_back;
269 }
270
Willy Tarreau40aa0702013-03-10 23:51:38 +0100271 fd_ready:
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100272 if (fd >= global.maxsock) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200273 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100274 msg = "socket(): not enough free sockets, raise -n argument";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200275 goto err_unlink_temp;
276 }
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100277
278 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200279 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100280 msg = "cannot make UNIX socket non-blocking";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200281 goto err_unlink_temp;
282 }
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100283
Willy Tarreau40aa0702013-03-10 23:51:38 +0100284 if (!ext && bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
Willy Tarreau92fb9832007-10-16 17:34:28 +0200285 /* note that bind() creates the socket <tempname> on the file system */
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200286 if (errno == EADDRINUSE) {
287 /* the old process might still own it, let's retry */
288 err |= ERR_RETRYABLE | ERR_ALERT;
289 msg = "cannot listen to socket";
290 }
291 else {
292 err |= ERR_FATAL | ERR_ALERT;
293 msg = "cannot bind UNIX socket";
294 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200295 goto err_unlink_temp;
296 }
297
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100298 /* <uid> and <gid> different of -1 will be used to change the socket owner.
299 * If <mode> is not 0, it will be used to restrict access to the socket.
300 * While it is known not to be portable on every OS, it's still useful
Willy Tarreauccfccef2014-05-10 01:49:15 +0200301 * where it works. We also don't change permissions on abstract sockets.
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100302 */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200303 if (!ext && path[0] &&
Willy Tarreau40aa0702013-03-10 23:51:38 +0100304 (((listener->bind_conf->ux.uid != -1 || listener->bind_conf->ux.gid != -1) &&
305 (chown(tempname, listener->bind_conf->ux.uid, listener->bind_conf->ux.gid) == -1)) ||
306 (listener->bind_conf->ux.mode != 0 && chmod(tempname, listener->bind_conf->ux.mode) == -1))) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200307 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100308 msg = "cannot change UNIX socket ownership";
Willy Tarreaue6ad2b12007-10-18 12:45:54 +0200309 goto err_unlink_temp;
310 }
311
Willy Tarreau40aa0702013-03-10 23:51:38 +0100312 ready = 0;
313 ready_len = sizeof(ready);
314 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1)
315 ready = 0;
316
317 if (!(ext && ready) && /* only listen if not already done by external process */
Willy Tarreaue2711c72019-02-27 15:39:41 +0100318 listen(fd, listener_backlog(listener)) < 0) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200319 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100320 msg = "cannot listen to UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200321 goto err_unlink_temp;
322 }
323
Willy Tarreauccfccef2014-05-10 01:49:15 +0200324 /* Point of no return: we are ready, we'll switch the sockets. We don't
Willy Tarreau92fb9832007-10-16 17:34:28 +0200325 * fear loosing the socket <path> because we have a copy of it in
Willy Tarreauccfccef2014-05-10 01:49:15 +0200326 * backname. Abstract sockets are not renamed.
Willy Tarreau92fb9832007-10-16 17:34:28 +0200327 */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200328 if (!ext && path[0] && rename(tempname, path) < 0) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200329 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100330 msg = "cannot switch final and temporary UNIX sockets";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200331 goto err_rename;
332 }
333
Willy Tarreau68986ab2017-06-16 10:34:20 +0200334 /* Cleanup: only unlink if we didn't inherit the fd from the parent */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200335 if (!ext && path[0])
Willy Tarreau40aa0702013-03-10 23:51:38 +0100336 unlink(backname);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200337
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100338 /* the socket is now listening */
339 listener->fd = fd;
340 listener->state = LI_LISTEN;
341
Willy Tarreaua9786b62018-01-25 07:22:13 +0100342 fd_insert(fd, listener, listener->proto->accept,
Willy Tarreau0948a782020-02-12 10:15:34 +0100343 thread_mask(listener->bind_conf->bind_thread) & all_threads_mask);
Willy Tarreaua9786b62018-01-25 07:22:13 +0100344
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200345 return err;
346
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100347 err_rename:
348 ret = rename(backname, path);
349 if (ret < 0 && errno == ENOENT)
350 unlink(path);
351 err_unlink_temp:
Jan Seda7319b642014-06-26 20:44:05 +0200352 if (!ext && path[0])
Willy Tarreau40aa0702013-03-10 23:51:38 +0100353 unlink(tempname);
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100354 close(fd);
355 err_unlink_back:
Jan Seda7319b642014-06-26 20:44:05 +0200356 if (!ext && path[0])
Willy Tarreau40aa0702013-03-10 23:51:38 +0100357 unlink(backname);
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100358 err_return:
Willy Tarreau40aa0702013-03-10 23:51:38 +0100359 if (msg && errlen) {
360 if (!ext)
361 snprintf(errmsg, errlen, "%s [%s]", msg, path);
362 else
363 snprintf(errmsg, errlen, "%s [fd %d]", msg, fd);
364 }
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200365 return err;
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100366}
367
368/* This function closes the UNIX sockets for the specified listener.
369 * The listener enters the LI_ASSIGNED state. It always returns ERR_NONE.
370 */
371static int uxst_unbind_listener(struct listener *listener)
372{
Willy Tarreaube58c382011-07-24 18:28:10 +0200373 if (listener->state > LI_ASSIGNED) {
374 unbind_listener(listener);
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100375 }
376 return ERR_NONE;
377}
378
Willy Tarreau32282382017-09-15 07:44:44 +0200379/* Add <listener> to the list of unix stream listeners (port is ignored). The
380 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
381 * The number of listeners for the protocol is updated.
Willy Tarreaudaacf362019-07-24 16:45:02 +0200382 *
383 * Must be called with proto_lock held.
384 *
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100385 */
Willy Tarreau9d5be5c2017-09-15 07:55:51 +0200386static void uxst_add_listener(struct listener *listener, int port)
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100387{
388 if (listener->state != LI_INIT)
389 return;
390 listener->state = LI_ASSIGNED;
391 listener->proto = &proto_unix;
392 LIST_ADDQ(&proto_unix.listeners, &listener->proto_list);
393 proto_unix.nb_listeners++;
394}
395
Willy Tarreaufd0e0082014-07-07 21:07:51 +0200396/* Pause a listener. Returns < 0 in case of failure, 0 if the listener
397 * was totally stopped, or > 0 if correctly paused. Nothing is done for
398 * plain unix sockets since currently it's the new process which handles
399 * the renaming. Abstract sockets are completely unbound.
400 */
Willy Tarreau31794892017-09-15 07:59:31 +0200401static int uxst_pause_listener(struct listener *l)
Willy Tarreaufd0e0082014-07-07 21:07:51 +0200402{
403 if (((struct sockaddr_un *)&l->addr)->sun_path[0])
404 return 1;
405
Christopher Faulet510c0d62018-03-16 10:04:47 +0100406 /* Listener's lock already held. Call lockless version of
407 * unbind_listener. */
408 do_unbind_listener(l, 1);
Willy Tarreaufd0e0082014-07-07 21:07:51 +0200409 return 0;
410}
411
Willy Tarreau47f48c42014-05-09 22:57:47 +0200412
413/*
414 * This function initiates a UNIX connection establishment to the target assigned
Willy Tarreauca9f5a92019-07-17 16:40:37 +0200415 * to connection <conn> using (si->{target,dst}). The source address is ignored
Willy Tarreau47f48c42014-05-09 22:57:47 +0200416 * and will be selected by the system. conn->target may point either to a valid
417 * server or to a backend, depending on conn->target. Only OBJ_TYPE_PROXY and
418 * OBJ_TYPE_SERVER are supported. The <data> parameter is a boolean indicating
419 * whether there are data waiting for being sent or not, in order to adjust data
420 * write polling and on some platforms. The <delack> argument is ignored.
421 *
422 * Note that a pending send_proxy message accounts for data.
423 *
424 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +0200425 * - SF_ERR_NONE if everything's OK
426 * - SF_ERR_SRVTO if there are no more servers
427 * - SF_ERR_SRVCL if the connection was refused by the server
428 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
429 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
430 * - SF_ERR_INTERNAL for any other purely internal errors
Tim Düsterhus4896c442016-11-29 02:15:19 +0100431 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau47f48c42014-05-09 22:57:47 +0200432 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200433 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
Willy Tarreau47f48c42014-05-09 22:57:47 +0200434 * it's invalid and the caller has nothing to do.
435 */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200436static int uxst_connect_server(struct connection *conn, int flags)
Willy Tarreau47f48c42014-05-09 22:57:47 +0200437{
438 int fd;
439 struct server *srv;
440 struct proxy *be;
441
Willy Tarreau47f48c42014-05-09 22:57:47 +0200442 switch (obj_type(conn->target)) {
443 case OBJ_TYPE_PROXY:
444 be = objt_proxy(conn->target);
445 srv = NULL;
446 break;
447 case OBJ_TYPE_SERVER:
448 srv = objt_server(conn->target);
449 be = srv->proxy;
450 break;
451 default:
452 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200453 return SF_ERR_INTERNAL;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200454 }
455
Willy Tarreau585744b2017-08-24 14:31:19 +0200456 if ((fd = conn->handle.fd = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
Willy Tarreau47f48c42014-05-09 22:57:47 +0200457 qfprintf(stderr, "Cannot get a server socket.\n");
458
459 if (errno == ENFILE) {
460 conn->err_code = CO_ER_SYS_FDLIM;
461 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100462 "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n",
463 be->id, global.maxsock);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200464 }
465 else if (errno == EMFILE) {
466 conn->err_code = CO_ER_PROC_FDLIM;
467 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100468 "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n",
469 be->id, global.maxsock);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200470 }
471 else if (errno == ENOBUFS || errno == ENOMEM) {
472 conn->err_code = CO_ER_SYS_MEMLIM;
473 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100474 "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n",
475 be->id, global.maxsock);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200476 }
477 else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
478 conn->err_code = CO_ER_NOPROTO;
479 }
480 else
481 conn->err_code = CO_ER_SOCK_ERR;
482
483 /* this is a resource error */
484 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200485 return SF_ERR_RESOURCE;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200486 }
487
488 if (fd >= global.maxsock) {
489 /* do not log anything there, it's a normal condition when this option
490 * is used to serialize connections to a server !
491 */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100492 ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
Willy Tarreau47f48c42014-05-09 22:57:47 +0200493 close(fd);
494 conn->err_code = CO_ER_CONF_FDLIM;
495 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200496 return SF_ERR_PRXCOND; /* it is a configuration limit */
Willy Tarreau47f48c42014-05-09 22:57:47 +0200497 }
498
499 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
500 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
501 close(fd);
502 conn->err_code = CO_ER_SOCK_ERR;
503 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200504 return SF_ERR_INTERNAL;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200505 }
506
William Lallemandc03eb012018-11-27 12:02:37 +0100507 if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) {
508 ha_alert("Cannot set CLOEXEC on client socket.\n");
509 close(fd);
510 conn->err_code = CO_ER_SOCK_ERR;
511 conn->flags |= CO_FL_ERROR;
512 return SF_ERR_INTERNAL;
513 }
514
Willy Tarreau47f48c42014-05-09 22:57:47 +0200515 /* if a send_proxy is there, there are data */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200516 if (conn->send_proxy_ofs)
517 flags |= CONNECT_HAS_DATA;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200518
519 if (global.tune.server_sndbuf)
520 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
521
522 if (global.tune.server_rcvbuf)
523 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
524
Willy Tarreauca9f5a92019-07-17 16:40:37 +0200525 if (connect(fd, (struct sockaddr *)conn->dst, get_addr_len(conn->dst)) == -1) {
Willy Tarreau94841792017-01-25 14:27:38 +0100526 if (errno == EINPROGRESS || errno == EALREADY) {
Willy Tarreau7bb21532014-05-10 09:48:28 +0200527 conn->flags |= CO_FL_WAIT_L4_CONN;
528 }
Willy Tarreau94841792017-01-25 14:27:38 +0100529 else if (errno == EISCONN) {
530 conn->flags &= ~CO_FL_WAIT_L4_CONN;
531 }
Willy Tarreau7bb21532014-05-10 09:48:28 +0200532 else if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau47f48c42014-05-09 22:57:47 +0200533 char *msg;
534 if (errno == EAGAIN || errno == EADDRNOTAVAIL) {
Lukas Tribus9f256d42016-01-26 20:33:14 +0100535 msg = "can't connect to destination unix socket, check backlog size on the server";
Willy Tarreau47f48c42014-05-09 22:57:47 +0200536 conn->err_code = CO_ER_FREE_PORTS;
537 }
538 else {
539 msg = "local address already in use";
540 conn->err_code = CO_ER_ADDR_INUSE;
541 }
542
543 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
544 close(fd);
545 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
546 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200547 return SF_ERR_RESOURCE;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200548 }
549 else if (errno == ETIMEDOUT) {
550 close(fd);
551 conn->err_code = CO_ER_SOCK_ERR;
552 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200553 return SF_ERR_SRVTO;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200554 }
555 else { // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
556 close(fd);
557 conn->err_code = CO_ER_SOCK_ERR;
558 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200559 return SF_ERR_SRVCL;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200560 }
561 }
Willy Tarreau7bb21532014-05-10 09:48:28 +0200562 else {
563 /* connect() already succeeded, which is quite usual for unix
Willy Tarreau94841792017-01-25 14:27:38 +0100564 * sockets. Let's avoid a second connect() probe to complete it.
Willy Tarreau7bb21532014-05-10 09:48:28 +0200565 */
566 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau7bb21532014-05-10 09:48:28 +0200567 }
Willy Tarreau47f48c42014-05-09 22:57:47 +0200568
569 conn->flags |= CO_FL_ADDR_TO_SET;
570
571 /* Prepare to send a few handshakes related to the on-wire protocol. */
572 if (conn->send_proxy_ofs)
573 conn->flags |= CO_FL_SEND_PROXY;
574
575 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreau7bb21532014-05-10 09:48:28 +0200576 fdtab[fd].linger_risk = 0; /* no need to disable lingering */
Willy Tarreau47f48c42014-05-09 22:57:47 +0200577
Willy Tarreau4c69cff2020-03-04 16:38:00 +0100578 if (conn->flags & CO_FL_WAIT_L4_CONN) {
579 fd_want_send(fd);
580 fd_cant_send(fd);
581 }
Willy Tarreauccf3f6d2019-09-05 17:05:05 +0200582
Willy Tarreau47f48c42014-05-09 22:57:47 +0200583 if (conn_xprt_init(conn) < 0) {
Willy Tarreau8c829012017-10-05 18:02:11 +0200584 conn_full_close(conn);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200585 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200586 return SF_ERR_RESOURCE;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200587 }
588
Willy Tarreaue7dff022015-04-03 01:14:29 +0200589 return SF_ERR_NONE; /* connection is OK */
Willy Tarreau47f48c42014-05-09 22:57:47 +0200590}
591
592
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100593/********************************
594 * 3) protocol-oriented functions
595 ********************************/
596
597
Willy Tarreau92fb9832007-10-16 17:34:28 +0200598/* This function creates all UNIX sockets bound to the protocol entry <proto>.
599 * It is intended to be used as the protocol's bind_all() function.
600 * The sockets will be registered but not added to any fd_set, in order not to
601 * loose them across the fork(). A call to uxst_enable_listeners() is needed
602 * to complete initialization.
603 *
Willy Tarreaudaacf362019-07-24 16:45:02 +0200604 * Must be called with proto_lock held.
605 *
Willy Tarreau92fb9832007-10-16 17:34:28 +0200606 * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL.
607 */
Emeric Bruncf20bf12010-10-22 16:06:11 +0200608static int uxst_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreau92fb9832007-10-16 17:34:28 +0200609{
610 struct listener *listener;
611 int err = ERR_NONE;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200612
613 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200614 err |= uxst_bind_listener(listener, errmsg, errlen);
615 if (err & ERR_ABORT)
616 break;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200617 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200618 return err;
619}
620
Willy Tarreau92fb9832007-10-16 17:34:28 +0200621
622/* This function stops all listening UNIX sockets bound to the protocol
623 * <proto>. It does not detaches them from the protocol.
624 * It always returns ERR_NONE.
Willy Tarreaudaacf362019-07-24 16:45:02 +0200625 *
626 * Must be called with proto_lock held.
627 *
Willy Tarreau92fb9832007-10-16 17:34:28 +0200628 */
629static int uxst_unbind_listeners(struct protocol *proto)
630{
631 struct listener *listener;
632
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100633 list_for_each_entry(listener, &proto->listeners, proto_list)
634 uxst_unbind_listener(listener);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200635 return ERR_NONE;
636}
637
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200638/* parse the "mode" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200639static 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 +0200640{
Willy Tarreaua1a247b2017-10-04 14:43:44 +0200641 char *endptr;
642
643 conf->ux.mode = strtol(args[cur_arg + 1], &endptr, 8);
644
645 if (!*args[cur_arg + 1] || *endptr) {
646 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 +0200647 return ERR_ALERT | ERR_FATAL;
648 }
649
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200650 return 0;
651}
652
653/* parse the "gid" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200654static 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 +0200655{
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200656 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200657 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200658 return ERR_ALERT | ERR_FATAL;
659 }
660
Willy Tarreau290e63a2012-09-20 18:07:14 +0200661 conf->ux.gid = atol(args[cur_arg + 1]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200662 return 0;
663}
664
665/* parse the "group" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200666static 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 +0200667{
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200668 struct group *group;
669
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200670 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200671 memprintf(err, "'%s' : missing group name", args[cur_arg]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200672 return ERR_ALERT | ERR_FATAL;
673 }
674
675 group = getgrnam(args[cur_arg + 1]);
676 if (!group) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200677 memprintf(err, "'%s' : unknown group name '%s'", args[cur_arg], args[cur_arg + 1]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200678 return ERR_ALERT | ERR_FATAL;
679 }
680
Willy Tarreau290e63a2012-09-20 18:07:14 +0200681 conf->ux.gid = group->gr_gid;
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200682 return 0;
683}
684
685/* parse the "uid" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200686static 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 +0200687{
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200688 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200689 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200690 return ERR_ALERT | ERR_FATAL;
691 }
692
Willy Tarreau290e63a2012-09-20 18:07:14 +0200693 conf->ux.uid = atol(args[cur_arg + 1]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200694 return 0;
695}
696
697/* parse the "user" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200698static 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 +0200699{
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200700 struct passwd *user;
701
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200702 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200703 memprintf(err, "'%s' : missing user name", args[cur_arg]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200704 return ERR_ALERT | ERR_FATAL;
705 }
706
707 user = getpwnam(args[cur_arg + 1]);
708 if (!user) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200709 memprintf(err, "'%s' : unknown user name '%s'", args[cur_arg], args[cur_arg + 1]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200710 return ERR_ALERT | ERR_FATAL;
711 }
712
Willy Tarreau290e63a2012-09-20 18:07:14 +0200713 conf->ux.uid = user->pw_uid;
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200714 return 0;
715}
716
717/* Note: must not be declared <const> as its list will be overwritten.
718 * Please take care of keeping this list alphabetically sorted, doing so helps
719 * all code contributors.
720 * Optional keywords are also declared with a NULL ->parse() function so that
721 * the config parser can report an appropriate error when a known keyword was
722 * not enabled.
723 */
Willy Tarreau51fb7652012-09-18 18:24:39 +0200724static struct bind_kw_list bind_kws = { "UNIX", { }, {
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200725 { "gid", bind_parse_gid, 1 }, /* set the socket's gid */
726 { "group", bind_parse_group, 1 }, /* set the socket's gid from the group name */
727 { "mode", bind_parse_mode, 1 }, /* set the socket's mode (eg: 0644)*/
728 { "uid", bind_parse_uid, 1 }, /* set the socket's uid */
729 { "user", bind_parse_user, 1 }, /* set the socket's uid from the user name */
730 { NULL, NULL, 0 },
731}};
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100732
Willy Tarreau0108d902018-11-25 19:14:37 +0100733INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200734
735/*
736 * Local variables:
737 * c-indent-level: 8
738 * c-basic-offset: 8
739 * End:
740 */