help.c 16 KB

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