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