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