63704efff58f23f2fd60c1922e85e63f88d1faff
[src/app-framework-main.git] / src / secmgr-wrap.c
1 /*
2  Copyright (C) 2015-2020 IoT.bzh
3
4  author: José Bollo <jose.bollo@iot.bzh>
5
6  Licensed under the Apache License, Version 2.0 (the "License");
7  you may not use this file except in compliance with the License.
8  You may obtain a copy of the License at
9
10      http://www.apache.org/licenses/LICENSE-2.0
11
12  Unless required by applicable law or agreed to in writing, software
13  distributed under the License is distributed on an "AS IS" BASIS,
14  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  See the License for the specific language governing permissions and
16  limitations under the License.
17 */
18
19 #include <string.h>
20 #include <errno.h>
21 #include <assert.h>
22
23 #if SIMULATE_SECURITY_MANAGER
24 #include "simulation/security-manager.h"
25 #else
26 #include <security-manager.h>
27 #endif
28
29 #include "verbose.h"
30 #include "secmgr-wrap.h"
31
32 static app_inst_req *request = NULL;
33
34 static int retcode(enum lib_retcode rc)
35 {
36         switch (rc) {
37         case SECURITY_MANAGER_SUCCESS: return 0;
38         case SECURITY_MANAGER_ERROR_INPUT_PARAM: errno = EINVAL; break;
39         case SECURITY_MANAGER_ERROR_MEMORY: errno = ENOMEM; break;
40         case SECURITY_MANAGER_ERROR_REQ_NOT_COMPLETE: errno = EBADMSG; break;
41         case SECURITY_MANAGER_ERROR_AUTHENTICATION_FAILED: errno = EPERM; break;
42         case SECURITY_MANAGER_ERROR_ACCESS_DENIED: errno = EACCES; break;
43         default: errno = ECANCELED; break;
44         }
45         return -1;
46 }
47
48 int secmgr_init(const char *id)
49 {
50         int rc;
51         assert(request == NULL);
52         rc = security_manager_app_inst_req_new(&request);
53         if (rc != SECURITY_MANAGER_SUCCESS)
54                 ERROR("security_manager_app_inst_req_new failed");
55         else {
56                 rc = security_manager_app_inst_req_set_pkg_id(request, id);
57                 if (rc != SECURITY_MANAGER_SUCCESS)
58                         ERROR("security_manager_app_inst_req_set_pkg_id failed");
59                 else {
60                         rc = security_manager_app_inst_req_set_app_id(request, id);
61                         if (rc != SECURITY_MANAGER_SUCCESS)
62                                 ERROR("security_manager_app_inst_req_set_app_id failed");
63                 }
64         }
65         if (rc != SECURITY_MANAGER_SUCCESS)
66                 secmgr_cancel();
67         return retcode(rc);
68 }
69
70 void secmgr_cancel()
71 {
72         security_manager_app_inst_req_free(request);
73         request = NULL;
74 }
75
76 int secmgr_install()
77 {
78         int rc;
79         assert(request != NULL);
80         rc = security_manager_app_install(request);
81         if (rc != SECURITY_MANAGER_SUCCESS)
82                 ERROR("security_manager_app_install failed");
83         secmgr_cancel();
84         return retcode(rc);
85 }
86
87 int secmgr_uninstall()
88 {
89         int rc;
90         assert(request != NULL);
91         rc = security_manager_app_uninstall(request);
92         if (rc != SECURITY_MANAGER_SUCCESS)
93                 ERROR("security_manager_app_uninstall failed");
94         secmgr_cancel();
95         return retcode(rc);
96 }
97
98 int secmgr_permit(const char *permission)
99 {
100         int rc;
101         assert(request != NULL);
102         rc = security_manager_app_inst_req_add_privilege(request, permission);
103         if (rc != SECURITY_MANAGER_SUCCESS)
104                 ERROR("security_manager_app_inst_add_privilege %s failed", permission);
105         return retcode(rc);
106 }
107
108 static int addpath(const char *pathname, enum app_install_path_type type)
109 {
110         int rc;
111         assert(request != NULL);
112         rc = security_manager_app_inst_req_add_path(request, pathname, type);
113         if (rc != SECURITY_MANAGER_SUCCESS)
114                 ERROR("security_manager_app_inst_add_path %s failed", pathname);
115         return retcode(rc);
116 }
117
118 int secmgr_path_public_read_only(const char *pathname)
119 {
120         return addpath(pathname, SECURITY_MANAGER_PATH_RO);
121 }
122
123 int secmgr_path_read_only(const char *pathname)
124 {
125         return addpath(pathname, SECURITY_MANAGER_PATH_RO);
126 }
127
128 int secmgr_path_read_write(const char *pathname)
129 {
130         return addpath(pathname, SECURITY_MANAGER_PATH_RW);
131 }
132
133 int secmgr_prepare_exec(const char *appid)
134 {
135         return retcode(security_manager_prepare_app(appid));
136 }
137