2 * dpkg - main program for package management
3 * force.c - force operation support
5 * Copyright © 1994,1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
6 * Copyright © 2006-2019 Guillem Jover <guillem@debian.org>
8 * This is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
32 #include <dpkg/macros.h>
33 #include <dpkg/i18n.h>
34 #include <dpkg/dpkg.h>
35 #include <dpkg/dpkg-db.h>
36 #include <dpkg/options.h>
40 static int force_mask
;
41 static int force_flags
;
50 forcetype_str(enum forcetype type
)
53 case FORCETYPE_DISABLED
:
55 case FORCETYPE_ENABLED
:
57 case FORCETYPE_DAMAGE
:
60 internerr("unknown force type '%d'", type
);
64 static const struct forceinfo
{
74 N_("Set all force options"),
79 N_("Use MAC based security if available"),
84 N_("Replace a package with a lower version"),
89 N_("Configure any package which may help this one"),
94 N_("Install or remove incidental packages even when on hold"),
99 N_("Try to (de)install things even when not root"),
104 N_("PATH is missing important programs, problems likely"),
109 N_("Install a package even if it fails authenticity check"),
114 N_("Process even packages with wrong versions"),
117 FORCE_STATOVERRIDE_ADD
,
119 N_("Overwrite an existing stat override when adding it"),
121 "statoverride-remove",
122 FORCE_STATOVERRIDE_DEL
,
124 N_("Ignore a missing stat override when removing it"),
129 N_("Overwrite a file from one package with another"),
131 "overwrite-diverted",
132 FORCE_OVERWRITE_DIVERTED
,
134 N_("Overwrite a diverted file with an undiverted version"),
139 N_("Overwrite one package's directory with another's file"),
144 N_("Do not perform safe I/O operations when unpacking"),
147 FORCE_SCRIPT_CHROOTLESS
,
149 N_("Do not chroot into maintainer script environment"),
154 N_("Always use the new config files, don't prompt"),
159 N_("Always use the old config files, don't prompt"),
164 N_("Use the default option for new config files if one\n"
165 "is available, don't prompt. If no default can be found,\n"
166 "you will be prompted unless one of the confold or\n"
167 "confnew options is also given"),
172 N_("Always install missing config files"),
177 N_("Offer to replace config files with no new versions"),
182 N_("Process even packages with wrong or no architecture"),
187 N_("Install even if it would break another package"),
192 N_("Allow installation of conflicting packages"),
197 N_("Turn all dependency problems into warnings"),
200 FORCE_DEPENDS_VERSION
,
202 N_("Turn dependency version problems into warnings"),
205 FORCE_REMOVE_REINSTREQ
,
207 N_("Remove packages which require installation"),
210 FORCE_REMOVE_PROTECTED
,
212 N_("Remove a protected package"),
215 FORCE_REMOVE_ESSENTIAL
,
217 N_("Remove an essential package"),
226 return (flags
& force_flags
) == flags
;
232 force_flags
|= flags
;
236 reset_force(int flags
)
238 force_flags
&= ~flags
;
242 get_force_string(void)
244 const struct forceinfo
*fip
;
245 struct varbuf vb
= VARBUF_INIT
;
247 for (fip
= forceinfos
; fip
->name
; fip
++) {
248 if ((enum force_flags
)fip
->flag
== FORCE_ALL
||
249 (fip
->flag
& force_mask
) != fip
->flag
||
250 !in_force(fip
->flag
))
254 varbuf_add_char(&vb
, ',');
255 varbuf_add_str(&vb
, fip
->name
);
259 return varbuf_detach(&vb
);
263 print_forceinfo_line(int type
, const char *name
, const char *desc
)
265 printf(" %s %-18s %s\n", forcetype_str(type
), name
, desc
);
269 print_forceinfo(const struct forceinfo
*fi
)
273 desc
= m_strdup(gettext(fi
->desc
));
275 line
= strtok(desc
, "\n");
276 print_forceinfo_line(fi
->type
, fi
->name
, line
);
277 while ((line
= strtok(NULL
, "\n")))
278 print_forceinfo_line(FORCETYPE_DISABLED
, "", line
);
284 parse_force(const char *value
, bool set
)
286 const struct forceinfo
*fip
;
288 if (strcmp(value
, "help") == 0) {
289 char *force_string
= get_force_string();
292 "%s forcing options - control behavior when problems found:\n"
293 " warn but continue: --force-<thing>,<thing>,...\n"
294 " stop with error: --refuse-<thing>,<thing>,... | --no-force-<thing>,...\n"
295 " Forcing things:\n"), dpkg_get_progname());
297 for (fip
= forceinfos
; fip
->name
; fip
++)
298 if ((enum force_flags
)fip
->flag
== FORCE_ALL
||
299 (fip
->flag
& force_mask
) == fip
->flag
)
300 print_forceinfo(fip
);
304 "WARNING - use of options marked [!] can seriously damage your installation.\n"
305 "Forcing options marked [*] are enabled by default.\n"));
306 m_output(stdout
, _("<standard output>"));
310 "Currently enabled options:\n"
311 " %s\n"), force_string
);
322 comma
= strchrnul(value
, ',');
323 l
= (size_t)(comma
- value
);
324 for (fip
= forceinfos
; fip
->name
; fip
++)
325 if (strncmp(fip
->name
, value
, l
) == 0 &&
326 strlen(fip
->name
) == l
)
330 badusage(_("unknown force/refuse option '%.*s'"),
331 (int)min(l
, 250), value
);
332 } else if (fip
->flag
) {
334 set_force(fip
->flag
);
336 reset_force(fip
->flag
);
338 warning(_("obsolete force/refuse option '%s'"),
349 set_force_default(int mask
)
351 const char *force_env
;
352 const struct forceinfo
*fip
;
356 /* If we get passed force options from the environment, do not
357 * initialize from the built-in defaults. */
358 force_env
= getenv("DPKG_FORCE");
359 if (force_env
!= NULL
) {
360 if (force_env
[0] != '\0')
361 parse_force(force_env
, 1);
365 for (fip
= forceinfos
; fip
->name
; fip
++)
366 if (fip
->type
== FORCETYPE_ENABLED
)
367 set_force(fip
->flag
);
371 set_force_option(const struct cmdinfo
*cip
, const char *value
)
373 bool set
= cip
->arg_int
;
375 parse_force(value
, set
);
379 reset_force_option(const struct cmdinfo
*cip
, const char *value
)
381 reset_force(cip
->arg_int
);
385 forcibleerr(int forceflag
, const char *fmt
, ...)
390 if (in_force(forceflag
)) {
391 warning(_("overriding problem because --force enabled:"));
400 forcible_nonroot_error(int rc
)
402 if (in_force(FORCE_NON_ROOT
) && errno
== EPERM
)