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
|
/*
Copyright 2015 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.
*/
#define _GNU_SOURCE
#include <errno.h>
#include <syslog.h>
#include <string.h>
#include "verbose.h"
#include "wgtpkg.h"
struct permission {
char *name;
unsigned granted: 1;
unsigned requested: 1;
unsigned level: 3;
};
static const char prefix_of_permissions[] = PREFIXPERMISSION;
static int nrpermissions = 0;
static struct permission *permissions = NULL;
static int indexiter = 0;
/* check is the name has the correct prefix for permissions */
int is_standard_permission(const char *name)
{
return 0 == memcmp(name, prefix_of_permissions, sizeof(prefix_of_permissions) - 1);
}
/* retrieves the permission of name */
static struct permission *get_permission(const char *name)
{
int i;
for (i = 0 ; i < nrpermissions ; i++)
if (0 == strcmp(permissions[i].name, name))
return permissions+i;
return NULL;
}
/* add a permission of name */
static struct permission *add_permission(const char *name)
{
struct permission *p = get_permission(name);
if (!p) {
p = realloc(permissions, ((nrpermissions + 8) & ~7) * sizeof(*p));
if (p) {
permissions = p;
p = permissions + nrpermissions;
memset(p, 0, sizeof(*p));
p->name = strdup(name);
if (!p->name)
p = NULL;
}
}
return p;
}
/* remove any granting */
void reset_permissions()
{
int i;
for (i = 0 ; i < nrpermissions ; i++)
permissions[i].granted = 0;
}
/* remove any granting */
void crop_permissions(unsigned level)
{
int i;
for (i = 0 ; i < nrpermissions ; i++)
if (permissions[i].level < level)
permissions[i].granted = 0;
}
/* add permissions granted for installation */
void grant_permission_list(const char *list)
{
struct permission *p;
char *iter, c;
int n;
static const char separators[] = " \t\n\r,";
iter = strdupa(list);
iter += strspn(iter, separators);
while(*iter) {
n = strcspn(iter, separators);
c = iter[n];
iter[n] = 0;
p = add_permission(iter);
if (!p) {
ERROR("Can't allocate permission");
exit(1);
}
p->granted = 1;
iter += n;
*iter =c;
iter += strspn(iter, separators);
}
}
/* checks if the permission 'name' is recorded */
int permission_exists(const char *name)
{
return !!get_permission(name);
}
/* request the permission, returns 1 if granted or 0 otherwise */
int request_permission(const char *name)
{
struct permission *p = get_permission(name);
if (p) {
p->requested = 1;
if (p->granted)
return 1;
}
return 0;
}
/* iteration over granted and requested permissions */
const char *first_usable_permission()
{
indexiter = 0;
return next_usable_permission();
}
const char *next_usable_permission()
{
while(indexiter < nrpermissions) {
struct permission *p = &permissions[indexiter++];
if (p->granted && p->requested)
return p->name;
}
return NULL;
}
|