/*
- Copyright 2015 IoT.bzh
+ Copyright 2015, 2016, 2017 IoT.bzh
+
+ author: José Bollo <jose.bollo@iot.bzh>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
#include <errno.h>
#include <string.h>
+#include <stdlib.h>
#include "verbose.h"
-#include "wgtpkg.h"
+#include "wgtpkg-permissions.h"
struct permission {
char *name;
unsigned level: 3;
};
-static const char prefix_of_permissions[] = FWK_PREFIX_PERMISSION;
-
-static int nrpermissions = 0;
+static unsigned int nrpermissions = 0;
static struct permission *permissions = NULL;
-static int indexiter = 0;
-
-/* 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);
-}
+static unsigned int indexiter = 0;
/* retrieves the permission of name */
static struct permission *get_permission(const char *name)
{
- int i;
+ unsigned int i;
for (i = 0 ; i < nrpermissions ; i++)
if (0 == strcmp(permissions[i].name, name))
{
struct permission *p = get_permission(name);
if (!p) {
- p = realloc(permissions, ((nrpermissions + 8) & ~7) * sizeof(*p));
+ p = realloc(permissions,
+ ((nrpermissions + 8) & ~(unsigned)7) * sizeof(*p));
if (p) {
permissions = p;
p = permissions + nrpermissions;
p->name = strdup(name);
if (!p->name)
p = NULL;
+ else
+ nrpermissions++;
}
}
return p;
/* remove any granting */
void reset_permissions()
{
- int i;
+ unsigned int i;
+ for (i = 0 ; i < nrpermissions ; i++)
+ permissions[i].granted = permissions[i].requested = 0;
+}
+
+/* remove any requested permission */
+void reset_requested_permissions()
+{
+ unsigned int i;
for (i = 0 ; i < nrpermissions ; i++)
- permissions[i].granted = 0;
+ permissions[i].requested = 0;
}
/* remove any granting */
void crop_permissions(unsigned level)
{
- int i;
+ unsigned 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)
+int grant_permission_list(const char *list)
{
struct permission *p;
char *iter, c;
- int n;
+ unsigned int n;
static const char separators[] = " \t\n\r,";
iter = strdupa(list);
iter += strspn(iter, separators);
while(*iter) {
- n = strcspn(iter, separators);
+ n = (unsigned)strcspn(iter, separators);
c = iter[n];
iter[n] = 0;
p = add_permission(iter);
if (!p) {
- ERROR("Can't allocate permission");
- exit(1);
+ errno = ENOMEM;
+ return -1;
}
p->granted = 1;
iter += n;
*iter =c;
iter += strspn(iter, separators);
}
+ return 0;
}
/* checks if the permission 'name' is recorded */
/* request the permission, returns 1 if granted or 0 otherwise */
int request_permission(const char *name)
{
+#define HACK_ALLOWING_REQUESTED_PERMISSIONS 1
+
struct permission *p = get_permission(name);
+
+#if defined(HACK_ALLOWING_REQUESTED_PERMISSIONS) && HACK_ALLOWING_REQUESTED_PERMISSIONS
+ if (!p)
+ p = add_permission(name);
+#endif
if (p) {
+#if defined(HACK_ALLOWING_REQUESTED_PERMISSIONS) && HACK_ALLOWING_REQUESTED_PERMISSIONS
+ p->granted = 1;
+#endif
p->requested = 1;
if (p->granted)
return 1;
}
return 0;
+
+#undef HACK_ALLOWING_REQUESTED_PERMISSIONS
}
/* iteration over granted and requested permissions */