88bce0579feb8a4615dd826e77213f122403b0cb
[src/app-framework-main.git] / src / wgtpkg-permissions.c
1 /*
2  Copyright 2015 IoT.bzh
3
4  author: José Bollo <jose.bollo@iot.bzh>
5
6  Licensed under the Apache License, Version 2.0 (the "License");
7  you may not use this file except in compliance with the License.
8  You may obtain a copy of the License at
9
10      http://www.apache.org/licenses/LICENSE-2.0
11
12  Unless required by applicable law or agreed to in writing, software
13  distributed under the License is distributed on an "AS IS" BASIS,
14  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  See the License for the specific language governing permissions and
16  limitations under the License.
17 */
18
19 #define _GNU_SOURCE
20
21 #include <errno.h>
22 #include <string.h>
23 #include <stdlib.h>
24
25 #include "verbose.h"
26 #include "wgtpkg-permissions.h"
27
28 struct permission {
29         char *name;
30         unsigned granted: 1;
31         unsigned requested: 1;
32         unsigned level: 3;
33 };
34
35 static const char prefix_of_permissions[] = FWK_PREFIX_PERMISSION;
36
37 static int nrpermissions = 0;
38 static struct permission *permissions = NULL;
39 static int indexiter = 0;
40
41 /* check is the name has the correct prefix for permissions */
42 int is_standard_permission(const char *name)
43 {
44         return 0 == memcmp(name, prefix_of_permissions, sizeof(prefix_of_permissions) - 1);
45 }
46
47 /* retrieves the permission of name */
48 static struct permission *get_permission(const char *name)
49 {
50         int i;
51
52         for (i = 0 ; i < nrpermissions ; i++)
53                 if (0 == strcmp(permissions[i].name, name))
54                         return permissions+i;
55         return NULL;
56 }
57
58 /* add a permission of name */
59 static struct permission *add_permission(const char *name)
60 {
61         struct permission *p = get_permission(name);
62         if (!p) {
63                 p = realloc(permissions, ((nrpermissions + 8) & ~7) * sizeof(*p));
64                 if (p) {
65                         permissions = p;
66                         p = permissions + nrpermissions;
67                         memset(p, 0, sizeof(*p));
68                         p->name = strdup(name);
69                         if (!p->name)
70                                 p = NULL;
71                 }
72         }
73         return p;
74 }
75
76 /* remove any granting */
77 void reset_permissions()
78 {
79         int i;
80         for (i = 0 ; i < nrpermissions ; i++)
81                 permissions[i].granted = 0;
82 }
83
84 /* remove any granting */
85 void crop_permissions(unsigned level)
86 {
87         int i;
88         for (i = 0 ; i < nrpermissions ; i++)
89                 if (permissions[i].level < level)
90                         permissions[i].granted = 0;
91 }
92
93 /* add permissions granted for installation */
94 void grant_permission_list(const char *list)
95 {
96         struct permission *p;
97         char *iter, c;
98         int n;
99         static const char separators[] = " \t\n\r,";
100
101         iter = strdupa(list);
102         iter += strspn(iter, separators);
103         while(*iter) {
104                 n = strcspn(iter, separators);
105                 c = iter[n];
106                 iter[n] = 0;
107                 p = add_permission(iter);
108                 if (!p) {
109                         ERROR("Can't allocate permission");
110                         exit(1);
111                 }
112                 p->granted = 1;
113                 iter += n;
114                 *iter =c;
115                 iter += strspn(iter, separators);
116         }
117 }
118
119 /* checks if the permission 'name' is recorded */
120 int permission_exists(const char *name)
121 {
122         return !!get_permission(name);
123 }
124
125 /* request the permission, returns 1 if granted or 0 otherwise */
126 int request_permission(const char *name)
127 {
128         struct permission *p = get_permission(name);
129         if (p) {
130                 p->requested = 1;
131                 if (p->granted)
132                         return 1;
133         }
134         return 0;
135 }
136
137 /* iteration over granted and requested permissions */
138 const char *first_usable_permission()
139 {
140         indexiter = 0;
141         return next_usable_permission();
142 }
143
144 const char *next_usable_permission()
145 {
146         while(indexiter < nrpermissions) {
147                 struct permission *p = &permissions[indexiter++];
148                 if (p->granted && p->requested)
149                         return p->name;
150         }
151         return NULL;
152 }
153