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