ArcCheck.pm revision 1.26
1# ex:ts=8 sw=4: 2# $OpenBSD: ArcCheck.pm,v 1.26 2014/08/10 10:01:03 espie Exp $ 3# 4# Copyright (c) 2005-2006 Marc Espie <espie@openbsd.org> 5# 6# Permission to use, copy, modify, and distribute this software for any 7# purpose with or without fee is hereby granted, provided that the above 8# copyright notice and this permission notice appear in all copies. 9# 10# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 18# Supplementary code to handle archives in the package context. 19# Contrarily to GNU-tar, we do not change the archive format, but by 20# convention, the names LongName\d+ and LongLink\d correspond to names 21# too long to fit. The actual names reside in the PLIST, but the archive 22# is still a valid archive. 23 24use strict; 25use warnings; 26 27use OpenBSD::Ustar; 28 29package OpenBSD::Ustar::Object; 30 31# match archive header name against PackingElement item 32sub check_name 33{ 34 my ($self, $item) = @_; 35 return $self->name eq $item->name; 36} 37 38# match archive header link name against actual link names 39sub check_linkname 40{ 41 my ($self, $linkname) = @_; 42 my $c = $self->{linkname}; 43 if ($self->isHardLink && defined $self->{cwd}) { 44 $c = $self->{cwd}.'/'.$c; 45 } 46 return $c eq $linkname; 47} 48 49use POSIX; 50 51sub verify_modes 52{ 53 my ($o, $item) = @_; 54 my $result = 1; 55 56 if (!defined $item->{owner} && !$o->isSymLink) { 57 if ($o->{uname} ne 'root' && $o->{uname} ne 'bin') { 58 $o->errsay("Error: no \@owner for #1 (#2)", 59 $item->fullname, $o->{uname}); 60 $result = 0; 61 } 62 } 63 if (!defined $item->{group} && !$o->isSymLink) { 64 if ($o->{gname} ne 'bin' && $o->{gname} ne 'wheel') { 65 if (($o->{mode} & (S_ISUID | S_ISGID | S_IWGRP)) != 0) { 66 $o->errsay("Error: no \@group for #1 (#2), which has mode #3", 67 $item->fullname, $o->{uname}, 68 sprintf("%4o", $o->{mode} & (S_IRWXU | S_IRWXG | S_IRWXO | S_ISUID | S_ISGID))); 69 $result = 0; 70 } else { 71 $o->errsay("Warning: no \@group for #1 (#2)", 72 $item->fullname, $o->{gname}); 73 } 74 } 75 } 76 if (!defined $item->{mode} && $o->isFile) { 77 if (($o->{mode} & (S_ISUID | S_ISGID | S_IWOTH)) != 0 || 78 ($o->{mode} & S_IROTH) == 0 || ($o->{mode} & S_IRGRP) == 0) { 79 $o->errsay("Error: weird mode for #1: #2", 80 $item->fullname, 81 sprintf("%4o", $o->{mode} & (S_IRWXU | S_IRWXG | S_IRWXO | S_ISUID | S_ISGID))); 82 $result = 0; 83 } 84 } 85 return $result; 86} 87 88package OpenBSD::Ustar; 89use POSIX; 90 91# prepare item and introduce long names where needed. 92sub prepare_long 93{ 94 my ($self, $item) = @_; 95 my $entry; 96 if (defined $item->{wtempname}) { 97 $entry = $self->prepare($item->{wtempname}, ''); 98 } else { 99 $entry = $self->prepare($item->name); 100 } 101 if ($< && $entry->{uid} == $<) { 102 $entry->{uname} = $item->{owner} // "root"; 103 delete $entry->{uid}; 104 } 105 if ($( && $entry->{gid} == $() { 106 $entry->{gname} = $item->{group} // "bin"; 107 delete $entry->{gid}; 108 } 109 $entry->recheck_owner; 110 if (!defined $entry->{uname}) { 111 $self->fatal("No user name for #1 (uid #2)", 112 $item->name, $entry->{uid}); 113 } 114 if (!defined $entry->{gname}) { 115 $self->fatal("No group name for #1 (uid #2)", 116 $item->name, $entry->{gid}); 117 } 118 # disallow writable files/dirs without explicit annotation 119 if (!defined $item->{mode}) { 120 $entry->{mode} &= ~(S_IWUSR|S_IWGRP|S_IWOTH); 121 } 122 # if we're going to set the group or owner, sguid bits won't 123 # survive the extraction 124 if (defined $item->{group} || defined $item->{owner}) { 125 $entry->{mode} &= ~(S_ISUID|S_ISGID); 126 } 127 # likewise, we skip links on extractions, so hey, don't even care 128 # about modes and stuff. 129 if ($entry->isSymLink) { 130 $entry->{mode} = 0777; 131 $entry->{uid} = 0; 132 $entry->{gid} = 0; 133 $entry->{uname} = 'root'; 134 $entry->{gname} = 'wheel'; 135 } 136 137 $entry->set_name($item->name); 138 return $entry; 139} 140 1411; 142