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