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_defined(desc->icons, "icon");
75 result |= check_defined(desc->content_src, "content");
78 if (desc->icons->next) {
79 ERROR("widget has more than one icon defined (temporary constraints)");
86 static int check_permissions(const char *name, int required)
88 if (permission_exists(name)) {
89 if (request_permission(name)) {
90 DEBUG("granted permission: %s", name);
91 } else if (required) {
92 ERROR("ungranted permission required: %s", name);
96 INFO("ungranted permission optional: %s", name);
102 static int check_widget(const struct wgt_desc *desc)
105 const struct wgt_desc_feature *feature;
107 result = check_temporary_constraints(desc);
108 feature = desc->features;
110 if (!check_permissions(feature->name, feature->required))
112 feature = feature->next;
117 static int move_widget(const char *root, const struct wgt_desc *desc, int force)
119 char newdir[PATH_MAX];
122 rc = snprintf(newdir, sizeof newdir, "%s/%s/%s", root, desc->id, desc->version);
123 if (rc >= sizeof newdir) {
124 ERROR("path to long in move_widget");
129 return move_workdir(newdir, 1, force);
132 static int install_icon(const struct wgt_desc *desc)
135 char target[PATH_MAX];
138 create_directory(FWK_ICON_DIR, 0755, 1);
139 rc = snprintf(link, sizeof link, "%s/%s@%s", FWK_ICON_DIR, desc->id, desc->version);
140 if (rc >= sizeof link) {
141 ERROR("link to long in install_icon");
146 rc = snprintf(target, sizeof target, "%s/%s", workdir, desc->icons->src);
147 if (rc >= sizeof target) {
148 ERROR("target to long in install_icon");
154 rc = symlink(target, link);
156 ERROR("can't create link %s -> %s", link, target);
160 static int install_security(const struct wgt_desc *desc)
162 char path[PATH_MAX], *head;
163 const char *icon, *perm;
164 int rc, len, lic, lf;
168 rc = secmgr_init(desc->id);
172 rc = secmgr_path_public_read_only(workdir);
176 /* instal the files */
177 head = stpcpy(path, workdir);
178 assert(sizeof path > (head - path));
179 len = (int)(sizeof path - (head - path));
181 ERROR("root path too long in install_security");
182 errno = ENAMETOOLONG;
187 icon = desc->icons->src;
188 lic = (int)strlen(icon);
192 f = file_of_index(i++);
193 lf = (int)strlen(f->name);
195 ERROR("path too long in install_security");
196 errno = ENAMETOOLONG;
199 strcpy(head, f->name);
200 if (lf <= lic && !memcmp(f->name, icon, lf) && (!f->name[lf] || f->name[lf] == '/'))
201 rc = secmgr_path_public_read_only(path);
203 rc = secmgr_path_read_only(path);
208 /* install the permissions */
209 perm = first_usable_permission();
211 rc = secmgr_permit(perm);
214 perm = next_usable_permission();
217 rc = secmgr_install();
225 /* install the widget of the file */
226 struct wgt_info *install_widget(const char *wgtfile, const char *root, int force)
228 struct wgt_info *ifo;
229 const struct wgt_desc *desc;
231 NOTICE("-- INSTALLING widget %s to %s --", wgtfile, root);
234 create_directory(root, 0755, 1);
235 if (make_workdir_base(root, "TMP", 0)) {
236 ERROR("failed to create a working directory");
240 if (zread(wgtfile, 0))
243 if (check_all_signatures())
246 ifo = wgt_info_createat(workdirfd, NULL, 1, 1, 1);
250 desc = wgt_info_desc(ifo);
251 if (check_widget(desc))
254 if (move_widget(root, desc, force))
257 if (install_icon(desc))
260 if (install_security(desc))