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