config.c 14.2 KB
Newer Older
Jack Moffitt's avatar
Jack Moffitt committed
1 2 3
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Michael Smith's avatar
Michael Smith committed
4 5
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
Jack Moffitt's avatar
Jack Moffitt committed
6 7 8 9 10 11 12 13 14
#include "config.h"

#define CONFIG_DEFAULT_LOCATION "Earth"
#define CONFIG_DEFAULT_ADMIN "icemaster@localhost"
#define CONFIG_DEFAULT_CLIENT_LIMIT 256
#define CONFIG_DEFAULT_SOURCE_LIMIT 16
#define CONFIG_DEFAULT_THREADPOOL_SIZE 4
#define CONFIG_DEFAULT_CLIENT_TIMEOUT 30
#define CONFIG_DEFAULT_HEADER_TIMEOUT 15
15
#define CONFIG_DEFAULT_SOURCE_TIMEOUT 10
Jack Moffitt's avatar
Jack Moffitt committed
16 17 18 19 20 21
#define CONFIG_DEFAULT_SOURCE_PASSWORD "changeme"
#define CONFIG_DEFAULT_TOUCH_FREQ 5
#define CONFIG_DEFAULT_HOSTNAME "localhost"
#define CONFIG_DEFAULT_PORT 8888
#define CONFIG_DEFAULT_ACCESS_LOG "access.log"
#define CONFIG_DEFAULT_ERROR_LOG "error.log"
22
#define CONFIG_DEFAULT_LOG_LEVEL 4
23 24 25 26
#define CONFIG_DEFAULT_CHROOT 0
#define CONFIG_DEFAULT_CHUID 0
#define CONFIG_DEFAULT_USER NULL
#define CONFIG_DEFAULT_GROUP NULL
27
#define CONFIG_MASTER_UPDATE_INTERVAL 120
Jack Moffitt's avatar
Jack Moffitt committed
28

29 30 31
#ifndef _WIN32
#define CONFIG_DEFAULT_BASE_DIR "/usr/local/icecast"
#define CONFIG_DEFAULT_LOG_DIR "/usr/local/icecast/logs"
32
#define CONFIG_DEFAULT_WEBROOT_DIR "/usr/local/icecast/webroot"
33 34 35
#else
#define CONFIG_DEFAULT_BASE_DIR ".\\"
#define CONFIG_DEFAULT_LOG_DIR ".\\logs"
36
#define CONFIG_DEFAULT_WEBROOT_DIR ".\\webroot"
37 38
#endif

Jack Moffitt's avatar
Jack Moffitt committed
39 40 41 42 43 44 45 46 47
ice_config_t _configuration;
char *_config_filename;

static void _set_defaults(void);
static void _parse_root(xmlDocPtr doc, xmlNodePtr node);
static void _parse_limits(xmlDocPtr doc, xmlNodePtr node);
static void _parse_directory(xmlDocPtr doc, xmlNodePtr node);
static void _parse_paths(xmlDocPtr doc, xmlNodePtr node);
static void _parse_logging(xmlDocPtr doc, xmlNodePtr node);
48
static void _parse_security(xmlDocPtr doc, xmlNodePtr node);
Jack Moffitt's avatar
Jack Moffitt committed
49 50 51 52 53 54 55 56 57 58 59
static void _add_server(xmlDocPtr doc, xmlNodePtr node);

void config_initialize(void)
{
	memset(&_configuration, 0, sizeof(ice_config_t));
	_set_defaults();
	_config_filename = NULL;
}

void config_shutdown(void)
{
60
	ice_config_dir_t *dirnode, *nextdirnode;
61
    ice_config_t *c = &_configuration;
Jack Moffitt's avatar
Jack Moffitt committed
62

63
	if (_config_filename) free(_config_filename);
64 65 66 67 68 69 70 71 72 73 74 75 76

	if (c->location && c->location != CONFIG_DEFAULT_LOCATION) 
        xmlFree(c->location);
	if (c->admin && c->admin != CONFIG_DEFAULT_ADMIN) 
        xmlFree(c->admin);
	if (c->source_password && c->source_password != CONFIG_DEFAULT_SOURCE_PASSWORD)
        xmlFree(c->source_password);
	if (c->hostname && c->hostname != CONFIG_DEFAULT_HOSTNAME) 
        xmlFree(c->hostname);
	if (c->base_dir && c->base_dir != CONFIG_DEFAULT_BASE_DIR) 
        xmlFree(c->base_dir);
	if (c->log_dir && c->log_dir != CONFIG_DEFAULT_LOG_DIR) 
        xmlFree(c->log_dir);
77 78
    if (c->webroot_dir && s->webroot_dir != CONFIG_DEFAULT_WEBROOT_DIR)
        xmlFree(c->webroot_dir);
79 80 81 82 83 84 85
	if (c->access_log && c->access_log != CONFIG_DEFAULT_ACCESS_LOG) 
        xmlFree(c->access_log);
	if (c->error_log && c->error_log != CONFIG_DEFAULT_ERROR_LOG) 
        xmlFree(c->error_log);
    if (c->bind_address) xmlFree(c->bind_address);
    if (c->user) xmlFree(c->user);
    if (c->group) xmlFree(c->group);
86 87 88
    dirnode = _configuration.dir_list;
    while(dirnode) {
        nextdirnode = dirnode->next;
89
        xmlFree(dirnode->host);
90 91 92
        free(dirnode);
        dirnode = nextdirnode;
    }
Jack Moffitt's avatar
Jack Moffitt committed
93

94
    memset(c, 0, sizeof(ice_config_t));
Jack Moffitt's avatar
Jack Moffitt committed
95 96 97 98 99 100 101 102 103 104 105 106 107
}

int config_parse_file(const char *filename)
{
	xmlDocPtr doc;
	xmlNodePtr node;

	if (filename == NULL || strcmp(filename, "") == 0) return CONFIG_EINSANE;
	
	_config_filename = (char *)strdup(filename);

	doc = xmlParseFile(_config_filename);
	if (doc == NULL) {
108
		return CONFIG_EPARSE;
Jack Moffitt's avatar
Jack Moffitt committed
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
	}

	node = xmlDocGetRootElement(doc);
	if (node == NULL) {
		xmlFreeDoc(doc);
		return CONFIG_ENOROOT;
	}

	if (strcmp(node->name, "icecast") != 0) {
		xmlFreeDoc(doc);
		return CONFIG_EBADROOT;
	}

	_parse_root(doc, node->xmlChildrenNode);

	xmlFreeDoc(doc);

	return 0;
}

int config_parse_cmdline(int arg, char **argv)
{
	return 0;
}

int config_rehash(void)
{
	return 0;
}

ice_config_t *config_get_config(void)
{
	return &_configuration;
}

static void _set_defaults(void)
{
146 147
	_configuration.location = CONFIG_DEFAULT_LOCATION;
	_configuration.admin = CONFIG_DEFAULT_ADMIN;
Jack Moffitt's avatar
Jack Moffitt committed
148 149 150 151 152
	_configuration.client_limit = CONFIG_DEFAULT_CLIENT_LIMIT;
	_configuration.source_limit = CONFIG_DEFAULT_SOURCE_LIMIT;
	_configuration.threadpool_size = CONFIG_DEFAULT_THREADPOOL_SIZE;
	_configuration.client_timeout = CONFIG_DEFAULT_CLIENT_TIMEOUT;
	_configuration.header_timeout = CONFIG_DEFAULT_HEADER_TIMEOUT;
153
	_configuration.source_timeout = CONFIG_DEFAULT_SOURCE_TIMEOUT;
154
	_configuration.source_password = CONFIG_DEFAULT_SOURCE_PASSWORD;
Jack Moffitt's avatar
Jack Moffitt committed
155 156
	_configuration.touch_freq = CONFIG_DEFAULT_TOUCH_FREQ;
	_configuration.dir_list = NULL;
157
	_configuration.hostname = CONFIG_DEFAULT_HOSTNAME;
Jack Moffitt's avatar
Jack Moffitt committed
158 159
	_configuration.port = CONFIG_DEFAULT_PORT;
	_configuration.bind_address = NULL;
160 161 162
	_configuration.master_server = NULL;
	_configuration.master_server_port = CONFIG_DEFAULT_PORT;
    _configuration.master_update_interval = CONFIG_MASTER_UPDATE_INTERVAL;
163 164
	_configuration.base_dir = CONFIG_DEFAULT_BASE_DIR;
	_configuration.log_dir = CONFIG_DEFAULT_LOG_DIR;
165
    _configuration.webroot_dir = CONFIG_DEFAULT_WEBROOT_DIR;
166 167
	_configuration.access_log = CONFIG_DEFAULT_ACCESS_LOG;
	_configuration.error_log = CONFIG_DEFAULT_ERROR_LOG;
168
	_configuration.loglevel = CONFIG_DEFAULT_LOG_LEVEL;
169 170 171 172
    _configuration.chroot = CONFIG_DEFAULT_CHROOT;
    _configuration.chuid = CONFIG_DEFAULT_CHUID;
    _configuration.user = CONFIG_DEFAULT_USER;
    _configuration.group = CONFIG_DEFAULT_GROUP;
Jack Moffitt's avatar
Jack Moffitt committed
173 174 175 176 177 178 179 180 181 182 183
}

static void _parse_root(xmlDocPtr doc, xmlNodePtr node)
{
	char *tmp;

	do {
		if (node == NULL) break;
		if (xmlIsBlankNode(node)) continue;

		if (strcmp(node->name, "location") == 0) {
184
			if (_configuration.location && _configuration.location != CONFIG_DEFAULT_LOCATION) xmlFree(_configuration.location);
Jack Moffitt's avatar
Jack Moffitt committed
185 186
			_configuration.location = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
		} else if (strcmp(node->name, "admin") == 0) {
187
			if (_configuration.admin && _configuration.admin != CONFIG_DEFAULT_ADMIN) xmlFree(_configuration.admin);
Jack Moffitt's avatar
Jack Moffitt committed
188 189
			_configuration.admin = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
		} else if (strcmp(node->name, "source-password") == 0) {
190 191 192 193 194 195
            char *mount, *pass;
            if ((mount = (char *)xmlGetProp(node, "mount")) != NULL) {
                pass = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
                /* FIXME: This is a placeholder for per-mount passwords */
            }
            else {
196
			    if (_configuration.source_password && _configuration.source_password != CONFIG_DEFAULT_SOURCE_PASSWORD) xmlFree(_configuration.source_password);
197 198
			    _configuration.source_password = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
            }
Jack Moffitt's avatar
Jack Moffitt committed
199
		} else if (strcmp(node->name, "hostname") == 0) {
200
			if (_configuration.hostname && _configuration.hostname != CONFIG_DEFAULT_HOSTNAME) xmlFree(_configuration.hostname);
Jack Moffitt's avatar
Jack Moffitt committed
201 202 203 204
			_configuration.hostname = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
		} else if (strcmp(node->name, "port") == 0) {
			tmp = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
			_configuration.port = atoi(tmp);
205
			if (tmp) xmlFree(tmp);
Jack Moffitt's avatar
Jack Moffitt committed
206
		} else if (strcmp(node->name, "bind-address") == 0) {
207
			if (_configuration.bind_address) xmlFree(_configuration.bind_address);
Jack Moffitt's avatar
Jack Moffitt committed
208
			_configuration.bind_address = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
209
		} else if (strcmp(node->name, "master-server") == 0) {
210
			if (_configuration.master_server) xmlFree(_configuration.master_server);
211 212 213 214 215 216 217
			_configuration.master_server = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
		} else if (strcmp(node->name, "master-server-port") == 0) {
			tmp = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
			_configuration.master_server_port = atoi(tmp);
        } else if (strcmp(node->name, "master-update-interval") == 0) {
            tmp = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
            _configuration.master_update_interval = atoi(tmp);
Jack Moffitt's avatar
Jack Moffitt committed
218 219 220 221 222 223 224 225
		} else if (strcmp(node->name, "limits") == 0) {
			_parse_limits(doc, node->xmlChildrenNode);
		} else if (strcmp(node->name, "directory") == 0) {
			_parse_directory(doc, node->xmlChildrenNode);
		} else if (strcmp(node->name, "paths") == 0) {
			_parse_paths(doc, node->xmlChildrenNode);
		} else if (strcmp(node->name, "logging") == 0) {
			_parse_logging(doc, node->xmlChildrenNode);
226 227
        } else if (strcmp(node->name, "security") == 0) {
            _parse_security(doc, node->xmlChildrenNode);
Jack Moffitt's avatar
Jack Moffitt committed
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
		}
	} while ((node = node->next));
}

static void _parse_limits(xmlDocPtr doc, xmlNodePtr node)
{
	char *tmp;

	do {
		if (node == NULL) break;
		if (xmlIsBlankNode(node)) continue;

		if (strcmp(node->name, "clients") == 0) {
			tmp = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
			_configuration.client_limit = atoi(tmp);
243
			if (tmp) xmlFree(tmp);
Jack Moffitt's avatar
Jack Moffitt committed
244 245 246
		} else if (strcmp(node->name, "sources") == 0) {
			tmp = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
			_configuration.source_limit = atoi(tmp);
247
			if (tmp) xmlFree(tmp);
Jack Moffitt's avatar
Jack Moffitt committed
248 249 250
		} else if (strcmp(node->name, "threadpool") == 0) {
			tmp = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
			_configuration.threadpool_size = atoi(tmp);
251
			if (tmp) xmlFree(tmp);
Jack Moffitt's avatar
Jack Moffitt committed
252 253 254
		} else if (strcmp(node->name, "client-timeout") == 0) {
			tmp = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
			_configuration.client_timeout = atoi(tmp);
255
			if (tmp) xmlFree(tmp);
Jack Moffitt's avatar
Jack Moffitt committed
256 257 258
		} else if (strcmp(node->name, "header-timeout") == 0) {
			tmp = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
			_configuration.header_timeout = atoi(tmp);
259
			if (tmp) xmlFree(tmp);
260 261 262
		} else if (strcmp(node->name, "source-timeout") == 0) {
			tmp = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
			_configuration.source_timeout = atoi(tmp);
263
			if (tmp) xmlFree(tmp);
Jack Moffitt's avatar
Jack Moffitt committed
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
		}
	} while ((node = node->next));
}

static void _parse_directory(xmlDocPtr doc, xmlNodePtr node)
{
	char *tmp;

	do {
		if (node == NULL) break;
		if (xmlIsBlankNode(node)) continue;

		if (strcmp(node->name, "server") == 0) {
			_add_server(doc, node->xmlChildrenNode);
		} else if (strcmp(node->name, "touch-freq") == 0) {
			tmp = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
			_configuration.touch_freq = atoi(tmp);
281
			if (tmp) xmlFree(tmp);
Jack Moffitt's avatar
Jack Moffitt committed
282 283 284 285 286 287 288 289 290 291 292
		}
	} while ((node = node->next));
}

static void _parse_paths(xmlDocPtr doc, xmlNodePtr node)
{
	do {
		if (node == NULL) break;
		if (xmlIsBlankNode(node)) continue;

		if (strcmp(node->name, "basedir") == 0) {
293
			if (_configuration.base_dir && _configuration.base_dir != CONFIG_DEFAULT_BASE_DIR) xmlFree(_configuration.base_dir);
Jack Moffitt's avatar
Jack Moffitt committed
294 295
			_configuration.base_dir = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
		} else if (strcmp(node->name, "logdir") == 0) {
296
			if (_configuration.log_dir && _configuration.log_dir != CONFIG_DEFAULT_LOG_DIR) xmlFree(_configuration.log_dir);
Jack Moffitt's avatar
Jack Moffitt committed
297
			_configuration.log_dir = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
298
		} else if (strcmp(node->name, "webroot") == 0) {
299
			if (_configuration.webroot_dir && _configuration.webroot_dir != CONFIG_DEFAULT_WEBROOT_DIR) xmlFree(_configuration.webroot_dir);
300
			_configuration.webroot_dir = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
Jack Moffitt's avatar
Jack Moffitt committed
301 302 303 304 305 306 307 308 309 310 311
		}
	} while ((node = node->next));
}

static void _parse_logging(xmlDocPtr doc, xmlNodePtr node)
{
	do {
		if (node == NULL) break;
		if (xmlIsBlankNode(node)) continue;

		if (strcmp(node->name, "accesslog") == 0) {
312
			if (_configuration.access_log && _configuration.access_log != CONFIG_DEFAULT_ACCESS_LOG) xmlFree(_configuration.access_log);
Jack Moffitt's avatar
Jack Moffitt committed
313 314
			_configuration.access_log = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
		} else if (strcmp(node->name, "errorlog") == 0) {
315
			if (_configuration.error_log && _configuration.error_log != CONFIG_DEFAULT_ERROR_LOG) xmlFree(_configuration.error_log);
Jack Moffitt's avatar
Jack Moffitt committed
316
			_configuration.error_log = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
317 318 319
		} else if (strcmp(node->name, "loglevel") == 0) {
           char *tmp = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
           _configuration.loglevel = atoi(tmp);
320
           if (tmp) xmlFree(tmp);
321
        }
Jack Moffitt's avatar
Jack Moffitt committed
322 323 324
	} while ((node = node->next));
}

325 326 327 328 329 330 331 332 333 334 335 336
static void _parse_security(xmlDocPtr doc, xmlNodePtr node)
{
   char *tmp;
   xmlNodePtr oldnode;

   do {
       if (node == NULL) break;
       if (xmlIsBlankNode(node)) continue;

       if (strcmp(node->name, "chroot") == 0) {
           tmp = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
           _configuration.chroot = atoi(tmp);
337
           if (tmp) xmlFree(tmp);
338 339 340 341 342 343 344 345
       } else if (strcmp(node->name, "changeowner") == 0) {
           _configuration.chuid = 1;
           oldnode = node;
           node = node->xmlChildrenNode;
           do {
               if(node == NULL) break;
               if(xmlIsBlankNode(node)) continue;
               if(strcmp(node->name, "user") == 0) {
346
                   if(_configuration.user) xmlFree(_configuration.user);
347 348
                   _configuration.user = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
               } else if(strcmp(node->name, "group") == 0) {
349
                   if(_configuration.group) xmlFree(_configuration.group);
350 351 352 353 354 355 356 357
                   _configuration.group = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
               }
           } while((node = node->next));
           node = oldnode;
       }
   } while ((node = node->next));
}

Jack Moffitt's avatar
Jack Moffitt committed
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
static void _add_server(xmlDocPtr doc, xmlNodePtr node)
{
	ice_config_dir_t *dirnode, *server;
	int addnode;
	char *tmp;

	server = (ice_config_dir_t *)malloc(sizeof(ice_config_dir_t));
	server->touch_freq = _configuration.touch_freq;
	server->host = NULL;
	addnode = 0;
	
	do {
		if (node == NULL) break;
		if (xmlIsBlankNode(node)) continue;

		if (strcmp(node->name, "host") == 0) {
			server->host = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
			addnode = 1;
		} else if (strcmp(node->name, "touch-freq") == 0) {
			tmp = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
			server->touch_freq = atoi(tmp);
379
			if (tmp) xmlFree(tmp);
Jack Moffitt's avatar
Jack Moffitt committed
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
		}
		server->next = NULL;
	} while ((node = node->next));

	if (addnode) {
		dirnode = _configuration.dir_list;
		if (dirnode == NULL) {
			_configuration.dir_list = server;
		} else {
			while (dirnode->next) dirnode = dirnode->next;
			
			dirnode->next = server;
		}
		
		server = NULL;
		addnode = 0;
	}
	
}