work in progress
[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 "verbose.h"
24 #include "wgtpkg.h"
25
26 struct permission {
27         char *name;
28         unsigned granted: 1;
29         unsigned requested: 1;
30         unsigned level: 3;
31 };
32
33 static const char prefix_of_permissions[] = PREFIXPERMISSION;
34
35 static int nrpermissions = 0;
36 static struct permission *permissions = NULL;
37 static int indexiter = 0;
38
39 /* check is the name has the correct prefix for permissions */
40 int is_standard_permission(const char *name)
41 {
42         return 0 == memcmp(name, prefix_of_permissions, sizeof(prefix_of_permissions) - 1);
43 }
44
45 /* retrieves the permission of name */
46 static struct permission *get_permission(const char *name)
47 {
48         int i;
49
50         for (i = 0 ; i < nrpermissions ; i++)
51                 if (0 == strcmp(permissions[i].name, name))
52                         return permissions+i;
53         return NULL;
54 }
55
56 /* add a permission of name */
57 static struct permission *add_permission(const char *name)
58 {
59         struct permission *p = get_permission(name);
60         if (!p) {
61                 p = realloc(permissions, ((nrpermissions + 8) & ~7) * sizeof(*p));
62                 if (p) {
63                         permissions = p;
64                         p = permissions + nrpermissions;
65                         memset(p, 0, sizeof(*p));
66                         p->name = strdup(name);
67                         if (!p->name)
68                                 p = NULL;
69                 }
70         }
71         return p;
72 }
73
74 /* remove any granting */
75 void reset_permissions()
76 {
77         int i;
78         for (i = 0 ; i < nrpermissions ; i++)
79                 permissions[i].granted = 0;
80 }
81
82 /* remove any granting */
83 void crop_permissions(unsigned level)
84 {
85         int i;
86         for (i = 0 ; i < nrpermissions ; i++)
87                 if (permissions[i].level < level)
88                         permissions[i].granted = 0;
89 }
90
91 /* add permissions granted for installation */
92 void grant_permission_list(const char *list)
93 {
94         struct permission *p;
95         char *iter, c;
96         int n;
97         static const char separators[] = " \t\n\r,";
98
99         iter = strdupa(list);
100         iter += strspn(iter, separators);
101         while(*iter) {
102                 n = strcspn(iter, separators);
103                 c = iter[n];
104                 iter[n] = 0;
105                 p = add_permission(iter);
106                 if (!p) {
107                         ERROR("Can't allocate permission");
108                         exit(1);
109                 }
110                 p->granted = 1;
111                 iter += n;
112                 *iter =c;
113                 iter += strspn(iter, separators);
114         }
115 }
116
117 /* checks if the permission 'name' is recorded */
118 int permission_exists(const char *name)
119 {
120         return !!get_permission(name);
121 }
122
123 /* request the permission, returns 1 if granted or 0 otherwise */
124 int request_permission(const char *name)
125 {
126         struct permission *p = get_permission(name);
127         if (p) {
128                 p->requested = 1;
129                 if (p->granted)
130                         return 1;
131         }
132         return 0;
133 }
134
135 /* iteration over granted and requested permissions */
136 const char *first_usable_permission()
137 {
138         indexiter = 0;
139         return next_usable_permission();
140 }
141
142 const char *next_usable_permission()
143 {
144         while(indexiter < nrpermissions) {
145                 struct permission *p = &permissions[indexiter++];
146                 if (p->granted && p->requested)
147                         return p->name;
148         }
149         return NULL;
150 }
151