client: Do not leak buffer tree node on exit.
[paraslash.git] / attribute.c
1 /*
2  * Copyright (C) 1997-2011 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file attribute.c Attribute handling functions. */
8
9 #include <regex.h>
10 #include <osl.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 "ipc.h"
19
20 static struct osl_table *attribute_table;
21 static int greatest_att_bitnum;
22
23 /** The columns of the attribute table. */
24 enum attribute_table_columns {
25         /** The bit number (0-63). */
26         ATTCOL_BITNUM,
27         /** The name of the attribute. */
28         ATTCOL_NAME,
29         /** Number of columns in this table. */
30         NUM_ATT_COLUMNS
31 };
32
33 static int char_compare(const struct osl_object *obj1, const struct osl_object *obj2)
34 {
35         const unsigned char *c1 = (const unsigned char*)obj1->data;
36         const unsigned char *c2 = (const unsigned char*)obj2->data;
37         if (*c1 > *c2)
38                 return 1;
39         if (*c1 < *c2)
40                 return -1;
41         return 0;
42 }
43
44 static struct osl_column_description att_cols[] = {
45         [ATTCOL_BITNUM] = {
46                 .storage_type = OSL_MAPPED_STORAGE,
47                 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
48                 .name = "bitnum",
49                 .compare_function = char_compare,
50                 .data_size = 1
51         },
52         [ATTCOL_NAME] = {
53                 .storage_type = OSL_MAPPED_STORAGE,
54                 .storage_flags = OSL_RBTREE | OSL_UNIQUE,
55                 .name = "name",
56                 .compare_function = string_compare,
57         }
58 };
59
60 static struct osl_table_description attribute_table_desc = {
61         .name = "attributes",
62         .num_columns = NUM_ATT_COLUMNS,
63         .flags = 0,
64         .column_descriptions = att_cols
65 };
66
67 static void find_greatest_att_bitnum(void)
68 {
69         unsigned char c = 63;
70         do {
71                 struct osl_row *row;
72                 struct osl_object obj = {.data = &c, .size = 1};
73                 if (osl_get_row(attribute_table, ATTCOL_BITNUM, &obj,
74                                 &row) >= 0) {
75                         greatest_att_bitnum = c;
76                         return;
77                 }
78         } while (c--);
79         PARA_INFO_LOG("no attributes\n");
80         greatest_att_bitnum = -E_NO_ATTRIBUTES;
81 }
82
83 /**
84  * Retrieve the identifier (number) of an attribute.
85  *
86  * \param att_name The name of the attribute.
87  * \param bitnum Result pointer.
88  *
89  * \return Positive on success, negative on errors.
90  */
91 int get_attribute_bitnum_by_name(const char *att_name, unsigned char *bitnum)
92 {
93         struct osl_object obj = {.data = (char *)att_name,
94                 .size = strlen(att_name) + 1};
95         struct osl_row *row;
96         int ret = osl(osl_get_row(attribute_table, ATTCOL_NAME, &obj, &row));
97
98         if (ret < 0)
99                 return ret;
100         ret = osl(osl_get_object(attribute_table, row, ATTCOL_BITNUM, &obj));
101         if (ret < 0)
102                 return ret;
103         *bitnum = *(unsigned char *)obj.data;
104         return 1;
105 }
106
107 /**
108  * Flags used by the lsatt command.
109  *
110  * \param \sa com_lsatt().
111  */
112 enum lsatt_flags {
113         /** Whether "-a" was given for the lsatt command. */
114         LSATT_FLAG_SORT_BY_ID = 1,
115         /** Whether "-l" was given for the lsatt command. */
116         LSATT_FLAG_LONG = 2,
117         /** Reverse sort order. */
118         LSATT_FLAG_REVERSE = 4
119 };
120
121 /** Data passed to the action function of lsatt */
122 struct lsatt_action_data {
123         /** The result buffer. */
124         struct para_buffer pb;
125         /** The given flags for the lsatt command. */
126         unsigned flags;
127 };
128
129 static int print_attribute(struct osl_table *table, struct osl_row *row,
130                 const char *name, void *data)
131 {
132         struct lsatt_action_data *laad = data;
133         struct osl_object bitnum_obj;
134         int ret;
135
136         if (!(laad->flags & LSATT_FLAG_LONG))
137                 return para_printf(&laad->pb, "%s\n", name);
138         ret = osl(osl_get_object(table, row, ATTCOL_BITNUM, &bitnum_obj));
139         if (ret < 0) {
140                 para_printf(&laad->pb, "%s: %s\n", name, para_strerror(-ret));
141                 return ret;
142         }
143         return para_printf(&laad->pb, "%u\t%s\n", *(unsigned char*)bitnum_obj.data,
144                 name);
145 }
146
147 static void com_lsatt_callback(int fd, const struct osl_object *query)
148 {
149         struct lsatt_action_data laad = {
150                 .flags = *(unsigned *) query->data,
151                 .pb = {
152                         .max_size = SHMMAX,
153                         .private_data = &fd,
154                         .max_size_handler = pass_buffer_as_shm
155                 }
156
157         };
158         struct pattern_match_data pmd = {
159                 .table = attribute_table,
160                 .loop_col_num = ATTCOL_BITNUM,
161                 .match_col_num = ATTCOL_NAME,
162                 .patterns = {.data = (char *)query->data + sizeof(laad.flags),
163                         .size = query->size - sizeof(laad.flags)},
164                 .pm_flags = PM_NO_PATTERN_MATCHES_EVERYTHING,
165                 .data = &laad,
166                 .action = print_attribute
167         };
168         if (laad.flags & LSATT_FLAG_SORT_BY_ID)
169                 pmd.loop_col_num = ATTCOL_NAME;
170         if (laad.flags & LSATT_FLAG_REVERSE)
171                 pmd.pm_flags |= PM_REVERSE_LOOP;
172         for_each_matching_row(&pmd);
173         if (laad.pb.offset)
174                 pass_buffer_as_shm(laad.pb.buf, laad.pb.offset, &fd);
175         free(laad.pb.buf);
176 }
177
178 int com_lsatt(struct stream_cipher_context *scc, int argc, char * const * const argv)
179 {
180         unsigned flags = 0;
181         struct osl_object options = {.data = &flags, .size = sizeof(flags)};
182         int ret, i;
183
184         for (i = 1; i < argc; i++) {
185                 const char *arg = argv[i];
186                 if (arg[0] != '-')
187                         break;
188                 if (!strcmp(arg, "--")) {
189                         i++;
190                         break;
191                 }
192                 if (!strcmp(arg, "-i")) {
193                         flags |= LSATT_FLAG_SORT_BY_ID;
194                         continue;
195                 }
196                 if (!strcmp(arg, "-l")) {
197                         flags |= LSATT_FLAG_LONG;
198                         continue;
199                 }
200                 if (!strcmp(arg, "-r")) {
201                         flags |= LSATT_FLAG_REVERSE;
202                         continue;
203                 }
204         }
205         ret = send_option_arg_callback_request(&options, argc - i, argv + i,
206                 com_lsatt_callback, sc_send_result, scc);
207         if (!ret) {
208                 if (argc > 1)
209                         ret = sc_send_va_buffer(scc, "no matches\n");
210         } else if (ret < 0)
211                 sc_send_va_buffer(scc, "%s\n", para_strerror(-ret));
212         return ret;
213 }
214
215 static void com_setatt_callback(__a_unused int fd, const struct osl_object *query)
216 {
217         char *p;
218         uint64_t add_mask = 0, del_mask = 0;
219         int ret;
220         size_t len;
221         struct osl_object obj;
222         struct osl_row *row;
223
224         for (p = query->data; p < (char *)query->data + query->size; p += len + 1) {
225                 char c;
226
227                 len = strlen(p);
228                 ret = -E_ATTR_SYNTAX;
229                 if (!*p)
230                         goto out;
231                 c = p[len - 1];
232                 if (c != '+' && c != '-')
233                         break;
234                 p[len - 1] = '\0';
235                 obj.data = p;
236                 obj.size = len + 1;
237                 ret = osl(osl_get_row(attribute_table, ATTCOL_NAME, &obj, &row));
238                 if (ret < 0)
239                         goto out;
240                 ret = osl(osl_get_object(attribute_table, row, ATTCOL_BITNUM,
241                         &obj));
242                 if (ret < 0)
243                         goto out;
244                 if (c == '+')
245                         add_mask |= (1UL << *(unsigned char *)obj.data);
246                 else
247                         del_mask |= (1UL << *(unsigned char *)obj.data);
248         }
249         ret = -E_ATTR_SYNTAX;
250         if (!add_mask && !del_mask)
251                 goto out;
252         PARA_DEBUG_LOG("masks: %llx:%llx\n",(long long unsigned)add_mask,
253                 (long long unsigned)del_mask);
254         for (; p < (char *)query->data + query->size; p += len + 1) { /* TODO: fnmatch */
255                 struct afs_info old_afsi, new_afsi;
256                 struct afsi_change_event_data aced = {.old_afsi = &old_afsi};
257
258                 len = strlen(p);
259                 ret = aft_get_row_of_path(p, &aced.aft_row);
260                 if (ret < 0)
261                         goto out;
262                 ret = get_afsi_object_of_row(aced.aft_row, &obj);
263                 if (ret < 0)
264                         goto out;
265                 ret = load_afsi(&old_afsi, &obj);
266                 if (ret < 0)
267                         goto out;
268                 new_afsi = old_afsi;
269                 new_afsi.attributes |= add_mask;
270                 new_afsi.attributes &= ~del_mask;
271                 save_afsi(&new_afsi, &obj); /* in-place update */
272                 afs_event(AFSI_CHANGE, NULL, &aced);
273         }
274 out:
275         if (ret < 0)
276                 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
277 }
278
279 int com_setatt(__a_unused struct stream_cipher_context *scc, int argc, char * const * const argv)
280 {
281         if (argc < 3)
282                 return -E_ATTR_SYNTAX;
283         return send_standard_callback_request(argc - 1, argv + 1, com_setatt_callback,
284                 NULL, NULL);
285 }
286
287 struct addatt_event_data {
288         const char *name;
289         unsigned char bitnum;
290 };
291
292
293 static void com_addatt_callback(int fd, const struct osl_object *query)
294 {
295         char *p;
296         int ret = 1, ret2 = 0;
297         struct para_buffer pb = {
298                 .max_size = SHMMAX,
299                 .private_data = &fd,
300                 .max_size_handler = pass_buffer_as_shm
301         };
302         size_t len;
303
304         for (p = query->data; p < (char *)query->data + query->size; p += len + 1) {
305                 struct osl_object objs[NUM_ATT_COLUMNS];
306                 struct osl_row *row;
307                 unsigned char bitnum;
308                 struct addatt_event_data aed;
309
310                 len = strlen(p);
311                 if (!len || p[len - 1] == '-' || p[len - 1] == '+') {
312                         ret2 = para_printf(&pb, "invalid attribute name: %s\n", p);
313                         if (ret2 < 0)
314                                 goto out;
315                         continue;
316                 }
317                 ret = get_attribute_bitnum_by_name(p, &bitnum);
318                 if (ret >= 0) {
319                         ret2 = para_printf(&pb, "attribute \"%s\" already exists\n", p);
320                         if (ret2 < 0)
321                                 goto out;
322                         continue;
323                 }
324                 if (ret != -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND)) /* error */
325                         goto out;
326                 objs[ATTCOL_BITNUM].size = 1;
327                 /* find smallest unused attribute */
328                 for (bitnum = 0; bitnum < 64; bitnum++) {
329                         objs[ATTCOL_BITNUM].data = &bitnum;
330                         ret = osl(osl_get_row(attribute_table, ATTCOL_BITNUM,
331                                 &objs[ATTCOL_BITNUM], &row));
332                         if (ret == -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND))
333                                 break; /* this bitnum is unused, use it */
334                         if (ret < 0) /* error */
335                                 goto out;
336                         /* this bit is already in use, try next bit */
337                 }
338                 if (bitnum == 64) {
339                         ret = -E_ATT_TABLE_FULL;
340                         goto out;
341                 }
342                 objs[ATTCOL_NAME].data = p;
343                 objs[ATTCOL_NAME].size = len + 1;
344                 ret = osl(osl_add_row(attribute_table, objs));
345                 if (ret < 0)
346                         goto out;
347                 aed.name = p;
348                 aed.bitnum = bitnum;
349                 afs_event(ATTRIBUTE_ADD, &pb, &aed);
350                 greatest_att_bitnum = PARA_MAX(greatest_att_bitnum, (int)bitnum);
351         }
352 out:
353         if (ret < 0 && ret2 >= 0)
354                 para_printf(&pb, "%s: %s\n", p, para_strerror(-ret));
355         if (pb.offset)
356                 pass_buffer_as_shm(pb.buf, pb.offset, &fd);
357         free(pb.buf);
358 }
359
360 int com_addatt(struct stream_cipher_context *scc, int argc, char * const * const argv)
361 {
362         int ret;
363
364         if (argc < 2)
365                 return -E_ATTR_SYNTAX;
366         ret = send_standard_callback_request(argc - 1, argv + 1, com_addatt_callback,
367                 sc_send_result, scc);
368         if (ret < 0)
369                 sc_send_va_buffer(scc, "%s\n", para_strerror(-ret));
370         return ret;
371 }
372
373 static void com_mvatt_callback(int fd, const struct osl_object *query)
374 {
375         char *old = query->data;
376         size_t size = strlen(old) + 1;
377         char *new = old + size;
378         struct osl_object obj = {.data = old, .size = size};
379         struct osl_row *row;
380         struct para_buffer pb = {
381                 .max_size = SHMMAX,
382                 .private_data = &fd,
383                 .max_size_handler = pass_buffer_as_shm
384         };
385         int ret;
386
387         ret = osl(osl_get_row(attribute_table, ATTCOL_NAME, &obj, &row));
388         if (ret < 0)
389                 goto out;
390         obj.data = new;
391         obj.size = strlen(new) + 1;
392         ret = osl(osl_update_object(attribute_table, row, ATTCOL_NAME, &obj));
393 out:
394         if (ret < 0)
395                 para_printf(&pb, "%s\n", para_strerror(-ret));
396         else
397                 afs_event(ATTRIBUTE_RENAME, &pb, NULL);
398         if (pb.offset)
399                 pass_buffer_as_shm(pb.buf, pb.offset, &fd);
400         free(pb.buf);
401 }
402
403 int com_mvatt(struct stream_cipher_context *scc, int argc, char * const * const argv)
404 {
405         int ret;
406
407         if (argc != 3)
408                 return -E_ATTR_SYNTAX;
409         ret = send_standard_callback_request(argc - 1, argv + 1, com_mvatt_callback,
410                 sc_send_result, scc);
411         if (ret < 0)
412                 sc_send_va_buffer(scc, "%s\n", para_strerror(-ret));
413         return ret;
414 }
415
416 /** Data passed to the action handler of com_rmatt(). */
417 struct remove_attribute_action_data {
418         /** Message buffer. */
419         struct para_buffer pb;
420         /** Numver of attributes removed. */
421         int num_removed;
422         /** Bitwise "or" of the removed attributes. */
423         uint64_t mask_of_removed_atts;
424 };
425
426 static int remove_attribute(struct osl_table *table, struct osl_row *row,
427                 const char *name, void *data)
428 {
429         struct remove_attribute_action_data *raad = data;
430         int ret;
431         struct rmatt_event_data red = {.name = name};
432
433         ret = get_attribute_bitnum_by_name(name, &red.bitnum);
434         if (ret < 0)
435                 return para_printf(&raad->pb, "%s: %s\n", name, para_strerror(-ret));
436         ret = osl(osl_del_row(table, row));
437         if (ret < 0)
438                 return para_printf(&raad->pb, "%s: %s\n", name, para_strerror(-ret));
439         ret = para_printf(&raad->pb, "removed attribute %s\n", name);
440         raad->num_removed++;
441         raad->mask_of_removed_atts |= (1 << red.bitnum);
442         afs_event(ATTRIBUTE_REMOVE, &raad->pb, &red);
443         return ret;
444 }
445
446 static void com_rmatt_callback(int fd, const struct osl_object *query)
447 {
448         struct remove_attribute_action_data raad = {
449                 .num_removed = 0,
450                 .pb = {
451                         .max_size = SHMMAX,
452                         .private_data = &fd,
453                         .max_size_handler = pass_buffer_as_shm
454                 }
455         };
456         int ret, ret2 = 0;
457         struct pattern_match_data pmd = {
458                 .table = attribute_table,
459                 .patterns = *query,
460                 .loop_col_num = ATTCOL_BITNUM,
461                 .match_col_num = ATTCOL_NAME,
462                 .data = &raad,
463                 .action = remove_attribute
464         };
465         ret = for_each_matching_row(&pmd);
466         if (ret < 0)
467                 ret2 = para_printf(&raad.pb, "%s\n", para_strerror(-ret));
468         else if (!raad.num_removed)
469                 ret2 = para_printf(&raad.pb, "no match -- nothing removed\n");
470         if (ret2 >= 0 && raad.pb.offset)
471                 pass_buffer_as_shm(raad.pb.buf, raad.pb.offset, &fd);
472         free(raad.pb.buf);
473 }
474
475 int com_rmatt(struct stream_cipher_context *scc, int argc, char * const * const argv)
476 {
477         int ret;
478
479         if (argc < 2)
480                 return -E_ATTR_SYNTAX;
481         ret = send_standard_callback_request(argc - 1, argv + 1, com_rmatt_callback,
482                 sc_send_result, scc);
483         if (ret < 0)
484                 sc_send_va_buffer(scc, "%s\n", para_strerror(-ret));
485         return ret;
486 }
487
488 /**
489  * Return a binary representation of the given attribute value.
490  *
491  * \param atts Pointer to the attribute value.
492  * \param buf Result.
493  *
494  * This function prints a string of at most 64 characters plus the terminating
495  * \p NULL character into \a buf which must be provided by the caller and at
496  * least 65 bytes long. The "x" character is used for set attributes and "-" is
497  * used for unset attributes.
498  *
499  * In practice, not all 64 attributes are defined. In this case, the function
500  * only prints \a N + 1 charaters where \a N is the greatest id of a defined
501  * attribute.
502  */
503 void get_attribute_bitmap(const uint64_t *atts, char *buf)
504 {
505         int i;
506         const uint64_t one = 1;
507
508         for (i = 0; i <= greatest_att_bitnum; i++)
509                 buf[greatest_att_bitnum - i] = (*atts & (one << i))? 'x' : '-';
510         buf[i] = '\0';
511 }
512
513 /**
514  * Get a string containing the set attributes in text form.
515  *
516  * \param atts The attribute bitmap.
517  * \param delim The delimiter to separate matching attribute names.
518  * \param text Result pointer.
519  *
520  * \return Positive on success, negative on errors. If no attributes have
521  * been defined, \a *text is NULL.
522  */
523 int get_attribute_text(uint64_t *atts, const char *delim, char **text)
524 {
525         int i, ret;
526         const uint64_t one = 1;
527
528         *text = NULL;
529         if (greatest_att_bitnum < 0) { /* no attributes available */
530                 *text = para_strdup("(no attributes available)");
531                 return 1;
532         }
533         for (i = 0; i <= greatest_att_bitnum; i++) {
534                 unsigned char bn = i;
535                 struct osl_object obj = {.data = &bn, .size = 1};
536                 struct osl_row *row;
537
538                 if (!(*atts & (one << i)))
539                         continue;
540                 ret = osl(osl_get_row(attribute_table, ATTCOL_BITNUM, &obj, &row));
541                 if (ret < 0)
542                         goto err;
543                 ret = osl(osl_get_object(attribute_table, row, ATTCOL_NAME, &obj));
544                 if (ret < 0)
545                         goto err;
546                 if (*text) {
547                         char *tmp = make_message("%s%s%s", *text, delim, (char *)obj.data);
548                         free(*text);
549                         *text = tmp;
550                 } else
551                         *text = para_strdup(obj.data);
552         }
553         if (!*text) /* no attributes set */
554                 *text = para_strdup("");
555         return 1;
556 err:
557         free(*text);
558         return ret;
559 }
560
561 /**
562  * Close the attribute table.
563  *
564  * \sa osl_close_table().
565  */
566 static void attribute_close(void)
567 {
568         osl_close_table(attribute_table, OSL_MARK_CLEAN);
569         attribute_table = NULL;
570 }
571
572 /**
573  * Open the attribute table.
574  *
575  * \param dir The database directory.
576  *
577  * \return Positive on success, negative on errors.
578  *
579  * \sa osl_open_table().
580  */
581 static int attribute_open(const char *dir)
582 {
583         int ret;
584
585         attribute_table_desc.dir = dir;
586         ret = osl(osl_open_table(&attribute_table_desc, &attribute_table));
587         greatest_att_bitnum = -1; /* no atts available */
588         if (ret >= 0) {
589                 find_greatest_att_bitnum();
590                 return ret;
591         }
592         attribute_table = NULL;
593         if (ret >= 0 || ret == -OSL_ERRNO_TO_PARA_ERROR(E_OSL_NOENT))
594                 return 1;
595         return ret;
596 }
597
598 static int attribute_create(const char *dir)
599 {
600         attribute_table_desc.dir = dir;
601         return osl(osl_create_table(&attribute_table_desc));
602 }
603
604 /**
605  * Initialize the attribute table structure.
606  *
607  * \param t The table structure to initialize.
608  */
609 void attribute_init(struct afs_table *t)
610 {
611         t->open = attribute_open;
612         t->close = attribute_close;
613         t->create = attribute_create;
614 }