doc: add new document quick-tutorial
[src/app-framework-main.git] / src / secmgr-wrap.c
1 /*
2  Copyright 2015 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 #include <security-manager.h>
24
25 #include "verbose.h"
26 #include "secmgr-wrap.h"
27
28 static app_inst_req *request = NULL;
29
30 static int retcode(enum lib_retcode rc)
31 {
32         switch (rc) {
33         case SECURITY_MANAGER_SUCCESS: return 0;
34         case SECURITY_MANAGER_ERROR_INPUT_PARAM: errno = EINVAL; break;
35         case SECURITY_MANAGER_ERROR_MEMORY: errno = ENOMEM; break;
36         case SECURITY_MANAGER_ERROR_REQ_NOT_COMPLETE: errno = EBADMSG; break;
37         case SECURITY_MANAGER_ERROR_AUTHENTICATION_FAILED: errno = EPERM; break;
38         case SECURITY_MANAGER_ERROR_ACCESS_DENIED: errno = EACCES; break;
39         default: errno = ECANCELED; break;
40         }
41         return -1;
42 }
43
44 int secmgr_init(const char *id)
45 {
46         int rc;
47         assert(request == NULL);
48         rc = security_manager_app_inst_req_new(&request);
49         if (rc != SECURITY_MANAGER_SUCCESS)
50                 ERROR("security_manager_app_inst_req_new failed");
51         else {
52                 rc = security_manager_app_inst_req_set_pkg_id(request, id);
53                 if (rc != SECURITY_MANAGER_SUCCESS)
54                         ERROR("security_manager_app_inst_req_set_pkg_id failed");
55                 else {
56                         rc = security_manager_app_inst_req_set_app_id(request, id);
57                         if (rc != SECURITY_MANAGER_SUCCESS)
58                                 ERROR("security_manager_app_inst_req_set_app_id failed");
59                 }
60         }
61         if (rc != SECURITY_MANAGER_SUCCESS)
62                 secmgr_cancel();
63         return retcode(rc);
64 }
65
66 void secmgr_cancel()
67 {
68         security_manager_app_inst_req_free(request);
69         request = NULL;
70 }
71
72 int secmgr_install()
73 {
74         int rc;
75         assert(request != NULL);
76         rc = security_manager_app_install(request);
77         if (rc != SECURITY_MANAGER_SUCCESS)
78                 ERROR("security_manager_app_install failed");
79         secmgr_cancel();
80         return retcode(rc);
81 }
82
83 int secmgr_uninstall()
84 {
85         int rc;
86         assert(request != NULL);
87         rc = security_manager_app_uninstall(request);
88         if (rc != SECURITY_MANAGER_SUCCESS)
89                 ERROR("security_manager_app_uninstall failed");
90         secmgr_cancel();
91         return retcode(rc);
92 }
93
94 int secmgr_permit(const char *permission)
95 {
96         int rc;
97         assert(request != NULL);
98         rc = security_manager_app_inst_req_add_privilege(request, permission);
99         if (rc != SECURITY_MANAGER_SUCCESS)
100                 ERROR("security_manager_app_inst_add_privilege %s failed", permission);
101         return retcode(rc);
102 }
103
104 static int addpath(const char *pathname, enum app_install_path_type type)
105 {
106         int rc;
107         assert(request != NULL);
108         rc = security_manager_app_inst_req_add_path(request, pathname, type);
109         if (rc != SECURITY_MANAGER_SUCCESS)
110                 ERROR("security_manager_app_inst_add_path %s failed", pathname);
111         return retcode(rc);
112 }
113
114 int secmgr_path_public_read_only(const char *pathname)
115 {
116         return addpath(pathname, SECURITY_MANAGER_PATH_PUBLIC_RO);
117 }
118
119 int secmgr_path_read_only(const char *pathname)
120 {
121         return addpath(pathname, SECURITY_MANAGER_PATH_RO);
122 }
123
124 int secmgr_path_read_write(const char *pathname)
125 {
126         return addpath(pathname, SECURITY_MANAGER_PATH_RW);
127 }
128
129 int secmgr_prepare_exec(const char *appid)
130 {
131         return retcode(security_manager_prepare_app(appid));
132 }
133