blob: d68267e836bce832bbfb0ee2cc7ae3beff497547 [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 Tarreau92fb9832007-10-16 17:34:28 +020033#include <common/mini-clist.h>
34#include <common/standard.h>
35#include <common/time.h>
36#include <common/version.h>
37
Willy Tarreau92fb9832007-10-16 17:34:28 +020038#include <types/global.h>
Willy Tarreau92fb9832007-10-16 17:34:28 +020039
Willy Tarreau47f48c42014-05-09 22:57:47 +020040#include <proto/connection.h>
Willy Tarreau92fb9832007-10-16 17:34:28 +020041#include <proto/fd.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020042#include <proto/listener.h>
Willy Tarreau92fb9832007-10-16 17:34:28 +020043#include <proto/log.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020044#include <proto/protocol.h>
Willy Tarreau92fb9832007-10-16 17:34:28 +020045#include <proto/proto_uxst.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);
Willy Tarreau47f48c42014-05-09 22:57:47 +020051static int uxst_connect_server(struct connection *conn, int data, int delack);
Willy Tarreaudabf2e22007-10-28 21:59:24 +010052
53/* Note: must not be declared <const> as its list will be overwritten */
54static struct protocol proto_unix = {
55 .name = "unix_stream",
56 .sock_domain = PF_UNIX,
57 .sock_type = SOCK_STREAM,
58 .sock_prot = 0,
59 .sock_family = AF_UNIX,
60 .sock_addrlen = sizeof(struct sockaddr_un),
61 .l3_addrlen = sizeof(((struct sockaddr_un*)0)->sun_path),/* path len */
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020062 .accept = &listener_accept,
Willy Tarreau47f48c42014-05-09 22:57:47 +020063 .connect = &uxst_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020064 .bind = uxst_bind_listener,
Willy Tarreaudabf2e22007-10-28 21:59:24 +010065 .bind_all = uxst_bind_listeners,
66 .unbind_all = uxst_unbind_listeners,
67 .enable_all = enable_all_listeners,
68 .disable_all = disable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +020069 .get_src = uxst_get_src,
70 .get_dst = uxst_get_dst,
Willy Tarreaufd0e0082014-07-07 21:07:51 +020071 .pause = uxst_pause_listener,
Willy Tarreaudabf2e22007-10-28 21:59:24 +010072 .listeners = LIST_HEAD_INIT(proto_unix.listeners),
73 .nb_listeners = 0,
74};
75
Willy Tarreaudabf2e22007-10-28 21:59:24 +010076/********************************
77 * 1) low-level socket functions
78 ********************************/
79
Willy Tarreau59b94792012-05-11 16:16:40 +020080/*
81 * Retrieves the source address for the socket <fd>, with <dir> indicating
82 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
83 * success, -1 in case of error. The socket's source address is stored in
84 * <sa> for <salen> bytes.
85 */
86int uxst_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
87{
88 if (dir)
89 return getsockname(fd, sa, &salen);
90 else
91 return getpeername(fd, sa, &salen);
92}
93
94
95/*
96 * Retrieves the original destination address for the socket <fd>, with <dir>
97 * indicating if we're a listener (=0) or an initiator (!=0). It returns 0 in
98 * case of success, -1 in case of error. The socket's source address is stored
99 * in <sa> for <salen> bytes.
100 */
101int uxst_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
102{
103 if (dir)
104 return getpeername(fd, sa, &salen);
105 else
106 return getsockname(fd, sa, &salen);
107}
108
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100109
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100110/* Tries to destroy the UNIX stream socket <path>. The socket must not be used
111 * anymore. It practises best effort, and no error is returned.
112 */
113static void destroy_uxst_socket(const char *path)
114{
115 struct sockaddr_un addr;
116 int sock, ret;
117
Willy Tarreau40aa0702013-03-10 23:51:38 +0100118 /* if the path was cleared, we do nothing */
119 if (!*path)
120 return;
121
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100122 /* We might have been chrooted, so we may not be able to access the
123 * socket. In order to avoid bothering the other end, we connect with a
124 * wrong protocol, namely SOCK_DGRAM. The return code from connect()
125 * is enough to know if the socket is still live or not. If it's live
126 * in mode SOCK_STREAM, we get EPROTOTYPE or anything else but not
127 * ECONNREFUSED. In this case, we do not touch it because it's used
128 * by some other process.
129 */
130 sock = socket(PF_UNIX, SOCK_DGRAM, 0);
131 if (sock < 0)
132 return;
133
134 addr.sun_family = AF_UNIX;
135 strncpy(addr.sun_path, path, sizeof(addr.sun_path));
136 addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
137 ret = connect(sock, (struct sockaddr *)&addr, sizeof(addr));
138 if (ret < 0 && errno == ECONNREFUSED) {
139 /* Connect failed: the socket still exists but is not used
140 * anymore. Let's remove this socket now.
141 */
142 unlink(path);
143 }
144 close(sock);
145}
146
147
148/********************************
149 * 2) listener-oriented functions
150 ********************************/
151
152
Olivier Houchardf886e342017-04-05 22:24:59 +0200153static int uxst_find_compatible_fd(struct listener *l)
154{
155 struct xfer_sock_list *xfer_sock = xfer_sock_list;
156 int ret = -1;
157
158 while (xfer_sock) {
159 struct sockaddr_un *un1 = (void *)&l->addr;
160 struct sockaddr_un *un2 = (void *)&xfer_sock->addr;
161
162 /*
163 * The bound socket's path as returned by getsockaddr
164 * will be the temporary name <sockname>.XXXXX.tmp,
165 * so we can't just compare the two names
166 */
167 if (xfer_sock->addr.ss_family == AF_UNIX &&
168 strncmp(un1->sun_path, un2->sun_path,
169 strlen(un1->sun_path)) == 0) {
170 char *after_sockname = un2->sun_path +
171 strlen(un1->sun_path);
172 /* Make a reasonnable effort to check that
173 * it is indeed a haproxy-generated temporary
174 * name, it's not perfect, but probably good enough.
175 */
176 if (after_sockname[0] == '.') {
177 after_sockname++;
178 while (after_sockname[0] >= '0' &&
179 after_sockname[0] <= '9')
180 after_sockname++;
181 if (!strcmp(after_sockname, ".tmp"))
182 break;
183 }
184 }
185 xfer_sock = xfer_sock->next;
186 }
187 if (xfer_sock != NULL) {
188 ret = xfer_sock->fd;
189 if (xfer_sock == xfer_sock_list)
190 xfer_sock_list = xfer_sock->next;
191 if (xfer_sock->prev)
192 xfer_sock->prev->next = xfer_sock->next;
193 if (xfer_sock->next)
194 xfer_sock->next->prev = xfer_sock->next->prev;
195 free(xfer_sock);
196 }
197 return ret;
198
199}
200
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100201/* This function creates a UNIX socket associated to the listener. It changes
202 * the state from ASSIGNED to LISTEN. The socket is NOT enabled for polling.
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100203 * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL. It
204 * may return a warning or an error message in <errmsg> if the message is at
205 * most <errlen> bytes long (including '\0'). Note that <errmsg> may be NULL if
206 * <errlen> is also zero.
Willy Tarreau92fb9832007-10-16 17:34:28 +0200207 */
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100208static int uxst_bind_listener(struct listener *listener, char *errmsg, int errlen)
Willy Tarreau92fb9832007-10-16 17:34:28 +0200209{
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100210 int fd;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200211 char tempname[MAXPATHLEN];
212 char backname[MAXPATHLEN];
213 struct sockaddr_un addr;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100214 const char *msg = NULL;
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100215 const char *path;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100216 int ext, ready;
217 socklen_t ready_len;
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200218 int err;
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100219 int ret;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200220
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200221 err = ERR_NONE;
222
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100223 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100224 if (errlen)
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100225 *errmsg = 0;
226
227 if (listener->state != LI_ASSIGNED)
228 return ERR_NONE; /* already bound */
229
Olivier Houchardf886e342017-04-05 22:24:59 +0200230 if (listener->fd == -1)
231 listener->fd = uxst_find_compatible_fd(listener);
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100232 path = ((struct sockaddr_un *)&listener->addr)->sun_path;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200233
Willy Tarreau40aa0702013-03-10 23:51:38 +0100234 /* if the listener already has an fd assigned, then we were offered the
235 * fd by an external process (most likely the parent), and we don't want
236 * to create a new socket. However we still want to set a few flags on
237 * the socket.
238 */
239 fd = listener->fd;
240 ext = (fd >= 0);
241 if (ext)
242 goto fd_ready;
243
Willy Tarreauccfccef2014-05-10 01:49:15 +0200244 if (path[0]) {
245 ret = snprintf(tempname, MAXPATHLEN, "%s.%d.tmp", path, pid);
246 if (ret < 0 || ret >= MAXPATHLEN) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200247 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200248 msg = "name too long for UNIX socket";
249 goto err_return;
250 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200251
Willy Tarreauccfccef2014-05-10 01:49:15 +0200252 ret = snprintf(backname, MAXPATHLEN, "%s.%d.bak", path, pid);
253 if (ret < 0 || ret >= MAXPATHLEN) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200254 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200255 msg = "name too long for UNIX socket";
256 goto err_return;
257 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200258
Willy Tarreauccfccef2014-05-10 01:49:15 +0200259 /* 2. clean existing orphaned entries */
260 if (unlink(tempname) < 0 && errno != ENOENT) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200261 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200262 msg = "error when trying to unlink previous UNIX socket";
263 goto err_return;
264 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200265
Willy Tarreauccfccef2014-05-10 01:49:15 +0200266 if (unlink(backname) < 0 && errno != ENOENT) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200267 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200268 msg = "error when trying to unlink previous UNIX socket";
269 goto err_return;
270 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200271
Willy Tarreauccfccef2014-05-10 01:49:15 +0200272 /* 3. backup existing socket */
273 if (link(path, backname) < 0 && errno != ENOENT) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200274 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200275 msg = "error when trying to preserve previous UNIX socket";
276 goto err_return;
277 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200278
Willy Tarreauccfccef2014-05-10 01:49:15 +0200279 strncpy(addr.sun_path, tempname, sizeof(addr.sun_path));
280 addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200281 }
Willy Tarreauccfccef2014-05-10 01:49:15 +0200282 else {
283 /* first char is zero, it's an abstract socket whose address
284 * is defined by all the bytes past this zero.
285 */
286 memcpy(addr.sun_path, path, sizeof(addr.sun_path));
287 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200288 addr.sun_family = AF_UNIX;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200289
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100290 fd = socket(PF_UNIX, SOCK_STREAM, 0);
291 if (fd < 0) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200292 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100293 msg = "cannot create UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200294 goto err_unlink_back;
295 }
296
Willy Tarreau40aa0702013-03-10 23:51:38 +0100297 fd_ready:
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100298 if (fd >= global.maxsock) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200299 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100300 msg = "socket(): not enough free sockets, raise -n argument";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200301 goto err_unlink_temp;
302 }
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100303
304 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200305 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100306 msg = "cannot make UNIX socket non-blocking";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200307 goto err_unlink_temp;
308 }
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100309
Willy Tarreau40aa0702013-03-10 23:51:38 +0100310 if (!ext && bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
Willy Tarreau92fb9832007-10-16 17:34:28 +0200311 /* note that bind() creates the socket <tempname> on the file system */
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200312 if (errno == EADDRINUSE) {
313 /* the old process might still own it, let's retry */
314 err |= ERR_RETRYABLE | ERR_ALERT;
315 msg = "cannot listen to socket";
316 }
317 else {
318 err |= ERR_FATAL | ERR_ALERT;
319 msg = "cannot bind UNIX socket";
320 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200321 goto err_unlink_temp;
322 }
323
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100324 /* <uid> and <gid> different of -1 will be used to change the socket owner.
325 * If <mode> is not 0, it will be used to restrict access to the socket.
326 * While it is known not to be portable on every OS, it's still useful
Willy Tarreauccfccef2014-05-10 01:49:15 +0200327 * where it works. We also don't change permissions on abstract sockets.
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100328 */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200329 if (!ext && path[0] &&
Willy Tarreau40aa0702013-03-10 23:51:38 +0100330 (((listener->bind_conf->ux.uid != -1 || listener->bind_conf->ux.gid != -1) &&
331 (chown(tempname, listener->bind_conf->ux.uid, listener->bind_conf->ux.gid) == -1)) ||
332 (listener->bind_conf->ux.mode != 0 && chmod(tempname, listener->bind_conf->ux.mode) == -1))) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200333 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100334 msg = "cannot change UNIX socket ownership";
Willy Tarreaue6ad2b12007-10-18 12:45:54 +0200335 goto err_unlink_temp;
336 }
337
Willy Tarreau40aa0702013-03-10 23:51:38 +0100338 ready = 0;
339 ready_len = sizeof(ready);
340 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1)
341 ready = 0;
342
343 if (!(ext && ready) && /* only listen if not already done by external process */
344 listen(fd, listener->backlog ? listener->backlog : listener->maxconn) < 0) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200345 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100346 msg = "cannot listen to UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200347 goto err_unlink_temp;
348 }
349
Willy Tarreauccfccef2014-05-10 01:49:15 +0200350 /* Point of no return: we are ready, we'll switch the sockets. We don't
Willy Tarreau92fb9832007-10-16 17:34:28 +0200351 * fear loosing the socket <path> because we have a copy of it in
Willy Tarreauccfccef2014-05-10 01:49:15 +0200352 * backname. Abstract sockets are not renamed.
Willy Tarreau92fb9832007-10-16 17:34:28 +0200353 */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200354 if (!ext && path[0] && rename(tempname, path) < 0) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200355 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100356 msg = "cannot switch final and temporary UNIX sockets";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200357 goto err_rename;
358 }
359
Willy Tarreauccfccef2014-05-10 01:49:15 +0200360 /* Cleanup: If we're bound to an fd inherited from the parent, we
Willy Tarreau40aa0702013-03-10 23:51:38 +0100361 * want to ensure that destroy_uxst_socket() will never remove the
Willy Tarreauccfccef2014-05-10 01:49:15 +0200362 * path, and for this we simply clear the path to the socket, which
363 * under Linux corresponds to an abstract socket.
Willy Tarreau40aa0702013-03-10 23:51:38 +0100364 */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200365 if (!ext && path[0])
Willy Tarreau40aa0702013-03-10 23:51:38 +0100366 unlink(backname);
367 else
368 ((struct sockaddr_un *)&listener->addr)->sun_path[0] = 0;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200369
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100370 /* the socket is now listening */
371 listener->fd = fd;
372 listener->state = LI_LISTEN;
373
374 /* the function for the accept() event */
375 fd_insert(fd);
Willy Tarreauaece46a2012-07-06 12:25:58 +0200376 fdtab[fd].iocb = listener->proto->accept;
Willy Tarreaueabf3132008-08-29 23:36:51 +0200377 fdtab[fd].owner = listener; /* reference the listener instead of a task */
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200378 return err;
379
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100380 err_rename:
381 ret = rename(backname, path);
382 if (ret < 0 && errno == ENOENT)
383 unlink(path);
384 err_unlink_temp:
Jan Seda7319b642014-06-26 20:44:05 +0200385 if (!ext && path[0])
Willy Tarreau40aa0702013-03-10 23:51:38 +0100386 unlink(tempname);
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100387 close(fd);
388 err_unlink_back:
Jan Seda7319b642014-06-26 20:44:05 +0200389 if (!ext && path[0])
Willy Tarreau40aa0702013-03-10 23:51:38 +0100390 unlink(backname);
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100391 err_return:
Willy Tarreau40aa0702013-03-10 23:51:38 +0100392 if (msg && errlen) {
393 if (!ext)
394 snprintf(errmsg, errlen, "%s [%s]", msg, path);
395 else
396 snprintf(errmsg, errlen, "%s [fd %d]", msg, fd);
397 }
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200398 return err;
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100399}
400
401/* This function closes the UNIX sockets for the specified listener.
402 * The listener enters the LI_ASSIGNED state. It always returns ERR_NONE.
403 */
404static int uxst_unbind_listener(struct listener *listener)
405{
Willy Tarreaube58c382011-07-24 18:28:10 +0200406 if (listener->state > LI_ASSIGNED) {
407 unbind_listener(listener);
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100408 destroy_uxst_socket(((struct sockaddr_un *)&listener->addr)->sun_path);
409 }
410 return ERR_NONE;
411}
412
413/* Add a listener to the list of unix stream listeners. The listener's state
414 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
415 * listeners is updated. This is the function to use to add a new listener.
416 */
417void uxst_add_listener(struct listener *listener)
418{
419 if (listener->state != LI_INIT)
420 return;
421 listener->state = LI_ASSIGNED;
422 listener->proto = &proto_unix;
423 LIST_ADDQ(&proto_unix.listeners, &listener->proto_list);
424 proto_unix.nb_listeners++;
425}
426
Willy Tarreaufd0e0082014-07-07 21:07:51 +0200427/* Pause a listener. Returns < 0 in case of failure, 0 if the listener
428 * was totally stopped, or > 0 if correctly paused. Nothing is done for
429 * plain unix sockets since currently it's the new process which handles
430 * the renaming. Abstract sockets are completely unbound.
431 */
432int uxst_pause_listener(struct listener *l)
433{
434 if (((struct sockaddr_un *)&l->addr)->sun_path[0])
435 return 1;
436
437 unbind_listener(l);
438 return 0;
439}
440
Willy Tarreau47f48c42014-05-09 22:57:47 +0200441
442/*
443 * This function initiates a UNIX connection establishment to the target assigned
444 * to connection <conn> using (si->{target,addr.to}). The source address is ignored
445 * and will be selected by the system. conn->target may point either to a valid
446 * server or to a backend, depending on conn->target. Only OBJ_TYPE_PROXY and
447 * OBJ_TYPE_SERVER are supported. The <data> parameter is a boolean indicating
448 * whether there are data waiting for being sent or not, in order to adjust data
449 * write polling and on some platforms. The <delack> argument is ignored.
450 *
451 * Note that a pending send_proxy message accounts for data.
452 *
453 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +0200454 * - SF_ERR_NONE if everything's OK
455 * - SF_ERR_SRVTO if there are no more servers
456 * - SF_ERR_SRVCL if the connection was refused by the server
457 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
458 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
459 * - SF_ERR_INTERNAL for any other purely internal errors
Tim Düsterhus4896c442016-11-29 02:15:19 +0100460 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau47f48c42014-05-09 22:57:47 +0200461 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200462 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
Willy Tarreau47f48c42014-05-09 22:57:47 +0200463 * it's invalid and the caller has nothing to do.
464 */
465int uxst_connect_server(struct connection *conn, int data, int delack)
466{
467 int fd;
468 struct server *srv;
469 struct proxy *be;
470
Willy Tarreau7bb21532014-05-10 09:48:28 +0200471 conn->flags = 0;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200472
473 switch (obj_type(conn->target)) {
474 case OBJ_TYPE_PROXY:
475 be = objt_proxy(conn->target);
476 srv = NULL;
477 break;
478 case OBJ_TYPE_SERVER:
479 srv = objt_server(conn->target);
480 be = srv->proxy;
481 break;
482 default:
483 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200484 return SF_ERR_INTERNAL;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200485 }
486
487 if ((fd = conn->t.sock.fd = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
488 qfprintf(stderr, "Cannot get a server socket.\n");
489
490 if (errno == ENFILE) {
491 conn->err_code = CO_ER_SYS_FDLIM;
492 send_log(be, LOG_EMERG,
493 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
494 be->id, maxfd);
495 }
496 else if (errno == EMFILE) {
497 conn->err_code = CO_ER_PROC_FDLIM;
498 send_log(be, LOG_EMERG,
499 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
500 be->id, maxfd);
501 }
502 else if (errno == ENOBUFS || errno == ENOMEM) {
503 conn->err_code = CO_ER_SYS_MEMLIM;
504 send_log(be, LOG_EMERG,
505 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
506 be->id, maxfd);
507 }
508 else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
509 conn->err_code = CO_ER_NOPROTO;
510 }
511 else
512 conn->err_code = CO_ER_SOCK_ERR;
513
514 /* this is a resource error */
515 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200516 return SF_ERR_RESOURCE;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200517 }
518
519 if (fd >= global.maxsock) {
520 /* do not log anything there, it's a normal condition when this option
521 * is used to serialize connections to a server !
522 */
523 Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
524 close(fd);
525 conn->err_code = CO_ER_CONF_FDLIM;
526 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200527 return SF_ERR_PRXCOND; /* it is a configuration limit */
Willy Tarreau47f48c42014-05-09 22:57:47 +0200528 }
529
530 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
531 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
532 close(fd);
533 conn->err_code = CO_ER_SOCK_ERR;
534 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200535 return SF_ERR_INTERNAL;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200536 }
537
538 /* if a send_proxy is there, there are data */
539 data |= conn->send_proxy_ofs;
540
541 if (global.tune.server_sndbuf)
542 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
543
544 if (global.tune.server_rcvbuf)
545 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
546
Willy Tarreau7bb21532014-05-10 09:48:28 +0200547 if (connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) == -1) {
Willy Tarreau94841792017-01-25 14:27:38 +0100548 if (errno == EINPROGRESS || errno == EALREADY) {
Willy Tarreau7bb21532014-05-10 09:48:28 +0200549 conn->flags |= CO_FL_WAIT_L4_CONN;
550 }
Willy Tarreau94841792017-01-25 14:27:38 +0100551 else if (errno == EISCONN) {
552 conn->flags &= ~CO_FL_WAIT_L4_CONN;
553 }
Willy Tarreau7bb21532014-05-10 09:48:28 +0200554 else if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau47f48c42014-05-09 22:57:47 +0200555 char *msg;
556 if (errno == EAGAIN || errno == EADDRNOTAVAIL) {
Lukas Tribus9f256d42016-01-26 20:33:14 +0100557 msg = "can't connect to destination unix socket, check backlog size on the server";
Willy Tarreau47f48c42014-05-09 22:57:47 +0200558 conn->err_code = CO_ER_FREE_PORTS;
559 }
560 else {
561 msg = "local address already in use";
562 conn->err_code = CO_ER_ADDR_INUSE;
563 }
564
565 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
566 close(fd);
567 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
568 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200569 return SF_ERR_RESOURCE;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200570 }
571 else if (errno == ETIMEDOUT) {
572 close(fd);
573 conn->err_code = CO_ER_SOCK_ERR;
574 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200575 return SF_ERR_SRVTO;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200576 }
577 else { // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
578 close(fd);
579 conn->err_code = CO_ER_SOCK_ERR;
580 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200581 return SF_ERR_SRVCL;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200582 }
583 }
Willy Tarreau7bb21532014-05-10 09:48:28 +0200584 else {
585 /* connect() already succeeded, which is quite usual for unix
Willy Tarreau94841792017-01-25 14:27:38 +0100586 * sockets. Let's avoid a second connect() probe to complete it.
Willy Tarreau7bb21532014-05-10 09:48:28 +0200587 */
588 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau7bb21532014-05-10 09:48:28 +0200589 }
Willy Tarreau47f48c42014-05-09 22:57:47 +0200590
591 conn->flags |= CO_FL_ADDR_TO_SET;
592
593 /* Prepare to send a few handshakes related to the on-wire protocol. */
594 if (conn->send_proxy_ofs)
595 conn->flags |= CO_FL_SEND_PROXY;
596
597 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreau7bb21532014-05-10 09:48:28 +0200598 fdtab[fd].linger_risk = 0; /* no need to disable lingering */
Willy Tarreau47f48c42014-05-09 22:57:47 +0200599
600 if (conn_xprt_init(conn) < 0) {
601 conn_force_close(conn);
602 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200603 return SF_ERR_RESOURCE;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200604 }
605
Willy Tarreau94841792017-01-25 14:27:38 +0100606 if (conn->flags & (CO_FL_HANDSHAKE | CO_FL_WAIT_L4_CONN)) {
607 conn_sock_want_send(conn); /* for connect status, proxy protocol or SSL */
608 }
609 else {
610 /* If there's no more handshake, we need to notify the data
611 * layer when the connection is already OK otherwise we'll have
612 * no other opportunity to do it later (eg: health checks).
613 */
614 data = 1;
615 }
616
Willy Tarreau47f48c42014-05-09 22:57:47 +0200617 if (data)
618 conn_data_want_send(conn); /* prepare to send data if any */
619
Willy Tarreaue7dff022015-04-03 01:14:29 +0200620 return SF_ERR_NONE; /* connection is OK */
Willy Tarreau47f48c42014-05-09 22:57:47 +0200621}
622
623
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100624/********************************
625 * 3) protocol-oriented functions
626 ********************************/
627
628
Willy Tarreau92fb9832007-10-16 17:34:28 +0200629/* This function creates all UNIX sockets bound to the protocol entry <proto>.
630 * It is intended to be used as the protocol's bind_all() function.
631 * The sockets will be registered but not added to any fd_set, in order not to
632 * loose them across the fork(). A call to uxst_enable_listeners() is needed
633 * to complete initialization.
634 *
635 * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL.
636 */
Emeric Bruncf20bf12010-10-22 16:06:11 +0200637static int uxst_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreau92fb9832007-10-16 17:34:28 +0200638{
639 struct listener *listener;
640 int err = ERR_NONE;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200641
642 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200643 err |= uxst_bind_listener(listener, errmsg, errlen);
644 if (err & ERR_ABORT)
645 break;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200646 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200647 return err;
648}
649
Willy Tarreau92fb9832007-10-16 17:34:28 +0200650
651/* This function stops all listening UNIX sockets bound to the protocol
652 * <proto>. It does not detaches them from the protocol.
653 * It always returns ERR_NONE.
654 */
655static int uxst_unbind_listeners(struct protocol *proto)
656{
657 struct listener *listener;
658
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100659 list_for_each_entry(listener, &proto->listeners, proto_list)
660 uxst_unbind_listener(listener);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200661 return ERR_NONE;
662}
663
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200664/* parse the "mode" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200665static 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 +0200666{
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200667 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200668 memprintf(err, "'%s' : missing mode (octal integer expected)", args[cur_arg]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200669 return ERR_ALERT | ERR_FATAL;
670 }
671
Willy Tarreau290e63a2012-09-20 18:07:14 +0200672 conf->ux.mode = strtol(args[cur_arg + 1], NULL, 8);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200673 return 0;
674}
675
676/* parse the "gid" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200677static 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 +0200678{
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200679 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200680 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200681 return ERR_ALERT | ERR_FATAL;
682 }
683
Willy Tarreau290e63a2012-09-20 18:07:14 +0200684 conf->ux.gid = atol(args[cur_arg + 1]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200685 return 0;
686}
687
688/* parse the "group" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200689static 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 +0200690{
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200691 struct group *group;
692
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200693 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200694 memprintf(err, "'%s' : missing group name", args[cur_arg]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200695 return ERR_ALERT | ERR_FATAL;
696 }
697
698 group = getgrnam(args[cur_arg + 1]);
699 if (!group) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200700 memprintf(err, "'%s' : unknown group name '%s'", args[cur_arg], args[cur_arg + 1]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200701 return ERR_ALERT | ERR_FATAL;
702 }
703
Willy Tarreau290e63a2012-09-20 18:07:14 +0200704 conf->ux.gid = group->gr_gid;
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200705 return 0;
706}
707
708/* parse the "uid" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200709static 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 +0200710{
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200711 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200712 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200713 return ERR_ALERT | ERR_FATAL;
714 }
715
Willy Tarreau290e63a2012-09-20 18:07:14 +0200716 conf->ux.uid = atol(args[cur_arg + 1]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200717 return 0;
718}
719
720/* parse the "user" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200721static 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 +0200722{
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200723 struct passwd *user;
724
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200725 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200726 memprintf(err, "'%s' : missing user name", args[cur_arg]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200727 return ERR_ALERT | ERR_FATAL;
728 }
729
730 user = getpwnam(args[cur_arg + 1]);
731 if (!user) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200732 memprintf(err, "'%s' : unknown user name '%s'", args[cur_arg], args[cur_arg + 1]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200733 return ERR_ALERT | ERR_FATAL;
734 }
735
Willy Tarreau290e63a2012-09-20 18:07:14 +0200736 conf->ux.uid = user->pw_uid;
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200737 return 0;
738}
739
740/* Note: must not be declared <const> as its list will be overwritten.
741 * Please take care of keeping this list alphabetically sorted, doing so helps
742 * all code contributors.
743 * Optional keywords are also declared with a NULL ->parse() function so that
744 * the config parser can report an appropriate error when a known keyword was
745 * not enabled.
746 */
Willy Tarreau51fb7652012-09-18 18:24:39 +0200747static struct bind_kw_list bind_kws = { "UNIX", { }, {
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200748 { "gid", bind_parse_gid, 1 }, /* set the socket's gid */
749 { "group", bind_parse_group, 1 }, /* set the socket's gid from the group name */
750 { "mode", bind_parse_mode, 1 }, /* set the socket's mode (eg: 0644)*/
751 { "uid", bind_parse_uid, 1 }, /* set the socket's uid */
752 { "user", bind_parse_user, 1 }, /* set the socket's uid from the user name */
753 { NULL, NULL, 0 },
754}};
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100755
756/********************************
757 * 4) high-level functions
758 ********************************/
759
Willy Tarreau92fb9832007-10-16 17:34:28 +0200760__attribute__((constructor))
761static void __uxst_protocol_init(void)
762{
763 protocol_register(&proto_unix);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200764 bind_register_keywords(&bind_kws);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200765}
766
767
768/*
769 * Local variables:
770 * c-indent-level: 8
771 * c-basic-offset: 8
772 * End:
773 */