2 * This file is part of DisOrder.
3 * Copyright (C) 2004, 2005, 2006, 2007 Richard Kettlewell
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.
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.
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
25 #include <sys/types.h>
26 #include <sys/socket.h>
39 #include <netinet/in.h>
45 #include "server-queue.h"
52 #include "configuration.h"
63 #include "trackname.h"
71 # define NONCE_SIZE 16
74 int volume_left, volume_right; /* last known volume */
76 /** @brief Accept all well-formed login attempts
87 /** @brief One client connection */
89 /** @brief Read commands from here */
91 /** @brief Send responses to here */
93 /** @brief Underlying file descriptor */
95 /** @brief Unique identifier for connection used in log messages */
97 /** @brief Login name or NULL */
99 /** @brief Event loop */
101 /** @brief Nonce chosen for this connection */
102 unsigned char nonce[NONCE_SIZE];
103 /** @brief Current reader callback
105 * We change this depending on whether we're servicing the @b log command
107 ev_reader_callback *reader;
108 /** @brief Event log output sending to this connection */
109 struct eventlog_output *lo;
110 /** @brief Parent listener */
111 const struct listener *l;
112 /** @brief Login cookie or NULL */
114 /** @brief Connection rights */
118 static int reader_callback(ev_source *ev,
125 static const char *noyes[] = { "no", "yes" };
127 /** @brief Called when a connection's writer fails or is shut down
129 * If the connection still has a raeder that is cancelled.
131 static int writer_error(ev_source attribute((unused)) *ev,
136 D(("server writer_error S%x %d", c->tag, errno_value));
137 if(errno_value == 0) {
139 D(("S%x writer completed", c->tag));
141 if(errno_value != EPIPE)
142 error(errno_value, "S%x write error on socket", c->tag);
144 D(("cancel reader"));
145 ev_reader_cancel(c->r);
148 D(("done cancel reader"));
155 /** @brief Called when a conncetion's reader fails or is shut down
157 * If connection still has a writer then it is closed.
159 static int reader_error(ev_source attribute((unused)) *ev,
164 D(("server reader_error S%x %d", c->tag, errno_value));
165 error(errno_value, "S%x read error on socket", c->tag);
167 ev_writer_close(c->w);
174 static int c_disable(struct conn *c, char **vec, int nvec) {
176 disable_playing(c->who);
177 else if(nvec == 1 && !strcmp(vec[0], "now"))
178 disable_playing(c->who);
180 sink_writes(ev_writer_sink(c->w), "550 invalid argument\n");
181 return 1; /* completed */
183 sink_writes(ev_writer_sink(c->w), "250 OK\n");
184 return 1; /* completed */
187 static int c_enable(struct conn *c,
188 char attribute((unused)) **vec,
189 int attribute((unused)) nvec) {
190 enable_playing(c->who, c->ev);
191 /* Enable implicitly unpauses if there is nothing playing */
192 if(paused && !playing) resume_playing(c->who);
193 sink_writes(ev_writer_sink(c->w), "250 OK\n");
194 return 1; /* completed */
197 static int c_enabled(struct conn *c,
198 char attribute((unused)) **vec,
199 int attribute((unused)) nvec) {
200 sink_printf(ev_writer_sink(c->w), "252 %s\n", noyes[playing_is_enabled()]);
201 return 1; /* completed */
204 static int c_play(struct conn *c, char **vec,
205 int attribute((unused)) nvec) {
207 struct queue_entry *q;
209 if(!trackdb_exists(vec[0])) {
210 sink_writes(ev_writer_sink(c->w), "550 track is not in database\n");
213 if(!(track = trackdb_resolve(vec[0]))) {
214 sink_writes(ev_writer_sink(c->w), "550 cannot resolve track\n");
217 q = queue_add(track, c->who, WHERE_BEFORE_RANDOM);
219 /* If we added the first track, and something is playing, then prepare the
220 * new track. If nothing is playing then we don't bother as it wouldn't gain
222 if(q == qhead.next && playing)
224 sink_printf(ev_writer_sink(c->w), "252 %s\n", q->id);
225 /* If the queue was empty but we are for some reason paused then
227 if(!playing) resume_playing(0);
229 return 1; /* completed */
232 static int c_remove(struct conn *c, char **vec,
233 int attribute((unused)) nvec) {
234 struct queue_entry *q;
237 if(!(q = queue_find(vec[0]))) {
238 sink_writes(ev_writer_sink(c->w), "550 no such track on the queue\n");
242 if(!strcmp(q->submitter, c->who))
243 r = RIGHT_REMOVE_MINE;
245 r = RIGHT_REMOVE_ANY;
247 r = RIGHT_REMOVE_RANDOM;
248 if(!(c->rights & r)) {
249 sink_writes(ev_writer_sink(c->w),
250 "550 Not authorized to remove that track\n");
253 queue_remove(q, c->who);
254 /* De-prepare the track. */
256 /* If we removed a random track then add another one. */
257 if(q->state == playing_random)
259 /* Prepare whatever the next head track is. */
260 if(qhead.next != &qhead)
261 prepare(c->ev, qhead.next);
263 sink_writes(ev_writer_sink(c->w), "250 removed\n");
264 return 1; /* completed */
267 static int c_scratch(struct conn *c,
273 sink_writes(ev_writer_sink(c->w), "250 nothing is playing\n");
274 return 1; /* completed */
276 /* TODO there is a bug here: if we specify an ID but it's not the currently
277 * playing track then you will get 550 if you weren't authorized to scratch
278 * the currently playing track. */
279 if(playing->submitter)
280 if(!strcmp(playing->submitter, c->who))
281 r = RIGHT_SCRATCH_MINE;
283 r = RIGHT_SCRATCH_ANY;
285 r = RIGHT_SCRATCH_RANDOM;
286 if(!(c->rights & r)) {
287 sink_writes(ev_writer_sink(c->w),
288 "550 Not authorized to scratch that track\n");
291 scratch(c->who, nvec == 1 ? vec[0] : 0);
292 /* If you scratch an unpaused track then it is automatically unpaused */
294 sink_writes(ev_writer_sink(c->w), "250 scratched\n");
295 return 1; /* completed */
298 static int c_pause(struct conn *c,
299 char attribute((unused)) **vec,
300 int attribute((unused)) nvec) {
302 sink_writes(ev_writer_sink(c->w), "250 nothing is playing\n");
303 return 1; /* completed */
306 sink_writes(ev_writer_sink(c->w), "250 already paused\n");
307 return 1; /* completed */
309 if(pause_playing(c->who) < 0)
310 sink_writes(ev_writer_sink(c->w), "550 cannot pause this track\n");
312 sink_writes(ev_writer_sink(c->w), "250 paused\n");
316 static int c_resume(struct conn *c,
317 char attribute((unused)) **vec,
318 int attribute((unused)) nvec) {
320 sink_writes(ev_writer_sink(c->w), "250 not paused\n");
321 return 1; /* completed */
323 resume_playing(c->who);
324 sink_writes(ev_writer_sink(c->w), "250 paused\n");
328 static int c_shutdown(struct conn *c,
329 char attribute((unused)) **vec,
330 int attribute((unused)) nvec) {
331 info("S%x shut down by %s", c->tag, c->who);
332 sink_writes(ev_writer_sink(c->w), "250 shutting down\n");
333 ev_writer_flush(c->w);
337 static int c_reconfigure(struct conn *c,
338 char attribute((unused)) **vec,
339 int attribute((unused)) nvec) {
340 info("S%x reconfigure by %s", c->tag, c->who);
341 if(reconfigure(c->ev, 1))
342 sink_writes(ev_writer_sink(c->w), "550 error reading new config\n");
344 sink_writes(ev_writer_sink(c->w), "250 installed new config\n");
345 return 1; /* completed */
348 static int c_rescan(struct conn *c,
349 char attribute((unused)) **vec,
350 int attribute((unused)) nvec) {
351 info("S%x rescan by %s", c->tag, c->who);
352 trackdb_rescan(c->ev);
353 sink_writes(ev_writer_sink(c->w), "250 initiated rescan\n");
354 return 1; /* completed */
357 static int c_version(struct conn *c,
358 char attribute((unused)) **vec,
359 int attribute((unused)) nvec) {
360 /* VERSION had better only use the basic character set */
361 sink_printf(ev_writer_sink(c->w), "251 %s\n", disorder_short_version_string);
362 return 1; /* completed */
365 static int c_playing(struct conn *c,
366 char attribute((unused)) **vec,
367 int attribute((unused)) nvec) {
369 queue_fix_sofar(playing);
370 playing->expected = 0;
371 sink_printf(ev_writer_sink(c->w), "252 %s\n", queue_marshall(playing));
373 sink_printf(ev_writer_sink(c->w), "259 nothing playing\n");
374 return 1; /* completed */
377 static const char *connection_host(struct conn *c) {
380 struct sockaddr_in in;
381 struct sockaddr_in6 in6;
387 /* get connection data */
389 if(getpeername(c->fd, &u.sa, &l) < 0) {
390 error(errno, "S%x error calling getpeername", c->tag);
393 if(c->l->pf != PF_UNIX) {
394 if((n = getnameinfo(&u.sa, l,
395 host, sizeof host, 0, 0, NI_NUMERICHOST))) {
396 error(0, "S%x error calling getnameinfo: %s", c->tag, gai_strerror(n));
399 return xstrdup(host);
404 static int c_user(struct conn *c,
406 int attribute((unused)) nvec) {
408 const char *res, *host, *password;
412 sink_writes(ev_writer_sink(c->w), "530 already authenticated\n");
415 /* get connection data */
416 if(!(host = connection_host(c))) {
417 sink_writes(ev_writer_sink(c->w), "530 authentication failure\n");
421 k = trackdb_getuserinfo(vec[0]);
422 /* reject nonexistent users */
424 error(0, "S%x unknown user '%s' from %s", c->tag, vec[0], host);
425 sink_writes(ev_writer_sink(c->w), "530 authentication failed\n");
428 /* reject unconfirmed users */
429 if(kvp_get(k, "confirmation")) {
430 error(0, "S%x unconfirmed user '%s' from %s", c->tag, vec[0], host);
431 sink_writes(ev_writer_sink(c->w), "530 authentication failed\n");
434 password = kvp_get(k, "password");
435 if(!password) password = "";
436 if(parse_rights(kvp_get(k, "rights"), &rights)) {
437 error(0, "error parsing rights for %s", vec[0]);
438 sink_writes(ev_writer_sink(c->w), "530 authentication failed\n");
441 /* check whether the response is right */
442 res = authhash(c->nonce, sizeof c->nonce, password,
443 config->authorization_algorithm);
444 if(wideopen || (res && !strcmp(res, vec[1]))) {
447 /* currently we only bother logging remote connections */
448 if(strcmp(host, "local")) {
449 info("S%x %s connected from %s", c->tag, vec[0], host);
450 c->rights |= RIGHT__LOCAL;
452 sink_writes(ev_writer_sink(c->w), "230 OK\n");
455 /* oops, response was wrong */
456 info("S%x authentication failure for %s from %s", c->tag, vec[0], host);
457 sink_writes(ev_writer_sink(c->w), "530 authentication failed\n");
461 static int c_recent(struct conn *c,
462 char attribute((unused)) **vec,
463 int attribute((unused)) nvec) {
464 const struct queue_entry *q;
466 sink_writes(ev_writer_sink(c->w), "253 Tracks follow\n");
467 for(q = phead.next; q != &phead; q = q->next)
468 sink_printf(ev_writer_sink(c->w), " %s\n", queue_marshall(q));
469 sink_writes(ev_writer_sink(c->w), ".\n");
470 return 1; /* completed */
473 static int c_queue(struct conn *c,
474 char attribute((unused)) **vec,
475 int attribute((unused)) nvec) {
476 struct queue_entry *q;
481 sink_writes(ev_writer_sink(c->w), "253 Tracks follow\n");
482 if(playing_is_enabled() && !paused) {
484 queue_fix_sofar(playing);
485 if((l = trackdb_get(playing->track, "_length"))
486 && (length = atol(l))) {
488 when += length - playing->sofar + config->gap;
491 /* Nothing is playing but playing is enabled, so whatever is
492 * first in the queue can be expected to start immediately. */
495 for(q = qhead.next; q != &qhead; q = q->next) {
496 /* fill in estimated start time */
498 sink_printf(ev_writer_sink(c->w), " %s\n", queue_marshall(q));
499 /* update for next track */
501 if((l = trackdb_get(q->track, "_length"))
502 && (length = atol(l)))
503 when += length + config->gap;
508 sink_writes(ev_writer_sink(c->w), ".\n");
509 return 1; /* completed */
512 static int output_list(struct conn *c, char **vec) {
514 sink_printf(ev_writer_sink(c->w), "%s\n", *vec++);
515 sink_writes(ev_writer_sink(c->w), ".\n");
519 static int files_dirs(struct conn *c,
522 enum trackdb_listable what) {
523 const char *dir, *re, *errstr;
529 case 0: dir = 0; re = 0; break;
530 case 1: dir = vec[0]; re = 0; break;
531 case 2: dir = vec[0]; re = vec[1]; break;
534 /* A bit of a bodge to make sure the args don't trample on cache keys */
535 if(dir && strchr(dir, '\n')) {
536 sink_writes(ev_writer_sink(c->w), "550 invalid directory name\n");
539 if(re && strchr(re, '\n')) {
540 sink_writes(ev_writer_sink(c->w), "550 invalid regexp\n");
543 /* We bother eliminating "" because the web interface is relatively
544 * likely to send it */
546 byte_xasprintf(&key, "%d\n%s\n%s", (int)what, dir ? dir : "", re);
547 fvec = (char **)cache_get(&cache_files_type, key);
549 /* Got a cache hit, don't store the answer in the cache */
552 rec = 0; /* quieten compiler */
554 /* Cache miss, we'll do the lookup and key != 0 so we'll store the answer
556 if(!(rec = pcre_compile(re, PCRE_CASELESS|PCRE_UTF8,
557 &errstr, &erroffset, 0))) {
558 sink_printf(ev_writer_sink(c->w), "550 Error compiling regexp: %s\n",
562 /* It only counts as a miss if the regexp was valid. */
563 ++cache_files_misses;
566 /* No regexp, don't bother caching the result */
572 /* No cache hit (either because a miss, or because we did not look) so do
575 fvec = trackdb_list(dir, 0, what, rec);
577 fvec = trackdb_list(0, 0, what, rec);
580 /* Put the answer in the cache */
581 cache_put(&cache_files_type, key, fvec);
582 sink_writes(ev_writer_sink(c->w), "253 Listing follow\n");
583 return output_list(c, fvec);
586 static int c_files(struct conn *c,
589 return files_dirs(c, vec, nvec, trackdb_files);
592 static int c_dirs(struct conn *c,
595 return files_dirs(c, vec, nvec, trackdb_directories);
598 static int c_allfiles(struct conn *c,
601 return files_dirs(c, vec, nvec, trackdb_directories|trackdb_files);
604 static int c_get(struct conn *c,
606 int attribute((unused)) nvec) {
609 if(vec[1][0] != '_' && (v = trackdb_get(vec[0], vec[1])))
610 sink_printf(ev_writer_sink(c->w), "252 %s\n", v);
612 sink_writes(ev_writer_sink(c->w), "555 not found\n");
616 static int c_length(struct conn *c,
618 int attribute((unused)) nvec) {
619 const char *track, *v;
621 if(!(track = trackdb_resolve(vec[0]))) {
622 sink_writes(ev_writer_sink(c->w), "550 cannot resolve track\n");
625 if((v = trackdb_get(track, "_length")))
626 sink_printf(ev_writer_sink(c->w), "252 %s\n", v);
628 sink_writes(ev_writer_sink(c->w), "550 not found\n");
632 static int c_set(struct conn *c,
634 int attribute((unused)) nvec) {
635 if(vec[1][0] != '_' && !trackdb_set(vec[0], vec[1], vec[2]))
636 sink_writes(ev_writer_sink(c->w), "250 OK\n");
638 sink_writes(ev_writer_sink(c->w), "550 not found\n");
642 static int c_prefs(struct conn *c,
644 int attribute((unused)) nvec) {
647 k = trackdb_get_all(vec[0]);
648 sink_writes(ev_writer_sink(c->w), "253 prefs follow\n");
649 for(; k; k = k->next)
650 if(k->name[0] != '_') /* omit internal values */
651 sink_printf(ev_writer_sink(c->w),
652 " %s %s\n", quoteutf8(k->name), quoteutf8(k->value));
653 sink_writes(ev_writer_sink(c->w), ".\n");
657 static int c_exists(struct conn *c,
659 int attribute((unused)) nvec) {
660 sink_printf(ev_writer_sink(c->w), "252 %s\n", noyes[trackdb_exists(vec[0])]);
664 static void search_parse_error(const char *msg, void *u) {
665 *(const char **)u = msg;
668 static int c_search(struct conn *c,
670 int attribute((unused)) nvec) {
671 char **terms, **results;
672 int nterms, nresults, n;
673 const char *e = "unknown error";
675 /* This is a bit of a bodge. Initially it's there to make the eclient
676 * interface a bit more convenient to add searching to, but it has the more
677 * compelling advantage that if everything uses it, then interpretation of
678 * user-supplied search strings will be the same everywhere. */
679 if(!(terms = split(vec[0], &nterms, SPLIT_QUOTES, search_parse_error, &e))) {
680 sink_printf(ev_writer_sink(c->w), "550 %s\n", e);
682 results = trackdb_search(terms, nterms, &nresults);
683 sink_printf(ev_writer_sink(c->w), "253 %d matches\n", nresults);
684 for(n = 0; n < nresults; ++n)
685 sink_printf(ev_writer_sink(c->w), "%s\n", results[n]);
686 sink_writes(ev_writer_sink(c->w), ".\n");
691 static int c_random_enable(struct conn *c,
692 char attribute((unused)) **vec,
693 int attribute((unused)) nvec) {
694 enable_random(c->who, c->ev);
695 /* Enable implicitly unpauses if there is nothing playing */
696 if(paused && !playing) resume_playing(c->who);
697 sink_writes(ev_writer_sink(c->w), "250 OK\n");
698 return 1; /* completed */
701 static int c_random_disable(struct conn *c,
702 char attribute((unused)) **vec,
703 int attribute((unused)) nvec) {
704 disable_random(c->who);
705 sink_writes(ev_writer_sink(c->w), "250 OK\n");
706 return 1; /* completed */
709 static int c_random_enabled(struct conn *c,
710 char attribute((unused)) **vec,
711 int attribute((unused)) nvec) {
712 sink_printf(ev_writer_sink(c->w), "252 %s\n", noyes[random_is_enabled()]);
713 return 1; /* completed */
716 static void got_stats(char *stats, void *u) {
717 struct conn *const c = u;
719 sink_printf(ev_writer_sink(c->w), "253 stats\n%s\n.\n", stats);
720 /* Now we can start processing commands again */
721 ev_reader_enable(c->r);
724 static int c_stats(struct conn *c,
725 char attribute((unused)) **vec,
726 int attribute((unused)) nvec) {
727 trackdb_stats_subprocess(c->ev, got_stats, c);
728 return 0; /* not yet complete */
731 static int c_volume(struct conn *c,
743 l = r = atoi(vec[0]);
754 rights = set ? RIGHT_VOLUME : RIGHT_READ;
755 if(!(c->rights & rights)) {
756 sink_writes(ev_writer_sink(c->w), "530 Prohibited\n");
759 if(mixer_control(&l, &r, set))
760 sink_writes(ev_writer_sink(c->w), "550 error accessing mixer\n");
762 sink_printf(ev_writer_sink(c->w), "252 %d %d\n", l, r);
763 if(l != volume_left || r != volume_right) {
766 snprintf(lb, sizeof lb, "%d", l);
767 snprintf(rb, sizeof rb, "%d", r);
768 eventlog("volume", lb, rb, (char *)0);
774 /** @brief Called when data arrives on a log connection
776 * We just discard all such data. The client may occasionally send data as a
779 static int logging_reader_callback(ev_source attribute((unused)) *ev,
781 void attribute((unused)) *ptr,
783 int attribute((unused)) eof,
784 void attribute((unused)) *u) {
787 ev_reader_consume(reader, bytes);
789 /* Oops, that's all for now */
790 D(("logging reader eof"));
793 ev_writer_close(c->w);
801 static void logclient(const char *msg, void *user) {
802 struct conn *c = user;
805 /* This connection has gone up in smoke for some reason */
806 eventlog_remove(c->lo);
809 sink_printf(ev_writer_sink(c->w), "%"PRIxMAX" %s\n",
810 (uintmax_t)time(0), msg);
813 static int c_log(struct conn *c,
814 char attribute((unused)) **vec,
815 int attribute((unused)) nvec) {
818 sink_writes(ev_writer_sink(c->w), "254 OK\n");
819 /* pump out initial state */
821 sink_printf(ev_writer_sink(c->w), "%"PRIxMAX" state %s\n",
823 playing_is_enabled() ? "enable_play" : "disable_play");
824 sink_printf(ev_writer_sink(c->w), "%"PRIxMAX" state %s\n",
826 random_is_enabled() ? "enable_random" : "disable_random");
827 sink_printf(ev_writer_sink(c->w), "%"PRIxMAX" state %s\n",
829 paused ? "pause" : "resume");
831 sink_printf(ev_writer_sink(c->w), "%"PRIxMAX" state playing\n",
834 sink_printf(ev_writer_sink(c->w), "%"PRIxMAX" volume %d %d\n",
835 (uintmax_t)now, volume_left, volume_right);
836 c->lo = xmalloc(sizeof *c->lo);
837 c->lo->fn = logclient;
840 c->reader = logging_reader_callback;
844 /** @brief Test whether a move is allowed
845 * @param c Connection
846 * @param qs List of IDs on queue
847 * @param nqs Number of IDs
848 * @return 0 if move is prohibited, non-0 if it is allowed
850 static int has_move_rights(struct conn *c, struct queue_entry **qs, int nqs) {
853 for(; nqs > 0; ++qs, --nqs) {
854 struct queue_entry *const q = *qs;
857 if(!strcmp(q->submitter, c->who))
858 r |= RIGHT_MOVE_MINE;
862 r |= RIGHT_MOVE_RANDOM;
864 return (c->rights & r) == r;
867 static int c_move(struct conn *c,
869 int attribute((unused)) nvec) {
870 struct queue_entry *q;
873 if(!(q = queue_find(vec[0]))) {
874 sink_writes(ev_writer_sink(c->w), "550 no such track on the queue\n");
877 if(!has_move_rights(c, &q, 1)) {
878 sink_writes(ev_writer_sink(c->w),
879 "550 Not authorized to move that track\n");
882 n = queue_move(q, atoi(vec[1]), c->who);
883 sink_printf(ev_writer_sink(c->w), "252 %d\n", n);
884 /* If we've moved to the head of the queue then prepare the track. */
890 static int c_moveafter(struct conn *c,
892 int attribute((unused)) nvec) {
893 struct queue_entry *q, **qs;
897 if(!(q = queue_find(vec[0]))) {
898 sink_writes(ev_writer_sink(c->w), "550 no such track on the queue\n");
905 qs = xcalloc(nvec, sizeof *qs);
906 for(n = 0; n < nvec; ++n)
907 if(!(qs[n] = queue_find(vec[n]))) {
908 sink_writes(ev_writer_sink(c->w), "550 no such track on the queue\n");
911 if(!has_move_rights(c, qs, nvec)) {
912 sink_writes(ev_writer_sink(c->w),
913 "550 Not authorized to move those tracks\n");
916 queue_moveafter(q, nvec, qs, c->who);
917 sink_printf(ev_writer_sink(c->w), "250 Moved tracks\n");
918 /* If we've moved to the head of the queue then prepare the track. */
924 static int c_part(struct conn *c,
926 int attribute((unused)) nvec) {
927 sink_printf(ev_writer_sink(c->w), "252 %s\n",
928 trackdb_getpart(vec[0], vec[1], vec[2]));
932 static int c_resolve(struct conn *c,
934 int attribute((unused)) nvec) {
937 if(!(track = trackdb_resolve(vec[0]))) {
938 sink_writes(ev_writer_sink(c->w), "550 cannot resolve track\n");
941 sink_printf(ev_writer_sink(c->w), "252 %s\n", track);
945 static int c_tags(struct conn *c,
946 char attribute((unused)) **vec,
947 int attribute((unused)) nvec) {
948 char **tags = trackdb_alltags();
950 sink_printf(ev_writer_sink(c->w), "253 Tag list follows\n");
952 sink_printf(ev_writer_sink(c->w), "%s%s\n",
953 **tags == '.' ? "." : "", *tags);
956 sink_writes(ev_writer_sink(c->w), ".\n");
957 return 1; /* completed */
960 static int c_set_global(struct conn *c,
962 int attribute((unused)) nvec) {
963 if(vec[0][0] == '_') {
964 sink_writes(ev_writer_sink(c->w), "550 cannot set internal global preferences\n");
967 trackdb_set_global(vec[0], vec[1], c->who);
968 sink_printf(ev_writer_sink(c->w), "250 OK\n");
972 static int c_get_global(struct conn *c,
974 int attribute((unused)) nvec) {
975 const char *s = trackdb_get_global(vec[0]);
978 sink_printf(ev_writer_sink(c->w), "252 %s\n", s);
980 sink_writes(ev_writer_sink(c->w), "555 not found\n");
984 static int c_nop(struct conn *c,
985 char attribute((unused)) **vec,
986 int attribute((unused)) nvec) {
987 sink_printf(ev_writer_sink(c->w), "250 Quack\n");
991 static int c_new(struct conn *c,
994 char **tracks = trackdb_new(0, nvec > 0 ? atoi(vec[0]) : INT_MAX);
996 sink_printf(ev_writer_sink(c->w), "253 New track list follows\n");
998 sink_printf(ev_writer_sink(c->w), "%s%s\n",
999 **tracks == '.' ? "." : "", *tracks);
1002 sink_writes(ev_writer_sink(c->w), ".\n");
1003 return 1; /* completed */
1007 static int c_rtp_address(struct conn *c,
1008 char attribute((unused)) **vec,
1009 int attribute((unused)) nvec) {
1010 if(config->speaker_backend == BACKEND_NETWORK) {
1011 sink_printf(ev_writer_sink(c->w), "252 %s %s\n",
1012 quoteutf8(config->broadcast.s[0]),
1013 quoteutf8(config->broadcast.s[1]));
1015 sink_writes(ev_writer_sink(c->w), "550 No RTP\n");
1019 static int c_cookie(struct conn *c,
1021 int attribute((unused)) nvec) {
1026 /* Can't log in twice on the same connection */
1028 sink_writes(ev_writer_sink(c->w), "530 already authenticated\n");
1031 /* Get some kind of peer identifcation */
1032 if(!(host = connection_host(c))) {
1033 sink_writes(ev_writer_sink(c->w), "530 authentication failure\n");
1036 /* Check the cookie */
1037 user = verify_cookie(vec[0], &rights);
1039 sink_writes(ev_writer_sink(c->w), "530 authentication failure\n");
1046 if(strcmp(host, "local")) {
1047 info("S%x %s connected with cookie from %s", c->tag, user, host);
1048 c->rights |= RIGHT__LOCAL;
1050 sink_writes(ev_writer_sink(c->w), "230 OK\n");
1054 static int c_make_cookie(struct conn *c,
1055 char attribute((unused)) **vec,
1056 int attribute((unused)) nvec) {
1057 const char *cookie = make_cookie(c->who);
1060 sink_printf(ev_writer_sink(c->w), "252 %s\n", quoteutf8(cookie));
1062 sink_writes(ev_writer_sink(c->w), "550 Cannot create cookie\n");
1066 static int c_revoke(struct conn *c,
1067 char attribute((unused)) **vec,
1068 int attribute((unused)) nvec) {
1070 revoke_cookie(c->cookie);
1071 sink_writes(ev_writer_sink(c->w), "250 OK\n");
1073 sink_writes(ev_writer_sink(c->w), "550 Did not log in with cookie\n");
1077 static int c_adduser(struct conn *c,
1079 int attribute((unused)) nvec) {
1080 if(trackdb_adduser(vec[0], vec[1], default_rights(), 0))
1081 sink_writes(ev_writer_sink(c->w), "550 Cannot create user\n");
1083 sink_writes(ev_writer_sink(c->w), "250 User created\n");
1087 static int c_deluser(struct conn *c,
1089 int attribute((unused)) nvec) {
1090 if(trackdb_deluser(vec[0]))
1091 sink_writes(ev_writer_sink(c->w), "550 Cannot deleted user\n");
1093 sink_writes(ev_writer_sink(c->w), "250 User deleted\n");
1097 static int c_edituser(struct conn *c,
1099 int attribute((unused)) nvec) {
1100 /* RIGHT_ADMIN can do anything; otherwise you can only set your own email
1101 * address and password. */
1102 if((c->rights & RIGHT_ADMIN)
1103 || (!strcmp(c->who, vec[0])
1104 && (!strcmp(vec[1], "email")
1105 || !strcmp(vec[1], "password")))) {
1106 if(trackdb_edituserinfo(vec[0], vec[1], vec[2]))
1107 sink_writes(ev_writer_sink(c->w), "550 Failed to change setting\n");
1109 sink_writes(ev_writer_sink(c->w), "250 OK\n");
1111 sink_writes(ev_writer_sink(c->w), "550 Restricted to administrators\n");
1115 static int c_userinfo(struct conn *c,
1116 char attribute((unused)) **vec,
1117 int attribute((unused)) nvec) {
1121 /* RIGHT_ADMIN allows anything; otherwise you can only get your own email
1122 * address and righst list. */
1123 if((c->rights & RIGHT_ADMIN)
1124 || (!strcmp(c->who, vec[0])
1125 && (!strcmp(vec[1], "email")
1126 || !strcmp(vec[1], "rights")))) {
1127 if((k = trackdb_getuserinfo(vec[0])))
1128 if((value = kvp_get(k, vec[1])))
1129 sink_printf(ev_writer_sink(c->w), "252 %s\n", quoteutf8(value));
1131 sink_writes(ev_writer_sink(c->w), "555 Not set\n");
1133 sink_writes(ev_writer_sink(c->w), "550 No such user\n");
1135 sink_writes(ev_writer_sink(c->w), "550 Restricted to administrators\n");
1139 static int c_users(struct conn *c,
1140 char attribute((unused)) **vec,
1141 int attribute((unused)) nvec) {
1142 /* TODO de-dupe with c_tags */
1143 char **users = trackdb_listusers();
1145 sink_writes(ev_writer_sink(c->w), "253 User list follows\n");
1147 sink_printf(ev_writer_sink(c->w), "%s%s\n",
1148 **users == '.' ? "." : "", *users);
1151 sink_writes(ev_writer_sink(c->w), ".\n");
1152 return 1; /* completed */
1155 static const struct command {
1156 /** @brief Command name */
1159 /** @brief Minimum number of arguments */
1162 /** @brief Maximum number of arguments */
1165 /** @brief Function to process command */
1166 int (*fn)(struct conn *, char **, int);
1168 /** @brief Rights required to execute command
1170 * 0 means that the command can be issued without logging in. If multiple
1171 * bits are listed here any of those rights will do.
1175 { "adduser", 2, 2, c_adduser, RIGHT_ADMIN|RIGHT__LOCAL },
1176 { "allfiles", 0, 2, c_allfiles, RIGHT_READ },
1177 { "cookie", 1, 1, c_cookie, 0 },
1178 { "deluser", 1, 1, c_deluser, RIGHT_ADMIN|RIGHT__LOCAL },
1179 { "dirs", 0, 2, c_dirs, RIGHT_READ },
1180 { "disable", 0, 1, c_disable, RIGHT_GLOBAL_PREFS },
1181 { "edituser", 3, 3, c_edituser, RIGHT_ADMIN|RIGHT_USERINFO },
1182 { "enable", 0, 0, c_enable, RIGHT_GLOBAL_PREFS },
1183 { "enabled", 0, 0, c_enabled, RIGHT_READ },
1184 { "exists", 1, 1, c_exists, RIGHT_READ },
1185 { "files", 0, 2, c_files, RIGHT_READ },
1186 { "get", 2, 2, c_get, RIGHT_READ },
1187 { "get-global", 1, 1, c_get_global, RIGHT_READ },
1188 { "length", 1, 1, c_length, RIGHT_READ },
1189 { "log", 0, 0, c_log, RIGHT_READ },
1190 { "make-cookie", 0, 0, c_make_cookie, RIGHT_READ },
1191 { "move", 2, 2, c_move, RIGHT_MOVE__MASK },
1192 { "moveafter", 1, INT_MAX, c_moveafter, RIGHT_MOVE__MASK },
1193 { "new", 0, 1, c_new, RIGHT_READ },
1194 { "nop", 0, 0, c_nop, 0 },
1195 { "part", 3, 3, c_part, RIGHT_READ },
1196 { "pause", 0, 0, c_pause, RIGHT_PAUSE },
1197 { "play", 1, 1, c_play, RIGHT_PLAY },
1198 { "playing", 0, 0, c_playing, RIGHT_READ },
1199 { "prefs", 1, 1, c_prefs, RIGHT_READ },
1200 { "queue", 0, 0, c_queue, RIGHT_READ },
1201 { "random-disable", 0, 0, c_random_disable, RIGHT_GLOBAL_PREFS },
1202 { "random-enable", 0, 0, c_random_enable, RIGHT_GLOBAL_PREFS },
1203 { "random-enabled", 0, 0, c_random_enabled, RIGHT_READ },
1204 { "recent", 0, 0, c_recent, RIGHT_READ },
1205 { "reconfigure", 0, 0, c_reconfigure, RIGHT_ADMIN },
1206 { "remove", 1, 1, c_remove, RIGHT_REMOVE__MASK },
1207 { "rescan", 0, 0, c_rescan, RIGHT_RESCAN },
1208 { "resolve", 1, 1, c_resolve, RIGHT_READ },
1209 { "resume", 0, 0, c_resume, RIGHT_PAUSE },
1210 { "revoke", 0, 0, c_revoke, RIGHT_READ },
1211 { "rtp-address", 0, 0, c_rtp_address, 0 },
1212 { "scratch", 0, 1, c_scratch, RIGHT_SCRATCH__MASK },
1213 { "search", 1, 1, c_search, RIGHT_READ },
1214 { "set", 3, 3, c_set, RIGHT_PREFS, },
1215 { "set-global", 2, 2, c_set_global, RIGHT_GLOBAL_PREFS },
1216 { "shutdown", 0, 0, c_shutdown, RIGHT_ADMIN },
1217 { "stats", 0, 0, c_stats, RIGHT_READ },
1218 { "tags", 0, 0, c_tags, RIGHT_READ },
1219 { "unset", 2, 2, c_set, RIGHT_PREFS },
1220 { "unset-global", 1, 1, c_set_global, RIGHT_GLOBAL_PREFS },
1221 { "user", 2, 2, c_user, 0 },
1222 { "userinfo", 2, 2, c_userinfo, RIGHT_READ },
1223 { "users", 0, 0, c_users, RIGHT_READ },
1224 { "version", 0, 0, c_version, RIGHT_READ },
1225 { "volume", 0, 2, c_volume, RIGHT_READ|RIGHT_VOLUME }
1228 static void command_error(const char *msg, void *u) {
1231 sink_printf(ev_writer_sink(c->w), "500 parse error: %s\n", msg);
1234 /* process a command. Return 1 if complete, 0 if incomplete. */
1235 static int command(struct conn *c, char *line) {
1239 D(("server command %s", line));
1240 /* We force everything into NFC as early as possible */
1241 if(!(line = utf8_compose_canon(line, strlen(line), 0))) {
1242 sink_writes(ev_writer_sink(c->w), "500 cannot normalize command\n");
1245 if(!(vec = split(line, &nvec, SPLIT_QUOTES, command_error, c))) {
1246 sink_writes(ev_writer_sink(c->w), "500 cannot parse command\n");
1250 sink_writes(ev_writer_sink(c->w), "500 do what?\n");
1253 if((n = TABLE_FIND(commands, struct command, name, vec[0])) < 0)
1254 sink_writes(ev_writer_sink(c->w), "500 unknown command\n");
1256 if(commands[n].rights
1257 && !(c->rights & commands[n].rights)) {
1258 sink_writes(ev_writer_sink(c->w), "530 Prohibited\n");
1263 if(nvec < commands[n].minargs) {
1264 sink_writes(ev_writer_sink(c->w), "500 missing argument(s)\n");
1267 if(nvec > commands[n].maxargs) {
1268 sink_writes(ev_writer_sink(c->w), "500 too many arguments\n");
1271 return commands[n].fn(c, vec, nvec);
1273 return 1; /* completed */
1276 /* redirect to the right reader callback for our current state */
1277 static int redirect_reader_callback(ev_source *ev,
1285 return c->reader(ev, reader, ptr, bytes, eof, u);
1288 /* the main command reader */
1289 static int reader_callback(ev_source attribute((unused)) *ev,
1299 D(("server reader_callback"));
1300 while((eol = memchr(ptr, '\n', bytes))) {
1302 ev_reader_consume(reader, eol - (char *)ptr);
1303 complete = command(c, ptr);
1304 bytes -= (eol - (char *)ptr);
1307 /* the command had better have set a new reader callback */
1309 /* there are further bytes to read, or we are at eof; arrange for the
1310 * command's reader callback to handle them */
1311 return ev_reader_incomplete(reader);
1312 /* nothing's going on right now */
1315 /* command completed, we can go around and handle the next one */
1319 error(0, "S%x unterminated line", c->tag);
1320 D(("normal reader close"));
1323 D(("close associated writer"));
1324 ev_writer_close(c->w);
1331 static int listen_callback(ev_source *ev,
1333 const struct sockaddr attribute((unused)) *remote,
1334 socklen_t attribute((unused)) rlen,
1336 const struct listener *l = u;
1337 struct conn *c = xmalloc(sizeof *c);
1338 static unsigned tags;
1340 D(("server listen_callback fd %d (%s)", fd, l->name));
1345 c->w = ev_writer_new(ev, fd, writer_error, c,
1347 c->r = ev_reader_new(ev, fd, redirect_reader_callback, reader_error, c,
1351 c->reader = reader_callback;
1354 gcry_randomize(c->nonce, sizeof c->nonce, GCRY_STRONG_RANDOM);
1355 sink_printf(ev_writer_sink(c->w), "231 %s %s\n",
1356 config->authorization_algorithm,
1357 hex(c->nonce, sizeof c->nonce));
1361 int server_start(ev_source *ev, int pf,
1362 size_t socklen, const struct sockaddr *sa,
1365 struct listener *l = xmalloc(sizeof *l);
1366 static const int one = 1;
1368 D(("server_init socket %s", name));
1369 fd = xsocket(pf, SOCK_STREAM, 0);
1370 xsetsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one);
1371 if(bind(fd, sa, socklen) < 0) {
1372 error(errno, "error binding to %s", name);
1380 if(ev_listen(ev, fd, listen_callback, l, "server listener"))
1385 int server_stop(ev_source *ev, int fd) {
1387 return ev_listen_cancel(ev, fd);