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 | ||
668 | static int c_stats(struct conn *c, | |
669 | char attribute((unused)) **vec, | |
670 | int attribute((unused)) nvec) { | |
671 | char **v; | |
672 | int nv, n; | |
673 | ||
674 | v = trackdb_stats(&nv); | |
675 | sink_printf(ev_writer_sink(c->w), "253 stats\n"); | |
676 | for(n = 0; n < nv; ++n) { | |
677 | if(v[n][0] == '.') | |
678 | sink_writes(ev_writer_sink(c->w), "."); | |
679 | sink_printf(ev_writer_sink(c->w), "%s\n", v[n]); | |
680 | } | |
681 | sink_writes(ev_writer_sink(c->w), ".\n"); | |
682 | return 1; | |
683 | } | |
684 | ||
685 | static int c_volume(struct conn *c, | |
686 | char **vec, | |
687 | int nvec) { | |
688 | int l, r, set; | |
689 | char lb[32], rb[32]; | |
690 | ||
691 | switch(nvec) { | |
692 | case 0: | |
693 | set = 0; | |
694 | break; | |
695 | case 1: | |
696 | l = r = atoi(vec[0]); | |
697 | set = 1; | |
698 | break; | |
699 | case 2: | |
700 | l = atoi(vec[0]); | |
701 | r = atoi(vec[1]); | |
702 | set = 1; | |
703 | break; | |
704 | default: | |
705 | abort(); | |
706 | } | |
707 | if(mixer_control(&l, &r, set)) | |
708 | sink_writes(ev_writer_sink(c->w), "550 error accessing mixer\n"); | |
709 | else { | |
710 | sink_printf(ev_writer_sink(c->w), "252 %d %d\n", l, r); | |
711 | if(l != volume_left || r != volume_right) { | |
712 | volume_left = l; | |
713 | volume_right = r; | |
714 | snprintf(lb, sizeof lb, "%d", l); | |
715 | snprintf(rb, sizeof rb, "%d", r); | |
716 | eventlog("volume", lb, rb, (char *)0); | |
717 | } | |
718 | } | |
719 | return 1; | |
720 | } | |
721 | ||
722 | /* we are logging, and some data is available to read */ | |
723 | static int logging_reader_callback(ev_source *ev, | |
724 | ev_reader *reader, | |
725 | int fd, | |
726 | void *ptr, | |
727 | size_t bytes, | |
728 | int eof, | |
729 | void *u) { | |
730 | struct conn *c = u; | |
731 | ||
732 | /* don't log to this conn any more */ | |
733 | eventlog_remove(c->lo); | |
734 | /* terminate the log output */ | |
735 | sink_writes(ev_writer_sink(c->w), ".\n"); | |
736 | /* restore the reader callback */ | |
737 | c->reader = reader_callback; | |
738 | /* ...and exit via it */ | |
739 | return c->reader(ev, reader, fd, ptr, bytes, eof, u); | |
740 | } | |
741 | ||
742 | static void logclient(const char *msg, void *user) { | |
743 | struct conn *c = user; | |
744 | ||
745 | sink_printf(ev_writer_sink(c->w), "%"PRIxMAX" %s\n", | |
746 | (uintmax_t)time(0), msg); | |
747 | } | |
748 | ||
749 | static int c_log(struct conn *c, | |
750 | char attribute((unused)) **vec, | |
751 | int attribute((unused)) nvec) { | |
752 | time_t now; | |
753 | ||
754 | sink_writes(ev_writer_sink(c->w), "254 OK\n"); | |
755 | /* pump out initial state */ | |
756 | time(&now); | |
757 | sink_printf(ev_writer_sink(c->w), "%"PRIxMAX" state %s\n", | |
758 | (uintmax_t)now, | |
759 | playing_is_enabled() ? "enable_play" : "disable_play"); | |
760 | sink_printf(ev_writer_sink(c->w), "%"PRIxMAX" state %s\n", | |
761 | (uintmax_t)now, | |
762 | random_is_enabled() ? "enable_random" : "disable_random"); | |
763 | sink_printf(ev_writer_sink(c->w), "%"PRIxMAX" state %s\n", | |
764 | (uintmax_t)now, | |
765 | paused ? "pause" : "resume"); | |
5abe307a RK |
766 | if(playing) |
767 | sink_printf(ev_writer_sink(c->w), "%"PRIxMAX" state playing\n", | |
768 | (uintmax_t)now); | |
df44d69b RK |
769 | /* Initial volume */ |
770 | sink_printf(ev_writer_sink(c->w), "%"PRIxMAX" volume %d %d\n", | |
771 | (uintmax_t)now, volume_left, volume_right); | |
460b9539 | 772 | c->lo = xmalloc(sizeof *c->lo); |
773 | c->lo->fn = logclient; | |
774 | c->lo->user = c; | |
775 | eventlog_add(c->lo); | |
776 | c->reader = logging_reader_callback; | |
777 | return 0; | |
778 | } | |
779 | ||
780 | static void post_move_cleanup(void) { | |
781 | struct queue_entry *q; | |
782 | ||
138baa2d RK |
783 | /* If we have caused any random tracks to not be at the end then we make them |
784 | * no longer be random. */ | |
460b9539 | 785 | for(q = qhead.next; q != &qhead; q = q->next) |
786 | if(q->state == playing_random && q->next != &qhead) | |
787 | q->state = playing_unplayed; | |
788 | /* That might mean we need to add a new random track. */ | |
789 | add_random_track(); | |
790 | queue_write(); | |
791 | } | |
792 | ||
793 | static int c_move(struct conn *c, | |
794 | char **vec, | |
795 | int attribute((unused)) nvec) { | |
796 | struct queue_entry *q; | |
797 | int n; | |
798 | ||
799 | if(config->restrictions & RESTRICT_MOVE) { | |
800 | if(!trusted(c)) { | |
801 | sink_writes(ev_writer_sink(c->w), | |
802 | "550 only trusted users can move tracks\n"); | |
803 | return 1; | |
804 | } | |
805 | } | |
806 | if(!(q = queue_find(vec[0]))) { | |
807 | sink_writes(ev_writer_sink(c->w), "550 no such track on the queue\n"); | |
808 | return 1; | |
809 | } | |
810 | n = queue_move(q, atoi(vec[1]), c->who); | |
811 | post_move_cleanup(); | |
812 | sink_printf(ev_writer_sink(c->w), "252 %d\n", n); | |
813 | /* If we've moved to the head of the queue then prepare the track. */ | |
814 | if(q == qhead.next) | |
815 | prepare(c->ev, q); | |
816 | return 1; | |
817 | } | |
818 | ||
819 | static int c_moveafter(struct conn *c, | |
820 | char **vec, | |
821 | int attribute((unused)) nvec) { | |
822 | struct queue_entry *q, **qs; | |
823 | int n; | |
824 | ||
825 | if(config->restrictions & RESTRICT_MOVE) { | |
826 | if(!trusted(c)) { | |
827 | sink_writes(ev_writer_sink(c->w), | |
828 | "550 only trusted users can move tracks\n"); | |
829 | return 1; | |
830 | } | |
831 | } | |
832 | if(vec[0][0]) { | |
833 | if(!(q = queue_find(vec[0]))) { | |
834 | sink_writes(ev_writer_sink(c->w), "550 no such track on the queue\n"); | |
835 | return 1; | |
836 | } | |
837 | } else | |
838 | q = 0; | |
839 | ++vec; | |
840 | --nvec; | |
841 | qs = xcalloc(nvec, sizeof *qs); | |
842 | for(n = 0; n < nvec; ++n) | |
843 | if(!(qs[n] = queue_find(vec[n]))) { | |
844 | sink_writes(ev_writer_sink(c->w), "550 no such track on the queue\n"); | |
845 | return 1; | |
846 | } | |
847 | queue_moveafter(q, nvec, qs, c->who); | |
848 | post_move_cleanup(); | |
849 | sink_printf(ev_writer_sink(c->w), "250 Moved tracks\n"); | |
850 | /* If we've moved to the head of the queue then prepare the track. */ | |
851 | if(q == qhead.next) | |
852 | prepare(c->ev, q); | |
853 | return 1; | |
854 | } | |
855 | ||
856 | static int c_part(struct conn *c, | |
857 | char **vec, | |
858 | int attribute((unused)) nvec) { | |
859 | sink_printf(ev_writer_sink(c->w), "252 %s\n", | |
860 | trackdb_getpart(vec[0], vec[1], vec[2])); | |
861 | return 1; | |
862 | } | |
863 | ||
864 | static int c_resolve(struct conn *c, | |
865 | char **vec, | |
866 | int attribute((unused)) nvec) { | |
867 | const char *track; | |
868 | ||
869 | if(!(track = trackdb_resolve(vec[0]))) { | |
870 | sink_writes(ev_writer_sink(c->w), "550 cannot resolve track\n"); | |
871 | return 1; | |
872 | } | |
873 | sink_printf(ev_writer_sink(c->w), "252 %s\n", track); | |
874 | return 1; | |
875 | } | |
876 | ||
877 | static int c_tags(struct conn *c, | |
878 | char attribute((unused)) **vec, | |
879 | int attribute((unused)) nvec) { | |
880 | char **tags = trackdb_alltags(); | |
881 | ||
882 | sink_printf(ev_writer_sink(c->w), "253 Tag list follows\n"); | |
883 | while(*tags) { | |
884 | sink_printf(ev_writer_sink(c->w), "%s%s\n", | |
885 | **tags == '.' ? "." : "", *tags); | |
886 | ++tags; | |
887 | } | |
888 | sink_writes(ev_writer_sink(c->w), ".\n"); | |
889 | return 1; /* completed */ | |
890 | ||
891 | } | |
892 | ||
893 | static int c_set_global(struct conn *c, | |
894 | char **vec, | |
895 | int attribute((unused)) nvec) { | |
896 | trackdb_set_global(vec[0], vec[1], c->who); | |
897 | sink_printf(ev_writer_sink(c->w), "250 OK\n"); | |
898 | return 1; | |
899 | } | |
900 | ||
901 | static int c_get_global(struct conn *c, | |
902 | char **vec, | |
903 | int attribute((unused)) nvec) { | |
904 | const char *s = trackdb_get_global(vec[0]); | |
905 | ||
906 | if(s) | |
907 | sink_printf(ev_writer_sink(c->w), "252 %s\n", s); | |
908 | else | |
909 | sink_writes(ev_writer_sink(c->w), "550 not found\n"); | |
910 | return 1; | |
911 | } | |
912 | ||
7858930d | 913 | static int c_nop(struct conn *c, |
914 | char attribute((unused)) **vec, | |
915 | int attribute((unused)) nvec) { | |
916 | sink_printf(ev_writer_sink(c->w), "250 Quack\n"); | |
917 | return 1; | |
918 | } | |
919 | ||
2a10b70b RK |
920 | static int c_new(struct conn *c, |
921 | char **vec, | |
922 | int nvec) { | |
923 | char **tracks = trackdb_new(0, nvec > 0 ? atoi(vec[0]) : INT_MAX); | |
924 | ||
925 | sink_printf(ev_writer_sink(c->w), "253 New track list follows\n"); | |
926 | while(*tracks) { | |
927 | sink_printf(ev_writer_sink(c->w), "%s%s\n", | |
928 | **tracks == '.' ? "." : "", *tracks); | |
929 | ++tracks; | |
930 | } | |
931 | sink_writes(ev_writer_sink(c->w), ".\n"); | |
932 | return 1; /* completed */ | |
933 | ||
934 | } | |
935 | ||
ca831831 RK |
936 | static int c_rtp_address(struct conn *c, |
937 | char attribute((unused)) **vec, | |
938 | int attribute((unused)) nvec) { | |
939 | if(config->speaker_backend == BACKEND_NETWORK) { | |
940 | sink_printf(ev_writer_sink(c->w), "252 %s %s\n", | |
941 | quoteutf8(config->broadcast.s[0]), | |
942 | quoteutf8(config->broadcast.s[1])); | |
943 | } else | |
944 | sink_writes(ev_writer_sink(c->w), "550 No RTP\n"); | |
945 | return 1; | |
946 | } | |
947 | ||
460b9539 | 948 | #define C_AUTH 0001 /* must be authenticated */ |
949 | #define C_TRUSTED 0002 /* must be trusted user */ | |
950 | ||
951 | static const struct command { | |
952 | const char *name; | |
953 | int minargs, maxargs; | |
954 | int (*fn)(struct conn *, char **, int); | |
955 | unsigned flags; | |
956 | } commands[] = { | |
957 | { "allfiles", 0, 2, c_allfiles, C_AUTH }, | |
958 | { "become", 1, 1, c_become, C_AUTH|C_TRUSTED }, | |
959 | { "dirs", 0, 2, c_dirs, C_AUTH }, | |
960 | { "disable", 0, 1, c_disable, C_AUTH }, | |
961 | { "enable", 0, 0, c_enable, C_AUTH }, | |
962 | { "enabled", 0, 0, c_enabled, C_AUTH }, | |
963 | { "exists", 1, 1, c_exists, C_AUTH }, | |
964 | { "files", 0, 2, c_files, C_AUTH }, | |
965 | { "get", 2, 2, c_get, C_AUTH }, | |
966 | { "get-global", 1, 1, c_get_global, C_AUTH }, | |
967 | { "length", 1, 1, c_length, C_AUTH }, | |
968 | { "log", 0, 0, c_log, C_AUTH }, | |
969 | { "move", 2, 2, c_move, C_AUTH }, | |
970 | { "moveafter", 1, INT_MAX, c_moveafter, C_AUTH }, | |
2a10b70b | 971 | { "new", 0, 1, c_new, C_AUTH }, |
7858930d | 972 | { "nop", 0, 0, c_nop, C_AUTH }, |
460b9539 | 973 | { "part", 3, 3, c_part, C_AUTH }, |
974 | { "pause", 0, 0, c_pause, C_AUTH }, | |
975 | { "play", 1, 1, c_play, C_AUTH }, | |
976 | { "playing", 0, 0, c_playing, C_AUTH }, | |
977 | { "prefs", 1, 1, c_prefs, C_AUTH }, | |
978 | { "queue", 0, 0, c_queue, C_AUTH }, | |
979 | { "random-disable", 0, 0, c_random_disable, C_AUTH }, | |
980 | { "random-enable", 0, 0, c_random_enable, C_AUTH }, | |
981 | { "random-enabled", 0, 0, c_random_enabled, C_AUTH }, | |
982 | { "recent", 0, 0, c_recent, C_AUTH }, | |
983 | { "reconfigure", 0, 0, c_reconfigure, C_AUTH|C_TRUSTED }, | |
984 | { "remove", 1, 1, c_remove, C_AUTH }, | |
985 | { "rescan", 0, 0, c_rescan, C_AUTH|C_TRUSTED }, | |
986 | { "resolve", 1, 1, c_resolve, C_AUTH }, | |
987 | { "resume", 0, 0, c_resume, C_AUTH }, | |
ca831831 | 988 | { "rtp-address", 0, 0, c_rtp_address, C_AUTH }, |
460b9539 | 989 | { "scratch", 0, 1, c_scratch, C_AUTH }, |
990 | { "search", 1, 1, c_search, C_AUTH }, | |
991 | { "set", 3, 3, c_set, C_AUTH, }, | |
992 | { "set-global", 2, 2, c_set_global, C_AUTH }, | |
993 | { "shutdown", 0, 0, c_shutdown, C_AUTH|C_TRUSTED }, | |
994 | { "stats", 0, 0, c_stats, C_AUTH }, | |
995 | { "tags", 0, 0, c_tags, C_AUTH }, | |
996 | { "unset", 2, 2, c_set, C_AUTH }, | |
997 | { "unset-global", 1, 1, c_set_global, C_AUTH }, | |
998 | { "user", 2, 2, c_user, 0 }, | |
999 | { "version", 0, 0, c_version, C_AUTH }, | |
1000 | { "volume", 0, 2, c_volume, C_AUTH } | |
1001 | }; | |
1002 | ||
1003 | static void command_error(const char *msg, void *u) { | |
1004 | struct conn *c = u; | |
1005 | ||
1006 | sink_printf(ev_writer_sink(c->w), "500 parse error: %s\n", msg); | |
1007 | } | |
1008 | ||
1009 | /* process a command. Return 1 if complete, 0 if incomplete. */ | |
1010 | static int command(struct conn *c, char *line) { | |
1011 | char **vec; | |
1012 | int nvec, n; | |
1013 | ||
1014 | D(("server command %s", line)); | |
1015 | if(!(vec = split(line, &nvec, SPLIT_QUOTES, command_error, c))) { | |
1016 | sink_writes(ev_writer_sink(c->w), "500 cannot parse command\n"); | |
1017 | return 1; | |
1018 | } | |
1019 | if(nvec == 0) { | |
1020 | sink_writes(ev_writer_sink(c->w), "500 do what?\n"); | |
1021 | return 1; | |
1022 | } | |
1023 | if((n = TABLE_FIND(commands, struct command, name, vec[0])) < 0) | |
1024 | sink_writes(ev_writer_sink(c->w), "500 unknown command\n"); | |
1025 | else { | |
1026 | if((commands[n].flags & C_AUTH) && !c->who) { | |
1027 | sink_writes(ev_writer_sink(c->w), "530 not authenticated\n"); | |
1028 | return 1; | |
1029 | } | |
1030 | if((commands[n].flags & C_TRUSTED) && !trusted(c)) { | |
1031 | sink_writes(ev_writer_sink(c->w), "530 insufficient privilege\n"); | |
1032 | return 1; | |
1033 | } | |
1034 | ++vec; | |
1035 | --nvec; | |
1036 | if(nvec < commands[n].minargs) { | |
1037 | sink_writes(ev_writer_sink(c->w), "500 missing argument(s)\n"); | |
1038 | return 1; | |
1039 | } | |
1040 | if(nvec > commands[n].maxargs) { | |
1041 | sink_writes(ev_writer_sink(c->w), "500 too many arguments\n"); | |
1042 | return 1; | |
1043 | } | |
1044 | return commands[n].fn(c, vec, nvec); | |
1045 | } | |
1046 | return 1; /* completed */ | |
1047 | } | |
1048 | ||
1049 | /* redirect to the right reader callback for our current state */ | |
1050 | static int redirect_reader_callback(ev_source *ev, | |
1051 | ev_reader *reader, | |
1052 | int fd, | |
1053 | void *ptr, | |
1054 | size_t bytes, | |
1055 | int eof, | |
1056 | void *u) { | |
1057 | struct conn *c = u; | |
1058 | ||
1059 | return c->reader(ev, reader, fd, ptr, bytes, eof, u); | |
1060 | } | |
1061 | ||
1062 | /* the main command reader */ | |
1063 | static int reader_callback(ev_source attribute((unused)) *ev, | |
1064 | ev_reader *reader, | |
1065 | int attribute((unused)) fd, | |
1066 | void *ptr, | |
1067 | size_t bytes, | |
1068 | int eof, | |
1069 | void *u) { | |
1070 | struct conn *c = u; | |
1071 | char *eol; | |
1072 | int complete; | |
1073 | ||
1074 | D(("server reader_callback")); | |
1075 | while((eol = memchr(ptr, '\n', bytes))) { | |
1076 | *eol++ = 0; | |
1077 | ev_reader_consume(reader, eol - (char *)ptr); | |
1078 | complete = command(c, ptr); | |
1079 | bytes -= (eol - (char *)ptr); | |
1080 | ptr = eol; | |
1081 | if(!complete) { | |
1082 | /* the command had better have set a new reader callback */ | |
1083 | if(bytes || eof) | |
1084 | /* there are further bytes to read, or we are at eof; arrange for the | |
1085 | * command's reader callback to handle them */ | |
1086 | return ev_reader_incomplete(reader); | |
1087 | /* nothing's going on right now */ | |
1088 | return 0; | |
1089 | } | |
1090 | /* command completed, we can go around and handle the next one */ | |
1091 | } | |
1092 | if(eof) { | |
1093 | if(bytes) | |
1094 | error(0, "S%x unterminated line", c->tag); | |
1095 | c->r = 0; | |
1096 | return ev_writer_close(c->w); | |
1097 | } | |
1098 | return 0; | |
1099 | } | |
1100 | ||
1101 | static int listen_callback(ev_source *ev, | |
1102 | int fd, | |
1103 | const struct sockaddr attribute((unused)) *remote, | |
1104 | socklen_t attribute((unused)) rlen, | |
1105 | void *u) { | |
1106 | const struct listener *l = u; | |
1107 | struct conn *c = xmalloc(sizeof *c); | |
1108 | static unsigned tags; | |
1109 | ||
1110 | D(("server listen_callback fd %d (%s)", fd, l->name)); | |
1111 | nonblock(fd); | |
1112 | cloexec(fd); | |
1113 | c->tag = tags++; | |
1114 | c->ev = ev; | |
e8c92ba7 RK |
1115 | c->w = ev_writer_new(ev, fd, writer_error, c, |
1116 | "client writer"); | |
1117 | c->r = ev_reader_new(ev, fd, redirect_reader_callback, reader_error, c, | |
1118 | "client reader"); | |
460b9539 | 1119 | c->fd = fd; |
1120 | c->reader = reader_callback; | |
1121 | c->l = l; | |
1122 | gcry_randomize(c->nonce, sizeof c->nonce, GCRY_STRONG_RANDOM); | |
637fdea3 RK |
1123 | if(!strcmp(config->authorization_algorithm, "sha1") |
1124 | || !strcmp(config->authorization_algorithm, "SHA1")) { | |
1125 | sink_printf(ev_writer_sink(c->w), "231 %s\n", | |
1126 | hex(c->nonce, sizeof c->nonce)); | |
1127 | } else { | |
1128 | sink_printf(ev_writer_sink(c->w), "231 %s %s\n", | |
1129 | config->authorization_algorithm, | |
1130 | hex(c->nonce, sizeof c->nonce)); | |
1131 | } | |
460b9539 | 1132 | return 0; |
1133 | } | |
1134 | ||
1135 | int server_start(ev_source *ev, int pf, | |
1136 | size_t socklen, const struct sockaddr *sa, | |
1137 | const char *name) { | |
1138 | int fd; | |
1139 | struct listener *l = xmalloc(sizeof *l); | |
1140 | static const int one = 1; | |
1141 | ||
1142 | D(("server_init socket %s", name)); | |
1143 | fd = xsocket(pf, SOCK_STREAM, 0); | |
1144 | xsetsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one); | |
1145 | if(bind(fd, sa, socklen) < 0) { | |
1146 | error(errno, "error binding to %s", name); | |
1147 | return -1; | |
1148 | } | |
1149 | xlisten(fd, 128); | |
1150 | nonblock(fd); | |
1151 | cloexec(fd); | |
1152 | l->name = name; | |
1153 | l->pf = pf; | |
e8c92ba7 RK |
1154 | if(ev_listen(ev, fd, listen_callback, l, "server listener")) |
1155 | exit(EXIT_FAILURE); | |
460b9539 | 1156 | return fd; |
1157 | } | |
1158 | ||
1159 | int server_stop(ev_source *ev, int fd) { | |
1160 | xclose(fd); | |
1161 | return ev_listen_cancel(ev, fd); | |
1162 | } | |
1163 | ||
1164 | /* | |
1165 | Local Variables: | |
1166 | c-basic-offset:2 | |
1167 | comment-column:40 | |
1168 | fill-column:79 | |
1169 | End: | |
1170 | */ |