Merge branch 'master' into next
[paraslash.git] / attribute.c
1 /*
2 * Copyright (C) 1997-2009 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 <openssl/rc4.h>
11 #include <osl.h>
12
13 #include "para.h"
14 #include "error.h"
15 #include "crypt.h"
16 #include "string.h"
17 #include "afh.h"
18 #include "afs.h"
19 #include "net.h"
20 #include "ipc.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 = SHMMAX,
155 .private_data = &fd,
156 .max_size_handler = pass_buffer_as_shm
157 }
158
159 };
160 struct pattern_match_data pmd = {
161 .table = attribute_table,
162 .loop_col_num = ATTCOL_BITNUM,
163 .match_col_num = ATTCOL_NAME,
164 .patterns = {.data = (char *)query->data + sizeof(laad.flags),
165 .size = query->size - sizeof(laad.flags)},
166 .pm_flags = PM_NO_PATTERN_MATCHES_EVERYTHING,
167 .data = &laad,
168 .action = print_attribute
169 };
170 if (laad.flags & LSATT_FLAG_SORT_BY_ID)
171 pmd.loop_col_num = ATTCOL_NAME;
172 if (laad.flags & LSATT_FLAG_REVERSE)
173 pmd.pm_flags |= PM_REVERSE_LOOP;
174 for_each_matching_row(&pmd);
175 if (laad.pb.offset)
176 pass_buffer_as_shm(laad.pb.buf, laad.pb.offset, &fd);
177 free(laad.pb.buf);
178 }
179
180 int com_lsatt(struct rc4_context *rc4c, int argc, char * const * const argv)
181 {
182 unsigned flags = 0;
183 struct osl_object options = {.data = &flags, .size = sizeof(flags)};
184 int ret, i;
185
186 for (i = 1; i < argc; i++) {
187 const char *arg = argv[i];
188 if (arg[0] != '-')
189 break;
190 if (!strcmp(arg, "--")) {
191 i++;
192 break;
193 }
194 if (!strcmp(arg, "-i")) {
195 flags |= LSATT_FLAG_SORT_BY_ID;
196 continue;
197 }
198 if (!strcmp(arg, "-l")) {
199 flags |= LSATT_FLAG_LONG;
200 continue;
201 }
202 if (!strcmp(arg, "-r")) {
203 flags |= LSATT_FLAG_REVERSE;
204 continue;
205 }
206 }
207 ret = send_option_arg_callback_request(&options, argc - i, argv + i,
208 com_lsatt_callback, rc4_send_result, rc4c);
209 if (!ret) {
210 if (argc > 1)
211 ret = rc4_send_va_buffer(rc4c, "no matches\n");
212 } else if (ret < 0)
213 rc4_send_va_buffer(rc4c, "%s\n", para_strerror(-ret));
214 return ret;
215 }
216
217 static void com_setatt_callback(__a_unused int fd, const struct osl_object *query)
218 {
219 char *p;
220 uint64_t add_mask = 0, del_mask = 0;
221 int ret;
222 size_t len;
223 struct osl_object obj;
224 struct osl_row *row;
225
226 for (p = query->data; p < (char *)query->data + query->size; p += len + 1) {
227 char c;
228
229 len = strlen(p);
230 ret = -E_ATTR_SYNTAX;
231 if (!*p)
232 goto out;
233 c = p[len - 1];
234 if (c != '+' && c != '-')
235 break;
236 p[len - 1] = '\0';
237 obj.data = p;
238 obj.size = len + 1;
239 ret = osl(osl_get_row(attribute_table, ATTCOL_NAME, &obj, &row));
240 if (ret < 0)
241 goto out;
242 ret = osl(osl_get_object(attribute_table, row, ATTCOL_BITNUM,
243 &obj));
244 if (ret < 0)
245 goto out;
246 if (c == '+')
247 add_mask |= (1UL << *(unsigned char *)obj.data);
248 else
249 del_mask |= (1UL << *(unsigned char *)obj.data);
250 }
251 ret = -E_ATTR_SYNTAX;
252 if (!add_mask && !del_mask)
253 goto out;
254 PARA_DEBUG_LOG("masks: %llx:%llx\n",(long long unsigned)add_mask,
255 (long long unsigned)del_mask);
256 for (; p < (char *)query->data + query->size; p += len + 1) { /* TODO: fnmatch */
257 struct afs_info old_afsi, new_afsi;
258 struct afsi_change_event_data aced = {.old_afsi = &old_afsi};
259
260 len = strlen(p);
261 ret = aft_get_row_of_path(p, &aced.aft_row);
262 if (ret < 0)
263 goto out;
264 ret = get_afsi_object_of_row(aced.aft_row, &obj);
265 if (ret < 0)
266 goto out;
267 ret = load_afsi(&old_afsi, &obj);
268 if (ret < 0)
269 goto out;
270 new_afsi = old_afsi;
271 new_afsi.attributes |= add_mask;
272 new_afsi.attributes &= ~del_mask;
273 save_afsi(&new_afsi, &obj); /* in-place update */
274 afs_event(AFSI_CHANGE, NULL, &aced);
275 }
276 out:
277 if (ret < 0)
278 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
279 }
280
281 int com_setatt(__a_unused struct rc4_context *rc4c, int argc, char * const * const argv)
282 {
283 if (argc < 3)
284 return -E_ATTR_SYNTAX;
285 return send_standard_callback_request(argc - 1, argv + 1, com_setatt_callback,
286 NULL, NULL);
287 }
288
289 struct addatt_event_data {
290 const char *name;
291 unsigned char bitnum;
292 };
293
294
295 static void com_addatt_callback(int fd, const struct osl_object *query)
296 {
297 char *p;
298 int ret = 1, ret2 = 0;
299 struct para_buffer pb = {
300 .max_size = SHMMAX,
301 .private_data = &fd,
302 .max_size_handler = pass_buffer_as_shm
303 };
304 size_t len;
305
306 for (p = query->data; p < (char *)query->data + query->size; p += len + 1) {
307 struct osl_object objs[NUM_ATT_COLUMNS];
308 struct osl_row *row;
309 unsigned char bitnum;
310 struct addatt_event_data aed;
311
312 len = strlen(p);
313 if (!len || p[len - 1] == '-' || p[len - 1] == '+') {
314 ret2 = para_printf(&pb, "invalid attribute name: %s\n", p);
315 if (ret2 < 0)
316 goto out;
317 continue;
318 }
319 ret = get_attribute_bitnum_by_name(p, &bitnum);
320 if (ret >= 0) {
321 ret2 = para_printf(&pb, "attribute \"%s\" already exists\n", p);
322 if (ret2 < 0)
323 goto out;
324 continue;
325 }
326 if (ret != -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND)) /* error */
327 goto out;
328 objs[ATTCOL_BITNUM].size = 1;
329 /* find smallest unused attribute */
330 for (bitnum = 0; bitnum < 64; bitnum++) {
331 objs[ATTCOL_BITNUM].data = &bitnum;
332 ret = osl(osl_get_row(attribute_table, ATTCOL_BITNUM,
333 &objs[ATTCOL_BITNUM], &row));
334 if (ret == -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND))
335 break; /* this bitnum is unused, use it */
336 if (ret < 0) /* error */
337 goto out;
338 /* this bit is already in use, try next bit */
339 }
340 if (bitnum == 64) {
341 ret = -E_ATT_TABLE_FULL;
342 goto out;
343 }
344 objs[ATTCOL_NAME].data = p;
345 objs[ATTCOL_NAME].size = len + 1;
346 ret = osl(osl_add_row(attribute_table, objs));
347 if (ret < 0)
348 goto out;
349 aed.name = p;
350 aed.bitnum = bitnum;
351 afs_event(ATTRIBUTE_ADD, &pb, &aed);
352 greatest_att_bitnum = PARA_MAX(greatest_att_bitnum, (int)bitnum);
353 }
354 out:
355 if (ret < 0 && ret2 >= 0)
356 para_printf(&pb, "%s: %s\n", p, para_strerror(-ret));
357 if (pb.offset)
358 pass_buffer_as_shm(pb.buf, pb.offset, &fd);
359 free(pb.buf);
360 }
361
362 int com_addatt(struct rc4_context *rc4c, int argc, char * const * const argv)
363 {
364 int ret;
365
366 if (argc < 2)
367 return -E_ATTR_SYNTAX;
368 ret = send_standard_callback_request(argc - 1, argv + 1, com_addatt_callback,
369 rc4_send_result, rc4c);
370 if (ret < 0)
371 rc4_send_va_buffer(rc4c, "%s\n", para_strerror(-ret));
372 return ret;
373 }
374
375 static void com_mvatt_callback(int fd, const struct osl_object *query)
376 {
377 char *old = query->data;
378 size_t size = strlen(old) + 1;
379 char *new = old + size;
380 struct osl_object obj = {.data = old, .size = size};
381 struct osl_row *row;
382 struct para_buffer pb = {
383 .max_size = SHMMAX,
384 .private_data = &fd,
385 .max_size_handler = pass_buffer_as_shm
386 };
387 int ret;
388
389 ret = osl(osl_get_row(attribute_table, ATTCOL_NAME, &obj, &row));
390 if (ret < 0)
391 goto out;
392 obj.data = new;
393 obj.size = strlen(new) + 1;
394 ret = osl(osl_update_object(attribute_table, row, ATTCOL_NAME, &obj));
395 out:
396 if (ret < 0)
397 para_printf(&pb, "%s\n", para_strerror(-ret));
398 else
399 afs_event(ATTRIBUTE_RENAME, &pb, NULL);
400 if (pb.offset)
401 pass_buffer_as_shm(pb.buf, pb.offset, &fd);
402 free(pb.buf);
403 }
404
405 int com_mvatt(struct rc4_context *rc4c, int argc, char * const * const argv)
406 {
407 int ret;
408
409 if (argc != 3)
410 return -E_ATTR_SYNTAX;
411 ret = send_standard_callback_request(argc - 1, argv + 1, com_mvatt_callback,
412 rc4_send_result, rc4c);
413 if (ret < 0)
414 rc4_send_va_buffer(rc4c, "%s\n", para_strerror(-ret));
415 return ret;
416 }
417
418 /** Data passed to the action handler of com_rmatt(). */
419 struct remove_attribute_action_data {
420 /** Message buffer. */
421 struct para_buffer pb;
422 /** Numver of attributes removed. */
423 int num_removed;
424 /** Bitwise "or" of the removed attributes. */
425 uint64_t mask_of_removed_atts;
426 };
427
428 static int remove_attribute(struct osl_table *table, struct osl_row *row,
429 const char *name, void *data)
430 {
431 struct remove_attribute_action_data *raad = data;
432 int ret;
433 struct rmatt_event_data red = {.name = name};
434
435 ret = get_attribute_bitnum_by_name(name, &red.bitnum);
436 if (ret < 0)
437 return para_printf(&raad->pb, "%s: %s\n", name, para_strerror(-ret));
438 ret = osl(osl_del_row(table, row));
439 if (ret < 0)
440 return para_printf(&raad->pb, "%s: %s\n", name, para_strerror(-ret));
441 ret = para_printf(&raad->pb, "removed attribute %s\n", name);
442 raad->num_removed++;
443 raad->mask_of_removed_atts |= (1 << red.bitnum);
444 afs_event(ATTRIBUTE_REMOVE, &raad->pb, &red);
445 return ret;
446 }
447
448 static void com_rmatt_callback(int fd, const struct osl_object *query)
449 {
450 struct remove_attribute_action_data raad = {
451 .num_removed = 0,
452 .pb = {
453 .max_size = SHMMAX,
454 .private_data = &fd,
455 .max_size_handler = pass_buffer_as_shm
456 }
457 };
458 int ret, ret2 = 0;
459 struct pattern_match_data pmd = {
460 .table = attribute_table,
461 .patterns = *query,
462 .loop_col_num = ATTCOL_BITNUM,
463 .match_col_num = ATTCOL_NAME,
464 .data = &raad,
465 .action = remove_attribute
466 };
467 ret = for_each_matching_row(&pmd);
468 if (ret < 0)
469 ret2 = para_printf(&raad.pb, "%s\n", para_strerror(-ret));
470 else if (!raad.num_removed)
471 ret2 = para_printf(&raad.pb, "no match -- nothing removed\n");
472 if (ret2 >= 0 && raad.pb.offset)
473 pass_buffer_as_shm(raad.pb.buf, raad.pb.offset, &fd);
474 free(raad.pb.buf);
475 }
476
477 int com_rmatt(struct rc4_context *rc4c, int argc, char * const * const argv)
478 {
479 int ret;
480
481 if (argc < 2)
482 return -E_ATTR_SYNTAX;
483 ret = send_standard_callback_request(argc - 1, argv + 1, com_rmatt_callback,
484 rc4_send_result, rc4c);
485 if (ret < 0)
486 rc4_send_va_buffer(rc4c, "%s\n", para_strerror(-ret));
487 return ret;
488 }
489
490 /**
491 * Return a binary representation of the given attribute value.
492 *
493 * \param atts Pointer to the attribute value.
494 * \param buf Result.
495 *
496 * This function prints a string of at most 64 characters plus the terminating
497 * \p NULL character into \a buf which must be provided by the caller and at
498 * least 65 bytes long. The "x" character is used for set attributes and "-" is
499 * used for unset attributes.
500 *
501 * In practice, not all 64 attributes are defined. In this case, the function
502 * only prints \a N + 1 charaters where \a N is the greatest id of a defined
503 * attribute.
504 */
505 void get_attribute_bitmap(const uint64_t *atts, char *buf)
506 {
507 int i;
508 const uint64_t one = 1;
509
510 for (i = 0; i <= greatest_att_bitnum; i++)
511 buf[greatest_att_bitnum - i] = (*atts & (one << i))? 'x' : '-';
512 buf[i] = '\0';
513 }
514
515 /**
516 * Get a string containing the set attributes in text form.
517 *
518 * \param atts The attribute bitmap.
519 * \param delim The delimiter to separate matching attribute names.
520 * \param text Result pointer.
521 *
522 * \return Positive on success, negative on errors. If no attributes have
523 * been defined, \a *text is NULL.
524 */
525 int get_attribute_text(uint64_t *atts, const char *delim, char **text)
526 {
527 int i, ret;
528 const uint64_t one = 1;
529
530 *text = NULL;
531 if (greatest_att_bitnum < 0) { /* no attributes available */
532 *text = para_strdup("(no attributes available)");
533 return 1;
534 }
535 for (i = 0; i <= greatest_att_bitnum; i++) {
536 unsigned char bn = i;
537 struct osl_object obj = {.data = &bn, .size = 1};
538 struct osl_row *row;
539
540 if (!(*atts & (one << i)))
541 continue;
542 ret = osl(osl_get_row(attribute_table, ATTCOL_BITNUM, &obj, &row));
543 if (ret < 0)
544 goto err;
545 ret = osl(osl_get_object(attribute_table, row, ATTCOL_NAME, &obj));
546 if (ret < 0)
547 goto err;
548 if (*text) {
549 char *tmp = make_message("%s%s%s", *text, delim, (char *)obj.data);
550 free(*text);
551 *text = tmp;
552 } else
553 *text = para_strdup(obj.data);
554 }
555 if (!*text) /* no attributes set */
556 *text = para_strdup("");
557 return 1;
558 err:
559 free(*text);
560 return ret;
561 }
562
563 /**
564 * Close the attribute table.
565 *
566 * \sa osl_close_table().
567 */
568 static void attribute_close(void)
569 {
570 osl_close_table(attribute_table, OSL_MARK_CLEAN);
571 attribute_table = NULL;
572 }
573
574 /**
575 * Open the attribute table.
576 *
577 * \param dir The database directory.
578 *
579 * \return Positive on success, negative on errors.
580 *
581 * \sa osl_open_table().
582 */
583 static int attribute_open(const char *dir)
584 {
585 int ret;
586
587 attribute_table_desc.dir = dir;
588 ret = osl(osl_open_table(&attribute_table_desc, &attribute_table));
589 greatest_att_bitnum = -1; /* no atts available */
590 if (ret >= 0) {
591 find_greatest_att_bitnum();
592 return ret;
593 }
594 attribute_table = NULL;
595 if (ret >= 0 || ret == -OSL_ERRNO_TO_PARA_ERROR(E_OSL_NOENT))
596 return 1;
597 return ret;
598 }
599
600 static int attribute_create(const char *dir)
601 {
602 attribute_table_desc.dir = dir;
603 return osl(osl_create_table(&attribute_table_desc));
604 }
605
606 /**
607 * Initialize the attribute table structure.
608 *
609 * \param t The table structure to initialize.
610 */
611 void attribute_init(struct afs_table *t)
612 {
613 t->open = attribute_open;
614 t->close = attribute_close;
615 t->create = attribute_create;
616 }