Commit | Line | Data |
---|---|---|
460b9539 | 1 | /* |
2 | * This file is part of DisOrder. | |
53710d44 | 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" |
53710d44 | 20 | #include "basen.h" |
460b9539 | 21 | |
22 | #ifndef NONCE_SIZE | |
23 | # define NONCE_SIZE 16 | |
24 | #endif | |
25 | ||
10114017 | 26 | #ifndef CONFIRM_SIZE |
53710d44 RK |
27 | /** @brief Size of nonce in confirmation string in 32-bit words |
28 | * | |
29 | * 64 bits gives 11 digits (in base 62). | |
30 | */ | |
31 | # define CONFIRM_SIZE 2 | |
10114017 | 32 | #endif |
33 | ||
460b9539 | 34 | int volume_left, volume_right; /* last known volume */ |
35 | ||
18e6d6e6 | 36 | /** @brief Accept all well-formed login attempts |
37 | * | |
38 | * Used in debugging. | |
39 | */ | |
40 | int wideopen; | |
41 | ||
460b9539 | 42 | struct listener { |
43 | const char *name; | |
44 | int pf; | |
45 | }; | |
46 | ||
397ef7bb | 47 | /** @brief One client connection */ |
460b9539 | 48 | struct conn { |
397ef7bb | 49 | /** @brief Read commands from here */ |
460b9539 | 50 | ev_reader *r; |
397ef7bb | 51 | /** @brief Send responses to here */ |
460b9539 | 52 | ev_writer *w; |
397ef7bb | 53 | /** @brief Underlying file descriptor */ |
460b9539 | 54 | int fd; |
397ef7bb | 55 | /** @brief Unique identifier for connection used in log messages */ |
460b9539 | 56 | unsigned tag; |
397ef7bb | 57 | /** @brief Login name or NULL */ |
460b9539 | 58 | char *who; |
397ef7bb | 59 | /** @brief Event loop */ |
460b9539 | 60 | ev_source *ev; |
397ef7bb | 61 | /** @brief Nonce chosen for this connection */ |
460b9539 | 62 | unsigned char nonce[NONCE_SIZE]; |
397ef7bb RK |
63 | /** @brief Current reader callback |
64 | * | |
65 | * We change this depending on whether we're servicing the @b log command | |
66 | */ | |
460b9539 | 67 | ev_reader_callback *reader; |
397ef7bb | 68 | /** @brief Event log output sending to this connection */ |
460b9539 | 69 | struct eventlog_output *lo; |
397ef7bb | 70 | /** @brief Parent listener */ |
460b9539 | 71 | const struct listener *l; |
b12be54a RK |
72 | /** @brief Login cookie or NULL */ |
73 | char *cookie; | |
eb5dc014 RK |
74 | /** @brief Connection rights */ |
75 | rights_type rights; | |
0126692c | 76 | /** @brief Next connection */ |
77 | struct conn *next; | |
dd9af5cb RK |
78 | /** @brief True if pending rescan had 'wait' set */ |
79 | int rescan_wait; | |
460b9539 | 80 | }; |
81 | ||
0126692c | 82 | /** @brief Linked list of connections */ |
83 | static struct conn *connections; | |
84 | ||
460b9539 | 85 | static int reader_callback(ev_source *ev, |
86 | ev_reader *reader, | |
460b9539 | 87 | void *ptr, |
88 | size_t bytes, | |
89 | int eof, | |
90 | void *u); | |
91 | ||
92 | static const char *noyes[] = { "no", "yes" }; | |
93 | ||
0126692c | 94 | /** @brief Remove a connection from the connection list */ |
95 | static void remove_connection(struct conn *c) { | |
96 | struct conn **cc; | |
97 | ||
98 | for(cc = &connections; *cc && *cc != c; cc = &(*cc)->next) | |
99 | ; | |
100 | if(*cc) | |
101 | *cc = c->next; | |
102 | } | |
103 | ||
397ef7bb RK |
104 | /** @brief Called when a connection's writer fails or is shut down |
105 | * | |
106 | * If the connection still has a raeder that is cancelled. | |
107 | */ | |
460b9539 | 108 | static int writer_error(ev_source attribute((unused)) *ev, |
460b9539 | 109 | int errno_value, |
110 | void *u) { | |
111 | struct conn *c = u; | |
112 | ||
8d8b8c1f | 113 | D(("server writer_error S%x %d", c->tag, errno_value)); |
460b9539 | 114 | if(errno_value == 0) { |
115 | /* writer is done */ | |
8d8b8c1f | 116 | D(("S%x writer completed", c->tag)); |
460b9539 | 117 | } else { |
118 | if(errno_value != EPIPE) | |
119 | error(errno_value, "S%x write error on socket", c->tag); | |
38b8221f | 120 | if(c->r) { |
8d8b8c1f | 121 | D(("cancel reader")); |
38b8221f RK |
122 | ev_reader_cancel(c->r); |
123 | c->r = 0; | |
124 | } | |
8d8b8c1f | 125 | D(("done cancel reader")); |
460b9539 | 126 | } |
38b8221f | 127 | c->w = 0; |
75d64210 | 128 | ev_report(ev); |
0126692c | 129 | remove_connection(c); |
460b9539 | 130 | return 0; |
131 | } | |
132 | ||
397ef7bb RK |
133 | /** @brief Called when a conncetion's reader fails or is shut down |
134 | * | |
135 | * If connection still has a writer then it is closed. | |
136 | */ | |
460b9539 | 137 | static int reader_error(ev_source attribute((unused)) *ev, |
460b9539 | 138 | int errno_value, |
139 | void *u) { | |
140 | struct conn *c = u; | |
141 | ||
8d8b8c1f | 142 | D(("server reader_error S%x %d", c->tag, errno_value)); |
1563f6f6 | 143 | error(errno_value, "S%x read error on socket", c->tag); |
38b8221f RK |
144 | if(c->w) |
145 | ev_writer_close(c->w); | |
146 | c->w = 0; | |
147 | c->r = 0; | |
768d7355 | 148 | ev_report(ev); |
0126692c | 149 | remove_connection(c); |
460b9539 | 150 | return 0; |
151 | } | |
152 | ||
460b9539 | 153 | static int c_disable(struct conn *c, char **vec, int nvec) { |
154 | if(nvec == 0) | |
155 | disable_playing(c->who); | |
156 | else if(nvec == 1 && !strcmp(vec[0], "now")) | |
157 | disable_playing(c->who); | |
158 | else { | |
159 | sink_writes(ev_writer_sink(c->w), "550 invalid argument\n"); | |
160 | return 1; /* completed */ | |
161 | } | |
162 | sink_writes(ev_writer_sink(c->w), "250 OK\n"); | |
163 | return 1; /* completed */ | |
164 | } | |
165 | ||
166 | static int c_enable(struct conn *c, | |
167 | char attribute((unused)) **vec, | |
168 | int attribute((unused)) nvec) { | |
169 | enable_playing(c->who, c->ev); | |
170 | /* Enable implicitly unpauses if there is nothing playing */ | |
171 | if(paused && !playing) resume_playing(c->who); | |
172 | sink_writes(ev_writer_sink(c->w), "250 OK\n"); | |
173 | return 1; /* completed */ | |
174 | } | |
175 | ||
176 | static int c_enabled(struct conn *c, | |
177 | char attribute((unused)) **vec, | |
178 | int attribute((unused)) nvec) { | |
179 | sink_printf(ev_writer_sink(c->w), "252 %s\n", noyes[playing_is_enabled()]); | |
180 | return 1; /* completed */ | |
181 | } | |
182 | ||
183 | static int c_play(struct conn *c, char **vec, | |
184 | int attribute((unused)) nvec) { | |
185 | const char *track; | |
186 | struct queue_entry *q; | |
187 | ||
188 | if(!trackdb_exists(vec[0])) { | |
189 | sink_writes(ev_writer_sink(c->w), "550 track is not in database\n"); | |
190 | return 1; | |
191 | } | |
192 | if(!(track = trackdb_resolve(vec[0]))) { | |
193 | sink_writes(ev_writer_sink(c->w), "550 cannot resolve track\n"); | |
194 | return 1; | |
195 | } | |
2dc2f478 | 196 | q = queue_add(track, c->who, WHERE_BEFORE_RANDOM, origin_picked); |
460b9539 | 197 | queue_write(); |
198 | /* If we added the first track, and something is playing, then prepare the | |
199 | * new track. If nothing is playing then we don't bother as it wouldn't gain | |
200 | * anything. */ | |
201 | if(q == qhead.next && playing) | |
202 | prepare(c->ev, q); | |
81e440ce | 203 | sink_printf(ev_writer_sink(c->w), "252 %s\n", q->id); |
460b9539 | 204 | /* If the queue was empty but we are for some reason paused then |
205 | * unpause. */ | |
206 | if(!playing) resume_playing(0); | |
207 | play(c->ev); | |
208 | return 1; /* completed */ | |
209 | } | |
210 | ||
211 | static int c_remove(struct conn *c, char **vec, | |
212 | int attribute((unused)) nvec) { | |
213 | struct queue_entry *q; | |
214 | ||
215 | if(!(q = queue_find(vec[0]))) { | |
216 | sink_writes(ev_writer_sink(c->w), "550 no such track on the queue\n"); | |
217 | return 1; | |
218 | } | |
938d8157 | 219 | if(!right_removable(c->rights, c->who, q)) { |
834e7c4a | 220 | error(0, "%s attempted remove but lacks required rights", c->who); |
eb5dc014 | 221 | sink_writes(ev_writer_sink(c->w), |
b4a80f69 | 222 | "510 Not authorized to remove that track\n"); |
eb5dc014 | 223 | return 1; |
460b9539 | 224 | } |
225 | queue_remove(q, c->who); | |
226 | /* De-prepare the track. */ | |
227 | abandon(c->ev, q); | |
49a773eb RK |
228 | /* See about adding a new random track */ |
229 | add_random_track(c->ev); | |
460b9539 | 230 | /* Prepare whatever the next head track is. */ |
231 | if(qhead.next != &qhead) | |
232 | prepare(c->ev, qhead.next); | |
233 | queue_write(); | |
234 | sink_writes(ev_writer_sink(c->w), "250 removed\n"); | |
235 | return 1; /* completed */ | |
236 | } | |
237 | ||
238 | static int c_scratch(struct conn *c, | |
239 | char **vec, | |
240 | int nvec) { | |
241 | if(!playing) { | |
242 | sink_writes(ev_writer_sink(c->w), "250 nothing is playing\n"); | |
243 | return 1; /* completed */ | |
244 | } | |
eb5dc014 RK |
245 | /* TODO there is a bug here: if we specify an ID but it's not the currently |
246 | * playing track then you will get 550 if you weren't authorized to scratch | |
247 | * the currently playing track. */ | |
938d8157 | 248 | if(!right_scratchable(c->rights, c->who, playing)) { |
834e7c4a | 249 | error(0, "%s attempted scratch but lacks required rights", c->who); |
eb5dc014 | 250 | sink_writes(ev_writer_sink(c->w), |
b4a80f69 | 251 | "510 Not authorized to scratch that track\n"); |
eb5dc014 | 252 | return 1; |
460b9539 | 253 | } |
254 | scratch(c->who, nvec == 1 ? vec[0] : 0); | |
255 | /* If you scratch an unpaused track then it is automatically unpaused */ | |
256 | resume_playing(0); | |
257 | sink_writes(ev_writer_sink(c->w), "250 scratched\n"); | |
258 | return 1; /* completed */ | |
259 | } | |
260 | ||
261 | static int c_pause(struct conn *c, | |
262 | char attribute((unused)) **vec, | |
263 | int attribute((unused)) nvec) { | |
264 | if(!playing) { | |
265 | sink_writes(ev_writer_sink(c->w), "250 nothing is playing\n"); | |
266 | return 1; /* completed */ | |
267 | } | |
268 | if(paused) { | |
269 | sink_writes(ev_writer_sink(c->w), "250 already paused\n"); | |
270 | return 1; /* completed */ | |
271 | } | |
272 | if(pause_playing(c->who) < 0) | |
273 | sink_writes(ev_writer_sink(c->w), "550 cannot pause this track\n"); | |
274 | else | |
275 | sink_writes(ev_writer_sink(c->w), "250 paused\n"); | |
276 | return 1; | |
277 | } | |
278 | ||
279 | static int c_resume(struct conn *c, | |
280 | char attribute((unused)) **vec, | |
281 | int attribute((unused)) nvec) { | |
282 | if(!paused) { | |
283 | sink_writes(ev_writer_sink(c->w), "250 not paused\n"); | |
284 | return 1; /* completed */ | |
285 | } | |
286 | resume_playing(c->who); | |
287 | sink_writes(ev_writer_sink(c->w), "250 paused\n"); | |
288 | return 1; | |
289 | } | |
290 | ||
291 | static int c_shutdown(struct conn *c, | |
292 | char attribute((unused)) **vec, | |
293 | int attribute((unused)) nvec) { | |
294 | info("S%x shut down by %s", c->tag, c->who); | |
295 | sink_writes(ev_writer_sink(c->w), "250 shutting down\n"); | |
296 | ev_writer_flush(c->w); | |
297 | quit(c->ev); | |
298 | } | |
299 | ||
300 | static int c_reconfigure(struct conn *c, | |
301 | char attribute((unused)) **vec, | |
302 | int attribute((unused)) nvec) { | |
303 | info("S%x reconfigure by %s", c->tag, c->who); | |
304 | if(reconfigure(c->ev, 1)) | |
305 | sink_writes(ev_writer_sink(c->w), "550 error reading new config\n"); | |
306 | else | |
307 | sink_writes(ev_writer_sink(c->w), "250 installed new config\n"); | |
308 | return 1; /* completed */ | |
309 | } | |
310 | ||
dd9af5cb RK |
311 | static void finished_rescan(void *ru) { |
312 | struct conn *const c = ru; | |
313 | ||
314 | sink_writes(ev_writer_sink(c->w), "250 rescan completed\n"); | |
315 | /* Turn this connection back on */ | |
316 | ev_reader_enable(c->r); | |
317 | } | |
318 | ||
319 | static void start_fresh_rescan(void *ru) { | |
320 | struct conn *const c = ru; | |
321 | ||
322 | if(trackdb_rescan_underway()) { | |
323 | /* Some other waiter beat us to it. However in this case we're happy to | |
324 | * piggyback; the requirement is that a new rescan be started, not that it | |
325 | * was _our_ rescan. */ | |
326 | if(c->rescan_wait) { | |
327 | /* We block until the rescan completes */ | |
328 | trackdb_add_rescanned(finished_rescan, c); | |
329 | } else { | |
330 | /* We report that the new rescan has started */ | |
331 | sink_writes(ev_writer_sink(c->w), "250 rescan initiated\n"); | |
332 | /* Turn this connection back on */ | |
333 | ev_reader_enable(c->r); | |
334 | } | |
335 | } else { | |
336 | /* We are the first connection to get a callback so we must start a | |
337 | * rescan. */ | |
338 | if(c->rescan_wait) { | |
339 | /* We want to block until the new rescan completes */ | |
340 | trackdb_rescan(c->ev, 1/*check*/, finished_rescan, c); | |
341 | } else { | |
342 | /* We can report back immediately */ | |
343 | trackdb_rescan(c->ev, 1/*check*/, 0, 0); | |
344 | sink_writes(ev_writer_sink(c->w), "250 rescan initiated\n"); | |
345 | /* Turn this connection back on */ | |
346 | ev_reader_enable(c->r); | |
347 | } | |
348 | } | |
349 | } | |
350 | ||
460b9539 | 351 | static int c_rescan(struct conn *c, |
dd9af5cb RK |
352 | char **vec, |
353 | int nvec) { | |
d867af10 | 354 | int flag_wait = 0, flag_fresh = 0, n; |
dd9af5cb RK |
355 | |
356 | /* Parse flags */ | |
357 | for(n = 0; n < nvec; ++n) { | |
358 | if(!strcmp(vec[n], "wait")) | |
d867af10 | 359 | flag_wait = 1; /* wait for rescan to complete */ |
dd9af5cb RK |
360 | #if 0 |
361 | /* Currently disabled because untested (and hard to test). */ | |
362 | else if(!strcmp(vec[n], "fresh")) | |
d867af10 | 363 | flag_fresh = 1; /* don't piggyback underway rescan */ |
dd9af5cb RK |
364 | #endif |
365 | else { | |
366 | sink_writes(ev_writer_sink(c->w), "550 unknown flag\n"); | |
367 | return 1; /* completed */ | |
368 | } | |
369 | } | |
370 | /* Report what was requested */ | |
371 | info("S%x rescan by %s (%s %s)", c->tag, c->who, | |
d867af10 RK |
372 | flag_wait ? "wait" : "", |
373 | flag_fresh ? "fresh" : ""); | |
dd9af5cb | 374 | if(trackdb_rescan_underway()) { |
d867af10 | 375 | if(flag_fresh) { |
dd9af5cb RK |
376 | /* We want a fresh rescan but there is already one underway. Arrange a |
377 | * callback when it completes and then set off a new one. */ | |
d867af10 | 378 | c->rescan_wait = flag_wait; |
dd9af5cb | 379 | trackdb_add_rescanned(start_fresh_rescan, c); |
d867af10 | 380 | if(flag_wait) |
dd9af5cb RK |
381 | return 0; |
382 | else { | |
383 | sink_writes(ev_writer_sink(c->w), "250 rescan queued\n"); | |
384 | return 1; | |
385 | } | |
386 | } else { | |
387 | /* There's a rescan underway, and it's acceptable to piggyback on it */ | |
d867af10 | 388 | if(flag_wait) { |
dd9af5cb RK |
389 | /* We want to block until completion. */ |
390 | trackdb_add_rescanned(finished_rescan, c); | |
391 | return 0; | |
392 | } else { | |
393 | /* We don't want to block. So we just report that things are in | |
394 | * hand. */ | |
395 | sink_writes(ev_writer_sink(c->w), "250 rescan already underway\n"); | |
396 | return 1; | |
397 | } | |
398 | } | |
399 | } else { | |
400 | /* No rescan is underway. fresh is therefore irrelevant. */ | |
d867af10 | 401 | if(flag_wait) { |
dd9af5cb RK |
402 | /* We want to block until completion */ |
403 | trackdb_rescan(c->ev, 1/*check*/, finished_rescan, c); | |
404 | return 0; | |
405 | } else { | |
406 | /* We don't want to block. */ | |
407 | trackdb_rescan(c->ev, 1/*check*/, 0, 0); | |
408 | sink_writes(ev_writer_sink(c->w), "250 rescan initiated\n"); | |
409 | return 1; /* completed */ | |
410 | } | |
411 | } | |
460b9539 | 412 | } |
413 | ||
414 | static int c_version(struct conn *c, | |
415 | char attribute((unused)) **vec, | |
416 | int attribute((unused)) nvec) { | |
417 | /* VERSION had better only use the basic character set */ | |
a05e4467 | 418 | sink_printf(ev_writer_sink(c->w), "251 %s\n", disorder_short_version_string); |
460b9539 | 419 | return 1; /* completed */ |
420 | } | |
421 | ||
422 | static int c_playing(struct conn *c, | |
423 | char attribute((unused)) **vec, | |
424 | int attribute((unused)) nvec) { | |
425 | if(playing) { | |
426 | queue_fix_sofar(playing); | |
427 | playing->expected = 0; | |
428 | sink_printf(ev_writer_sink(c->w), "252 %s\n", queue_marshall(playing)); | |
429 | } else | |
430 | sink_printf(ev_writer_sink(c->w), "259 nothing playing\n"); | |
431 | return 1; /* completed */ | |
432 | } | |
433 | ||
b12be54a | 434 | static const char *connection_host(struct conn *c) { |
460b9539 | 435 | union { |
436 | struct sockaddr sa; | |
437 | struct sockaddr_in in; | |
438 | struct sockaddr_in6 in6; | |
439 | } u; | |
440 | socklen_t l; | |
b12be54a | 441 | int n; |
460b9539 | 442 | char host[1024]; |
443 | ||
460b9539 | 444 | /* get connection data */ |
445 | l = sizeof u; | |
446 | if(getpeername(c->fd, &u.sa, &l) < 0) { | |
447 | error(errno, "S%x error calling getpeername", c->tag); | |
b12be54a | 448 | return 0; |
460b9539 | 449 | } |
450 | if(c->l->pf != PF_UNIX) { | |
451 | if((n = getnameinfo(&u.sa, l, | |
452 | host, sizeof host, 0, 0, NI_NUMERICHOST))) { | |
453 | error(0, "S%x error calling getnameinfo: %s", c->tag, gai_strerror(n)); | |
b12be54a | 454 | return 0; |
460b9539 | 455 | } |
b12be54a | 456 | return xstrdup(host); |
18e6d6e6 | 457 | } else |
b12be54a RK |
458 | return "local"; |
459 | } | |
460 | ||
461 | static int c_user(struct conn *c, | |
462 | char **vec, | |
463 | int attribute((unused)) nvec) { | |
eb5dc014 | 464 | struct kvp *k; |
f0feb22e | 465 | const char *res, *host, *password; |
eb5dc014 | 466 | rights_type rights; |
b12be54a RK |
467 | |
468 | if(c->who) { | |
469 | sink_writes(ev_writer_sink(c->w), "530 already authenticated\n"); | |
470 | return 1; | |
471 | } | |
472 | /* get connection data */ | |
473 | if(!(host = connection_host(c))) { | |
474 | sink_writes(ev_writer_sink(c->w), "530 authentication failure\n"); | |
475 | return 1; | |
476 | } | |
460b9539 | 477 | /* find the user */ |
eb5dc014 | 478 | k = trackdb_getuserinfo(vec[0]); |
f0feb22e | 479 | /* reject nonexistent users */ |
eb5dc014 RK |
480 | if(!k) { |
481 | error(0, "S%x unknown user '%s' from %s", c->tag, vec[0], host); | |
482 | sink_writes(ev_writer_sink(c->w), "530 authentication failed\n"); | |
483 | return 1; | |
484 | } | |
485 | /* reject unconfirmed users */ | |
486 | if(kvp_get(k, "confirmation")) { | |
487 | error(0, "S%x unconfirmed user '%s' from %s", c->tag, vec[0], host); | |
488 | sink_writes(ev_writer_sink(c->w), "530 authentication failed\n"); | |
489 | return 1; | |
490 | } | |
491 | password = kvp_get(k, "password"); | |
492 | if(!password) password = ""; | |
0f55e905 | 493 | if(parse_rights(kvp_get(k, "rights"), &rights, 1)) { |
eb5dc014 | 494 | error(0, "error parsing rights for %s", vec[0]); |
18e6d6e6 | 495 | sink_writes(ev_writer_sink(c->w), "530 authentication failed\n"); |
496 | return 1; | |
497 | } | |
f0feb22e RK |
498 | /* check whether the response is right */ |
499 | res = authhash(c->nonce, sizeof c->nonce, password, | |
637fdea3 | 500 | config->authorization_algorithm); |
18e6d6e6 | 501 | if(wideopen || (res && !strcmp(res, vec[1]))) { |
502 | c->who = vec[0]; | |
eb5dc014 | 503 | c->rights = rights; |
18e6d6e6 | 504 | /* currently we only bother logging remote connections */ |
30365519 | 505 | if(strcmp(host, "local")) |
18e6d6e6 | 506 | info("S%x %s connected from %s", c->tag, vec[0], host); |
30365519 | 507 | else |
eb5dc014 | 508 | c->rights |= RIGHT__LOCAL; |
18e6d6e6 | 509 | sink_writes(ev_writer_sink(c->w), "230 OK\n"); |
510 | return 1; | |
460b9539 | 511 | } |
512 | /* oops, response was wrong */ | |
18e6d6e6 | 513 | info("S%x authentication failure for %s from %s", c->tag, vec[0], host); |
460b9539 | 514 | sink_writes(ev_writer_sink(c->w), "530 authentication failed\n"); |
515 | return 1; | |
516 | } | |
517 | ||
518 | static int c_recent(struct conn *c, | |
519 | char attribute((unused)) **vec, | |
520 | int attribute((unused)) nvec) { | |
521 | const struct queue_entry *q; | |
522 | ||
523 | sink_writes(ev_writer_sink(c->w), "253 Tracks follow\n"); | |
524 | for(q = phead.next; q != &phead; q = q->next) | |
525 | sink_printf(ev_writer_sink(c->w), " %s\n", queue_marshall(q)); | |
526 | sink_writes(ev_writer_sink(c->w), ".\n"); | |
527 | return 1; /* completed */ | |
528 | } | |
529 | ||
530 | static int c_queue(struct conn *c, | |
531 | char attribute((unused)) **vec, | |
532 | int attribute((unused)) nvec) { | |
533 | struct queue_entry *q; | |
534 | time_t when = 0; | |
535 | const char *l; | |
536 | long length; | |
537 | ||
538 | sink_writes(ev_writer_sink(c->w), "253 Tracks follow\n"); | |
539 | if(playing_is_enabled() && !paused) { | |
540 | if(playing) { | |
541 | queue_fix_sofar(playing); | |
542 | if((l = trackdb_get(playing->track, "_length")) | |
543 | && (length = atol(l))) { | |
544 | time(&when); | |
545 | when += length - playing->sofar + config->gap; | |
546 | } | |
547 | } else | |
548 | /* Nothing is playing but playing is enabled, so whatever is | |
549 | * first in the queue can be expected to start immediately. */ | |
550 | time(&when); | |
551 | } | |
552 | for(q = qhead.next; q != &qhead; q = q->next) { | |
553 | /* fill in estimated start time */ | |
554 | q->expected = when; | |
555 | sink_printf(ev_writer_sink(c->w), " %s\n", queue_marshall(q)); | |
556 | /* update for next track */ | |
557 | if(when) { | |
558 | if((l = trackdb_get(q->track, "_length")) | |
559 | && (length = atol(l))) | |
560 | when += length + config->gap; | |
561 | else | |
562 | when = 0; | |
563 | } | |
564 | } | |
565 | sink_writes(ev_writer_sink(c->w), ".\n"); | |
566 | return 1; /* completed */ | |
567 | } | |
568 | ||
569 | static int output_list(struct conn *c, char **vec) { | |
570 | while(*vec) | |
571 | sink_printf(ev_writer_sink(c->w), "%s\n", *vec++); | |
572 | sink_writes(ev_writer_sink(c->w), ".\n"); | |
573 | return 1; | |
574 | } | |
575 | ||
576 | static int files_dirs(struct conn *c, | |
577 | char **vec, | |
578 | int nvec, | |
579 | enum trackdb_listable what) { | |
580 | const char *dir, *re, *errstr; | |
581 | int erroffset; | |
582 | pcre *rec; | |
583 | char **fvec, *key; | |
584 | ||
585 | switch(nvec) { | |
586 | case 0: dir = 0; re = 0; break; | |
587 | case 1: dir = vec[0]; re = 0; break; | |
588 | case 2: dir = vec[0]; re = vec[1]; break; | |
589 | default: abort(); | |
590 | } | |
591 | /* A bit of a bodge to make sure the args don't trample on cache keys */ | |
592 | if(dir && strchr(dir, '\n')) { | |
593 | sink_writes(ev_writer_sink(c->w), "550 invalid directory name\n"); | |
594 | return 1; | |
595 | } | |
596 | if(re && strchr(re, '\n')) { | |
597 | sink_writes(ev_writer_sink(c->w), "550 invalid regexp\n"); | |
598 | return 1; | |
599 | } | |
600 | /* We bother eliminating "" because the web interface is relatively | |
601 | * likely to send it */ | |
602 | if(re && *re) { | |
603 | byte_xasprintf(&key, "%d\n%s\n%s", (int)what, dir ? dir : "", re); | |
604 | fvec = (char **)cache_get(&cache_files_type, key); | |
605 | if(fvec) { | |
606 | /* Got a cache hit, don't store the answer in the cache */ | |
607 | key = 0; | |
608 | ++cache_files_hits; | |
609 | rec = 0; /* quieten compiler */ | |
610 | } else { | |
611 | /* Cache miss, we'll do the lookup and key != 0 so we'll store the answer | |
612 | * in the cache. */ | |
613 | if(!(rec = pcre_compile(re, PCRE_CASELESS|PCRE_UTF8, | |
614 | &errstr, &erroffset, 0))) { | |
615 | sink_printf(ev_writer_sink(c->w), "550 Error compiling regexp: %s\n", | |
616 | errstr); | |
617 | return 1; | |
618 | } | |
619 | /* It only counts as a miss if the regexp was valid. */ | |
620 | ++cache_files_misses; | |
621 | } | |
622 | } else { | |
623 | /* No regexp, don't bother caching the result */ | |
624 | rec = 0; | |
625 | key = 0; | |
626 | fvec = 0; | |
627 | } | |
628 | if(!fvec) { | |
629 | /* No cache hit (either because a miss, or because we did not look) so do | |
630 | * the lookup */ | |
631 | if(dir && *dir) | |
632 | fvec = trackdb_list(dir, 0, what, rec); | |
633 | else | |
634 | fvec = trackdb_list(0, 0, what, rec); | |
635 | } | |
636 | if(key) | |
637 | /* Put the answer in the cache */ | |
638 | cache_put(&cache_files_type, key, fvec); | |
639 | sink_writes(ev_writer_sink(c->w), "253 Listing follow\n"); | |
640 | return output_list(c, fvec); | |
641 | } | |
642 | ||
643 | static int c_files(struct conn *c, | |
644 | char **vec, | |
645 | int nvec) { | |
646 | return files_dirs(c, vec, nvec, trackdb_files); | |
647 | } | |
648 | ||
649 | static int c_dirs(struct conn *c, | |
650 | char **vec, | |
651 | int nvec) { | |
652 | return files_dirs(c, vec, nvec, trackdb_directories); | |
653 | } | |
654 | ||
655 | static int c_allfiles(struct conn *c, | |
656 | char **vec, | |
657 | int nvec) { | |
658 | return files_dirs(c, vec, nvec, trackdb_directories|trackdb_files); | |
659 | } | |
660 | ||
661 | static int c_get(struct conn *c, | |
662 | char **vec, | |
663 | int attribute((unused)) nvec) { | |
27ed5a69 | 664 | const char *v, *track; |
460b9539 | 665 | |
27ed5a69 RK |
666 | if(!(track = trackdb_resolve(vec[0]))) { |
667 | sink_writes(ev_writer_sink(c->w), "550 cannot resolve track\n"); | |
668 | return 1; | |
669 | } | |
670 | if(vec[1][0] != '_' && (v = trackdb_get(track, vec[1]))) | |
7b32e917 | 671 | sink_printf(ev_writer_sink(c->w), "252 %s\n", quoteutf8(v)); |
460b9539 | 672 | else |
fb1bc1f5 | 673 | sink_writes(ev_writer_sink(c->w), "555 not found\n"); |
460b9539 | 674 | return 1; |
675 | } | |
676 | ||
677 | static int c_length(struct conn *c, | |
678 | char **vec, | |
679 | int attribute((unused)) nvec) { | |
680 | const char *track, *v; | |
681 | ||
682 | if(!(track = trackdb_resolve(vec[0]))) { | |
683 | sink_writes(ev_writer_sink(c->w), "550 cannot resolve track\n"); | |
684 | return 1; | |
685 | } | |
686 | if((v = trackdb_get(track, "_length"))) | |
7b32e917 | 687 | sink_printf(ev_writer_sink(c->w), "252 %s\n", quoteutf8(v)); |
460b9539 | 688 | else |
689 | sink_writes(ev_writer_sink(c->w), "550 not found\n"); | |
690 | return 1; | |
691 | } | |
692 | ||
693 | static int c_set(struct conn *c, | |
694 | char **vec, | |
695 | int attribute((unused)) nvec) { | |
27ed5a69 RK |
696 | const char *track; |
697 | ||
698 | if(!(track = trackdb_resolve(vec[0]))) { | |
699 | sink_writes(ev_writer_sink(c->w), "550 cannot resolve track\n"); | |
700 | return 1; | |
701 | } | |
702 | if(vec[1][0] != '_' && !trackdb_set(track, vec[1], vec[2])) | |
460b9539 | 703 | sink_writes(ev_writer_sink(c->w), "250 OK\n"); |
704 | else | |
705 | sink_writes(ev_writer_sink(c->w), "550 not found\n"); | |
706 | return 1; | |
707 | } | |
708 | ||
709 | static int c_prefs(struct conn *c, | |
710 | char **vec, | |
711 | int attribute((unused)) nvec) { | |
712 | struct kvp *k; | |
27ed5a69 | 713 | const char *track; |
460b9539 | 714 | |
27ed5a69 RK |
715 | if(!(track = trackdb_resolve(vec[0]))) { |
716 | sink_writes(ev_writer_sink(c->w), "550 cannot resolve track\n"); | |
717 | return 1; | |
718 | } | |
719 | k = trackdb_get_all(track); | |
460b9539 | 720 | sink_writes(ev_writer_sink(c->w), "253 prefs follow\n"); |
721 | for(; k; k = k->next) | |
722 | if(k->name[0] != '_') /* omit internal values */ | |
723 | sink_printf(ev_writer_sink(c->w), | |
724 | " %s %s\n", quoteutf8(k->name), quoteutf8(k->value)); | |
725 | sink_writes(ev_writer_sink(c->w), ".\n"); | |
726 | return 1; | |
727 | } | |
728 | ||
729 | static int c_exists(struct conn *c, | |
730 | char **vec, | |
731 | int attribute((unused)) nvec) { | |
27ed5a69 | 732 | /* trackdb_exists() does its own alias checking */ |
460b9539 | 733 | sink_printf(ev_writer_sink(c->w), "252 %s\n", noyes[trackdb_exists(vec[0])]); |
734 | return 1; | |
735 | } | |
736 | ||
737 | static void search_parse_error(const char *msg, void *u) { | |
738 | *(const char **)u = msg; | |
739 | } | |
740 | ||
741 | static int c_search(struct conn *c, | |
742 | char **vec, | |
743 | int attribute((unused)) nvec) { | |
744 | char **terms, **results; | |
745 | int nterms, nresults, n; | |
746 | const char *e = "unknown error"; | |
747 | ||
748 | /* This is a bit of a bodge. Initially it's there to make the eclient | |
749 | * interface a bit more convenient to add searching to, but it has the more | |
750 | * compelling advantage that if everything uses it, then interpretation of | |
751 | * user-supplied search strings will be the same everywhere. */ | |
752 | if(!(terms = split(vec[0], &nterms, SPLIT_QUOTES, search_parse_error, &e))) { | |
753 | sink_printf(ev_writer_sink(c->w), "550 %s\n", e); | |
754 | } else { | |
755 | results = trackdb_search(terms, nterms, &nresults); | |
756 | sink_printf(ev_writer_sink(c->w), "253 %d matches\n", nresults); | |
757 | for(n = 0; n < nresults; ++n) | |
758 | sink_printf(ev_writer_sink(c->w), "%s\n", results[n]); | |
759 | sink_writes(ev_writer_sink(c->w), ".\n"); | |
760 | } | |
761 | return 1; | |
762 | } | |
763 | ||
764 | static int c_random_enable(struct conn *c, | |
765 | char attribute((unused)) **vec, | |
766 | int attribute((unused)) nvec) { | |
767 | enable_random(c->who, c->ev); | |
768 | /* Enable implicitly unpauses if there is nothing playing */ | |
769 | if(paused && !playing) resume_playing(c->who); | |
770 | sink_writes(ev_writer_sink(c->w), "250 OK\n"); | |
771 | return 1; /* completed */ | |
772 | } | |
773 | ||
774 | static int c_random_disable(struct conn *c, | |
775 | char attribute((unused)) **vec, | |
776 | int attribute((unused)) nvec) { | |
777 | disable_random(c->who); | |
778 | sink_writes(ev_writer_sink(c->w), "250 OK\n"); | |
779 | return 1; /* completed */ | |
780 | } | |
781 | ||
782 | static int c_random_enabled(struct conn *c, | |
783 | char attribute((unused)) **vec, | |
784 | int attribute((unused)) nvec) { | |
785 | sink_printf(ev_writer_sink(c->w), "252 %s\n", noyes[random_is_enabled()]); | |
786 | return 1; /* completed */ | |
787 | } | |
788 | ||
d6dde5a3 RK |
789 | static void got_stats(char *stats, void *u) { |
790 | struct conn *const c = u; | |
791 | ||
792 | sink_printf(ev_writer_sink(c->w), "253 stats\n%s\n.\n", stats); | |
793 | /* Now we can start processing commands again */ | |
794 | ev_reader_enable(c->r); | |
795 | } | |
796 | ||
460b9539 | 797 | static int c_stats(struct conn *c, |
798 | char attribute((unused)) **vec, | |
799 | int attribute((unused)) nvec) { | |
d6dde5a3 RK |
800 | trackdb_stats_subprocess(c->ev, got_stats, c); |
801 | return 0; /* not yet complete */ | |
460b9539 | 802 | } |
803 | ||
804 | static int c_volume(struct conn *c, | |
805 | char **vec, | |
806 | int nvec) { | |
807 | int l, r, set; | |
808 | char lb[32], rb[32]; | |
eb5dc014 | 809 | rights_type rights; |
460b9539 | 810 | |
811 | switch(nvec) { | |
812 | case 0: | |
813 | set = 0; | |
814 | break; | |
815 | case 1: | |
816 | l = r = atoi(vec[0]); | |
817 | set = 1; | |
818 | break; | |
819 | case 2: | |
820 | l = atoi(vec[0]); | |
821 | r = atoi(vec[1]); | |
822 | set = 1; | |
823 | break; | |
824 | default: | |
825 | abort(); | |
826 | } | |
eb5dc014 RK |
827 | rights = set ? RIGHT_VOLUME : RIGHT_READ; |
828 | if(!(c->rights & rights)) { | |
834e7c4a | 829 | error(0, "%s attempted to set volume but lacks required rights", c->who); |
b4a80f69 | 830 | sink_writes(ev_writer_sink(c->w), "510 Prohibited\n"); |
eb5dc014 RK |
831 | return 1; |
832 | } | |
b50cfb8a | 833 | if(!api || !api->set_volume) { |
460b9539 | 834 | sink_writes(ev_writer_sink(c->w), "550 error accessing mixer\n"); |
b50cfb8a RK |
835 | return 1; |
836 | } | |
e4d0463f | 837 | (set ? api->set_volume : api->get_volume)(&l, &r); |
b50cfb8a RK |
838 | sink_printf(ev_writer_sink(c->w), "252 %d %d\n", l, r); |
839 | if(l != volume_left || r != volume_right) { | |
840 | volume_left = l; | |
841 | volume_right = r; | |
842 | snprintf(lb, sizeof lb, "%d", l); | |
843 | snprintf(rb, sizeof rb, "%d", r); | |
844 | eventlog("volume", lb, rb, (char *)0); | |
460b9539 | 845 | } |
846 | return 1; | |
847 | } | |
848 | ||
397ef7bb RK |
849 | /** @brief Called when data arrives on a log connection |
850 | * | |
851 | * We just discard all such data. The client may occasionally send data as a | |
852 | * keepalive. | |
853 | */ | |
854 | static int logging_reader_callback(ev_source attribute((unused)) *ev, | |
460b9539 | 855 | ev_reader *reader, |
397ef7bb | 856 | void attribute((unused)) *ptr, |
460b9539 | 857 | size_t bytes, |
397ef7bb RK |
858 | int attribute((unused)) eof, |
859 | void attribute((unused)) *u) { | |
460b9539 | 860 | struct conn *c = u; |
861 | ||
397ef7bb RK |
862 | ev_reader_consume(reader, bytes); |
863 | if(eof) { | |
864 | /* Oops, that's all for now */ | |
8d8b8c1f | 865 | D(("logging reader eof")); |
397ef7bb | 866 | if(c->w) { |
8d8b8c1f | 867 | D(("close writer")); |
397ef7bb RK |
868 | ev_writer_close(c->w); |
869 | c->w = 0; | |
870 | } | |
871 | c->r = 0; | |
0126692c | 872 | remove_connection(c); |
f6033c46 | 873 | } |
397ef7bb | 874 | return 0; |
460b9539 | 875 | } |
876 | ||
877 | static void logclient(const char *msg, void *user) { | |
878 | struct conn *c = user; | |
879 | ||
f6033c46 RK |
880 | if(!c->w || !c->r) { |
881 | /* This connection has gone up in smoke for some reason */ | |
882 | eventlog_remove(c->lo); | |
ad492e00 | 883 | c->lo = 0; |
f6033c46 RK |
884 | return; |
885 | } | |
75e7b7c3 RK |
886 | /* user_* messages are restricted */ |
887 | if(!strncmp(msg, "user_", 5)) { | |
58d2aad2 RK |
888 | /* They are only sent to admin users */ |
889 | if(!(c->rights & RIGHT_ADMIN)) | |
890 | return; | |
891 | /* They are not sent over TCP connections unless remote user-management is | |
892 | * enabled */ | |
893 | if(!config->remote_userman && !(c->rights & RIGHT__LOCAL)) | |
894 | return; | |
895 | } | |
460b9539 | 896 | sink_printf(ev_writer_sink(c->w), "%"PRIxMAX" %s\n", |
897 | (uintmax_t)time(0), msg); | |
898 | } | |
899 | ||
900 | static int c_log(struct conn *c, | |
901 | char attribute((unused)) **vec, | |
902 | int attribute((unused)) nvec) { | |
903 | time_t now; | |
904 | ||
905 | sink_writes(ev_writer_sink(c->w), "254 OK\n"); | |
906 | /* pump out initial state */ | |
907 | time(&now); | |
908 | sink_printf(ev_writer_sink(c->w), "%"PRIxMAX" state %s\n", | |
909 | (uintmax_t)now, | |
910 | playing_is_enabled() ? "enable_play" : "disable_play"); | |
911 | sink_printf(ev_writer_sink(c->w), "%"PRIxMAX" state %s\n", | |
912 | (uintmax_t)now, | |
913 | random_is_enabled() ? "enable_random" : "disable_random"); | |
914 | sink_printf(ev_writer_sink(c->w), "%"PRIxMAX" state %s\n", | |
915 | (uintmax_t)now, | |
916 | paused ? "pause" : "resume"); | |
5abe307a RK |
917 | if(playing) |
918 | sink_printf(ev_writer_sink(c->w), "%"PRIxMAX" state playing\n", | |
919 | (uintmax_t)now); | |
df44d69b RK |
920 | /* Initial volume */ |
921 | sink_printf(ev_writer_sink(c->w), "%"PRIxMAX" volume %d %d\n", | |
922 | (uintmax_t)now, volume_left, volume_right); | |
460b9539 | 923 | c->lo = xmalloc(sizeof *c->lo); |
924 | c->lo->fn = logclient; | |
925 | c->lo->user = c; | |
926 | eventlog_add(c->lo); | |
927 | c->reader = logging_reader_callback; | |
928 | return 0; | |
929 | } | |
930 | ||
eb5dc014 RK |
931 | /** @brief Test whether a move is allowed |
932 | * @param c Connection | |
933 | * @param qs List of IDs on queue | |
934 | * @param nqs Number of IDs | |
935 | * @return 0 if move is prohibited, non-0 if it is allowed | |
936 | */ | |
937 | static int has_move_rights(struct conn *c, struct queue_entry **qs, int nqs) { | |
eb5dc014 RK |
938 | for(; nqs > 0; ++qs, --nqs) { |
939 | struct queue_entry *const q = *qs; | |
940 | ||
938d8157 | 941 | if(!right_movable(c->rights, c->who, q)) |
942 | return 0; | |
eb5dc014 | 943 | } |
938d8157 | 944 | return 1; |
eb5dc014 RK |
945 | } |
946 | ||
460b9539 | 947 | static int c_move(struct conn *c, |
948 | char **vec, | |
949 | int attribute((unused)) nvec) { | |
950 | struct queue_entry *q; | |
951 | int n; | |
952 | ||
460b9539 | 953 | if(!(q = queue_find(vec[0]))) { |
954 | sink_writes(ev_writer_sink(c->w), "550 no such track on the queue\n"); | |
955 | return 1; | |
956 | } | |
eb5dc014 | 957 | if(!has_move_rights(c, &q, 1)) { |
834e7c4a | 958 | error(0, "%s attempted move but lacks required rights", c->who); |
eb5dc014 | 959 | sink_writes(ev_writer_sink(c->w), |
b4a80f69 | 960 | "510 Not authorized to move that track\n"); |
eb5dc014 RK |
961 | return 1; |
962 | } | |
460b9539 | 963 | n = queue_move(q, atoi(vec[1]), c->who); |
460b9539 | 964 | sink_printf(ev_writer_sink(c->w), "252 %d\n", n); |
965 | /* If we've moved to the head of the queue then prepare the track. */ | |
966 | if(q == qhead.next) | |
967 | prepare(c->ev, q); | |
968 | return 1; | |
969 | } | |
970 | ||
971 | static int c_moveafter(struct conn *c, | |
972 | char **vec, | |
973 | int attribute((unused)) nvec) { | |
974 | struct queue_entry *q, **qs; | |
975 | int n; | |
976 | ||
460b9539 | 977 | if(vec[0][0]) { |
978 | if(!(q = queue_find(vec[0]))) { | |
979 | sink_writes(ev_writer_sink(c->w), "550 no such track on the queue\n"); | |
980 | return 1; | |
981 | } | |
982 | } else | |
983 | q = 0; | |
984 | ++vec; | |
985 | --nvec; | |
986 | qs = xcalloc(nvec, sizeof *qs); | |
987 | for(n = 0; n < nvec; ++n) | |
988 | if(!(qs[n] = queue_find(vec[n]))) { | |
989 | sink_writes(ev_writer_sink(c->w), "550 no such track on the queue\n"); | |
990 | return 1; | |
991 | } | |
eb5dc014 | 992 | if(!has_move_rights(c, qs, nvec)) { |
834e7c4a | 993 | error(0, "%s attempted moveafter but lacks required rights", c->who); |
eb5dc014 | 994 | sink_writes(ev_writer_sink(c->w), |
b4a80f69 | 995 | "510 Not authorized to move those tracks\n"); |
eb5dc014 RK |
996 | return 1; |
997 | } | |
460b9539 | 998 | queue_moveafter(q, nvec, qs, c->who); |
460b9539 | 999 | sink_printf(ev_writer_sink(c->w), "250 Moved tracks\n"); |
1000 | /* If we've moved to the head of the queue then prepare the track. */ | |
1001 | if(q == qhead.next) | |
1002 | prepare(c->ev, q); | |
1003 | return 1; | |
1004 | } | |
1005 | ||
1006 | static int c_part(struct conn *c, | |
1007 | char **vec, | |
1008 | int attribute((unused)) nvec) { | |
27ed5a69 RK |
1009 | const char *track; |
1010 | ||
1011 | if(!(track = trackdb_resolve(vec[0]))) { | |
1012 | sink_writes(ev_writer_sink(c->w), "550 cannot resolve track\n"); | |
1013 | return 1; | |
1014 | } | |
460b9539 | 1015 | sink_printf(ev_writer_sink(c->w), "252 %s\n", |
27ed5a69 | 1016 | quoteutf8(trackdb_getpart(track, vec[1], vec[2]))); |
460b9539 | 1017 | return 1; |
1018 | } | |
1019 | ||
1020 | static int c_resolve(struct conn *c, | |
1021 | char **vec, | |
1022 | int attribute((unused)) nvec) { | |
1023 | const char *track; | |
1024 | ||
1025 | if(!(track = trackdb_resolve(vec[0]))) { | |
1026 | sink_writes(ev_writer_sink(c->w), "550 cannot resolve track\n"); | |
1027 | return 1; | |
1028 | } | |
7b32e917 | 1029 | sink_printf(ev_writer_sink(c->w), "252 %s\n", quoteutf8(track)); |
460b9539 | 1030 | return 1; |
1031 | } | |
1032 | ||
1033 | static int c_tags(struct conn *c, | |
1034 | char attribute((unused)) **vec, | |
1035 | int attribute((unused)) nvec) { | |
1036 | char **tags = trackdb_alltags(); | |
1037 | ||
1038 | sink_printf(ev_writer_sink(c->w), "253 Tag list follows\n"); | |
1039 | while(*tags) { | |
1040 | sink_printf(ev_writer_sink(c->w), "%s%s\n", | |
1041 | **tags == '.' ? "." : "", *tags); | |
1042 | ++tags; | |
1043 | } | |
1044 | sink_writes(ev_writer_sink(c->w), ".\n"); | |
1045 | return 1; /* completed */ | |
460b9539 | 1046 | } |
1047 | ||
1048 | static int c_set_global(struct conn *c, | |
1049 | char **vec, | |
1050 | int attribute((unused)) nvec) { | |
f9635e06 RK |
1051 | if(vec[0][0] == '_') { |
1052 | sink_writes(ev_writer_sink(c->w), "550 cannot set internal global preferences\n"); | |
1053 | return 1; | |
1054 | } | |
460b9539 | 1055 | trackdb_set_global(vec[0], vec[1], c->who); |
1056 | sink_printf(ev_writer_sink(c->w), "250 OK\n"); | |
1057 | return 1; | |
1058 | } | |
1059 | ||
1060 | static int c_get_global(struct conn *c, | |
1061 | char **vec, | |
1062 | int attribute((unused)) nvec) { | |
1063 | const char *s = trackdb_get_global(vec[0]); | |
1064 | ||
1065 | if(s) | |
7b32e917 | 1066 | sink_printf(ev_writer_sink(c->w), "252 %s\n", quoteutf8(s)); |
460b9539 | 1067 | else |
fb1bc1f5 | 1068 | sink_writes(ev_writer_sink(c->w), "555 not found\n"); |
460b9539 | 1069 | return 1; |
1070 | } | |
1071 | ||
7858930d | 1072 | static int c_nop(struct conn *c, |
1073 | char attribute((unused)) **vec, | |
1074 | int attribute((unused)) nvec) { | |
1075 | sink_printf(ev_writer_sink(c->w), "250 Quack\n"); | |
1076 | return 1; | |
1077 | } | |
1078 | ||
2a10b70b RK |
1079 | static int c_new(struct conn *c, |
1080 | char **vec, | |
1081 | int nvec) { | |
d742bb47 RK |
1082 | int max, n; |
1083 | char **tracks; | |
2a10b70b | 1084 | |
d742bb47 RK |
1085 | if(nvec > 0) |
1086 | max = atoi(vec[0]); | |
1087 | else | |
1088 | max = INT_MAX; | |
1089 | if(max <= 0 || max > config->new_max) | |
1090 | max = config->new_max; | |
1091 | tracks = trackdb_new(0, max); | |
2a10b70b | 1092 | sink_printf(ev_writer_sink(c->w), "253 New track list follows\n"); |
d742bb47 | 1093 | n = 0; |
2a10b70b RK |
1094 | while(*tracks) { |
1095 | sink_printf(ev_writer_sink(c->w), "%s%s\n", | |
1096 | **tracks == '.' ? "." : "", *tracks); | |
1097 | ++tracks; | |
1098 | } | |
1099 | sink_writes(ev_writer_sink(c->w), ".\n"); | |
1100 | return 1; /* completed */ | |
1101 | ||
1102 | } | |
1103 | ||
ca831831 RK |
1104 | static int c_rtp_address(struct conn *c, |
1105 | char attribute((unused)) **vec, | |
1106 | int attribute((unused)) nvec) { | |
b50cfb8a | 1107 | if(api == &uaudio_rtp) { |
76e72f65 RK |
1108 | char **addr; |
1109 | ||
1110 | netaddress_format(&config->broadcast, NULL, &addr); | |
ca831831 | 1111 | sink_printf(ev_writer_sink(c->w), "252 %s %s\n", |
76e72f65 RK |
1112 | quoteutf8(addr[1]), |
1113 | quoteutf8(addr[2])); | |
ca831831 RK |
1114 | } else |
1115 | sink_writes(ev_writer_sink(c->w), "550 No RTP\n"); | |
1116 | return 1; | |
1117 | } | |
b12be54a RK |
1118 | |
1119 | static int c_cookie(struct conn *c, | |
1120 | char **vec, | |
1121 | int attribute((unused)) nvec) { | |
1122 | const char *host; | |
1123 | char *user; | |
eb5dc014 | 1124 | rights_type rights; |
b12be54a RK |
1125 | |
1126 | /* Can't log in twice on the same connection */ | |
1127 | if(c->who) { | |
1128 | sink_writes(ev_writer_sink(c->w), "530 already authenticated\n"); | |
1129 | return 1; | |
1130 | } | |
1131 | /* Get some kind of peer identifcation */ | |
1132 | if(!(host = connection_host(c))) { | |
1133 | sink_writes(ev_writer_sink(c->w), "530 authentication failure\n"); | |
1134 | return 1; | |
1135 | } | |
1136 | /* Check the cookie */ | |
eb5dc014 | 1137 | user = verify_cookie(vec[0], &rights); |
b12be54a RK |
1138 | if(!user) { |
1139 | sink_writes(ev_writer_sink(c->w), "530 authentication failure\n"); | |
1140 | return 1; | |
1141 | } | |
1142 | /* Log in */ | |
e48c28bb | 1143 | c->who = user; |
b12be54a | 1144 | c->cookie = vec[0]; |
eb5dc014 | 1145 | c->rights = rights; |
30365519 | 1146 | if(strcmp(host, "local")) |
b12be54a | 1147 | info("S%x %s connected with cookie from %s", c->tag, user, host); |
30365519 | 1148 | else |
eb5dc014 | 1149 | c->rights |= RIGHT__LOCAL; |
0227f67d RK |
1150 | /* Response contains username so client knows who they are acting as */ |
1151 | sink_printf(ev_writer_sink(c->w), "232 %s\n", quoteutf8(user)); | |
b12be54a RK |
1152 | return 1; |
1153 | } | |
1154 | ||
1155 | static int c_make_cookie(struct conn *c, | |
1156 | char attribute((unused)) **vec, | |
1157 | int attribute((unused)) nvec) { | |
1158 | const char *cookie = make_cookie(c->who); | |
1159 | ||
1160 | if(cookie) | |
eb5dc014 | 1161 | sink_printf(ev_writer_sink(c->w), "252 %s\n", quoteutf8(cookie)); |
b12be54a RK |
1162 | else |
1163 | sink_writes(ev_writer_sink(c->w), "550 Cannot create cookie\n"); | |
1164 | return 1; | |
1165 | } | |
1166 | ||
1167 | static int c_revoke(struct conn *c, | |
1168 | char attribute((unused)) **vec, | |
1169 | int attribute((unused)) nvec) { | |
1170 | if(c->cookie) { | |
1171 | revoke_cookie(c->cookie); | |
1172 | sink_writes(ev_writer_sink(c->w), "250 OK\n"); | |
1173 | } else | |
1174 | sink_writes(ev_writer_sink(c->w), "550 Did not log in with cookie\n"); | |
1175 | return 1; | |
1176 | } | |
1177 | ||
f0feb22e RK |
1178 | static int c_adduser(struct conn *c, |
1179 | char **vec, | |
0f55e905 RK |
1180 | int nvec) { |
1181 | const char *rights; | |
1182 | ||
810b8083 | 1183 | if(!config->remote_userman && !(c->rights & RIGHT__LOCAL)) { |
39e7dcfb | 1184 | error(0, "S%x: remote adduser", c->tag); |
810b8083 RK |
1185 | sink_writes(ev_writer_sink(c->w), "550 Remote user management is disabled\n"); |
1186 | return 1; | |
1187 | } | |
0f55e905 RK |
1188 | if(nvec > 2) { |
1189 | rights = vec[2]; | |
1190 | if(parse_rights(vec[2], 0, 1)) { | |
1191 | sink_writes(ev_writer_sink(c->w), "550 Invalid rights list\n"); | |
1192 | return -1; | |
1193 | } | |
1194 | } else | |
1195 | rights = config->default_rights; | |
1196 | if(trackdb_adduser(vec[0], vec[1], rights, | |
ba39faf6 | 1197 | 0/*email*/, 0/*confirmation*/)) |
f0feb22e RK |
1198 | sink_writes(ev_writer_sink(c->w), "550 Cannot create user\n"); |
1199 | else | |
1200 | sink_writes(ev_writer_sink(c->w), "250 User created\n"); | |
1201 | return 1; | |
1202 | } | |
1203 | ||
1204 | static int c_deluser(struct conn *c, | |
1205 | char **vec, | |
1206 | int attribute((unused)) nvec) { | |
0126692c | 1207 | struct conn *d; |
1208 | ||
810b8083 | 1209 | if(!config->remote_userman && !(c->rights & RIGHT__LOCAL)) { |
39e7dcfb | 1210 | error(0, "S%x: remote deluser", c->tag); |
810b8083 RK |
1211 | sink_writes(ev_writer_sink(c->w), "550 Remote user management is disabled\n"); |
1212 | return 1; | |
1213 | } | |
0126692c | 1214 | if(trackdb_deluser(vec[0])) { |
ba39faf6 | 1215 | sink_writes(ev_writer_sink(c->w), "550 Cannot delete user\n"); |
0126692c | 1216 | return 1; |
1217 | } | |
1218 | /* Zap connections belonging to deleted user */ | |
1219 | for(d = connections; d; d = d->next) | |
1220 | if(!strcmp(d->who, vec[0])) | |
1221 | d->rights = 0; | |
1222 | sink_writes(ev_writer_sink(c->w), "250 User deleted\n"); | |
f0feb22e RK |
1223 | return 1; |
1224 | } | |
1225 | ||
1226 | static int c_edituser(struct conn *c, | |
5df73aeb | 1227 | char **vec, |
f0feb22e | 1228 | int attribute((unused)) nvec) { |
0126692c | 1229 | struct conn *d; |
1230 | ||
810b8083 | 1231 | if(!config->remote_userman && !(c->rights & RIGHT__LOCAL)) { |
39e7dcfb | 1232 | error(0, "S%x: remote edituser", c->tag); |
810b8083 RK |
1233 | sink_writes(ev_writer_sink(c->w), "550 Remote user management is disabled\n"); |
1234 | return 1; | |
1235 | } | |
eb5dc014 RK |
1236 | /* RIGHT_ADMIN can do anything; otherwise you can only set your own email |
1237 | * address and password. */ | |
1238 | if((c->rights & RIGHT_ADMIN) | |
5df73aeb RK |
1239 | || (!strcmp(c->who, vec[0]) |
1240 | && (!strcmp(vec[1], "email") | |
1241 | || !strcmp(vec[1], "password")))) { | |
0126692c | 1242 | if(trackdb_edituserinfo(vec[0], vec[1], vec[2])) { |
5df73aeb | 1243 | sink_writes(ev_writer_sink(c->w), "550 Failed to change setting\n"); |
0126692c | 1244 | return 1; |
1245 | } | |
1246 | if(!strcmp(vec[1], "password")) { | |
1247 | /* Zap all connections for this user after a password change */ | |
1248 | for(d = connections; d; d = d->next) | |
1249 | if(!strcmp(d->who, vec[0])) | |
1250 | d->rights = 0; | |
1251 | } else if(!strcmp(vec[1], "rights")) { | |
1252 | /* Update rights for this user */ | |
1253 | rights_type r; | |
1254 | ||
ad492e00 RK |
1255 | if(!parse_rights(vec[2], &r, 1)) { |
1256 | const char *new_rights = rights_string(r); | |
1257 | for(d = connections; d; d = d->next) { | |
1258 | if(!strcmp(d->who, vec[0])) { | |
1259 | /* Update rights */ | |
0126692c | 1260 | d->rights = r; |
ad492e00 RK |
1261 | /* Notify any log connections */ |
1262 | if(d->lo) | |
1263 | sink_printf(ev_writer_sink(d->w), | |
75e7b7c3 | 1264 | "%"PRIxMAX" rights_changed %s\n", |
ad492e00 | 1265 | (uintmax_t)time(0), |
75e7b7c3 | 1266 | quoteutf8(new_rights)); |
ad492e00 RK |
1267 | } |
1268 | } | |
1269 | } | |
0126692c | 1270 | } |
1271 | sink_writes(ev_writer_sink(c->w), "250 OK\n"); | |
834e7c4a | 1272 | } else { |
1273 | error(0, "%s attempted edituser but lacks required rights", c->who); | |
b4a80f69 | 1274 | sink_writes(ev_writer_sink(c->w), "510 Restricted to administrators\n"); |
834e7c4a | 1275 | } |
f0feb22e RK |
1276 | return 1; |
1277 | } | |
1278 | ||
1279 | static int c_userinfo(struct conn *c, | |
1280 | char attribute((unused)) **vec, | |
1281 | int attribute((unused)) nvec) { | |
5df73aeb RK |
1282 | struct kvp *k; |
1283 | const char *value; | |
eb5dc014 | 1284 | |
39e7dcfb RK |
1285 | /* We allow remote querying of rights so that clients can figure out what |
1286 | * they're allowed to do */ | |
1287 | if(!config->remote_userman | |
1288 | && !(c->rights & RIGHT__LOCAL) | |
1289 | && strcmp(vec[1], "rights")) { | |
1290 | error(0, "S%x: remote userinfo %s %s", c->tag, vec[0], vec[1]); | |
810b8083 RK |
1291 | sink_writes(ev_writer_sink(c->w), "550 Remote user management is disabled\n"); |
1292 | return 1; | |
1293 | } | |
eb5dc014 | 1294 | /* RIGHT_ADMIN allows anything; otherwise you can only get your own email |
29f5fbd2 | 1295 | * address and rights list. */ |
eb5dc014 | 1296 | if((c->rights & RIGHT_ADMIN) |
5df73aeb RK |
1297 | || (!strcmp(c->who, vec[0]) |
1298 | && (!strcmp(vec[1], "email") | |
1299 | || !strcmp(vec[1], "rights")))) { | |
1300 | if((k = trackdb_getuserinfo(vec[0]))) | |
1301 | if((value = kvp_get(k, vec[1]))) | |
1302 | sink_printf(ev_writer_sink(c->w), "252 %s\n", quoteutf8(value)); | |
1303 | else | |
1304 | sink_writes(ev_writer_sink(c->w), "555 Not set\n"); | |
1305 | else | |
1306 | sink_writes(ev_writer_sink(c->w), "550 No such user\n"); | |
834e7c4a | 1307 | } else { |
1308 | error(0, "%s attempted userinfo but lacks required rights", c->who); | |
b4a80f69 | 1309 | sink_writes(ev_writer_sink(c->w), "510 Restricted to administrators\n"); |
834e7c4a | 1310 | } |
f0feb22e RK |
1311 | return 1; |
1312 | } | |
1313 | ||
c3be4f19 RK |
1314 | static int c_users(struct conn *c, |
1315 | char attribute((unused)) **vec, | |
1316 | int attribute((unused)) nvec) { | |
1317 | /* TODO de-dupe with c_tags */ | |
1318 | char **users = trackdb_listusers(); | |
1319 | ||
1320 | sink_writes(ev_writer_sink(c->w), "253 User list follows\n"); | |
1321 | while(*users) { | |
1322 | sink_printf(ev_writer_sink(c->w), "%s%s\n", | |
1323 | **users == '.' ? "." : "", *users); | |
1324 | ++users; | |
1325 | } | |
1326 | sink_writes(ev_writer_sink(c->w), ".\n"); | |
1327 | return 1; /* completed */ | |
1328 | } | |
1329 | ||
ba39faf6 RK |
1330 | static int c_register(struct conn *c, |
1331 | char **vec, | |
1332 | int attribute((unused)) nvec) { | |
53710d44 RK |
1333 | char *cs; |
1334 | uint32_t nonce[CONFIRM_SIZE]; | |
1335 | char nonce_str[(32 * CONFIRM_SIZE) / 5 + 1]; | |
1336 | ||
1337 | /* The confirmation string is username/base62(nonce). The confirmation | |
1338 | * process will pick the username back out to identify them but the _whole_ | |
1339 | * string is used as the confirmation string. Base 62 means we used only | |
1340 | * letters and digits, minimizing the chance of the URL being mispasted. */ | |
1341 | gcry_randomize(nonce, sizeof nonce, GCRY_STRONG_RANDOM); | |
1342 | if(basen(nonce, CONFIRM_SIZE, nonce_str, sizeof nonce_str, 62)) { | |
1343 | error(0, "buffer too small encoding confirmation string"); | |
1344 | sink_writes(ev_writer_sink(c->w), "550 Cannot create user\n"); | |
1345 | } | |
1346 | byte_xasprintf(&cs, "%s/%s", vec[0], nonce_str); | |
ba39faf6 RK |
1347 | if(trackdb_adduser(vec[0], vec[1], config->default_rights, vec[2], cs)) |
1348 | sink_writes(ev_writer_sink(c->w), "550 Cannot create user\n"); | |
1349 | else | |
1350 | sink_printf(ev_writer_sink(c->w), "252 %s\n", quoteutf8(cs)); | |
1351 | return 1; | |
1352 | } | |
1353 | ||
1354 | static int c_confirm(struct conn *c, | |
1355 | char **vec, | |
1356 | int attribute((unused)) nvec) { | |
ba39faf6 | 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 | } | |
53710d44 RK |
1366 | /* Picking the LAST / means we don't (here) rule out slashes in usernames. */ |
1367 | if(!(sep = strrchr(vec[0], '/'))) { | |
ba39faf6 RK |
1368 | sink_writes(ev_writer_sink(c->w), "550 Malformed confirmation string\n"); |
1369 | return 1; | |
1370 | } | |
53710d44 | 1371 | user = xstrndup(vec[0], sep - vec[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); | |
80dc2c5f | 1853 | info("listening on %s", name); |
460b9539 | 1854 | return fd; |
1855 | } | |
1856 | ||
1857 | int server_stop(ev_source *ev, int fd) { | |
1858 | xclose(fd); | |
1859 | return ev_listen_cancel(ev, fd); | |
1860 | } | |
1861 | ||
1862 | /* | |
1863 | Local Variables: | |
1864 | c-basic-offset:2 | |
1865 | comment-column:40 | |
1866 | fill-column:79 | |
1867 | End: | |
1868 | */ |