blob: ae6e3388c3ac9b56c5f227115c1a0ffcde484a67 [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 Tarreau9d5be5c2017-09-15 07:55:51 +020052static void uxst_add_listener(struct listener *listener, int port);
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 Tarreaudabf2e22007-10-28 21:59:24 +010078/********************************
79 * 1) low-level socket functions
80 ********************************/
81
Willy Tarreau59b94792012-05-11 16:16:40 +020082/*
83 * Retrieves the source address for the socket <fd>, with <dir> indicating
84 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
85 * success, -1 in case of error. The socket's source address is stored in
86 * <sa> for <salen> bytes.
87 */
88int uxst_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
89{
90 if (dir)
91 return getsockname(fd, sa, &salen);
92 else
93 return getpeername(fd, sa, &salen);
94}
95
96
97/*
98 * Retrieves the original destination address for the socket <fd>, with <dir>
99 * indicating if we're a listener (=0) or an initiator (!=0). It returns 0 in
100 * case of success, -1 in case of error. The socket's source address is stored
101 * in <sa> for <salen> bytes.
102 */
103int uxst_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
104{
105 if (dir)
106 return getpeername(fd, sa, &salen);
107 else
108 return getsockname(fd, sa, &salen);
109}
110
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100111
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100112/********************************
113 * 2) listener-oriented functions
114 ********************************/
115
116
Olivier Houchardf886e342017-04-05 22:24:59 +0200117static int uxst_find_compatible_fd(struct listener *l)
118{
119 struct xfer_sock_list *xfer_sock = xfer_sock_list;
120 int ret = -1;
121
122 while (xfer_sock) {
123 struct sockaddr_un *un1 = (void *)&l->addr;
124 struct sockaddr_un *un2 = (void *)&xfer_sock->addr;
125
126 /*
127 * The bound socket's path as returned by getsockaddr
128 * will be the temporary name <sockname>.XXXXX.tmp,
129 * so we can't just compare the two names
130 */
131 if (xfer_sock->addr.ss_family == AF_UNIX &&
132 strncmp(un1->sun_path, un2->sun_path,
133 strlen(un1->sun_path)) == 0) {
134 char *after_sockname = un2->sun_path +
135 strlen(un1->sun_path);
136 /* Make a reasonnable effort to check that
137 * it is indeed a haproxy-generated temporary
138 * name, it's not perfect, but probably good enough.
139 */
140 if (after_sockname[0] == '.') {
141 after_sockname++;
142 while (after_sockname[0] >= '0' &&
143 after_sockname[0] <= '9')
144 after_sockname++;
145 if (!strcmp(after_sockname, ".tmp"))
146 break;
147 }
148 }
149 xfer_sock = xfer_sock->next;
150 }
151 if (xfer_sock != NULL) {
152 ret = xfer_sock->fd;
153 if (xfer_sock == xfer_sock_list)
154 xfer_sock_list = xfer_sock->next;
155 if (xfer_sock->prev)
156 xfer_sock->prev->next = xfer_sock->next;
157 if (xfer_sock->next)
158 xfer_sock->next->prev = xfer_sock->next->prev;
159 free(xfer_sock);
160 }
161 return ret;
162
163}
164
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100165/* This function creates a UNIX socket associated to the listener. It changes
166 * the state from ASSIGNED to LISTEN. The socket is NOT enabled for polling.
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100167 * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL. It
168 * may return a warning or an error message in <errmsg> if the message is at
169 * most <errlen> bytes long (including '\0'). Note that <errmsg> may be NULL if
170 * <errlen> is also zero.
Willy Tarreau92fb9832007-10-16 17:34:28 +0200171 */
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100172static int uxst_bind_listener(struct listener *listener, char *errmsg, int errlen)
Willy Tarreau92fb9832007-10-16 17:34:28 +0200173{
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100174 int fd;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200175 char tempname[MAXPATHLEN];
176 char backname[MAXPATHLEN];
177 struct sockaddr_un addr;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100178 const char *msg = NULL;
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100179 const char *path;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100180 int ext, ready;
181 socklen_t ready_len;
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200182 int err;
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100183 int ret;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200184
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200185 err = ERR_NONE;
186
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100187 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100188 if (errlen)
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100189 *errmsg = 0;
190
191 if (listener->state != LI_ASSIGNED)
192 return ERR_NONE; /* already bound */
193
Olivier Houchardf886e342017-04-05 22:24:59 +0200194 if (listener->fd == -1)
195 listener->fd = uxst_find_compatible_fd(listener);
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100196 path = ((struct sockaddr_un *)&listener->addr)->sun_path;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200197
Willy Tarreau40aa0702013-03-10 23:51:38 +0100198 /* if the listener already has an fd assigned, then we were offered the
199 * fd by an external process (most likely the parent), and we don't want
200 * to create a new socket. However we still want to set a few flags on
201 * the socket.
202 */
203 fd = listener->fd;
204 ext = (fd >= 0);
205 if (ext)
206 goto fd_ready;
207
Willy Tarreauccfccef2014-05-10 01:49:15 +0200208 if (path[0]) {
209 ret = snprintf(tempname, MAXPATHLEN, "%s.%d.tmp", path, pid);
210 if (ret < 0 || ret >= MAXPATHLEN) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200211 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200212 msg = "name too long for UNIX socket";
213 goto err_return;
214 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200215
Willy Tarreauccfccef2014-05-10 01:49:15 +0200216 ret = snprintf(backname, MAXPATHLEN, "%s.%d.bak", path, pid);
217 if (ret < 0 || ret >= MAXPATHLEN) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200218 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200219 msg = "name too long for UNIX socket";
220 goto err_return;
221 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200222
Willy Tarreauccfccef2014-05-10 01:49:15 +0200223 /* 2. clean existing orphaned entries */
224 if (unlink(tempname) < 0 && errno != ENOENT) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200225 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200226 msg = "error when trying to unlink previous UNIX socket";
227 goto err_return;
228 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200229
Willy Tarreauccfccef2014-05-10 01:49:15 +0200230 if (unlink(backname) < 0 && errno != ENOENT) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200231 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200232 msg = "error when trying to unlink previous UNIX socket";
233 goto err_return;
234 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200235
Willy Tarreauccfccef2014-05-10 01:49:15 +0200236 /* 3. backup existing socket */
237 if (link(path, backname) < 0 && errno != ENOENT) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200238 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200239 msg = "error when trying to preserve previous UNIX socket";
240 goto err_return;
241 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200242
Willy Tarreauccfccef2014-05-10 01:49:15 +0200243 strncpy(addr.sun_path, tempname, sizeof(addr.sun_path));
244 addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200245 }
Willy Tarreauccfccef2014-05-10 01:49:15 +0200246 else {
247 /* first char is zero, it's an abstract socket whose address
248 * is defined by all the bytes past this zero.
249 */
250 memcpy(addr.sun_path, path, sizeof(addr.sun_path));
251 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200252 addr.sun_family = AF_UNIX;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200253
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100254 fd = socket(PF_UNIX, SOCK_STREAM, 0);
255 if (fd < 0) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200256 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100257 msg = "cannot create UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200258 goto err_unlink_back;
259 }
260
Willy Tarreau40aa0702013-03-10 23:51:38 +0100261 fd_ready:
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100262 if (fd >= global.maxsock) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200263 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100264 msg = "socket(): not enough free sockets, raise -n argument";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200265 goto err_unlink_temp;
266 }
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100267
268 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200269 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100270 msg = "cannot make UNIX socket non-blocking";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200271 goto err_unlink_temp;
272 }
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100273
Willy Tarreau40aa0702013-03-10 23:51:38 +0100274 if (!ext && bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
Willy Tarreau92fb9832007-10-16 17:34:28 +0200275 /* note that bind() creates the socket <tempname> on the file system */
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200276 if (errno == EADDRINUSE) {
277 /* the old process might still own it, let's retry */
278 err |= ERR_RETRYABLE | ERR_ALERT;
279 msg = "cannot listen to socket";
280 }
281 else {
282 err |= ERR_FATAL | ERR_ALERT;
283 msg = "cannot bind UNIX socket";
284 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200285 goto err_unlink_temp;
286 }
287
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100288 /* <uid> and <gid> different of -1 will be used to change the socket owner.
289 * If <mode> is not 0, it will be used to restrict access to the socket.
290 * While it is known not to be portable on every OS, it's still useful
Willy Tarreauccfccef2014-05-10 01:49:15 +0200291 * where it works. We also don't change permissions on abstract sockets.
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100292 */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200293 if (!ext && path[0] &&
Willy Tarreau40aa0702013-03-10 23:51:38 +0100294 (((listener->bind_conf->ux.uid != -1 || listener->bind_conf->ux.gid != -1) &&
295 (chown(tempname, listener->bind_conf->ux.uid, listener->bind_conf->ux.gid) == -1)) ||
296 (listener->bind_conf->ux.mode != 0 && chmod(tempname, listener->bind_conf->ux.mode) == -1))) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200297 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100298 msg = "cannot change UNIX socket ownership";
Willy Tarreaue6ad2b12007-10-18 12:45:54 +0200299 goto err_unlink_temp;
300 }
301
Willy Tarreau40aa0702013-03-10 23:51:38 +0100302 ready = 0;
303 ready_len = sizeof(ready);
304 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1)
305 ready = 0;
306
307 if (!(ext && ready) && /* only listen if not already done by external process */
308 listen(fd, listener->backlog ? listener->backlog : listener->maxconn) < 0) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200309 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100310 msg = "cannot listen to UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200311 goto err_unlink_temp;
312 }
313
Willy Tarreauccfccef2014-05-10 01:49:15 +0200314 /* Point of no return: we are ready, we'll switch the sockets. We don't
Willy Tarreau92fb9832007-10-16 17:34:28 +0200315 * fear loosing the socket <path> because we have a copy of it in
Willy Tarreauccfccef2014-05-10 01:49:15 +0200316 * backname. Abstract sockets are not renamed.
Willy Tarreau92fb9832007-10-16 17:34:28 +0200317 */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200318 if (!ext && path[0] && rename(tempname, path) < 0) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200319 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100320 msg = "cannot switch final and temporary UNIX sockets";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200321 goto err_rename;
322 }
323
Willy Tarreau68986ab2017-06-16 10:34:20 +0200324 /* Cleanup: only unlink if we didn't inherit the fd from the parent */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200325 if (!ext && path[0])
Willy Tarreau40aa0702013-03-10 23:51:38 +0100326 unlink(backname);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200327
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100328 /* the socket is now listening */
329 listener->fd = fd;
330 listener->state = LI_LISTEN;
331
332 /* the function for the accept() event */
333 fd_insert(fd);
Willy Tarreauaece46a2012-07-06 12:25:58 +0200334 fdtab[fd].iocb = listener->proto->accept;
Willy Tarreaueabf3132008-08-29 23:36:51 +0200335 fdtab[fd].owner = listener; /* reference the listener instead of a task */
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200336 return err;
337
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100338 err_rename:
339 ret = rename(backname, path);
340 if (ret < 0 && errno == ENOENT)
341 unlink(path);
342 err_unlink_temp:
Jan Seda7319b642014-06-26 20:44:05 +0200343 if (!ext && path[0])
Willy Tarreau40aa0702013-03-10 23:51:38 +0100344 unlink(tempname);
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100345 close(fd);
346 err_unlink_back:
Jan Seda7319b642014-06-26 20:44:05 +0200347 if (!ext && path[0])
Willy Tarreau40aa0702013-03-10 23:51:38 +0100348 unlink(backname);
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100349 err_return:
Willy Tarreau40aa0702013-03-10 23:51:38 +0100350 if (msg && errlen) {
351 if (!ext)
352 snprintf(errmsg, errlen, "%s [%s]", msg, path);
353 else
354 snprintf(errmsg, errlen, "%s [fd %d]", msg, fd);
355 }
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200356 return err;
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100357}
358
359/* This function closes the UNIX sockets for the specified listener.
360 * The listener enters the LI_ASSIGNED state. It always returns ERR_NONE.
361 */
362static int uxst_unbind_listener(struct listener *listener)
363{
Willy Tarreaube58c382011-07-24 18:28:10 +0200364 if (listener->state > LI_ASSIGNED) {
365 unbind_listener(listener);
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100366 }
367 return ERR_NONE;
368}
369
Willy Tarreau32282382017-09-15 07:44:44 +0200370/* Add <listener> to the list of unix stream listeners (port is ignored). The
371 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
372 * The number of listeners for the protocol is updated.
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100373 */
Willy Tarreau9d5be5c2017-09-15 07:55:51 +0200374static void uxst_add_listener(struct listener *listener, int port)
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100375{
376 if (listener->state != LI_INIT)
377 return;
378 listener->state = LI_ASSIGNED;
379 listener->proto = &proto_unix;
380 LIST_ADDQ(&proto_unix.listeners, &listener->proto_list);
381 proto_unix.nb_listeners++;
382}
383
Willy Tarreaufd0e0082014-07-07 21:07:51 +0200384/* Pause a listener. Returns < 0 in case of failure, 0 if the listener
385 * was totally stopped, or > 0 if correctly paused. Nothing is done for
386 * plain unix sockets since currently it's the new process which handles
387 * the renaming. Abstract sockets are completely unbound.
388 */
389int uxst_pause_listener(struct listener *l)
390{
391 if (((struct sockaddr_un *)&l->addr)->sun_path[0])
392 return 1;
393
394 unbind_listener(l);
395 return 0;
396}
397
Willy Tarreau47f48c42014-05-09 22:57:47 +0200398
399/*
400 * This function initiates a UNIX connection establishment to the target assigned
401 * to connection <conn> using (si->{target,addr.to}). The source address is ignored
402 * and will be selected by the system. conn->target may point either to a valid
403 * server or to a backend, depending on conn->target. Only OBJ_TYPE_PROXY and
404 * OBJ_TYPE_SERVER are supported. The <data> parameter is a boolean indicating
405 * whether there are data waiting for being sent or not, in order to adjust data
406 * write polling and on some platforms. The <delack> argument is ignored.
407 *
408 * Note that a pending send_proxy message accounts for data.
409 *
410 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +0200411 * - SF_ERR_NONE if everything's OK
412 * - SF_ERR_SRVTO if there are no more servers
413 * - SF_ERR_SRVCL if the connection was refused by the server
414 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
415 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
416 * - SF_ERR_INTERNAL for any other purely internal errors
Tim Düsterhus4896c442016-11-29 02:15:19 +0100417 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau47f48c42014-05-09 22:57:47 +0200418 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200419 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
Willy Tarreau47f48c42014-05-09 22:57:47 +0200420 * it's invalid and the caller has nothing to do.
421 */
422int uxst_connect_server(struct connection *conn, int data, int delack)
423{
424 int fd;
425 struct server *srv;
426 struct proxy *be;
427
Willy Tarreau7bb21532014-05-10 09:48:28 +0200428 conn->flags = 0;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200429
430 switch (obj_type(conn->target)) {
431 case OBJ_TYPE_PROXY:
432 be = objt_proxy(conn->target);
433 srv = NULL;
434 break;
435 case OBJ_TYPE_SERVER:
436 srv = objt_server(conn->target);
437 be = srv->proxy;
438 break;
439 default:
440 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200441 return SF_ERR_INTERNAL;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200442 }
443
Willy Tarreau585744b2017-08-24 14:31:19 +0200444 if ((fd = conn->handle.fd = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
Willy Tarreau47f48c42014-05-09 22:57:47 +0200445 qfprintf(stderr, "Cannot get a server socket.\n");
446
447 if (errno == ENFILE) {
448 conn->err_code = CO_ER_SYS_FDLIM;
449 send_log(be, LOG_EMERG,
450 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
451 be->id, maxfd);
452 }
453 else if (errno == EMFILE) {
454 conn->err_code = CO_ER_PROC_FDLIM;
455 send_log(be, LOG_EMERG,
456 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
457 be->id, maxfd);
458 }
459 else if (errno == ENOBUFS || errno == ENOMEM) {
460 conn->err_code = CO_ER_SYS_MEMLIM;
461 send_log(be, LOG_EMERG,
462 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
463 be->id, maxfd);
464 }
465 else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
466 conn->err_code = CO_ER_NOPROTO;
467 }
468 else
469 conn->err_code = CO_ER_SOCK_ERR;
470
471 /* this is a resource error */
472 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200473 return SF_ERR_RESOURCE;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200474 }
475
476 if (fd >= global.maxsock) {
477 /* do not log anything there, it's a normal condition when this option
478 * is used to serialize connections to a server !
479 */
480 Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
481 close(fd);
482 conn->err_code = CO_ER_CONF_FDLIM;
483 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200484 return SF_ERR_PRXCOND; /* it is a configuration limit */
Willy Tarreau47f48c42014-05-09 22:57:47 +0200485 }
486
487 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
488 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
489 close(fd);
490 conn->err_code = CO_ER_SOCK_ERR;
491 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200492 return SF_ERR_INTERNAL;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200493 }
494
495 /* if a send_proxy is there, there are data */
496 data |= conn->send_proxy_ofs;
497
498 if (global.tune.server_sndbuf)
499 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
500
501 if (global.tune.server_rcvbuf)
502 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
503
Willy Tarreau7bb21532014-05-10 09:48:28 +0200504 if (connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) == -1) {
Willy Tarreau94841792017-01-25 14:27:38 +0100505 if (errno == EINPROGRESS || errno == EALREADY) {
Willy Tarreau7bb21532014-05-10 09:48:28 +0200506 conn->flags |= CO_FL_WAIT_L4_CONN;
507 }
Willy Tarreau94841792017-01-25 14:27:38 +0100508 else if (errno == EISCONN) {
509 conn->flags &= ~CO_FL_WAIT_L4_CONN;
510 }
Willy Tarreau7bb21532014-05-10 09:48:28 +0200511 else if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau47f48c42014-05-09 22:57:47 +0200512 char *msg;
513 if (errno == EAGAIN || errno == EADDRNOTAVAIL) {
Lukas Tribus9f256d42016-01-26 20:33:14 +0100514 msg = "can't connect to destination unix socket, check backlog size on the server";
Willy Tarreau47f48c42014-05-09 22:57:47 +0200515 conn->err_code = CO_ER_FREE_PORTS;
516 }
517 else {
518 msg = "local address already in use";
519 conn->err_code = CO_ER_ADDR_INUSE;
520 }
521
522 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
523 close(fd);
524 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
525 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200526 return SF_ERR_RESOURCE;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200527 }
528 else if (errno == ETIMEDOUT) {
529 close(fd);
530 conn->err_code = CO_ER_SOCK_ERR;
531 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200532 return SF_ERR_SRVTO;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200533 }
534 else { // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
535 close(fd);
536 conn->err_code = CO_ER_SOCK_ERR;
537 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200538 return SF_ERR_SRVCL;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200539 }
540 }
Willy Tarreau7bb21532014-05-10 09:48:28 +0200541 else {
542 /* connect() already succeeded, which is quite usual for unix
Willy Tarreau94841792017-01-25 14:27:38 +0100543 * sockets. Let's avoid a second connect() probe to complete it.
Willy Tarreau7bb21532014-05-10 09:48:28 +0200544 */
545 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau7bb21532014-05-10 09:48:28 +0200546 }
Willy Tarreau47f48c42014-05-09 22:57:47 +0200547
548 conn->flags |= CO_FL_ADDR_TO_SET;
549
550 /* Prepare to send a few handshakes related to the on-wire protocol. */
551 if (conn->send_proxy_ofs)
552 conn->flags |= CO_FL_SEND_PROXY;
553
554 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreau7bb21532014-05-10 09:48:28 +0200555 fdtab[fd].linger_risk = 0; /* no need to disable lingering */
Willy Tarreau47f48c42014-05-09 22:57:47 +0200556
557 if (conn_xprt_init(conn) < 0) {
558 conn_force_close(conn);
559 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200560 return SF_ERR_RESOURCE;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200561 }
562
Willy Tarreau94841792017-01-25 14:27:38 +0100563 if (conn->flags & (CO_FL_HANDSHAKE | CO_FL_WAIT_L4_CONN)) {
564 conn_sock_want_send(conn); /* for connect status, proxy protocol or SSL */
565 }
566 else {
567 /* If there's no more handshake, we need to notify the data
568 * layer when the connection is already OK otherwise we'll have
569 * no other opportunity to do it later (eg: health checks).
570 */
571 data = 1;
572 }
573
Willy Tarreau47f48c42014-05-09 22:57:47 +0200574 if (data)
575 conn_data_want_send(conn); /* prepare to send data if any */
576
Willy Tarreaue7dff022015-04-03 01:14:29 +0200577 return SF_ERR_NONE; /* connection is OK */
Willy Tarreau47f48c42014-05-09 22:57:47 +0200578}
579
580
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100581/********************************
582 * 3) protocol-oriented functions
583 ********************************/
584
585
Willy Tarreau92fb9832007-10-16 17:34:28 +0200586/* This function creates all UNIX sockets bound to the protocol entry <proto>.
587 * It is intended to be used as the protocol's bind_all() function.
588 * The sockets will be registered but not added to any fd_set, in order not to
589 * loose them across the fork(). A call to uxst_enable_listeners() is needed
590 * to complete initialization.
591 *
592 * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL.
593 */
Emeric Bruncf20bf12010-10-22 16:06:11 +0200594static int uxst_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreau92fb9832007-10-16 17:34:28 +0200595{
596 struct listener *listener;
597 int err = ERR_NONE;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200598
599 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200600 err |= uxst_bind_listener(listener, errmsg, errlen);
601 if (err & ERR_ABORT)
602 break;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200603 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200604 return err;
605}
606
Willy Tarreau92fb9832007-10-16 17:34:28 +0200607
608/* This function stops all listening UNIX sockets bound to the protocol
609 * <proto>. It does not detaches them from the protocol.
610 * It always returns ERR_NONE.
611 */
612static int uxst_unbind_listeners(struct protocol *proto)
613{
614 struct listener *listener;
615
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100616 list_for_each_entry(listener, &proto->listeners, proto_list)
617 uxst_unbind_listener(listener);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200618 return ERR_NONE;
619}
620
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200621/* parse the "mode" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200622static 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 +0200623{
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200624 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200625 memprintf(err, "'%s' : missing mode (octal integer expected)", args[cur_arg]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200626 return ERR_ALERT | ERR_FATAL;
627 }
628
Willy Tarreau290e63a2012-09-20 18:07:14 +0200629 conf->ux.mode = strtol(args[cur_arg + 1], NULL, 8);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200630 return 0;
631}
632
633/* parse the "gid" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200634static 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 +0200635{
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200636 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200637 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200638 return ERR_ALERT | ERR_FATAL;
639 }
640
Willy Tarreau290e63a2012-09-20 18:07:14 +0200641 conf->ux.gid = atol(args[cur_arg + 1]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200642 return 0;
643}
644
645/* parse the "group" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200646static 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 +0200647{
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200648 struct group *group;
649
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200650 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200651 memprintf(err, "'%s' : missing group name", args[cur_arg]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200652 return ERR_ALERT | ERR_FATAL;
653 }
654
655 group = getgrnam(args[cur_arg + 1]);
656 if (!group) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200657 memprintf(err, "'%s' : unknown group name '%s'", args[cur_arg], args[cur_arg + 1]);
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 = group->gr_gid;
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200662 return 0;
663}
664
665/* parse the "uid" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200666static 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 +0200667{
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200668 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200669 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200670 return ERR_ALERT | ERR_FATAL;
671 }
672
Willy Tarreau290e63a2012-09-20 18:07:14 +0200673 conf->ux.uid = atol(args[cur_arg + 1]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200674 return 0;
675}
676
677/* parse the "user" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200678static 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 +0200679{
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200680 struct passwd *user;
681
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200682 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200683 memprintf(err, "'%s' : missing user name", args[cur_arg]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200684 return ERR_ALERT | ERR_FATAL;
685 }
686
687 user = getpwnam(args[cur_arg + 1]);
688 if (!user) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200689 memprintf(err, "'%s' : unknown user name '%s'", args[cur_arg], args[cur_arg + 1]);
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 = user->pw_uid;
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200694 return 0;
695}
696
697/* Note: must not be declared <const> as its list will be overwritten.
698 * Please take care of keeping this list alphabetically sorted, doing so helps
699 * all code contributors.
700 * Optional keywords are also declared with a NULL ->parse() function so that
701 * the config parser can report an appropriate error when a known keyword was
702 * not enabled.
703 */
Willy Tarreau51fb7652012-09-18 18:24:39 +0200704static struct bind_kw_list bind_kws = { "UNIX", { }, {
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200705 { "gid", bind_parse_gid, 1 }, /* set the socket's gid */
706 { "group", bind_parse_group, 1 }, /* set the socket's gid from the group name */
707 { "mode", bind_parse_mode, 1 }, /* set the socket's mode (eg: 0644)*/
708 { "uid", bind_parse_uid, 1 }, /* set the socket's uid */
709 { "user", bind_parse_user, 1 }, /* set the socket's uid from the user name */
710 { NULL, NULL, 0 },
711}};
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100712
713/********************************
714 * 4) high-level functions
715 ********************************/
716
Willy Tarreau92fb9832007-10-16 17:34:28 +0200717__attribute__((constructor))
718static void __uxst_protocol_init(void)
719{
720 protocol_register(&proto_unix);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200721 bind_register_keywords(&bind_kws);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200722}
723
724
725/*
726 * Local variables:
727 * c-indent-level: 8
728 * c-basic-offset: 8
729 * End:
730 */