main.c 13.5 KB
Newer Older
1 2 3 4 5
/* Icecast
 *
 * This program is distributed under the GNU General Public License, version 2.
 * A copy of this license is included with this source.
 *
6
 * Copyright 2000-2004, Jack Moffitt <jack@xiph.org, 
7 8
 *                      Michael Smith <msmith@xiph.org>,
 *                      oddsock <oddsock@xiph.org>,
9
 *                      Karl Heyes <karl@xiph.org>,
10
 *                      and others (see AUTHORS for details).
Philipp Schafft's avatar
Philipp Schafft committed
11
 * Copyright 2011-2012, Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>,
12
 * Copyright 2014,      Thomas B. Ruecker <thomas@ruecker.fi>.
13 14
 */

15
/* -*- c-basic-offset: 4; indent-tabs-mode: nil; -*- */
16 17 18 19
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

Jack Moffitt's avatar
Jack Moffitt committed
20 21
#include <stdio.h>
#include <string.h>
22 23 24 25 26 27 28 29 30 31
#include <errno.h>

#ifdef WIN32
#define _WIN32_WINNT 0x0400
/* For getpid() */
#include <process.h>
#include <windows.h>
#define snprintf _snprintf
#define getpid _getpid
#endif
Jack Moffitt's avatar
Jack Moffitt committed
32

33 34 35
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
36 37 38
#ifdef HAVE_CURL
#include <curl/curl.h>
#endif
39

Karl Heyes's avatar
Karl Heyes committed
40 41 42 43 44
#include "thread/thread.h"
#include "avl/avl.h"
#include "net/sock.h"
#include "net/resolver.h"
#include "httpp/httpp.h"
Jack Moffitt's avatar
Jack Moffitt committed
45

46
#if HAVE_SYS_TYPES_H
47
#include <sys/types.h>
48 49
#endif
#if HAVE_GRP_H
50
#include <grp.h>
51 52
#endif
#if HAVE_PWD_H
53 54
#include <pwd.h>
#endif
Jack Moffitt's avatar
Jack Moffitt committed
55

56
#include "cfgfile.h"
Jack Moffitt's avatar
Jack Moffitt committed
57 58 59
#include "sighandler.h"

#include "global.h"
60
#include "compat.h"
Jack Moffitt's avatar
Jack Moffitt committed
61 62 63
#include "connection.h"
#include "refbuf.h"
#include "client.h"
64
#include "slave.h"
Jack Moffitt's avatar
Jack Moffitt committed
65 66
#include "stats.h"
#include "logging.h"
67
#include "xslt.h"
68
#include "fserve.h"
69
#include "yp.h"
70
#include "auth.h"
Jack Moffitt's avatar
Jack Moffitt committed
71

72 73
#include <libxml/xmlmemory.h>

Jack Moffitt's avatar
Jack Moffitt committed
74 75 76
#undef CATMODULE
#define CATMODULE "main"

77 78 79
static int background;
static char *pidfile = NULL;

80
static void _fatal_error(const char *perr)
81
{
82 83 84
#ifdef WIN32_SERVICE
    MessageBox(NULL, perr, "Error", MB_SERVICE_NOTIFICATION);
#elif defined(WIN32)
85 86 87 88 89 90
    MessageBox(NULL, perr, "Error", MB_OK);
#else
    fprintf(stdout, "%s\n", perr);
#endif
}

91
static void _print_usage(void)
Jack Moffitt's avatar
Jack Moffitt committed
92
{
93
    printf("%s\n\n", ICECAST_VERSION_STRING);
94 95
    printf("usage: icecast [-b] -c <file>\n");
    printf("or   : icecast {-v|--version}\n");
96
    printf("options:\n");
97 98 99
    printf("\t-c <file>       Specify configuration file\n");
    printf("\t-v or --version Display version info\n");
    printf("\t-b              Run icecast in the background\n");
100
    printf("\n");
Jack Moffitt's avatar
Jack Moffitt committed
101 102
}

103 104
static void _stop_logging(void)
{
105 106
    log_close(errorlog);
    log_close(accesslog);
107
    log_close(playlistlog);
108 109
}

110
void initialize_subsystems(void)
Jack Moffitt's avatar
Jack Moffitt committed
111
{
112 113 114 115 116 117 118 119
    log_initialize();
    thread_initialize();
    sock_initialize();
    resolver_initialize();
    config_initialize();
    connection_initialize();
    global_initialize();
    refbuf_initialize();
120

121
    xslt_initialize();
122
#ifdef HAVE_CURL_GLOBAL_INIT
123 124
    curl_global_init (CURL_GLOBAL_ALL);
#endif
Jack Moffitt's avatar
Jack Moffitt committed
125 126
}

127
void shutdown_subsystems(void)
Jack Moffitt's avatar
Jack Moffitt committed
128
{
129
    fserve_shutdown();
130 131
    refbuf_shutdown();
    slave_shutdown();
132
    auth_shutdown();
133
    yp_shutdown();
134
    stats_shutdown();
135

136 137 138 139 140 141
    global_shutdown();
    connection_shutdown();
    config_shutdown();
    resolver_shutdown();
    sock_shutdown();
    thread_shutdown();
142

143 144 145 146
#ifdef HAVE_CURL
    curl_global_cleanup();
#endif

147 148
    /* Now that these are done, we can stop the loggers. */
    _stop_logging();
149
    log_shutdown();
150
    xslt_shutdown();
Jack Moffitt's avatar
Jack Moffitt committed
151 152
}

153
static int _parse_config_opts(int argc, char **argv, char *filename, int size)
Jack Moffitt's avatar
Jack Moffitt committed
154
{
155
    int i = 1;
156 157
    int config_ok = 0;

158
    background = 0;
159
    if (argc < 2) return -1;
Jack Moffitt's avatar
Jack Moffitt committed
160

161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
    while (i < argc) {
        if (strcmp(argv[i], "-b") == 0) {
#ifndef WIN32
            pid_t pid;
            fprintf(stdout, "Starting icecast2\nDetaching from the console\n");

            pid = fork();

            if (pid > 0) {
                /* exit the parent */
                exit(0);
            }
            else if(pid < 0) {
                fprintf(stderr, "FATAL: Unable to fork child!");
                exit(1);
            }
            background = 1;
#endif
        }
180
        if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--version") == 0) {
181
            fprintf(stdout, "%s\n", ICECAST_VERSION_STRING);
182 183 184
            exit(0);
        }

185 186 187
        if (strcmp(argv[i], "-c") == 0) {
            if (i + 1 < argc) {
                strncpy(filename, argv[i + 1], size-1);
188
                filename[size-1] = 0;
189
                config_ok = 1;
190 191 192 193
            } else {
                return -1;
            }
        }
194
        i++;
195 196
    }

197 198 199 200
    if(config_ok)
        return 1;
    else
        return -1;
Jack Moffitt's avatar
Jack Moffitt committed
201 202
}

203
static int _start_logging(void)
Jack Moffitt's avatar
Jack Moffitt committed
204
{
205 206
    char fn_error[FILENAME_MAX];
    char fn_access[FILENAME_MAX];
207
    char fn_playlist[FILENAME_MAX];
208
    char buf[1024];
209
    int log_to_stderr;
210

211
    ice_config_t *config = config_get_config_unlocked();
Jack Moffitt's avatar
Jack Moffitt committed
212

213
    if(strcmp(config->error_log, "-")) {
214
        snprintf(fn_error, FILENAME_MAX, "%s%s%s", config->log_dir, PATH_SEPARATOR, config->error_log);
215
        errorlog = log_open(fn_error);
216
        log_to_stderr = 0;
217 218 219
        if (config->logsize)
            log_set_trigger (errorlog, config->logsize);
        log_set_archive_timestamp(errorlog, config->logarchive);
220 221
    } else {
        errorlog = log_open_file(stderr);
222
        log_to_stderr = 1;
223
    }
224 225 226

    if (errorlog < 0) {
        buf[sizeof(buf)-1] = 0;
227 228 229
        snprintf(buf, sizeof(buf)-1, 
                "FATAL: could not open error logging (%s): %s",
                log_to_stderr?"standard error":fn_error,
230 231 232 233 234
                strerror(errno));
        _fatal_error(buf);
    }
    log_set_level(errorlog, config->loglevel);

235
    if(strcmp(config->access_log, "-")) {
236
        snprintf(fn_access, FILENAME_MAX, "%s%s%s", config->log_dir, PATH_SEPARATOR, config->access_log);
237
        accesslog = log_open(fn_access);
238
        log_to_stderr = 0;
239 240 241
        if (config->logsize)
            log_set_trigger (accesslog, config->logsize);
        log_set_archive_timestamp(accesslog, config->logarchive);
242 243
    } else {
        accesslog = log_open_file(stderr);
244
        log_to_stderr = 1;
245
    }
246 247 248

    if (accesslog < 0) {
        buf[sizeof(buf)-1] = 0;
249 250 251
        snprintf(buf, sizeof(buf)-1, 
                "FATAL: could not open access logging (%s): %s",
                log_to_stderr?"standard error":fn_access,
252 253 254
                strerror(errno));
        _fatal_error(buf);
    }
255 256 257 258 259 260 261 262 263 264 265 266 267

    if(config->playlist_log) {
        snprintf(fn_playlist, FILENAME_MAX, "%s%s%s", config->log_dir, PATH_SEPARATOR, config->playlist_log);
        playlistlog = log_open(fn_playlist);
        if (playlistlog < 0) {
            buf[sizeof(buf)-1] = 0;
            snprintf(buf, sizeof(buf)-1, 
                "FATAL: could not open playlist logging (%s): %s",
                log_to_stderr?"standard error":fn_playlist,
                strerror(errno));
            _fatal_error(buf);
        }
        log_to_stderr = 0;
268 269 270
        if (config->logsize)
            log_set_trigger (playlistlog, config->logsize);
        log_set_archive_timestamp(playlistlog, config->logarchive);
271 272 273
    } else {
        playlistlog = -1;
    }
274

275 276
    log_set_level(errorlog, config->loglevel);
    log_set_level(accesslog, 4);
277
    log_set_level(playlistlog, 4);
278 279 280 281

    if (errorlog >= 0 && accesslog >= 0) return 1;
    
    return 0;
Jack Moffitt's avatar
Jack Moffitt committed
282 283 284
}


285
static int _start_listening(void)
Jack Moffitt's avatar
Jack Moffitt committed
286
{
287 288
    int i;
    for(i=0; i < global.server_sockets; i++) {
289 290
        if (sock_listen(global.serversock[i], ICE_LISTEN_QUEUE) == SOCK_ERROR)
            return 0;
Jack Moffitt's avatar
Jack Moffitt committed
291

292
        sock_set_blocking(global.serversock[i], 0);
293
    }
Jack Moffitt's avatar
Jack Moffitt committed
294

295
    return 1;
Jack Moffitt's avatar
Jack Moffitt committed
296 297
}

298
/* bind the socket and start listening */
299
static int _server_proc_init(void)
Jack Moffitt's avatar
Jack Moffitt committed
300
{
301
    ice_config_t *config = config_get_config_unlocked();
302

303
    if (connection_setup_sockets (config) < 1)
304
        return 0;
Jack Moffitt's avatar
Jack Moffitt committed
305

306
    if (!_start_listening()) {
307
        _fatal_error("Failed trying to listen on server socket");
308 309
        return 0;
    }
310

311 312 313 314 315 316 317 318 319 320 321 322
    /* recreate the pid file */
    if (config->pidfile)
    {
        FILE *f;
        pidfile = strdup (config->pidfile);
        if (pidfile && (f = fopen (config->pidfile, "w")) != NULL)
        {
            fprintf (f, "%d\n", (int)getpid());
            fclose (f);
        }
    }

323
    return 1;
324
}
Jack Moffitt's avatar
Jack Moffitt committed
325

326 327 328
/* this is the heart of the beast */
static void _server_proc(void)
{
329 330 331 332 333 334
    if (background)
    {
        fclose (stdin);
        fclose (stdout);
        fclose (stderr);
    }
335
    connection_accept_loop();
Jack Moffitt's avatar
Jack Moffitt committed
336

337
    connection_setup_sockets (NULL);
Jack Moffitt's avatar
Jack Moffitt committed
338 339
}

340
/* chroot the process. Watch out - we need to do this before starting other
341
 * threads. Change uid as well, after figuring out uid _first_ */
342

Michael Smith's avatar
Michael Smith committed
343
static void _ch_root_uid_setup(void)
344
{
345
   ice_config_t *conf = config_get_config_unlocked();
346
#ifdef HAVE_SETUID
347 348 349 350 351 352 353
   struct passwd *user;
   struct group *group;
   uid_t uid=-1;
   gid_t gid=-1;

   if(conf->chuid)
   {
354 355 356 357 358 359 360 361 362
       if(conf->user) {
           user = getpwnam(conf->user);
           if(user)
               uid = user->pw_uid;
           else
               fprintf(stderr, "Couldn't find user \"%s\" in password file\n", conf->user);
       }
       if(conf->group) {
           group = getgrnam(conf->group);
363

364 365 366 367 368
           if(group)
               gid = group->gr_gid;
           else
               fprintf(stderr, "Couldn't find group \"%s\" in groups file\n", conf->group);
       }
369 370 371
   }
#endif

372
#if HAVE_CHROOT
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
   if (conf->chroot)
   {
       if(getuid()) /* root check */
       {
           fprintf(stderr, "WARNING: Cannot change server root unless running as root.\n");
           return;
       }
       if(chroot(conf->base_dir))
       {
           fprintf(stderr,"WARNING: Couldn't change server root: %s\n", strerror(errno));
           return;
       }
       else
           fprintf(stdout, "Changed root successfully to \"%s\".\n", conf->base_dir);

   }   
#endif
390
#if HAVE_SETUID
391

392 393 394 395 396 397 398 399
   if(conf->chuid)
   {
       if(getuid()) /* root check */
       {
           fprintf(stderr, "WARNING: Can't change user id unless you are root.\n");
           return;
       }

400
       if(uid != (uid_t)-1 && gid != (gid_t)-1) {
401
           if(!setgid(gid))
402
               fprintf(stdout, "Changed groupid to %i.\n", (int)gid);
403 404
           else
               fprintf(stdout, "Error changing groupid: %s.\n", strerror(errno));
405 406 407 408
           if(!initgroups(conf->user, gid))
               fprintf(stdout, "Changed supplementary groups based on user: %s.\n", conf->user);
	   else
               fprintf(stdout, "Error changing supplementary groups: %s.\n", strerror(errno));
409
           if(!setuid(uid))
410
               fprintf(stdout, "Changed userid to %i.\n", (int)uid);
411 412 413
           else
               fprintf(stdout, "Error changing userid: %s.\n", strerror(errno));
       }
414 415
   }
#endif
416
}
Philipp Schafft's avatar
Philipp Schafft committed
417

418
#ifdef WIN32_SERVICE
Philipp Schafft's avatar
Philipp Schafft committed
419 420 421 422
int mainService(int argc, char **argv)
#else
int main(int argc, char **argv)
#endif
Jack Moffitt's avatar
Jack Moffitt committed
423
{
424 425
    int res, ret;
    char filename[512];
426
    char pbuf[1024];
427 428 429 430

    /* parse the '-c icecast.xml' option
    ** only, so that we can read a configfile
    */
431
    res = _parse_config_opts(argc, argv, filename, 512);
432
    if (res == 1) {
433
#if !defined(_WIN32) || defined(_CONSOLE) || defined(__MINGW32__) || defined(__MINGW64__)
434
        /* startup all the modules */
435 436
        initialize_subsystems();
#endif
437
        /* parse the config file */
438
        config_get_config();
439
        ret = config_initial_parse_file(filename);
440
        config_release_config();
441
        if (ret < 0) {
442 443 444 445
            memset(pbuf, '\000', sizeof(pbuf));
            snprintf(pbuf, sizeof(pbuf)-1, 
                "FATAL: error parsing config file (%s)", filename);
            _fatal_error(pbuf);
446 447
            switch (ret) {
            case CONFIG_EINSANE:
448
                _fatal_error("filename was null or blank");
449 450
                break;
            case CONFIG_ENOROOT:
451
                _fatal_error("no root element found");
452 453
                break;
            case CONFIG_EBADROOT:
454
                _fatal_error("root element is not <icecast>");
455 456
                break;
            default:
457
                _fatal_error("XML config parsing error");
458 459
                break;
            }
460
#if !defined(_WIN32) || defined(_CONSOLE) || defined(__MINGW32__) || defined(__MINGW64__)
461 462
            shutdown_subsystems();
#endif
463
            return 1;
464 465 466 467 468 469 470 471
        }
    } else if (res == -1) {
        _print_usage();
        return 1;
    }
    
    /* override config file options with commandline options */
    config_parse_cmdline(argc, argv);
Jack Moffitt's avatar
Jack Moffitt committed
472

473 474
    /* Bind socket, before we change userid */
    if(!_server_proc_init()) {
475
        _fatal_error("Server startup failed. Exiting");
476
        shutdown_subsystems();
477 478
        return 1;
    }
479

Michael Smith's avatar
Michael Smith committed
480
    _ch_root_uid_setup(); /* Change user id and root if requested/possible */
481 482

    stats_initialize(); /* We have to do this later on because of threading */
483
    fserve_initialize(); /* This too */
484

485
#ifdef HAVE_SETUID 
486 487 488 489
    /* We'll only have getuid() if we also have setuid(), it's reasonable to
     * assume */
    if(!getuid()) /* Running as root! Don't allow this */
    {
490
        fprintf(stderr, "ERROR: You should not run icecast2 as root\n");
491
        fprintf(stderr, "Use the changeowner directive in the config file\n");
492
        shutdown_subsystems();
493 494 495 496 497 498 499
        return 1;
    }
#endif

    /* setup default signal handlers */
    sighandler_initialize();

500
    if (!_start_logging()) {
501
        _fatal_error("FATAL: Could not start logging");
502
        shutdown_subsystems();
503 504
        return 1;
    }
Jack Moffitt's avatar
Jack Moffitt committed
505

506
    LOG_INFO("%s server started", ICECAST_VERSION_STRING);
Jack Moffitt's avatar
Jack Moffitt committed
507

508
    /* REM 3D Graphics */
Jack Moffitt's avatar
Jack Moffitt committed
509

510 511
    /* let her rip */
    global.running = ICE_RUNNING;
512 513 514 515

    /* Startup yp thread */
    yp_initialize();

516 517
    /* Do this after logging init */
    slave_initialize();
518
    auth_initialise ();
519

520
    _server_proc();
Jack Moffitt's avatar
Jack Moffitt committed
521

522
    LOG_INFO("Shutting down");
523
#if !defined(_WIN32) || defined(_CONSOLE) || defined(__MINGW32__) || defined(__MINGW64__)
524 525
    shutdown_subsystems();
#endif
526 527 528 529 530 531
    if (pidfile)
    {
        remove (pidfile);
        free (pidfile);
    }

532
    return 0;
Jack Moffitt's avatar
Jack Moffitt committed
533 534 535
}