work in progress
[src/app-framework-main.git] / src / wgtpkg-install.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 #define _GNU_SOURCE
18
19 #include <errno.h>
20 #include <syslog.h>
21 #include <string.h>
22 #include <ctype.h>
23
24 #include "verbose.h"
25 #include "wgtpkg.h"
26 #include "wgt.h"
27 #include "wgt-info.h"
28 #include "secmgr-wrap.h"
29
30 static int check_defined(const void *data, const char *name)
31 {
32         if (data)
33                 return 0;
34         syslog(LOG_ERR, "widget has no defined '%s' (temporary constraints)", name);
35         errno = EINVAL;
36         return -1;
37 }
38
39 static int check_valid_string(const char *value, const char *name)
40 {
41         int pos;
42         char c;
43
44         if (check_defined(value, name))
45                 return -1;
46         pos = 0;
47         c = value[pos];
48         while(c) {
49                 if (!isalnum(c) && !strchr(".-_", c)) {
50                         syslog(LOG_ERR, "forbidden char %c in '%s' -> '%s' (temporary constraints)", c, name, value);
51                         errno = EINVAL;
52                         return -1;                      
53                 }
54                 c = value[++pos];
55         }
56         return 0;
57 }
58
59 static int check_temporary_constraints(const struct wgt_desc *desc)
60 {
61         int result = check_valid_string(desc->id, "id");
62         result |= check_valid_string(desc->version, "version");
63         result |= check_defined(desc->icons, "icon");
64         result |= check_defined(desc->content_src, "content");
65         if (result)
66                 return result;
67         if (desc->icons->next) {
68                 syslog(LOG_ERR, "widget has more than one icon defined (temporary constraints)");
69                 errno = EINVAL;
70                 result = -1;
71         }
72         return 0;
73 }
74
75 static int check_permissions(const char *name, int required)
76 {
77         if (permission_exists(name)) {
78                 if (request_permission(name)) {
79                         debug("granted permission: %s", name);
80                 } else if (required) {
81                         syslog(LOG_ERR, "ungranted permission required: %s", name);
82                         errno = EPERM;
83                         return 0;
84                 } else {
85                         notice("ungranted permission optional: %s", name);
86                 }
87         }
88         return 1;
89 }
90
91 static int check_widget(const struct wgt_desc *desc)
92 {
93         int result;
94         const struct wgt_desc_feature *feature;
95
96         result = check_temporary_constraints(desc);
97         feature = desc->features;
98         while(feature) {
99                 if (!check_permissions(feature->name, feature->required))
100                         result = -1;
101                 feature = feature->next;
102         }
103         return result;
104 }
105
106 static int move_widget(const char *root, const struct wgt_desc *desc, int force)
107 {
108         char newdir[PATH_MAX];
109         int rc;
110
111         rc = snprintf(newdir, sizeof newdir, "%s/%s/%s", root, desc->id, desc->version);
112         if (rc >= sizeof newdir) {
113                 syslog(LOG_ERR, "path to long: %s/%s/%s", root, desc->id, desc->version);
114                 errno = EINVAL;
115                 return -1;
116         }
117
118         return move_workdir(newdir, 1, force);
119 }
120
121 static int install_security(struct wgt_info *ifo)
122 {
123         int rc;
124
125         rc = secmgr_init(wgt_info_desc(ifo)->
126 }
127
128 /* install the widget of the file */
129 void install_widget(const char *wgtfile, const char *root, int force)
130 {
131         struct wgt_info *ifo;
132         const struct wgt_desc *desc;
133
134         notice("-- INSTALLING widget %s --", wgtfile);
135
136         /* workdir */
137         if (make_workdir_base(root, "UNPACK", 0)) {
138                 syslog(LOG_ERR, "failed to create a working directory");
139                 goto error1;
140         }
141
142         if (zread(wgtfile, 0))
143                 goto error2;
144
145         if (check_all_signatures())
146                 goto error2;
147
148         ifo = wgt_info_createat(workdirfd, NULL, 1, 1, 1);
149         if (!ifo)
150                 goto error2;
151
152         desc = wgt_info_desc(ifo);
153         if (check_widget(desc))
154                 goto error3;
155
156         if (move_widget(root, desc, force))
157                 goto error3;
158
159         
160         
161         return;
162
163 error3:
164         wgt_info_unref(ifo);
165
166 error2:
167         remove_workdir();
168
169 error1:
170         return;
171 }
172