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 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 (otherconff->obsolete || otherconff->remove_on_upgrade)
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, cdr2 = VARBUF_INIT;
383 : : char *cdr2rest;
384 : : int rc;
385 : :
386 : 0 : usenode = namenodetouse(fsys_hash_find_node(conff->name, FHFF_NOCOPY),
387 : : pkg, &pkg->installed);
388 : :
389 : 0 : rc = conffderef(pkg, &cdr, usenode->name);
390 [ # # ]: 0 : if (rc == -1) {
391 : 0 : conff->hash = EMPTYHASHFLAG;
392 : 0 : return;
393 : : }
394 : 0 : md5hash(pkg, currenthash, cdr.buf);
395 : :
396 : 0 : varbuf_reset(&cdr2);
397 : 0 : varbuf_add_str(&cdr2, cdr.buf);
398 : 0 : varbuf_end_str(&cdr2);
399 : : /* XXX: Make sure there's enough room for extensions. */
400 : 0 : varbuf_grow(&cdr2, 50);
401 : 0 : cdr2rest = cdr2.buf + strlen(cdr.buf);
402 : : /* From now on we can just strcpy(cdr2rest, extension); */
403 : :
404 : 0 : strcpy(cdr2rest, DPKGNEWEXT);
405 : : /* If the .dpkg-new file is no longer there, ignore this one. */
406 [ # # ]: 0 : if (lstat(cdr2.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 : : cdr2.buf);
415 : : }
416 : 0 : md5hash(pkg, newdisthash, cdr2.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, cdr2.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, cdr2.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 : strcpy(cdr2rest, DPKGOLDEXT);
476 [ # # # # ]: 0 : if (unlink(cdr2.buf) && errno != ENOENT)
477 : 0 : warning(_("%s: failed to remove old backup '%.250s': %s"),
478 : : pkg_name(pkg, pnaw_nonambig), cdr2.buf,
479 : 0 : strerror(errno));
480 : :
481 : 0 : varbuf_add_str(&cdr, DPKGDISTEXT);
482 : 0 : varbuf_end_str(&cdr);
483 : 0 : strcpy(cdr2rest, DPKGNEWEXT);
484 : 0 : trig_path_activate(usenode, pkg);
485 [ # # ]: 0 : if (rename(cdr2.buf, cdr.buf))
486 : 0 : warning(_("%s: failed to rename '%.250s' to '%.250s': %s"),
487 : : pkg_name(pkg, pnaw_nonambig), cdr2.buf, cdr.buf,
488 : 0 : strerror(errno));
489 : 0 : break;
490 : 0 : case CFO_KEEP:
491 : 0 : strcpy(cdr2rest, DPKGNEWEXT);
492 [ # # ]: 0 : if (unlink(cdr2.buf))
493 : 0 : warning(_("%s: failed to remove '%.250s': %s"),
494 : : pkg_name(pkg, pnaw_nonambig), cdr2.buf,
495 : 0 : strerror(errno));
496 : 0 : break;
497 : 0 : case CFO_INSTALL | CFOF_BACKUP:
498 : 0 : strcpy(cdr2rest, DPKGDISTEXT);
499 [ # # # # ]: 0 : if (unlink(cdr2.buf) && errno != ENOENT)
500 : 0 : warning(_("%s: failed to remove old distributed version '%.250s': %s"),
501 : : pkg_name(pkg, pnaw_nonambig), cdr2.buf,
502 : 0 : strerror(errno));
503 : 0 : strcpy(cdr2rest, DPKGOLDEXT);
504 [ # # # # ]: 0 : if (unlink(cdr2.buf) && errno != ENOENT)
505 : 0 : warning(_("%s: failed to remove '%.250s' (before overwrite): %s"),
506 : : pkg_name(pkg, pnaw_nonambig), cdr2.buf,
507 : 0 : strerror(errno));
508 [ # # ]: 0 : if (!(what & CFOF_USER_DEL))
509 [ # # ]: 0 : if (link(cdr.buf, cdr2.buf))
510 : 0 : warning(_("%s: failed to link '%.250s' to '%.250s': %s"),
511 : : pkg_name(pkg, pnaw_nonambig), cdr.buf,
512 : 0 : cdr2.buf, strerror(errno));
513 : : /* Fall through. */
514 : : case CFO_INSTALL:
515 : 0 : printf(_("Installing new version of config file %s ...\n"),
516 : : usenode->name);
517 : : /* Fall through. */
518 : 0 : case CFO_NEW_CONFF:
519 : 0 : strcpy(cdr2rest, DPKGNEWEXT);
520 : 0 : trig_path_activate(usenode, pkg);
521 [ # # ]: 0 : if (rename(cdr2.buf, cdr.buf))
522 : 0 : ohshite(_("unable to install '%.250s' as '%.250s'"),
523 : : cdr2.buf, cdr.buf);
524 : 0 : break;
525 : 0 : default:
526 : 0 : internerr("unknown conffopt '%d'", what);
527 : : }
528 : :
529 : 0 : conff->hash = nfstrsave(newdisthash);
530 : 0 : modstatdb_note(pkg);
531 : :
532 : 0 : varbuf_destroy(&cdr);
533 : 0 : varbuf_destroy(&cdr2);
534 : : }
535 : :
536 : : /**
537 : : * Process the deferred configure package.
538 : : *
539 : : * @param pkg The package to act on.
540 : : */
541 : : void
542 : 0 : deferred_configure(struct pkginfo *pkg)
543 : : {
544 : 0 : struct varbuf aemsgs = VARBUF_INIT;
545 : : struct conffile *conff;
546 : : struct pkginfo *otherpkg;
547 : : enum dep_check ok;
548 : :
549 [ # # ]: 0 : if (pkg->status == PKG_STAT_NOTINSTALLED)
550 : 0 : ohshit(_("no package named '%s' is installed, cannot configure"),
551 : : pkg_name(pkg, pnaw_nonambig));
552 [ # # ]: 0 : if (pkg->status == PKG_STAT_INSTALLED)
553 : 0 : ohshit(_("package %.250s is already installed and configured"),
554 : : pkg_name(pkg, pnaw_nonambig));
555 [ # # ]: 0 : if (pkg->status != PKG_STAT_UNPACKED &&
556 [ # # ]: 0 : pkg->status != PKG_STAT_HALFCONFIGURED)
557 : 0 : ohshit(_("package %.250s is not ready for configuration\n"
558 : : " cannot configure (current status '%.250s')"),
559 : : pkg_name(pkg, pnaw_nonambig),
560 : : pkg_status_name(pkg));
561 : :
562 [ # # ]: 0 : for (otherpkg = &pkg->set->pkg; otherpkg; otherpkg = otherpkg->arch_next) {
563 [ # # ]: 0 : if (otherpkg == pkg)
564 : 0 : continue;
565 [ # # ]: 0 : if (otherpkg->status <= PKG_STAT_CONFIGFILES)
566 : 0 : continue;
567 : :
568 [ # # ]: 0 : if (otherpkg->status < PKG_STAT_UNPACKED)
569 : 0 : ohshit(_("package %s cannot be configured because "
570 : : "%s is not ready (current status '%s')"),
571 : : pkg_name(pkg, pnaw_always),
572 : : pkg_name(otherpkg, pnaw_always),
573 : : pkg_status_name(otherpkg));
574 : :
575 [ # # ]: 0 : if (dpkg_version_compare(&pkg->installed.version,
576 : 0 : &otherpkg->installed.version))
577 : 0 : ohshit(_("package %s %s cannot be configured because "
578 : : "%s is at a different version (%s)"),
579 : : pkg_name(pkg, pnaw_always),
580 : 0 : versiondescribe(&pkg->installed.version,
581 : : vdew_nonambig),
582 : : pkg_name(otherpkg, pnaw_always),
583 : 0 : versiondescribe(&otherpkg->installed.version,
584 : : vdew_nonambig));
585 : : }
586 : :
587 [ # # ]: 0 : if (dependtry >= DEPEND_TRY_CYCLES)
588 [ # # ]: 0 : if (findbreakcycle(pkg))
589 : 0 : sincenothing = 0;
590 : :
591 : 0 : ok = dependencies_ok(pkg, NULL, &aemsgs);
592 [ # # ]: 0 : if (ok == DEP_CHECK_DEFER) {
593 : 0 : varbuf_destroy(&aemsgs);
594 : 0 : ensure_package_clientdata(pkg);
595 : 0 : pkg->clientdata->istobe = PKG_ISTOBE_INSTALLNEW;
596 : 0 : enqueue_package(pkg);
597 : 0 : return;
598 : : }
599 : :
600 : 0 : trigproc_reset_cycle();
601 : :
602 : : /*
603 : : * At this point removal from the queue is confirmed. This
604 : : * represents irreversible progress wrt trigger cycles. Only
605 : : * packages in PKG_STAT_UNPACKED are automatically added to the
606 : : * configuration queue, and during configuration and trigger
607 : : * processing new packages can't enter into unpacked.
608 : : */
609 : :
610 [ # # ]: 0 : ok = breakses_ok(pkg, &aemsgs) ? ok : DEP_CHECK_HALT;
611 [ # # ]: 0 : if (ok == DEP_CHECK_HALT) {
612 : 0 : sincenothing = 0;
613 : 0 : varbuf_end_str(&aemsgs);
614 : 0 : notice(_("dependency problems prevent configuration of %s:\n%s"),
615 : : pkg_name(pkg, pnaw_nonambig), aemsgs.buf);
616 : 0 : varbuf_destroy(&aemsgs);
617 : 0 : ohshit(_("dependency problems - leaving unconfigured"));
618 [ # # ]: 0 : } else if (aemsgs.used) {
619 : 0 : varbuf_end_str(&aemsgs);
620 : 0 : notice(_("%s: dependency problems, but configuring anyway as you requested:\n%s"),
621 : : pkg_name(pkg, pnaw_nonambig), aemsgs.buf);
622 : : }
623 : 0 : varbuf_destroy(&aemsgs);
624 : 0 : sincenothing = 0;
625 : :
626 [ # # ]: 0 : if (pkg->eflag & PKG_EFLAG_REINSTREQ)
627 : 0 : forcibleerr(FORCE_REMOVE_REINSTREQ,
628 : 0 : _("package is in a very bad inconsistent state; you should\n"
629 : : " reinstall it before attempting configuration"));
630 : :
631 : 0 : printf(_("Setting up %s (%s) ...\n"), pkg_name(pkg, pnaw_nonambig),
632 : 0 : versiondescribe(&pkg->installed.version, vdew_nonambig));
633 : 0 : log_action("configure", pkg, &pkg->installed);
634 : :
635 : 0 : trig_activate_packageprocessing(pkg);
636 : :
637 [ # # ]: 0 : if (f_noact) {
638 : 0 : pkg_set_status(pkg, PKG_STAT_INSTALLED);
639 : 0 : ensure_package_clientdata(pkg);
640 : 0 : pkg->clientdata->istobe = PKG_ISTOBE_NORMAL;
641 : 0 : return;
642 : : }
643 : :
644 [ # # ]: 0 : if (pkg->status == PKG_STAT_UNPACKED) {
645 : 0 : debug(dbg_general, "deferred_configure updating conffiles");
646 : : /* This will not do at all the right thing with overridden
647 : : * conffiles or conffiles that are the ‘target’ of an override;
648 : : * all the references here would be to the ‘contested’
649 : : * filename, and in any case there'd only be one hash for both
650 : : * ‘versions’ of the conffile.
651 : : *
652 : : * Overriding conffiles is a silly thing to do anyway :-). */
653 : :
654 : 0 : modstatdb_note(pkg);
655 : :
656 : : /* On entry, the ‘new’ version of each conffile has been
657 : : * unpacked as ‘*.dpkg-new’, and the ‘installed’ version is
658 : : * as-yet untouched in ‘*’. The hash of the ‘old distributed’
659 : : * version is in the conffiles data for the package. If
660 : : * ‘*.dpkg-new’ no longer exists we assume that we've
661 : : * already processed this one. */
662 [ # # ]: 0 : for (conff = pkg->installed.conffiles; conff; conff = conff->next) {
663 [ # # # # ]: 0 : if (conff->obsolete || conff->remove_on_upgrade)
664 : 0 : continue;
665 : 0 : deferred_configure_conffile(pkg, conff);
666 : : }
667 : :
668 : 0 : pkg_set_status(pkg, PKG_STAT_HALFCONFIGURED);
669 : : }
670 : :
671 [ # # ]: 0 : if (pkg->status != PKG_STAT_HALFCONFIGURED)
672 : 0 : internerr("package %s in state %s, instead of half-configured",
673 : : pkg_name(pkg, pnaw_always), pkg_status_name(pkg));
674 : :
675 : 0 : modstatdb_note(pkg);
676 : :
677 [ # # ]: 0 : maintscript_postinst(pkg, "configure",
678 : 0 : dpkg_version_is_informative(&pkg->configversion) ?
679 : 0 : versiondescribe(&pkg->configversion,
680 : : vdew_nonambig) : "",
681 : : NULL);
682 : :
683 : 0 : pkg_reset_eflags(pkg);
684 : 0 : pkg->trigpend_head = NULL;
685 : 0 : post_postinst_tasks(pkg, PKG_STAT_INSTALLED);
686 : : }
687 : :
688 : : /**
689 : : * Dereference a file by following all possibly used symlinks.
690 : : *
691 : : * @param[in] pkg The package to act on.
692 : : * @param[out] result The dereference conffile path.
693 : : * @param[in] in The conffile path to dereference.
694 : : *
695 : : * @return An error code for the operation.
696 : : * @retval 0 Everything went ok.
697 : : * @retval -1 Otherwise.
698 : : */
699 : : int
700 : 0 : conffderef(struct pkginfo *pkg, struct varbuf *result, const char *in)
701 : : {
702 : : static struct varbuf target = VARBUF_INIT;
703 : : struct stat stab;
704 : : ssize_t r;
705 : : int loopprotect;
706 : :
707 : 0 : varbuf_reset(result);
708 : 0 : varbuf_add_str(result, dpkg_fsys_get_dir());
709 : 0 : varbuf_add_str(result, in);
710 : 0 : varbuf_end_str(result);
711 : :
712 : 0 : loopprotect = 0;
713 : :
714 : : for (;;) {
715 : 0 : debug(dbg_conffdetail, "conffderef in='%s' current working='%s'",
716 : : in, result->buf);
717 [ # # ]: 0 : if (lstat(result->buf, &stab)) {
718 [ # # ]: 0 : if (errno != ENOENT)
719 : 0 : warning(_("%s: unable to stat config file '%s'\n"
720 : : " (= '%s'): %s"),
721 : : pkg_name(pkg, pnaw_nonambig), in,
722 : 0 : result->buf, strerror(errno));
723 : 0 : debug(dbg_conffdetail, "conffderef nonexistent");
724 : 0 : return 0;
725 [ # # ]: 0 : } else if (S_ISREG(stab.st_mode)) {
726 : 0 : debug(dbg_conff, "conffderef in='%s' result='%s'",
727 : : in, result->buf);
728 : 0 : return 0;
729 [ # # ]: 0 : } else if (S_ISLNK(stab.st_mode)) {
730 : 0 : debug(dbg_conffdetail, "conffderef symlink loopprotect=%d",
731 : : loopprotect);
732 [ # # ]: 0 : if (loopprotect++ >= 25) {
733 : 0 : warning(_("%s: config file '%s' is a circular link\n"
734 : : " (= '%s')"),
735 : : pkg_name(pkg, pnaw_nonambig), in,
736 : : result->buf);
737 : 0 : return -1;
738 : : }
739 : :
740 : 0 : varbuf_reset(&target);
741 : 0 : varbuf_grow(&target, stab.st_size + 1);
742 : 0 : r = readlink(result->buf, target.buf, target.size);
743 [ # # ]: 0 : if (r < 0) {
744 : 0 : warning(_("%s: unable to readlink conffile '%s'\n"
745 : : " (= '%s'): %s"),
746 : : pkg_name(pkg, pnaw_nonambig), in,
747 : 0 : result->buf, strerror(errno));
748 : 0 : return -1;
749 [ # # ]: 0 : } else if (r != stab.st_size) {
750 : 0 : warning(_("symbolic link '%.250s' size has "
751 : : "changed from %jd to %zd"),
752 : 0 : result->buf, (intmax_t)stab.st_size, r);
753 : : /* If the returned size is smaller, let's
754 : : * proceed, otherwise error out. */
755 [ # # ]: 0 : if (r > stab.st_size)
756 : 0 : return -1;
757 : : }
758 : 0 : varbuf_trunc(&target, r);
759 : 0 : varbuf_end_str(&target);
760 : :
761 : 0 : debug(dbg_conffdetail,
762 : : "conffderef readlink gave %zd, '%s'",
763 : : r, target.buf);
764 : :
765 [ # # ]: 0 : if (target.buf[0] == '/') {
766 : 0 : varbuf_reset(result);
767 : 0 : varbuf_add_str(result, dpkg_fsys_get_dir());
768 : 0 : debug(dbg_conffdetail,
769 : : "conffderef readlink absolute");
770 : : } else {
771 [ # # # # ]: 0 : for (r = result->used - 1; r > 0 && result->buf[r] != '/'; r--)
772 : : ;
773 [ # # ]: 0 : if (r < 0) {
774 : 0 : warning(_("%s: conffile '%.250s' resolves to degenerate filename\n"
775 : : " ('%s' is a symlink to '%s')"),
776 : : pkg_name(pkg, pnaw_nonambig),
777 : : in, result->buf, target.buf);
778 : 0 : return -1;
779 : : }
780 [ # # ]: 0 : if (result->buf[r] == '/')
781 : 0 : r++;
782 : 0 : varbuf_trunc(result, r);
783 : 0 : debug(dbg_conffdetail,
784 : : "conffderef readlink relative to '%.*s'",
785 : 0 : (int)result->used, result->buf);
786 : : }
787 : 0 : varbuf_add_buf(result, target.buf, target.used);
788 : 0 : varbuf_end_str(result);
789 : : } else {
790 : 0 : warning(_("%s: conffile '%.250s' is not a plain file or symlink (= '%s')"),
791 : : pkg_name(pkg, pnaw_nonambig), in, result->buf);
792 : 0 : return -1;
793 : : }
794 : : }
795 : : }
796 : :
797 : : /**
798 : : * Generate a file contents MD5 hash.
799 : : *
800 : : * The caller is responsible for providing a buffer for the hash result
801 : : * at least MD5HASHLEN + 1 characters long.
802 : : *
803 : : * @param[in] pkg The package to act on.
804 : : * @param[out] hashbuf The buffer to store the generated hash.
805 : : * @param[in] fn The filename.
806 : : */
807 : : void
808 : 0 : md5hash(struct pkginfo *pkg, char *hashbuf, const char *fn)
809 : : {
810 : : struct dpkg_error err;
811 : : static int fd;
812 : :
813 : 0 : fd = open(fn, O_RDONLY);
814 : :
815 [ # # ]: 0 : if (fd >= 0) {
816 : 0 : push_cleanup(cu_closefd, ehflag_bombout, 1, &fd);
817 [ # # ]: 0 : if (fd_md5(fd, hashbuf, -1, &err) < 0)
818 : 0 : ohshit(_("cannot compute MD5 digest for file '%s': %s"),
819 : : fn, err.str);
820 : 0 : pop_cleanup(ehflag_normaltidy); /* fd = open(cdr.buf) */
821 : 0 : close(fd);
822 [ # # ]: 0 : } else if (errno == ENOENT) {
823 : 0 : strcpy(hashbuf, NONEXISTENTFLAG);
824 : : } else {
825 : 0 : warning(_("%s: unable to open %s to compute its digest: %s"),
826 : 0 : pkg_name(pkg, pnaw_nonambig), fn, strerror(errno));
827 : 0 : strcpy(hashbuf, EMPTYHASHFLAG);
828 : : }
829 : 0 : }
|