ArcCheck.pm revision 1.12
1# ex:ts=8 sw=4:
2# $OpenBSD: ArcCheck.pm,v 1.12 2009/04/22 20:46:21 naddy 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#
24
25package OpenBSD::Ustar::Object;
26
27# match archive header name against PackingElement item
28sub check_name
29{
30	my ($self, $item) = @_;
31	return 1 if $self->name eq $item->name;
32	if ($self->name =~ m/^LongName\d+$/o) {
33		$self->set_name($item->name);
34		return 1;
35	}
36	return 0;
37}
38
39# match archive header link name against actual link names
40sub check_linkname
41{
42	my ($self, $linkname) = @_;
43	my $c = $self->{linkname};
44	if ($self->isHardLink && defined $self->{cwd}) {
45		$c = $self->{cwd}.'/'.$c;
46	}
47	return 1 if $c eq $linkname;
48	if ($self->{linkname} =~ m/^Long(?:Link|Name)\d+$/o) {
49		$self->{linkname} = $linkname;
50		if ($self->isHardLink && defined $self->{cwd}) {
51			$self->{linkname} =~ s|^$self->{cwd}/||;
52		}
53		return 1;
54	}
55	return 0;
56}
57
58use POSIX;
59
60sub verify_modes
61{
62	my ($o, $item) = @_;
63	my $result = 1;
64
65	if (!defined $item->{owner} && !$o->isSymLink) {
66	    if ($o->{uname} ne 'root' && $o->{uname} ne 'bin') {
67		    print STDERR "Error: no \@owner for ",
68			$item->fullname, " (", $o->{uname}, ")\n";
69	    		$result = 0;
70	    }
71	}
72	if (!defined $item->{group} && !$o->isSymLink) {
73	    if ($o->{gname} ne 'bin' && $o->{gname} ne 'wheel') {
74		if (($o->{mode} & (S_ISUID | S_ISGID | S_IWGRP)) != 0) {
75		    print STDERR "Error: no \@group for ",
76			$item->fullname, " (", $o->{uname},
77			"), which has mode ",
78			sprintf("%4o", $o->{mode} & (S_IRWXU | S_IRWXG | S_IRWXO | S_ISUID | S_ISGID)), "\n";
79	    		$result = 0;
80		} else {
81		    print STDERR "Warning: no \@group for ",
82			$item->fullname, " (", $o->{gname}, ")\n";
83	    	}
84	    }
85	}
86	if (!defined $item->{mode} && $o->isFile) {
87	    if (($o->{mode} & (S_ISUID | S_ISGID | S_IWOTH)) != 0) {
88		    print STDERR "Error: weird mode for ",
89			$item->fullname, ": ",
90			sprintf("%4o", $o->{mode} & (S_IRWXU | S_IRWXG | S_IRWXO | S_ISUID | S_ISGID)), "\n";
91	    		$result = 0;
92	    }
93	}
94	return $result;
95}
96
97# copy long items, avoiding duplicate long names.
98sub copy_long
99{
100	my ($self, $wrarc) = @_;
101	if ($self->name =~ m/^LongName(\d+)$/o) {
102		$wrarc->{name_index} = $1 + 1;
103	}
104	if (length($self->name) > MAXFILENAME+MAXPREFIX+1) {
105		$wrarc->{name_index} = 0 if !defined $wrarc->{name_index};
106		$self->set_name('LongName'.$wrarc->{name_index}++);
107	}
108	$self->copy($wrarc);
109}
110
111package OpenBSD::Ustar;
112
113# prepare item and introduce long names where needed.
114sub prepare_long
115{
116	my ($self, $item) = @_;
117	my $filename = $item->name;
118	my $entry = $self->prepare($filename);
119	if (!defined $entry->{uname}) {
120		die "No user name for ", $entry->name, " (uid ", $entry->{uid}, ")";
121	}
122	if (!defined $entry->{gname}) {
123		die "No group name for ", $entry->name, " (gid ", $entry->{gid}. ")";
124	}
125	my ($prefix, $name) = split_name($entry->name);
126	if (length($name) > MAXFILENAME || length($prefix) > MAXPREFIX) {
127		$self->{name_index} = 0 if !defined $self->{name_index};
128		$entry->set_name('LongName'.$self->{name_index}++);
129	}
130	if (length($entry->{linkname}) > MAXLINKNAME) {
131		$self->{linkname_index} = 0 if !defined $self->{linkname_index};
132		$entry->{linkname} = 'LongLink'.$self->{linkname_index}++;
133	}
134	return $entry;
135}
136
1371;
138