Commit | Line | Data |
---|---|---|
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 | ||
70 | int volume_left, volume_right; /* last known volume */ | |
71 | ||
18e6d6e6 | 72 | /** @brief Accept all well-formed login attempts |
73 | * | |
74 | * Used in debugging. | |
75 | */ | |
76 | int wideopen; | |
77 | ||
460b9539 | 78 | struct listener { |
79 | const char *name; | |
80 | int pf; | |
81 | }; | |
82 | ||
83 | struct 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 | ||
96 | static 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 | ||
104 | static const char *noyes[] = { "no", "yes" }; | |
105 | ||
106 | static 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 | ||
133 | static 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 */ | |
147 | static 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 | ||
156 | static 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 | ||
169 | static 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 | ||
179 | static 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 | ||
186 | static 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 | ||
214 | static 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 | ||
243 | static 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 | ||
264 | static 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 | ||
282 | static 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 | ||
294 | static 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 | ||
303 | static 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 | ||
314 | static 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 | ||
323 | static 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 | ||
331 | static 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 | ||
343 | static 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 | ||
351 | static 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 | } | |
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; | |
460b9539 | 402 | } |
403 | /* oops, response was wrong */ | |
18e6d6e6 | 404 | info("S%x authentication failure for %s from %s", c->tag, vec[0], host); |
460b9539 | 405 | sink_writes(ev_writer_sink(c->w), "530 authentication failed\n"); |
406 | return 1; | |
407 | } | |
408 | ||
409 | static 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 | ||
421 | static 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 | ||
460 | static 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 | ||
467 | static 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 | ||
534 | static int c_files(struct conn *c, | |
535 | char **vec, | |
536 | int nvec) { | |
537 | return files_dirs(c, vec, nvec, trackdb_files); | |
538 | } | |
539 | ||
540 | static int c_dirs(struct conn *c, | |
541 | char **vec, | |
542 | int nvec) { | |
543 | return files_dirs(c, vec, nvec, trackdb_directories); | |
544 | } | |
545 | ||
546 | static 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 | ||
552 | static 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 | ||
564 | static 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 | ||
580 | static 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 | ||
590 | static 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 | ||
605 | static 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 | ||
612 | static void search_parse_error(const char *msg, void *u) { | |
613 | *(const char **)u = msg; | |
614 | } | |
615 | ||
616 | static 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 | ||
639 | static 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 | ||
649 | static 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 | ||
657 | static 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 | ||
664 | static 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 | ||
681 | static 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 */ | |
719 | static 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 | ||
738 | static 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 | ||
745 | static 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"); | |
5abe307a RK |
762 | if(playing) |
763 | sink_printf(ev_writer_sink(c->w), "%"PRIxMAX" state playing\n", | |
764 | (uintmax_t)now); | |
460b9539 | 765 | c->lo = xmalloc(sizeof *c->lo); |
766 | c->lo->fn = logclient; | |
767 | c->lo->user = c; | |
768 | eventlog_add(c->lo); | |
769 | c->reader = logging_reader_callback; | |
770 | return 0; | |
771 | } | |
772 | ||
773 | static void post_move_cleanup(void) { | |
774 | struct queue_entry *q; | |
775 | ||
776 | /* If we have caused the random track to not be at the end then we make it no | |
777 | * longer be random. */ | |
778 | for(q = qhead.next; q != &qhead; q = q->next) | |
779 | if(q->state == playing_random && q->next != &qhead) | |
780 | q->state = playing_unplayed; | |
781 | /* That might mean we need to add a new random track. */ | |
782 | add_random_track(); | |
783 | queue_write(); | |
784 | } | |
785 | ||
786 | static int c_move(struct conn *c, | |
787 | char **vec, | |
788 | int attribute((unused)) nvec) { | |
789 | struct queue_entry *q; | |
790 | int n; | |
791 | ||
792 | if(config->restrictions & RESTRICT_MOVE) { | |
793 | if(!trusted(c)) { | |
794 | sink_writes(ev_writer_sink(c->w), | |
795 | "550 only trusted users can move tracks\n"); | |
796 | return 1; | |
797 | } | |
798 | } | |
799 | if(!(q = queue_find(vec[0]))) { | |
800 | sink_writes(ev_writer_sink(c->w), "550 no such track on the queue\n"); | |
801 | return 1; | |
802 | } | |
803 | n = queue_move(q, atoi(vec[1]), c->who); | |
804 | post_move_cleanup(); | |
805 | sink_printf(ev_writer_sink(c->w), "252 %d\n", n); | |
806 | /* If we've moved to the head of the queue then prepare the track. */ | |
807 | if(q == qhead.next) | |
808 | prepare(c->ev, q); | |
809 | return 1; | |
810 | } | |
811 | ||
812 | static int c_moveafter(struct conn *c, | |
813 | char **vec, | |
814 | int attribute((unused)) nvec) { | |
815 | struct queue_entry *q, **qs; | |
816 | int n; | |
817 | ||
818 | if(config->restrictions & RESTRICT_MOVE) { | |
819 | if(!trusted(c)) { | |
820 | sink_writes(ev_writer_sink(c->w), | |
821 | "550 only trusted users can move tracks\n"); | |
822 | return 1; | |
823 | } | |
824 | } | |
825 | if(vec[0][0]) { | |
826 | if(!(q = queue_find(vec[0]))) { | |
827 | sink_writes(ev_writer_sink(c->w), "550 no such track on the queue\n"); | |
828 | return 1; | |
829 | } | |
830 | } else | |
831 | q = 0; | |
832 | ++vec; | |
833 | --nvec; | |
834 | qs = xcalloc(nvec, sizeof *qs); | |
835 | for(n = 0; n < nvec; ++n) | |
836 | if(!(qs[n] = queue_find(vec[n]))) { | |
837 | sink_writes(ev_writer_sink(c->w), "550 no such track on the queue\n"); | |
838 | return 1; | |
839 | } | |
840 | queue_moveafter(q, nvec, qs, c->who); | |
841 | post_move_cleanup(); | |
842 | sink_printf(ev_writer_sink(c->w), "250 Moved tracks\n"); | |
843 | /* If we've moved to the head of the queue then prepare the track. */ | |
844 | if(q == qhead.next) | |
845 | prepare(c->ev, q); | |
846 | return 1; | |
847 | } | |
848 | ||
849 | static int c_part(struct conn *c, | |
850 | char **vec, | |
851 | int attribute((unused)) nvec) { | |
852 | sink_printf(ev_writer_sink(c->w), "252 %s\n", | |
853 | trackdb_getpart(vec[0], vec[1], vec[2])); | |
854 | return 1; | |
855 | } | |
856 | ||
857 | static int c_resolve(struct conn *c, | |
858 | char **vec, | |
859 | int attribute((unused)) nvec) { | |
860 | const char *track; | |
861 | ||
862 | if(!(track = trackdb_resolve(vec[0]))) { | |
863 | sink_writes(ev_writer_sink(c->w), "550 cannot resolve track\n"); | |
864 | return 1; | |
865 | } | |
866 | sink_printf(ev_writer_sink(c->w), "252 %s\n", track); | |
867 | return 1; | |
868 | } | |
869 | ||
870 | static int c_tags(struct conn *c, | |
871 | char attribute((unused)) **vec, | |
872 | int attribute((unused)) nvec) { | |
873 | char **tags = trackdb_alltags(); | |
874 | ||
875 | sink_printf(ev_writer_sink(c->w), "253 Tag list follows\n"); | |
876 | while(*tags) { | |
877 | sink_printf(ev_writer_sink(c->w), "%s%s\n", | |
878 | **tags == '.' ? "." : "", *tags); | |
879 | ++tags; | |
880 | } | |
881 | sink_writes(ev_writer_sink(c->w), ".\n"); | |
882 | return 1; /* completed */ | |
883 | ||
884 | } | |
885 | ||
886 | static int c_set_global(struct conn *c, | |
887 | char **vec, | |
888 | int attribute((unused)) nvec) { | |
889 | trackdb_set_global(vec[0], vec[1], c->who); | |
890 | sink_printf(ev_writer_sink(c->w), "250 OK\n"); | |
891 | return 1; | |
892 | } | |
893 | ||
894 | static int c_get_global(struct conn *c, | |
895 | char **vec, | |
896 | int attribute((unused)) nvec) { | |
897 | const char *s = trackdb_get_global(vec[0]); | |
898 | ||
899 | if(s) | |
900 | sink_printf(ev_writer_sink(c->w), "252 %s\n", s); | |
901 | else | |
902 | sink_writes(ev_writer_sink(c->w), "550 not found\n"); | |
903 | return 1; | |
904 | } | |
905 | ||
7858930d | 906 | static int c_nop(struct conn *c, |
907 | char attribute((unused)) **vec, | |
908 | int attribute((unused)) nvec) { | |
909 | sink_printf(ev_writer_sink(c->w), "250 Quack\n"); | |
910 | return 1; | |
911 | } | |
912 | ||
460b9539 | 913 | #define C_AUTH 0001 /* must be authenticated */ |
914 | #define C_TRUSTED 0002 /* must be trusted user */ | |
915 | ||
916 | static const struct command { | |
917 | const char *name; | |
918 | int minargs, maxargs; | |
919 | int (*fn)(struct conn *, char **, int); | |
920 | unsigned flags; | |
921 | } commands[] = { | |
922 | { "allfiles", 0, 2, c_allfiles, C_AUTH }, | |
923 | { "become", 1, 1, c_become, C_AUTH|C_TRUSTED }, | |
924 | { "dirs", 0, 2, c_dirs, C_AUTH }, | |
925 | { "disable", 0, 1, c_disable, C_AUTH }, | |
926 | { "enable", 0, 0, c_enable, C_AUTH }, | |
927 | { "enabled", 0, 0, c_enabled, C_AUTH }, | |
928 | { "exists", 1, 1, c_exists, C_AUTH }, | |
929 | { "files", 0, 2, c_files, C_AUTH }, | |
930 | { "get", 2, 2, c_get, C_AUTH }, | |
931 | { "get-global", 1, 1, c_get_global, C_AUTH }, | |
932 | { "length", 1, 1, c_length, C_AUTH }, | |
933 | { "log", 0, 0, c_log, C_AUTH }, | |
934 | { "move", 2, 2, c_move, C_AUTH }, | |
935 | { "moveafter", 1, INT_MAX, c_moveafter, C_AUTH }, | |
7858930d | 936 | { "nop", 0, 0, c_nop, C_AUTH }, |
460b9539 | 937 | { "part", 3, 3, c_part, C_AUTH }, |
938 | { "pause", 0, 0, c_pause, C_AUTH }, | |
939 | { "play", 1, 1, c_play, C_AUTH }, | |
940 | { "playing", 0, 0, c_playing, C_AUTH }, | |
941 | { "prefs", 1, 1, c_prefs, C_AUTH }, | |
942 | { "queue", 0, 0, c_queue, C_AUTH }, | |
943 | { "random-disable", 0, 0, c_random_disable, C_AUTH }, | |
944 | { "random-enable", 0, 0, c_random_enable, C_AUTH }, | |
945 | { "random-enabled", 0, 0, c_random_enabled, C_AUTH }, | |
946 | { "recent", 0, 0, c_recent, C_AUTH }, | |
947 | { "reconfigure", 0, 0, c_reconfigure, C_AUTH|C_TRUSTED }, | |
948 | { "remove", 1, 1, c_remove, C_AUTH }, | |
949 | { "rescan", 0, 0, c_rescan, C_AUTH|C_TRUSTED }, | |
950 | { "resolve", 1, 1, c_resolve, C_AUTH }, | |
951 | { "resume", 0, 0, c_resume, C_AUTH }, | |
952 | { "scratch", 0, 1, c_scratch, C_AUTH }, | |
953 | { "search", 1, 1, c_search, C_AUTH }, | |
954 | { "set", 3, 3, c_set, C_AUTH, }, | |
955 | { "set-global", 2, 2, c_set_global, C_AUTH }, | |
956 | { "shutdown", 0, 0, c_shutdown, C_AUTH|C_TRUSTED }, | |
957 | { "stats", 0, 0, c_stats, C_AUTH }, | |
958 | { "tags", 0, 0, c_tags, C_AUTH }, | |
959 | { "unset", 2, 2, c_set, C_AUTH }, | |
960 | { "unset-global", 1, 1, c_set_global, C_AUTH }, | |
961 | { "user", 2, 2, c_user, 0 }, | |
962 | { "version", 0, 0, c_version, C_AUTH }, | |
963 | { "volume", 0, 2, c_volume, C_AUTH } | |
964 | }; | |
965 | ||
966 | static void command_error(const char *msg, void *u) { | |
967 | struct conn *c = u; | |
968 | ||
969 | sink_printf(ev_writer_sink(c->w), "500 parse error: %s\n", msg); | |
970 | } | |
971 | ||
972 | /* process a command. Return 1 if complete, 0 if incomplete. */ | |
973 | static int command(struct conn *c, char *line) { | |
974 | char **vec; | |
975 | int nvec, n; | |
976 | ||
977 | D(("server command %s", line)); | |
978 | if(!(vec = split(line, &nvec, SPLIT_QUOTES, command_error, c))) { | |
979 | sink_writes(ev_writer_sink(c->w), "500 cannot parse command\n"); | |
980 | return 1; | |
981 | } | |
982 | if(nvec == 0) { | |
983 | sink_writes(ev_writer_sink(c->w), "500 do what?\n"); | |
984 | return 1; | |
985 | } | |
986 | if((n = TABLE_FIND(commands, struct command, name, vec[0])) < 0) | |
987 | sink_writes(ev_writer_sink(c->w), "500 unknown command\n"); | |
988 | else { | |
989 | if((commands[n].flags & C_AUTH) && !c->who) { | |
990 | sink_writes(ev_writer_sink(c->w), "530 not authenticated\n"); | |
991 | return 1; | |
992 | } | |
993 | if((commands[n].flags & C_TRUSTED) && !trusted(c)) { | |
994 | sink_writes(ev_writer_sink(c->w), "530 insufficient privilege\n"); | |
995 | return 1; | |
996 | } | |
997 | ++vec; | |
998 | --nvec; | |
999 | if(nvec < commands[n].minargs) { | |
1000 | sink_writes(ev_writer_sink(c->w), "500 missing argument(s)\n"); | |
1001 | return 1; | |
1002 | } | |
1003 | if(nvec > commands[n].maxargs) { | |
1004 | sink_writes(ev_writer_sink(c->w), "500 too many arguments\n"); | |
1005 | return 1; | |
1006 | } | |
1007 | return commands[n].fn(c, vec, nvec); | |
1008 | } | |
1009 | return 1; /* completed */ | |
1010 | } | |
1011 | ||
1012 | /* redirect to the right reader callback for our current state */ | |
1013 | static int redirect_reader_callback(ev_source *ev, | |
1014 | ev_reader *reader, | |
1015 | int fd, | |
1016 | void *ptr, | |
1017 | size_t bytes, | |
1018 | int eof, | |
1019 | void *u) { | |
1020 | struct conn *c = u; | |
1021 | ||
1022 | return c->reader(ev, reader, fd, ptr, bytes, eof, u); | |
1023 | } | |
1024 | ||
1025 | /* the main command reader */ | |
1026 | static int reader_callback(ev_source attribute((unused)) *ev, | |
1027 | ev_reader *reader, | |
1028 | int attribute((unused)) fd, | |
1029 | void *ptr, | |
1030 | size_t bytes, | |
1031 | int eof, | |
1032 | void *u) { | |
1033 | struct conn *c = u; | |
1034 | char *eol; | |
1035 | int complete; | |
1036 | ||
1037 | D(("server reader_callback")); | |
1038 | while((eol = memchr(ptr, '\n', bytes))) { | |
1039 | *eol++ = 0; | |
1040 | ev_reader_consume(reader, eol - (char *)ptr); | |
1041 | complete = command(c, ptr); | |
1042 | bytes -= (eol - (char *)ptr); | |
1043 | ptr = eol; | |
1044 | if(!complete) { | |
1045 | /* the command had better have set a new reader callback */ | |
1046 | if(bytes || eof) | |
1047 | /* there are further bytes to read, or we are at eof; arrange for the | |
1048 | * command's reader callback to handle them */ | |
1049 | return ev_reader_incomplete(reader); | |
1050 | /* nothing's going on right now */ | |
1051 | return 0; | |
1052 | } | |
1053 | /* command completed, we can go around and handle the next one */ | |
1054 | } | |
1055 | if(eof) { | |
1056 | if(bytes) | |
1057 | error(0, "S%x unterminated line", c->tag); | |
1058 | c->r = 0; | |
1059 | return ev_writer_close(c->w); | |
1060 | } | |
1061 | return 0; | |
1062 | } | |
1063 | ||
1064 | static int listen_callback(ev_source *ev, | |
1065 | int fd, | |
1066 | const struct sockaddr attribute((unused)) *remote, | |
1067 | socklen_t attribute((unused)) rlen, | |
1068 | void *u) { | |
1069 | const struct listener *l = u; | |
1070 | struct conn *c = xmalloc(sizeof *c); | |
1071 | static unsigned tags; | |
1072 | ||
1073 | D(("server listen_callback fd %d (%s)", fd, l->name)); | |
1074 | nonblock(fd); | |
1075 | cloexec(fd); | |
1076 | c->tag = tags++; | |
1077 | c->ev = ev; | |
1078 | c->w = ev_writer_new(ev, fd, writer_error, c); | |
1079 | c->r = ev_reader_new(ev, fd, redirect_reader_callback, reader_error, c); | |
1080 | c->fd = fd; | |
1081 | c->reader = reader_callback; | |
1082 | c->l = l; | |
1083 | gcry_randomize(c->nonce, sizeof c->nonce, GCRY_STRONG_RANDOM); | |
1084 | sink_printf(ev_writer_sink(c->w), "231 %s\n", hex(c->nonce, sizeof c->nonce)); | |
1085 | return 0; | |
1086 | } | |
1087 | ||
1088 | int server_start(ev_source *ev, int pf, | |
1089 | size_t socklen, const struct sockaddr *sa, | |
1090 | const char *name) { | |
1091 | int fd; | |
1092 | struct listener *l = xmalloc(sizeof *l); | |
1093 | static const int one = 1; | |
1094 | ||
1095 | D(("server_init socket %s", name)); | |
1096 | fd = xsocket(pf, SOCK_STREAM, 0); | |
1097 | xsetsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one); | |
1098 | if(bind(fd, sa, socklen) < 0) { | |
1099 | error(errno, "error binding to %s", name); | |
1100 | return -1; | |
1101 | } | |
1102 | xlisten(fd, 128); | |
1103 | nonblock(fd); | |
1104 | cloexec(fd); | |
1105 | l->name = name; | |
1106 | l->pf = pf; | |
1107 | if(ev_listen(ev, fd, listen_callback, l)) exit(EXIT_FAILURE); | |
1108 | return fd; | |
1109 | } | |
1110 | ||
1111 | int server_stop(ev_source *ev, int fd) { | |
1112 | xclose(fd); | |
1113 | return ev_listen_cancel(ev, fd); | |
1114 | } | |
1115 | ||
1116 | /* | |
1117 | Local Variables: | |
1118 | c-basic-offset:2 | |
1119 | comment-column:40 | |
1120 | fill-column:79 | |
1121 | End: | |
1122 | */ |