Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 1 | /* |
| 2 | * UNIX SOCK_STREAM protocol layer (uxst) |
| 3 | * |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 4 | * Copyright 2000-2010 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 5 | * |
| 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 Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 16 | #include <pwd.h> |
| 17 | #include <grp.h> |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 18 | #include <stdio.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <string.h> |
| 21 | #include <syslog.h> |
| 22 | #include <time.h> |
| 23 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 24 | #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 Tarreau | d740bab | 2007-10-28 11:14:07 +0100 | [diff] [blame] | 32 | #include <common/errors.h> |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 33 | #include <common/initcall.h> |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 34 | #include <common/mini-clist.h> |
| 35 | #include <common/standard.h> |
| 36 | #include <common/time.h> |
| 37 | #include <common/version.h> |
| 38 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 39 | #include <types/global.h> |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 40 | |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 41 | #include <proto/connection.h> |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 42 | #include <proto/fd.h> |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 43 | #include <proto/listener.h> |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 44 | #include <proto/log.h> |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 45 | #include <proto/protocol.h> |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 46 | #include <proto/task.h> |
| 47 | |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 48 | static int uxst_bind_listener(struct listener *listener, char *errmsg, int errlen); |
| 49 | static int uxst_bind_listeners(struct protocol *proto, char *errmsg, int errlen); |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 50 | static int uxst_unbind_listeners(struct protocol *proto); |
Olivier Houchard | fdcb007 | 2019-05-06 18:32:29 +0200 | [diff] [blame] | 51 | static int uxst_connect_server(struct connection *conn, int flags); |
Willy Tarreau | 9d5be5c | 2017-09-15 07:55:51 +0200 | [diff] [blame] | 52 | static void uxst_add_listener(struct listener *listener, int port); |
Willy Tarreau | 3179489 | 2017-09-15 07:59:31 +0200 | [diff] [blame] | 53 | static int uxst_pause_listener(struct listener *l); |
| 54 | static int uxst_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir); |
| 55 | static int uxst_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir); |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 56 | |
| 57 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 58 | static struct protocol proto_unix = { |
| 59 | .name = "unix_stream", |
| 60 | .sock_domain = PF_UNIX, |
| 61 | .sock_type = SOCK_STREAM, |
| 62 | .sock_prot = 0, |
| 63 | .sock_family = AF_UNIX, |
| 64 | .sock_addrlen = sizeof(struct sockaddr_un), |
| 65 | .l3_addrlen = sizeof(((struct sockaddr_un*)0)->sun_path),/* path len */ |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 66 | .accept = &listener_accept, |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 67 | .connect = &uxst_connect_server, |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 68 | .bind = uxst_bind_listener, |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 69 | .bind_all = uxst_bind_listeners, |
| 70 | .unbind_all = uxst_unbind_listeners, |
| 71 | .enable_all = enable_all_listeners, |
| 72 | .disable_all = disable_all_listeners, |
Willy Tarreau | 59b9479 | 2012-05-11 16:16:40 +0200 | [diff] [blame] | 73 | .get_src = uxst_get_src, |
| 74 | .get_dst = uxst_get_dst, |
Willy Tarreau | fd0e008 | 2014-07-07 21:07:51 +0200 | [diff] [blame] | 75 | .pause = uxst_pause_listener, |
Willy Tarreau | 9d5be5c | 2017-09-15 07:55:51 +0200 | [diff] [blame] | 76 | .add = uxst_add_listener, |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 77 | .listeners = LIST_HEAD_INIT(proto_unix.listeners), |
| 78 | .nb_listeners = 0, |
| 79 | }; |
| 80 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 81 | INITCALL1(STG_REGISTER, protocol_register, &proto_unix); |
| 82 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 83 | /******************************** |
| 84 | * 1) low-level socket functions |
| 85 | ********************************/ |
| 86 | |
Willy Tarreau | 59b9479 | 2012-05-11 16:16:40 +0200 | [diff] [blame] | 87 | /* |
| 88 | * Retrieves the source address for the socket <fd>, with <dir> indicating |
| 89 | * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of |
| 90 | * success, -1 in case of error. The socket's source address is stored in |
| 91 | * <sa> for <salen> bytes. |
| 92 | */ |
Willy Tarreau | 3179489 | 2017-09-15 07:59:31 +0200 | [diff] [blame] | 93 | static int uxst_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir) |
Willy Tarreau | 59b9479 | 2012-05-11 16:16:40 +0200 | [diff] [blame] | 94 | { |
| 95 | if (dir) |
| 96 | return getsockname(fd, sa, &salen); |
| 97 | else |
| 98 | return getpeername(fd, sa, &salen); |
| 99 | } |
| 100 | |
| 101 | |
| 102 | /* |
| 103 | * Retrieves the original destination address for the socket <fd>, with <dir> |
| 104 | * indicating if we're a listener (=0) or an initiator (!=0). It returns 0 in |
| 105 | * case of success, -1 in case of error. The socket's source address is stored |
| 106 | * in <sa> for <salen> bytes. |
| 107 | */ |
Willy Tarreau | 3179489 | 2017-09-15 07:59:31 +0200 | [diff] [blame] | 108 | static int uxst_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir) |
Willy Tarreau | 59b9479 | 2012-05-11 16:16:40 +0200 | [diff] [blame] | 109 | { |
| 110 | if (dir) |
| 111 | return getpeername(fd, sa, &salen); |
| 112 | else |
| 113 | return getsockname(fd, sa, &salen); |
| 114 | } |
| 115 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 116 | |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 117 | /******************************** |
| 118 | * 2) listener-oriented functions |
| 119 | ********************************/ |
| 120 | |
| 121 | |
Olivier Houchard | f886e34 | 2017-04-05 22:24:59 +0200 | [diff] [blame] | 122 | static int uxst_find_compatible_fd(struct listener *l) |
| 123 | { |
| 124 | struct xfer_sock_list *xfer_sock = xfer_sock_list; |
| 125 | int ret = -1; |
| 126 | |
| 127 | while (xfer_sock) { |
| 128 | struct sockaddr_un *un1 = (void *)&l->addr; |
| 129 | struct sockaddr_un *un2 = (void *)&xfer_sock->addr; |
| 130 | |
| 131 | /* |
| 132 | * The bound socket's path as returned by getsockaddr |
| 133 | * will be the temporary name <sockname>.XXXXX.tmp, |
| 134 | * so we can't just compare the two names |
| 135 | */ |
| 136 | if (xfer_sock->addr.ss_family == AF_UNIX && |
| 137 | strncmp(un1->sun_path, un2->sun_path, |
| 138 | strlen(un1->sun_path)) == 0) { |
| 139 | char *after_sockname = un2->sun_path + |
| 140 | strlen(un1->sun_path); |
| 141 | /* Make a reasonnable effort to check that |
| 142 | * it is indeed a haproxy-generated temporary |
| 143 | * name, it's not perfect, but probably good enough. |
| 144 | */ |
| 145 | if (after_sockname[0] == '.') { |
| 146 | after_sockname++; |
| 147 | while (after_sockname[0] >= '0' && |
| 148 | after_sockname[0] <= '9') |
| 149 | after_sockname++; |
| 150 | if (!strcmp(after_sockname, ".tmp")) |
| 151 | break; |
Olivier Houchard | b4dd15b | 2018-06-06 18:34:34 +0200 | [diff] [blame] | 152 | /* abns sockets sun_path starts with a \0 */ |
| 153 | } else if (un1->sun_path[0] == 0 |
| 154 | && un2->sun_path[0] == 0 |
| 155 | && !memcmp(&un1->sun_path[1], &un2->sun_path[1], |
| 156 | sizeof(un1->sun_path) - 1)) |
| 157 | break; |
Olivier Houchard | f886e34 | 2017-04-05 22:24:59 +0200 | [diff] [blame] | 158 | } |
| 159 | xfer_sock = xfer_sock->next; |
| 160 | } |
| 161 | if (xfer_sock != NULL) { |
| 162 | ret = xfer_sock->fd; |
| 163 | if (xfer_sock == xfer_sock_list) |
| 164 | xfer_sock_list = xfer_sock->next; |
| 165 | if (xfer_sock->prev) |
| 166 | xfer_sock->prev->next = xfer_sock->next; |
| 167 | if (xfer_sock->next) |
Olivier Houchard | ec9516a | 2018-03-08 18:25:49 +0100 | [diff] [blame] | 168 | xfer_sock->next->prev = xfer_sock->prev; |
Olivier Houchard | f886e34 | 2017-04-05 22:24:59 +0200 | [diff] [blame] | 169 | free(xfer_sock); |
| 170 | } |
| 171 | return ret; |
| 172 | |
| 173 | } |
| 174 | |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 175 | /* This function creates a UNIX socket associated to the listener. It changes |
| 176 | * the state from ASSIGNED to LISTEN. The socket is NOT enabled for polling. |
Willy Tarreau | 8ab505b | 2013-01-24 01:41:38 +0100 | [diff] [blame] | 177 | * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL. It |
| 178 | * may return a warning or an error message in <errmsg> if the message is at |
| 179 | * most <errlen> bytes long (including '\0'). Note that <errmsg> may be NULL if |
| 180 | * <errlen> is also zero. |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 181 | */ |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 182 | static int uxst_bind_listener(struct listener *listener, char *errmsg, int errlen) |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 183 | { |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 184 | int fd; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 185 | char tempname[MAXPATHLEN]; |
| 186 | char backname[MAXPATHLEN]; |
| 187 | struct sockaddr_un addr; |
Willy Tarreau | b40dc94 | 2010-11-07 12:10:51 +0100 | [diff] [blame] | 188 | const char *msg = NULL; |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 189 | const char *path; |
Willy Tarreau | 327ea5a | 2020-02-11 06:43:37 +0100 | [diff] [blame] | 190 | int maxpathlen; |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 191 | int ext, ready; |
| 192 | socklen_t ready_len; |
Willy Tarreau | 3c5efa2 | 2014-07-07 18:36:45 +0200 | [diff] [blame] | 193 | int err; |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 194 | int ret; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 195 | |
Willy Tarreau | 3c5efa2 | 2014-07-07 18:36:45 +0200 | [diff] [blame] | 196 | err = ERR_NONE; |
| 197 | |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 198 | /* ensure we never return garbage */ |
Willy Tarreau | 8ab505b | 2013-01-24 01:41:38 +0100 | [diff] [blame] | 199 | if (errlen) |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 200 | *errmsg = 0; |
| 201 | |
| 202 | if (listener->state != LI_ASSIGNED) |
| 203 | return ERR_NONE; /* already bound */ |
| 204 | |
Olivier Houchard | f886e34 | 2017-04-05 22:24:59 +0200 | [diff] [blame] | 205 | if (listener->fd == -1) |
| 206 | listener->fd = uxst_find_compatible_fd(listener); |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 207 | path = ((struct sockaddr_un *)&listener->addr)->sun_path; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 208 | |
Willy Tarreau | 327ea5a | 2020-02-11 06:43:37 +0100 | [diff] [blame] | 209 | maxpathlen = MIN(MAXPATHLEN, sizeof(addr.sun_path)); |
| 210 | |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 211 | /* if the listener already has an fd assigned, then we were offered the |
| 212 | * fd by an external process (most likely the parent), and we don't want |
| 213 | * to create a new socket. However we still want to set a few flags on |
| 214 | * the socket. |
| 215 | */ |
| 216 | fd = listener->fd; |
| 217 | ext = (fd >= 0); |
| 218 | if (ext) |
| 219 | goto fd_ready; |
| 220 | |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 221 | if (path[0]) { |
Willy Tarreau | 327ea5a | 2020-02-11 06:43:37 +0100 | [diff] [blame] | 222 | ret = snprintf(tempname, maxpathlen, "%s.%d.tmp", path, pid); |
| 223 | if (ret < 0 || ret >= maxpathlen) { |
Willy Tarreau | 3c5efa2 | 2014-07-07 18:36:45 +0200 | [diff] [blame] | 224 | err |= ERR_FATAL | ERR_ALERT; |
Willy Tarreau | 327ea5a | 2020-02-11 06:43:37 +0100 | [diff] [blame] | 225 | msg = "name too long for UNIX socket (limit usually 97)"; |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 226 | goto err_return; |
| 227 | } |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 228 | |
Willy Tarreau | 327ea5a | 2020-02-11 06:43:37 +0100 | [diff] [blame] | 229 | ret = snprintf(backname, maxpathlen, "%s.%d.bak", path, pid); |
| 230 | if (ret < 0 || ret >= maxpathlen) { |
Willy Tarreau | 3c5efa2 | 2014-07-07 18:36:45 +0200 | [diff] [blame] | 231 | err |= ERR_FATAL | ERR_ALERT; |
Willy Tarreau | 327ea5a | 2020-02-11 06:43:37 +0100 | [diff] [blame] | 232 | msg = "name too long for UNIX socket (limit usually 97)"; |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 233 | goto err_return; |
| 234 | } |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 235 | |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 236 | /* 2. clean existing orphaned entries */ |
| 237 | if (unlink(tempname) < 0 && errno != ENOENT) { |
Willy Tarreau | 3c5efa2 | 2014-07-07 18:36:45 +0200 | [diff] [blame] | 238 | err |= ERR_FATAL | ERR_ALERT; |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 239 | msg = "error when trying to unlink previous UNIX socket"; |
| 240 | goto err_return; |
| 241 | } |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 242 | |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 243 | if (unlink(backname) < 0 && errno != ENOENT) { |
Willy Tarreau | 3c5efa2 | 2014-07-07 18:36:45 +0200 | [diff] [blame] | 244 | err |= ERR_FATAL | ERR_ALERT; |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 245 | msg = "error when trying to unlink previous UNIX socket"; |
| 246 | goto err_return; |
| 247 | } |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 248 | |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 249 | /* 3. backup existing socket */ |
| 250 | if (link(path, backname) < 0 && errno != ENOENT) { |
Willy Tarreau | 3c5efa2 | 2014-07-07 18:36:45 +0200 | [diff] [blame] | 251 | err |= ERR_FATAL | ERR_ALERT; |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 252 | msg = "error when trying to preserve previous UNIX socket"; |
| 253 | goto err_return; |
| 254 | } |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 255 | |
Willy Tarreau | 719e07c | 2019-12-11 16:29:10 +0100 | [diff] [blame] | 256 | strncpy(addr.sun_path, tempname, sizeof(addr.sun_path) - 1); |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 257 | addr.sun_path[sizeof(addr.sun_path) - 1] = 0; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 258 | } |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 259 | else { |
| 260 | /* first char is zero, it's an abstract socket whose address |
| 261 | * is defined by all the bytes past this zero. |
| 262 | */ |
| 263 | memcpy(addr.sun_path, path, sizeof(addr.sun_path)); |
| 264 | } |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 265 | addr.sun_family = AF_UNIX; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 266 | |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 267 | fd = socket(PF_UNIX, SOCK_STREAM, 0); |
| 268 | if (fd < 0) { |
Willy Tarreau | 3c5efa2 | 2014-07-07 18:36:45 +0200 | [diff] [blame] | 269 | err |= ERR_FATAL | ERR_ALERT; |
Willy Tarreau | b40dc94 | 2010-11-07 12:10:51 +0100 | [diff] [blame] | 270 | msg = "cannot create UNIX socket"; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 271 | goto err_unlink_back; |
| 272 | } |
| 273 | |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 274 | fd_ready: |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 275 | if (fd >= global.maxsock) { |
Willy Tarreau | 3c5efa2 | 2014-07-07 18:36:45 +0200 | [diff] [blame] | 276 | err |= ERR_FATAL | ERR_ALERT; |
Willy Tarreau | b40dc94 | 2010-11-07 12:10:51 +0100 | [diff] [blame] | 277 | msg = "socket(): not enough free sockets, raise -n argument"; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 278 | goto err_unlink_temp; |
| 279 | } |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 280 | |
| 281 | if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) { |
Willy Tarreau | 3c5efa2 | 2014-07-07 18:36:45 +0200 | [diff] [blame] | 282 | err |= ERR_FATAL | ERR_ALERT; |
Willy Tarreau | b40dc94 | 2010-11-07 12:10:51 +0100 | [diff] [blame] | 283 | msg = "cannot make UNIX socket non-blocking"; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 284 | goto err_unlink_temp; |
| 285 | } |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 286 | |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 287 | if (!ext && bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) { |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 288 | /* note that bind() creates the socket <tempname> on the file system */ |
Willy Tarreau | 3c5efa2 | 2014-07-07 18:36:45 +0200 | [diff] [blame] | 289 | if (errno == EADDRINUSE) { |
| 290 | /* the old process might still own it, let's retry */ |
| 291 | err |= ERR_RETRYABLE | ERR_ALERT; |
| 292 | msg = "cannot listen to socket"; |
| 293 | } |
| 294 | else { |
| 295 | err |= ERR_FATAL | ERR_ALERT; |
| 296 | msg = "cannot bind UNIX socket"; |
| 297 | } |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 298 | goto err_unlink_temp; |
| 299 | } |
| 300 | |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 301 | /* <uid> and <gid> different of -1 will be used to change the socket owner. |
| 302 | * If <mode> is not 0, it will be used to restrict access to the socket. |
| 303 | * While it is known not to be portable on every OS, it's still useful |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 304 | * where it works. We also don't change permissions on abstract sockets. |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 305 | */ |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 306 | if (!ext && path[0] && |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 307 | (((listener->bind_conf->ux.uid != -1 || listener->bind_conf->ux.gid != -1) && |
| 308 | (chown(tempname, listener->bind_conf->ux.uid, listener->bind_conf->ux.gid) == -1)) || |
| 309 | (listener->bind_conf->ux.mode != 0 && chmod(tempname, listener->bind_conf->ux.mode) == -1))) { |
Willy Tarreau | 3c5efa2 | 2014-07-07 18:36:45 +0200 | [diff] [blame] | 310 | err |= ERR_FATAL | ERR_ALERT; |
Willy Tarreau | b40dc94 | 2010-11-07 12:10:51 +0100 | [diff] [blame] | 311 | msg = "cannot change UNIX socket ownership"; |
Willy Tarreau | e6ad2b1 | 2007-10-18 12:45:54 +0200 | [diff] [blame] | 312 | goto err_unlink_temp; |
| 313 | } |
| 314 | |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 315 | ready = 0; |
| 316 | ready_len = sizeof(ready); |
| 317 | if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1) |
| 318 | ready = 0; |
| 319 | |
| 320 | if (!(ext && ready) && /* only listen if not already done by external process */ |
Willy Tarreau | e2711c7 | 2019-02-27 15:39:41 +0100 | [diff] [blame] | 321 | listen(fd, listener_backlog(listener)) < 0) { |
Willy Tarreau | 3c5efa2 | 2014-07-07 18:36:45 +0200 | [diff] [blame] | 322 | err |= ERR_FATAL | ERR_ALERT; |
Willy Tarreau | b40dc94 | 2010-11-07 12:10:51 +0100 | [diff] [blame] | 323 | msg = "cannot listen to UNIX socket"; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 324 | goto err_unlink_temp; |
| 325 | } |
| 326 | |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 327 | /* Point of no return: we are ready, we'll switch the sockets. We don't |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 328 | * fear loosing the socket <path> because we have a copy of it in |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 329 | * backname. Abstract sockets are not renamed. |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 330 | */ |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 331 | if (!ext && path[0] && rename(tempname, path) < 0) { |
Willy Tarreau | 3c5efa2 | 2014-07-07 18:36:45 +0200 | [diff] [blame] | 332 | err |= ERR_FATAL | ERR_ALERT; |
Willy Tarreau | b40dc94 | 2010-11-07 12:10:51 +0100 | [diff] [blame] | 333 | msg = "cannot switch final and temporary UNIX sockets"; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 334 | goto err_rename; |
| 335 | } |
| 336 | |
Willy Tarreau | 68986ab | 2017-06-16 10:34:20 +0200 | [diff] [blame] | 337 | /* Cleanup: only unlink if we didn't inherit the fd from the parent */ |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 338 | if (!ext && path[0]) |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 339 | unlink(backname); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 340 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 341 | /* the socket is now listening */ |
| 342 | listener->fd = fd; |
| 343 | listener->state = LI_LISTEN; |
| 344 | |
Willy Tarreau | a9786b6 | 2018-01-25 07:22:13 +0100 | [diff] [blame] | 345 | fd_insert(fd, listener, listener->proto->accept, |
Willy Tarreau | 0948a78 | 2020-02-12 10:15:34 +0100 | [diff] [blame^] | 346 | thread_mask(listener->bind_conf->bind_thread) & all_threads_mask); |
Willy Tarreau | a9786b6 | 2018-01-25 07:22:13 +0100 | [diff] [blame] | 347 | |
Willy Tarreau | 3c5efa2 | 2014-07-07 18:36:45 +0200 | [diff] [blame] | 348 | return err; |
| 349 | |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 350 | err_rename: |
| 351 | ret = rename(backname, path); |
| 352 | if (ret < 0 && errno == ENOENT) |
| 353 | unlink(path); |
| 354 | err_unlink_temp: |
Jan Seda | 7319b64 | 2014-06-26 20:44:05 +0200 | [diff] [blame] | 355 | if (!ext && path[0]) |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 356 | unlink(tempname); |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 357 | close(fd); |
| 358 | err_unlink_back: |
Jan Seda | 7319b64 | 2014-06-26 20:44:05 +0200 | [diff] [blame] | 359 | if (!ext && path[0]) |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 360 | unlink(backname); |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 361 | err_return: |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 362 | if (msg && errlen) { |
| 363 | if (!ext) |
| 364 | snprintf(errmsg, errlen, "%s [%s]", msg, path); |
| 365 | else |
| 366 | snprintf(errmsg, errlen, "%s [fd %d]", msg, fd); |
| 367 | } |
Willy Tarreau | 3c5efa2 | 2014-07-07 18:36:45 +0200 | [diff] [blame] | 368 | return err; |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | /* This function closes the UNIX sockets for the specified listener. |
| 372 | * The listener enters the LI_ASSIGNED state. It always returns ERR_NONE. |
| 373 | */ |
| 374 | static int uxst_unbind_listener(struct listener *listener) |
| 375 | { |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 376 | if (listener->state > LI_ASSIGNED) { |
| 377 | unbind_listener(listener); |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 378 | } |
| 379 | return ERR_NONE; |
| 380 | } |
| 381 | |
Willy Tarreau | 3228238 | 2017-09-15 07:44:44 +0200 | [diff] [blame] | 382 | /* Add <listener> to the list of unix stream listeners (port is ignored). The |
| 383 | * listener's state is automatically updated from LI_INIT to LI_ASSIGNED. |
| 384 | * The number of listeners for the protocol is updated. |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 385 | * |
| 386 | * Must be called with proto_lock held. |
| 387 | * |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 388 | */ |
Willy Tarreau | 9d5be5c | 2017-09-15 07:55:51 +0200 | [diff] [blame] | 389 | static void uxst_add_listener(struct listener *listener, int port) |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 390 | { |
| 391 | if (listener->state != LI_INIT) |
| 392 | return; |
| 393 | listener->state = LI_ASSIGNED; |
| 394 | listener->proto = &proto_unix; |
| 395 | LIST_ADDQ(&proto_unix.listeners, &listener->proto_list); |
| 396 | proto_unix.nb_listeners++; |
| 397 | } |
| 398 | |
Willy Tarreau | fd0e008 | 2014-07-07 21:07:51 +0200 | [diff] [blame] | 399 | /* Pause a listener. Returns < 0 in case of failure, 0 if the listener |
| 400 | * was totally stopped, or > 0 if correctly paused. Nothing is done for |
| 401 | * plain unix sockets since currently it's the new process which handles |
| 402 | * the renaming. Abstract sockets are completely unbound. |
| 403 | */ |
Willy Tarreau | 3179489 | 2017-09-15 07:59:31 +0200 | [diff] [blame] | 404 | static int uxst_pause_listener(struct listener *l) |
Willy Tarreau | fd0e008 | 2014-07-07 21:07:51 +0200 | [diff] [blame] | 405 | { |
| 406 | if (((struct sockaddr_un *)&l->addr)->sun_path[0]) |
| 407 | return 1; |
| 408 | |
Christopher Faulet | 510c0d6 | 2018-03-16 10:04:47 +0100 | [diff] [blame] | 409 | /* Listener's lock already held. Call lockless version of |
| 410 | * unbind_listener. */ |
| 411 | do_unbind_listener(l, 1); |
Willy Tarreau | fd0e008 | 2014-07-07 21:07:51 +0200 | [diff] [blame] | 412 | return 0; |
| 413 | } |
| 414 | |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 415 | |
| 416 | /* |
| 417 | * This function initiates a UNIX connection establishment to the target assigned |
Willy Tarreau | ca9f5a9 | 2019-07-17 16:40:37 +0200 | [diff] [blame] | 418 | * to connection <conn> using (si->{target,dst}). The source address is ignored |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 419 | * and will be selected by the system. conn->target may point either to a valid |
| 420 | * server or to a backend, depending on conn->target. Only OBJ_TYPE_PROXY and |
| 421 | * OBJ_TYPE_SERVER are supported. The <data> parameter is a boolean indicating |
| 422 | * whether there are data waiting for being sent or not, in order to adjust data |
| 423 | * write polling and on some platforms. The <delack> argument is ignored. |
| 424 | * |
| 425 | * Note that a pending send_proxy message accounts for data. |
| 426 | * |
| 427 | * It can return one of : |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 428 | * - SF_ERR_NONE if everything's OK |
| 429 | * - SF_ERR_SRVTO if there are no more servers |
| 430 | * - SF_ERR_SRVCL if the connection was refused by the server |
| 431 | * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn) |
| 432 | * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...) |
| 433 | * - SF_ERR_INTERNAL for any other purely internal errors |
Tim Düsterhus | 4896c44 | 2016-11-29 02:15:19 +0100 | [diff] [blame] | 434 | * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted. |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 435 | * |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 436 | * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 437 | * it's invalid and the caller has nothing to do. |
| 438 | */ |
Olivier Houchard | fdcb007 | 2019-05-06 18:32:29 +0200 | [diff] [blame] | 439 | static int uxst_connect_server(struct connection *conn, int flags) |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 440 | { |
| 441 | int fd; |
| 442 | struct server *srv; |
| 443 | struct proxy *be; |
| 444 | |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 445 | switch (obj_type(conn->target)) { |
| 446 | case OBJ_TYPE_PROXY: |
| 447 | be = objt_proxy(conn->target); |
| 448 | srv = NULL; |
| 449 | break; |
| 450 | case OBJ_TYPE_SERVER: |
| 451 | srv = objt_server(conn->target); |
| 452 | be = srv->proxy; |
| 453 | break; |
| 454 | default: |
| 455 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 456 | return SF_ERR_INTERNAL; |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 457 | } |
| 458 | |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 459 | if ((fd = conn->handle.fd = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) { |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 460 | qfprintf(stderr, "Cannot get a server socket.\n"); |
| 461 | |
| 462 | if (errno == ENFILE) { |
| 463 | conn->err_code = CO_ER_SYS_FDLIM; |
| 464 | send_log(be, LOG_EMERG, |
Willy Tarreau | c5532ac | 2018-01-29 15:06:04 +0100 | [diff] [blame] | 465 | "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n", |
| 466 | be->id, global.maxsock); |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 467 | } |
| 468 | else if (errno == EMFILE) { |
| 469 | conn->err_code = CO_ER_PROC_FDLIM; |
| 470 | send_log(be, LOG_EMERG, |
Willy Tarreau | c5532ac | 2018-01-29 15:06:04 +0100 | [diff] [blame] | 471 | "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n", |
| 472 | be->id, global.maxsock); |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 473 | } |
| 474 | else if (errno == ENOBUFS || errno == ENOMEM) { |
| 475 | conn->err_code = CO_ER_SYS_MEMLIM; |
| 476 | send_log(be, LOG_EMERG, |
Willy Tarreau | c5532ac | 2018-01-29 15:06:04 +0100 | [diff] [blame] | 477 | "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n", |
| 478 | be->id, global.maxsock); |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 479 | } |
| 480 | else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) { |
| 481 | conn->err_code = CO_ER_NOPROTO; |
| 482 | } |
| 483 | else |
| 484 | conn->err_code = CO_ER_SOCK_ERR; |
| 485 | |
| 486 | /* this is a resource error */ |
| 487 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 488 | return SF_ERR_RESOURCE; |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | if (fd >= global.maxsock) { |
| 492 | /* do not log anything there, it's a normal condition when this option |
| 493 | * is used to serialize connections to a server ! |
| 494 | */ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 495 | ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n"); |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 496 | close(fd); |
| 497 | conn->err_code = CO_ER_CONF_FDLIM; |
| 498 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 499 | return SF_ERR_PRXCOND; /* it is a configuration limit */ |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) { |
| 503 | qfprintf(stderr,"Cannot set client socket to non blocking mode.\n"); |
| 504 | close(fd); |
| 505 | conn->err_code = CO_ER_SOCK_ERR; |
| 506 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 507 | return SF_ERR_INTERNAL; |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 508 | } |
| 509 | |
William Lallemand | c03eb01 | 2018-11-27 12:02:37 +0100 | [diff] [blame] | 510 | if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) { |
| 511 | ha_alert("Cannot set CLOEXEC on client socket.\n"); |
| 512 | close(fd); |
| 513 | conn->err_code = CO_ER_SOCK_ERR; |
| 514 | conn->flags |= CO_FL_ERROR; |
| 515 | return SF_ERR_INTERNAL; |
| 516 | } |
| 517 | |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 518 | /* if a send_proxy is there, there are data */ |
Olivier Houchard | fdcb007 | 2019-05-06 18:32:29 +0200 | [diff] [blame] | 519 | if (conn->send_proxy_ofs) |
| 520 | flags |= CONNECT_HAS_DATA; |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 521 | |
| 522 | if (global.tune.server_sndbuf) |
| 523 | setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf)); |
| 524 | |
| 525 | if (global.tune.server_rcvbuf) |
| 526 | setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf)); |
| 527 | |
Willy Tarreau | ca9f5a9 | 2019-07-17 16:40:37 +0200 | [diff] [blame] | 528 | if (connect(fd, (struct sockaddr *)conn->dst, get_addr_len(conn->dst)) == -1) { |
Willy Tarreau | 9484179 | 2017-01-25 14:27:38 +0100 | [diff] [blame] | 529 | if (errno == EINPROGRESS || errno == EALREADY) { |
Willy Tarreau | 7bb2153 | 2014-05-10 09:48:28 +0200 | [diff] [blame] | 530 | conn->flags |= CO_FL_WAIT_L4_CONN; |
| 531 | } |
Willy Tarreau | 9484179 | 2017-01-25 14:27:38 +0100 | [diff] [blame] | 532 | else if (errno == EISCONN) { |
| 533 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 534 | } |
Willy Tarreau | 7bb2153 | 2014-05-10 09:48:28 +0200 | [diff] [blame] | 535 | else if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) { |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 536 | char *msg; |
| 537 | if (errno == EAGAIN || errno == EADDRNOTAVAIL) { |
Lukas Tribus | 9f256d4 | 2016-01-26 20:33:14 +0100 | [diff] [blame] | 538 | msg = "can't connect to destination unix socket, check backlog size on the server"; |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 539 | conn->err_code = CO_ER_FREE_PORTS; |
| 540 | } |
| 541 | else { |
| 542 | msg = "local address already in use"; |
| 543 | conn->err_code = CO_ER_ADDR_INUSE; |
| 544 | } |
| 545 | |
| 546 | qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg); |
| 547 | close(fd); |
| 548 | send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg); |
| 549 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 550 | return SF_ERR_RESOURCE; |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 551 | } |
| 552 | else if (errno == ETIMEDOUT) { |
| 553 | close(fd); |
| 554 | conn->err_code = CO_ER_SOCK_ERR; |
| 555 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 556 | return SF_ERR_SRVTO; |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 557 | } |
| 558 | else { // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM) |
| 559 | close(fd); |
| 560 | conn->err_code = CO_ER_SOCK_ERR; |
| 561 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 562 | return SF_ERR_SRVCL; |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 563 | } |
| 564 | } |
Willy Tarreau | 7bb2153 | 2014-05-10 09:48:28 +0200 | [diff] [blame] | 565 | else { |
| 566 | /* connect() already succeeded, which is quite usual for unix |
Willy Tarreau | 9484179 | 2017-01-25 14:27:38 +0100 | [diff] [blame] | 567 | * sockets. Let's avoid a second connect() probe to complete it. |
Willy Tarreau | 7bb2153 | 2014-05-10 09:48:28 +0200 | [diff] [blame] | 568 | */ |
| 569 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Willy Tarreau | 7bb2153 | 2014-05-10 09:48:28 +0200 | [diff] [blame] | 570 | } |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 571 | |
| 572 | conn->flags |= CO_FL_ADDR_TO_SET; |
| 573 | |
| 574 | /* Prepare to send a few handshakes related to the on-wire protocol. */ |
| 575 | if (conn->send_proxy_ofs) |
| 576 | conn->flags |= CO_FL_SEND_PROXY; |
| 577 | |
| 578 | conn_ctrl_init(conn); /* registers the FD */ |
Willy Tarreau | 7bb2153 | 2014-05-10 09:48:28 +0200 | [diff] [blame] | 579 | fdtab[fd].linger_risk = 0; /* no need to disable lingering */ |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 580 | |
Willy Tarreau | ccf3f6d | 2019-09-05 17:05:05 +0200 | [diff] [blame] | 581 | if (conn->flags & CO_FL_WAIT_L4_CONN) |
| 582 | fd_cant_recv(fd); // we'll change this once the connection is validated |
| 583 | |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 584 | if (conn_xprt_init(conn) < 0) { |
Willy Tarreau | 8c82901 | 2017-10-05 18:02:11 +0200 | [diff] [blame] | 585 | conn_full_close(conn); |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 586 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 587 | return SF_ERR_RESOURCE; |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 588 | } |
| 589 | |
Olivier Houchard | 03abf2d | 2019-05-28 10:12:02 +0200 | [diff] [blame] | 590 | conn_xprt_want_send(conn); /* for connect status, proxy protocol or SSL */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 591 | return SF_ERR_NONE; /* connection is OK */ |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 595 | /******************************** |
| 596 | * 3) protocol-oriented functions |
| 597 | ********************************/ |
| 598 | |
| 599 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 600 | /* This function creates all UNIX sockets bound to the protocol entry <proto>. |
| 601 | * It is intended to be used as the protocol's bind_all() function. |
| 602 | * The sockets will be registered but not added to any fd_set, in order not to |
| 603 | * loose them across the fork(). A call to uxst_enable_listeners() is needed |
| 604 | * to complete initialization. |
| 605 | * |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 606 | * Must be called with proto_lock held. |
| 607 | * |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 608 | * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL. |
| 609 | */ |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 610 | static int uxst_bind_listeners(struct protocol *proto, char *errmsg, int errlen) |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 611 | { |
| 612 | struct listener *listener; |
| 613 | int err = ERR_NONE; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 614 | |
| 615 | list_for_each_entry(listener, &proto->listeners, proto_list) { |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 616 | err |= uxst_bind_listener(listener, errmsg, errlen); |
| 617 | if (err & ERR_ABORT) |
| 618 | break; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 619 | } |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 620 | return err; |
| 621 | } |
| 622 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 623 | |
| 624 | /* This function stops all listening UNIX sockets bound to the protocol |
| 625 | * <proto>. It does not detaches them from the protocol. |
| 626 | * It always returns ERR_NONE. |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 627 | * |
| 628 | * Must be called with proto_lock held. |
| 629 | * |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 630 | */ |
| 631 | static int uxst_unbind_listeners(struct protocol *proto) |
| 632 | { |
| 633 | struct listener *listener; |
| 634 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 635 | list_for_each_entry(listener, &proto->listeners, proto_list) |
| 636 | uxst_unbind_listener(listener); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 637 | return ERR_NONE; |
| 638 | } |
| 639 | |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 640 | /* parse the "mode" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 641 | static int bind_parse_mode(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 642 | { |
Willy Tarreau | a1a247b | 2017-10-04 14:43:44 +0200 | [diff] [blame] | 643 | char *endptr; |
| 644 | |
| 645 | conf->ux.mode = strtol(args[cur_arg + 1], &endptr, 8); |
| 646 | |
| 647 | if (!*args[cur_arg + 1] || *endptr) { |
| 648 | memprintf(err, "'%s' : missing or invalid mode '%s' (octal integer expected)", args[cur_arg], args[cur_arg + 1]); |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 649 | return ERR_ALERT | ERR_FATAL; |
| 650 | } |
| 651 | |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 652 | return 0; |
| 653 | } |
| 654 | |
| 655 | /* parse the "gid" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 656 | static int bind_parse_gid(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 657 | { |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 658 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 659 | memprintf(err, "'%s' : missing value", args[cur_arg]); |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 660 | return ERR_ALERT | ERR_FATAL; |
| 661 | } |
| 662 | |
Willy Tarreau | 290e63a | 2012-09-20 18:07:14 +0200 | [diff] [blame] | 663 | conf->ux.gid = atol(args[cur_arg + 1]); |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 664 | return 0; |
| 665 | } |
| 666 | |
| 667 | /* parse the "group" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 668 | static int bind_parse_group(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 669 | { |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 670 | struct group *group; |
| 671 | |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 672 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 673 | memprintf(err, "'%s' : missing group name", args[cur_arg]); |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 674 | return ERR_ALERT | ERR_FATAL; |
| 675 | } |
| 676 | |
| 677 | group = getgrnam(args[cur_arg + 1]); |
| 678 | if (!group) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 679 | memprintf(err, "'%s' : unknown group name '%s'", args[cur_arg], args[cur_arg + 1]); |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 680 | return ERR_ALERT | ERR_FATAL; |
| 681 | } |
| 682 | |
Willy Tarreau | 290e63a | 2012-09-20 18:07:14 +0200 | [diff] [blame] | 683 | conf->ux.gid = group->gr_gid; |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 684 | return 0; |
| 685 | } |
| 686 | |
| 687 | /* parse the "uid" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 688 | static int bind_parse_uid(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 689 | { |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 690 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 691 | memprintf(err, "'%s' : missing value", args[cur_arg]); |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 692 | return ERR_ALERT | ERR_FATAL; |
| 693 | } |
| 694 | |
Willy Tarreau | 290e63a | 2012-09-20 18:07:14 +0200 | [diff] [blame] | 695 | conf->ux.uid = atol(args[cur_arg + 1]); |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 696 | return 0; |
| 697 | } |
| 698 | |
| 699 | /* parse the "user" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 700 | static int bind_parse_user(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 701 | { |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 702 | struct passwd *user; |
| 703 | |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 704 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 705 | memprintf(err, "'%s' : missing user name", args[cur_arg]); |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 706 | return ERR_ALERT | ERR_FATAL; |
| 707 | } |
| 708 | |
| 709 | user = getpwnam(args[cur_arg + 1]); |
| 710 | if (!user) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 711 | memprintf(err, "'%s' : unknown user name '%s'", args[cur_arg], args[cur_arg + 1]); |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 712 | return ERR_ALERT | ERR_FATAL; |
| 713 | } |
| 714 | |
Willy Tarreau | 290e63a | 2012-09-20 18:07:14 +0200 | [diff] [blame] | 715 | conf->ux.uid = user->pw_uid; |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 716 | return 0; |
| 717 | } |
| 718 | |
| 719 | /* Note: must not be declared <const> as its list will be overwritten. |
| 720 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 721 | * all code contributors. |
| 722 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 723 | * the config parser can report an appropriate error when a known keyword was |
| 724 | * not enabled. |
| 725 | */ |
Willy Tarreau | 51fb765 | 2012-09-18 18:24:39 +0200 | [diff] [blame] | 726 | static struct bind_kw_list bind_kws = { "UNIX", { }, { |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 727 | { "gid", bind_parse_gid, 1 }, /* set the socket's gid */ |
| 728 | { "group", bind_parse_group, 1 }, /* set the socket's gid from the group name */ |
| 729 | { "mode", bind_parse_mode, 1 }, /* set the socket's mode (eg: 0644)*/ |
| 730 | { "uid", bind_parse_uid, 1 }, /* set the socket's uid */ |
| 731 | { "user", bind_parse_user, 1 }, /* set the socket's uid from the user name */ |
| 732 | { NULL, NULL, 0 }, |
| 733 | }}; |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 734 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 735 | INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 736 | |
| 737 | /* |
| 738 | * Local variables: |
| 739 | * c-indent-level: 8 |
| 740 | * c-basic-offset: 8 |
| 741 | * End: |
| 742 | */ |