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
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"
77 # define NONCE_SIZE 16
81 # define CONFIRM_SIZE 10
84 int volume_left
, volume_right
; /* last known volume */
86 /** @brief Accept all well-formed login attempts
97 /** @brief One client connection */
99 /** @brief Read commands from here */
101 /** @brief Send responses to here */
103 /** @brief Underlying file descriptor */
105 /** @brief Unique identifier for connection used in log messages */
107 /** @brief Login name or NULL */
109 /** @brief Event loop */
111 /** @brief Nonce chosen for this connection */
112 unsigned char nonce
[NONCE_SIZE
];
113 /** @brief Current reader callback
115 * We change this depending on whether we're servicing the @b log command
117 ev_reader_callback
*reader
;
118 /** @brief Event log output sending to this connection */
119 struct eventlog_output
*lo
;
120 /** @brief Parent listener */
121 const struct listener
*l
;
122 /** @brief Login cookie or NULL */
124 /** @brief Connection rights */
126 /** @brief Next connection */
128 /** @brief True if pending rescan had 'wait' set */
132 /** @brief Linked list of connections */
133 static struct conn
*connections
;
135 static int reader_callback(ev_source
*ev
,
142 static const char *noyes
[] = { "no", "yes" };
144 /** @brief Remove a connection from the connection list */
145 static void remove_connection(struct conn
*c
) {
148 for(cc
= &connections
; *cc
&& *cc
!= c
; cc
= &(*cc
)->next
)
154 /** @brief Called when a connection's writer fails or is shut down
156 * If the connection still has a raeder that is cancelled.
158 static int writer_error(ev_source
attribute((unused
)) *ev
,
163 D(("server writer_error S%x %d", c
->tag
, errno_value
));
164 if(errno_value
== 0) {
166 D(("S%x writer completed", c
->tag
));
168 if(errno_value
!= EPIPE
)
169 error(errno_value
, "S%x write error on socket", c
->tag
);
171 D(("cancel reader"));
172 ev_reader_cancel(c
->r
);
175 D(("done cancel reader"));
179 remove_connection(c
);
183 /** @brief Called when a conncetion's reader fails or is shut down
185 * If connection still has a writer then it is closed.
187 static int reader_error(ev_source
attribute((unused
)) *ev
,
192 D(("server reader_error S%x %d", c
->tag
, errno_value
));
193 error(errno_value
, "S%x read error on socket", c
->tag
);
195 ev_writer_close(c
->w
);
199 remove_connection(c
);
203 static int c_disable(struct conn
*c
, char **vec
, int nvec
) {
205 disable_playing(c
->who
);
206 else if(nvec
== 1 && !strcmp(vec
[0], "now"))
207 disable_playing(c
->who
);
209 sink_writes(ev_writer_sink(c
->w
), "550 invalid argument\n");
210 return 1; /* completed */
212 sink_writes(ev_writer_sink(c
->w
), "250 OK\n");
213 return 1; /* completed */
216 static int c_enable(struct conn
*c
,
217 char attribute((unused
)) **vec
,
218 int attribute((unused
)) nvec
) {
219 enable_playing(c
->who
, c
->ev
);
220 /* Enable implicitly unpauses if there is nothing playing */
221 if(paused
&& !playing
) resume_playing(c
->who
);
222 sink_writes(ev_writer_sink(c
->w
), "250 OK\n");
223 return 1; /* completed */
226 static int c_enabled(struct conn
*c
,
227 char attribute((unused
)) **vec
,
228 int attribute((unused
)) nvec
) {
229 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", noyes
[playing_is_enabled()]);
230 return 1; /* completed */
233 static int c_play(struct conn
*c
, char **vec
,
234 int attribute((unused
)) nvec
) {
236 struct queue_entry
*q
;
238 if(!trackdb_exists(vec
[0])) {
239 sink_writes(ev_writer_sink(c
->w
), "550 track is not in database\n");
242 if(!(track
= trackdb_resolve(vec
[0]))) {
243 sink_writes(ev_writer_sink(c
->w
), "550 cannot resolve track\n");
246 q
= queue_add(track
, c
->who
, WHERE_BEFORE_RANDOM
);
248 /* If we added the first track, and something is playing, then prepare the
249 * new track. If nothing is playing then we don't bother as it wouldn't gain
251 if(q
== qhead
.next
&& playing
)
253 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", q
->id
);
254 /* If the queue was empty but we are for some reason paused then
256 if(!playing
) resume_playing(0);
258 return 1; /* completed */
261 static int c_remove(struct conn
*c
, char **vec
,
262 int attribute((unused
)) nvec
) {
263 struct queue_entry
*q
;
265 if(!(q
= queue_find(vec
[0]))) {
266 sink_writes(ev_writer_sink(c
->w
), "550 no such track on the queue\n");
269 if(!right_removable(c
->rights
, c
->who
, q
)) {
270 error(0, "%s attempted remove but lacks required rights", c
->who
);
271 sink_writes(ev_writer_sink(c
->w
),
272 "510 Not authorized to remove that track\n");
275 queue_remove(q
, c
->who
);
276 /* De-prepare the track. */
278 /* See about adding a new random track */
279 add_random_track(c
->ev
);
280 /* Prepare whatever the next head track is. */
281 if(qhead
.next
!= &qhead
)
282 prepare(c
->ev
, qhead
.next
);
284 sink_writes(ev_writer_sink(c
->w
), "250 removed\n");
285 return 1; /* completed */
288 static int c_scratch(struct conn
*c
,
292 sink_writes(ev_writer_sink(c
->w
), "250 nothing is playing\n");
293 return 1; /* completed */
295 /* TODO there is a bug here: if we specify an ID but it's not the currently
296 * playing track then you will get 550 if you weren't authorized to scratch
297 * the currently playing track. */
298 if(!right_scratchable(c
->rights
, c
->who
, playing
)) {
299 error(0, "%s attempted scratch but lacks required rights", c
->who
);
300 sink_writes(ev_writer_sink(c
->w
),
301 "510 Not authorized to scratch that track\n");
304 scratch(c
->who
, nvec
== 1 ? vec
[0] : 0);
305 /* If you scratch an unpaused track then it is automatically unpaused */
307 sink_writes(ev_writer_sink(c
->w
), "250 scratched\n");
308 return 1; /* completed */
311 static int c_pause(struct conn
*c
,
312 char attribute((unused
)) **vec
,
313 int attribute((unused
)) nvec
) {
315 sink_writes(ev_writer_sink(c
->w
), "250 nothing is playing\n");
316 return 1; /* completed */
319 sink_writes(ev_writer_sink(c
->w
), "250 already paused\n");
320 return 1; /* completed */
322 if(pause_playing(c
->who
) < 0)
323 sink_writes(ev_writer_sink(c
->w
), "550 cannot pause this track\n");
325 sink_writes(ev_writer_sink(c
->w
), "250 paused\n");
329 static int c_resume(struct conn
*c
,
330 char attribute((unused
)) **vec
,
331 int attribute((unused
)) nvec
) {
333 sink_writes(ev_writer_sink(c
->w
), "250 not paused\n");
334 return 1; /* completed */
336 resume_playing(c
->who
);
337 sink_writes(ev_writer_sink(c
->w
), "250 paused\n");
341 static int c_shutdown(struct conn
*c
,
342 char attribute((unused
)) **vec
,
343 int attribute((unused
)) nvec
) {
344 info("S%x shut down by %s", c
->tag
, c
->who
);
345 sink_writes(ev_writer_sink(c
->w
), "250 shutting down\n");
346 ev_writer_flush(c
->w
);
350 static int c_reconfigure(struct conn
*c
,
351 char attribute((unused
)) **vec
,
352 int attribute((unused
)) nvec
) {
353 info("S%x reconfigure by %s", c
->tag
, c
->who
);
354 if(reconfigure(c
->ev
, 1))
355 sink_writes(ev_writer_sink(c
->w
), "550 error reading new config\n");
357 sink_writes(ev_writer_sink(c
->w
), "250 installed new config\n");
358 return 1; /* completed */
361 static void finished_rescan(void *ru
) {
362 struct conn
*const c
= ru
;
364 sink_writes(ev_writer_sink(c
->w
), "250 rescan completed\n");
365 /* Turn this connection back on */
366 ev_reader_enable(c
->r
);
369 static void start_fresh_rescan(void *ru
) {
370 struct conn
*const c
= ru
;
372 if(trackdb_rescan_underway()) {
373 /* Some other waiter beat us to it. However in this case we're happy to
374 * piggyback; the requirement is that a new rescan be started, not that it
375 * was _our_ rescan. */
377 /* We block until the rescan completes */
378 trackdb_add_rescanned(finished_rescan
, c
);
380 /* We report that the new rescan has started */
381 sink_writes(ev_writer_sink(c
->w
), "250 rescan initiated\n");
382 /* Turn this connection back on */
383 ev_reader_enable(c
->r
);
386 /* We are the first connection to get a callback so we must start a
389 /* We want to block until the new rescan completes */
390 trackdb_rescan(c
->ev
, 1/*check*/, finished_rescan
, c
);
392 /* We can report back immediately */
393 trackdb_rescan(c
->ev
, 1/*check*/, 0, 0);
394 sink_writes(ev_writer_sink(c
->w
), "250 rescan initiated\n");
395 /* Turn this connection back on */
396 ev_reader_enable(c
->r
);
401 static int c_rescan(struct conn
*c
,
404 int wait
= 0, fresh
= 0, n
;
407 for(n
= 0; n
< nvec
; ++n
) {
408 if(!strcmp(vec
[n
], "wait"))
409 wait
= 1; /* wait for rescan to complete */
411 /* Currently disabled because untested (and hard to test). */
412 else if(!strcmp(vec
[n
], "fresh"))
413 fresh
= 1; /* don't piggyback underway rescan */
416 sink_writes(ev_writer_sink(c
->w
), "550 unknown flag\n");
417 return 1; /* completed */
420 /* Report what was requested */
421 info("S%x rescan by %s (%s %s)", c
->tag
, c
->who
,
423 fresh ?
"fresh" : "");
424 if(trackdb_rescan_underway()) {
426 /* We want a fresh rescan but there is already one underway. Arrange a
427 * callback when it completes and then set off a new one. */
428 c
->rescan_wait
= wait
;
429 trackdb_add_rescanned(start_fresh_rescan
, c
);
433 sink_writes(ev_writer_sink(c
->w
), "250 rescan queued\n");
437 /* There's a rescan underway, and it's acceptable to piggyback on it */
439 /* We want to block until completion. */
440 trackdb_add_rescanned(finished_rescan
, c
);
443 /* We don't want to block. So we just report that things are in
445 sink_writes(ev_writer_sink(c
->w
), "250 rescan already underway\n");
450 /* No rescan is underway. fresh is therefore irrelevant. */
452 /* We want to block until completion */
453 trackdb_rescan(c
->ev
, 1/*check*/, finished_rescan
, c
);
456 /* We don't want to block. */
457 trackdb_rescan(c
->ev
, 1/*check*/, 0, 0);
458 sink_writes(ev_writer_sink(c
->w
), "250 rescan initiated\n");
459 return 1; /* completed */
464 static int c_version(struct conn
*c
,
465 char attribute((unused
)) **vec
,
466 int attribute((unused
)) nvec
) {
467 /* VERSION had better only use the basic character set */
468 sink_printf(ev_writer_sink(c
->w
), "251 %s\n", disorder_short_version_string
);
469 return 1; /* completed */
472 static int c_playing(struct conn
*c
,
473 char attribute((unused
)) **vec
,
474 int attribute((unused
)) nvec
) {
476 queue_fix_sofar(playing
);
477 playing
->expected
= 0;
478 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", queue_marshall(playing
));
480 sink_printf(ev_writer_sink(c
->w
), "259 nothing playing\n");
481 return 1; /* completed */
484 static const char *connection_host(struct conn
*c
) {
487 struct sockaddr_in in
;
488 struct sockaddr_in6 in6
;
494 /* get connection data */
496 if(getpeername(c
->fd
, &u
.sa
, &l
) < 0) {
497 error(errno
, "S%x error calling getpeername", c
->tag
);
500 if(c
->l
->pf
!= PF_UNIX
) {
501 if((n
= getnameinfo(&u
.sa
, l
,
502 host
, sizeof host
, 0, 0, NI_NUMERICHOST
))) {
503 error(0, "S%x error calling getnameinfo: %s", c
->tag
, gai_strerror(n
));
506 return xstrdup(host
);
511 static int c_user(struct conn
*c
,
513 int attribute((unused
)) nvec
) {
515 const char *res
, *host
, *password
;
519 sink_writes(ev_writer_sink(c
->w
), "530 already authenticated\n");
522 /* get connection data */
523 if(!(host
= connection_host(c
))) {
524 sink_writes(ev_writer_sink(c
->w
), "530 authentication failure\n");
528 k
= trackdb_getuserinfo(vec
[0]);
529 /* reject nonexistent users */
531 error(0, "S%x unknown user '%s' from %s", c
->tag
, vec
[0], host
);
532 sink_writes(ev_writer_sink(c
->w
), "530 authentication failed\n");
535 /* reject unconfirmed users */
536 if(kvp_get(k
, "confirmation")) {
537 error(0, "S%x unconfirmed user '%s' from %s", c
->tag
, vec
[0], host
);
538 sink_writes(ev_writer_sink(c
->w
), "530 authentication failed\n");
541 password
= kvp_get(k
, "password");
542 if(!password
) password
= "";
543 if(parse_rights(kvp_get(k
, "rights"), &rights
, 1)) {
544 error(0, "error parsing rights for %s", vec
[0]);
545 sink_writes(ev_writer_sink(c
->w
), "530 authentication failed\n");
548 /* check whether the response is right */
549 res
= authhash(c
->nonce
, sizeof c
->nonce
, password
,
550 config
->authorization_algorithm
);
551 if(wideopen
|| (res
&& !strcmp(res
, vec
[1]))) {
554 /* currently we only bother logging remote connections */
555 if(strcmp(host
, "local"))
556 info("S%x %s connected from %s", c
->tag
, vec
[0], host
);
558 c
->rights
|= RIGHT__LOCAL
;
559 sink_writes(ev_writer_sink(c
->w
), "230 OK\n");
562 /* oops, response was wrong */
563 info("S%x authentication failure for %s from %s", c
->tag
, vec
[0], host
);
564 sink_writes(ev_writer_sink(c
->w
), "530 authentication failed\n");
568 static int c_recent(struct conn
*c
,
569 char attribute((unused
)) **vec
,
570 int attribute((unused
)) nvec
) {
571 const struct queue_entry
*q
;
573 sink_writes(ev_writer_sink(c
->w
), "253 Tracks follow\n");
574 for(q
= phead
.next
; q
!= &phead
; q
= q
->next
)
575 sink_printf(ev_writer_sink(c
->w
), " %s\n", queue_marshall(q
));
576 sink_writes(ev_writer_sink(c
->w
), ".\n");
577 return 1; /* completed */
580 static int c_queue(struct conn
*c
,
581 char attribute((unused
)) **vec
,
582 int attribute((unused
)) nvec
) {
583 struct queue_entry
*q
;
588 sink_writes(ev_writer_sink(c
->w
), "253 Tracks follow\n");
589 if(playing_is_enabled() && !paused
) {
591 queue_fix_sofar(playing
);
592 if((l
= trackdb_get(playing
->track
, "_length"))
593 && (length
= atol(l
))) {
595 when
+= length
- playing
->sofar
+ config
->gap
;
598 /* Nothing is playing but playing is enabled, so whatever is
599 * first in the queue can be expected to start immediately. */
602 for(q
= qhead
.next
; q
!= &qhead
; q
= q
->next
) {
603 /* fill in estimated start time */
605 sink_printf(ev_writer_sink(c
->w
), " %s\n", queue_marshall(q
));
606 /* update for next track */
608 if((l
= trackdb_get(q
->track
, "_length"))
609 && (length
= atol(l
)))
610 when
+= length
+ config
->gap
;
615 sink_writes(ev_writer_sink(c
->w
), ".\n");
616 return 1; /* completed */
619 static int output_list(struct conn
*c
, char **vec
) {
621 sink_printf(ev_writer_sink(c
->w
), "%s\n", *vec
++);
622 sink_writes(ev_writer_sink(c
->w
), ".\n");
626 static int files_dirs(struct conn
*c
,
629 enum trackdb_listable what
) {
630 const char *dir
, *re
, *errstr
;
636 case 0: dir
= 0; re
= 0; break;
637 case 1: dir
= vec
[0]; re
= 0; break;
638 case 2: dir
= vec
[0]; re
= vec
[1]; break;
641 /* A bit of a bodge to make sure the args don't trample on cache keys */
642 if(dir
&& strchr(dir
, '\n')) {
643 sink_writes(ev_writer_sink(c
->w
), "550 invalid directory name\n");
646 if(re
&& strchr(re
, '\n')) {
647 sink_writes(ev_writer_sink(c
->w
), "550 invalid regexp\n");
650 /* We bother eliminating "" because the web interface is relatively
651 * likely to send it */
653 byte_xasprintf(&key
, "%d\n%s\n%s", (int)what
, dir ? dir
: "", re
);
654 fvec
= (char **)cache_get(&cache_files_type
, key
);
656 /* Got a cache hit, don't store the answer in the cache */
659 rec
= 0; /* quieten compiler */
661 /* Cache miss, we'll do the lookup and key != 0 so we'll store the answer
663 if(!(rec
= pcre_compile(re
, PCRE_CASELESS
|PCRE_UTF8
,
664 &errstr
, &erroffset
, 0))) {
665 sink_printf(ev_writer_sink(c
->w
), "550 Error compiling regexp: %s\n",
669 /* It only counts as a miss if the regexp was valid. */
670 ++cache_files_misses
;
673 /* No regexp, don't bother caching the result */
679 /* No cache hit (either because a miss, or because we did not look) so do
682 fvec
= trackdb_list(dir
, 0, what
, rec
);
684 fvec
= trackdb_list(0, 0, what
, rec
);
687 /* Put the answer in the cache */
688 cache_put(&cache_files_type
, key
, fvec
);
689 sink_writes(ev_writer_sink(c
->w
), "253 Listing follow\n");
690 return output_list(c
, fvec
);
693 static int c_files(struct conn
*c
,
696 return files_dirs(c
, vec
, nvec
, trackdb_files
);
699 static int c_dirs(struct conn
*c
,
702 return files_dirs(c
, vec
, nvec
, trackdb_directories
);
705 static int c_allfiles(struct conn
*c
,
708 return files_dirs(c
, vec
, nvec
, trackdb_directories
|trackdb_files
);
711 static int c_get(struct conn
*c
,
713 int attribute((unused
)) nvec
) {
714 const char *v
, *track
;
716 if(!(track
= trackdb_resolve(vec
[0]))) {
717 sink_writes(ev_writer_sink(c
->w
), "550 cannot resolve track\n");
720 if(vec
[1][0] != '_' && (v
= trackdb_get(track
, vec
[1])))
721 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", quoteutf8(v
));
723 sink_writes(ev_writer_sink(c
->w
), "555 not found\n");
727 static int c_length(struct conn
*c
,
729 int attribute((unused
)) nvec
) {
730 const char *track
, *v
;
732 if(!(track
= trackdb_resolve(vec
[0]))) {
733 sink_writes(ev_writer_sink(c
->w
), "550 cannot resolve track\n");
736 if((v
= trackdb_get(track
, "_length")))
737 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", quoteutf8(v
));
739 sink_writes(ev_writer_sink(c
->w
), "550 not found\n");
743 static int c_set(struct conn
*c
,
745 int attribute((unused
)) nvec
) {
748 if(!(track
= trackdb_resolve(vec
[0]))) {
749 sink_writes(ev_writer_sink(c
->w
), "550 cannot resolve track\n");
752 if(vec
[1][0] != '_' && !trackdb_set(track
, vec
[1], vec
[2]))
753 sink_writes(ev_writer_sink(c
->w
), "250 OK\n");
755 sink_writes(ev_writer_sink(c
->w
), "550 not found\n");
759 static int c_prefs(struct conn
*c
,
761 int attribute((unused
)) nvec
) {
765 if(!(track
= trackdb_resolve(vec
[0]))) {
766 sink_writes(ev_writer_sink(c
->w
), "550 cannot resolve track\n");
769 k
= trackdb_get_all(track
);
770 sink_writes(ev_writer_sink(c
->w
), "253 prefs follow\n");
771 for(; k
; k
= k
->next
)
772 if(k
->name
[0] != '_') /* omit internal values */
773 sink_printf(ev_writer_sink(c
->w
),
774 " %s %s\n", quoteutf8(k
->name
), quoteutf8(k
->value
));
775 sink_writes(ev_writer_sink(c
->w
), ".\n");
779 static int c_exists(struct conn
*c
,
781 int attribute((unused
)) nvec
) {
782 /* trackdb_exists() does its own alias checking */
783 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", noyes
[trackdb_exists(vec
[0])]);
787 static void search_parse_error(const char *msg
, void *u
) {
788 *(const char **)u
= msg
;
791 static int c_search(struct conn
*c
,
793 int attribute((unused
)) nvec
) {
794 char **terms
, **results
;
795 int nterms
, nresults
, n
;
796 const char *e
= "unknown error";
798 /* This is a bit of a bodge. Initially it's there to make the eclient
799 * interface a bit more convenient to add searching to, but it has the more
800 * compelling advantage that if everything uses it, then interpretation of
801 * user-supplied search strings will be the same everywhere. */
802 if(!(terms
= split(vec
[0], &nterms
, SPLIT_QUOTES
, search_parse_error
, &e
))) {
803 sink_printf(ev_writer_sink(c
->w
), "550 %s\n", e
);
805 results
= trackdb_search(terms
, nterms
, &nresults
);
806 sink_printf(ev_writer_sink(c
->w
), "253 %d matches\n", nresults
);
807 for(n
= 0; n
< nresults
; ++n
)
808 sink_printf(ev_writer_sink(c
->w
), "%s\n", results
[n
]);
809 sink_writes(ev_writer_sink(c
->w
), ".\n");
814 static int c_random_enable(struct conn
*c
,
815 char attribute((unused
)) **vec
,
816 int attribute((unused
)) nvec
) {
817 enable_random(c
->who
, c
->ev
);
818 /* Enable implicitly unpauses if there is nothing playing */
819 if(paused
&& !playing
) resume_playing(c
->who
);
820 sink_writes(ev_writer_sink(c
->w
), "250 OK\n");
821 return 1; /* completed */
824 static int c_random_disable(struct conn
*c
,
825 char attribute((unused
)) **vec
,
826 int attribute((unused
)) nvec
) {
827 disable_random(c
->who
);
828 sink_writes(ev_writer_sink(c
->w
), "250 OK\n");
829 return 1; /* completed */
832 static int c_random_enabled(struct conn
*c
,
833 char attribute((unused
)) **vec
,
834 int attribute((unused
)) nvec
) {
835 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", noyes
[random_is_enabled()]);
836 return 1; /* completed */
839 static void got_stats(char *stats
, void *u
) {
840 struct conn
*const c
= u
;
842 sink_printf(ev_writer_sink(c
->w
), "253 stats\n%s\n.\n", stats
);
843 /* Now we can start processing commands again */
844 ev_reader_enable(c
->r
);
847 static int c_stats(struct conn
*c
,
848 char attribute((unused
)) **vec
,
849 int attribute((unused
)) nvec
) {
850 trackdb_stats_subprocess(c
->ev
, got_stats
, c
);
851 return 0; /* not yet complete */
854 static int c_volume(struct conn
*c
,
866 l
= r
= atoi(vec
[0]);
877 rights
= set ? RIGHT_VOLUME
: RIGHT_READ
;
878 if(!(c
->rights
& rights
)) {
879 error(0, "%s attempted to set volume but lacks required rights", c
->who
);
880 sink_writes(ev_writer_sink(c
->w
), "510 Prohibited\n");
883 if(mixer_control(-1/*as configured*/, &l
, &r
, set
))
884 sink_writes(ev_writer_sink(c
->w
), "550 error accessing mixer\n");
886 sink_printf(ev_writer_sink(c
->w
), "252 %d %d\n", l
, r
);
887 if(l
!= volume_left
|| r
!= volume_right
) {
890 snprintf(lb
, sizeof lb
, "%d", l
);
891 snprintf(rb
, sizeof rb
, "%d", r
);
892 eventlog("volume", lb
, rb
, (char *)0);
898 /** @brief Called when data arrives on a log connection
900 * We just discard all such data. The client may occasionally send data as a
903 static int logging_reader_callback(ev_source
attribute((unused
)) *ev
,
905 void attribute((unused
)) *ptr
,
907 int attribute((unused
)) eof
,
908 void attribute((unused
)) *u
) {
911 ev_reader_consume(reader
, bytes
);
913 /* Oops, that's all for now */
914 D(("logging reader eof"));
917 ev_writer_close(c
->w
);
921 remove_connection(c
);
926 static void logclient(const char *msg
, void *user
) {
927 struct conn
*c
= user
;
930 /* This connection has gone up in smoke for some reason */
931 eventlog_remove(c
->lo
);
934 sink_printf(ev_writer_sink(c
->w
), "%"PRIxMAX
" %s\n",
935 (uintmax_t)time(0), msg
);
938 static int c_log(struct conn
*c
,
939 char attribute((unused
)) **vec
,
940 int attribute((unused
)) nvec
) {
943 sink_writes(ev_writer_sink(c
->w
), "254 OK\n");
944 /* pump out initial state */
946 sink_printf(ev_writer_sink(c
->w
), "%"PRIxMAX
" state %s\n",
948 playing_is_enabled() ?
"enable_play" : "disable_play");
949 sink_printf(ev_writer_sink(c
->w
), "%"PRIxMAX
" state %s\n",
951 random_is_enabled() ?
"enable_random" : "disable_random");
952 sink_printf(ev_writer_sink(c
->w
), "%"PRIxMAX
" state %s\n",
954 paused ?
"pause" : "resume");
956 sink_printf(ev_writer_sink(c
->w
), "%"PRIxMAX
" state playing\n",
959 sink_printf(ev_writer_sink(c
->w
), "%"PRIxMAX
" volume %d %d\n",
960 (uintmax_t)now
, volume_left
, volume_right
);
961 c
->lo
= xmalloc(sizeof *c
->lo
);
962 c
->lo
->fn
= logclient
;
965 c
->reader
= logging_reader_callback
;
969 /** @brief Test whether a move is allowed
970 * @param c Connection
971 * @param qs List of IDs on queue
972 * @param nqs Number of IDs
973 * @return 0 if move is prohibited, non-0 if it is allowed
975 static int has_move_rights(struct conn
*c
, struct queue_entry
**qs
, int nqs
) {
976 for(; nqs
> 0; ++qs
, --nqs
) {
977 struct queue_entry
*const q
= *qs
;
979 if(!right_movable(c
->rights
, c
->who
, q
))
985 static int c_move(struct conn
*c
,
987 int attribute((unused
)) nvec
) {
988 struct queue_entry
*q
;
991 if(!(q
= queue_find(vec
[0]))) {
992 sink_writes(ev_writer_sink(c
->w
), "550 no such track on the queue\n");
995 if(!has_move_rights(c
, &q
, 1)) {
996 error(0, "%s attempted move but lacks required rights", c
->who
);
997 sink_writes(ev_writer_sink(c
->w
),
998 "510 Not authorized to move that track\n");
1001 n
= queue_move(q
, atoi(vec
[1]), c
->who
);
1002 sink_printf(ev_writer_sink(c
->w
), "252 %d\n", n
);
1003 /* If we've moved to the head of the queue then prepare the track. */
1009 static int c_moveafter(struct conn
*c
,
1011 int attribute((unused
)) nvec
) {
1012 struct queue_entry
*q
, **qs
;
1016 if(!(q
= queue_find(vec
[0]))) {
1017 sink_writes(ev_writer_sink(c
->w
), "550 no such track on the queue\n");
1024 qs
= xcalloc(nvec
, sizeof *qs
);
1025 for(n
= 0; n
< nvec
; ++n
)
1026 if(!(qs
[n
] = queue_find(vec
[n
]))) {
1027 sink_writes(ev_writer_sink(c
->w
), "550 no such track on the queue\n");
1030 if(!has_move_rights(c
, qs
, nvec
)) {
1031 error(0, "%s attempted moveafter but lacks required rights", c
->who
);
1032 sink_writes(ev_writer_sink(c
->w
),
1033 "510 Not authorized to move those tracks\n");
1036 queue_moveafter(q
, nvec
, qs
, c
->who
);
1037 sink_printf(ev_writer_sink(c
->w
), "250 Moved tracks\n");
1038 /* If we've moved to the head of the queue then prepare the track. */
1044 static int c_part(struct conn
*c
,
1046 int attribute((unused
)) nvec
) {
1049 if(!(track
= trackdb_resolve(vec
[0]))) {
1050 sink_writes(ev_writer_sink(c
->w
), "550 cannot resolve track\n");
1053 sink_printf(ev_writer_sink(c
->w
), "252 %s\n",
1054 quoteutf8(trackdb_getpart(track
, vec
[1], vec
[2])));
1058 static int c_resolve(struct conn
*c
,
1060 int attribute((unused
)) nvec
) {
1063 if(!(track
= trackdb_resolve(vec
[0]))) {
1064 sink_writes(ev_writer_sink(c
->w
), "550 cannot resolve track\n");
1067 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", quoteutf8(track
));
1071 static int c_tags(struct conn
*c
,
1072 char attribute((unused
)) **vec
,
1073 int attribute((unused
)) nvec
) {
1074 char **tags
= trackdb_alltags();
1076 sink_printf(ev_writer_sink(c
->w
), "253 Tag list follows\n");
1078 sink_printf(ev_writer_sink(c
->w
), "%s%s\n",
1079 **tags
== '.' ?
"." : "", *tags
);
1082 sink_writes(ev_writer_sink(c
->w
), ".\n");
1083 return 1; /* completed */
1086 static int c_set_global(struct conn
*c
,
1088 int attribute((unused
)) nvec
) {
1089 if(vec
[0][0] == '_') {
1090 sink_writes(ev_writer_sink(c
->w
), "550 cannot set internal global preferences\n");
1093 trackdb_set_global(vec
[0], vec
[1], c
->who
);
1094 sink_printf(ev_writer_sink(c
->w
), "250 OK\n");
1098 static int c_get_global(struct conn
*c
,
1100 int attribute((unused
)) nvec
) {
1101 const char *s
= trackdb_get_global(vec
[0]);
1104 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", quoteutf8(s
));
1106 sink_writes(ev_writer_sink(c
->w
), "555 not found\n");
1110 static int c_nop(struct conn
*c
,
1111 char attribute((unused
)) **vec
,
1112 int attribute((unused
)) nvec
) {
1113 sink_printf(ev_writer_sink(c
->w
), "250 Quack\n");
1117 static int c_new(struct conn
*c
,
1127 if(max
<= 0 || max
> config
->new_max
)
1128 max
= config
->new_max
;
1129 tracks
= trackdb_new(0, max
);
1130 sink_printf(ev_writer_sink(c
->w
), "253 New track list follows\n");
1133 sink_printf(ev_writer_sink(c
->w
), "%s%s\n",
1134 **tracks
== '.' ?
"." : "", *tracks
);
1137 sink_writes(ev_writer_sink(c
->w
), ".\n");
1138 return 1; /* completed */
1142 static int c_rtp_address(struct conn
*c
,
1143 char attribute((unused
)) **vec
,
1144 int attribute((unused
)) nvec
) {
1145 if(config
->api
== BACKEND_NETWORK
) {
1146 sink_printf(ev_writer_sink(c
->w
), "252 %s %s\n",
1147 quoteutf8(config
->broadcast
.s
[0]),
1148 quoteutf8(config
->broadcast
.s
[1]));
1150 sink_writes(ev_writer_sink(c
->w
), "550 No RTP\n");
1154 static int c_cookie(struct conn
*c
,
1156 int attribute((unused
)) nvec
) {
1161 /* Can't log in twice on the same connection */
1163 sink_writes(ev_writer_sink(c
->w
), "530 already authenticated\n");
1166 /* Get some kind of peer identifcation */
1167 if(!(host
= connection_host(c
))) {
1168 sink_writes(ev_writer_sink(c
->w
), "530 authentication failure\n");
1171 /* Check the cookie */
1172 user
= verify_cookie(vec
[0], &rights
);
1174 sink_writes(ev_writer_sink(c
->w
), "530 authentication failure\n");
1181 if(strcmp(host
, "local"))
1182 info("S%x %s connected with cookie from %s", c
->tag
, user
, host
);
1184 c
->rights
|= RIGHT__LOCAL
;
1185 /* Response contains username so client knows who they are acting as */
1186 sink_printf(ev_writer_sink(c
->w
), "232 %s\n", quoteutf8(user
));
1190 static int c_make_cookie(struct conn
*c
,
1191 char attribute((unused
)) **vec
,
1192 int attribute((unused
)) nvec
) {
1193 const char *cookie
= make_cookie(c
->who
);
1196 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", quoteutf8(cookie
));
1198 sink_writes(ev_writer_sink(c
->w
), "550 Cannot create cookie\n");
1202 static int c_revoke(struct conn
*c
,
1203 char attribute((unused
)) **vec
,
1204 int attribute((unused
)) nvec
) {
1206 revoke_cookie(c
->cookie
);
1207 sink_writes(ev_writer_sink(c
->w
), "250 OK\n");
1209 sink_writes(ev_writer_sink(c
->w
), "550 Did not log in with cookie\n");
1213 static int c_adduser(struct conn
*c
,
1218 if(!config
->remote_userman
&& !(c
->rights
& RIGHT__LOCAL
)) {
1219 error(0, "S%x: remote adduser", c
->tag
);
1220 sink_writes(ev_writer_sink(c
->w
), "550 Remote user management is disabled\n");
1225 if(parse_rights(vec
[2], 0, 1)) {
1226 sink_writes(ev_writer_sink(c
->w
), "550 Invalid rights list\n");
1230 rights
= config
->default_rights
;
1231 if(trackdb_adduser(vec
[0], vec
[1], rights
,
1232 0/*email*/, 0/*confirmation*/))
1233 sink_writes(ev_writer_sink(c
->w
), "550 Cannot create user\n");
1235 sink_writes(ev_writer_sink(c
->w
), "250 User created\n");
1239 static int c_deluser(struct conn
*c
,
1241 int attribute((unused
)) nvec
) {
1244 if(!config
->remote_userman
&& !(c
->rights
& RIGHT__LOCAL
)) {
1245 error(0, "S%x: remote deluser", c
->tag
);
1246 sink_writes(ev_writer_sink(c
->w
), "550 Remote user management is disabled\n");
1249 if(trackdb_deluser(vec
[0])) {
1250 sink_writes(ev_writer_sink(c
->w
), "550 Cannot delete user\n");
1253 /* Zap connections belonging to deleted user */
1254 for(d
= connections
; d
; d
= d
->next
)
1255 if(!strcmp(d
->who
, vec
[0]))
1257 sink_writes(ev_writer_sink(c
->w
), "250 User deleted\n");
1261 static int c_edituser(struct conn
*c
,
1263 int attribute((unused
)) nvec
) {
1266 if(!config
->remote_userman
&& !(c
->rights
& RIGHT__LOCAL
)) {
1267 error(0, "S%x: remote edituser", c
->tag
);
1268 sink_writes(ev_writer_sink(c
->w
), "550 Remote user management is disabled\n");
1271 /* RIGHT_ADMIN can do anything; otherwise you can only set your own email
1272 * address and password. */
1273 if((c
->rights
& RIGHT_ADMIN
)
1274 || (!strcmp(c
->who
, vec
[0])
1275 && (!strcmp(vec
[1], "email")
1276 || !strcmp(vec
[1], "password")))) {
1277 if(trackdb_edituserinfo(vec
[0], vec
[1], vec
[2])) {
1278 sink_writes(ev_writer_sink(c
->w
), "550 Failed to change setting\n");
1281 if(!strcmp(vec
[1], "password")) {
1282 /* Zap all connections for this user after a password change */
1283 for(d
= connections
; d
; d
= d
->next
)
1284 if(!strcmp(d
->who
, vec
[0]))
1286 } else if(!strcmp(vec
[1], "rights")) {
1287 /* Update rights for this user */
1290 if(parse_rights(vec
[2], &r
, 1))
1291 for(d
= connections
; d
; d
= d
->next
)
1292 if(!strcmp(d
->who
, vec
[0]))
1295 sink_writes(ev_writer_sink(c
->w
), "250 OK\n");
1297 error(0, "%s attempted edituser but lacks required rights", c
->who
);
1298 sink_writes(ev_writer_sink(c
->w
), "510 Restricted to administrators\n");
1303 static int c_userinfo(struct conn
*c
,
1304 char attribute((unused
)) **vec
,
1305 int attribute((unused
)) nvec
) {
1309 /* We allow remote querying of rights so that clients can figure out what
1310 * they're allowed to do */
1311 if(!config
->remote_userman
1312 && !(c
->rights
& RIGHT__LOCAL
)
1313 && strcmp(vec
[1], "rights")) {
1314 error(0, "S%x: remote userinfo %s %s", c
->tag
, vec
[0], vec
[1]);
1315 sink_writes(ev_writer_sink(c
->w
), "550 Remote user management is disabled\n");
1318 /* RIGHT_ADMIN allows anything; otherwise you can only get your own email
1319 * address and rights list. */
1320 if((c
->rights
& RIGHT_ADMIN
)
1321 || (!strcmp(c
->who
, vec
[0])
1322 && (!strcmp(vec
[1], "email")
1323 || !strcmp(vec
[1], "rights")))) {
1324 if((k
= trackdb_getuserinfo(vec
[0])))
1325 if((value
= kvp_get(k
, vec
[1])))
1326 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", quoteutf8(value
));
1328 sink_writes(ev_writer_sink(c
->w
), "555 Not set\n");
1330 sink_writes(ev_writer_sink(c
->w
), "550 No such user\n");
1332 error(0, "%s attempted userinfo but lacks required rights", c
->who
);
1333 sink_writes(ev_writer_sink(c
->w
), "510 Restricted to administrators\n");
1338 static int c_users(struct conn
*c
,
1339 char attribute((unused
)) **vec
,
1340 int attribute((unused
)) nvec
) {
1341 /* TODO de-dupe with c_tags */
1342 char **users
= trackdb_listusers();
1344 sink_writes(ev_writer_sink(c
->w
), "253 User list follows\n");
1346 sink_printf(ev_writer_sink(c
->w
), "%s%s\n",
1347 **users
== '.' ?
"." : "", *users
);
1350 sink_writes(ev_writer_sink(c
->w
), ".\n");
1351 return 1; /* completed */
1354 /** @brief Base64 mapping table for confirmation strings
1356 * This is used with generic_to_base64() and generic_base64(). We cannot use
1357 * the MIME table as that contains '+' and '=' which get quoted when
1358 * URL-encoding. (The CGI still does the URL encoding but it is desirable to
1359 * avoid it being necessary.)
1361 static const char confirm_base64_table
[] =
1362 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789/.*";
1364 static int c_register(struct conn
*c
,
1366 int attribute((unused
)) nvec
) {
1371 /* The confirmation string is base64(username;nonce) */
1372 bufsize
= strlen(vec
[0]) + CONFIRM_SIZE
+ 2;
1373 buf
= xmalloc_noptr(bufsize
);
1374 offset
= byte_snprintf(buf
, bufsize
, "%s;", vec
[0]);
1375 gcry_randomize(buf
+ offset
, CONFIRM_SIZE
, GCRY_STRONG_RANDOM
);
1376 cs
= generic_to_base64((uint8_t *)buf
, offset
+ CONFIRM_SIZE
,
1377 confirm_base64_table
);
1378 if(trackdb_adduser(vec
[0], vec
[1], config
->default_rights
, vec
[2], cs
))
1379 sink_writes(ev_writer_sink(c
->w
), "550 Cannot create user\n");
1381 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", quoteutf8(cs
));
1385 static int c_confirm(struct conn
*c
,
1387 int attribute((unused
)) nvec
) {
1393 /* Get some kind of peer identifcation */
1394 if(!(host
= connection_host(c
))) {
1395 sink_writes(ev_writer_sink(c
->w
), "530 Authentication failure\n");
1398 if(!(user
= generic_base64(vec
[0], &nuser
, confirm_base64_table
))
1399 || !(sep
= memchr(user
, ';', nuser
))) {
1400 sink_writes(ev_writer_sink(c
->w
), "550 Malformed confirmation string\n");
1404 if(trackdb_confirm(user
, vec
[0], &rights
))
1405 sink_writes(ev_writer_sink(c
->w
), "550 Incorrect confirmation string\n");
1410 if(strcmp(host
, "local"))
1411 info("S%x %s confirmed from %s", c
->tag
, user
, host
);
1413 c
->rights
|= RIGHT__LOCAL
;
1414 /* Response contains username so client knows who they are acting as */
1415 sink_printf(ev_writer_sink(c
->w
), "232 %s\n", quoteutf8(user
));
1420 static int sent_reminder(ev_source
attribute((unused
)) *ev
,
1421 pid_t
attribute((unused
)) pid
,
1423 const struct rusage
attribute((unused
)) *rusage
,
1425 struct conn
*const c
= u
;
1427 /* Tell the client what went down */
1429 sink_writes(ev_writer_sink(c
->w
), "250 OK\n");
1431 error(0, "reminder subprocess %s", wstat(status
));
1432 sink_writes(ev_writer_sink(c
->w
), "550 Cannot send a reminder email\n");
1434 /* Re-enable this connection */
1435 ev_reader_enable(c
->r
);
1439 static int c_reminder(struct conn
*c
,
1441 int attribute((unused
)) nvec
) {
1443 const char *password
, *email
, *text
, *encoding
, *charset
, *content_type
;
1448 static hash
*last_reminder
;
1450 if(!config
->mail_sender
) {
1451 error(0, "cannot send password reminders because mail_sender not set");
1452 sink_writes(ev_writer_sink(c
->w
), "550 Cannot send a reminder email\n");
1455 if(!(k
= trackdb_getuserinfo(vec
[0]))) {
1456 error(0, "reminder for user '%s' who does not exist", vec
[0]);
1457 sink_writes(ev_writer_sink(c
->w
), "550 Cannot send a reminder email\n");
1460 if(!(email
= kvp_get(k
, "email"))
1461 || !strchr(email
, '@')) {
1462 error(0, "user '%s' has no valid email address", vec
[0]);
1463 sink_writes(ev_writer_sink(c
->w
), "550 Cannot send a reminder email\n");
1466 if(!(password
= kvp_get(k
, "password"))
1468 error(0, "user '%s' has no password", vec
[0]);
1469 sink_writes(ev_writer_sink(c
->w
), "550 Cannot send a reminder email\n");
1472 /* Rate-limit reminders. This hash is bounded in size by the number of
1473 * users. If this is actually a problem for anyone then we can periodically
1476 last_reminder
= hash_new(sizeof (time_t));
1477 last
= hash_find(last_reminder
, vec
[0]);
1479 if(last
&& now
< *last
+ config
->reminder_interval
) {
1480 error(0, "sent a password reminder to '%s' too recently", vec
[0]);
1481 sink_writes(ev_writer_sink(c
->w
), "550 Cannot send a reminder email\n");
1484 /* Send the reminder */
1485 /* TODO this should be templatized and to some extent merged with
1486 * the code in act_register() */
1487 byte_xasprintf((char **)&text
,
1488 "Someone requested that you be sent a reminder of your DisOrder password.\n"
1489 "Your password is:\n"
1492 if(!(text
= mime_encode_text(text
, &charset
, &encoding
)))
1493 fatal(0, "cannot encode email");
1494 byte_xasprintf((char **)&content_type
, "text/plain;charset=%s",
1495 quote822(charset
, 0));
1496 pid
= sendmail_subprocess("", config
->mail_sender
, email
,
1497 "DisOrder password reminder",
1498 encoding
, content_type
, text
);
1500 sink_writes(ev_writer_sink(c
->w
), "550 Cannot send a reminder email\n");
1503 hash_add(last_reminder
, vec
[0], &now
, HASH_INSERT_OR_REPLACE
);
1504 info("sending a passsword reminder to user '%s'", vec
[0]);
1505 /* We can only continue when the subprocess finishes */
1506 ev_child(c
->ev
, pid
, 0, sent_reminder
, c
);
1510 static int c_schedule_list(struct conn
*c
,
1511 char attribute((unused
)) **vec
,
1512 int attribute((unused
)) nvec
) {
1513 char **ids
= schedule_list(0);
1514 sink_writes(ev_writer_sink(c
->w
), "253 ID list follows\n");
1516 sink_printf(ev_writer_sink(c
->w
), "%s\n", *ids
++);
1517 sink_writes(ev_writer_sink(c
->w
), ".\n");
1518 return 1; /* completed */
1521 static int c_schedule_get(struct conn
*c
,
1523 int attribute((unused
)) nvec
) {
1524 struct kvp
*actiondata
= schedule_get(vec
[0]), *k
;
1527 sink_writes(ev_writer_sink(c
->w
), "555 No such event\n");
1528 return 1; /* completed */
1530 /* Scheduled events are public information. Anyone with RIGHT_READ can see
1532 sink_writes(ev_writer_sink(c
->w
), "253 Event information follows\n");
1533 for(k
= actiondata
; k
; k
= k
->next
)
1534 sink_printf(ev_writer_sink(c
->w
), " %s %s\n",
1535 quoteutf8(k
->name
), quoteutf8(k
->value
));
1536 sink_writes(ev_writer_sink(c
->w
), ".\n");
1537 return 1; /* completed */
1540 static int c_schedule_del(struct conn
*c
,
1542 int attribute((unused
)) nvec
) {
1543 struct kvp
*actiondata
= schedule_get(vec
[0]);
1546 sink_writes(ev_writer_sink(c
->w
), "555 No such event\n");
1547 return 1; /* completed */
1549 /* If you have admin rights you can delete anything. If you don't then you
1550 * can only delete your own scheduled events. */
1551 if(!(c
->rights
& RIGHT_ADMIN
)) {
1552 const char *who
= kvp_get(actiondata
, "who");
1554 if(!who
|| !c
->who
|| strcmp(who
, c
->who
)) {
1555 sink_writes(ev_writer_sink(c
->w
), "551 Not authorized\n");
1556 return 1; /* completed */
1559 if(schedule_del(vec
[0]))
1560 sink_writes(ev_writer_sink(c
->w
), "550 Could not delete scheduled event\n");
1562 sink_writes(ev_writer_sink(c
->w
), "250 Deleted\n");
1563 return 1; /* completed */
1566 static int c_schedule_add(struct conn
*c
,
1569 struct kvp
*actiondata
= 0;
1572 /* Standard fields */
1573 kvp_set(&actiondata
, "who", c
->who
);
1574 kvp_set(&actiondata
, "when", vec
[0]);
1575 kvp_set(&actiondata
, "priority", vec
[1]);
1576 kvp_set(&actiondata
, "action", vec
[2]);
1577 /* Action-dependent fields */
1578 if(!strcmp(vec
[2], "play")) {
1580 sink_writes(ev_writer_sink(c
->w
), "550 Wrong number of arguments\n");
1583 if(!trackdb_exists(vec
[3])) {
1584 sink_writes(ev_writer_sink(c
->w
), "550 Track is not in database\n");
1587 kvp_set(&actiondata
, "track", vec
[3]);
1588 } else if(!strcmp(vec
[2], "set-global")) {
1589 if(nvec
< 4 || nvec
> 5) {
1590 sink_writes(ev_writer_sink(c
->w
), "550 Wrong number of arguments\n");
1593 kvp_set(&actiondata
, "key", vec
[3]);
1595 kvp_set(&actiondata
, "value", vec
[4]);
1597 sink_writes(ev_writer_sink(c
->w
), "550 Unknown action\n");
1600 /* schedule_add() checks user rights */
1601 id
= schedule_add(c
->ev
, actiondata
);
1603 sink_writes(ev_writer_sink(c
->w
), "550 Cannot add scheduled event\n");
1605 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", id
);
1609 static const struct command
{
1610 /** @brief Command name */
1613 /** @brief Minimum number of arguments */
1616 /** @brief Maximum number of arguments */
1619 /** @brief Function to process command */
1620 int (*fn
)(struct conn
*, char **, int);
1622 /** @brief Rights required to execute command
1624 * 0 means that the command can be issued without logging in. If multiple
1625 * bits are listed here any of those rights will do.
1629 { "adduser", 2, 3, c_adduser
, RIGHT_ADMIN
|RIGHT__LOCAL
},
1630 { "allfiles", 0, 2, c_allfiles
, RIGHT_READ
},
1631 { "confirm", 1, 1, c_confirm
, 0 },
1632 { "cookie", 1, 1, c_cookie
, 0 },
1633 { "deluser", 1, 1, c_deluser
, RIGHT_ADMIN
|RIGHT__LOCAL
},
1634 { "dirs", 0, 2, c_dirs
, RIGHT_READ
},
1635 { "disable", 0, 1, c_disable
, RIGHT_GLOBAL_PREFS
},
1636 { "edituser", 3, 3, c_edituser
, RIGHT_ADMIN
|RIGHT_USERINFO
},
1637 { "enable", 0, 0, c_enable
, RIGHT_GLOBAL_PREFS
},
1638 { "enabled", 0, 0, c_enabled
, RIGHT_READ
},
1639 { "exists", 1, 1, c_exists
, RIGHT_READ
},
1640 { "files", 0, 2, c_files
, RIGHT_READ
},
1641 { "get", 2, 2, c_get
, RIGHT_READ
},
1642 { "get-global", 1, 1, c_get_global
, RIGHT_READ
},
1643 { "length", 1, 1, c_length
, RIGHT_READ
},
1644 { "log", 0, 0, c_log
, RIGHT_READ
},
1645 { "make-cookie", 0, 0, c_make_cookie
, RIGHT_READ
},
1646 { "move", 2, 2, c_move
, RIGHT_MOVE__MASK
},
1647 { "moveafter", 1, INT_MAX
, c_moveafter
, RIGHT_MOVE__MASK
},
1648 { "new", 0, 1, c_new
, RIGHT_READ
},
1649 { "nop", 0, 0, c_nop
, 0 },
1650 { "part", 3, 3, c_part
, RIGHT_READ
},
1651 { "pause", 0, 0, c_pause
, RIGHT_PAUSE
},
1652 { "play", 1, 1, c_play
, RIGHT_PLAY
},
1653 { "playing", 0, 0, c_playing
, RIGHT_READ
},
1654 { "prefs", 1, 1, c_prefs
, RIGHT_READ
},
1655 { "queue", 0, 0, c_queue
, RIGHT_READ
},
1656 { "random-disable", 0, 0, c_random_disable
, RIGHT_GLOBAL_PREFS
},
1657 { "random-enable", 0, 0, c_random_enable
, RIGHT_GLOBAL_PREFS
},
1658 { "random-enabled", 0, 0, c_random_enabled
, RIGHT_READ
},
1659 { "recent", 0, 0, c_recent
, RIGHT_READ
},
1660 { "reconfigure", 0, 0, c_reconfigure
, RIGHT_ADMIN
},
1661 { "register", 3, 3, c_register
, RIGHT_REGISTER
|RIGHT__LOCAL
},
1662 { "reminder", 1, 1, c_reminder
, RIGHT__LOCAL
},
1663 { "remove", 1, 1, c_remove
, RIGHT_REMOVE__MASK
},
1664 { "rescan", 0, INT_MAX
, c_rescan
, RIGHT_RESCAN
},
1665 { "resolve", 1, 1, c_resolve
, RIGHT_READ
},
1666 { "resume", 0, 0, c_resume
, RIGHT_PAUSE
},
1667 { "revoke", 0, 0, c_revoke
, RIGHT_READ
},
1668 { "rtp-address", 0, 0, c_rtp_address
, 0 },
1669 { "schedule-add", 3, INT_MAX
, c_schedule_add
, RIGHT_READ
},
1670 { "schedule-del", 1, 1, c_schedule_del
, RIGHT_READ
},
1671 { "schedule-get", 1, 1, c_schedule_get
, RIGHT_READ
},
1672 { "schedule-list", 0, 0, c_schedule_list
, RIGHT_READ
},
1673 { "scratch", 0, 1, c_scratch
, RIGHT_SCRATCH__MASK
},
1674 { "search", 1, 1, c_search
, RIGHT_READ
},
1675 { "set", 3, 3, c_set
, RIGHT_PREFS
, },
1676 { "set-global", 2, 2, c_set_global
, RIGHT_GLOBAL_PREFS
},
1677 { "shutdown", 0, 0, c_shutdown
, RIGHT_ADMIN
},
1678 { "stats", 0, 0, c_stats
, RIGHT_READ
},
1679 { "tags", 0, 0, c_tags
, RIGHT_READ
},
1680 { "unset", 2, 2, c_set
, RIGHT_PREFS
},
1681 { "unset-global", 1, 1, c_set_global
, RIGHT_GLOBAL_PREFS
},
1682 { "user", 2, 2, c_user
, 0 },
1683 { "userinfo", 2, 2, c_userinfo
, RIGHT_READ
},
1684 { "users", 0, 0, c_users
, RIGHT_READ
},
1685 { "version", 0, 0, c_version
, RIGHT_READ
},
1686 { "volume", 0, 2, c_volume
, RIGHT_READ
|RIGHT_VOLUME
}
1689 static void command_error(const char *msg
, void *u
) {
1692 sink_printf(ev_writer_sink(c
->w
), "500 parse error: %s\n", msg
);
1695 /* process a command. Return 1 if complete, 0 if incomplete. */
1696 static int command(struct conn
*c
, char *line
) {
1700 D(("server command %s", line
));
1701 /* We force everything into NFC as early as possible */
1702 if(!(line
= utf8_compose_canon(line
, strlen(line
), 0))) {
1703 sink_writes(ev_writer_sink(c
->w
), "500 cannot normalize command\n");
1706 if(!(vec
= split(line
, &nvec
, SPLIT_QUOTES
, command_error
, c
))) {
1707 sink_writes(ev_writer_sink(c
->w
), "500 cannot parse command\n");
1711 sink_writes(ev_writer_sink(c
->w
), "500 do what?\n");
1714 if((n
= TABLE_FIND(commands
, struct command
, name
, vec
[0])) < 0)
1715 sink_writes(ev_writer_sink(c
->w
), "500 unknown command\n");
1717 if(commands
[n
].rights
1718 && !(c
->rights
& commands
[n
].rights
)) {
1719 error(0, "%s attempted %s but lacks required rights", c
->who ? c
->who
: "NULL",
1721 sink_writes(ev_writer_sink(c
->w
), "510 Prohibited\n");
1726 if(nvec
< commands
[n
].minargs
) {
1727 sink_writes(ev_writer_sink(c
->w
), "500 missing argument(s)\n");
1730 if(nvec
> commands
[n
].maxargs
) {
1731 sink_writes(ev_writer_sink(c
->w
), "500 too many arguments\n");
1734 return commands
[n
].fn(c
, vec
, nvec
);
1736 return 1; /* completed */
1739 /* redirect to the right reader callback for our current state */
1740 static int redirect_reader_callback(ev_source
*ev
,
1748 return c
->reader(ev
, reader
, ptr
, bytes
, eof
, u
);
1751 /* the main command reader */
1752 static int reader_callback(ev_source
attribute((unused
)) *ev
,
1762 D(("server reader_callback"));
1763 while((eol
= memchr(ptr
, '\n', bytes
))) {
1765 ev_reader_consume(reader
, eol
- (char *)ptr
);
1766 complete
= command(c
, ptr
);
1767 bytes
-= (eol
- (char *)ptr
);
1770 /* the command had better have set a new reader callback */
1772 /* there are further bytes to read, or we are at eof; arrange for the
1773 * command's reader callback to handle them */
1774 return ev_reader_incomplete(reader
);
1775 /* nothing's going on right now */
1778 /* command completed, we can go around and handle the next one */
1782 error(0, "S%x unterminated line", c
->tag
);
1783 D(("normal reader close"));
1786 D(("close associated writer"));
1787 ev_writer_close(c
->w
);
1790 remove_connection(c
);
1795 static int listen_callback(ev_source
*ev
,
1797 const struct sockaddr
attribute((unused
)) *remote
,
1798 socklen_t
attribute((unused
)) rlen
,
1800 const struct listener
*l
= u
;
1801 struct conn
*c
= xmalloc(sizeof *c
);
1802 static unsigned tags
;
1804 D(("server listen_callback fd %d (%s)", fd
, l
->name
));
1809 c
->w
= ev_writer_new(ev
, fd
, writer_error
, c
,
1811 c
->r
= ev_reader_new(ev
, fd
, redirect_reader_callback
, reader_error
, c
,
1815 c
->reader
= reader_callback
;
1818 gcry_randomize(c
->nonce
, sizeof c
->nonce
, GCRY_STRONG_RANDOM
);
1819 sink_printf(ev_writer_sink(c
->w
), "231 %d %s %s\n",
1821 config
->authorization_algorithm
,
1822 hex(c
->nonce
, sizeof c
->nonce
));
1826 int server_start(ev_source
*ev
, int pf
,
1827 size_t socklen
, const struct sockaddr
*sa
,
1830 struct listener
*l
= xmalloc(sizeof *l
);
1831 static const int one
= 1;
1833 D(("server_init socket %s", name
));
1834 fd
= xsocket(pf
, SOCK_STREAM
, 0);
1835 xsetsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &one
, sizeof one
);
1836 if(bind(fd
, sa
, socklen
) < 0) {
1837 error(errno
, "error binding to %s", name
);
1845 if(ev_listen(ev
, fd
, listen_callback
, l
, "server listener"))
1850 int server_stop(ev_source
*ev
, int fd
) {
1852 return ev_listen_cancel(ev
, fd
);