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