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 /* If we removed a random track then add another one. */
276 if(q
->state
== playing_random
)
278 /* Prepare whatever the next head track is. */
279 if(qhead
.next
!= &qhead
)
280 prepare(c
->ev
, qhead
.next
);
282 sink_writes(ev_writer_sink(c
->w
), "250 removed\n");
283 return 1; /* completed */
286 static int c_scratch(struct conn
*c
,
290 sink_writes(ev_writer_sink(c
->w
), "250 nothing is playing\n");
291 return 1; /* completed */
293 /* TODO there is a bug here: if we specify an ID but it's not the currently
294 * playing track then you will get 550 if you weren't authorized to scratch
295 * the currently playing track. */
296 if(!right_scratchable(c
->rights
, c
->who
, playing
)) {
297 error(0, "%s attempted scratch but lacks required rights", c
->who
);
298 sink_writes(ev_writer_sink(c
->w
),
299 "510 Not authorized to scratch that track\n");
302 scratch(c
->who
, nvec
== 1 ? vec
[0] : 0);
303 /* If you scratch an unpaused track then it is automatically unpaused */
305 sink_writes(ev_writer_sink(c
->w
), "250 scratched\n");
306 return 1; /* completed */
309 static int c_pause(struct conn
*c
,
310 char attribute((unused
)) **vec
,
311 int attribute((unused
)) nvec
) {
313 sink_writes(ev_writer_sink(c
->w
), "250 nothing is playing\n");
314 return 1; /* completed */
317 sink_writes(ev_writer_sink(c
->w
), "250 already paused\n");
318 return 1; /* completed */
320 if(pause_playing(c
->who
) < 0)
321 sink_writes(ev_writer_sink(c
->w
), "550 cannot pause this track\n");
323 sink_writes(ev_writer_sink(c
->w
), "250 paused\n");
327 static int c_resume(struct conn
*c
,
328 char attribute((unused
)) **vec
,
329 int attribute((unused
)) nvec
) {
331 sink_writes(ev_writer_sink(c
->w
), "250 not paused\n");
332 return 1; /* completed */
334 resume_playing(c
->who
);
335 sink_writes(ev_writer_sink(c
->w
), "250 paused\n");
339 static int c_shutdown(struct conn
*c
,
340 char attribute((unused
)) **vec
,
341 int attribute((unused
)) nvec
) {
342 info("S%x shut down by %s", c
->tag
, c
->who
);
343 sink_writes(ev_writer_sink(c
->w
), "250 shutting down\n");
344 ev_writer_flush(c
->w
);
348 static int c_reconfigure(struct conn
*c
,
349 char attribute((unused
)) **vec
,
350 int attribute((unused
)) nvec
) {
351 info("S%x reconfigure by %s", c
->tag
, c
->who
);
352 if(reconfigure(c
->ev
, 1))
353 sink_writes(ev_writer_sink(c
->w
), "550 error reading new config\n");
355 sink_writes(ev_writer_sink(c
->w
), "250 installed new config\n");
356 return 1; /* completed */
359 static int c_rescan(struct conn
*c
,
360 char attribute((unused
)) **vec
,
361 int attribute((unused
)) nvec
) {
362 info("S%x rescan by %s", c
->tag
, c
->who
);
363 trackdb_rescan(c
->ev
, 1/*check*/);
364 sink_writes(ev_writer_sink(c
->w
), "250 initiated rescan\n");
365 return 1; /* completed */
368 static int c_version(struct conn
*c
,
369 char attribute((unused
)) **vec
,
370 int attribute((unused
)) nvec
) {
371 /* VERSION had better only use the basic character set */
372 sink_printf(ev_writer_sink(c
->w
), "251 %s\n", disorder_short_version_string
);
373 return 1; /* completed */
376 static int c_playing(struct conn
*c
,
377 char attribute((unused
)) **vec
,
378 int attribute((unused
)) nvec
) {
380 queue_fix_sofar(playing
);
381 playing
->expected
= 0;
382 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", queue_marshall(playing
));
384 sink_printf(ev_writer_sink(c
->w
), "259 nothing playing\n");
385 return 1; /* completed */
388 static const char *connection_host(struct conn
*c
) {
391 struct sockaddr_in in
;
392 struct sockaddr_in6 in6
;
398 /* get connection data */
400 if(getpeername(c
->fd
, &u
.sa
, &l
) < 0) {
401 error(errno
, "S%x error calling getpeername", c
->tag
);
404 if(c
->l
->pf
!= PF_UNIX
) {
405 if((n
= getnameinfo(&u
.sa
, l
,
406 host
, sizeof host
, 0, 0, NI_NUMERICHOST
))) {
407 error(0, "S%x error calling getnameinfo: %s", c
->tag
, gai_strerror(n
));
410 return xstrdup(host
);
415 static int c_user(struct conn
*c
,
417 int attribute((unused
)) nvec
) {
419 const char *res
, *host
, *password
;
423 sink_writes(ev_writer_sink(c
->w
), "530 already authenticated\n");
426 /* get connection data */
427 if(!(host
= connection_host(c
))) {
428 sink_writes(ev_writer_sink(c
->w
), "530 authentication failure\n");
432 k
= trackdb_getuserinfo(vec
[0]);
433 /* reject nonexistent users */
435 error(0, "S%x unknown user '%s' from %s", c
->tag
, vec
[0], host
);
436 sink_writes(ev_writer_sink(c
->w
), "530 authentication failed\n");
439 /* reject unconfirmed users */
440 if(kvp_get(k
, "confirmation")) {
441 error(0, "S%x unconfirmed user '%s' from %s", c
->tag
, vec
[0], host
);
442 sink_writes(ev_writer_sink(c
->w
), "530 authentication failed\n");
445 password
= kvp_get(k
, "password");
446 if(!password
) password
= "";
447 if(parse_rights(kvp_get(k
, "rights"), &rights
, 1)) {
448 error(0, "error parsing rights for %s", vec
[0]);
449 sink_writes(ev_writer_sink(c
->w
), "530 authentication failed\n");
452 /* check whether the response is right */
453 res
= authhash(c
->nonce
, sizeof c
->nonce
, password
,
454 config
->authorization_algorithm
);
455 if(wideopen
|| (res
&& !strcmp(res
, vec
[1]))) {
458 /* currently we only bother logging remote connections */
459 if(strcmp(host
, "local"))
460 info("S%x %s connected from %s", c
->tag
, vec
[0], host
);
462 c
->rights
|= RIGHT__LOCAL
;
463 sink_writes(ev_writer_sink(c
->w
), "230 OK\n");
466 /* oops, response was wrong */
467 info("S%x authentication failure for %s from %s", c
->tag
, vec
[0], host
);
468 sink_writes(ev_writer_sink(c
->w
), "530 authentication failed\n");
472 static int c_recent(struct conn
*c
,
473 char attribute((unused
)) **vec
,
474 int attribute((unused
)) nvec
) {
475 const struct queue_entry
*q
;
477 sink_writes(ev_writer_sink(c
->w
), "253 Tracks follow\n");
478 for(q
= phead
.next
; q
!= &phead
; q
= q
->next
)
479 sink_printf(ev_writer_sink(c
->w
), " %s\n", queue_marshall(q
));
480 sink_writes(ev_writer_sink(c
->w
), ".\n");
481 return 1; /* completed */
484 static int c_queue(struct conn
*c
,
485 char attribute((unused
)) **vec
,
486 int attribute((unused
)) nvec
) {
487 struct queue_entry
*q
;
492 sink_writes(ev_writer_sink(c
->w
), "253 Tracks follow\n");
493 if(playing_is_enabled() && !paused
) {
495 queue_fix_sofar(playing
);
496 if((l
= trackdb_get(playing
->track
, "_length"))
497 && (length
= atol(l
))) {
499 when
+= length
- playing
->sofar
+ config
->gap
;
502 /* Nothing is playing but playing is enabled, so whatever is
503 * first in the queue can be expected to start immediately. */
506 for(q
= qhead
.next
; q
!= &qhead
; q
= q
->next
) {
507 /* fill in estimated start time */
509 sink_printf(ev_writer_sink(c
->w
), " %s\n", queue_marshall(q
));
510 /* update for next track */
512 if((l
= trackdb_get(q
->track
, "_length"))
513 && (length
= atol(l
)))
514 when
+= length
+ config
->gap
;
519 sink_writes(ev_writer_sink(c
->w
), ".\n");
520 return 1; /* completed */
523 static int output_list(struct conn
*c
, char **vec
) {
525 sink_printf(ev_writer_sink(c
->w
), "%s\n", *vec
++);
526 sink_writes(ev_writer_sink(c
->w
), ".\n");
530 static int files_dirs(struct conn
*c
,
533 enum trackdb_listable what
) {
534 const char *dir
, *re
, *errstr
;
540 case 0: dir
= 0; re
= 0; break;
541 case 1: dir
= vec
[0]; re
= 0; break;
542 case 2: dir
= vec
[0]; re
= vec
[1]; break;
545 /* A bit of a bodge to make sure the args don't trample on cache keys */
546 if(dir
&& strchr(dir
, '\n')) {
547 sink_writes(ev_writer_sink(c
->w
), "550 invalid directory name\n");
550 if(re
&& strchr(re
, '\n')) {
551 sink_writes(ev_writer_sink(c
->w
), "550 invalid regexp\n");
554 /* We bother eliminating "" because the web interface is relatively
555 * likely to send it */
557 byte_xasprintf(&key
, "%d\n%s\n%s", (int)what
, dir ? dir
: "", re
);
558 fvec
= (char **)cache_get(&cache_files_type
, key
);
560 /* Got a cache hit, don't store the answer in the cache */
563 rec
= 0; /* quieten compiler */
565 /* Cache miss, we'll do the lookup and key != 0 so we'll store the answer
567 if(!(rec
= pcre_compile(re
, PCRE_CASELESS
|PCRE_UTF8
,
568 &errstr
, &erroffset
, 0))) {
569 sink_printf(ev_writer_sink(c
->w
), "550 Error compiling regexp: %s\n",
573 /* It only counts as a miss if the regexp was valid. */
574 ++cache_files_misses
;
577 /* No regexp, don't bother caching the result */
583 /* No cache hit (either because a miss, or because we did not look) so do
586 fvec
= trackdb_list(dir
, 0, what
, rec
);
588 fvec
= trackdb_list(0, 0, what
, rec
);
591 /* Put the answer in the cache */
592 cache_put(&cache_files_type
, key
, fvec
);
593 sink_writes(ev_writer_sink(c
->w
), "253 Listing follow\n");
594 return output_list(c
, fvec
);
597 static int c_files(struct conn
*c
,
600 return files_dirs(c
, vec
, nvec
, trackdb_files
);
603 static int c_dirs(struct conn
*c
,
606 return files_dirs(c
, vec
, nvec
, trackdb_directories
);
609 static int c_allfiles(struct conn
*c
,
612 return files_dirs(c
, vec
, nvec
, trackdb_directories
|trackdb_files
);
615 static int c_get(struct conn
*c
,
617 int attribute((unused
)) nvec
) {
620 if(vec
[1][0] != '_' && (v
= trackdb_get(vec
[0], vec
[1])))
621 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", quoteutf8(v
));
623 sink_writes(ev_writer_sink(c
->w
), "555 not found\n");
627 static int c_length(struct conn
*c
,
629 int attribute((unused
)) nvec
) {
630 const char *track
, *v
;
632 if(!(track
= trackdb_resolve(vec
[0]))) {
633 sink_writes(ev_writer_sink(c
->w
), "550 cannot resolve track\n");
636 if((v
= trackdb_get(track
, "_length")))
637 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", quoteutf8(v
));
639 sink_writes(ev_writer_sink(c
->w
), "550 not found\n");
643 static int c_set(struct conn
*c
,
645 int attribute((unused
)) nvec
) {
646 if(vec
[1][0] != '_' && !trackdb_set(vec
[0], vec
[1], vec
[2]))
647 sink_writes(ev_writer_sink(c
->w
), "250 OK\n");
649 sink_writes(ev_writer_sink(c
->w
), "550 not found\n");
653 static int c_prefs(struct conn
*c
,
655 int attribute((unused
)) nvec
) {
658 k
= trackdb_get_all(vec
[0]);
659 sink_writes(ev_writer_sink(c
->w
), "253 prefs follow\n");
660 for(; k
; k
= k
->next
)
661 if(k
->name
[0] != '_') /* omit internal values */
662 sink_printf(ev_writer_sink(c
->w
),
663 " %s %s\n", quoteutf8(k
->name
), quoteutf8(k
->value
));
664 sink_writes(ev_writer_sink(c
->w
), ".\n");
668 static int c_exists(struct conn
*c
,
670 int attribute((unused
)) nvec
) {
671 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", noyes
[trackdb_exists(vec
[0])]);
675 static void search_parse_error(const char *msg
, void *u
) {
676 *(const char **)u
= msg
;
679 static int c_search(struct conn
*c
,
681 int attribute((unused
)) nvec
) {
682 char **terms
, **results
;
683 int nterms
, nresults
, n
;
684 const char *e
= "unknown error";
686 /* This is a bit of a bodge. Initially it's there to make the eclient
687 * interface a bit more convenient to add searching to, but it has the more
688 * compelling advantage that if everything uses it, then interpretation of
689 * user-supplied search strings will be the same everywhere. */
690 if(!(terms
= split(vec
[0], &nterms
, SPLIT_QUOTES
, search_parse_error
, &e
))) {
691 sink_printf(ev_writer_sink(c
->w
), "550 %s\n", e
);
693 results
= trackdb_search(terms
, nterms
, &nresults
);
694 sink_printf(ev_writer_sink(c
->w
), "253 %d matches\n", nresults
);
695 for(n
= 0; n
< nresults
; ++n
)
696 sink_printf(ev_writer_sink(c
->w
), "%s\n", results
[n
]);
697 sink_writes(ev_writer_sink(c
->w
), ".\n");
702 static int c_random_enable(struct conn
*c
,
703 char attribute((unused
)) **vec
,
704 int attribute((unused
)) nvec
) {
705 enable_random(c
->who
, c
->ev
);
706 /* Enable implicitly unpauses if there is nothing playing */
707 if(paused
&& !playing
) resume_playing(c
->who
);
708 sink_writes(ev_writer_sink(c
->w
), "250 OK\n");
709 return 1; /* completed */
712 static int c_random_disable(struct conn
*c
,
713 char attribute((unused
)) **vec
,
714 int attribute((unused
)) nvec
) {
715 disable_random(c
->who
);
716 sink_writes(ev_writer_sink(c
->w
), "250 OK\n");
717 return 1; /* completed */
720 static int c_random_enabled(struct conn
*c
,
721 char attribute((unused
)) **vec
,
722 int attribute((unused
)) nvec
) {
723 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", noyes
[random_is_enabled()]);
724 return 1; /* completed */
727 static void got_stats(char *stats
, void *u
) {
728 struct conn
*const c
= u
;
730 sink_printf(ev_writer_sink(c
->w
), "253 stats\n%s\n.\n", stats
);
731 /* Now we can start processing commands again */
732 ev_reader_enable(c
->r
);
735 static int c_stats(struct conn
*c
,
736 char attribute((unused
)) **vec
,
737 int attribute((unused
)) nvec
) {
738 trackdb_stats_subprocess(c
->ev
, got_stats
, c
);
739 return 0; /* not yet complete */
742 static int c_volume(struct conn
*c
,
754 l
= r
= atoi(vec
[0]);
765 rights
= set ? RIGHT_VOLUME
: RIGHT_READ
;
766 if(!(c
->rights
& rights
)) {
767 error(0, "%s attempted to set volume but lacks required rights", c
->who
);
768 sink_writes(ev_writer_sink(c
->w
), "510 Prohibited\n");
771 if(mixer_control(&l
, &r
, set
))
772 sink_writes(ev_writer_sink(c
->w
), "550 error accessing mixer\n");
774 sink_printf(ev_writer_sink(c
->w
), "252 %d %d\n", l
, r
);
775 if(l
!= volume_left
|| r
!= volume_right
) {
778 snprintf(lb
, sizeof lb
, "%d", l
);
779 snprintf(rb
, sizeof rb
, "%d", r
);
780 eventlog("volume", lb
, rb
, (char *)0);
786 /** @brief Called when data arrives on a log connection
788 * We just discard all such data. The client may occasionally send data as a
791 static int logging_reader_callback(ev_source
attribute((unused
)) *ev
,
793 void attribute((unused
)) *ptr
,
795 int attribute((unused
)) eof
,
796 void attribute((unused
)) *u
) {
799 ev_reader_consume(reader
, bytes
);
801 /* Oops, that's all for now */
802 D(("logging reader eof"));
805 ev_writer_close(c
->w
);
809 remove_connection(c
);
814 static void logclient(const char *msg
, void *user
) {
815 struct conn
*c
= user
;
818 /* This connection has gone up in smoke for some reason */
819 eventlog_remove(c
->lo
);
822 sink_printf(ev_writer_sink(c
->w
), "%"PRIxMAX
" %s\n",
823 (uintmax_t)time(0), msg
);
826 static int c_log(struct conn
*c
,
827 char attribute((unused
)) **vec
,
828 int attribute((unused
)) nvec
) {
831 sink_writes(ev_writer_sink(c
->w
), "254 OK\n");
832 /* pump out initial state */
834 sink_printf(ev_writer_sink(c
->w
), "%"PRIxMAX
" state %s\n",
836 playing_is_enabled() ?
"enable_play" : "disable_play");
837 sink_printf(ev_writer_sink(c
->w
), "%"PRIxMAX
" state %s\n",
839 random_is_enabled() ?
"enable_random" : "disable_random");
840 sink_printf(ev_writer_sink(c
->w
), "%"PRIxMAX
" state %s\n",
842 paused ?
"pause" : "resume");
844 sink_printf(ev_writer_sink(c
->w
), "%"PRIxMAX
" state playing\n",
847 sink_printf(ev_writer_sink(c
->w
), "%"PRIxMAX
" volume %d %d\n",
848 (uintmax_t)now
, volume_left
, volume_right
);
849 c
->lo
= xmalloc(sizeof *c
->lo
);
850 c
->lo
->fn
= logclient
;
853 c
->reader
= logging_reader_callback
;
857 /** @brief Test whether a move is allowed
858 * @param c Connection
859 * @param qs List of IDs on queue
860 * @param nqs Number of IDs
861 * @return 0 if move is prohibited, non-0 if it is allowed
863 static int has_move_rights(struct conn
*c
, struct queue_entry
**qs
, int nqs
) {
864 for(; nqs
> 0; ++qs
, --nqs
) {
865 struct queue_entry
*const q
= *qs
;
867 if(!right_movable(c
->rights
, c
->who
, q
))
873 static int c_move(struct conn
*c
,
875 int attribute((unused
)) nvec
) {
876 struct queue_entry
*q
;
879 if(!(q
= queue_find(vec
[0]))) {
880 sink_writes(ev_writer_sink(c
->w
), "550 no such track on the queue\n");
883 if(!has_move_rights(c
, &q
, 1)) {
884 error(0, "%s attempted move but lacks required rights", c
->who
);
885 sink_writes(ev_writer_sink(c
->w
),
886 "510 Not authorized to move that track\n");
889 n
= queue_move(q
, atoi(vec
[1]), c
->who
);
890 sink_printf(ev_writer_sink(c
->w
), "252 %d\n", n
);
891 /* If we've moved to the head of the queue then prepare the track. */
897 static int c_moveafter(struct conn
*c
,
899 int attribute((unused
)) nvec
) {
900 struct queue_entry
*q
, **qs
;
904 if(!(q
= queue_find(vec
[0]))) {
905 sink_writes(ev_writer_sink(c
->w
), "550 no such track on the queue\n");
912 qs
= xcalloc(nvec
, sizeof *qs
);
913 for(n
= 0; n
< nvec
; ++n
)
914 if(!(qs
[n
] = queue_find(vec
[n
]))) {
915 sink_writes(ev_writer_sink(c
->w
), "550 no such track on the queue\n");
918 if(!has_move_rights(c
, qs
, nvec
)) {
919 error(0, "%s attempted moveafter but lacks required rights", c
->who
);
920 sink_writes(ev_writer_sink(c
->w
),
921 "510 Not authorized to move those tracks\n");
924 queue_moveafter(q
, nvec
, qs
, c
->who
);
925 sink_printf(ev_writer_sink(c
->w
), "250 Moved tracks\n");
926 /* If we've moved to the head of the queue then prepare the track. */
932 static int c_part(struct conn
*c
,
934 int attribute((unused
)) nvec
) {
935 sink_printf(ev_writer_sink(c
->w
), "252 %s\n",
936 quoteutf8(trackdb_getpart(vec
[0], vec
[1], vec
[2])));
940 static int c_resolve(struct conn
*c
,
942 int attribute((unused
)) nvec
) {
945 if(!(track
= trackdb_resolve(vec
[0]))) {
946 sink_writes(ev_writer_sink(c
->w
), "550 cannot resolve track\n");
949 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", quoteutf8(track
));
953 static int c_tags(struct conn
*c
,
954 char attribute((unused
)) **vec
,
955 int attribute((unused
)) nvec
) {
956 char **tags
= trackdb_alltags();
958 sink_printf(ev_writer_sink(c
->w
), "253 Tag list follows\n");
960 sink_printf(ev_writer_sink(c
->w
), "%s%s\n",
961 **tags
== '.' ?
"." : "", *tags
);
964 sink_writes(ev_writer_sink(c
->w
), ".\n");
965 return 1; /* completed */
968 static int c_set_global(struct conn
*c
,
970 int attribute((unused
)) nvec
) {
971 if(vec
[0][0] == '_') {
972 sink_writes(ev_writer_sink(c
->w
), "550 cannot set internal global preferences\n");
975 trackdb_set_global(vec
[0], vec
[1], c
->who
);
976 sink_printf(ev_writer_sink(c
->w
), "250 OK\n");
980 static int c_get_global(struct conn
*c
,
982 int attribute((unused
)) nvec
) {
983 const char *s
= trackdb_get_global(vec
[0]);
986 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", quoteutf8(s
));
988 sink_writes(ev_writer_sink(c
->w
), "555 not found\n");
992 static int c_nop(struct conn
*c
,
993 char attribute((unused
)) **vec
,
994 int attribute((unused
)) nvec
) {
995 sink_printf(ev_writer_sink(c
->w
), "250 Quack\n");
999 static int c_new(struct conn
*c
,
1009 if(max
<= 0 || max
> config
->new_max
)
1010 max
= config
->new_max
;
1011 tracks
= trackdb_new(0, max
);
1012 sink_printf(ev_writer_sink(c
->w
), "253 New track list follows\n");
1015 sink_printf(ev_writer_sink(c
->w
), "%s%s\n",
1016 **tracks
== '.' ?
"." : "", *tracks
);
1019 sink_writes(ev_writer_sink(c
->w
), ".\n");
1020 return 1; /* completed */
1024 static int c_rtp_address(struct conn
*c
,
1025 char attribute((unused
)) **vec
,
1026 int attribute((unused
)) nvec
) {
1027 if(config
->api
== BACKEND_NETWORK
) {
1028 sink_printf(ev_writer_sink(c
->w
), "252 %s %s\n",
1029 quoteutf8(config
->broadcast
.s
[0]),
1030 quoteutf8(config
->broadcast
.s
[1]));
1032 sink_writes(ev_writer_sink(c
->w
), "550 No RTP\n");
1036 static int c_cookie(struct conn
*c
,
1038 int attribute((unused
)) nvec
) {
1043 /* Can't log in twice on the same connection */
1045 sink_writes(ev_writer_sink(c
->w
), "530 already authenticated\n");
1048 /* Get some kind of peer identifcation */
1049 if(!(host
= connection_host(c
))) {
1050 sink_writes(ev_writer_sink(c
->w
), "530 authentication failure\n");
1053 /* Check the cookie */
1054 user
= verify_cookie(vec
[0], &rights
);
1056 sink_writes(ev_writer_sink(c
->w
), "530 authentication failure\n");
1063 if(strcmp(host
, "local"))
1064 info("S%x %s connected with cookie from %s", c
->tag
, user
, host
);
1066 c
->rights
|= RIGHT__LOCAL
;
1067 /* Response contains username so client knows who they are acting as */
1068 sink_printf(ev_writer_sink(c
->w
), "232 %s\n", quoteutf8(user
));
1072 static int c_make_cookie(struct conn
*c
,
1073 char attribute((unused
)) **vec
,
1074 int attribute((unused
)) nvec
) {
1075 const char *cookie
= make_cookie(c
->who
);
1078 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", quoteutf8(cookie
));
1080 sink_writes(ev_writer_sink(c
->w
), "550 Cannot create cookie\n");
1084 static int c_revoke(struct conn
*c
,
1085 char attribute((unused
)) **vec
,
1086 int attribute((unused
)) nvec
) {
1088 revoke_cookie(c
->cookie
);
1089 sink_writes(ev_writer_sink(c
->w
), "250 OK\n");
1091 sink_writes(ev_writer_sink(c
->w
), "550 Did not log in with cookie\n");
1095 static int c_adduser(struct conn
*c
,
1102 if(parse_rights(vec
[2], 0, 1)) {
1103 sink_writes(ev_writer_sink(c
->w
), "550 Invalid rights list\n");
1107 rights
= config
->default_rights
;
1108 if(trackdb_adduser(vec
[0], vec
[1], rights
,
1109 0/*email*/, 0/*confirmation*/))
1110 sink_writes(ev_writer_sink(c
->w
), "550 Cannot create user\n");
1112 sink_writes(ev_writer_sink(c
->w
), "250 User created\n");
1116 static int c_deluser(struct conn
*c
,
1118 int attribute((unused
)) nvec
) {
1121 if(trackdb_deluser(vec
[0])) {
1122 sink_writes(ev_writer_sink(c
->w
), "550 Cannot delete user\n");
1125 /* Zap connections belonging to deleted user */
1126 for(d
= connections
; d
; d
= d
->next
)
1127 if(!strcmp(d
->who
, vec
[0]))
1129 sink_writes(ev_writer_sink(c
->w
), "250 User deleted\n");
1133 static int c_edituser(struct conn
*c
,
1135 int attribute((unused
)) nvec
) {
1138 /* RIGHT_ADMIN can do anything; otherwise you can only set your own email
1139 * address and password. */
1140 if((c
->rights
& RIGHT_ADMIN
)
1141 || (!strcmp(c
->who
, vec
[0])
1142 && (!strcmp(vec
[1], "email")
1143 || !strcmp(vec
[1], "password")))) {
1144 if(trackdb_edituserinfo(vec
[0], vec
[1], vec
[2])) {
1145 sink_writes(ev_writer_sink(c
->w
), "550 Failed to change setting\n");
1148 if(!strcmp(vec
[1], "password")) {
1149 /* Zap all connections for this user after a password change */
1150 for(d
= connections
; d
; d
= d
->next
)
1151 if(!strcmp(d
->who
, vec
[0]))
1153 } else if(!strcmp(vec
[1], "rights")) {
1154 /* Update rights for this user */
1157 if(parse_rights(vec
[2], &r
, 1))
1158 for(d
= connections
; d
; d
= d
->next
)
1159 if(!strcmp(d
->who
, vec
[0]))
1162 sink_writes(ev_writer_sink(c
->w
), "250 OK\n");
1164 error(0, "%s attempted edituser but lacks required rights", c
->who
);
1165 sink_writes(ev_writer_sink(c
->w
), "510 Restricted to administrators\n");
1170 static int c_userinfo(struct conn
*c
,
1171 char attribute((unused
)) **vec
,
1172 int attribute((unused
)) nvec
) {
1176 /* RIGHT_ADMIN allows anything; otherwise you can only get your own email
1177 * address and righst list. */
1178 if((c
->rights
& RIGHT_ADMIN
)
1179 || (!strcmp(c
->who
, vec
[0])
1180 && (!strcmp(vec
[1], "email")
1181 || !strcmp(vec
[1], "rights")))) {
1182 if((k
= trackdb_getuserinfo(vec
[0])))
1183 if((value
= kvp_get(k
, vec
[1])))
1184 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", quoteutf8(value
));
1186 sink_writes(ev_writer_sink(c
->w
), "555 Not set\n");
1188 sink_writes(ev_writer_sink(c
->w
), "550 No such user\n");
1190 error(0, "%s attempted userinfo but lacks required rights", c
->who
);
1191 sink_writes(ev_writer_sink(c
->w
), "510 Restricted to administrators\n");
1196 static int c_users(struct conn
*c
,
1197 char attribute((unused
)) **vec
,
1198 int attribute((unused
)) nvec
) {
1199 /* TODO de-dupe with c_tags */
1200 char **users
= trackdb_listusers();
1202 sink_writes(ev_writer_sink(c
->w
), "253 User list follows\n");
1204 sink_printf(ev_writer_sink(c
->w
), "%s%s\n",
1205 **users
== '.' ?
"." : "", *users
);
1208 sink_writes(ev_writer_sink(c
->w
), ".\n");
1209 return 1; /* completed */
1212 /** @brief Base64 mapping table for confirmation strings
1214 * This is used with generic_to_base64() and generic_base64(). We cannot use
1215 * the MIME table as that contains '+' and '=' which get quoted when
1216 * URL-encoding. (The CGI still does the URL encoding but it is desirable to
1217 * avoid it being necessary.)
1219 static const char confirm_base64_table
[] =
1220 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789/.*";
1222 static int c_register(struct conn
*c
,
1224 int attribute((unused
)) nvec
) {
1229 /* The confirmation string is base64(username;nonce) */
1230 bufsize
= strlen(vec
[0]) + CONFIRM_SIZE
+ 2;
1231 buf
= xmalloc_noptr(bufsize
);
1232 offset
= byte_snprintf(buf
, bufsize
, "%s;", vec
[0]);
1233 gcry_randomize(buf
+ offset
, CONFIRM_SIZE
, GCRY_STRONG_RANDOM
);
1234 cs
= generic_to_base64((uint8_t *)buf
, offset
+ CONFIRM_SIZE
,
1235 confirm_base64_table
);
1236 if(trackdb_adduser(vec
[0], vec
[1], config
->default_rights
, vec
[2], cs
))
1237 sink_writes(ev_writer_sink(c
->w
), "550 Cannot create user\n");
1239 sink_printf(ev_writer_sink(c
->w
), "252 %s\n", quoteutf8(cs
));
1243 static int c_confirm(struct conn
*c
,
1245 int attribute((unused
)) nvec
) {
1251 /* Get some kind of peer identifcation */
1252 if(!(host
= connection_host(c
))) {
1253 sink_writes(ev_writer_sink(c
->w
), "530 Authentication failure\n");
1256 if(!(user
= generic_base64(vec
[0], &nuser
, confirm_base64_table
))
1257 || !(sep
= memchr(user
, ';', nuser
))) {
1258 sink_writes(ev_writer_sink(c
->w
), "550 Malformed confirmation string\n");
1262 if(trackdb_confirm(user
, vec
[0], &rights
))
1263 sink_writes(ev_writer_sink(c
->w
), "550 Incorrect confirmation string\n");
1268 if(strcmp(host
, "local"))
1269 info("S%x %s confirmed from %s", c
->tag
, user
, host
);
1271 c
->rights
|= RIGHT__LOCAL
;
1272 /* Response contains username so client knows who they are acting as */
1273 sink_printf(ev_writer_sink(c
->w
), "232 %s\n", quoteutf8(user
));
1278 static int sent_reminder(ev_source
attribute((unused
)) *ev
,
1279 pid_t
attribute((unused
)) pid
,
1281 const struct rusage
attribute((unused
)) *rusage
,
1283 struct conn
*const c
= u
;
1285 /* Tell the client what went down */
1287 sink_writes(ev_writer_sink(c
->w
), "250 OK\n");
1289 error(0, "reminder subprocess %s", wstat(status
));
1290 sink_writes(ev_writer_sink(c
->w
), "550 Cannot send a reminder email\n");
1292 /* Re-enable this connection */
1293 ev_reader_enable(c
->r
);
1297 static int c_reminder(struct conn
*c
,
1299 int attribute((unused
)) nvec
) {
1301 const char *password
, *email
, *text
, *encoding
, *charset
, *content_type
;
1306 static hash
*last_reminder
;
1308 if(!config
->mail_sender
) {
1309 error(0, "cannot send password reminders because mail_sender not set");
1310 sink_writes(ev_writer_sink(c
->w
), "550 Cannot send a reminder email\n");
1313 if(!(k
= trackdb_getuserinfo(vec
[0]))) {
1314 error(0, "reminder for user '%s' who does not exist", vec
[0]);
1315 sink_writes(ev_writer_sink(c
->w
), "550 Cannot send a reminder email\n");
1318 if(!(email
= kvp_get(k
, "email"))
1319 || !strchr(email
, '@')) {
1320 error(0, "user '%s' has no valid email address", vec
[0]);
1321 sink_writes(ev_writer_sink(c
->w
), "550 Cannot send a reminder email\n");
1324 if(!(password
= kvp_get(k
, "password"))
1326 error(0, "user '%s' has no password", vec
[0]);
1327 sink_writes(ev_writer_sink(c
->w
), "550 Cannot send a reminder email\n");
1330 /* Rate-limit reminders. This hash is bounded in size by the number of
1331 * users. If this is actually a problem for anyone then we can periodically
1334 last_reminder
= hash_new(sizeof (time_t));
1335 last
= hash_find(last_reminder
, vec
[0]);
1337 if(last
&& now
< *last
+ config
->reminder_interval
) {
1338 error(0, "sent a password reminder to '%s' too recently", vec
[0]);
1339 sink_writes(ev_writer_sink(c
->w
), "550 Cannot send a reminder email\n");
1342 /* Send the reminder */
1343 /* TODO this should be templatized and to some extent merged with
1344 * the code in act_register() */
1345 byte_xasprintf((char **)&text
,
1346 "Someone requested that you be sent a reminder of your DisOrder password.\n"
1347 "Your password is:\n"
1350 if(!(text
= mime_encode_text(text
, &charset
, &encoding
)))
1351 fatal(0, "cannot encode email");
1352 byte_xasprintf((char **)&content_type
, "text/plain;charset=%s",
1353 quote822(charset
, 0));
1354 pid
= sendmail_subprocess("", config
->mail_sender
, email
,
1355 "DisOrder password reminder",
1356 encoding
, content_type
, text
);
1358 sink_writes(ev_writer_sink(c
->w
), "550 Cannot send a reminder email\n");
1361 hash_add(last_reminder
, vec
[0], &now
, HASH_INSERT_OR_REPLACE
);
1362 info("sending a passsword reminder to user '%s'", vec
[0]);
1363 /* We can only continue when the subprocess finishes */
1364 ev_child(c
->ev
, pid
, 0, sent_reminder
, c
);
1368 static const struct command
{
1369 /** @brief Command name */
1372 /** @brief Minimum number of arguments */
1375 /** @brief Maximum number of arguments */
1378 /** @brief Function to process command */
1379 int (*fn
)(struct conn
*, char **, int);
1381 /** @brief Rights required to execute command
1383 * 0 means that the command can be issued without logging in. If multiple
1384 * bits are listed here any of those rights will do.
1388 { "adduser", 2, 3, c_adduser
, RIGHT_ADMIN
|RIGHT__LOCAL
},
1389 { "allfiles", 0, 2, c_allfiles
, RIGHT_READ
},
1390 { "confirm", 1, 1, c_confirm
, 0 },
1391 { "cookie", 1, 1, c_cookie
, 0 },
1392 { "deluser", 1, 1, c_deluser
, RIGHT_ADMIN
|RIGHT__LOCAL
},
1393 { "dirs", 0, 2, c_dirs
, RIGHT_READ
},
1394 { "disable", 0, 1, c_disable
, RIGHT_GLOBAL_PREFS
},
1395 { "edituser", 3, 3, c_edituser
, RIGHT_ADMIN
|RIGHT_USERINFO
},
1396 { "enable", 0, 0, c_enable
, RIGHT_GLOBAL_PREFS
},
1397 { "enabled", 0, 0, c_enabled
, RIGHT_READ
},
1398 { "exists", 1, 1, c_exists
, RIGHT_READ
},
1399 { "files", 0, 2, c_files
, RIGHT_READ
},
1400 { "get", 2, 2, c_get
, RIGHT_READ
},
1401 { "get-global", 1, 1, c_get_global
, RIGHT_READ
},
1402 { "length", 1, 1, c_length
, RIGHT_READ
},
1403 { "log", 0, 0, c_log
, RIGHT_READ
},
1404 { "make-cookie", 0, 0, c_make_cookie
, RIGHT_READ
},
1405 { "move", 2, 2, c_move
, RIGHT_MOVE__MASK
},
1406 { "moveafter", 1, INT_MAX
, c_moveafter
, RIGHT_MOVE__MASK
},
1407 { "new", 0, 1, c_new
, RIGHT_READ
},
1408 { "nop", 0, 0, c_nop
, 0 },
1409 { "part", 3, 3, c_part
, RIGHT_READ
},
1410 { "pause", 0, 0, c_pause
, RIGHT_PAUSE
},
1411 { "play", 1, 1, c_play
, RIGHT_PLAY
},
1412 { "playing", 0, 0, c_playing
, RIGHT_READ
},
1413 { "prefs", 1, 1, c_prefs
, RIGHT_READ
},
1414 { "queue", 0, 0, c_queue
, RIGHT_READ
},
1415 { "random-disable", 0, 0, c_random_disable
, RIGHT_GLOBAL_PREFS
},
1416 { "random-enable", 0, 0, c_random_enable
, RIGHT_GLOBAL_PREFS
},
1417 { "random-enabled", 0, 0, c_random_enabled
, RIGHT_READ
},
1418 { "recent", 0, 0, c_recent
, RIGHT_READ
},
1419 { "reconfigure", 0, 0, c_reconfigure
, RIGHT_ADMIN
},
1420 { "register", 3, 3, c_register
, RIGHT_REGISTER
|RIGHT__LOCAL
},
1421 { "reminder", 1, 1, c_reminder
, RIGHT__LOCAL
},
1422 { "remove", 1, 1, c_remove
, RIGHT_REMOVE__MASK
},
1423 { "rescan", 0, 0, c_rescan
, RIGHT_RESCAN
},
1424 { "resolve", 1, 1, c_resolve
, RIGHT_READ
},
1425 { "resume", 0, 0, c_resume
, RIGHT_PAUSE
},
1426 { "revoke", 0, 0, c_revoke
, RIGHT_READ
},
1427 { "rtp-address", 0, 0, c_rtp_address
, 0 },
1428 { "scratch", 0, 1, c_scratch
, RIGHT_SCRATCH__MASK
},
1429 { "search", 1, 1, c_search
, RIGHT_READ
},
1430 { "set", 3, 3, c_set
, RIGHT_PREFS
, },
1431 { "set-global", 2, 2, c_set_global
, RIGHT_GLOBAL_PREFS
},
1432 { "shutdown", 0, 0, c_shutdown
, RIGHT_ADMIN
},
1433 { "stats", 0, 0, c_stats
, RIGHT_READ
},
1434 { "tags", 0, 0, c_tags
, RIGHT_READ
},
1435 { "unset", 2, 2, c_set
, RIGHT_PREFS
},
1436 { "unset-global", 1, 1, c_set_global
, RIGHT_GLOBAL_PREFS
},
1437 { "user", 2, 2, c_user
, 0 },
1438 { "userinfo", 2, 2, c_userinfo
, RIGHT_READ
},
1439 { "users", 0, 0, c_users
, RIGHT_READ
},
1440 { "version", 0, 0, c_version
, RIGHT_READ
},
1441 { "volume", 0, 2, c_volume
, RIGHT_READ
|RIGHT_VOLUME
}
1444 static void command_error(const char *msg
, void *u
) {
1447 sink_printf(ev_writer_sink(c
->w
), "500 parse error: %s\n", msg
);
1450 /* process a command. Return 1 if complete, 0 if incomplete. */
1451 static int command(struct conn
*c
, char *line
) {
1455 D(("server command %s", line
));
1456 /* We force everything into NFC as early as possible */
1457 if(!(line
= utf8_compose_canon(line
, strlen(line
), 0))) {
1458 sink_writes(ev_writer_sink(c
->w
), "500 cannot normalize command\n");
1461 if(!(vec
= split(line
, &nvec
, SPLIT_QUOTES
, command_error
, c
))) {
1462 sink_writes(ev_writer_sink(c
->w
), "500 cannot parse command\n");
1466 sink_writes(ev_writer_sink(c
->w
), "500 do what?\n");
1469 if((n
= TABLE_FIND(commands
, struct command
, name
, vec
[0])) < 0)
1470 sink_writes(ev_writer_sink(c
->w
), "500 unknown command\n");
1472 if(commands
[n
].rights
1473 && !(c
->rights
& commands
[n
].rights
)) {
1474 error(0, "%s attempted %s but lacks required rights", c
->who ? c
->who
: "NULL",
1476 sink_writes(ev_writer_sink(c
->w
), "510 Prohibited\n");
1481 if(nvec
< commands
[n
].minargs
) {
1482 sink_writes(ev_writer_sink(c
->w
), "500 missing argument(s)\n");
1485 if(nvec
> commands
[n
].maxargs
) {
1486 sink_writes(ev_writer_sink(c
->w
), "500 too many arguments\n");
1489 return commands
[n
].fn(c
, vec
, nvec
);
1491 return 1; /* completed */
1494 /* redirect to the right reader callback for our current state */
1495 static int redirect_reader_callback(ev_source
*ev
,
1503 return c
->reader(ev
, reader
, ptr
, bytes
, eof
, u
);
1506 /* the main command reader */
1507 static int reader_callback(ev_source
attribute((unused
)) *ev
,
1517 D(("server reader_callback"));
1518 while((eol
= memchr(ptr
, '\n', bytes
))) {
1520 ev_reader_consume(reader
, eol
- (char *)ptr
);
1521 complete
= command(c
, ptr
);
1522 bytes
-= (eol
- (char *)ptr
);
1525 /* the command had better have set a new reader callback */
1527 /* there are further bytes to read, or we are at eof; arrange for the
1528 * command's reader callback to handle them */
1529 return ev_reader_incomplete(reader
);
1530 /* nothing's going on right now */
1533 /* command completed, we can go around and handle the next one */
1537 error(0, "S%x unterminated line", c
->tag
);
1538 D(("normal reader close"));
1541 D(("close associated writer"));
1542 ev_writer_close(c
->w
);
1545 remove_connection(c
);
1550 static int listen_callback(ev_source
*ev
,
1552 const struct sockaddr
attribute((unused
)) *remote
,
1553 socklen_t
attribute((unused
)) rlen
,
1555 const struct listener
*l
= u
;
1556 struct conn
*c
= xmalloc(sizeof *c
);
1557 static unsigned tags
;
1559 D(("server listen_callback fd %d (%s)", fd
, l
->name
));
1564 c
->w
= ev_writer_new(ev
, fd
, writer_error
, c
,
1566 c
->r
= ev_reader_new(ev
, fd
, redirect_reader_callback
, reader_error
, c
,
1570 c
->reader
= reader_callback
;
1573 gcry_randomize(c
->nonce
, sizeof c
->nonce
, GCRY_STRONG_RANDOM
);
1574 sink_printf(ev_writer_sink(c
->w
), "231 %d %s %s\n",
1576 config
->authorization_algorithm
,
1577 hex(c
->nonce
, sizeof c
->nonce
));
1581 int server_start(ev_source
*ev
, int pf
,
1582 size_t socklen
, const struct sockaddr
*sa
,
1585 struct listener
*l
= xmalloc(sizeof *l
);
1586 static const int one
= 1;
1588 D(("server_init socket %s", name
));
1589 fd
= xsocket(pf
, SOCK_STREAM
, 0);
1590 xsetsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &one
, sizeof one
);
1591 if(bind(fd
, sa
, socklen
) < 0) {
1592 error(errno
, "error binding to %s", name
);
1600 if(ev_listen(ev
, fd
, listen_callback
, l
, "server listener"))
1605 int server_stop(ev_source
*ev
, int fd
) {
1607 return ev_listen_cancel(ev
, fd
);