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