help.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  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->name);
  59. r=
  60. (namenode->divert->useinstead && namenode->divert->pkg != pkg)
  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->pkg ? namenode->divert->pkg->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. bool
  133. force_depends(struct deppossi *possi)
  134. {
  135. return fc_depends ||
  136. ignore_depends(possi->ed) ||
  137. ignore_depends(possi->up->up);
  138. }
  139. bool
  140. force_breaks(struct deppossi *possi)
  141. {
  142. return fc_breaks ||
  143. ignore_depends(possi->ed) ||
  144. ignore_depends(possi->up->up);
  145. }
  146. bool
  147. force_conflicts(struct deppossi *possi)
  148. {
  149. return fc_conflicts;
  150. }
  151. /**
  152. * Returns the path to the script inside the chroot.
  153. */
  154. static const char *
  155. preexecscript(struct command *cmd)
  156. {
  157. const char *admindir = dpkg_db_get_dir();
  158. size_t instdirl = strlen(instdir);
  159. if (*instdir) {
  160. if (strncmp(admindir, instdir, instdirl) != 0)
  161. ohshit(_("admindir must be inside instdir for dpkg to work properly"));
  162. if (setenv("DPKG_ADMINDIR", admindir + instdirl, 1) < 0)
  163. ohshite(_("unable to setenv for subprocesses"));
  164. if (chroot(instdir)) ohshite(_("failed to chroot to `%.250s'"),instdir);
  165. if (chdir("/"))
  166. ohshite(_("failed to chdir to `%.255s'"), "/");
  167. }
  168. if (debug_has_flag(dbg_scripts)) {
  169. struct varbuf args = VARBUF_INIT;
  170. const char **argv = cmd->argv;
  171. while (*++argv) {
  172. varbuf_add_char(&args, ' ');
  173. varbuf_add_str(&args, *argv);
  174. }
  175. varbuf_end_str(&args);
  176. debug(dbg_scripts, "fork/exec %s (%s )", cmd->filename, args.buf);
  177. varbuf_destroy(&args);
  178. }
  179. if (!instdirl)
  180. return cmd->filename;
  181. assert(strlen(cmd->filename) >= instdirl);
  182. return cmd->filename + instdirl;
  183. }
  184. void
  185. post_postinst_tasks(struct pkginfo *pkg, enum pkgstatus new_status)
  186. {
  187. if (new_status < stat_triggersawaited)
  188. pkg->status = new_status;
  189. else
  190. pkg->status = pkg->trigaw.head ? stat_triggersawaited :
  191. pkg->trigpend_head ? stat_triggerspending : stat_installed;
  192. post_postinst_tasks_core(pkg);
  193. }
  194. void
  195. post_postinst_tasks_core(struct pkginfo *pkg)
  196. {
  197. modstatdb_note(pkg);
  198. if (!f_noact) {
  199. debug(dbg_triggersdetail, "post_postinst_tasks_core - trig_incorporate");
  200. trig_incorporate(msdbrw_write);
  201. }
  202. }
  203. static void
  204. post_script_tasks(void)
  205. {
  206. ensure_diversions();
  207. debug(dbg_triggersdetail,
  208. "post_script_tasks - ensure_diversions; trig_incorporate");
  209. trig_incorporate(msdbrw_write);
  210. }
  211. static void
  212. cu_post_script_tasks(int argc, void **argv)
  213. {
  214. post_script_tasks();
  215. }
  216. static void setexecute(const char *path, struct stat *stab) {
  217. if ((stab->st_mode & 0555) == 0555) return;
  218. if (!chmod(path,0755)) return;
  219. ohshite(_("unable to set execute permissions on `%.250s'"),path);
  220. }
  221. static int
  222. do_script(struct pkginfo *pkg, struct pkgbin *pif,
  223. struct command *cmd, struct stat *stab, int warn)
  224. {
  225. pid_t pid;
  226. int r;
  227. setexecute(cmd->filename, stab);
  228. push_cleanup(cu_post_script_tasks, ehflag_bombout, NULL, 0, 0);
  229. pid = subproc_fork();
  230. if (pid == 0) {
  231. if (setenv("DPKG_MAINTSCRIPT_PACKAGE", pkg->name, 1) ||
  232. setenv("DPKG_MAINTSCRIPT_ARCH", pif->arch->name, 1) ||
  233. setenv("DPKG_MAINTSCRIPT_NAME", cmd->argv[0], 1) ||
  234. setenv("DPKG_RUNNING_VERSION", PACKAGE_VERSION, 1))
  235. ohshite(_("unable to setenv for maintainer script"));
  236. cmd->filename = cmd->argv[0] = preexecscript(cmd);
  237. command_exec(cmd);
  238. }
  239. subproc_signals_setup(cmd->name); /* This does a push_cleanup(). */
  240. r = subproc_wait_check(pid, cmd->name, warn);
  241. pop_cleanup(ehflag_normaltidy);
  242. pop_cleanup(ehflag_normaltidy);
  243. return r;
  244. }
  245. static int
  246. vmaintainer_script_installed(struct pkginfo *pkg, const char *scriptname,
  247. const char *desc, va_list args)
  248. {
  249. struct command cmd;
  250. const char *scriptpath;
  251. struct stat stab;
  252. char buf[100];
  253. scriptpath= pkgadminfile(pkg,scriptname);
  254. sprintf(buf, _("installed %s script"), desc);
  255. command_init(&cmd, scriptpath, buf);
  256. command_add_arg(&cmd, scriptname);
  257. command_add_argv(&cmd, args);
  258. if (stat(scriptpath,&stab)) {
  259. command_destroy(&cmd);
  260. if (errno == ENOENT) {
  261. debug(dbg_scripts, "vmaintainer_script_installed nonexistent %s",
  262. scriptname);
  263. return 0;
  264. }
  265. ohshite(_("unable to stat %s `%.250s'"), buf, scriptpath);
  266. }
  267. do_script(pkg, &pkg->installed, &cmd, &stab, 0);
  268. command_destroy(&cmd);
  269. return 1;
  270. }
  271. /*
  272. * All ...'s in maintainer_script_* are const char *'s.
  273. */
  274. int
  275. maintainer_script_installed(struct pkginfo *pkg, const char *scriptname,
  276. const char *desc, ...)
  277. {
  278. int r;
  279. va_list args;
  280. va_start(args, desc);
  281. r = vmaintainer_script_installed(pkg, scriptname, desc, args);
  282. va_end(args);
  283. if (r)
  284. post_script_tasks();
  285. return r;
  286. }
  287. int
  288. maintainer_script_postinst(struct pkginfo *pkg, ...)
  289. {
  290. int r;
  291. va_list args;
  292. va_start(args, pkg);
  293. r = vmaintainer_script_installed(pkg, POSTINSTFILE, "post-installation", args);
  294. va_end(args);
  295. if (r)
  296. ensure_diversions();
  297. return r;
  298. }
  299. int
  300. maintainer_script_new(struct pkginfo *pkg,
  301. const char *scriptname, const char *desc,
  302. const char *cidir, char *cidirrest, ...)
  303. {
  304. struct command cmd;
  305. struct stat stab;
  306. va_list args;
  307. char buf[100];
  308. strcpy(cidirrest, scriptname);
  309. sprintf(buf, _("new %s script"), desc);
  310. va_start(args, cidirrest);
  311. command_init(&cmd, cidir, buf);
  312. command_add_arg(&cmd, scriptname);
  313. command_add_argv(&cmd, args);
  314. va_end(args);
  315. if (stat(cidir,&stab)) {
  316. command_destroy(&cmd);
  317. if (errno == ENOENT) {
  318. debug(dbg_scripts,"maintainer_script_new nonexistent %s `%s'",scriptname,cidir);
  319. return 0;
  320. }
  321. ohshite(_("unable to stat %s `%.250s'"), buf, cidir);
  322. }
  323. do_script(pkg, &pkg->available, &cmd, &stab, 0);
  324. command_destroy(&cmd);
  325. post_script_tasks();
  326. return 1;
  327. }
  328. int maintainer_script_alternative(struct pkginfo *pkg,
  329. const char *scriptname, const char *desc,
  330. const char *cidir, char *cidirrest,
  331. const char *ifok, const char *iffallback) {
  332. struct command cmd;
  333. const char *oldscriptpath;
  334. struct stat stab;
  335. char buf[100];
  336. oldscriptpath= pkgadminfile(pkg,scriptname);
  337. sprintf(buf, _("old %s script"), desc);
  338. command_init(&cmd, oldscriptpath, buf);
  339. command_add_args(&cmd, scriptname, ifok,
  340. versiondescribe(&pkg->available.version, vdew_nonambig),
  341. NULL);
  342. if (stat(oldscriptpath,&stab)) {
  343. if (errno == ENOENT) {
  344. debug(dbg_scripts,"maintainer_script_alternative nonexistent %s `%s'",
  345. scriptname,oldscriptpath);
  346. command_destroy(&cmd);
  347. return 0;
  348. }
  349. warning(_("unable to stat %s '%.250s': %s"),
  350. cmd.name, oldscriptpath, strerror(errno));
  351. } else {
  352. if (!do_script(pkg, &pkg->installed, &cmd, &stab, PROCWARN)) {
  353. command_destroy(&cmd);
  354. post_script_tasks();
  355. return 1;
  356. }
  357. }
  358. fprintf(stderr, _("dpkg - trying script from the new package instead ...\n"));
  359. strcpy(cidirrest,scriptname);
  360. sprintf(buf, _("new %s script"), desc);
  361. command_destroy(&cmd);
  362. command_init(&cmd, cidir, buf);
  363. command_add_args(&cmd, scriptname, iffallback,
  364. versiondescribe(&pkg->installed.version, vdew_nonambig),
  365. NULL);
  366. if (stat(cidir,&stab)) {
  367. command_destroy(&cmd);
  368. if (errno == ENOENT)
  369. ohshit(_("there is no script in the new version of the package - giving up"));
  370. else
  371. ohshite(_("unable to stat %s `%.250s'"),buf,cidir);
  372. }
  373. do_script(pkg, &pkg->available, &cmd, &stab, 0);
  374. fprintf(stderr, _("dpkg: ... it looks like that went OK.\n"));
  375. command_destroy(&cmd);
  376. post_script_tasks();
  377. return 1;
  378. }
  379. void clear_istobes(void) {
  380. struct pkgiterator *it;
  381. struct pkginfo *pkg;
  382. it = pkg_db_iter_new();
  383. while ((pkg = pkg_db_iter_next(it)) != NULL) {
  384. ensure_package_clientdata(pkg);
  385. pkg->clientdata->istobe= itb_normal;
  386. pkg->clientdata->replacingfilesandsaid= 0;
  387. }
  388. pkg_db_iter_free(it);
  389. }
  390. /*
  391. * Returns true if the directory contains conffiles belonging to pkg,
  392. * false otherwise.
  393. */
  394. bool
  395. dir_has_conffiles(struct filenamenode *file, struct pkginfo *pkg)
  396. {
  397. struct conffile *conff;
  398. size_t namelen;
  399. debug(dbg_veryverbose, "dir_has_conffiles '%s' (from %s)", file->name,
  400. pkg->name);
  401. namelen = strlen(file->name);
  402. for (conff= pkg->installed.conffiles; conff; conff= conff->next) {
  403. if (strncmp(file->name, conff->name, namelen) == 0 &&
  404. conff->name[namelen] == '/') {
  405. debug(dbg_veryverbose, "directory %s has conffile %s from %s",
  406. file->name, conff->name, pkg->name);
  407. return true;
  408. }
  409. }
  410. debug(dbg_veryverbose, "dir_has_conffiles no");
  411. return false;
  412. }
  413. /*
  414. * Returns true if the file is used by packages other than pkg,
  415. * false otherwise.
  416. */
  417. bool
  418. dir_is_used_by_others(struct filenamenode *file, struct pkginfo *pkg)
  419. {
  420. struct filepackages_iterator *iter;
  421. struct pkginfo *other_pkg;
  422. debug(dbg_veryverbose, "dir_is_used_by_others '%s' (except %s)", file->name,
  423. pkg ? pkg->name : "<none>");
  424. iter = filepackages_iter_new(file);
  425. while ((other_pkg = filepackages_iter_next(iter))) {
  426. debug(dbg_veryverbose, "dir_is_used_by_others considering %s ...",
  427. other_pkg->name);
  428. if (other_pkg == pkg)
  429. continue;
  430. debug(dbg_veryverbose, "dir_is_used_by_others yes");
  431. return true;
  432. }
  433. filepackages_iter_free(iter);
  434. debug(dbg_veryverbose, "dir_is_used_by_others no");
  435. return false;
  436. }
  437. /*
  438. * Returns true if the file is used by pkg, false otherwise.
  439. */
  440. bool
  441. dir_is_used_by_pkg(struct filenamenode *file, struct pkginfo *pkg,
  442. struct fileinlist *list)
  443. {
  444. struct fileinlist *node;
  445. size_t namelen;
  446. debug(dbg_veryverbose, "dir_is_used_by_pkg '%s' (by %s)",
  447. file->name, pkg ? pkg->name : "<none>");
  448. namelen = strlen(file->name);
  449. for (node = list; node; node = node->next) {
  450. debug(dbg_veryverbose, "dir_is_used_by_pkg considering %s ...",
  451. node->namenode->name);
  452. if (strncmp(file->name, node->namenode->name, namelen) == 0 &&
  453. node->namenode->name[namelen] == '/') {
  454. debug(dbg_veryverbose, "dir_is_used_by_pkg yes");
  455. return true;
  456. }
  457. }
  458. debug(dbg_veryverbose, "dir_is_used_by_pkg no");
  459. return false;
  460. }
  461. void oldconffsetflags(const struct conffile *searchconff) {
  462. struct filenamenode *namenode;
  463. while (searchconff) {
  464. namenode= findnamenode(searchconff->name, 0); /* XXX */
  465. namenode->flags |= fnnf_old_conff;
  466. if (!namenode->oldhash)
  467. namenode->oldhash= searchconff->hash;
  468. debug(dbg_conffdetail, "oldconffsetflags `%s' namenode %p flags %o",
  469. searchconff->name, namenode, namenode->flags);
  470. searchconff= searchconff->next;
  471. }
  472. }
  473. /*
  474. * If the pathname to remove is:
  475. *
  476. * 1. a sticky or set-id file, or
  477. * 2. an unknown object (i.e., not a file, link, directory, fifo or socket)
  478. *
  479. * we change its mode so that a malicious user cannot use it, even if it's
  480. * linked to another file.
  481. */
  482. int
  483. secure_unlink(const char *pathname)
  484. {
  485. struct stat stab;
  486. if (lstat(pathname,&stab)) return -1;
  487. return secure_unlink_statted(pathname, &stab);
  488. }
  489. int
  490. secure_unlink_statted(const char *pathname, const struct stat *stab)
  491. {
  492. if (S_ISREG(stab->st_mode) ? (stab->st_mode & 07000) :
  493. !(S_ISLNK(stab->st_mode) || S_ISDIR(stab->st_mode) ||
  494. S_ISFIFO(stab->st_mode) || S_ISSOCK(stab->st_mode))) {
  495. if (chmod(pathname, 0600))
  496. return -1;
  497. }
  498. if (unlink(pathname)) return -1;
  499. return 0;
  500. }
  501. void ensure_pathname_nonexisting(const char *pathname) {
  502. pid_t pid;
  503. const char *u;
  504. u = path_skip_slash_dotslash(pathname);
  505. assert(*u);
  506. debug(dbg_eachfile,"ensure_pathname_nonexisting `%s'",pathname);
  507. if (!rmdir(pathname))
  508. return; /* Deleted it OK, it was a directory. */
  509. if (errno == ENOENT || errno == ELOOP) return;
  510. if (errno == ENOTDIR) {
  511. /* Either it's a file, or one of the path components is. If one
  512. * of the path components is this will fail again ... */
  513. if (secure_unlink(pathname) == 0)
  514. return; /* OK, it was. */
  515. if (errno == ENOTDIR) return;
  516. }
  517. if (errno != ENOTEMPTY && errno != EEXIST) { /* Huh? */
  518. ohshite(_("unable to securely remove '%.255s'"), pathname);
  519. }
  520. pid = subproc_fork();
  521. if (pid == 0) {
  522. execlp(RM, "rm", "-rf", "--", pathname, NULL);
  523. ohshite(_("unable to execute %s (%s)"), _("rm command for cleanup"), RM);
  524. }
  525. debug(dbg_eachfile,"ensure_pathname_nonexisting running rm -rf");
  526. subproc_wait_check(pid, "rm cleanup", 0);
  527. }
  528. void log_action(const char *action, struct pkginfo *pkg) {
  529. log_message("%s %s %s %s", action, pkg->name,
  530. versiondescribe(&pkg->installed.version, vdew_nonambig),
  531. versiondescribe(&pkg->available.version, vdew_nonambig));
  532. statusfd_send("processing: %s: %s", action, pkg->name);
  533. }