4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
30 #include "secmgr-wrap.h"
32 static int check_defined(const void *data, const char *name)
36 ERROR("widget has no defined '%s' (temporary constraints)", name);
41 static int check_valid_string(const char *value, const char *name)
46 if (check_defined(value, name))
51 if (!isalnum(c) && !strchr(".-_", c)) {
52 ERROR("forbidden char %c in '%s' -> '%s' (temporary constraints)", c, name, value);
61 static int check_temporary_constraints(const struct wgt_desc *desc)
63 int result = check_valid_string(desc->id, "id");
64 result |= check_valid_string(desc->version, "version");
65 result |= check_defined(desc->icons, "icon");
66 result |= check_defined(desc->content_src, "content");
69 if (desc->icons->next) {
70 ERROR("widget has more than one icon defined (temporary constraints)");
77 static int check_permissions(const char *name, int required)
79 if (permission_exists(name)) {
80 if (request_permission(name)) {
81 DEBUG("granted permission: %s", name);
82 } else if (required) {
83 ERROR("ungranted permission required: %s", name);
87 INFO("ungranted permission optional: %s", name);
93 static int check_widget(const struct wgt_desc *desc)
96 const struct wgt_desc_feature *feature;
98 result = check_temporary_constraints(desc);
99 feature = desc->features;
101 if (!check_permissions(feature->name, feature->required))
103 feature = feature->next;
108 static int move_widget(const char *root, const struct wgt_desc *desc, int force)
110 char newdir[PATH_MAX];
113 rc = snprintf(newdir, sizeof newdir, "%s/%s/%s", root, desc->id, desc->version);
114 if (rc >= sizeof newdir) {
115 ERROR("path to long in move_widget");
120 return move_workdir(newdir, 1, force);
123 static int install_icon(const struct wgt_desc *desc)
126 char target[PATH_MAX];
129 rc = snprintf(link, sizeof link, "%s/%s@%s", ICONDESTDIR, desc->id, desc->version);
130 if (rc >= sizeof link) {
131 ERROR("link to long in install_icon");
136 rc = snprintf(target, sizeof target, "%s/%s", workdir, desc->icons->src);
137 if (rc >= sizeof target) {
138 ERROR("target to long in install_icon");
144 rc = symlink(target, link);
146 ERROR("can't create link %s -> %s", link, target);
150 static int install_security(const struct wgt_desc *desc)
152 char path[PATH_MAX], *head;
153 const char *icon, *perm;
154 int rc, len, lic, lf;
158 rc = secmgr_init(desc->id);
162 rc = secmgr_path_public_read_only(workdir);
166 /* instal the files */
167 head = stpcpy(path, workdir);
168 assert(sizeof path > (head - path));
169 len = (int)(sizeof path - (head - path));
171 ERROR("root path too long in install_security");
172 errno = ENAMETOOLONG;
177 icon = desc->icons->src;
178 lic = (int)strlen(icon);
182 f = file_of_index(i++);
183 lf = (int)strlen(f->name);
185 ERROR("path too long in install_security");
186 errno = ENAMETOOLONG;
189 strcpy(head, f->name);
190 if (lf <= lic && !memcmp(f->name, icon, lf) && (!f->name[lf] || f->name[lf] == '/'))
191 rc = secmgr_path_public_read_only(path);
193 rc = secmgr_path_read_only(path);
198 /* install the permissions */
199 perm = first_usable_permission();
201 rc = secmgr_permit(perm);
204 perm = next_usable_permission();
207 rc = secmgr_install();
215 /* install the widget of the file */
216 void install_widget(const char *wgtfile, const char *root, int force)
218 struct wgt_info *ifo;
219 const struct wgt_desc *desc;
221 NOTICE("-- INSTALLING widget %s --", wgtfile);
224 if (make_workdir_base(root, "TMP", 0)) {
225 ERROR("failed to create a working directory");
229 if (zread(wgtfile, 0))
232 if (check_all_signatures())
235 ifo = wgt_info_createat(workdirfd, NULL, 1, 1, 1);
239 desc = wgt_info_desc(ifo);
240 if (check_widget(desc))
243 if (move_widget(root, desc, force))
246 if (install_icon(desc))
249 if (install_security(desc))