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"
76 # define NONCE_SIZE 16
80 # define CONFIRM_SIZE 10
83 int volume_left
, volume_right
; /* last known volume */
85 /** @brief Accept all well-formed login attempts
96 /** @brief One client connection */
98 /** @brief Read commands from here */
100 /** @brief Send responses to here */
102 /** @brief Underlying file descriptor */
104 /** @brief Unique identifier for connection used in log messages */
106 /** @brief Login name or NULL */
108 /** @brief Event loop */
110 /** @brief Nonce chosen for this connection */
111 unsigned char nonce
[NONCE_SIZE
];
112 /** @brief Current reader callback
114 * We change this depending on whether we're servicing the @b log command
116 ev_reader_callback
*reader
;
117 /** @brief Event log output sending to this connection */
118 struct eventlog_output
*lo
;
119 /** @brief Parent listener */
120 const struct listener
*l
;
121 /** @brief Login cookie or NULL */
123 /** @brief Connection rights */
125 /** @brief Next connection */
127 /** @brief True if pending rescan had 'wait' set */
131 /** @brief Linked list of connections */
132 static struct conn
*connections
;
134 static int reader_callback(ev_source
*ev
,
141 static const char *noyes
[] = { "no", "yes" };
143 /** @brief Remove a connection from the connection list */
144 static void remove_connection(struct conn
*c
) {
147 for(cc
= &connections
; *cc
&& *cc
!= c
; cc
= &(*cc
)->next
)
153 /** @brief Called when a connection's writer fails or is shut down
155 * If the connection still has a raeder that is cancelled.
157 static int writer_error(ev_source
attribute((unused
)) *ev
,
162 D(("server writer_error S%x %d", c
->tag
, errno_value
));
163 if(errno_value
== 0) {
165 D(("S%x writer completed", c
->tag
));
167 if(errno_value
!= EPIPE
)
168 error(errno_value
, "S%x write error on socket", c
->tag
);
170 D(("cancel reader"));
171 ev_reader_cancel(c
->r
);
174 D(("done cancel reader"));
178 remove_connection(c
);
182 /** @brief Called when a conncetion's reader fails or is shut down
184 * If connection still has a writer then it is closed.
186 static int reader_error(ev_source
attribute((unused
)) *ev
,
191 D(("server reader_error S%x %d", c
->tag
, errno_value
));
192 error(errno_value
, "S%x read error on socket", c
->tag
);
194 ev_writer_close(c
->w
);
198 remove_connection(c
);
202 static int c_disable(struct conn
*c
, char **vec
, int nvec
) {
204 disable_playing(c
->who
);
205 else if(nvec
== 1 && !strcmp(vec
[0], "now"))
206 disable_playing(c
->who
);
208 sink_writes(ev_writer_sink(c
->w
), "550 invalid argument\n");
209 return 1; /* completed */
211 sink_writes(ev_writer_sink(c
->w
), "250 OK\n");
212 return 1; /* completed */
215 static int c_enable(struct conn
*c
,
216 char attribute((unused
)) **vec
,
217 int attribute((unused
)) nvec
) {
218 enable_playing(c
->who
, c
->ev
);
219 /* Enable implicitly unpauses if there is nothing playing */
220 if(paused
&& !playing
) resume_playing(c
->who
);
221 sink_writes(ev_writer_sink(c
->w
), "250 OK\n");
222 return 1; /* completed */
225 static int c_enabled(struct conn
*c
,
226 char attribute((unused
)) **vec
,
227 int attribute((unused
)) nvec
) {
228 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", noyes
[playing_is_enabled()]);
229 return 1; /* completed */
232 static int c_play(struct conn
*c
, char **vec
,
233 int attribute((unused
)) nvec
) {
235 struct queue_entry
*q
;
237 if(!trackdb_exists(vec
[0])) {
238 sink_writes(ev_writer_sink(c
->w
), "550 track is not in database\n");
241 if(!(track
= trackdb_resolve(vec
[0]))) {
242 sink_writes(ev_writer_sink(c
->w
), "550 cannot resolve track\n");
245 q
= queue_add(track
, c
->who
, WHERE_BEFORE_RANDOM
);
247 /* If we added the first track, and something is playing, then prepare the
248 * new track. If nothing is playing then we don't bother as it wouldn't gain
250 if(q
== qhead
.next
&& playing
)
252 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", q
->id
);
253 /* If the queue was empty but we are for some reason paused then
255 if(!playing
) resume_playing(0);
257 return 1; /* completed */
260 static int c_remove(struct conn
*c
, char **vec
,
261 int attribute((unused
)) nvec
) {
262 struct queue_entry
*q
;
264 if(!(q
= queue_find(vec
[0]))) {
265 sink_writes(ev_writer_sink(c
->w
), "550 no such track on the queue\n");
268 if(!right_removable(c
->rights
, c
->who
, q
)) {
269 error(0, "%s attempted remove but lacks required rights", c
->who
);
270 sink_writes(ev_writer_sink(c
->w
),
271 "510 Not authorized to remove that track\n");
274 queue_remove(q
, c
->who
);
275 /* De-prepare the track. */
277 /* See about adding a new random track */
278 add_random_track(c
->ev
);
279 /* Prepare whatever the next head track is. */
280 if(qhead
.next
!= &qhead
)
281 prepare(c
->ev
, qhead
.next
);
283 sink_writes(ev_writer_sink(c
->w
), "250 removed\n");
284 return 1; /* completed */
287 static int c_scratch(struct conn
*c
,
291 sink_writes(ev_writer_sink(c
->w
), "250 nothing is playing\n");
292 return 1; /* completed */
294 /* TODO there is a bug here: if we specify an ID but it's not the currently
295 * playing track then you will get 550 if you weren't authorized to scratch
296 * the currently playing track. */
297 if(!right_scratchable(c
->rights
, c
->who
, playing
)) {
298 error(0, "%s attempted scratch but lacks required rights", c
->who
);
299 sink_writes(ev_writer_sink(c
->w
),
300 "510 Not authorized to scratch that track\n");
303 scratch(c
->who
, nvec
== 1 ? vec
[0] : 0);
304 /* If you scratch an unpaused track then it is automatically unpaused */
306 sink_writes(ev_writer_sink(c
->w
), "250 scratched\n");
307 return 1; /* completed */
310 static int c_pause(struct conn
*c
,
311 char attribute((unused
)) **vec
,
312 int attribute((unused
)) nvec
) {
314 sink_writes(ev_writer_sink(c
->w
), "250 nothing is playing\n");
315 return 1; /* completed */
318 sink_writes(ev_writer_sink(c
->w
), "250 already paused\n");
319 return 1; /* completed */
321 if(pause_playing(c
->who
) < 0)
322 sink_writes(ev_writer_sink(c
->w
), "550 cannot pause this track\n");
324 sink_writes(ev_writer_sink(c
->w
), "250 paused\n");
328 static int c_resume(struct conn
*c
,
329 char attribute((unused
)) **vec
,
330 int attribute((unused
)) nvec
) {
332 sink_writes(ev_writer_sink(c
->w
), "250 not paused\n");
333 return 1; /* completed */
335 resume_playing(c
->who
);
336 sink_writes(ev_writer_sink(c
->w
), "250 paused\n");
340 static int c_shutdown(struct conn
*c
,
341 char attribute((unused
)) **vec
,
342 int attribute((unused
)) nvec
) {
343 info("S%x shut down by %s", c
->tag
, c
->who
);
344 sink_writes(ev_writer_sink(c
->w
), "250 shutting down\n");
345 ev_writer_flush(c
->w
);
349 static int c_reconfigure(struct conn
*c
,
350 char attribute((unused
)) **vec
,
351 int attribute((unused
)) nvec
) {
352 info("S%x reconfigure by %s", c
->tag
, c
->who
);
353 if(reconfigure(c
->ev
, 1))
354 sink_writes(ev_writer_sink(c
->w
), "550 error reading new config\n");
356 sink_writes(ev_writer_sink(c
->w
), "250 installed new config\n");
357 return 1; /* completed */
360 static void finished_rescan(void *ru
) {
361 struct conn
*const c
= ru
;
363 sink_writes(ev_writer_sink(c
->w
), "250 rescan completed\n");
364 /* Turn this connection back on */
365 ev_reader_enable(c
->r
);
368 static void start_fresh_rescan(void *ru
) {
369 struct conn
*const c
= ru
;
371 if(trackdb_rescan_underway()) {
372 /* Some other waiter beat us to it. However in this case we're happy to
373 * piggyback; the requirement is that a new rescan be started, not that it
374 * was _our_ rescan. */
376 /* We block until the rescan completes */
377 trackdb_add_rescanned(finished_rescan
, c
);
379 /* We report that the new rescan has started */
380 sink_writes(ev_writer_sink(c
->w
), "250 rescan initiated\n");
381 /* Turn this connection back on */
382 ev_reader_enable(c
->r
);
385 /* We are the first connection to get a callback so we must start a
388 /* We want to block until the new rescan completes */
389 trackdb_rescan(c
->ev
, 1/*check*/, finished_rescan
, c
);
391 /* We can report back immediately */
392 trackdb_rescan(c
->ev
, 1/*check*/, 0, 0);
393 sink_writes(ev_writer_sink(c
->w
), "250 rescan initiated\n");
394 /* Turn this connection back on */
395 ev_reader_enable(c
->r
);
400 static int c_rescan(struct conn
*c
,
403 int wait
= 0, fresh
= 0, n
;
406 for(n
= 0; n
< nvec
; ++n
) {
407 if(!strcmp(vec
[n
], "wait"))
408 wait
= 1; /* wait for rescan to complete */
410 /* Currently disabled because untested (and hard to test). */
411 else if(!strcmp(vec
[n
], "fresh"))
412 fresh
= 1; /* don't piggyback underway rescan */
415 sink_writes(ev_writer_sink(c
->w
), "550 unknown flag\n");
416 return 1; /* completed */
419 /* Report what was requested */
420 info("S%x rescan by %s (%s %s)", c
->tag
, c
->who
,
422 fresh ?
"fresh" : "");
423 if(trackdb_rescan_underway()) {
425 /* We want a fresh rescan but there is already one underway. Arrange a
426 * callback when it completes and then set off a new one. */
427 c
->rescan_wait
= wait
;
428 trackdb_add_rescanned(start_fresh_rescan
, c
);
432 sink_writes(ev_writer_sink(c
->w
), "250 rescan queued\n");
436 /* There's a rescan underway, and it's acceptable to piggyback on it */
438 /* We want to block until completion. */
439 trackdb_add_rescanned(finished_rescan
, c
);
442 /* We don't want to block. So we just report that things are in
444 sink_writes(ev_writer_sink(c
->w
), "250 rescan already underway\n");
449 /* No rescan is underway. fresh is therefore irrelevant. */
451 /* We want to block until completion */
452 trackdb_rescan(c
->ev
, 1/*check*/, finished_rescan
, c
);
455 /* We don't want to block. */
456 trackdb_rescan(c
->ev
, 1/*check*/, 0, 0);
457 sink_writes(ev_writer_sink(c
->w
), "250 rescan initiated\n");
458 return 1; /* completed */
463 static int c_version(struct conn
*c
,
464 char attribute((unused
)) **vec
,
465 int attribute((unused
)) nvec
) {
466 /* VERSION had better only use the basic character set */
467 sink_printf(ev_writer_sink(c
->w
), "251 %s\n", disorder_short_version_string
);
468 return 1; /* completed */
471 static int c_playing(struct conn
*c
,
472 char attribute((unused
)) **vec
,
473 int attribute((unused
)) nvec
) {
475 queue_fix_sofar(playing
);
476 playing
->expected
= 0;
477 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", queue_marshall(playing
));
479 sink_printf(ev_writer_sink(c
->w
), "259 nothing playing\n");
480 return 1; /* completed */
483 static const char *connection_host(struct conn
*c
) {
486 struct sockaddr_in in
;
487 struct sockaddr_in6 in6
;
493 /* get connection data */
495 if(getpeername(c
->fd
, &u
.sa
, &l
) < 0) {
496 error(errno
, "S%x error calling getpeername", c
->tag
);
499 if(c
->l
->pf
!= PF_UNIX
) {
500 if((n
= getnameinfo(&u
.sa
, l
,
501 host
, sizeof host
, 0, 0, NI_NUMERICHOST
))) {
502 error(0, "S%x error calling getnameinfo: %s", c
->tag
, gai_strerror(n
));
505 return xstrdup(host
);
510 static int c_user(struct conn
*c
,
512 int attribute((unused
)) nvec
) {
514 const char *res
, *host
, *password
;
518 sink_writes(ev_writer_sink(c
->w
), "530 already authenticated\n");
521 /* get connection data */
522 if(!(host
= connection_host(c
))) {
523 sink_writes(ev_writer_sink(c
->w
), "530 authentication failure\n");
527 k
= trackdb_getuserinfo(vec
[0]);
528 /* reject nonexistent users */
530 error(0, "S%x unknown user '%s' from %s", c
->tag
, vec
[0], host
);
531 sink_writes(ev_writer_sink(c
->w
), "530 authentication failed\n");
534 /* reject unconfirmed users */
535 if(kvp_get(k
, "confirmation")) {
536 error(0, "S%x unconfirmed user '%s' from %s", c
->tag
, vec
[0], host
);
537 sink_writes(ev_writer_sink(c
->w
), "530 authentication failed\n");
540 password
= kvp_get(k
, "password");
541 if(!password
) password
= "";
542 if(parse_rights(kvp_get(k
, "rights"), &rights
, 1)) {
543 error(0, "error parsing rights for %s", vec
[0]);
544 sink_writes(ev_writer_sink(c
->w
), "530 authentication failed\n");
547 /* check whether the response is right */
548 res
= authhash(c
->nonce
, sizeof c
->nonce
, password
,
549 config
->authorization_algorithm
);
550 if(wideopen
|| (res
&& !strcmp(res
, vec
[1]))) {
553 /* currently we only bother logging remote connections */
554 if(strcmp(host
, "local"))
555 info("S%x %s connected from %s", c
->tag
, vec
[0], host
);
557 c
->rights
|= RIGHT__LOCAL
;
558 sink_writes(ev_writer_sink(c
->w
), "230 OK\n");
561 /* oops, response was wrong */
562 info("S%x authentication failure for %s from %s", c
->tag
, vec
[0], host
);
563 sink_writes(ev_writer_sink(c
->w
), "530 authentication failed\n");
567 static int c_recent(struct conn
*c
,
568 char attribute((unused
)) **vec
,
569 int attribute((unused
)) nvec
) {
570 const struct queue_entry
*q
;
572 sink_writes(ev_writer_sink(c
->w
), "253 Tracks follow\n");
573 for(q
= phead
.next
; q
!= &phead
; q
= q
->next
)
574 sink_printf(ev_writer_sink(c
->w
), " %s\n", queue_marshall(q
));
575 sink_writes(ev_writer_sink(c
->w
), ".\n");
576 return 1; /* completed */
579 static int c_queue(struct conn
*c
,
580 char attribute((unused
)) **vec
,
581 int attribute((unused
)) nvec
) {
582 struct queue_entry
*q
;
587 sink_writes(ev_writer_sink(c
->w
), "253 Tracks follow\n");
588 if(playing_is_enabled() && !paused
) {
590 queue_fix_sofar(playing
);
591 if((l
= trackdb_get(playing
->track
, "_length"))
592 && (length
= atol(l
))) {
594 when
+= length
- playing
->sofar
+ config
->gap
;
597 /* Nothing is playing but playing is enabled, so whatever is
598 * first in the queue can be expected to start immediately. */
601 for(q
= qhead
.next
; q
!= &qhead
; q
= q
->next
) {
602 /* fill in estimated start time */
604 sink_printf(ev_writer_sink(c
->w
), " %s\n", queue_marshall(q
));
605 /* update for next track */
607 if((l
= trackdb_get(q
->track
, "_length"))
608 && (length
= atol(l
)))
609 when
+= length
+ config
->gap
;
614 sink_writes(ev_writer_sink(c
->w
), ".\n");
615 return 1; /* completed */
618 static int output_list(struct conn
*c
, char **vec
) {
620 sink_printf(ev_writer_sink(c
->w
), "%s\n", *vec
++);
621 sink_writes(ev_writer_sink(c
->w
), ".\n");
625 static int files_dirs(struct conn
*c
,
628 enum trackdb_listable what
) {
629 const char *dir
, *re
, *errstr
;
635 case 0: dir
= 0; re
= 0; break;
636 case 1: dir
= vec
[0]; re
= 0; break;
637 case 2: dir
= vec
[0]; re
= vec
[1]; break;
640 /* A bit of a bodge to make sure the args don't trample on cache keys */
641 if(dir
&& strchr(dir
, '\n')) {
642 sink_writes(ev_writer_sink(c
->w
), "550 invalid directory name\n");
645 if(re
&& strchr(re
, '\n')) {
646 sink_writes(ev_writer_sink(c
->w
), "550 invalid regexp\n");
649 /* We bother eliminating "" because the web interface is relatively
650 * likely to send it */
652 byte_xasprintf(&key
, "%d\n%s\n%s", (int)what
, dir ? dir
: "", re
);
653 fvec
= (char **)cache_get(&cache_files_type
, key
);
655 /* Got a cache hit, don't store the answer in the cache */
658 rec
= 0; /* quieten compiler */
660 /* Cache miss, we'll do the lookup and key != 0 so we'll store the answer
662 if(!(rec
= pcre_compile(re
, PCRE_CASELESS
|PCRE_UTF8
,
663 &errstr
, &erroffset
, 0))) {
664 sink_printf(ev_writer_sink(c
->w
), "550 Error compiling regexp: %s\n",
668 /* It only counts as a miss if the regexp was valid. */
669 ++cache_files_misses
;
672 /* No regexp, don't bother caching the result */
678 /* No cache hit (either because a miss, or because we did not look) so do
681 fvec
= trackdb_list(dir
, 0, what
, rec
);
683 fvec
= trackdb_list(0, 0, what
, rec
);
686 /* Put the answer in the cache */
687 cache_put(&cache_files_type
, key
, fvec
);
688 sink_writes(ev_writer_sink(c
->w
), "253 Listing follow\n");
689 return output_list(c
, fvec
);
692 static int c_files(struct conn
*c
,
695 return files_dirs(c
, vec
, nvec
, trackdb_files
);
698 static int c_dirs(struct conn
*c
,
701 return files_dirs(c
, vec
, nvec
, trackdb_directories
);
704 static int c_allfiles(struct conn
*c
,
707 return files_dirs(c
, vec
, nvec
, trackdb_directories
|trackdb_files
);
710 static int c_get(struct conn
*c
,
712 int attribute((unused
)) nvec
) {
713 const char *v
, *track
;
715 if(!(track
= trackdb_resolve(vec
[0]))) {
716 sink_writes(ev_writer_sink(c
->w
), "550 cannot resolve track\n");
719 if(vec
[1][0] != '_' && (v
= trackdb_get(track
, vec
[1])))
720 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", quoteutf8(v
));
722 sink_writes(ev_writer_sink(c
->w
), "555 not found\n");
726 static int c_length(struct conn
*c
,
728 int attribute((unused
)) nvec
) {
729 const char *track
, *v
;
731 if(!(track
= trackdb_resolve(vec
[0]))) {
732 sink_writes(ev_writer_sink(c
->w
), "550 cannot resolve track\n");
735 if((v
= trackdb_get(track
, "_length")))
736 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", quoteutf8(v
));
738 sink_writes(ev_writer_sink(c
->w
), "550 not found\n");
742 static int c_set(struct conn
*c
,
744 int attribute((unused
)) nvec
) {
747 if(!(track
= trackdb_resolve(vec
[0]))) {
748 sink_writes(ev_writer_sink(c
->w
), "550 cannot resolve track\n");
751 if(vec
[1][0] != '_' && !trackdb_set(track
, vec
[1], vec
[2]))
752 sink_writes(ev_writer_sink(c
->w
), "250 OK\n");
754 sink_writes(ev_writer_sink(c
->w
), "550 not found\n");
758 static int c_prefs(struct conn
*c
,
760 int attribute((unused
)) nvec
) {
764 if(!(track
= trackdb_resolve(vec
[0]))) {
765 sink_writes(ev_writer_sink(c
->w
), "550 cannot resolve track\n");
768 k
= trackdb_get_all(track
);
769 sink_writes(ev_writer_sink(c
->w
), "253 prefs follow\n");
770 for(; k
; k
= k
->next
)
771 if(k
->name
[0] != '_') /* omit internal values */
772 sink_printf(ev_writer_sink(c
->w
),
773 " %s %s\n", quoteutf8(k
->name
), quoteutf8(k
->value
));
774 sink_writes(ev_writer_sink(c
->w
), ".\n");
778 static int c_exists(struct conn
*c
,
780 int attribute((unused
)) nvec
) {
781 /* trackdb_exists() does its own alias checking */
782 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", noyes
[trackdb_exists(vec
[0])]);
786 static void search_parse_error(const char *msg
, void *u
) {
787 *(const char **)u
= msg
;
790 static int c_search(struct conn
*c
,
792 int attribute((unused
)) nvec
) {
793 char **terms
, **results
;
794 int nterms
, nresults
, n
;
795 const char *e
= "unknown error";
797 /* This is a bit of a bodge. Initially it's there to make the eclient
798 * interface a bit more convenient to add searching to, but it has the more
799 * compelling advantage that if everything uses it, then interpretation of
800 * user-supplied search strings will be the same everywhere. */
801 if(!(terms
= split(vec
[0], &nterms
, SPLIT_QUOTES
, search_parse_error
, &e
))) {
802 sink_printf(ev_writer_sink(c
->w
), "550 %s\n", e
);
804 results
= trackdb_search(terms
, nterms
, &nresults
);
805 sink_printf(ev_writer_sink(c
->w
), "253 %d matches\n", nresults
);
806 for(n
= 0; n
< nresults
; ++n
)
807 sink_printf(ev_writer_sink(c
->w
), "%s\n", results
[n
]);
808 sink_writes(ev_writer_sink(c
->w
), ".\n");
813 static int c_random_enable(struct conn
*c
,
814 char attribute((unused
)) **vec
,
815 int attribute((unused
)) nvec
) {
816 enable_random(c
->who
, c
->ev
);
817 /* Enable implicitly unpauses if there is nothing playing */
818 if(paused
&& !playing
) resume_playing(c
->who
);
819 sink_writes(ev_writer_sink(c
->w
), "250 OK\n");
820 return 1; /* completed */
823 static int c_random_disable(struct conn
*c
,
824 char attribute((unused
)) **vec
,
825 int attribute((unused
)) nvec
) {
826 disable_random(c
->who
);
827 sink_writes(ev_writer_sink(c
->w
), "250 OK\n");
828 return 1; /* completed */
831 static int c_random_enabled(struct conn
*c
,
832 char attribute((unused
)) **vec
,
833 int attribute((unused
)) nvec
) {
834 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", noyes
[random_is_enabled()]);
835 return 1; /* completed */
838 static void got_stats(char *stats
, void *u
) {
839 struct conn
*const c
= u
;
841 sink_printf(ev_writer_sink(c
->w
), "253 stats\n%s\n.\n", stats
);
842 /* Now we can start processing commands again */
843 ev_reader_enable(c
->r
);
846 static int c_stats(struct conn
*c
,
847 char attribute((unused
)) **vec
,
848 int attribute((unused
)) nvec
) {
849 trackdb_stats_subprocess(c
->ev
, got_stats
, c
);
850 return 0; /* not yet complete */
853 static int c_volume(struct conn
*c
,
865 l
= r
= atoi(vec
[0]);
876 rights
= set ? RIGHT_VOLUME
: RIGHT_READ
;
877 if(!(c
->rights
& rights
)) {
878 error(0, "%s attempted to set volume but lacks required rights", c
->who
);
879 sink_writes(ev_writer_sink(c
->w
), "510 Prohibited\n");
882 if(mixer_control(-1/*as configured*/, &l
, &r
, set
))
883 sink_writes(ev_writer_sink(c
->w
), "550 error accessing mixer\n");
885 sink_printf(ev_writer_sink(c
->w
), "252 %d %d\n", l
, r
);
886 if(l
!= volume_left
|| r
!= volume_right
) {
889 snprintf(lb
, sizeof lb
, "%d", l
);
890 snprintf(rb
, sizeof rb
, "%d", r
);
891 eventlog("volume", lb
, rb
, (char *)0);
897 /** @brief Called when data arrives on a log connection
899 * We just discard all such data. The client may occasionally send data as a
902 static int logging_reader_callback(ev_source
attribute((unused
)) *ev
,
904 void attribute((unused
)) *ptr
,
906 int attribute((unused
)) eof
,
907 void attribute((unused
)) *u
) {
910 ev_reader_consume(reader
, bytes
);
912 /* Oops, that's all for now */
913 D(("logging reader eof"));
916 ev_writer_close(c
->w
);
920 remove_connection(c
);
925 static void logclient(const char *msg
, void *user
) {
926 struct conn
*c
= user
;
929 /* This connection has gone up in smoke for some reason */
930 eventlog_remove(c
->lo
);
933 sink_printf(ev_writer_sink(c
->w
), "%"PRIxMAX
" %s\n",
934 (uintmax_t)time(0), msg
);
937 static int c_log(struct conn
*c
,
938 char attribute((unused
)) **vec
,
939 int attribute((unused
)) nvec
) {
942 sink_writes(ev_writer_sink(c
->w
), "254 OK\n");
943 /* pump out initial state */
945 sink_printf(ev_writer_sink(c
->w
), "%"PRIxMAX
" state %s\n",
947 playing_is_enabled() ?
"enable_play" : "disable_play");
948 sink_printf(ev_writer_sink(c
->w
), "%"PRIxMAX
" state %s\n",
950 random_is_enabled() ?
"enable_random" : "disable_random");
951 sink_printf(ev_writer_sink(c
->w
), "%"PRIxMAX
" state %s\n",
953 paused ?
"pause" : "resume");
955 sink_printf(ev_writer_sink(c
->w
), "%"PRIxMAX
" state playing\n",
958 sink_printf(ev_writer_sink(c
->w
), "%"PRIxMAX
" volume %d %d\n",
959 (uintmax_t)now
, volume_left
, volume_right
);
960 c
->lo
= xmalloc(sizeof *c
->lo
);
961 c
->lo
->fn
= logclient
;
964 c
->reader
= logging_reader_callback
;
968 /** @brief Test whether a move is allowed
969 * @param c Connection
970 * @param qs List of IDs on queue
971 * @param nqs Number of IDs
972 * @return 0 if move is prohibited, non-0 if it is allowed
974 static int has_move_rights(struct conn
*c
, struct queue_entry
**qs
, int nqs
) {
975 for(; nqs
> 0; ++qs
, --nqs
) {
976 struct queue_entry
*const q
= *qs
;
978 if(!right_movable(c
->rights
, c
->who
, q
))
984 static int c_move(struct conn
*c
,
986 int attribute((unused
)) nvec
) {
987 struct queue_entry
*q
;
990 if(!(q
= queue_find(vec
[0]))) {
991 sink_writes(ev_writer_sink(c
->w
), "550 no such track on the queue\n");
994 if(!has_move_rights(c
, &q
, 1)) {
995 error(0, "%s attempted move but lacks required rights", c
->who
);
996 sink_writes(ev_writer_sink(c
->w
),
997 "510 Not authorized to move that track\n");
1000 n
= queue_move(q
, atoi(vec
[1]), c
->who
);
1001 sink_printf(ev_writer_sink(c
->w
), "252 %d\n", n
);
1002 /* If we've moved to the head of the queue then prepare the track. */
1008 static int c_moveafter(struct conn
*c
,
1010 int attribute((unused
)) nvec
) {
1011 struct queue_entry
*q
, **qs
;
1015 if(!(q
= queue_find(vec
[0]))) {
1016 sink_writes(ev_writer_sink(c
->w
), "550 no such track on the queue\n");
1023 qs
= xcalloc(nvec
, sizeof *qs
);
1024 for(n
= 0; n
< nvec
; ++n
)
1025 if(!(qs
[n
] = queue_find(vec
[n
]))) {
1026 sink_writes(ev_writer_sink(c
->w
), "550 no such track on the queue\n");
1029 if(!has_move_rights(c
, qs
, nvec
)) {
1030 error(0, "%s attempted moveafter but lacks required rights", c
->who
);
1031 sink_writes(ev_writer_sink(c
->w
),
1032 "510 Not authorized to move those tracks\n");
1035 queue_moveafter(q
, nvec
, qs
, c
->who
);
1036 sink_printf(ev_writer_sink(c
->w
), "250 Moved tracks\n");
1037 /* If we've moved to the head of the queue then prepare the track. */
1043 static int c_part(struct conn
*c
,
1045 int attribute((unused
)) nvec
) {
1048 if(!(track
= trackdb_resolve(vec
[0]))) {
1049 sink_writes(ev_writer_sink(c
->w
), "550 cannot resolve track\n");
1052 sink_printf(ev_writer_sink(c
->w
), "252 %s\n",
1053 quoteutf8(trackdb_getpart(track
, vec
[1], vec
[2])));
1057 static int c_resolve(struct conn
*c
,
1059 int attribute((unused
)) nvec
) {
1062 if(!(track
= trackdb_resolve(vec
[0]))) {
1063 sink_writes(ev_writer_sink(c
->w
), "550 cannot resolve track\n");
1066 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", quoteutf8(track
));
1070 static int c_tags(struct conn
*c
,
1071 char attribute((unused
)) **vec
,
1072 int attribute((unused
)) nvec
) {
1073 char **tags
= trackdb_alltags();
1075 sink_printf(ev_writer_sink(c
->w
), "253 Tag list follows\n");
1077 sink_printf(ev_writer_sink(c
->w
), "%s%s\n",
1078 **tags
== '.' ?
"." : "", *tags
);
1081 sink_writes(ev_writer_sink(c
->w
), ".\n");
1082 return 1; /* completed */
1085 static int c_set_global(struct conn
*c
,
1087 int attribute((unused
)) nvec
) {
1088 if(vec
[0][0] == '_') {
1089 sink_writes(ev_writer_sink(c
->w
), "550 cannot set internal global preferences\n");
1092 trackdb_set_global(vec
[0], vec
[1], c
->who
);
1093 sink_printf(ev_writer_sink(c
->w
), "250 OK\n");
1097 static int c_get_global(struct conn
*c
,
1099 int attribute((unused
)) nvec
) {
1100 const char *s
= trackdb_get_global(vec
[0]);
1103 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", quoteutf8(s
));
1105 sink_writes(ev_writer_sink(c
->w
), "555 not found\n");
1109 static int c_nop(struct conn
*c
,
1110 char attribute((unused
)) **vec
,
1111 int attribute((unused
)) nvec
) {
1112 sink_printf(ev_writer_sink(c
->w
), "250 Quack\n");
1116 static int c_new(struct conn
*c
,
1126 if(max
<= 0 || max
> config
->new_max
)
1127 max
= config
->new_max
;
1128 tracks
= trackdb_new(0, max
);
1129 sink_printf(ev_writer_sink(c
->w
), "253 New track list follows\n");
1132 sink_printf(ev_writer_sink(c
->w
), "%s%s\n",
1133 **tracks
== '.' ?
"." : "", *tracks
);
1136 sink_writes(ev_writer_sink(c
->w
), ".\n");
1137 return 1; /* completed */
1141 static int c_rtp_address(struct conn
*c
,
1142 char attribute((unused
)) **vec
,
1143 int attribute((unused
)) nvec
) {
1144 if(config
->api
== BACKEND_NETWORK
) {
1145 sink_printf(ev_writer_sink(c
->w
), "252 %s %s\n",
1146 quoteutf8(config
->broadcast
.s
[0]),
1147 quoteutf8(config
->broadcast
.s
[1]));
1149 sink_writes(ev_writer_sink(c
->w
), "550 No RTP\n");
1153 static int c_cookie(struct conn
*c
,
1155 int attribute((unused
)) nvec
) {
1160 /* Can't log in twice on the same connection */
1162 sink_writes(ev_writer_sink(c
->w
), "530 already authenticated\n");
1165 /* Get some kind of peer identifcation */
1166 if(!(host
= connection_host(c
))) {
1167 sink_writes(ev_writer_sink(c
->w
), "530 authentication failure\n");
1170 /* Check the cookie */
1171 user
= verify_cookie(vec
[0], &rights
);
1173 sink_writes(ev_writer_sink(c
->w
), "530 authentication failure\n");
1180 if(strcmp(host
, "local"))
1181 info("S%x %s connected with cookie from %s", c
->tag
, user
, host
);
1183 c
->rights
|= RIGHT__LOCAL
;
1184 /* Response contains username so client knows who they are acting as */
1185 sink_printf(ev_writer_sink(c
->w
), "232 %s\n", quoteutf8(user
));
1189 static int c_make_cookie(struct conn
*c
,
1190 char attribute((unused
)) **vec
,
1191 int attribute((unused
)) nvec
) {
1192 const char *cookie
= make_cookie(c
->who
);
1195 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", quoteutf8(cookie
));
1197 sink_writes(ev_writer_sink(c
->w
), "550 Cannot create cookie\n");
1201 static int c_revoke(struct conn
*c
,
1202 char attribute((unused
)) **vec
,
1203 int attribute((unused
)) nvec
) {
1205 revoke_cookie(c
->cookie
);
1206 sink_writes(ev_writer_sink(c
->w
), "250 OK\n");
1208 sink_writes(ev_writer_sink(c
->w
), "550 Did not log in with cookie\n");
1212 static int c_adduser(struct conn
*c
,
1217 if(!config
->remote_userman
&& !(c
->rights
& RIGHT__LOCAL
)) {
1218 error(0, "S%x: remote adduser", c
->tag
);
1219 sink_writes(ev_writer_sink(c
->w
), "550 Remote user management is disabled\n");
1224 if(parse_rights(vec
[2], 0, 1)) {
1225 sink_writes(ev_writer_sink(c
->w
), "550 Invalid rights list\n");
1229 rights
= config
->default_rights
;
1230 if(trackdb_adduser(vec
[0], vec
[1], rights
,
1231 0/*email*/, 0/*confirmation*/))
1232 sink_writes(ev_writer_sink(c
->w
), "550 Cannot create user\n");
1234 sink_writes(ev_writer_sink(c
->w
), "250 User created\n");
1238 static int c_deluser(struct conn
*c
,
1240 int attribute((unused
)) nvec
) {
1243 if(!config
->remote_userman
&& !(c
->rights
& RIGHT__LOCAL
)) {
1244 error(0, "S%x: remote deluser", c
->tag
);
1245 sink_writes(ev_writer_sink(c
->w
), "550 Remote user management is disabled\n");
1248 if(trackdb_deluser(vec
[0])) {
1249 sink_writes(ev_writer_sink(c
->w
), "550 Cannot delete user\n");
1252 /* Zap connections belonging to deleted user */
1253 for(d
= connections
; d
; d
= d
->next
)
1254 if(!strcmp(d
->who
, vec
[0]))
1256 sink_writes(ev_writer_sink(c
->w
), "250 User deleted\n");
1260 static int c_edituser(struct conn
*c
,
1262 int attribute((unused
)) nvec
) {
1265 if(!config
->remote_userman
&& !(c
->rights
& RIGHT__LOCAL
)) {
1266 error(0, "S%x: remote edituser", c
->tag
);
1267 sink_writes(ev_writer_sink(c
->w
), "550 Remote user management is disabled\n");
1270 /* RIGHT_ADMIN can do anything; otherwise you can only set your own email
1271 * address and password. */
1272 if((c
->rights
& RIGHT_ADMIN
)
1273 || (!strcmp(c
->who
, vec
[0])
1274 && (!strcmp(vec
[1], "email")
1275 || !strcmp(vec
[1], "password")))) {
1276 if(trackdb_edituserinfo(vec
[0], vec
[1], vec
[2])) {
1277 sink_writes(ev_writer_sink(c
->w
), "550 Failed to change setting\n");
1280 if(!strcmp(vec
[1], "password")) {
1281 /* Zap all connections for this user after a password change */
1282 for(d
= connections
; d
; d
= d
->next
)
1283 if(!strcmp(d
->who
, vec
[0]))
1285 } else if(!strcmp(vec
[1], "rights")) {
1286 /* Update rights for this user */
1289 if(parse_rights(vec
[2], &r
, 1))
1290 for(d
= connections
; d
; d
= d
->next
)
1291 if(!strcmp(d
->who
, vec
[0]))
1294 sink_writes(ev_writer_sink(c
->w
), "250 OK\n");
1296 error(0, "%s attempted edituser but lacks required rights", c
->who
);
1297 sink_writes(ev_writer_sink(c
->w
), "510 Restricted to administrators\n");
1302 static int c_userinfo(struct conn
*c
,
1303 char attribute((unused
)) **vec
,
1304 int attribute((unused
)) nvec
) {
1308 /* We allow remote querying of rights so that clients can figure out what
1309 * they're allowed to do */
1310 if(!config
->remote_userman
1311 && !(c
->rights
& RIGHT__LOCAL
)
1312 && strcmp(vec
[1], "rights")) {
1313 error(0, "S%x: remote userinfo %s %s", c
->tag
, vec
[0], vec
[1]);
1314 sink_writes(ev_writer_sink(c
->w
), "550 Remote user management is disabled\n");
1317 /* RIGHT_ADMIN allows anything; otherwise you can only get your own email
1318 * address and rights list. */
1319 if((c
->rights
& RIGHT_ADMIN
)
1320 || (!strcmp(c
->who
, vec
[0])
1321 && (!strcmp(vec
[1], "email")
1322 || !strcmp(vec
[1], "rights")))) {
1323 if((k
= trackdb_getuserinfo(vec
[0])))
1324 if((value
= kvp_get(k
, vec
[1])))
1325 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", quoteutf8(value
));
1327 sink_writes(ev_writer_sink(c
->w
), "555 Not set\n");
1329 sink_writes(ev_writer_sink(c
->w
), "550 No such user\n");
1331 error(0, "%s attempted userinfo but lacks required rights", c
->who
);
1332 sink_writes(ev_writer_sink(c
->w
), "510 Restricted to administrators\n");
1337 static int c_users(struct conn
*c
,
1338 char attribute((unused
)) **vec
,
1339 int attribute((unused
)) nvec
) {
1340 /* TODO de-dupe with c_tags */
1341 char **users
= trackdb_listusers();
1343 sink_writes(ev_writer_sink(c
->w
), "253 User list follows\n");
1345 sink_printf(ev_writer_sink(c
->w
), "%s%s\n",
1346 **users
== '.' ?
"." : "", *users
);
1349 sink_writes(ev_writer_sink(c
->w
), ".\n");
1350 return 1; /* completed */
1353 /** @brief Base64 mapping table for confirmation strings
1355 * This is used with generic_to_base64() and generic_base64(). We cannot use
1356 * the MIME table as that contains '+' and '=' which get quoted when
1357 * URL-encoding. (The CGI still does the URL encoding but it is desirable to
1358 * avoid it being necessary.)
1360 static const char confirm_base64_table
[] =
1361 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789/.*";
1363 static int c_register(struct conn
*c
,
1365 int attribute((unused
)) nvec
) {
1370 /* The confirmation string is base64(username;nonce) */
1371 bufsize
= strlen(vec
[0]) + CONFIRM_SIZE
+ 2;
1372 buf
= xmalloc_noptr(bufsize
);
1373 offset
= byte_snprintf(buf
, bufsize
, "%s;", vec
[0]);
1374 gcry_randomize(buf
+ offset
, CONFIRM_SIZE
, GCRY_STRONG_RANDOM
);
1375 cs
= generic_to_base64((uint8_t *)buf
, offset
+ CONFIRM_SIZE
,
1376 confirm_base64_table
);
1377 if(trackdb_adduser(vec
[0], vec
[1], config
->default_rights
, vec
[2], cs
))
1378 sink_writes(ev_writer_sink(c
->w
), "550 Cannot create user\n");
1380 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", quoteutf8(cs
));
1384 static int c_confirm(struct conn
*c
,
1386 int attribute((unused
)) nvec
) {
1392 /* Get some kind of peer identifcation */
1393 if(!(host
= connection_host(c
))) {
1394 sink_writes(ev_writer_sink(c
->w
), "530 Authentication failure\n");
1397 if(!(user
= generic_base64(vec
[0], &nuser
, confirm_base64_table
))
1398 || !(sep
= memchr(user
, ';', nuser
))) {
1399 sink_writes(ev_writer_sink(c
->w
), "550 Malformed confirmation string\n");
1403 if(trackdb_confirm(user
, vec
[0], &rights
))
1404 sink_writes(ev_writer_sink(c
->w
), "550 Incorrect confirmation string\n");
1409 if(strcmp(host
, "local"))
1410 info("S%x %s confirmed from %s", c
->tag
, user
, host
);
1412 c
->rights
|= RIGHT__LOCAL
;
1413 /* Response contains username so client knows who they are acting as */
1414 sink_printf(ev_writer_sink(c
->w
), "232 %s\n", quoteutf8(user
));
1419 static int sent_reminder(ev_source
attribute((unused
)) *ev
,
1420 pid_t
attribute((unused
)) pid
,
1422 const struct rusage
attribute((unused
)) *rusage
,
1424 struct conn
*const c
= u
;
1426 /* Tell the client what went down */
1428 sink_writes(ev_writer_sink(c
->w
), "250 OK\n");
1430 error(0, "reminder subprocess %s", wstat(status
));
1431 sink_writes(ev_writer_sink(c
->w
), "550 Cannot send a reminder email\n");
1433 /* Re-enable this connection */
1434 ev_reader_enable(c
->r
);
1438 static int c_reminder(struct conn
*c
,
1440 int attribute((unused
)) nvec
) {
1442 const char *password
, *email
, *text
, *encoding
, *charset
, *content_type
;
1447 static hash
*last_reminder
;
1449 if(!config
->mail_sender
) {
1450 error(0, "cannot send password reminders because mail_sender not set");
1451 sink_writes(ev_writer_sink(c
->w
), "550 Cannot send a reminder email\n");
1454 if(!(k
= trackdb_getuserinfo(vec
[0]))) {
1455 error(0, "reminder for user '%s' who does not exist", vec
[0]);
1456 sink_writes(ev_writer_sink(c
->w
), "550 Cannot send a reminder email\n");
1459 if(!(email
= kvp_get(k
, "email"))
1460 || !strchr(email
, '@')) {
1461 error(0, "user '%s' has no valid email address", vec
[0]);
1462 sink_writes(ev_writer_sink(c
->w
), "550 Cannot send a reminder email\n");
1465 if(!(password
= kvp_get(k
, "password"))
1467 error(0, "user '%s' has no password", vec
[0]);
1468 sink_writes(ev_writer_sink(c
->w
), "550 Cannot send a reminder email\n");
1471 /* Rate-limit reminders. This hash is bounded in size by the number of
1472 * users. If this is actually a problem for anyone then we can periodically
1475 last_reminder
= hash_new(sizeof (time_t));
1476 last
= hash_find(last_reminder
, vec
[0]);
1478 if(last
&& now
< *last
+ config
->reminder_interval
) {
1479 error(0, "sent a password reminder to '%s' too recently", vec
[0]);
1480 sink_writes(ev_writer_sink(c
->w
), "550 Cannot send a reminder email\n");
1483 /* Send the reminder */
1484 /* TODO this should be templatized and to some extent merged with
1485 * the code in act_register() */
1486 byte_xasprintf((char **)&text
,
1487 "Someone requested that you be sent a reminder of your DisOrder password.\n"
1488 "Your password is:\n"
1491 if(!(text
= mime_encode_text(text
, &charset
, &encoding
)))
1492 fatal(0, "cannot encode email");
1493 byte_xasprintf((char **)&content_type
, "text/plain;charset=%s",
1494 quote822(charset
, 0));
1495 pid
= sendmail_subprocess("", config
->mail_sender
, email
,
1496 "DisOrder password reminder",
1497 encoding
, content_type
, text
);
1499 sink_writes(ev_writer_sink(c
->w
), "550 Cannot send a reminder email\n");
1502 hash_add(last_reminder
, vec
[0], &now
, HASH_INSERT_OR_REPLACE
);
1503 info("sending a passsword reminder to user '%s'", vec
[0]);
1504 /* We can only continue when the subprocess finishes */
1505 ev_child(c
->ev
, pid
, 0, sent_reminder
, c
);
1509 static const struct command
{
1510 /** @brief Command name */
1513 /** @brief Minimum number of arguments */
1516 /** @brief Maximum number of arguments */
1519 /** @brief Function to process command */
1520 int (*fn
)(struct conn
*, char **, int);
1522 /** @brief Rights required to execute command
1524 * 0 means that the command can be issued without logging in. If multiple
1525 * bits are listed here any of those rights will do.
1529 { "adduser", 2, 3, c_adduser
, RIGHT_ADMIN
|RIGHT__LOCAL
},
1530 { "allfiles", 0, 2, c_allfiles
, RIGHT_READ
},
1531 { "confirm", 1, 1, c_confirm
, 0 },
1532 { "cookie", 1, 1, c_cookie
, 0 },
1533 { "deluser", 1, 1, c_deluser
, RIGHT_ADMIN
|RIGHT__LOCAL
},
1534 { "dirs", 0, 2, c_dirs
, RIGHT_READ
},
1535 { "disable", 0, 1, c_disable
, RIGHT_GLOBAL_PREFS
},
1536 { "edituser", 3, 3, c_edituser
, RIGHT_ADMIN
|RIGHT_USERINFO
},
1537 { "enable", 0, 0, c_enable
, RIGHT_GLOBAL_PREFS
},
1538 { "enabled", 0, 0, c_enabled
, RIGHT_READ
},
1539 { "exists", 1, 1, c_exists
, RIGHT_READ
},
1540 { "files", 0, 2, c_files
, RIGHT_READ
},
1541 { "get", 2, 2, c_get
, RIGHT_READ
},
1542 { "get-global", 1, 1, c_get_global
, RIGHT_READ
},
1543 { "length", 1, 1, c_length
, RIGHT_READ
},
1544 { "log", 0, 0, c_log
, RIGHT_READ
},
1545 { "make-cookie", 0, 0, c_make_cookie
, RIGHT_READ
},
1546 { "move", 2, 2, c_move
, RIGHT_MOVE__MASK
},
1547 { "moveafter", 1, INT_MAX
, c_moveafter
, RIGHT_MOVE__MASK
},
1548 { "new", 0, 1, c_new
, RIGHT_READ
},
1549 { "nop", 0, 0, c_nop
, 0 },
1550 { "part", 3, 3, c_part
, RIGHT_READ
},
1551 { "pause", 0, 0, c_pause
, RIGHT_PAUSE
},
1552 { "play", 1, 1, c_play
, RIGHT_PLAY
},
1553 { "playing", 0, 0, c_playing
, RIGHT_READ
},
1554 { "prefs", 1, 1, c_prefs
, RIGHT_READ
},
1555 { "queue", 0, 0, c_queue
, RIGHT_READ
},
1556 { "random-disable", 0, 0, c_random_disable
, RIGHT_GLOBAL_PREFS
},
1557 { "random-enable", 0, 0, c_random_enable
, RIGHT_GLOBAL_PREFS
},
1558 { "random-enabled", 0, 0, c_random_enabled
, RIGHT_READ
},
1559 { "recent", 0, 0, c_recent
, RIGHT_READ
},
1560 { "reconfigure", 0, 0, c_reconfigure
, RIGHT_ADMIN
},
1561 { "register", 3, 3, c_register
, RIGHT_REGISTER
|RIGHT__LOCAL
},
1562 { "reminder", 1, 1, c_reminder
, RIGHT__LOCAL
},
1563 { "remove", 1, 1, c_remove
, RIGHT_REMOVE__MASK
},
1564 { "rescan", 0, INT_MAX
, c_rescan
, RIGHT_RESCAN
},
1565 { "resolve", 1, 1, c_resolve
, RIGHT_READ
},
1566 { "resume", 0, 0, c_resume
, RIGHT_PAUSE
},
1567 { "revoke", 0, 0, c_revoke
, RIGHT_READ
},
1568 { "rtp-address", 0, 0, c_rtp_address
, 0 },
1569 { "scratch", 0, 1, c_scratch
, RIGHT_SCRATCH__MASK
},
1570 { "search", 1, 1, c_search
, RIGHT_READ
},
1571 { "set", 3, 3, c_set
, RIGHT_PREFS
, },
1572 { "set-global", 2, 2, c_set_global
, RIGHT_GLOBAL_PREFS
},
1573 { "shutdown", 0, 0, c_shutdown
, RIGHT_ADMIN
},
1574 { "stats", 0, 0, c_stats
, RIGHT_READ
},
1575 { "tags", 0, 0, c_tags
, RIGHT_READ
},
1576 { "unset", 2, 2, c_set
, RIGHT_PREFS
},
1577 { "unset-global", 1, 1, c_set_global
, RIGHT_GLOBAL_PREFS
},
1578 { "user", 2, 2, c_user
, 0 },
1579 { "userinfo", 2, 2, c_userinfo
, RIGHT_READ
},
1580 { "users", 0, 0, c_users
, RIGHT_READ
},
1581 { "version", 0, 0, c_version
, RIGHT_READ
},
1582 { "volume", 0, 2, c_volume
, RIGHT_READ
|RIGHT_VOLUME
}
1585 static void command_error(const char *msg
, void *u
) {
1588 sink_printf(ev_writer_sink(c
->w
), "500 parse error: %s\n", msg
);
1591 /* process a command. Return 1 if complete, 0 if incomplete. */
1592 static int command(struct conn
*c
, char *line
) {
1596 D(("server command %s", line
));
1597 /* We force everything into NFC as early as possible */
1598 if(!(line
= utf8_compose_canon(line
, strlen(line
), 0))) {
1599 sink_writes(ev_writer_sink(c
->w
), "500 cannot normalize command\n");
1602 if(!(vec
= split(line
, &nvec
, SPLIT_QUOTES
, command_error
, c
))) {
1603 sink_writes(ev_writer_sink(c
->w
), "500 cannot parse command\n");
1607 sink_writes(ev_writer_sink(c
->w
), "500 do what?\n");
1610 if((n
= TABLE_FIND(commands
, struct command
, name
, vec
[0])) < 0)
1611 sink_writes(ev_writer_sink(c
->w
), "500 unknown command\n");
1613 if(commands
[n
].rights
1614 && !(c
->rights
& commands
[n
].rights
)) {
1615 error(0, "%s attempted %s but lacks required rights", c
->who ? c
->who
: "NULL",
1617 sink_writes(ev_writer_sink(c
->w
), "510 Prohibited\n");
1622 if(nvec
< commands
[n
].minargs
) {
1623 sink_writes(ev_writer_sink(c
->w
), "500 missing argument(s)\n");
1626 if(nvec
> commands
[n
].maxargs
) {
1627 sink_writes(ev_writer_sink(c
->w
), "500 too many arguments\n");
1630 return commands
[n
].fn(c
, vec
, nvec
);
1632 return 1; /* completed */
1635 /* redirect to the right reader callback for our current state */
1636 static int redirect_reader_callback(ev_source
*ev
,
1644 return c
->reader(ev
, reader
, ptr
, bytes
, eof
, u
);
1647 /* the main command reader */
1648 static int reader_callback(ev_source
attribute((unused
)) *ev
,
1658 D(("server reader_callback"));
1659 while((eol
= memchr(ptr
, '\n', bytes
))) {
1661 ev_reader_consume(reader
, eol
- (char *)ptr
);
1662 complete
= command(c
, ptr
);
1663 bytes
-= (eol
- (char *)ptr
);
1666 /* the command had better have set a new reader callback */
1668 /* there are further bytes to read, or we are at eof; arrange for the
1669 * command's reader callback to handle them */
1670 return ev_reader_incomplete(reader
);
1671 /* nothing's going on right now */
1674 /* command completed, we can go around and handle the next one */
1678 error(0, "S%x unterminated line", c
->tag
);
1679 D(("normal reader close"));
1682 D(("close associated writer"));
1683 ev_writer_close(c
->w
);
1686 remove_connection(c
);
1691 static int listen_callback(ev_source
*ev
,
1693 const struct sockaddr
attribute((unused
)) *remote
,
1694 socklen_t
attribute((unused
)) rlen
,
1696 const struct listener
*l
= u
;
1697 struct conn
*c
= xmalloc(sizeof *c
);
1698 static unsigned tags
;
1700 D(("server listen_callback fd %d (%s)", fd
, l
->name
));
1705 c
->w
= ev_writer_new(ev
, fd
, writer_error
, c
,
1707 c
->r
= ev_reader_new(ev
, fd
, redirect_reader_callback
, reader_error
, c
,
1711 c
->reader
= reader_callback
;
1714 gcry_randomize(c
->nonce
, sizeof c
->nonce
, GCRY_STRONG_RANDOM
);
1715 sink_printf(ev_writer_sink(c
->w
), "231 %d %s %s\n",
1717 config
->authorization_algorithm
,
1718 hex(c
->nonce
, sizeof c
->nonce
));
1722 int server_start(ev_source
*ev
, int pf
,
1723 size_t socklen
, const struct sockaddr
*sa
,
1726 struct listener
*l
= xmalloc(sizeof *l
);
1727 static const int one
= 1;
1729 D(("server_init socket %s", name
));
1730 fd
= xsocket(pf
, SOCK_STREAM
, 0);
1731 xsetsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &one
, sizeof one
);
1732 if(bind(fd
, sa
, socklen
) < 0) {
1733 error(errno
, "error binding to %s", name
);
1741 if(ev_listen(ev
, fd
, listen_callback
, l
, "server listener"))
1746 int server_stop(ev_source
*ev
, int fd
) {
1748 return ev_listen_cancel(ev
, fd
);