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 "secmgr-wrap.h"
32 #include "utils-dir.h"
34 static int check_defined(const void *data, const char *name)
38 ERROR("widget has no defined '%s' (temporary constraints)", name);
43 static int check_valid_string(const char *value, const char *name)
48 if (check_defined(value, name))
53 if (!isalnum(c) && !strchr(".-_", c)) {
54 ERROR("forbidden char %c in '%s' -> '%s' (temporary constraints)", c, name, value);
63 static int check_temporary_constraints(const struct wgt_desc *desc)
65 int result = check_valid_string(desc->id, "id");
66 result |= check_valid_string(desc->version, "version");
67 result |= check_defined(desc->icons, "icon");
68 result |= check_defined(desc->content_src, "content");
71 if (desc->icons->next) {
72 ERROR("widget has more than one icon defined (temporary constraints)");
79 static int check_permissions(const char *name, int required)
81 if (permission_exists(name)) {
82 if (request_permission(name)) {
83 DEBUG("granted permission: %s", name);
84 } else if (required) {
85 ERROR("ungranted permission required: %s", name);
89 INFO("ungranted permission optional: %s", name);
95 static int check_widget(const struct wgt_desc *desc)
98 const struct wgt_desc_feature *feature;
100 result = check_temporary_constraints(desc);
101 feature = desc->features;
103 if (!check_permissions(feature->name, feature->required))
105 feature = feature->next;
110 static int move_widget(const char *root, const struct wgt_desc *desc, int force)
112 char newdir[PATH_MAX];
115 rc = snprintf(newdir, sizeof newdir, "%s/%s/%s", root, desc->id, desc->version);
116 if (rc >= sizeof newdir) {
117 ERROR("path to long in move_widget");
122 return move_workdir(newdir, 1, force);
125 static int install_icon(const struct wgt_desc *desc)
128 char target[PATH_MAX];
131 create_directory(FWK_ICON_DIR, 0755, 1);
132 rc = snprintf(link, sizeof link, "%s/%s@%s", FWK_ICON_DIR, desc->id, desc->version);
133 if (rc >= sizeof link) {
134 ERROR("link to long in install_icon");
139 rc = snprintf(target, sizeof target, "%s/%s", workdir, desc->icons->src);
140 if (rc >= sizeof target) {
141 ERROR("target to long in install_icon");
147 rc = symlink(target, link);
149 ERROR("can't create link %s -> %s", link, target);
153 static int install_security(const struct wgt_desc *desc)
155 char path[PATH_MAX], *head;
156 const char *icon, *perm;
157 int rc, len, lic, lf;
161 rc = secmgr_init(desc->id);
165 rc = secmgr_path_public_read_only(workdir);
169 /* instal the files */
170 head = stpcpy(path, workdir);
171 assert(sizeof path > (head - path));
172 len = (int)(sizeof path - (head - path));
174 ERROR("root path too long in install_security");
175 errno = ENAMETOOLONG;
180 icon = desc->icons->src;
181 lic = (int)strlen(icon);
185 f = file_of_index(i++);
186 lf = (int)strlen(f->name);
188 ERROR("path too long in install_security");
189 errno = ENAMETOOLONG;
192 strcpy(head, f->name);
193 if (lf <= lic && !memcmp(f->name, icon, lf) && (!f->name[lf] || f->name[lf] == '/'))
194 rc = secmgr_path_public_read_only(path);
196 rc = secmgr_path_read_only(path);
201 /* install the permissions */
202 perm = first_usable_permission();
204 rc = secmgr_permit(perm);
207 perm = next_usable_permission();
210 rc = secmgr_install();
218 /* install the widget of the file */
219 void install_widget(const char *wgtfile, const char *root, int force)
221 struct wgt_info *ifo;
222 const struct wgt_desc *desc;
224 NOTICE("-- INSTALLING widget %s --", wgtfile);
227 create_directory(root, 0755, 1);
228 if (make_workdir_base(root, "TMP", 0)) {
229 ERROR("failed to create a working directory");
233 if (zread(wgtfile, 0))
236 if (check_all_signatures())
239 ifo = wgt_info_createat(workdirfd, NULL, 1, 1, 1);
243 desc = wgt_info_desc(ifo);
244 if (check_widget(desc))
247 if (move_widget(root, desc, force))
250 if (install_icon(desc))
253 if (install_security(desc))