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