help.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*
  2. * dpkg - main program for package management
  3. * help.c - various helper routines
  4. *
  5. * Copyright © 1995 Ian Jackson <ian@chiark.greenend.org.uk>
  6. * Copyright © 2007-2012 Guillem Jover <guillem@debian.org>
  7. *
  8. * This is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. #include <config.h>
  22. #include <compat.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <assert.h>
  26. #include <errno.h>
  27. #include <string.h>
  28. #include <unistd.h>
  29. #include <stdlib.h>
  30. #include <dpkg/i18n.h>
  31. #include <dpkg/dpkg.h>
  32. #include <dpkg/dpkg-db.h>
  33. #include <dpkg/path.h>
  34. #include <dpkg/subproc.h>
  35. #include "filesdb.h"
  36. #include "main.h"
  37. const char *const statusstrings[]= {
  38. [stat_notinstalled] = N_("not installed"),
  39. [stat_configfiles] = N_("not installed but configs remain"),
  40. [stat_halfinstalled] = N_("broken due to failed removal or installation"),
  41. [stat_unpacked] = N_("unpacked but not configured"),
  42. [stat_halfconfigured] = N_("broken due to postinst failure"),
  43. [stat_triggersawaited] = N_("awaiting trigger processing by another package"),
  44. [stat_triggerspending] = N_("triggered"),
  45. [stat_installed] = N_("installed")
  46. };
  47. struct filenamenode *
  48. namenodetouse(struct filenamenode *namenode, struct pkginfo *pkg,
  49. struct pkgbin *pkgbin)
  50. {
  51. struct filenamenode *r;
  52. if (!namenode->divert) {
  53. r = namenode;
  54. return r;
  55. }
  56. debug(dbg_eachfile, "namenodetouse namenode='%s' pkg=%s",
  57. namenode->name, pkgbin_name(pkg, pkgbin, pnaw_always));
  58. r=
  59. (namenode->divert->useinstead && namenode->divert->pkgset != pkg->set)
  60. ? namenode->divert->useinstead : namenode;
  61. debug(dbg_eachfile,
  62. "namenodetouse ... useinstead=%s camefrom=%s pkg=%s return %s",
  63. namenode->divert->useinstead ? namenode->divert->useinstead->name : "<none>",
  64. namenode->divert->camefrom ? namenode->divert->camefrom->name : "<none>",
  65. namenode->divert->pkgset ? namenode->divert->pkgset->name : "<none>",
  66. r->name);
  67. return r;
  68. }
  69. /**
  70. * Verify that some programs can be found in the PATH.
  71. */
  72. void checkpath(void) {
  73. static const char *const prog_list[] = {
  74. DEFAULTSHELL,
  75. RM,
  76. TAR,
  77. FIND,
  78. BACKEND,
  79. /* Mac OS X uses dyld (Mach-O) instead of ld.so (ELF), and does not have
  80. * an ldconfig. */
  81. #if defined(__APPLE__) && defined(__MACH__)
  82. "update_dyld_shared_cache",
  83. #else
  84. "ldconfig",
  85. #endif
  86. #if BUILD_START_STOP_DAEMON
  87. "start-stop-daemon",
  88. #endif
  89. NULL
  90. };
  91. const char *const *prog;
  92. const char *path_list;
  93. struct varbuf filename = VARBUF_INIT;
  94. int warned= 0;
  95. path_list = getenv("PATH");
  96. if (!path_list)
  97. ohshit(_("PATH is not set"));
  98. for (prog = prog_list; *prog; prog++) {
  99. struct stat stab;
  100. const char *path, *path_end;
  101. size_t path_len;
  102. for (path = path_list; path; path = path_end ? path_end + 1 : NULL) {
  103. path_end = strchr(path, ':');
  104. path_len = path_end ? (size_t)(path_end - path) : strlen(path);
  105. varbuf_reset(&filename);
  106. varbuf_add_buf(&filename, path, path_len);
  107. if (path_len)
  108. varbuf_add_char(&filename, '/');
  109. varbuf_add_str(&filename, *prog);
  110. varbuf_end_str(&filename);
  111. if (stat(filename.buf, &stab) == 0 && (stab.st_mode & 0111))
  112. break;
  113. }
  114. if (!path) {
  115. warning(_("'%s' not found in PATH or not executable"), *prog);
  116. warned++;
  117. }
  118. }
  119. varbuf_destroy(&filename);
  120. if (warned)
  121. forcibleerr(fc_badpath,
  122. P_("%d expected program not found in PATH or not executable\n%s",
  123. "%d expected programs not found in PATH or not executable\n%s",
  124. warned),
  125. warned, _("Note: root's PATH should usually contain "
  126. "/usr/local/sbin, /usr/sbin and /sbin"));
  127. }
  128. bool
  129. ignore_depends(struct pkginfo *pkg)
  130. {
  131. struct pkg_list *id;
  132. for (id= ignoredependss; id; id= id->next)
  133. if (id->pkg == pkg)
  134. return true;
  135. return false;
  136. }
  137. static bool
  138. ignore_depends_possi(struct deppossi *possi)
  139. {
  140. struct deppossi_pkg_iterator *possi_iter;
  141. struct pkginfo *pkg;
  142. possi_iter = deppossi_pkg_iter_new(possi, wpb_installed);
  143. while ((pkg = deppossi_pkg_iter_next(possi_iter))) {
  144. if (ignore_depends(pkg)) {
  145. deppossi_pkg_iter_free(possi_iter);
  146. return true;
  147. }
  148. }
  149. deppossi_pkg_iter_free(possi_iter);
  150. return false;
  151. }
  152. bool
  153. force_depends(struct deppossi *possi)
  154. {
  155. return fc_depends ||
  156. ignore_depends_possi(possi) ||
  157. ignore_depends(possi->up->up);
  158. }
  159. bool
  160. force_breaks(struct deppossi *possi)
  161. {
  162. return fc_breaks ||
  163. ignore_depends_possi(possi) ||
  164. ignore_depends(possi->up->up);
  165. }
  166. bool
  167. force_conflicts(struct deppossi *possi)
  168. {
  169. return fc_conflicts;
  170. }
  171. void clear_istobes(void) {
  172. struct pkgiterator *it;
  173. struct pkginfo *pkg;
  174. it = pkg_db_iter_new();
  175. while ((pkg = pkg_db_iter_next_pkg(it)) != NULL) {
  176. ensure_package_clientdata(pkg);
  177. pkg->clientdata->istobe= itb_normal;
  178. pkg->clientdata->replacingfilesandsaid= 0;
  179. }
  180. pkg_db_iter_free(it);
  181. }
  182. /*
  183. * Returns true if the directory contains conffiles belonging to pkg,
  184. * false otherwise.
  185. */
  186. bool
  187. dir_has_conffiles(struct filenamenode *file, struct pkginfo *pkg)
  188. {
  189. struct conffile *conff;
  190. size_t namelen;
  191. debug(dbg_veryverbose, "dir_has_conffiles '%s' (from %s)", file->name,
  192. pkg_name(pkg, pnaw_always));
  193. namelen = strlen(file->name);
  194. for (conff= pkg->installed.conffiles; conff; conff= conff->next) {
  195. if (conff->obsolete)
  196. continue;
  197. if (strncmp(file->name, conff->name, namelen) == 0 &&
  198. conff->name[namelen] == '/') {
  199. debug(dbg_veryverbose, "directory %s has conffile %s from %s",
  200. file->name, conff->name, pkg_name(pkg, pnaw_always));
  201. return true;
  202. }
  203. }
  204. debug(dbg_veryverbose, "dir_has_conffiles no");
  205. return false;
  206. }
  207. /*
  208. * Returns true if the file is used by packages other than pkg,
  209. * false otherwise.
  210. */
  211. bool
  212. dir_is_used_by_others(struct filenamenode *file, struct pkginfo *pkg)
  213. {
  214. struct filepackages_iterator *iter;
  215. struct pkginfo *other_pkg;
  216. debug(dbg_veryverbose, "dir_is_used_by_others '%s' (except %s)", file->name,
  217. pkg ? pkg_name(pkg, pnaw_always) : "<none>");
  218. iter = filepackages_iter_new(file);
  219. while ((other_pkg = filepackages_iter_next(iter))) {
  220. debug(dbg_veryverbose, "dir_is_used_by_others considering %s ...",
  221. pkg_name(other_pkg, pnaw_always));
  222. if (other_pkg == pkg)
  223. continue;
  224. debug(dbg_veryverbose, "dir_is_used_by_others yes");
  225. return true;
  226. }
  227. filepackages_iter_free(iter);
  228. debug(dbg_veryverbose, "dir_is_used_by_others no");
  229. return false;
  230. }
  231. /*
  232. * Returns true if the file is used by pkg, false otherwise.
  233. */
  234. bool
  235. dir_is_used_by_pkg(struct filenamenode *file, struct pkginfo *pkg,
  236. struct fileinlist *list)
  237. {
  238. struct fileinlist *node;
  239. size_t namelen;
  240. debug(dbg_veryverbose, "dir_is_used_by_pkg '%s' (by %s)",
  241. file->name, pkg ? pkg_name(pkg, pnaw_always) : "<none>");
  242. namelen = strlen(file->name);
  243. for (node = list; node; node = node->next) {
  244. debug(dbg_veryverbose, "dir_is_used_by_pkg considering %s ...",
  245. node->namenode->name);
  246. if (strncmp(file->name, node->namenode->name, namelen) == 0 &&
  247. node->namenode->name[namelen] == '/') {
  248. debug(dbg_veryverbose, "dir_is_used_by_pkg yes");
  249. return true;
  250. }
  251. }
  252. debug(dbg_veryverbose, "dir_is_used_by_pkg no");
  253. return false;
  254. }
  255. /**
  256. * Mark a conffile as obsolete.
  257. *
  258. * @param pkg The package owning the conffile.
  259. * @param namenode The namenode for the obsolete conffile.
  260. */
  261. void
  262. conffile_mark_obsolete(struct pkginfo *pkg, struct filenamenode *namenode)
  263. {
  264. struct conffile *conff;
  265. for (conff = pkg->installed.conffiles; conff; conff = conff->next) {
  266. if (strcmp(conff->name, namenode->name) == 0) {
  267. debug(dbg_conff, "marking %s conffile %s as obsolete",
  268. pkg_name(pkg, pnaw_always), conff->name);
  269. conff->obsolete = true;
  270. return;
  271. }
  272. }
  273. }
  274. void oldconffsetflags(const struct conffile *searchconff) {
  275. struct filenamenode *namenode;
  276. while (searchconff) {
  277. namenode= findnamenode(searchconff->name, 0); /* XXX */
  278. namenode->flags |= fnnf_old_conff;
  279. if (!namenode->oldhash)
  280. namenode->oldhash= searchconff->hash;
  281. debug(dbg_conffdetail, "oldconffsetflags '%s' namenode %p flags %o",
  282. searchconff->name, namenode, namenode->flags);
  283. searchconff= searchconff->next;
  284. }
  285. }
  286. /*
  287. * If the pathname to remove is:
  288. *
  289. * 1. a sticky or set-id file, or
  290. * 2. an unknown object (i.e., not a file, link, directory, fifo or socket)
  291. *
  292. * we change its mode so that a malicious user cannot use it, even if it's
  293. * linked to another file.
  294. */
  295. int
  296. secure_unlink(const char *pathname)
  297. {
  298. struct stat stab;
  299. if (lstat(pathname,&stab)) return -1;
  300. return secure_unlink_statted(pathname, &stab);
  301. }
  302. int
  303. secure_unlink_statted(const char *pathname, const struct stat *stab)
  304. {
  305. if (S_ISREG(stab->st_mode) ? (stab->st_mode & 07000) :
  306. !(S_ISLNK(stab->st_mode) || S_ISDIR(stab->st_mode) ||
  307. S_ISFIFO(stab->st_mode) || S_ISSOCK(stab->st_mode))) {
  308. if (chmod(pathname, 0600))
  309. return -1;
  310. }
  311. if (unlink(pathname)) return -1;
  312. return 0;
  313. }
  314. void ensure_pathname_nonexisting(const char *pathname) {
  315. pid_t pid;
  316. const char *u;
  317. u = path_skip_slash_dotslash(pathname);
  318. assert(*u);
  319. debug(dbg_eachfile, "ensure_pathname_nonexisting '%s'", pathname);
  320. if (!rmdir(pathname))
  321. return; /* Deleted it OK, it was a directory. */
  322. if (errno == ENOENT || errno == ELOOP) return;
  323. if (errno == ENOTDIR) {
  324. /* Either it's a file, or one of the path components is. If one
  325. * of the path components is this will fail again ... */
  326. if (secure_unlink(pathname) == 0)
  327. return; /* OK, it was. */
  328. if (errno == ENOTDIR) return;
  329. }
  330. if (errno != ENOTEMPTY && errno != EEXIST) { /* Huh? */
  331. ohshite(_("unable to securely remove '%.255s'"), pathname);
  332. }
  333. pid = subproc_fork();
  334. if (pid == 0) {
  335. execlp(RM, "rm", "-rf", "--", pathname, NULL);
  336. ohshite(_("unable to execute %s (%s)"), _("rm command for cleanup"), RM);
  337. }
  338. debug(dbg_eachfile, "ensure_pathname_nonexisting running rm -rf '%s'",
  339. pathname);
  340. subproc_wait_check(pid, "rm cleanup", 0);
  341. }
  342. void
  343. log_action(const char *action, struct pkginfo *pkg, struct pkgbin *pkgbin)
  344. {
  345. log_message("%s %s %s %s", action, pkgbin_name(pkg, pkgbin, pnaw_always),
  346. versiondescribe(&pkg->installed.version, vdew_nonambig),
  347. versiondescribe(&pkg->available.version, vdew_nonambig));
  348. statusfd_send("processing: %s: %s", action,
  349. pkgbin_name(pkg, pkgbin, pnaw_nonambig));
  350. }