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