X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;ds=inline;f=src%2Fwgtpkg-permissions.c;fp=src%2Fwgtpkg-permissions.c;h=25758e407a89faeb1f9b741a88d8fbc0f6c08ca8;hb=9ab266df6642c6e930e03b3024d7c3d53ef88bbc;hp=0000000000000000000000000000000000000000;hpb=26d75de504d808191dbadc9a735009a214a789b0;p=src%2Fapp-framework-main.git 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 +#include +#include + +#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; +} +