aboutsummaryrefslogtreecommitdiffstats
path: root/src/afb-perm.c
blob: b4d2b5e31c1d5ee7274948bc49d3431a6a7d31a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/*
 * Copyright (C) 2017 "IoT.bzh"
 * Author José Bollo <jose.bollo@iot.bzh>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>

#include "afb-perm.h"
#include "verbose.h"

/*********************************************************************
*** SECTION node
*********************************************************************/

/**
 * types of nodes
 */
enum type
{
	Text,	/**< text node */
	And,	/**< and node */
	Or,	/**< or node */
	Not	/**< not node */
};

/**
 * structure for nodes
 */
struct node
{
	enum type type;	/**< type of the node */
	union {
		struct node *children[2]; /**< possible subnodes */
		char text[1]; /**< text of the node */
	};
};

/**
 * make a text node and return it
 * @param type type of the node (should be Text)
 * @param text the text to set for the node
 * @param length the length of the text
 * @return the allocated node or NULL on memory depletion
 */
static struct node *node_make_text(enum type type, const char *text, size_t length)
{
	struct node *node;

	node = malloc(length + sizeof *node);
	if (!node)
		errno = ENOMEM;
	else {
		node->type = type;
		memcpy(node->text, text, length);
		node->text[length] = 0;
	}
	return node;
}

/**
 * make a node with sub nodes and return it
 * @param type type of the node (should be And, Or or Text)
 * @param left the "left" sub node
 * @param right the "right" sub node (if any or NULL)
 * @return the allocated node or NULL on memory depletion
 */
static struct node *node_make_parent(enum type type, struct node *left, struct node *right)
{
	struct node *node;

	node = malloc(sizeof *node);
	if (!node)
		errno = ENOMEM;
	else {
		node->type = type;
		node->children[0] = left;
		node->children[1] = right;
	}
	return node;
}

/**
 * Frees the node and its possible subnodes
 * @param node the node to free
 */
static void node_free(struct node *node)
{
	struct node *left, *right;

	if (node) {
		switch (node->type) {
		case Text:
			free(node);
			break;
		case And:
		case Or:
			left = node->children[0];
			right = node->children[1];
			free(node);
			node_free(left);
			node_free(right);
			break;
		case Not:
			left = node->children[0];
			free(node);
			node_free(left);
			break;
		}
	}
}

/**
 * Checks the permissions for the 'node' using the 'check' function
 * and its 'closure'.
 * @param node the node to check
 * @param check the function that checks if a pernmission of 'name' is granted for 'closure'
 * @param closure the context closure for the function 'check'
 * @return 1 if the permission is granted or 0 otherwise
 */
static int node_check(struct node *node, int (*check)(void *closure, const char *name), void *closure)
{
	int rc;

	switch (node->type) {
	case Text:
		rc = check(closure, node->text);
		break;
	case And:
		rc = node_check(node->children[0], check, closure);
		if (rc)
			rc = node_check(node->children[1], check, closure);
		break;
	case Or:
		rc = node_check(node->children[0], check, closure);
		if (!rc)
			rc = node_check(node->children[1], check, closure);
		break;
	case Not:
		rc = !node_check(node->children[0], check, closure);
		break;
	}
	return rc;
}

/*********************************************************************
*** SECTION parse
*********************************************************************/

/**
 * the symbol types
 */
enum symbol
{
	TEXT,	/**< a common text, name of a permission */
	AND,	/**< and keyword */
	OR,	/**< or keyword */
	NOT,	/**< not keyword */
	OBRA,	/**< open bracket */
	CBRA,	/**< close bracket */
	END	/**< end of input */
};

/**
 * structure for parsing permission description
 */
struct parse
{
	const char *desc;	/**< initial permission description */
	const char *symbol;	/**< current symbol parsed */
	size_t symlen;		/**< length of the current symbol */
	enum symbol type;	/**< type of the current symbol */
};

/**
 * updates parse to point to the next symbol if any
 * @param parse parser state to update
 */
static void parse_next(struct parse *parse)
{
	const char *scan;
	size_t len;

	/* skip current symbol */
	scan = &parse->symbol[parse->symlen];

	/* skip white spaces */
	while (*scan && isspace(*scan))
		scan++;

	/* scan the symbol */
	switch (*scan) {
	case 0:
		len = 0;
		parse->type = END;
		break;
	case '(':
		len = 1;
		parse->type = OBRA;
		break;
	case ')':
		len = 1;
		parse->type = CBRA;
		break;
	default:
		/* compute the length */
		len = 0;
		while (scan[len] && !isspace(scan[len]) && scan[len] != ')' && scan[len] != '(')
			len++;
		parse->type = TEXT;

		/* fall to keyword if any */
		switch(len) {
		case 2:
			if (!strncasecmp(scan, "or", len))
				parse->type = OR;
			break;
		case 3:
			if (!strncasecmp(scan, "and", len))
				parse->type = AND;
			else if (!strncasecmp(scan, "not", len))
				parse->type = NOT;
			break;
		}
		break;
	}
	parse->symbol = scan;
	parse->symlen = len;
}

/**
 * Init the parser state 'parse' for the description 'desc'
 * @param parse the parser state to initialise
 * @param desc the description of the permissions to be parsed
 */
static void parse_init(struct parse *parse, const char *desc)
{
	parse->desc = desc;
	parse->symbol = desc;
	parse->symlen = 0;
	parse_next(parse);
}

/*********************************************************************
*** SECTION node_parse
*********************************************************************/

static struct node *node_parse_or(struct parse *parse);

/**
 * Parse a permission name
 * @param parser the parser state
 * @return the parsed node or NULL in case of error
 * in case of error errno is set to EINVAL or ENOMEM
 */
static struct node *node_parse_text(struct parse *parse)
{
	struct node *node;

	if (parse->type == TEXT) {
		node = node_make_text(Text, parse->symbol, parse->symlen);
		parse_next(parse);
	} else {
		errno = EINVAL;
		node = NULL;
	}
	return node;
}

/**
 * Parse a term that is either a name (text) or a sub expression
 * enclosed in brackets.
 * @param parser the parser state
 * @return the parsed node or NULL in case of error
 * in case of error errno is set to EINVAL or ENOMEM
 */
static struct node *node_parse_term(struct parse *parse)
{
	struct node *node;

	if (parse->type != OBRA)
		node = node_parse_text(parse);
	else {
		parse_next(parse);
		node = node_parse_or(parse);
		if (parse->type == CBRA)
			parse_next(parse);
		else {
			errno = EINVAL;
			node_free(node);
			node = NULL;
		}
	}
	return node;
}

/**
 * Parse a term potentially prefixed by not.
 * @param parser the parser state
 * @return the parsed node or NULL in case of error
 * in case of error errno is set to EINVAL or ENOMEM
 */
static struct node *node_parse_not(struct parse *parse)
{
	struct node *node, *not;

	if (parse->type != NOT)
		node = node_parse_term(parse);
	else {
		parse_next(parse);
		node = node_parse_term(parse);
		if (node) {
			not = node_make_parent(Not, node, NULL);
			if (not)
				node = not;
			else {
				node_free(node);
				node = NULL;
			}
		}
	}
	return node;
}

/**
 * Parse a potential sequence of terms connected with the
 * given operator (AND or OR). The function takes care to
 * create an evaluation tree that respects the order given
 * by the description and that will limit the recursivity
 * depth.
 * @param parser the parser state
 * @param operator the symbol type of the operator scanned
 * @param subparse the function for parsing terms of the sequence
 * @param type the node type corresponding to the operator
 * @return the parsed node or NULL in case of error
 * in case of error errno is set to EINVAL or ENOMEM
 */
static struct node *node_parse_infix(
		struct parse *parse,
		enum symbol operator,
		struct node *(*subparse)(struct parse*),
		enum type type
)
{
	struct node *root, **up, *right, *node;

	root = subparse(parse);
	if (root) {
		up = &root;
		while (parse->type == operator) {
			parse_next(parse);
			right = subparse(parse);
			if (!right) {
				node_free(root);
				root = NULL;
				break;
			}
			node = node_make_parent(type, *up, right);
			if (!node) {
				node_free(right);
				node_free(root);
				root = NULL;
				break;
			}
			*up = node;
			up = &node->children[1];
		}
	}
	return root;
}

/**
 * Parse a potential sequence of anded terms.
 * @param parser the parser state
 * @return the parsed node or NULL in case of error
 * in case of error errno is set to EINVAL or ENOMEM
 */
static struct node *node_parse_and(struct parse *parse)
{
	return node_parse_infix(parse, AND, node_parse_not, And);
}

/**
 * Parse a potential sequence of ored terms.
 * @param parser the parser state
 * @return the parsed node or NULL in case of error
 * in case of error errno is set to EINVAL or ENOMEM
 */
static struct node *node_parse_or(struct parse *parse)
{
	return node_parse_infix(parse, OR, node_parse_and, Or);
}

/**
 * Parse description of permissions.
 * @param desc the description to parse
 * @return the parsed node or NULL in case of error
 * in case of error errno is set to EINVAL or ENOMEM
 */
static struct node *node_parse(const char *desc)
{
	struct node *node;
	struct parse parse;

	parse_init(&parse, desc);
	node = node_parse_or(&parse);
	if (node && parse.type != END) {
		node_free(node);
		node = NULL;
	}
	return node;
}

/*********************************************************************
*** SECTION perm
*********************************************************************/

/**
 * structure for storing permissions
 */
struct afb_perm
{
	struct node *root;	/**< root node descripbing the permission */
	int refcount;		/**< the count of use of the structure */
};

/**
 * allocates the structure for the given root
 * @param root the root node to keep
 * @return the created permission object or NULL
 */
static struct afb_perm *make_perm(struct node *root)
{
	struct afb_perm *perm;

	perm = malloc(sizeof *perm);
	if (!perm)
		errno = ENOMEM;
	else {
		perm->root = root;
		perm->refcount = 1;
	}
	return perm;
}

/**
 * Creates the permission for the given description
 * @param desc the description of the permission to create
 * @return the created permission object or NULL
 */
struct afb_perm *afb_perm_parse(const char *desc)
{
	struct node *root;
	struct afb_perm *perm;

	root = node_parse(desc);
	if (root) {
		perm = make_perm(root);
		if (perm)
			return perm;
		node_free(root);
	}
	return NULL;
}

/**
 * Adds a reference to the permissions
 * @param perm the permission to reference
 */
void afb_perm_addref(struct afb_perm *perm)
{
	assert(perm);
	perm->refcount++;
}

/**
 * Removes a reference to the permissions
 * @param perm the permission to dereference
 */
void afb_perm_unref(struct afb_perm *perm)
{
	if (perm && !--perm->refcount) {
		node_free(perm->root);
		free(perm);
	}
}

/**
 * Checks permission 'perm' using the 'check' function
 * and its 'closure'.
 * @param perm the permission to check
 * @param check the function that checks if a pernmission of 'name' is granted for 'closure'
 * @param closure the context closure for the function 'check'
 * @return 1 if the permission is granted or 0 otherwise
 */
int afb_perm_check(struct afb_perm *perm, int (*check)(void *closure, const char *name), void *closure)
{
	return node_check(perm->root, check, closure);
}