Merge branch 'crypt' into next
[paraslash.git] / blob.c
1 /*
2 * Copyright (C) 2007-2009 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file blob.c Macros and functions for blob handling. */
8
9 #include <fnmatch.h>
10 #include <openssl/rc4.h>
11
12 #include "para.h"
13 #include "error.h"
14 #include "crypt.h"
15 #include "string.h"
16 #include "afh.h"
17 #include "afs.h"
18 #include "net.h"
19 #include "ipc.h"
20
21 static struct osl_column_description blob_cols[] = {
22 [BLOBCOL_ID] = {
23 .storage_type = OSL_MAPPED_STORAGE,
24 .storage_flags = OSL_RBTREE | OSL_UNIQUE | OSL_FIXED_SIZE,
25 .name = "id",
26 .data_size = 4,
27 .compare_function = uint32_compare
28 },
29 [BLOBCOL_NAME] = {
30 .storage_type = OSL_MAPPED_STORAGE,
31 .storage_flags = OSL_RBTREE | OSL_UNIQUE,
32 .name = "name",
33 .compare_function = string_compare
34 },
35 [BLOBCOL_DEF] = {
36 .storage_type = OSL_DISK_STORAGE,
37 .storage_flags = 0,
38 .name = "definition"
39 }
40 };
41
42 /** \cond doxygen isn't smart enough to recognize these */
43 INIT_BLOB_TABLE(lyrics);
44 INIT_BLOB_TABLE(images);
45 INIT_BLOB_TABLE(moods);
46 INIT_BLOB_TABLE(playlists);
47 /** \endcond */
48
49 /** Flags that may be passed to the \p ls functions of each blob type. */
50 enum blob_ls_flags {
51 /** List both id and name. */
52 BLOB_LS_FLAG_LONG = 1,
53 /** Reverse sort order. */
54 BLOB_LS_FLAG_REVERSE = 2,
55 /** Sort by id instead of name. */
56 BLOB_LS_FLAG_SORT_BY_ID = 4,
57 };
58
59 /** Structure passed to the \p print_blob function. */
60 struct lsblob_action_data {
61 /** The flags given at the command line. */
62 uint32_t flags;
63 /** Message buffer. */
64 struct para_buffer pb;
65 };
66
67 static int print_blob(struct osl_table *table, struct osl_row *row,
68 const char *name, void *data)
69 {
70 struct lsblob_action_data *lbad = data;
71 struct osl_object obj;
72 uint32_t id;
73 int ret;
74
75 if (!(lbad->flags & BLOB_LS_FLAG_LONG))
76 return para_printf(&lbad->pb, "%s\n", name);
77 ret = osl_get_object(table, row, BLOBCOL_ID, &obj);
78 if (ret < 0) {
79 para_printf(&lbad->pb, "%s: %s\n", name, para_strerror(-ret));
80 return ret;
81 }
82 id = *(uint32_t *)obj.data;
83 return para_printf(&lbad->pb, "%u\t%s\n", id, name);
84 }
85
86 static void com_lsblob_callback(struct osl_table *table,
87 int fd, const struct osl_object *query)
88 {
89 struct lsblob_action_data lbad = {
90 .flags = *(uint32_t *)query->data,
91 .pb = {
92 .max_size = SHMMAX,
93 .private_data = &fd,
94 .max_size_handler = pass_buffer_as_shm
95 }
96 };
97 struct pattern_match_data pmd = {
98 .table = table,
99 .patterns = {.data = (char *)query->data + sizeof(uint32_t),
100 .size = query->size - sizeof(uint32_t)},
101 .pm_flags = PM_NO_PATTERN_MATCHES_EVERYTHING | PM_SKIP_EMPTY_NAME,
102 .match_col_num = BLOBCOL_NAME,
103 .data = &lbad,
104 .action = print_blob,
105 };
106 int ret;
107
108 if (lbad.flags & BLOB_LS_FLAG_REVERSE)
109 pmd.pm_flags |= PM_REVERSE_LOOP;
110 if (!(lbad.flags & BLOB_LS_FLAG_SORT_BY_ID))
111 pmd.loop_col_num = BLOBCOL_NAME;
112 else
113 pmd.loop_col_num = BLOBCOL_ID;
114 ret = for_each_matching_row(&pmd);
115 if (ret < 0)
116 para_printf(&lbad.pb, "%s\n", para_strerror(-ret));
117 if (lbad.pb.offset)
118 pass_buffer_as_shm(lbad.pb.buf, lbad.pb.offset, &fd);
119 free(lbad.pb.buf);
120 }
121
122 static int com_lsblob(callback_function *f, struct rc4_context *rc4c, int argc, char * const * const argv)
123 {
124 uint32_t flags = 0;
125 struct osl_object options = {.data = &flags, .size = sizeof(flags)};
126 int i;
127
128 for (i = 1; i < argc; i++) {
129 const char *arg = argv[i];
130 if (arg[0] != '-')
131 break;
132 if (!strcmp(arg, "--")) {
133 i++;
134 break;
135 }
136 if (!strcmp(arg, "-l")) {
137 flags |= BLOB_LS_FLAG_LONG;
138 continue;
139 }
140 if (!strcmp(arg, "-i")) {
141 flags |= BLOB_LS_FLAG_SORT_BY_ID;
142 continue;
143 }
144 if (!strcmp(arg, "-r")) {
145 flags |= BLOB_LS_FLAG_REVERSE;
146 continue;
147 }
148 break;
149 }
150 // if (argc > i)
151 // return -E_BLOB_SYNTAX;
152 return send_option_arg_callback_request(&options, argc - i,
153 argv + i, f, rc4_send_result, rc4c);
154 }
155
156 static int cat_blob(struct osl_table *table, struct osl_row *row,
157 __a_unused const char *name, void *data)
158 {
159 int ret = 0, ret2;
160 struct osl_object obj;
161
162 ret = osl_open_disk_object(table, row, BLOBCOL_DEF, &obj);
163 if (ret < 0)
164 return ret;
165 if (obj.size)
166 ret = pass_buffer_as_shm(obj.data, obj.size, data);
167 ret2 = osl_close_disk_object(&obj);
168 return (ret < 0)? ret : ret2;
169 }
170
171 static void com_catblob_callback(struct osl_table *table, int fd,
172 const struct osl_object *query)
173 {
174 struct pattern_match_data pmd = {
175 .table = table,
176 .patterns = *query,
177 .loop_col_num = BLOBCOL_NAME,
178 .match_col_num = BLOBCOL_NAME,
179 .pm_flags = PM_SKIP_EMPTY_NAME,
180 .data = &fd,
181 .action = cat_blob
182 };
183 for_each_matching_row(&pmd);
184 }
185
186 static int com_catblob(callback_function *f, struct rc4_context *rc4c, int argc,
187 char * const * const argv)
188 {
189 if (argc < 2)
190 return -E_BLOB_SYNTAX;
191 return send_standard_callback_request(argc - 1, argv + 1, f,
192 rc4_send_result, rc4c);
193 }
194
195 /** Used for removing rows from a blob table. */
196 struct rmblob_data {
197 /** Message buffer. */
198 struct para_buffer pb;
199 /** Number of removed blobs. */
200 unsigned num_removed;
201 };
202
203 static int remove_blob(struct osl_table *table, struct osl_row *row,
204 const char *name, void *data)
205 {
206 struct rmblob_data *rmbd = data;
207 int ret = osl_del_row(table, row);
208 if (ret < 0) {
209 para_printf(&rmbd->pb, "%s: %s\n", name, para_strerror(-ret));
210 return ret;
211 }
212 rmbd->num_removed++;
213 return 1;
214 }
215
216 static void com_rmblob_callback(struct osl_table *table, int fd,
217 const struct osl_object *query)
218 {
219 int ret, ret2 = 0;
220 struct rmblob_data rmbd = {
221 .num_removed = 0,
222 .pb = {
223 .max_size = SHMMAX,
224 .private_data = &fd,
225 .max_size_handler = pass_buffer_as_shm
226 }
227 };
228 struct pattern_match_data pmd = {
229 .table = table,
230 .patterns = *query,
231 .loop_col_num = BLOBCOL_NAME,
232 .match_col_num = BLOBCOL_NAME,
233 .pm_flags = PM_SKIP_EMPTY_NAME,
234 .data = &rmbd,
235 .action = remove_blob
236 };
237 ret = for_each_matching_row(&pmd);
238 if (ret < 0) {
239 ret2 = para_printf(&rmbd.pb, "%s\n", para_strerror(-ret));
240 if (ret2 < 0)
241 goto out;
242 }
243 if (!rmbd.num_removed)
244 ret2 = para_printf(&rmbd.pb, "no matches, nothing removed\n");
245 else {
246 ret2 = para_printf(&rmbd.pb, "removed %d blobs\n", rmbd.num_removed);
247 afs_event(BLOB_RENAME, NULL, table);
248 }
249 out:
250 if (ret2 >= 0 && rmbd.pb.offset)
251 pass_buffer_as_shm(rmbd.pb.buf, rmbd.pb.offset, &fd);
252 free(rmbd.pb.buf);
253 }
254
255 static int com_rmblob(callback_function *f, struct rc4_context *rc4c, int argc,
256 char * const * const argv)
257 {
258 if (argc < 2)
259 return -E_MOOD_SYNTAX;
260 return send_option_arg_callback_request(NULL, argc - 1, argv + 1, f,
261 rc4_send_result, rc4c);
262 }
263
264 static void com_addblob_callback(struct osl_table *table, __a_unused int fd,
265 const struct osl_object *query)
266 {
267 struct osl_object objs[NUM_BLOB_COLUMNS];
268 char *name = query->data;
269 size_t name_len = strlen(name) + 1;
270 uint32_t id;
271 unsigned num_rows;
272 int ret;
273
274 ret = osl_get_num_rows(table, &num_rows);
275 if (ret < 0)
276 goto out;
277 if (!num_rows) { /* this is the first entry ever added */
278 /* insert dummy row containing the id */
279 id = 2; /* this entry will be entry #1, so 2 is the next */
280 objs[BLOBCOL_ID].data = &id;
281 objs[BLOBCOL_ID].size = sizeof(id);
282 objs[BLOBCOL_NAME].data = "";
283 objs[BLOBCOL_NAME].size = 1;
284 objs[BLOBCOL_DEF].data = "";
285 objs[BLOBCOL_DEF].size = 1;
286 ret = osl_add_row(table, objs);
287 if (ret < 0)
288 goto out;
289 } else {
290 /* check if name already exists */
291 struct osl_row *row;
292 struct osl_object obj = {.data = name, .size = name_len};
293 ret = osl_get_row(table, BLOBCOL_NAME, &obj, &row);
294 if (ret < 0 && ret != -E_RB_KEY_NOT_FOUND)
295 goto out;
296 if (ret >= 0) { /* we already have a blob with this name */
297 obj.data = name + name_len;
298 obj.size = query->size - name_len;
299 ret = osl_update_object(table, row, BLOBCOL_DEF, &obj);
300 goto out;
301 }
302 /* new blob, get id of the dummy row and increment it */
303 obj.data = "";
304 obj.size = 1;
305 ret = osl_get_row(table, BLOBCOL_NAME, &obj, &row);
306 if (ret < 0)
307 goto out;
308 ret = osl_get_object(table, row, BLOBCOL_ID, &obj);
309 if (ret < 0)
310 goto out;
311 id = *(uint32_t *)obj.data + 1;
312 obj.data = &id;
313 ret = osl_update_object(table, row, BLOBCOL_ID, &obj);
314 if (ret < 0)
315 goto out;
316 }
317 id--;
318 objs[BLOBCOL_ID].data = &id;
319 objs[BLOBCOL_ID].size = sizeof(id);
320 objs[BLOBCOL_NAME].data = name;
321 objs[BLOBCOL_NAME].size = name_len;
322 objs[BLOBCOL_DEF].data = name + name_len;
323 objs[BLOBCOL_DEF].size = query->size - name_len;
324 ret = osl_add_row(table, objs);
325 if (ret < 0)
326 goto out;
327 afs_event(BLOB_ADD, NULL, table);
328 out:
329 if (ret < 0)
330 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
331 }
332
333 /*
334 * write input from fd to dynamically allocated buffer,
335 * but maximal max_size byte.
336 */
337 static int fd2buf(struct rc4_context *rc4c, unsigned max_size, struct osl_object *obj)
338 {
339 const size_t chunk_size = 1024;
340 size_t size = 2048, received = 0;
341 int ret;
342 char *buf = para_malloc(size);
343
344 for (;;) {
345 ret = rc4_recv_bin_buffer(rc4c, buf + received, chunk_size);
346 if (ret <= 0)
347 break;
348 received += ret;
349 if (received + chunk_size >= size) {
350 size *= 2;
351 ret = -E_INPUT_TOO_LARGE;
352 if (size > max_size)
353 break;
354 buf = para_realloc(buf, size);
355 }
356 }
357 obj->data = buf;
358 obj->size = received;
359 if (ret < 0)
360 free(buf);
361 return ret;
362 }
363
364 /*
365 * Read data from a file descriptor, and send it to the afs process.
366 *
367 * \param rc4c crypt context containing the file descriptor to read data from.
368 * \param arg_obj Pointer to the arguments to \a f.
369 * \param f The callback function.
370 * \param max_len Don't read more than that many bytes from stdin.
371 * \param result_handler See \ref send_callback_request.
372 * \param private_result_data See \ref send_callback_request.
373 *
374 * This function is used by commands that wish to let para_server store
375 * arbitrary data specified by the user (for instance the add_blob family of
376 * commands). First, at most \a max_len bytes are read and decrypted from the
377 * file descriptor given by \a rc4c. The result is concatenated with the buffer
378 * given by \a arg_obj, and the combined buffer is made available to the afs
379 * process via the callback method. See \ref send_callback_request for details.
380 *
381 * \return Negative on errors, the return value of the underlying call to
382 * send_callback_request() otherwise.
383 */
384 static int stdin_command(struct rc4_context *rc4c, struct osl_object *arg_obj,
385 callback_function *f, unsigned max_len,
386 callback_result_handler *result_handler,
387 void *private_result_data)
388 {
389 struct osl_object query, stdin_obj;
390 int ret;
391
392 ret = rc4_send_buffer(rc4c, AWAITING_DATA_MSG);
393 if (ret < 0)
394 return ret;
395 ret = fd2buf(rc4c, max_len, &stdin_obj);
396 if (ret < 0)
397 return ret;
398 query.size = arg_obj->size + stdin_obj.size;
399 query.data = para_malloc(query.size);
400 memcpy(query.data, arg_obj->data, arg_obj->size);
401 memcpy((char *)query.data + arg_obj->size, stdin_obj.data, stdin_obj.size);
402 free(stdin_obj.data);
403 ret = send_callback_request(f, &query, result_handler, private_result_data);
404 free(query.data);
405 return ret;
406 }
407
408 static int com_addblob(callback_function *f, struct rc4_context *rc4c, int argc,
409 char * const * const argv)
410 {
411 struct osl_object arg_obj;
412
413 if (argc != 2)
414 return -E_BLOB_SYNTAX;
415 if (!*argv[1]) /* empty name is reserved for the dummy row */
416 return -E_BLOB_SYNTAX;
417 arg_obj.size = strlen(argv[1]) + 1;
418 arg_obj.data = (char *)argv[1];
419 return stdin_command(rc4c, &arg_obj, f, 10 * 1024 * 1024, NULL, NULL);
420 }
421
422 /* FIXME: Print output to client, not to log file */
423 static void com_mvblob_callback(struct osl_table *table, __a_unused int fd,
424 const struct osl_object *query)
425 {
426 char *src = (char *) query->data;
427 struct osl_object obj = {.data = src, .size = strlen(src) + 1};
428 char *dest = src + obj.size;
429 struct osl_row *row;
430 int ret = osl_get_row(table, BLOBCOL_NAME, &obj, &row);
431
432 if (ret < 0)
433 goto out;
434 obj.data = dest;
435 obj.size = strlen(dest) + 1;
436 ret = osl_update_object(table, row, BLOBCOL_NAME, &obj);
437 if (ret < 0)
438 goto out;
439 afs_event(BLOB_RENAME, NULL, table);
440 out:
441 if (ret < 0)
442 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
443 }
444
445 static int com_mvblob(callback_function *f, __a_unused struct rc4_context *rc4c,
446 int argc, char * const * const argv)
447 {
448 if (argc != 3)
449 return -E_MOOD_SYNTAX;
450 return send_option_arg_callback_request(NULL, argc - 1, argv + 1, f,
451 NULL, NULL);
452 }
453
454 #define DEFINE_BLOB_COMMAND(cmd_name, table_name, cmd_prefix) \
455 static void com_ ## cmd_name ## cmd_prefix ## _callback(int fd, const struct osl_object *query) \
456 { \
457 return com_ ## cmd_name ## blob_callback(table_name ## _table, fd, query); \
458 } \
459 int com_ ## cmd_name ## cmd_prefix(struct rc4_context *rc4c, int argc, char * const * const argv) \
460 { \
461 return com_ ## cmd_name ## blob(com_ ## cmd_name ## cmd_prefix ## _callback, rc4c, argc, argv); \
462 }
463
464 static int blob_get_name_by_id(struct osl_table *table, uint32_t id,
465 char **name)
466 {
467 struct osl_row *row;
468 struct osl_object obj = {.data = &id, .size = sizeof(id)};
469 int ret;
470
471 *name = NULL;
472 if (!id)
473 return 1;
474 ret = osl_get_row(table, BLOBCOL_ID, &obj, &row);
475 if (ret < 0)
476 return ret;
477 ret = osl_get_object(table, row, BLOBCOL_NAME, &obj);
478 if (ret < 0)
479 return ret;
480 *name = (char *)obj.data;
481 return 1;
482 }
483
484 /** Define the \p get_name_by_id function for this blob type. */
485 #define DEFINE_GET_NAME_BY_ID(table_name, cmd_prefix) \
486 int cmd_prefix ## _get_name_by_id(uint32_t id, char **name) \
487 { \
488 return blob_get_name_by_id(table_name ## _table, id, name); \
489 }
490
491
492 static int blob_get_def_by_name(struct osl_table *table, char *name,
493 struct osl_object *def)
494 {
495 struct osl_row *row;
496 struct osl_object obj = {.data = name, .size = strlen(name) + 1};
497 int ret;
498
499 def->data = NULL;
500 if (!*name)
501 return 1;
502 ret = osl_get_row(table, BLOBCOL_NAME, &obj, &row);
503 if (ret < 0)
504 return ret;
505 return osl_open_disk_object(table, row, BLOBCOL_DEF, def);
506 }
507
508 /** Define the \p get_def_by_id function for this blob type. */
509 #define DEFINE_GET_DEF_BY_NAME(table_name, cmd_prefix) \
510 int cmd_prefix ## _get_def_by_name(char *name, struct osl_object *def) \
511 { \
512 return blob_get_def_by_name(table_name ## _table, name, def); \
513 }
514
515 static int blob_get_def_by_id(struct osl_table *table, uint32_t id,
516 struct osl_object *def)
517 {
518 struct osl_row *row;
519 struct osl_object obj = {.data = &id, .size = sizeof(id)};
520 int ret;
521
522 def->data = NULL;
523 if (!id)
524 return 1;
525 ret = osl_get_row(table, BLOBCOL_ID, &obj, &row);
526 if (ret < 0)
527 return ret;
528 return osl_open_disk_object(table, row, BLOBCOL_DEF, def);
529 }
530
531 /** Define the \p get_def_by_id function for this blob type. */
532 #define DEFINE_GET_DEF_BY_ID(table_name, cmd_prefix) \
533 int cmd_prefix ## _get_def_by_id(uint32_t id, struct osl_object *def) \
534 { \
535 return blob_get_def_by_id(table_name ## _table, id, def); \
536 }
537
538 static int blob_get_name_and_def_by_row(struct osl_table *table,
539 const struct osl_row *row, char **name, struct osl_object *def)
540 {
541 struct osl_object obj;
542 int ret = osl_get_object(table, row, BLOBCOL_NAME, &obj);
543 if (ret < 0)
544 return ret;
545 *name = obj.data;
546 return osl_open_disk_object(table, row, BLOBCOL_DEF, def);
547 }
548 /** Define the \p get_name_and_def_by_row function for this blob type. */
549 #define DEFINE_GET_NAME_AND_DEF_BY_ROW(table_name, cmd_prefix) \
550 int cmd_prefix ## _get_name_and_def_by_row(const struct osl_row *row, \
551 char **name, struct osl_object *def) \
552 { \
553 return blob_get_name_and_def_by_row(table_name ## _table, \
554 row, name, def); \
555 }
556
557 /** Define the \p close function for this blob type. */
558 #define DEFINE_BLOB_CLOSE(table_name) \
559 void table_name ## _close(void) \
560 { \
561 osl_close_table(table_name ## _table, OSL_MARK_CLEAN); \
562 table_name ## _table = NULL; \
563 }
564
565 /** Define the \p create function for this blob type. */
566 #define DEFINE_BLOB_CREATE(table_name) \
567 int table_name ## _create(const char *dir) \
568 { \
569 table_name ## _table_desc.dir = dir; \
570 return osl_create_table(&table_name ## _table_desc); \
571 }
572
573 static int blob_open(struct osl_table **table,
574 struct osl_table_description *desc,
575 const char *dir)
576 {
577 int ret;
578 desc->dir = dir;
579 ret = osl_open_table(desc, table);
580 if (ret >= 0)
581 return ret;
582 *table = NULL;
583 if (ret >= 0 || is_errno(-ret, ENOENT))
584 return 1;
585 return ret;
586 }
587
588 #define DEFINE_BLOB_OPEN(table_name) \
589 int table_name ## _open(const char *dir) \
590 { \
591 return blob_open(&table_name ## _table, \
592 &table_name ## _table_desc, dir); \
593 }
594
595
596 /** Define the \p init function for this blob type. */
597 #define DEFINE_BLOB_INIT(table_name) \
598 void table_name ## _init(struct afs_table *t) \
599 { \
600 t->name = table_name ## _table_desc.name; \
601 t->open = table_name ## _open; \
602 t->close = table_name ## _close; \
603 t->create = table_name ## _create;\
604 t->event_handler = table_name ##_event_handler; \
605 table_name ## _table = NULL; \
606 }
607
608
609 /** Define all functions for this blob type. */
610 #define DEFINE_BLOB_FUNCTIONS(table_name, cmd_prefix) \
611 DEFINE_BLOB_OPEN(table_name) \
612 DEFINE_BLOB_CLOSE(table_name) \
613 DEFINE_BLOB_CREATE(table_name) \
614 DEFINE_BLOB_INIT(table_name) \
615 DEFINE_BLOB_COMMAND(ls, table_name, cmd_prefix) \
616 DEFINE_BLOB_COMMAND(cat, table_name, cmd_prefix) \
617 DEFINE_BLOB_COMMAND(add, table_name, cmd_prefix) \
618 DEFINE_BLOB_COMMAND(rm, table_name, cmd_prefix) \
619 DEFINE_BLOB_COMMAND(mv, table_name, cmd_prefix) \
620 DEFINE_GET_NAME_BY_ID(table_name, cmd_prefix); \
621 DEFINE_GET_DEF_BY_ID(table_name, cmd_prefix); \
622 DEFINE_GET_DEF_BY_NAME(table_name, cmd_prefix); \
623 DEFINE_GET_NAME_AND_DEF_BY_ROW(table_name, cmd_prefix); \
624
625 /** \cond doxygen isn't smart enough to recognize these */
626 DEFINE_BLOB_FUNCTIONS(lyrics, lyr);
627 DEFINE_BLOB_FUNCTIONS(images, img);
628 DEFINE_BLOB_FUNCTIONS(moods, mood);
629 DEFINE_BLOB_FUNCTIONS(playlists, pl);
630 /** \endcond */