pkg_subst 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #! /usr/bin/perl
  2. # ex:ts=8 sw=4:
  3. # $OpenBSD: pkg_subst,v 1.6 2015/11/07 09:52:24 espie Exp $
  4. #
  5. # Copyright (c) 2008 Marc Espie <espie@openbsd.org>
  6. #
  7. # Permission to use, copy, modify, and distribute this software for any
  8. # purpose with or without fee is hereby granted, provided that the above
  9. # copyright notice and this permission notice appear in all copies.
  10. #
  11. # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  12. # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  13. # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  14. # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  15. # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  16. # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  17. # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  18. # rather simple script
  19. use strict;
  20. use warnings;
  21. use OpenBSD::Getopt;
  22. use OpenBSD::Subst;
  23. use OpenBSD::State;
  24. use OpenBSD::IdCache;
  25. sub fatal
  26. {
  27. print STDERR @_, "\n";
  28. exit 1;
  29. }
  30. my @basedirs;
  31. sub ensure_basedir
  32. {
  33. my $file = shift;
  34. for my $b (@basedirs) {
  35. return if $file =~ m/^\Q$b\E\//;
  36. }
  37. # try harder!
  38. require File::Spec;
  39. $file = File::Spec->canonpath(File::Spec->rel2abs($file));
  40. for my $b (@basedirs) {
  41. return if $file =~ m/^\Q$b\E\//;
  42. }
  43. fatal("must use -m to copy $file which is outside ".
  44. join(" ", @basedirs));
  45. }
  46. my $subst = OpenBSD::Subst->new;
  47. our ($opt_c, $opt_i);
  48. my ($fuid, $fgid, $fmode);
  49. my ($uidc, $gidc);
  50. my $ui = OpenBSD::State->new('pkg_subst');
  51. $ui->usage_is( '[-ci] [-Dvar=value ...] [-B basedir] [-g group] [-m mode] [-o owner] [file ...]');
  52. $ui->do_options(
  53. sub {
  54. getopts('B:D:g:m:o:chi',
  55. {'D' =>
  56. sub {
  57. $subst->parse_option(shift);
  58. },
  59. 'o' => sub {
  60. my $owner = shift;
  61. $uidc //= OpenBSD::UidCache->new;
  62. $fuid = $uidc->lookup($owner, -1);
  63. fatal("$owner is not a valid user") if $fuid == -1;
  64. },
  65. 'g' => sub {
  66. my $group = shift;
  67. $gidc //= OpenBSD::GidCache->new;
  68. $fgid = $gidc->lookup($group, -1);
  69. fatal("$group is not a valid group") if $fgid == -1;
  70. },
  71. 'B' => sub {
  72. push(@basedirs, shift);
  73. },
  74. 'm' => sub {
  75. my $mode = shift;
  76. $fmode = oct($mode);
  77. fatal("$mode is not a valid mode") if $fmode == 0;
  78. },
  79. 'h' => sub { $ui->usage; },
  80. });
  81. });
  82. my $bak = $subst->value('BAK');
  83. if (!defined $bak) {
  84. $bak = '.beforesubst';
  85. }
  86. if (@ARGV == 0) {
  87. $subst->copy_fh2(\*STDIN, \*STDOUT);
  88. }
  89. while (my $src = shift) {
  90. my $dest;
  91. if ($opt_c) {
  92. $dest = shift or die "odd number of files";
  93. if (@basedirs > 0 && !defined $fmode) {
  94. ensure_basedir($src);
  95. }
  96. } else {
  97. $dest = $src;
  98. $src .= $bak;
  99. rename($dest, $src) or fatal("Can't rename $dest: $!");
  100. }
  101. my $fh = $subst->copy($src, $dest);
  102. # copy rights, owner, group as well
  103. my ($uid, $gid, $mode) = (stat $src)[4, 5, 2];
  104. my $r1 = chown $fuid // $uid, $fgid // $gid, $fh;
  105. if (defined $fmode) {
  106. $mode = $fmode;
  107. }
  108. my $r2 = chmod $mode & 07777, $fh;
  109. if (defined $fuid || defined $fgid || $< == 0) {
  110. if ($r1 == 0 && !$opt_i) {
  111. fatal("chown on $dest failed");
  112. }
  113. if ($r2 == 0) {
  114. fatal("chmod on $dest failed");
  115. }
  116. }
  117. }