configure.c 25 KB

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