blob: 27ff0fa407866b4c61398715d5c41e3f4ec3be6e [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
153/* This function creates a UNIX socket associated to the listener. It changes
154 * the state from ASSIGNED to LISTEN. The socket is NOT enabled for polling.
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100155 * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL. It
156 * may return a warning or an error message in <errmsg> if the message is at
157 * most <errlen> bytes long (including '\0'). Note that <errmsg> may be NULL if
158 * <errlen> is also zero.
Willy Tarreau92fb9832007-10-16 17:34:28 +0200159 */
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100160static int uxst_bind_listener(struct listener *listener, char *errmsg, int errlen)
Willy Tarreau92fb9832007-10-16 17:34:28 +0200161{
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100162 int fd;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200163 char tempname[MAXPATHLEN];
164 char backname[MAXPATHLEN];
165 struct sockaddr_un addr;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100166 const char *msg = NULL;
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100167 const char *path;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100168 int ext, ready;
169 socklen_t ready_len;
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200170 int err;
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100171 int ret;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200172
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200173 err = ERR_NONE;
174
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100175 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100176 if (errlen)
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100177 *errmsg = 0;
178
179 if (listener->state != LI_ASSIGNED)
180 return ERR_NONE; /* already bound */
181
182 path = ((struct sockaddr_un *)&listener->addr)->sun_path;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200183
Willy Tarreau40aa0702013-03-10 23:51:38 +0100184 /* if the listener already has an fd assigned, then we were offered the
185 * fd by an external process (most likely the parent), and we don't want
186 * to create a new socket. However we still want to set a few flags on
187 * the socket.
188 */
189 fd = listener->fd;
190 ext = (fd >= 0);
191 if (ext)
192 goto fd_ready;
193
Willy Tarreauccfccef2014-05-10 01:49:15 +0200194 if (path[0]) {
195 ret = snprintf(tempname, MAXPATHLEN, "%s.%d.tmp", path, pid);
196 if (ret < 0 || ret >= MAXPATHLEN) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200197 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200198 msg = "name too long for UNIX socket";
199 goto err_return;
200 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200201
Willy Tarreauccfccef2014-05-10 01:49:15 +0200202 ret = snprintf(backname, MAXPATHLEN, "%s.%d.bak", path, pid);
203 if (ret < 0 || ret >= MAXPATHLEN) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200204 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200205 msg = "name too long for UNIX socket";
206 goto err_return;
207 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200208
Willy Tarreauccfccef2014-05-10 01:49:15 +0200209 /* 2. clean existing orphaned entries */
210 if (unlink(tempname) < 0 && errno != ENOENT) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200211 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200212 msg = "error when trying to unlink previous UNIX socket";
213 goto err_return;
214 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200215
Willy Tarreauccfccef2014-05-10 01:49:15 +0200216 if (unlink(backname) < 0 && errno != ENOENT) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200217 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200218 msg = "error when trying to unlink previous UNIX socket";
219 goto err_return;
220 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200221
Willy Tarreauccfccef2014-05-10 01:49:15 +0200222 /* 3. backup existing socket */
223 if (link(path, backname) < 0 && errno != ENOENT) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200224 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200225 msg = "error when trying to preserve previous UNIX socket";
226 goto err_return;
227 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200228
Willy Tarreauccfccef2014-05-10 01:49:15 +0200229 strncpy(addr.sun_path, tempname, sizeof(addr.sun_path));
230 addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200231 }
Willy Tarreauccfccef2014-05-10 01:49:15 +0200232 else {
233 /* first char is zero, it's an abstract socket whose address
234 * is defined by all the bytes past this zero.
235 */
236 memcpy(addr.sun_path, path, sizeof(addr.sun_path));
237 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200238 addr.sun_family = AF_UNIX;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200239
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100240 fd = socket(PF_UNIX, SOCK_STREAM, 0);
241 if (fd < 0) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200242 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100243 msg = "cannot create UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200244 goto err_unlink_back;
245 }
246
Willy Tarreau40aa0702013-03-10 23:51:38 +0100247 fd_ready:
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100248 if (fd >= global.maxsock) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200249 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100250 msg = "socket(): not enough free sockets, raise -n argument";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200251 goto err_unlink_temp;
252 }
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100253
254 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200255 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100256 msg = "cannot make UNIX socket non-blocking";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200257 goto err_unlink_temp;
258 }
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100259
Willy Tarreau40aa0702013-03-10 23:51:38 +0100260 if (!ext && bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
Willy Tarreau92fb9832007-10-16 17:34:28 +0200261 /* note that bind() creates the socket <tempname> on the file system */
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200262 if (errno == EADDRINUSE) {
263 /* the old process might still own it, let's retry */
264 err |= ERR_RETRYABLE | ERR_ALERT;
265 msg = "cannot listen to socket";
266 }
267 else {
268 err |= ERR_FATAL | ERR_ALERT;
269 msg = "cannot bind UNIX socket";
270 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200271 goto err_unlink_temp;
272 }
273
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100274 /* <uid> and <gid> different of -1 will be used to change the socket owner.
275 * If <mode> is not 0, it will be used to restrict access to the socket.
276 * While it is known not to be portable on every OS, it's still useful
Willy Tarreauccfccef2014-05-10 01:49:15 +0200277 * where it works. We also don't change permissions on abstract sockets.
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100278 */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200279 if (!ext && path[0] &&
Willy Tarreau40aa0702013-03-10 23:51:38 +0100280 (((listener->bind_conf->ux.uid != -1 || listener->bind_conf->ux.gid != -1) &&
281 (chown(tempname, listener->bind_conf->ux.uid, listener->bind_conf->ux.gid) == -1)) ||
282 (listener->bind_conf->ux.mode != 0 && chmod(tempname, listener->bind_conf->ux.mode) == -1))) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200283 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100284 msg = "cannot change UNIX socket ownership";
Willy Tarreaue6ad2b12007-10-18 12:45:54 +0200285 goto err_unlink_temp;
286 }
287
Willy Tarreau40aa0702013-03-10 23:51:38 +0100288 ready = 0;
289 ready_len = sizeof(ready);
290 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1)
291 ready = 0;
292
293 if (!(ext && ready) && /* only listen if not already done by external process */
294 listen(fd, listener->backlog ? listener->backlog : listener->maxconn) < 0) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200295 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100296 msg = "cannot listen to UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200297 goto err_unlink_temp;
298 }
299
Willy Tarreauccfccef2014-05-10 01:49:15 +0200300 /* Point of no return: we are ready, we'll switch the sockets. We don't
Willy Tarreau92fb9832007-10-16 17:34:28 +0200301 * fear loosing the socket <path> because we have a copy of it in
Willy Tarreauccfccef2014-05-10 01:49:15 +0200302 * backname. Abstract sockets are not renamed.
Willy Tarreau92fb9832007-10-16 17:34:28 +0200303 */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200304 if (!ext && path[0] && rename(tempname, path) < 0) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200305 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100306 msg = "cannot switch final and temporary UNIX sockets";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200307 goto err_rename;
308 }
309
Willy Tarreauccfccef2014-05-10 01:49:15 +0200310 /* Cleanup: If we're bound to an fd inherited from the parent, we
Willy Tarreau40aa0702013-03-10 23:51:38 +0100311 * want to ensure that destroy_uxst_socket() will never remove the
Willy Tarreauccfccef2014-05-10 01:49:15 +0200312 * path, and for this we simply clear the path to the socket, which
313 * under Linux corresponds to an abstract socket.
Willy Tarreau40aa0702013-03-10 23:51:38 +0100314 */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200315 if (!ext && path[0])
Willy Tarreau40aa0702013-03-10 23:51:38 +0100316 unlink(backname);
317 else
318 ((struct sockaddr_un *)&listener->addr)->sun_path[0] = 0;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200319
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100320 /* the socket is now listening */
321 listener->fd = fd;
322 listener->state = LI_LISTEN;
323
324 /* the function for the accept() event */
325 fd_insert(fd);
Willy Tarreauaece46a2012-07-06 12:25:58 +0200326 fdtab[fd].iocb = listener->proto->accept;
Willy Tarreaueabf3132008-08-29 23:36:51 +0200327 fdtab[fd].owner = listener; /* reference the listener instead of a task */
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200328 return err;
329
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100330 err_rename:
331 ret = rename(backname, path);
332 if (ret < 0 && errno == ENOENT)
333 unlink(path);
334 err_unlink_temp:
Jan Seda7319b642014-06-26 20:44:05 +0200335 if (!ext && path[0])
Willy Tarreau40aa0702013-03-10 23:51:38 +0100336 unlink(tempname);
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100337 close(fd);
338 err_unlink_back:
Jan Seda7319b642014-06-26 20:44:05 +0200339 if (!ext && path[0])
Willy Tarreau40aa0702013-03-10 23:51:38 +0100340 unlink(backname);
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100341 err_return:
Willy Tarreau40aa0702013-03-10 23:51:38 +0100342 if (msg && errlen) {
343 if (!ext)
344 snprintf(errmsg, errlen, "%s [%s]", msg, path);
345 else
346 snprintf(errmsg, errlen, "%s [fd %d]", msg, fd);
347 }
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200348 return err;
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100349}
350
351/* This function closes the UNIX sockets for the specified listener.
352 * The listener enters the LI_ASSIGNED state. It always returns ERR_NONE.
353 */
354static int uxst_unbind_listener(struct listener *listener)
355{
Willy Tarreaube58c382011-07-24 18:28:10 +0200356 if (listener->state > LI_ASSIGNED) {
357 unbind_listener(listener);
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100358 destroy_uxst_socket(((struct sockaddr_un *)&listener->addr)->sun_path);
359 }
360 return ERR_NONE;
361}
362
363/* Add a listener to the list of unix stream listeners. The listener's state
364 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
365 * listeners is updated. This is the function to use to add a new listener.
366 */
367void uxst_add_listener(struct listener *listener)
368{
369 if (listener->state != LI_INIT)
370 return;
371 listener->state = LI_ASSIGNED;
372 listener->proto = &proto_unix;
373 LIST_ADDQ(&proto_unix.listeners, &listener->proto_list);
374 proto_unix.nb_listeners++;
375}
376
Willy Tarreaufd0e0082014-07-07 21:07:51 +0200377/* Pause a listener. Returns < 0 in case of failure, 0 if the listener
378 * was totally stopped, or > 0 if correctly paused. Nothing is done for
379 * plain unix sockets since currently it's the new process which handles
380 * the renaming. Abstract sockets are completely unbound.
381 */
382int uxst_pause_listener(struct listener *l)
383{
384 if (((struct sockaddr_un *)&l->addr)->sun_path[0])
385 return 1;
386
387 unbind_listener(l);
388 return 0;
389}
390
Willy Tarreau47f48c42014-05-09 22:57:47 +0200391
392/*
393 * This function initiates a UNIX connection establishment to the target assigned
394 * to connection <conn> using (si->{target,addr.to}). The source address is ignored
395 * and will be selected by the system. conn->target may point either to a valid
396 * server or to a backend, depending on conn->target. Only OBJ_TYPE_PROXY and
397 * OBJ_TYPE_SERVER are supported. The <data> parameter is a boolean indicating
398 * whether there are data waiting for being sent or not, in order to adjust data
399 * write polling and on some platforms. The <delack> argument is ignored.
400 *
401 * Note that a pending send_proxy message accounts for data.
402 *
403 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +0200404 * - SF_ERR_NONE if everything's OK
405 * - SF_ERR_SRVTO if there are no more servers
406 * - SF_ERR_SRVCL if the connection was refused by the server
407 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
408 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
409 * - SF_ERR_INTERNAL for any other purely internal errors
Tim Düsterhus4896c442016-11-29 02:15:19 +0100410 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau47f48c42014-05-09 22:57:47 +0200411 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200412 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
Willy Tarreau47f48c42014-05-09 22:57:47 +0200413 * it's invalid and the caller has nothing to do.
414 */
415int uxst_connect_server(struct connection *conn, int data, int delack)
416{
417 int fd;
418 struct server *srv;
419 struct proxy *be;
420
Willy Tarreau7bb21532014-05-10 09:48:28 +0200421 conn->flags = 0;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200422
423 switch (obj_type(conn->target)) {
424 case OBJ_TYPE_PROXY:
425 be = objt_proxy(conn->target);
426 srv = NULL;
427 break;
428 case OBJ_TYPE_SERVER:
429 srv = objt_server(conn->target);
430 be = srv->proxy;
431 break;
432 default:
433 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200434 return SF_ERR_INTERNAL;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200435 }
436
437 if ((fd = conn->t.sock.fd = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
438 qfprintf(stderr, "Cannot get a server socket.\n");
439
440 if (errno == ENFILE) {
441 conn->err_code = CO_ER_SYS_FDLIM;
442 send_log(be, LOG_EMERG,
443 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
444 be->id, maxfd);
445 }
446 else if (errno == EMFILE) {
447 conn->err_code = CO_ER_PROC_FDLIM;
448 send_log(be, LOG_EMERG,
449 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
450 be->id, maxfd);
451 }
452 else if (errno == ENOBUFS || errno == ENOMEM) {
453 conn->err_code = CO_ER_SYS_MEMLIM;
454 send_log(be, LOG_EMERG,
455 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
456 be->id, maxfd);
457 }
458 else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
459 conn->err_code = CO_ER_NOPROTO;
460 }
461 else
462 conn->err_code = CO_ER_SOCK_ERR;
463
464 /* this is a resource error */
465 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200466 return SF_ERR_RESOURCE;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200467 }
468
469 if (fd >= global.maxsock) {
470 /* do not log anything there, it's a normal condition when this option
471 * is used to serialize connections to a server !
472 */
473 Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
474 close(fd);
475 conn->err_code = CO_ER_CONF_FDLIM;
476 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200477 return SF_ERR_PRXCOND; /* it is a configuration limit */
Willy Tarreau47f48c42014-05-09 22:57:47 +0200478 }
479
480 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
481 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
482 close(fd);
483 conn->err_code = CO_ER_SOCK_ERR;
484 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200485 return SF_ERR_INTERNAL;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200486 }
487
488 /* if a send_proxy is there, there are data */
489 data |= conn->send_proxy_ofs;
490
491 if (global.tune.server_sndbuf)
492 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
493
494 if (global.tune.server_rcvbuf)
495 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
496
Willy Tarreau7bb21532014-05-10 09:48:28 +0200497 if (connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) == -1) {
Willy Tarreau94841792017-01-25 14:27:38 +0100498 if (errno == EINPROGRESS || errno == EALREADY) {
Willy Tarreau7bb21532014-05-10 09:48:28 +0200499 conn->flags |= CO_FL_WAIT_L4_CONN;
500 }
Willy Tarreau94841792017-01-25 14:27:38 +0100501 else if (errno == EISCONN) {
502 conn->flags &= ~CO_FL_WAIT_L4_CONN;
503 }
Willy Tarreau7bb21532014-05-10 09:48:28 +0200504 else if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau47f48c42014-05-09 22:57:47 +0200505 char *msg;
506 if (errno == EAGAIN || errno == EADDRNOTAVAIL) {
Lukas Tribus9f256d42016-01-26 20:33:14 +0100507 msg = "can't connect to destination unix socket, check backlog size on the server";
Willy Tarreau47f48c42014-05-09 22:57:47 +0200508 conn->err_code = CO_ER_FREE_PORTS;
509 }
510 else {
511 msg = "local address already in use";
512 conn->err_code = CO_ER_ADDR_INUSE;
513 }
514
515 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
516 close(fd);
517 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
518 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200519 return SF_ERR_RESOURCE;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200520 }
521 else if (errno == ETIMEDOUT) {
522 close(fd);
523 conn->err_code = CO_ER_SOCK_ERR;
524 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200525 return SF_ERR_SRVTO;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200526 }
527 else { // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
528 close(fd);
529 conn->err_code = CO_ER_SOCK_ERR;
530 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200531 return SF_ERR_SRVCL;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200532 }
533 }
Willy Tarreau7bb21532014-05-10 09:48:28 +0200534 else {
535 /* connect() already succeeded, which is quite usual for unix
Willy Tarreau94841792017-01-25 14:27:38 +0100536 * sockets. Let's avoid a second connect() probe to complete it.
Willy Tarreau7bb21532014-05-10 09:48:28 +0200537 */
538 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau7bb21532014-05-10 09:48:28 +0200539 }
Willy Tarreau47f48c42014-05-09 22:57:47 +0200540
541 conn->flags |= CO_FL_ADDR_TO_SET;
542
543 /* Prepare to send a few handshakes related to the on-wire protocol. */
544 if (conn->send_proxy_ofs)
545 conn->flags |= CO_FL_SEND_PROXY;
546
547 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreau7bb21532014-05-10 09:48:28 +0200548 fdtab[fd].linger_risk = 0; /* no need to disable lingering */
Willy Tarreau47f48c42014-05-09 22:57:47 +0200549
550 if (conn_xprt_init(conn) < 0) {
551 conn_force_close(conn);
552 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200553 return SF_ERR_RESOURCE;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200554 }
555
Willy Tarreau94841792017-01-25 14:27:38 +0100556 if (conn->flags & (CO_FL_HANDSHAKE | CO_FL_WAIT_L4_CONN)) {
557 conn_sock_want_send(conn); /* for connect status, proxy protocol or SSL */
558 }
559 else {
560 /* If there's no more handshake, we need to notify the data
561 * layer when the connection is already OK otherwise we'll have
562 * no other opportunity to do it later (eg: health checks).
563 */
564 data = 1;
565 }
566
Willy Tarreau47f48c42014-05-09 22:57:47 +0200567 if (data)
568 conn_data_want_send(conn); /* prepare to send data if any */
569
Willy Tarreaue7dff022015-04-03 01:14:29 +0200570 return SF_ERR_NONE; /* connection is OK */
Willy Tarreau47f48c42014-05-09 22:57:47 +0200571}
572
573
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100574/********************************
575 * 3) protocol-oriented functions
576 ********************************/
577
578
Willy Tarreau92fb9832007-10-16 17:34:28 +0200579/* This function creates all UNIX sockets bound to the protocol entry <proto>.
580 * It is intended to be used as the protocol's bind_all() function.
581 * The sockets will be registered but not added to any fd_set, in order not to
582 * loose them across the fork(). A call to uxst_enable_listeners() is needed
583 * to complete initialization.
584 *
585 * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL.
586 */
Emeric Bruncf20bf12010-10-22 16:06:11 +0200587static int uxst_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreau92fb9832007-10-16 17:34:28 +0200588{
589 struct listener *listener;
590 int err = ERR_NONE;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200591
592 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200593 err |= uxst_bind_listener(listener, errmsg, errlen);
594 if (err & ERR_ABORT)
595 break;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200596 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200597 return err;
598}
599
Willy Tarreau92fb9832007-10-16 17:34:28 +0200600
601/* This function stops all listening UNIX sockets bound to the protocol
602 * <proto>. It does not detaches them from the protocol.
603 * It always returns ERR_NONE.
604 */
605static int uxst_unbind_listeners(struct protocol *proto)
606{
607 struct listener *listener;
608
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100609 list_for_each_entry(listener, &proto->listeners, proto_list)
610 uxst_unbind_listener(listener);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200611 return ERR_NONE;
612}
613
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200614/* parse the "mode" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200615static 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 +0200616{
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200617 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200618 memprintf(err, "'%s' : missing mode (octal integer expected)", args[cur_arg]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200619 return ERR_ALERT | ERR_FATAL;
620 }
621
Willy Tarreau290e63a2012-09-20 18:07:14 +0200622 conf->ux.mode = strtol(args[cur_arg + 1], NULL, 8);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200623 return 0;
624}
625
626/* parse the "gid" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200627static 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 +0200628{
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200629 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200630 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200631 return ERR_ALERT | ERR_FATAL;
632 }
633
Willy Tarreau290e63a2012-09-20 18:07:14 +0200634 conf->ux.gid = atol(args[cur_arg + 1]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200635 return 0;
636}
637
638/* parse the "group" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200639static 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 +0200640{
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200641 struct group *group;
642
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200643 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200644 memprintf(err, "'%s' : missing group name", args[cur_arg]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200645 return ERR_ALERT | ERR_FATAL;
646 }
647
648 group = getgrnam(args[cur_arg + 1]);
649 if (!group) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200650 memprintf(err, "'%s' : unknown group name '%s'", args[cur_arg], args[cur_arg + 1]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200651 return ERR_ALERT | ERR_FATAL;
652 }
653
Willy Tarreau290e63a2012-09-20 18:07:14 +0200654 conf->ux.gid = group->gr_gid;
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200655 return 0;
656}
657
658/* parse the "uid" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200659static 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 +0200660{
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200661 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200662 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200663 return ERR_ALERT | ERR_FATAL;
664 }
665
Willy Tarreau290e63a2012-09-20 18:07:14 +0200666 conf->ux.uid = atol(args[cur_arg + 1]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200667 return 0;
668}
669
670/* parse the "user" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200671static 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 +0200672{
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200673 struct passwd *user;
674
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200675 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200676 memprintf(err, "'%s' : missing user name", args[cur_arg]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200677 return ERR_ALERT | ERR_FATAL;
678 }
679
680 user = getpwnam(args[cur_arg + 1]);
681 if (!user) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200682 memprintf(err, "'%s' : unknown user name '%s'", args[cur_arg], args[cur_arg + 1]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200683 return ERR_ALERT | ERR_FATAL;
684 }
685
Willy Tarreau290e63a2012-09-20 18:07:14 +0200686 conf->ux.uid = user->pw_uid;
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200687 return 0;
688}
689
690/* Note: must not be declared <const> as its list will be overwritten.
691 * Please take care of keeping this list alphabetically sorted, doing so helps
692 * all code contributors.
693 * Optional keywords are also declared with a NULL ->parse() function so that
694 * the config parser can report an appropriate error when a known keyword was
695 * not enabled.
696 */
Willy Tarreau51fb7652012-09-18 18:24:39 +0200697static struct bind_kw_list bind_kws = { "UNIX", { }, {
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200698 { "gid", bind_parse_gid, 1 }, /* set the socket's gid */
699 { "group", bind_parse_group, 1 }, /* set the socket's gid from the group name */
700 { "mode", bind_parse_mode, 1 }, /* set the socket's mode (eg: 0644)*/
701 { "uid", bind_parse_uid, 1 }, /* set the socket's uid */
702 { "user", bind_parse_user, 1 }, /* set the socket's uid from the user name */
703 { NULL, NULL, 0 },
704}};
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100705
706/********************************
707 * 4) high-level functions
708 ********************************/
709
Willy Tarreau92fb9832007-10-16 17:34:28 +0200710__attribute__((constructor))
711static void __uxst_protocol_init(void)
712{
713 protocol_register(&proto_unix);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200714 bind_register_keywords(&bind_kws);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200715}
716
717
718/*
719 * Local variables:
720 * c-indent-level: 8
721 * c-basic-offset: 8
722 * End:
723 */