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"
72 # define NONCE_SIZE 16
75 int volume_left
, volume_right
; /* last known volume */
77 /** @brief Accept all well-formed login attempts
88 /** @brief One client connection */
90 /** @brief Read commands from here */
92 /** @brief Send responses to here */
94 /** @brief Underlying file descriptor */
96 /** @brief Unique identifier for connection used in log messages */
98 /** @brief Login name or NULL */
100 /** @brief Event loop */
102 /** @brief Nonce chosen for this connection */
103 unsigned char nonce
[NONCE_SIZE
];
104 /** @brief Current reader callback
106 * We change this depending on whether we're servicing the @b log command
108 ev_reader_callback
*reader
;
109 /** @brief Event log output sending to this connection */
110 struct eventlog_output
*lo
;
111 /** @brief Parent listener */
112 const struct listener
*l
;
113 /** @brief Login cookie or NULL */
115 /** @brief Connection rights */
119 static int reader_callback(ev_source
*ev
,
126 static const char *noyes
[] = { "no", "yes" };
128 /** @brief Called when a connection's writer fails or is shut down
130 * If the connection still has a raeder that is cancelled.
132 static int writer_error(ev_source
attribute((unused
)) *ev
,
137 D(("server writer_error S%x %d", c
->tag
, errno_value
));
138 if(errno_value
== 0) {
140 D(("S%x writer completed", c
->tag
));
142 if(errno_value
!= EPIPE
)
143 error(errno_value
, "S%x write error on socket", c
->tag
);
145 D(("cancel reader"));
146 ev_reader_cancel(c
->r
);
149 D(("done cancel reader"));
156 /** @brief Called when a conncetion's reader fails or is shut down
158 * If connection still has a writer then it is closed.
160 static int reader_error(ev_source
attribute((unused
)) *ev
,
165 D(("server reader_error S%x %d", c
->tag
, errno_value
));
166 error(errno_value
, "S%x read error on socket", c
->tag
);
168 ev_writer_close(c
->w
);
175 static int c_disable(struct conn
*c
, char **vec
, int nvec
) {
177 disable_playing(c
->who
);
178 else if(nvec
== 1 && !strcmp(vec
[0], "now"))
179 disable_playing(c
->who
);
181 sink_writes(ev_writer_sink(c
->w
), "550 invalid argument\n");
182 return 1; /* completed */
184 sink_writes(ev_writer_sink(c
->w
), "250 OK\n");
185 return 1; /* completed */
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 */
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 */
205 static int c_play(struct conn
*c
, char **vec
,
206 int attribute((unused
)) nvec
) {
208 struct queue_entry
*q
;
210 if(!trackdb_exists(vec
[0])) {
211 sink_writes(ev_writer_sink(c
->w
), "550 track is not in database\n");
214 if(!(track
= trackdb_resolve(vec
[0]))) {
215 sink_writes(ev_writer_sink(c
->w
), "550 cannot resolve track\n");
218 q
= queue_add(track
, c
->who
, WHERE_BEFORE_RANDOM
);
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
223 if(q
== qhead
.next
&& playing
)
225 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", q
->id
);
226 /* If the queue was empty but we are for some reason paused then
228 if(!playing
) resume_playing(0);
230 return 1; /* completed */
233 static int c_remove(struct conn
*c
, char **vec
,
234 int attribute((unused
)) nvec
) {
235 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");
241 if(!right_removable(c
->rights
, c
->who
, q
)) {
242 error(0, "%s attempted remove but lacks required rights", c
->who
);
243 sink_writes(ev_writer_sink(c
->w
),
244 "510 Not authorized to remove that track\n");
247 queue_remove(q
, c
->who
);
248 /* De-prepare the track. */
250 /* If we removed a random track then add another one. */
251 if(q
->state
== playing_random
)
253 /* Prepare whatever the next head track is. */
254 if(qhead
.next
!= &qhead
)
255 prepare(c
->ev
, qhead
.next
);
257 sink_writes(ev_writer_sink(c
->w
), "250 removed\n");
258 return 1; /* completed */
261 static int c_scratch(struct conn
*c
,
265 sink_writes(ev_writer_sink(c
->w
), "250 nothing is playing\n");
266 return 1; /* completed */
268 /* TODO there is a bug here: if we specify an ID but it's not the currently
269 * playing track then you will get 550 if you weren't authorized to scratch
270 * the currently playing track. */
271 if(!right_scratchable(c
->rights
, c
->who
, playing
)) {
272 error(0, "%s attempted scratch but lacks required rights", c
->who
);
273 sink_writes(ev_writer_sink(c
->w
),
274 "510 Not authorized to scratch that track\n");
277 scratch(c
->who
, nvec
== 1 ? vec
[0] : 0);
278 /* If you scratch an unpaused track then it is automatically unpaused */
280 sink_writes(ev_writer_sink(c
->w
), "250 scratched\n");
281 return 1; /* completed */
284 static int c_pause(struct conn
*c
,
285 char attribute((unused
)) **vec
,
286 int attribute((unused
)) nvec
) {
288 sink_writes(ev_writer_sink(c
->w
), "250 nothing is playing\n");
289 return 1; /* completed */
292 sink_writes(ev_writer_sink(c
->w
), "250 already paused\n");
293 return 1; /* completed */
295 if(pause_playing(c
->who
) < 0)
296 sink_writes(ev_writer_sink(c
->w
), "550 cannot pause this track\n");
298 sink_writes(ev_writer_sink(c
->w
), "250 paused\n");
302 static int c_resume(struct conn
*c
,
303 char attribute((unused
)) **vec
,
304 int attribute((unused
)) nvec
) {
306 sink_writes(ev_writer_sink(c
->w
), "250 not paused\n");
307 return 1; /* completed */
309 resume_playing(c
->who
);
310 sink_writes(ev_writer_sink(c
->w
), "250 paused\n");
314 static int c_shutdown(struct conn
*c
,
315 char attribute((unused
)) **vec
,
316 int attribute((unused
)) nvec
) {
317 info("S%x shut down by %s", c
->tag
, c
->who
);
318 sink_writes(ev_writer_sink(c
->w
), "250 shutting down\n");
319 ev_writer_flush(c
->w
);
323 static int c_reconfigure(struct conn
*c
,
324 char attribute((unused
)) **vec
,
325 int attribute((unused
)) nvec
) {
326 info("S%x reconfigure by %s", c
->tag
, c
->who
);
327 if(reconfigure(c
->ev
, 1))
328 sink_writes(ev_writer_sink(c
->w
), "550 error reading new config\n");
330 sink_writes(ev_writer_sink(c
->w
), "250 installed new config\n");
331 return 1; /* completed */
334 static int c_rescan(struct conn
*c
,
335 char attribute((unused
)) **vec
,
336 int attribute((unused
)) nvec
) {
337 info("S%x rescan by %s", c
->tag
, c
->who
);
338 trackdb_rescan(c
->ev
);
339 sink_writes(ev_writer_sink(c
->w
), "250 initiated rescan\n");
340 return 1; /* completed */
343 static int c_version(struct conn
*c
,
344 char attribute((unused
)) **vec
,
345 int attribute((unused
)) nvec
) {
346 /* VERSION had better only use the basic character set */
347 sink_printf(ev_writer_sink(c
->w
), "251 %s\n", disorder_short_version_string
);
348 return 1; /* completed */
351 static int c_playing(struct conn
*c
,
352 char attribute((unused
)) **vec
,
353 int attribute((unused
)) nvec
) {
355 queue_fix_sofar(playing
);
356 playing
->expected
= 0;
357 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", queue_marshall(playing
));
359 sink_printf(ev_writer_sink(c
->w
), "259 nothing playing\n");
360 return 1; /* completed */
363 static const char *connection_host(struct conn
*c
) {
366 struct sockaddr_in in
;
367 struct sockaddr_in6 in6
;
373 /* get connection data */
375 if(getpeername(c
->fd
, &u
.sa
, &l
) < 0) {
376 error(errno
, "S%x error calling getpeername", c
->tag
);
379 if(c
->l
->pf
!= PF_UNIX
) {
380 if((n
= getnameinfo(&u
.sa
, l
,
381 host
, sizeof host
, 0, 0, NI_NUMERICHOST
))) {
382 error(0, "S%x error calling getnameinfo: %s", c
->tag
, gai_strerror(n
));
385 return xstrdup(host
);
390 static int c_user(struct conn
*c
,
392 int attribute((unused
)) nvec
) {
394 const char *res
, *host
, *password
;
398 sink_writes(ev_writer_sink(c
->w
), "530 already authenticated\n");
401 /* get connection data */
402 if(!(host
= connection_host(c
))) {
403 sink_writes(ev_writer_sink(c
->w
), "530 authentication failure\n");
407 k
= trackdb_getuserinfo(vec
[0]);
408 /* reject nonexistent users */
410 error(0, "S%x unknown user '%s' from %s", c
->tag
, vec
[0], host
);
411 sink_writes(ev_writer_sink(c
->w
), "530 authentication failed\n");
414 /* reject unconfirmed users */
415 if(kvp_get(k
, "confirmation")) {
416 error(0, "S%x unconfirmed user '%s' from %s", c
->tag
, vec
[0], host
);
417 sink_writes(ev_writer_sink(c
->w
), "530 authentication failed\n");
420 password
= kvp_get(k
, "password");
421 if(!password
) password
= "";
422 if(parse_rights(kvp_get(k
, "rights"), &rights
, 1)) {
423 error(0, "error parsing rights for %s", vec
[0]);
424 sink_writes(ev_writer_sink(c
->w
), "530 authentication failed\n");
427 /* check whether the response is right */
428 res
= authhash(c
->nonce
, sizeof c
->nonce
, password
,
429 config
->authorization_algorithm
);
430 if(wideopen
|| (res
&& !strcmp(res
, vec
[1]))) {
433 /* currently we only bother logging remote connections */
434 if(strcmp(host
, "local")) {
435 info("S%x %s connected from %s", c
->tag
, vec
[0], host
);
436 c
->rights
|= RIGHT__LOCAL
;
438 sink_writes(ev_writer_sink(c
->w
), "230 OK\n");
441 /* oops, response was wrong */
442 info("S%x authentication failure for %s from %s", c
->tag
, vec
[0], host
);
443 sink_writes(ev_writer_sink(c
->w
), "530 authentication failed\n");
447 static int c_recent(struct conn
*c
,
448 char attribute((unused
)) **vec
,
449 int attribute((unused
)) nvec
) {
450 const struct queue_entry
*q
;
452 sink_writes(ev_writer_sink(c
->w
), "253 Tracks follow\n");
453 for(q
= phead
.next
; q
!= &phead
; q
= q
->next
)
454 sink_printf(ev_writer_sink(c
->w
), " %s\n", queue_marshall(q
));
455 sink_writes(ev_writer_sink(c
->w
), ".\n");
456 return 1; /* completed */
459 static int c_queue(struct conn
*c
,
460 char attribute((unused
)) **vec
,
461 int attribute((unused
)) nvec
) {
462 struct queue_entry
*q
;
467 sink_writes(ev_writer_sink(c
->w
), "253 Tracks follow\n");
468 if(playing_is_enabled() && !paused
) {
470 queue_fix_sofar(playing
);
471 if((l
= trackdb_get(playing
->track
, "_length"))
472 && (length
= atol(l
))) {
474 when
+= length
- playing
->sofar
+ config
->gap
;
477 /* Nothing is playing but playing is enabled, so whatever is
478 * first in the queue can be expected to start immediately. */
481 for(q
= qhead
.next
; q
!= &qhead
; q
= q
->next
) {
482 /* fill in estimated start time */
484 sink_printf(ev_writer_sink(c
->w
), " %s\n", queue_marshall(q
));
485 /* update for next track */
487 if((l
= trackdb_get(q
->track
, "_length"))
488 && (length
= atol(l
)))
489 when
+= length
+ config
->gap
;
494 sink_writes(ev_writer_sink(c
->w
), ".\n");
495 return 1; /* completed */
498 static int output_list(struct conn
*c
, char **vec
) {
500 sink_printf(ev_writer_sink(c
->w
), "%s\n", *vec
++);
501 sink_writes(ev_writer_sink(c
->w
), ".\n");
505 static int files_dirs(struct conn
*c
,
508 enum trackdb_listable what
) {
509 const char *dir
, *re
, *errstr
;
515 case 0: dir
= 0; re
= 0; break;
516 case 1: dir
= vec
[0]; re
= 0; break;
517 case 2: dir
= vec
[0]; re
= vec
[1]; break;
520 /* A bit of a bodge to make sure the args don't trample on cache keys */
521 if(dir
&& strchr(dir
, '\n')) {
522 sink_writes(ev_writer_sink(c
->w
), "550 invalid directory name\n");
525 if(re
&& strchr(re
, '\n')) {
526 sink_writes(ev_writer_sink(c
->w
), "550 invalid regexp\n");
529 /* We bother eliminating "" because the web interface is relatively
530 * likely to send it */
532 byte_xasprintf(&key
, "%d\n%s\n%s", (int)what
, dir ? dir
: "", re
);
533 fvec
= (char **)cache_get(&cache_files_type
, key
);
535 /* Got a cache hit, don't store the answer in the cache */
538 rec
= 0; /* quieten compiler */
540 /* Cache miss, we'll do the lookup and key != 0 so we'll store the answer
542 if(!(rec
= pcre_compile(re
, PCRE_CASELESS
|PCRE_UTF8
,
543 &errstr
, &erroffset
, 0))) {
544 sink_printf(ev_writer_sink(c
->w
), "550 Error compiling regexp: %s\n",
548 /* It only counts as a miss if the regexp was valid. */
549 ++cache_files_misses
;
552 /* No regexp, don't bother caching the result */
558 /* No cache hit (either because a miss, or because we did not look) so do
561 fvec
= trackdb_list(dir
, 0, what
, rec
);
563 fvec
= trackdb_list(0, 0, what
, rec
);
566 /* Put the answer in the cache */
567 cache_put(&cache_files_type
, key
, fvec
);
568 sink_writes(ev_writer_sink(c
->w
), "253 Listing follow\n");
569 return output_list(c
, fvec
);
572 static int c_files(struct conn
*c
,
575 return files_dirs(c
, vec
, nvec
, trackdb_files
);
578 static int c_dirs(struct conn
*c
,
581 return files_dirs(c
, vec
, nvec
, trackdb_directories
);
584 static int c_allfiles(struct conn
*c
,
587 return files_dirs(c
, vec
, nvec
, trackdb_directories
|trackdb_files
);
590 static int c_get(struct conn
*c
,
592 int attribute((unused
)) nvec
) {
595 if(vec
[1][0] != '_' && (v
= trackdb_get(vec
[0], vec
[1])))
596 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", quoteutf8(v
));
598 sink_writes(ev_writer_sink(c
->w
), "555 not found\n");
602 static int c_length(struct conn
*c
,
604 int attribute((unused
)) nvec
) {
605 const char *track
, *v
;
607 if(!(track
= trackdb_resolve(vec
[0]))) {
608 sink_writes(ev_writer_sink(c
->w
), "550 cannot resolve track\n");
611 if((v
= trackdb_get(track
, "_length")))
612 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", quoteutf8(v
));
614 sink_writes(ev_writer_sink(c
->w
), "550 not found\n");
618 static int c_set(struct conn
*c
,
620 int attribute((unused
)) nvec
) {
621 if(vec
[1][0] != '_' && !trackdb_set(vec
[0], vec
[1], vec
[2]))
622 sink_writes(ev_writer_sink(c
->w
), "250 OK\n");
624 sink_writes(ev_writer_sink(c
->w
), "550 not found\n");
628 static int c_prefs(struct conn
*c
,
630 int attribute((unused
)) nvec
) {
633 k
= trackdb_get_all(vec
[0]);
634 sink_writes(ev_writer_sink(c
->w
), "253 prefs follow\n");
635 for(; k
; k
= k
->next
)
636 if(k
->name
[0] != '_') /* omit internal values */
637 sink_printf(ev_writer_sink(c
->w
),
638 " %s %s\n", quoteutf8(k
->name
), quoteutf8(k
->value
));
639 sink_writes(ev_writer_sink(c
->w
), ".\n");
643 static int c_exists(struct conn
*c
,
645 int attribute((unused
)) nvec
) {
646 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", noyes
[trackdb_exists(vec
[0])]);
650 static void search_parse_error(const char *msg
, void *u
) {
651 *(const char **)u
= msg
;
654 static int c_search(struct conn
*c
,
656 int attribute((unused
)) nvec
) {
657 char **terms
, **results
;
658 int nterms
, nresults
, n
;
659 const char *e
= "unknown error";
661 /* This is a bit of a bodge. Initially it's there to make the eclient
662 * interface a bit more convenient to add searching to, but it has the more
663 * compelling advantage that if everything uses it, then interpretation of
664 * user-supplied search strings will be the same everywhere. */
665 if(!(terms
= split(vec
[0], &nterms
, SPLIT_QUOTES
, search_parse_error
, &e
))) {
666 sink_printf(ev_writer_sink(c
->w
), "550 %s\n", e
);
668 results
= trackdb_search(terms
, nterms
, &nresults
);
669 sink_printf(ev_writer_sink(c
->w
), "253 %d matches\n", nresults
);
670 for(n
= 0; n
< nresults
; ++n
)
671 sink_printf(ev_writer_sink(c
->w
), "%s\n", results
[n
]);
672 sink_writes(ev_writer_sink(c
->w
), ".\n");
677 static int c_random_enable(struct conn
*c
,
678 char attribute((unused
)) **vec
,
679 int attribute((unused
)) nvec
) {
680 enable_random(c
->who
, c
->ev
);
681 /* Enable implicitly unpauses if there is nothing playing */
682 if(paused
&& !playing
) resume_playing(c
->who
);
683 sink_writes(ev_writer_sink(c
->w
), "250 OK\n");
684 return 1; /* completed */
687 static int c_random_disable(struct conn
*c
,
688 char attribute((unused
)) **vec
,
689 int attribute((unused
)) nvec
) {
690 disable_random(c
->who
);
691 sink_writes(ev_writer_sink(c
->w
), "250 OK\n");
692 return 1; /* completed */
695 static int c_random_enabled(struct conn
*c
,
696 char attribute((unused
)) **vec
,
697 int attribute((unused
)) nvec
) {
698 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", noyes
[random_is_enabled()]);
699 return 1; /* completed */
702 static void got_stats(char *stats
, void *u
) {
703 struct conn
*const c
= u
;
705 sink_printf(ev_writer_sink(c
->w
), "253 stats\n%s\n.\n", stats
);
706 /* Now we can start processing commands again */
707 ev_reader_enable(c
->r
);
710 static int c_stats(struct conn
*c
,
711 char attribute((unused
)) **vec
,
712 int attribute((unused
)) nvec
) {
713 trackdb_stats_subprocess(c
->ev
, got_stats
, c
);
714 return 0; /* not yet complete */
717 static int c_volume(struct conn
*c
,
729 l
= r
= atoi(vec
[0]);
740 rights
= set ? RIGHT_VOLUME
: RIGHT_READ
;
741 if(!(c
->rights
& rights
)) {
742 error(0, "%s attempted to set volume but lacks required rights", c
->who
);
743 sink_writes(ev_writer_sink(c
->w
), "510 Prohibited\n");
746 if(mixer_control(&l
, &r
, set
))
747 sink_writes(ev_writer_sink(c
->w
), "550 error accessing mixer\n");
749 sink_printf(ev_writer_sink(c
->w
), "252 %d %d\n", l
, r
);
750 if(l
!= volume_left
|| r
!= volume_right
) {
753 snprintf(lb
, sizeof lb
, "%d", l
);
754 snprintf(rb
, sizeof rb
, "%d", r
);
755 eventlog("volume", lb
, rb
, (char *)0);
761 /** @brief Called when data arrives on a log connection
763 * We just discard all such data. The client may occasionally send data as a
766 static int logging_reader_callback(ev_source
attribute((unused
)) *ev
,
768 void attribute((unused
)) *ptr
,
770 int attribute((unused
)) eof
,
771 void attribute((unused
)) *u
) {
774 ev_reader_consume(reader
, bytes
);
776 /* Oops, that's all for now */
777 D(("logging reader eof"));
780 ev_writer_close(c
->w
);
788 static void logclient(const char *msg
, void *user
) {
789 struct conn
*c
= user
;
792 /* This connection has gone up in smoke for some reason */
793 eventlog_remove(c
->lo
);
796 sink_printf(ev_writer_sink(c
->w
), "%"PRIxMAX
" %s\n",
797 (uintmax_t)time(0), msg
);
800 static int c_log(struct conn
*c
,
801 char attribute((unused
)) **vec
,
802 int attribute((unused
)) nvec
) {
805 sink_writes(ev_writer_sink(c
->w
), "254 OK\n");
806 /* pump out initial state */
808 sink_printf(ev_writer_sink(c
->w
), "%"PRIxMAX
" state %s\n",
810 playing_is_enabled() ?
"enable_play" : "disable_play");
811 sink_printf(ev_writer_sink(c
->w
), "%"PRIxMAX
" state %s\n",
813 random_is_enabled() ?
"enable_random" : "disable_random");
814 sink_printf(ev_writer_sink(c
->w
), "%"PRIxMAX
" state %s\n",
816 paused ?
"pause" : "resume");
818 sink_printf(ev_writer_sink(c
->w
), "%"PRIxMAX
" state playing\n",
821 sink_printf(ev_writer_sink(c
->w
), "%"PRIxMAX
" volume %d %d\n",
822 (uintmax_t)now
, volume_left
, volume_right
);
823 c
->lo
= xmalloc(sizeof *c
->lo
);
824 c
->lo
->fn
= logclient
;
827 c
->reader
= logging_reader_callback
;
831 /** @brief Test whether a move is allowed
832 * @param c Connection
833 * @param qs List of IDs on queue
834 * @param nqs Number of IDs
835 * @return 0 if move is prohibited, non-0 if it is allowed
837 static int has_move_rights(struct conn
*c
, struct queue_entry
**qs
, int nqs
) {
838 for(; nqs
> 0; ++qs
, --nqs
) {
839 struct queue_entry
*const q
= *qs
;
841 if(!right_movable(c
->rights
, c
->who
, q
))
847 static int c_move(struct conn
*c
,
849 int attribute((unused
)) nvec
) {
850 struct queue_entry
*q
;
853 if(!(q
= queue_find(vec
[0]))) {
854 sink_writes(ev_writer_sink(c
->w
), "550 no such track on the queue\n");
857 if(!has_move_rights(c
, &q
, 1)) {
858 error(0, "%s attempted move but lacks required rights", c
->who
);
859 sink_writes(ev_writer_sink(c
->w
),
860 "510 Not authorized to move that track\n");
863 n
= queue_move(q
, atoi(vec
[1]), c
->who
);
864 sink_printf(ev_writer_sink(c
->w
), "252 %d\n", n
);
865 /* If we've moved to the head of the queue then prepare the track. */
871 static int c_moveafter(struct conn
*c
,
873 int attribute((unused
)) nvec
) {
874 struct queue_entry
*q
, **qs
;
878 if(!(q
= queue_find(vec
[0]))) {
879 sink_writes(ev_writer_sink(c
->w
), "550 no such track on the queue\n");
886 qs
= xcalloc(nvec
, sizeof *qs
);
887 for(n
= 0; n
< nvec
; ++n
)
888 if(!(qs
[n
] = queue_find(vec
[n
]))) {
889 sink_writes(ev_writer_sink(c
->w
), "550 no such track on the queue\n");
892 if(!has_move_rights(c
, qs
, nvec
)) {
893 error(0, "%s attempted moveafter but lacks required rights", c
->who
);
894 sink_writes(ev_writer_sink(c
->w
),
895 "510 Not authorized to move those tracks\n");
898 queue_moveafter(q
, nvec
, qs
, c
->who
);
899 sink_printf(ev_writer_sink(c
->w
), "250 Moved tracks\n");
900 /* If we've moved to the head of the queue then prepare the track. */
906 static int c_part(struct conn
*c
,
908 int attribute((unused
)) nvec
) {
909 sink_printf(ev_writer_sink(c
->w
), "252 %s\n",
910 quoteutf8(trackdb_getpart(vec
[0], vec
[1], vec
[2])));
914 static int c_resolve(struct conn
*c
,
916 int attribute((unused
)) nvec
) {
919 if(!(track
= trackdb_resolve(vec
[0]))) {
920 sink_writes(ev_writer_sink(c
->w
), "550 cannot resolve track\n");
923 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", quoteutf8(track
));
927 static int c_tags(struct conn
*c
,
928 char attribute((unused
)) **vec
,
929 int attribute((unused
)) nvec
) {
930 char **tags
= trackdb_alltags();
932 sink_printf(ev_writer_sink(c
->w
), "253 Tag list follows\n");
934 sink_printf(ev_writer_sink(c
->w
), "%s%s\n",
935 **tags
== '.' ?
"." : "", *tags
);
938 sink_writes(ev_writer_sink(c
->w
), ".\n");
939 return 1; /* completed */
942 static int c_set_global(struct conn
*c
,
944 int attribute((unused
)) nvec
) {
945 if(vec
[0][0] == '_') {
946 sink_writes(ev_writer_sink(c
->w
), "550 cannot set internal global preferences\n");
949 trackdb_set_global(vec
[0], vec
[1], c
->who
);
950 sink_printf(ev_writer_sink(c
->w
), "250 OK\n");
954 static int c_get_global(struct conn
*c
,
956 int attribute((unused
)) nvec
) {
957 const char *s
= trackdb_get_global(vec
[0]);
960 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", quoteutf8(s
));
962 sink_writes(ev_writer_sink(c
->w
), "555 not found\n");
966 static int c_nop(struct conn
*c
,
967 char attribute((unused
)) **vec
,
968 int attribute((unused
)) nvec
) {
969 sink_printf(ev_writer_sink(c
->w
), "250 Quack\n");
973 static int c_new(struct conn
*c
,
976 char **tracks
= trackdb_new(0, nvec
> 0 ?
atoi(vec
[0]) : INT_MAX
);
978 sink_printf(ev_writer_sink(c
->w
), "253 New track list follows\n");
980 sink_printf(ev_writer_sink(c
->w
), "%s%s\n",
981 **tracks
== '.' ?
"." : "", *tracks
);
984 sink_writes(ev_writer_sink(c
->w
), ".\n");
985 return 1; /* completed */
989 static int c_rtp_address(struct conn
*c
,
990 char attribute((unused
)) **vec
,
991 int attribute((unused
)) nvec
) {
992 if(config
->api
== BACKEND_NETWORK
) {
993 sink_printf(ev_writer_sink(c
->w
), "252 %s %s\n",
994 quoteutf8(config
->broadcast
.s
[0]),
995 quoteutf8(config
->broadcast
.s
[1]));
997 sink_writes(ev_writer_sink(c
->w
), "550 No RTP\n");
1001 static int c_cookie(struct conn
*c
,
1003 int attribute((unused
)) nvec
) {
1008 /* Can't log in twice on the same connection */
1010 sink_writes(ev_writer_sink(c
->w
), "530 already authenticated\n");
1013 /* Get some kind of peer identifcation */
1014 if(!(host
= connection_host(c
))) {
1015 sink_writes(ev_writer_sink(c
->w
), "530 authentication failure\n");
1018 /* Check the cookie */
1019 user
= verify_cookie(vec
[0], &rights
);
1021 sink_writes(ev_writer_sink(c
->w
), "530 authentication failure\n");
1028 if(strcmp(host
, "local")) {
1029 info("S%x %s connected with cookie from %s", c
->tag
, user
, host
);
1030 c
->rights
|= RIGHT__LOCAL
;
1032 /* Response contains username so client knows who they are acting as */
1033 sink_printf(ev_writer_sink(c
->w
), "232 %s\n", quoteutf8(user
));
1037 static int c_make_cookie(struct conn
*c
,
1038 char attribute((unused
)) **vec
,
1039 int attribute((unused
)) nvec
) {
1040 const char *cookie
= make_cookie(c
->who
);
1043 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", quoteutf8(cookie
));
1045 sink_writes(ev_writer_sink(c
->w
), "550 Cannot create cookie\n");
1049 static int c_revoke(struct conn
*c
,
1050 char attribute((unused
)) **vec
,
1051 int attribute((unused
)) nvec
) {
1053 revoke_cookie(c
->cookie
);
1054 sink_writes(ev_writer_sink(c
->w
), "250 OK\n");
1056 sink_writes(ev_writer_sink(c
->w
), "550 Did not log in with cookie\n");
1060 static int c_adduser(struct conn
*c
,
1067 if(parse_rights(vec
[2], 0, 1)) {
1068 sink_writes(ev_writer_sink(c
->w
), "550 Invalid rights list\n");
1072 rights
= config
->default_rights
;
1073 if(trackdb_adduser(vec
[0], vec
[1], rights
,
1074 0/*email*/, 0/*confirmation*/))
1075 sink_writes(ev_writer_sink(c
->w
), "550 Cannot create user\n");
1077 sink_writes(ev_writer_sink(c
->w
), "250 User created\n");
1081 static int c_deluser(struct conn
*c
,
1083 int attribute((unused
)) nvec
) {
1084 if(trackdb_deluser(vec
[0]))
1085 sink_writes(ev_writer_sink(c
->w
), "550 Cannot delete user\n");
1087 sink_writes(ev_writer_sink(c
->w
), "250 User deleted\n");
1091 static int c_edituser(struct conn
*c
,
1093 int attribute((unused
)) nvec
) {
1094 /* RIGHT_ADMIN can do anything; otherwise you can only set your own email
1095 * address and password. */
1096 if((c
->rights
& RIGHT_ADMIN
)
1097 || (!strcmp(c
->who
, vec
[0])
1098 && (!strcmp(vec
[1], "email")
1099 || !strcmp(vec
[1], "password")))) {
1100 if(trackdb_edituserinfo(vec
[0], vec
[1], vec
[2]))
1101 sink_writes(ev_writer_sink(c
->w
), "550 Failed to change setting\n");
1103 sink_writes(ev_writer_sink(c
->w
), "250 OK\n");
1105 error(0, "%s attempted edituser but lacks required rights", c
->who
);
1106 sink_writes(ev_writer_sink(c
->w
), "510 Restricted to administrators\n");
1111 static int c_userinfo(struct conn
*c
,
1112 char attribute((unused
)) **vec
,
1113 int attribute((unused
)) nvec
) {
1117 /* RIGHT_ADMIN allows anything; otherwise you can only get your own email
1118 * address and righst list. */
1119 if((c
->rights
& RIGHT_ADMIN
)
1120 || (!strcmp(c
->who
, vec
[0])
1121 && (!strcmp(vec
[1], "email")
1122 || !strcmp(vec
[1], "rights")))) {
1123 if((k
= trackdb_getuserinfo(vec
[0])))
1124 if((value
= kvp_get(k
, vec
[1])))
1125 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", quoteutf8(value
));
1127 sink_writes(ev_writer_sink(c
->w
), "555 Not set\n");
1129 sink_writes(ev_writer_sink(c
->w
), "550 No such user\n");
1131 error(0, "%s attempted userinfo but lacks required rights", c
->who
);
1132 sink_writes(ev_writer_sink(c
->w
), "510 Restricted to administrators\n");
1137 static int c_users(struct conn
*c
,
1138 char attribute((unused
)) **vec
,
1139 int attribute((unused
)) nvec
) {
1140 /* TODO de-dupe with c_tags */
1141 char **users
= trackdb_listusers();
1143 sink_writes(ev_writer_sink(c
->w
), "253 User list follows\n");
1145 sink_printf(ev_writer_sink(c
->w
), "%s%s\n",
1146 **users
== '.' ?
"." : "", *users
);
1149 sink_writes(ev_writer_sink(c
->w
), ".\n");
1150 return 1; /* completed */
1153 static int c_register(struct conn
*c
,
1155 int attribute((unused
)) nvec
) {
1160 /* The confirmation string is base64(username;nonce) */
1161 bufsize
= strlen(vec
[0]) + NONCE_SIZE
+ 2;
1162 buf
= xmalloc_noptr(bufsize
);
1163 offset
= byte_snprintf(buf
, bufsize
, "%s;", vec
[0]);
1164 gcry_randomize(buf
+ offset
, NONCE_SIZE
, GCRY_STRONG_RANDOM
);
1165 cs
= mime_to_base64((uint8_t *)buf
, offset
+ NONCE_SIZE
);
1166 if(trackdb_adduser(vec
[0], vec
[1], config
->default_rights
, vec
[2], cs
))
1167 sink_writes(ev_writer_sink(c
->w
), "550 Cannot create user\n");
1169 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", quoteutf8(cs
));
1173 static int c_confirm(struct conn
*c
,
1175 int attribute((unused
)) nvec
) {
1179 if(!(user
= mime_base64(vec
[0], &nuser
))
1180 || !(sep
= memchr(user
, ';', nuser
))) {
1181 sink_writes(ev_writer_sink(c
->w
), "550 Malformed confirmation string\n");
1185 if(trackdb_confirm(user
, vec
[0]))
1186 sink_writes(ev_writer_sink(c
->w
), "550 Incorrect confirmation string\n");
1188 sink_writes(ev_writer_sink(c
->w
), "250 OK\n");
1192 static const struct command
{
1193 /** @brief Command name */
1196 /** @brief Minimum number of arguments */
1199 /** @brief Maximum number of arguments */
1202 /** @brief Function to process command */
1203 int (*fn
)(struct conn
*, char **, int);
1205 /** @brief Rights required to execute command
1207 * 0 means that the command can be issued without logging in. If multiple
1208 * bits are listed here any of those rights will do.
1212 { "adduser", 2, 3, c_adduser
, RIGHT_ADMIN
|RIGHT__LOCAL
},
1213 { "allfiles", 0, 2, c_allfiles
, RIGHT_READ
},
1214 { "confirm", 1, 1, c_confirm
, 0 },
1215 { "cookie", 1, 1, c_cookie
, 0 },
1216 { "deluser", 1, 1, c_deluser
, RIGHT_ADMIN
|RIGHT__LOCAL
},
1217 { "dirs", 0, 2, c_dirs
, RIGHT_READ
},
1218 { "disable", 0, 1, c_disable
, RIGHT_GLOBAL_PREFS
},
1219 { "edituser", 3, 3, c_edituser
, RIGHT_ADMIN
|RIGHT_USERINFO
},
1220 { "enable", 0, 0, c_enable
, RIGHT_GLOBAL_PREFS
},
1221 { "enabled", 0, 0, c_enabled
, RIGHT_READ
},
1222 { "exists", 1, 1, c_exists
, RIGHT_READ
},
1223 { "files", 0, 2, c_files
, RIGHT_READ
},
1224 { "get", 2, 2, c_get
, RIGHT_READ
},
1225 { "get-global", 1, 1, c_get_global
, RIGHT_READ
},
1226 { "length", 1, 1, c_length
, RIGHT_READ
},
1227 { "log", 0, 0, c_log
, RIGHT_READ
},
1228 { "make-cookie", 0, 0, c_make_cookie
, RIGHT_READ
},
1229 { "move", 2, 2, c_move
, RIGHT_MOVE__MASK
},
1230 { "moveafter", 1, INT_MAX
, c_moveafter
, RIGHT_MOVE__MASK
},
1231 { "new", 0, 1, c_new
, RIGHT_READ
},
1232 { "nop", 0, 0, c_nop
, 0 },
1233 { "part", 3, 3, c_part
, RIGHT_READ
},
1234 { "pause", 0, 0, c_pause
, RIGHT_PAUSE
},
1235 { "play", 1, 1, c_play
, RIGHT_PLAY
},
1236 { "playing", 0, 0, c_playing
, RIGHT_READ
},
1237 { "prefs", 1, 1, c_prefs
, RIGHT_READ
},
1238 { "queue", 0, 0, c_queue
, RIGHT_READ
},
1239 { "random-disable", 0, 0, c_random_disable
, RIGHT_GLOBAL_PREFS
},
1240 { "random-enable", 0, 0, c_random_enable
, RIGHT_GLOBAL_PREFS
},
1241 { "random-enabled", 0, 0, c_random_enabled
, RIGHT_READ
},
1242 { "recent", 0, 0, c_recent
, RIGHT_READ
},
1243 { "reconfigure", 0, 0, c_reconfigure
, RIGHT_ADMIN
},
1244 { "register", 3, 3, c_register
, RIGHT_REGISTER
|RIGHT__LOCAL
},
1245 { "remove", 1, 1, c_remove
, RIGHT_REMOVE__MASK
},
1246 { "rescan", 0, 0, c_rescan
, RIGHT_RESCAN
},
1247 { "resolve", 1, 1, c_resolve
, RIGHT_READ
},
1248 { "resume", 0, 0, c_resume
, RIGHT_PAUSE
},
1249 { "revoke", 0, 0, c_revoke
, RIGHT_READ
},
1250 { "rtp-address", 0, 0, c_rtp_address
, 0 },
1251 { "scratch", 0, 1, c_scratch
, RIGHT_SCRATCH__MASK
},
1252 { "search", 1, 1, c_search
, RIGHT_READ
},
1253 { "set", 3, 3, c_set
, RIGHT_PREFS
, },
1254 { "set-global", 2, 2, c_set_global
, RIGHT_GLOBAL_PREFS
},
1255 { "shutdown", 0, 0, c_shutdown
, RIGHT_ADMIN
},
1256 { "stats", 0, 0, c_stats
, RIGHT_READ
},
1257 { "tags", 0, 0, c_tags
, RIGHT_READ
},
1258 { "unset", 2, 2, c_set
, RIGHT_PREFS
},
1259 { "unset-global", 1, 1, c_set_global
, RIGHT_GLOBAL_PREFS
},
1260 { "user", 2, 2, c_user
, 0 },
1261 { "userinfo", 2, 2, c_userinfo
, RIGHT_READ
},
1262 { "users", 0, 0, c_users
, RIGHT_READ
},
1263 { "version", 0, 0, c_version
, RIGHT_READ
},
1264 { "volume", 0, 2, c_volume
, RIGHT_READ
|RIGHT_VOLUME
}
1267 static void command_error(const char *msg
, void *u
) {
1270 sink_printf(ev_writer_sink(c
->w
), "500 parse error: %s\n", msg
);
1273 /* process a command. Return 1 if complete, 0 if incomplete. */
1274 static int command(struct conn
*c
, char *line
) {
1278 D(("server command %s", line
));
1279 /* We force everything into NFC as early as possible */
1280 if(!(line
= utf8_compose_canon(line
, strlen(line
), 0))) {
1281 sink_writes(ev_writer_sink(c
->w
), "500 cannot normalize command\n");
1284 if(!(vec
= split(line
, &nvec
, SPLIT_QUOTES
, command_error
, c
))) {
1285 sink_writes(ev_writer_sink(c
->w
), "500 cannot parse command\n");
1289 sink_writes(ev_writer_sink(c
->w
), "500 do what?\n");
1292 if((n
= TABLE_FIND(commands
, struct command
, name
, vec
[0])) < 0)
1293 sink_writes(ev_writer_sink(c
->w
), "500 unknown command\n");
1295 if(commands
[n
].rights
1296 && !(c
->rights
& commands
[n
].rights
)) {
1297 error(0, "%s attempted %s but lacks required rights", c
->who ? c
->who
: "NULL",
1299 sink_writes(ev_writer_sink(c
->w
), "510 Prohibited\n");
1304 if(nvec
< commands
[n
].minargs
) {
1305 sink_writes(ev_writer_sink(c
->w
), "500 missing argument(s)\n");
1308 if(nvec
> commands
[n
].maxargs
) {
1309 sink_writes(ev_writer_sink(c
->w
), "500 too many arguments\n");
1312 return commands
[n
].fn(c
, vec
, nvec
);
1314 return 1; /* completed */
1317 /* redirect to the right reader callback for our current state */
1318 static int redirect_reader_callback(ev_source
*ev
,
1326 return c
->reader(ev
, reader
, ptr
, bytes
, eof
, u
);
1329 /* the main command reader */
1330 static int reader_callback(ev_source
attribute((unused
)) *ev
,
1340 D(("server reader_callback"));
1341 while((eol
= memchr(ptr
, '\n', bytes
))) {
1343 ev_reader_consume(reader
, eol
- (char *)ptr
);
1344 complete
= command(c
, ptr
);
1345 bytes
-= (eol
- (char *)ptr
);
1348 /* the command had better have set a new reader callback */
1350 /* there are further bytes to read, or we are at eof; arrange for the
1351 * command's reader callback to handle them */
1352 return ev_reader_incomplete(reader
);
1353 /* nothing's going on right now */
1356 /* command completed, we can go around and handle the next one */
1360 error(0, "S%x unterminated line", c
->tag
);
1361 D(("normal reader close"));
1364 D(("close associated writer"));
1365 ev_writer_close(c
->w
);
1372 static int listen_callback(ev_source
*ev
,
1374 const struct sockaddr
attribute((unused
)) *remote
,
1375 socklen_t
attribute((unused
)) rlen
,
1377 const struct listener
*l
= u
;
1378 struct conn
*c
= xmalloc(sizeof *c
);
1379 static unsigned tags
;
1381 D(("server listen_callback fd %d (%s)", fd
, l
->name
));
1386 c
->w
= ev_writer_new(ev
, fd
, writer_error
, c
,
1388 c
->r
= ev_reader_new(ev
, fd
, redirect_reader_callback
, reader_error
, c
,
1392 c
->reader
= reader_callback
;
1395 gcry_randomize(c
->nonce
, sizeof c
->nonce
, GCRY_STRONG_RANDOM
);
1396 sink_printf(ev_writer_sink(c
->w
), "231 %d %s %s\n",
1398 config
->authorization_algorithm
,
1399 hex(c
->nonce
, sizeof c
->nonce
));
1403 int server_start(ev_source
*ev
, int pf
,
1404 size_t socklen
, const struct sockaddr
*sa
,
1407 struct listener
*l
= xmalloc(sizeof *l
);
1408 static const int one
= 1;
1410 D(("server_init socket %s", name
));
1411 fd
= xsocket(pf
, SOCK_STREAM
, 0);
1412 xsetsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &one
, sizeof one
);
1413 if(bind(fd
, sa
, socklen
) < 0) {
1414 error(errno
, "error binding to %s", name
);
1422 if(ev_listen(ev
, fd
, listen_callback
, l
, "server listener"))
1427 int server_stop(ev_source
*ev
, int fd
) {
1429 return ev_listen_cancel(ev
, fd
);