diff options
author | Niels Thykier <niels@thykier.net> | 2022-11-20 14:32:38 +0000 |
---|---|---|
committer | Niels Thykier <niels@thykier.net> | 2022-11-20 18:41:48 +0000 |
commit | c90db60a241df81a1865f1e64677f83006407338 (patch) | |
tree | 4f72a93d7cf06e2dbef102dadabf3317b3a71793 | |
parent | f07654a8fd662ef1ec51d72ccda0d5ed92f14078 (diff) |
Dh_Lib.pm: Ensure `install_{file,prog,lib}` uses chown when root should be used
Previously, they would skip the `chown` call and rely on `dh_fixperms`
or `dpkg-deb` to fix the ownership. However, that could cause issues
when the helper was run after `dh_fixperms` and install files that
`dpkg-deb` would not reset ownership of.
Gbp-Dch: full
Signed-off-by: Niels Thykier <niels@thykier.net>
-rw-r--r-- | lib/Debian/Debhelper/Dh_Lib.pm | 26 | ||||
-rw-r--r-- | t/Test/DH.pm | 13 | ||||
-rwxr-xr-x | t/dh_installinit/dh_installinit.t | 2 | ||||
-rwxr-xr-x | t/dh_installman/01-basics.t | 10 | ||||
-rwxr-xr-x | t/dh_installsystemd/dh_installsystemd.t | 24 | ||||
-rwxr-xr-x | t/dh_installsystemd/dh_systemd.t | 12 | ||||
-rwxr-xr-x | t/dh_installsystemduser/dh_installsystemduser.t | 2 |
7 files changed, 51 insertions, 38 deletions
diff --git a/lib/Debian/Debhelper/Dh_Lib.pm b/lib/Debian/Debhelper/Dh_Lib.pm index 10e67f90..7adcf8c0 100644 --- a/lib/Debian/Debhelper/Dh_Lib.pm +++ b/lib/Debian/Debhelper/Dh_Lib.pm @@ -631,40 +631,46 @@ sub error_exitcode { { my $_loaded = 0; sub install_file { - unshift(@_, 0644); + unshift(@_, 1, 0644); goto \&_install_file_to_path; } sub install_prog { - unshift(@_, 0755); + unshift(@_, 1, 0755); goto \&_install_file_to_path; } sub install_lib { - unshift(@_, 0644); + unshift(@_, 1, 0644); goto \&_install_file_to_path; } sub _install_file_to_path { - my ($mode, $source, $dest) = @_; + my ($consider_using_root, $mode, $source, $dest) = @_; if (not $_loaded) { $_loaded++; require File::Copy; } - verbose_print(sprintf('install -p -m%04o %s', $mode, escape_shell($source, $dest))) - if $dh{VERBOSE}; + my $use_root = !$consider_using_root && should_use_root(); + if ($dh{VERBOSE}) { + my $install_opts = $use_root ? '-o 0 -g 0 ' : ''; + verbose_print(sprintf('install -p %s-m%04o %s', $install_opts, $mode, escape_shell($source, $dest))) + } return 1 if $dh{NO_ACT}; # "install -p -mXXXX foo bar" silently discards broken # symlinks to install the file in place. File::Copy does not, # so emulate it manually. (#868204) if ( -l $dest and not -e $dest and not unlink($dest) and $! != ENOENT) { - error("unlink $dest failed: $!"); + error("unlink(\"$dest\") failed: $!"); + } + File::Copy::copy($source, $dest) or error("copy(\"$source\", \"$dest\"): $!"); + chmod($mode, $dest) or error("chmod($mode, \"$dest\"): $!"); + if ($use_root) { + chown(0, 0, $dest) or error("chown(0, 0, \"$dest\") failed: $!"); } - File::Copy::copy($source, $dest) or error("copy($source, $dest): $!"); - chmod($mode, $dest) or error("chmod($mode, $dest): $!"); my (@stat) = stat($source); error("stat($source): $!") if not @stat; utime($stat[8], $stat[9], $dest) - or error(sprintf("utime(%d, %d, %s): $!", $stat[8] , $stat[9], $dest)); + or error(sprintf("utime(%d, %d, \"%s\"): $!", $stat[8] , $stat[9], $dest)); return 1; } } diff --git a/t/Test/DH.pm b/t/Test/DH.pm index 9658d90a..7d85353d 100644 --- a/t/Test/DH.pm +++ b/t/Test/DH.pm @@ -44,7 +44,7 @@ use Debian::Debhelper::Dh_Lib qw(!dirname); our @EXPORT = qw( each_compat_up_to_and_incl_subtest each_compat_subtest each_compat_from_and_above_subtest run_dh_tool - create_empty_file readlines + create_empty_file readlines copy_file error find_script non_deprecated_compat_levels each_compat_from_x_to_and_incl_y_subtest ); @@ -54,6 +54,13 @@ our ($TEST_DH_COMPAT); my $START_DIR = getcwd(); my $TEST_DIR; + +sub copy_file { + my ($src, $dest, $mode) = @_; + $mode //= 0644; + return Debian::Debhelper::Dh_Lib::_install_file_to_path(0, $mode, $src, $dest); +} + sub run_dh_tool { my (@cmd) = @_; my $compat = $TEST_DH_COMPAT; @@ -100,7 +107,7 @@ sub _prepare_test_root { debian/changelog ); for my $file (@files) { - install_file($file, "${dir}/${file}"); + copy_file($file, "${dir}/${file}"); } if (@::TEST_DH_EXTRA_TEMPLATE_FILES) { my $test_dir = ($TEST_DIR //= dirname($0)); @@ -111,7 +118,7 @@ sub _prepare_test_root { my $install_dir = dirname($file); mkdirs($install_dir); } - install_file("${actual_dir}/${file}", "${dir}/${file}"); + copy_file("${actual_dir}/${file}", "${dir}/${file}"); } } } diff --git a/t/dh_installinit/dh_installinit.t b/t/dh_installinit/dh_installinit.t index d290df21..7a7817c3 100755 --- a/t/dh_installinit/dh_installinit.t +++ b/t/dh_installinit/dh_installinit.t @@ -36,7 +36,7 @@ each_compat_from_and_above_subtest(11, sub { ok(run_dh_tool('dh_clean')); make_path(qw(debian/foo/lib/systemd/system/ debian/bar debian/baz)); - install_file('debian/foo.service', 'debian/foo/lib/systemd/system/foo.service'); + copy_file('debian/foo.service', 'debian/foo/lib/systemd/system/foo.service'); ok(run_dh_tool('dh_installinit')); ok(!find_script('foo', 'postinst')); ok(run_dh_tool('dh_clean')); diff --git a/t/dh_installman/01-basics.t b/t/dh_installman/01-basics.t index 05a15ecf..141059a5 100755 --- a/t/dh_installman/01-basics.t +++ b/t/dh_installman/01-basics.t @@ -77,11 +77,11 @@ each_compat_subtest { } mkdirs('debian/debhelper/usr/share/man/man1'); mkdirs('debian/debhelper/usr/share/man/man3'); - install_file('generated-manpages/manpage-uncompressed.1', 'debian/debhelper/usr/share/man/man1/manpage-uncompressed.1'); - install_file('generated-manpages/manpage-compressed.1.gz', 'debian/debhelper/usr/share/man/man1/manpage-compressed.1.gz'); - install_file('generated-manpages/manpage-perl.3perl', 'debian/debhelper/usr/share/man/man3/manpage-perl.3perl'); - install_file('generated-manpages/libmanpage.so.1.2.3.1', 'debian/debhelper/usr/share/man/man1/libmanpage.so.1.2.3.1'); - install_file('libmanpage.so.9.2.3', 'debian/debhelper/usr/share/man/man3/libmanpage.so.9.2.3'); + copy_file('generated-manpages/manpage-uncompressed.1', 'debian/debhelper/usr/share/man/man1/manpage-uncompressed.1'); + copy_file('generated-manpages/manpage-compressed.1.gz', 'debian/debhelper/usr/share/man/man1/manpage-compressed.1.gz'); + copy_file('generated-manpages/manpage-perl.3perl', 'debian/debhelper/usr/share/man/man3/manpage-perl.3perl'); + copy_file('generated-manpages/libmanpage.so.1.2.3.1', 'debian/debhelper/usr/share/man/man1/libmanpage.so.1.2.3.1'); + copy_file('libmanpage.so.9.2.3', 'debian/debhelper/usr/share/man/man3/libmanpage.so.9.2.3'); ok(run_dh_tool('dh_installman')); ok(-e 'debian/debhelper/usr/share/man/man1/manpage-uncompressed.1'); ok(-e 'debian/debhelper/usr/share/man/man1/manpage-compressed.1'); diff --git a/t/dh_installsystemd/dh_installsystemd.t b/t/dh_installsystemd/dh_installsystemd.t index eb3a5305..bbb4eb0d 100755 --- a/t/dh_installsystemd/dh_installsystemd.t +++ b/t/dh_installsystemd/dh_installsystemd.t @@ -106,7 +106,7 @@ each_compat_subtest { ok(run_dh_tool('dh_clean')); make_path('debian/foo/lib/systemd/system/'); - install_file('debian/foo2.service', 'debian/foo/lib/systemd/system/foo2.service'); + copy_file('debian/foo2.service', 'debian/foo/lib/systemd/system/foo2.service'); ok(run_dh_tool('dh_installsystemd')); ok(-e 'debian/foo/lib/systemd/system/foo.service'); ok(find_script('foo', 'postinst')); @@ -119,7 +119,7 @@ each_compat_subtest { # lib -> usr/lib (if we ever support that) make_path('debian/foo/lib/systemd/system/'); - install_file('debian/foo2.service', 'debian/foo/lib/systemd/system/foo2.service'); + copy_file('debian/foo2.service', 'debian/foo/lib/systemd/system/foo2.service'); ok(run_dh_tool('dh_installsystemd')); ok(-e 'debian/foo/lib/systemd/system/foo2.service'); ok(find_script('foo', 'postinst')); @@ -132,8 +132,8 @@ each_compat_subtest { # lib -> usr/lib with both no-empty (if we ever suport that) make_path('debian/foo/lib/systemd/system/'); make_path('debian/foo/lib/systemd/system/'); - install_file('debian/foo2.service', 'debian/foo/lib/systemd/system/foo2.service'); - install_file('debian/foo2.service', 'debian/foo/lib/systemd/system/bar.service'); + copy_file('debian/foo2.service', 'debian/foo/lib/systemd/system/foo2.service'); + copy_file('debian/foo2.service', 'debian/foo/lib/systemd/system/bar.service'); ok(run_dh_tool('dh_installsystemd')); ok(-e 'debian/foo/lib/systemd/system/bar.service'); ok(-e 'debian/foo/lib/systemd/system/foo2.service'); @@ -145,7 +145,7 @@ each_compat_subtest { ok(run_dh_tool('dh_clean')); make_path('debian/foo/lib/systemd/system/'); - install_file('debian/foo2.service', 'debian/foo/lib/systemd/system/foo2.service'); + copy_file('debian/foo2.service', 'debian/foo/lib/systemd/system/foo2.service'); ok(run_dh_tool('dh_installsystemd', '--no-start')); ok(-e 'debian/foo/lib/systemd/system/foo.service'); ok(find_script('foo', 'postinst')); @@ -156,7 +156,7 @@ each_compat_subtest { ok(run_dh_tool('dh_clean')); make_path('debian/foo/lib/systemd/system/'); - install_file('debian/foo2.service', 'debian/foo/lib/systemd/system/foo2.service'); + copy_file('debian/foo2.service', 'debian/foo/lib/systemd/system/foo2.service'); ok(run_dh_tool('dh_installsystemd', '-p', 'foo', '--no-start', 'foo.service')); ok(run_dh_tool('dh_installsystemd', '-p', 'foo', 'foo2.service')); ok(-e 'debian/foo/lib/systemd/system/foo.service'); @@ -168,7 +168,7 @@ each_compat_subtest { ok(run_dh_tool('dh_clean')); make_path('debian/foo/lib/systemd/system/'); - install_file('debian/foo2.service', 'debian/foo/lib/systemd/system/foo2.service'); + copy_file('debian/foo2.service', 'debian/foo/lib/systemd/system/foo2.service'); ok(run_dh_tool('dh_installsystemd', '-p', 'foo', '--no-enable', 'foo.service')); ok(run_dh_tool('dh_installsystemd', '-p', 'foo', 'foo2.service')); ok(-e 'debian/foo/lib/systemd/system/foo.service'); @@ -189,7 +189,7 @@ each_compat_subtest { # Quoting #764730 make_path('debian/foo/lib/systemd/system/'); - install_file('debian/foo.service', 'debian/foo/lib/systemd/system/foo\x2dfuse.service'); + copy_file('debian/foo.service', 'debian/foo/lib/systemd/system/foo\x2dfuse.service'); ok(run_dh_tool('dh_installsystemd')); unit_is_enabled('foo', 'foo\x2dfuse', 1); unit_is_started('foo', 'foo\x2dfuse', 1); @@ -197,7 +197,7 @@ each_compat_subtest { # --name flag #870768 make_path('debian/foo/lib/systemd/system/'); - install_file('debian/foo2.service', 'debian/foo/lib/systemd/system/foo2.service'); + copy_file('debian/foo2.service', 'debian/foo/lib/systemd/system/foo2.service'); ok(run_dh_tool('dh_installsystemd', '--name=foo')); unit_is_enabled('foo', 'foo', 1); unit_is_started('foo', 'foo', 1); @@ -211,7 +211,7 @@ each_compat_subtest { ok(run_dh_tool('dh_clean')); make_path('debian/foo/lib/systemd/system/'); - install_file('debian/foo.service', 'debian/foo/lib/systemd/system/target.service'); + copy_file('debian/foo.service', 'debian/foo/lib/systemd/system/target.service'); make_symlink_raw_target('target.service', 'debian/foo/lib/systemd/system/source.service'); ok(run_dh_tool('dh_installsystemd')); unit_is_enabled('foo', 'foo', 1); @@ -224,7 +224,7 @@ each_compat_subtest { each_compat_up_to_and_incl_subtest(11, sub { make_path('debian/foo/lib/systemd/system/'); make_path('debian/foo/etc/init.d/'); - install_file('debian/foo.service', 'debian/foo/lib/systemd/system/target.service'); + copy_file('debian/foo.service', 'debian/foo/lib/systemd/system/target.service'); make_symlink_raw_target('target.service', 'debian/foo/lib/systemd/system/source.service'); write_file('debian/foo/etc/init.d/source', '# something'); ok(run_dh_tool('dh_installsystemd')); @@ -241,7 +241,7 @@ each_compat_up_to_and_incl_subtest(11, sub { each_compat_from_and_above_subtest(12, sub { make_path('debian/foo/lib/systemd/system/'); make_path('debian/foo/etc/init.d/'); - install_file('debian/foo.service', 'debian/foo/lib/systemd/system/target.service'); + copy_file('debian/foo.service', 'debian/foo/lib/systemd/system/target.service'); make_symlink_raw_target('target.service', 'debian/foo/lib/systemd/system/source.service'); write_file('debian/foo/etc/init.d/source', '# something'); ok(run_dh_tool('dh_installsystemd')); diff --git a/t/dh_installsystemd/dh_systemd.t b/t/dh_installsystemd/dh_systemd.t index 378966b5..c3c94015 100755 --- a/t/dh_installsystemd/dh_systemd.t +++ b/t/dh_installsystemd/dh_systemd.t @@ -54,7 +54,7 @@ each_compat_from_x_to_and_incl_y_subtest(10, 10, sub { ok(run_dh_tool('dh_clean')); make_path('debian/foo/lib/systemd/system/'); - install_file('debian/foo2.service', 'debian/foo/lib/systemd/system/foo2.service'); + copy_file('debian/foo2.service', 'debian/foo/lib/systemd/system/foo2.service'); ok(run_dh_tool('dh_systemd_enable')); ok(run_dh_tool('dh_systemd_start')); ok(-e "debian/foo/lib/systemd/system/foo.service"); @@ -66,7 +66,7 @@ each_compat_from_x_to_and_incl_y_subtest(10, 10, sub { ok(run_dh_tool('dh_clean')); make_path('debian/foo/lib/systemd/system/'); - install_file('debian/foo2.service', 'debian/foo/lib/systemd/system/foo2.service'); + copy_file('debian/foo2.service', 'debian/foo/lib/systemd/system/foo2.service'); ok(run_dh_tool('dh_systemd_enable')); ok(run_dh_tool('dh_systemd_start', '--no-start')); ok(-e "debian/foo/lib/systemd/system/foo.service"); @@ -78,7 +78,7 @@ each_compat_from_x_to_and_incl_y_subtest(10, 10, sub { ok(run_dh_tool('dh_clean')); make_path('debian/foo/lib/systemd/system/'); - install_file('debian/foo2.service', 'debian/foo/lib/systemd/system/foo2.service'); + copy_file('debian/foo2.service', 'debian/foo/lib/systemd/system/foo2.service'); ok(run_dh_tool('dh_systemd_enable')); ok(run_dh_tool('dh_systemd_start', '--no-start', 'debian/foo.service')); ok(run_dh_tool('dh_systemd_start', '-p', 'foo', 'foo2.service')); @@ -91,7 +91,7 @@ each_compat_from_x_to_and_incl_y_subtest(10, 10, sub { ok(run_dh_tool('dh_clean')); make_path('debian/foo/lib/systemd/system/'); - install_file('debian/foo2.service', 'debian/foo/lib/systemd/system/foo2.service'); + copy_file('debian/foo2.service', 'debian/foo/lib/systemd/system/foo2.service'); ok(run_dh_tool('dh_systemd_enable', '--no-enable', 'debian/foo.service')); ok(run_dh_tool('dh_systemd_enable', '-p', 'foo', 'foo2.service')); ok(run_dh_tool('dh_systemd_start')); @@ -104,7 +104,7 @@ each_compat_from_x_to_and_incl_y_subtest(10, 10, sub { ok(run_dh_tool('dh_clean')); make_path('debian/foo/lib/systemd/system/'); - install_file('debian/foo.service', 'debian/foo/lib/systemd/system/foo.service'); + copy_file('debian/foo.service', 'debian/foo/lib/systemd/system/foo.service'); ok(run_dh_tool('dh_systemd_start', '--no-restart-after-upgrade')); my $matches = grep { m{deb-systemd-invoke start .*foo.service} } `cat debian/foo.postinst.debhelper`; ok($matches == 1); @@ -112,7 +112,7 @@ each_compat_from_x_to_and_incl_y_subtest(10, 10, sub { # Quoting #764730 make_path('debian/foo/lib/systemd/system/'); - install_file('debian/foo.service', 'debian/foo/lib/systemd/system/foo\x2dfuse.service'); + copy_file('debian/foo.service', 'debian/foo/lib/systemd/system/foo\x2dfuse.service'); ok(run_dh_tool('dh_systemd_enable')); ok(run_dh_tool('dh_systemd_start')); unit_is_enabled('foo', 'foo\x2dfuse', 1); diff --git a/t/dh_installsystemduser/dh_installsystemduser.t b/t/dh_installsystemduser/dh_installsystemduser.t index c3f7f842..51717280 100755 --- a/t/dh_installsystemduser/dh_installsystemduser.t +++ b/t/dh_installsystemduser/dh_installsystemduser.t @@ -68,7 +68,7 @@ sub isnt_started { _unit_check_user_started(@_, 0); } each_compat_subtest { my ($compat) = @_; make_path('debian/foo/usr/lib/systemd/user/'); - install_file('debian/foo.user.service', 'debian/foo/usr/lib/systemd/user/bar.service'); + copy_file('debian/foo.user.service', 'debian/foo/usr/lib/systemd/user/bar.service'); ok(run_dh_tool('dh_installsystemduser')); ok(-e 'debian/foo/usr/lib/systemd/user/foo.service'); is_enabled('foo', 'foo.service'); |