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