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