4 author: José Bollo <jose.bollo@iot.bzh>
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
10 http://www.apache.org/licenses/LICENSE-2.0
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.
32 #include "wgtpkg-files.h"
33 #include "wgtpkg-workdir.h"
34 #include "wgtpkg-zip.h"
35 #include "wgtpkg-permissions.h"
36 #include "wgtpkg-digsig.h"
37 #include "wgtpkg-install.h"
38 #include "secmgr-wrap.h"
39 #include "utils-dir.h"
41 static int check_defined(const void *data, const char *name)
45 ERROR("widget has no defined '%s' (temporary constraints)", name);
50 static int check_valid_string(const char *value, const char *name)
55 if (check_defined(value, name))
60 if (!isalnum(c) && !strchr(".-_", c)) {
61 ERROR("forbidden char %c in '%s' -> '%s' (temporary constraints)", c, name, value);
70 static int check_temporary_constraints(const struct wgt_desc *desc)
72 int result = check_valid_string(desc->id, "id");
73 result |= check_valid_string(desc->version, "version");
74 result |= check_valid_string(desc->ver, "ver");
75 result |= check_defined(desc->icons, "icon");
76 result |= check_defined(desc->content_src, "content");
79 if (desc->icons->next) {
80 ERROR("widget has more than one icon defined (temporary constraints)");
87 static int check_permissions(const char *name, int required)
89 if (permission_exists(name)) {
90 if (request_permission(name)) {
91 DEBUG("granted permission: %s", name);
92 } else if (required) {
93 ERROR("ungranted permission required: %s", name);
97 INFO("ungranted permission optional: %s", name);
103 static int check_widget(const struct wgt_desc *desc)
106 const struct wgt_desc_feature *feature;
108 result = check_temporary_constraints(desc);
109 feature = desc->features;
111 if (!check_permissions(feature->name, feature->required))
113 feature = feature->next;
118 static int move_widget(const char *root, const struct wgt_desc *desc, int force)
120 char newdir[PATH_MAX];
123 rc = snprintf(newdir, sizeof newdir, "%s/%s/%s", root, desc->id, desc->ver);
124 if (rc >= sizeof newdir) {
125 ERROR("path to long in move_widget");
130 return move_workdir(newdir, 1, force);
133 static int install_icon(const struct wgt_desc *desc)
136 char target[PATH_MAX];
139 create_directory(FWK_ICON_DIR, 0755, 1);
140 rc = snprintf(link, sizeof link, "%s/%s@%s", FWK_ICON_DIR, desc->id, desc->ver);
141 if (rc >= sizeof link) {
142 ERROR("link to long in install_icon");
147 rc = snprintf(target, sizeof target, "%s/%s", workdir, desc->icons->src);
148 if (rc >= sizeof target) {
149 ERROR("target to long in install_icon");
155 rc = symlink(target, link);
157 ERROR("can't create link %s -> %s", link, target);
161 static int install_security(const struct wgt_desc *desc)
163 char path[PATH_MAX], *head;
164 const char *icon, *perm;
165 int rc, len, lic, lf;
169 rc = secmgr_init(desc->id);
173 rc = secmgr_path_public_read_only(workdir);
177 /* instal the files */
178 head = stpcpy(path, workdir);
179 assert(sizeof path > (head - path));
180 len = (int)(sizeof path - (head - path));
182 ERROR("root path too long in install_security");
183 errno = ENAMETOOLONG;
188 icon = desc->icons->src;
189 lic = (int)strlen(icon);
193 f = file_of_index(i++);
194 lf = (int)strlen(f->name);
196 ERROR("path too long in install_security");
197 errno = ENAMETOOLONG;
200 strcpy(head, f->name);
201 if (lf <= lic && !memcmp(f->name, icon, lf) && (!f->name[lf] || f->name[lf] == '/'))
202 rc = secmgr_path_public_read_only(path);
204 rc = secmgr_path_read_only(path);
209 /* install the permissions */
210 perm = first_usable_permission();
212 rc = secmgr_permit(perm);
215 perm = next_usable_permission();
218 rc = secmgr_install();
226 /* install the widget of the file */
227 struct wgt_info *install_widget(const char *wgtfile, const char *root, int force)
229 struct wgt_info *ifo;
230 const struct wgt_desc *desc;
232 NOTICE("-- INSTALLING widget %s to %s --", wgtfile, root);
235 create_directory(root, 0755, 1);
236 if (make_workdir(root, "TMP", 0)) {
237 ERROR("failed to create a working directory");
241 if (zread(wgtfile, 0))
244 if (check_all_signatures())
247 ifo = wgt_info_createat(workdirfd, NULL, 1, 1, 1);
251 desc = wgt_info_desc(ifo);
252 if (check_widget(desc))
255 if (move_widget(root, desc, force))
258 if (install_icon(desc))
261 if (install_security(desc))