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