2 * This file is part of DisOrder.
3 * Copyright (C) 2004-2008 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
21 #include "disorder-server.h"
24 # define NONCE_SIZE 16
28 # define CONFIRM_SIZE 10
31 int volume_left
, volume_right
; /* last known volume */
33 /** @brief Accept all well-formed login attempts
44 /** @brief One client connection */
46 /** @brief Read commands from here */
48 /** @brief Send responses to here */
50 /** @brief Underlying file descriptor */
52 /** @brief Unique identifier for connection used in log messages */
54 /** @brief Login name or NULL */
56 /** @brief Event loop */
58 /** @brief Nonce chosen for this connection */
59 unsigned char nonce
[NONCE_SIZE
];
60 /** @brief Current reader callback
62 * We change this depending on whether we're servicing the @b log command
64 ev_reader_callback
*reader
;
65 /** @brief Event log output sending to this connection */
66 struct eventlog_output
*lo
;
67 /** @brief Parent listener */
68 const struct listener
*l
;
69 /** @brief Login cookie or NULL */
71 /** @brief Connection rights */
73 /** @brief Next connection */
75 /** @brief True if pending rescan had 'wait' set */
79 /** @brief Linked list of connections */
80 static struct conn
*connections
;
82 static int reader_callback(ev_source
*ev
,
89 static const char *noyes
[] = { "no", "yes" };
91 /** @brief Remove a connection from the connection list */
92 static void remove_connection(struct conn
*c
) {
95 for(cc
= &connections
; *cc
&& *cc
!= c
; cc
= &(*cc
)->next
)
101 /** @brief Called when a connection's writer fails or is shut down
103 * If the connection still has a raeder that is cancelled.
105 static int writer_error(ev_source
attribute((unused
)) *ev
,
110 D(("server writer_error S%x %d", c
->tag
, errno_value
));
111 if(errno_value
== 0) {
113 D(("S%x writer completed", c
->tag
));
115 if(errno_value
!= EPIPE
)
116 error(errno_value
, "S%x write error on socket", c
->tag
);
118 D(("cancel reader"));
119 ev_reader_cancel(c
->r
);
122 D(("done cancel reader"));
126 remove_connection(c
);
130 /** @brief Called when a conncetion's reader fails or is shut down
132 * If connection still has a writer then it is closed.
134 static int reader_error(ev_source
attribute((unused
)) *ev
,
139 D(("server reader_error S%x %d", c
->tag
, errno_value
));
140 error(errno_value
, "S%x read error on socket", c
->tag
);
142 ev_writer_close(c
->w
);
146 remove_connection(c
);
150 static int c_disable(struct conn
*c
, char **vec
, int nvec
) {
152 disable_playing(c
->who
);
153 else if(nvec
== 1 && !strcmp(vec
[0], "now"))
154 disable_playing(c
->who
);
156 sink_writes(ev_writer_sink(c
->w
), "550 invalid argument\n");
157 return 1; /* completed */
159 sink_writes(ev_writer_sink(c
->w
), "250 OK\n");
160 return 1; /* completed */
163 static int c_enable(struct conn
*c
,
164 char attribute((unused
)) **vec
,
165 int attribute((unused
)) nvec
) {
166 enable_playing(c
->who
, c
->ev
);
167 /* Enable implicitly unpauses if there is nothing playing */
168 if(paused
&& !playing
) resume_playing(c
->who
);
169 sink_writes(ev_writer_sink(c
->w
), "250 OK\n");
170 return 1; /* completed */
173 static int c_enabled(struct conn
*c
,
174 char attribute((unused
)) **vec
,
175 int attribute((unused
)) nvec
) {
176 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", noyes
[playing_is_enabled()]);
177 return 1; /* completed */
180 static int c_play(struct conn
*c
, char **vec
,
181 int attribute((unused
)) nvec
) {
183 struct queue_entry
*q
;
185 if(!trackdb_exists(vec
[0])) {
186 sink_writes(ev_writer_sink(c
->w
), "550 track is not in database\n");
189 if(!(track
= trackdb_resolve(vec
[0]))) {
190 sink_writes(ev_writer_sink(c
->w
), "550 cannot resolve track\n");
193 q
= queue_add(track
, c
->who
, WHERE_BEFORE_RANDOM
);
195 /* If we added the first track, and something is playing, then prepare the
196 * new track. If nothing is playing then we don't bother as it wouldn't gain
198 if(q
== qhead
.next
&& playing
)
200 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", q
->id
);
201 /* If the queue was empty but we are for some reason paused then
203 if(!playing
) resume_playing(0);
205 return 1; /* completed */
208 static int c_remove(struct conn
*c
, char **vec
,
209 int attribute((unused
)) nvec
) {
210 struct queue_entry
*q
;
212 if(!(q
= queue_find(vec
[0]))) {
213 sink_writes(ev_writer_sink(c
->w
), "550 no such track on the queue\n");
216 if(!right_removable(c
->rights
, c
->who
, q
)) {
217 error(0, "%s attempted remove but lacks required rights", c
->who
);
218 sink_writes(ev_writer_sink(c
->w
),
219 "510 Not authorized to remove that track\n");
222 queue_remove(q
, c
->who
);
223 /* De-prepare the track. */
225 /* See about adding a new random track */
226 add_random_track(c
->ev
);
227 /* Prepare whatever the next head track is. */
228 if(qhead
.next
!= &qhead
)
229 prepare(c
->ev
, qhead
.next
);
231 sink_writes(ev_writer_sink(c
->w
), "250 removed\n");
232 return 1; /* completed */
235 static int c_scratch(struct conn
*c
,
239 sink_writes(ev_writer_sink(c
->w
), "250 nothing is playing\n");
240 return 1; /* completed */
242 /* TODO there is a bug here: if we specify an ID but it's not the currently
243 * playing track then you will get 550 if you weren't authorized to scratch
244 * the currently playing track. */
245 if(!right_scratchable(c
->rights
, c
->who
, playing
)) {
246 error(0, "%s attempted scratch but lacks required rights", c
->who
);
247 sink_writes(ev_writer_sink(c
->w
),
248 "510 Not authorized to scratch that track\n");
251 scratch(c
->who
, nvec
== 1 ? vec
[0] : 0);
252 /* If you scratch an unpaused track then it is automatically unpaused */
254 sink_writes(ev_writer_sink(c
->w
), "250 scratched\n");
255 return 1; /* completed */
258 static int c_pause(struct conn
*c
,
259 char attribute((unused
)) **vec
,
260 int attribute((unused
)) nvec
) {
262 sink_writes(ev_writer_sink(c
->w
), "250 nothing is playing\n");
263 return 1; /* completed */
266 sink_writes(ev_writer_sink(c
->w
), "250 already paused\n");
267 return 1; /* completed */
269 if(pause_playing(c
->who
) < 0)
270 sink_writes(ev_writer_sink(c
->w
), "550 cannot pause this track\n");
272 sink_writes(ev_writer_sink(c
->w
), "250 paused\n");
276 static int c_resume(struct conn
*c
,
277 char attribute((unused
)) **vec
,
278 int attribute((unused
)) nvec
) {
280 sink_writes(ev_writer_sink(c
->w
), "250 not paused\n");
281 return 1; /* completed */
283 resume_playing(c
->who
);
284 sink_writes(ev_writer_sink(c
->w
), "250 paused\n");
288 static int c_shutdown(struct conn
*c
,
289 char attribute((unused
)) **vec
,
290 int attribute((unused
)) nvec
) {
291 info("S%x shut down by %s", c
->tag
, c
->who
);
292 sink_writes(ev_writer_sink(c
->w
), "250 shutting down\n");
293 ev_writer_flush(c
->w
);
297 static int c_reconfigure(struct conn
*c
,
298 char attribute((unused
)) **vec
,
299 int attribute((unused
)) nvec
) {
300 info("S%x reconfigure by %s", c
->tag
, c
->who
);
301 if(reconfigure(c
->ev
, 1))
302 sink_writes(ev_writer_sink(c
->w
), "550 error reading new config\n");
304 sink_writes(ev_writer_sink(c
->w
), "250 installed new config\n");
305 return 1; /* completed */
308 static void finished_rescan(void *ru
) {
309 struct conn
*const c
= ru
;
311 sink_writes(ev_writer_sink(c
->w
), "250 rescan completed\n");
312 /* Turn this connection back on */
313 ev_reader_enable(c
->r
);
316 static void start_fresh_rescan(void *ru
) {
317 struct conn
*const c
= ru
;
319 if(trackdb_rescan_underway()) {
320 /* Some other waiter beat us to it. However in this case we're happy to
321 * piggyback; the requirement is that a new rescan be started, not that it
322 * was _our_ rescan. */
324 /* We block until the rescan completes */
325 trackdb_add_rescanned(finished_rescan
, c
);
327 /* We report that the new rescan has started */
328 sink_writes(ev_writer_sink(c
->w
), "250 rescan initiated\n");
329 /* Turn this connection back on */
330 ev_reader_enable(c
->r
);
333 /* We are the first connection to get a callback so we must start a
336 /* We want to block until the new rescan completes */
337 trackdb_rescan(c
->ev
, 1/*check*/, finished_rescan
, c
);
339 /* We can report back immediately */
340 trackdb_rescan(c
->ev
, 1/*check*/, 0, 0);
341 sink_writes(ev_writer_sink(c
->w
), "250 rescan initiated\n");
342 /* Turn this connection back on */
343 ev_reader_enable(c
->r
);
348 static int c_rescan(struct conn
*c
,
351 int flag_wait
= 0, flag_fresh
= 0, n
;
354 for(n
= 0; n
< nvec
; ++n
) {
355 if(!strcmp(vec
[n
], "wait"))
356 flag_wait
= 1; /* wait for rescan to complete */
358 /* Currently disabled because untested (and hard to test). */
359 else if(!strcmp(vec
[n
], "fresh"))
360 flag_fresh
= 1; /* don't piggyback underway rescan */
363 sink_writes(ev_writer_sink(c
->w
), "550 unknown flag\n");
364 return 1; /* completed */
367 /* Report what was requested */
368 info("S%x rescan by %s (%s %s)", c
->tag
, c
->who
,
369 flag_wait ?
"wait" : "",
370 flag_fresh ?
"fresh" : "");
371 if(trackdb_rescan_underway()) {
373 /* We want a fresh rescan but there is already one underway. Arrange a
374 * callback when it completes and then set off a new one. */
375 c
->rescan_wait
= flag_wait
;
376 trackdb_add_rescanned(start_fresh_rescan
, c
);
380 sink_writes(ev_writer_sink(c
->w
), "250 rescan queued\n");
384 /* There's a rescan underway, and it's acceptable to piggyback on it */
386 /* We want to block until completion. */
387 trackdb_add_rescanned(finished_rescan
, c
);
390 /* We don't want to block. So we just report that things are in
392 sink_writes(ev_writer_sink(c
->w
), "250 rescan already underway\n");
397 /* No rescan is underway. fresh is therefore irrelevant. */
399 /* We want to block until completion */
400 trackdb_rescan(c
->ev
, 1/*check*/, finished_rescan
, c
);
403 /* We don't want to block. */
404 trackdb_rescan(c
->ev
, 1/*check*/, 0, 0);
405 sink_writes(ev_writer_sink(c
->w
), "250 rescan initiated\n");
406 return 1; /* completed */
411 static int c_version(struct conn
*c
,
412 char attribute((unused
)) **vec
,
413 int attribute((unused
)) nvec
) {
414 /* VERSION had better only use the basic character set */
415 sink_printf(ev_writer_sink(c
->w
), "251 %s\n", disorder_short_version_string
);
416 return 1; /* completed */
419 static int c_playing(struct conn
*c
,
420 char attribute((unused
)) **vec
,
421 int attribute((unused
)) nvec
) {
423 queue_fix_sofar(playing
);
424 playing
->expected
= 0;
425 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", queue_marshall(playing
));
427 sink_printf(ev_writer_sink(c
->w
), "259 nothing playing\n");
428 return 1; /* completed */
431 static const char *connection_host(struct conn
*c
) {
434 struct sockaddr_in in
;
435 struct sockaddr_in6 in6
;
441 /* get connection data */
443 if(getpeername(c
->fd
, &u
.sa
, &l
) < 0) {
444 error(errno
, "S%x error calling getpeername", c
->tag
);
447 if(c
->l
->pf
!= PF_UNIX
) {
448 if((n
= getnameinfo(&u
.sa
, l
,
449 host
, sizeof host
, 0, 0, NI_NUMERICHOST
))) {
450 error(0, "S%x error calling getnameinfo: %s", c
->tag
, gai_strerror(n
));
453 return xstrdup(host
);
458 static int c_user(struct conn
*c
,
460 int attribute((unused
)) nvec
) {
462 const char *res
, *host
, *password
;
466 sink_writes(ev_writer_sink(c
->w
), "530 already authenticated\n");
469 /* get connection data */
470 if(!(host
= connection_host(c
))) {
471 sink_writes(ev_writer_sink(c
->w
), "530 authentication failure\n");
475 k
= trackdb_getuserinfo(vec
[0]);
476 /* reject nonexistent users */
478 error(0, "S%x unknown user '%s' from %s", c
->tag
, vec
[0], host
);
479 sink_writes(ev_writer_sink(c
->w
), "530 authentication failed\n");
482 /* reject unconfirmed users */
483 if(kvp_get(k
, "confirmation")) {
484 error(0, "S%x unconfirmed user '%s' from %s", c
->tag
, vec
[0], host
);
485 sink_writes(ev_writer_sink(c
->w
), "530 authentication failed\n");
488 password
= kvp_get(k
, "password");
489 if(!password
) password
= "";
490 if(parse_rights(kvp_get(k
, "rights"), &rights
, 1)) {
491 error(0, "error parsing rights for %s", vec
[0]);
492 sink_writes(ev_writer_sink(c
->w
), "530 authentication failed\n");
495 /* check whether the response is right */
496 res
= authhash(c
->nonce
, sizeof c
->nonce
, password
,
497 config
->authorization_algorithm
);
498 if(wideopen
|| (res
&& !strcmp(res
, vec
[1]))) {
501 /* currently we only bother logging remote connections */
502 if(strcmp(host
, "local"))
503 info("S%x %s connected from %s", c
->tag
, vec
[0], host
);
505 c
->rights
|= RIGHT__LOCAL
;
506 sink_writes(ev_writer_sink(c
->w
), "230 OK\n");
509 /* oops, response was wrong */
510 info("S%x authentication failure for %s from %s", c
->tag
, vec
[0], host
);
511 sink_writes(ev_writer_sink(c
->w
), "530 authentication failed\n");
515 static int c_recent(struct conn
*c
,
516 char attribute((unused
)) **vec
,
517 int attribute((unused
)) nvec
) {
518 const struct queue_entry
*q
;
520 sink_writes(ev_writer_sink(c
->w
), "253 Tracks follow\n");
521 for(q
= phead
.next
; q
!= &phead
; q
= q
->next
)
522 sink_printf(ev_writer_sink(c
->w
), " %s\n", queue_marshall(q
));
523 sink_writes(ev_writer_sink(c
->w
), ".\n");
524 return 1; /* completed */
527 static int c_queue(struct conn
*c
,
528 char attribute((unused
)) **vec
,
529 int attribute((unused
)) nvec
) {
530 struct queue_entry
*q
;
535 sink_writes(ev_writer_sink(c
->w
), "253 Tracks follow\n");
536 if(playing_is_enabled() && !paused
) {
538 queue_fix_sofar(playing
);
539 if((l
= trackdb_get(playing
->track
, "_length"))
540 && (length
= atol(l
))) {
542 when
+= length
- playing
->sofar
+ config
->gap
;
545 /* Nothing is playing but playing is enabled, so whatever is
546 * first in the queue can be expected to start immediately. */
549 for(q
= qhead
.next
; q
!= &qhead
; q
= q
->next
) {
550 /* fill in estimated start time */
552 sink_printf(ev_writer_sink(c
->w
), " %s\n", queue_marshall(q
));
553 /* update for next track */
555 if((l
= trackdb_get(q
->track
, "_length"))
556 && (length
= atol(l
)))
557 when
+= length
+ config
->gap
;
562 sink_writes(ev_writer_sink(c
->w
), ".\n");
563 return 1; /* completed */
566 static int output_list(struct conn
*c
, char **vec
) {
568 sink_printf(ev_writer_sink(c
->w
), "%s\n", *vec
++);
569 sink_writes(ev_writer_sink(c
->w
), ".\n");
573 static int files_dirs(struct conn
*c
,
576 enum trackdb_listable what
) {
577 const char *dir
, *re
, *errstr
;
583 case 0: dir
= 0; re
= 0; break;
584 case 1: dir
= vec
[0]; re
= 0; break;
585 case 2: dir
= vec
[0]; re
= vec
[1]; break;
588 /* A bit of a bodge to make sure the args don't trample on cache keys */
589 if(dir
&& strchr(dir
, '\n')) {
590 sink_writes(ev_writer_sink(c
->w
), "550 invalid directory name\n");
593 if(re
&& strchr(re
, '\n')) {
594 sink_writes(ev_writer_sink(c
->w
), "550 invalid regexp\n");
597 /* We bother eliminating "" because the web interface is relatively
598 * likely to send it */
600 byte_xasprintf(&key
, "%d\n%s\n%s", (int)what
, dir ? dir
: "", re
);
601 fvec
= (char **)cache_get(&cache_files_type
, key
);
603 /* Got a cache hit, don't store the answer in the cache */
606 rec
= 0; /* quieten compiler */
608 /* Cache miss, we'll do the lookup and key != 0 so we'll store the answer
610 if(!(rec
= pcre_compile(re
, PCRE_CASELESS
|PCRE_UTF8
,
611 &errstr
, &erroffset
, 0))) {
612 sink_printf(ev_writer_sink(c
->w
), "550 Error compiling regexp: %s\n",
616 /* It only counts as a miss if the regexp was valid. */
617 ++cache_files_misses
;
620 /* No regexp, don't bother caching the result */
626 /* No cache hit (either because a miss, or because we did not look) so do
629 fvec
= trackdb_list(dir
, 0, what
, rec
);
631 fvec
= trackdb_list(0, 0, what
, rec
);
634 /* Put the answer in the cache */
635 cache_put(&cache_files_type
, key
, fvec
);
636 sink_writes(ev_writer_sink(c
->w
), "253 Listing follow\n");
637 return output_list(c
, fvec
);
640 static int c_files(struct conn
*c
,
643 return files_dirs(c
, vec
, nvec
, trackdb_files
);
646 static int c_dirs(struct conn
*c
,
649 return files_dirs(c
, vec
, nvec
, trackdb_directories
);
652 static int c_allfiles(struct conn
*c
,
655 return files_dirs(c
, vec
, nvec
, trackdb_directories
|trackdb_files
);
658 static int c_get(struct conn
*c
,
660 int attribute((unused
)) nvec
) {
661 const char *v
, *track
;
663 if(!(track
= trackdb_resolve(vec
[0]))) {
664 sink_writes(ev_writer_sink(c
->w
), "550 cannot resolve track\n");
667 if(vec
[1][0] != '_' && (v
= trackdb_get(track
, vec
[1])))
668 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", quoteutf8(v
));
670 sink_writes(ev_writer_sink(c
->w
), "555 not found\n");
674 static int c_length(struct conn
*c
,
676 int attribute((unused
)) nvec
) {
677 const char *track
, *v
;
679 if(!(track
= trackdb_resolve(vec
[0]))) {
680 sink_writes(ev_writer_sink(c
->w
), "550 cannot resolve track\n");
683 if((v
= trackdb_get(track
, "_length")))
684 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", quoteutf8(v
));
686 sink_writes(ev_writer_sink(c
->w
), "550 not found\n");
690 static int c_set(struct conn
*c
,
692 int attribute((unused
)) nvec
) {
695 if(!(track
= trackdb_resolve(vec
[0]))) {
696 sink_writes(ev_writer_sink(c
->w
), "550 cannot resolve track\n");
699 if(vec
[1][0] != '_' && !trackdb_set(track
, vec
[1], vec
[2]))
700 sink_writes(ev_writer_sink(c
->w
), "250 OK\n");
702 sink_writes(ev_writer_sink(c
->w
), "550 not found\n");
706 static int c_prefs(struct conn
*c
,
708 int attribute((unused
)) nvec
) {
712 if(!(track
= trackdb_resolve(vec
[0]))) {
713 sink_writes(ev_writer_sink(c
->w
), "550 cannot resolve track\n");
716 k
= trackdb_get_all(track
);
717 sink_writes(ev_writer_sink(c
->w
), "253 prefs follow\n");
718 for(; k
; k
= k
->next
)
719 if(k
->name
[0] != '_') /* omit internal values */
720 sink_printf(ev_writer_sink(c
->w
),
721 " %s %s\n", quoteutf8(k
->name
), quoteutf8(k
->value
));
722 sink_writes(ev_writer_sink(c
->w
), ".\n");
726 static int c_exists(struct conn
*c
,
728 int attribute((unused
)) nvec
) {
729 /* trackdb_exists() does its own alias checking */
730 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", noyes
[trackdb_exists(vec
[0])]);
734 static void search_parse_error(const char *msg
, void *u
) {
735 *(const char **)u
= msg
;
738 static int c_search(struct conn
*c
,
740 int attribute((unused
)) nvec
) {
741 char **terms
, **results
;
742 int nterms
, nresults
, n
;
743 const char *e
= "unknown error";
745 /* This is a bit of a bodge. Initially it's there to make the eclient
746 * interface a bit more convenient to add searching to, but it has the more
747 * compelling advantage that if everything uses it, then interpretation of
748 * user-supplied search strings will be the same everywhere. */
749 if(!(terms
= split(vec
[0], &nterms
, SPLIT_QUOTES
, search_parse_error
, &e
))) {
750 sink_printf(ev_writer_sink(c
->w
), "550 %s\n", e
);
752 results
= trackdb_search(terms
, nterms
, &nresults
);
753 sink_printf(ev_writer_sink(c
->w
), "253 %d matches\n", nresults
);
754 for(n
= 0; n
< nresults
; ++n
)
755 sink_printf(ev_writer_sink(c
->w
), "%s\n", results
[n
]);
756 sink_writes(ev_writer_sink(c
->w
), ".\n");
761 static int c_random_enable(struct conn
*c
,
762 char attribute((unused
)) **vec
,
763 int attribute((unused
)) nvec
) {
764 enable_random(c
->who
, c
->ev
);
765 /* Enable implicitly unpauses if there is nothing playing */
766 if(paused
&& !playing
) resume_playing(c
->who
);
767 sink_writes(ev_writer_sink(c
->w
), "250 OK\n");
768 return 1; /* completed */
771 static int c_random_disable(struct conn
*c
,
772 char attribute((unused
)) **vec
,
773 int attribute((unused
)) nvec
) {
774 disable_random(c
->who
);
775 sink_writes(ev_writer_sink(c
->w
), "250 OK\n");
776 return 1; /* completed */
779 static int c_random_enabled(struct conn
*c
,
780 char attribute((unused
)) **vec
,
781 int attribute((unused
)) nvec
) {
782 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", noyes
[random_is_enabled()]);
783 return 1; /* completed */
786 static void got_stats(char *stats
, void *u
) {
787 struct conn
*const c
= u
;
789 sink_printf(ev_writer_sink(c
->w
), "253 stats\n%s\n.\n", stats
);
790 /* Now we can start processing commands again */
791 ev_reader_enable(c
->r
);
794 static int c_stats(struct conn
*c
,
795 char attribute((unused
)) **vec
,
796 int attribute((unused
)) nvec
) {
797 trackdb_stats_subprocess(c
->ev
, got_stats
, c
);
798 return 0; /* not yet complete */
801 static int c_volume(struct conn
*c
,
813 l
= r
= atoi(vec
[0]);
824 rights
= set ? RIGHT_VOLUME
: RIGHT_READ
;
825 if(!(c
->rights
& rights
)) {
826 error(0, "%s attempted to set volume but lacks required rights", c
->who
);
827 sink_writes(ev_writer_sink(c
->w
), "510 Prohibited\n");
830 if(mixer_control(-1/*as configured*/, &l
, &r
, set
))
831 sink_writes(ev_writer_sink(c
->w
), "550 error accessing mixer\n");
833 sink_printf(ev_writer_sink(c
->w
), "252 %d %d\n", l
, r
);
834 if(l
!= volume_left
|| r
!= volume_right
) {
837 snprintf(lb
, sizeof lb
, "%d", l
);
838 snprintf(rb
, sizeof rb
, "%d", r
);
839 eventlog("volume", lb
, rb
, (char *)0);
845 /** @brief Called when data arrives on a log connection
847 * We just discard all such data. The client may occasionally send data as a
850 static int logging_reader_callback(ev_source
attribute((unused
)) *ev
,
852 void attribute((unused
)) *ptr
,
854 int attribute((unused
)) eof
,
855 void attribute((unused
)) *u
) {
858 ev_reader_consume(reader
, bytes
);
860 /* Oops, that's all for now */
861 D(("logging reader eof"));
864 ev_writer_close(c
->w
);
868 remove_connection(c
);
873 static void logclient(const char *msg
, void *user
) {
874 struct conn
*c
= user
;
877 /* This connection has gone up in smoke for some reason */
878 eventlog_remove(c
->lo
);
882 /* user_* messages are restricted */
883 if(!strncmp(msg
, "user_", 5)) {
884 /* They are only sent to admin users */
885 if(!(c
->rights
& RIGHT_ADMIN
))
887 /* They are not sent over TCP connections unless remote user-management is
889 if(!config
->remote_userman
&& !(c
->rights
& RIGHT__LOCAL
))
892 sink_printf(ev_writer_sink(c
->w
), "%"PRIxMAX
" %s\n",
893 (uintmax_t)time(0), msg
);
896 static int c_log(struct conn
*c
,
897 char attribute((unused
)) **vec
,
898 int attribute((unused
)) nvec
) {
901 sink_writes(ev_writer_sink(c
->w
), "254 OK\n");
902 /* pump out initial state */
904 sink_printf(ev_writer_sink(c
->w
), "%"PRIxMAX
" state %s\n",
906 playing_is_enabled() ?
"enable_play" : "disable_play");
907 sink_printf(ev_writer_sink(c
->w
), "%"PRIxMAX
" state %s\n",
909 random_is_enabled() ?
"enable_random" : "disable_random");
910 sink_printf(ev_writer_sink(c
->w
), "%"PRIxMAX
" state %s\n",
912 paused ?
"pause" : "resume");
914 sink_printf(ev_writer_sink(c
->w
), "%"PRIxMAX
" state playing\n",
917 sink_printf(ev_writer_sink(c
->w
), "%"PRIxMAX
" volume %d %d\n",
918 (uintmax_t)now
, volume_left
, volume_right
);
919 c
->lo
= xmalloc(sizeof *c
->lo
);
920 c
->lo
->fn
= logclient
;
923 c
->reader
= logging_reader_callback
;
927 /** @brief Test whether a move is allowed
928 * @param c Connection
929 * @param qs List of IDs on queue
930 * @param nqs Number of IDs
931 * @return 0 if move is prohibited, non-0 if it is allowed
933 static int has_move_rights(struct conn
*c
, struct queue_entry
**qs
, int nqs
) {
934 for(; nqs
> 0; ++qs
, --nqs
) {
935 struct queue_entry
*const q
= *qs
;
937 if(!right_movable(c
->rights
, c
->who
, q
))
943 static int c_move(struct conn
*c
,
945 int attribute((unused
)) nvec
) {
946 struct queue_entry
*q
;
949 if(!(q
= queue_find(vec
[0]))) {
950 sink_writes(ev_writer_sink(c
->w
), "550 no such track on the queue\n");
953 if(!has_move_rights(c
, &q
, 1)) {
954 error(0, "%s attempted move but lacks required rights", c
->who
);
955 sink_writes(ev_writer_sink(c
->w
),
956 "510 Not authorized to move that track\n");
959 n
= queue_move(q
, atoi(vec
[1]), c
->who
);
960 sink_printf(ev_writer_sink(c
->w
), "252 %d\n", n
);
961 /* If we've moved to the head of the queue then prepare the track. */
967 static int c_moveafter(struct conn
*c
,
969 int attribute((unused
)) nvec
) {
970 struct queue_entry
*q
, **qs
;
974 if(!(q
= queue_find(vec
[0]))) {
975 sink_writes(ev_writer_sink(c
->w
), "550 no such track on the queue\n");
982 qs
= xcalloc(nvec
, sizeof *qs
);
983 for(n
= 0; n
< nvec
; ++n
)
984 if(!(qs
[n
] = queue_find(vec
[n
]))) {
985 sink_writes(ev_writer_sink(c
->w
), "550 no such track on the queue\n");
988 if(!has_move_rights(c
, qs
, nvec
)) {
989 error(0, "%s attempted moveafter but lacks required rights", c
->who
);
990 sink_writes(ev_writer_sink(c
->w
),
991 "510 Not authorized to move those tracks\n");
994 queue_moveafter(q
, nvec
, qs
, c
->who
);
995 sink_printf(ev_writer_sink(c
->w
), "250 Moved tracks\n");
996 /* If we've moved to the head of the queue then prepare the track. */
1002 static int c_part(struct conn
*c
,
1004 int attribute((unused
)) nvec
) {
1007 if(!(track
= trackdb_resolve(vec
[0]))) {
1008 sink_writes(ev_writer_sink(c
->w
), "550 cannot resolve track\n");
1011 sink_printf(ev_writer_sink(c
->w
), "252 %s\n",
1012 quoteutf8(trackdb_getpart(track
, vec
[1], vec
[2])));
1016 static int c_resolve(struct conn
*c
,
1018 int attribute((unused
)) nvec
) {
1021 if(!(track
= trackdb_resolve(vec
[0]))) {
1022 sink_writes(ev_writer_sink(c
->w
), "550 cannot resolve track\n");
1025 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", quoteutf8(track
));
1029 static int c_tags(struct conn
*c
,
1030 char attribute((unused
)) **vec
,
1031 int attribute((unused
)) nvec
) {
1032 char **tags
= trackdb_alltags();
1034 sink_printf(ev_writer_sink(c
->w
), "253 Tag list follows\n");
1036 sink_printf(ev_writer_sink(c
->w
), "%s%s\n",
1037 **tags
== '.' ?
"." : "", *tags
);
1040 sink_writes(ev_writer_sink(c
->w
), ".\n");
1041 return 1; /* completed */
1044 static int c_set_global(struct conn
*c
,
1046 int attribute((unused
)) nvec
) {
1047 if(vec
[0][0] == '_') {
1048 sink_writes(ev_writer_sink(c
->w
), "550 cannot set internal global preferences\n");
1051 trackdb_set_global(vec
[0], vec
[1], c
->who
);
1052 sink_printf(ev_writer_sink(c
->w
), "250 OK\n");
1056 static int c_get_global(struct conn
*c
,
1058 int attribute((unused
)) nvec
) {
1059 const char *s
= trackdb_get_global(vec
[0]);
1062 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", quoteutf8(s
));
1064 sink_writes(ev_writer_sink(c
->w
), "555 not found\n");
1068 static int c_nop(struct conn
*c
,
1069 char attribute((unused
)) **vec
,
1070 int attribute((unused
)) nvec
) {
1071 sink_printf(ev_writer_sink(c
->w
), "250 Quack\n");
1075 static int c_new(struct conn
*c
,
1085 if(max
<= 0 || max
> config
->new_max
)
1086 max
= config
->new_max
;
1087 tracks
= trackdb_new(0, max
);
1088 sink_printf(ev_writer_sink(c
->w
), "253 New track list follows\n");
1091 sink_printf(ev_writer_sink(c
->w
), "%s%s\n",
1092 **tracks
== '.' ?
"." : "", *tracks
);
1095 sink_writes(ev_writer_sink(c
->w
), ".\n");
1096 return 1; /* completed */
1100 static int c_rtp_address(struct conn
*c
,
1101 char attribute((unused
)) **vec
,
1102 int attribute((unused
)) nvec
) {
1103 if(config
->api
== BACKEND_NETWORK
) {
1104 sink_printf(ev_writer_sink(c
->w
), "252 %s %s\n",
1105 quoteutf8(config
->broadcast
.s
[0]),
1106 quoteutf8(config
->broadcast
.s
[1]));
1108 sink_writes(ev_writer_sink(c
->w
), "550 No RTP\n");
1112 static int c_cookie(struct conn
*c
,
1114 int attribute((unused
)) nvec
) {
1119 /* Can't log in twice on the same connection */
1121 sink_writes(ev_writer_sink(c
->w
), "530 already authenticated\n");
1124 /* Get some kind of peer identifcation */
1125 if(!(host
= connection_host(c
))) {
1126 sink_writes(ev_writer_sink(c
->w
), "530 authentication failure\n");
1129 /* Check the cookie */
1130 user
= verify_cookie(vec
[0], &rights
);
1132 sink_writes(ev_writer_sink(c
->w
), "530 authentication failure\n");
1139 if(strcmp(host
, "local"))
1140 info("S%x %s connected with cookie from %s", c
->tag
, user
, host
);
1142 c
->rights
|= RIGHT__LOCAL
;
1143 /* Response contains username so client knows who they are acting as */
1144 sink_printf(ev_writer_sink(c
->w
), "232 %s\n", quoteutf8(user
));
1148 static int c_make_cookie(struct conn
*c
,
1149 char attribute((unused
)) **vec
,
1150 int attribute((unused
)) nvec
) {
1151 const char *cookie
= make_cookie(c
->who
);
1154 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", quoteutf8(cookie
));
1156 sink_writes(ev_writer_sink(c
->w
), "550 Cannot create cookie\n");
1160 static int c_revoke(struct conn
*c
,
1161 char attribute((unused
)) **vec
,
1162 int attribute((unused
)) nvec
) {
1164 revoke_cookie(c
->cookie
);
1165 sink_writes(ev_writer_sink(c
->w
), "250 OK\n");
1167 sink_writes(ev_writer_sink(c
->w
), "550 Did not log in with cookie\n");
1171 static int c_adduser(struct conn
*c
,
1176 if(!config
->remote_userman
&& !(c
->rights
& RIGHT__LOCAL
)) {
1177 error(0, "S%x: remote adduser", c
->tag
);
1178 sink_writes(ev_writer_sink(c
->w
), "550 Remote user management is disabled\n");
1183 if(parse_rights(vec
[2], 0, 1)) {
1184 sink_writes(ev_writer_sink(c
->w
), "550 Invalid rights list\n");
1188 rights
= config
->default_rights
;
1189 if(trackdb_adduser(vec
[0], vec
[1], rights
,
1190 0/*email*/, 0/*confirmation*/))
1191 sink_writes(ev_writer_sink(c
->w
), "550 Cannot create user\n");
1193 sink_writes(ev_writer_sink(c
->w
), "250 User created\n");
1197 static int c_deluser(struct conn
*c
,
1199 int attribute((unused
)) nvec
) {
1202 if(!config
->remote_userman
&& !(c
->rights
& RIGHT__LOCAL
)) {
1203 error(0, "S%x: remote deluser", c
->tag
);
1204 sink_writes(ev_writer_sink(c
->w
), "550 Remote user management is disabled\n");
1207 if(trackdb_deluser(vec
[0])) {
1208 sink_writes(ev_writer_sink(c
->w
), "550 Cannot delete user\n");
1211 /* Zap connections belonging to deleted user */
1212 for(d
= connections
; d
; d
= d
->next
)
1213 if(!strcmp(d
->who
, vec
[0]))
1215 sink_writes(ev_writer_sink(c
->w
), "250 User deleted\n");
1219 static int c_edituser(struct conn
*c
,
1221 int attribute((unused
)) nvec
) {
1224 if(!config
->remote_userman
&& !(c
->rights
& RIGHT__LOCAL
)) {
1225 error(0, "S%x: remote edituser", c
->tag
);
1226 sink_writes(ev_writer_sink(c
->w
), "550 Remote user management is disabled\n");
1229 /* RIGHT_ADMIN can do anything; otherwise you can only set your own email
1230 * address and password. */
1231 if((c
->rights
& RIGHT_ADMIN
)
1232 || (!strcmp(c
->who
, vec
[0])
1233 && (!strcmp(vec
[1], "email")
1234 || !strcmp(vec
[1], "password")))) {
1235 if(trackdb_edituserinfo(vec
[0], vec
[1], vec
[2])) {
1236 sink_writes(ev_writer_sink(c
->w
), "550 Failed to change setting\n");
1239 if(!strcmp(vec
[1], "password")) {
1240 /* Zap all connections for this user after a password change */
1241 for(d
= connections
; d
; d
= d
->next
)
1242 if(!strcmp(d
->who
, vec
[0]))
1244 } else if(!strcmp(vec
[1], "rights")) {
1245 /* Update rights for this user */
1248 if(!parse_rights(vec
[2], &r
, 1)) {
1249 const char *new_rights
= rights_string(r
);
1250 for(d
= connections
; d
; d
= d
->next
) {
1251 if(!strcmp(d
->who
, vec
[0])) {
1254 /* Notify any log connections */
1256 sink_printf(ev_writer_sink(d
->w
),
1257 "%"PRIxMAX
" rights_changed %s\n",
1259 quoteutf8(new_rights
));
1264 sink_writes(ev_writer_sink(c
->w
), "250 OK\n");
1266 error(0, "%s attempted edituser but lacks required rights", c
->who
);
1267 sink_writes(ev_writer_sink(c
->w
), "510 Restricted to administrators\n");
1272 static int c_userinfo(struct conn
*c
,
1273 char attribute((unused
)) **vec
,
1274 int attribute((unused
)) nvec
) {
1278 /* We allow remote querying of rights so that clients can figure out what
1279 * they're allowed to do */
1280 if(!config
->remote_userman
1281 && !(c
->rights
& RIGHT__LOCAL
)
1282 && strcmp(vec
[1], "rights")) {
1283 error(0, "S%x: remote userinfo %s %s", c
->tag
, vec
[0], vec
[1]);
1284 sink_writes(ev_writer_sink(c
->w
), "550 Remote user management is disabled\n");
1287 /* RIGHT_ADMIN allows anything; otherwise you can only get your own email
1288 * address and rights list. */
1289 if((c
->rights
& RIGHT_ADMIN
)
1290 || (!strcmp(c
->who
, vec
[0])
1291 && (!strcmp(vec
[1], "email")
1292 || !strcmp(vec
[1], "rights")))) {
1293 if((k
= trackdb_getuserinfo(vec
[0])))
1294 if((value
= kvp_get(k
, vec
[1])))
1295 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", quoteutf8(value
));
1297 sink_writes(ev_writer_sink(c
->w
), "555 Not set\n");
1299 sink_writes(ev_writer_sink(c
->w
), "550 No such user\n");
1301 error(0, "%s attempted userinfo but lacks required rights", c
->who
);
1302 sink_writes(ev_writer_sink(c
->w
), "510 Restricted to administrators\n");
1307 static int c_users(struct conn
*c
,
1308 char attribute((unused
)) **vec
,
1309 int attribute((unused
)) nvec
) {
1310 /* TODO de-dupe with c_tags */
1311 char **users
= trackdb_listusers();
1313 sink_writes(ev_writer_sink(c
->w
), "253 User list follows\n");
1315 sink_printf(ev_writer_sink(c
->w
), "%s%s\n",
1316 **users
== '.' ?
"." : "", *users
);
1319 sink_writes(ev_writer_sink(c
->w
), ".\n");
1320 return 1; /* completed */
1323 /** @brief Base64 mapping table for confirmation strings
1325 * This is used with generic_to_base64() and generic_base64(). We cannot use
1326 * the MIME table as that contains '+' and '=' which get quoted when
1327 * URL-encoding. (The CGI still does the URL encoding but it is desirable to
1328 * avoid it being necessary.)
1330 static const char confirm_base64_table
[] =
1331 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789/.*";
1333 static int c_register(struct conn
*c
,
1335 int attribute((unused
)) nvec
) {
1340 /* The confirmation string is base64(username;nonce) */
1341 bufsize
= strlen(vec
[0]) + CONFIRM_SIZE
+ 2;
1342 buf
= xmalloc_noptr(bufsize
);
1343 offset
= byte_snprintf(buf
, bufsize
, "%s;", vec
[0]);
1344 gcry_randomize(buf
+ offset
, CONFIRM_SIZE
, GCRY_STRONG_RANDOM
);
1345 cs
= generic_to_base64((uint8_t *)buf
, offset
+ CONFIRM_SIZE
,
1346 confirm_base64_table
);
1347 if(trackdb_adduser(vec
[0], vec
[1], config
->default_rights
, vec
[2], cs
))
1348 sink_writes(ev_writer_sink(c
->w
), "550 Cannot create user\n");
1350 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", quoteutf8(cs
));
1354 static int c_confirm(struct conn
*c
,
1356 int attribute((unused
)) nvec
) {
1362 /* Get some kind of peer identifcation */
1363 if(!(host
= connection_host(c
))) {
1364 sink_writes(ev_writer_sink(c
->w
), "530 Authentication failure\n");
1367 if(!(user
= generic_base64(vec
[0], &nuser
, confirm_base64_table
))
1368 || !(sep
= memchr(user
, ';', nuser
))) {
1369 sink_writes(ev_writer_sink(c
->w
), "550 Malformed confirmation string\n");
1373 if(trackdb_confirm(user
, vec
[0], &rights
))
1374 sink_writes(ev_writer_sink(c
->w
), "550 Incorrect confirmation string\n");
1379 if(strcmp(host
, "local"))
1380 info("S%x %s confirmed from %s", c
->tag
, user
, host
);
1382 c
->rights
|= RIGHT__LOCAL
;
1383 /* Response contains username so client knows who they are acting as */
1384 sink_printf(ev_writer_sink(c
->w
), "232 %s\n", quoteutf8(user
));
1389 static int sent_reminder(ev_source
attribute((unused
)) *ev
,
1390 pid_t
attribute((unused
)) pid
,
1392 const struct rusage
attribute((unused
)) *rusage
,
1394 struct conn
*const c
= u
;
1396 /* Tell the client what went down */
1398 sink_writes(ev_writer_sink(c
->w
), "250 OK\n");
1400 error(0, "reminder subprocess %s", wstat(status
));
1401 sink_writes(ev_writer_sink(c
->w
), "550 Cannot send a reminder email\n");
1403 /* Re-enable this connection */
1404 ev_reader_enable(c
->r
);
1408 static int c_reminder(struct conn
*c
,
1410 int attribute((unused
)) nvec
) {
1412 const char *password
, *email
, *text
, *encoding
, *charset
, *content_type
;
1417 static hash
*last_reminder
;
1419 if(!config
->mail_sender
) {
1420 error(0, "cannot send password reminders because mail_sender not set");
1421 sink_writes(ev_writer_sink(c
->w
), "550 Cannot send a reminder email\n");
1424 if(!(k
= trackdb_getuserinfo(vec
[0]))) {
1425 error(0, "reminder for user '%s' who does not exist", vec
[0]);
1426 sink_writes(ev_writer_sink(c
->w
), "550 Cannot send a reminder email\n");
1429 if(!(email
= kvp_get(k
, "email"))
1430 || !email_valid(email
)) {
1431 error(0, "user '%s' has no valid email address", vec
[0]);
1432 sink_writes(ev_writer_sink(c
->w
), "550 Cannot send a reminder email\n");
1435 if(!(password
= kvp_get(k
, "password"))
1437 error(0, "user '%s' has no password", vec
[0]);
1438 sink_writes(ev_writer_sink(c
->w
), "550 Cannot send a reminder email\n");
1441 /* Rate-limit reminders. This hash is bounded in size by the number of
1442 * users. If this is actually a problem for anyone then we can periodically
1445 last_reminder
= hash_new(sizeof (time_t));
1446 last
= hash_find(last_reminder
, vec
[0]);
1448 if(last
&& now
< *last
+ config
->reminder_interval
) {
1449 error(0, "sent a password reminder to '%s' too recently", vec
[0]);
1450 sink_writes(ev_writer_sink(c
->w
), "550 Cannot send a reminder email\n");
1453 /* Send the reminder */
1454 /* TODO this should be templatized and to some extent merged with
1455 * the code in act_register() */
1456 byte_xasprintf((char **)&text
,
1457 "Someone requested that you be sent a reminder of your DisOrder password.\n"
1458 "Your password is:\n"
1461 if(!(text
= mime_encode_text(text
, &charset
, &encoding
)))
1462 fatal(0, "cannot encode email");
1463 byte_xasprintf((char **)&content_type
, "text/plain;charset=%s",
1464 quote822(charset
, 0));
1465 pid
= sendmail_subprocess("", config
->mail_sender
, email
,
1466 "DisOrder password reminder",
1467 encoding
, content_type
, text
);
1469 sink_writes(ev_writer_sink(c
->w
), "550 Cannot send a reminder email\n");
1472 hash_add(last_reminder
, vec
[0], &now
, HASH_INSERT_OR_REPLACE
);
1473 info("sending a passsword reminder to user '%s'", vec
[0]);
1474 /* We can only continue when the subprocess finishes */
1475 ev_child(c
->ev
, pid
, 0, sent_reminder
, c
);
1479 static int c_schedule_list(struct conn
*c
,
1480 char attribute((unused
)) **vec
,
1481 int attribute((unused
)) nvec
) {
1482 char **ids
= schedule_list(0);
1483 sink_writes(ev_writer_sink(c
->w
), "253 ID list follows\n");
1485 sink_printf(ev_writer_sink(c
->w
), "%s\n", *ids
++);
1486 sink_writes(ev_writer_sink(c
->w
), ".\n");
1487 return 1; /* completed */
1490 static int c_schedule_get(struct conn
*c
,
1492 int attribute((unused
)) nvec
) {
1493 struct kvp
*actiondata
= schedule_get(vec
[0]), *k
;
1496 sink_writes(ev_writer_sink(c
->w
), "555 No such event\n");
1497 return 1; /* completed */
1499 /* Scheduled events are public information. Anyone with RIGHT_READ can see
1501 sink_writes(ev_writer_sink(c
->w
), "253 Event information follows\n");
1502 for(k
= actiondata
; k
; k
= k
->next
)
1503 sink_printf(ev_writer_sink(c
->w
), " %s %s\n",
1504 quoteutf8(k
->name
), quoteutf8(k
->value
));
1505 sink_writes(ev_writer_sink(c
->w
), ".\n");
1506 return 1; /* completed */
1509 static int c_schedule_del(struct conn
*c
,
1511 int attribute((unused
)) nvec
) {
1512 struct kvp
*actiondata
= schedule_get(vec
[0]);
1515 sink_writes(ev_writer_sink(c
->w
), "555 No such event\n");
1516 return 1; /* completed */
1518 /* If you have admin rights you can delete anything. If you don't then you
1519 * can only delete your own scheduled events. */
1520 if(!(c
->rights
& RIGHT_ADMIN
)) {
1521 const char *who
= kvp_get(actiondata
, "who");
1523 if(!who
|| !c
->who
|| strcmp(who
, c
->who
)) {
1524 sink_writes(ev_writer_sink(c
->w
), "551 Not authorized\n");
1525 return 1; /* completed */
1528 if(schedule_del(vec
[0]))
1529 sink_writes(ev_writer_sink(c
->w
), "550 Could not delete scheduled event\n");
1531 sink_writes(ev_writer_sink(c
->w
), "250 Deleted\n");
1532 return 1; /* completed */
1535 static int c_schedule_add(struct conn
*c
,
1538 struct kvp
*actiondata
= 0;
1541 /* Standard fields */
1542 kvp_set(&actiondata
, "who", c
->who
);
1543 kvp_set(&actiondata
, "when", vec
[0]);
1544 kvp_set(&actiondata
, "priority", vec
[1]);
1545 kvp_set(&actiondata
, "action", vec
[2]);
1546 /* Action-dependent fields */
1547 if(!strcmp(vec
[2], "play")) {
1549 sink_writes(ev_writer_sink(c
->w
), "550 Wrong number of arguments\n");
1552 if(!trackdb_exists(vec
[3])) {
1553 sink_writes(ev_writer_sink(c
->w
), "550 Track is not in database\n");
1556 kvp_set(&actiondata
, "track", vec
[3]);
1557 } else if(!strcmp(vec
[2], "set-global")) {
1558 if(nvec
< 4 || nvec
> 5) {
1559 sink_writes(ev_writer_sink(c
->w
), "550 Wrong number of arguments\n");
1562 kvp_set(&actiondata
, "key", vec
[3]);
1564 kvp_set(&actiondata
, "value", vec
[4]);
1566 sink_writes(ev_writer_sink(c
->w
), "550 Unknown action\n");
1569 /* schedule_add() checks user rights */
1570 id
= schedule_add(c
->ev
, actiondata
);
1572 sink_writes(ev_writer_sink(c
->w
), "550 Cannot add scheduled event\n");
1574 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", id
);
1578 static const struct command
{
1579 /** @brief Command name */
1582 /** @brief Minimum number of arguments */
1585 /** @brief Maximum number of arguments */
1588 /** @brief Function to process command */
1589 int (*fn
)(struct conn
*, char **, int);
1591 /** @brief Rights required to execute command
1593 * 0 means that the command can be issued without logging in. If multiple
1594 * bits are listed here any of those rights will do.
1598 { "adduser", 2, 3, c_adduser
, RIGHT_ADMIN
|RIGHT__LOCAL
},
1599 { "allfiles", 0, 2, c_allfiles
, RIGHT_READ
},
1600 { "confirm", 1, 1, c_confirm
, 0 },
1601 { "cookie", 1, 1, c_cookie
, 0 },
1602 { "deluser", 1, 1, c_deluser
, RIGHT_ADMIN
|RIGHT__LOCAL
},
1603 { "dirs", 0, 2, c_dirs
, RIGHT_READ
},
1604 { "disable", 0, 1, c_disable
, RIGHT_GLOBAL_PREFS
},
1605 { "edituser", 3, 3, c_edituser
, RIGHT_ADMIN
|RIGHT_USERINFO
},
1606 { "enable", 0, 0, c_enable
, RIGHT_GLOBAL_PREFS
},
1607 { "enabled", 0, 0, c_enabled
, RIGHT_READ
},
1608 { "exists", 1, 1, c_exists
, RIGHT_READ
},
1609 { "files", 0, 2, c_files
, RIGHT_READ
},
1610 { "get", 2, 2, c_get
, RIGHT_READ
},
1611 { "get-global", 1, 1, c_get_global
, RIGHT_READ
},
1612 { "length", 1, 1, c_length
, RIGHT_READ
},
1613 { "log", 0, 0, c_log
, RIGHT_READ
},
1614 { "make-cookie", 0, 0, c_make_cookie
, RIGHT_READ
},
1615 { "move", 2, 2, c_move
, RIGHT_MOVE__MASK
},
1616 { "moveafter", 1, INT_MAX
, c_moveafter
, RIGHT_MOVE__MASK
},
1617 { "new", 0, 1, c_new
, RIGHT_READ
},
1618 { "nop", 0, 0, c_nop
, 0 },
1619 { "part", 3, 3, c_part
, RIGHT_READ
},
1620 { "pause", 0, 0, c_pause
, RIGHT_PAUSE
},
1621 { "play", 1, 1, c_play
, RIGHT_PLAY
},
1622 { "playing", 0, 0, c_playing
, RIGHT_READ
},
1623 { "prefs", 1, 1, c_prefs
, RIGHT_READ
},
1624 { "queue", 0, 0, c_queue
, RIGHT_READ
},
1625 { "random-disable", 0, 0, c_random_disable
, RIGHT_GLOBAL_PREFS
},
1626 { "random-enable", 0, 0, c_random_enable
, RIGHT_GLOBAL_PREFS
},
1627 { "random-enabled", 0, 0, c_random_enabled
, RIGHT_READ
},
1628 { "recent", 0, 0, c_recent
, RIGHT_READ
},
1629 { "reconfigure", 0, 0, c_reconfigure
, RIGHT_ADMIN
},
1630 { "register", 3, 3, c_register
, RIGHT_REGISTER
|RIGHT__LOCAL
},
1631 { "reminder", 1, 1, c_reminder
, RIGHT__LOCAL
},
1632 { "remove", 1, 1, c_remove
, RIGHT_REMOVE__MASK
},
1633 { "rescan", 0, INT_MAX
, c_rescan
, RIGHT_RESCAN
},
1634 { "resolve", 1, 1, c_resolve
, RIGHT_READ
},
1635 { "resume", 0, 0, c_resume
, RIGHT_PAUSE
},
1636 { "revoke", 0, 0, c_revoke
, RIGHT_READ
},
1637 { "rtp-address", 0, 0, c_rtp_address
, 0 },
1638 { "schedule-add", 3, INT_MAX
, c_schedule_add
, RIGHT_READ
},
1639 { "schedule-del", 1, 1, c_schedule_del
, RIGHT_READ
},
1640 { "schedule-get", 1, 1, c_schedule_get
, RIGHT_READ
},
1641 { "schedule-list", 0, 0, c_schedule_list
, RIGHT_READ
},
1642 { "scratch", 0, 1, c_scratch
, RIGHT_SCRATCH__MASK
},
1643 { "search", 1, 1, c_search
, RIGHT_READ
},
1644 { "set", 3, 3, c_set
, RIGHT_PREFS
, },
1645 { "set-global", 2, 2, c_set_global
, RIGHT_GLOBAL_PREFS
},
1646 { "shutdown", 0, 0, c_shutdown
, RIGHT_ADMIN
},
1647 { "stats", 0, 0, c_stats
, RIGHT_READ
},
1648 { "tags", 0, 0, c_tags
, RIGHT_READ
},
1649 { "unset", 2, 2, c_set
, RIGHT_PREFS
},
1650 { "unset-global", 1, 1, c_set_global
, RIGHT_GLOBAL_PREFS
},
1651 { "user", 2, 2, c_user
, 0 },
1652 { "userinfo", 2, 2, c_userinfo
, RIGHT_READ
},
1653 { "users", 0, 0, c_users
, RIGHT_READ
},
1654 { "version", 0, 0, c_version
, RIGHT_READ
},
1655 { "volume", 0, 2, c_volume
, RIGHT_READ
|RIGHT_VOLUME
}
1658 static void command_error(const char *msg
, void *u
) {
1661 sink_printf(ev_writer_sink(c
->w
), "500 parse error: %s\n", msg
);
1664 /* process a command. Return 1 if complete, 0 if incomplete. */
1665 static int command(struct conn
*c
, char *line
) {
1669 D(("server command %s", line
));
1670 /* We force everything into NFC as early as possible */
1671 if(!(line
= utf8_compose_canon(line
, strlen(line
), 0))) {
1672 sink_writes(ev_writer_sink(c
->w
), "500 cannot normalize command\n");
1675 if(!(vec
= split(line
, &nvec
, SPLIT_QUOTES
, command_error
, c
))) {
1676 sink_writes(ev_writer_sink(c
->w
), "500 cannot parse command\n");
1680 sink_writes(ev_writer_sink(c
->w
), "500 do what?\n");
1683 if((n
= TABLE_FIND(commands
, name
, vec
[0])) < 0)
1684 sink_writes(ev_writer_sink(c
->w
), "500 unknown command\n");
1686 if(commands
[n
].rights
1687 && !(c
->rights
& commands
[n
].rights
)) {
1688 error(0, "%s attempted %s but lacks required rights", c
->who ? c
->who
: "NULL",
1690 sink_writes(ev_writer_sink(c
->w
), "510 Prohibited\n");
1695 if(nvec
< commands
[n
].minargs
) {
1696 sink_writes(ev_writer_sink(c
->w
), "500 missing argument(s)\n");
1699 if(nvec
> commands
[n
].maxargs
) {
1700 sink_writes(ev_writer_sink(c
->w
), "500 too many arguments\n");
1703 return commands
[n
].fn(c
, vec
, nvec
);
1705 return 1; /* completed */
1708 /* redirect to the right reader callback for our current state */
1709 static int redirect_reader_callback(ev_source
*ev
,
1717 return c
->reader(ev
, reader
, ptr
, bytes
, eof
, u
);
1720 /* the main command reader */
1721 static int reader_callback(ev_source
attribute((unused
)) *ev
,
1731 D(("server reader_callback"));
1732 while((eol
= memchr(ptr
, '\n', bytes
))) {
1734 ev_reader_consume(reader
, eol
- (char *)ptr
);
1735 complete
= command(c
, ptr
);
1736 bytes
-= (eol
- (char *)ptr
);
1739 /* the command had better have set a new reader callback */
1741 /* there are further bytes to read, or we are at eof; arrange for the
1742 * command's reader callback to handle them */
1743 return ev_reader_incomplete(reader
);
1744 /* nothing's going on right now */
1747 /* command completed, we can go around and handle the next one */
1751 error(0, "S%x unterminated line", c
->tag
);
1752 D(("normal reader close"));
1755 D(("close associated writer"));
1756 ev_writer_close(c
->w
);
1759 remove_connection(c
);
1764 static int listen_callback(ev_source
*ev
,
1766 const struct sockaddr
attribute((unused
)) *remote
,
1767 socklen_t
attribute((unused
)) rlen
,
1769 const struct listener
*l
= u
;
1770 struct conn
*c
= xmalloc(sizeof *c
);
1771 static unsigned tags
;
1773 D(("server listen_callback fd %d (%s)", fd
, l
->name
));
1776 c
->next
= connections
;
1779 c
->w
= ev_writer_new(ev
, fd
, writer_error
, c
,
1781 c
->r
= ev_reader_new(ev
, fd
, redirect_reader_callback
, reader_error
, c
,
1785 c
->reader
= reader_callback
;
1789 gcry_randomize(c
->nonce
, sizeof c
->nonce
, GCRY_STRONG_RANDOM
);
1790 sink_printf(ev_writer_sink(c
->w
), "231 %d %s %s\n",
1792 config
->authorization_algorithm
,
1793 hex(c
->nonce
, sizeof c
->nonce
));
1797 int server_start(ev_source
*ev
, int pf
,
1798 size_t socklen
, const struct sockaddr
*sa
,
1801 struct listener
*l
= xmalloc(sizeof *l
);
1802 static const int one
= 1;
1804 D(("server_init socket %s", name
));
1805 fd
= xsocket(pf
, SOCK_STREAM
, 0);
1806 xsetsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &one
, sizeof one
);
1807 if(bind(fd
, sa
, socklen
) < 0) {
1808 error(errno
, "error binding to %s", name
);
1816 if(ev_listen(ev
, fd
, listen_callback
, l
, "server listener"))
1821 int server_stop(ev_source
*ev
, int fd
) {
1823 return ev_listen_cancel(ev
, fd
);