config.c 13.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 23 24 25
#define CONFIG_DEFAULT_CHROOT 0
#define CONFIG_DEFAULT_CHUID 0
#define CONFIG_DEFAULT_USER NULL
#define CONFIG_DEFAULT_GROUP NULL
26
#define CONFIG_MASTER_UPDATE_INTERVAL 120
Jack Moffitt's avatar
Jack Moffitt committed
27

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

Jack Moffitt's avatar
Jack Moffitt committed
38 39 40 41 42 43 44 45 46
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);
47
static void _parse_security(xmlDocPtr doc, xmlNodePtr node);
Jack Moffitt's avatar
Jack Moffitt committed
48 49 50 51 52 53 54 55 56 57 58
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)
{
59
	ice_config_dir_t *dirnode, *nextdirnode;
Jack Moffitt's avatar
Jack Moffitt committed
60

61
	if (_config_filename) free(_config_filename);
Jack Moffitt's avatar
Jack Moffitt committed
62 63 64 65 66 67 68 69
	if (_configuration.location) free(_configuration.location);
	if (_configuration.admin) free(_configuration.admin);
	if (_configuration.source_password) free(_configuration.source_password);
	if (_configuration.hostname) free(_configuration.hostname);
	if (_configuration.base_dir) free(_configuration.base_dir);
	if (_configuration.log_dir) free(_configuration.log_dir);
	if (_configuration.access_log) free(_configuration.access_log);
	if (_configuration.error_log) free(_configuration.error_log);
70
    if (_configuration.bind_address) free(_configuration.bind_address);
71 72
    if (_configuration.user) free(_configuration.user);
    if (_configuration.group) free(_configuration.group);
73 74 75 76 77 78 79
    dirnode = _configuration.dir_list;
    while(dirnode) {
        nextdirnode = dirnode->next;
        free(dirnode->host);
        free(dirnode);
        dirnode = nextdirnode;
    }
Jack Moffitt's avatar
Jack Moffitt committed
80

81
    memset(&_configuration, 0, sizeof(ice_config_t));
Jack Moffitt's avatar
Jack Moffitt committed
82 83 84 85 86 87 88 89 90 91 92 93 94
}

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) {
95
		return CONFIG_EPARSE;
Jack Moffitt's avatar
Jack Moffitt committed
96 97 98 99 100 101 102 103 104 105 106 107 108 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
	}

	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)
{
	_configuration.location = (char *)strdup(CONFIG_DEFAULT_LOCATION);
	_configuration.admin = (char *)strdup(CONFIG_DEFAULT_ADMIN);
	_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;
140
	_configuration.source_timeout = CONFIG_DEFAULT_SOURCE_TIMEOUT;
Jack Moffitt's avatar
Jack Moffitt committed
141 142 143 144 145 146
	_configuration.source_password = (char *)strdup(CONFIG_DEFAULT_SOURCE_PASSWORD);
	_configuration.touch_freq = CONFIG_DEFAULT_TOUCH_FREQ;
	_configuration.dir_list = NULL;
	_configuration.hostname = (char *)strdup(CONFIG_DEFAULT_HOSTNAME);
	_configuration.port = CONFIG_DEFAULT_PORT;
	_configuration.bind_address = NULL;
147 148 149
	_configuration.master_server = NULL;
	_configuration.master_server_port = CONFIG_DEFAULT_PORT;
    _configuration.master_update_interval = CONFIG_MASTER_UPDATE_INTERVAL;
Jack Moffitt's avatar
Jack Moffitt committed
150 151 152 153
	_configuration.base_dir = (char *)strdup(CONFIG_DEFAULT_BASE_DIR);
	_configuration.log_dir = (char *)strdup(CONFIG_DEFAULT_LOG_DIR);
	_configuration.access_log = (char *)strdup(CONFIG_DEFAULT_ACCESS_LOG);
	_configuration.error_log = (char *)strdup(CONFIG_DEFAULT_ERROR_LOG);
154 155 156 157
    _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
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
}

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) {
			if (_configuration.location) free(_configuration.location);
			_configuration.location = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
		} else if (strcmp(node->name, "admin") == 0) {
			if (_configuration.admin) free(_configuration.admin);
			_configuration.admin = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
		} else if (strcmp(node->name, "source-password") == 0) {
175 176 177 178 179 180 181 182 183
            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 {
			    if (_configuration.source_password) free(_configuration.source_password);
			    _configuration.source_password = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
            }
Jack Moffitt's avatar
Jack Moffitt committed
184 185 186 187 188 189 190 191 192 193
		} else if (strcmp(node->name, "hostname") == 0) {
			if (_configuration.hostname) free(_configuration.hostname);
			_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);
			if (tmp) free(tmp);
		} else if (strcmp(node->name, "bind-address") == 0) {
			if (_configuration.bind_address) free(_configuration.bind_address);
			_configuration.bind_address = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
194 195 196 197 198 199 200 201 202
		} else if (strcmp(node->name, "master-server") == 0) {
			if (_configuration.master_server) free(_configuration.master_server);
			_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
203 204 205 206 207 208 209 210
		} 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);
211 212
        } else if (strcmp(node->name, "security") == 0) {
            _parse_security(doc, node->xmlChildrenNode);
Jack Moffitt's avatar
Jack Moffitt committed
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
		}
	} 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);
			if (tmp) free(tmp);
		} else if (strcmp(node->name, "sources") == 0) {
			tmp = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
			_configuration.source_limit = atoi(tmp);
			if (tmp) free(tmp);
		} else if (strcmp(node->name, "threadpool") == 0) {
			tmp = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
			_configuration.threadpool_size = atoi(tmp);
			if (tmp) free(tmp);
		} else if (strcmp(node->name, "client-timeout") == 0) {
			tmp = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
			_configuration.client_timeout = atoi(tmp);
			if (tmp) free(tmp);
		} else if (strcmp(node->name, "header-timeout") == 0) {
			tmp = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
			_configuration.header_timeout = atoi(tmp);
			if (tmp) free(tmp);
245 246 247 248
		} else if (strcmp(node->name, "source-timeout") == 0) {
			tmp = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
			_configuration.source_timeout = atoi(tmp);
			if (tmp) free(tmp);
Jack Moffitt's avatar
Jack Moffitt committed
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
		}
	} 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);
			if (tmp) free(tmp);
		}
	} 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) {
			if (_configuration.base_dir) free(_configuration.base_dir);
			_configuration.base_dir = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
		} else if (strcmp(node->name, "logdir") == 0) {
			if (_configuration.log_dir) free(_configuration.log_dir);
			_configuration.log_dir = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
283 284 285
		} else if (strcmp(node->name, "webroot") == 0) {
			if (_configuration.webroot_dir) free(_configuration.webroot_dir);
			_configuration.webroot_dir = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
Jack Moffitt's avatar
Jack Moffitt committed
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
		}
	} 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) {
			if (_configuration.access_log) free(_configuration.access_log);
			_configuration.access_log = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
		} else if (strcmp(node->name, "errorlog") == 0) {
			if (_configuration.error_log) free(_configuration.error_log);
			_configuration.error_log = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
		}
	} while ((node = node->next));
}

306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
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);
           if (tmp) free(tmp);
       } 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) {
                   if(_configuration.user) free(_configuration.user);
                   _configuration.user = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
               } else if(strcmp(node->name, "group") == 0) {
                   if(_configuration.group) free(_configuration.group);
                   _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
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
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);
			if (tmp) free(tmp);
		}
		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;
	}
	
	if (server) {
		if (server->host) free(server->host);
		free(server);
		server = NULL;
	}
}