chiark / gitweb /
lib/client.[ch]: Delete `disorder_client_af' which is no longer used.
[disorder] / lib / client.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder.
cca89d7c 3 * Copyright (C) 2004-13 Richard Kettlewell
460b9539 4 *
e7eb3a27 5 * This program is free software: you can redistribute it and/or modify
460b9539 6 * it under the terms of the GNU General Public License as published by
e7eb3a27 7 * the Free Software Foundation, either version 3 of the License, or
460b9539 8 * (at your option) any later version.
e7eb3a27
RK
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
460b9539 15 * You should have received a copy of the GNU General Public License
e7eb3a27 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
460b9539 17 */
c4e2cfdd
RK
18/** @file lib/client.c
19 * @brief Simple C client
20 *
158d0961 21 * See @ref lib/eclient.c for an asynchronous-capable client
c4e2cfdd
RK
22 * implementation.
23 */
460b9539 24
05b75f8d 25#include "common.h"
460b9539 26
27#include <sys/types.h>
cca89d7c
RK
28#if HAVE_SYS_SOCKET_H
29# include <sys/socket.h>
30#endif
31#if HAVE_NETINET_IN_H
32# include <netinet/in.h>
33#endif
34#if HAVE_SYS_UN_H
35# include <sys/un.h>
36#endif
37#if HAVE_UNISTD_H
38# include <unistd.h>
39#endif
460b9539 40#include <errno.h>
cca89d7c
RK
41#if HAVE_NETDB_H
42# include <netdb.h>
43#endif
460b9539 44
45#include "log.h"
46#include "mem.h"
460b9539 47#include "queue.h"
48#include "client.h"
49#include "charset.h"
50#include "hex.h"
51#include "split.h"
52#include "vector.h"
53#include "inputline.h"
54#include "kvp.h"
55#include "syscalls.h"
56#include "printf.h"
57#include "sink.h"
58#include "addr.h"
59#include "authhash.h"
60#include "client-common.h"
5df73aeb 61#include "rights.h"
758aa6c3 62#include "kvp.h"
96f6312a 63#include "socketio.h"
460b9539 64
0590cedc 65/** @brief Client handle contents */
460b9539 66struct disorder_client {
0590cedc 67 /** @brief Stream to read from */
96f6312a 68 struct source *input;
0590cedc 69 /** @brief Stream to write to */
96f6312a 70 struct sink *output;
0590cedc 71 /** @brief Peer description */
460b9539 72 char *ident;
0590cedc 73 /** @brief Username */
460b9539 74 char *user;
0590cedc 75 /** @brief Report errors to @c stderr */
460b9539 76 int verbose;
0590cedc 77 /** @brief Last error string */
e9e8a16d 78 const char *last;
96af1d7e
RK
79 /** @brief Address family */
80 int family;
96f6312a
RK
81 /** @brief True if open */
82 int open;
83 /** @brief Socket I/O context */
84 struct socketio sio;
ff92debd
MW
85 /** @brief Whether to try to open a privileged connection */
86 int trypriv;
460b9539 87};
88
c4e2cfdd
RK
89/** @brief Create a new client
90 * @param verbose If nonzero, write extra junk to stderr
91 * @return Pointer to new client
92 *
fdf98378 93 * You must call disorder_connect(), disorder_connect_user() or
94 * disorder_connect_cookie() to connect it. Use disorder_close() to
95 * dispose of the client when finished with it.
c4e2cfdd 96 */
460b9539 97disorder_client *disorder_new(int verbose) {
98 disorder_client *c = xmalloc(sizeof (struct disorder_client));
99
100 c->verbose = verbose;
96af1d7e 101 c->family = -1;
ff92debd 102 c->trypriv = 1;
460b9539 103 return c;
104}
105
ff92debd
MW
106/** @brief Don't try to make a privileged connection
107 * @param c Client
108 *
109 * You must call this before any of the connection functions (e.g.,
110 * disorder_connect(), disorder_connect_user()), if at all.
111 */
112void disorder_force_unpriv(disorder_client *c) {
113 assert(!c->open);
114 c->trypriv = 0;
115}
116
e31de822
MW
117/** @brief Determine the local socket address of this client */
118int disorder_client_sockname(disorder_client *c,
119 struct sockaddr *sa, socklen_t *len_inout) {
120 int rc;
121 if((rc = getsockname(c->sio.sd, sa, len_inout)))
122 disorder_error(errno, "failed to read client socket name");
123 return rc;
124}
125
126/** @brief Determine the remote peer address for this client */
127int disorder_client_peername(disorder_client *c,
128 struct sockaddr *sa, socklen_t *len_inout) {
129 int rc;
130 if((rc = getpeername(c->sio.sd, sa, len_inout)))
131 disorder_error(errno, "failed to read client socket name");
132 return rc;
133}
134
c4e2cfdd
RK
135/** @brief Read a response line
136 * @param c Client
137 * @param rp Where to store response, or NULL (UTF-8)
138 * @return Response code 0-999 or -1 on error
139 */
460b9539 140static int response(disorder_client *c, char **rp) {
141 char *r;
96f6312a 142 char errbuf[1024];
460b9539 143
96f6312a
RK
144 if(inputlines(c->ident, c->input, &r, '\n')) {
145 byte_xasprintf((char **)&c->last, "input error: %s",
146 format_error(c->input->eclass, source_err(c->input), errbuf, sizeof errbuf));
460b9539 147 return -1;
e9e8a16d 148 }
460b9539 149 D(("response: %s", r));
150 if(rp)
151 *rp = r;
152 if(r[0] >= '0' && r[0] <= '9'
153 && r[1] >= '0' && r[1] <= '9'
154 && r[2] >= '0' && r[2] <= '9'
bb037be2 155 && r[3] == ' ') {
156 c->last = r + 4;
460b9539 157 return (r[0] * 10 + r[1]) * 10 + r[2] - 111 * '0';
bb037be2 158 } else {
e9e8a16d 159 c->last = "invalid reply format";
2e9ba080 160 disorder_error(0, "invalid reply format from %s", c->ident);
460b9539 161 return -1;
162 }
163}
164
bb037be2 165/** @brief Return last response string
166 * @param c Client
167 * @return Last response string (UTF-8, English) or NULL
168 */
169const char *disorder_last(disorder_client *c) {
170 return c->last;
171}
172
c4e2cfdd
RK
173/** @brief Read and partially parse a response
174 * @param c Client
175 * @param rp Where to store response text (or NULL) (UTF-8)
176 * @return 0 on success, non-0 on error
177 *
178 * 5xx responses count as errors.
179 *
180 * @p rp will NOT be filled in for xx9 responses (where it is just
181 * commentary for a command where it would normally be meaningful).
182 *
183 * NB that the response will NOT be converted to the local encoding.
460b9539 184 */
185static int check_response(disorder_client *c, char **rp) {
186 int rc;
187 char *r;
188
189 if((rc = response(c, &r)) == -1)
190 return -1;
191 else if(rc / 100 == 2) {
192 if(rp)
193 *rp = (rc % 10 == 9) ? 0 : xstrdup(r + 4);
f74f4f32 194 xfree(r);
460b9539 195 return 0;
196 } else {
197 if(c->verbose)
2e9ba080 198 disorder_error(0, "from %s: %s", c->ident, utf82mb(r));
f74f4f32 199 xfree(r);
2bead829 200 return rc;
460b9539 201 }
202}
203
c4e2cfdd
RK
204/** @brief Issue a command and parse a simple response
205 * @param c Client
206 * @param rp Where to store result, or NULL
207 * @param cmd Command
208 * @param ap Arguments (UTF-8), terminated by (char *)0
209 * @return 0 on success, non-0 on error
210 *
211 * 5xx responses count as errors.
212 *
213 * @p rp will NOT be filled in for xx9 responses (where it is just
214 * commentary for a command where it would normally be meaningful).
215 *
216 * NB that the response will NOT be converted to the local encoding
217 * nor will quotes be stripped. See dequote().
39c53343 218 *
ad131c25 219 * Put @ref disorder__body in the argument list followed by a char **
08af2413 220 * and int giving the body to follow the command. If the int is @c -1
0bc1d67c
RK
221 * then the list is assumed to be NULL-terminated. This may be used
222 * only once.
223 *
ad131c25 224 * Put @ref disorder__list in the argument list followed by a char **
0bc1d67c
RK
225 * and int giving a list of arguments to include. If the int is @c -1
226 * then the list is assumed to be NULL-terminated. This may be used
227 * any number of times.
39c53343 228 *
bcb2af72
RK
229 * Put @ref disorder__integer in the argument list followed by a long to
230 * send its value in decimal. This may be used any number of times.
231 *
232 * Put @ref disorder__time in the argument list followed by a time_t
233 * to send its value in decimal. This may be used any number of
234 * times.
235 *
39c53343
RK
236 * Usually you would call this via one of the following interfaces:
237 * - disorder_simple()
c4e2cfdd 238 */
460b9539 239static int disorder_simple_v(disorder_client *c,
240 char **rp,
39c53343 241 const char *cmd,
39c53343 242 va_list ap) {
460b9539 243 const char *arg;
244 struct dynstr d;
08af2413
RK
245 char **body = NULL;
246 int nbody = 0;
247 int has_body = 0;
96f6312a 248 char errbuf[1024];
460b9539 249
96f6312a 250 if(!c->open) {
e9e8a16d 251 c->last = "not connected";
2e9ba080 252 disorder_error(0, "not connected to server");
62ef2216 253 return -1;
254 }
460b9539 255 if(cmd) {
256 dynstr_init(&d);
257 dynstr_append_string(&d, cmd);
258 while((arg = va_arg(ap, const char *))) {
ad131c25 259 if(arg == disorder__body) {
08af2413
RK
260 body = va_arg(ap, char **);
261 nbody = va_arg(ap, int);
262 has_body = 1;
ad131c25 263 } else if(arg == disorder__list) {
0bc1d67c
RK
264 char **list = va_arg(ap, char **);
265 int nlist = va_arg(ap, int);
05b3f1f6 266 int n;
0bc1d67c
RK
267 if(nlist < 0) {
268 for(nlist = 0; list[nlist]; ++nlist)
269 ;
270 }
05b3f1f6 271 for(n = 0; n < nlist; ++n) {
0bc1d67c
RK
272 dynstr_append(&d, ' ');
273 dynstr_append_string(&d, quoteutf8(arg));
274 }
bcb2af72
RK
275 } else if(arg == disorder__integer) {
276 long n = va_arg(ap, long);
277 char buffer[16];
3c95f40e 278 byte_snprintf(buffer, sizeof buffer, "%ld", n);
bcb2af72
RK
279 dynstr_append(&d, ' ');
280 dynstr_append_string(&d, buffer);
281 } else if(arg == disorder__time) {
282 time_t n = va_arg(ap, time_t);
283 char buffer[16];
3c95f40e 284 byte_snprintf(buffer, sizeof buffer, "%lld", (long long)n);
bcb2af72
RK
285 dynstr_append(&d, ' ');
286 dynstr_append_string(&d, buffer);
08af2413
RK
287 } else {
288 dynstr_append(&d, ' ');
289 dynstr_append_string(&d, quoteutf8(arg));
290 }
460b9539 291 }
292 dynstr_append(&d, '\n');
293 dynstr_terminate(&d);
294 D(("command: %s", d.vec));
96f6312a 295 if(sink_write(c->output, d.vec, d.nvec) < 0)
39c53343 296 goto write_error;
f74f4f32 297 xfree(d.vec);
08af2413 298 if(has_body) {
05b3f1f6 299 int n;
39c53343
RK
300 if(nbody < 0)
301 for(nbody = 0; body[nbody]; ++nbody)
302 ;
05b3f1f6 303 for(n = 0; n < nbody; ++n) {
39c53343 304 if(body[n][0] == '.')
96f6312a 305 if(sink_writec(c->output, '.') < 0)
39c53343 306 goto write_error;
96f6312a 307 if(sink_writes(c->output, body[n]) < 0)
39c53343 308 goto write_error;
96f6312a 309 if(sink_writec(c->output, '\n') < 0)
39c53343
RK
310 goto write_error;
311 }
96f6312a 312 if(sink_writes(c->output, ".\n") < 0)
39c53343 313 goto write_error;
460b9539 314 }
96f6312a 315 if(sink_flush(c->output))
39c53343 316 goto write_error;
460b9539 317 }
318 return check_response(c, rp);
39c53343 319write_error:
96f6312a
RK
320 byte_xasprintf((char **)&c->last, "write error: %s",
321 format_error(c->output->eclass, sink_err(c->output), errbuf, sizeof errbuf));
322 disorder_error(0, "%s: %s", c->ident, c->last);
39c53343 323 return -1;
460b9539 324}
325
c4e2cfdd
RK
326/** @brief Issue a command and parse a simple response
327 * @param c Client
328 * @param rp Where to store result, or NULL (UTF-8)
329 * @param cmd Command
330 * @return 0 on success, non-0 on error
331 *
332 * The remaining arguments are command arguments, terminated by (char
333 * *)0. They should be in UTF-8.
334 *
335 * 5xx responses count as errors.
336 *
337 * @p rp will NOT be filled in for xx9 responses (where it is just
338 * commentary for a command where it would normally be meaningful).
339 *
340 * NB that the response will NOT be converted to the local encoding
341 * nor will quotes be stripped. See dequote().
460b9539 342 */
343static int disorder_simple(disorder_client *c,
344 char **rp,
345 const char *cmd, ...) {
346 va_list ap;
347 int ret;
348
349 va_start(ap, cmd);
08af2413 350 ret = disorder_simple_v(c, rp, cmd, ap);
460b9539 351 va_end(ap);
352 return ret;
353}
354
dab87ecc
RK
355/** @brief Issue a command and split the response
356 * @param c Client
357 * @param vecp Where to store results
358 * @param nvecp Where to store count of results
359 * @param expected Expected count (or -1 to not check)
360 * @param cmd Command
361 * @return 0 on success, non-0 on error
362 *
363 * The remaining arguments are command arguments, terminated by (char
364 * *)0. They should be in UTF-8.
365 *
366 * 5xx responses count as errors.
367 *
368 * @p rp will NOT be filled in for xx9 responses (where it is just
369 * commentary for a command where it would normally be meaningful).
370 *
371 * NB that the response will NOT be converted to the local encoding
372 * nor will quotes be stripped. See dequote().
373 */
374static int disorder_simple_split(disorder_client *c,
375 char ***vecp,
376 int *nvecp,
377 int expected,
378 const char *cmd, ...) {
379 va_list ap;
380 int ret;
381 char *r;
382 char **vec;
383 int nvec;
384
385 va_start(ap, cmd);
386 ret = disorder_simple_v(c, &r, cmd, ap);
387 va_end(ap);
388 if(!ret) {
389 vec = split(r, &nvec, SPLIT_QUOTES, 0, 0);
390 xfree(r);
391 if(expected < 0 || nvec == expected) {
392 *vecp = vec;
393 *nvecp = nvec;
394 } else {
395 disorder_error(0, "malformed reply to %s", cmd);
396 c->last = "malformed reply";
397 ret = -1;
398 free_strings(nvec, vec);
399 }
400 }
401 if(ret) {
402 *vecp = NULL;
403 *nvecp = 0;
404 }
405 return ret;
406}
407
0227f67d
RK
408/** @brief Dequote a result string
409 * @param rc 0 on success, non-0 on error
410 * @param rp Where result string is stored (UTF-8)
411 * @return @p rc
412 *
413 * This is used as a wrapper around disorder_simple() to dequote
414 * results in place.
415 */
416static int dequote(int rc, char **rp) {
417 char **rr;
2bead829 418
0227f67d
RK
419 if(!rc) {
420 if((rr = split(*rp, 0, SPLIT_QUOTES, 0, 0)) && *rr) {
f74f4f32 421 xfree(*rp);
0227f67d 422 *rp = *rr;
f74f4f32 423 xfree(rr);
0227f67d
RK
424 return 0;
425 }
2e9ba080 426 disorder_error(0, "invalid reply: %s", *rp);
460b9539 427 }
2bead829 428 return rc;
460b9539 429}
430
0227f67d 431/** @brief Generic connection routine
319d7107 432 * @param conf Configuration to follow
c4e2cfdd 433 * @param c Client
0227f67d
RK
434 * @param username Username to log in with or NULL
435 * @param password Password to log in with or NULL
436 * @param cookie Cookie to log in with or NULL
c4e2cfdd
RK
437 * @return 0 on success, non-0 on error
438 *
0227f67d
RK
439 * @p cookie is tried first if not NULL. If it is NULL then @p
440 * username must not be. If @p username is not NULL then nor may @p
441 * password be.
c4e2cfdd 442 */
319d7107
RK
443int disorder_connect_generic(struct config *conf,
444 disorder_client *c,
445 const char *username,
446 const char *password,
447 const char *cookie) {
96f6312a
RK
448 SOCKET sd = INVALID_SOCKET;
449 int nrvec = 0, rc;
f74f4f32 450 unsigned char *nonce = NULL;
460b9539 451 size_t nl;
f74f4f32
RK
452 char *res = NULL;
453 char *r = NULL, **rvec = NULL;
7b32e917 454 const char *protocol, *algorithm, *challenge;
f74f4f32 455 struct sockaddr *sa = NULL;
0227f67d 456 socklen_t salen;
96f6312a 457 char errbuf[1024];
460b9539 458
ff92debd
MW
459 if((salen = disorder_find_server(conf,
460 (c->trypriv ? 0 : DISORDER_FS_NOTPRIV),
461 &sa, &c->ident)) == (socklen_t)-1)
460b9539 462 return -1;
96f6312a
RK
463 c->input = 0;
464 c->output = 0;
465 if((sd = socket(sa->sa_family, SOCK_STREAM, 0)) < 0) {
466 byte_xasprintf((char **)&c->last, "socket: %s",
467 format_error(ec_socket, socket_error(), errbuf, sizeof errbuf));
468 disorder_error(0, "%s", c->last);
460b9539 469 return -1;
470 }
96af1d7e 471 c->family = sa->sa_family;
96f6312a
RK
472 if(connect(sd, sa, salen) < 0) {
473 byte_xasprintf((char **)&c->last, "connect: %s",
474 format_error(ec_socket, socket_error(), errbuf, sizeof errbuf));
475 disorder_error(0, "%s", c->last);
460b9539 476 goto error;
477 }
96f6312a
RK
478 socketio_init(&c->sio, sd);
479 c->open = 1;
480 sd = INVALID_SOCKET;
481 c->output = sink_socketio(&c->sio);
482 c->input = source_socketio(&c->sio);
2bead829 483 if((rc = disorder_simple(c, &r, 0, (const char *)0)))
484 goto error_rc;
637fdea3 485 if(!(rvec = split(r, &nrvec, SPLIT_QUOTES, 0, 0)))
2bead829 486 goto error;
7b32e917 487 if(nrvec != 3) {
e9e8a16d 488 c->last = "cannot parse server greeting";
2e9ba080 489 disorder_error(0, "cannot parse server greeting %s", r);
2bead829 490 goto error;
637fdea3 491 }
f74f4f32 492 protocol = rvec[0];
7b32e917 493 if(strcmp(protocol, "2")) {
e9e8a16d 494 c->last = "unknown protocol version";
2e9ba080 495 disorder_error(0, "unknown protocol version: %s", protocol);
2bead829 496 goto error;
7b32e917 497 }
f74f4f32
RK
498 algorithm = rvec[1];
499 challenge = rvec[2];
7b32e917 500 if(!(nonce = unhex(challenge, &nl)))
2bead829 501 goto error;
0227f67d
RK
502 if(cookie) {
503 if(!dequote(disorder_simple(c, &c->user, "cookie", cookie, (char *)0),
504 &c->user))
505 return 0; /* success */
506 if(!username) {
e9e8a16d 507 c->last = "cookie failed and no username";
2e9ba080 508 disorder_error(0, "cookie did not work and no username available");
2bead829 509 goto error;
0227f67d
RK
510 }
511 }
e9e8a16d
RK
512 if(!(res = authhash(nonce, nl, password, algorithm))) {
513 c->last = "error computing authorization hash";
514 goto error;
515 }
2bead829 516 if((rc = disorder_simple(c, 0, "user", username, res, (char *)0)))
517 goto error_rc;
460b9539 518 c->user = xstrdup(username);
f74f4f32
RK
519 xfree(res);
520 free_strings(nrvec, rvec);
521 xfree(nonce);
522 xfree(sa);
523 xfree(r);
460b9539 524 return 0;
525error:
2bead829 526 rc = -1;
527error_rc:
96f6312a
RK
528 xfree(c->output);
529 c->output = NULL;
530 xfree(c->input);
531 c->input = NULL;
532 if(c->open) { socketio_close(&c->sio); c->open = 0; }
533 if(sd != INVALID_SOCKET) closesocket(sd);
2bead829 534 return rc;
460b9539 535}
536
fdf98378 537/** @brief Connect a client with a specified username and password
538 * @param c Client
539 * @param username Username to log in with
540 * @param password Password to log in with
541 * @return 0 on success, non-0 on error
542 */
543int disorder_connect_user(disorder_client *c,
544 const char *username,
545 const char *password) {
319d7107
RK
546 return disorder_connect_generic(config,
547 c,
fdf98378 548 username,
549 password,
550 0);
551}
552
0227f67d
RK
553/** @brief Connect a client
554 * @param c Client
555 * @return 0 on success, non-0 on error
556 *
557 * The connection will use the username and password found in @ref
558 * config, or directly from the database if no password is found and
559 * the database is readable (usually only for root).
560 */
561int disorder_connect(disorder_client *c) {
562 const char *username, *password;
563
564 if(!(username = config->username)) {
e9e8a16d 565 c->last = "no username";
2e9ba080 566 disorder_error(0, "no username configured");
0227f67d
RK
567 return -1;
568 }
569 password = config->password;
1be9101e
RK
570 /* If we're connecting as 'root' guess that we're the system root
571 * user (or the jukebox user), both of which can use the privileged
572 * socket. They can also furtle with the db directly: that is why
573 * privileged socket does not represent a privilege escalation. */
574 if(!password
575 && !strcmp(username, "root"))
576 password = "anything will do for root";
0227f67d
RK
577 if(!password) {
578 /* Oh well */
e9e8a16d 579 c->last = "no password";
2e9ba080 580 disorder_error(0, "no password configured for user '%s'", username);
0227f67d
RK
581 return -1;
582 }
319d7107
RK
583 return disorder_connect_generic(config,
584 c,
0227f67d
RK
585 username,
586 password,
587 0);
588}
589
590/** @brief Connect a client
591 * @param c Client
592 * @param cookie Cookie to log in with, or NULL
593 * @return 0 on success, non-0 on error
594 *
595 * If @p cookie is NULL or does not work then we attempt to log in as
596 * guest instead (so when the cookie expires only an extra round trip
70adc86b 597 * is needed rather than a complete new login).
0227f67d
RK
598 */
599int disorder_connect_cookie(disorder_client *c,
600 const char *cookie) {
319d7107
RK
601 return disorder_connect_generic(config,
602 c,
0227f67d
RK
603 "guest",
604 "",
605 cookie);
606}
607
c4e2cfdd
RK
608/** @brief Close a client
609 * @param c Client
610 * @return 0 on succcess, non-0 on errior
611 *
612 * The client is still closed even on error. It might well be
613 * appropriate to ignore the return value.
614 */
460b9539 615int disorder_close(disorder_client *c) {
616 int ret = 0;
617
96f6312a
RK
618 if(c->open)
619 socketio_close(&c->sio);
620 xfree(c->output);
621 c->output = NULL;
622 xfree(c->input);
623 c->input = NULL;
f74f4f32 624 xfree(c->ident);
0227f67d 625 c->ident = 0;
f74f4f32 626 xfree(c->user);
0227f67d 627 c->user = 0;
375d9478 628 return ret;
460b9539 629}
630
460b9539 631static void client_error(const char *msg,
632 void attribute((unused)) *u) {
2e9ba080 633 disorder_error(0, "error parsing reply: %s", msg);
460b9539 634}
635
ec9c0462 636/** @brief Get a single queue entry
c4e2cfdd 637 * @param c Client
ec9c0462 638 * @param cmd Command
c4e2cfdd
RK
639 * @param qp Where to store track information
640 * @return 0 on success, non-0 on error
c4e2cfdd 641 */
ec9c0462
RK
642static int onequeue(disorder_client *c, const char *cmd,
643 struct queue_entry **qp) {
460b9539 644 char *r;
645 struct queue_entry *q;
2bead829 646 int rc;
460b9539 647
ec9c0462 648 if((rc = disorder_simple(c, &r, cmd, (char *)0)))
2bead829 649 return rc;
460b9539 650 if(r) {
651 q = xmalloc(sizeof *q);
652 if(queue_unmarshall(q, r, client_error, 0))
653 return -1;
654 *qp = q;
655 } else
656 *qp = 0;
657 return 0;
658}
659
0590cedc 660/** @brief Fetch the queue, recent list, etc */
c12575c6
RK
661static int readqueue(disorder_client *c,
662 struct queue_entry **qp) {
460b9539 663 struct queue_entry *qh, **qt = &qh, *q;
664 char *l;
96f6312a 665 char errbuf[1024];
460b9539 666
96f6312a 667 while(inputlines(c->ident, c->input, &l, '\n') >= 0) {
460b9539 668 if(!strcmp(l, ".")) {
669 *qt = 0;
670 *qp = qh;
b251ac34 671 xfree(l);
460b9539 672 return 0;
673 }
674 q = xmalloc(sizeof *q);
675 if(!queue_unmarshall(q, l, client_error, 0)) {
676 *qt = q;
677 qt = &q->next;
678 }
b251ac34 679 xfree(l);
460b9539 680 }
96f6312a
RK
681 if(source_err(c->input)) {
682 byte_xasprintf((char **)&c->last, "input error: %s",
683 format_error(c->input->eclass, source_err(c->input), errbuf, sizeof errbuf));
e9e8a16d 684 } else {
c12575c6 685 c->last = "input error: unexpected EOF";
e9e8a16d 686 }
96f6312a 687 disorder_error(0, "%s: %s", c->ident, c->last);
460b9539 688 return -1;
689}
690
c4e2cfdd
RK
691/** @brief Read a dot-stuffed list
692 * @param c Client
693 * @param vecp Where to store list (UTF-8)
694 * @param nvecp Where to store number of items, or NULL
695 * @return 0 on success, non-0 on error
696 *
697 * The list will have a final NULL not counted in @p nvecp.
698 */
460b9539 699static int readlist(disorder_client *c, char ***vecp, int *nvecp) {
700 char *l;
701 struct vector v;
96f6312a 702 char errbuf[1024];
460b9539 703
704 vector_init(&v);
96f6312a 705 while(inputlines(c->ident, c->input, &l, '\n') >= 0) {
460b9539 706 if(!strcmp(l, ".")) {
707 vector_terminate(&v);
708 if(nvecp)
709 *nvecp = v.nvec;
710 *vecp = v.vec;
5d2b941b 711 xfree(l);
460b9539 712 return 0;
713 }
5d2b941b
RK
714 vector_append(&v, xstrdup(l + (*l == '.')));
715 xfree(l);
460b9539 716 }
96f6312a
RK
717 if(source_err(c->input)) {
718 byte_xasprintf((char **)&c->last, "input error: %s",
719 format_error(c->input->eclass, source_err(c->input), errbuf, sizeof errbuf));
e9e8a16d
RK
720 } else {
721 c->last = "input error: unexpxected EOF";
e9e8a16d 722 }
96f6312a 723 disorder_error(0, "%s: %s", c->ident, c->last);
460b9539 724 return -1;
725}
726
c4e2cfdd
RK
727/** @brief Return the user we logged in with
728 * @param c Client
729 * @return User name (owned by @p c, don't modify)
730 */
460b9539 731char *disorder_user(disorder_client *c) {
732 return c->user;
733}
734
5dc19ffd 735static void pairlist_error_handler(const char *msg,
460b9539 736 void attribute((unused)) *u) {
5dc19ffd 737 disorder_error(0, "error handling key-value pair reply: %s", msg);
460b9539 738}
739
5dc19ffd 740/** @brief Get a list of key-value pairs
c4e2cfdd 741 * @param c Client
c4e2cfdd 742 * @param kp Where to store linked list of preferences
5dc19ffd
RK
743 * @param cmd Command
744 * @param ... Arguments
c4e2cfdd
RK
745 * @return 0 on success, non-0 on error
746 */
5dc19ffd 747static int pairlist(disorder_client *c, struct kvp **kp, const char *cmd, ...) {
460b9539 748 char **vec, **pvec;
2bead829 749 int nvec, npvec, n, rc;
460b9539 750 struct kvp *k;
5dc19ffd 751 va_list ap;
460b9539 752
5dc19ffd
RK
753 va_start(ap, cmd);
754 rc = disorder_simple_v(c, 0, cmd, ap);
755 va_end(ap);
756 if(rc)
2bead829 757 return rc;
5dc19ffd
RK
758 if((rc = readlist(c, &vec, &nvec)))
759 return rc;
460b9539 760 for(n = 0; n < nvec; ++n) {
5dc19ffd 761 if(!(pvec = split(vec[n], &npvec, SPLIT_QUOTES, pairlist_error_handler, 0)))
460b9539 762 return -1;
763 if(npvec != 2) {
5dc19ffd 764 pairlist_error_handler("malformed response", 0);
460b9539 765 return -1;
766 }
767 *kp = k = xmalloc(sizeof *k);
768 k->name = pvec[0];
769 k->value = pvec[1];
770 kp = &k->next;
0d047a12 771 xfree(pvec);
460b9539 772 }
0d047a12 773 free_strings(nvec, vec);
460b9539 774 *kp = 0;
775 return 0;
776}
777
9e42afcd
RK
778#if _WIN32
779# define boolean bodge_boolean
780#endif
781
c4e2cfdd
RK
782/** @brief Parse a boolean response
783 * @param cmd Command for use in error messsage
784 * @param value Result from server
785 * @param flagp Where to store result
786 * @return 0 on success, non-0 on error
787 */
460b9539 788static int boolean(const char *cmd, const char *value,
789 int *flagp) {
790 if(!strcmp(value, "yes")) *flagp = 1;
791 else if(!strcmp(value, "no")) *flagp = 0;
792 else {
2e9ba080 793 disorder_error(0, "malformed response to '%s'", cmd);
460b9539 794 return -1;
795 }
796 return 0;
797}
798
c4e2cfdd
RK
799/** @brief Log to a sink
800 * @param c Client
801 * @param s Sink to write log lines to
802 * @return 0 on success, non-0 on error
803 */
460b9539 804int disorder_log(disorder_client *c, struct sink *s) {
805 char *l;
2bead829 806 int rc;
96f6312a 807 char errbuf[1024];
460b9539 808
2bead829 809 if((rc = disorder_simple(c, 0, "log", (char *)0)))
810 return rc;
96f6312a 811 while(inputlines(c->ident, c->input, &l, '\n') >= 0 && strcmp(l, "."))
460b9539 812 if(sink_printf(s, "%s\n", l) < 0) return -1;
96f6312a 813 if(source_err(c->input)) {
e9e8a16d 814 byte_xasprintf((char **)&c->last, "input error: %s",
96f6312a
RK
815 format_error(c->input->eclass, source_err(c->input), errbuf, sizeof errbuf));
816 return -1;
817 } else if(source_eof(c->input)) {
818 byte_xasprintf((char **)&c->last, "input error: unexpected EOF");
e9e8a16d
RK
819 return -1;
820 }
9e42afcd 821
460b9539 822 return 0;
823}
824
7788b7c7
RK
825#include "client-stubs.c"
826
460b9539 827/*
828Local Variables:
829c-basic-offset:2
830comment-column:40
831End:
832*/