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 */
129 /** @brief Linked list of connections */
130 static struct conn
*connections
;
132 static int reader_callback(ev_source
*ev
,
139 static const char *noyes
[] = { "no", "yes" };
141 /** @brief Remove a connection from the connection list */
142 static void remove_connection(struct conn
*c
) {
145 for(cc
= &connections
; *cc
&& *cc
!= c
; cc
= &(*cc
)->next
)
151 /** @brief Called when a connection's writer fails or is shut down
153 * If the connection still has a raeder that is cancelled.
155 static int writer_error(ev_source
attribute((unused
)) *ev
,
160 D(("server writer_error S%x %d", c
->tag
, errno_value
));
161 if(errno_value
== 0) {
163 D(("S%x writer completed", c
->tag
));
165 if(errno_value
!= EPIPE
)
166 error(errno_value
, "S%x write error on socket", c
->tag
);
168 D(("cancel reader"));
169 ev_reader_cancel(c
->r
);
172 D(("done cancel reader"));
176 remove_connection(c
);
180 /** @brief Called when a conncetion's reader fails or is shut down
182 * If connection still has a writer then it is closed.
184 static int reader_error(ev_source
attribute((unused
)) *ev
,
189 D(("server reader_error S%x %d", c
->tag
, errno_value
));
190 error(errno_value
, "S%x read error on socket", c
->tag
);
192 ev_writer_close(c
->w
);
196 remove_connection(c
);
200 static int c_disable(struct conn
*c
, char **vec
, int nvec
) {
202 disable_playing(c
->who
);
203 else if(nvec
== 1 && !strcmp(vec
[0], "now"))
204 disable_playing(c
->who
);
206 sink_writes(ev_writer_sink(c
->w
), "550 invalid argument\n");
207 return 1; /* completed */
209 sink_writes(ev_writer_sink(c
->w
), "250 OK\n");
210 return 1; /* completed */
213 static int c_enable(struct conn
*c
,
214 char attribute((unused
)) **vec
,
215 int attribute((unused
)) nvec
) {
216 enable_playing(c
->who
, c
->ev
);
217 /* Enable implicitly unpauses if there is nothing playing */
218 if(paused
&& !playing
) resume_playing(c
->who
);
219 sink_writes(ev_writer_sink(c
->w
), "250 OK\n");
220 return 1; /* completed */
223 static int c_enabled(struct conn
*c
,
224 char attribute((unused
)) **vec
,
225 int attribute((unused
)) nvec
) {
226 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", noyes
[playing_is_enabled()]);
227 return 1; /* completed */
230 static int c_play(struct conn
*c
, char **vec
,
231 int attribute((unused
)) nvec
) {
233 struct queue_entry
*q
;
235 if(!trackdb_exists(vec
[0])) {
236 sink_writes(ev_writer_sink(c
->w
), "550 track is not in database\n");
239 if(!(track
= trackdb_resolve(vec
[0]))) {
240 sink_writes(ev_writer_sink(c
->w
), "550 cannot resolve track\n");
243 q
= queue_add(track
, c
->who
, WHERE_BEFORE_RANDOM
);
245 /* If we added the first track, and something is playing, then prepare the
246 * new track. If nothing is playing then we don't bother as it wouldn't gain
248 if(q
== qhead
.next
&& playing
)
250 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", q
->id
);
251 /* If the queue was empty but we are for some reason paused then
253 if(!playing
) resume_playing(0);
255 return 1; /* completed */
258 static int c_remove(struct conn
*c
, char **vec
,
259 int attribute((unused
)) nvec
) {
260 struct queue_entry
*q
;
262 if(!(q
= queue_find(vec
[0]))) {
263 sink_writes(ev_writer_sink(c
->w
), "550 no such track on the queue\n");
266 if(!right_removable(c
->rights
, c
->who
, q
)) {
267 error(0, "%s attempted remove but lacks required rights", c
->who
);
268 sink_writes(ev_writer_sink(c
->w
),
269 "510 Not authorized to remove that track\n");
272 queue_remove(q
, c
->who
);
273 /* De-prepare the track. */
275 /* See about adding a new random track */
276 add_random_track(c
->ev
);
277 /* Prepare whatever the next head track is. */
278 if(qhead
.next
!= &qhead
)
279 prepare(c
->ev
, qhead
.next
);
281 sink_writes(ev_writer_sink(c
->w
), "250 removed\n");
282 return 1; /* completed */
285 static int c_scratch(struct conn
*c
,
289 sink_writes(ev_writer_sink(c
->w
), "250 nothing is playing\n");
290 return 1; /* completed */
292 /* TODO there is a bug here: if we specify an ID but it's not the currently
293 * playing track then you will get 550 if you weren't authorized to scratch
294 * the currently playing track. */
295 if(!right_scratchable(c
->rights
, c
->who
, playing
)) {
296 error(0, "%s attempted scratch but lacks required rights", c
->who
);
297 sink_writes(ev_writer_sink(c
->w
),
298 "510 Not authorized to scratch that track\n");
301 scratch(c
->who
, nvec
== 1 ? vec
[0] : 0);
302 /* If you scratch an unpaused track then it is automatically unpaused */
304 sink_writes(ev_writer_sink(c
->w
), "250 scratched\n");
305 return 1; /* completed */
308 static int c_pause(struct conn
*c
,
309 char attribute((unused
)) **vec
,
310 int attribute((unused
)) nvec
) {
312 sink_writes(ev_writer_sink(c
->w
), "250 nothing is playing\n");
313 return 1; /* completed */
316 sink_writes(ev_writer_sink(c
->w
), "250 already paused\n");
317 return 1; /* completed */
319 if(pause_playing(c
->who
) < 0)
320 sink_writes(ev_writer_sink(c
->w
), "550 cannot pause this track\n");
322 sink_writes(ev_writer_sink(c
->w
), "250 paused\n");
326 static int c_resume(struct conn
*c
,
327 char attribute((unused
)) **vec
,
328 int attribute((unused
)) nvec
) {
330 sink_writes(ev_writer_sink(c
->w
), "250 not paused\n");
331 return 1; /* completed */
333 resume_playing(c
->who
);
334 sink_writes(ev_writer_sink(c
->w
), "250 paused\n");
338 static int c_shutdown(struct conn
*c
,
339 char attribute((unused
)) **vec
,
340 int attribute((unused
)) nvec
) {
341 info("S%x shut down by %s", c
->tag
, c
->who
);
342 sink_writes(ev_writer_sink(c
->w
), "250 shutting down\n");
343 ev_writer_flush(c
->w
);
347 static int c_reconfigure(struct conn
*c
,
348 char attribute((unused
)) **vec
,
349 int attribute((unused
)) nvec
) {
350 info("S%x reconfigure by %s", c
->tag
, c
->who
);
351 if(reconfigure(c
->ev
, 1))
352 sink_writes(ev_writer_sink(c
->w
), "550 error reading new config\n");
354 sink_writes(ev_writer_sink(c
->w
), "250 installed new config\n");
355 return 1; /* completed */
358 static int c_rescan(struct conn
*c
,
359 char attribute((unused
)) **vec
,
360 int attribute((unused
)) nvec
) {
361 info("S%x rescan by %s", c
->tag
, c
->who
);
362 trackdb_rescan(c
->ev
, 1/*check*/);
363 sink_writes(ev_writer_sink(c
->w
), "250 initiated rescan\n");
364 return 1; /* completed */
367 static int c_version(struct conn
*c
,
368 char attribute((unused
)) **vec
,
369 int attribute((unused
)) nvec
) {
370 /* VERSION had better only use the basic character set */
371 sink_printf(ev_writer_sink(c
->w
), "251 %s\n", disorder_short_version_string
);
372 return 1; /* completed */
375 static int c_playing(struct conn
*c
,
376 char attribute((unused
)) **vec
,
377 int attribute((unused
)) nvec
) {
379 queue_fix_sofar(playing
);
380 playing
->expected
= 0;
381 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", queue_marshall(playing
));
383 sink_printf(ev_writer_sink(c
->w
), "259 nothing playing\n");
384 return 1; /* completed */
387 static const char *connection_host(struct conn
*c
) {
390 struct sockaddr_in in
;
391 struct sockaddr_in6 in6
;
397 /* get connection data */
399 if(getpeername(c
->fd
, &u
.sa
, &l
) < 0) {
400 error(errno
, "S%x error calling getpeername", c
->tag
);
403 if(c
->l
->pf
!= PF_UNIX
) {
404 if((n
= getnameinfo(&u
.sa
, l
,
405 host
, sizeof host
, 0, 0, NI_NUMERICHOST
))) {
406 error(0, "S%x error calling getnameinfo: %s", c
->tag
, gai_strerror(n
));
409 return xstrdup(host
);
414 static int c_user(struct conn
*c
,
416 int attribute((unused
)) nvec
) {
418 const char *res
, *host
, *password
;
422 sink_writes(ev_writer_sink(c
->w
), "530 already authenticated\n");
425 /* get connection data */
426 if(!(host
= connection_host(c
))) {
427 sink_writes(ev_writer_sink(c
->w
), "530 authentication failure\n");
431 k
= trackdb_getuserinfo(vec
[0]);
432 /* reject nonexistent users */
434 error(0, "S%x unknown user '%s' from %s", c
->tag
, vec
[0], host
);
435 sink_writes(ev_writer_sink(c
->w
), "530 authentication failed\n");
438 /* reject unconfirmed users */
439 if(kvp_get(k
, "confirmation")) {
440 error(0, "S%x unconfirmed user '%s' from %s", c
->tag
, vec
[0], host
);
441 sink_writes(ev_writer_sink(c
->w
), "530 authentication failed\n");
444 password
= kvp_get(k
, "password");
445 if(!password
) password
= "";
446 if(parse_rights(kvp_get(k
, "rights"), &rights
, 1)) {
447 error(0, "error parsing rights for %s", vec
[0]);
448 sink_writes(ev_writer_sink(c
->w
), "530 authentication failed\n");
451 /* check whether the response is right */
452 res
= authhash(c
->nonce
, sizeof c
->nonce
, password
,
453 config
->authorization_algorithm
);
454 if(wideopen
|| (res
&& !strcmp(res
, vec
[1]))) {
457 /* currently we only bother logging remote connections */
458 if(strcmp(host
, "local"))
459 info("S%x %s connected from %s", c
->tag
, vec
[0], host
);
461 c
->rights
|= RIGHT__LOCAL
;
462 sink_writes(ev_writer_sink(c
->w
), "230 OK\n");
465 /* oops, response was wrong */
466 info("S%x authentication failure for %s from %s", c
->tag
, vec
[0], host
);
467 sink_writes(ev_writer_sink(c
->w
), "530 authentication failed\n");
471 static int c_recent(struct conn
*c
,
472 char attribute((unused
)) **vec
,
473 int attribute((unused
)) nvec
) {
474 const struct queue_entry
*q
;
476 sink_writes(ev_writer_sink(c
->w
), "253 Tracks follow\n");
477 for(q
= phead
.next
; q
!= &phead
; q
= q
->next
)
478 sink_printf(ev_writer_sink(c
->w
), " %s\n", queue_marshall(q
));
479 sink_writes(ev_writer_sink(c
->w
), ".\n");
480 return 1; /* completed */
483 static int c_queue(struct conn
*c
,
484 char attribute((unused
)) **vec
,
485 int attribute((unused
)) nvec
) {
486 struct queue_entry
*q
;
491 sink_writes(ev_writer_sink(c
->w
), "253 Tracks follow\n");
492 if(playing_is_enabled() && !paused
) {
494 queue_fix_sofar(playing
);
495 if((l
= trackdb_get(playing
->track
, "_length"))
496 && (length
= atol(l
))) {
498 when
+= length
- playing
->sofar
+ config
->gap
;
501 /* Nothing is playing but playing is enabled, so whatever is
502 * first in the queue can be expected to start immediately. */
505 for(q
= qhead
.next
; q
!= &qhead
; q
= q
->next
) {
506 /* fill in estimated start time */
508 sink_printf(ev_writer_sink(c
->w
), " %s\n", queue_marshall(q
));
509 /* update for next track */
511 if((l
= trackdb_get(q
->track
, "_length"))
512 && (length
= atol(l
)))
513 when
+= length
+ config
->gap
;
518 sink_writes(ev_writer_sink(c
->w
), ".\n");
519 return 1; /* completed */
522 static int output_list(struct conn
*c
, char **vec
) {
524 sink_printf(ev_writer_sink(c
->w
), "%s\n", *vec
++);
525 sink_writes(ev_writer_sink(c
->w
), ".\n");
529 static int files_dirs(struct conn
*c
,
532 enum trackdb_listable what
) {
533 const char *dir
, *re
, *errstr
;
539 case 0: dir
= 0; re
= 0; break;
540 case 1: dir
= vec
[0]; re
= 0; break;
541 case 2: dir
= vec
[0]; re
= vec
[1]; break;
544 /* A bit of a bodge to make sure the args don't trample on cache keys */
545 if(dir
&& strchr(dir
, '\n')) {
546 sink_writes(ev_writer_sink(c
->w
), "550 invalid directory name\n");
549 if(re
&& strchr(re
, '\n')) {
550 sink_writes(ev_writer_sink(c
->w
), "550 invalid regexp\n");
553 /* We bother eliminating "" because the web interface is relatively
554 * likely to send it */
556 byte_xasprintf(&key
, "%d\n%s\n%s", (int)what
, dir ? dir
: "", re
);
557 fvec
= (char **)cache_get(&cache_files_type
, key
);
559 /* Got a cache hit, don't store the answer in the cache */
562 rec
= 0; /* quieten compiler */
564 /* Cache miss, we'll do the lookup and key != 0 so we'll store the answer
566 if(!(rec
= pcre_compile(re
, PCRE_CASELESS
|PCRE_UTF8
,
567 &errstr
, &erroffset
, 0))) {
568 sink_printf(ev_writer_sink(c
->w
), "550 Error compiling regexp: %s\n",
572 /* It only counts as a miss if the regexp was valid. */
573 ++cache_files_misses
;
576 /* No regexp, don't bother caching the result */
582 /* No cache hit (either because a miss, or because we did not look) so do
585 fvec
= trackdb_list(dir
, 0, what
, rec
);
587 fvec
= trackdb_list(0, 0, what
, rec
);
590 /* Put the answer in the cache */
591 cache_put(&cache_files_type
, key
, fvec
);
592 sink_writes(ev_writer_sink(c
->w
), "253 Listing follow\n");
593 return output_list(c
, fvec
);
596 static int c_files(struct conn
*c
,
599 return files_dirs(c
, vec
, nvec
, trackdb_files
);
602 static int c_dirs(struct conn
*c
,
605 return files_dirs(c
, vec
, nvec
, trackdb_directories
);
608 static int c_allfiles(struct conn
*c
,
611 return files_dirs(c
, vec
, nvec
, trackdb_directories
|trackdb_files
);
614 static int c_get(struct conn
*c
,
616 int attribute((unused
)) nvec
) {
619 if(vec
[1][0] != '_' && (v
= trackdb_get(vec
[0], vec
[1])))
620 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", quoteutf8(v
));
622 sink_writes(ev_writer_sink(c
->w
), "555 not found\n");
626 static int c_length(struct conn
*c
,
628 int attribute((unused
)) nvec
) {
629 const char *track
, *v
;
631 if(!(track
= trackdb_resolve(vec
[0]))) {
632 sink_writes(ev_writer_sink(c
->w
), "550 cannot resolve track\n");
635 if((v
= trackdb_get(track
, "_length")))
636 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", quoteutf8(v
));
638 sink_writes(ev_writer_sink(c
->w
), "550 not found\n");
642 static int c_set(struct conn
*c
,
644 int attribute((unused
)) nvec
) {
645 if(vec
[1][0] != '_' && !trackdb_set(vec
[0], vec
[1], vec
[2]))
646 sink_writes(ev_writer_sink(c
->w
), "250 OK\n");
648 sink_writes(ev_writer_sink(c
->w
), "550 not found\n");
652 static int c_prefs(struct conn
*c
,
654 int attribute((unused
)) nvec
) {
657 k
= trackdb_get_all(vec
[0]);
658 sink_writes(ev_writer_sink(c
->w
), "253 prefs follow\n");
659 for(; k
; k
= k
->next
)
660 if(k
->name
[0] != '_') /* omit internal values */
661 sink_printf(ev_writer_sink(c
->w
),
662 " %s %s\n", quoteutf8(k
->name
), quoteutf8(k
->value
));
663 sink_writes(ev_writer_sink(c
->w
), ".\n");
667 static int c_exists(struct conn
*c
,
669 int attribute((unused
)) nvec
) {
670 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", noyes
[trackdb_exists(vec
[0])]);
674 static void search_parse_error(const char *msg
, void *u
) {
675 *(const char **)u
= msg
;
678 static int c_search(struct conn
*c
,
680 int attribute((unused
)) nvec
) {
681 char **terms
, **results
;
682 int nterms
, nresults
, n
;
683 const char *e
= "unknown error";
685 /* This is a bit of a bodge. Initially it's there to make the eclient
686 * interface a bit more convenient to add searching to, but it has the more
687 * compelling advantage that if everything uses it, then interpretation of
688 * user-supplied search strings will be the same everywhere. */
689 if(!(terms
= split(vec
[0], &nterms
, SPLIT_QUOTES
, search_parse_error
, &e
))) {
690 sink_printf(ev_writer_sink(c
->w
), "550 %s\n", e
);
692 results
= trackdb_search(terms
, nterms
, &nresults
);
693 sink_printf(ev_writer_sink(c
->w
), "253 %d matches\n", nresults
);
694 for(n
= 0; n
< nresults
; ++n
)
695 sink_printf(ev_writer_sink(c
->w
), "%s\n", results
[n
]);
696 sink_writes(ev_writer_sink(c
->w
), ".\n");
701 static int c_random_enable(struct conn
*c
,
702 char attribute((unused
)) **vec
,
703 int attribute((unused
)) nvec
) {
704 enable_random(c
->who
, c
->ev
);
705 /* Enable implicitly unpauses if there is nothing playing */
706 if(paused
&& !playing
) resume_playing(c
->who
);
707 sink_writes(ev_writer_sink(c
->w
), "250 OK\n");
708 return 1; /* completed */
711 static int c_random_disable(struct conn
*c
,
712 char attribute((unused
)) **vec
,
713 int attribute((unused
)) nvec
) {
714 disable_random(c
->who
);
715 sink_writes(ev_writer_sink(c
->w
), "250 OK\n");
716 return 1; /* completed */
719 static int c_random_enabled(struct conn
*c
,
720 char attribute((unused
)) **vec
,
721 int attribute((unused
)) nvec
) {
722 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", noyes
[random_is_enabled()]);
723 return 1; /* completed */
726 static void got_stats(char *stats
, void *u
) {
727 struct conn
*const c
= u
;
729 sink_printf(ev_writer_sink(c
->w
), "253 stats\n%s\n.\n", stats
);
730 /* Now we can start processing commands again */
731 ev_reader_enable(c
->r
);
734 static int c_stats(struct conn
*c
,
735 char attribute((unused
)) **vec
,
736 int attribute((unused
)) nvec
) {
737 trackdb_stats_subprocess(c
->ev
, got_stats
, c
);
738 return 0; /* not yet complete */
741 static int c_volume(struct conn
*c
,
753 l
= r
= atoi(vec
[0]);
764 rights
= set ? RIGHT_VOLUME
: RIGHT_READ
;
765 if(!(c
->rights
& rights
)) {
766 error(0, "%s attempted to set volume but lacks required rights", c
->who
);
767 sink_writes(ev_writer_sink(c
->w
), "510 Prohibited\n");
770 if(mixer_control(-1/*as configured*/, &l
, &r
, set
))
771 sink_writes(ev_writer_sink(c
->w
), "550 error accessing mixer\n");
773 sink_printf(ev_writer_sink(c
->w
), "252 %d %d\n", l
, r
);
774 if(l
!= volume_left
|| r
!= volume_right
) {
777 snprintf(lb
, sizeof lb
, "%d", l
);
778 snprintf(rb
, sizeof rb
, "%d", r
);
779 eventlog("volume", lb
, rb
, (char *)0);
785 /** @brief Called when data arrives on a log connection
787 * We just discard all such data. The client may occasionally send data as a
790 static int logging_reader_callback(ev_source
attribute((unused
)) *ev
,
792 void attribute((unused
)) *ptr
,
794 int attribute((unused
)) eof
,
795 void attribute((unused
)) *u
) {
798 ev_reader_consume(reader
, bytes
);
800 /* Oops, that's all for now */
801 D(("logging reader eof"));
804 ev_writer_close(c
->w
);
808 remove_connection(c
);
813 static void logclient(const char *msg
, void *user
) {
814 struct conn
*c
= user
;
817 /* This connection has gone up in smoke for some reason */
818 eventlog_remove(c
->lo
);
821 sink_printf(ev_writer_sink(c
->w
), "%"PRIxMAX
" %s\n",
822 (uintmax_t)time(0), msg
);
825 static int c_log(struct conn
*c
,
826 char attribute((unused
)) **vec
,
827 int attribute((unused
)) nvec
) {
830 sink_writes(ev_writer_sink(c
->w
), "254 OK\n");
831 /* pump out initial state */
833 sink_printf(ev_writer_sink(c
->w
), "%"PRIxMAX
" state %s\n",
835 playing_is_enabled() ?
"enable_play" : "disable_play");
836 sink_printf(ev_writer_sink(c
->w
), "%"PRIxMAX
" state %s\n",
838 random_is_enabled() ?
"enable_random" : "disable_random");
839 sink_printf(ev_writer_sink(c
->w
), "%"PRIxMAX
" state %s\n",
841 paused ?
"pause" : "resume");
843 sink_printf(ev_writer_sink(c
->w
), "%"PRIxMAX
" state playing\n",
846 sink_printf(ev_writer_sink(c
->w
), "%"PRIxMAX
" volume %d %d\n",
847 (uintmax_t)now
, volume_left
, volume_right
);
848 c
->lo
= xmalloc(sizeof *c
->lo
);
849 c
->lo
->fn
= logclient
;
852 c
->reader
= logging_reader_callback
;
856 /** @brief Test whether a move is allowed
857 * @param c Connection
858 * @param qs List of IDs on queue
859 * @param nqs Number of IDs
860 * @return 0 if move is prohibited, non-0 if it is allowed
862 static int has_move_rights(struct conn
*c
, struct queue_entry
**qs
, int nqs
) {
863 for(; nqs
> 0; ++qs
, --nqs
) {
864 struct queue_entry
*const q
= *qs
;
866 if(!right_movable(c
->rights
, c
->who
, q
))
872 static int c_move(struct conn
*c
,
874 int attribute((unused
)) nvec
) {
875 struct queue_entry
*q
;
878 if(!(q
= queue_find(vec
[0]))) {
879 sink_writes(ev_writer_sink(c
->w
), "550 no such track on the queue\n");
882 if(!has_move_rights(c
, &q
, 1)) {
883 error(0, "%s attempted move but lacks required rights", c
->who
);
884 sink_writes(ev_writer_sink(c
->w
),
885 "510 Not authorized to move that track\n");
888 n
= queue_move(q
, atoi(vec
[1]), c
->who
);
889 sink_printf(ev_writer_sink(c
->w
), "252 %d\n", n
);
890 /* If we've moved to the head of the queue then prepare the track. */
896 static int c_moveafter(struct conn
*c
,
898 int attribute((unused
)) nvec
) {
899 struct queue_entry
*q
, **qs
;
903 if(!(q
= queue_find(vec
[0]))) {
904 sink_writes(ev_writer_sink(c
->w
), "550 no such track on the queue\n");
911 qs
= xcalloc(nvec
, sizeof *qs
);
912 for(n
= 0; n
< nvec
; ++n
)
913 if(!(qs
[n
] = queue_find(vec
[n
]))) {
914 sink_writes(ev_writer_sink(c
->w
), "550 no such track on the queue\n");
917 if(!has_move_rights(c
, qs
, nvec
)) {
918 error(0, "%s attempted moveafter but lacks required rights", c
->who
);
919 sink_writes(ev_writer_sink(c
->w
),
920 "510 Not authorized to move those tracks\n");
923 queue_moveafter(q
, nvec
, qs
, c
->who
);
924 sink_printf(ev_writer_sink(c
->w
), "250 Moved tracks\n");
925 /* If we've moved to the head of the queue then prepare the track. */
931 static int c_part(struct conn
*c
,
933 int attribute((unused
)) nvec
) {
934 sink_printf(ev_writer_sink(c
->w
), "252 %s\n",
935 quoteutf8(trackdb_getpart(vec
[0], vec
[1], vec
[2])));
939 static int c_resolve(struct conn
*c
,
941 int attribute((unused
)) nvec
) {
944 if(!(track
= trackdb_resolve(vec
[0]))) {
945 sink_writes(ev_writer_sink(c
->w
), "550 cannot resolve track\n");
948 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", quoteutf8(track
));
952 static int c_tags(struct conn
*c
,
953 char attribute((unused
)) **vec
,
954 int attribute((unused
)) nvec
) {
955 char **tags
= trackdb_alltags();
957 sink_printf(ev_writer_sink(c
->w
), "253 Tag list follows\n");
959 sink_printf(ev_writer_sink(c
->w
), "%s%s\n",
960 **tags
== '.' ?
"." : "", *tags
);
963 sink_writes(ev_writer_sink(c
->w
), ".\n");
964 return 1; /* completed */
967 static int c_set_global(struct conn
*c
,
969 int attribute((unused
)) nvec
) {
970 if(vec
[0][0] == '_') {
971 sink_writes(ev_writer_sink(c
->w
), "550 cannot set internal global preferences\n");
974 trackdb_set_global(vec
[0], vec
[1], c
->who
);
975 sink_printf(ev_writer_sink(c
->w
), "250 OK\n");
979 static int c_get_global(struct conn
*c
,
981 int attribute((unused
)) nvec
) {
982 const char *s
= trackdb_get_global(vec
[0]);
985 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", quoteutf8(s
));
987 sink_writes(ev_writer_sink(c
->w
), "555 not found\n");
991 static int c_nop(struct conn
*c
,
992 char attribute((unused
)) **vec
,
993 int attribute((unused
)) nvec
) {
994 sink_printf(ev_writer_sink(c
->w
), "250 Quack\n");
998 static int c_new(struct conn
*c
,
1008 if(max
<= 0 || max
> config
->new_max
)
1009 max
= config
->new_max
;
1010 tracks
= trackdb_new(0, max
);
1011 sink_printf(ev_writer_sink(c
->w
), "253 New track list follows\n");
1014 sink_printf(ev_writer_sink(c
->w
), "%s%s\n",
1015 **tracks
== '.' ?
"." : "", *tracks
);
1018 sink_writes(ev_writer_sink(c
->w
), ".\n");
1019 return 1; /* completed */
1023 static int c_rtp_address(struct conn
*c
,
1024 char attribute((unused
)) **vec
,
1025 int attribute((unused
)) nvec
) {
1026 if(config
->api
== BACKEND_NETWORK
) {
1027 sink_printf(ev_writer_sink(c
->w
), "252 %s %s\n",
1028 quoteutf8(config
->broadcast
.s
[0]),
1029 quoteutf8(config
->broadcast
.s
[1]));
1031 sink_writes(ev_writer_sink(c
->w
), "550 No RTP\n");
1035 static int c_cookie(struct conn
*c
,
1037 int attribute((unused
)) nvec
) {
1042 /* Can't log in twice on the same connection */
1044 sink_writes(ev_writer_sink(c
->w
), "530 already authenticated\n");
1047 /* Get some kind of peer identifcation */
1048 if(!(host
= connection_host(c
))) {
1049 sink_writes(ev_writer_sink(c
->w
), "530 authentication failure\n");
1052 /* Check the cookie */
1053 user
= verify_cookie(vec
[0], &rights
);
1055 sink_writes(ev_writer_sink(c
->w
), "530 authentication failure\n");
1062 if(strcmp(host
, "local"))
1063 info("S%x %s connected with cookie from %s", c
->tag
, user
, host
);
1065 c
->rights
|= RIGHT__LOCAL
;
1066 /* Response contains username so client knows who they are acting as */
1067 sink_printf(ev_writer_sink(c
->w
), "232 %s\n", quoteutf8(user
));
1071 static int c_make_cookie(struct conn
*c
,
1072 char attribute((unused
)) **vec
,
1073 int attribute((unused
)) nvec
) {
1074 const char *cookie
= make_cookie(c
->who
);
1077 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", quoteutf8(cookie
));
1079 sink_writes(ev_writer_sink(c
->w
), "550 Cannot create cookie\n");
1083 static int c_revoke(struct conn
*c
,
1084 char attribute((unused
)) **vec
,
1085 int attribute((unused
)) nvec
) {
1087 revoke_cookie(c
->cookie
);
1088 sink_writes(ev_writer_sink(c
->w
), "250 OK\n");
1090 sink_writes(ev_writer_sink(c
->w
), "550 Did not log in with cookie\n");
1094 static int c_adduser(struct conn
*c
,
1099 if(!config
->remote_userman
&& !(c
->rights
& RIGHT__LOCAL
)) {
1100 error(0, "S%x: remote adduser", c
->tag
);
1101 sink_writes(ev_writer_sink(c
->w
), "550 Remote user management is disabled\n");
1106 if(parse_rights(vec
[2], 0, 1)) {
1107 sink_writes(ev_writer_sink(c
->w
), "550 Invalid rights list\n");
1111 rights
= config
->default_rights
;
1112 if(trackdb_adduser(vec
[0], vec
[1], rights
,
1113 0/*email*/, 0/*confirmation*/))
1114 sink_writes(ev_writer_sink(c
->w
), "550 Cannot create user\n");
1116 sink_writes(ev_writer_sink(c
->w
), "250 User created\n");
1120 static int c_deluser(struct conn
*c
,
1122 int attribute((unused
)) nvec
) {
1125 if(!config
->remote_userman
&& !(c
->rights
& RIGHT__LOCAL
)) {
1126 error(0, "S%x: remote deluser", c
->tag
);
1127 sink_writes(ev_writer_sink(c
->w
), "550 Remote user management is disabled\n");
1130 if(trackdb_deluser(vec
[0])) {
1131 sink_writes(ev_writer_sink(c
->w
), "550 Cannot delete user\n");
1134 /* Zap connections belonging to deleted user */
1135 for(d
= connections
; d
; d
= d
->next
)
1136 if(!strcmp(d
->who
, vec
[0]))
1138 sink_writes(ev_writer_sink(c
->w
), "250 User deleted\n");
1142 static int c_edituser(struct conn
*c
,
1144 int attribute((unused
)) nvec
) {
1147 if(!config
->remote_userman
&& !(c
->rights
& RIGHT__LOCAL
)) {
1148 error(0, "S%x: remote edituser", c
->tag
);
1149 sink_writes(ev_writer_sink(c
->w
), "550 Remote user management is disabled\n");
1152 /* RIGHT_ADMIN can do anything; otherwise you can only set your own email
1153 * address and password. */
1154 if((c
->rights
& RIGHT_ADMIN
)
1155 || (!strcmp(c
->who
, vec
[0])
1156 && (!strcmp(vec
[1], "email")
1157 || !strcmp(vec
[1], "password")))) {
1158 if(trackdb_edituserinfo(vec
[0], vec
[1], vec
[2])) {
1159 sink_writes(ev_writer_sink(c
->w
), "550 Failed to change setting\n");
1162 if(!strcmp(vec
[1], "password")) {
1163 /* Zap all connections for this user after a password change */
1164 for(d
= connections
; d
; d
= d
->next
)
1165 if(!strcmp(d
->who
, vec
[0]))
1167 } else if(!strcmp(vec
[1], "rights")) {
1168 /* Update rights for this user */
1171 if(parse_rights(vec
[2], &r
, 1))
1172 for(d
= connections
; d
; d
= d
->next
)
1173 if(!strcmp(d
->who
, vec
[0]))
1176 sink_writes(ev_writer_sink(c
->w
), "250 OK\n");
1178 error(0, "%s attempted edituser but lacks required rights", c
->who
);
1179 sink_writes(ev_writer_sink(c
->w
), "510 Restricted to administrators\n");
1184 static int c_userinfo(struct conn
*c
,
1185 char attribute((unused
)) **vec
,
1186 int attribute((unused
)) nvec
) {
1190 /* We allow remote querying of rights so that clients can figure out what
1191 * they're allowed to do */
1192 if(!config
->remote_userman
1193 && !(c
->rights
& RIGHT__LOCAL
)
1194 && strcmp(vec
[1], "rights")) {
1195 error(0, "S%x: remote userinfo %s %s", c
->tag
, vec
[0], vec
[1]);
1196 sink_writes(ev_writer_sink(c
->w
), "550 Remote user management is disabled\n");
1199 /* RIGHT_ADMIN allows anything; otherwise you can only get your own email
1200 * address and rights list. */
1201 if((c
->rights
& RIGHT_ADMIN
)
1202 || (!strcmp(c
->who
, vec
[0])
1203 && (!strcmp(vec
[1], "email")
1204 || !strcmp(vec
[1], "rights")))) {
1205 if((k
= trackdb_getuserinfo(vec
[0])))
1206 if((value
= kvp_get(k
, vec
[1])))
1207 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", quoteutf8(value
));
1209 sink_writes(ev_writer_sink(c
->w
), "555 Not set\n");
1211 sink_writes(ev_writer_sink(c
->w
), "550 No such user\n");
1213 error(0, "%s attempted userinfo but lacks required rights", c
->who
);
1214 sink_writes(ev_writer_sink(c
->w
), "510 Restricted to administrators\n");
1219 static int c_users(struct conn
*c
,
1220 char attribute((unused
)) **vec
,
1221 int attribute((unused
)) nvec
) {
1222 /* TODO de-dupe with c_tags */
1223 char **users
= trackdb_listusers();
1225 sink_writes(ev_writer_sink(c
->w
), "253 User list follows\n");
1227 sink_printf(ev_writer_sink(c
->w
), "%s%s\n",
1228 **users
== '.' ?
"." : "", *users
);
1231 sink_writes(ev_writer_sink(c
->w
), ".\n");
1232 return 1; /* completed */
1235 /** @brief Base64 mapping table for confirmation strings
1237 * This is used with generic_to_base64() and generic_base64(). We cannot use
1238 * the MIME table as that contains '+' and '=' which get quoted when
1239 * URL-encoding. (The CGI still does the URL encoding but it is desirable to
1240 * avoid it being necessary.)
1242 static const char confirm_base64_table
[] =
1243 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789/.*";
1245 static int c_register(struct conn
*c
,
1247 int attribute((unused
)) nvec
) {
1252 /* The confirmation string is base64(username;nonce) */
1253 bufsize
= strlen(vec
[0]) + CONFIRM_SIZE
+ 2;
1254 buf
= xmalloc_noptr(bufsize
);
1255 offset
= byte_snprintf(buf
, bufsize
, "%s;", vec
[0]);
1256 gcry_randomize(buf
+ offset
, CONFIRM_SIZE
, GCRY_STRONG_RANDOM
);
1257 cs
= generic_to_base64((uint8_t *)buf
, offset
+ CONFIRM_SIZE
,
1258 confirm_base64_table
);
1259 if(trackdb_adduser(vec
[0], vec
[1], config
->default_rights
, vec
[2], cs
))
1260 sink_writes(ev_writer_sink(c
->w
), "550 Cannot create user\n");
1262 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", quoteutf8(cs
));
1266 static int c_confirm(struct conn
*c
,
1268 int attribute((unused
)) nvec
) {
1274 /* Get some kind of peer identifcation */
1275 if(!(host
= connection_host(c
))) {
1276 sink_writes(ev_writer_sink(c
->w
), "530 Authentication failure\n");
1279 if(!(user
= generic_base64(vec
[0], &nuser
, confirm_base64_table
))
1280 || !(sep
= memchr(user
, ';', nuser
))) {
1281 sink_writes(ev_writer_sink(c
->w
), "550 Malformed confirmation string\n");
1285 if(trackdb_confirm(user
, vec
[0], &rights
))
1286 sink_writes(ev_writer_sink(c
->w
), "550 Incorrect confirmation string\n");
1291 if(strcmp(host
, "local"))
1292 info("S%x %s confirmed from %s", c
->tag
, user
, host
);
1294 c
->rights
|= RIGHT__LOCAL
;
1295 /* Response contains username so client knows who they are acting as */
1296 sink_printf(ev_writer_sink(c
->w
), "232 %s\n", quoteutf8(user
));
1301 static int sent_reminder(ev_source
attribute((unused
)) *ev
,
1302 pid_t
attribute((unused
)) pid
,
1304 const struct rusage
attribute((unused
)) *rusage
,
1306 struct conn
*const c
= u
;
1308 /* Tell the client what went down */
1310 sink_writes(ev_writer_sink(c
->w
), "250 OK\n");
1312 error(0, "reminder subprocess %s", wstat(status
));
1313 sink_writes(ev_writer_sink(c
->w
), "550 Cannot send a reminder email\n");
1315 /* Re-enable this connection */
1316 ev_reader_enable(c
->r
);
1320 static int c_reminder(struct conn
*c
,
1322 int attribute((unused
)) nvec
) {
1324 const char *password
, *email
, *text
, *encoding
, *charset
, *content_type
;
1329 static hash
*last_reminder
;
1331 if(!config
->mail_sender
) {
1332 error(0, "cannot send password reminders because mail_sender not set");
1333 sink_writes(ev_writer_sink(c
->w
), "550 Cannot send a reminder email\n");
1336 if(!(k
= trackdb_getuserinfo(vec
[0]))) {
1337 error(0, "reminder for user '%s' who does not exist", vec
[0]);
1338 sink_writes(ev_writer_sink(c
->w
), "550 Cannot send a reminder email\n");
1341 if(!(email
= kvp_get(k
, "email"))
1342 || !strchr(email
, '@')) {
1343 error(0, "user '%s' has no valid email address", vec
[0]);
1344 sink_writes(ev_writer_sink(c
->w
), "550 Cannot send a reminder email\n");
1347 if(!(password
= kvp_get(k
, "password"))
1349 error(0, "user '%s' has no password", vec
[0]);
1350 sink_writes(ev_writer_sink(c
->w
), "550 Cannot send a reminder email\n");
1353 /* Rate-limit reminders. This hash is bounded in size by the number of
1354 * users. If this is actually a problem for anyone then we can periodically
1357 last_reminder
= hash_new(sizeof (time_t));
1358 last
= hash_find(last_reminder
, vec
[0]);
1360 if(last
&& now
< *last
+ config
->reminder_interval
) {
1361 error(0, "sent a password reminder to '%s' too recently", vec
[0]);
1362 sink_writes(ev_writer_sink(c
->w
), "550 Cannot send a reminder email\n");
1365 /* Send the reminder */
1366 /* TODO this should be templatized and to some extent merged with
1367 * the code in act_register() */
1368 byte_xasprintf((char **)&text
,
1369 "Someone requested that you be sent a reminder of your DisOrder password.\n"
1370 "Your password is:\n"
1373 if(!(text
= mime_encode_text(text
, &charset
, &encoding
)))
1374 fatal(0, "cannot encode email");
1375 byte_xasprintf((char **)&content_type
, "text/plain;charset=%s",
1376 quote822(charset
, 0));
1377 pid
= sendmail_subprocess("", config
->mail_sender
, email
,
1378 "DisOrder password reminder",
1379 encoding
, content_type
, text
);
1381 sink_writes(ev_writer_sink(c
->w
), "550 Cannot send a reminder email\n");
1384 hash_add(last_reminder
, vec
[0], &now
, HASH_INSERT_OR_REPLACE
);
1385 info("sending a passsword reminder to user '%s'", vec
[0]);
1386 /* We can only continue when the subprocess finishes */
1387 ev_child(c
->ev
, pid
, 0, sent_reminder
, c
);
1391 static const struct command
{
1392 /** @brief Command name */
1395 /** @brief Minimum number of arguments */
1398 /** @brief Maximum number of arguments */
1401 /** @brief Function to process command */
1402 int (*fn
)(struct conn
*, char **, int);
1404 /** @brief Rights required to execute command
1406 * 0 means that the command can be issued without logging in. If multiple
1407 * bits are listed here any of those rights will do.
1411 { "adduser", 2, 3, c_adduser
, RIGHT_ADMIN
|RIGHT__LOCAL
},
1412 { "allfiles", 0, 2, c_allfiles
, RIGHT_READ
},
1413 { "confirm", 1, 1, c_confirm
, 0 },
1414 { "cookie", 1, 1, c_cookie
, 0 },
1415 { "deluser", 1, 1, c_deluser
, RIGHT_ADMIN
|RIGHT__LOCAL
},
1416 { "dirs", 0, 2, c_dirs
, RIGHT_READ
},
1417 { "disable", 0, 1, c_disable
, RIGHT_GLOBAL_PREFS
},
1418 { "edituser", 3, 3, c_edituser
, RIGHT_ADMIN
|RIGHT_USERINFO
},
1419 { "enable", 0, 0, c_enable
, RIGHT_GLOBAL_PREFS
},
1420 { "enabled", 0, 0, c_enabled
, RIGHT_READ
},
1421 { "exists", 1, 1, c_exists
, RIGHT_READ
},
1422 { "files", 0, 2, c_files
, RIGHT_READ
},
1423 { "get", 2, 2, c_get
, RIGHT_READ
},
1424 { "get-global", 1, 1, c_get_global
, RIGHT_READ
},
1425 { "length", 1, 1, c_length
, RIGHT_READ
},
1426 { "log", 0, 0, c_log
, RIGHT_READ
},
1427 { "make-cookie", 0, 0, c_make_cookie
, RIGHT_READ
},
1428 { "move", 2, 2, c_move
, RIGHT_MOVE__MASK
},
1429 { "moveafter", 1, INT_MAX
, c_moveafter
, RIGHT_MOVE__MASK
},
1430 { "new", 0, 1, c_new
, RIGHT_READ
},
1431 { "nop", 0, 0, c_nop
, 0 },
1432 { "part", 3, 3, c_part
, RIGHT_READ
},
1433 { "pause", 0, 0, c_pause
, RIGHT_PAUSE
},
1434 { "play", 1, 1, c_play
, RIGHT_PLAY
},
1435 { "playing", 0, 0, c_playing
, RIGHT_READ
},
1436 { "prefs", 1, 1, c_prefs
, RIGHT_READ
},
1437 { "queue", 0, 0, c_queue
, RIGHT_READ
},
1438 { "random-disable", 0, 0, c_random_disable
, RIGHT_GLOBAL_PREFS
},
1439 { "random-enable", 0, 0, c_random_enable
, RIGHT_GLOBAL_PREFS
},
1440 { "random-enabled", 0, 0, c_random_enabled
, RIGHT_READ
},
1441 { "recent", 0, 0, c_recent
, RIGHT_READ
},
1442 { "reconfigure", 0, 0, c_reconfigure
, RIGHT_ADMIN
},
1443 { "register", 3, 3, c_register
, RIGHT_REGISTER
|RIGHT__LOCAL
},
1444 { "reminder", 1, 1, c_reminder
, RIGHT__LOCAL
},
1445 { "remove", 1, 1, c_remove
, RIGHT_REMOVE__MASK
},
1446 { "rescan", 0, 0, c_rescan
, RIGHT_RESCAN
},
1447 { "resolve", 1, 1, c_resolve
, RIGHT_READ
},
1448 { "resume", 0, 0, c_resume
, RIGHT_PAUSE
},
1449 { "revoke", 0, 0, c_revoke
, RIGHT_READ
},
1450 { "rtp-address", 0, 0, c_rtp_address
, 0 },
1451 { "scratch", 0, 1, c_scratch
, RIGHT_SCRATCH__MASK
},
1452 { "search", 1, 1, c_search
, RIGHT_READ
},
1453 { "set", 3, 3, c_set
, RIGHT_PREFS
, },
1454 { "set-global", 2, 2, c_set_global
, RIGHT_GLOBAL_PREFS
},
1455 { "shutdown", 0, 0, c_shutdown
, RIGHT_ADMIN
},
1456 { "stats", 0, 0, c_stats
, RIGHT_READ
},
1457 { "tags", 0, 0, c_tags
, RIGHT_READ
},
1458 { "unset", 2, 2, c_set
, RIGHT_PREFS
},
1459 { "unset-global", 1, 1, c_set_global
, RIGHT_GLOBAL_PREFS
},
1460 { "user", 2, 2, c_user
, 0 },
1461 { "userinfo", 2, 2, c_userinfo
, RIGHT_READ
},
1462 { "users", 0, 0, c_users
, RIGHT_READ
},
1463 { "version", 0, 0, c_version
, RIGHT_READ
},
1464 { "volume", 0, 2, c_volume
, RIGHT_READ
|RIGHT_VOLUME
}
1467 static void command_error(const char *msg
, void *u
) {
1470 sink_printf(ev_writer_sink(c
->w
), "500 parse error: %s\n", msg
);
1473 /* process a command. Return 1 if complete, 0 if incomplete. */
1474 static int command(struct conn
*c
, char *line
) {
1478 D(("server command %s", line
));
1479 /* We force everything into NFC as early as possible */
1480 if(!(line
= utf8_compose_canon(line
, strlen(line
), 0))) {
1481 sink_writes(ev_writer_sink(c
->w
), "500 cannot normalize command\n");
1484 if(!(vec
= split(line
, &nvec
, SPLIT_QUOTES
, command_error
, c
))) {
1485 sink_writes(ev_writer_sink(c
->w
), "500 cannot parse command\n");
1489 sink_writes(ev_writer_sink(c
->w
), "500 do what?\n");
1492 if((n
= TABLE_FIND(commands
, struct command
, name
, vec
[0])) < 0)
1493 sink_writes(ev_writer_sink(c
->w
), "500 unknown command\n");
1495 if(commands
[n
].rights
1496 && !(c
->rights
& commands
[n
].rights
)) {
1497 error(0, "%s attempted %s but lacks required rights", c
->who ? c
->who
: "NULL",
1499 sink_writes(ev_writer_sink(c
->w
), "510 Prohibited\n");
1504 if(nvec
< commands
[n
].minargs
) {
1505 sink_writes(ev_writer_sink(c
->w
), "500 missing argument(s)\n");
1508 if(nvec
> commands
[n
].maxargs
) {
1509 sink_writes(ev_writer_sink(c
->w
), "500 too many arguments\n");
1512 return commands
[n
].fn(c
, vec
, nvec
);
1514 return 1; /* completed */
1517 /* redirect to the right reader callback for our current state */
1518 static int redirect_reader_callback(ev_source
*ev
,
1526 return c
->reader(ev
, reader
, ptr
, bytes
, eof
, u
);
1529 /* the main command reader */
1530 static int reader_callback(ev_source
attribute((unused
)) *ev
,
1540 D(("server reader_callback"));
1541 while((eol
= memchr(ptr
, '\n', bytes
))) {
1543 ev_reader_consume(reader
, eol
- (char *)ptr
);
1544 complete
= command(c
, ptr
);
1545 bytes
-= (eol
- (char *)ptr
);
1548 /* the command had better have set a new reader callback */
1550 /* there are further bytes to read, or we are at eof; arrange for the
1551 * command's reader callback to handle them */
1552 return ev_reader_incomplete(reader
);
1553 /* nothing's going on right now */
1556 /* command completed, we can go around and handle the next one */
1560 error(0, "S%x unterminated line", c
->tag
);
1561 D(("normal reader close"));
1564 D(("close associated writer"));
1565 ev_writer_close(c
->w
);
1568 remove_connection(c
);
1573 static int listen_callback(ev_source
*ev
,
1575 const struct sockaddr
attribute((unused
)) *remote
,
1576 socklen_t
attribute((unused
)) rlen
,
1578 const struct listener
*l
= u
;
1579 struct conn
*c
= xmalloc(sizeof *c
);
1580 static unsigned tags
;
1582 D(("server listen_callback fd %d (%s)", fd
, l
->name
));
1587 c
->w
= ev_writer_new(ev
, fd
, writer_error
, c
,
1589 c
->r
= ev_reader_new(ev
, fd
, redirect_reader_callback
, reader_error
, c
,
1593 c
->reader
= reader_callback
;
1596 gcry_randomize(c
->nonce
, sizeof c
->nonce
, GCRY_STRONG_RANDOM
);
1597 sink_printf(ev_writer_sink(c
->w
), "231 %d %s %s\n",
1599 config
->authorization_algorithm
,
1600 hex(c
->nonce
, sizeof c
->nonce
));
1604 int server_start(ev_source
*ev
, int pf
,
1605 size_t socklen
, const struct sockaddr
*sa
,
1608 struct listener
*l
= xmalloc(sizeof *l
);
1609 static const int one
= 1;
1611 D(("server_init socket %s", name
));
1612 fd
= xsocket(pf
, SOCK_STREAM
, 0);
1613 xsetsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &one
, sizeof one
);
1614 if(bind(fd
, sa
, socklen
) < 0) {
1615 error(errno
, "error binding to %s", name
);
1623 if(ev_listen(ev
, fd
, listen_callback
, l
, "server listener"))
1628 int server_stop(ev_source
*ev
, int fd
) {
1630 return ev_listen_cancel(ev
, fd
);