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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
28 unsigned requested: 1;
32 static const char prefix_of_permissions[] = PREFIXPERMISSION;
34 static int nrpermissions = 0;
35 static struct permission *permissions = NULL;
37 /* check is the name has the correct prefix for permissions */
38 int is_standard_permission(const char *name)
40 return 0 == memcmp(name, prefix_of_permissions, sizeof(prefix_of_permissions) - 1);
43 /* retrieves the permission of name */
44 static struct permission *get_permission(const char *name)
48 for (i = 0 ; i < nrpermissions ; i++)
49 if (0 == strcmp(permissions[i].name, name))
54 /* add a permission of name */
55 static struct permission *add_permission(const char *name)
57 struct permission *p = get_permission(name);
59 p = realloc(permissions, ((nrpermissions + 8) & ~7) * sizeof(*p));
62 p = permissions + nrpermissions;
63 memset(p, 0, sizeof(*p));
64 p->name = strdup(name);
72 /* remove any granting */
73 void reset_permissions()
76 for (i = 0 ; i < nrpermissions ; i++)
77 permissions[i].granted = 0;
80 /* remove any granting */
81 void crop_permissions(unsigned level)
84 for (i = 0 ; i < nrpermissions ; i++)
85 if (permissions[i].level < level)
86 permissions[i].granted = 0;
89 /* add permissions granted for installation */
90 void grant_permission_list(const char *list)
95 static const char separators[] = " \t\n\r,";
98 iter += strspn(iter, separators);
100 n = strcspn(iter, separators);
103 p = add_permission(iter);
105 syslog(LOG_ERR, "Can't allocate permission");
111 iter += strspn(iter, separators);
115 /* checks if the permission 'name' is recorded */
116 int permission_exists(const char *name)
118 return !!get_permission(name);
121 /* request the permission, returns 1 if granted or 0 otherwise */
122 int request_permission(const char *name)
124 struct permission *p = get_permission(name);