chiark / gitweb /
empeg integration
[disorder] / server / server.c
CommitLineData
460b9539 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
18e6d6e6 72/** @brief Accept all well-formed login attempts
73 *
74 * Used in debugging.
75 */
76int wideopen;
77
460b9539 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 }
18e6d6e6 382 } else
383 strcpy(host, "local");
460b9539 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 */
18e6d6e6 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 }
637fdea3
RK
394 res = authhash(c->nonce, sizeof c->nonce, config->allow.s[n].s[1],
395 config->authorization_algorithm);
18e6d6e6 396 if(wideopen || (res && !strcmp(res, vec[1]))) {
397 c->who = vec[0];
398 /* currently we only bother logging remote connections */
399 if(c->l->pf != PF_UNIX)
400 info("S%x %s connected from %s", c->tag, vec[0], host);
401 sink_writes(ev_writer_sink(c->w), "230 OK\n");
402 return 1;
460b9539 403 }
404 /* oops, response was wrong */
18e6d6e6 405 info("S%x authentication failure for %s from %s", c->tag, vec[0], host);
460b9539 406 sink_writes(ev_writer_sink(c->w), "530 authentication failed\n");
407 return 1;
408}
409
410static int c_recent(struct conn *c,
411 char attribute((unused)) **vec,
412 int attribute((unused)) nvec) {
413 const struct queue_entry *q;
414
415 sink_writes(ev_writer_sink(c->w), "253 Tracks follow\n");
416 for(q = phead.next; q != &phead; q = q->next)
417 sink_printf(ev_writer_sink(c->w), " %s\n", queue_marshall(q));
418 sink_writes(ev_writer_sink(c->w), ".\n");
419 return 1; /* completed */
420}
421
422static int c_queue(struct conn *c,
423 char attribute((unused)) **vec,
424 int attribute((unused)) nvec) {
425 struct queue_entry *q;
426 time_t when = 0;
427 const char *l;
428 long length;
429
430 sink_writes(ev_writer_sink(c->w), "253 Tracks follow\n");
431 if(playing_is_enabled() && !paused) {
432 if(playing) {
433 queue_fix_sofar(playing);
434 if((l = trackdb_get(playing->track, "_length"))
435 && (length = atol(l))) {
436 time(&when);
437 when += length - playing->sofar + config->gap;
438 }
439 } else
440 /* Nothing is playing but playing is enabled, so whatever is
441 * first in the queue can be expected to start immediately. */
442 time(&when);
443 }
444 for(q = qhead.next; q != &qhead; q = q->next) {
445 /* fill in estimated start time */
446 q->expected = when;
447 sink_printf(ev_writer_sink(c->w), " %s\n", queue_marshall(q));
448 /* update for next track */
449 if(when) {
450 if((l = trackdb_get(q->track, "_length"))
451 && (length = atol(l)))
452 when += length + config->gap;
453 else
454 when = 0;
455 }
456 }
457 sink_writes(ev_writer_sink(c->w), ".\n");
458 return 1; /* completed */
459}
460
461static int output_list(struct conn *c, char **vec) {
462 while(*vec)
463 sink_printf(ev_writer_sink(c->w), "%s\n", *vec++);
464 sink_writes(ev_writer_sink(c->w), ".\n");
465 return 1;
466}
467
468static int files_dirs(struct conn *c,
469 char **vec,
470 int nvec,
471 enum trackdb_listable what) {
472 const char *dir, *re, *errstr;
473 int erroffset;
474 pcre *rec;
475 char **fvec, *key;
476
477 switch(nvec) {
478 case 0: dir = 0; re = 0; break;
479 case 1: dir = vec[0]; re = 0; break;
480 case 2: dir = vec[0]; re = vec[1]; break;
481 default: abort();
482 }
483 /* A bit of a bodge to make sure the args don't trample on cache keys */
484 if(dir && strchr(dir, '\n')) {
485 sink_writes(ev_writer_sink(c->w), "550 invalid directory name\n");
486 return 1;
487 }
488 if(re && strchr(re, '\n')) {
489 sink_writes(ev_writer_sink(c->w), "550 invalid regexp\n");
490 return 1;
491 }
492 /* We bother eliminating "" because the web interface is relatively
493 * likely to send it */
494 if(re && *re) {
495 byte_xasprintf(&key, "%d\n%s\n%s", (int)what, dir ? dir : "", re);
496 fvec = (char **)cache_get(&cache_files_type, key);
497 if(fvec) {
498 /* Got a cache hit, don't store the answer in the cache */
499 key = 0;
500 ++cache_files_hits;
501 rec = 0; /* quieten compiler */
502 } else {
503 /* Cache miss, we'll do the lookup and key != 0 so we'll store the answer
504 * in the cache. */
505 if(!(rec = pcre_compile(re, PCRE_CASELESS|PCRE_UTF8,
506 &errstr, &erroffset, 0))) {
507 sink_printf(ev_writer_sink(c->w), "550 Error compiling regexp: %s\n",
508 errstr);
509 return 1;
510 }
511 /* It only counts as a miss if the regexp was valid. */
512 ++cache_files_misses;
513 }
514 } else {
515 /* No regexp, don't bother caching the result */
516 rec = 0;
517 key = 0;
518 fvec = 0;
519 }
520 if(!fvec) {
521 /* No cache hit (either because a miss, or because we did not look) so do
522 * the lookup */
523 if(dir && *dir)
524 fvec = trackdb_list(dir, 0, what, rec);
525 else
526 fvec = trackdb_list(0, 0, what, rec);
527 }
528 if(key)
529 /* Put the answer in the cache */
530 cache_put(&cache_files_type, key, fvec);
531 sink_writes(ev_writer_sink(c->w), "253 Listing follow\n");
532 return output_list(c, fvec);
533}
534
535static int c_files(struct conn *c,
536 char **vec,
537 int nvec) {
538 return files_dirs(c, vec, nvec, trackdb_files);
539}
540
541static int c_dirs(struct conn *c,
542 char **vec,
543 int nvec) {
544 return files_dirs(c, vec, nvec, trackdb_directories);
545}
546
547static int c_allfiles(struct conn *c,
548 char **vec,
549 int nvec) {
550 return files_dirs(c, vec, nvec, trackdb_directories|trackdb_files);
551}
552
553static int c_get(struct conn *c,
554 char **vec,
555 int attribute((unused)) nvec) {
556 const char *v;
557
558 if(vec[1][0] != '_' && (v = trackdb_get(vec[0], vec[1])))
559 sink_printf(ev_writer_sink(c->w), "252 %s\n", v);
560 else
561 sink_writes(ev_writer_sink(c->w), "550 not found\n");
562 return 1;
563}
564
565static int c_length(struct conn *c,
566 char **vec,
567 int attribute((unused)) nvec) {
568 const char *track, *v;
569
570 if(!(track = trackdb_resolve(vec[0]))) {
571 sink_writes(ev_writer_sink(c->w), "550 cannot resolve track\n");
572 return 1;
573 }
574 if((v = trackdb_get(track, "_length")))
575 sink_printf(ev_writer_sink(c->w), "252 %s\n", v);
576 else
577 sink_writes(ev_writer_sink(c->w), "550 not found\n");
578 return 1;
579}
580
581static int c_set(struct conn *c,
582 char **vec,
583 int attribute((unused)) nvec) {
584 if(vec[1][0] != '_' && !trackdb_set(vec[0], vec[1], vec[2]))
585 sink_writes(ev_writer_sink(c->w), "250 OK\n");
586 else
587 sink_writes(ev_writer_sink(c->w), "550 not found\n");
588 return 1;
589}
590
591static int c_prefs(struct conn *c,
592 char **vec,
593 int attribute((unused)) nvec) {
594 struct kvp *k;
595
596 k = trackdb_get_all(vec[0]);
597 sink_writes(ev_writer_sink(c->w), "253 prefs follow\n");
598 for(; k; k = k->next)
599 if(k->name[0] != '_') /* omit internal values */
600 sink_printf(ev_writer_sink(c->w),
601 " %s %s\n", quoteutf8(k->name), quoteutf8(k->value));
602 sink_writes(ev_writer_sink(c->w), ".\n");
603 return 1;
604}
605
606static int c_exists(struct conn *c,
607 char **vec,
608 int attribute((unused)) nvec) {
609 sink_printf(ev_writer_sink(c->w), "252 %s\n", noyes[trackdb_exists(vec[0])]);
610 return 1;
611}
612
613static void search_parse_error(const char *msg, void *u) {
614 *(const char **)u = msg;
615}
616
617static int c_search(struct conn *c,
618 char **vec,
619 int attribute((unused)) nvec) {
620 char **terms, **results;
621 int nterms, nresults, n;
622 const char *e = "unknown error";
623
624 /* This is a bit of a bodge. Initially it's there to make the eclient
625 * interface a bit more convenient to add searching to, but it has the more
626 * compelling advantage that if everything uses it, then interpretation of
627 * user-supplied search strings will be the same everywhere. */
628 if(!(terms = split(vec[0], &nterms, SPLIT_QUOTES, search_parse_error, &e))) {
629 sink_printf(ev_writer_sink(c->w), "550 %s\n", e);
630 } else {
631 results = trackdb_search(terms, nterms, &nresults);
632 sink_printf(ev_writer_sink(c->w), "253 %d matches\n", nresults);
633 for(n = 0; n < nresults; ++n)
634 sink_printf(ev_writer_sink(c->w), "%s\n", results[n]);
635 sink_writes(ev_writer_sink(c->w), ".\n");
636 }
637 return 1;
638}
639
640static int c_random_enable(struct conn *c,
641 char attribute((unused)) **vec,
642 int attribute((unused)) nvec) {
643 enable_random(c->who, c->ev);
644 /* Enable implicitly unpauses if there is nothing playing */
645 if(paused && !playing) resume_playing(c->who);
646 sink_writes(ev_writer_sink(c->w), "250 OK\n");
647 return 1; /* completed */
648}
649
650static int c_random_disable(struct conn *c,
651 char attribute((unused)) **vec,
652 int attribute((unused)) nvec) {
653 disable_random(c->who);
654 sink_writes(ev_writer_sink(c->w), "250 OK\n");
655 return 1; /* completed */
656}
657
658static int c_random_enabled(struct conn *c,
659 char attribute((unused)) **vec,
660 int attribute((unused)) nvec) {
661 sink_printf(ev_writer_sink(c->w), "252 %s\n", noyes[random_is_enabled()]);
662 return 1; /* completed */
663}
664
665static int c_stats(struct conn *c,
666 char attribute((unused)) **vec,
667 int attribute((unused)) nvec) {
668 char **v;
669 int nv, n;
670
671 v = trackdb_stats(&nv);
672 sink_printf(ev_writer_sink(c->w), "253 stats\n");
673 for(n = 0; n < nv; ++n) {
674 if(v[n][0] == '.')
675 sink_writes(ev_writer_sink(c->w), ".");
676 sink_printf(ev_writer_sink(c->w), "%s\n", v[n]);
677 }
678 sink_writes(ev_writer_sink(c->w), ".\n");
679 return 1;
680}
681
682static int c_volume(struct conn *c,
683 char **vec,
684 int nvec) {
685 int l, r, set;
686 char lb[32], rb[32];
687
688 switch(nvec) {
689 case 0:
690 set = 0;
691 break;
692 case 1:
693 l = r = atoi(vec[0]);
694 set = 1;
695 break;
696 case 2:
697 l = atoi(vec[0]);
698 r = atoi(vec[1]);
699 set = 1;
700 break;
701 default:
702 abort();
703 }
704 if(mixer_control(&l, &r, set))
705 sink_writes(ev_writer_sink(c->w), "550 error accessing mixer\n");
706 else {
707 sink_printf(ev_writer_sink(c->w), "252 %d %d\n", l, r);
708 if(l != volume_left || r != volume_right) {
709 volume_left = l;
710 volume_right = r;
711 snprintf(lb, sizeof lb, "%d", l);
712 snprintf(rb, sizeof rb, "%d", r);
713 eventlog("volume", lb, rb, (char *)0);
714 }
715 }
716 return 1;
717}
718
719/* we are logging, and some data is available to read */
720static int logging_reader_callback(ev_source *ev,
721 ev_reader *reader,
722 int fd,
723 void *ptr,
724 size_t bytes,
725 int eof,
726 void *u) {
727 struct conn *c = u;
728
729 /* don't log to this conn any more */
730 eventlog_remove(c->lo);
731 /* terminate the log output */
732 sink_writes(ev_writer_sink(c->w), ".\n");
733 /* restore the reader callback */
734 c->reader = reader_callback;
735 /* ...and exit via it */
736 return c->reader(ev, reader, fd, ptr, bytes, eof, u);
737}
738
739static void logclient(const char *msg, void *user) {
740 struct conn *c = user;
741
742 sink_printf(ev_writer_sink(c->w), "%"PRIxMAX" %s\n",
743 (uintmax_t)time(0), msg);
744}
745
746static int c_log(struct conn *c,
747 char attribute((unused)) **vec,
748 int attribute((unused)) nvec) {
749 time_t now;
750
751 sink_writes(ev_writer_sink(c->w), "254 OK\n");
752 /* pump out initial state */
753 time(&now);
754 sink_printf(ev_writer_sink(c->w), "%"PRIxMAX" state %s\n",
755 (uintmax_t)now,
756 playing_is_enabled() ? "enable_play" : "disable_play");
757 sink_printf(ev_writer_sink(c->w), "%"PRIxMAX" state %s\n",
758 (uintmax_t)now,
759 random_is_enabled() ? "enable_random" : "disable_random");
760 sink_printf(ev_writer_sink(c->w), "%"PRIxMAX" state %s\n",
761 (uintmax_t)now,
762 paused ? "pause" : "resume");
5abe307a
RK
763 if(playing)
764 sink_printf(ev_writer_sink(c->w), "%"PRIxMAX" state playing\n",
765 (uintmax_t)now);
df44d69b
RK
766 /* Initial volume */
767 sink_printf(ev_writer_sink(c->w), "%"PRIxMAX" volume %d %d\n",
768 (uintmax_t)now, volume_left, volume_right);
460b9539 769 c->lo = xmalloc(sizeof *c->lo);
770 c->lo->fn = logclient;
771 c->lo->user = c;
772 eventlog_add(c->lo);
773 c->reader = logging_reader_callback;
774 return 0;
775}
776
777static void post_move_cleanup(void) {
778 struct queue_entry *q;
779
780 /* If we have caused the random track to not be at the end then we make it no
781 * longer be random. */
782 for(q = qhead.next; q != &qhead; q = q->next)
783 if(q->state == playing_random && q->next != &qhead)
784 q->state = playing_unplayed;
785 /* That might mean we need to add a new random track. */
786 add_random_track();
787 queue_write();
788}
789
790static int c_move(struct conn *c,
791 char **vec,
792 int attribute((unused)) nvec) {
793 struct queue_entry *q;
794 int n;
795
796 if(config->restrictions & RESTRICT_MOVE) {
797 if(!trusted(c)) {
798 sink_writes(ev_writer_sink(c->w),
799 "550 only trusted users can move tracks\n");
800 return 1;
801 }
802 }
803 if(!(q = queue_find(vec[0]))) {
804 sink_writes(ev_writer_sink(c->w), "550 no such track on the queue\n");
805 return 1;
806 }
807 n = queue_move(q, atoi(vec[1]), c->who);
808 post_move_cleanup();
809 sink_printf(ev_writer_sink(c->w), "252 %d\n", n);
810 /* If we've moved to the head of the queue then prepare the track. */
811 if(q == qhead.next)
812 prepare(c->ev, q);
813 return 1;
814}
815
816static int c_moveafter(struct conn *c,
817 char **vec,
818 int attribute((unused)) nvec) {
819 struct queue_entry *q, **qs;
820 int n;
821
822 if(config->restrictions & RESTRICT_MOVE) {
823 if(!trusted(c)) {
824 sink_writes(ev_writer_sink(c->w),
825 "550 only trusted users can move tracks\n");
826 return 1;
827 }
828 }
829 if(vec[0][0]) {
830 if(!(q = queue_find(vec[0]))) {
831 sink_writes(ev_writer_sink(c->w), "550 no such track on the queue\n");
832 return 1;
833 }
834 } else
835 q = 0;
836 ++vec;
837 --nvec;
838 qs = xcalloc(nvec, sizeof *qs);
839 for(n = 0; n < nvec; ++n)
840 if(!(qs[n] = queue_find(vec[n]))) {
841 sink_writes(ev_writer_sink(c->w), "550 no such track on the queue\n");
842 return 1;
843 }
844 queue_moveafter(q, nvec, qs, c->who);
845 post_move_cleanup();
846 sink_printf(ev_writer_sink(c->w), "250 Moved tracks\n");
847 /* If we've moved to the head of the queue then prepare the track. */
848 if(q == qhead.next)
849 prepare(c->ev, q);
850 return 1;
851}
852
853static int c_part(struct conn *c,
854 char **vec,
855 int attribute((unused)) nvec) {
856 sink_printf(ev_writer_sink(c->w), "252 %s\n",
857 trackdb_getpart(vec[0], vec[1], vec[2]));
858 return 1;
859}
860
861static int c_resolve(struct conn *c,
862 char **vec,
863 int attribute((unused)) nvec) {
864 const char *track;
865
866 if(!(track = trackdb_resolve(vec[0]))) {
867 sink_writes(ev_writer_sink(c->w), "550 cannot resolve track\n");
868 return 1;
869 }
870 sink_printf(ev_writer_sink(c->w), "252 %s\n", track);
871 return 1;
872}
873
874static int c_tags(struct conn *c,
875 char attribute((unused)) **vec,
876 int attribute((unused)) nvec) {
877 char **tags = trackdb_alltags();
878
879 sink_printf(ev_writer_sink(c->w), "253 Tag list follows\n");
880 while(*tags) {
881 sink_printf(ev_writer_sink(c->w), "%s%s\n",
882 **tags == '.' ? "." : "", *tags);
883 ++tags;
884 }
885 sink_writes(ev_writer_sink(c->w), ".\n");
886 return 1; /* completed */
887
888}
889
890static int c_set_global(struct conn *c,
891 char **vec,
892 int attribute((unused)) nvec) {
893 trackdb_set_global(vec[0], vec[1], c->who);
894 sink_printf(ev_writer_sink(c->w), "250 OK\n");
895 return 1;
896}
897
898static int c_get_global(struct conn *c,
899 char **vec,
900 int attribute((unused)) nvec) {
901 const char *s = trackdb_get_global(vec[0]);
902
903 if(s)
904 sink_printf(ev_writer_sink(c->w), "252 %s\n", s);
905 else
906 sink_writes(ev_writer_sink(c->w), "550 not found\n");
907 return 1;
908}
909
7858930d 910static int c_nop(struct conn *c,
911 char attribute((unused)) **vec,
912 int attribute((unused)) nvec) {
913 sink_printf(ev_writer_sink(c->w), "250 Quack\n");
914 return 1;
915}
916
2a10b70b
RK
917static int c_new(struct conn *c,
918 char **vec,
919 int nvec) {
920 char **tracks = trackdb_new(0, nvec > 0 ? atoi(vec[0]) : INT_MAX);
921
922 sink_printf(ev_writer_sink(c->w), "253 New track list follows\n");
923 while(*tracks) {
924 sink_printf(ev_writer_sink(c->w), "%s%s\n",
925 **tracks == '.' ? "." : "", *tracks);
926 ++tracks;
927 }
928 sink_writes(ev_writer_sink(c->w), ".\n");
929 return 1; /* completed */
930
931}
932
460b9539 933#define C_AUTH 0001 /* must be authenticated */
934#define C_TRUSTED 0002 /* must be trusted user */
935
936static const struct command {
937 const char *name;
938 int minargs, maxargs;
939 int (*fn)(struct conn *, char **, int);
940 unsigned flags;
941} commands[] = {
942 { "allfiles", 0, 2, c_allfiles, C_AUTH },
943 { "become", 1, 1, c_become, C_AUTH|C_TRUSTED },
944 { "dirs", 0, 2, c_dirs, C_AUTH },
945 { "disable", 0, 1, c_disable, C_AUTH },
946 { "enable", 0, 0, c_enable, C_AUTH },
947 { "enabled", 0, 0, c_enabled, C_AUTH },
948 { "exists", 1, 1, c_exists, C_AUTH },
949 { "files", 0, 2, c_files, C_AUTH },
950 { "get", 2, 2, c_get, C_AUTH },
951 { "get-global", 1, 1, c_get_global, C_AUTH },
952 { "length", 1, 1, c_length, C_AUTH },
953 { "log", 0, 0, c_log, C_AUTH },
954 { "move", 2, 2, c_move, C_AUTH },
955 { "moveafter", 1, INT_MAX, c_moveafter, C_AUTH },
2a10b70b 956 { "new", 0, 1, c_new, C_AUTH },
7858930d 957 { "nop", 0, 0, c_nop, C_AUTH },
460b9539 958 { "part", 3, 3, c_part, C_AUTH },
959 { "pause", 0, 0, c_pause, C_AUTH },
960 { "play", 1, 1, c_play, C_AUTH },
961 { "playing", 0, 0, c_playing, C_AUTH },
962 { "prefs", 1, 1, c_prefs, C_AUTH },
963 { "queue", 0, 0, c_queue, C_AUTH },
964 { "random-disable", 0, 0, c_random_disable, C_AUTH },
965 { "random-enable", 0, 0, c_random_enable, C_AUTH },
966 { "random-enabled", 0, 0, c_random_enabled, C_AUTH },
967 { "recent", 0, 0, c_recent, C_AUTH },
968 { "reconfigure", 0, 0, c_reconfigure, C_AUTH|C_TRUSTED },
969 { "remove", 1, 1, c_remove, C_AUTH },
970 { "rescan", 0, 0, c_rescan, C_AUTH|C_TRUSTED },
971 { "resolve", 1, 1, c_resolve, C_AUTH },
972 { "resume", 0, 0, c_resume, C_AUTH },
973 { "scratch", 0, 1, c_scratch, C_AUTH },
974 { "search", 1, 1, c_search, C_AUTH },
975 { "set", 3, 3, c_set, C_AUTH, },
976 { "set-global", 2, 2, c_set_global, C_AUTH },
977 { "shutdown", 0, 0, c_shutdown, C_AUTH|C_TRUSTED },
978 { "stats", 0, 0, c_stats, C_AUTH },
979 { "tags", 0, 0, c_tags, C_AUTH },
980 { "unset", 2, 2, c_set, C_AUTH },
981 { "unset-global", 1, 1, c_set_global, C_AUTH },
982 { "user", 2, 2, c_user, 0 },
983 { "version", 0, 0, c_version, C_AUTH },
984 { "volume", 0, 2, c_volume, C_AUTH }
985};
986
987static void command_error(const char *msg, void *u) {
988 struct conn *c = u;
989
990 sink_printf(ev_writer_sink(c->w), "500 parse error: %s\n", msg);
991}
992
993/* process a command. Return 1 if complete, 0 if incomplete. */
994static int command(struct conn *c, char *line) {
995 char **vec;
996 int nvec, n;
997
998 D(("server command %s", line));
999 if(!(vec = split(line, &nvec, SPLIT_QUOTES, command_error, c))) {
1000 sink_writes(ev_writer_sink(c->w), "500 cannot parse command\n");
1001 return 1;
1002 }
1003 if(nvec == 0) {
1004 sink_writes(ev_writer_sink(c->w), "500 do what?\n");
1005 return 1;
1006 }
1007 if((n = TABLE_FIND(commands, struct command, name, vec[0])) < 0)
1008 sink_writes(ev_writer_sink(c->w), "500 unknown command\n");
1009 else {
1010 if((commands[n].flags & C_AUTH) && !c->who) {
1011 sink_writes(ev_writer_sink(c->w), "530 not authenticated\n");
1012 return 1;
1013 }
1014 if((commands[n].flags & C_TRUSTED) && !trusted(c)) {
1015 sink_writes(ev_writer_sink(c->w), "530 insufficient privilege\n");
1016 return 1;
1017 }
1018 ++vec;
1019 --nvec;
1020 if(nvec < commands[n].minargs) {
1021 sink_writes(ev_writer_sink(c->w), "500 missing argument(s)\n");
1022 return 1;
1023 }
1024 if(nvec > commands[n].maxargs) {
1025 sink_writes(ev_writer_sink(c->w), "500 too many arguments\n");
1026 return 1;
1027 }
1028 return commands[n].fn(c, vec, nvec);
1029 }
1030 return 1; /* completed */
1031}
1032
1033/* redirect to the right reader callback for our current state */
1034static int redirect_reader_callback(ev_source *ev,
1035 ev_reader *reader,
1036 int fd,
1037 void *ptr,
1038 size_t bytes,
1039 int eof,
1040 void *u) {
1041 struct conn *c = u;
1042
1043 return c->reader(ev, reader, fd, ptr, bytes, eof, u);
1044}
1045
1046/* the main command reader */
1047static int reader_callback(ev_source attribute((unused)) *ev,
1048 ev_reader *reader,
1049 int attribute((unused)) fd,
1050 void *ptr,
1051 size_t bytes,
1052 int eof,
1053 void *u) {
1054 struct conn *c = u;
1055 char *eol;
1056 int complete;
1057
1058 D(("server reader_callback"));
1059 while((eol = memchr(ptr, '\n', bytes))) {
1060 *eol++ = 0;
1061 ev_reader_consume(reader, eol - (char *)ptr);
1062 complete = command(c, ptr);
1063 bytes -= (eol - (char *)ptr);
1064 ptr = eol;
1065 if(!complete) {
1066 /* the command had better have set a new reader callback */
1067 if(bytes || eof)
1068 /* there are further bytes to read, or we are at eof; arrange for the
1069 * command's reader callback to handle them */
1070 return ev_reader_incomplete(reader);
1071 /* nothing's going on right now */
1072 return 0;
1073 }
1074 /* command completed, we can go around and handle the next one */
1075 }
1076 if(eof) {
1077 if(bytes)
1078 error(0, "S%x unterminated line", c->tag);
1079 c->r = 0;
1080 return ev_writer_close(c->w);
1081 }
1082 return 0;
1083}
1084
1085static int listen_callback(ev_source *ev,
1086 int fd,
1087 const struct sockaddr attribute((unused)) *remote,
1088 socklen_t attribute((unused)) rlen,
1089 void *u) {
1090 const struct listener *l = u;
1091 struct conn *c = xmalloc(sizeof *c);
1092 static unsigned tags;
1093
1094 D(("server listen_callback fd %d (%s)", fd, l->name));
1095 nonblock(fd);
1096 cloexec(fd);
1097 c->tag = tags++;
1098 c->ev = ev;
1099 c->w = ev_writer_new(ev, fd, writer_error, c);
1100 c->r = ev_reader_new(ev, fd, redirect_reader_callback, reader_error, c);
1101 c->fd = fd;
1102 c->reader = reader_callback;
1103 c->l = l;
1104 gcry_randomize(c->nonce, sizeof c->nonce, GCRY_STRONG_RANDOM);
637fdea3
RK
1105 if(!strcmp(config->authorization_algorithm, "sha1")
1106 || !strcmp(config->authorization_algorithm, "SHA1")) {
1107 sink_printf(ev_writer_sink(c->w), "231 %s\n",
1108 hex(c->nonce, sizeof c->nonce));
1109 } else {
1110 sink_printf(ev_writer_sink(c->w), "231 %s %s\n",
1111 config->authorization_algorithm,
1112 hex(c->nonce, sizeof c->nonce));
1113 }
460b9539 1114 return 0;
1115}
1116
1117int server_start(ev_source *ev, int pf,
1118 size_t socklen, const struct sockaddr *sa,
1119 const char *name) {
1120 int fd;
1121 struct listener *l = xmalloc(sizeof *l);
1122 static const int one = 1;
1123
1124 D(("server_init socket %s", name));
1125 fd = xsocket(pf, SOCK_STREAM, 0);
1126 xsetsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one);
1127 if(bind(fd, sa, socklen) < 0) {
1128 error(errno, "error binding to %s", name);
1129 return -1;
1130 }
1131 xlisten(fd, 128);
1132 nonblock(fd);
1133 cloexec(fd);
1134 l->name = name;
1135 l->pf = pf;
1136 if(ev_listen(ev, fd, listen_callback, l)) exit(EXIT_FAILURE);
1137 return fd;
1138}
1139
1140int server_stop(ev_source *ev, int fd) {
1141 xclose(fd);
1142 return ev_listen_cancel(ev, fd);
1143}
1144
1145/*
1146Local Variables:
1147c-basic-offset:2
1148comment-column:40
1149fill-column:79
1150End:
1151*/