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.
31 #include "wgtpkg-install.h"
32 #include "secmgr-wrap.h"
33 #include "utils-dir.h"
35 static int check_defined(const void *data, const char *name)
39 ERROR("widget has no defined '%s' (temporary constraints)", name);
44 static int check_valid_string(const char *value, const char *name)
49 if (check_defined(value, name))
54 if (!isalnum(c) && !strchr(".-_", c)) {
55 ERROR("forbidden char %c in '%s' -> '%s' (temporary constraints)", c, name, value);
64 static int check_temporary_constraints(const struct wgt_desc *desc)
66 int result = check_valid_string(desc->id, "id");
67 result |= check_valid_string(desc->version, "version");
68 result |= check_defined(desc->icons, "icon");
69 result |= check_defined(desc->content_src, "content");
72 if (desc->icons->next) {
73 ERROR("widget has more than one icon defined (temporary constraints)");
80 static int check_permissions(const char *name, int required)
82 if (permission_exists(name)) {
83 if (request_permission(name)) {
84 DEBUG("granted permission: %s", name);
85 } else if (required) {
86 ERROR("ungranted permission required: %s", name);
90 INFO("ungranted permission optional: %s", name);
96 static int check_widget(const struct wgt_desc *desc)
99 const struct wgt_desc_feature *feature;
101 result = check_temporary_constraints(desc);
102 feature = desc->features;
104 if (!check_permissions(feature->name, feature->required))
106 feature = feature->next;
111 static int move_widget(const char *root, const struct wgt_desc *desc, int force)
113 char newdir[PATH_MAX];
116 rc = snprintf(newdir, sizeof newdir, "%s/%s/%s", root, desc->id, desc->version);
117 if (rc >= sizeof newdir) {
118 ERROR("path to long in move_widget");
123 return move_workdir(newdir, 1, force);
126 static int install_icon(const struct wgt_desc *desc)
129 char target[PATH_MAX];
132 create_directory(FWK_ICON_DIR, 0755, 1);
133 rc = snprintf(link, sizeof link, "%s/%s@%s", FWK_ICON_DIR, desc->id, desc->version);
134 if (rc >= sizeof link) {
135 ERROR("link to long in install_icon");
140 rc = snprintf(target, sizeof target, "%s/%s", workdir, desc->icons->src);
141 if (rc >= sizeof target) {
142 ERROR("target to long in install_icon");
148 rc = symlink(target, link);
150 ERROR("can't create link %s -> %s", link, target);
154 static int install_security(const struct wgt_desc *desc)
156 char path[PATH_MAX], *head;
157 const char *icon, *perm;
158 int rc, len, lic, lf;
162 rc = secmgr_init(desc->id);
166 rc = secmgr_path_public_read_only(workdir);
170 /* instal the files */
171 head = stpcpy(path, workdir);
172 assert(sizeof path > (head - path));
173 len = (int)(sizeof path - (head - path));
175 ERROR("root path too long in install_security");
176 errno = ENAMETOOLONG;
181 icon = desc->icons->src;
182 lic = (int)strlen(icon);
186 f = file_of_index(i++);
187 lf = (int)strlen(f->name);
189 ERROR("path too long in install_security");
190 errno = ENAMETOOLONG;
193 strcpy(head, f->name);
194 if (lf <= lic && !memcmp(f->name, icon, lf) && (!f->name[lf] || f->name[lf] == '/'))
195 rc = secmgr_path_public_read_only(path);
197 rc = secmgr_path_read_only(path);
202 /* install the permissions */
203 perm = first_usable_permission();
205 rc = secmgr_permit(perm);
208 perm = next_usable_permission();
211 rc = secmgr_install();
219 /* install the widget of the file */
220 struct wgt_info *install_widget(const char *wgtfile, const char *root, int force)
222 struct wgt_info *ifo;
223 const struct wgt_desc *desc;
225 NOTICE("-- INSTALLING widget %s to %s --", wgtfile, root);
228 create_directory(root, 0755, 1);
229 if (make_workdir_base(root, "TMP", 0)) {
230 ERROR("failed to create a working directory");
234 if (zread(wgtfile, 0))
237 if (check_all_signatures())
240 ifo = wgt_info_createat(workdirfd, NULL, 1, 1, 1);
244 desc = wgt_info_desc(ifo);
245 if (check_widget(desc))
248 if (move_widget(root, desc, force))
251 if (install_icon(desc))
254 if (install_security(desc))