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 | |
44 | #if HAVE_PCRE_H | |
45 | # include <pcre.h> | |
46 | #endif | |
460b9539 | 47 | |
48 | #include "log.h" | |
49 | #include "mem.h" | |
460b9539 | 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" | |
5df73aeb | 64 | #include "rights.h" |
758aa6c3 | 65 | #include "kvp.h" |
96f6312a | 66 | #include "socketio.h" |
460b9539 | 67 | |
0590cedc | 68 | /** @brief Client handle contents */ |
460b9539 | 69 | struct disorder_client { |
0590cedc | 70 | /** @brief Stream to read from */ |
96f6312a | 71 | struct source *input; |
0590cedc | 72 | /** @brief Stream to write to */ |
96f6312a | 73 | struct sink *output; |
0590cedc | 74 | /** @brief Peer description */ |
460b9539 | 75 | char *ident; |
0590cedc | 76 | /** @brief Username */ |
460b9539 | 77 | char *user; |
0590cedc | 78 | /** @brief Report errors to @c stderr */ |
460b9539 | 79 | int verbose; |
0590cedc | 80 | /** @brief Last error string */ |
e9e8a16d | 81 | const char *last; |
96af1d7e RK |
82 | /** @brief Address family */ |
83 | int family; | |
96f6312a RK |
84 | /** @brief True if open */ |
85 | int open; | |
86 | /** @brief Socket I/O context */ | |
87 | struct socketio sio; | |
ff92debd MW |
88 | /** @brief Whether to try to open a privileged connection */ |
89 | int trypriv; | |
460b9539 | 90 | }; |
91 | ||
c4e2cfdd RK |
92 | /** @brief Create a new client |
93 | * @param verbose If nonzero, write extra junk to stderr | |
94 | * @return Pointer to new client | |
95 | * | |
fdf98378 | 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. | |
c4e2cfdd | 99 | */ |
460b9539 | 100 | disorder_client *disorder_new(int verbose) { |
101 | disorder_client *c = xmalloc(sizeof (struct disorder_client)); | |
102 | ||
103 | c->verbose = verbose; | |
96af1d7e | 104 | c->family = -1; |
ff92debd | 105 | c->trypriv = 1; |
460b9539 | 106 | return c; |
107 | } | |
108 | ||
ff92debd MW |
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 | ||
96af1d7e RK |
120 | /** @brief Return the address family used by this client */ |
121 | int disorder_client_af(disorder_client *c) { | |
122 | return c->family; | |
123 | } | |
124 | ||
c4e2cfdd RK |
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 | */ | |
460b9539 | 130 | static int response(disorder_client *c, char **rp) { |
131 | char *r; | |
96f6312a | 132 | char errbuf[1024]; |
460b9539 | 133 | |
96f6312a RK |
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)); | |
460b9539 | 137 | return -1; |
e9e8a16d | 138 | } |
460b9539 | 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' | |
bb037be2 | 145 | && r[3] == ' ') { |
146 | c->last = r + 4; | |
460b9539 | 147 | return (r[0] * 10 + r[1]) * 10 + r[2] - 111 * '0'; |
bb037be2 | 148 | } else { |
e9e8a16d | 149 | c->last = "invalid reply format"; |
2e9ba080 | 150 | disorder_error(0, "invalid reply format from %s", c->ident); |
460b9539 | 151 | return -1; |
152 | } | |
153 | } | |
154 | ||
bb037be2 | 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 | ||
c4e2cfdd RK |
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. | |
460b9539 | 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); | |
f74f4f32 | 184 | xfree(r); |
460b9539 | 185 | return 0; |
186 | } else { | |
187 | if(c->verbose) | |
2e9ba080 | 188 | disorder_error(0, "from %s: %s", c->ident, utf82mb(r)); |
f74f4f32 | 189 | xfree(r); |
2bead829 | 190 | return rc; |
460b9539 | 191 | } |
192 | } | |
193 | ||
c4e2cfdd RK |
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(). | |
39c53343 | 208 | * |
ad131c25 | 209 | * Put @ref disorder__body in the argument list followed by a char ** |
08af2413 | 210 | * and int giving the body to follow the command. If the int is @c -1 |
0bc1d67c RK |
211 | * then the list is assumed to be NULL-terminated. This may be used |
212 | * only once. | |
213 | * | |
ad131c25 | 214 | * Put @ref disorder__list in the argument list followed by a char ** |
0bc1d67c RK |
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. | |
39c53343 | 218 | * |
bcb2af72 RK |
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 | * | |
39c53343 RK |
226 | * Usually you would call this via one of the following interfaces: |
227 | * - disorder_simple() | |
c4e2cfdd | 228 | */ |
460b9539 | 229 | static int disorder_simple_v(disorder_client *c, |
230 | char **rp, | |
39c53343 | 231 | const char *cmd, |
39c53343 | 232 | va_list ap) { |
460b9539 | 233 | const char *arg; |
234 | struct dynstr d; | |
08af2413 RK |
235 | char **body = NULL; |
236 | int nbody = 0; | |
237 | int has_body = 0; | |
96f6312a | 238 | char errbuf[1024]; |
460b9539 | 239 | |
96f6312a | 240 | if(!c->open) { |
e9e8a16d | 241 | c->last = "not connected"; |
2e9ba080 | 242 | disorder_error(0, "not connected to server"); |
62ef2216 | 243 | return -1; |
244 | } | |
460b9539 | 245 | if(cmd) { |
246 | dynstr_init(&d); | |
247 | dynstr_append_string(&d, cmd); | |
248 | while((arg = va_arg(ap, const char *))) { | |
ad131c25 | 249 | if(arg == disorder__body) { |
08af2413 RK |
250 | body = va_arg(ap, char **); |
251 | nbody = va_arg(ap, int); | |
252 | has_body = 1; | |
ad131c25 | 253 | } else if(arg == disorder__list) { |
0bc1d67c RK |
254 | char **list = va_arg(ap, char **); |
255 | int nlist = va_arg(ap, int); | |
05b3f1f6 | 256 | int n; |
0bc1d67c RK |
257 | if(nlist < 0) { |
258 | for(nlist = 0; list[nlist]; ++nlist) | |
259 | ; | |
260 | } | |
05b3f1f6 | 261 | for(n = 0; n < nlist; ++n) { |
0bc1d67c RK |
262 | dynstr_append(&d, ' '); |
263 | dynstr_append_string(&d, quoteutf8(arg)); | |
264 | } | |
bcb2af72 RK |
265 | } else if(arg == disorder__integer) { |
266 | long n = va_arg(ap, long); | |
267 | char buffer[16]; | |
3c95f40e | 268 | byte_snprintf(buffer, sizeof buffer, "%ld", n); |
bcb2af72 RK |
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]; | |
3c95f40e | 274 | byte_snprintf(buffer, sizeof buffer, "%lld", (long long)n); |
bcb2af72 RK |
275 | dynstr_append(&d, ' '); |
276 | dynstr_append_string(&d, buffer); | |
08af2413 RK |
277 | } else { |
278 | dynstr_append(&d, ' '); | |
279 | dynstr_append_string(&d, quoteutf8(arg)); | |
280 | } | |
460b9539 | 281 | } |
282 | dynstr_append(&d, '\n'); | |
283 | dynstr_terminate(&d); | |
284 | D(("command: %s", d.vec)); | |
96f6312a | 285 | if(sink_write(c->output, d.vec, d.nvec) < 0) |
39c53343 | 286 | goto write_error; |
f74f4f32 | 287 | xfree(d.vec); |
08af2413 | 288 | if(has_body) { |
05b3f1f6 | 289 | int n; |
39c53343 RK |
290 | if(nbody < 0) |
291 | for(nbody = 0; body[nbody]; ++nbody) | |
292 | ; | |
05b3f1f6 | 293 | for(n = 0; n < nbody; ++n) { |
39c53343 | 294 | if(body[n][0] == '.') |
96f6312a | 295 | if(sink_writec(c->output, '.') < 0) |
39c53343 | 296 | goto write_error; |
96f6312a | 297 | if(sink_writes(c->output, body[n]) < 0) |
39c53343 | 298 | goto write_error; |
96f6312a | 299 | if(sink_writec(c->output, '\n') < 0) |
39c53343 RK |
300 | goto write_error; |
301 | } | |
96f6312a | 302 | if(sink_writes(c->output, ".\n") < 0) |
39c53343 | 303 | goto write_error; |
460b9539 | 304 | } |
96f6312a | 305 | if(sink_flush(c->output)) |
39c53343 | 306 | goto write_error; |
460b9539 | 307 | } |
308 | return check_response(c, rp); | |
39c53343 | 309 | write_error: |
96f6312a RK |
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); | |
39c53343 | 313 | return -1; |
460b9539 | 314 | } |
315 | ||
c4e2cfdd RK |
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(). | |
460b9539 | 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); | |
08af2413 | 340 | ret = disorder_simple_v(c, rp, cmd, ap); |
460b9539 | 341 | va_end(ap); |
342 | return ret; | |
343 | } | |
344 | ||
dab87ecc RK |
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 | ||
0227f67d RK |
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; | |
2bead829 | 408 | |
0227f67d RK |
409 | if(!rc) { |
410 | if((rr = split(*rp, 0, SPLIT_QUOTES, 0, 0)) && *rr) { | |
f74f4f32 | 411 | xfree(*rp); |
0227f67d | 412 | *rp = *rr; |
f74f4f32 | 413 | xfree(rr); |
0227f67d RK |
414 | return 0; |
415 | } | |
2e9ba080 | 416 | disorder_error(0, "invalid reply: %s", *rp); |
460b9539 | 417 | } |
2bead829 | 418 | return rc; |
460b9539 | 419 | } |
420 | ||
0227f67d | 421 | /** @brief Generic connection routine |
319d7107 | 422 | * @param conf Configuration to follow |
c4e2cfdd | 423 | * @param c Client |
0227f67d RK |
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 | |
c4e2cfdd RK |
427 | * @return 0 on success, non-0 on error |
428 | * | |
0227f67d RK |
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. | |
c4e2cfdd | 432 | */ |
319d7107 RK |
433 | int disorder_connect_generic(struct config *conf, |
434 | disorder_client *c, | |
435 | const char *username, | |
436 | const char *password, | |
437 | const char *cookie) { | |
96f6312a RK |
438 | SOCKET sd = INVALID_SOCKET; |
439 | int nrvec = 0, rc; | |
f74f4f32 | 440 | unsigned char *nonce = NULL; |
460b9539 | 441 | size_t nl; |
f74f4f32 RK |
442 | char *res = NULL; |
443 | char *r = NULL, **rvec = NULL; | |
7b32e917 | 444 | const char *protocol, *algorithm, *challenge; |
f74f4f32 | 445 | struct sockaddr *sa = NULL; |
0227f67d | 446 | socklen_t salen; |
96f6312a | 447 | char errbuf[1024]; |
460b9539 | 448 | |
ff92debd MW |
449 | if((salen = disorder_find_server(conf, |
450 | (c->trypriv ? 0 : DISORDER_FS_NOTPRIV), | |
451 | &sa, &c->ident)) == (socklen_t)-1) | |
460b9539 | 452 | return -1; |
96f6312a RK |
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); | |
460b9539 | 459 | return -1; |
460 | } | |
96af1d7e | 461 | c->family = sa->sa_family; |
96f6312a RK |
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); | |
460b9539 | 466 | goto error; |
467 | } | |
96f6312a RK |
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); | |
2bead829 | 473 | if((rc = disorder_simple(c, &r, 0, (const char *)0))) |
474 | goto error_rc; | |
637fdea3 | 475 | if(!(rvec = split(r, &nrvec, SPLIT_QUOTES, 0, 0))) |
2bead829 | 476 | goto error; |
7b32e917 | 477 | if(nrvec != 3) { |
e9e8a16d | 478 | c->last = "cannot parse server greeting"; |
2e9ba080 | 479 | disorder_error(0, "cannot parse server greeting %s", r); |
2bead829 | 480 | goto error; |
637fdea3 | 481 | } |
f74f4f32 | 482 | protocol = rvec[0]; |
7b32e917 | 483 | if(strcmp(protocol, "2")) { |
e9e8a16d | 484 | c->last = "unknown protocol version"; |
2e9ba080 | 485 | disorder_error(0, "unknown protocol version: %s", protocol); |
2bead829 | 486 | goto error; |
7b32e917 | 487 | } |
f74f4f32 RK |
488 | algorithm = rvec[1]; |
489 | challenge = rvec[2]; | |
7b32e917 | 490 | if(!(nonce = unhex(challenge, &nl))) |
2bead829 | 491 | goto error; |
0227f67d RK |
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) { | |
e9e8a16d | 497 | c->last = "cookie failed and no username"; |
2e9ba080 | 498 | disorder_error(0, "cookie did not work and no username available"); |
2bead829 | 499 | goto error; |
0227f67d RK |
500 | } |
501 | } | |
e9e8a16d RK |
502 | if(!(res = authhash(nonce, nl, password, algorithm))) { |
503 | c->last = "error computing authorization hash"; | |
504 | goto error; | |
505 | } | |
2bead829 | 506 | if((rc = disorder_simple(c, 0, "user", username, res, (char *)0))) |
507 | goto error_rc; | |
460b9539 | 508 | c->user = xstrdup(username); |
f74f4f32 RK |
509 | xfree(res); |
510 | free_strings(nrvec, rvec); | |
511 | xfree(nonce); | |
512 | xfree(sa); | |
513 | xfree(r); | |
460b9539 | 514 | return 0; |
515 | error: | |
2bead829 | 516 | rc = -1; |
517 | error_rc: | |
96f6312a RK |
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); | |
2bead829 | 524 | return rc; |
460b9539 | 525 | } |
526 | ||
fdf98378 | 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) { | |
319d7107 RK |
536 | return disorder_connect_generic(config, |
537 | c, | |
fdf98378 | 538 | username, |
539 | password, | |
540 | 0); | |
541 | } | |
542 | ||
0227f67d RK |
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)) { | |
e9e8a16d | 555 | c->last = "no username"; |
2e9ba080 | 556 | disorder_error(0, "no username configured"); |
0227f67d RK |
557 | return -1; |
558 | } | |
559 | password = config->password; | |
1be9101e RK |
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"; | |
0227f67d RK |
567 | if(!password) { |
568 | /* Oh well */ | |
e9e8a16d | 569 | c->last = "no password"; |
2e9ba080 | 570 | disorder_error(0, "no password configured for user '%s'", username); |
0227f67d RK |
571 | return -1; |
572 | } | |
319d7107 RK |
573 | return disorder_connect_generic(config, |
574 | c, | |
0227f67d RK |
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 | |
70adc86b | 587 | * is needed rather than a complete new login). |
0227f67d RK |
588 | */ |
589 | int disorder_connect_cookie(disorder_client *c, | |
590 | const char *cookie) { | |
319d7107 RK |
591 | return disorder_connect_generic(config, |
592 | c, | |
0227f67d RK |
593 | "guest", |
594 | "", | |
595 | cookie); | |
596 | } | |
597 | ||
c4e2cfdd RK |
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 | */ | |
460b9539 | 605 | int disorder_close(disorder_client *c) { |
606 | int ret = 0; | |
607 | ||
96f6312a RK |
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; | |
f74f4f32 | 614 | xfree(c->ident); |
0227f67d | 615 | c->ident = 0; |
f74f4f32 | 616 | xfree(c->user); |
0227f67d | 617 | c->user = 0; |
375d9478 | 618 | return ret; |
460b9539 | 619 | } |
620 | ||
460b9539 | 621 | static void client_error(const char *msg, |
622 | void attribute((unused)) *u) { | |
2e9ba080 | 623 | disorder_error(0, "error parsing reply: %s", msg); |
460b9539 | 624 | } |
625 | ||
ec9c0462 | 626 | /** @brief Get a single queue entry |
c4e2cfdd | 627 | * @param c Client |
ec9c0462 | 628 | * @param cmd Command |
c4e2cfdd RK |
629 | * @param qp Where to store track information |
630 | * @return 0 on success, non-0 on error | |
c4e2cfdd | 631 | */ |
ec9c0462 RK |
632 | static int onequeue(disorder_client *c, const char *cmd, |
633 | struct queue_entry **qp) { | |
460b9539 | 634 | char *r; |
635 | struct queue_entry *q; | |
2bead829 | 636 | int rc; |
460b9539 | 637 | |
ec9c0462 | 638 | if((rc = disorder_simple(c, &r, cmd, (char *)0))) |
2bead829 | 639 | return rc; |
460b9539 | 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 | ||
0590cedc | 650 | /** @brief Fetch the queue, recent list, etc */ |
c12575c6 RK |
651 | static int readqueue(disorder_client *c, |
652 | struct queue_entry **qp) { | |
460b9539 | 653 | struct queue_entry *qh, **qt = &qh, *q; |
654 | char *l; | |
96f6312a | 655 | char errbuf[1024]; |
460b9539 | 656 | |
96f6312a | 657 | while(inputlines(c->ident, c->input, &l, '\n') >= 0) { |
460b9539 | 658 | if(!strcmp(l, ".")) { |
659 | *qt = 0; | |
660 | *qp = qh; | |
b251ac34 | 661 | xfree(l); |
460b9539 | 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 | } | |
b251ac34 | 669 | xfree(l); |
460b9539 | 670 | } |
96f6312a RK |
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)); | |
e9e8a16d | 674 | } else { |
c12575c6 | 675 | c->last = "input error: unexpected EOF"; |
e9e8a16d | 676 | } |
96f6312a | 677 | disorder_error(0, "%s: %s", c->ident, c->last); |
460b9539 | 678 | return -1; |
679 | } | |
680 | ||
c4e2cfdd RK |
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 | */ | |
460b9539 | 689 | static int readlist(disorder_client *c, char ***vecp, int *nvecp) { |
690 | char *l; | |
691 | struct vector v; | |
96f6312a | 692 | char errbuf[1024]; |
460b9539 | 693 | |
694 | vector_init(&v); | |
96f6312a | 695 | while(inputlines(c->ident, c->input, &l, '\n') >= 0) { |
460b9539 | 696 | if(!strcmp(l, ".")) { |
697 | vector_terminate(&v); | |
698 | if(nvecp) | |
699 | *nvecp = v.nvec; | |
700 | *vecp = v.vec; | |
5d2b941b | 701 | xfree(l); |
460b9539 | 702 | return 0; |
703 | } | |
5d2b941b RK |
704 | vector_append(&v, xstrdup(l + (*l == '.'))); |
705 | xfree(l); | |
460b9539 | 706 | } |
96f6312a RK |
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)); | |
e9e8a16d RK |
710 | } else { |
711 | c->last = "input error: unexpxected EOF"; | |
e9e8a16d | 712 | } |
96f6312a | 713 | disorder_error(0, "%s: %s", c->ident, c->last); |
460b9539 | 714 | return -1; |
715 | } | |
716 | ||
c4e2cfdd RK |
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 | */ | |
460b9539 | 721 | char *disorder_user(disorder_client *c) { |
722 | return c->user; | |
723 | } | |
724 | ||
5dc19ffd | 725 | static void pairlist_error_handler(const char *msg, |
460b9539 | 726 | void attribute((unused)) *u) { |
5dc19ffd | 727 | disorder_error(0, "error handling key-value pair reply: %s", msg); |
460b9539 | 728 | } |
729 | ||
5dc19ffd | 730 | /** @brief Get a list of key-value pairs |
c4e2cfdd | 731 | * @param c Client |
c4e2cfdd | 732 | * @param kp Where to store linked list of preferences |
5dc19ffd RK |
733 | * @param cmd Command |
734 | * @param ... Arguments | |
c4e2cfdd RK |
735 | * @return 0 on success, non-0 on error |
736 | */ | |
5dc19ffd | 737 | static int pairlist(disorder_client *c, struct kvp **kp, const char *cmd, ...) { |
460b9539 | 738 | char **vec, **pvec; |
2bead829 | 739 | int nvec, npvec, n, rc; |
460b9539 | 740 | struct kvp *k; |
5dc19ffd | 741 | va_list ap; |
460b9539 | 742 | |
5dc19ffd RK |
743 | va_start(ap, cmd); |
744 | rc = disorder_simple_v(c, 0, cmd, ap); | |
745 | va_end(ap); | |
746 | if(rc) | |
2bead829 | 747 | return rc; |
5dc19ffd RK |
748 | if((rc = readlist(c, &vec, &nvec))) |
749 | return rc; | |
460b9539 | 750 | for(n = 0; n < nvec; ++n) { |
5dc19ffd | 751 | if(!(pvec = split(vec[n], &npvec, SPLIT_QUOTES, pairlist_error_handler, 0))) |
460b9539 | 752 | return -1; |
753 | if(npvec != 2) { | |
5dc19ffd | 754 | pairlist_error_handler("malformed response", 0); |
460b9539 | 755 | return -1; |
756 | } | |
757 | *kp = k = xmalloc(sizeof *k); | |
758 | k->name = pvec[0]; | |
759 | k->value = pvec[1]; | |
760 | kp = &k->next; | |
0d047a12 | 761 | xfree(pvec); |
460b9539 | 762 | } |
0d047a12 | 763 | free_strings(nvec, vec); |
460b9539 | 764 | *kp = 0; |
765 | return 0; | |
766 | } | |
767 | ||
9e42afcd RK |
768 | #if _WIN32 |
769 | # define boolean bodge_boolean | |
770 | #endif | |
771 | ||
c4e2cfdd RK |
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 | */ | |
460b9539 | 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 { | |
2e9ba080 | 783 | disorder_error(0, "malformed response to '%s'", cmd); |
460b9539 | 784 | return -1; |
785 | } | |
786 | return 0; | |
787 | } | |
788 | ||
c4e2cfdd RK |
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 | */ | |
460b9539 | 794 | int disorder_log(disorder_client *c, struct sink *s) { |
795 | char *l; | |
2bead829 | 796 | int rc; |
96f6312a | 797 | char errbuf[1024]; |
460b9539 | 798 | |
2bead829 | 799 | if((rc = disorder_simple(c, 0, "log", (char *)0))) |
800 | return rc; | |
96f6312a | 801 | while(inputlines(c->ident, c->input, &l, '\n') >= 0 && strcmp(l, ".")) |
460b9539 | 802 | if(sink_printf(s, "%s\n", l) < 0) return -1; |
96f6312a | 803 | if(source_err(c->input)) { |
e9e8a16d | 804 | byte_xasprintf((char **)&c->last, "input error: %s", |
96f6312a RK |
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"); | |
e9e8a16d RK |
809 | return -1; |
810 | } | |
9e42afcd | 811 | |
460b9539 | 812 | return 0; |
813 | } | |
814 | ||
7788b7c7 RK |
815 | #include "client-stubs.c" |
816 | ||
460b9539 | 817 | /* |
818 | Local Variables: | |
819 | c-basic-offset:2 | |
820 | comment-column:40 | |
821 | End: | |
822 | */ |