2004-01-28 20:02:12 -05:00
|
|
|
/* Icecast
|
|
|
|
*
|
|
|
|
* This program is distributed under the GNU General Public License, version 2.
|
|
|
|
* A copy of this license is included with this source.
|
|
|
|
*
|
|
|
|
* Copyright 2000-2004, Jack Moffitt <jack@xiph.org,
|
|
|
|
* Michael Smith <msmith@xiph.org>,
|
|
|
|
* oddsock <oddsock@xiph.org>,
|
|
|
|
* Karl Heyes <karl@xiph.org>
|
|
|
|
* and others (see AUTHORS for details).
|
|
|
|
*/
|
|
|
|
|
2001-09-09 22:21:46 -04:00
|
|
|
/* client.c
|
|
|
|
**
|
|
|
|
** client interface implementation
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
|
2003-07-20 21:58:54 -04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2001-09-09 22:21:46 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2003-07-16 15:41:59 -04:00
|
|
|
#include "thread/thread.h"
|
|
|
|
#include "avl/avl.h"
|
|
|
|
#include "httpp/httpp.h"
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2005-05-06 11:57:15 -04:00
|
|
|
#include "cfgfile.h"
|
2001-09-09 22:21:46 -04:00
|
|
|
#include "connection.h"
|
|
|
|
#include "refbuf.h"
|
2005-06-03 11:35:52 -04:00
|
|
|
#include "format.h"
|
2005-05-06 11:57:15 -04:00
|
|
|
#include "stats.h"
|
2005-08-12 11:27:32 -04:00
|
|
|
#include "fserve.h"
|
2001-09-09 22:21:46 -04:00
|
|
|
|
|
|
|
#include "client.h"
|
|
|
|
#include "logging.h"
|
2007-11-20 21:55:11 -05:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#define snprintf _snprintf
|
|
|
|
#endif
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2004-07-16 11:47:12 -04:00
|
|
|
#undef CATMODULE
|
|
|
|
#define CATMODULE "client"
|
|
|
|
|
2005-08-24 20:07:17 -04:00
|
|
|
/* create a client_t with the provided connection and parser details. Return
|
|
|
|
* 0 on success, -1 if server limit has been reached. In either case a
|
|
|
|
* client_t is returned just in case a message needs to be returned. Should
|
|
|
|
* be called with global lock held.
|
|
|
|
*/
|
2005-08-11 19:29:58 -04:00
|
|
|
int client_create (client_t **c_ptr, connection_t *con, http_parser_t *parser)
|
2001-09-09 22:21:46 -04:00
|
|
|
{
|
2005-08-11 19:29:58 -04:00
|
|
|
ice_config_t *config;
|
2003-03-14 21:10:19 -05:00
|
|
|
client_t *client = (client_t *)calloc(1, sizeof(client_t));
|
2005-08-11 19:29:58 -04:00
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
if (client == NULL)
|
2005-08-24 20:07:17 -04:00
|
|
|
abort();
|
2005-08-11 19:29:58 -04:00
|
|
|
|
|
|
|
config = config_get_config ();
|
2005-05-06 11:57:15 -04:00
|
|
|
|
|
|
|
global.clients++;
|
2005-08-11 19:29:58 -04:00
|
|
|
if (config->client_limit < global.clients)
|
|
|
|
WARN2 ("server client limit reached (%d/%d)", config->client_limit, global.clients);
|
|
|
|
else
|
|
|
|
ret = 0;
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2005-08-11 19:29:58 -04:00
|
|
|
config_release_config ();
|
|
|
|
|
|
|
|
stats_event_args (NULL, "clients", "%d", global.clients);
|
2003-03-14 21:10:19 -05:00
|
|
|
client->con = con;
|
|
|
|
client->parser = parser;
|
2005-08-24 20:07:17 -04:00
|
|
|
client->refbuf = refbuf_new (PER_CLIENT_REFBUF_SIZE);
|
|
|
|
client->refbuf->len = 0; /* force reader code to ignore buffer contents */
|
2003-03-14 21:10:19 -05:00
|
|
|
client->pos = 0;
|
2005-06-07 21:36:51 -04:00
|
|
|
client->write_to_client = format_generic_write_to_client;
|
2005-08-11 19:29:58 -04:00
|
|
|
*c_ptr = client;
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2005-08-11 19:29:58 -04:00
|
|
|
return ret;
|
2001-09-09 22:21:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void client_destroy(client_t *client)
|
|
|
|
{
|
2004-02-02 19:48:02 -05:00
|
|
|
if (client == NULL)
|
|
|
|
return;
|
2005-08-07 19:01:04 -04:00
|
|
|
|
2005-10-05 22:41:51 -04:00
|
|
|
/* release the buffer now, as the buffer could be on the source queue
|
|
|
|
* and may of disappeared after auth completes */
|
|
|
|
if (client->refbuf)
|
|
|
|
{
|
|
|
|
refbuf_release (client->refbuf);
|
|
|
|
client->refbuf = NULL;
|
|
|
|
}
|
|
|
|
|
2007-08-21 18:30:30 -04:00
|
|
|
if (auth_release_listener (client))
|
2005-08-07 19:01:04 -04:00
|
|
|
return;
|
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
/* write log entry if ip is set (some things don't set it, like outgoing
|
2003-02-14 06:44:08 -05:00
|
|
|
* slave requests
|
|
|
|
*/
|
2005-08-08 22:14:20 -04:00
|
|
|
if (client->respcode && client->parser)
|
2003-03-14 21:10:19 -05:00
|
|
|
logging_access(client);
|
2007-08-28 23:51:22 -04:00
|
|
|
|
2005-06-10 11:42:06 -04:00
|
|
|
if (client->con)
|
|
|
|
connection_close(client->con);
|
2005-08-08 22:14:20 -04:00
|
|
|
if (client->parser)
|
|
|
|
httpp_destroy(client->parser);
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2005-05-06 11:57:15 -04:00
|
|
|
global_lock ();
|
|
|
|
global.clients--;
|
|
|
|
stats_event_args (NULL, "clients", "%d", global.clients);
|
|
|
|
global_unlock ();
|
|
|
|
|
2004-02-29 09:38:15 -05:00
|
|
|
/* we need to free client specific format data (if any) */
|
|
|
|
if (client->free_client_data)
|
|
|
|
client->free_client_data (client);
|
|
|
|
|
2004-01-14 20:01:09 -05:00
|
|
|
free(client->username);
|
2005-08-08 22:14:20 -04:00
|
|
|
free(client->password);
|
2004-01-14 20:01:09 -05:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
free(client);
|
2001-09-09 22:21:46 -04:00
|
|
|
}
|
2002-08-12 10:48:31 -04:00
|
|
|
|
2009-01-13 20:18:22 -05:00
|
|
|
/* return -1 for failed, 0 for authenticated, 1 for pending
|
|
|
|
*/
|
|
|
|
int client_check_source_auth (client_t *client, const char *mount)
|
|
|
|
{
|
|
|
|
ice_config_t *config = config_get_config();
|
|
|
|
char *pass = config->source_password;
|
|
|
|
char *user = "source";
|
|
|
|
int ret = -1;
|
|
|
|
mount_proxy *mountinfo = config_find_mount (config, mount);
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (mountinfo)
|
|
|
|
{
|
|
|
|
ret = 1;
|
|
|
|
if (auth_stream_authenticate (client, mount, mountinfo) > 0)
|
|
|
|
break;
|
|
|
|
ret = -1;
|
|
|
|
if (mountinfo->password)
|
|
|
|
pass = mountinfo->password;
|
|
|
|
if (mountinfo->username)
|
|
|
|
user = mountinfo->username;
|
|
|
|
}
|
|
|
|
if (connection_check_pass (client->parser, user, pass) > 0)
|
|
|
|
ret = 0;
|
|
|
|
} while (0);
|
|
|
|
config_release_config();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2005-05-08 09:51:05 -04:00
|
|
|
|
|
|
|
/* helper function for reading data from a client */
|
|
|
|
int client_read_bytes (client_t *client, void *buf, unsigned len)
|
|
|
|
{
|
2005-08-11 19:29:58 -04:00
|
|
|
int bytes;
|
2007-08-28 23:51:22 -04:00
|
|
|
|
2005-08-11 19:29:58 -04:00
|
|
|
if (client->refbuf && client->refbuf->len)
|
|
|
|
{
|
|
|
|
/* we have data to read from a refbuf first */
|
|
|
|
if (client->refbuf->len < len)
|
|
|
|
len = client->refbuf->len;
|
|
|
|
memcpy (buf, client->refbuf->data, len);
|
2005-08-18 16:37:35 -04:00
|
|
|
if (len < client->refbuf->len)
|
2005-08-11 19:29:58 -04:00
|
|
|
{
|
|
|
|
char *ptr = client->refbuf->data;
|
|
|
|
memmove (ptr, ptr+len, client->refbuf->len - len);
|
|
|
|
}
|
|
|
|
client->refbuf->len -= len;
|
|
|
|
return len;
|
|
|
|
}
|
2007-08-28 23:51:22 -04:00
|
|
|
bytes = client->con->read (client->con, buf, len);
|
2005-05-08 09:51:05 -04:00
|
|
|
|
2007-08-28 23:51:22 -04:00
|
|
|
if (bytes == -1 && client->con->error)
|
|
|
|
DEBUG0 ("reading from connection has failed");
|
|
|
|
|
|
|
|
return bytes;
|
2005-05-08 09:51:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-12-31 01:28:39 -05:00
|
|
|
void client_send_400(client_t *client, char *message) {
|
2012-07-17 19:55:09 -04:00
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
ret = util_http_build_header(client->refbuf->data, PER_CLIENT_REFBUF_SIZE, 0,
|
|
|
|
0, 400, NULL,
|
|
|
|
"text/html", NULL,
|
|
|
|
"");
|
|
|
|
|
|
|
|
snprintf(client->refbuf->data + ret, PER_CLIENT_REFBUF_SIZE - ret,
|
|
|
|
"<b>%s</b>\r\n", message);
|
|
|
|
|
2004-04-20 03:05:07 -04:00
|
|
|
client->respcode = 400;
|
2005-08-12 11:27:32 -04:00
|
|
|
client->refbuf->len = strlen (client->refbuf->data);
|
|
|
|
fserve_add_client (client, NULL);
|
2002-12-31 01:28:39 -05:00
|
|
|
}
|
|
|
|
|
2002-08-12 10:48:31 -04:00
|
|
|
void client_send_404(client_t *client, char *message) {
|
2012-07-17 19:55:09 -04:00
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
ret = util_http_build_header(client->refbuf->data, PER_CLIENT_REFBUF_SIZE, 0,
|
|
|
|
0, 404, NULL,
|
|
|
|
"text/html", NULL,
|
|
|
|
"");
|
|
|
|
|
|
|
|
snprintf(client->refbuf->data + ret, PER_CLIENT_REFBUF_SIZE - ret,
|
|
|
|
"<b>%s</b>\r\n", message);
|
2002-08-12 10:48:31 -04:00
|
|
|
|
2002-08-16 11:28:46 -04:00
|
|
|
client->respcode = 404;
|
2005-08-12 11:27:32 -04:00
|
|
|
client->refbuf->len = strlen (client->refbuf->data);
|
|
|
|
fserve_add_client (client, NULL);
|
2002-08-12 10:48:31 -04:00
|
|
|
}
|
|
|
|
|
2003-03-02 05:13:59 -05:00
|
|
|
|
2002-08-16 10:26:48 -04:00
|
|
|
void client_send_401(client_t *client) {
|
2012-07-17 19:55:09 -04:00
|
|
|
util_http_build_header(client->refbuf->data, PER_CLIENT_REFBUF_SIZE, 0,
|
|
|
|
0, 401, NULL,
|
|
|
|
"text/plain", NULL,
|
|
|
|
"You need to authenticate\r\n");
|
2002-08-16 11:28:46 -04:00
|
|
|
client->respcode = 401;
|
2005-08-12 11:27:32 -04:00
|
|
|
client->refbuf->len = strlen (client->refbuf->data);
|
|
|
|
fserve_add_client (client, NULL);
|
2002-08-16 10:26:48 -04:00
|
|
|
}
|
2004-05-17 00:33:46 -04:00
|
|
|
|
2005-11-16 19:54:28 -05:00
|
|
|
void client_send_403(client_t *client, const char *reason)
|
|
|
|
{
|
2012-07-17 19:55:09 -04:00
|
|
|
util_http_build_header(client->refbuf->data, PER_CLIENT_REFBUF_SIZE, 0,
|
|
|
|
0, 403, reason,
|
|
|
|
"text/plain", NULL,
|
|
|
|
"Forbidden");
|
2004-05-17 00:33:46 -04:00
|
|
|
client->respcode = 403;
|
2005-11-16 19:54:28 -05:00
|
|
|
client->refbuf->len = strlen (client->refbuf->data);
|
|
|
|
fserve_add_client (client, NULL);
|
2004-05-17 00:33:46 -04:00
|
|
|
}
|
2004-07-16 11:47:12 -04:00
|
|
|
|
|
|
|
|
|
|
|
/* helper function for sending the data to a client */
|
|
|
|
int client_send_bytes (client_t *client, const void *buf, unsigned len)
|
|
|
|
{
|
2007-08-28 23:51:22 -04:00
|
|
|
int ret = client->con->send (client->con, buf, len);
|
|
|
|
|
|
|
|
if (client->con->error)
|
2004-07-16 11:47:12 -04:00
|
|
|
DEBUG0 ("Client connection died");
|
2007-08-28 23:51:22 -04:00
|
|
|
|
2004-07-16 11:47:12 -04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2004-08-20 11:13:59 -04:00
|
|
|
void client_set_queue (client_t *client, refbuf_t *refbuf)
|
|
|
|
{
|
|
|
|
refbuf_t *to_release = client->refbuf;
|
|
|
|
|
|
|
|
client->refbuf = refbuf;
|
2004-08-23 15:01:18 -04:00
|
|
|
if (refbuf)
|
|
|
|
refbuf_addref (client->refbuf);
|
2004-08-20 11:13:59 -04:00
|
|
|
client->pos = 0;
|
|
|
|
if (to_release)
|
|
|
|
refbuf_release (to_release);
|
|
|
|
}
|
|
|
|
|