Commit | Line | Data |
---|---|---|
460b9539 | 1 | /* |
2 | * This file is part of DisOrder. | |
b50cfb8a | 3 | * Copyright (C) 2004-2009 Richard Kettlewell |
460b9539 | 4 | * |
e7eb3a27 | 5 | * This program is free software: you can redistribute it and/or modify |
460b9539 | 6 | * it under the terms of the GNU General Public License as published by |
e7eb3a27 | 7 | * the Free Software Foundation, either version 3 of the License, or |
460b9539 | 8 | * (at your option) any later version. |
9 | * | |
e7eb3a27 RK |
10 | * This program is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
460b9539 | 15 | * You should have received a copy of the GNU General Public License |
e7eb3a27 | 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
460b9539 | 17 | */ |
18 | ||
05b75f8d | 19 | #include "disorder-server.h" |
460b9539 | 20 | |
21 | #ifndef NONCE_SIZE | |
22 | # define NONCE_SIZE 16 | |
23 | #endif | |
24 | ||
10114017 | 25 | #ifndef CONFIRM_SIZE |
26 | # define CONFIRM_SIZE 10 | |
27 | #endif | |
28 | ||
460b9539 | 29 | int volume_left, volume_right; /* last known volume */ |
30 | ||
18e6d6e6 | 31 | /** @brief Accept all well-formed login attempts |
32 | * | |
33 | * Used in debugging. | |
34 | */ | |
35 | int wideopen; | |
36 | ||
460b9539 | 37 | struct listener { |
38 | const char *name; | |
39 | int pf; | |
40 | }; | |
41 | ||
397ef7bb | 42 | /** @brief One client connection */ |
460b9539 | 43 | struct conn { |
397ef7bb | 44 | /** @brief Read commands from here */ |
460b9539 | 45 | ev_reader *r; |
397ef7bb | 46 | /** @brief Send responses to here */ |
460b9539 | 47 | ev_writer *w; |
397ef7bb | 48 | /** @brief Underlying file descriptor */ |
460b9539 | 49 | int fd; |
397ef7bb | 50 | /** @brief Unique identifier for connection used in log messages */ |
460b9539 | 51 | unsigned tag; |
397ef7bb | 52 | /** @brief Login name or NULL */ |
460b9539 | 53 | char *who; |
397ef7bb | 54 | /** @brief Event loop */ |
460b9539 | 55 | ev_source *ev; |
397ef7bb | 56 | /** @brief Nonce chosen for this connection */ |
460b9539 | 57 | unsigned char nonce[NONCE_SIZE]; |
397ef7bb RK |
58 | /** @brief Current reader callback |
59 | * | |
60 | * We change this depending on whether we're servicing the @b log command | |
61 | */ | |
460b9539 | 62 | ev_reader_callback *reader; |
397ef7bb | 63 | /** @brief Event log output sending to this connection */ |
460b9539 | 64 | struct eventlog_output *lo; |
397ef7bb | 65 | /** @brief Parent listener */ |
460b9539 | 66 | const struct listener *l; |
b12be54a RK |
67 | /** @brief Login cookie or NULL */ |
68 | char *cookie; | |
eb5dc014 RK |
69 | /** @brief Connection rights */ |
70 | rights_type rights; | |
0126692c | 71 | /** @brief Next connection */ |
72 | struct conn *next; | |
dd9af5cb RK |
73 | /** @brief True if pending rescan had 'wait' set */ |
74 | int rescan_wait; | |
460b9539 | 75 | }; |
76 | ||
0126692c | 77 | /** @brief Linked list of connections */ |
78 | static struct conn *connections; | |
79 | ||
460b9539 | 80 | static int reader_callback(ev_source *ev, |
81 | ev_reader *reader, | |
460b9539 | 82 | void *ptr, |
83 | size_t bytes, | |
84 | int eof, | |
85 | void *u); | |
86 | ||
87 | static const char *noyes[] = { "no", "yes" }; | |
88 | ||
0126692c | 89 | /** @brief Remove a connection from the connection list */ |
90 | static void remove_connection(struct conn *c) { | |
91 | struct conn **cc; | |
92 | ||
93 | for(cc = &connections; *cc && *cc != c; cc = &(*cc)->next) | |
94 | ; | |
95 | if(*cc) | |
96 | *cc = c->next; | |
97 | } | |
98 | ||
397ef7bb RK |
99 | /** @brief Called when a connection's writer fails or is shut down |
100 | * | |
101 | * If the connection still has a raeder that is cancelled. | |
102 | */ | |
460b9539 | 103 | static int writer_error(ev_source attribute((unused)) *ev, |
460b9539 | 104 | int errno_value, |
105 | void *u) { | |
106 | struct conn *c = u; | |
107 | ||
8d8b8c1f | 108 | D(("server writer_error S%x %d", c->tag, errno_value)); |
460b9539 | 109 | if(errno_value == 0) { |
110 | /* writer is done */ | |
8d8b8c1f | 111 | D(("S%x writer completed", c->tag)); |
460b9539 | 112 | } else { |
113 | if(errno_value != EPIPE) | |
114 | error(errno_value, "S%x write error on socket", c->tag); | |
38b8221f | 115 | if(c->r) { |
8d8b8c1f | 116 | D(("cancel reader")); |
38b8221f RK |
117 | ev_reader_cancel(c->r); |
118 | c->r = 0; | |
119 | } | |
8d8b8c1f | 120 | D(("done cancel reader")); |
460b9539 | 121 | } |
38b8221f | 122 | c->w = 0; |
75d64210 | 123 | ev_report(ev); |
0126692c | 124 | remove_connection(c); |
460b9539 | 125 | return 0; |
126 | } | |
127 | ||
397ef7bb RK |
128 | /** @brief Called when a conncetion's reader fails or is shut down |
129 | * | |
130 | * If connection still has a writer then it is closed. | |
131 | */ | |
460b9539 | 132 | static int reader_error(ev_source attribute((unused)) *ev, |
460b9539 | 133 | int errno_value, |
134 | void *u) { | |
135 | struct conn *c = u; | |
136 | ||
8d8b8c1f | 137 | D(("server reader_error S%x %d", c->tag, errno_value)); |
1563f6f6 | 138 | error(errno_value, "S%x read error on socket", c->tag); |
38b8221f RK |
139 | if(c->w) |
140 | ev_writer_close(c->w); | |
141 | c->w = 0; | |
142 | c->r = 0; | |
768d7355 | 143 | ev_report(ev); |
0126692c | 144 | remove_connection(c); |
460b9539 | 145 | return 0; |
146 | } | |
147 | ||
460b9539 | 148 | static int c_disable(struct conn *c, char **vec, int nvec) { |
149 | if(nvec == 0) | |
150 | disable_playing(c->who); | |
151 | else if(nvec == 1 && !strcmp(vec[0], "now")) | |
152 | disable_playing(c->who); | |
153 | else { | |
154 | sink_writes(ev_writer_sink(c->w), "550 invalid argument\n"); | |
155 | return 1; /* completed */ | |
156 | } | |
157 | sink_writes(ev_writer_sink(c->w), "250 OK\n"); | |
158 | return 1; /* completed */ | |
159 | } | |
160 | ||
161 | static int c_enable(struct conn *c, | |
162 | char attribute((unused)) **vec, | |
163 | int attribute((unused)) nvec) { | |
164 | enable_playing(c->who, c->ev); | |
165 | /* Enable implicitly unpauses if there is nothing playing */ | |
166 | if(paused && !playing) resume_playing(c->who); | |
167 | sink_writes(ev_writer_sink(c->w), "250 OK\n"); | |
168 | return 1; /* completed */ | |
169 | } | |
170 | ||
171 | static int c_enabled(struct conn *c, | |
172 | char attribute((unused)) **vec, | |
173 | int attribute((unused)) nvec) { | |
174 | sink_printf(ev_writer_sink(c->w), "252 %s\n", noyes[playing_is_enabled()]); | |
175 | return 1; /* completed */ | |
176 | } | |
177 | ||
178 | static int c_play(struct conn *c, char **vec, | |
179 | int attribute((unused)) nvec) { | |
180 | const char *track; | |
181 | struct queue_entry *q; | |
182 | ||
183 | if(!trackdb_exists(vec[0])) { | |
184 | sink_writes(ev_writer_sink(c->w), "550 track is not in database\n"); | |
185 | return 1; | |
186 | } | |
187 | if(!(track = trackdb_resolve(vec[0]))) { | |
188 | sink_writes(ev_writer_sink(c->w), "550 cannot resolve track\n"); | |
189 | return 1; | |
190 | } | |
2dc2f478 | 191 | q = queue_add(track, c->who, WHERE_BEFORE_RANDOM, origin_picked); |
460b9539 | 192 | queue_write(); |
193 | /* If we added the first track, and something is playing, then prepare the | |
194 | * new track. If nothing is playing then we don't bother as it wouldn't gain | |
195 | * anything. */ | |
196 | if(q == qhead.next && playing) | |
197 | prepare(c->ev, q); | |
81e440ce | 198 | sink_printf(ev_writer_sink(c->w), "252 %s\n", q->id); |
460b9539 | 199 | /* If the queue was empty but we are for some reason paused then |
200 | * unpause. */ | |
201 | if(!playing) resume_playing(0); | |
202 | play(c->ev); | |
203 | return 1; /* completed */ | |
204 | } | |
205 | ||
206 | static int c_remove(struct conn *c, char **vec, | |
207 | int attribute((unused)) nvec) { | |
208 | struct queue_entry *q; | |
209 | ||
210 | if(!(q = queue_find(vec[0]))) { | |
211 | sink_writes(ev_writer_sink(c->w), "550 no such track on the queue\n"); | |
212 | return 1; | |
213 | } | |
938d8157 | 214 | if(!right_removable(c->rights, c->who, q)) { |
834e7c4a | 215 | error(0, "%s attempted remove but lacks required rights", c->who); |
eb5dc014 | 216 | sink_writes(ev_writer_sink(c->w), |
b4a80f69 | 217 | "510 Not authorized to remove that track\n"); |
eb5dc014 | 218 | return 1; |
460b9539 | 219 | } |
220 | queue_remove(q, c->who); | |
221 | /* De-prepare the track. */ | |
222 | abandon(c->ev, q); | |
49a773eb RK |
223 | /* See about adding a new random track */ |
224 | add_random_track(c->ev); | |
460b9539 | 225 | /* Prepare whatever the next head track is. */ |
226 | if(qhead.next != &qhead) | |
227 | prepare(c->ev, qhead.next); | |
228 | queue_write(); | |
229 | sink_writes(ev_writer_sink(c->w), "250 removed\n"); | |
230 | return 1; /* completed */ | |
231 | } | |
232 | ||
233 | static int c_scratch(struct conn *c, | |
234 | char **vec, | |
235 | int nvec) { | |
236 | if(!playing) { | |
237 | sink_writes(ev_writer_sink(c->w), "250 nothing is playing\n"); | |
238 | return 1; /* completed */ | |
239 | } | |
eb5dc014 RK |
240 | /* TODO there is a bug here: if we specify an ID but it's not the currently |
241 | * playing track then you will get 550 if you weren't authorized to scratch | |
242 | * the currently playing track. */ | |
938d8157 | 243 | if(!right_scratchable(c->rights, c->who, playing)) { |
834e7c4a | 244 | error(0, "%s attempted scratch but lacks required rights", c->who); |
eb5dc014 | 245 | sink_writes(ev_writer_sink(c->w), |
b4a80f69 | 246 | "510 Not authorized to scratch that track\n"); |
eb5dc014 | 247 | return 1; |
460b9539 | 248 | } |
249 | scratch(c->who, nvec == 1 ? vec[0] : 0); | |
250 | /* If you scratch an unpaused track then it is automatically unpaused */ | |
251 | resume_playing(0); | |
252 | sink_writes(ev_writer_sink(c->w), "250 scratched\n"); | |
253 | return 1; /* completed */ | |
254 | } | |
255 | ||
256 | static int c_pause(struct conn *c, | |
257 | char attribute((unused)) **vec, | |
258 | int attribute((unused)) nvec) { | |
259 | if(!playing) { | |
260 | sink_writes(ev_writer_sink(c->w), "250 nothing is playing\n"); | |
261 | return 1; /* completed */ | |
262 | } | |
263 | if(paused) { | |
264 | sink_writes(ev_writer_sink(c->w), "250 already paused\n"); | |
265 | return 1; /* completed */ | |
266 | } | |
267 | if(pause_playing(c->who) < 0) | |
268 | sink_writes(ev_writer_sink(c->w), "550 cannot pause this track\n"); | |
269 | else | |
270 | sink_writes(ev_writer_sink(c->w), "250 paused\n"); | |
271 | return 1; | |
272 | } | |
273 | ||
274 | static int c_resume(struct conn *c, | |
275 | char attribute((unused)) **vec, | |
276 | int attribute((unused)) nvec) { | |
277 | if(!paused) { | |
278 | sink_writes(ev_writer_sink(c->w), "250 not paused\n"); | |
279 | return 1; /* completed */ | |
280 | } | |
281 | resume_playing(c->who); | |
282 | sink_writes(ev_writer_sink(c->w), "250 paused\n"); | |
283 | return 1; | |
284 | } | |
285 | ||
286 | static int c_shutdown(struct conn *c, | |
287 | char attribute((unused)) **vec, | |
288 | int attribute((unused)) nvec) { | |
289 | info("S%x shut down by %s", c->tag, c->who); | |
290 | sink_writes(ev_writer_sink(c->w), "250 shutting down\n"); | |
291 | ev_writer_flush(c->w); | |
292 | quit(c->ev); | |
293 | } | |
294 | ||
295 | static int c_reconfigure(struct conn *c, | |
296 | char attribute((unused)) **vec, | |
297 | int attribute((unused)) nvec) { | |
298 | info("S%x reconfigure by %s", c->tag, c->who); | |
299 | if(reconfigure(c->ev, 1)) | |
300 | sink_writes(ev_writer_sink(c->w), "550 error reading new config\n"); | |
301 | else | |
302 | sink_writes(ev_writer_sink(c->w), "250 installed new config\n"); | |
303 | return 1; /* completed */ | |
304 | } | |
305 | ||
dd9af5cb RK |
306 | static void finished_rescan(void *ru) { |
307 | struct conn *const c = ru; | |
308 | ||
309 | sink_writes(ev_writer_sink(c->w), "250 rescan completed\n"); | |
310 | /* Turn this connection back on */ | |
311 | ev_reader_enable(c->r); | |
312 | } | |
313 | ||
314 | static void start_fresh_rescan(void *ru) { | |
315 | struct conn *const c = ru; | |
316 | ||
317 | if(trackdb_rescan_underway()) { | |
318 | /* Some other waiter beat us to it. However in this case we're happy to | |
319 | * piggyback; the requirement is that a new rescan be started, not that it | |
320 | * was _our_ rescan. */ | |
321 | if(c->rescan_wait) { | |
322 | /* We block until the rescan completes */ | |
323 | trackdb_add_rescanned(finished_rescan, c); | |
324 | } else { | |
325 | /* We report that the new rescan has started */ | |
326 | sink_writes(ev_writer_sink(c->w), "250 rescan initiated\n"); | |
327 | /* Turn this connection back on */ | |
328 | ev_reader_enable(c->r); | |
329 | } | |
330 | } else { | |
331 | /* We are the first connection to get a callback so we must start a | |
332 | * rescan. */ | |
333 | if(c->rescan_wait) { | |
334 | /* We want to block until the new rescan completes */ | |
335 | trackdb_rescan(c->ev, 1/*check*/, finished_rescan, c); | |
336 | } else { | |
337 | /* We can report back immediately */ | |
338 | trackdb_rescan(c->ev, 1/*check*/, 0, 0); | |
339 | sink_writes(ev_writer_sink(c->w), "250 rescan initiated\n"); | |
340 | /* Turn this connection back on */ | |
341 | ev_reader_enable(c->r); | |
342 | } | |
343 | } | |
344 | } | |
345 | ||
460b9539 | 346 | static int c_rescan(struct conn *c, |
dd9af5cb RK |
347 | char **vec, |
348 | int nvec) { | |
d867af10 | 349 | int flag_wait = 0, flag_fresh = 0, n; |
dd9af5cb RK |
350 | |
351 | /* Parse flags */ | |
352 | for(n = 0; n < nvec; ++n) { | |
353 | if(!strcmp(vec[n], "wait")) | |
d867af10 | 354 | flag_wait = 1; /* wait for rescan to complete */ |
dd9af5cb RK |
355 | #if 0 |
356 | /* Currently disabled because untested (and hard to test). */ | |
357 | else if(!strcmp(vec[n], "fresh")) | |
d867af10 | 358 | flag_fresh = 1; /* don't piggyback underway rescan */ |
dd9af5cb RK |
359 | #endif |
360 | else { | |
361 | sink_writes(ev_writer_sink(c->w), "550 unknown flag\n"); | |
362 | return 1; /* completed */ | |
363 | } | |
364 | } | |
365 | /* Report what was requested */ | |
366 | info("S%x rescan by %s (%s %s)", c->tag, c->who, | |
d867af10 RK |
367 | flag_wait ? "wait" : "", |
368 | flag_fresh ? "fresh" : ""); | |
dd9af5cb | 369 | if(trackdb_rescan_underway()) { |
d867af10 | 370 | if(flag_fresh) { |
dd9af5cb RK |
371 | /* We want a fresh rescan but there is already one underway. Arrange a |
372 | * callback when it completes and then set off a new one. */ | |
d867af10 | 373 | c->rescan_wait = flag_wait; |
dd9af5cb | 374 | trackdb_add_rescanned(start_fresh_rescan, c); |
d867af10 | 375 | if(flag_wait) |
dd9af5cb RK |
376 | return 0; |
377 | else { | |
378 | sink_writes(ev_writer_sink(c->w), "250 rescan queued\n"); | |
379 | return 1; | |
380 | } | |
381 | } else { | |
382 | /* There's a rescan underway, and it's acceptable to piggyback on it */ | |
d867af10 | 383 | if(flag_wait) { |
dd9af5cb RK |
384 | /* We want to block until completion. */ |
385 | trackdb_add_rescanned(finished_rescan, c); | |
386 | return 0; | |
387 | } else { | |
388 | /* We don't want to block. So we just report that things are in | |
389 | * hand. */ | |
390 | sink_writes(ev_writer_sink(c->w), "250 rescan already underway\n"); | |
391 | return 1; | |
392 | } | |
393 | } | |
394 | } else { | |
395 | /* No rescan is underway. fresh is therefore irrelevant. */ | |
d867af10 | 396 | if(flag_wait) { |
dd9af5cb RK |
397 | /* We want to block until completion */ |
398 | trackdb_rescan(c->ev, 1/*check*/, finished_rescan, c); | |
399 | return 0; | |
400 | } else { | |
401 | /* We don't want to block. */ | |
402 | trackdb_rescan(c->ev, 1/*check*/, 0, 0); | |
403 | sink_writes(ev_writer_sink(c->w), "250 rescan initiated\n"); | |
404 | return 1; /* completed */ | |
405 | } | |
406 | } | |
460b9539 | 407 | } |
408 | ||
409 | static int c_version(struct conn *c, | |
410 | char attribute((unused)) **vec, | |
411 | int attribute((unused)) nvec) { | |
412 | /* VERSION had better only use the basic character set */ | |
a05e4467 | 413 | sink_printf(ev_writer_sink(c->w), "251 %s\n", disorder_short_version_string); |
460b9539 | 414 | return 1; /* completed */ |
415 | } | |
416 | ||
417 | static int c_playing(struct conn *c, | |
418 | char attribute((unused)) **vec, | |
419 | int attribute((unused)) nvec) { | |
420 | if(playing) { | |
421 | queue_fix_sofar(playing); | |
422 | playing->expected = 0; | |
423 | sink_printf(ev_writer_sink(c->w), "252 %s\n", queue_marshall(playing)); | |
424 | } else | |
425 | sink_printf(ev_writer_sink(c->w), "259 nothing playing\n"); | |
426 | return 1; /* completed */ | |
427 | } | |
428 | ||
b12be54a | 429 | static const char *connection_host(struct conn *c) { |
460b9539 | 430 | union { |
431 | struct sockaddr sa; | |
432 | struct sockaddr_in in; | |
433 | struct sockaddr_in6 in6; | |
434 | } u; | |
435 | socklen_t l; | |
b12be54a | 436 | int n; |
460b9539 | 437 | char host[1024]; |
438 | ||
460b9539 | 439 | /* get connection data */ |
440 | l = sizeof u; | |
441 | if(getpeername(c->fd, &u.sa, &l) < 0) { | |
442 | error(errno, "S%x error calling getpeername", c->tag); | |
b12be54a | 443 | return 0; |
460b9539 | 444 | } |
445 | if(c->l->pf != PF_UNIX) { | |
446 | if((n = getnameinfo(&u.sa, l, | |
447 | host, sizeof host, 0, 0, NI_NUMERICHOST))) { | |
448 | error(0, "S%x error calling getnameinfo: %s", c->tag, gai_strerror(n)); | |
b12be54a | 449 | return 0; |
460b9539 | 450 | } |
b12be54a | 451 | return xstrdup(host); |
18e6d6e6 | 452 | } else |
b12be54a RK |
453 | return "local"; |
454 | } | |
455 | ||
456 | static int c_user(struct conn *c, | |
457 | char **vec, | |
458 | int attribute((unused)) nvec) { | |
eb5dc014 | 459 | struct kvp *k; |
f0feb22e | 460 | const char *res, *host, *password; |
eb5dc014 | 461 | rights_type rights; |
b12be54a RK |
462 | |
463 | if(c->who) { | |
464 | sink_writes(ev_writer_sink(c->w), "530 already authenticated\n"); | |
465 | return 1; | |
466 | } | |
467 | /* get connection data */ | |
468 | if(!(host = connection_host(c))) { | |
469 | sink_writes(ev_writer_sink(c->w), "530 authentication failure\n"); | |
470 | return 1; | |
471 | } | |
460b9539 | 472 | /* find the user */ |
eb5dc014 | 473 | k = trackdb_getuserinfo(vec[0]); |
f0feb22e | 474 | /* reject nonexistent users */ |
eb5dc014 RK |
475 | if(!k) { |
476 | error(0, "S%x unknown user '%s' from %s", c->tag, vec[0], host); | |
477 | sink_writes(ev_writer_sink(c->w), "530 authentication failed\n"); | |
478 | return 1; | |
479 | } | |
480 | /* reject unconfirmed users */ | |
481 | if(kvp_get(k, "confirmation")) { | |
482 | error(0, "S%x unconfirmed user '%s' from %s", c->tag, vec[0], host); | |
483 | sink_writes(ev_writer_sink(c->w), "530 authentication failed\n"); | |
484 | return 1; | |
485 | } | |
486 | password = kvp_get(k, "password"); | |
487 | if(!password) password = ""; | |
0f55e905 | 488 | if(parse_rights(kvp_get(k, "rights"), &rights, 1)) { |
eb5dc014 | 489 | error(0, "error parsing rights for %s", vec[0]); |
18e6d6e6 | 490 | sink_writes(ev_writer_sink(c->w), "530 authentication failed\n"); |
491 | return 1; | |
492 | } | |
f0feb22e RK |
493 | /* check whether the response is right */ |
494 | res = authhash(c->nonce, sizeof c->nonce, password, | |
637fdea3 | 495 | config->authorization_algorithm); |
18e6d6e6 | 496 | if(wideopen || (res && !strcmp(res, vec[1]))) { |
497 | c->who = vec[0]; | |
eb5dc014 | 498 | c->rights = rights; |
18e6d6e6 | 499 | /* currently we only bother logging remote connections */ |
30365519 | 500 | if(strcmp(host, "local")) |
18e6d6e6 | 501 | info("S%x %s connected from %s", c->tag, vec[0], host); |
30365519 | 502 | else |
eb5dc014 | 503 | c->rights |= RIGHT__LOCAL; |
18e6d6e6 | 504 | sink_writes(ev_writer_sink(c->w), "230 OK\n"); |
505 | return 1; | |
460b9539 | 506 | } |
507 | /* oops, response was wrong */ | |
18e6d6e6 | 508 | info("S%x authentication failure for %s from %s", c->tag, vec[0], host); |
460b9539 | 509 | sink_writes(ev_writer_sink(c->w), "530 authentication failed\n"); |
510 | return 1; | |
511 | } | |
512 | ||
513 | static int c_recent(struct conn *c, | |
514 | char attribute((unused)) **vec, | |
515 | int attribute((unused)) nvec) { | |
516 | const struct queue_entry *q; | |
517 | ||
518 | sink_writes(ev_writer_sink(c->w), "253 Tracks follow\n"); | |
519 | for(q = phead.next; q != &phead; q = q->next) | |
520 | sink_printf(ev_writer_sink(c->w), " %s\n", queue_marshall(q)); | |
521 | sink_writes(ev_writer_sink(c->w), ".\n"); | |
522 | return 1; /* completed */ | |
523 | } | |
524 | ||
525 | static int c_queue(struct conn *c, | |
526 | char attribute((unused)) **vec, | |
527 | int attribute((unused)) nvec) { | |
528 | struct queue_entry *q; | |
529 | time_t when = 0; | |
530 | const char *l; | |
531 | long length; | |
532 | ||
533 | sink_writes(ev_writer_sink(c->w), "253 Tracks follow\n"); | |
534 | if(playing_is_enabled() && !paused) { | |
535 | if(playing) { | |
536 | queue_fix_sofar(playing); | |
537 | if((l = trackdb_get(playing->track, "_length")) | |
538 | && (length = atol(l))) { | |
539 | time(&when); | |
540 | when += length - playing->sofar + config->gap; | |
541 | } | |
542 | } else | |
543 | /* Nothing is playing but playing is enabled, so whatever is | |
544 | * first in the queue can be expected to start immediately. */ | |
545 | time(&when); | |
546 | } | |
547 | for(q = qhead.next; q != &qhead; q = q->next) { | |
548 | /* fill in estimated start time */ | |
549 | q->expected = when; | |
550 | sink_printf(ev_writer_sink(c->w), " %s\n", queue_marshall(q)); | |
551 | /* update for next track */ | |
552 | if(when) { | |
553 | if((l = trackdb_get(q->track, "_length")) | |
554 | && (length = atol(l))) | |
555 | when += length + config->gap; | |
556 | else | |
557 | when = 0; | |
558 | } | |
559 | } | |
560 | sink_writes(ev_writer_sink(c->w), ".\n"); | |
561 | return 1; /* completed */ | |
562 | } | |
563 | ||
564 | static int output_list(struct conn *c, char **vec) { | |
565 | while(*vec) | |
566 | sink_printf(ev_writer_sink(c->w), "%s\n", *vec++); | |
567 | sink_writes(ev_writer_sink(c->w), ".\n"); | |
568 | return 1; | |
569 | } | |
570 | ||
571 | static int files_dirs(struct conn *c, | |
572 | char **vec, | |
573 | int nvec, | |
574 | enum trackdb_listable what) { | |
575 | const char *dir, *re, *errstr; | |
576 | int erroffset; | |
577 | pcre *rec; | |
578 | char **fvec, *key; | |
579 | ||
580 | switch(nvec) { | |
581 | case 0: dir = 0; re = 0; break; | |
582 | case 1: dir = vec[0]; re = 0; break; | |
583 | case 2: dir = vec[0]; re = vec[1]; break; | |
584 | default: abort(); | |
585 | } | |
586 | /* A bit of a bodge to make sure the args don't trample on cache keys */ | |
587 | if(dir && strchr(dir, '\n')) { | |
588 | sink_writes(ev_writer_sink(c->w), "550 invalid directory name\n"); | |
589 | return 1; | |
590 | } | |
591 | if(re && strchr(re, '\n')) { | |
592 | sink_writes(ev_writer_sink(c->w), "550 invalid regexp\n"); | |
593 | return 1; | |
594 | } | |
595 | /* We bother eliminating "" because the web interface is relatively | |
596 | * likely to send it */ | |
597 | if(re && *re) { | |
598 | byte_xasprintf(&key, "%d\n%s\n%s", (int)what, dir ? dir : "", re); | |
599 | fvec = (char **)cache_get(&cache_files_type, key); | |
600 | if(fvec) { | |
601 | /* Got a cache hit, don't store the answer in the cache */ | |
602 | key = 0; | |
603 | ++cache_files_hits; | |
604 | rec = 0; /* quieten compiler */ | |
605 | } else { | |
606 | /* Cache miss, we'll do the lookup and key != 0 so we'll store the answer | |
607 | * in the cache. */ | |
608 | if(!(rec = pcre_compile(re, PCRE_CASELESS|PCRE_UTF8, | |
609 | &errstr, &erroffset, 0))) { | |
610 | sink_printf(ev_writer_sink(c->w), "550 Error compiling regexp: %s\n", | |
611 | errstr); | |
612 | return 1; | |
613 | } | |
614 | /* It only counts as a miss if the regexp was valid. */ | |
615 | ++cache_files_misses; | |
616 | } | |
617 | } else { | |
618 | /* No regexp, don't bother caching the result */ | |
619 | rec = 0; | |
620 | key = 0; | |
621 | fvec = 0; | |
622 | } | |
623 | if(!fvec) { | |
624 | /* No cache hit (either because a miss, or because we did not look) so do | |
625 | * the lookup */ | |
626 | if(dir && *dir) | |
627 | fvec = trackdb_list(dir, 0, what, rec); | |
628 | else | |
629 | fvec = trackdb_list(0, 0, what, rec); | |
630 | } | |
631 | if(key) | |
632 | /* Put the answer in the cache */ | |
633 | cache_put(&cache_files_type, key, fvec); | |
634 | sink_writes(ev_writer_sink(c->w), "253 Listing follow\n"); | |
635 | return output_list(c, fvec); | |
636 | } | |
637 | ||
638 | static int c_files(struct conn *c, | |
639 | char **vec, | |
640 | int nvec) { | |
641 | return files_dirs(c, vec, nvec, trackdb_files); | |
642 | } | |
643 | ||
644 | static int c_dirs(struct conn *c, | |
645 | char **vec, | |
646 | int nvec) { | |
647 | return files_dirs(c, vec, nvec, trackdb_directories); | |
648 | } | |
649 | ||
650 | static int c_allfiles(struct conn *c, | |
651 | char **vec, | |
652 | int nvec) { | |
653 | return files_dirs(c, vec, nvec, trackdb_directories|trackdb_files); | |
654 | } | |
655 | ||
656 | static int c_get(struct conn *c, | |
657 | char **vec, | |
658 | int attribute((unused)) nvec) { | |
27ed5a69 | 659 | const char *v, *track; |
460b9539 | 660 | |
27ed5a69 RK |
661 | if(!(track = trackdb_resolve(vec[0]))) { |
662 | sink_writes(ev_writer_sink(c->w), "550 cannot resolve track\n"); | |
663 | return 1; | |
664 | } | |
665 | if(vec[1][0] != '_' && (v = trackdb_get(track, vec[1]))) | |
7b32e917 | 666 | sink_printf(ev_writer_sink(c->w), "252 %s\n", quoteutf8(v)); |
460b9539 | 667 | else |
fb1bc1f5 | 668 | sink_writes(ev_writer_sink(c->w), "555 not found\n"); |
460b9539 | 669 | return 1; |
670 | } | |
671 | ||
672 | static int c_length(struct conn *c, | |
673 | char **vec, | |
674 | int attribute((unused)) nvec) { | |
675 | const char *track, *v; | |
676 | ||
677 | if(!(track = trackdb_resolve(vec[0]))) { | |
678 | sink_writes(ev_writer_sink(c->w), "550 cannot resolve track\n"); | |
679 | return 1; | |
680 | } | |
681 | if((v = trackdb_get(track, "_length"))) | |
7b32e917 | 682 | sink_printf(ev_writer_sink(c->w), "252 %s\n", quoteutf8(v)); |
460b9539 | 683 | else |
684 | sink_writes(ev_writer_sink(c->w), "550 not found\n"); | |
685 | return 1; | |
686 | } | |
687 | ||
688 | static int c_set(struct conn *c, | |
689 | char **vec, | |
690 | int attribute((unused)) nvec) { | |
27ed5a69 RK |
691 | const char *track; |
692 | ||
693 | if(!(track = trackdb_resolve(vec[0]))) { | |
694 | sink_writes(ev_writer_sink(c->w), "550 cannot resolve track\n"); | |
695 | return 1; | |
696 | } | |
697 | if(vec[1][0] != '_' && !trackdb_set(track, vec[1], vec[2])) | |
460b9539 | 698 | sink_writes(ev_writer_sink(c->w), "250 OK\n"); |
699 | else | |
700 | sink_writes(ev_writer_sink(c->w), "550 not found\n"); | |
701 | return 1; | |
702 | } | |
703 | ||
704 | static int c_prefs(struct conn *c, | |
705 | char **vec, | |
706 | int attribute((unused)) nvec) { | |
707 | struct kvp *k; | |
27ed5a69 | 708 | const char *track; |
460b9539 | 709 | |
27ed5a69 RK |
710 | if(!(track = trackdb_resolve(vec[0]))) { |
711 | sink_writes(ev_writer_sink(c->w), "550 cannot resolve track\n"); | |
712 | return 1; | |
713 | } | |
714 | k = trackdb_get_all(track); | |
460b9539 | 715 | sink_writes(ev_writer_sink(c->w), "253 prefs follow\n"); |
716 | for(; k; k = k->next) | |
717 | if(k->name[0] != '_') /* omit internal values */ | |
718 | sink_printf(ev_writer_sink(c->w), | |
719 | " %s %s\n", quoteutf8(k->name), quoteutf8(k->value)); | |
720 | sink_writes(ev_writer_sink(c->w), ".\n"); | |
721 | return 1; | |
722 | } | |
723 | ||
724 | static int c_exists(struct conn *c, | |
725 | char **vec, | |
726 | int attribute((unused)) nvec) { | |
27ed5a69 | 727 | /* trackdb_exists() does its own alias checking */ |
460b9539 | 728 | sink_printf(ev_writer_sink(c->w), "252 %s\n", noyes[trackdb_exists(vec[0])]); |
729 | return 1; | |
730 | } | |
731 | ||
732 | static void search_parse_error(const char *msg, void *u) { | |
733 | *(const char **)u = msg; | |
734 | } | |
735 | ||
736 | static int c_search(struct conn *c, | |
737 | char **vec, | |
738 | int attribute((unused)) nvec) { | |
739 | char **terms, **results; | |
740 | int nterms, nresults, n; | |
741 | const char *e = "unknown error"; | |
742 | ||
743 | /* This is a bit of a bodge. Initially it's there to make the eclient | |
744 | * interface a bit more convenient to add searching to, but it has the more | |
745 | * compelling advantage that if everything uses it, then interpretation of | |
746 | * user-supplied search strings will be the same everywhere. */ | |
747 | if(!(terms = split(vec[0], &nterms, SPLIT_QUOTES, search_parse_error, &e))) { | |
748 | sink_printf(ev_writer_sink(c->w), "550 %s\n", e); | |
749 | } else { | |
750 | results = trackdb_search(terms, nterms, &nresults); | |
751 | sink_printf(ev_writer_sink(c->w), "253 %d matches\n", nresults); | |
752 | for(n = 0; n < nresults; ++n) | |
753 | sink_printf(ev_writer_sink(c->w), "%s\n", results[n]); | |
754 | sink_writes(ev_writer_sink(c->w), ".\n"); | |
755 | } | |
756 | return 1; | |
757 | } | |
758 | ||
759 | static int c_random_enable(struct conn *c, | |
760 | char attribute((unused)) **vec, | |
761 | int attribute((unused)) nvec) { | |
762 | enable_random(c->who, c->ev); | |
763 | /* Enable implicitly unpauses if there is nothing playing */ | |
764 | if(paused && !playing) resume_playing(c->who); | |
765 | sink_writes(ev_writer_sink(c->w), "250 OK\n"); | |
766 | return 1; /* completed */ | |
767 | } | |
768 | ||
769 | static int c_random_disable(struct conn *c, | |
770 | char attribute((unused)) **vec, | |
771 | int attribute((unused)) nvec) { | |
772 | disable_random(c->who); | |
773 | sink_writes(ev_writer_sink(c->w), "250 OK\n"); | |
774 | return 1; /* completed */ | |
775 | } | |
776 | ||
777 | static int c_random_enabled(struct conn *c, | |
778 | char attribute((unused)) **vec, | |
779 | int attribute((unused)) nvec) { | |
780 | sink_printf(ev_writer_sink(c->w), "252 %s\n", noyes[random_is_enabled()]); | |
781 | return 1; /* completed */ | |
782 | } | |
783 | ||
d6dde5a3 RK |
784 | static void got_stats(char *stats, void *u) { |
785 | struct conn *const c = u; | |
786 | ||
787 | sink_printf(ev_writer_sink(c->w), "253 stats\n%s\n.\n", stats); | |
788 | /* Now we can start processing commands again */ | |
789 | ev_reader_enable(c->r); | |
790 | } | |
791 | ||
460b9539 | 792 | static int c_stats(struct conn *c, |
793 | char attribute((unused)) **vec, | |
794 | int attribute((unused)) nvec) { | |
d6dde5a3 RK |
795 | trackdb_stats_subprocess(c->ev, got_stats, c); |
796 | return 0; /* not yet complete */ | |
460b9539 | 797 | } |
798 | ||
799 | static int c_volume(struct conn *c, | |
800 | char **vec, | |
801 | int nvec) { | |
802 | int l, r, set; | |
803 | char lb[32], rb[32]; | |
eb5dc014 | 804 | rights_type rights; |
460b9539 | 805 | |
806 | switch(nvec) { | |
807 | case 0: | |
808 | set = 0; | |
809 | break; | |
810 | case 1: | |
811 | l = r = atoi(vec[0]); | |
812 | set = 1; | |
813 | break; | |
814 | case 2: | |
815 | l = atoi(vec[0]); | |
816 | r = atoi(vec[1]); | |
817 | set = 1; | |
818 | break; | |
819 | default: | |
820 | abort(); | |
821 | } | |
eb5dc014 RK |
822 | rights = set ? RIGHT_VOLUME : RIGHT_READ; |
823 | if(!(c->rights & rights)) { | |
834e7c4a | 824 | error(0, "%s attempted to set volume but lacks required rights", c->who); |
b4a80f69 | 825 | sink_writes(ev_writer_sink(c->w), "510 Prohibited\n"); |
eb5dc014 RK |
826 | return 1; |
827 | } | |
b50cfb8a | 828 | if(!api || !api->set_volume) { |
460b9539 | 829 | sink_writes(ev_writer_sink(c->w), "550 error accessing mixer\n"); |
b50cfb8a RK |
830 | return 1; |
831 | } | |
e4d0463f | 832 | (set ? api->set_volume : api->get_volume)(&l, &r); |
b50cfb8a RK |
833 | sink_printf(ev_writer_sink(c->w), "252 %d %d\n", l, r); |
834 | if(l != volume_left || r != volume_right) { | |
835 | volume_left = l; | |
836 | volume_right = r; | |
837 | snprintf(lb, sizeof lb, "%d", l); | |
838 | snprintf(rb, sizeof rb, "%d", r); | |
839 | eventlog("volume", lb, rb, (char *)0); | |
460b9539 | 840 | } |
841 | return 1; | |
842 | } | |
843 | ||
397ef7bb RK |
844 | /** @brief Called when data arrives on a log connection |
845 | * | |
846 | * We just discard all such data. The client may occasionally send data as a | |
847 | * keepalive. | |
848 | */ | |
849 | static int logging_reader_callback(ev_source attribute((unused)) *ev, | |
460b9539 | 850 | ev_reader *reader, |
397ef7bb | 851 | void attribute((unused)) *ptr, |
460b9539 | 852 | size_t bytes, |
397ef7bb RK |
853 | int attribute((unused)) eof, |
854 | void attribute((unused)) *u) { | |
460b9539 | 855 | struct conn *c = u; |
856 | ||
397ef7bb RK |
857 | ev_reader_consume(reader, bytes); |
858 | if(eof) { | |
859 | /* Oops, that's all for now */ | |
8d8b8c1f | 860 | D(("logging reader eof")); |
397ef7bb | 861 | if(c->w) { |
8d8b8c1f | 862 | D(("close writer")); |
397ef7bb RK |
863 | ev_writer_close(c->w); |
864 | c->w = 0; | |
865 | } | |
866 | c->r = 0; | |
0126692c | 867 | remove_connection(c); |
f6033c46 | 868 | } |
397ef7bb | 869 | return 0; |
460b9539 | 870 | } |
871 | ||
872 | static void logclient(const char *msg, void *user) { | |
873 | struct conn *c = user; | |
874 | ||
f6033c46 RK |
875 | if(!c->w || !c->r) { |
876 | /* This connection has gone up in smoke for some reason */ | |
877 | eventlog_remove(c->lo); | |
ad492e00 | 878 | c->lo = 0; |
f6033c46 RK |
879 | return; |
880 | } | |
75e7b7c3 RK |
881 | /* user_* messages are restricted */ |
882 | if(!strncmp(msg, "user_", 5)) { | |
58d2aad2 RK |
883 | /* They are only sent to admin users */ |
884 | if(!(c->rights & RIGHT_ADMIN)) | |
885 | return; | |
886 | /* They are not sent over TCP connections unless remote user-management is | |
887 | * enabled */ | |
888 | if(!config->remote_userman && !(c->rights & RIGHT__LOCAL)) | |
889 | return; | |
890 | } | |
460b9539 | 891 | sink_printf(ev_writer_sink(c->w), "%"PRIxMAX" %s\n", |
892 | (uintmax_t)time(0), msg); | |
893 | } | |
894 | ||
895 | static int c_log(struct conn *c, | |
896 | char attribute((unused)) **vec, | |
897 | int attribute((unused)) nvec) { | |
898 | time_t now; | |
899 | ||
900 | sink_writes(ev_writer_sink(c->w), "254 OK\n"); | |
901 | /* pump out initial state */ | |
902 | time(&now); | |
903 | sink_printf(ev_writer_sink(c->w), "%"PRIxMAX" state %s\n", | |
904 | (uintmax_t)now, | |
905 | playing_is_enabled() ? "enable_play" : "disable_play"); | |
906 | sink_printf(ev_writer_sink(c->w), "%"PRIxMAX" state %s\n", | |
907 | (uintmax_t)now, | |
908 | random_is_enabled() ? "enable_random" : "disable_random"); | |
909 | sink_printf(ev_writer_sink(c->w), "%"PRIxMAX" state %s\n", | |
910 | (uintmax_t)now, | |
911 | paused ? "pause" : "resume"); | |
5abe307a RK |
912 | if(playing) |
913 | sink_printf(ev_writer_sink(c->w), "%"PRIxMAX" state playing\n", | |
914 | (uintmax_t)now); | |
df44d69b RK |
915 | /* Initial volume */ |
916 | sink_printf(ev_writer_sink(c->w), "%"PRIxMAX" volume %d %d\n", | |
917 | (uintmax_t)now, volume_left, volume_right); | |
460b9539 | 918 | c->lo = xmalloc(sizeof *c->lo); |
919 | c->lo->fn = logclient; | |
920 | c->lo->user = c; | |
921 | eventlog_add(c->lo); | |
922 | c->reader = logging_reader_callback; | |
923 | return 0; | |
924 | } | |
925 | ||
eb5dc014 RK |
926 | /** @brief Test whether a move is allowed |
927 | * @param c Connection | |
928 | * @param qs List of IDs on queue | |
929 | * @param nqs Number of IDs | |
930 | * @return 0 if move is prohibited, non-0 if it is allowed | |
931 | */ | |
932 | static int has_move_rights(struct conn *c, struct queue_entry **qs, int nqs) { | |
eb5dc014 RK |
933 | for(; nqs > 0; ++qs, --nqs) { |
934 | struct queue_entry *const q = *qs; | |
935 | ||
938d8157 | 936 | if(!right_movable(c->rights, c->who, q)) |
937 | return 0; | |
eb5dc014 | 938 | } |
938d8157 | 939 | return 1; |
eb5dc014 RK |
940 | } |
941 | ||
460b9539 | 942 | static int c_move(struct conn *c, |
943 | char **vec, | |
944 | int attribute((unused)) nvec) { | |
945 | struct queue_entry *q; | |
946 | int n; | |
947 | ||
460b9539 | 948 | if(!(q = queue_find(vec[0]))) { |
949 | sink_writes(ev_writer_sink(c->w), "550 no such track on the queue\n"); | |
950 | return 1; | |
951 | } | |
eb5dc014 | 952 | if(!has_move_rights(c, &q, 1)) { |
834e7c4a | 953 | error(0, "%s attempted move but lacks required rights", c->who); |
eb5dc014 | 954 | sink_writes(ev_writer_sink(c->w), |
b4a80f69 | 955 | "510 Not authorized to move that track\n"); |
eb5dc014 RK |
956 | return 1; |
957 | } | |
460b9539 | 958 | n = queue_move(q, atoi(vec[1]), c->who); |
460b9539 | 959 | sink_printf(ev_writer_sink(c->w), "252 %d\n", n); |
960 | /* If we've moved to the head of the queue then prepare the track. */ | |
961 | if(q == qhead.next) | |
962 | prepare(c->ev, q); | |
963 | return 1; | |
964 | } | |
965 | ||
966 | static int c_moveafter(struct conn *c, | |
967 | char **vec, | |
968 | int attribute((unused)) nvec) { | |
969 | struct queue_entry *q, **qs; | |
970 | int n; | |
971 | ||
460b9539 | 972 | if(vec[0][0]) { |
973 | if(!(q = queue_find(vec[0]))) { | |
974 | sink_writes(ev_writer_sink(c->w), "550 no such track on the queue\n"); | |
975 | return 1; | |
976 | } | |
977 | } else | |
978 | q = 0; | |
979 | ++vec; | |
980 | --nvec; | |
981 | qs = xcalloc(nvec, sizeof *qs); | |
982 | for(n = 0; n < nvec; ++n) | |
983 | if(!(qs[n] = queue_find(vec[n]))) { | |
984 | sink_writes(ev_writer_sink(c->w), "550 no such track on the queue\n"); | |
985 | return 1; | |
986 | } | |
eb5dc014 | 987 | if(!has_move_rights(c, qs, nvec)) { |
834e7c4a | 988 | error(0, "%s attempted moveafter but lacks required rights", c->who); |
eb5dc014 | 989 | sink_writes(ev_writer_sink(c->w), |
b4a80f69 | 990 | "510 Not authorized to move those tracks\n"); |
eb5dc014 RK |
991 | return 1; |
992 | } | |
460b9539 | 993 | queue_moveafter(q, nvec, qs, c->who); |
460b9539 | 994 | sink_printf(ev_writer_sink(c->w), "250 Moved tracks\n"); |
995 | /* If we've moved to the head of the queue then prepare the track. */ | |
996 | if(q == qhead.next) | |
997 | prepare(c->ev, q); | |
998 | return 1; | |
999 | } | |
1000 | ||
1001 | static int c_part(struct conn *c, | |
1002 | char **vec, | |
1003 | int attribute((unused)) nvec) { | |
27ed5a69 RK |
1004 | const char *track; |
1005 | ||
1006 | if(!(track = trackdb_resolve(vec[0]))) { | |
1007 | sink_writes(ev_writer_sink(c->w), "550 cannot resolve track\n"); | |
1008 | return 1; | |
1009 | } | |
460b9539 | 1010 | sink_printf(ev_writer_sink(c->w), "252 %s\n", |
27ed5a69 | 1011 | quoteutf8(trackdb_getpart(track, vec[1], vec[2]))); |
460b9539 | 1012 | return 1; |
1013 | } | |
1014 | ||
1015 | static int c_resolve(struct conn *c, | |
1016 | char **vec, | |
1017 | int attribute((unused)) nvec) { | |
1018 | const char *track; | |
1019 | ||
1020 | if(!(track = trackdb_resolve(vec[0]))) { | |
1021 | sink_writes(ev_writer_sink(c->w), "550 cannot resolve track\n"); | |
1022 | return 1; | |
1023 | } | |
7b32e917 | 1024 | sink_printf(ev_writer_sink(c->w), "252 %s\n", quoteutf8(track)); |
460b9539 | 1025 | return 1; |
1026 | } | |
1027 | ||
1028 | static int c_tags(struct conn *c, | |
1029 | char attribute((unused)) **vec, | |
1030 | int attribute((unused)) nvec) { | |
1031 | char **tags = trackdb_alltags(); | |
1032 | ||
1033 | sink_printf(ev_writer_sink(c->w), "253 Tag list follows\n"); | |
1034 | while(*tags) { | |
1035 | sink_printf(ev_writer_sink(c->w), "%s%s\n", | |
1036 | **tags == '.' ? "." : "", *tags); | |
1037 | ++tags; | |
1038 | } | |
1039 | sink_writes(ev_writer_sink(c->w), ".\n"); | |
1040 | return 1; /* completed */ | |
460b9539 | 1041 | } |
1042 | ||
1043 | static int c_set_global(struct conn *c, | |
1044 | char **vec, | |
1045 | int attribute((unused)) nvec) { | |
f9635e06 RK |
1046 | if(vec[0][0] == '_') { |
1047 | sink_writes(ev_writer_sink(c->w), "550 cannot set internal global preferences\n"); | |
1048 | return 1; | |
1049 | } | |
460b9539 | 1050 | trackdb_set_global(vec[0], vec[1], c->who); |
1051 | sink_printf(ev_writer_sink(c->w), "250 OK\n"); | |
1052 | return 1; | |
1053 | } | |
1054 | ||
1055 | static int c_get_global(struct conn *c, | |
1056 | char **vec, | |
1057 | int attribute((unused)) nvec) { | |
1058 | const char *s = trackdb_get_global(vec[0]); | |
1059 | ||
1060 | if(s) | |
7b32e917 | 1061 | sink_printf(ev_writer_sink(c->w), "252 %s\n", quoteutf8(s)); |
460b9539 | 1062 | else |
fb1bc1f5 | 1063 | sink_writes(ev_writer_sink(c->w), "555 not found\n"); |
460b9539 | 1064 | return 1; |
1065 | } | |
1066 | ||
7858930d | 1067 | static int c_nop(struct conn *c, |
1068 | char attribute((unused)) **vec, | |
1069 | int attribute((unused)) nvec) { | |
1070 | sink_printf(ev_writer_sink(c->w), "250 Quack\n"); | |
1071 | return 1; | |
1072 | } | |
1073 | ||
2a10b70b RK |
1074 | static int c_new(struct conn *c, |
1075 | char **vec, | |
1076 | int nvec) { | |
d742bb47 RK |
1077 | int max, n; |
1078 | char **tracks; | |
2a10b70b | 1079 | |
d742bb47 RK |
1080 | if(nvec > 0) |
1081 | max = atoi(vec[0]); | |
1082 | else | |
1083 | max = INT_MAX; | |
1084 | if(max <= 0 || max > config->new_max) | |
1085 | max = config->new_max; | |
1086 | tracks = trackdb_new(0, max); | |
2a10b70b | 1087 | sink_printf(ev_writer_sink(c->w), "253 New track list follows\n"); |
d742bb47 | 1088 | n = 0; |
2a10b70b RK |
1089 | while(*tracks) { |
1090 | sink_printf(ev_writer_sink(c->w), "%s%s\n", | |
1091 | **tracks == '.' ? "." : "", *tracks); | |
1092 | ++tracks; | |
1093 | } | |
1094 | sink_writes(ev_writer_sink(c->w), ".\n"); | |
1095 | return 1; /* completed */ | |
1096 | ||
1097 | } | |
1098 | ||
ca831831 RK |
1099 | static int c_rtp_address(struct conn *c, |
1100 | char attribute((unused)) **vec, | |
1101 | int attribute((unused)) nvec) { | |
b50cfb8a | 1102 | if(api == &uaudio_rtp) { |
ca831831 RK |
1103 | sink_printf(ev_writer_sink(c->w), "252 %s %s\n", |
1104 | quoteutf8(config->broadcast.s[0]), | |
1105 | quoteutf8(config->broadcast.s[1])); | |
1106 | } else | |
1107 | sink_writes(ev_writer_sink(c->w), "550 No RTP\n"); | |
1108 | return 1; | |
1109 | } | |
b12be54a RK |
1110 | |
1111 | static int c_cookie(struct conn *c, | |
1112 | char **vec, | |
1113 | int attribute((unused)) nvec) { | |
1114 | const char *host; | |
1115 | char *user; | |
eb5dc014 | 1116 | rights_type rights; |
b12be54a RK |
1117 | |
1118 | /* Can't log in twice on the same connection */ | |
1119 | if(c->who) { | |
1120 | sink_writes(ev_writer_sink(c->w), "530 already authenticated\n"); | |
1121 | return 1; | |
1122 | } | |
1123 | /* Get some kind of peer identifcation */ | |
1124 | if(!(host = connection_host(c))) { | |
1125 | sink_writes(ev_writer_sink(c->w), "530 authentication failure\n"); | |
1126 | return 1; | |
1127 | } | |
1128 | /* Check the cookie */ | |
eb5dc014 | 1129 | user = verify_cookie(vec[0], &rights); |
b12be54a RK |
1130 | if(!user) { |
1131 | sink_writes(ev_writer_sink(c->w), "530 authentication failure\n"); | |
1132 | return 1; | |
1133 | } | |
1134 | /* Log in */ | |
e48c28bb | 1135 | c->who = user; |
b12be54a | 1136 | c->cookie = vec[0]; |
eb5dc014 | 1137 | c->rights = rights; |
30365519 | 1138 | if(strcmp(host, "local")) |
b12be54a | 1139 | info("S%x %s connected with cookie from %s", c->tag, user, host); |
30365519 | 1140 | else |
eb5dc014 | 1141 | c->rights |= RIGHT__LOCAL; |
0227f67d RK |
1142 | /* Response contains username so client knows who they are acting as */ |
1143 | sink_printf(ev_writer_sink(c->w), "232 %s\n", quoteutf8(user)); | |
b12be54a RK |
1144 | return 1; |
1145 | } | |
1146 | ||
1147 | static int c_make_cookie(struct conn *c, | |
1148 | char attribute((unused)) **vec, | |
1149 | int attribute((unused)) nvec) { | |
1150 | const char *cookie = make_cookie(c->who); | |
1151 | ||
1152 | if(cookie) | |
eb5dc014 | 1153 | sink_printf(ev_writer_sink(c->w), "252 %s\n", quoteutf8(cookie)); |
b12be54a RK |
1154 | else |
1155 | sink_writes(ev_writer_sink(c->w), "550 Cannot create cookie\n"); | |
1156 | return 1; | |
1157 | } | |
1158 | ||
1159 | static int c_revoke(struct conn *c, | |
1160 | char attribute((unused)) **vec, | |
1161 | int attribute((unused)) nvec) { | |
1162 | if(c->cookie) { | |
1163 | revoke_cookie(c->cookie); | |
1164 | sink_writes(ev_writer_sink(c->w), "250 OK\n"); | |
1165 | } else | |
1166 | sink_writes(ev_writer_sink(c->w), "550 Did not log in with cookie\n"); | |
1167 | return 1; | |
1168 | } | |
1169 | ||
f0feb22e RK |
1170 | static int c_adduser(struct conn *c, |
1171 | char **vec, | |
0f55e905 RK |
1172 | int nvec) { |
1173 | const char *rights; | |
1174 | ||
810b8083 | 1175 | if(!config->remote_userman && !(c->rights & RIGHT__LOCAL)) { |
39e7dcfb | 1176 | error(0, "S%x: remote adduser", c->tag); |
810b8083 RK |
1177 | sink_writes(ev_writer_sink(c->w), "550 Remote user management is disabled\n"); |
1178 | return 1; | |
1179 | } | |
0f55e905 RK |
1180 | if(nvec > 2) { |
1181 | rights = vec[2]; | |
1182 | if(parse_rights(vec[2], 0, 1)) { | |
1183 | sink_writes(ev_writer_sink(c->w), "550 Invalid rights list\n"); | |
1184 | return -1; | |
1185 | } | |
1186 | } else | |
1187 | rights = config->default_rights; | |
1188 | if(trackdb_adduser(vec[0], vec[1], rights, | |
ba39faf6 | 1189 | 0/*email*/, 0/*confirmation*/)) |
f0feb22e RK |
1190 | sink_writes(ev_writer_sink(c->w), "550 Cannot create user\n"); |
1191 | else | |
1192 | sink_writes(ev_writer_sink(c->w), "250 User created\n"); | |
1193 | return 1; | |
1194 | } | |
1195 | ||
1196 | static int c_deluser(struct conn *c, | |
1197 | char **vec, | |
1198 | int attribute((unused)) nvec) { | |
0126692c | 1199 | struct conn *d; |
1200 | ||
810b8083 | 1201 | if(!config->remote_userman && !(c->rights & RIGHT__LOCAL)) { |
39e7dcfb | 1202 | error(0, "S%x: remote deluser", c->tag); |
810b8083 RK |
1203 | sink_writes(ev_writer_sink(c->w), "550 Remote user management is disabled\n"); |
1204 | return 1; | |
1205 | } | |
0126692c | 1206 | if(trackdb_deluser(vec[0])) { |
ba39faf6 | 1207 | sink_writes(ev_writer_sink(c->w), "550 Cannot delete user\n"); |
0126692c | 1208 | return 1; |
1209 | } | |
1210 | /* Zap connections belonging to deleted user */ | |
1211 | for(d = connections; d; d = d->next) | |
1212 | if(!strcmp(d->who, vec[0])) | |
1213 | d->rights = 0; | |
1214 | sink_writes(ev_writer_sink(c->w), "250 User deleted\n"); | |
f0feb22e RK |
1215 | return 1; |
1216 | } | |
1217 | ||
1218 | static int c_edituser(struct conn *c, | |
5df73aeb | 1219 | char **vec, |
f0feb22e | 1220 | int attribute((unused)) nvec) { |
0126692c | 1221 | struct conn *d; |
1222 | ||
810b8083 | 1223 | if(!config->remote_userman && !(c->rights & RIGHT__LOCAL)) { |
39e7dcfb | 1224 | error(0, "S%x: remote edituser", c->tag); |
810b8083 RK |
1225 | sink_writes(ev_writer_sink(c->w), "550 Remote user management is disabled\n"); |
1226 | return 1; | |
1227 | } | |
eb5dc014 RK |
1228 | /* RIGHT_ADMIN can do anything; otherwise you can only set your own email |
1229 | * address and password. */ | |
1230 | if((c->rights & RIGHT_ADMIN) | |
5df73aeb RK |
1231 | || (!strcmp(c->who, vec[0]) |
1232 | && (!strcmp(vec[1], "email") | |
1233 | || !strcmp(vec[1], "password")))) { | |
0126692c | 1234 | if(trackdb_edituserinfo(vec[0], vec[1], vec[2])) { |
5df73aeb | 1235 | sink_writes(ev_writer_sink(c->w), "550 Failed to change setting\n"); |
0126692c | 1236 | return 1; |
1237 | } | |
1238 | if(!strcmp(vec[1], "password")) { | |
1239 | /* Zap all connections for this user after a password change */ | |
1240 | for(d = connections; d; d = d->next) | |
1241 | if(!strcmp(d->who, vec[0])) | |
1242 | d->rights = 0; | |
1243 | } else if(!strcmp(vec[1], "rights")) { | |
1244 | /* Update rights for this user */ | |
1245 | rights_type r; | |
1246 | ||
ad492e00 RK |
1247 | if(!parse_rights(vec[2], &r, 1)) { |
1248 | const char *new_rights = rights_string(r); | |
1249 | for(d = connections; d; d = d->next) { | |
1250 | if(!strcmp(d->who, vec[0])) { | |
1251 | /* Update rights */ | |
0126692c | 1252 | d->rights = r; |
ad492e00 RK |
1253 | /* Notify any log connections */ |
1254 | if(d->lo) | |
1255 | sink_printf(ev_writer_sink(d->w), | |
75e7b7c3 | 1256 | "%"PRIxMAX" rights_changed %s\n", |
ad492e00 | 1257 | (uintmax_t)time(0), |
75e7b7c3 | 1258 | quoteutf8(new_rights)); |
ad492e00 RK |
1259 | } |
1260 | } | |
1261 | } | |
0126692c | 1262 | } |
1263 | sink_writes(ev_writer_sink(c->w), "250 OK\n"); | |
834e7c4a | 1264 | } else { |
1265 | error(0, "%s attempted edituser but lacks required rights", c->who); | |
b4a80f69 | 1266 | sink_writes(ev_writer_sink(c->w), "510 Restricted to administrators\n"); |
834e7c4a | 1267 | } |
f0feb22e RK |
1268 | return 1; |
1269 | } | |
1270 | ||
1271 | static int c_userinfo(struct conn *c, | |
1272 | char attribute((unused)) **vec, | |
1273 | int attribute((unused)) nvec) { | |
5df73aeb RK |
1274 | struct kvp *k; |
1275 | const char *value; | |
eb5dc014 | 1276 | |
39e7dcfb RK |
1277 | /* We allow remote querying of rights so that clients can figure out what |
1278 | * they're allowed to do */ | |
1279 | if(!config->remote_userman | |
1280 | && !(c->rights & RIGHT__LOCAL) | |
1281 | && strcmp(vec[1], "rights")) { | |
1282 | error(0, "S%x: remote userinfo %s %s", c->tag, vec[0], vec[1]); | |
810b8083 RK |
1283 | sink_writes(ev_writer_sink(c->w), "550 Remote user management is disabled\n"); |
1284 | return 1; | |
1285 | } | |
eb5dc014 | 1286 | /* RIGHT_ADMIN allows anything; otherwise you can only get your own email |
29f5fbd2 | 1287 | * address and rights list. */ |
eb5dc014 | 1288 | if((c->rights & RIGHT_ADMIN) |
5df73aeb RK |
1289 | || (!strcmp(c->who, vec[0]) |
1290 | && (!strcmp(vec[1], "email") | |
1291 | || !strcmp(vec[1], "rights")))) { | |
1292 | if((k = trackdb_getuserinfo(vec[0]))) | |
1293 | if((value = kvp_get(k, vec[1]))) | |
1294 | sink_printf(ev_writer_sink(c->w), "252 %s\n", quoteutf8(value)); | |
1295 | else | |
1296 | sink_writes(ev_writer_sink(c->w), "555 Not set\n"); | |
1297 | else | |
1298 | sink_writes(ev_writer_sink(c->w), "550 No such user\n"); | |
834e7c4a | 1299 | } else { |
1300 | error(0, "%s attempted userinfo but lacks required rights", c->who); | |
b4a80f69 | 1301 | sink_writes(ev_writer_sink(c->w), "510 Restricted to administrators\n"); |
834e7c4a | 1302 | } |
f0feb22e RK |
1303 | return 1; |
1304 | } | |
1305 | ||
c3be4f19 RK |
1306 | static int c_users(struct conn *c, |
1307 | char attribute((unused)) **vec, | |
1308 | int attribute((unused)) nvec) { | |
1309 | /* TODO de-dupe with c_tags */ | |
1310 | char **users = trackdb_listusers(); | |
1311 | ||
1312 | sink_writes(ev_writer_sink(c->w), "253 User list follows\n"); | |
1313 | while(*users) { | |
1314 | sink_printf(ev_writer_sink(c->w), "%s%s\n", | |
1315 | **users == '.' ? "." : "", *users); | |
1316 | ++users; | |
1317 | } | |
1318 | sink_writes(ev_writer_sink(c->w), ".\n"); | |
1319 | return 1; /* completed */ | |
1320 | } | |
1321 | ||
86e3aea7 | 1322 | /** @brief Base64 mapping table for confirmation strings |
1323 | * | |
1324 | * This is used with generic_to_base64() and generic_base64(). We cannot use | |
1325 | * the MIME table as that contains '+' and '=' which get quoted when | |
1326 | * URL-encoding. (The CGI still does the URL encoding but it is desirable to | |
1327 | * avoid it being necessary.) | |
1328 | */ | |
1329 | static const char confirm_base64_table[] = | |
1330 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789/.*"; | |
1331 | ||
ba39faf6 RK |
1332 | static int c_register(struct conn *c, |
1333 | char **vec, | |
1334 | int attribute((unused)) nvec) { | |
1335 | char *buf, *cs; | |
1336 | size_t bufsize; | |
1337 | int offset; | |
1338 | ||
1339 | /* The confirmation string is base64(username;nonce) */ | |
10114017 | 1340 | bufsize = strlen(vec[0]) + CONFIRM_SIZE + 2; |
ba39faf6 RK |
1341 | buf = xmalloc_noptr(bufsize); |
1342 | offset = byte_snprintf(buf, bufsize, "%s;", vec[0]); | |
10114017 | 1343 | gcry_randomize(buf + offset, CONFIRM_SIZE, GCRY_STRONG_RANDOM); |
86e3aea7 | 1344 | cs = generic_to_base64((uint8_t *)buf, offset + CONFIRM_SIZE, |
1345 | confirm_base64_table); | |
ba39faf6 RK |
1346 | if(trackdb_adduser(vec[0], vec[1], config->default_rights, vec[2], cs)) |
1347 | sink_writes(ev_writer_sink(c->w), "550 Cannot create user\n"); | |
1348 | else | |
1349 | sink_printf(ev_writer_sink(c->w), "252 %s\n", quoteutf8(cs)); | |
1350 | return 1; | |
1351 | } | |
1352 | ||
1353 | static int c_confirm(struct conn *c, | |
1354 | char **vec, | |
1355 | int attribute((unused)) nvec) { | |
1356 | size_t nuser; | |
1357 | char *user, *sep; | |
30365519 | 1358 | rights_type rights; |
1359 | const char *host; | |
ba39faf6 | 1360 | |
30365519 | 1361 | /* Get some kind of peer identifcation */ |
1362 | if(!(host = connection_host(c))) { | |
1363 | sink_writes(ev_writer_sink(c->w), "530 Authentication failure\n"); | |
1364 | return 1; | |
1365 | } | |
86e3aea7 | 1366 | if(!(user = generic_base64(vec[0], &nuser, confirm_base64_table)) |
ba39faf6 RK |
1367 | || !(sep = memchr(user, ';', nuser))) { |
1368 | sink_writes(ev_writer_sink(c->w), "550 Malformed confirmation string\n"); | |
1369 | return 1; | |
1370 | } | |
1371 | *sep = 0; | |
30365519 | 1372 | if(trackdb_confirm(user, vec[0], &rights)) |
ba39faf6 | 1373 | sink_writes(ev_writer_sink(c->w), "550 Incorrect confirmation string\n"); |
30365519 | 1374 | else { |
1375 | c->who = user; | |
1376 | c->cookie = 0; | |
1377 | c->rights = rights; | |
1378 | if(strcmp(host, "local")) | |
1379 | info("S%x %s confirmed from %s", c->tag, user, host); | |
1380 | else | |
1381 | c->rights |= RIGHT__LOCAL; | |
1382 | /* Response contains username so client knows who they are acting as */ | |
1383 | sink_printf(ev_writer_sink(c->w), "232 %s\n", quoteutf8(user)); | |
1384 | } | |
ba39faf6 RK |
1385 | return 1; |
1386 | } | |
6207d2f3 | 1387 | |
1388 | static int sent_reminder(ev_source attribute((unused)) *ev, | |
1389 | pid_t attribute((unused)) pid, | |
1390 | int status, | |
1391 | const struct rusage attribute((unused)) *rusage, | |
1392 | void *u) { | |
1393 | struct conn *const c = u; | |
1394 | ||
1395 | /* Tell the client what went down */ | |
1396 | if(!status) { | |
1397 | sink_writes(ev_writer_sink(c->w), "250 OK\n"); | |
1398 | } else { | |
1399 | error(0, "reminder subprocess %s", wstat(status)); | |
1400 | sink_writes(ev_writer_sink(c->w), "550 Cannot send a reminder email\n"); | |
1401 | } | |
1402 | /* Re-enable this connection */ | |
1403 | ev_reader_enable(c->r); | |
1404 | return 0; | |
1405 | } | |
1406 | ||
1407 | static int c_reminder(struct conn *c, | |
1408 | char **vec, | |
1409 | int attribute((unused)) nvec) { | |
1410 | struct kvp *k; | |
1411 | const char *password, *email, *text, *encoding, *charset, *content_type; | |
1412 | const time_t *last; | |
1413 | time_t now; | |
1414 | pid_t pid; | |
1415 | ||
1416 | static hash *last_reminder; | |
1417 | ||
1418 | if(!config->mail_sender) { | |
1419 | error(0, "cannot send password reminders because mail_sender not set"); | |
1420 | sink_writes(ev_writer_sink(c->w), "550 Cannot send a reminder email\n"); | |
1421 | return 1; | |
1422 | } | |
1423 | if(!(k = trackdb_getuserinfo(vec[0]))) { | |
1424 | error(0, "reminder for user '%s' who does not exist", vec[0]); | |
1425 | sink_writes(ev_writer_sink(c->w), "550 Cannot send a reminder email\n"); | |
1426 | return 1; | |
1427 | } | |
1428 | if(!(email = kvp_get(k, "email")) | |
33e95f03 | 1429 | || !email_valid(email)) { |
6207d2f3 | 1430 | error(0, "user '%s' has no valid email address", vec[0]); |
1431 | sink_writes(ev_writer_sink(c->w), "550 Cannot send a reminder email\n"); | |
1432 | return 1; | |
1433 | } | |
1434 | if(!(password = kvp_get(k, "password")) | |
1435 | || !*password) { | |
1436 | error(0, "user '%s' has no password", vec[0]); | |
1437 | sink_writes(ev_writer_sink(c->w), "550 Cannot send a reminder email\n"); | |
1438 | return 1; | |
1439 | } | |
1440 | /* Rate-limit reminders. This hash is bounded in size by the number of | |
1441 | * users. If this is actually a problem for anyone then we can periodically | |
1442 | * clean it. */ | |
1443 | if(!last_reminder) | |
1444 | last_reminder = hash_new(sizeof (time_t)); | |
1445 | last = hash_find(last_reminder, vec[0]); | |
1446 | time(&now); | |
1447 | if(last && now < *last + config->reminder_interval) { | |
1448 | error(0, "sent a password reminder to '%s' too recently", vec[0]); | |
1449 | sink_writes(ev_writer_sink(c->w), "550 Cannot send a reminder email\n"); | |
1450 | return 1; | |
1451 | } | |
1452 | /* Send the reminder */ | |
1453 | /* TODO this should be templatized and to some extent merged with | |
1454 | * the code in act_register() */ | |
1455 | byte_xasprintf((char **)&text, | |
1456 | "Someone requested that you be sent a reminder of your DisOrder password.\n" | |
1457 | "Your password is:\n" | |
1458 | "\n" | |
1459 | " %s\n", password); | |
1460 | if(!(text = mime_encode_text(text, &charset, &encoding))) | |
1461 | fatal(0, "cannot encode email"); | |
1462 | byte_xasprintf((char **)&content_type, "text/plain;charset=%s", | |
1463 | quote822(charset, 0)); | |
1464 | pid = sendmail_subprocess("", config->mail_sender, email, | |
1465 | "DisOrder password reminder", | |
1466 | encoding, content_type, text); | |
1467 | if(pid < 0) { | |
1468 | sink_writes(ev_writer_sink(c->w), "550 Cannot send a reminder email\n"); | |
1469 | return 1; | |
1470 | } | |
1471 | hash_add(last_reminder, vec[0], &now, HASH_INSERT_OR_REPLACE); | |
1472 | info("sending a passsword reminder to user '%s'", vec[0]); | |
1473 | /* We can only continue when the subprocess finishes */ | |
1474 | ev_child(c->ev, pid, 0, sent_reminder, c); | |
1475 | return 0; | |
1476 | } | |
1477 | ||
2b33c4cf RK |
1478 | static int c_schedule_list(struct conn *c, |
1479 | char attribute((unused)) **vec, | |
1480 | int attribute((unused)) nvec) { | |
1481 | char **ids = schedule_list(0); | |
1482 | sink_writes(ev_writer_sink(c->w), "253 ID list follows\n"); | |
1483 | while(*ids) | |
1484 | sink_printf(ev_writer_sink(c->w), "%s\n", *ids++); | |
1485 | sink_writes(ev_writer_sink(c->w), ".\n"); | |
1486 | return 1; /* completed */ | |
1487 | } | |
1488 | ||
1489 | static int c_schedule_get(struct conn *c, | |
1490 | char **vec, | |
1491 | int attribute((unused)) nvec) { | |
1492 | struct kvp *actiondata = schedule_get(vec[0]), *k; | |
1493 | ||
1494 | if(!actiondata) { | |
1495 | sink_writes(ev_writer_sink(c->w), "555 No such event\n"); | |
1496 | return 1; /* completed */ | |
1497 | } | |
1498 | /* Scheduled events are public information. Anyone with RIGHT_READ can see | |
1499 | * them. */ | |
1500 | sink_writes(ev_writer_sink(c->w), "253 Event information follows\n"); | |
1501 | for(k = actiondata; k; k = k->next) | |
1502 | sink_printf(ev_writer_sink(c->w), " %s %s\n", | |
1503 | quoteutf8(k->name), quoteutf8(k->value)); | |
1504 | sink_writes(ev_writer_sink(c->w), ".\n"); | |
1505 | return 1; /* completed */ | |
1506 | } | |
1507 | ||
1508 | static int c_schedule_del(struct conn *c, | |
1509 | char **vec, | |
1510 | int attribute((unused)) nvec) { | |
1511 | struct kvp *actiondata = schedule_get(vec[0]); | |
1512 | ||
1513 | if(!actiondata) { | |
1514 | sink_writes(ev_writer_sink(c->w), "555 No such event\n"); | |
1515 | return 1; /* completed */ | |
1516 | } | |
1517 | /* If you have admin rights you can delete anything. If you don't then you | |
1518 | * can only delete your own scheduled events. */ | |
1519 | if(!(c->rights & RIGHT_ADMIN)) { | |
1520 | const char *who = kvp_get(actiondata, "who"); | |
1521 | ||
1522 | if(!who || !c->who || strcmp(who, c->who)) { | |
1523 | sink_writes(ev_writer_sink(c->w), "551 Not authorized\n"); | |
1524 | return 1; /* completed */ | |
1525 | } | |
1526 | } | |
1527 | if(schedule_del(vec[0])) | |
1528 | sink_writes(ev_writer_sink(c->w), "550 Could not delete scheduled event\n"); | |
1529 | else | |
1530 | sink_writes(ev_writer_sink(c->w), "250 Deleted\n"); | |
1531 | return 1; /* completed */ | |
1532 | } | |
1533 | ||
1534 | static int c_schedule_add(struct conn *c, | |
1535 | char **vec, | |
1536 | int nvec) { | |
1537 | struct kvp *actiondata = 0; | |
067eeb5f | 1538 | const char *id; |
2b33c4cf RK |
1539 | |
1540 | /* Standard fields */ | |
1541 | kvp_set(&actiondata, "who", c->who); | |
1542 | kvp_set(&actiondata, "when", vec[0]); | |
1543 | kvp_set(&actiondata, "priority", vec[1]); | |
1544 | kvp_set(&actiondata, "action", vec[2]); | |
1545 | /* Action-dependent fields */ | |
1546 | if(!strcmp(vec[2], "play")) { | |
1547 | if(nvec != 4) { | |
1548 | sink_writes(ev_writer_sink(c->w), "550 Wrong number of arguments\n"); | |
1549 | return 1; | |
1550 | } | |
1551 | if(!trackdb_exists(vec[3])) { | |
1552 | sink_writes(ev_writer_sink(c->w), "550 Track is not in database\n"); | |
1553 | return 1; | |
1554 | } | |
1555 | kvp_set(&actiondata, "track", vec[3]); | |
1556 | } else if(!strcmp(vec[2], "set-global")) { | |
1557 | if(nvec < 4 || nvec > 5) { | |
1558 | sink_writes(ev_writer_sink(c->w), "550 Wrong number of arguments\n"); | |
1559 | return 1; | |
1560 | } | |
1561 | kvp_set(&actiondata, "key", vec[3]); | |
1562 | if(nvec > 4) | |
1563 | kvp_set(&actiondata, "value", vec[4]); | |
1564 | } else { | |
1565 | sink_writes(ev_writer_sink(c->w), "550 Unknown action\n"); | |
1566 | return 1; | |
1567 | } | |
1568 | /* schedule_add() checks user rights */ | |
1569 | id = schedule_add(c->ev, actiondata); | |
1570 | if(!id) | |
1571 | sink_writes(ev_writer_sink(c->w), "550 Cannot add scheduled event\n"); | |
1572 | else | |
1573 | sink_printf(ev_writer_sink(c->w), "252 %s\n", id); | |
1574 | return 1; | |
1575 | } | |
1576 | ||
d42e98ca RK |
1577 | static int c_adopt(struct conn *c, |
1578 | char **vec, | |
1579 | int attribute((unused)) nvec) { | |
1580 | struct queue_entry *q; | |
1581 | ||
1582 | if(!c->who) { | |
1583 | sink_writes(ev_writer_sink(c->w), "550 no identity\n"); | |
1584 | return 1; | |
1585 | } | |
1586 | if(!(q = queue_find(vec[0]))) { | |
1587 | sink_writes(ev_writer_sink(c->w), "550 no such track on the queue\n"); | |
1588 | return 1; | |
1589 | } | |
1590 | if(q->origin != origin_random) { | |
1591 | sink_writes(ev_writer_sink(c->w), "550 not a random track\n"); | |
1592 | return 1; | |
1593 | } | |
1594 | q->origin = origin_adopted; | |
1595 | q->submitter = xstrdup(c->who); | |
1596 | eventlog("adopted", q->id, q->submitter, (char *)0); | |
1597 | queue_write(); | |
1598 | sink_writes(ev_writer_sink(c->w), "250 OK\n"); | |
1599 | return 1; | |
1600 | } | |
1601 | ||
460b9539 | 1602 | static const struct command { |
eb5dc014 | 1603 | /** @brief Command name */ |
460b9539 | 1604 | const char *name; |
eb5dc014 RK |
1605 | |
1606 | /** @brief Minimum number of arguments */ | |
1607 | int minargs; | |
1608 | ||
1609 | /** @brief Maximum number of arguments */ | |
1610 | int maxargs; | |
1611 | ||
1612 | /** @brief Function to process command */ | |
460b9539 | 1613 | int (*fn)(struct conn *, char **, int); |
eb5dc014 RK |
1614 | |
1615 | /** @brief Rights required to execute command | |
1616 | * | |
1617 | * 0 means that the command can be issued without logging in. If multiple | |
1618 | * bits are listed here any of those rights will do. | |
1619 | */ | |
1620 | rights_type rights; | |
460b9539 | 1621 | } commands[] = { |
0f55e905 | 1622 | { "adduser", 2, 3, c_adduser, RIGHT_ADMIN|RIGHT__LOCAL }, |
d42e98ca | 1623 | { "adopt", 1, 1, c_adopt, RIGHT_PLAY }, |
eb5dc014 | 1624 | { "allfiles", 0, 2, c_allfiles, RIGHT_READ }, |
ba39faf6 | 1625 | { "confirm", 1, 1, c_confirm, 0 }, |
b12be54a | 1626 | { "cookie", 1, 1, c_cookie, 0 }, |
eb5dc014 RK |
1627 | { "deluser", 1, 1, c_deluser, RIGHT_ADMIN|RIGHT__LOCAL }, |
1628 | { "dirs", 0, 2, c_dirs, RIGHT_READ }, | |
1629 | { "disable", 0, 1, c_disable, RIGHT_GLOBAL_PREFS }, | |
1630 | { "edituser", 3, 3, c_edituser, RIGHT_ADMIN|RIGHT_USERINFO }, | |
1631 | { "enable", 0, 0, c_enable, RIGHT_GLOBAL_PREFS }, | |
1632 | { "enabled", 0, 0, c_enabled, RIGHT_READ }, | |
1633 | { "exists", 1, 1, c_exists, RIGHT_READ }, | |
1634 | { "files", 0, 2, c_files, RIGHT_READ }, | |
1635 | { "get", 2, 2, c_get, RIGHT_READ }, | |
1636 | { "get-global", 1, 1, c_get_global, RIGHT_READ }, | |
1637 | { "length", 1, 1, c_length, RIGHT_READ }, | |
1638 | { "log", 0, 0, c_log, RIGHT_READ }, | |
1639 | { "make-cookie", 0, 0, c_make_cookie, RIGHT_READ }, | |
1640 | { "move", 2, 2, c_move, RIGHT_MOVE__MASK }, | |
1641 | { "moveafter", 1, INT_MAX, c_moveafter, RIGHT_MOVE__MASK }, | |
1642 | { "new", 0, 1, c_new, RIGHT_READ }, | |
1643 | { "nop", 0, 0, c_nop, 0 }, | |
1644 | { "part", 3, 3, c_part, RIGHT_READ }, | |
1645 | { "pause", 0, 0, c_pause, RIGHT_PAUSE }, | |
1646 | { "play", 1, 1, c_play, RIGHT_PLAY }, | |
1647 | { "playing", 0, 0, c_playing, RIGHT_READ }, | |
1648 | { "prefs", 1, 1, c_prefs, RIGHT_READ }, | |
1649 | { "queue", 0, 0, c_queue, RIGHT_READ }, | |
1650 | { "random-disable", 0, 0, c_random_disable, RIGHT_GLOBAL_PREFS }, | |
1651 | { "random-enable", 0, 0, c_random_enable, RIGHT_GLOBAL_PREFS }, | |
1652 | { "random-enabled", 0, 0, c_random_enabled, RIGHT_READ }, | |
1653 | { "recent", 0, 0, c_recent, RIGHT_READ }, | |
1654 | { "reconfigure", 0, 0, c_reconfigure, RIGHT_ADMIN }, | |
ba39faf6 | 1655 | { "register", 3, 3, c_register, RIGHT_REGISTER|RIGHT__LOCAL }, |
6207d2f3 | 1656 | { "reminder", 1, 1, c_reminder, RIGHT__LOCAL }, |
eb5dc014 | 1657 | { "remove", 1, 1, c_remove, RIGHT_REMOVE__MASK }, |
dd9af5cb | 1658 | { "rescan", 0, INT_MAX, c_rescan, RIGHT_RESCAN }, |
eb5dc014 RK |
1659 | { "resolve", 1, 1, c_resolve, RIGHT_READ }, |
1660 | { "resume", 0, 0, c_resume, RIGHT_PAUSE }, | |
1661 | { "revoke", 0, 0, c_revoke, RIGHT_READ }, | |
1662 | { "rtp-address", 0, 0, c_rtp_address, 0 }, | |
2b33c4cf RK |
1663 | { "schedule-add", 3, INT_MAX, c_schedule_add, RIGHT_READ }, |
1664 | { "schedule-del", 1, 1, c_schedule_del, RIGHT_READ }, | |
1665 | { "schedule-get", 1, 1, c_schedule_get, RIGHT_READ }, | |
1666 | { "schedule-list", 0, 0, c_schedule_list, RIGHT_READ }, | |
eb5dc014 RK |
1667 | { "scratch", 0, 1, c_scratch, RIGHT_SCRATCH__MASK }, |
1668 | { "search", 1, 1, c_search, RIGHT_READ }, | |
1669 | { "set", 3, 3, c_set, RIGHT_PREFS, }, | |
1670 | { "set-global", 2, 2, c_set_global, RIGHT_GLOBAL_PREFS }, | |
1671 | { "shutdown", 0, 0, c_shutdown, RIGHT_ADMIN }, | |
1672 | { "stats", 0, 0, c_stats, RIGHT_READ }, | |
1673 | { "tags", 0, 0, c_tags, RIGHT_READ }, | |
1674 | { "unset", 2, 2, c_set, RIGHT_PREFS }, | |
1675 | { "unset-global", 1, 1, c_set_global, RIGHT_GLOBAL_PREFS }, | |
460b9539 | 1676 | { "user", 2, 2, c_user, 0 }, |
eb5dc014 RK |
1677 | { "userinfo", 2, 2, c_userinfo, RIGHT_READ }, |
1678 | { "users", 0, 0, c_users, RIGHT_READ }, | |
1679 | { "version", 0, 0, c_version, RIGHT_READ }, | |
1680 | { "volume", 0, 2, c_volume, RIGHT_READ|RIGHT_VOLUME } | |
460b9539 | 1681 | }; |
1682 | ||
1683 | static void command_error(const char *msg, void *u) { | |
1684 | struct conn *c = u; | |
1685 | ||
1686 | sink_printf(ev_writer_sink(c->w), "500 parse error: %s\n", msg); | |
1687 | } | |
1688 | ||
1689 | /* process a command. Return 1 if complete, 0 if incomplete. */ | |
1690 | static int command(struct conn *c, char *line) { | |
1691 | char **vec; | |
1692 | int nvec, n; | |
1693 | ||
1694 | D(("server command %s", line)); | |
f9635e06 RK |
1695 | /* We force everything into NFC as early as possible */ |
1696 | if(!(line = utf8_compose_canon(line, strlen(line), 0))) { | |
1697 | sink_writes(ev_writer_sink(c->w), "500 cannot normalize command\n"); | |
1698 | return 1; | |
1699 | } | |
460b9539 | 1700 | if(!(vec = split(line, &nvec, SPLIT_QUOTES, command_error, c))) { |
1701 | sink_writes(ev_writer_sink(c->w), "500 cannot parse command\n"); | |
1702 | return 1; | |
1703 | } | |
1704 | if(nvec == 0) { | |
1705 | sink_writes(ev_writer_sink(c->w), "500 do what?\n"); | |
1706 | return 1; | |
1707 | } | |
ba937f01 | 1708 | if((n = TABLE_FIND(commands, name, vec[0])) < 0) |
460b9539 | 1709 | sink_writes(ev_writer_sink(c->w), "500 unknown command\n"); |
1710 | else { | |
eb5dc014 RK |
1711 | if(commands[n].rights |
1712 | && !(c->rights & commands[n].rights)) { | |
834e7c4a | 1713 | error(0, "%s attempted %s but lacks required rights", c->who ? c->who : "NULL", |
1714 | commands[n].name); | |
b4a80f69 | 1715 | sink_writes(ev_writer_sink(c->w), "510 Prohibited\n"); |
460b9539 | 1716 | return 1; |
1717 | } | |
1718 | ++vec; | |
1719 | --nvec; | |
1720 | if(nvec < commands[n].minargs) { | |
1721 | sink_writes(ev_writer_sink(c->w), "500 missing argument(s)\n"); | |
1722 | return 1; | |
1723 | } | |
1724 | if(nvec > commands[n].maxargs) { | |
1725 | sink_writes(ev_writer_sink(c->w), "500 too many arguments\n"); | |
1726 | return 1; | |
1727 | } | |
1728 | return commands[n].fn(c, vec, nvec); | |
1729 | } | |
1730 | return 1; /* completed */ | |
1731 | } | |
1732 | ||
1733 | /* redirect to the right reader callback for our current state */ | |
1734 | static int redirect_reader_callback(ev_source *ev, | |
1735 | ev_reader *reader, | |
460b9539 | 1736 | void *ptr, |
1737 | size_t bytes, | |
1738 | int eof, | |
1739 | void *u) { | |
1740 | struct conn *c = u; | |
1741 | ||
75d64210 | 1742 | return c->reader(ev, reader, ptr, bytes, eof, u); |
460b9539 | 1743 | } |
1744 | ||
1745 | /* the main command reader */ | |
1746 | static int reader_callback(ev_source attribute((unused)) *ev, | |
1747 | ev_reader *reader, | |
460b9539 | 1748 | void *ptr, |
1749 | size_t bytes, | |
1750 | int eof, | |
1751 | void *u) { | |
1752 | struct conn *c = u; | |
1753 | char *eol; | |
1754 | int complete; | |
1755 | ||
1756 | D(("server reader_callback")); | |
1757 | while((eol = memchr(ptr, '\n', bytes))) { | |
1758 | *eol++ = 0; | |
1759 | ev_reader_consume(reader, eol - (char *)ptr); | |
1760 | complete = command(c, ptr); | |
1761 | bytes -= (eol - (char *)ptr); | |
1762 | ptr = eol; | |
1763 | if(!complete) { | |
1764 | /* the command had better have set a new reader callback */ | |
1765 | if(bytes || eof) | |
1766 | /* there are further bytes to read, or we are at eof; arrange for the | |
1767 | * command's reader callback to handle them */ | |
1768 | return ev_reader_incomplete(reader); | |
1769 | /* nothing's going on right now */ | |
1770 | return 0; | |
1771 | } | |
1772 | /* command completed, we can go around and handle the next one */ | |
1773 | } | |
1774 | if(eof) { | |
1775 | if(bytes) | |
1776 | error(0, "S%x unterminated line", c->tag); | |
8d8b8c1f | 1777 | D(("normal reader close")); |
397ef7bb RK |
1778 | c->r = 0; |
1779 | if(c->w) { | |
8d8b8c1f | 1780 | D(("close associated writer")); |
397ef7bb RK |
1781 | ev_writer_close(c->w); |
1782 | c->w = 0; | |
1783 | } | |
0126692c | 1784 | remove_connection(c); |
460b9539 | 1785 | } |
1786 | return 0; | |
1787 | } | |
1788 | ||
1789 | static int listen_callback(ev_source *ev, | |
1790 | int fd, | |
1791 | const struct sockaddr attribute((unused)) *remote, | |
1792 | socklen_t attribute((unused)) rlen, | |
1793 | void *u) { | |
1794 | const struct listener *l = u; | |
1795 | struct conn *c = xmalloc(sizeof *c); | |
1796 | static unsigned tags; | |
1797 | ||
1798 | D(("server listen_callback fd %d (%s)", fd, l->name)); | |
1799 | nonblock(fd); | |
1800 | cloexec(fd); | |
ad492e00 | 1801 | c->next = connections; |
460b9539 | 1802 | c->tag = tags++; |
1803 | c->ev = ev; | |
e8c92ba7 RK |
1804 | c->w = ev_writer_new(ev, fd, writer_error, c, |
1805 | "client writer"); | |
31e2a93e RK |
1806 | if(!c->w) { |
1807 | error(0, "ev_writer_new for file inbound connection (fd=%d) failed", | |
1808 | fd); | |
1809 | close(fd); | |
1810 | return 0; | |
1811 | } | |
e8c92ba7 RK |
1812 | c->r = ev_reader_new(ev, fd, redirect_reader_callback, reader_error, c, |
1813 | "client reader"); | |
31e2a93e RK |
1814 | if(!c->r) |
1815 | /* Main reason for failure is the FD is too big and that will already have | |
1816 | * been handled */ | |
1817 | fatal(0, "ev_reader_new for file inbound connection (fd=%d) failed", fd); | |
75d64210 | 1818 | ev_tie(c->r, c->w); |
460b9539 | 1819 | c->fd = fd; |
1820 | c->reader = reader_callback; | |
1821 | c->l = l; | |
eb5dc014 | 1822 | c->rights = 0; |
ad492e00 | 1823 | connections = c; |
460b9539 | 1824 | gcry_randomize(c->nonce, sizeof c->nonce, GCRY_STRONG_RANDOM); |
7b32e917 RK |
1825 | sink_printf(ev_writer_sink(c->w), "231 %d %s %s\n", |
1826 | 2, | |
b3141726 RK |
1827 | config->authorization_algorithm, |
1828 | hex(c->nonce, sizeof c->nonce)); | |
460b9539 | 1829 | return 0; |
1830 | } | |
1831 | ||
1832 | int server_start(ev_source *ev, int pf, | |
1833 | size_t socklen, const struct sockaddr *sa, | |
1834 | const char *name) { | |
1835 | int fd; | |
1836 | struct listener *l = xmalloc(sizeof *l); | |
1837 | static const int one = 1; | |
1838 | ||
1839 | D(("server_init socket %s", name)); | |
1840 | fd = xsocket(pf, SOCK_STREAM, 0); | |
1841 | xsetsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one); | |
1842 | if(bind(fd, sa, socklen) < 0) { | |
1843 | error(errno, "error binding to %s", name); | |
1844 | return -1; | |
1845 | } | |
1846 | xlisten(fd, 128); | |
1847 | nonblock(fd); | |
1848 | cloexec(fd); | |
1849 | l->name = name; | |
1850 | l->pf = pf; | |
e8c92ba7 RK |
1851 | if(ev_listen(ev, fd, listen_callback, l, "server listener")) |
1852 | exit(EXIT_FAILURE); | |
460b9539 | 1853 | return fd; |
1854 | } | |
1855 | ||
1856 | int server_stop(ev_source *ev, int fd) { | |
1857 | xclose(fd); | |
1858 | return ev_listen_cancel(ev, fd); | |
1859 | } | |
1860 | ||
1861 | /* | |
1862 | Local Variables: | |
1863 | c-basic-offset:2 | |
1864 | comment-column:40 | |
1865 | fill-column:79 | |
1866 | End: | |
1867 | */ |