admin.c 12.3 KB
Newer Older
1
2
#include <string.h>
#include <stdlib.h>
3
4
#include <stdarg.h>
#include <time.h>
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

#include "config.h"
#include "connection.h"
#include "refbuf.h"
#include "client.h"
#include "source.h"
#include "global.h"
#include "event.h"
#include "stats.h"

#include "format.h"
#include "format_mp3.h"

#include "logging.h"

#define CATMODULE "admin"

22
23
24
25
26
27
#define COMMAND_ERROR             (-1)

/* Mount-specific commands */
#define COMMAND_FALLBACK            1
#define COMMAND_METADATA_UPDATE     2
#define COMMAND_SHOW_LISTENERS      3
28
#define COMMAND_MOVE_CLIENTS        4
29
30
31
32

/* Global commands */
#define COMMAND_LIST_MOUNTS       101
#define COMMAND_RAW_STATS         102
33
#define COMMAND_RAW_LISTSTREAM    103
34

35
36
/* Client management commands */
#define COMMAND_KILL_CLIENT       201
37
#define COMMAND_KILL_SOURCE       202
38

39
40
41
42
43
44
int admin_get_command(char *command)
{
    if(!strcmp(command, "fallbacks"))
        return COMMAND_FALLBACK;
    else if(!strcmp(command, "metadata"))
        return COMMAND_METADATA_UPDATE;
45
46
    else if(!strcmp(command, "listclients"))
        return COMMAND_SHOW_LISTENERS;
47
48
49
50
51
52
    else if(!strcmp(command, "rawstats"))
        return COMMAND_RAW_STATS;
    else if(!strcmp(command, "stats.xml")) /* The old way */
        return COMMAND_RAW_STATS;
    else if(!strcmp(command, "listmounts"))
        return COMMAND_LIST_MOUNTS;
53
54
    else if(!strcmp(command, "streamlist"))
        return COMMAND_RAW_LISTSTREAM;
55
56
    else if(!strcmp(command, "moveclients"))
        return COMMAND_MOVE_CLIENTS;
57
58
    else if(!strcmp(command, "killclient"))
        return COMMAND_KILL_CLIENT;
59
60
    else if(!strcmp(command, "killsource"))
        return COMMAND_KILL_SOURCE;
61
62
63
64
65
66
    else
        return COMMAND_ERROR;
}

static void command_fallback(client_t *client, source_t *source);
static void command_metadata(client_t *client, source_t *source);
67
static void command_show_listeners(client_t *client, source_t *source);
68
static void command_move_clients(client_t *client, source_t *source);
69
70

static void command_raw_stats(client_t *client);
71
static void command_list_mounts(client_t *client, int formatted);
72

73
static void command_kill_client(client_t *client, source_t *source);
74
static void command_kill_source(client_t *client, source_t *source);
75

76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
static void admin_handle_mount_request(client_t *client, source_t *source,
        int command);
static void admin_handle_general_request(client_t *client, int command);

void admin_handle_request(client_t *client, char *uri)
{
    char *mount, *command_string;
    int command;

    if(strncmp("/admin/", uri, 7)) {
        ERROR0("Internal error: admin request isn't");
        client_send_401(client);
        return;
    }

    command_string = uri + 7;

    command = admin_get_command(command_string);

    if(command < 0) {
        ERROR1("Error parsing command string or unrecognised command: %s",
                command_string);
        client_send_400(client, "Unrecognised command");
        return;
    }

    mount = httpp_get_query_param(client->parser, "mount");

    if(mount != NULL) {
        source_t *source;

        /* This is a mount request, handle it as such */
108
109
        if(!connection_check_admin_pass(client->parser)) {
            if(!connection_check_source_pass(client->parser, mount)) {
110
                INFO1("Bad or missing password on mount modification admin "
111
112
113
114
                      "request (command: %s)", command_string);
                client_send_401(client);
                return;
            }
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
        }
        
        avl_tree_rlock(global.source_tree);
        source = source_find_mount(mount);
        avl_tree_unlock(global.source_tree);

        if(source == NULL) {
            WARN2("Admin command %s on non-existent source %s", 
                    command_string, mount);
            client_send_400(client, "Source does not exist");
            return;
        }

        INFO2("Received admin command %s on mount \"%s\"", 
                command_string, mount);

        admin_handle_mount_request(client, source, command);
    }
    else {

        if(!connection_check_admin_pass(client->parser)) {
136
            INFO1("Bad or missing password on admin command "
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
                  "request (command: %s)", command_string);
            client_send_401(client);
            return;
        }
        
        admin_handle_general_request(client, command);
    }
}

static void admin_handle_general_request(client_t *client, int command)
{
    switch(command) {
        case COMMAND_RAW_STATS:
            command_raw_stats(client);
            break;
152
        case COMMAND_LIST_MOUNTS:
153
154
155
156
            command_list_mounts(client, 1);
            break;
        case COMMAND_RAW_LISTSTREAM:
            command_list_mounts(client, 0);
157
            break;
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
        default:
            WARN0("General admin request not recognised");
            client_send_400(client, "Unknown admin request");
            return;
    }
}

static void admin_handle_mount_request(client_t *client, source_t *source, 
        int command)
{
    switch(command) {
        case COMMAND_FALLBACK:
            command_fallback(client, source);
            break;
        case COMMAND_METADATA_UPDATE:
            command_metadata(client, source);
            break;
175
176
177
        case COMMAND_SHOW_LISTENERS:
            command_show_listeners(client, source);
            break;
178
179
180
        case COMMAND_MOVE_CLIENTS:
            command_move_clients(client, source);
            break;
181
182
183
        case COMMAND_KILL_CLIENT:
            command_kill_client(client, source);
            break;
184
185
186
        case COMMAND_KILL_SOURCE:
            command_kill_source(client, source);
            break;
187
188
189
        default:
            WARN0("Mount request not recognised");
            client_send_400(client, "Mount request unknown");
190
            break;
191
192
193
194
195
196
197
198
199
200
201
202
    }
}

#define COMMAND_REQUIRE(client,name,var) \
    do { \
        (var) = httpp_get_query_param((client)->parser, (name)); \
        if((var) == NULL) { \
            client_send_400((client), "Missing parameter"); \
            return; \
        } \
    } while(0);

203
static void html_success(client_t *client, char *message)
204
205
206
207
208
209
210
211
212
213
214
215
{
    int bytes;

    client->respcode = 200;
    bytes = sock_write(client->con->sock, 
            "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" 
            "<html><head><title>Admin request successful</title></head>"
            "<body><p>%s</p></body></html>", message);
    if(bytes > 0) client->con->sent_bytes = bytes;
    client_destroy(client);
}

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
static void html_head(client_t *client)
{
    int bytes;

    client->respcode = 200;
    bytes = sock_write(client->con->sock,
            "HTTP/1.0 200 OK\r\n"
            "Content-Type: text/html\r\n"
            "\r\n"
            "<html><head><title>Admin request</title></head>"
            "<body>");
    if(bytes > 0) client->con->sent_bytes = bytes;
}

static void html_write(client_t *client, char *fmt, ...)
{
    int bytes;
    va_list ap;

    va_start(ap, fmt);
    bytes = sock_write_fmt(client->con->sock, fmt, ap);
    va_end(ap);
    if(bytes > 0) client->con->sent_bytes = bytes;
}

241
242
243
244
245
246
247
248
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
static void command_move_clients(client_t *client, source_t *source)
{
    char *dest_source;
    source_t *dest;
    avl_node *client_node;
    client_t *current;

    COMMAND_REQUIRE(client, "destination", dest_source);
    
    avl_tree_rlock(global.source_tree);
    dest = source_find_mount(dest_source);
    avl_tree_unlock(global.source_tree);

    if(dest == NULL) {
        client_send_400(client, "No such source");
        return;
    }

    avl_tree_wlock(source->client_tree);
    client_node = avl_get_first(source->client_tree);
    while(client_node) {
        current = (client_t *)client_node->key;

        avl_tree_wlock(dest->pending_tree);
        avl_insert(dest->pending_tree, current);
        avl_tree_unlock(dest->pending_tree);

        client_node = avl_get_next(client_node);

        avl_delete(source->client_tree, current, source_remove_client);
    }

    avl_tree_unlock(source->client_tree);
        
    html_success(client, "Clients moved");
}

278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
static void command_show_listeners(client_t *client, source_t *source)
{
    avl_node *client_node;
    client_t *current;
    time_t now = time(NULL);

    DEBUG1("Dumping listeners on mountpoint %s", source->mount);

    html_head(client);

    html_write(client, 
            "<table><tr><td>IP</td><td>Connected</td><td>ID</td></tr>");

    avl_tree_rlock(source->client_tree);

    client_node = avl_get_first(source->client_tree);
    while(client_node) {
        current = (client_t *)client_node->key;

        html_write(client, "<tr><td>%s</td><td>%d</td><td>%ld</td></tr>",
                current->con->ip, now-current->con->con_time, current->con->id);

        client_node = avl_get_next(client_node);
    }

    avl_tree_unlock(source->client_tree);

    html_write(client, "</table></body></html>");

    client_destroy(client);
}

310
311
312
313
314
315
316
static void command_kill_source(client_t *client, source_t *source)
{
    source->running = 0;

    html_success(client, "Removing source");
}

317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
static void command_kill_client(client_t *client, source_t *source)
{
    char *idtext;
    int id;
    client_t *listener;

    COMMAND_REQUIRE(client, "id", idtext);

    id = atoi(idtext);

    listener = source_find_client(source, id);

    if(listener != NULL) {
        INFO1("Admin request: client %d removed", id);

        /* This tags it for removal on the next iteration of the main source
         * loop
         */
        listener->con->error = 1;

        html_success(client, "Client removed");
    }
    else {
        html_success(client, "Client not found");
    }
}

344
345
346
347
348
349
350
351
352
353
354
355
356
static void command_fallback(client_t *client, source_t *source)
{
    char *fallback;
    char *old;

    DEBUG0("Got fallback request");

    COMMAND_REQUIRE(client, "fallback", fallback);

    old = source->fallback_mount;
    source->fallback_mount = strdup(fallback);
    free(old);

357
    html_success(client, "Fallback configured");
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
387
388
389
390
391
392
}

static void command_metadata(client_t *client, source_t *source)
{
    char *action;
    char *value;
    mp3_state *state;

    DEBUG0("Got metadata update request");

    COMMAND_REQUIRE(client, "mode", action);
    COMMAND_REQUIRE(client, "song", value);

    if(source->format->type != FORMAT_TYPE_MP3) {
        client_send_400(client, "Not mp3, cannot update metadata");
        return;
    }

    if(strcmp(action, "updinfo") != 0) {
        client_send_400(client, "No such action");
        return;
    }

    state = source->format->_state;

    thread_mutex_lock(&(state->lock));
    free(state->metadata);
    state->metadata = strdup(value);
    state->metadata_age++;
    state->metadata_raw = 0;
    thread_mutex_unlock(&(state->lock));

    DEBUG2("Metadata on mountpoint %s changed to \"%s\"", source->mount, value);
    stats_event(source->mount, "title", value);

393
    html_success(client, "Metadata update successful");
394
395
396
397
398
399
400
401
402
403
}

static void command_raw_stats(client_t *client) {
    DEBUG0("Stats request, sending xml stats");

    stats_sendxml(client);
    client_destroy(client);
    return;
}

404
static void command_list_mounts(client_t *client, int formatted) {
405
406
    avl_node *node;
    source_t *source;
407
    int bytes;
408
409
410

    DEBUG0("List mounts request");

411
412
    if(formatted) {
        html_head(client);
413

414
415
416
417
418
419
420
421
422
423
424
425
        html_write(client, 
                "<table><tr><td>Mountpoint</td><td>Fallback</td>"
                "<td>Format</td><td>Listeners</td></tr>");
    }
    else {
        client->respcode = 200;
        bytes = sock_write(client->con->sock,
                "HTTP/1.0 200 OK\r\n"
                "Content-Type: text/html\r\n"
                "\r\n");
        if(bytes > 0) client->con->sent_bytes = bytes;
    }
426
427
428
429
430
431
432

    avl_tree_rlock(global.source_tree);

    node = avl_get_first(global.source_tree);
    while(node) {
        source = (source_t *)node->key;

433
434
435
436
437
438
439
440
441
442
443
        if(formatted) {
            html_write(client, 
                    "<tr><td>%s</td><td>%s</td><td>%s</td><td>%ld</td></tr>",
                    source->mount, (source->fallback_mount != NULL)?
                    source->fallback_mount:"", 
                    source->format->format_description, source->listeners);
        }
        else {
            bytes = sock_write(client->con->sock, "%s\r\n", source->mount);
            if(bytes > 0) client->con->sent_bytes += bytes;
        }
444
445
446
447
448
449

        node = avl_get_next(node);
    }

    avl_tree_unlock(global.source_tree);

450
451
    if(formatted) 
        html_write(client, "</table></body></html>");
452
453
454
455
456

    client_destroy(client);
    return;
}