move the user handling code to a separate file.
remove "run_as" for open, we can do things directly. More explicit code on open.
This commit is contained in:
parent
8c89d7a3e4
commit
e2f35e7d02
@ -1,5 +1,5 @@
|
||||
# ex:ts=8 sw=4:
|
||||
# $OpenBSD: Affinity.pm,v 1.13 2015/05/01 19:42:54 espie Exp $
|
||||
# $OpenBSD: Affinity.pm,v 1.14 2015/05/02 09:44:40 espie Exp $
|
||||
#
|
||||
# Copyright (c) 2012-2013 Marc Espie <espie@openbsd.org>
|
||||
#
|
||||
@ -23,7 +23,7 @@ use warnings;
|
||||
|
||||
# note that this is only superficially similar to locks
|
||||
|
||||
use DPB::Config;
|
||||
use DPB::User;
|
||||
package DPB::Affinity;
|
||||
our @ISA = (qw(DPB::UserProxy));
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
# ex:ts=8 sw=4:
|
||||
# $OpenBSD: Config.pm,v 1.41 2015/05/01 19:42:54 espie Exp $
|
||||
# $OpenBSD: Config.pm,v 1.42 2015/05/02 09:44:40 espie Exp $
|
||||
#
|
||||
# Copyright (c) 2010-2013 Marc Espie <espie@openbsd.org>
|
||||
#
|
||||
@ -22,6 +22,8 @@ use warnings;
|
||||
# config file.
|
||||
|
||||
package DPB::Config;
|
||||
use DPB::User;
|
||||
|
||||
sub parse_command_line
|
||||
{
|
||||
my ($class, $state) = @_;
|
||||
@ -72,12 +74,12 @@ sub parse_command_line
|
||||
}
|
||||
}
|
||||
$state->{chroot} = $state->opt('B');
|
||||
$state->{base_user} = DPB::Id->from_uid($<);
|
||||
$state->{base_user} = DPB::User->from_uid($<);
|
||||
if (!defined $state->{base_user}) {
|
||||
$state->usage("Can't figure out who I am");
|
||||
}
|
||||
if ($state->defines('BUILD_USER')) {
|
||||
$state->{build_user} = DPB::Id->new($state->defines('BUILD_USER'));
|
||||
$state->{build_user} = DPB::User->new($state->defines('BUILD_USER'));
|
||||
}
|
||||
|
||||
($state->{ports}, $state->{localarch},
|
||||
@ -363,86 +365,4 @@ sub parse_hosts_file
|
||||
}
|
||||
}
|
||||
|
||||
package DPB::Id;
|
||||
|
||||
sub from_uid
|
||||
{
|
||||
my ($class, $u) = @_;
|
||||
if (my ($l, undef, $uid, $gid) = getpwuid $u) {
|
||||
bless { user => $l, uid => $uid, gid => $gid }, $class;
|
||||
} else {
|
||||
return undef;
|
||||
}
|
||||
}
|
||||
|
||||
sub new
|
||||
{
|
||||
my ($class, $u) = @_;
|
||||
# XXX getpwnam for local access, distant access is different
|
||||
if (my ($l, undef, $uid, $gid) = getpwnam $u) {
|
||||
bless { user => $l, uid => $uid, gid => $gid }, $class;
|
||||
} else {
|
||||
bless { user => $u}, $class;
|
||||
}
|
||||
}
|
||||
|
||||
sub user
|
||||
{
|
||||
my $self = shift;
|
||||
return $self->{user};
|
||||
}
|
||||
|
||||
sub run_as
|
||||
{
|
||||
my ($self, $code) = @_;
|
||||
local ($>, $)) = ($self->{uid}, $self->{gid});
|
||||
&$code;
|
||||
}
|
||||
|
||||
sub make_path
|
||||
{
|
||||
my ($self, @directories) = @_;
|
||||
require File::Path;
|
||||
my $p = {};
|
||||
if ($self->{uid}) {
|
||||
$p->{uid} = $self->{uid};
|
||||
} else {
|
||||
$p->{owner} = $self->{user};
|
||||
}
|
||||
if ($self->{gid}) {
|
||||
$p->{gid} = $self->{gid};
|
||||
}
|
||||
File::Path::make_path(@directories, $p);
|
||||
}
|
||||
|
||||
sub open
|
||||
{
|
||||
my ($self, $mode, $filename) = @_;
|
||||
my $fh;
|
||||
$self->run_as(
|
||||
sub {
|
||||
open $fh, $mode, $filename;
|
||||
});
|
||||
return $fh;
|
||||
}
|
||||
|
||||
package DPB::UserProxy;
|
||||
sub run_as
|
||||
{
|
||||
my ($self, $code) = @_;
|
||||
$self->{user}->run_as($code);
|
||||
}
|
||||
|
||||
sub make_path
|
||||
{
|
||||
my ($self, @dirs) = @_;
|
||||
$self->{user}->make_path(@dirs);
|
||||
}
|
||||
|
||||
sub open
|
||||
{
|
||||
my ($self, @parms) = @_;
|
||||
return $self->{user}->open(@parms);
|
||||
}
|
||||
|
||||
1;
|
||||
|
@ -1,5 +1,5 @@
|
||||
# ex:ts=8 sw=4:
|
||||
# $OpenBSD: Fetch.pm,v 1.66 2015/04/30 21:43:28 espie Exp $
|
||||
# $OpenBSD: Fetch.pm,v 1.67 2015/05/02 09:44:40 espie Exp $
|
||||
#
|
||||
# Copyright (c) 2010-2013 Marc Espie <espie@openbsd.org>
|
||||
#
|
||||
@ -19,6 +19,7 @@ use warnings;
|
||||
use DPB::Clock;
|
||||
use DPB::Distfile;
|
||||
use OpenBSD::md5;
|
||||
use DPB::User;
|
||||
|
||||
# handles fetch information, if required
|
||||
package DPB::Fetch;
|
||||
@ -40,7 +41,7 @@ sub new
|
||||
$o->{cdrom_only} = 1;
|
||||
}
|
||||
my $fh = $o->open('<', "$distdir/distinfo");
|
||||
if ($fh) {
|
||||
if (defined $fh) {
|
||||
print "Reading distinfo...";
|
||||
while (<$fh>) {
|
||||
if (m/^SHA256\s*\((.*)\) \= (.*)/) {
|
||||
@ -56,7 +57,7 @@ sub new
|
||||
# e.g., keep only most recent checksum seen
|
||||
$o->make_path($distdir);
|
||||
$fh = $o->open('>', "$distdir/distinfo.new");
|
||||
if ($fh) {
|
||||
if (defined $fh) {
|
||||
for my $k (sort keys %{$o->{sha}}) {
|
||||
print $fh "SHA256 ($k) = ", $o->{sha}{$k}->stringize,
|
||||
"\n";
|
||||
|
@ -1,5 +1,5 @@
|
||||
# ex:ts=8 sw=4:
|
||||
# $OpenBSD: HostProperties.pm,v 1.6 2015/05/01 19:42:54 espie Exp $
|
||||
# $OpenBSD: HostProperties.pm,v 1.7 2015/05/02 09:44:40 espie Exp $
|
||||
#
|
||||
# Copyright (c) 2010-2013 Marc Espie <espie@openbsd.org>
|
||||
#
|
||||
@ -19,6 +19,7 @@ use strict;
|
||||
use warnings;
|
||||
|
||||
package DPB::HostProperties;
|
||||
use DPB::User;
|
||||
|
||||
my $has_sf = 0;
|
||||
my $has_mem = 0;
|
||||
@ -60,7 +61,7 @@ sub set_user
|
||||
my $mode = $tag."_dirmode";
|
||||
if (defined $prop->{$user}) {
|
||||
$prop->{$user} =
|
||||
DPB::Id->new($prop->{$user});
|
||||
DPB::User->new($prop->{$user});
|
||||
} else {
|
||||
$prop->{$user} = $prop->{$default."_user"};
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
# ex:ts=8 sw=4:
|
||||
# $OpenBSD: Locks.pm,v 1.31 2015/05/01 19:42:54 espie Exp $
|
||||
# $OpenBSD: Locks.pm,v 1.32 2015/05/02 09:44:40 espie Exp $
|
||||
#
|
||||
# Copyright (c) 2010-2013 Marc Espie <espie@openbsd.org>
|
||||
#
|
||||
@ -17,7 +17,7 @@
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
use DPB::Config;
|
||||
use DPB::User;
|
||||
|
||||
package DPB::Locks;
|
||||
our @ISA = (qw(DPB::UserProxy));
|
||||
|
@ -1,5 +1,5 @@
|
||||
# ex:ts=8 sw=4:
|
||||
# $OpenBSD: Logger.pm,v 1.18 2015/05/01 20:49:46 espie Exp $
|
||||
# $OpenBSD: Logger.pm,v 1.19 2015/05/02 09:44:40 espie Exp $
|
||||
#
|
||||
# Copyright (c) 2010-2013 Marc Espie <espie@openbsd.org>
|
||||
#
|
||||
@ -17,7 +17,7 @@
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
use DPB::Config;
|
||||
use DPB::User;
|
||||
|
||||
package DPB::Logger;
|
||||
our @ISA = (qw(DPB::UserProxy));
|
||||
|
105
infrastructure/lib/DPB/User.pm
Normal file
105
infrastructure/lib/DPB/User.pm
Normal file
@ -0,0 +1,105 @@
|
||||
# ex:ts=8 sw=4:
|
||||
# $OpenBSD: User.pm,v 1.1 2015/05/02 09:44:40 espie Exp $
|
||||
#
|
||||
# Copyright (c) 2010-2013 Marc Espie <espie@openbsd.org>
|
||||
#
|
||||
# Permission to use, copy, modify, and distribute this software for any
|
||||
# purpose with or without fee is hereby granted, provided that the above
|
||||
# copyright notice and this permission notice appear in all copies.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
# handling user personalities
|
||||
|
||||
package DPB::User;
|
||||
|
||||
sub from_uid
|
||||
{
|
||||
my ($class, $u) = @_;
|
||||
if (my ($l, undef, $uid, $gid) = getpwuid $u) {
|
||||
bless { user => $l, uid => $uid, gid => $gid }, $class;
|
||||
} else {
|
||||
return undef;
|
||||
}
|
||||
}
|
||||
|
||||
sub new
|
||||
{
|
||||
my ($class, $u) = @_;
|
||||
# XXX getpwnam for local access, distant access is different
|
||||
if (my ($l, undef, $uid, $gid) = getpwnam $u) {
|
||||
bless { user => $l, uid => $uid, gid => $gid }, $class;
|
||||
} else {
|
||||
bless { user => $u}, $class;
|
||||
}
|
||||
}
|
||||
|
||||
sub user
|
||||
{
|
||||
my $self = shift;
|
||||
return $self->{user};
|
||||
}
|
||||
|
||||
sub run_as
|
||||
{
|
||||
my ($self, $code) = @_;
|
||||
local ($>, $)) = ($self->{uid}, $self->{gid});
|
||||
&$code;
|
||||
}
|
||||
|
||||
sub make_path
|
||||
{
|
||||
my ($self, @directories) = @_;
|
||||
require File::Path;
|
||||
my $p = {};
|
||||
if ($self->{uid}) {
|
||||
$p->{uid} = $self->{uid};
|
||||
} else {
|
||||
$p->{owner} = $self->{user};
|
||||
}
|
||||
if ($self->{gid}) {
|
||||
$p->{gid} = $self->{gid};
|
||||
}
|
||||
File::Path::make_path(@directories, $p);
|
||||
}
|
||||
|
||||
sub open
|
||||
{
|
||||
my ($self, $mode, $filename) = @_;
|
||||
local ($>, $)) = ($self->{uid}, $self->{gid});
|
||||
if (open(my $fh, $mode, $filename)) {
|
||||
return $fh;
|
||||
} else {
|
||||
return undef;
|
||||
}
|
||||
}
|
||||
|
||||
package DPB::UserProxy;
|
||||
sub run_as
|
||||
{
|
||||
my ($self, $code) = @_;
|
||||
$self->{user}->run_as($code);
|
||||
}
|
||||
|
||||
sub make_path
|
||||
{
|
||||
my ($self, @dirs) = @_;
|
||||
$self->{user}->make_path(@dirs);
|
||||
}
|
||||
|
||||
sub open
|
||||
{
|
||||
my ($self, @parms) = @_;
|
||||
return $self->{user}->open(@parms);
|
||||
}
|
||||
|
||||
1;
|
Loading…
x
Reference in New Issue
Block a user