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 | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 40 | #include <proto/fd.h> |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 41 | #include <proto/listener.h> |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 42 | #include <proto/log.h> |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 43 | #include <proto/protocol.h> |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 44 | #include <proto/proto_uxst.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); |
| 50 | |
| 51 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 52 | static struct protocol proto_unix = { |
| 53 | .name = "unix_stream", |
| 54 | .sock_domain = PF_UNIX, |
| 55 | .sock_type = SOCK_STREAM, |
| 56 | .sock_prot = 0, |
| 57 | .sock_family = AF_UNIX, |
| 58 | .sock_addrlen = sizeof(struct sockaddr_un), |
| 59 | .l3_addrlen = sizeof(((struct sockaddr_un*)0)->sun_path),/* path len */ |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 60 | .accept = &listener_accept, |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 61 | .bind = uxst_bind_listener, |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 62 | .bind_all = uxst_bind_listeners, |
| 63 | .unbind_all = uxst_unbind_listeners, |
| 64 | .enable_all = enable_all_listeners, |
| 65 | .disable_all = disable_all_listeners, |
Willy Tarreau | 59b9479 | 2012-05-11 16:16:40 +0200 | [diff] [blame] | 66 | .get_src = uxst_get_src, |
| 67 | .get_dst = uxst_get_dst, |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 68 | .listeners = LIST_HEAD_INIT(proto_unix.listeners), |
| 69 | .nb_listeners = 0, |
| 70 | }; |
| 71 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 72 | /******************************** |
| 73 | * 1) low-level socket functions |
| 74 | ********************************/ |
| 75 | |
Willy Tarreau | 59b9479 | 2012-05-11 16:16:40 +0200 | [diff] [blame] | 76 | /* |
| 77 | * Retrieves the source address for the socket <fd>, with <dir> indicating |
| 78 | * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of |
| 79 | * success, -1 in case of error. The socket's source address is stored in |
| 80 | * <sa> for <salen> bytes. |
| 81 | */ |
| 82 | int uxst_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir) |
| 83 | { |
| 84 | if (dir) |
| 85 | return getsockname(fd, sa, &salen); |
| 86 | else |
| 87 | return getpeername(fd, sa, &salen); |
| 88 | } |
| 89 | |
| 90 | |
| 91 | /* |
| 92 | * Retrieves the original destination address for the socket <fd>, with <dir> |
| 93 | * indicating if we're a listener (=0) or an initiator (!=0). It returns 0 in |
| 94 | * case of success, -1 in case of error. The socket's source address is stored |
| 95 | * in <sa> for <salen> bytes. |
| 96 | */ |
| 97 | int uxst_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir) |
| 98 | { |
| 99 | if (dir) |
| 100 | return getpeername(fd, sa, &salen); |
| 101 | else |
| 102 | return getsockname(fd, sa, &salen); |
| 103 | } |
| 104 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 105 | |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 106 | /* Tries to destroy the UNIX stream socket <path>. The socket must not be used |
| 107 | * anymore. It practises best effort, and no error is returned. |
| 108 | */ |
| 109 | static void destroy_uxst_socket(const char *path) |
| 110 | { |
| 111 | struct sockaddr_un addr; |
| 112 | int sock, ret; |
| 113 | |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 114 | /* if the path was cleared, we do nothing */ |
| 115 | if (!*path) |
| 116 | return; |
| 117 | |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 118 | /* We might have been chrooted, so we may not be able to access the |
| 119 | * socket. In order to avoid bothering the other end, we connect with a |
| 120 | * wrong protocol, namely SOCK_DGRAM. The return code from connect() |
| 121 | * is enough to know if the socket is still live or not. If it's live |
| 122 | * in mode SOCK_STREAM, we get EPROTOTYPE or anything else but not |
| 123 | * ECONNREFUSED. In this case, we do not touch it because it's used |
| 124 | * by some other process. |
| 125 | */ |
| 126 | sock = socket(PF_UNIX, SOCK_DGRAM, 0); |
| 127 | if (sock < 0) |
| 128 | return; |
| 129 | |
| 130 | addr.sun_family = AF_UNIX; |
| 131 | strncpy(addr.sun_path, path, sizeof(addr.sun_path)); |
| 132 | addr.sun_path[sizeof(addr.sun_path) - 1] = 0; |
| 133 | ret = connect(sock, (struct sockaddr *)&addr, sizeof(addr)); |
| 134 | if (ret < 0 && errno == ECONNREFUSED) { |
| 135 | /* Connect failed: the socket still exists but is not used |
| 136 | * anymore. Let's remove this socket now. |
| 137 | */ |
| 138 | unlink(path); |
| 139 | } |
| 140 | close(sock); |
| 141 | } |
| 142 | |
| 143 | |
| 144 | /******************************** |
| 145 | * 2) listener-oriented functions |
| 146 | ********************************/ |
| 147 | |
| 148 | |
| 149 | /* This function creates a UNIX socket associated to the listener. It changes |
| 150 | * 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] | 151 | * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL. It |
| 152 | * may return a warning or an error message in <errmsg> if the message is at |
| 153 | * most <errlen> bytes long (including '\0'). Note that <errmsg> may be NULL if |
| 154 | * <errlen> is also zero. |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 155 | */ |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 156 | static int uxst_bind_listener(struct listener *listener, char *errmsg, int errlen) |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 157 | { |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 158 | int fd; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 159 | char tempname[MAXPATHLEN]; |
| 160 | char backname[MAXPATHLEN]; |
| 161 | struct sockaddr_un addr; |
Willy Tarreau | b40dc94 | 2010-11-07 12:10:51 +0100 | [diff] [blame] | 162 | const char *msg = NULL; |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 163 | const char *path; |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 164 | int ext, ready; |
| 165 | socklen_t ready_len; |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 166 | |
| 167 | int ret; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 168 | |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 169 | /* ensure we never return garbage */ |
Willy Tarreau | 8ab505b | 2013-01-24 01:41:38 +0100 | [diff] [blame] | 170 | if (errlen) |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 171 | *errmsg = 0; |
| 172 | |
| 173 | if (listener->state != LI_ASSIGNED) |
| 174 | return ERR_NONE; /* already bound */ |
| 175 | |
| 176 | path = ((struct sockaddr_un *)&listener->addr)->sun_path; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 177 | |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 178 | /* if the listener already has an fd assigned, then we were offered the |
| 179 | * fd by an external process (most likely the parent), and we don't want |
| 180 | * to create a new socket. However we still want to set a few flags on |
| 181 | * the socket. |
| 182 | */ |
| 183 | fd = listener->fd; |
| 184 | ext = (fd >= 0); |
| 185 | if (ext) |
| 186 | goto fd_ready; |
| 187 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 188 | /* 1. create socket names */ |
| 189 | if (!path[0]) { |
Willy Tarreau | b40dc94 | 2010-11-07 12:10:51 +0100 | [diff] [blame] | 190 | msg = "Invalid empty name for a UNIX socket"; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 191 | goto err_return; |
| 192 | } |
| 193 | |
| 194 | ret = snprintf(tempname, MAXPATHLEN, "%s.%d.tmp", path, pid); |
| 195 | if (ret < 0 || ret >= MAXPATHLEN) { |
Willy Tarreau | b40dc94 | 2010-11-07 12:10:51 +0100 | [diff] [blame] | 196 | msg = "name too long for UNIX socket"; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 197 | goto err_return; |
| 198 | } |
| 199 | |
| 200 | ret = snprintf(backname, MAXPATHLEN, "%s.%d.bak", path, pid); |
| 201 | if (ret < 0 || ret >= MAXPATHLEN) { |
Willy Tarreau | b40dc94 | 2010-11-07 12:10:51 +0100 | [diff] [blame] | 202 | msg = "name too long for UNIX socket"; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 203 | goto err_return; |
| 204 | } |
| 205 | |
| 206 | /* 2. clean existing orphaned entries */ |
| 207 | if (unlink(tempname) < 0 && errno != ENOENT) { |
Willy Tarreau | b40dc94 | 2010-11-07 12:10:51 +0100 | [diff] [blame] | 208 | msg = "error when trying to unlink previous UNIX socket"; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 209 | goto err_return; |
| 210 | } |
| 211 | |
| 212 | if (unlink(backname) < 0 && errno != ENOENT) { |
Willy Tarreau | b40dc94 | 2010-11-07 12:10:51 +0100 | [diff] [blame] | 213 | msg = "error when trying to unlink previous UNIX socket"; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 214 | goto err_return; |
| 215 | } |
| 216 | |
| 217 | /* 3. backup existing socket */ |
| 218 | if (link(path, backname) < 0 && errno != ENOENT) { |
Willy Tarreau | b40dc94 | 2010-11-07 12:10:51 +0100 | [diff] [blame] | 219 | msg = "error when trying to preserve previous UNIX socket"; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 220 | goto err_return; |
| 221 | } |
| 222 | |
| 223 | /* 4. prepare new socket */ |
| 224 | addr.sun_family = AF_UNIX; |
| 225 | strncpy(addr.sun_path, tempname, sizeof(addr.sun_path)); |
| 226 | addr.sun_path[sizeof(addr.sun_path) - 1] = 0; |
| 227 | |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 228 | fd = socket(PF_UNIX, SOCK_STREAM, 0); |
| 229 | if (fd < 0) { |
Willy Tarreau | b40dc94 | 2010-11-07 12:10:51 +0100 | [diff] [blame] | 230 | msg = "cannot create UNIX socket"; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 231 | goto err_unlink_back; |
| 232 | } |
| 233 | |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 234 | fd_ready: |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 235 | if (fd >= global.maxsock) { |
Willy Tarreau | b40dc94 | 2010-11-07 12:10:51 +0100 | [diff] [blame] | 236 | msg = "socket(): not enough free sockets, raise -n argument"; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 237 | goto err_unlink_temp; |
| 238 | } |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 239 | |
| 240 | if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) { |
Willy Tarreau | b40dc94 | 2010-11-07 12:10:51 +0100 | [diff] [blame] | 241 | msg = "cannot make UNIX socket non-blocking"; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 242 | goto err_unlink_temp; |
| 243 | } |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 244 | |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 245 | if (!ext && bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) { |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 246 | /* note that bind() creates the socket <tempname> on the file system */ |
Willy Tarreau | b40dc94 | 2010-11-07 12:10:51 +0100 | [diff] [blame] | 247 | msg = "cannot bind UNIX socket"; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 248 | goto err_unlink_temp; |
| 249 | } |
| 250 | |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 251 | /* <uid> and <gid> different of -1 will be used to change the socket owner. |
| 252 | * If <mode> is not 0, it will be used to restrict access to the socket. |
| 253 | * While it is known not to be portable on every OS, it's still useful |
| 254 | * where it works. |
| 255 | */ |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 256 | if (!ext && |
| 257 | (((listener->bind_conf->ux.uid != -1 || listener->bind_conf->ux.gid != -1) && |
| 258 | (chown(tempname, listener->bind_conf->ux.uid, listener->bind_conf->ux.gid) == -1)) || |
| 259 | (listener->bind_conf->ux.mode != 0 && chmod(tempname, listener->bind_conf->ux.mode) == -1))) { |
Willy Tarreau | b40dc94 | 2010-11-07 12:10:51 +0100 | [diff] [blame] | 260 | msg = "cannot change UNIX socket ownership"; |
Willy Tarreau | e6ad2b1 | 2007-10-18 12:45:54 +0200 | [diff] [blame] | 261 | goto err_unlink_temp; |
| 262 | } |
| 263 | |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 264 | ready = 0; |
| 265 | ready_len = sizeof(ready); |
| 266 | if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1) |
| 267 | ready = 0; |
| 268 | |
| 269 | if (!(ext && ready) && /* only listen if not already done by external process */ |
| 270 | listen(fd, listener->backlog ? listener->backlog : listener->maxconn) < 0) { |
Willy Tarreau | b40dc94 | 2010-11-07 12:10:51 +0100 | [diff] [blame] | 271 | msg = "cannot listen to UNIX socket"; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 272 | goto err_unlink_temp; |
| 273 | } |
| 274 | |
| 275 | /* 5. install. |
| 276 | * Point of no return: we are ready, we'll switch the sockets. We don't |
| 277 | * fear loosing the socket <path> because we have a copy of it in |
| 278 | * backname. |
| 279 | */ |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 280 | if (!ext && rename(tempname, path) < 0) { |
Willy Tarreau | b40dc94 | 2010-11-07 12:10:51 +0100 | [diff] [blame] | 281 | msg = "cannot switch final and temporary UNIX sockets"; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 282 | goto err_rename; |
| 283 | } |
| 284 | |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 285 | /* 6. cleanup. If we're bound to an fd inherited from the parent, we |
| 286 | * want to ensure that destroy_uxst_socket() will never remove the |
| 287 | * path, and for this we simply clear the path to the socket. |
| 288 | */ |
| 289 | if (!ext) |
| 290 | unlink(backname); |
| 291 | else |
| 292 | ((struct sockaddr_un *)&listener->addr)->sun_path[0] = 0; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 293 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 294 | /* the socket is now listening */ |
| 295 | listener->fd = fd; |
| 296 | listener->state = LI_LISTEN; |
| 297 | |
| 298 | /* the function for the accept() event */ |
| 299 | fd_insert(fd); |
Willy Tarreau | aece46a | 2012-07-06 12:25:58 +0200 | [diff] [blame] | 300 | fdtab[fd].iocb = listener->proto->accept; |
Willy Tarreau | eabf313 | 2008-08-29 23:36:51 +0200 | [diff] [blame] | 301 | fdtab[fd].owner = listener; /* reference the listener instead of a task */ |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 302 | return ERR_NONE; |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 303 | err_rename: |
| 304 | ret = rename(backname, path); |
| 305 | if (ret < 0 && errno == ENOENT) |
| 306 | unlink(path); |
| 307 | err_unlink_temp: |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 308 | if (!ext) |
| 309 | unlink(tempname); |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 310 | close(fd); |
| 311 | err_unlink_back: |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 312 | if (!ext) |
| 313 | unlink(backname); |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 314 | err_return: |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 315 | if (msg && errlen) { |
| 316 | if (!ext) |
| 317 | snprintf(errmsg, errlen, "%s [%s]", msg, path); |
| 318 | else |
| 319 | snprintf(errmsg, errlen, "%s [fd %d]", msg, fd); |
| 320 | } |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 321 | return ERR_FATAL | ERR_ALERT; |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | /* This function closes the UNIX sockets for the specified listener. |
| 325 | * The listener enters the LI_ASSIGNED state. It always returns ERR_NONE. |
| 326 | */ |
| 327 | static int uxst_unbind_listener(struct listener *listener) |
| 328 | { |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 329 | if (listener->state > LI_ASSIGNED) { |
| 330 | unbind_listener(listener); |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 331 | destroy_uxst_socket(((struct sockaddr_un *)&listener->addr)->sun_path); |
| 332 | } |
| 333 | return ERR_NONE; |
| 334 | } |
| 335 | |
| 336 | /* Add a listener to the list of unix stream listeners. The listener's state |
| 337 | * is automatically updated from LI_INIT to LI_ASSIGNED. The number of |
| 338 | * listeners is updated. This is the function to use to add a new listener. |
| 339 | */ |
| 340 | void uxst_add_listener(struct listener *listener) |
| 341 | { |
| 342 | if (listener->state != LI_INIT) |
| 343 | return; |
| 344 | listener->state = LI_ASSIGNED; |
| 345 | listener->proto = &proto_unix; |
| 346 | LIST_ADDQ(&proto_unix.listeners, &listener->proto_list); |
| 347 | proto_unix.nb_listeners++; |
| 348 | } |
| 349 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 350 | /******************************** |
| 351 | * 3) protocol-oriented functions |
| 352 | ********************************/ |
| 353 | |
| 354 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 355 | /* This function creates all UNIX sockets bound to the protocol entry <proto>. |
| 356 | * It is intended to be used as the protocol's bind_all() function. |
| 357 | * The sockets will be registered but not added to any fd_set, in order not to |
| 358 | * loose them across the fork(). A call to uxst_enable_listeners() is needed |
| 359 | * to complete initialization. |
| 360 | * |
| 361 | * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL. |
| 362 | */ |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 363 | static int uxst_bind_listeners(struct protocol *proto, char *errmsg, int errlen) |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 364 | { |
| 365 | struct listener *listener; |
| 366 | int err = ERR_NONE; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 367 | |
| 368 | list_for_each_entry(listener, &proto->listeners, proto_list) { |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 369 | err |= uxst_bind_listener(listener, errmsg, errlen); |
| 370 | if (err & ERR_ABORT) |
| 371 | break; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 372 | } |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 373 | return err; |
| 374 | } |
| 375 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 376 | |
| 377 | /* This function stops all listening UNIX sockets bound to the protocol |
| 378 | * <proto>. It does not detaches them from the protocol. |
| 379 | * It always returns ERR_NONE. |
| 380 | */ |
| 381 | static int uxst_unbind_listeners(struct protocol *proto) |
| 382 | { |
| 383 | struct listener *listener; |
| 384 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 385 | list_for_each_entry(listener, &proto->listeners, proto_list) |
| 386 | uxst_unbind_listener(listener); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 387 | return ERR_NONE; |
| 388 | } |
| 389 | |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 390 | /* parse the "mode" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 391 | 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] | 392 | { |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 393 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 394 | memprintf(err, "'%s' : missing mode (octal integer expected)", args[cur_arg]); |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 395 | return ERR_ALERT | ERR_FATAL; |
| 396 | } |
| 397 | |
Willy Tarreau | 290e63a | 2012-09-20 18:07:14 +0200 | [diff] [blame] | 398 | conf->ux.mode = strtol(args[cur_arg + 1], NULL, 8); |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 399 | return 0; |
| 400 | } |
| 401 | |
| 402 | /* parse the "gid" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 403 | 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] | 404 | { |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 405 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 406 | memprintf(err, "'%s' : missing value", args[cur_arg]); |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 407 | return ERR_ALERT | ERR_FATAL; |
| 408 | } |
| 409 | |
Willy Tarreau | 290e63a | 2012-09-20 18:07:14 +0200 | [diff] [blame] | 410 | conf->ux.gid = atol(args[cur_arg + 1]); |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 411 | return 0; |
| 412 | } |
| 413 | |
| 414 | /* parse the "group" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 415 | 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] | 416 | { |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 417 | struct group *group; |
| 418 | |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 419 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 420 | memprintf(err, "'%s' : missing group name", args[cur_arg]); |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 421 | return ERR_ALERT | ERR_FATAL; |
| 422 | } |
| 423 | |
| 424 | group = getgrnam(args[cur_arg + 1]); |
| 425 | if (!group) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 426 | 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] | 427 | return ERR_ALERT | ERR_FATAL; |
| 428 | } |
| 429 | |
Willy Tarreau | 290e63a | 2012-09-20 18:07:14 +0200 | [diff] [blame] | 430 | conf->ux.gid = group->gr_gid; |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 431 | return 0; |
| 432 | } |
| 433 | |
| 434 | /* parse the "uid" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 435 | 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] | 436 | { |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 437 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 438 | memprintf(err, "'%s' : missing value", args[cur_arg]); |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 439 | return ERR_ALERT | ERR_FATAL; |
| 440 | } |
| 441 | |
Willy Tarreau | 290e63a | 2012-09-20 18:07:14 +0200 | [diff] [blame] | 442 | conf->ux.uid = atol(args[cur_arg + 1]); |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 443 | return 0; |
| 444 | } |
| 445 | |
| 446 | /* parse the "user" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 447 | 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] | 448 | { |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 449 | struct passwd *user; |
| 450 | |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 451 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 452 | memprintf(err, "'%s' : missing user name", args[cur_arg]); |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 453 | return ERR_ALERT | ERR_FATAL; |
| 454 | } |
| 455 | |
| 456 | user = getpwnam(args[cur_arg + 1]); |
| 457 | if (!user) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 458 | 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] | 459 | return ERR_ALERT | ERR_FATAL; |
| 460 | } |
| 461 | |
Willy Tarreau | 290e63a | 2012-09-20 18:07:14 +0200 | [diff] [blame] | 462 | conf->ux.uid = user->pw_uid; |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 463 | return 0; |
| 464 | } |
| 465 | |
| 466 | /* Note: must not be declared <const> as its list will be overwritten. |
| 467 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 468 | * all code contributors. |
| 469 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 470 | * the config parser can report an appropriate error when a known keyword was |
| 471 | * not enabled. |
| 472 | */ |
Willy Tarreau | 51fb765 | 2012-09-18 18:24:39 +0200 | [diff] [blame] | 473 | static struct bind_kw_list bind_kws = { "UNIX", { }, { |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 474 | { "gid", bind_parse_gid, 1 }, /* set the socket's gid */ |
| 475 | { "group", bind_parse_group, 1 }, /* set the socket's gid from the group name */ |
| 476 | { "mode", bind_parse_mode, 1 }, /* set the socket's mode (eg: 0644)*/ |
| 477 | { "uid", bind_parse_uid, 1 }, /* set the socket's uid */ |
| 478 | { "user", bind_parse_user, 1 }, /* set the socket's uid from the user name */ |
| 479 | { NULL, NULL, 0 }, |
| 480 | }}; |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 481 | |
| 482 | /******************************** |
| 483 | * 4) high-level functions |
| 484 | ********************************/ |
| 485 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 486 | __attribute__((constructor)) |
| 487 | static void __uxst_protocol_init(void) |
| 488 | { |
| 489 | protocol_register(&proto_unix); |
Willy Tarreau | d0a895d | 2012-09-18 17:40:35 +0200 | [diff] [blame] | 490 | bind_register_keywords(&bind_kws); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | |
| 494 | /* |
| 495 | * Local variables: |
| 496 | * c-indent-level: 8 |
| 497 | * c-basic-offset: 8 |
| 498 | * End: |
| 499 | */ |