blob: 3ea468f45e6678f10631e64e2bd42b4801c389ff [file] [log] [blame]
Willy Tarreau92fb9832007-10-16 17:34:28 +02001/*
2 * UNIX SOCK_STREAM protocol layer (uxst)
3 *
Willy Tarreaueb472682010-05-28 18:46:57 +02004 * Copyright 2000-2010 Willy Tarreau <w@1wt.eu>
Willy Tarreau92fb9832007-10-16 17:34:28 +02005 *
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 Tarreaud0a895d2012-09-18 17:40:35 +020016#include <pwd.h>
17#include <grp.h>
Willy Tarreau92fb9832007-10-16 17:34:28 +020018#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21#include <syslog.h>
22#include <time.h>
23
Willy Tarreau92fb9832007-10-16 17:34:28 +020024#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 Tarreaud740bab2007-10-28 11:14:07 +010032#include <common/errors.h>
Willy Tarreau92fb9832007-10-16 17:34:28 +020033#include <common/mini-clist.h>
34#include <common/standard.h>
35#include <common/time.h>
36#include <common/version.h>
37
Willy Tarreau92fb9832007-10-16 17:34:28 +020038#include <types/global.h>
Willy Tarreau92fb9832007-10-16 17:34:28 +020039
Willy Tarreau92fb9832007-10-16 17:34:28 +020040#include <proto/fd.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020041#include <proto/listener.h>
Willy Tarreau92fb9832007-10-16 17:34:28 +020042#include <proto/log.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020043#include <proto/protocol.h>
Willy Tarreau92fb9832007-10-16 17:34:28 +020044#include <proto/proto_uxst.h>
Willy Tarreau92fb9832007-10-16 17:34:28 +020045#include <proto/task.h>
46
Emeric Bruncf20bf12010-10-22 16:06:11 +020047static int uxst_bind_listener(struct listener *listener, char *errmsg, int errlen);
48static int uxst_bind_listeners(struct protocol *proto, char *errmsg, int errlen);
Willy Tarreaudabf2e22007-10-28 21:59:24 +010049static int uxst_unbind_listeners(struct protocol *proto);
50
51/* Note: must not be declared <const> as its list will be overwritten */
52static 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 Tarreaubbebbbf2012-05-07 21:22:09 +020060 .accept = &listener_accept,
Emeric Bruncf20bf12010-10-22 16:06:11 +020061 .bind = uxst_bind_listener,
Willy Tarreaudabf2e22007-10-28 21:59:24 +010062 .bind_all = uxst_bind_listeners,
63 .unbind_all = uxst_unbind_listeners,
64 .enable_all = enable_all_listeners,
65 .disable_all = disable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +020066 .get_src = uxst_get_src,
67 .get_dst = uxst_get_dst,
Willy Tarreaudabf2e22007-10-28 21:59:24 +010068 .listeners = LIST_HEAD_INIT(proto_unix.listeners),
69 .nb_listeners = 0,
70};
71
Willy Tarreaudabf2e22007-10-28 21:59:24 +010072/********************************
73 * 1) low-level socket functions
74 ********************************/
75
Willy Tarreau59b94792012-05-11 16:16:40 +020076/*
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 */
82int 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 */
97int 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 Tarreaudabf2e22007-10-28 21:59:24 +0100105
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100106/* 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 */
109static void destroy_uxst_socket(const char *path)
110{
111 struct sockaddr_un addr;
112 int sock, ret;
113
114 /* We might have been chrooted, so we may not be able to access the
115 * socket. In order to avoid bothering the other end, we connect with a
116 * wrong protocol, namely SOCK_DGRAM. The return code from connect()
117 * is enough to know if the socket is still live or not. If it's live
118 * in mode SOCK_STREAM, we get EPROTOTYPE or anything else but not
119 * ECONNREFUSED. In this case, we do not touch it because it's used
120 * by some other process.
121 */
122 sock = socket(PF_UNIX, SOCK_DGRAM, 0);
123 if (sock < 0)
124 return;
125
126 addr.sun_family = AF_UNIX;
127 strncpy(addr.sun_path, path, sizeof(addr.sun_path));
128 addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
129 ret = connect(sock, (struct sockaddr *)&addr, sizeof(addr));
130 if (ret < 0 && errno == ECONNREFUSED) {
131 /* Connect failed: the socket still exists but is not used
132 * anymore. Let's remove this socket now.
133 */
134 unlink(path);
135 }
136 close(sock);
137}
138
139
140/********************************
141 * 2) listener-oriented functions
142 ********************************/
143
144
145/* This function creates a UNIX socket associated to the listener. It changes
146 * the state from ASSIGNED to LISTEN. The socket is NOT enabled for polling.
147 * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL.
Willy Tarreau92fb9832007-10-16 17:34:28 +0200148 */
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100149static int uxst_bind_listener(struct listener *listener, char *errmsg, int errlen)
Willy Tarreau92fb9832007-10-16 17:34:28 +0200150{
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100151 int fd;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200152 char tempname[MAXPATHLEN];
153 char backname[MAXPATHLEN];
154 struct sockaddr_un addr;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100155 const char *msg = NULL;
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100156 const char *path;
157
158 int ret;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200159
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100160 /* ensure we never return garbage */
161 if (errmsg && errlen)
162 *errmsg = 0;
163
164 if (listener->state != LI_ASSIGNED)
165 return ERR_NONE; /* already bound */
166
167 path = ((struct sockaddr_un *)&listener->addr)->sun_path;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200168
169 /* 1. create socket names */
170 if (!path[0]) {
Willy Tarreaub40dc942010-11-07 12:10:51 +0100171 msg = "Invalid empty name for a UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200172 goto err_return;
173 }
174
175 ret = snprintf(tempname, MAXPATHLEN, "%s.%d.tmp", path, pid);
176 if (ret < 0 || ret >= MAXPATHLEN) {
Willy Tarreaub40dc942010-11-07 12:10:51 +0100177 msg = "name too long for UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200178 goto err_return;
179 }
180
181 ret = snprintf(backname, MAXPATHLEN, "%s.%d.bak", path, pid);
182 if (ret < 0 || ret >= MAXPATHLEN) {
Willy Tarreaub40dc942010-11-07 12:10:51 +0100183 msg = "name too long for UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200184 goto err_return;
185 }
186
187 /* 2. clean existing orphaned entries */
188 if (unlink(tempname) < 0 && errno != ENOENT) {
Willy Tarreaub40dc942010-11-07 12:10:51 +0100189 msg = "error when trying to unlink previous UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200190 goto err_return;
191 }
192
193 if (unlink(backname) < 0 && errno != ENOENT) {
Willy Tarreaub40dc942010-11-07 12:10:51 +0100194 msg = "error when trying to unlink previous UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200195 goto err_return;
196 }
197
198 /* 3. backup existing socket */
199 if (link(path, backname) < 0 && errno != ENOENT) {
Willy Tarreaub40dc942010-11-07 12:10:51 +0100200 msg = "error when trying to preserve previous UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200201 goto err_return;
202 }
203
204 /* 4. prepare new socket */
205 addr.sun_family = AF_UNIX;
206 strncpy(addr.sun_path, tempname, sizeof(addr.sun_path));
207 addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
208
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100209 fd = socket(PF_UNIX, SOCK_STREAM, 0);
210 if (fd < 0) {
Willy Tarreaub40dc942010-11-07 12:10:51 +0100211 msg = "cannot create UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200212 goto err_unlink_back;
213 }
214
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100215 if (fd >= global.maxsock) {
Willy Tarreaub40dc942010-11-07 12:10:51 +0100216 msg = "socket(): not enough free sockets, raise -n argument";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200217 goto err_unlink_temp;
218 }
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100219
220 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaub40dc942010-11-07 12:10:51 +0100221 msg = "cannot make UNIX socket non-blocking";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200222 goto err_unlink_temp;
223 }
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100224
225 if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
Willy Tarreau92fb9832007-10-16 17:34:28 +0200226 /* note that bind() creates the socket <tempname> on the file system */
Willy Tarreaub40dc942010-11-07 12:10:51 +0100227 msg = "cannot bind UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200228 goto err_unlink_temp;
229 }
230
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100231 /* <uid> and <gid> different of -1 will be used to change the socket owner.
232 * If <mode> is not 0, it will be used to restrict access to the socket.
233 * While it is known not to be portable on every OS, it's still useful
234 * where it works.
235 */
236 if (((listener->perm.ux.uid != -1 || listener->perm.ux.gid != -1) &&
237 (chown(tempname, listener->perm.ux.uid, listener->perm.ux.gid) == -1)) ||
238 (listener->perm.ux.mode != 0 && chmod(tempname, listener->perm.ux.mode) == -1)) {
Willy Tarreaub40dc942010-11-07 12:10:51 +0100239 msg = "cannot change UNIX socket ownership";
Willy Tarreaue6ad2b12007-10-18 12:45:54 +0200240 goto err_unlink_temp;
241 }
242
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100243 if (listen(fd, listener->backlog ? listener->backlog : listener->maxconn) < 0) {
Willy Tarreaub40dc942010-11-07 12:10:51 +0100244 msg = "cannot listen to UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200245 goto err_unlink_temp;
246 }
247
248 /* 5. install.
249 * Point of no return: we are ready, we'll switch the sockets. We don't
250 * fear loosing the socket <path> because we have a copy of it in
251 * backname.
252 */
253 if (rename(tempname, path) < 0) {
Willy Tarreaub40dc942010-11-07 12:10:51 +0100254 msg = "cannot switch final and temporary UNIX sockets";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200255 goto err_rename;
256 }
257
258 /* 6. cleanup */
259 unlink(backname); /* no need to keep this one either */
260
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100261 /* the socket is now listening */
262 listener->fd = fd;
263 listener->state = LI_LISTEN;
264
265 /* the function for the accept() event */
266 fd_insert(fd);
Willy Tarreauaece46a2012-07-06 12:25:58 +0200267 fdtab[fd].iocb = listener->proto->accept;
Willy Tarreaueabf3132008-08-29 23:36:51 +0200268 fdtab[fd].owner = listener; /* reference the listener instead of a task */
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100269 return ERR_NONE;
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100270 err_rename:
271 ret = rename(backname, path);
272 if (ret < 0 && errno == ENOENT)
273 unlink(path);
274 err_unlink_temp:
275 unlink(tempname);
276 close(fd);
277 err_unlink_back:
278 unlink(backname);
279 err_return:
280 if (msg && errlen)
281 snprintf(errmsg, errlen, "%s [%s]", msg, path);
282 return ERR_FATAL | ERR_ALERT;
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100283}
284
285/* This function closes the UNIX sockets for the specified listener.
286 * The listener enters the LI_ASSIGNED state. It always returns ERR_NONE.
287 */
288static int uxst_unbind_listener(struct listener *listener)
289{
Willy Tarreaube58c382011-07-24 18:28:10 +0200290 if (listener->state > LI_ASSIGNED) {
291 unbind_listener(listener);
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100292 destroy_uxst_socket(((struct sockaddr_un *)&listener->addr)->sun_path);
293 }
294 return ERR_NONE;
295}
296
297/* Add a listener to the list of unix stream listeners. The listener's state
298 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
299 * listeners is updated. This is the function to use to add a new listener.
300 */
301void uxst_add_listener(struct listener *listener)
302{
303 if (listener->state != LI_INIT)
304 return;
305 listener->state = LI_ASSIGNED;
306 listener->proto = &proto_unix;
307 LIST_ADDQ(&proto_unix.listeners, &listener->proto_list);
308 proto_unix.nb_listeners++;
309}
310
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100311/********************************
312 * 3) protocol-oriented functions
313 ********************************/
314
315
Willy Tarreau92fb9832007-10-16 17:34:28 +0200316/* This function creates all UNIX sockets bound to the protocol entry <proto>.
317 * It is intended to be used as the protocol's bind_all() function.
318 * The sockets will be registered but not added to any fd_set, in order not to
319 * loose them across the fork(). A call to uxst_enable_listeners() is needed
320 * to complete initialization.
321 *
322 * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL.
323 */
Emeric Bruncf20bf12010-10-22 16:06:11 +0200324static int uxst_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreau92fb9832007-10-16 17:34:28 +0200325{
326 struct listener *listener;
327 int err = ERR_NONE;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200328
329 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200330 err |= uxst_bind_listener(listener, errmsg, errlen);
331 if (err & ERR_ABORT)
332 break;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200333 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200334 return err;
335}
336
Willy Tarreau92fb9832007-10-16 17:34:28 +0200337
338/* This function stops all listening UNIX sockets bound to the protocol
339 * <proto>. It does not detaches them from the protocol.
340 * It always returns ERR_NONE.
341 */
342static int uxst_unbind_listeners(struct protocol *proto)
343{
344 struct listener *listener;
345
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100346 list_for_each_entry(listener, &proto->listeners, proto_list)
347 uxst_unbind_listener(listener);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200348 return ERR_NONE;
349}
350
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200351/* parse the "mode" bind keyword */
352static int bind_parse_mode(char **args, int cur_arg, struct proxy *px, struct listener *last, char **err)
353{
354 struct listener *l;
355 int val;
356
357 if (px->listen->addr.ss_family != AF_UNIX) {
358 if (err)
359 memprintf(err, "'%s' option is only supported on unix sockets", args[cur_arg]);
360 return ERR_ALERT | ERR_FATAL;
361 }
362
363 if (!*args[cur_arg + 1]) {
364 if (err)
365 memprintf(err, "'%s' : missing mode (octal integer expected)", args[cur_arg]);
366 return ERR_ALERT | ERR_FATAL;
367 }
368
369 val = strtol(args[cur_arg + 1], NULL, 8);
370
371 for (l = px->listen; l != last; l = l->next)
372 l->perm.ux.mode = val;
373
374 return 0;
375}
376
377/* parse the "gid" bind keyword */
378static int bind_parse_gid(char **args, int cur_arg, struct proxy *px, struct listener *last, char **err)
379{
380 struct listener *l;
381 int val;
382
383 if (px->listen->addr.ss_family != AF_UNIX) {
384 if (err)
385 memprintf(err, "'%s' option is only supported on unix sockets", args[cur_arg]);
386 return ERR_ALERT | ERR_FATAL;
387 }
388
389 if (!*args[cur_arg + 1]) {
390 if (err)
391 memprintf(err, "'%s' : missing value", args[cur_arg]);
392 return ERR_ALERT | ERR_FATAL;
393 }
394
395 val = atol(args[cur_arg + 1]);
396 for (l = px->listen; l != last; l = l->next)
397 l->perm.ux.gid = val;
398
399 return 0;
400}
401
402/* parse the "group" bind keyword */
403static int bind_parse_group(char **args, int cur_arg, struct proxy *px, struct listener *last, char **err)
404{
405 struct listener *l;
406 struct group *group;
407
408 if (px->listen->addr.ss_family != AF_UNIX) {
409 if (err)
410 memprintf(err, "'%s' option is only supported on unix sockets", args[cur_arg]);
411 return ERR_ALERT | ERR_FATAL;
412 }
413
414 if (!*args[cur_arg + 1]) {
415 if (err)
416 memprintf(err, "'%s' : missing group name", args[cur_arg]);
417 return ERR_ALERT | ERR_FATAL;
418 }
419
420 group = getgrnam(args[cur_arg + 1]);
421 if (!group) {
422 if (err)
423 memprintf(err, "'%s' : unknown group name '%s'", args[cur_arg], args[cur_arg + 1]);
424 return ERR_ALERT | ERR_FATAL;
425 }
426
427 for (l = px->listen; l != last; l = l->next)
428 l->perm.ux.gid = group->gr_gid;
429
430 return 0;
431}
432
433/* parse the "uid" bind keyword */
434static int bind_parse_uid(char **args, int cur_arg, struct proxy *px, struct listener *last, char **err)
435{
436 struct listener *l;
437 int val;
438
439 if (px->listen->addr.ss_family != AF_UNIX) {
440 if (err)
441 memprintf(err, "'%s' option is only supported on unix sockets", args[cur_arg]);
442 return ERR_ALERT | ERR_FATAL;
443 }
444
445 if (!*args[cur_arg + 1]) {
446 if (err)
447 memprintf(err, "'%s' : missing value", args[cur_arg]);
448 return ERR_ALERT | ERR_FATAL;
449 }
450
451 val = atol(args[cur_arg + 1]);
452 for (l = px->listen; l != last; l = l->next)
453 l->perm.ux.uid = val;
454
455 return 0;
456}
457
458/* parse the "user" bind keyword */
459static int bind_parse_user(char **args, int cur_arg, struct proxy *px, struct listener *last, char **err)
460{
461 struct listener *l;
462 struct passwd *user;
463
464 if (px->listen->addr.ss_family != AF_UNIX) {
465 if (err)
466 memprintf(err, "'%s' option is only supported on unix sockets", args[cur_arg]);
467 return ERR_ALERT | ERR_FATAL;
468 }
469
470 if (!*args[cur_arg + 1]) {
471 if (err)
472 memprintf(err, "'%s' : missing user name", args[cur_arg]);
473 return ERR_ALERT | ERR_FATAL;
474 }
475
476 user = getpwnam(args[cur_arg + 1]);
477 if (!user) {
478 if (err)
479 memprintf(err, "'%s' : unknown user name '%s'", args[cur_arg], args[cur_arg + 1]);
480 return ERR_ALERT | ERR_FATAL;
481 }
482
483 for (l = px->listen; l != last; l = l->next)
484 l->perm.ux.uid = user->pw_uid;
485
486 return 0;
487}
488
489/* Note: must not be declared <const> as its list will be overwritten.
490 * Please take care of keeping this list alphabetically sorted, doing so helps
491 * all code contributors.
492 * Optional keywords are also declared with a NULL ->parse() function so that
493 * the config parser can report an appropriate error when a known keyword was
494 * not enabled.
495 */
496static struct bind_kw_list bind_kws = {{ },{
497 { "gid", bind_parse_gid, 1 }, /* set the socket's gid */
498 { "group", bind_parse_group, 1 }, /* set the socket's gid from the group name */
499 { "mode", bind_parse_mode, 1 }, /* set the socket's mode (eg: 0644)*/
500 { "uid", bind_parse_uid, 1 }, /* set the socket's uid */
501 { "user", bind_parse_user, 1 }, /* set the socket's uid from the user name */
502 { NULL, NULL, 0 },
503}};
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100504
505/********************************
506 * 4) high-level functions
507 ********************************/
508
Willy Tarreau92fb9832007-10-16 17:34:28 +0200509__attribute__((constructor))
510static void __uxst_protocol_init(void)
511{
512 protocol_register(&proto_unix);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200513 bind_register_keywords(&bind_kws);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200514}
515
516
517/*
518 * Local variables:
519 * c-indent-level: 8
520 * c-basic-offset: 8
521 * End:
522 */