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