1# ex:ts=8 sw=4: 2# $OpenBSD: IdCache.pm,v 1.12 2023/06/13 09:07:17 espie Exp $ 3# 4# Copyright (c) 2002-2005 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 17use v5.36; 18 19package OpenBSD::SimpleIdCache; 20sub new($class) 21{ 22 bless {}, $class; 23} 24 25sub lookup($self, $name, $default = undef) 26{ 27 my $r; 28 29 if (defined $self->{$name}) { 30 $r = $self->{$name}; 31 } else { 32 $r = $self->_convert($name); 33 if (!defined $r) { 34 $r = $default; 35 } 36 $self->{$name} = $r; 37 } 38 return $r; 39} 40 41 42package OpenBSD::IdCache; 43our @ISA=qw(OpenBSD::SimpleIdCache); 44 45sub lookup($self, $name, $default = undef) 46{ 47 if ($name =~ m/^\d+$/o) { 48 return $name; 49 } else { 50 return $self->SUPER::lookup($name, $default); 51 } 52} 53 54package OpenBSD::UidCache; 55our @ISA=qw(OpenBSD::IdCache); 56 57sub _convert($, $key) 58{ 59 my @entry = getpwnam($key); 60 return @entry == 0 ? undef : $entry[2]; 61} 62 63package OpenBSD::GidCache; 64our @ISA=qw(OpenBSD::IdCache); 65 66sub _convert($, $key) 67{ 68 my @entry = getgrnam($key); 69 return @entry == 0 ? undef : $entry[2]; 70} 71 72package OpenBSD::UnameCache; 73our @ISA=qw(OpenBSD::SimpleIdCache); 74 75sub _convert($, $key) 76{ 77 return getpwuid($key); 78} 79 80package OpenBSD::GnameCache; 81our @ISA=qw(OpenBSD::SimpleIdCache); 82 83sub _convert($, $key) 84{ 85 return getgrgid($key); 86} 87 881; 89