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