chiark / gitweb /
create users.db (but don't do anythin with it). tighter db permissions
[disorder] / lib / client.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder.
1020001c 3 * Copyright (C) 2004, 2005, 2006, 2007 Richard Kettlewell
460b9539 4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 * USA
19 */
20
21#include <config.h>
22#include "types.h"
23
24#include <sys/types.h>
25#include <sys/socket.h>
26#include <netinet/in.h>
27#include <sys/un.h>
28#include <string.h>
29#include <stdio.h>
30#include <unistd.h>
31#include <errno.h>
32#include <netdb.h>
33#include <stdlib.h>
34
35#include "log.h"
36#include "mem.h"
37#include "configuration.h"
38#include "queue.h"
39#include "client.h"
40#include "charset.h"
41#include "hex.h"
42#include "split.h"
43#include "vector.h"
44#include "inputline.h"
45#include "kvp.h"
46#include "syscalls.h"
47#include "printf.h"
48#include "sink.h"
49#include "addr.h"
50#include "authhash.h"
51#include "client-common.h"
52
53struct disorder_client {
54 FILE *fpin, *fpout;
55 char *ident;
56 char *user;
57 int verbose;
58};
59
60disorder_client *disorder_new(int verbose) {
61 disorder_client *c = xmalloc(sizeof (struct disorder_client));
62
63 c->verbose = verbose;
64 return c;
65}
66
67/* read a response line.
68 * If @rp@ is not a null pointer, returns the whole response through it.
69 * Return value is the response code, or -1 on error. */
70static int response(disorder_client *c, char **rp) {
71 char *r;
72
73 if(inputline(c->ident, c->fpin, &r, '\n'))
74 return -1;
75 D(("response: %s", r));
76 if(rp)
77 *rp = r;
78 if(r[0] >= '0' && r[0] <= '9'
79 && r[1] >= '0' && r[1] <= '9'
80 && r[2] >= '0' && r[2] <= '9'
81 && r[3] == ' ')
82 return (r[0] * 10 + r[1]) * 10 + r[2] - 111 * '0';
83 else {
84 error(0, "invalid reply format from %s", c->ident);
85 return -1;
86 }
87}
88
89/* Read a response.
90 * If @rp@ is not a null pointer then the response text (excluding
91 * the status code) is returned through it, UNLESS the response code
92 * is xx9.
93 * Return value is 0 for 2xx responses and -1 otherwise.
94 */
95static int check_response(disorder_client *c, char **rp) {
96 int rc;
97 char *r;
98
99 if((rc = response(c, &r)) == -1)
100 return -1;
101 else if(rc / 100 == 2) {
102 if(rp)
103 *rp = (rc % 10 == 9) ? 0 : xstrdup(r + 4);
104 return 0;
105 } else {
106 if(c->verbose)
107 error(0, "from %s: %s", c->ident, utf82mb(r));
108 return -1;
109 }
110}
111
112static int disorder_simple_v(disorder_client *c,
113 char **rp,
114 const char *cmd, va_list ap) {
115 const char *arg;
116 struct dynstr d;
117
118 if(cmd) {
119 dynstr_init(&d);
120 dynstr_append_string(&d, cmd);
121 while((arg = va_arg(ap, const char *))) {
122 dynstr_append(&d, ' ');
123 dynstr_append_string(&d, quoteutf8(arg));
124 }
125 dynstr_append(&d, '\n');
126 dynstr_terminate(&d);
127 D(("command: %s", d.vec));
128 if(fputs(d.vec, c->fpout) < 0 || fflush(c->fpout)) {
129 error(errno, "error writing to %s", c->ident);
130 return -1;
131 }
132 }
133 return check_response(c, rp);
134}
135
136/* Execute a simple command with any number of arguments.
137 * @rp@ and return value as for check_response().
138 */
139static int disorder_simple(disorder_client *c,
140 char **rp,
141 const char *cmd, ...) {
142 va_list ap;
143 int ret;
144
145 va_start(ap, cmd);
146 ret = disorder_simple_v(c, rp, cmd, ap);
147 va_end(ap);
148 return ret;
149}
150
151static int connect_sock(void *vc,
152 const struct sockaddr *sa,
153 socklen_t len,
154 const char *ident) {
155 const char *username, *password;
156 disorder_client *c = vc;
157 int n;
158
159 if(!(username = config->username)) {
160 error(0, "no username configured");
161 return -1;
162 }
163 if(!(password = config->password)) {
164 for(n = 0; (n < config->allow.n
165 && strcmp(config->allow.s[n].s[0], username)); ++n)
166 ;
167 if(n < config->allow.n)
168 password = config->allow.s[n].s[1];
169 else {
170 error(0, "no password configured");
171 return -1;
172 }
173 }
174 return disorder_connect_sock(c, sa, len, username, password, ident);
175}
176
177int disorder_connect(disorder_client *c) {
178 return with_sockaddr(c, connect_sock);
179}
180
181static int check_running(void attribute((unused)) *c,
182 const struct sockaddr *sa,
183 socklen_t len,
184 const char attribute((unused)) *ident) {
185 int fd, ret;
186
187 if((fd = socket(sa->sa_family, SOCK_STREAM, 0)) < 0)
188 fatal(errno, "error calling socket");
189 if(connect(fd, sa, len) < 0) {
190 if(errno == ECONNREFUSED || errno == ENOENT)
191 ret = 0;
192 else
193 fatal(errno, "error calling connect");
194 } else
195 ret = 1;
196 xclose(fd);
197 return ret;
198}
199
200int disorder_running(disorder_client *c) {
201 return with_sockaddr(c, check_running);
202}
203
204int disorder_connect_sock(disorder_client *c,
205 const struct sockaddr *sa,
206 socklen_t len,
207 const char *username,
208 const char *password,
209 const char *ident) {
637fdea3 210 int fd = -1, fd2 = -1, nrvec;
460b9539 211 unsigned char *nonce;
212 size_t nl;
213 const char *res;
637fdea3
RK
214 char *r, **rvec;
215 const char *algo = "SHA1";
460b9539 216
217 if(!password) {
218 error(0, "no password found");
219 return -1;
220 }
221 c->fpin = c->fpout = 0;
222 if((fd = socket(sa->sa_family, SOCK_STREAM, 0)) < 0) {
223 error(errno, "error calling socket");
224 return -1;
225 }
226 if(connect(fd, sa, len) < 0) {
227 error(errno, "error calling connect");
228 goto error;
229 }
230 if((fd2 = dup(fd)) < 0) {
231 error(errno, "error calling dup");
232 goto error;
233 }
234 if(!(c->fpin = fdopen(fd, "rb"))) {
235 error(errno, "error calling fdopen");
236 goto error;
237 }
238 fd = -1;
239 if(!(c->fpout = fdopen(fd2, "wb"))) {
240 error(errno, "error calling fdopen");
241 goto error;
242 }
243 fd2 = -1;
244 c->ident = xstrdup(ident);
245 if(disorder_simple(c, &r, 0, (const char *)0))
246 return -1;
637fdea3
RK
247 if(!(rvec = split(r, &nrvec, SPLIT_QUOTES, 0, 0)))
248 return -1;
249 if(nrvec > 1) {
250 algo = *rvec++;
251 --nrvec;
252 }
253 if(!nrvec)
254 return -1;
255 if(!(nonce = unhex(*rvec, &nl)))
460b9539 256 return -1;
637fdea3 257 if(!(res = authhash(nonce, nl, password, algo))) goto error;
460b9539 258 if(disorder_simple(c, 0, "user", username, res, (char *)0))
259 return -1;
260 c->user = xstrdup(username);
261 return 0;
262error:
263 if(c->fpin) fclose(c->fpin);
264 if(c->fpout) fclose(c->fpout);
265 if(fd2 != -1) close(fd2);
266 if(fd != -1) close(fd);
267 return -1;
268}
269
270int disorder_close(disorder_client *c) {
271 int ret = 0;
272
273 if(c->fpin) {
274 if(fclose(c->fpin) < 0) {
275 error(errno, "error calling fclose");
276 ret = -1;
277 }
278 c->fpin = 0;
279 }
280 if(c->fpout) {
281 if(fclose(c->fpout) < 0) {
282 error(errno, "error calling fclose");
283 ret = -1;
284 }
285 c->fpout = 0;
286 }
287 return 0;
288}
289
290int disorder_become(disorder_client *c, const char *user) {
291 if(disorder_simple(c, 0, "become", user, (char *)0)) return -1;
292 c->user = xstrdup(user);
293 return 0;
294}
295
296int disorder_play(disorder_client *c, const char *track) {
297 return disorder_simple(c, 0, "play", track, (char *)0);
298}
299
300int disorder_remove(disorder_client *c, const char *track) {
301 return disorder_simple(c, 0, "remove", track, (char *)0);
302}
303
304int disorder_move(disorder_client *c, const char *track, int delta) {
305 char d[16];
306
307 byte_snprintf(d, sizeof d, "%d", delta);
308 return disorder_simple(c, 0, "move", track, d, (char *)0);
309}
310
311int disorder_enable(disorder_client *c) {
312 return disorder_simple(c, 0, "enable", (char *)0);
313}
314
315int disorder_disable(disorder_client *c) {
316 return disorder_simple(c, 0, "disable", (char *)0);
317}
318
319int disorder_scratch(disorder_client *c, const char *id) {
320 return disorder_simple(c, 0, "scratch", id, (char *)0);
321}
322
323int disorder_shutdown(disorder_client *c) {
324 return disorder_simple(c, 0, "shutdown", (char *)0);
325}
326
327int disorder_reconfigure(disorder_client *c) {
328 return disorder_simple(c, 0, "reconfigure", (char *)0);
329}
330
331int disorder_rescan(disorder_client *c) {
332 return disorder_simple(c, 0, "rescan", (char *)0);
333}
334
335int disorder_version(disorder_client *c, char **rp) {
336 return disorder_simple(c, rp, "version", (char *)0);
337}
338
339static void client_error(const char *msg,
340 void attribute((unused)) *u) {
341 error(0, "error parsing reply: %s", msg);
342}
343
344int disorder_playing(disorder_client *c, struct queue_entry **qp) {
345 char *r;
346 struct queue_entry *q;
347
348 if(disorder_simple(c, &r, "playing", (char *)0))
349 return -1;
350 if(r) {
351 q = xmalloc(sizeof *q);
352 if(queue_unmarshall(q, r, client_error, 0))
353 return -1;
354 *qp = q;
355 } else
356 *qp = 0;
357 return 0;
358}
359
360static int disorder_somequeue(disorder_client *c,
361 const char *cmd, struct queue_entry **qp) {
362 struct queue_entry *qh, **qt = &qh, *q;
363 char *l;
364
365 if(disorder_simple(c, 0, cmd, (char *)0))
366 return -1;
367 while(inputline(c->ident, c->fpin, &l, '\n') >= 0) {
368 if(!strcmp(l, ".")) {
369 *qt = 0;
370 *qp = qh;
371 return 0;
372 }
373 q = xmalloc(sizeof *q);
374 if(!queue_unmarshall(q, l, client_error, 0)) {
375 *qt = q;
376 qt = &q->next;
377 }
378 }
379 if(ferror(c->fpin))
380 error(errno, "error reading %s", c->ident);
381 else
382 error(0, "error reading %s: unexpected EOF", c->ident);
383 return -1;
384}
385
386int disorder_recent(disorder_client *c, struct queue_entry **qp) {
387 return disorder_somequeue(c, "recent", qp);
388}
389
390int disorder_queue(disorder_client *c, struct queue_entry **qp) {
391 return disorder_somequeue(c, "queue", qp);
392}
393
394static int readlist(disorder_client *c, char ***vecp, int *nvecp) {
395 char *l;
396 struct vector v;
397
398 vector_init(&v);
399 while(inputline(c->ident, c->fpin, &l, '\n') >= 0) {
400 if(!strcmp(l, ".")) {
401 vector_terminate(&v);
402 if(nvecp)
403 *nvecp = v.nvec;
404 *vecp = v.vec;
405 return 0;
406 }
407 vector_append(&v, l + (*l == '.'));
408 }
409 if(ferror(c->fpin))
410 error(errno, "error reading %s", c->ident);
411 else
412 error(0, "error reading %s: unexpected EOF", c->ident);
413 return -1;
414}
415
416static int disorder_simple_list(disorder_client *c,
417 char ***vecp, int *nvecp,
418 const char *cmd, ...) {
419 va_list ap;
420 int ret;
421
422 va_start(ap, cmd);
423 ret = disorder_simple_v(c, 0, cmd, ap);
424 va_end(ap);
425 if(ret) return ret;
426 return readlist(c, vecp, nvecp);
427}
428
429int disorder_directories(disorder_client *c, const char *dir, const char *re,
430 char ***vecp, int *nvecp) {
431 return disorder_simple_list(c, vecp, nvecp, "dirs", dir, re, (char *)0);
432}
433
434int disorder_files(disorder_client *c, const char *dir, const char *re,
435 char ***vecp, int *nvecp) {
436 return disorder_simple_list(c, vecp, nvecp, "files", dir, re, (char *)0);
437}
438
439int disorder_allfiles(disorder_client *c, const char *dir, const char *re,
440 char ***vecp, int *nvecp) {
441 return disorder_simple_list(c, vecp, nvecp, "allfiles", dir, re, (char *)0);
442}
443
444char *disorder_user(disorder_client *c) {
445 return c->user;
446}
447
448int disorder_set(disorder_client *c, const char *track,
449 const char *key, const char *value) {
450 return disorder_simple(c, 0, "set", track, key, value, (char *)0);
451}
452
453int disorder_unset(disorder_client *c, const char *track,
454 const char *key) {
455 return disorder_simple(c, 0, "unset", track, key, (char *)0);
456}
457
458int disorder_get(disorder_client *c,
459 const char *track, const char *key, char **valuep) {
460 return disorder_simple(c, valuep, "get", track, key, (char *)0);
461}
462
463static void pref_error_handler(const char *msg,
464 void attribute((unused)) *u) {
465 error(0, "error handling 'prefs' reply: %s", msg);
466}
467
468int disorder_prefs(disorder_client *c, const char *track, struct kvp **kp) {
469 char **vec, **pvec;
470 int nvec, npvec, n;
471 struct kvp *k;
472
473 if(disorder_simple_list(c, &vec, &nvec, "prefs", track, (char *)0))
474 return -1;
475 for(n = 0; n < nvec; ++n) {
476 if(!(pvec = split(vec[n], &npvec, SPLIT_QUOTES, pref_error_handler, 0)))
477 return -1;
478 if(npvec != 2) {
479 pref_error_handler("malformed response", 0);
480 return -1;
481 }
482 *kp = k = xmalloc(sizeof *k);
483 k->name = pvec[0];
484 k->value = pvec[1];
485 kp = &k->next;
486 }
487 *kp = 0;
488 return 0;
489}
490
491static int boolean(const char *cmd, const char *value,
492 int *flagp) {
493 if(!strcmp(value, "yes")) *flagp = 1;
494 else if(!strcmp(value, "no")) *flagp = 0;
495 else {
496 error(0, "malformed response to '%s'", cmd);
497 return -1;
498 }
499 return 0;
500}
501
502int disorder_exists(disorder_client *c, const char *track, int *existsp) {
503 char *v;
504
505 if(disorder_simple(c, &v, "exists", track, (char *)0)) return -1;
506 return boolean("exists", v, existsp);
507}
508
509int disorder_enabled(disorder_client *c, int *enabledp) {
510 char *v;
511
512 if(disorder_simple(c, &v, "enabled", (char *)0)) return -1;
513 return boolean("enabled", v, enabledp);
514}
515
516int disorder_length(disorder_client *c, const char *track,
517 long *valuep) {
518 char *value;
519
520 if(disorder_simple(c, &value, "length", track, (char *)0)) return -1;
521 *valuep = atol(value);
522 return 0;
523}
524
525int disorder_search(disorder_client *c, const char *terms,
526 char ***vecp, int *nvecp) {
527 return disorder_simple_list(c, vecp, nvecp, "search", terms, (char *)0);
528}
529
530int disorder_random_enable(disorder_client *c) {
531 return disorder_simple(c, 0, "random-enable", (char *)0);
532}
533
534int disorder_random_disable(disorder_client *c) {
535 return disorder_simple(c, 0, "random-disable", (char *)0);
536}
537
538int disorder_random_enabled(disorder_client *c, int *enabledp) {
539 char *v;
540
541 if(disorder_simple(c, &v, "random-enabled", (char *)0)) return -1;
542 return boolean("random-enabled", v, enabledp);
543}
544
545int disorder_stats(disorder_client *c,
546 char ***vecp, int *nvecp) {
547 return disorder_simple_list(c, vecp, nvecp, "stats", (char *)0);
548}
549
550int disorder_set_volume(disorder_client *c, int left, int right) {
551 char *ls, *rs;
552
553 if(byte_asprintf(&ls, "%d", left) < 0
554 || byte_asprintf(&rs, "%d", right) < 0)
555 return -1;
556 if(disorder_simple(c, 0, "volume", ls, rs, (char *)0)) return -1;
557 return 0;
558}
559
560int disorder_get_volume(disorder_client *c, int *left, int *right) {
561 char *r;
562
563 if(disorder_simple(c, &r, "volume", (char *)0)) return -1;
564 if(sscanf(r, "%d %d", left, right) != 2) {
565 error(0, "error parsing response to 'volume': '%s'", r);
566 return -1;
567 }
568 return 0;
569}
570
571int disorder_log(disorder_client *c, struct sink *s) {
572 char *l;
573
574 if(disorder_simple(c, 0, "log", (char *)0)) return -1;
575 while(inputline(c->ident, c->fpin, &l, '\n') >= 0 && strcmp(l, "."))
576 if(sink_printf(s, "%s\n", l) < 0) return -1;
577 if(ferror(c->fpin) || feof(c->fpin)) return -1;
578 return 0;
579}
580
581int disorder_part(disorder_client *c, char **partp,
582 const char *track, const char *context, const char *part) {
583 return disorder_simple(c, partp, "part", track, context, part, (char *)0);
584}
585
586int disorder_resolve(disorder_client *c, char **trackp, const char *track) {
587 return disorder_simple(c, trackp, "resolve", track, (char *)0);
588}
589
590int disorder_pause(disorder_client *c) {
591 return disorder_simple(c, 0, "pause", (char *)0);
592}
593
594int disorder_resume(disorder_client *c) {
595 return disorder_simple(c, 0, "resume", (char *)0);
596}
597
598int disorder_tags(disorder_client *c,
599 char ***vecp, int *nvecp) {
600 return disorder_simple_list(c, vecp, nvecp, "tags", (char *)0);
601}
602
2a10b70b
RK
603/** @brief Get recentl added tracks
604 * @param c Client
605 * @param vecp Where to store pointer to list
606 * @param nvecp Where to store count
607 * @param max Maximum tracks to fetch, or 0 for all available
608 * @return 0 on success, non-0 on error
609 */
610int disorder_new_tracks(disorder_client *c,
611 char ***vecp, int *nvecp,
612 int max) {
613 char limit[32];
614
615 sprintf(limit, "%d", max);
616 return disorder_simple_list(c, vecp, nvecp, "new", limit, (char *)0);
617}
618
460b9539 619int disorder_set_global(disorder_client *c,
620 const char *key, const char *value) {
621 return disorder_simple(c, 0, "set-global", key, value, (char *)0);
622}
623
624int disorder_unset_global(disorder_client *c, const char *key) {
625 return disorder_simple(c, 0, "unset-global", key, (char *)0);
626}
627
628int disorder_get_global(disorder_client *c, const char *key, char **valuep) {
629 return disorder_simple(c, valuep, "get-global", key, (char *)0);
630}
631
ca831831
RK
632int disorder_rtp_address(disorder_client *c, char **addressp, char **portp) {
633 char *r;
634 int rc, n;
635 char **vec;
636
637 if((rc = disorder_simple(c, &r, "rtp-address", (char *)0)))
638 return rc;
639 vec = split(r, &n, SPLIT_QUOTES, 0, 0);
640 if(n != 2) {
641 error(0, "malformed rtp-address reply");
642 return -1;
643 }
644 *addressp = vec[0];
645 *portp = vec[1];
646 return 0;
647}
648
460b9539 649/*
650Local Variables:
651c-basic-offset:2
652comment-column:40
653End:
654*/