1 /* Icecast
2 *
3 * This program is distributed under the GNU General Public License, version 2.
4 * A copy of this license is included with this source.
5 *
6 * Copyright 2000-2004, Jack Moffitt <jack@xiph.org,
7 * Michael Smith <msmith@xiph.org>,
8 * oddsock <oddsock@xiph.org>,
9 * Karl Heyes <karl@xiph.org>
10 * and others (see AUTHORS for details).
11 * Copyright 2011-2012, Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>,
12 */
13
14 /* client.c
15 **
16 ** client interface implementation
17 **
18 */
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "thread/thread.h"
28 #include "avl/avl.h"
29 #include "httpp/httpp.h"
30
31 #include "cfgfile.h"
32 #include "connection.h"
33 #include "refbuf.h"
34 #include "format.h"
35 #include "stats.h"
36 #include "fserve.h"
37
38 #include "client.h"
39 #include "logging.h"
40
41 #include "util.h"
42
43 #ifdef _WIN32
44 #define snprintf _snprintf
45 #endif
46
47 #undef CATMODULE
48 #define CATMODULE "client"
49
50 /* create a client_t with the provided connection and parser details. Return
51 * 0 on success, -1 if server limit has been reached. In either case a
52 * client_t is returned just in case a message needs to be returned. Should
53 * be called with global lock held.
54 */
client_create(client_t ** c_ptr,connection_t * con,http_parser_t * parser)55 int client_create (client_t **c_ptr, connection_t *con, http_parser_t *parser)
56 {
57 ice_config_t *config;
58 client_t *client = (client_t *)calloc(1, sizeof(client_t));
59 int ret = -1;
60
61 if (client == NULL)
62 abort();
63
64 config = config_get_config ();
65
66 global.clients++;
67 if (config->client_limit < global.clients)
68 ICECAST_LOG_WARN("server client limit reached (%d/%d)", config->client_limit, global.clients);
69 else
70 ret = 0;
71
72 config_release_config ();
73
74 stats_event_args (NULL, "clients", "%d", global.clients);
75 client->con = con;
76 client->parser = parser;
77 client->refbuf = refbuf_new (PER_CLIENT_REFBUF_SIZE);
78 client->refbuf->len = 0; /* force reader code to ignore buffer contents */
79 client->pos = 0;
80 client->write_to_client = format_generic_write_to_client;
81 *c_ptr = client;
82
83 return ret;
84 }
85
client_destroy(client_t * client)86 void client_destroy(client_t *client)
87 {
88 if (client == NULL)
89 return;
90
91 /* release the buffer now, as the buffer could be on the source queue
92 * and may of disappeared after auth completes */
93 if (client->refbuf)
94 {
95 refbuf_release (client->refbuf);
96 client->refbuf = NULL;
97 }
98
99 if (auth_release_listener (client))
100 return;
101
102 /* write log entry if ip is set (some things don't set it, like outgoing
103 * slave requests
104 */
105 if (client->respcode && client->parser)
106 logging_access(client);
107
108 if (client->con)
109 connection_close(client->con);
110 if (client->parser)
111 httpp_destroy(client->parser);
112
113 global_lock ();
114 global.clients--;
115 stats_event_args (NULL, "clients", "%d", global.clients);
116 global_unlock ();
117
118 /* we need to free client specific format data (if any) */
119 if (client->free_client_data)
120 client->free_client_data (client);
121
122 free(client->username);
123 free(client->password);
124
125 free(client);
126 }
127
128 /* return -1 for failed, 0 for authenticated, 1 for pending
129 */
client_check_source_auth(client_t * client,const char * mount)130 int client_check_source_auth (client_t *client, const char *mount)
131 {
132 ice_config_t *config = config_get_config();
133 char *pass = config->source_password;
134 char *user = "source";
135 int ret = -1;
136 mount_proxy *mountinfo = config_find_mount (config, mount, MOUNT_TYPE_NORMAL);
137
138 do
139 {
140 if (mountinfo)
141 {
142 ret = 1;
143 if (auth_stream_authenticate (client, mount, mountinfo) > 0)
144 break;
145 ret = -1;
146 if (mountinfo->password)
147 pass = mountinfo->password;
148 if (mountinfo->username)
149 user = mountinfo->username;
150 }
151 if (connection_check_pass (client->parser, user, pass) > 0)
152 ret = 0;
153 } while (0);
154 config_release_config();
155 return ret;
156 }
157
158
159 /* helper function for reading data from a client */
client_read_bytes(client_t * client,void * buf,unsigned len)160 int client_read_bytes (client_t *client, void *buf, unsigned len)
161 {
162 int bytes;
163
164 if (client->refbuf && client->refbuf->len)
165 {
166 /* we have data to read from a refbuf first */
167 if (client->refbuf->len < len)
168 len = client->refbuf->len;
169 memcpy (buf, client->refbuf->data, len);
170 if (len < client->refbuf->len)
171 {
172 char *ptr = client->refbuf->data;
173 memmove (ptr, ptr+len, client->refbuf->len - len);
174 }
175 client->refbuf->len -= len;
176 return len;
177 }
178 bytes = client->con->read (client->con, buf, len);
179
180 if (bytes == -1 && client->con->error)
181 ICECAST_LOG_DEBUG("reading from connection has failed");
182
183 return bytes;
184 }
185
client_send_error(client_t * client,int status,int plain,const char * message)186 void client_send_error(client_t *client, int status, int plain, const char *message)
187 {
188 ssize_t ret;
189
190 ret = util_http_build_header(client->refbuf->data, PER_CLIENT_REFBUF_SIZE, 0,
191 0, status, NULL,
192 plain ? "text/plain" : "text/html", "utf-8",
193 plain ? message : "", NULL);
194
195 if (ret == -1 || ret >= PER_CLIENT_REFBUF_SIZE) {
196 ICECAST_LOG_ERROR("Dropping client as we can not build response headers.");
197 client_send_500(client, "Header generation failed.");
198 return;
199 }
200
201 if (!plain)
202 snprintf(client->refbuf->data + ret, PER_CLIENT_REFBUF_SIZE - ret,
203 "<html><head><title>Error %i</title></head><body><b>%i - %s</b></body></html>\r\n",
204 status, status, message);
205
206 client->respcode = status;
207 client->refbuf->len = strlen (client->refbuf->data);
208 fserve_add_client (client, NULL);
209 }
210
client_send_100(client_t * client)211 void client_send_100(client_t *client)
212 {
213 /* On demand inject a HTTP/1.1 100 Continue to make sure clients are happy */
214 static const char str[] = "HTTP/1.1 100 Continue\r\nContent-Length: 0\r\n\r\n";
215 const size_t len = strlen(str);
216 client_send_bytes(client, str, len);
217 }
218
client_send_400(client_t * client,const char * message)219 void client_send_400(client_t *client, const char *message)
220 {
221 client_send_error(client, 400, 0, message);
222 }
223
client_send_404(client_t * client,const char * message)224 void client_send_404(client_t *client, const char *message)
225 {
226 client_send_error(client, 404, 0, message);
227 }
228
client_send_401(client_t * client)229 void client_send_401(client_t *client)
230 {
231 client_send_error(client, 401, 1, "You need to authenticate\r\n");
232 }
233
client_send_403(client_t * client,const char * message)234 void client_send_403(client_t *client, const char *message)
235 {
236 client_send_error(client, 403, 1, message);
237 }
238
239 /* this function is designed to work even if client is in bad state */
client_send_500(client_t * client,const char * message)240 void client_send_500(client_t *client, const char *message) {
241 const char header[] = "HTTP/1.0 500 Internal Server Error\r\nContent-Type: text/plain; charset=utf-8\r\n\r\n"
242 "500 - Internal Server Error\n---------------------------\n";
243 const size_t header_len = sizeof(header) - 1;
244 int ret;
245
246 ret = client_send_bytes(client, header, header_len);
247
248 /* only send message if we have one AND if header could have transmitted completly */
249 if (message && ret == header_len)
250 client_send_bytes(client, message, strlen(message));
251
252 client_destroy(client);
253 }
254
255 /* helper function for sending the data to a client */
client_send_bytes(client_t * client,const void * buf,unsigned len)256 int client_send_bytes (client_t *client, const void *buf, unsigned len)
257 {
258 int ret = client->con->send (client->con, buf, len);
259
260 if (client->con->error)
261 ICECAST_LOG_DEBUG("Client connection died");
262
263 return ret;
264 }
265
client_set_queue(client_t * client,refbuf_t * refbuf)266 void client_set_queue (client_t *client, refbuf_t *refbuf)
267 {
268 refbuf_t *to_release = client->refbuf;
269
270 client->refbuf = refbuf;
271 if (refbuf)
272 refbuf_addref (client->refbuf);
273 client->pos = 0;
274 if (to_release)
275 refbuf_release (to_release);
276 }
277
278