Enforce numeric application IDs
[src/app-framework-main.git] / src / wgtpkg-uninstall.c
1 /*
2  Copyright (C) 2015-2019 IoT.bzh
3
4  author: José Bollo <jose.bollo@iot.bzh>
5
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
9
10      http://www.apache.org/licenses/LICENSE-2.0
11
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.
17 */
18
19 #define _GNU_SOURCE
20
21 #include <limits.h>
22 #include <errno.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include <assert.h>
26 #include <unistd.h>
27 #include <fcntl.h>
28
29 #include "verbose.h"
30 #include "utils-dir.h"
31 #include "secmgr-wrap.h"
32 #include "wgtpkg-unit.h"
33 #include "wgt.h"
34 #include "wgt-info.h"
35
36 /* uninstall the widget of idaver */
37 int uninstall_widget(const char *idaver, const char *root)
38 {
39         char *id;
40         char *ver;
41         char path[PATH_MAX];
42         const char *at;
43         int rc, rc2;
44         struct unitconf uconf;
45         struct wgt_info *ifo;
46
47         NOTICE("-- UNINSTALLING widget of id %s from %s --", idaver, root);
48
49         /* find the last '@' of the id */
50         at = strrchr(idaver, '@');
51         if (at == NULL) {
52                 ERROR("bad widget id '%s', no @", idaver);
53                 errno = EINVAL;
54                 return -1;
55         }
56         id = strndupa(idaver, (size_t)(at - idaver));
57         ver = strdupa(at + 1);
58
59         /* compute the path */
60         rc = snprintf(path, sizeof path, "%s/%s/%s", root, id, ver);
61         if (rc >= (int)sizeof path) {
62                 ERROR("bad widget id '%s', too long", idaver);
63                 errno = EINVAL;
64                 return -1;
65         }
66
67         /* removes the units */
68         ifo = wgt_info_createat(AT_FDCWD, path, 1, 1, 1);
69         if (!ifo) {
70                 ERROR("can't read widget config in directory '%s': %m", path);
71                 return -1;
72         }
73         uconf.installdir = path;
74         uconf.icondir = FWK_ICON_DIR;
75         uconf.new_afid = 0;
76         uconf.base_http_ports = 0;
77         unit_uninstall(ifo, &uconf);
78         wgt_info_unref(ifo);
79
80         /* removes the directory of the application */
81         rc = remove_directory(path, 1);
82         if (rc < 0) {
83                 ERROR("while removing directory '%s': %m", path);
84                 return -1;
85         }
86
87         /* removes the icon of the application */
88         rc = snprintf(path, sizeof path, "%s/%s", FWK_ICON_DIR, idaver);
89         assert(rc < (int)sizeof path);
90         rc = unlink(path);
91         if (rc < 0 && errno != ENOENT)
92                 ERROR("can't remove '%s': %m", path);
93
94         /* removes the parent directory if empty */
95         rc2 = snprintf(path, sizeof path, "%s/%s", root, id);
96         assert(rc2 < (int)sizeof path);
97         rc2 = rmdir(path);
98         if (rc < 0 && errno == ENOTEMPTY)
99                 return rc;
100         if (rc < 0) {
101                 ERROR("while removing directory '%s': %m", path);
102                 return -1;
103         }
104
105         /*
106          * parent directory removed: last occurrence of the application
107          * uninstall it for the security-manager
108          */
109         rc2 = secmgr_init(id);
110         if (rc2) {
111                 ERROR("can't init security manager context");
112                 return -1;
113         }
114         rc2 = secmgr_uninstall();
115         if (rc2) {
116                 ERROR("can't uninstall security manager context");
117                 return -1;
118         }
119         return rc;
120 }
121