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