help.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  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. *
  7. * This is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <config.h>
  21. #include <compat.h>
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <sys/wait.h>
  25. #include <assert.h>
  26. #include <errno.h>
  27. #include <string.h>
  28. #include <time.h>
  29. #include <dirent.h>
  30. #include <unistd.h>
  31. #include <stdlib.h>
  32. #include <dpkg/i18n.h>
  33. #include <dpkg/dpkg.h>
  34. #include <dpkg/dpkg-db.h>
  35. #include <dpkg/path.h>
  36. #include <dpkg/subproc.h>
  37. #include <dpkg/command.h>
  38. #include <dpkg/triglib.h>
  39. #include "filesdb.h"
  40. #include "main.h"
  41. const char *const statusstrings[]= {
  42. [stat_notinstalled] = N_("not installed"),
  43. [stat_configfiles] = N_("not installed but configs remain"),
  44. [stat_halfinstalled] = N_("broken due to failed removal or installation"),
  45. [stat_unpacked] = N_("unpacked but not configured"),
  46. [stat_halfconfigured] = N_("broken due to postinst failure"),
  47. [stat_triggersawaited] = N_("awaiting trigger processing by another package"),
  48. [stat_triggerspending] = N_("triggered"),
  49. [stat_installed] = N_("installed")
  50. };
  51. struct filenamenode *namenodetouse(struct filenamenode *namenode, struct pkginfo *pkg) {
  52. struct filenamenode *r;
  53. if (!namenode->divert) {
  54. r = namenode;
  55. return r;
  56. }
  57. debug(dbg_eachfile,"namenodetouse namenode=`%s' pkg=%s",
  58. namenode->name, pkg->set->name);
  59. r=
  60. (namenode->divert->useinstead && namenode->divert->pkgset != pkg->set)
  61. ? namenode->divert->useinstead : namenode;
  62. debug(dbg_eachfile,
  63. "namenodetouse ... useinstead=%s camefrom=%s pkg=%s return %s",
  64. namenode->divert->useinstead ? namenode->divert->useinstead->name : "<none>",
  65. namenode->divert->camefrom ? namenode->divert->camefrom->name : "<none>",
  66. namenode->divert->pkgset ? namenode->divert->pkgset->name : "<none>",
  67. r->name);
  68. return r;
  69. }
  70. /**
  71. * Verify that some programs can be found in the PATH.
  72. */
  73. void checkpath(void) {
  74. static const char *const prog_list[] = {
  75. DEFAULTSHELL,
  76. RM,
  77. TAR,
  78. FIND,
  79. BACKEND,
  80. "ldconfig",
  81. #if WITH_START_STOP_DAEMON
  82. "start-stop-daemon",
  83. #endif
  84. NULL
  85. };
  86. const char *const *prog;
  87. const char *path_list;
  88. struct varbuf filename = VARBUF_INIT;
  89. int warned= 0;
  90. path_list = getenv("PATH");
  91. if (!path_list)
  92. ohshit(_("PATH is not set."));
  93. for (prog = prog_list; *prog; prog++) {
  94. struct stat stab;
  95. const char *path, *path_end;
  96. size_t path_len;
  97. for (path = path_list; path; path = path_end ? path_end + 1 : NULL) {
  98. path_end = strchr(path, ':');
  99. path_len = path_end ? (size_t)(path_end - path) : strlen(path);
  100. varbuf_reset(&filename);
  101. varbuf_add_buf(&filename, path, path_len);
  102. if (path_len)
  103. varbuf_add_char(&filename, '/');
  104. varbuf_add_str(&filename, *prog);
  105. varbuf_end_str(&filename);
  106. if (stat(filename.buf, &stab) == 0 && (stab.st_mode & 0111))
  107. break;
  108. }
  109. if (!path) {
  110. warning(_("'%s' not found in PATH or not executable."), *prog);
  111. warned++;
  112. }
  113. }
  114. varbuf_destroy(&filename);
  115. if (warned)
  116. forcibleerr(fc_badpath,
  117. P_("%d expected program not found in PATH or not executable.\n%s",
  118. "%d expected programs not found in PATH or not executable.\n%s",
  119. warned),
  120. warned, _("Note: root's PATH should usually contain "
  121. "/usr/local/sbin, /usr/sbin and /sbin."));
  122. }
  123. bool
  124. ignore_depends(struct pkginfo *pkg)
  125. {
  126. struct pkg_list *id;
  127. for (id= ignoredependss; id; id= id->next)
  128. if (id->pkg == pkg)
  129. return true;
  130. return false;
  131. }
  132. static bool
  133. ignore_depends_possi(struct deppossi *possi)
  134. {
  135. struct deppossi_pkg_iterator *possi_iter;
  136. struct pkginfo *pkg;
  137. possi_iter = deppossi_pkg_iter_new(possi, wpb_installed);
  138. while ((pkg = deppossi_pkg_iter_next(possi_iter))) {
  139. if (ignore_depends(pkg)) {
  140. deppossi_pkg_iter_free(possi_iter);
  141. return true;
  142. }
  143. }
  144. deppossi_pkg_iter_free(possi_iter);
  145. return false;
  146. }
  147. bool
  148. force_depends(struct deppossi *possi)
  149. {
  150. return fc_depends ||
  151. ignore_depends_possi(possi) ||
  152. ignore_depends(possi->up->up);
  153. }
  154. bool
  155. force_breaks(struct deppossi *possi)
  156. {
  157. return fc_breaks ||
  158. ignore_depends_possi(possi) ||
  159. ignore_depends(possi->up->up);
  160. }
  161. bool
  162. force_conflicts(struct deppossi *possi)
  163. {
  164. return fc_conflicts;
  165. }
  166. /**
  167. * Returns the path to the script inside the chroot.
  168. */
  169. static const char *
  170. preexecscript(struct command *cmd)
  171. {
  172. const char *admindir = dpkg_db_get_dir();
  173. size_t instdirl = strlen(instdir);
  174. if (*instdir) {
  175. if (strncmp(admindir, instdir, instdirl) != 0)
  176. ohshit(_("admindir must be inside instdir for dpkg to work properly"));
  177. if (setenv("DPKG_ADMINDIR", admindir + instdirl, 1) < 0)
  178. ohshite(_("unable to setenv for subprocesses"));
  179. if (chroot(instdir)) ohshite(_("failed to chroot to `%.250s'"),instdir);
  180. if (chdir("/"))
  181. ohshite(_("failed to chdir to `%.255s'"), "/");
  182. }
  183. if (debug_has_flag(dbg_scripts)) {
  184. struct varbuf args = VARBUF_INIT;
  185. const char **argv = cmd->argv;
  186. while (*++argv) {
  187. varbuf_add_char(&args, ' ');
  188. varbuf_add_str(&args, *argv);
  189. }
  190. varbuf_end_str(&args);
  191. debug(dbg_scripts, "fork/exec %s (%s )", cmd->filename, args.buf);
  192. varbuf_destroy(&args);
  193. }
  194. if (!instdirl)
  195. return cmd->filename;
  196. assert(strlen(cmd->filename) >= instdirl);
  197. return cmd->filename + instdirl;
  198. }
  199. void
  200. post_postinst_tasks(struct pkginfo *pkg, enum pkgstatus new_status)
  201. {
  202. if (new_status < stat_triggersawaited)
  203. pkg->status = new_status;
  204. else
  205. pkg->status = pkg->trigaw.head ? stat_triggersawaited :
  206. pkg->trigpend_head ? stat_triggerspending : stat_installed;
  207. post_postinst_tasks_core(pkg);
  208. }
  209. void
  210. post_postinst_tasks_core(struct pkginfo *pkg)
  211. {
  212. modstatdb_note(pkg);
  213. if (!f_noact) {
  214. debug(dbg_triggersdetail, "post_postinst_tasks_core - trig_incorporate");
  215. trig_incorporate(msdbrw_write);
  216. }
  217. }
  218. static void
  219. post_script_tasks(void)
  220. {
  221. ensure_diversions();
  222. debug(dbg_triggersdetail,
  223. "post_script_tasks - ensure_diversions; trig_incorporate");
  224. trig_incorporate(msdbrw_write);
  225. }
  226. static void
  227. cu_post_script_tasks(int argc, void **argv)
  228. {
  229. post_script_tasks();
  230. }
  231. static void setexecute(const char *path, struct stat *stab) {
  232. if ((stab->st_mode & 0555) == 0555) return;
  233. if (!chmod(path,0755)) return;
  234. ohshite(_("unable to set execute permissions on `%.250s'"),path);
  235. }
  236. static int
  237. do_script(struct pkginfo *pkg, struct pkgbin *pif,
  238. struct command *cmd, struct stat *stab, int warn)
  239. {
  240. pid_t pid;
  241. int r;
  242. setexecute(cmd->filename, stab);
  243. push_cleanup(cu_post_script_tasks, ehflag_bombout, NULL, 0, 0);
  244. pid = subproc_fork();
  245. if (pid == 0) {
  246. if (setenv("DPKG_MAINTSCRIPT_PACKAGE", pkg->set->name, 1) ||
  247. setenv("DPKG_MAINTSCRIPT_ARCH", pif->arch->name, 1) ||
  248. setenv("DPKG_MAINTSCRIPT_NAME", cmd->argv[0], 1) ||
  249. setenv("DPKG_RUNNING_VERSION", PACKAGE_VERSION, 1))
  250. ohshite(_("unable to setenv for maintainer script"));
  251. cmd->filename = cmd->argv[0] = preexecscript(cmd);
  252. command_exec(cmd);
  253. }
  254. subproc_signals_setup(cmd->name); /* This does a push_cleanup(). */
  255. r = subproc_wait_check(pid, cmd->name, warn);
  256. pop_cleanup(ehflag_normaltidy);
  257. pop_cleanup(ehflag_normaltidy);
  258. return r;
  259. }
  260. static int
  261. vmaintainer_script_installed(struct pkginfo *pkg, const char *scriptname,
  262. const char *desc, va_list args)
  263. {
  264. struct command cmd;
  265. const char *scriptpath;
  266. struct stat stab;
  267. char buf[100];
  268. scriptpath = pkgadminfile(pkg, &pkg->installed, scriptname);
  269. sprintf(buf, _("installed %s script"), desc);
  270. command_init(&cmd, scriptpath, buf);
  271. command_add_arg(&cmd, scriptname);
  272. command_add_argv(&cmd, args);
  273. if (stat(scriptpath,&stab)) {
  274. command_destroy(&cmd);
  275. if (errno == ENOENT) {
  276. debug(dbg_scripts, "vmaintainer_script_installed nonexistent %s",
  277. scriptname);
  278. return 0;
  279. }
  280. ohshite(_("unable to stat %s `%.250s'"), buf, scriptpath);
  281. }
  282. do_script(pkg, &pkg->installed, &cmd, &stab, 0);
  283. command_destroy(&cmd);
  284. return 1;
  285. }
  286. /*
  287. * All ...'s in maintainer_script_* are const char *'s.
  288. */
  289. int
  290. maintainer_script_installed(struct pkginfo *pkg, const char *scriptname,
  291. const char *desc, ...)
  292. {
  293. int r;
  294. va_list args;
  295. va_start(args, desc);
  296. r = vmaintainer_script_installed(pkg, scriptname, desc, args);
  297. va_end(args);
  298. if (r)
  299. post_script_tasks();
  300. return r;
  301. }
  302. int
  303. maintainer_script_postinst(struct pkginfo *pkg, ...)
  304. {
  305. int r;
  306. va_list args;
  307. va_start(args, pkg);
  308. r = vmaintainer_script_installed(pkg, POSTINSTFILE, "post-installation", args);
  309. va_end(args);
  310. if (r)
  311. ensure_diversions();
  312. return r;
  313. }
  314. int
  315. maintainer_script_new(struct pkginfo *pkg,
  316. const char *scriptname, const char *desc,
  317. const char *cidir, char *cidirrest, ...)
  318. {
  319. struct command cmd;
  320. struct stat stab;
  321. va_list args;
  322. char buf[100];
  323. strcpy(cidirrest, scriptname);
  324. sprintf(buf, _("new %s script"), desc);
  325. va_start(args, cidirrest);
  326. command_init(&cmd, cidir, buf);
  327. command_add_arg(&cmd, scriptname);
  328. command_add_argv(&cmd, args);
  329. va_end(args);
  330. if (stat(cidir,&stab)) {
  331. command_destroy(&cmd);
  332. if (errno == ENOENT) {
  333. debug(dbg_scripts,"maintainer_script_new nonexistent %s `%s'",scriptname,cidir);
  334. return 0;
  335. }
  336. ohshite(_("unable to stat %s `%.250s'"), buf, cidir);
  337. }
  338. do_script(pkg, &pkg->available, &cmd, &stab, 0);
  339. command_destroy(&cmd);
  340. post_script_tasks();
  341. return 1;
  342. }
  343. int maintainer_script_alternative(struct pkginfo *pkg,
  344. const char *scriptname, const char *desc,
  345. const char *cidir, char *cidirrest,
  346. const char *ifok, const char *iffallback) {
  347. struct command cmd;
  348. const char *oldscriptpath;
  349. struct stat stab;
  350. char buf[100];
  351. oldscriptpath = pkgadminfile(pkg, &pkg->installed, scriptname);
  352. sprintf(buf, _("old %s script"), desc);
  353. command_init(&cmd, oldscriptpath, buf);
  354. command_add_args(&cmd, scriptname, ifok,
  355. versiondescribe(&pkg->available.version, vdew_nonambig),
  356. NULL);
  357. if (stat(oldscriptpath,&stab)) {
  358. if (errno == ENOENT) {
  359. debug(dbg_scripts,"maintainer_script_alternative nonexistent %s `%s'",
  360. scriptname,oldscriptpath);
  361. command_destroy(&cmd);
  362. return 0;
  363. }
  364. warning(_("unable to stat %s '%.250s': %s"),
  365. cmd.name, oldscriptpath, strerror(errno));
  366. } else {
  367. if (!do_script(pkg, &pkg->installed, &cmd, &stab, PROCWARN)) {
  368. command_destroy(&cmd);
  369. post_script_tasks();
  370. return 1;
  371. }
  372. }
  373. fprintf(stderr, _("dpkg - trying script from the new package instead ...\n"));
  374. strcpy(cidirrest,scriptname);
  375. sprintf(buf, _("new %s script"), desc);
  376. command_destroy(&cmd);
  377. command_init(&cmd, cidir, buf);
  378. command_add_args(&cmd, scriptname, iffallback,
  379. versiondescribe(&pkg->installed.version, vdew_nonambig),
  380. NULL);
  381. if (stat(cidir,&stab)) {
  382. command_destroy(&cmd);
  383. if (errno == ENOENT)
  384. ohshit(_("there is no script in the new version of the package - giving up"));
  385. else
  386. ohshite(_("unable to stat %s `%.250s'"),buf,cidir);
  387. }
  388. do_script(pkg, &pkg->available, &cmd, &stab, 0);
  389. fprintf(stderr, _("dpkg: ... it looks like that went OK.\n"));
  390. command_destroy(&cmd);
  391. post_script_tasks();
  392. return 1;
  393. }
  394. void clear_istobes(void) {
  395. struct pkgiterator *it;
  396. struct pkginfo *pkg;
  397. it = pkg_db_iter_new();
  398. while ((pkg = pkg_db_iter_next_pkg(it)) != NULL) {
  399. ensure_package_clientdata(pkg);
  400. pkg->clientdata->istobe= itb_normal;
  401. pkg->clientdata->replacingfilesandsaid= 0;
  402. }
  403. pkg_db_iter_free(it);
  404. }
  405. /*
  406. * Returns true if the directory contains conffiles belonging to pkg,
  407. * false otherwise.
  408. */
  409. bool
  410. dir_has_conffiles(struct filenamenode *file, struct pkginfo *pkg)
  411. {
  412. struct conffile *conff;
  413. size_t namelen;
  414. debug(dbg_veryverbose, "dir_has_conffiles '%s' (from %s)", file->name,
  415. pkg->set->name);
  416. namelen = strlen(file->name);
  417. for (conff= pkg->installed.conffiles; conff; conff= conff->next) {
  418. if (strncmp(file->name, conff->name, namelen) == 0 &&
  419. conff->name[namelen] == '/') {
  420. debug(dbg_veryverbose, "directory %s has conffile %s from %s",
  421. file->name, conff->name, pkg->set->name);
  422. return true;
  423. }
  424. }
  425. debug(dbg_veryverbose, "dir_has_conffiles no");
  426. return false;
  427. }
  428. /*
  429. * Returns true if the file is used by packages other than pkg,
  430. * false otherwise.
  431. */
  432. bool
  433. dir_is_used_by_others(struct filenamenode *file, struct pkginfo *pkg)
  434. {
  435. struct filepackages_iterator *iter;
  436. struct pkginfo *other_pkg;
  437. debug(dbg_veryverbose, "dir_is_used_by_others '%s' (except %s)", file->name,
  438. pkg ? pkg->set->name : "<none>");
  439. iter = filepackages_iter_new(file);
  440. while ((other_pkg = filepackages_iter_next(iter))) {
  441. debug(dbg_veryverbose, "dir_is_used_by_others considering %s ...",
  442. other_pkg->set->name);
  443. if (other_pkg == pkg)
  444. continue;
  445. debug(dbg_veryverbose, "dir_is_used_by_others yes");
  446. return true;
  447. }
  448. filepackages_iter_free(iter);
  449. debug(dbg_veryverbose, "dir_is_used_by_others no");
  450. return false;
  451. }
  452. /*
  453. * Returns true if the file is used by pkg, false otherwise.
  454. */
  455. bool
  456. dir_is_used_by_pkg(struct filenamenode *file, struct pkginfo *pkg,
  457. struct fileinlist *list)
  458. {
  459. struct fileinlist *node;
  460. size_t namelen;
  461. debug(dbg_veryverbose, "dir_is_used_by_pkg '%s' (by %s)",
  462. file->name, pkg ? pkg->set->name : "<none>");
  463. namelen = strlen(file->name);
  464. for (node = list; node; node = node->next) {
  465. debug(dbg_veryverbose, "dir_is_used_by_pkg considering %s ...",
  466. node->namenode->name);
  467. if (strncmp(file->name, node->namenode->name, namelen) == 0 &&
  468. node->namenode->name[namelen] == '/') {
  469. debug(dbg_veryverbose, "dir_is_used_by_pkg yes");
  470. return true;
  471. }
  472. }
  473. debug(dbg_veryverbose, "dir_is_used_by_pkg no");
  474. return false;
  475. }
  476. void oldconffsetflags(const struct conffile *searchconff) {
  477. struct filenamenode *namenode;
  478. while (searchconff) {
  479. namenode= findnamenode(searchconff->name, 0); /* XXX */
  480. namenode->flags |= fnnf_old_conff;
  481. if (!namenode->oldhash)
  482. namenode->oldhash= searchconff->hash;
  483. debug(dbg_conffdetail, "oldconffsetflags `%s' namenode %p flags %o",
  484. searchconff->name, namenode, namenode->flags);
  485. searchconff= searchconff->next;
  486. }
  487. }
  488. /*
  489. * If the pathname to remove is:
  490. *
  491. * 1. a sticky or set-id file, or
  492. * 2. an unknown object (i.e., not a file, link, directory, fifo or socket)
  493. *
  494. * we change its mode so that a malicious user cannot use it, even if it's
  495. * linked to another file.
  496. */
  497. int
  498. secure_unlink(const char *pathname)
  499. {
  500. struct stat stab;
  501. if (lstat(pathname,&stab)) return -1;
  502. return secure_unlink_statted(pathname, &stab);
  503. }
  504. int
  505. secure_unlink_statted(const char *pathname, const struct stat *stab)
  506. {
  507. if (S_ISREG(stab->st_mode) ? (stab->st_mode & 07000) :
  508. !(S_ISLNK(stab->st_mode) || S_ISDIR(stab->st_mode) ||
  509. S_ISFIFO(stab->st_mode) || S_ISSOCK(stab->st_mode))) {
  510. if (chmod(pathname, 0600))
  511. return -1;
  512. }
  513. if (unlink(pathname)) return -1;
  514. return 0;
  515. }
  516. void ensure_pathname_nonexisting(const char *pathname) {
  517. pid_t pid;
  518. const char *u;
  519. u = path_skip_slash_dotslash(pathname);
  520. assert(*u);
  521. debug(dbg_eachfile,"ensure_pathname_nonexisting `%s'",pathname);
  522. if (!rmdir(pathname))
  523. return; /* Deleted it OK, it was a directory. */
  524. if (errno == ENOENT || errno == ELOOP) return;
  525. if (errno == ENOTDIR) {
  526. /* Either it's a file, or one of the path components is. If one
  527. * of the path components is this will fail again ... */
  528. if (secure_unlink(pathname) == 0)
  529. return; /* OK, it was. */
  530. if (errno == ENOTDIR) return;
  531. }
  532. if (errno != ENOTEMPTY && errno != EEXIST) { /* Huh? */
  533. ohshite(_("unable to securely remove '%.255s'"), pathname);
  534. }
  535. pid = subproc_fork();
  536. if (pid == 0) {
  537. execlp(RM, "rm", "-rf", "--", pathname, NULL);
  538. ohshite(_("unable to execute %s (%s)"), _("rm command for cleanup"), RM);
  539. }
  540. debug(dbg_eachfile,"ensure_pathname_nonexisting running rm -rf");
  541. subproc_wait_check(pid, "rm cleanup", 0);
  542. }
  543. void log_action(const char *action, struct pkginfo *pkg) {
  544. log_message("%s %s %s %s", action, pkg->set->name,
  545. versiondescribe(&pkg->installed.version, vdew_nonambig),
  546. versiondescribe(&pkg->available.version, vdew_nonambig));
  547. statusfd_send("processing: %s: %s", action, pkg->set->name);
  548. }