chiark / gitweb /
further debugging info() calls; marked TODO to remove when bug fixed
[disorder] / server / server.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder.
768d7355 3 * Copyright (C) 2004, 2005, 2006, 2007 Richard Kettlewell
460b9539 4 *
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.
9 *
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.
14 *
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
18 * USA
19 */
20
21#include <config.h>
22#include "types.h"
23
24#include <pwd.h>
25#include <sys/types.h>
26#include <sys/socket.h>
27#include <sys/time.h>
28#include <unistd.h>
29#include <stdlib.h>
30#include <string.h>
31#include <errno.h>
32#include <fcntl.h>
33#include <gcrypt.h>
34#include <stddef.h>
35#include <time.h>
36#include <limits.h>
37#include <pcre.h>
38#include <netdb.h>
39#include <netinet/in.h>
40
41#include "event.h"
42#include "server.h"
43#include "syscalls.h"
44#include "queue.h"
f08c0859 45#include "server-queue.h"
460b9539 46#include "play.h"
47#include "log.h"
48#include "mem.h"
49#include "state.h"
50#include "charset.h"
51#include "split.h"
52#include "configuration.h"
53#include "hex.h"
54#include "trackdb.h"
55#include "table.h"
56#include "kvp.h"
57#include "mixer.h"
58#include "sink.h"
59#include "authhash.h"
60#include "plugin.h"
61#include "printf.h"
62#include "trackname.h"
63#include "eventlog.h"
64#include "defs.h"
65#include "cache.h"
66
67#ifndef NONCE_SIZE
68# define NONCE_SIZE 16
69#endif
70
71int volume_left, volume_right; /* last known volume */
72
18e6d6e6 73/** @brief Accept all well-formed login attempts
74 *
75 * Used in debugging.
76 */
77int wideopen;
78
460b9539 79struct listener {
80 const char *name;
81 int pf;
82};
83
84struct conn {
85 ev_reader *r;
86 ev_writer *w;
87 int fd;
88 unsigned tag;
89 char *who;
90 ev_source *ev;
91 unsigned char nonce[NONCE_SIZE];
92 ev_reader_callback *reader;
93 struct eventlog_output *lo;
94 const struct listener *l;
95};
96
97static int reader_callback(ev_source *ev,
98 ev_reader *reader,
99 int fd,
100 void *ptr,
101 size_t bytes,
102 int eof,
103 void *u);
104
105static const char *noyes[] = { "no", "yes" };
106
107static int writer_error(ev_source attribute((unused)) *ev,
108 int fd,
109 int errno_value,
110 void *u) {
111 struct conn *c = u;
112
113 D(("server writer_error %d %d", fd, errno_value));
114 if(errno_value == 0) {
115 /* writer is done */
116 c->w = 0;
117 if(c->r == 0) {
295e0ff9
RK
118 //D(("server writer_error closes %d", fd));
119 info("server writer_error closes %d because all done", fd); /* TODO */
460b9539 120 xclose(fd); /* reader is done too, close */
121 } else {
295e0ff9
RK
122 //D(("server writer_error shutdown %d SHUT_WR", fd));
123 info("server writer_error shutdown %d SHUT_WR", fd); /* TODO */
460b9539 124 xshutdown(fd, SHUT_WR); /* reader is not done yet */
125 }
126 } else {
127 if(errno_value != EPIPE)
128 error(errno_value, "S%x write error on socket", c->tag);
129 if(c->r)
130 ev_reader_cancel(c->r);
295e0ff9 131 info("server writer_error closes %d because errno=%d", fd, errno_value); /* TODO */
460b9539 132 xclose(fd);
133 }
134 return 0;
135}
136
137static int reader_error(ev_source attribute((unused)) *ev,
138 int fd,
139 int errno_value,
140 void *u) {
141 struct conn *c = u;
142
143 D(("server reader_error %d %d", fd, errno_value));
144 error(errno, "S%x read error on socket", c->tag);
145 ev_writer_cancel(c->w);
768d7355 146 ev_report(ev);
295e0ff9 147 info("reader_error closing fd %d", fd); /* TODO */
460b9539 148 xclose(fd);
149 return 0;
150}
151
152/* return true if we are talking to a trusted user */
153static int trusted(struct conn *c) {
154 int n;
155
156 for(n = 0; (n < config->trust.n
157 && strcmp(config->trust.s[n], c->who)); ++n)
158 ;
159 return n < config->trust.n;
160}
161
162static int c_disable(struct conn *c, char **vec, int nvec) {
163 if(nvec == 0)
164 disable_playing(c->who);
165 else if(nvec == 1 && !strcmp(vec[0], "now"))
166 disable_playing(c->who);
167 else {
168 sink_writes(ev_writer_sink(c->w), "550 invalid argument\n");
169 return 1; /* completed */
170 }
171 sink_writes(ev_writer_sink(c->w), "250 OK\n");
172 return 1; /* completed */
173}
174
175static int c_enable(struct conn *c,
176 char attribute((unused)) **vec,
177 int attribute((unused)) nvec) {
178 enable_playing(c->who, c->ev);
179 /* Enable implicitly unpauses if there is nothing playing */
180 if(paused && !playing) resume_playing(c->who);
181 sink_writes(ev_writer_sink(c->w), "250 OK\n");
182 return 1; /* completed */
183}
184
185static int c_enabled(struct conn *c,
186 char attribute((unused)) **vec,
187 int attribute((unused)) nvec) {
188 sink_printf(ev_writer_sink(c->w), "252 %s\n", noyes[playing_is_enabled()]);
189 return 1; /* completed */
190}
191
192static int c_play(struct conn *c, char **vec,
193 int attribute((unused)) nvec) {
194 const char *track;
195 struct queue_entry *q;
196
197 if(!trackdb_exists(vec[0])) {
198 sink_writes(ev_writer_sink(c->w), "550 track is not in database\n");
199 return 1;
200 }
201 if(!(track = trackdb_resolve(vec[0]))) {
202 sink_writes(ev_writer_sink(c->w), "550 cannot resolve track\n");
203 return 1;
204 }
205 q = queue_add(track, c->who, WHERE_BEFORE_RANDOM);
206 queue_write();
207 /* If we added the first track, and something is playing, then prepare the
208 * new track. If nothing is playing then we don't bother as it wouldn't gain
209 * anything. */
210 if(q == qhead.next && playing)
211 prepare(c->ev, q);
212 sink_writes(ev_writer_sink(c->w), "250 queued\n");
213 /* If the queue was empty but we are for some reason paused then
214 * unpause. */
215 if(!playing) resume_playing(0);
216 play(c->ev);
217 return 1; /* completed */
218}
219
220static int c_remove(struct conn *c, char **vec,
221 int attribute((unused)) nvec) {
222 struct queue_entry *q;
223
224 if(!(q = queue_find(vec[0]))) {
225 sink_writes(ev_writer_sink(c->w), "550 no such track on the queue\n");
226 return 1;
227 }
228 if(config->restrictions & RESTRICT_REMOVE) {
229 /* can only remove tracks that you submitted */
230 if(!q->submitter || strcmp(q->submitter, c->who)) {
231 sink_writes(ev_writer_sink(c->w), "550 you didn't submit that track!\n");
232 return 1;
233 }
234 }
235 queue_remove(q, c->who);
236 /* De-prepare the track. */
237 abandon(c->ev, q);
238 /* If we removed the random track then add another one. */
239 if(q->state == playing_random)
240 add_random_track();
241 /* Prepare whatever the next head track is. */
242 if(qhead.next != &qhead)
243 prepare(c->ev, qhead.next);
244 queue_write();
245 sink_writes(ev_writer_sink(c->w), "250 removed\n");
246 return 1; /* completed */
247}
248
249static int c_scratch(struct conn *c,
250 char **vec,
251 int nvec) {
252 if(!playing) {
253 sink_writes(ev_writer_sink(c->w), "250 nothing is playing\n");
254 return 1; /* completed */
255 }
256 if(config->restrictions & RESTRICT_SCRATCH) {
257 /* can only scratch tracks you submitted and randomly selected ones */
258 if(playing->submitter && strcmp(playing->submitter, c->who)) {
259 sink_writes(ev_writer_sink(c->w), "550 you didn't submit that track!\n");
260 return 1;
261 }
262 }
263 scratch(c->who, nvec == 1 ? vec[0] : 0);
264 /* If you scratch an unpaused track then it is automatically unpaused */
265 resume_playing(0);
266 sink_writes(ev_writer_sink(c->w), "250 scratched\n");
267 return 1; /* completed */
268}
269
270static int c_pause(struct conn *c,
271 char attribute((unused)) **vec,
272 int attribute((unused)) nvec) {
273 if(!playing) {
274 sink_writes(ev_writer_sink(c->w), "250 nothing is playing\n");
275 return 1; /* completed */
276 }
277 if(paused) {
278 sink_writes(ev_writer_sink(c->w), "250 already paused\n");
279 return 1; /* completed */
280 }
281 if(pause_playing(c->who) < 0)
282 sink_writes(ev_writer_sink(c->w), "550 cannot pause this track\n");
283 else
284 sink_writes(ev_writer_sink(c->w), "250 paused\n");
285 return 1;
286}
287
288static int c_resume(struct conn *c,
289 char attribute((unused)) **vec,
290 int attribute((unused)) nvec) {
291 if(!paused) {
292 sink_writes(ev_writer_sink(c->w), "250 not paused\n");
293 return 1; /* completed */
294 }
295 resume_playing(c->who);
296 sink_writes(ev_writer_sink(c->w), "250 paused\n");
297 return 1;
298}
299
300static int c_shutdown(struct conn *c,
301 char attribute((unused)) **vec,
302 int attribute((unused)) nvec) {
303 info("S%x shut down by %s", c->tag, c->who);
304 sink_writes(ev_writer_sink(c->w), "250 shutting down\n");
305 ev_writer_flush(c->w);
306 quit(c->ev);
307}
308
309static int c_reconfigure(struct conn *c,
310 char attribute((unused)) **vec,
311 int attribute((unused)) nvec) {
312 info("S%x reconfigure by %s", c->tag, c->who);
313 if(reconfigure(c->ev, 1))
314 sink_writes(ev_writer_sink(c->w), "550 error reading new config\n");
315 else
316 sink_writes(ev_writer_sink(c->w), "250 installed new config\n");
317 return 1; /* completed */
318}
319
320static int c_rescan(struct conn *c,
321 char attribute((unused)) **vec,
322 int attribute((unused)) nvec) {
323 info("S%x rescan by %s", c->tag, c->who);
324 trackdb_rescan(c->ev);
325 sink_writes(ev_writer_sink(c->w), "250 initiated rescan\n");
326 return 1; /* completed */
327}
328
329static int c_version(struct conn *c,
330 char attribute((unused)) **vec,
331 int attribute((unused)) nvec) {
332 /* VERSION had better only use the basic character set */
333 sink_printf(ev_writer_sink(c->w), "251 %s\n", disorder_version_string);
334 return 1; /* completed */
335}
336
337static int c_playing(struct conn *c,
338 char attribute((unused)) **vec,
339 int attribute((unused)) nvec) {
340 if(playing) {
341 queue_fix_sofar(playing);
342 playing->expected = 0;
343 sink_printf(ev_writer_sink(c->w), "252 %s\n", queue_marshall(playing));
344 } else
345 sink_printf(ev_writer_sink(c->w), "259 nothing playing\n");
346 return 1; /* completed */
347}
348
349static int c_become(struct conn *c,
350 char **vec,
351 int attribute((unused)) nvec) {
352 c->who = vec[0];
353 sink_writes(ev_writer_sink(c->w), "230 OK\n");
354 return 1;
355}
356
357static int c_user(struct conn *c,
358 char **vec,
359 int attribute((unused)) nvec) {
360 int n;
361 const char *res;
362 union {
363 struct sockaddr sa;
364 struct sockaddr_in in;
365 struct sockaddr_in6 in6;
366 } u;
367 socklen_t l;
368 char host[1024];
369
370 if(c->who) {
371 sink_writes(ev_writer_sink(c->w), "530 already authenticated\n");
372 return 1;
373 }
374 /* get connection data */
375 l = sizeof u;
376 if(getpeername(c->fd, &u.sa, &l) < 0) {
377 error(errno, "S%x error calling getpeername", c->tag);
378 sink_writes(ev_writer_sink(c->w), "530 authentication failure\n");
379 return 1;
380 }
381 if(c->l->pf != PF_UNIX) {
382 if((n = getnameinfo(&u.sa, l,
383 host, sizeof host, 0, 0, NI_NUMERICHOST))) {
384 error(0, "S%x error calling getnameinfo: %s", c->tag, gai_strerror(n));
385 sink_writes(ev_writer_sink(c->w), "530 authentication failure\n");
386 return 1;
387 }
18e6d6e6 388 } else
389 strcpy(host, "local");
460b9539 390 /* find the user */
391 for(n = 0; n < config->allow.n
392 && strcmp(config->allow.s[n].s[0], vec[0]); ++n)
393 ;
394 /* if it's a real user check whether the response is right */
18e6d6e6 395 if(n >= config->allow.n) {
396 info("S%x unknown user '%s' from %s", c->tag, vec[0], host);
397 sink_writes(ev_writer_sink(c->w), "530 authentication failed\n");
398 return 1;
399 }
637fdea3
RK
400 res = authhash(c->nonce, sizeof c->nonce, config->allow.s[n].s[1],
401 config->authorization_algorithm);
18e6d6e6 402 if(wideopen || (res && !strcmp(res, vec[1]))) {
403 c->who = vec[0];
404 /* currently we only bother logging remote connections */
405 if(c->l->pf != PF_UNIX)
406 info("S%x %s connected from %s", c->tag, vec[0], host);
407 sink_writes(ev_writer_sink(c->w), "230 OK\n");
408 return 1;
460b9539 409 }
410 /* oops, response was wrong */
18e6d6e6 411 info("S%x authentication failure for %s from %s", c->tag, vec[0], host);
460b9539 412 sink_writes(ev_writer_sink(c->w), "530 authentication failed\n");
413 return 1;
414}
415
416static int c_recent(struct conn *c,
417 char attribute((unused)) **vec,
418 int attribute((unused)) nvec) {
419 const struct queue_entry *q;
420
421 sink_writes(ev_writer_sink(c->w), "253 Tracks follow\n");
422 for(q = phead.next; q != &phead; q = q->next)
423 sink_printf(ev_writer_sink(c->w), " %s\n", queue_marshall(q));
424 sink_writes(ev_writer_sink(c->w), ".\n");
425 return 1; /* completed */
426}
427
428static int c_queue(struct conn *c,
429 char attribute((unused)) **vec,
430 int attribute((unused)) nvec) {
431 struct queue_entry *q;
432 time_t when = 0;
433 const char *l;
434 long length;
435
436 sink_writes(ev_writer_sink(c->w), "253 Tracks follow\n");
437 if(playing_is_enabled() && !paused) {
438 if(playing) {
439 queue_fix_sofar(playing);
440 if((l = trackdb_get(playing->track, "_length"))
441 && (length = atol(l))) {
442 time(&when);
443 when += length - playing->sofar + config->gap;
444 }
445 } else
446 /* Nothing is playing but playing is enabled, so whatever is
447 * first in the queue can be expected to start immediately. */
448 time(&when);
449 }
450 for(q = qhead.next; q != &qhead; q = q->next) {
451 /* fill in estimated start time */
452 q->expected = when;
453 sink_printf(ev_writer_sink(c->w), " %s\n", queue_marshall(q));
454 /* update for next track */
455 if(when) {
456 if((l = trackdb_get(q->track, "_length"))
457 && (length = atol(l)))
458 when += length + config->gap;
459 else
460 when = 0;
461 }
462 }
463 sink_writes(ev_writer_sink(c->w), ".\n");
464 return 1; /* completed */
465}
466
467static int output_list(struct conn *c, char **vec) {
468 while(*vec)
469 sink_printf(ev_writer_sink(c->w), "%s\n", *vec++);
470 sink_writes(ev_writer_sink(c->w), ".\n");
471 return 1;
472}
473
474static int files_dirs(struct conn *c,
475 char **vec,
476 int nvec,
477 enum trackdb_listable what) {
478 const char *dir, *re, *errstr;
479 int erroffset;
480 pcre *rec;
481 char **fvec, *key;
482
483 switch(nvec) {
484 case 0: dir = 0; re = 0; break;
485 case 1: dir = vec[0]; re = 0; break;
486 case 2: dir = vec[0]; re = vec[1]; break;
487 default: abort();
488 }
489 /* A bit of a bodge to make sure the args don't trample on cache keys */
490 if(dir && strchr(dir, '\n')) {
491 sink_writes(ev_writer_sink(c->w), "550 invalid directory name\n");
492 return 1;
493 }
494 if(re && strchr(re, '\n')) {
495 sink_writes(ev_writer_sink(c->w), "550 invalid regexp\n");
496 return 1;
497 }
498 /* We bother eliminating "" because the web interface is relatively
499 * likely to send it */
500 if(re && *re) {
501 byte_xasprintf(&key, "%d\n%s\n%s", (int)what, dir ? dir : "", re);
502 fvec = (char **)cache_get(&cache_files_type, key);
503 if(fvec) {
504 /* Got a cache hit, don't store the answer in the cache */
505 key = 0;
506 ++cache_files_hits;
507 rec = 0; /* quieten compiler */
508 } else {
509 /* Cache miss, we'll do the lookup and key != 0 so we'll store the answer
510 * in the cache. */
511 if(!(rec = pcre_compile(re, PCRE_CASELESS|PCRE_UTF8,
512 &errstr, &erroffset, 0))) {
513 sink_printf(ev_writer_sink(c->w), "550 Error compiling regexp: %s\n",
514 errstr);
515 return 1;
516 }
517 /* It only counts as a miss if the regexp was valid. */
518 ++cache_files_misses;
519 }
520 } else {
521 /* No regexp, don't bother caching the result */
522 rec = 0;
523 key = 0;
524 fvec = 0;
525 }
526 if(!fvec) {
527 /* No cache hit (either because a miss, or because we did not look) so do
528 * the lookup */
529 if(dir && *dir)
530 fvec = trackdb_list(dir, 0, what, rec);
531 else
532 fvec = trackdb_list(0, 0, what, rec);
533 }
534 if(key)
535 /* Put the answer in the cache */
536 cache_put(&cache_files_type, key, fvec);
537 sink_writes(ev_writer_sink(c->w), "253 Listing follow\n");
538 return output_list(c, fvec);
539}
540
541static int c_files(struct conn *c,
542 char **vec,
543 int nvec) {
544 return files_dirs(c, vec, nvec, trackdb_files);
545}
546
547static int c_dirs(struct conn *c,
548 char **vec,
549 int nvec) {
550 return files_dirs(c, vec, nvec, trackdb_directories);
551}
552
553static int c_allfiles(struct conn *c,
554 char **vec,
555 int nvec) {
556 return files_dirs(c, vec, nvec, trackdb_directories|trackdb_files);
557}
558
559static int c_get(struct conn *c,
560 char **vec,
561 int attribute((unused)) nvec) {
562 const char *v;
563
564 if(vec[1][0] != '_' && (v = trackdb_get(vec[0], vec[1])))
565 sink_printf(ev_writer_sink(c->w), "252 %s\n", v);
566 else
567 sink_writes(ev_writer_sink(c->w), "550 not found\n");
568 return 1;
569}
570
571static int c_length(struct conn *c,
572 char **vec,
573 int attribute((unused)) nvec) {
574 const char *track, *v;
575
576 if(!(track = trackdb_resolve(vec[0]))) {
577 sink_writes(ev_writer_sink(c->w), "550 cannot resolve track\n");
578 return 1;
579 }
580 if((v = trackdb_get(track, "_length")))
581 sink_printf(ev_writer_sink(c->w), "252 %s\n", v);
582 else
583 sink_writes(ev_writer_sink(c->w), "550 not found\n");
584 return 1;
585}
586
587static int c_set(struct conn *c,
588 char **vec,
589 int attribute((unused)) nvec) {
590 if(vec[1][0] != '_' && !trackdb_set(vec[0], vec[1], vec[2]))
591 sink_writes(ev_writer_sink(c->w), "250 OK\n");
592 else
593 sink_writes(ev_writer_sink(c->w), "550 not found\n");
594 return 1;
595}
596
597static int c_prefs(struct conn *c,
598 char **vec,
599 int attribute((unused)) nvec) {
600 struct kvp *k;
601
602 k = trackdb_get_all(vec[0]);
603 sink_writes(ev_writer_sink(c->w), "253 prefs follow\n");
604 for(; k; k = k->next)
605 if(k->name[0] != '_') /* omit internal values */
606 sink_printf(ev_writer_sink(c->w),
607 " %s %s\n", quoteutf8(k->name), quoteutf8(k->value));
608 sink_writes(ev_writer_sink(c->w), ".\n");
609 return 1;
610}
611
612static int c_exists(struct conn *c,
613 char **vec,
614 int attribute((unused)) nvec) {
615 sink_printf(ev_writer_sink(c->w), "252 %s\n", noyes[trackdb_exists(vec[0])]);
616 return 1;
617}
618
619static void search_parse_error(const char *msg, void *u) {
620 *(const char **)u = msg;
621}
622
623static int c_search(struct conn *c,
624 char **vec,
625 int attribute((unused)) nvec) {
626 char **terms, **results;
627 int nterms, nresults, n;
628 const char *e = "unknown error";
629
630 /* This is a bit of a bodge. Initially it's there to make the eclient
631 * interface a bit more convenient to add searching to, but it has the more
632 * compelling advantage that if everything uses it, then interpretation of
633 * user-supplied search strings will be the same everywhere. */
634 if(!(terms = split(vec[0], &nterms, SPLIT_QUOTES, search_parse_error, &e))) {
635 sink_printf(ev_writer_sink(c->w), "550 %s\n", e);
636 } else {
637 results = trackdb_search(terms, nterms, &nresults);
638 sink_printf(ev_writer_sink(c->w), "253 %d matches\n", nresults);
639 for(n = 0; n < nresults; ++n)
640 sink_printf(ev_writer_sink(c->w), "%s\n", results[n]);
641 sink_writes(ev_writer_sink(c->w), ".\n");
642 }
643 return 1;
644}
645
646static int c_random_enable(struct conn *c,
647 char attribute((unused)) **vec,
648 int attribute((unused)) nvec) {
649 enable_random(c->who, c->ev);
650 /* Enable implicitly unpauses if there is nothing playing */
651 if(paused && !playing) resume_playing(c->who);
652 sink_writes(ev_writer_sink(c->w), "250 OK\n");
653 return 1; /* completed */
654}
655
656static int c_random_disable(struct conn *c,
657 char attribute((unused)) **vec,
658 int attribute((unused)) nvec) {
659 disable_random(c->who);
660 sink_writes(ev_writer_sink(c->w), "250 OK\n");
661 return 1; /* completed */
662}
663
664static int c_random_enabled(struct conn *c,
665 char attribute((unused)) **vec,
666 int attribute((unused)) nvec) {
667 sink_printf(ev_writer_sink(c->w), "252 %s\n", noyes[random_is_enabled()]);
668 return 1; /* completed */
669}
670
d6dde5a3
RK
671static void got_stats(char *stats, void *u) {
672 struct conn *const c = u;
673
674 sink_printf(ev_writer_sink(c->w), "253 stats\n%s\n.\n", stats);
675 /* Now we can start processing commands again */
676 ev_reader_enable(c->r);
677}
678
460b9539 679static int c_stats(struct conn *c,
680 char attribute((unused)) **vec,
681 int attribute((unused)) nvec) {
d6dde5a3
RK
682 trackdb_stats_subprocess(c->ev, got_stats, c);
683 return 0; /* not yet complete */
460b9539 684}
685
686static int c_volume(struct conn *c,
687 char **vec,
688 int nvec) {
689 int l, r, set;
690 char lb[32], rb[32];
691
692 switch(nvec) {
693 case 0:
694 set = 0;
695 break;
696 case 1:
697 l = r = atoi(vec[0]);
698 set = 1;
699 break;
700 case 2:
701 l = atoi(vec[0]);
702 r = atoi(vec[1]);
703 set = 1;
704 break;
705 default:
706 abort();
707 }
708 if(mixer_control(&l, &r, set))
709 sink_writes(ev_writer_sink(c->w), "550 error accessing mixer\n");
710 else {
711 sink_printf(ev_writer_sink(c->w), "252 %d %d\n", l, r);
712 if(l != volume_left || r != volume_right) {
713 volume_left = l;
714 volume_right = r;
715 snprintf(lb, sizeof lb, "%d", l);
716 snprintf(rb, sizeof rb, "%d", r);
717 eventlog("volume", lb, rb, (char *)0);
718 }
719 }
720 return 1;
721}
722
723/* we are logging, and some data is available to read */
724static int logging_reader_callback(ev_source *ev,
725 ev_reader *reader,
726 int fd,
727 void *ptr,
728 size_t bytes,
729 int eof,
730 void *u) {
731 struct conn *c = u;
732
733 /* don't log to this conn any more */
734 eventlog_remove(c->lo);
735 /* terminate the log output */
736 sink_writes(ev_writer_sink(c->w), ".\n");
737 /* restore the reader callback */
738 c->reader = reader_callback;
739 /* ...and exit via it */
740 return c->reader(ev, reader, fd, ptr, bytes, eof, u);
741}
742
743static void logclient(const char *msg, void *user) {
744 struct conn *c = user;
745
746 sink_printf(ev_writer_sink(c->w), "%"PRIxMAX" %s\n",
747 (uintmax_t)time(0), msg);
748}
749
750static int c_log(struct conn *c,
751 char attribute((unused)) **vec,
752 int attribute((unused)) nvec) {
753 time_t now;
754
755 sink_writes(ev_writer_sink(c->w), "254 OK\n");
756 /* pump out initial state */
757 time(&now);
758 sink_printf(ev_writer_sink(c->w), "%"PRIxMAX" state %s\n",
759 (uintmax_t)now,
760 playing_is_enabled() ? "enable_play" : "disable_play");
761 sink_printf(ev_writer_sink(c->w), "%"PRIxMAX" state %s\n",
762 (uintmax_t)now,
763 random_is_enabled() ? "enable_random" : "disable_random");
764 sink_printf(ev_writer_sink(c->w), "%"PRIxMAX" state %s\n",
765 (uintmax_t)now,
766 paused ? "pause" : "resume");
5abe307a
RK
767 if(playing)
768 sink_printf(ev_writer_sink(c->w), "%"PRIxMAX" state playing\n",
769 (uintmax_t)now);
df44d69b
RK
770 /* Initial volume */
771 sink_printf(ev_writer_sink(c->w), "%"PRIxMAX" volume %d %d\n",
772 (uintmax_t)now, volume_left, volume_right);
460b9539 773 c->lo = xmalloc(sizeof *c->lo);
774 c->lo->fn = logclient;
775 c->lo->user = c;
776 eventlog_add(c->lo);
777 c->reader = logging_reader_callback;
778 return 0;
779}
780
781static void post_move_cleanup(void) {
782 struct queue_entry *q;
783
138baa2d
RK
784 /* If we have caused any random tracks to not be at the end then we make them
785 * no longer be random. */
460b9539 786 for(q = qhead.next; q != &qhead; q = q->next)
787 if(q->state == playing_random && q->next != &qhead)
788 q->state = playing_unplayed;
789 /* That might mean we need to add a new random track. */
790 add_random_track();
791 queue_write();
792}
793
794static int c_move(struct conn *c,
795 char **vec,
796 int attribute((unused)) nvec) {
797 struct queue_entry *q;
798 int n;
799
800 if(config->restrictions & RESTRICT_MOVE) {
801 if(!trusted(c)) {
802 sink_writes(ev_writer_sink(c->w),
803 "550 only trusted users can move tracks\n");
804 return 1;
805 }
806 }
807 if(!(q = queue_find(vec[0]))) {
808 sink_writes(ev_writer_sink(c->w), "550 no such track on the queue\n");
809 return 1;
810 }
811 n = queue_move(q, atoi(vec[1]), c->who);
812 post_move_cleanup();
813 sink_printf(ev_writer_sink(c->w), "252 %d\n", n);
814 /* If we've moved to the head of the queue then prepare the track. */
815 if(q == qhead.next)
816 prepare(c->ev, q);
817 return 1;
818}
819
820static int c_moveafter(struct conn *c,
821 char **vec,
822 int attribute((unused)) nvec) {
823 struct queue_entry *q, **qs;
824 int n;
825
826 if(config->restrictions & RESTRICT_MOVE) {
827 if(!trusted(c)) {
828 sink_writes(ev_writer_sink(c->w),
829 "550 only trusted users can move tracks\n");
830 return 1;
831 }
832 }
833 if(vec[0][0]) {
834 if(!(q = queue_find(vec[0]))) {
835 sink_writes(ev_writer_sink(c->w), "550 no such track on the queue\n");
836 return 1;
837 }
838 } else
839 q = 0;
840 ++vec;
841 --nvec;
842 qs = xcalloc(nvec, sizeof *qs);
843 for(n = 0; n < nvec; ++n)
844 if(!(qs[n] = queue_find(vec[n]))) {
845 sink_writes(ev_writer_sink(c->w), "550 no such track on the queue\n");
846 return 1;
847 }
848 queue_moveafter(q, nvec, qs, c->who);
849 post_move_cleanup();
850 sink_printf(ev_writer_sink(c->w), "250 Moved tracks\n");
851 /* If we've moved to the head of the queue then prepare the track. */
852 if(q == qhead.next)
853 prepare(c->ev, q);
854 return 1;
855}
856
857static int c_part(struct conn *c,
858 char **vec,
859 int attribute((unused)) nvec) {
860 sink_printf(ev_writer_sink(c->w), "252 %s\n",
861 trackdb_getpart(vec[0], vec[1], vec[2]));
862 return 1;
863}
864
865static int c_resolve(struct conn *c,
866 char **vec,
867 int attribute((unused)) nvec) {
868 const char *track;
869
870 if(!(track = trackdb_resolve(vec[0]))) {
871 sink_writes(ev_writer_sink(c->w), "550 cannot resolve track\n");
872 return 1;
873 }
874 sink_printf(ev_writer_sink(c->w), "252 %s\n", track);
875 return 1;
876}
877
878static int c_tags(struct conn *c,
879 char attribute((unused)) **vec,
880 int attribute((unused)) nvec) {
881 char **tags = trackdb_alltags();
882
883 sink_printf(ev_writer_sink(c->w), "253 Tag list follows\n");
884 while(*tags) {
885 sink_printf(ev_writer_sink(c->w), "%s%s\n",
886 **tags == '.' ? "." : "", *tags);
887 ++tags;
888 }
889 sink_writes(ev_writer_sink(c->w), ".\n");
890 return 1; /* completed */
891
892}
893
894static int c_set_global(struct conn *c,
895 char **vec,
896 int attribute((unused)) nvec) {
897 trackdb_set_global(vec[0], vec[1], c->who);
898 sink_printf(ev_writer_sink(c->w), "250 OK\n");
899 return 1;
900}
901
902static int c_get_global(struct conn *c,
903 char **vec,
904 int attribute((unused)) nvec) {
905 const char *s = trackdb_get_global(vec[0]);
906
907 if(s)
908 sink_printf(ev_writer_sink(c->w), "252 %s\n", s);
909 else
910 sink_writes(ev_writer_sink(c->w), "550 not found\n");
911 return 1;
912}
913
7858930d 914static int c_nop(struct conn *c,
915 char attribute((unused)) **vec,
916 int attribute((unused)) nvec) {
917 sink_printf(ev_writer_sink(c->w), "250 Quack\n");
918 return 1;
919}
920
2a10b70b
RK
921static int c_new(struct conn *c,
922 char **vec,
923 int nvec) {
924 char **tracks = trackdb_new(0, nvec > 0 ? atoi(vec[0]) : INT_MAX);
925
926 sink_printf(ev_writer_sink(c->w), "253 New track list follows\n");
927 while(*tracks) {
928 sink_printf(ev_writer_sink(c->w), "%s%s\n",
929 **tracks == '.' ? "." : "", *tracks);
930 ++tracks;
931 }
932 sink_writes(ev_writer_sink(c->w), ".\n");
933 return 1; /* completed */
934
935}
936
ca831831
RK
937static int c_rtp_address(struct conn *c,
938 char attribute((unused)) **vec,
939 int attribute((unused)) nvec) {
940 if(config->speaker_backend == BACKEND_NETWORK) {
941 sink_printf(ev_writer_sink(c->w), "252 %s %s\n",
942 quoteutf8(config->broadcast.s[0]),
943 quoteutf8(config->broadcast.s[1]));
944 } else
945 sink_writes(ev_writer_sink(c->w), "550 No RTP\n");
946 return 1;
947}
948
460b9539 949#define C_AUTH 0001 /* must be authenticated */
950#define C_TRUSTED 0002 /* must be trusted user */
951
952static const struct command {
953 const char *name;
954 int minargs, maxargs;
955 int (*fn)(struct conn *, char **, int);
956 unsigned flags;
957} commands[] = {
958 { "allfiles", 0, 2, c_allfiles, C_AUTH },
959 { "become", 1, 1, c_become, C_AUTH|C_TRUSTED },
960 { "dirs", 0, 2, c_dirs, C_AUTH },
961 { "disable", 0, 1, c_disable, C_AUTH },
962 { "enable", 0, 0, c_enable, C_AUTH },
963 { "enabled", 0, 0, c_enabled, C_AUTH },
964 { "exists", 1, 1, c_exists, C_AUTH },
965 { "files", 0, 2, c_files, C_AUTH },
966 { "get", 2, 2, c_get, C_AUTH },
967 { "get-global", 1, 1, c_get_global, C_AUTH },
968 { "length", 1, 1, c_length, C_AUTH },
969 { "log", 0, 0, c_log, C_AUTH },
970 { "move", 2, 2, c_move, C_AUTH },
971 { "moveafter", 1, INT_MAX, c_moveafter, C_AUTH },
2a10b70b 972 { "new", 0, 1, c_new, C_AUTH },
7858930d 973 { "nop", 0, 0, c_nop, C_AUTH },
460b9539 974 { "part", 3, 3, c_part, C_AUTH },
975 { "pause", 0, 0, c_pause, C_AUTH },
976 { "play", 1, 1, c_play, C_AUTH },
977 { "playing", 0, 0, c_playing, C_AUTH },
978 { "prefs", 1, 1, c_prefs, C_AUTH },
979 { "queue", 0, 0, c_queue, C_AUTH },
980 { "random-disable", 0, 0, c_random_disable, C_AUTH },
981 { "random-enable", 0, 0, c_random_enable, C_AUTH },
982 { "random-enabled", 0, 0, c_random_enabled, C_AUTH },
983 { "recent", 0, 0, c_recent, C_AUTH },
984 { "reconfigure", 0, 0, c_reconfigure, C_AUTH|C_TRUSTED },
985 { "remove", 1, 1, c_remove, C_AUTH },
986 { "rescan", 0, 0, c_rescan, C_AUTH|C_TRUSTED },
987 { "resolve", 1, 1, c_resolve, C_AUTH },
988 { "resume", 0, 0, c_resume, C_AUTH },
ca831831 989 { "rtp-address", 0, 0, c_rtp_address, C_AUTH },
460b9539 990 { "scratch", 0, 1, c_scratch, C_AUTH },
991 { "search", 1, 1, c_search, C_AUTH },
992 { "set", 3, 3, c_set, C_AUTH, },
993 { "set-global", 2, 2, c_set_global, C_AUTH },
994 { "shutdown", 0, 0, c_shutdown, C_AUTH|C_TRUSTED },
995 { "stats", 0, 0, c_stats, C_AUTH },
996 { "tags", 0, 0, c_tags, C_AUTH },
997 { "unset", 2, 2, c_set, C_AUTH },
998 { "unset-global", 1, 1, c_set_global, C_AUTH },
999 { "user", 2, 2, c_user, 0 },
1000 { "version", 0, 0, c_version, C_AUTH },
1001 { "volume", 0, 2, c_volume, C_AUTH }
1002};
1003
1004static void command_error(const char *msg, void *u) {
1005 struct conn *c = u;
1006
1007 sink_printf(ev_writer_sink(c->w), "500 parse error: %s\n", msg);
1008}
1009
1010/* process a command. Return 1 if complete, 0 if incomplete. */
1011static int command(struct conn *c, char *line) {
1012 char **vec;
1013 int nvec, n;
1014
1015 D(("server command %s", line));
1016 if(!(vec = split(line, &nvec, SPLIT_QUOTES, command_error, c))) {
1017 sink_writes(ev_writer_sink(c->w), "500 cannot parse command\n");
1018 return 1;
1019 }
1020 if(nvec == 0) {
1021 sink_writes(ev_writer_sink(c->w), "500 do what?\n");
1022 return 1;
1023 }
1024 if((n = TABLE_FIND(commands, struct command, name, vec[0])) < 0)
1025 sink_writes(ev_writer_sink(c->w), "500 unknown command\n");
1026 else {
1027 if((commands[n].flags & C_AUTH) && !c->who) {
1028 sink_writes(ev_writer_sink(c->w), "530 not authenticated\n");
1029 return 1;
1030 }
1031 if((commands[n].flags & C_TRUSTED) && !trusted(c)) {
1032 sink_writes(ev_writer_sink(c->w), "530 insufficient privilege\n");
1033 return 1;
1034 }
1035 ++vec;
1036 --nvec;
1037 if(nvec < commands[n].minargs) {
1038 sink_writes(ev_writer_sink(c->w), "500 missing argument(s)\n");
1039 return 1;
1040 }
1041 if(nvec > commands[n].maxargs) {
1042 sink_writes(ev_writer_sink(c->w), "500 too many arguments\n");
1043 return 1;
1044 }
1045 return commands[n].fn(c, vec, nvec);
1046 }
1047 return 1; /* completed */
1048}
1049
1050/* redirect to the right reader callback for our current state */
1051static int redirect_reader_callback(ev_source *ev,
1052 ev_reader *reader,
1053 int fd,
1054 void *ptr,
1055 size_t bytes,
1056 int eof,
1057 void *u) {
1058 struct conn *c = u;
1059
1060 return c->reader(ev, reader, fd, ptr, bytes, eof, u);
1061}
1062
1063/* the main command reader */
1064static int reader_callback(ev_source attribute((unused)) *ev,
1065 ev_reader *reader,
1066 int attribute((unused)) fd,
1067 void *ptr,
1068 size_t bytes,
1069 int eof,
1070 void *u) {
1071 struct conn *c = u;
1072 char *eol;
1073 int complete;
1074
1075 D(("server reader_callback"));
1076 while((eol = memchr(ptr, '\n', bytes))) {
1077 *eol++ = 0;
1078 ev_reader_consume(reader, eol - (char *)ptr);
1079 complete = command(c, ptr);
1080 bytes -= (eol - (char *)ptr);
1081 ptr = eol;
1082 if(!complete) {
1083 /* the command had better have set a new reader callback */
1084 if(bytes || eof)
1085 /* there are further bytes to read, or we are at eof; arrange for the
1086 * command's reader callback to handle them */
1087 return ev_reader_incomplete(reader);
1088 /* nothing's going on right now */
1089 return 0;
1090 }
1091 /* command completed, we can go around and handle the next one */
1092 }
1093 if(eof) {
1094 if(bytes)
1095 error(0, "S%x unterminated line", c->tag);
1096 c->r = 0;
1097 return ev_writer_close(c->w);
1098 }
1099 return 0;
1100}
1101
1102static int listen_callback(ev_source *ev,
1103 int fd,
1104 const struct sockaddr attribute((unused)) *remote,
1105 socklen_t attribute((unused)) rlen,
1106 void *u) {
1107 const struct listener *l = u;
1108 struct conn *c = xmalloc(sizeof *c);
1109 static unsigned tags;
1110
1111 D(("server listen_callback fd %d (%s)", fd, l->name));
1112 nonblock(fd);
1113 cloexec(fd);
1114 c->tag = tags++;
1115 c->ev = ev;
e8c92ba7
RK
1116 c->w = ev_writer_new(ev, fd, writer_error, c,
1117 "client writer");
1118 c->r = ev_reader_new(ev, fd, redirect_reader_callback, reader_error, c,
1119 "client reader");
460b9539 1120 c->fd = fd;
1121 c->reader = reader_callback;
1122 c->l = l;
1123 gcry_randomize(c->nonce, sizeof c->nonce, GCRY_STRONG_RANDOM);
637fdea3
RK
1124 if(!strcmp(config->authorization_algorithm, "sha1")
1125 || !strcmp(config->authorization_algorithm, "SHA1")) {
1126 sink_printf(ev_writer_sink(c->w), "231 %s\n",
1127 hex(c->nonce, sizeof c->nonce));
1128 } else {
1129 sink_printf(ev_writer_sink(c->w), "231 %s %s\n",
1130 config->authorization_algorithm,
1131 hex(c->nonce, sizeof c->nonce));
1132 }
460b9539 1133 return 0;
1134}
1135
1136int server_start(ev_source *ev, int pf,
1137 size_t socklen, const struct sockaddr *sa,
1138 const char *name) {
1139 int fd;
1140 struct listener *l = xmalloc(sizeof *l);
1141 static const int one = 1;
1142
1143 D(("server_init socket %s", name));
1144 fd = xsocket(pf, SOCK_STREAM, 0);
1145 xsetsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one);
1146 if(bind(fd, sa, socklen) < 0) {
1147 error(errno, "error binding to %s", name);
1148 return -1;
1149 }
1150 xlisten(fd, 128);
1151 nonblock(fd);
1152 cloexec(fd);
1153 l->name = name;
1154 l->pf = pf;
e8c92ba7
RK
1155 if(ev_listen(ev, fd, listen_callback, l, "server listener"))
1156 exit(EXIT_FAILURE);
460b9539 1157 return fd;
1158}
1159
1160int server_stop(ev_source *ev, int fd) {
1161 xclose(fd);
1162 return ev_listen_cancel(ev, fd);
1163}
1164
1165/*
1166Local Variables:
1167c-basic-offset:2
1168comment-column:40
1169fill-column:79
1170End:
1171*/