Add a way to make_path with uig/gid.

Add a way to run local jobs as another user.
This commit is contained in:
espie 2015-04-26 17:36:20 +00:00
parent 9a3abc4aca
commit 107eee3773
2 changed files with 46 additions and 6 deletions

View File

@ -1,5 +1,5 @@
# ex:ts=8 sw=4:
# $OpenBSD: Config.pm,v 1.36 2015/04/25 14:24:03 espie Exp $
# $OpenBSD: Config.pm,v 1.37 2015/04/26 17:36:20 espie Exp $
#
# Copyright (c) 2010-2013 Marc Espie <espie@openbsd.org>
#
@ -325,6 +325,7 @@ sub parse_config_files
for my $u (qw(build_user log_user lock_user fetch_user)) {
$state->{$u} //= $prop->{$u};
}
$state->{default_prop} = $prop;
}
sub parse_hosts_file
@ -397,4 +398,36 @@ sub run_as
&$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};
}
if ($self->{dirmode}) {
$p->{mode} = $self->{dirmode};
}
File::Path::make_path(@directories, $p);
}
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);
}
1;

View File

@ -1,5 +1,5 @@
# ex:ts=8 sw=4:
# $OpenBSD: Shell.pm,v 1.3 2015/04/25 10:07:19 espie Exp $
# $OpenBSD: Shell.pm,v 1.4 2015/04/26 17:36:20 espie Exp $
#
# Copyright (c) 2010-2014 Marc Espie <espie@openbsd.org>
#
@ -67,6 +67,13 @@ sub sudo
return $self;
}
sub run_as
{
my ($self, $user) = @_;
$self->{user} = $user;
return $self;
}
sub nochroot
{
my $self = shift;
@ -195,9 +202,10 @@ sub exec
if (defined $chroot) {
chroot($chroot);
}
if (!$self->{sudo} && defined $self->prop->{build_user}) {
setuid($self->prop->{build_user}{uid});
setgid($self->prop->{build_user}{gid});
$self->{user} //= $self->prop->{build_user};
if (!$self->{sudo} && defined $self->{user}) {
setuid($self->{user}{uid});
setgid($self->{user}{gid});
}
if ($self->{env}) {
while (my ($k, $v) = each %{$self->{env}}) {
@ -220,5 +228,4 @@ sub exec
exec {$argv[0]} @argv;
}
1;