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