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