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