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