configure.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  1. /*
  2. * dpkg - main program for package management
  3. * configure.c - configure packages
  4. *
  5. * Copyright © 1995 Ian Jackson <ian@chiark.greenend.org.uk>
  6. * Copyright © 1999, 2002 Wichert Akkerman <wichert@deephackmode.org>
  7. * Copyright © 2007-2015 Guillem Jover <guillem@debian.org>
  8. * Copyright © 2011 Linaro Limited
  9. * Copyright © 2011 Raphaël Hertzog <hertzog@debian.org>
  10. *
  11. * This is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  23. */
  24. #include <config.h>
  25. #include <compat.h>
  26. #include <sys/types.h>
  27. #include <sys/stat.h>
  28. #include <sys/wait.h>
  29. #include <assert.h>
  30. #include <errno.h>
  31. #include <ctype.h>
  32. #include <string.h>
  33. #include <time.h>
  34. #include <fcntl.h>
  35. #include <dirent.h>
  36. #include <termios.h>
  37. #include <unistd.h>
  38. #include <stdlib.h>
  39. #include <stdio.h>
  40. #include <dpkg/macros.h>
  41. #include <dpkg/i18n.h>
  42. #include <dpkg/dpkg.h>
  43. #include <dpkg/dpkg-db.h>
  44. #include <dpkg/pkg.h>
  45. #include <dpkg/string.h>
  46. #include <dpkg/buffer.h>
  47. #include <dpkg/file.h>
  48. #include <dpkg/path.h>
  49. #include <dpkg/subproc.h>
  50. #include <dpkg/command.h>
  51. #include <dpkg/triglib.h>
  52. #include "filesdb.h"
  53. #include "main.h"
  54. enum conffopt {
  55. CFOF_PROMPT = DPKG_BIT(0),
  56. CFOF_KEEP = DPKG_BIT(1),
  57. CFOF_INSTALL = DPKG_BIT(2),
  58. CFOF_BACKUP = DPKG_BIT(3),
  59. CFOF_NEW_CONFF = DPKG_BIT(4),
  60. CFOF_IS_NEW = DPKG_BIT(5),
  61. CFOF_IS_OLD = DPKG_BIT(6),
  62. CFOF_USER_DEL = DPKG_BIT(7),
  63. CFO_KEEP = CFOF_KEEP,
  64. CFO_IDENTICAL = CFOF_KEEP,
  65. CFO_INSTALL = CFOF_INSTALL,
  66. CFO_NEW_CONFF = CFOF_NEW_CONFF | CFOF_INSTALL,
  67. CFO_PROMPT = CFOF_PROMPT,
  68. CFO_PROMPT_KEEP = CFOF_PROMPT | CFOF_KEEP,
  69. CFO_PROMPT_INSTALL = CFOF_PROMPT | CFOF_INSTALL,
  70. };
  71. static int conffoptcells[2][2] = {
  72. /* Distro !edited. */ /* Distro edited. */
  73. { CFO_KEEP, CFO_INSTALL }, /* User !edited. */
  74. { CFO_KEEP, CFO_PROMPT_KEEP }, /* User edited. */
  75. };
  76. static int
  77. show_prompt(const char *cfgfile, const char *realold, const char *realnew,
  78. int useredited, int distedited, enum conffopt what)
  79. {
  80. const char *s;
  81. int c, cc;
  82. /* Flush the terminal's input in case the user involuntarily
  83. * typed some characters. */
  84. tcflush(STDIN_FILENO, TCIFLUSH);
  85. fputs("\n", stderr);
  86. if (strcmp(cfgfile, realold) == 0)
  87. fprintf(stderr, _("Configuration file '%s'\n"), cfgfile);
  88. else
  89. fprintf(stderr, _("Configuration file '%s' (actually '%s')\n"),
  90. cfgfile, realold);
  91. if (what & CFOF_IS_NEW) {
  92. fprintf(stderr,
  93. _(" ==> File on system created by you or by a script.\n"
  94. " ==> File also in package provided by package maintainer.\n"));
  95. } else {
  96. fprintf(stderr, !useredited ?
  97. _(" Not modified since installation.\n") :
  98. !(what & CFOF_USER_DEL) ?
  99. _(" ==> Modified (by you or by a script) since installation.\n") :
  100. _(" ==> Deleted (by you or by a script) since installation.\n"));
  101. fprintf(stderr, distedited ?
  102. _(" ==> Package distributor has shipped an updated version.\n") :
  103. _(" Version in package is the same as at last installation.\n"));
  104. }
  105. /* No --force-confdef but a forcible situtation. */
  106. /* TODO: check if this condition can not be simplified to
  107. * just !fc_conff_def */
  108. if (!(fc_conff_def && (what & (CFOF_INSTALL | CFOF_KEEP)))) {
  109. if (fc_conff_new) {
  110. fprintf(stderr,
  111. _(" ==> Using new file as you requested.\n"));
  112. return 'y';
  113. } else if (fc_conff_old) {
  114. fprintf(stderr,
  115. _(" ==> Using current old file as you requested.\n"));
  116. return 'n';
  117. }
  118. }
  119. /* Force the default action (if there is one. */
  120. if (fc_conff_def) {
  121. if (what & CFOF_KEEP) {
  122. fprintf(stderr,
  123. _(" ==> Keeping old config file as default.\n"));
  124. return 'n';
  125. } else if (what & CFOF_INSTALL) {
  126. fprintf(stderr,
  127. _(" ==> Using new config file as default.\n"));
  128. return 'y';
  129. }
  130. }
  131. fprintf(stderr,
  132. _(" What would you like to do about it ? Your options are:\n"
  133. " Y or I : install the package maintainer's version\n"
  134. " N or O : keep your currently-installed version\n"
  135. " D : show the differences between the versions\n"
  136. " Z : start a shell to examine the situation\n"));
  137. if (what & CFOF_KEEP)
  138. fprintf(stderr,
  139. _(" The default action is to keep your current version.\n"));
  140. else if (what & CFOF_INSTALL)
  141. fprintf(stderr,
  142. _(" The default action is to install the new version.\n"));
  143. s = path_basename(cfgfile);
  144. fprintf(stderr, "*** %s (Y/I/N/O/D/Z) %s ? ", s,
  145. (what & CFOF_KEEP) ? _("[default=N]") :
  146. (what & CFOF_INSTALL) ? _("[default=Y]") :
  147. _("[no default]"));
  148. if (ferror(stderr))
  149. ohshite(_("error writing to stderr, discovered before conffile prompt"));
  150. cc = 0;
  151. while ((c = getchar()) != EOF && c != '\n')
  152. if (!isspace(c) && !cc)
  153. cc = tolower(c);
  154. if (c == EOF) {
  155. if (ferror(stdin))
  156. ohshite(_("read error on stdin at conffile prompt"));
  157. ohshit(_("end of file on stdin at conffile prompt"));
  158. }
  159. if (!cc) {
  160. if (what & CFOF_KEEP)
  161. return 'n';
  162. else if (what & CFOF_INSTALL)
  163. return 'y';
  164. }
  165. return cc;
  166. }
  167. /**
  168. * Show a diff between two files.
  169. *
  170. * @param old The path to the old file.
  171. * @param new The path to the new file.
  172. */
  173. static void
  174. show_diff(const char *old, const char *new)
  175. {
  176. pid_t pid;
  177. pid = subproc_fork();
  178. if (!pid) {
  179. /* Child process. */
  180. char cmdbuf[1024];
  181. sprintf(cmdbuf, DIFF " -Nu %.250s %.250s | %.250s",
  182. str_quote_meta(old), str_quote_meta(new),
  183. command_get_pager());
  184. command_shell(cmdbuf, _("conffile difference visualizer"));
  185. }
  186. /* Parent process. */
  187. subproc_reap(pid, _("conffile difference visualizer"), SUBPROC_NOCHECK);
  188. }
  189. /**
  190. * Spawn a new shell.
  191. *
  192. * Create a subprocess and execute a shell to allow the user to manually
  193. * solve the conffile conflict.
  194. *
  195. * @param confold The path to the old conffile.
  196. * @param confnew The path to the new conffile.
  197. */
  198. static void
  199. spawn_shell(const char *confold, const char *confnew)
  200. {
  201. pid_t pid;
  202. fputs(_("Type 'exit' when you're done.\n"), stderr);
  203. pid = subproc_fork();
  204. if (!pid) {
  205. /* Set useful variables for the user. */
  206. setenv("DPKG_SHELL_REASON", "conffile-prompt", 1);
  207. setenv("DPKG_CONFFILE_OLD", confold, 1);
  208. setenv("DPKG_CONFFILE_NEW", confnew, 1);
  209. command_shell(NULL, _("conffile shell"));
  210. }
  211. /* Parent process. */
  212. subproc_reap(pid, _("conffile shell"), SUBPROC_NOCHECK);
  213. }
  214. /**
  215. * Prompt the user for how to resolve a conffile conflict.
  216. *
  217. * When encountering a conffile conflict during configuration, the user will
  218. * normally be presented with a textual menu of possible actions. This
  219. * behavior is modified via various --force flags and perhaps on whether
  220. * or not a terminal is available to do the prompting.
  221. *
  222. * @param pkg The package owning the conffile.
  223. * @param cfgfile The path to the old conffile.
  224. * @param realold The path to the old conffile, dereferenced in case of a
  225. * symlink, otherwise equal to cfgfile.
  226. * @param realnew The path to the new conffile, dereferenced in case of a
  227. * symlink).
  228. * @param useredited A flag to indicate whether the file has been edited
  229. * locally. Set to nonzero to indicate that the file has been modified.
  230. * @param distedited A flag to indicate whether the file has been updated
  231. * between package versions. Set to nonzero to indicate that the file
  232. * has been updated.
  233. * @param what Hints on what action should be taken by defualt.
  234. *
  235. * @return The action which should be taken based on user input and/or the
  236. * default actions as configured by cmdline/configuration options.
  237. */
  238. static enum conffopt
  239. promptconfaction(struct pkginfo *pkg, const char *cfgfile,
  240. const char *realold, const char *realnew,
  241. int useredited, int distedited, enum conffopt what)
  242. {
  243. int cc;
  244. if (!(what & CFOF_PROMPT))
  245. return what;
  246. statusfd_send("status: %s : %s : '%s' '%s' %i %i ",
  247. cfgfile, "conffile-prompt",
  248. realold, realnew, useredited, distedited);
  249. do {
  250. cc = show_prompt(cfgfile, realold, realnew,
  251. useredited, distedited, what);
  252. /* FIXME: Say something if silently not install. */
  253. if (cc == 'd')
  254. show_diff(realold, realnew);
  255. if (cc == 'z')
  256. spawn_shell(realold, realnew);
  257. } while (!strchr("yino", cc));
  258. log_message("conffile %s %s", cfgfile,
  259. (cc == 'i' || cc == 'y') ? "install" : "keep");
  260. what &= CFOF_USER_DEL;
  261. switch (cc) {
  262. case 'i':
  263. case 'y':
  264. what |= CFOF_INSTALL | CFOF_BACKUP;
  265. break;
  266. case 'n':
  267. case 'o':
  268. what |= CFOF_KEEP | CFOF_BACKUP;
  269. break;
  270. default:
  271. internerr("unknown response '%d'", cc);
  272. }
  273. return what;
  274. }
  275. /**
  276. * Configure the ghost conffile instance.
  277. *
  278. * When the first instance of a package set is configured, the *.dpkg-new
  279. * files gets installed into their destination, which makes configuration of
  280. * conffiles from subsequent package instances be skept along with updates
  281. * to the Conffiles field hash.
  282. *
  283. * In case the conffile has already been processed, sync the hash from an
  284. * already configured package instance conffile.
  285. *
  286. * @param pkg The current package being configured.
  287. * @param conff The current conffile being configured.
  288. */
  289. static void
  290. deferred_configure_ghost_conffile(struct pkginfo *pkg, struct conffile *conff)
  291. {
  292. struct pkginfo *otherpkg;
  293. struct conffile *otherconff;
  294. for (otherpkg = &pkg->set->pkg; otherpkg; otherpkg = otherpkg->arch_next) {
  295. if (otherpkg == pkg)
  296. continue;
  297. if (otherpkg->status <= PKG_STAT_HALFCONFIGURED)
  298. continue;
  299. for (otherconff = otherpkg->installed.conffiles; otherconff;
  300. otherconff = otherconff->next) {
  301. if (otherconff->obsolete)
  302. continue;
  303. /* Check if we need to propagate the new hash from
  304. * an already processed conffile in another package
  305. * instance. */
  306. if (strcmp(otherconff->name, conff->name) == 0) {
  307. conff->hash = otherconff->hash;
  308. modstatdb_note(pkg);
  309. return;
  310. }
  311. }
  312. }
  313. }
  314. static void
  315. deferred_configure_conffile(struct pkginfo *pkg, struct conffile *conff)
  316. {
  317. struct filenamenode *usenode;
  318. char currenthash[MD5HASHLEN + 1], newdisthash[MD5HASHLEN + 1];
  319. int useredited, distedited;
  320. enum conffopt what;
  321. struct stat stab;
  322. struct varbuf cdr = VARBUF_INIT, cdr2 = VARBUF_INIT;
  323. char *cdr2rest;
  324. int rc;
  325. usenode = namenodetouse(findnamenode(conff->name, fnn_nocopy),
  326. pkg, &pkg->installed);
  327. rc = conffderef(pkg, &cdr, usenode->name);
  328. if (rc == -1) {
  329. conff->hash = EMPTYHASHFLAG;
  330. return;
  331. }
  332. md5hash(pkg, currenthash, cdr.buf);
  333. varbuf_reset(&cdr2);
  334. varbuf_add_str(&cdr2, cdr.buf);
  335. varbuf_end_str(&cdr2);
  336. /* XXX: Make sure there's enough room for extensions. */
  337. varbuf_grow(&cdr2, 50);
  338. cdr2rest = cdr2.buf + strlen(cdr.buf);
  339. /* From now on we can just strcpy(cdr2rest, extension); */
  340. strcpy(cdr2rest, DPKGNEWEXT);
  341. /* If the .dpkg-new file is no longer there, ignore this one. */
  342. if (lstat(cdr2.buf, &stab)) {
  343. if (errno == ENOENT) {
  344. /* But, sync the conffile hash value from another
  345. * package set instance. */
  346. deferred_configure_ghost_conffile(pkg, conff);
  347. return;
  348. }
  349. ohshite(_("unable to stat new distributed conffile '%.250s'"),
  350. cdr2.buf);
  351. }
  352. md5hash(pkg, newdisthash, cdr2.buf);
  353. /* Copy the permissions from the installed version to the new
  354. * distributed version. */
  355. if (!stat(cdr.buf, &stab))
  356. file_copy_perms(cdr.buf, cdr2.buf);
  357. else if (errno != ENOENT)
  358. ohshite(_("unable to stat current installed conffile '%.250s'"),
  359. cdr.buf);
  360. /* Select what to do. */
  361. if (strcmp(currenthash, newdisthash) == 0) {
  362. /* They're both the same so there's no point asking silly
  363. * questions. */
  364. useredited = -1;
  365. distedited = -1;
  366. what = CFO_IDENTICAL;
  367. } else if (strcmp(currenthash, NONEXISTENTFLAG) == 0 && fc_conff_miss) {
  368. fprintf(stderr,
  369. _("\n"
  370. "Configuration file '%s', does not exist on system.\n"
  371. "Installing new config file as you requested.\n"),
  372. usenode->name);
  373. what = CFO_NEW_CONFF;
  374. useredited = -1;
  375. distedited = -1;
  376. } else if (strcmp(conff->hash, NEWCONFFILEFLAG) == 0) {
  377. if (strcmp(currenthash, NONEXISTENTFLAG) == 0) {
  378. what = CFO_NEW_CONFF;
  379. useredited = -1;
  380. distedited = -1;
  381. } else {
  382. useredited = 1;
  383. distedited = 1;
  384. what = conffoptcells[useredited][distedited] |
  385. CFOF_IS_NEW;
  386. }
  387. } else {
  388. useredited = strcmp(conff->hash, currenthash) != 0;
  389. distedited = strcmp(conff->hash, newdisthash) != 0;
  390. if (fc_conff_ask && useredited)
  391. what = CFO_PROMPT_KEEP;
  392. else
  393. what = conffoptcells[useredited][distedited];
  394. if (strcmp(currenthash, NONEXISTENTFLAG) == 0)
  395. what |= CFOF_USER_DEL;
  396. }
  397. debug(dbg_conff,
  398. "deferred_configure '%s' (= '%s') useredited=%d distedited=%d what=%o",
  399. usenode->name, cdr.buf, useredited, distedited, what);
  400. what = promptconfaction(pkg, usenode->name, cdr.buf, cdr2.buf,
  401. useredited, distedited, what);
  402. switch (what & ~(CFOF_IS_NEW | CFOF_USER_DEL)) {
  403. case CFO_KEEP | CFOF_BACKUP:
  404. strcpy(cdr2rest, DPKGOLDEXT);
  405. if (unlink(cdr2.buf) && errno != ENOENT)
  406. warning(_("%s: failed to remove old backup '%.250s': %s"),
  407. pkg_name(pkg, pnaw_nonambig), cdr2.buf,
  408. strerror(errno));
  409. varbuf_add_str(&cdr, DPKGDISTEXT);
  410. varbuf_end_str(&cdr);
  411. strcpy(cdr2rest, DPKGNEWEXT);
  412. trig_path_activate(usenode, pkg);
  413. if (rename(cdr2.buf, cdr.buf))
  414. warning(_("%s: failed to rename '%.250s' to '%.250s': %s"),
  415. pkg_name(pkg, pnaw_nonambig), cdr2.buf, cdr.buf,
  416. strerror(errno));
  417. break;
  418. case CFO_KEEP:
  419. strcpy(cdr2rest, DPKGNEWEXT);
  420. if (unlink(cdr2.buf))
  421. warning(_("%s: failed to remove '%.250s': %s"),
  422. pkg_name(pkg, pnaw_nonambig), cdr2.buf,
  423. strerror(errno));
  424. break;
  425. case CFO_INSTALL | CFOF_BACKUP:
  426. strcpy(cdr2rest, DPKGDISTEXT);
  427. if (unlink(cdr2.buf) && errno != ENOENT)
  428. warning(_("%s: failed to remove old distributed version '%.250s': %s"),
  429. pkg_name(pkg, pnaw_nonambig), cdr2.buf,
  430. strerror(errno));
  431. strcpy(cdr2rest, DPKGOLDEXT);
  432. if (unlink(cdr2.buf) && errno != ENOENT)
  433. warning(_("%s: failed to remove '%.250s' (before overwrite): %s"),
  434. pkg_name(pkg, pnaw_nonambig), cdr2.buf,
  435. strerror(errno));
  436. if (!(what & CFOF_USER_DEL))
  437. if (link(cdr.buf, cdr2.buf))
  438. warning(_("%s: failed to link '%.250s' to '%.250s': %s"),
  439. pkg_name(pkg, pnaw_nonambig), cdr.buf,
  440. cdr2.buf, strerror(errno));
  441. /* Fall through. */
  442. case CFO_INSTALL:
  443. printf(_("Installing new version of config file %s ...\n"),
  444. usenode->name);
  445. /* Fall through. */
  446. case CFO_NEW_CONFF:
  447. strcpy(cdr2rest, DPKGNEWEXT);
  448. trig_path_activate(usenode, pkg);
  449. if (rename(cdr2.buf, cdr.buf))
  450. ohshite(_("unable to install '%.250s' as '%.250s'"),
  451. cdr2.buf, cdr.buf);
  452. break;
  453. default:
  454. internerr("unknown conffopt '%d'", what);
  455. }
  456. conff->hash = nfstrsave(newdisthash);
  457. modstatdb_note(pkg);
  458. varbuf_destroy(&cdr);
  459. varbuf_destroy(&cdr2);
  460. }
  461. /**
  462. * Process the deferred configure package.
  463. *
  464. * The algorithm for deciding what to configure first is as follows:
  465. * Loop through all packages doing a ‘try 1’ until we've been round
  466. * and nothing has been done, then do ‘try 2’ and ‘try 3’ likewise.
  467. * The incrementing of ‘dependtry’ is done by process_queue().
  468. *
  469. * Try 1:
  470. * Are all dependencies of this package done? If so, do it.
  471. * Are any of the dependencies missing or the wrong version?
  472. * If so, abort (unless --force-depends, in which case defer).
  473. * Will we need to configure a package we weren't given as an
  474. * argument? If so, abort ─ except if --force-configure-any,
  475. * in which case we add the package to the argument list.
  476. * If none of the above, defer the package.
  477. *
  478. * Try 2:
  479. * Find a cycle and break it (see above).
  480. * Do as for try 1.
  481. *
  482. * Try 3 (only if --force-depends-version):
  483. * Same as for try 2, but don't mind version number in dependencies.
  484. *
  485. * Try 4 (only if --force-depends):
  486. * Do anyway.
  487. *
  488. * @param pkg The package to act on.
  489. */
  490. void
  491. deferred_configure(struct pkginfo *pkg)
  492. {
  493. struct varbuf aemsgs = VARBUF_INIT;
  494. struct conffile *conff;
  495. struct pkginfo *otherpkg;
  496. enum dep_check ok;
  497. if (pkg->status == PKG_STAT_NOTINSTALLED)
  498. ohshit(_("no package named '%s' is installed, cannot configure"),
  499. pkg_name(pkg, pnaw_nonambig));
  500. if (pkg->status == PKG_STAT_INSTALLED)
  501. ohshit(_("package %.250s is already installed and configured"),
  502. pkg_name(pkg, pnaw_nonambig));
  503. if (pkg->status != PKG_STAT_UNPACKED &&
  504. pkg->status != PKG_STAT_HALFCONFIGURED)
  505. ohshit(_("package %.250s is not ready for configuration\n"
  506. " cannot configure (current status '%.250s')"),
  507. pkg_name(pkg, pnaw_nonambig),
  508. pkg_status_name(pkg));
  509. for (otherpkg = &pkg->set->pkg; otherpkg; otherpkg = otherpkg->arch_next) {
  510. if (otherpkg == pkg)
  511. continue;
  512. if (otherpkg->status <= PKG_STAT_CONFIGFILES)
  513. continue;
  514. if (otherpkg->status < PKG_STAT_UNPACKED)
  515. ohshit(_("package %s cannot be configured because "
  516. "%s is not ready (current status '%s')"),
  517. pkg_name(pkg, pnaw_always),
  518. pkg_name(otherpkg, pnaw_always),
  519. pkg_status_name(otherpkg));
  520. if (dpkg_version_compare(&pkg->installed.version,
  521. &otherpkg->installed.version))
  522. ohshit(_("package %s %s cannot be configured because "
  523. "%s is at a different version (%s)"),
  524. pkg_name(pkg, pnaw_always),
  525. versiondescribe(&pkg->installed.version,
  526. vdew_nonambig),
  527. pkg_name(otherpkg, pnaw_always),
  528. versiondescribe(&otherpkg->installed.version,
  529. vdew_nonambig));
  530. }
  531. if (dependtry > 1)
  532. if (findbreakcycle(pkg))
  533. sincenothing = 0;
  534. ok = dependencies_ok(pkg, NULL, &aemsgs);
  535. if (ok == DEP_CHECK_DEFER) {
  536. varbuf_destroy(&aemsgs);
  537. pkg->clientdata->istobe = PKG_ISTOBE_INSTALLNEW;
  538. enqueue_package(pkg);
  539. return;
  540. }
  541. trigproc_reset_cycle();
  542. /*
  543. * At this point removal from the queue is confirmed. This
  544. * represents irreversible progress wrt trigger cycles. Only
  545. * packages in PKG_STAT_UNPACKED are automatically added to the
  546. * configuration queue, and during configuration and trigger
  547. * processing new packages can't enter into unpacked.
  548. */
  549. ok = breakses_ok(pkg, &aemsgs) ? ok : DEP_CHECK_HALT;
  550. if (ok == DEP_CHECK_HALT) {
  551. sincenothing = 0;
  552. varbuf_end_str(&aemsgs);
  553. notice(_("dependency problems prevent configuration of %s:\n%s"),
  554. pkg_name(pkg, pnaw_nonambig), aemsgs.buf);
  555. varbuf_destroy(&aemsgs);
  556. ohshit(_("dependency problems - leaving unconfigured"));
  557. } else if (aemsgs.used) {
  558. varbuf_end_str(&aemsgs);
  559. notice(_("%s: dependency problems, but configuring anyway as you requested:\n%s"),
  560. pkg_name(pkg, pnaw_nonambig), aemsgs.buf);
  561. }
  562. varbuf_destroy(&aemsgs);
  563. sincenothing = 0;
  564. if (pkg->eflag & PKG_EFLAG_REINSTREQ)
  565. forcibleerr(fc_removereinstreq,
  566. _("package is in a very bad inconsistent state; you should\n"
  567. " reinstall it before attempting configuration"));
  568. printf(_("Setting up %s (%s) ...\n"), pkg_name(pkg, pnaw_nonambig),
  569. versiondescribe(&pkg->installed.version, vdew_nonambig));
  570. log_action("configure", pkg, &pkg->installed);
  571. trig_activate_packageprocessing(pkg);
  572. if (f_noact) {
  573. pkg_set_status(pkg, PKG_STAT_INSTALLED);
  574. pkg->clientdata->istobe = PKG_ISTOBE_NORMAL;
  575. return;
  576. }
  577. if (pkg->status == PKG_STAT_UNPACKED) {
  578. debug(dbg_general, "deferred_configure updating conffiles");
  579. /* This will not do at all the right thing with overridden
  580. * conffiles or conffiles that are the ‘target’ of an override;
  581. * all the references here would be to the ‘contested’
  582. * filename, and in any case there'd only be one hash for both
  583. * ‘versions’ of the conffile.
  584. *
  585. * Overriding conffiles is a silly thing to do anyway :-). */
  586. modstatdb_note(pkg);
  587. /* On entry, the ‘new’ version of each conffile has been
  588. * unpacked as ‘*.dpkg-new’, and the ‘installed’ version is
  589. * as-yet untouched in ‘*’. The hash of the ‘old distributed’
  590. * version is in the conffiles data for the package. If
  591. * ‘*.dpkg-new’ no longer exists we assume that we've
  592. * already processed this one. */
  593. for (conff = pkg->installed.conffiles; conff; conff = conff->next) {
  594. if (conff->obsolete)
  595. continue;
  596. deferred_configure_conffile(pkg, conff);
  597. }
  598. pkg_set_status(pkg, PKG_STAT_HALFCONFIGURED);
  599. }
  600. assert(pkg->status == PKG_STAT_HALFCONFIGURED);
  601. modstatdb_note(pkg);
  602. maintscript_postinst(pkg, "configure",
  603. dpkg_version_is_informative(&pkg->configversion) ?
  604. versiondescribe(&pkg->configversion,
  605. vdew_nonambig) : "",
  606. NULL);
  607. pkg_reset_eflags(pkg);
  608. pkg->trigpend_head = NULL;
  609. post_postinst_tasks(pkg, PKG_STAT_INSTALLED);
  610. }
  611. /**
  612. * Dereference a file by following all possibly used symlinks.
  613. *
  614. * @param[in] pkg The package to act on.
  615. * @param[out] result The dereference conffile path.
  616. * @param[in] in The conffile path to dereference.
  617. *
  618. * @return An error code for the operation.
  619. * @retval 0 Everything went ok.
  620. * @retval -1 Otherwise.
  621. */
  622. int
  623. conffderef(struct pkginfo *pkg, struct varbuf *result, const char *in)
  624. {
  625. static struct varbuf target = VARBUF_INIT;
  626. struct stat stab;
  627. ssize_t r;
  628. int loopprotect;
  629. varbuf_reset(result);
  630. varbuf_add_str(result, instdir);
  631. varbuf_add_str(result, in);
  632. varbuf_end_str(result);
  633. loopprotect = 0;
  634. for (;;) {
  635. debug(dbg_conffdetail, "conffderef in='%s' current working='%s'",
  636. in, result->buf);
  637. if (lstat(result->buf, &stab)) {
  638. if (errno != ENOENT)
  639. warning(_("%s: unable to stat config file '%s'\n"
  640. " (= '%s'): %s"),
  641. pkg_name(pkg, pnaw_nonambig), in,
  642. result->buf, strerror(errno));
  643. debug(dbg_conffdetail, "conffderef nonexistent");
  644. return 0;
  645. } else if (S_ISREG(stab.st_mode)) {
  646. debug(dbg_conff, "conffderef in='%s' result='%s'",
  647. in, result->buf);
  648. return 0;
  649. } else if (S_ISLNK(stab.st_mode)) {
  650. debug(dbg_conffdetail, "conffderef symlink loopprotect=%d",
  651. loopprotect);
  652. if (loopprotect++ >= 25) {
  653. warning(_("%s: config file '%s' is a circular link\n"
  654. " (= '%s')"),
  655. pkg_name(pkg, pnaw_nonambig), in,
  656. result->buf);
  657. return -1;
  658. }
  659. varbuf_reset(&target);
  660. varbuf_grow(&target, stab.st_size + 1);
  661. r = readlink(result->buf, target.buf, target.size);
  662. if (r < 0) {
  663. warning(_("%s: unable to readlink conffile '%s'\n"
  664. " (= '%s'): %s"),
  665. pkg_name(pkg, pnaw_nonambig), in,
  666. result->buf, strerror(errno));
  667. return -1;
  668. } else if (r != stab.st_size) {
  669. warning(_("symbolic link '%.250s' size has "
  670. "changed from %jd to %zd"),
  671. result->buf, stab.st_size, r);
  672. return -1;
  673. }
  674. varbuf_trunc(&target, r);
  675. varbuf_end_str(&target);
  676. debug(dbg_conffdetail,
  677. "conffderef readlink gave %zd, '%s'",
  678. r, target.buf);
  679. if (target.buf[0] == '/') {
  680. varbuf_reset(result);
  681. varbuf_add_str(result, instdir);
  682. debug(dbg_conffdetail,
  683. "conffderef readlink absolute");
  684. } else {
  685. for (r = result->used - 1; r > 0 && result->buf[r] != '/'; r--)
  686. ;
  687. if (r < 0) {
  688. warning(_("%s: conffile '%.250s' resolves to degenerate filename\n"
  689. " ('%s' is a symlink to '%s')"),
  690. pkg_name(pkg, pnaw_nonambig),
  691. in, result->buf, target.buf);
  692. return -1;
  693. }
  694. if (result->buf[r] == '/')
  695. r++;
  696. varbuf_trunc(result, r);
  697. debug(dbg_conffdetail,
  698. "conffderef readlink relative to '%.*s'",
  699. (int)result->used, result->buf);
  700. }
  701. varbuf_add_buf(result, target.buf, target.used);
  702. varbuf_end_str(result);
  703. } else {
  704. warning(_("%s: conffile '%.250s' is not a plain file or symlink (= '%s')"),
  705. pkg_name(pkg, pnaw_nonambig), in, result->buf);
  706. return -1;
  707. }
  708. }
  709. }
  710. /**
  711. * Generate a file contents MD5 hash.
  712. *
  713. * The caller is responsible for providing a buffer for the hash result
  714. * at least MD5HASHLEN + 1 characters long.
  715. *
  716. * @param[in] pkg The package to act on.
  717. * @param[out] hashbuf The buffer to store the generated hash.
  718. * @param[in] fn The filename.
  719. */
  720. void
  721. md5hash(struct pkginfo *pkg, char *hashbuf, const char *fn)
  722. {
  723. struct dpkg_error err;
  724. static int fd;
  725. fd = open(fn, O_RDONLY);
  726. if (fd >= 0) {
  727. push_cleanup(cu_closefd, ehflag_bombout, NULL, 0, 1, &fd);
  728. if (fd_md5(fd, hashbuf, -1, &err) < 0)
  729. ohshit(_("cannot compute MD5 hash for file '%s': %s"),
  730. fn, err.str);
  731. pop_cleanup(ehflag_normaltidy); /* fd = open(cdr.buf) */
  732. close(fd);
  733. } else if (errno == ENOENT) {
  734. strcpy(hashbuf, NONEXISTENTFLAG);
  735. } else {
  736. warning(_("%s: unable to open %s for hash: %s"),
  737. pkg_name(pkg, pnaw_nonambig), fn, strerror(errno));
  738. strcpy(hashbuf, EMPTYHASHFLAG);
  739. }
  740. }