🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.misc.discuss
Article #75064

login_name, or the "correct" way to determine the login name

#75064
From: David J. Camp
Date: Wed, 14 Nov 1990 07:05
58 lines
3044 bytes
POSIX and IEEE committees are having some difficulty figuring out how
cuserid() should work.  One proposal was to drop the function from the
standard, since they could not agree how it should work.

The following is a perl script using 'pstat' to determine the login
name of the current process.  It does not use /etc/utmp or any
environment variables.  It strictly uses the process table.

The algorithm is simple.  Start with the current process id, and
determine the parent process id.  Repeat until the user id of that
process is root.  The preceeding process was the login process.

I am not sure how it behaves with setuid processes that spawn shells.

It is not intended as a "real" solution.  It is just a demonstration
of the method.  The right solution should be a C program that looks
at the process table directly.  It should be able to determine setuid
situations an handle them as appropriate.

#! /usr/bin/perl
#    This is login_name, a perl script to return the login name of a user.
#    Copyright (C) 1990  David J. Camp
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 1, or (at your option)
#    any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# david@wubios.wustl.edu             ^     Mr. David J. Camp
# david%wubios@wugate.wustl.edu    < * >   +1 314 382 0584
# ...!uunet!wugate!wubios!david      v     "God loves material things."
system "pstat -p > /tmp/pstat$$" ;
open (PSTAT, "/tmp/pstat$$") ;
while (<PSTAT>)
    {
    @FIELDS = split (/[ \t]+/) ;
    $PID = $FIELDS[12] + 0 ;
    $UIDS[$PID] = $FIELDS[6] + 0 ;
    $PPIDS[$PID] = $FIELDS[13] + 0 ;
    }
close (PSTAT) ;
$PID=$$ + 0 ;
while ($UIDS [$PID] != 0)
    {
    $UID = $UIDS[$PID] ;
    $PID = $PPIDS[$PID] ;
    }
($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell) = getpwuid($UID);
print $name , "\n" ;
Technical Details
Message-ID: <9011140705.AA15691@wubios.wustl.edu>
Path: arc1-ca.pugleaf.net!gmdzi!unido!mcsun!uunet!tut.cis.ohio-state.edu!WUBIOS.WUSTL.EDU!david

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 1025b4,4e75b4f219814609,start
X-Google-Attributes: gid1025b4,public
X-Google-ArrivalTime: 1990-11-13 23:05:05 PST
Path: arc1-ca.pugleaf.net!gmdzi!unido!mcsun!uunet!tut.cis.ohio-state.edu!WUBIOS.WUSTL.EDU!david
From: david@WUBIOS.WUSTL.EDU (David J. Camp)
Newsgroups: gnu.misc.discuss
Subject: login_name, or the "correct" way to determine the login name
Message-ID: <9011140705.AA15691@wubios.wustl.edu>
Date: 14 Nov 90 07:05:05 GMT
Sender: karl@tut.cis.ohio-state.edu
Distribution: gnu
Organization: GNUs
Lines: 58
Posted: Wed Nov 14 08:05:05 1990