From 8d7a4daeb33dffa65969eb39fae817c5ddd2d2fb Mon Sep 17 00:00:00 2001 From: espie Date: Wed, 7 Feb 2018 11:22:39 +0000 Subject: [PATCH] prepare for session saving by separating the pkgfetch code. --- usr.sbin/pkg_add/OpenBSD/PackageRepository.pm | 46 +++++++++++++------ 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/usr.sbin/pkg_add/OpenBSD/PackageRepository.pm b/usr.sbin/pkg_add/OpenBSD/PackageRepository.pm index 86b98240354..e85889778dd 100644 --- a/usr.sbin/pkg_add/OpenBSD/PackageRepository.pm +++ b/usr.sbin/pkg_add/OpenBSD/PackageRepository.pm @@ -1,5 +1,5 @@ # ex:ts=8 sw=4: -# $OpenBSD: PackageRepository.pm,v 1.153 2018/02/06 16:34:35 espie Exp $ +# $OpenBSD: PackageRepository.pm,v 1.154 2018/02/07 11:22:39 espie Exp $ # # Copyright (c) 2003-2010 Marc Espie # @@ -743,23 +743,43 @@ our @ISA=qw(OpenBSD::PackageRepository::Distant); our %distant = (); -sub drop_privileges_and_setup_env +my ($fetch_uid, $fetch_gid, $fetch_user); + +sub fill_up_fetch_data { my $self = shift; - my $user = '_pkgfetch'; if ($< == 0) { - # we can't cache anything, we happen after the fork, - # right before exec - if (my (undef, undef, $uid, $gid) = getpwnam($user)) { - $( = $gid; - $) = "$gid $gid"; - $< = $uid; - $> = $uid; - } else { - $self->{state}->fatal("Couldn't change identity: can't find #1 user", $user); + $fetch_user = '_pkgfetch'; + unless ((undef, undef, $fetch_uid, $fetch_gid) = + getpwnam($fetch_user)) { + $self->{state}->fatal( + "Couldn't change identity: can't find #1 user", + $fetch_user); } } else { - ($user) = getpwuid($<); + ($fetch_user) = getpwuid($<); + } +} + +sub fetch_id +{ + my $self = shift; + if (!defined $fetch_user) { + $self->fill_up_fetch_data; + } + return ($fetch_uid, $fetch_gid, $fetch_user); +} + +sub drop_privileges_and_setup_env +{ + my $self = shift; + my ($uid, $gid, $user) = $self->fetch_id; + if (defined $uid) { + # we happen right before exec, so change id permanently + $( = $gid; + $) = "$gid $gid"; + $< = $uid; + $> = $uid; } # create sanitized env for ftp my %newenv = ( -- 2.20.1