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