2004-02-24 00:41:20 +01:00
|
|
|
/* the Music Player Daemon (MPD)
|
2007-04-05 05:22:33 +02:00
|
|
|
* Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
|
2004-02-24 00:41:20 +01:00
|
|
|
* This project's homepage is: http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "listen.h"
|
2008-08-28 20:02:43 +02:00
|
|
|
#include "client.h"
|
2004-02-24 00:41:20 +01:00
|
|
|
#include "conf.h"
|
|
|
|
#include "log.h"
|
2004-05-15 23:19:43 +02:00
|
|
|
#include "utils.h"
|
2008-01-03 08:29:49 +01:00
|
|
|
#include "os_compat.h"
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-04-12 06:19:26 +02:00
|
|
|
#include "../config.h"
|
|
|
|
|
2004-02-24 00:41:20 +01:00
|
|
|
#define MAXHOSTNAME 1024
|
|
|
|
|
2005-03-06 20:00:58 +01:00
|
|
|
#define ALLOW_REUSE 1
|
|
|
|
|
|
|
|
#define DEFAULT_PORT 6600
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-22 15:15:20 +02:00
|
|
|
#define BINDERROR() do { \
|
2007-05-26 20:15:54 +02:00
|
|
|
FATAL("unable to bind port %u: %s\n" \
|
|
|
|
"maybe MPD is still running?\n", \
|
|
|
|
port, strerror(errno)); \
|
2006-07-22 15:15:20 +02:00
|
|
|
} while (0);
|
|
|
|
|
2007-01-14 04:07:53 +01:00
|
|
|
static int *listenSockets;
|
|
|
|
static int numberOfListenSockets;
|
2007-06-01 19:44:03 +02:00
|
|
|
int boundPort;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2007-09-05 03:19:37 +02:00
|
|
|
/*
|
|
|
|
* redirect stdin to /dev/null to work around a libao bug
|
|
|
|
* there are likely other bugs in other libraries (and even our code!)
|
|
|
|
* that check for fd > 0, so it's easiest to just keep
|
|
|
|
* fd = 0 == /dev/null for now...
|
|
|
|
*/
|
|
|
|
static void redirect_stdin(void)
|
|
|
|
{
|
|
|
|
int fd, st;
|
|
|
|
struct stat ss;
|
|
|
|
|
|
|
|
if ((st = fstat(STDIN_FILENO, &ss)) < 0) {
|
|
|
|
if ((fd = open("/dev/null", O_RDONLY) > 0)) {
|
|
|
|
DEBUG("stdin closed, and could not open /dev/null "
|
|
|
|
"as fd=0, some external library bugs "
|
|
|
|
"may be exposed...\n");
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!isatty(STDIN_FILENO))
|
|
|
|
return;
|
|
|
|
if ((fd = open("/dev/null", O_RDONLY)) < 0)
|
|
|
|
FATAL("failed to open /dev/null %s\n", strerror(errno));
|
|
|
|
if (dup2(fd, STDIN_FILENO) < 0)
|
|
|
|
FATAL("dup2 stdin: %s\n", strerror(errno));
|
|
|
|
}
|
|
|
|
|
2008-10-15 07:30:24 +02:00
|
|
|
static int establishListen(int pf, const struct sockaddr *addrp,
|
|
|
|
socklen_t addrlen)
|
2006-07-19 20:56:54 +02:00
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
int sock;
|
2006-07-19 20:56:54 +02:00
|
|
|
int allowReuse = ALLOW_REUSE;
|
2008-10-18 18:21:49 +02:00
|
|
|
#ifdef HAVE_STRUCT_UCRED
|
2008-10-15 22:34:21 +02:00
|
|
|
int passcred = 1;
|
2008-10-17 17:40:28 +02:00
|
|
|
#endif
|
2006-07-19 20:56:54 +02:00
|
|
|
|
2007-05-26 20:15:54 +02:00
|
|
|
if ((sock = socket(pf, SOCK_STREAM, 0)) < 0)
|
|
|
|
FATAL("socket < 0\n");
|
2006-07-19 20:56:54 +02:00
|
|
|
|
2008-03-26 11:39:03 +01:00
|
|
|
if (set_nonblocking(sock) < 0) {
|
2007-05-26 20:15:54 +02:00
|
|
|
FATAL("problems setting nonblocking on listen socket: %s\n",
|
2006-07-20 18:02:40 +02:00
|
|
|
strerror(errno));
|
2006-07-19 20:56:54 +02:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&allowReuse,
|
|
|
|
sizeof(allowReuse)) < 0) {
|
2007-05-26 20:15:54 +02:00
|
|
|
FATAL("problems setsockopt'ing: %s\n", strerror(errno));
|
2006-07-19 20:56:54 +02:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (bind(sock, addrp, addrlen) < 0) {
|
2006-07-22 15:15:20 +02:00
|
|
|
close(sock);
|
|
|
|
return -1;
|
2006-07-19 20:56:54 +02:00
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2007-05-26 20:15:54 +02:00
|
|
|
if (listen(sock, 5) < 0)
|
|
|
|
FATAL("problems listen'ing: %s\n", strerror(errno));
|
2006-07-19 20:56:54 +02:00
|
|
|
|
2008-10-18 18:21:49 +02:00
|
|
|
#ifdef HAVE_STRUCT_UCRED
|
2008-10-15 22:34:21 +02:00
|
|
|
setsockopt(sock, SOL_SOCKET, SO_PASSCRED, &passcred, sizeof(passcred));
|
|
|
|
#endif
|
|
|
|
|
2006-07-19 20:56:54 +02:00
|
|
|
numberOfListenSockets++;
|
2006-07-20 18:02:40 +02:00
|
|
|
listenSockets =
|
2006-08-26 08:25:57 +02:00
|
|
|
xrealloc(listenSockets, sizeof(int) * numberOfListenSockets);
|
2006-07-19 20:56:54 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
listenSockets[numberOfListenSockets - 1] = sock;
|
2006-07-22 15:15:20 +02:00
|
|
|
|
|
|
|
return 0;
|
2006-07-19 20:56:54 +02:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static void parseListenConfigParam(unsigned int port, ConfigParam * param)
|
|
|
|
{
|
2008-04-12 06:07:11 +02:00
|
|
|
const struct sockaddr *addrp;
|
2007-08-27 10:05:55 +02:00
|
|
|
socklen_t addrlen;
|
2008-04-12 06:07:24 +02:00
|
|
|
#ifdef HAVE_TCP
|
2008-02-05 11:20:46 +01:00
|
|
|
struct sockaddr_in sin4;
|
2004-02-24 00:41:20 +01:00
|
|
|
#ifdef HAVE_IPV6
|
|
|
|
struct sockaddr_in6 sin6;
|
2006-07-22 19:19:59 +02:00
|
|
|
int useIpv6 = ipv6Supported();
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
memset(&sin6, 0, sizeof(struct sockaddr_in6));
|
|
|
|
sin6.sin6_port = htons(port);
|
|
|
|
sin6.sin6_family = AF_INET6;
|
|
|
|
#endif
|
2008-02-05 11:20:46 +01:00
|
|
|
memset(&sin4, 0, sizeof(struct sockaddr_in));
|
|
|
|
sin4.sin_port = htons(port);
|
|
|
|
sin4.sin_family = AF_INET;
|
2008-04-12 06:07:24 +02:00
|
|
|
#endif /* HAVE_TCP */
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!param || 0 == strcmp(param->value, "any")) {
|
2008-04-12 06:07:24 +02:00
|
|
|
#ifdef HAVE_TCP
|
2004-11-03 20:42:54 +01:00
|
|
|
DEBUG("binding to any address\n");
|
2006-07-22 19:19:59 +02:00
|
|
|
#ifdef HAVE_IPV6
|
|
|
|
if (useIpv6) {
|
|
|
|
sin6.sin6_addr = in6addr_any;
|
2008-04-12 06:07:11 +02:00
|
|
|
addrp = (const struct sockaddr *)&sin6;
|
2006-07-22 19:19:59 +02:00
|
|
|
addrlen = sizeof(struct sockaddr_in6);
|
2008-10-15 07:30:24 +02:00
|
|
|
if (establishListen(PF_INET6, addrp, addrlen) < 0)
|
2006-07-22 19:19:59 +02:00
|
|
|
BINDERROR();
|
|
|
|
}
|
|
|
|
#endif
|
2008-02-05 11:20:46 +01:00
|
|
|
sin4.sin_addr.s_addr = INADDR_ANY;
|
2008-04-12 06:07:11 +02:00
|
|
|
addrp = (const struct sockaddr *)&sin4;
|
2006-07-22 15:15:20 +02:00
|
|
|
addrlen = sizeof(struct sockaddr_in);
|
2006-07-22 19:19:59 +02:00
|
|
|
#ifdef HAVE_IPV6
|
2008-10-15 07:30:24 +02:00
|
|
|
if ((establishListen(PF_INET, addrp, addrlen) < 0) && !useIpv6) {
|
2006-07-22 19:19:59 +02:00
|
|
|
#else
|
2008-10-15 07:30:24 +02:00
|
|
|
if (establishListen(PF_INET, addrp, addrlen) < 0) {
|
2006-07-22 19:19:59 +02:00
|
|
|
#endif
|
2006-07-22 15:15:20 +02:00
|
|
|
BINDERROR();
|
|
|
|
}
|
2008-04-12 06:07:24 +02:00
|
|
|
#else /* HAVE_TCP */
|
|
|
|
FATAL("TCP support is disabled\n");
|
|
|
|
#endif /* HAVE_TCP */
|
|
|
|
#ifdef HAVE_UN
|
2008-04-12 06:07:18 +02:00
|
|
|
} else if (param->value[0] == '/') {
|
|
|
|
size_t path_length;
|
2008-11-07 10:33:48 +01:00
|
|
|
struct sockaddr_un s_un;
|
2008-04-12 06:07:18 +02:00
|
|
|
|
|
|
|
path_length = strlen(param->value);
|
2008-11-07 10:33:48 +01:00
|
|
|
if (path_length >= sizeof(s_un.sun_path))
|
2008-04-12 06:07:18 +02:00
|
|
|
FATAL("unix socket path is too long\n");
|
|
|
|
|
2008-04-12 06:20:56 +02:00
|
|
|
unlink(param->value);
|
|
|
|
|
2008-11-07 10:33:48 +01:00
|
|
|
s_un.sun_family = AF_UNIX;
|
|
|
|
memcpy(s_un.sun_path, param->value, path_length + 1);
|
2008-04-12 06:07:18 +02:00
|
|
|
|
2008-11-07 10:33:48 +01:00
|
|
|
addrp = (const struct sockaddr *)&s_un;
|
|
|
|
addrlen = sizeof(s_un);
|
2008-04-12 06:07:18 +02:00
|
|
|
|
2008-10-15 07:30:24 +02:00
|
|
|
if (establishListen(PF_UNIX, addrp, addrlen) < 0)
|
2008-04-12 06:07:37 +02:00
|
|
|
FATAL("unable to bind to %s: %s\n",
|
|
|
|
param->value, strerror(errno));
|
2008-10-30 18:03:18 +01:00
|
|
|
|
|
|
|
/* allow everybody to connect */
|
|
|
|
chmod(param->value, 0666);
|
|
|
|
|
2008-04-12 06:07:24 +02:00
|
|
|
#endif /* HAVE_UN */
|
2006-07-20 18:02:40 +02:00
|
|
|
} else {
|
2008-04-12 06:07:24 +02:00
|
|
|
#ifdef HAVE_TCP
|
2008-10-15 07:20:53 +02:00
|
|
|
struct addrinfo hints, *ai, *i;
|
|
|
|
char service[20];
|
|
|
|
int ret;
|
|
|
|
|
2004-11-03 20:42:54 +01:00
|
|
|
DEBUG("binding to address for %s\n", param->value);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-10-15 07:20:53 +02:00
|
|
|
memset(&hints, 0, sizeof(hints));
|
2008-12-02 10:22:21 +01:00
|
|
|
hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
|
2008-10-15 07:20:53 +02:00
|
|
|
hints.ai_family = PF_UNSPEC;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
hints.ai_protocol = IPPROTO_TCP;
|
|
|
|
|
|
|
|
snprintf(service, sizeof(service), "%u", port);
|
|
|
|
|
|
|
|
ret = getaddrinfo(param->value, service, &hints, &ai);
|
|
|
|
if (ret != 0)
|
|
|
|
FATAL("can't lookup host \"%s\" at line %i: %s\n",
|
|
|
|
param->value, param->line, gai_strerror(ret));
|
|
|
|
|
|
|
|
for (i = ai; i != NULL; i = i->ai_next)
|
2008-10-15 07:30:24 +02:00
|
|
|
if (establishListen(i->ai_family, i->ai_addr,
|
|
|
|
i->ai_addrlen) < 0)
|
2008-10-15 07:20:53 +02:00
|
|
|
BINDERROR();
|
|
|
|
|
|
|
|
freeaddrinfo(ai);
|
2008-04-12 06:07:24 +02:00
|
|
|
#else /* HAVE_TCP */
|
|
|
|
FATAL("TCP support is disabled\n");
|
|
|
|
#endif /* HAVE_TCP */
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void listenOnPort(void)
|
|
|
|
{
|
2005-03-06 20:00:58 +01:00
|
|
|
int port = DEFAULT_PORT;
|
2006-07-20 18:02:40 +02:00
|
|
|
ConfigParam *param = getNextConfigParam(CONF_BIND_TO_ADDRESS, NULL);
|
2007-02-28 13:34:36 +01:00
|
|
|
ConfigParam *portParam = getConfigParam(CONF_PORT);
|
|
|
|
|
|
|
|
if (portParam) {
|
|
|
|
char *test;
|
|
|
|
port = strtol(portParam->value, &test, 10);
|
|
|
|
if (port <= 0 || *test != '\0') {
|
2007-05-26 20:15:54 +02:00
|
|
|
FATAL("%s \"%s\" specified at line %i is not a "
|
2007-02-28 13:34:36 +01:00
|
|
|
"positive integer", CONF_PORT,
|
|
|
|
portParam->value, portParam->line);
|
2005-03-06 20:00:58 +01:00
|
|
|
}
|
|
|
|
}
|
2004-11-03 20:42:54 +01:00
|
|
|
|
2007-01-11 21:41:17 +01:00
|
|
|
boundPort = port;
|
|
|
|
|
2007-09-05 03:19:37 +02:00
|
|
|
redirect_stdin();
|
2004-11-03 20:42:54 +01:00
|
|
|
do {
|
2006-07-20 18:02:40 +02:00
|
|
|
parseListenConfigParam(port, param);
|
2004-11-03 20:42:54 +01:00
|
|
|
} while ((param = getNextConfigParam(CONF_BIND_TO_ADDRESS, param)));
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void addListenSocketsToFdSet(fd_set * fds, int *fdmax)
|
|
|
|
{
|
2004-11-03 20:42:54 +01:00
|
|
|
int i;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
for (i = 0; i < numberOfListenSockets; i++) {
|
2004-11-03 20:42:54 +01:00
|
|
|
FD_SET(listenSockets[i], fds);
|
2006-07-20 18:02:40 +02:00
|
|
|
if (listenSockets[i] > *fdmax)
|
|
|
|
*fdmax = listenSockets[i];
|
2004-11-03 20:42:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void closeAllListenSockets(void)
|
|
|
|
{
|
2004-11-03 20:42:54 +01:00
|
|
|
int i;
|
|
|
|
|
|
|
|
DEBUG("closeAllListenSockets called\n");
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
for (i = 0; i < numberOfListenSockets; i++) {
|
2005-03-19 13:39:50 +01:00
|
|
|
DEBUG("closing listen socket %i\n", i);
|
2006-07-20 18:02:40 +02:00
|
|
|
while (close(listenSockets[i]) < 0 && errno == EINTR) ;
|
2004-11-03 20:42:54 +01:00
|
|
|
}
|
2005-11-16 15:43:04 +01:00
|
|
|
freeAllListenSockets();
|
|
|
|
}
|
2004-11-03 20:42:54 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void freeAllListenSockets(void)
|
|
|
|
{
|
2004-11-03 20:42:54 +01:00
|
|
|
numberOfListenSockets = 0;
|
|
|
|
free(listenSockets);
|
|
|
|
listenSockets = NULL;
|
|
|
|
}
|
|
|
|
|
2008-10-15 22:34:21 +02:00
|
|
|
static int get_remote_uid(int fd)
|
|
|
|
{
|
2008-10-18 18:21:49 +02:00
|
|
|
#ifdef HAVE_STRUCT_UCRED
|
2008-10-15 22:34:21 +02:00
|
|
|
struct ucred cred;
|
|
|
|
socklen_t len = sizeof (cred);
|
|
|
|
|
|
|
|
if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cred, &len) < 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return cred.uid;
|
|
|
|
#else
|
|
|
|
(void)fd;
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void getConnections(fd_set * fds)
|
|
|
|
{
|
2004-11-03 20:42:54 +01:00
|
|
|
int i;
|
2004-02-24 00:41:20 +01:00
|
|
|
int fd = 0;
|
|
|
|
struct sockaddr sockAddr;
|
|
|
|
socklen_t socklen = sizeof(sockAddr);
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
for (i = 0; i < numberOfListenSockets; i++) {
|
|
|
|
if (FD_ISSET(listenSockets[i], fds)) {
|
|
|
|
if ((fd = accept(listenSockets[i], &sockAddr, &socklen))
|
|
|
|
>= 0) {
|
2008-10-15 22:34:21 +02:00
|
|
|
client_new(fd, &sockAddr, get_remote_uid(fd));
|
2006-07-20 18:02:40 +02:00
|
|
|
} else if (fd < 0
|
|
|
|
&& (errno != EAGAIN && errno != EINTR)) {
|
|
|
|
ERROR("Problems accept()'ing\n");
|
2004-11-03 20:42:54 +01:00
|
|
|
}
|
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
}
|