blob: 66093af65a6cf4cfb86db2f24ea0f0d755dff7fc [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 Tarreau40aa0702013-03-10 23:51:38 +0100190 int ext, ready;
191 socklen_t ready_len;
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200192 int err;
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100193 int ret;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200194
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200195 err = ERR_NONE;
196
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100197 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100198 if (errlen)
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100199 *errmsg = 0;
200
201 if (listener->state != LI_ASSIGNED)
202 return ERR_NONE; /* already bound */
203
Olivier Houchardf886e342017-04-05 22:24:59 +0200204 if (listener->fd == -1)
205 listener->fd = uxst_find_compatible_fd(listener);
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100206 path = ((struct sockaddr_un *)&listener->addr)->sun_path;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200207
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]) {
219 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 Tarreauccfccef2014-05-10 01:49:15 +0200222 msg = "name too long for UNIX socket";
223 goto err_return;
224 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200225
Willy Tarreauccfccef2014-05-10 01:49:15 +0200226 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 Tarreauccfccef2014-05-10 01:49:15 +0200229 msg = "name too long for UNIX socket";
230 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 Tarreauccfccef2014-05-10 01:49:15 +0200253 strncpy(addr.sun_path, tempname, sizeof(addr.sun_path));
254 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 Tarreaua36b3242019-02-02 13:14:34 +0100343 thread_mask(listener->bind_conf->bind_thread));
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 Tarreaudabf2e22007-10-28 21:59:24 +0100382 */
Willy Tarreau9d5be5c2017-09-15 07:55:51 +0200383static void uxst_add_listener(struct listener *listener, int port)
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100384{
385 if (listener->state != LI_INIT)
386 return;
387 listener->state = LI_ASSIGNED;
388 listener->proto = &proto_unix;
389 LIST_ADDQ(&proto_unix.listeners, &listener->proto_list);
390 proto_unix.nb_listeners++;
391}
392
Willy Tarreaufd0e0082014-07-07 21:07:51 +0200393/* Pause a listener. Returns < 0 in case of failure, 0 if the listener
394 * was totally stopped, or > 0 if correctly paused. Nothing is done for
395 * plain unix sockets since currently it's the new process which handles
396 * the renaming. Abstract sockets are completely unbound.
397 */
Willy Tarreau31794892017-09-15 07:59:31 +0200398static int uxst_pause_listener(struct listener *l)
Willy Tarreaufd0e0082014-07-07 21:07:51 +0200399{
400 if (((struct sockaddr_un *)&l->addr)->sun_path[0])
401 return 1;
402
Christopher Faulet510c0d62018-03-16 10:04:47 +0100403 /* Listener's lock already held. Call lockless version of
404 * unbind_listener. */
405 do_unbind_listener(l, 1);
Willy Tarreaufd0e0082014-07-07 21:07:51 +0200406 return 0;
407}
408
Willy Tarreau47f48c42014-05-09 22:57:47 +0200409
410/*
411 * This function initiates a UNIX connection establishment to the target assigned
412 * to connection <conn> using (si->{target,addr.to}). The source address is ignored
413 * and will be selected by the system. conn->target may point either to a valid
414 * server or to a backend, depending on conn->target. Only OBJ_TYPE_PROXY and
415 * OBJ_TYPE_SERVER are supported. The <data> parameter is a boolean indicating
416 * whether there are data waiting for being sent or not, in order to adjust data
417 * write polling and on some platforms. The <delack> argument is ignored.
418 *
419 * Note that a pending send_proxy message accounts for data.
420 *
421 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +0200422 * - SF_ERR_NONE if everything's OK
423 * - SF_ERR_SRVTO if there are no more servers
424 * - SF_ERR_SRVCL if the connection was refused by the server
425 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
426 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
427 * - SF_ERR_INTERNAL for any other purely internal errors
Tim Düsterhus4896c442016-11-29 02:15:19 +0100428 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau47f48c42014-05-09 22:57:47 +0200429 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200430 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
Willy Tarreau47f48c42014-05-09 22:57:47 +0200431 * it's invalid and the caller has nothing to do.
432 */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200433static int uxst_connect_server(struct connection *conn, int flags)
Willy Tarreau47f48c42014-05-09 22:57:47 +0200434{
435 int fd;
436 struct server *srv;
437 struct proxy *be;
438
Willy Tarreau47f48c42014-05-09 22:57:47 +0200439 switch (obj_type(conn->target)) {
440 case OBJ_TYPE_PROXY:
441 be = objt_proxy(conn->target);
442 srv = NULL;
443 break;
444 case OBJ_TYPE_SERVER:
445 srv = objt_server(conn->target);
446 be = srv->proxy;
447 break;
448 default:
449 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200450 return SF_ERR_INTERNAL;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200451 }
452
Willy Tarreau585744b2017-08-24 14:31:19 +0200453 if ((fd = conn->handle.fd = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
Willy Tarreau47f48c42014-05-09 22:57:47 +0200454 qfprintf(stderr, "Cannot get a server socket.\n");
455
456 if (errno == ENFILE) {
457 conn->err_code = CO_ER_SYS_FDLIM;
458 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100459 "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n",
460 be->id, global.maxsock);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200461 }
462 else if (errno == EMFILE) {
463 conn->err_code = CO_ER_PROC_FDLIM;
464 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100465 "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n",
466 be->id, global.maxsock);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200467 }
468 else if (errno == ENOBUFS || errno == ENOMEM) {
469 conn->err_code = CO_ER_SYS_MEMLIM;
470 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100471 "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n",
472 be->id, global.maxsock);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200473 }
474 else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
475 conn->err_code = CO_ER_NOPROTO;
476 }
477 else
478 conn->err_code = CO_ER_SOCK_ERR;
479
480 /* this is a resource error */
481 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200482 return SF_ERR_RESOURCE;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200483 }
484
485 if (fd >= global.maxsock) {
486 /* do not log anything there, it's a normal condition when this option
487 * is used to serialize connections to a server !
488 */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100489 ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
Willy Tarreau47f48c42014-05-09 22:57:47 +0200490 close(fd);
491 conn->err_code = CO_ER_CONF_FDLIM;
492 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200493 return SF_ERR_PRXCOND; /* it is a configuration limit */
Willy Tarreau47f48c42014-05-09 22:57:47 +0200494 }
495
496 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
497 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
498 close(fd);
499 conn->err_code = CO_ER_SOCK_ERR;
500 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200501 return SF_ERR_INTERNAL;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200502 }
503
William Lallemandc03eb012018-11-27 12:02:37 +0100504 if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) {
505 ha_alert("Cannot set CLOEXEC on client socket.\n");
506 close(fd);
507 conn->err_code = CO_ER_SOCK_ERR;
508 conn->flags |= CO_FL_ERROR;
509 return SF_ERR_INTERNAL;
510 }
511
Willy Tarreau47f48c42014-05-09 22:57:47 +0200512 /* if a send_proxy is there, there are data */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200513 if (conn->send_proxy_ofs)
514 flags |= CONNECT_HAS_DATA;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200515
516 if (global.tune.server_sndbuf)
517 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
518
519 if (global.tune.server_rcvbuf)
520 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
521
Willy Tarreau7bb21532014-05-10 09:48:28 +0200522 if (connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) == -1) {
Willy Tarreau94841792017-01-25 14:27:38 +0100523 if (errno == EINPROGRESS || errno == EALREADY) {
Willy Tarreau7bb21532014-05-10 09:48:28 +0200524 conn->flags |= CO_FL_WAIT_L4_CONN;
525 }
Willy Tarreau94841792017-01-25 14:27:38 +0100526 else if (errno == EISCONN) {
527 conn->flags &= ~CO_FL_WAIT_L4_CONN;
528 }
Willy Tarreau7bb21532014-05-10 09:48:28 +0200529 else if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau47f48c42014-05-09 22:57:47 +0200530 char *msg;
531 if (errno == EAGAIN || errno == EADDRNOTAVAIL) {
Lukas Tribus9f256d42016-01-26 20:33:14 +0100532 msg = "can't connect to destination unix socket, check backlog size on the server";
Willy Tarreau47f48c42014-05-09 22:57:47 +0200533 conn->err_code = CO_ER_FREE_PORTS;
534 }
535 else {
536 msg = "local address already in use";
537 conn->err_code = CO_ER_ADDR_INUSE;
538 }
539
540 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
541 close(fd);
542 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
543 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200544 return SF_ERR_RESOURCE;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200545 }
546 else if (errno == ETIMEDOUT) {
547 close(fd);
548 conn->err_code = CO_ER_SOCK_ERR;
549 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200550 return SF_ERR_SRVTO;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200551 }
552 else { // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
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_SRVCL;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200557 }
558 }
Willy Tarreau7bb21532014-05-10 09:48:28 +0200559 else {
560 /* connect() already succeeded, which is quite usual for unix
Willy Tarreau94841792017-01-25 14:27:38 +0100561 * sockets. Let's avoid a second connect() probe to complete it.
Willy Tarreau7bb21532014-05-10 09:48:28 +0200562 */
563 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau7bb21532014-05-10 09:48:28 +0200564 }
Willy Tarreau47f48c42014-05-09 22:57:47 +0200565
566 conn->flags |= CO_FL_ADDR_TO_SET;
567
568 /* Prepare to send a few handshakes related to the on-wire protocol. */
569 if (conn->send_proxy_ofs)
570 conn->flags |= CO_FL_SEND_PROXY;
571
572 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreau7bb21532014-05-10 09:48:28 +0200573 fdtab[fd].linger_risk = 0; /* no need to disable lingering */
Willy Tarreau47f48c42014-05-09 22:57:47 +0200574
575 if (conn_xprt_init(conn) < 0) {
Willy Tarreau8c829012017-10-05 18:02:11 +0200576 conn_full_close(conn);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200577 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200578 return SF_ERR_RESOURCE;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200579 }
580
Olivier Houchard03abf2d2019-05-28 10:12:02 +0200581 conn_xprt_want_send(conn); /* for connect status, proxy protocol or SSL */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200582 return SF_ERR_NONE; /* connection is OK */
Willy Tarreau47f48c42014-05-09 22:57:47 +0200583}
584
585
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100586/********************************
587 * 3) protocol-oriented functions
588 ********************************/
589
590
Willy Tarreau92fb9832007-10-16 17:34:28 +0200591/* This function creates all UNIX sockets bound to the protocol entry <proto>.
592 * It is intended to be used as the protocol's bind_all() function.
593 * The sockets will be registered but not added to any fd_set, in order not to
594 * loose them across the fork(). A call to uxst_enable_listeners() is needed
595 * to complete initialization.
596 *
597 * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL.
598 */
Emeric Bruncf20bf12010-10-22 16:06:11 +0200599static int uxst_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreau92fb9832007-10-16 17:34:28 +0200600{
601 struct listener *listener;
602 int err = ERR_NONE;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200603
604 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200605 err |= uxst_bind_listener(listener, errmsg, errlen);
606 if (err & ERR_ABORT)
607 break;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200608 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200609 return err;
610}
611
Willy Tarreau92fb9832007-10-16 17:34:28 +0200612
613/* This function stops all listening UNIX sockets bound to the protocol
614 * <proto>. It does not detaches them from the protocol.
615 * It always returns ERR_NONE.
616 */
617static int uxst_unbind_listeners(struct protocol *proto)
618{
619 struct listener *listener;
620
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100621 list_for_each_entry(listener, &proto->listeners, proto_list)
622 uxst_unbind_listener(listener);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200623 return ERR_NONE;
624}
625
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200626/* parse the "mode" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200627static 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 +0200628{
Willy Tarreaua1a247b2017-10-04 14:43:44 +0200629 char *endptr;
630
631 conf->ux.mode = strtol(args[cur_arg + 1], &endptr, 8);
632
633 if (!*args[cur_arg + 1] || *endptr) {
634 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 +0200635 return ERR_ALERT | ERR_FATAL;
636 }
637
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200638 return 0;
639}
640
641/* parse the "gid" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200642static 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 +0200643{
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200644 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200645 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200646 return ERR_ALERT | ERR_FATAL;
647 }
648
Willy Tarreau290e63a2012-09-20 18:07:14 +0200649 conf->ux.gid = atol(args[cur_arg + 1]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200650 return 0;
651}
652
653/* parse the "group" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200654static 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 +0200655{
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200656 struct group *group;
657
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200658 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200659 memprintf(err, "'%s' : missing group name", args[cur_arg]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200660 return ERR_ALERT | ERR_FATAL;
661 }
662
663 group = getgrnam(args[cur_arg + 1]);
664 if (!group) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200665 memprintf(err, "'%s' : unknown group name '%s'", args[cur_arg], args[cur_arg + 1]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200666 return ERR_ALERT | ERR_FATAL;
667 }
668
Willy Tarreau290e63a2012-09-20 18:07:14 +0200669 conf->ux.gid = group->gr_gid;
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200670 return 0;
671}
672
673/* parse the "uid" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200674static 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 +0200675{
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200676 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200677 memprintf(err, "'%s' : missing value", args[cur_arg]);
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.uid = atol(args[cur_arg + 1]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200682 return 0;
683}
684
685/* parse the "user" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200686static 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 +0200687{
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200688 struct passwd *user;
689
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200690 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200691 memprintf(err, "'%s' : missing user name", args[cur_arg]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200692 return ERR_ALERT | ERR_FATAL;
693 }
694
695 user = getpwnam(args[cur_arg + 1]);
696 if (!user) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200697 memprintf(err, "'%s' : unknown user name '%s'", args[cur_arg], args[cur_arg + 1]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200698 return ERR_ALERT | ERR_FATAL;
699 }
700
Willy Tarreau290e63a2012-09-20 18:07:14 +0200701 conf->ux.uid = user->pw_uid;
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200702 return 0;
703}
704
705/* Note: must not be declared <const> as its list will be overwritten.
706 * Please take care of keeping this list alphabetically sorted, doing so helps
707 * all code contributors.
708 * Optional keywords are also declared with a NULL ->parse() function so that
709 * the config parser can report an appropriate error when a known keyword was
710 * not enabled.
711 */
Willy Tarreau51fb7652012-09-18 18:24:39 +0200712static struct bind_kw_list bind_kws = { "UNIX", { }, {
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200713 { "gid", bind_parse_gid, 1 }, /* set the socket's gid */
714 { "group", bind_parse_group, 1 }, /* set the socket's gid from the group name */
715 { "mode", bind_parse_mode, 1 }, /* set the socket's mode (eg: 0644)*/
716 { "uid", bind_parse_uid, 1 }, /* set the socket's uid */
717 { "user", bind_parse_user, 1 }, /* set the socket's uid from the user name */
718 { NULL, NULL, 0 },
719}};
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100720
Willy Tarreau0108d902018-11-25 19:14:37 +0100721INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200722
723/*
724 * Local variables:
725 * c-indent-level: 8
726 * c-basic-offset: 8
727 * End:
728 */