XBPS Library API 20240111
The X Binary Package System
package_remove.c
1/*-
2 * Copyright (c) 2009-2015 Juan Romero Pardines.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include <sys/stat.h>
27
28#include <dirent.h>
29#include <errno.h>
30#include <fcntl.h>
31#include <libgen.h>
32#include <limits.h>
33#include <stdbool.h>
34#include <stdio.h>
35#include <stdlib.h>
36#include <string.h>
37#include <unistd.h>
38
39#include "xbps_api_impl.h"
40
41static bool
42check_remove_pkg_files(struct xbps_handle *xhp,
43 xbps_array_t obsoletes, const char *pkgver, uid_t euid)
44{
45 struct stat st;
46 bool fail = false;
47
48 if (euid == 0)
49 return false;
50
51 for (unsigned int i = 0; i < xbps_array_count(obsoletes); i++) {
52 const char *file = NULL;
53 xbps_array_get_cstring_nocopy(obsoletes, i, &file);
54 /*
55 * Check if effective user ID owns the file; this is
56 * enough to ensure the user has write permissions
57 * on the directory.
58 */
59 if (lstat(file, &st) == 0 && euid == st.st_uid) {
60 /* success */
61 continue;
62 }
63 if (errno != ENOENT) {
64 /*
65 * only bail out if something else than ENOENT
66 * is returned.
67 */
68 int rv = errno;
69 if (rv == 0) {
70 /* lstat succeeds but euid != uid */
71 rv = EPERM;
72 }
73 fail = true;
74 xbps_set_cb_state(xhp, XBPS_STATE_REMOVE_FILE_FAIL,
75 rv, pkgver,
76 "%s: cannot remove `%s': %s",
77 pkgver, file, strerror(rv));
78 }
79 errno = 0;
80 }
81 return fail;
82}
83
84static int
85remove_pkg_files(struct xbps_handle *xhp,
86 xbps_array_t obsoletes,
87 const char *pkgver)
88{
89 int rv = 0;
90
91 for (unsigned int i = 0; i < xbps_array_count(obsoletes); i++) {
92 const char *file = NULL;
93 xbps_array_get_cstring_nocopy(obsoletes, i, &file);
94 /*
95 * Remove the object if possible.
96 */
97 if (remove(file) == -1) {
98 xbps_set_cb_state(xhp, XBPS_STATE_REMOVE_FILE_FAIL,
99 errno, pkgver,
100 "%s: failed to remove `%s': %s", pkgver,
101 file, strerror(errno));
102 } else {
103 /* success */
104 xbps_set_cb_state(xhp, XBPS_STATE_REMOVE_FILE,
105 0, pkgver, "Removed `%s'", file);
106 }
107 }
108
109 return rv;
110}
111
112int HIDDEN
113xbps_remove_pkg(struct xbps_handle *xhp, const char *pkgver, bool update)
114{
115 xbps_dictionary_t pkgd = NULL, obsd = NULL;
116 xbps_array_t obsoletes = NULL;
117 char pkgname[XBPS_NAME_SIZE], metafile[PATH_MAX];
118 int rv = 0;
119 pkg_state_t state = 0;
120 uid_t euid;
121
122 assert(xhp);
123 assert(pkgver);
124
125 if (!xbps_pkg_name(pkgname, sizeof(pkgname), pkgver)) {
126 abort();
127 }
128
129 euid = geteuid();
130
131 if ((pkgd = xbps_pkgdb_get_pkg(xhp, pkgname)) == NULL) {
132 rv = errno;
133 xbps_dbg_printf("[remove] cannot find %s in pkgdb: %s\n",
134 pkgver, strerror(rv));
135 goto out;
136 }
137 if ((rv = xbps_pkg_state_dictionary(pkgd, &state)) != 0) {
138 xbps_dbg_printf("[remove] cannot find %s in pkgdb: %s\n",
139 pkgver, strerror(rv));
140 goto out;
141 }
142 xbps_dbg_printf("attempting to remove %s state %d\n", pkgver, state);
143
144 if (!update)
145 xbps_set_cb_state(xhp, XBPS_STATE_REMOVE, 0, pkgver, NULL);
146
147 if (chdir(xhp->rootdir) == -1) {
148 rv = errno;
149 xbps_set_cb_state(xhp, XBPS_STATE_REMOVE_FAIL,
150 rv, pkgver,
151 "%s: [remove] failed to chdir to rootdir `%s': %s",
152 pkgver, xhp->rootdir, strerror(rv));
153 goto out;
154 }
155
156 /* If package was "half-removed", remove it fully. */
157 if (state == XBPS_PKG_STATE_HALF_REMOVED)
158 goto purge;
159
160 /* show remove-msg if exists */
161 if ((rv = xbps_cb_message(xhp, pkgd, "remove-msg")) != 0)
162 goto out;
163
164 /* unregister alternatives */
165 if (update)
166 xbps_dictionary_set_bool(pkgd, "alternatives-update", true);
167
168 if ((rv = xbps_alternatives_unregister(xhp, pkgd)) != 0)
169 goto out;
170
171 /*
172 * If updating a package, we just need to execute the current
173 * pre-remove action target and we are done. Its files will be
174 * overwritten later in unpack phase.
175 */
176 if (update) {
177 return 0;
178 }
179
180 if (xbps_dictionary_get_dict(xhp->transd, "obsolete_files", &obsd))
181 obsoletes = xbps_dictionary_get(obsd, pkgname);
182
183 if (xbps_array_count(obsoletes) > 0) {
184 /*
185 * Do the removal in 2 phases:
186 * 1- check if user has enough perms to remove all entries
187 * 2- perform removal
188 */
189 if (check_remove_pkg_files(xhp, obsoletes, pkgver, euid)) {
190 rv = EPERM;
191 goto out;
192 }
193 /* Remove links */
194 if ((rv = remove_pkg_files(xhp, obsoletes, pkgver)) != 0)
195 goto out;
196 }
197
198 /*
199 * Set package state to "half-removed".
200 */
202 XBPS_PKG_STATE_HALF_REMOVED);
203 if (rv != 0) {
204 xbps_set_cb_state(xhp, XBPS_STATE_REMOVE_FAIL,
205 rv, pkgver,
206 "%s: [remove] failed to set state to half-removed: %s",
207 pkgver, strerror(rv));
208 goto out;
209 }
210 /* XXX: setting the state and then removing the package seems useless. */
211
212purge:
213 /*
214 * Remove package metadata plist.
215 */
216 snprintf(metafile, sizeof(metafile), "%s/.%s-files.plist", xhp->metadir, pkgname);
217 if (remove(metafile) == -1) {
218 if (errno != ENOENT) {
219 xbps_set_cb_state(xhp, XBPS_STATE_REMOVE_FAIL,
220 rv, pkgver,
221 "%s: failed to remove metadata file: %s",
222 pkgver, strerror(errno));
223 }
224 }
225 /*
226 * Unregister package from pkgdb.
227 */
228 xbps_dbg_printf("[remove] unregister %s returned %d\n", pkgver, rv);
229 xbps_set_cb_state(xhp, XBPS_STATE_REMOVE_DONE, 0, pkgver, NULL);
230 xbps_dictionary_remove(xhp->pkgdb, pkgname);
231out:
232 if (rv != 0) {
233 xbps_set_cb_state(xhp, XBPS_STATE_REMOVE_FAIL, rv, pkgver,
234 "%s: failed to remove package: %s", pkgver, strerror(rv));
235 }
236
237 return rv;
238}
int xbps_alternatives_unregister(struct xbps_handle *xhp, xbps_dictionary_t pkgd)
char metadir[XBPS_MAXPATH+sizeof(XBPS_META_PATH)]
Definition xbps.h:664
char rootdir[XBPS_MAXPATH]
Definition xbps.h:650
xbps_dictionary_t transd
Definition xbps.h:583
xbps_dictionary_t pkgdb
Definition xbps.h:576
Generic XBPS structure handler for initialization.
Definition xbps.h:550
xbps_dictionary_t xbps_pkgdb_get_pkg(struct xbps_handle *xhp, const char *pkg)
Definition pkgdb.c:379
int xbps_set_pkg_state_dictionary(xbps_dictionary_t dict, pkg_state_t state)
pkg_state_t
Definition xbps.h:1768
int xbps_pkg_state_dictionary(xbps_dictionary_t dict, pkg_state_t *state)
bool xbps_pkg_name(char *dst, size_t len, const char *pkg)
Definition util.c:253