aboutsummaryrefslogtreecommitdiffstats
path: root/src/wgtpkg-permissions.c
diff options
context:
space:
mode:
authorJosé Bollo <jose.bollo@iot.bzh>2015-12-11 14:57:32 +0100
committerJosé Bollo <jose.bollo@iot.bzh>2015-12-11 14:57:32 +0100
commit9ab266df6642c6e930e03b3024d7c3d53ef88bbc (patch)
tree95c7686ad33af3c012a02412938f9b2c0191dc7b /src/wgtpkg-permissions.c
parent26d75de504d808191dbadc9a735009a214a789b0 (diff)
don't change of directory anymore
Change-Id: I9f5906d3e053b3fa0f3000d07c4af3f666fe079a
Diffstat (limited to 'src/wgtpkg-permissions.c')
-rw-r--r--src/wgtpkg-permissions.c132
1 files changed, 132 insertions, 0 deletions
diff --git a/src/wgtpkg-permissions.c b/src/wgtpkg-permissions.c
new file mode 100644
index 0000000..25758e4
--- /dev/null
+++ b/src/wgtpkg-permissions.c
@@ -0,0 +1,132 @@
+/*
+ Copyright 2015 IoT.bzh
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+*/
+
+#define _GNU_SOURCE
+
+#include <errno.h>
+#include <syslog.h>
+#include <string.h>
+
+#include "wgtpkg.h"
+
+struct permission {
+ char *name;
+ unsigned granted: 1;
+ unsigned requested: 1;
+ unsigned level: 3;
+};
+
+static const char prefix_of_permissions[] = PREFIXPERMISSION;
+
+static int nrpermissions = 0;
+static struct permission *permissions = NULL;
+
+/* check is the name has the correct prefix for permissions */
+int is_standard_permission(const char *name)
+{
+ return 0 == memcmp(name, prefix_of_permissions, sizeof(prefix_of_permissions) - 1);
+}
+
+/* retrieves the permission of name */
+static struct permission *get_permission(const char *name)
+{
+ int i;
+
+ for (i = 0 ; i < nrpermissions ; i++)
+ if (0 == strcmp(permissions[i].name, name))
+ return permissions+i;
+ return NULL;
+}
+
+/* add a permission of name */
+static struct permission *add_permission(const char *name)
+{
+ struct permission *p = get_permission(name);
+ if (!p) {
+ p = realloc(permissions, ((nrpermissions + 8) & ~7) * sizeof(*p));
+ if (p) {
+ permissions = p;
+ p = permissions + nrpermissions;
+ memset(p, 0, sizeof(*p));
+ p->name = strdup(name);
+ if (!p->name)
+ p = NULL;
+ }
+ }
+ return p;
+}
+
+/* remove any granting */
+void reset_permissions()
+{
+ int i;
+ for (i = 0 ; i < nrpermissions ; i++)
+ permissions[i].granted = 0;
+}
+
+/* remove any granting */
+void crop_permissions(unsigned level)
+{
+ int i;
+ for (i = 0 ; i < nrpermissions ; i++)
+ if (permissions[i].level < level)
+ permissions[i].granted = 0;
+}
+
+/* add permissions granted for installation */
+void grant_permission_list(const char *list)
+{
+ struct permission *p;
+ char *iter, c;
+ int n;
+ static const char separators[] = " \t\n\r,";
+
+ iter = strdupa(list);
+ iter += strspn(iter, separators);
+ while(*iter) {
+ n = strcspn(iter, separators);
+ c = iter[n];
+ iter[n] = 0;
+ p = add_permission(iter);
+ if (!p) {
+ syslog(LOG_ERR, "Can't allocate permission");
+ exit(1);
+ }
+ p->granted = 1;
+ iter += n;
+ *iter =c;
+ iter += strspn(iter, separators);
+ }
+}
+
+/* checks if the permission 'name' is recorded */
+int permission_exists(const char *name)
+{
+ return !!get_permission(name);
+}
+
+/* request the permission, returns 1 if granted or 0 otherwise */
+int request_permission(const char *name)
+{
+ struct permission *p = get_permission(name);
+ if (p) {
+ p->requested = 1;
+ if (p->granted)
+ return 1;
+ }
+ return 0;
+}
+