Commit | Line | Data |
---|---|---|
460b9539 | 1 | /* |
2 | * This file is part of DisOrder. | |
964e027d | 3 | * Copyright (C) 2004-2008 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" | |
5df73aeb | 54 | #include "rights.h" |
460b9539 | 55 | #include "trackdb.h" |
56 | #include "table.h" | |
57 | #include "kvp.h" | |
58 | #include "mixer.h" | |
59 | #include "sink.h" | |
60 | #include "authhash.h" | |
61 | #include "plugin.h" | |
62 | #include "printf.h" | |
63 | #include "trackname.h" | |
64 | #include "eventlog.h" | |
65 | #include "defs.h" | |
66 | #include "cache.h" | |
f9635e06 | 67 | #include "unicode.h" |
b12be54a | 68 | #include "cookies.h" |
fce810c2 | 69 | #include "base64.h" |
6207d2f3 | 70 | #include "hash.h" |
71 | #include "mime.h" | |
72 | #include "sendmail.h" | |
73 | #include "wstat.h" | |
460b9539 | 74 | |
75 | #ifndef NONCE_SIZE | |
76 | # define NONCE_SIZE 16 | |
77 | #endif | |
78 | ||
10114017 | 79 | #ifndef CONFIRM_SIZE |
80 | # define CONFIRM_SIZE 10 | |
81 | #endif | |
82 | ||
460b9539 | 83 | int volume_left, volume_right; /* last known volume */ |
84 | ||
18e6d6e6 | 85 | /** @brief Accept all well-formed login attempts |
86 | * | |
87 | * Used in debugging. | |
88 | */ | |
89 | int wideopen; | |
90 | ||
460b9539 | 91 | struct listener { |
92 | const char *name; | |
93 | int pf; | |
94 | }; | |
95 | ||
397ef7bb | 96 | /** @brief One client connection */ |
460b9539 | 97 | struct conn { |
397ef7bb | 98 | /** @brief Read commands from here */ |
460b9539 | 99 | ev_reader *r; |
397ef7bb | 100 | /** @brief Send responses to here */ |
460b9539 | 101 | ev_writer *w; |
397ef7bb | 102 | /** @brief Underlying file descriptor */ |
460b9539 | 103 | int fd; |
397ef7bb | 104 | /** @brief Unique identifier for connection used in log messages */ |
460b9539 | 105 | unsigned tag; |
397ef7bb | 106 | /** @brief Login name or NULL */ |
460b9539 | 107 | char *who; |
397ef7bb | 108 | /** @brief Event loop */ |
460b9539 | 109 | ev_source *ev; |
397ef7bb | 110 | /** @brief Nonce chosen for this connection */ |
460b9539 | 111 | unsigned char nonce[NONCE_SIZE]; |
397ef7bb RK |
112 | /** @brief Current reader callback |
113 | * | |
114 | * We change this depending on whether we're servicing the @b log command | |
115 | */ | |
460b9539 | 116 | ev_reader_callback *reader; |
397ef7bb | 117 | /** @brief Event log output sending to this connection */ |
460b9539 | 118 | struct eventlog_output *lo; |
397ef7bb | 119 | /** @brief Parent listener */ |
460b9539 | 120 | const struct listener *l; |
b12be54a RK |
121 | /** @brief Login cookie or NULL */ |
122 | char *cookie; | |
eb5dc014 RK |
123 | /** @brief Connection rights */ |
124 | rights_type rights; | |
0126692c | 125 | /** @brief Next connection */ |
126 | struct conn *next; | |
460b9539 | 127 | }; |
128 | ||
0126692c | 129 | /** @brief Linked list of connections */ |
130 | static struct conn *connections; | |
131 | ||
460b9539 | 132 | static int reader_callback(ev_source *ev, |
133 | ev_reader *reader, | |
460b9539 | 134 | void *ptr, |
135 | size_t bytes, | |
136 | int eof, | |
137 | void *u); | |
138 | ||
139 | static const char *noyes[] = { "no", "yes" }; | |
140 | ||
0126692c | 141 | /** @brief Remove a connection from the connection list */ |
142 | static void remove_connection(struct conn *c) { | |
143 | struct conn **cc; | |
144 | ||
145 | for(cc = &connections; *cc && *cc != c; cc = &(*cc)->next) | |
146 | ; | |
147 | if(*cc) | |
148 | *cc = c->next; | |
149 | } | |
150 | ||
397ef7bb RK |
151 | /** @brief Called when a connection's writer fails or is shut down |
152 | * | |
153 | * If the connection still has a raeder that is cancelled. | |
154 | */ | |
460b9539 | 155 | static int writer_error(ev_source attribute((unused)) *ev, |
460b9539 | 156 | int errno_value, |
157 | void *u) { | |
158 | struct conn *c = u; | |
159 | ||
8d8b8c1f | 160 | D(("server writer_error S%x %d", c->tag, errno_value)); |
460b9539 | 161 | if(errno_value == 0) { |
162 | /* writer is done */ | |
8d8b8c1f | 163 | D(("S%x writer completed", c->tag)); |
460b9539 | 164 | } else { |
165 | if(errno_value != EPIPE) | |
166 | error(errno_value, "S%x write error on socket", c->tag); | |
38b8221f | 167 | if(c->r) { |
8d8b8c1f | 168 | D(("cancel reader")); |
38b8221f RK |
169 | ev_reader_cancel(c->r); |
170 | c->r = 0; | |
171 | } | |
8d8b8c1f | 172 | D(("done cancel reader")); |
460b9539 | 173 | } |
38b8221f | 174 | c->w = 0; |
75d64210 | 175 | ev_report(ev); |
0126692c | 176 | remove_connection(c); |
460b9539 | 177 | return 0; |
178 | } | |
179 | ||
397ef7bb RK |
180 | /** @brief Called when a conncetion's reader fails or is shut down |
181 | * | |
182 | * If connection still has a writer then it is closed. | |
183 | */ | |
460b9539 | 184 | static int reader_error(ev_source attribute((unused)) *ev, |
460b9539 | 185 | int errno_value, |
186 | void *u) { | |
187 | struct conn *c = u; | |
188 | ||
8d8b8c1f | 189 | D(("server reader_error S%x %d", c->tag, errno_value)); |
1563f6f6 | 190 | error(errno_value, "S%x read error on socket", c->tag); |
38b8221f RK |
191 | if(c->w) |
192 | ev_writer_close(c->w); | |
193 | c->w = 0; | |
194 | c->r = 0; | |
768d7355 | 195 | ev_report(ev); |
0126692c | 196 | remove_connection(c); |
460b9539 | 197 | return 0; |
198 | } | |
199 | ||
460b9539 | 200 | static int c_disable(struct conn *c, char **vec, int nvec) { |
201 | if(nvec == 0) | |
202 | disable_playing(c->who); | |
203 | else if(nvec == 1 && !strcmp(vec[0], "now")) | |
204 | disable_playing(c->who); | |
205 | else { | |
206 | sink_writes(ev_writer_sink(c->w), "550 invalid argument\n"); | |
207 | return 1; /* completed */ | |
208 | } | |
209 | sink_writes(ev_writer_sink(c->w), "250 OK\n"); | |
210 | return 1; /* completed */ | |
211 | } | |
212 | ||
213 | static int c_enable(struct conn *c, | |
214 | char attribute((unused)) **vec, | |
215 | int attribute((unused)) nvec) { | |
216 | enable_playing(c->who, c->ev); | |
217 | /* Enable implicitly unpauses if there is nothing playing */ | |
218 | if(paused && !playing) resume_playing(c->who); | |
219 | sink_writes(ev_writer_sink(c->w), "250 OK\n"); | |
220 | return 1; /* completed */ | |
221 | } | |
222 | ||
223 | static int c_enabled(struct conn *c, | |
224 | char attribute((unused)) **vec, | |
225 | int attribute((unused)) nvec) { | |
226 | sink_printf(ev_writer_sink(c->w), "252 %s\n", noyes[playing_is_enabled()]); | |
227 | return 1; /* completed */ | |
228 | } | |
229 | ||
230 | static int c_play(struct conn *c, char **vec, | |
231 | int attribute((unused)) nvec) { | |
232 | const char *track; | |
233 | struct queue_entry *q; | |
234 | ||
235 | if(!trackdb_exists(vec[0])) { | |
236 | sink_writes(ev_writer_sink(c->w), "550 track is not in database\n"); | |
237 | return 1; | |
238 | } | |
239 | if(!(track = trackdb_resolve(vec[0]))) { | |
240 | sink_writes(ev_writer_sink(c->w), "550 cannot resolve track\n"); | |
241 | return 1; | |
242 | } | |
243 | q = queue_add(track, c->who, WHERE_BEFORE_RANDOM); | |
244 | queue_write(); | |
245 | /* If we added the first track, and something is playing, then prepare the | |
246 | * new track. If nothing is playing then we don't bother as it wouldn't gain | |
247 | * anything. */ | |
248 | if(q == qhead.next && playing) | |
249 | prepare(c->ev, q); | |
81e440ce | 250 | sink_printf(ev_writer_sink(c->w), "252 %s\n", q->id); |
460b9539 | 251 | /* If the queue was empty but we are for some reason paused then |
252 | * unpause. */ | |
253 | if(!playing) resume_playing(0); | |
254 | play(c->ev); | |
255 | return 1; /* completed */ | |
256 | } | |
257 | ||
258 | static int c_remove(struct conn *c, char **vec, | |
259 | int attribute((unused)) nvec) { | |
260 | struct queue_entry *q; | |
261 | ||
262 | if(!(q = queue_find(vec[0]))) { | |
263 | sink_writes(ev_writer_sink(c->w), "550 no such track on the queue\n"); | |
264 | return 1; | |
265 | } | |
938d8157 | 266 | if(!right_removable(c->rights, c->who, q)) { |
834e7c4a | 267 | error(0, "%s attempted remove but lacks required rights", c->who); |
eb5dc014 | 268 | sink_writes(ev_writer_sink(c->w), |
b4a80f69 | 269 | "510 Not authorized to remove that track\n"); |
eb5dc014 | 270 | return 1; |
460b9539 | 271 | } |
272 | queue_remove(q, c->who); | |
273 | /* De-prepare the track. */ | |
274 | abandon(c->ev, q); | |
eb5dc014 | 275 | /* If we removed a random track then add another one. */ |
460b9539 | 276 | if(q->state == playing_random) |
277 | add_random_track(); | |
278 | /* Prepare whatever the next head track is. */ | |
279 | if(qhead.next != &qhead) | |
280 | prepare(c->ev, qhead.next); | |
281 | queue_write(); | |
282 | sink_writes(ev_writer_sink(c->w), "250 removed\n"); | |
283 | return 1; /* completed */ | |
284 | } | |
285 | ||
286 | static int c_scratch(struct conn *c, | |
287 | char **vec, | |
288 | int nvec) { | |
289 | if(!playing) { | |
290 | sink_writes(ev_writer_sink(c->w), "250 nothing is playing\n"); | |
291 | return 1; /* completed */ | |
292 | } | |
eb5dc014 RK |
293 | /* TODO there is a bug here: if we specify an ID but it's not the currently |
294 | * playing track then you will get 550 if you weren't authorized to scratch | |
295 | * the currently playing track. */ | |
938d8157 | 296 | if(!right_scratchable(c->rights, c->who, playing)) { |
834e7c4a | 297 | error(0, "%s attempted scratch but lacks required rights", c->who); |
eb5dc014 | 298 | sink_writes(ev_writer_sink(c->w), |
b4a80f69 | 299 | "510 Not authorized to scratch that track\n"); |
eb5dc014 | 300 | return 1; |
460b9539 | 301 | } |
302 | scratch(c->who, nvec == 1 ? vec[0] : 0); | |
303 | /* If you scratch an unpaused track then it is automatically unpaused */ | |
304 | resume_playing(0); | |
305 | sink_writes(ev_writer_sink(c->w), "250 scratched\n"); | |
306 | return 1; /* completed */ | |
307 | } | |
308 | ||
309 | static int c_pause(struct conn *c, | |
310 | char attribute((unused)) **vec, | |
311 | int attribute((unused)) nvec) { | |
312 | if(!playing) { | |
313 | sink_writes(ev_writer_sink(c->w), "250 nothing is playing\n"); | |
314 | return 1; /* completed */ | |
315 | } | |
316 | if(paused) { | |
317 | sink_writes(ev_writer_sink(c->w), "250 already paused\n"); | |
318 | return 1; /* completed */ | |
319 | } | |
320 | if(pause_playing(c->who) < 0) | |
321 | sink_writes(ev_writer_sink(c->w), "550 cannot pause this track\n"); | |
322 | else | |
323 | sink_writes(ev_writer_sink(c->w), "250 paused\n"); | |
324 | return 1; | |
325 | } | |
326 | ||
327 | static int c_resume(struct conn *c, | |
328 | char attribute((unused)) **vec, | |
329 | int attribute((unused)) nvec) { | |
330 | if(!paused) { | |
331 | sink_writes(ev_writer_sink(c->w), "250 not paused\n"); | |
332 | return 1; /* completed */ | |
333 | } | |
334 | resume_playing(c->who); | |
335 | sink_writes(ev_writer_sink(c->w), "250 paused\n"); | |
336 | return 1; | |
337 | } | |
338 | ||
339 | static int c_shutdown(struct conn *c, | |
340 | char attribute((unused)) **vec, | |
341 | int attribute((unused)) nvec) { | |
342 | info("S%x shut down by %s", c->tag, c->who); | |
343 | sink_writes(ev_writer_sink(c->w), "250 shutting down\n"); | |
344 | ev_writer_flush(c->w); | |
345 | quit(c->ev); | |
346 | } | |
347 | ||
348 | static int c_reconfigure(struct conn *c, | |
349 | char attribute((unused)) **vec, | |
350 | int attribute((unused)) nvec) { | |
351 | info("S%x reconfigure by %s", c->tag, c->who); | |
352 | if(reconfigure(c->ev, 1)) | |
353 | sink_writes(ev_writer_sink(c->w), "550 error reading new config\n"); | |
354 | else | |
355 | sink_writes(ev_writer_sink(c->w), "250 installed new config\n"); | |
356 | return 1; /* completed */ | |
357 | } | |
358 | ||
359 | static int c_rescan(struct conn *c, | |
360 | char attribute((unused)) **vec, | |
361 | int attribute((unused)) nvec) { | |
362 | info("S%x rescan by %s", c->tag, c->who); | |
ffac51d7 | 363 | trackdb_rescan(c->ev, 1/*check*/); |
460b9539 | 364 | sink_writes(ev_writer_sink(c->w), "250 initiated rescan\n"); |
365 | return 1; /* completed */ | |
366 | } | |
367 | ||
368 | static int c_version(struct conn *c, | |
369 | char attribute((unused)) **vec, | |
370 | int attribute((unused)) nvec) { | |
371 | /* VERSION had better only use the basic character set */ | |
a05e4467 | 372 | sink_printf(ev_writer_sink(c->w), "251 %s\n", disorder_short_version_string); |
460b9539 | 373 | return 1; /* completed */ |
374 | } | |
375 | ||
376 | static int c_playing(struct conn *c, | |
377 | char attribute((unused)) **vec, | |
378 | int attribute((unused)) nvec) { | |
379 | if(playing) { | |
380 | queue_fix_sofar(playing); | |
381 | playing->expected = 0; | |
382 | sink_printf(ev_writer_sink(c->w), "252 %s\n", queue_marshall(playing)); | |
383 | } else | |
384 | sink_printf(ev_writer_sink(c->w), "259 nothing playing\n"); | |
385 | return 1; /* completed */ | |
386 | } | |
387 | ||
b12be54a | 388 | static const char *connection_host(struct conn *c) { |
460b9539 | 389 | union { |
390 | struct sockaddr sa; | |
391 | struct sockaddr_in in; | |
392 | struct sockaddr_in6 in6; | |
393 | } u; | |
394 | socklen_t l; | |
b12be54a | 395 | int n; |
460b9539 | 396 | char host[1024]; |
397 | ||
460b9539 | 398 | /* get connection data */ |
399 | l = sizeof u; | |
400 | if(getpeername(c->fd, &u.sa, &l) < 0) { | |
401 | error(errno, "S%x error calling getpeername", c->tag); | |
b12be54a | 402 | return 0; |
460b9539 | 403 | } |
404 | if(c->l->pf != PF_UNIX) { | |
405 | if((n = getnameinfo(&u.sa, l, | |
406 | host, sizeof host, 0, 0, NI_NUMERICHOST))) { | |
407 | error(0, "S%x error calling getnameinfo: %s", c->tag, gai_strerror(n)); | |
b12be54a | 408 | return 0; |
460b9539 | 409 | } |
b12be54a | 410 | return xstrdup(host); |
18e6d6e6 | 411 | } else |
b12be54a RK |
412 | return "local"; |
413 | } | |
414 | ||
415 | static int c_user(struct conn *c, | |
416 | char **vec, | |
417 | int attribute((unused)) nvec) { | |
eb5dc014 | 418 | struct kvp *k; |
f0feb22e | 419 | const char *res, *host, *password; |
eb5dc014 | 420 | rights_type rights; |
b12be54a RK |
421 | |
422 | if(c->who) { | |
423 | sink_writes(ev_writer_sink(c->w), "530 already authenticated\n"); | |
424 | return 1; | |
425 | } | |
426 | /* get connection data */ | |
427 | if(!(host = connection_host(c))) { | |
428 | sink_writes(ev_writer_sink(c->w), "530 authentication failure\n"); | |
429 | return 1; | |
430 | } | |
460b9539 | 431 | /* find the user */ |
eb5dc014 | 432 | k = trackdb_getuserinfo(vec[0]); |
f0feb22e | 433 | /* reject nonexistent users */ |
eb5dc014 RK |
434 | if(!k) { |
435 | error(0, "S%x unknown user '%s' from %s", c->tag, vec[0], host); | |
436 | sink_writes(ev_writer_sink(c->w), "530 authentication failed\n"); | |
437 | return 1; | |
438 | } | |
439 | /* reject unconfirmed users */ | |
440 | if(kvp_get(k, "confirmation")) { | |
441 | error(0, "S%x unconfirmed user '%s' from %s", c->tag, vec[0], host); | |
442 | sink_writes(ev_writer_sink(c->w), "530 authentication failed\n"); | |
443 | return 1; | |
444 | } | |
445 | password = kvp_get(k, "password"); | |
446 | if(!password) password = ""; | |
0f55e905 | 447 | if(parse_rights(kvp_get(k, "rights"), &rights, 1)) { |
eb5dc014 | 448 | error(0, "error parsing rights for %s", vec[0]); |
18e6d6e6 | 449 | sink_writes(ev_writer_sink(c->w), "530 authentication failed\n"); |
450 | return 1; | |
451 | } | |
f0feb22e RK |
452 | /* check whether the response is right */ |
453 | res = authhash(c->nonce, sizeof c->nonce, password, | |
637fdea3 | 454 | config->authorization_algorithm); |
18e6d6e6 | 455 | if(wideopen || (res && !strcmp(res, vec[1]))) { |
456 | c->who = vec[0]; | |
eb5dc014 | 457 | c->rights = rights; |
18e6d6e6 | 458 | /* currently we only bother logging remote connections */ |
30365519 | 459 | if(strcmp(host, "local")) |
18e6d6e6 | 460 | info("S%x %s connected from %s", c->tag, vec[0], host); |
30365519 | 461 | else |
eb5dc014 | 462 | c->rights |= RIGHT__LOCAL; |
18e6d6e6 | 463 | sink_writes(ev_writer_sink(c->w), "230 OK\n"); |
464 | return 1; | |
460b9539 | 465 | } |
466 | /* oops, response was wrong */ | |
18e6d6e6 | 467 | info("S%x authentication failure for %s from %s", c->tag, vec[0], host); |
460b9539 | 468 | sink_writes(ev_writer_sink(c->w), "530 authentication failed\n"); |
469 | return 1; | |
470 | } | |
471 | ||
472 | static int c_recent(struct conn *c, | |
473 | char attribute((unused)) **vec, | |
474 | int attribute((unused)) nvec) { | |
475 | const struct queue_entry *q; | |
476 | ||
477 | sink_writes(ev_writer_sink(c->w), "253 Tracks follow\n"); | |
478 | for(q = phead.next; q != &phead; q = q->next) | |
479 | sink_printf(ev_writer_sink(c->w), " %s\n", queue_marshall(q)); | |
480 | sink_writes(ev_writer_sink(c->w), ".\n"); | |
481 | return 1; /* completed */ | |
482 | } | |
483 | ||
484 | static int c_queue(struct conn *c, | |
485 | char attribute((unused)) **vec, | |
486 | int attribute((unused)) nvec) { | |
487 | struct queue_entry *q; | |
488 | time_t when = 0; | |
489 | const char *l; | |
490 | long length; | |
491 | ||
492 | sink_writes(ev_writer_sink(c->w), "253 Tracks follow\n"); | |
493 | if(playing_is_enabled() && !paused) { | |
494 | if(playing) { | |
495 | queue_fix_sofar(playing); | |
496 | if((l = trackdb_get(playing->track, "_length")) | |
497 | && (length = atol(l))) { | |
498 | time(&when); | |
499 | when += length - playing->sofar + config->gap; | |
500 | } | |
501 | } else | |
502 | /* Nothing is playing but playing is enabled, so whatever is | |
503 | * first in the queue can be expected to start immediately. */ | |
504 | time(&when); | |
505 | } | |
506 | for(q = qhead.next; q != &qhead; q = q->next) { | |
507 | /* fill in estimated start time */ | |
508 | q->expected = when; | |
509 | sink_printf(ev_writer_sink(c->w), " %s\n", queue_marshall(q)); | |
510 | /* update for next track */ | |
511 | if(when) { | |
512 | if((l = trackdb_get(q->track, "_length")) | |
513 | && (length = atol(l))) | |
514 | when += length + config->gap; | |
515 | else | |
516 | when = 0; | |
517 | } | |
518 | } | |
519 | sink_writes(ev_writer_sink(c->w), ".\n"); | |
520 | return 1; /* completed */ | |
521 | } | |
522 | ||
523 | static int output_list(struct conn *c, char **vec) { | |
524 | while(*vec) | |
525 | sink_printf(ev_writer_sink(c->w), "%s\n", *vec++); | |
526 | sink_writes(ev_writer_sink(c->w), ".\n"); | |
527 | return 1; | |
528 | } | |
529 | ||
530 | static int files_dirs(struct conn *c, | |
531 | char **vec, | |
532 | int nvec, | |
533 | enum trackdb_listable what) { | |
534 | const char *dir, *re, *errstr; | |
535 | int erroffset; | |
536 | pcre *rec; | |
537 | char **fvec, *key; | |
538 | ||
539 | switch(nvec) { | |
540 | case 0: dir = 0; re = 0; break; | |
541 | case 1: dir = vec[0]; re = 0; break; | |
542 | case 2: dir = vec[0]; re = vec[1]; break; | |
543 | default: abort(); | |
544 | } | |
545 | /* A bit of a bodge to make sure the args don't trample on cache keys */ | |
546 | if(dir && strchr(dir, '\n')) { | |
547 | sink_writes(ev_writer_sink(c->w), "550 invalid directory name\n"); | |
548 | return 1; | |
549 | } | |
550 | if(re && strchr(re, '\n')) { | |
551 | sink_writes(ev_writer_sink(c->w), "550 invalid regexp\n"); | |
552 | return 1; | |
553 | } | |
554 | /* We bother eliminating "" because the web interface is relatively | |
555 | * likely to send it */ | |
556 | if(re && *re) { | |
557 | byte_xasprintf(&key, "%d\n%s\n%s", (int)what, dir ? dir : "", re); | |
558 | fvec = (char **)cache_get(&cache_files_type, key); | |
559 | if(fvec) { | |
560 | /* Got a cache hit, don't store the answer in the cache */ | |
561 | key = 0; | |
562 | ++cache_files_hits; | |
563 | rec = 0; /* quieten compiler */ | |
564 | } else { | |
565 | /* Cache miss, we'll do the lookup and key != 0 so we'll store the answer | |
566 | * in the cache. */ | |
567 | if(!(rec = pcre_compile(re, PCRE_CASELESS|PCRE_UTF8, | |
568 | &errstr, &erroffset, 0))) { | |
569 | sink_printf(ev_writer_sink(c->w), "550 Error compiling regexp: %s\n", | |
570 | errstr); | |
571 | return 1; | |
572 | } | |
573 | /* It only counts as a miss if the regexp was valid. */ | |
574 | ++cache_files_misses; | |
575 | } | |
576 | } else { | |
577 | /* No regexp, don't bother caching the result */ | |
578 | rec = 0; | |
579 | key = 0; | |
580 | fvec = 0; | |
581 | } | |
582 | if(!fvec) { | |
583 | /* No cache hit (either because a miss, or because we did not look) so do | |
584 | * the lookup */ | |
585 | if(dir && *dir) | |
586 | fvec = trackdb_list(dir, 0, what, rec); | |
587 | else | |
588 | fvec = trackdb_list(0, 0, what, rec); | |
589 | } | |
590 | if(key) | |
591 | /* Put the answer in the cache */ | |
592 | cache_put(&cache_files_type, key, fvec); | |
593 | sink_writes(ev_writer_sink(c->w), "253 Listing follow\n"); | |
594 | return output_list(c, fvec); | |
595 | } | |
596 | ||
597 | static int c_files(struct conn *c, | |
598 | char **vec, | |
599 | int nvec) { | |
600 | return files_dirs(c, vec, nvec, trackdb_files); | |
601 | } | |
602 | ||
603 | static int c_dirs(struct conn *c, | |
604 | char **vec, | |
605 | int nvec) { | |
606 | return files_dirs(c, vec, nvec, trackdb_directories); | |
607 | } | |
608 | ||
609 | static int c_allfiles(struct conn *c, | |
610 | char **vec, | |
611 | int nvec) { | |
612 | return files_dirs(c, vec, nvec, trackdb_directories|trackdb_files); | |
613 | } | |
614 | ||
615 | static int c_get(struct conn *c, | |
616 | char **vec, | |
617 | int attribute((unused)) nvec) { | |
618 | const char *v; | |
619 | ||
620 | if(vec[1][0] != '_' && (v = trackdb_get(vec[0], vec[1]))) | |
7b32e917 | 621 | sink_printf(ev_writer_sink(c->w), "252 %s\n", quoteutf8(v)); |
460b9539 | 622 | else |
fb1bc1f5 | 623 | sink_writes(ev_writer_sink(c->w), "555 not found\n"); |
460b9539 | 624 | return 1; |
625 | } | |
626 | ||
627 | static int c_length(struct conn *c, | |
628 | char **vec, | |
629 | int attribute((unused)) nvec) { | |
630 | const char *track, *v; | |
631 | ||
632 | if(!(track = trackdb_resolve(vec[0]))) { | |
633 | sink_writes(ev_writer_sink(c->w), "550 cannot resolve track\n"); | |
634 | return 1; | |
635 | } | |
636 | if((v = trackdb_get(track, "_length"))) | |
7b32e917 | 637 | sink_printf(ev_writer_sink(c->w), "252 %s\n", quoteutf8(v)); |
460b9539 | 638 | else |
639 | sink_writes(ev_writer_sink(c->w), "550 not found\n"); | |
640 | return 1; | |
641 | } | |
642 | ||
643 | static int c_set(struct conn *c, | |
644 | char **vec, | |
645 | int attribute((unused)) nvec) { | |
646 | if(vec[1][0] != '_' && !trackdb_set(vec[0], vec[1], vec[2])) | |
647 | sink_writes(ev_writer_sink(c->w), "250 OK\n"); | |
648 | else | |
649 | sink_writes(ev_writer_sink(c->w), "550 not found\n"); | |
650 | return 1; | |
651 | } | |
652 | ||
653 | static int c_prefs(struct conn *c, | |
654 | char **vec, | |
655 | int attribute((unused)) nvec) { | |
656 | struct kvp *k; | |
657 | ||
658 | k = trackdb_get_all(vec[0]); | |
659 | sink_writes(ev_writer_sink(c->w), "253 prefs follow\n"); | |
660 | for(; k; k = k->next) | |
661 | if(k->name[0] != '_') /* omit internal values */ | |
662 | sink_printf(ev_writer_sink(c->w), | |
663 | " %s %s\n", quoteutf8(k->name), quoteutf8(k->value)); | |
664 | sink_writes(ev_writer_sink(c->w), ".\n"); | |
665 | return 1; | |
666 | } | |
667 | ||
668 | static int c_exists(struct conn *c, | |
669 | char **vec, | |
670 | int attribute((unused)) nvec) { | |
671 | sink_printf(ev_writer_sink(c->w), "252 %s\n", noyes[trackdb_exists(vec[0])]); | |
672 | return 1; | |
673 | } | |
674 | ||
675 | static void search_parse_error(const char *msg, void *u) { | |
676 | *(const char **)u = msg; | |
677 | } | |
678 | ||
679 | static int c_search(struct conn *c, | |
680 | char **vec, | |
681 | int attribute((unused)) nvec) { | |
682 | char **terms, **results; | |
683 | int nterms, nresults, n; | |
684 | const char *e = "unknown error"; | |
685 | ||
686 | /* This is a bit of a bodge. Initially it's there to make the eclient | |
687 | * interface a bit more convenient to add searching to, but it has the more | |
688 | * compelling advantage that if everything uses it, then interpretation of | |
689 | * user-supplied search strings will be the same everywhere. */ | |
690 | if(!(terms = split(vec[0], &nterms, SPLIT_QUOTES, search_parse_error, &e))) { | |
691 | sink_printf(ev_writer_sink(c->w), "550 %s\n", e); | |
692 | } else { | |
693 | results = trackdb_search(terms, nterms, &nresults); | |
694 | sink_printf(ev_writer_sink(c->w), "253 %d matches\n", nresults); | |
695 | for(n = 0; n < nresults; ++n) | |
696 | sink_printf(ev_writer_sink(c->w), "%s\n", results[n]); | |
697 | sink_writes(ev_writer_sink(c->w), ".\n"); | |
698 | } | |
699 | return 1; | |
700 | } | |
701 | ||
702 | static int c_random_enable(struct conn *c, | |
703 | char attribute((unused)) **vec, | |
704 | int attribute((unused)) nvec) { | |
705 | enable_random(c->who, c->ev); | |
706 | /* Enable implicitly unpauses if there is nothing playing */ | |
707 | if(paused && !playing) resume_playing(c->who); | |
708 | sink_writes(ev_writer_sink(c->w), "250 OK\n"); | |
709 | return 1; /* completed */ | |
710 | } | |
711 | ||
712 | static int c_random_disable(struct conn *c, | |
713 | char attribute((unused)) **vec, | |
714 | int attribute((unused)) nvec) { | |
715 | disable_random(c->who); | |
716 | sink_writes(ev_writer_sink(c->w), "250 OK\n"); | |
717 | return 1; /* completed */ | |
718 | } | |
719 | ||
720 | static int c_random_enabled(struct conn *c, | |
721 | char attribute((unused)) **vec, | |
722 | int attribute((unused)) nvec) { | |
723 | sink_printf(ev_writer_sink(c->w), "252 %s\n", noyes[random_is_enabled()]); | |
724 | return 1; /* completed */ | |
725 | } | |
726 | ||
d6dde5a3 RK |
727 | static void got_stats(char *stats, void *u) { |
728 | struct conn *const c = u; | |
729 | ||
730 | sink_printf(ev_writer_sink(c->w), "253 stats\n%s\n.\n", stats); | |
731 | /* Now we can start processing commands again */ | |
732 | ev_reader_enable(c->r); | |
733 | } | |
734 | ||
460b9539 | 735 | static int c_stats(struct conn *c, |
736 | char attribute((unused)) **vec, | |
737 | int attribute((unused)) nvec) { | |
d6dde5a3 RK |
738 | trackdb_stats_subprocess(c->ev, got_stats, c); |
739 | return 0; /* not yet complete */ | |
460b9539 | 740 | } |
741 | ||
742 | static int c_volume(struct conn *c, | |
743 | char **vec, | |
744 | int nvec) { | |
745 | int l, r, set; | |
746 | char lb[32], rb[32]; | |
eb5dc014 | 747 | rights_type rights; |
460b9539 | 748 | |
749 | switch(nvec) { | |
750 | case 0: | |
751 | set = 0; | |
752 | break; | |
753 | case 1: | |
754 | l = r = atoi(vec[0]); | |
755 | set = 1; | |
756 | break; | |
757 | case 2: | |
758 | l = atoi(vec[0]); | |
759 | r = atoi(vec[1]); | |
760 | set = 1; | |
761 | break; | |
762 | default: | |
763 | abort(); | |
764 | } | |
eb5dc014 RK |
765 | rights = set ? RIGHT_VOLUME : RIGHT_READ; |
766 | if(!(c->rights & rights)) { | |
834e7c4a | 767 | error(0, "%s attempted to set volume but lacks required rights", c->who); |
b4a80f69 | 768 | sink_writes(ev_writer_sink(c->w), "510 Prohibited\n"); |
eb5dc014 RK |
769 | return 1; |
770 | } | |
460b9539 | 771 | if(mixer_control(&l, &r, set)) |
772 | sink_writes(ev_writer_sink(c->w), "550 error accessing mixer\n"); | |
773 | else { | |
774 | sink_printf(ev_writer_sink(c->w), "252 %d %d\n", l, r); | |
775 | if(l != volume_left || r != volume_right) { | |
776 | volume_left = l; | |
777 | volume_right = r; | |
778 | snprintf(lb, sizeof lb, "%d", l); | |
779 | snprintf(rb, sizeof rb, "%d", r); | |
780 | eventlog("volume", lb, rb, (char *)0); | |
781 | } | |
782 | } | |
783 | return 1; | |
784 | } | |
785 | ||
397ef7bb RK |
786 | /** @brief Called when data arrives on a log connection |
787 | * | |
788 | * We just discard all such data. The client may occasionally send data as a | |
789 | * keepalive. | |
790 | */ | |
791 | static int logging_reader_callback(ev_source attribute((unused)) *ev, | |
460b9539 | 792 | ev_reader *reader, |
397ef7bb | 793 | void attribute((unused)) *ptr, |
460b9539 | 794 | size_t bytes, |
397ef7bb RK |
795 | int attribute((unused)) eof, |
796 | void attribute((unused)) *u) { | |
460b9539 | 797 | struct conn *c = u; |
798 | ||
397ef7bb RK |
799 | ev_reader_consume(reader, bytes); |
800 | if(eof) { | |
801 | /* Oops, that's all for now */ | |
8d8b8c1f | 802 | D(("logging reader eof")); |
397ef7bb | 803 | if(c->w) { |
8d8b8c1f | 804 | D(("close writer")); |
397ef7bb RK |
805 | ev_writer_close(c->w); |
806 | c->w = 0; | |
807 | } | |
808 | c->r = 0; | |
0126692c | 809 | remove_connection(c); |
f6033c46 | 810 | } |
397ef7bb | 811 | return 0; |
460b9539 | 812 | } |
813 | ||
814 | static void logclient(const char *msg, void *user) { | |
815 | struct conn *c = user; | |
816 | ||
f6033c46 RK |
817 | if(!c->w || !c->r) { |
818 | /* This connection has gone up in smoke for some reason */ | |
819 | eventlog_remove(c->lo); | |
820 | return; | |
821 | } | |
460b9539 | 822 | sink_printf(ev_writer_sink(c->w), "%"PRIxMAX" %s\n", |
823 | (uintmax_t)time(0), msg); | |
824 | } | |
825 | ||
826 | static int c_log(struct conn *c, | |
827 | char attribute((unused)) **vec, | |
828 | int attribute((unused)) nvec) { | |
829 | time_t now; | |
830 | ||
831 | sink_writes(ev_writer_sink(c->w), "254 OK\n"); | |
832 | /* pump out initial state */ | |
833 | time(&now); | |
834 | sink_printf(ev_writer_sink(c->w), "%"PRIxMAX" state %s\n", | |
835 | (uintmax_t)now, | |
836 | playing_is_enabled() ? "enable_play" : "disable_play"); | |
837 | sink_printf(ev_writer_sink(c->w), "%"PRIxMAX" state %s\n", | |
838 | (uintmax_t)now, | |
839 | random_is_enabled() ? "enable_random" : "disable_random"); | |
840 | sink_printf(ev_writer_sink(c->w), "%"PRIxMAX" state %s\n", | |
841 | (uintmax_t)now, | |
842 | paused ? "pause" : "resume"); | |
5abe307a RK |
843 | if(playing) |
844 | sink_printf(ev_writer_sink(c->w), "%"PRIxMAX" state playing\n", | |
845 | (uintmax_t)now); | |
df44d69b RK |
846 | /* Initial volume */ |
847 | sink_printf(ev_writer_sink(c->w), "%"PRIxMAX" volume %d %d\n", | |
848 | (uintmax_t)now, volume_left, volume_right); | |
460b9539 | 849 | c->lo = xmalloc(sizeof *c->lo); |
850 | c->lo->fn = logclient; | |
851 | c->lo->user = c; | |
852 | eventlog_add(c->lo); | |
853 | c->reader = logging_reader_callback; | |
854 | return 0; | |
855 | } | |
856 | ||
eb5dc014 RK |
857 | /** @brief Test whether a move is allowed |
858 | * @param c Connection | |
859 | * @param qs List of IDs on queue | |
860 | * @param nqs Number of IDs | |
861 | * @return 0 if move is prohibited, non-0 if it is allowed | |
862 | */ | |
863 | static int has_move_rights(struct conn *c, struct queue_entry **qs, int nqs) { | |
eb5dc014 RK |
864 | for(; nqs > 0; ++qs, --nqs) { |
865 | struct queue_entry *const q = *qs; | |
866 | ||
938d8157 | 867 | if(!right_movable(c->rights, c->who, q)) |
868 | return 0; | |
eb5dc014 | 869 | } |
938d8157 | 870 | return 1; |
eb5dc014 RK |
871 | } |
872 | ||
460b9539 | 873 | static int c_move(struct conn *c, |
874 | char **vec, | |
875 | int attribute((unused)) nvec) { | |
876 | struct queue_entry *q; | |
877 | int n; | |
878 | ||
460b9539 | 879 | if(!(q = queue_find(vec[0]))) { |
880 | sink_writes(ev_writer_sink(c->w), "550 no such track on the queue\n"); | |
881 | return 1; | |
882 | } | |
eb5dc014 | 883 | if(!has_move_rights(c, &q, 1)) { |
834e7c4a | 884 | error(0, "%s attempted move but lacks required rights", c->who); |
eb5dc014 | 885 | sink_writes(ev_writer_sink(c->w), |
b4a80f69 | 886 | "510 Not authorized to move that track\n"); |
eb5dc014 RK |
887 | return 1; |
888 | } | |
460b9539 | 889 | n = queue_move(q, atoi(vec[1]), c->who); |
460b9539 | 890 | sink_printf(ev_writer_sink(c->w), "252 %d\n", n); |
891 | /* If we've moved to the head of the queue then prepare the track. */ | |
892 | if(q == qhead.next) | |
893 | prepare(c->ev, q); | |
894 | return 1; | |
895 | } | |
896 | ||
897 | static int c_moveafter(struct conn *c, | |
898 | char **vec, | |
899 | int attribute((unused)) nvec) { | |
900 | struct queue_entry *q, **qs; | |
901 | int n; | |
902 | ||
460b9539 | 903 | if(vec[0][0]) { |
904 | if(!(q = queue_find(vec[0]))) { | |
905 | sink_writes(ev_writer_sink(c->w), "550 no such track on the queue\n"); | |
906 | return 1; | |
907 | } | |
908 | } else | |
909 | q = 0; | |
910 | ++vec; | |
911 | --nvec; | |
912 | qs = xcalloc(nvec, sizeof *qs); | |
913 | for(n = 0; n < nvec; ++n) | |
914 | if(!(qs[n] = queue_find(vec[n]))) { | |
915 | sink_writes(ev_writer_sink(c->w), "550 no such track on the queue\n"); | |
916 | return 1; | |
917 | } | |
eb5dc014 | 918 | if(!has_move_rights(c, qs, nvec)) { |
834e7c4a | 919 | error(0, "%s attempted moveafter but lacks required rights", c->who); |
eb5dc014 | 920 | sink_writes(ev_writer_sink(c->w), |
b4a80f69 | 921 | "510 Not authorized to move those tracks\n"); |
eb5dc014 RK |
922 | return 1; |
923 | } | |
460b9539 | 924 | queue_moveafter(q, nvec, qs, c->who); |
460b9539 | 925 | sink_printf(ev_writer_sink(c->w), "250 Moved tracks\n"); |
926 | /* If we've moved to the head of the queue then prepare the track. */ | |
927 | if(q == qhead.next) | |
928 | prepare(c->ev, q); | |
929 | return 1; | |
930 | } | |
931 | ||
932 | static int c_part(struct conn *c, | |
933 | char **vec, | |
934 | int attribute((unused)) nvec) { | |
935 | sink_printf(ev_writer_sink(c->w), "252 %s\n", | |
7b32e917 | 936 | quoteutf8(trackdb_getpart(vec[0], vec[1], vec[2]))); |
460b9539 | 937 | return 1; |
938 | } | |
939 | ||
940 | static int c_resolve(struct conn *c, | |
941 | char **vec, | |
942 | int attribute((unused)) nvec) { | |
943 | const char *track; | |
944 | ||
945 | if(!(track = trackdb_resolve(vec[0]))) { | |
946 | sink_writes(ev_writer_sink(c->w), "550 cannot resolve track\n"); | |
947 | return 1; | |
948 | } | |
7b32e917 | 949 | sink_printf(ev_writer_sink(c->w), "252 %s\n", quoteutf8(track)); |
460b9539 | 950 | return 1; |
951 | } | |
952 | ||
953 | static int c_tags(struct conn *c, | |
954 | char attribute((unused)) **vec, | |
955 | int attribute((unused)) nvec) { | |
956 | char **tags = trackdb_alltags(); | |
957 | ||
958 | sink_printf(ev_writer_sink(c->w), "253 Tag list follows\n"); | |
959 | while(*tags) { | |
960 | sink_printf(ev_writer_sink(c->w), "%s%s\n", | |
961 | **tags == '.' ? "." : "", *tags); | |
962 | ++tags; | |
963 | } | |
964 | sink_writes(ev_writer_sink(c->w), ".\n"); | |
965 | return 1; /* completed */ | |
460b9539 | 966 | } |
967 | ||
968 | static int c_set_global(struct conn *c, | |
969 | char **vec, | |
970 | int attribute((unused)) nvec) { | |
f9635e06 RK |
971 | if(vec[0][0] == '_') { |
972 | sink_writes(ev_writer_sink(c->w), "550 cannot set internal global preferences\n"); | |
973 | return 1; | |
974 | } | |
460b9539 | 975 | trackdb_set_global(vec[0], vec[1], c->who); |
976 | sink_printf(ev_writer_sink(c->w), "250 OK\n"); | |
977 | return 1; | |
978 | } | |
979 | ||
980 | static int c_get_global(struct conn *c, | |
981 | char **vec, | |
982 | int attribute((unused)) nvec) { | |
983 | const char *s = trackdb_get_global(vec[0]); | |
984 | ||
985 | if(s) | |
7b32e917 | 986 | sink_printf(ev_writer_sink(c->w), "252 %s\n", quoteutf8(s)); |
460b9539 | 987 | else |
fb1bc1f5 | 988 | sink_writes(ev_writer_sink(c->w), "555 not found\n"); |
460b9539 | 989 | return 1; |
990 | } | |
991 | ||
7858930d | 992 | static int c_nop(struct conn *c, |
993 | char attribute((unused)) **vec, | |
994 | int attribute((unused)) nvec) { | |
995 | sink_printf(ev_writer_sink(c->w), "250 Quack\n"); | |
996 | return 1; | |
997 | } | |
998 | ||
2a10b70b RK |
999 | static int c_new(struct conn *c, |
1000 | char **vec, | |
1001 | int nvec) { | |
d742bb47 RK |
1002 | int max, n; |
1003 | char **tracks; | |
2a10b70b | 1004 | |
d742bb47 RK |
1005 | if(nvec > 0) |
1006 | max = atoi(vec[0]); | |
1007 | else | |
1008 | max = INT_MAX; | |
1009 | if(max <= 0 || max > config->new_max) | |
1010 | max = config->new_max; | |
1011 | tracks = trackdb_new(0, max); | |
2a10b70b | 1012 | sink_printf(ev_writer_sink(c->w), "253 New track list follows\n"); |
d742bb47 | 1013 | n = 0; |
2a10b70b RK |
1014 | while(*tracks) { |
1015 | sink_printf(ev_writer_sink(c->w), "%s%s\n", | |
1016 | **tracks == '.' ? "." : "", *tracks); | |
1017 | ++tracks; | |
1018 | } | |
1019 | sink_writes(ev_writer_sink(c->w), ".\n"); | |
1020 | return 1; /* completed */ | |
1021 | ||
1022 | } | |
1023 | ||
ca831831 RK |
1024 | static int c_rtp_address(struct conn *c, |
1025 | char attribute((unused)) **vec, | |
1026 | int attribute((unused)) nvec) { | |
bd8895a8 | 1027 | if(config->api == BACKEND_NETWORK) { |
ca831831 RK |
1028 | sink_printf(ev_writer_sink(c->w), "252 %s %s\n", |
1029 | quoteutf8(config->broadcast.s[0]), | |
1030 | quoteutf8(config->broadcast.s[1])); | |
1031 | } else | |
1032 | sink_writes(ev_writer_sink(c->w), "550 No RTP\n"); | |
1033 | return 1; | |
1034 | } | |
b12be54a RK |
1035 | |
1036 | static int c_cookie(struct conn *c, | |
1037 | char **vec, | |
1038 | int attribute((unused)) nvec) { | |
1039 | const char *host; | |
1040 | char *user; | |
eb5dc014 | 1041 | rights_type rights; |
b12be54a RK |
1042 | |
1043 | /* Can't log in twice on the same connection */ | |
1044 | if(c->who) { | |
1045 | sink_writes(ev_writer_sink(c->w), "530 already authenticated\n"); | |
1046 | return 1; | |
1047 | } | |
1048 | /* Get some kind of peer identifcation */ | |
1049 | if(!(host = connection_host(c))) { | |
1050 | sink_writes(ev_writer_sink(c->w), "530 authentication failure\n"); | |
1051 | return 1; | |
1052 | } | |
1053 | /* Check the cookie */ | |
eb5dc014 | 1054 | user = verify_cookie(vec[0], &rights); |
b12be54a RK |
1055 | if(!user) { |
1056 | sink_writes(ev_writer_sink(c->w), "530 authentication failure\n"); | |
1057 | return 1; | |
1058 | } | |
1059 | /* Log in */ | |
e48c28bb | 1060 | c->who = user; |
b12be54a | 1061 | c->cookie = vec[0]; |
eb5dc014 | 1062 | c->rights = rights; |
30365519 | 1063 | if(strcmp(host, "local")) |
b12be54a | 1064 | info("S%x %s connected with cookie from %s", c->tag, user, host); |
30365519 | 1065 | else |
eb5dc014 | 1066 | c->rights |= RIGHT__LOCAL; |
0227f67d RK |
1067 | /* Response contains username so client knows who they are acting as */ |
1068 | sink_printf(ev_writer_sink(c->w), "232 %s\n", quoteutf8(user)); | |
b12be54a RK |
1069 | return 1; |
1070 | } | |
1071 | ||
1072 | static int c_make_cookie(struct conn *c, | |
1073 | char attribute((unused)) **vec, | |
1074 | int attribute((unused)) nvec) { | |
1075 | const char *cookie = make_cookie(c->who); | |
1076 | ||
1077 | if(cookie) | |
eb5dc014 | 1078 | sink_printf(ev_writer_sink(c->w), "252 %s\n", quoteutf8(cookie)); |
b12be54a RK |
1079 | else |
1080 | sink_writes(ev_writer_sink(c->w), "550 Cannot create cookie\n"); | |
1081 | return 1; | |
1082 | } | |
1083 | ||
1084 | static int c_revoke(struct conn *c, | |
1085 | char attribute((unused)) **vec, | |
1086 | int attribute((unused)) nvec) { | |
1087 | if(c->cookie) { | |
1088 | revoke_cookie(c->cookie); | |
1089 | sink_writes(ev_writer_sink(c->w), "250 OK\n"); | |
1090 | } else | |
1091 | sink_writes(ev_writer_sink(c->w), "550 Did not log in with cookie\n"); | |
1092 | return 1; | |
1093 | } | |
1094 | ||
f0feb22e RK |
1095 | static int c_adduser(struct conn *c, |
1096 | char **vec, | |
0f55e905 RK |
1097 | int nvec) { |
1098 | const char *rights; | |
1099 | ||
1100 | if(nvec > 2) { | |
1101 | rights = vec[2]; | |
1102 | if(parse_rights(vec[2], 0, 1)) { | |
1103 | sink_writes(ev_writer_sink(c->w), "550 Invalid rights list\n"); | |
1104 | return -1; | |
1105 | } | |
1106 | } else | |
1107 | rights = config->default_rights; | |
1108 | if(trackdb_adduser(vec[0], vec[1], rights, | |
ba39faf6 | 1109 | 0/*email*/, 0/*confirmation*/)) |
f0feb22e RK |
1110 | sink_writes(ev_writer_sink(c->w), "550 Cannot create user\n"); |
1111 | else | |
1112 | sink_writes(ev_writer_sink(c->w), "250 User created\n"); | |
1113 | return 1; | |
1114 | } | |
1115 | ||
1116 | static int c_deluser(struct conn *c, | |
1117 | char **vec, | |
1118 | int attribute((unused)) nvec) { | |
0126692c | 1119 | struct conn *d; |
1120 | ||
1121 | if(trackdb_deluser(vec[0])) { | |
ba39faf6 | 1122 | sink_writes(ev_writer_sink(c->w), "550 Cannot delete user\n"); |
0126692c | 1123 | return 1; |
1124 | } | |
1125 | /* Zap connections belonging to deleted user */ | |
1126 | for(d = connections; d; d = d->next) | |
1127 | if(!strcmp(d->who, vec[0])) | |
1128 | d->rights = 0; | |
1129 | sink_writes(ev_writer_sink(c->w), "250 User deleted\n"); | |
f0feb22e RK |
1130 | return 1; |
1131 | } | |
1132 | ||
1133 | static int c_edituser(struct conn *c, | |
5df73aeb | 1134 | char **vec, |
f0feb22e | 1135 | int attribute((unused)) nvec) { |
0126692c | 1136 | struct conn *d; |
1137 | ||
eb5dc014 RK |
1138 | /* RIGHT_ADMIN can do anything; otherwise you can only set your own email |
1139 | * address and password. */ | |
1140 | if((c->rights & RIGHT_ADMIN) | |
5df73aeb RK |
1141 | || (!strcmp(c->who, vec[0]) |
1142 | && (!strcmp(vec[1], "email") | |
1143 | || !strcmp(vec[1], "password")))) { | |
0126692c | 1144 | if(trackdb_edituserinfo(vec[0], vec[1], vec[2])) { |
5df73aeb | 1145 | sink_writes(ev_writer_sink(c->w), "550 Failed to change setting\n"); |
0126692c | 1146 | return 1; |
1147 | } | |
1148 | if(!strcmp(vec[1], "password")) { | |
1149 | /* Zap all connections for this user after a password change */ | |
1150 | for(d = connections; d; d = d->next) | |
1151 | if(!strcmp(d->who, vec[0])) | |
1152 | d->rights = 0; | |
1153 | } else if(!strcmp(vec[1], "rights")) { | |
1154 | /* Update rights for this user */ | |
1155 | rights_type r; | |
1156 | ||
d3369bca | 1157 | if(parse_rights(vec[2], &r, 1)) |
0126692c | 1158 | for(d = connections; d; d = d->next) |
1159 | if(!strcmp(d->who, vec[0])) | |
1160 | d->rights = r; | |
1161 | } | |
1162 | sink_writes(ev_writer_sink(c->w), "250 OK\n"); | |
834e7c4a | 1163 | } else { |
1164 | error(0, "%s attempted edituser but lacks required rights", c->who); | |
b4a80f69 | 1165 | sink_writes(ev_writer_sink(c->w), "510 Restricted to administrators\n"); |
834e7c4a | 1166 | } |
f0feb22e RK |
1167 | return 1; |
1168 | } | |
1169 | ||
1170 | static int c_userinfo(struct conn *c, | |
1171 | char attribute((unused)) **vec, | |
1172 | int attribute((unused)) nvec) { | |
5df73aeb RK |
1173 | struct kvp *k; |
1174 | const char *value; | |
eb5dc014 RK |
1175 | |
1176 | /* RIGHT_ADMIN allows anything; otherwise you can only get your own email | |
1177 | * address and righst list. */ | |
1178 | if((c->rights & RIGHT_ADMIN) | |
5df73aeb RK |
1179 | || (!strcmp(c->who, vec[0]) |
1180 | && (!strcmp(vec[1], "email") | |
1181 | || !strcmp(vec[1], "rights")))) { | |
1182 | if((k = trackdb_getuserinfo(vec[0]))) | |
1183 | if((value = kvp_get(k, vec[1]))) | |
1184 | sink_printf(ev_writer_sink(c->w), "252 %s\n", quoteutf8(value)); | |
1185 | else | |
1186 | sink_writes(ev_writer_sink(c->w), "555 Not set\n"); | |
1187 | else | |
1188 | sink_writes(ev_writer_sink(c->w), "550 No such user\n"); | |
834e7c4a | 1189 | } else { |
1190 | error(0, "%s attempted userinfo but lacks required rights", c->who); | |
b4a80f69 | 1191 | sink_writes(ev_writer_sink(c->w), "510 Restricted to administrators\n"); |
834e7c4a | 1192 | } |
f0feb22e RK |
1193 | return 1; |
1194 | } | |
1195 | ||
c3be4f19 RK |
1196 | static int c_users(struct conn *c, |
1197 | char attribute((unused)) **vec, | |
1198 | int attribute((unused)) nvec) { | |
1199 | /* TODO de-dupe with c_tags */ | |
1200 | char **users = trackdb_listusers(); | |
1201 | ||
1202 | sink_writes(ev_writer_sink(c->w), "253 User list follows\n"); | |
1203 | while(*users) { | |
1204 | sink_printf(ev_writer_sink(c->w), "%s%s\n", | |
1205 | **users == '.' ? "." : "", *users); | |
1206 | ++users; | |
1207 | } | |
1208 | sink_writes(ev_writer_sink(c->w), ".\n"); | |
1209 | return 1; /* completed */ | |
1210 | } | |
1211 | ||
86e3aea7 | 1212 | /** @brief Base64 mapping table for confirmation strings |
1213 | * | |
1214 | * This is used with generic_to_base64() and generic_base64(). We cannot use | |
1215 | * the MIME table as that contains '+' and '=' which get quoted when | |
1216 | * URL-encoding. (The CGI still does the URL encoding but it is desirable to | |
1217 | * avoid it being necessary.) | |
1218 | */ | |
1219 | static const char confirm_base64_table[] = | |
1220 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789/.*"; | |
1221 | ||
ba39faf6 RK |
1222 | static int c_register(struct conn *c, |
1223 | char **vec, | |
1224 | int attribute((unused)) nvec) { | |
1225 | char *buf, *cs; | |
1226 | size_t bufsize; | |
1227 | int offset; | |
1228 | ||
1229 | /* The confirmation string is base64(username;nonce) */ | |
10114017 | 1230 | bufsize = strlen(vec[0]) + CONFIRM_SIZE + 2; |
ba39faf6 RK |
1231 | buf = xmalloc_noptr(bufsize); |
1232 | offset = byte_snprintf(buf, bufsize, "%s;", vec[0]); | |
10114017 | 1233 | gcry_randomize(buf + offset, CONFIRM_SIZE, GCRY_STRONG_RANDOM); |
86e3aea7 | 1234 | cs = generic_to_base64((uint8_t *)buf, offset + CONFIRM_SIZE, |
1235 | confirm_base64_table); | |
ba39faf6 RK |
1236 | if(trackdb_adduser(vec[0], vec[1], config->default_rights, vec[2], cs)) |
1237 | sink_writes(ev_writer_sink(c->w), "550 Cannot create user\n"); | |
1238 | else | |
1239 | sink_printf(ev_writer_sink(c->w), "252 %s\n", quoteutf8(cs)); | |
1240 | return 1; | |
1241 | } | |
1242 | ||
1243 | static int c_confirm(struct conn *c, | |
1244 | char **vec, | |
1245 | int attribute((unused)) nvec) { | |
1246 | size_t nuser; | |
1247 | char *user, *sep; | |
30365519 | 1248 | rights_type rights; |
1249 | const char *host; | |
ba39faf6 | 1250 | |
30365519 | 1251 | /* Get some kind of peer identifcation */ |
1252 | if(!(host = connection_host(c))) { | |
1253 | sink_writes(ev_writer_sink(c->w), "530 Authentication failure\n"); | |
1254 | return 1; | |
1255 | } | |
86e3aea7 | 1256 | if(!(user = generic_base64(vec[0], &nuser, confirm_base64_table)) |
ba39faf6 RK |
1257 | || !(sep = memchr(user, ';', nuser))) { |
1258 | sink_writes(ev_writer_sink(c->w), "550 Malformed confirmation string\n"); | |
1259 | return 1; | |
1260 | } | |
1261 | *sep = 0; | |
30365519 | 1262 | if(trackdb_confirm(user, vec[0], &rights)) |
ba39faf6 | 1263 | sink_writes(ev_writer_sink(c->w), "550 Incorrect confirmation string\n"); |
30365519 | 1264 | else { |
1265 | c->who = user; | |
1266 | c->cookie = 0; | |
1267 | c->rights = rights; | |
1268 | if(strcmp(host, "local")) | |
1269 | info("S%x %s confirmed from %s", c->tag, user, host); | |
1270 | else | |
1271 | c->rights |= RIGHT__LOCAL; | |
1272 | /* Response contains username so client knows who they are acting as */ | |
1273 | sink_printf(ev_writer_sink(c->w), "232 %s\n", quoteutf8(user)); | |
1274 | } | |
ba39faf6 RK |
1275 | return 1; |
1276 | } | |
6207d2f3 | 1277 | |
1278 | static int sent_reminder(ev_source attribute((unused)) *ev, | |
1279 | pid_t attribute((unused)) pid, | |
1280 | int status, | |
1281 | const struct rusage attribute((unused)) *rusage, | |
1282 | void *u) { | |
1283 | struct conn *const c = u; | |
1284 | ||
1285 | /* Tell the client what went down */ | |
1286 | if(!status) { | |
1287 | sink_writes(ev_writer_sink(c->w), "250 OK\n"); | |
1288 | } else { | |
1289 | error(0, "reminder subprocess %s", wstat(status)); | |
1290 | sink_writes(ev_writer_sink(c->w), "550 Cannot send a reminder email\n"); | |
1291 | } | |
1292 | /* Re-enable this connection */ | |
1293 | ev_reader_enable(c->r); | |
1294 | return 0; | |
1295 | } | |
1296 | ||
1297 | static int c_reminder(struct conn *c, | |
1298 | char **vec, | |
1299 | int attribute((unused)) nvec) { | |
1300 | struct kvp *k; | |
1301 | const char *password, *email, *text, *encoding, *charset, *content_type; | |
1302 | const time_t *last; | |
1303 | time_t now; | |
1304 | pid_t pid; | |
1305 | ||
1306 | static hash *last_reminder; | |
1307 | ||
1308 | if(!config->mail_sender) { | |
1309 | error(0, "cannot send password reminders because mail_sender not set"); | |
1310 | sink_writes(ev_writer_sink(c->w), "550 Cannot send a reminder email\n"); | |
1311 | return 1; | |
1312 | } | |
1313 | if(!(k = trackdb_getuserinfo(vec[0]))) { | |
1314 | error(0, "reminder for user '%s' who does not exist", vec[0]); | |
1315 | sink_writes(ev_writer_sink(c->w), "550 Cannot send a reminder email\n"); | |
1316 | return 1; | |
1317 | } | |
1318 | if(!(email = kvp_get(k, "email")) | |
1319 | || !strchr(email, '@')) { | |
1320 | error(0, "user '%s' has no valid email address", vec[0]); | |
1321 | sink_writes(ev_writer_sink(c->w), "550 Cannot send a reminder email\n"); | |
1322 | return 1; | |
1323 | } | |
1324 | if(!(password = kvp_get(k, "password")) | |
1325 | || !*password) { | |
1326 | error(0, "user '%s' has no password", vec[0]); | |
1327 | sink_writes(ev_writer_sink(c->w), "550 Cannot send a reminder email\n"); | |
1328 | return 1; | |
1329 | } | |
1330 | /* Rate-limit reminders. This hash is bounded in size by the number of | |
1331 | * users. If this is actually a problem for anyone then we can periodically | |
1332 | * clean it. */ | |
1333 | if(!last_reminder) | |
1334 | last_reminder = hash_new(sizeof (time_t)); | |
1335 | last = hash_find(last_reminder, vec[0]); | |
1336 | time(&now); | |
1337 | if(last && now < *last + config->reminder_interval) { | |
1338 | error(0, "sent a password reminder to '%s' too recently", vec[0]); | |
1339 | sink_writes(ev_writer_sink(c->w), "550 Cannot send a reminder email\n"); | |
1340 | return 1; | |
1341 | } | |
1342 | /* Send the reminder */ | |
1343 | /* TODO this should be templatized and to some extent merged with | |
1344 | * the code in act_register() */ | |
1345 | byte_xasprintf((char **)&text, | |
1346 | "Someone requested that you be sent a reminder of your DisOrder password.\n" | |
1347 | "Your password is:\n" | |
1348 | "\n" | |
1349 | " %s\n", password); | |
1350 | if(!(text = mime_encode_text(text, &charset, &encoding))) | |
1351 | fatal(0, "cannot encode email"); | |
1352 | byte_xasprintf((char **)&content_type, "text/plain;charset=%s", | |
1353 | quote822(charset, 0)); | |
1354 | pid = sendmail_subprocess("", config->mail_sender, email, | |
1355 | "DisOrder password reminder", | |
1356 | encoding, content_type, text); | |
1357 | if(pid < 0) { | |
1358 | sink_writes(ev_writer_sink(c->w), "550 Cannot send a reminder email\n"); | |
1359 | return 1; | |
1360 | } | |
1361 | hash_add(last_reminder, vec[0], &now, HASH_INSERT_OR_REPLACE); | |
1362 | info("sending a passsword reminder to user '%s'", vec[0]); | |
1363 | /* We can only continue when the subprocess finishes */ | |
1364 | ev_child(c->ev, pid, 0, sent_reminder, c); | |
1365 | return 0; | |
1366 | } | |
1367 | ||
460b9539 | 1368 | static const struct command { |
eb5dc014 | 1369 | /** @brief Command name */ |
460b9539 | 1370 | const char *name; |
eb5dc014 RK |
1371 | |
1372 | /** @brief Minimum number of arguments */ | |
1373 | int minargs; | |
1374 | ||
1375 | /** @brief Maximum number of arguments */ | |
1376 | int maxargs; | |
1377 | ||
1378 | /** @brief Function to process command */ | |
460b9539 | 1379 | int (*fn)(struct conn *, char **, int); |
eb5dc014 RK |
1380 | |
1381 | /** @brief Rights required to execute command | |
1382 | * | |
1383 | * 0 means that the command can be issued without logging in. If multiple | |
1384 | * bits are listed here any of those rights will do. | |
1385 | */ | |
1386 | rights_type rights; | |
460b9539 | 1387 | } commands[] = { |
0f55e905 | 1388 | { "adduser", 2, 3, c_adduser, RIGHT_ADMIN|RIGHT__LOCAL }, |
eb5dc014 | 1389 | { "allfiles", 0, 2, c_allfiles, RIGHT_READ }, |
ba39faf6 | 1390 | { "confirm", 1, 1, c_confirm, 0 }, |
b12be54a | 1391 | { "cookie", 1, 1, c_cookie, 0 }, |
eb5dc014 RK |
1392 | { "deluser", 1, 1, c_deluser, RIGHT_ADMIN|RIGHT__LOCAL }, |
1393 | { "dirs", 0, 2, c_dirs, RIGHT_READ }, | |
1394 | { "disable", 0, 1, c_disable, RIGHT_GLOBAL_PREFS }, | |
1395 | { "edituser", 3, 3, c_edituser, RIGHT_ADMIN|RIGHT_USERINFO }, | |
1396 | { "enable", 0, 0, c_enable, RIGHT_GLOBAL_PREFS }, | |
1397 | { "enabled", 0, 0, c_enabled, RIGHT_READ }, | |
1398 | { "exists", 1, 1, c_exists, RIGHT_READ }, | |
1399 | { "files", 0, 2, c_files, RIGHT_READ }, | |
1400 | { "get", 2, 2, c_get, RIGHT_READ }, | |
1401 | { "get-global", 1, 1, c_get_global, RIGHT_READ }, | |
1402 | { "length", 1, 1, c_length, RIGHT_READ }, | |
1403 | { "log", 0, 0, c_log, RIGHT_READ }, | |
1404 | { "make-cookie", 0, 0, c_make_cookie, RIGHT_READ }, | |
1405 | { "move", 2, 2, c_move, RIGHT_MOVE__MASK }, | |
1406 | { "moveafter", 1, INT_MAX, c_moveafter, RIGHT_MOVE__MASK }, | |
1407 | { "new", 0, 1, c_new, RIGHT_READ }, | |
1408 | { "nop", 0, 0, c_nop, 0 }, | |
1409 | { "part", 3, 3, c_part, RIGHT_READ }, | |
1410 | { "pause", 0, 0, c_pause, RIGHT_PAUSE }, | |
1411 | { "play", 1, 1, c_play, RIGHT_PLAY }, | |
1412 | { "playing", 0, 0, c_playing, RIGHT_READ }, | |
1413 | { "prefs", 1, 1, c_prefs, RIGHT_READ }, | |
1414 | { "queue", 0, 0, c_queue, RIGHT_READ }, | |
1415 | { "random-disable", 0, 0, c_random_disable, RIGHT_GLOBAL_PREFS }, | |
1416 | { "random-enable", 0, 0, c_random_enable, RIGHT_GLOBAL_PREFS }, | |
1417 | { "random-enabled", 0, 0, c_random_enabled, RIGHT_READ }, | |
1418 | { "recent", 0, 0, c_recent, RIGHT_READ }, | |
1419 | { "reconfigure", 0, 0, c_reconfigure, RIGHT_ADMIN }, | |
ba39faf6 | 1420 | { "register", 3, 3, c_register, RIGHT_REGISTER|RIGHT__LOCAL }, |
6207d2f3 | 1421 | { "reminder", 1, 1, c_reminder, RIGHT__LOCAL }, |
eb5dc014 RK |
1422 | { "remove", 1, 1, c_remove, RIGHT_REMOVE__MASK }, |
1423 | { "rescan", 0, 0, c_rescan, RIGHT_RESCAN }, | |
1424 | { "resolve", 1, 1, c_resolve, RIGHT_READ }, | |
1425 | { "resume", 0, 0, c_resume, RIGHT_PAUSE }, | |
1426 | { "revoke", 0, 0, c_revoke, RIGHT_READ }, | |
1427 | { "rtp-address", 0, 0, c_rtp_address, 0 }, | |
1428 | { "scratch", 0, 1, c_scratch, RIGHT_SCRATCH__MASK }, | |
1429 | { "search", 1, 1, c_search, RIGHT_READ }, | |
1430 | { "set", 3, 3, c_set, RIGHT_PREFS, }, | |
1431 | { "set-global", 2, 2, c_set_global, RIGHT_GLOBAL_PREFS }, | |
1432 | { "shutdown", 0, 0, c_shutdown, RIGHT_ADMIN }, | |
1433 | { "stats", 0, 0, c_stats, RIGHT_READ }, | |
1434 | { "tags", 0, 0, c_tags, RIGHT_READ }, | |
1435 | { "unset", 2, 2, c_set, RIGHT_PREFS }, | |
1436 | { "unset-global", 1, 1, c_set_global, RIGHT_GLOBAL_PREFS }, | |
460b9539 | 1437 | { "user", 2, 2, c_user, 0 }, |
eb5dc014 RK |
1438 | { "userinfo", 2, 2, c_userinfo, RIGHT_READ }, |
1439 | { "users", 0, 0, c_users, RIGHT_READ }, | |
1440 | { "version", 0, 0, c_version, RIGHT_READ }, | |
1441 | { "volume", 0, 2, c_volume, RIGHT_READ|RIGHT_VOLUME } | |
460b9539 | 1442 | }; |
1443 | ||
1444 | static void command_error(const char *msg, void *u) { | |
1445 | struct conn *c = u; | |
1446 | ||
1447 | sink_printf(ev_writer_sink(c->w), "500 parse error: %s\n", msg); | |
1448 | } | |
1449 | ||
1450 | /* process a command. Return 1 if complete, 0 if incomplete. */ | |
1451 | static int command(struct conn *c, char *line) { | |
1452 | char **vec; | |
1453 | int nvec, n; | |
1454 | ||
1455 | D(("server command %s", line)); | |
f9635e06 RK |
1456 | /* We force everything into NFC as early as possible */ |
1457 | if(!(line = utf8_compose_canon(line, strlen(line), 0))) { | |
1458 | sink_writes(ev_writer_sink(c->w), "500 cannot normalize command\n"); | |
1459 | return 1; | |
1460 | } | |
460b9539 | 1461 | if(!(vec = split(line, &nvec, SPLIT_QUOTES, command_error, c))) { |
1462 | sink_writes(ev_writer_sink(c->w), "500 cannot parse command\n"); | |
1463 | return 1; | |
1464 | } | |
1465 | if(nvec == 0) { | |
1466 | sink_writes(ev_writer_sink(c->w), "500 do what?\n"); | |
1467 | return 1; | |
1468 | } | |
1469 | if((n = TABLE_FIND(commands, struct command, name, vec[0])) < 0) | |
1470 | sink_writes(ev_writer_sink(c->w), "500 unknown command\n"); | |
1471 | else { | |
eb5dc014 RK |
1472 | if(commands[n].rights |
1473 | && !(c->rights & commands[n].rights)) { | |
834e7c4a | 1474 | error(0, "%s attempted %s but lacks required rights", c->who ? c->who : "NULL", |
1475 | commands[n].name); | |
b4a80f69 | 1476 | sink_writes(ev_writer_sink(c->w), "510 Prohibited\n"); |
460b9539 | 1477 | return 1; |
1478 | } | |
1479 | ++vec; | |
1480 | --nvec; | |
1481 | if(nvec < commands[n].minargs) { | |
1482 | sink_writes(ev_writer_sink(c->w), "500 missing argument(s)\n"); | |
1483 | return 1; | |
1484 | } | |
1485 | if(nvec > commands[n].maxargs) { | |
1486 | sink_writes(ev_writer_sink(c->w), "500 too many arguments\n"); | |
1487 | return 1; | |
1488 | } | |
1489 | return commands[n].fn(c, vec, nvec); | |
1490 | } | |
1491 | return 1; /* completed */ | |
1492 | } | |
1493 | ||
1494 | /* redirect to the right reader callback for our current state */ | |
1495 | static int redirect_reader_callback(ev_source *ev, | |
1496 | ev_reader *reader, | |
460b9539 | 1497 | void *ptr, |
1498 | size_t bytes, | |
1499 | int eof, | |
1500 | void *u) { | |
1501 | struct conn *c = u; | |
1502 | ||
75d64210 | 1503 | return c->reader(ev, reader, ptr, bytes, eof, u); |
460b9539 | 1504 | } |
1505 | ||
1506 | /* the main command reader */ | |
1507 | static int reader_callback(ev_source attribute((unused)) *ev, | |
1508 | ev_reader *reader, | |
460b9539 | 1509 | void *ptr, |
1510 | size_t bytes, | |
1511 | int eof, | |
1512 | void *u) { | |
1513 | struct conn *c = u; | |
1514 | char *eol; | |
1515 | int complete; | |
1516 | ||
1517 | D(("server reader_callback")); | |
1518 | while((eol = memchr(ptr, '\n', bytes))) { | |
1519 | *eol++ = 0; | |
1520 | ev_reader_consume(reader, eol - (char *)ptr); | |
1521 | complete = command(c, ptr); | |
1522 | bytes -= (eol - (char *)ptr); | |
1523 | ptr = eol; | |
1524 | if(!complete) { | |
1525 | /* the command had better have set a new reader callback */ | |
1526 | if(bytes || eof) | |
1527 | /* there are further bytes to read, or we are at eof; arrange for the | |
1528 | * command's reader callback to handle them */ | |
1529 | return ev_reader_incomplete(reader); | |
1530 | /* nothing's going on right now */ | |
1531 | return 0; | |
1532 | } | |
1533 | /* command completed, we can go around and handle the next one */ | |
1534 | } | |
1535 | if(eof) { | |
1536 | if(bytes) | |
1537 | error(0, "S%x unterminated line", c->tag); | |
8d8b8c1f | 1538 | D(("normal reader close")); |
397ef7bb RK |
1539 | c->r = 0; |
1540 | if(c->w) { | |
8d8b8c1f | 1541 | D(("close associated writer")); |
397ef7bb RK |
1542 | ev_writer_close(c->w); |
1543 | c->w = 0; | |
1544 | } | |
0126692c | 1545 | remove_connection(c); |
460b9539 | 1546 | } |
1547 | return 0; | |
1548 | } | |
1549 | ||
1550 | static int listen_callback(ev_source *ev, | |
1551 | int fd, | |
1552 | const struct sockaddr attribute((unused)) *remote, | |
1553 | socklen_t attribute((unused)) rlen, | |
1554 | void *u) { | |
1555 | const struct listener *l = u; | |
1556 | struct conn *c = xmalloc(sizeof *c); | |
1557 | static unsigned tags; | |
1558 | ||
1559 | D(("server listen_callback fd %d (%s)", fd, l->name)); | |
1560 | nonblock(fd); | |
1561 | cloexec(fd); | |
1562 | c->tag = tags++; | |
1563 | c->ev = ev; | |
e8c92ba7 RK |
1564 | c->w = ev_writer_new(ev, fd, writer_error, c, |
1565 | "client writer"); | |
1566 | c->r = ev_reader_new(ev, fd, redirect_reader_callback, reader_error, c, | |
1567 | "client reader"); | |
75d64210 | 1568 | ev_tie(c->r, c->w); |
460b9539 | 1569 | c->fd = fd; |
1570 | c->reader = reader_callback; | |
1571 | c->l = l; | |
eb5dc014 | 1572 | c->rights = 0; |
460b9539 | 1573 | gcry_randomize(c->nonce, sizeof c->nonce, GCRY_STRONG_RANDOM); |
7b32e917 RK |
1574 | sink_printf(ev_writer_sink(c->w), "231 %d %s %s\n", |
1575 | 2, | |
b3141726 RK |
1576 | config->authorization_algorithm, |
1577 | hex(c->nonce, sizeof c->nonce)); | |
460b9539 | 1578 | return 0; |
1579 | } | |
1580 | ||
1581 | int server_start(ev_source *ev, int pf, | |
1582 | size_t socklen, const struct sockaddr *sa, | |
1583 | const char *name) { | |
1584 | int fd; | |
1585 | struct listener *l = xmalloc(sizeof *l); | |
1586 | static const int one = 1; | |
1587 | ||
1588 | D(("server_init socket %s", name)); | |
1589 | fd = xsocket(pf, SOCK_STREAM, 0); | |
1590 | xsetsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one); | |
1591 | if(bind(fd, sa, socklen) < 0) { | |
1592 | error(errno, "error binding to %s", name); | |
1593 | return -1; | |
1594 | } | |
1595 | xlisten(fd, 128); | |
1596 | nonblock(fd); | |
1597 | cloexec(fd); | |
1598 | l->name = name; | |
1599 | l->pf = pf; | |
e8c92ba7 RK |
1600 | if(ev_listen(ev, fd, listen_callback, l, "server listener")) |
1601 | exit(EXIT_FAILURE); | |
460b9539 | 1602 | return fd; |
1603 | } | |
1604 | ||
1605 | int server_stop(ev_source *ev, int fd) { | |
1606 | xclose(fd); | |
1607 | return ev_listen_cancel(ev, fd); | |
1608 | } | |
1609 | ||
1610 | /* | |
1611 | Local Variables: | |
1612 | c-basic-offset:2 | |
1613 | comment-column:40 | |
1614 | fill-column:79 | |
1615 | End: | |
1616 | */ |