don't change of directory anymore
[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
37 /* check is the name has the correct prefix for permissions */
38 int is_standard_permission(const char *name)
39 {
40         return 0 == memcmp(name, prefix_of_permissions, sizeof(prefix_of_permissions) - 1);
41 }
42
43 /* retrieves the permission of name */
44 static struct permission *get_permission(const char *name)
45 {
46         int i;
47
48         for (i = 0 ; i < nrpermissions ; i++)
49                 if (0 == strcmp(permissions[i].name, name))
50                         return permissions+i;
51         return NULL;
52 }
53
54 /* add a permission of name */
55 static struct permission *add_permission(const char *name)
56 {
57         struct permission *p = get_permission(name);
58         if (!p) {
59                 p = realloc(permissions, ((nrpermissions + 8) & ~7) * sizeof(*p));
60                 if (p) {
61                         permissions = p;
62                         p = permissions + nrpermissions;
63                         memset(p, 0, sizeof(*p));
64                         p->name = strdup(name);
65                         if (!p->name)
66                                 p = NULL;
67                 }
68         }
69         return p;
70 }
71
72 /* remove any granting */
73 void reset_permissions()
74 {
75         int i;
76         for (i = 0 ; i < nrpermissions ; i++)
77                 permissions[i].granted = 0;
78 }
79
80 /* remove any granting */
81 void crop_permissions(unsigned level)
82 {
83         int i;
84         for (i = 0 ; i < nrpermissions ; i++)
85                 if (permissions[i].level < level)
86                         permissions[i].granted = 0;
87 }
88
89 /* add permissions granted for installation */
90 void grant_permission_list(const char *list)
91 {
92         struct permission *p;
93         char *iter, c;
94         int n;
95         static const char separators[] = " \t\n\r,";
96
97         iter = strdupa(list);
98         iter += strspn(iter, separators);
99         while(*iter) {
100                 n = strcspn(iter, separators);
101                 c = iter[n];
102                 iter[n] = 0;
103                 p = add_permission(iter);
104                 if (!p) {
105                         syslog(LOG_ERR, "Can't allocate permission");
106                         exit(1);
107                 }
108                 p->granted = 1;
109                 iter += n;
110                 *iter =c;
111                 iter += strspn(iter, separators);
112         }
113 }
114
115 /* checks if the permission 'name' is recorded */
116 int permission_exists(const char *name)
117 {
118         return !!get_permission(name);
119 }
120
121 /* request the permission, returns 1 if granted or 0 otherwise */
122 int request_permission(const char *name)
123 {
124         struct permission *p = get_permission(name);
125         if (p) {
126                 p->requested = 1;
127                 if (p->granted)
128                         return 1;
129         }
130         return 0;
131 }
132