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 ERROR("empty string forbidden in '%s' (temporary constraints)", name);
65 if (!isalnum(c) && !strchr(".-_", c)) {
66 ERROR("forbidden char %c in '%s' -> '%s' (temporary constraints)", c, name, value);
75 static int check_temporary_constraints(const struct wgt_desc *desc)
77 int result = check_valid_string(desc->id, "id");
78 result |= check_valid_string(desc->version, "version");
79 result |= check_valid_string(desc->ver, "ver");
80 result |= check_defined(desc->icons, "icon");
81 result |= check_defined(desc->content_src, "content");
84 if (desc->icons->next) {
85 ERROR("widget has more than one icon defined (temporary constraints)");
92 static int check_permissions(const char *name, int required)
94 if (permission_exists(name)) {
95 if (request_permission(name)) {
96 DEBUG("granted permission: %s", name);
97 } else if (required) {
98 ERROR("ungranted permission required: %s", name);
102 INFO("ungranted permission optional: %s", name);
108 static int check_widget(const struct wgt_desc *desc)
111 const struct wgt_desc_feature *feature;
113 result = check_temporary_constraints(desc);
114 feature = desc->features;
116 if (!check_permissions(feature->name, feature->required))
118 feature = feature->next;
123 static int move_widget(const char *root, const struct wgt_desc *desc, int force)
125 char newdir[PATH_MAX];
128 rc = snprintf(newdir, sizeof newdir, "%s/%s/%s", root, desc->id, desc->ver);
129 if (rc >= (int)sizeof newdir) {
130 ERROR("path to long in move_widget");
135 return move_workdir(newdir, 1, force);
138 static int install_icon(const struct wgt_desc *desc)
141 char target[PATH_MAX];
144 create_directory(FWK_ICON_DIR, 0755, 1);
145 rc = snprintf(link, sizeof link, "%s/%s", FWK_ICON_DIR, desc->idaver);
146 if (rc >= (int)sizeof link) {
147 ERROR("link to long in install_icon");
152 rc = snprintf(target, sizeof target, "%s/%s", workdir, desc->icons->src);
153 if (rc >= (int)sizeof target) {
154 ERROR("target to long in install_icon");
160 rc = symlink(target, link);
162 ERROR("can't create link %s -> %s", link, target);
166 static int install_security(const struct wgt_desc *desc)
168 char path[PATH_MAX], *head;
169 const char *icon, *perm;
171 unsigned int i, n, len, lic, lf;
174 rc = secmgr_init(desc->id);
178 rc = secmgr_path_public_read_only(workdir);
182 /* instal the files */
183 head = stpcpy(path, workdir);
184 assert(head < path + sizeof path);
185 len = (unsigned)((path + sizeof path) - head);
187 ERROR("root path too long in install_security");
188 errno = ENAMETOOLONG;
193 icon = desc->icons->src;
194 lic = (unsigned)strlen(icon);
198 f = file_of_index(i++);
199 lf = (unsigned)strlen(f->name);
201 ERROR("path too long in install_security");
202 errno = ENAMETOOLONG;
205 strcpy(head, f->name);
206 if (lf <= lic && !memcmp(f->name, icon, lf) && (!f->name[lf] || f->name[lf] == '/'))
207 rc = secmgr_path_public_read_only(path);
209 rc = secmgr_path_read_only(path);
214 /* install the permissions */
215 perm = first_usable_permission();
217 rc = secmgr_permit(perm);
220 perm = next_usable_permission();
223 rc = secmgr_install();
231 /* install the widget of the file */
232 struct wgt_info *install_widget(const char *wgtfile, const char *root, int force)
234 struct wgt_info *ifo;
235 const struct wgt_desc *desc;
237 NOTICE("-- INSTALLING widget %s to %s --", wgtfile, root);
240 create_directory(root, 0755, 1);
241 if (make_workdir(root, "TMP", 0)) {
242 ERROR("failed to create a working directory");
246 if (zread(wgtfile, 0))
249 if (check_all_signatures())
252 ifo = wgt_info_createat(workdirfd, NULL, 1, 1, 1);
256 reset_requested_permissions();
257 desc = wgt_info_desc(ifo);
258 if (check_widget(desc))
261 if (move_widget(root, desc, force))
264 if (install_icon(desc))
267 if (install_security(desc))