help.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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 <http://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. "ldconfig",
  80. #if BUILD_START_STOP_DAEMON
  81. "start-stop-daemon",
  82. #endif
  83. NULL
  84. };
  85. const char *const *prog;
  86. const char *path_list;
  87. struct varbuf filename = VARBUF_INIT;
  88. int warned= 0;
  89. path_list = getenv("PATH");
  90. if (!path_list)
  91. ohshit(_("PATH is not set."));
  92. for (prog = prog_list; *prog; prog++) {
  93. struct stat stab;
  94. const char *path, *path_end;
  95. size_t path_len;
  96. for (path = path_list; path; path = path_end ? path_end + 1 : NULL) {
  97. path_end = strchr(path, ':');
  98. path_len = path_end ? (size_t)(path_end - path) : strlen(path);
  99. varbuf_reset(&filename);
  100. varbuf_add_buf(&filename, path, path_len);
  101. if (path_len)
  102. varbuf_add_char(&filename, '/');
  103. varbuf_add_str(&filename, *prog);
  104. varbuf_end_str(&filename);
  105. if (stat(filename.buf, &stab) == 0 && (stab.st_mode & 0111))
  106. break;
  107. }
  108. if (!path) {
  109. warning(_("'%s' not found in PATH or not executable."), *prog);
  110. warned++;
  111. }
  112. }
  113. varbuf_destroy(&filename);
  114. if (warned)
  115. forcibleerr(fc_badpath,
  116. P_("%d expected program not found in PATH or not executable.\n%s",
  117. "%d expected programs not found in PATH or not executable.\n%s",
  118. warned),
  119. warned, _("Note: root's PATH should usually contain "
  120. "/usr/local/sbin, /usr/sbin and /sbin."));
  121. }
  122. bool
  123. ignore_depends(struct pkginfo *pkg)
  124. {
  125. struct pkg_list *id;
  126. for (id= ignoredependss; id; id= id->next)
  127. if (id->pkg == pkg)
  128. return true;
  129. return false;
  130. }
  131. static bool
  132. ignore_depends_possi(struct deppossi *possi)
  133. {
  134. struct deppossi_pkg_iterator *possi_iter;
  135. struct pkginfo *pkg;
  136. possi_iter = deppossi_pkg_iter_new(possi, wpb_installed);
  137. while ((pkg = deppossi_pkg_iter_next(possi_iter))) {
  138. if (ignore_depends(pkg)) {
  139. deppossi_pkg_iter_free(possi_iter);
  140. return true;
  141. }
  142. }
  143. deppossi_pkg_iter_free(possi_iter);
  144. return false;
  145. }
  146. bool
  147. force_depends(struct deppossi *possi)
  148. {
  149. return fc_depends ||
  150. ignore_depends_possi(possi) ||
  151. ignore_depends(possi->up->up);
  152. }
  153. bool
  154. force_breaks(struct deppossi *possi)
  155. {
  156. return fc_breaks ||
  157. ignore_depends_possi(possi) ||
  158. ignore_depends(possi->up->up);
  159. }
  160. bool
  161. force_conflicts(struct deppossi *possi)
  162. {
  163. return fc_conflicts;
  164. }
  165. void clear_istobes(void) {
  166. struct pkgiterator *it;
  167. struct pkginfo *pkg;
  168. it = pkg_db_iter_new();
  169. while ((pkg = pkg_db_iter_next_pkg(it)) != NULL) {
  170. ensure_package_clientdata(pkg);
  171. pkg->clientdata->istobe= itb_normal;
  172. pkg->clientdata->replacingfilesandsaid= 0;
  173. }
  174. pkg_db_iter_free(it);
  175. }
  176. /*
  177. * Returns true if the directory contains conffiles belonging to pkg,
  178. * false otherwise.
  179. */
  180. bool
  181. dir_has_conffiles(struct filenamenode *file, struct pkginfo *pkg)
  182. {
  183. struct conffile *conff;
  184. size_t namelen;
  185. debug(dbg_veryverbose, "dir_has_conffiles '%s' (from %s)", file->name,
  186. pkg_name(pkg, pnaw_always));
  187. namelen = strlen(file->name);
  188. for (conff= pkg->installed.conffiles; conff; conff= conff->next) {
  189. if (strncmp(file->name, conff->name, namelen) == 0 &&
  190. conff->name[namelen] == '/') {
  191. debug(dbg_veryverbose, "directory %s has conffile %s from %s",
  192. file->name, conff->name, pkg_name(pkg, pnaw_always));
  193. return true;
  194. }
  195. }
  196. debug(dbg_veryverbose, "dir_has_conffiles no");
  197. return false;
  198. }
  199. /*
  200. * Returns true if the file is used by packages other than pkg,
  201. * false otherwise.
  202. */
  203. bool
  204. dir_is_used_by_others(struct filenamenode *file, struct pkginfo *pkg)
  205. {
  206. struct filepackages_iterator *iter;
  207. struct pkginfo *other_pkg;
  208. debug(dbg_veryverbose, "dir_is_used_by_others '%s' (except %s)", file->name,
  209. pkg ? pkg_name(pkg, pnaw_always) : "<none>");
  210. iter = filepackages_iter_new(file);
  211. while ((other_pkg = filepackages_iter_next(iter))) {
  212. debug(dbg_veryverbose, "dir_is_used_by_others considering %s ...",
  213. pkg_name(other_pkg, pnaw_always));
  214. if (other_pkg == pkg)
  215. continue;
  216. debug(dbg_veryverbose, "dir_is_used_by_others yes");
  217. return true;
  218. }
  219. filepackages_iter_free(iter);
  220. debug(dbg_veryverbose, "dir_is_used_by_others no");
  221. return false;
  222. }
  223. /*
  224. * Returns true if the file is used by pkg, false otherwise.
  225. */
  226. bool
  227. dir_is_used_by_pkg(struct filenamenode *file, struct pkginfo *pkg,
  228. struct fileinlist *list)
  229. {
  230. struct fileinlist *node;
  231. size_t namelen;
  232. debug(dbg_veryverbose, "dir_is_used_by_pkg '%s' (by %s)",
  233. file->name, pkg ? pkg_name(pkg, pnaw_always) : "<none>");
  234. namelen = strlen(file->name);
  235. for (node = list; node; node = node->next) {
  236. debug(dbg_veryverbose, "dir_is_used_by_pkg considering %s ...",
  237. node->namenode->name);
  238. if (strncmp(file->name, node->namenode->name, namelen) == 0 &&
  239. node->namenode->name[namelen] == '/') {
  240. debug(dbg_veryverbose, "dir_is_used_by_pkg yes");
  241. return true;
  242. }
  243. }
  244. debug(dbg_veryverbose, "dir_is_used_by_pkg no");
  245. return false;
  246. }
  247. void oldconffsetflags(const struct conffile *searchconff) {
  248. struct filenamenode *namenode;
  249. while (searchconff) {
  250. namenode= findnamenode(searchconff->name, 0); /* XXX */
  251. namenode->flags |= fnnf_old_conff;
  252. if (!namenode->oldhash)
  253. namenode->oldhash= searchconff->hash;
  254. debug(dbg_conffdetail, "oldconffsetflags '%s' namenode %p flags %o",
  255. searchconff->name, namenode, namenode->flags);
  256. searchconff= searchconff->next;
  257. }
  258. }
  259. /*
  260. * If the pathname to remove is:
  261. *
  262. * 1. a sticky or set-id file, or
  263. * 2. an unknown object (i.e., not a file, link, directory, fifo or socket)
  264. *
  265. * we change its mode so that a malicious user cannot use it, even if it's
  266. * linked to another file.
  267. */
  268. int
  269. secure_unlink(const char *pathname)
  270. {
  271. struct stat stab;
  272. if (lstat(pathname,&stab)) return -1;
  273. return secure_unlink_statted(pathname, &stab);
  274. }
  275. int
  276. secure_unlink_statted(const char *pathname, const struct stat *stab)
  277. {
  278. if (S_ISREG(stab->st_mode) ? (stab->st_mode & 07000) :
  279. !(S_ISLNK(stab->st_mode) || S_ISDIR(stab->st_mode) ||
  280. S_ISFIFO(stab->st_mode) || S_ISSOCK(stab->st_mode))) {
  281. if (chmod(pathname, 0600))
  282. return -1;
  283. }
  284. if (unlink(pathname)) return -1;
  285. return 0;
  286. }
  287. void ensure_pathname_nonexisting(const char *pathname) {
  288. pid_t pid;
  289. const char *u;
  290. u = path_skip_slash_dotslash(pathname);
  291. assert(*u);
  292. debug(dbg_eachfile, "ensure_pathname_nonexisting '%s'", pathname);
  293. if (!rmdir(pathname))
  294. return; /* Deleted it OK, it was a directory. */
  295. if (errno == ENOENT || errno == ELOOP) return;
  296. if (errno == ENOTDIR) {
  297. /* Either it's a file, or one of the path components is. If one
  298. * of the path components is this will fail again ... */
  299. if (secure_unlink(pathname) == 0)
  300. return; /* OK, it was. */
  301. if (errno == ENOTDIR) return;
  302. }
  303. if (errno != ENOTEMPTY && errno != EEXIST) { /* Huh? */
  304. ohshite(_("unable to securely remove '%.255s'"), pathname);
  305. }
  306. pid = subproc_fork();
  307. if (pid == 0) {
  308. execlp(RM, "rm", "-rf", "--", pathname, NULL);
  309. ohshite(_("unable to execute %s (%s)"), _("rm command for cleanup"), RM);
  310. }
  311. debug(dbg_eachfile, "ensure_pathname_nonexisting running rm -rf '%s'",
  312. pathname);
  313. subproc_wait_check(pid, "rm cleanup", 0);
  314. }
  315. void
  316. log_action(const char *action, struct pkginfo *pkg, struct pkgbin *pkgbin)
  317. {
  318. log_message("%s %s %s %s", action, pkgbin_name(pkg, pkgbin, pnaw_always),
  319. versiondescribe(&pkg->installed.version, vdew_nonambig),
  320. versiondescribe(&pkg->available.version, vdew_nonambig));
  321. statusfd_send("processing: %s: %s", action,
  322. pkgbin_name(pkg, pkgbin, pnaw_nonambig));
  323. }