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