[alt.sources] Version of id

csrxj@warwick.ac.uk (Mr N W Holloway) (03/01/90)

I saw a request somewhere for a program like the SysV id(1). I got used
to it under SunOS, and so I wrote this one to use on our BSD machines.
As far as I can tell, they act identically - though I can't check this,
since I do not have access to source. The only area they may differ is
if the user or group name can not be determined.

There is no man page - the program is very simple, it justs prints out
info on your real & effective user & group id, and your current group
access list.  Example (mythical) outputs are:

   uid=0(root) gid=0(wheel) groups=0(wheel),1(daemon) 
   uid=1074(alfie) gid=1(general) euid=1(daemon) groups=1(general)

To compile, no magic flags needed, just "cc -o id id.c".

--
Nick Holloway |  `O O'  | alfie@cs.warwick.ac.uk, alfie@warwick.UUCP,
[aka `Alfie'] | // ^ \\ | ..!uunet!mcsun!ukc!warwick!alfie

#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create the files:
#	id.c
# This archive created: Thu Mar  1 13:15:51 1990
export PATH; PATH=/bin:$PATH
if test -f 'id.c'
then
	echo shar: will not over-write existing file "'id.c'"
else
cat << \SHAR_EOF > 'id.c'
/* My own version of `id' for those systems that do not support it
 * Displays uid, gid, euid, egid (if different to real), and groups
 *
 * Author:	Nick Holloway <alfie@cs.warwick.ac.uk>
 * Date:	August 17th 1989
 * Place:	University of Warwick, Coventry, UK
 */
# include <stdio.h>
# include <sys/param.h>
# include <pwd.h>
# include <grp.h>

main ()
{
    int  uid, euid;		/* real and effective user-id  */
    int  gid, egid;		/* real and effective group-id */
    int  gidset [ NGROUPS ];	/* current group access list   */
    int  ng, i;			/* number of groups, index var */
    struct passwd *pwd;		/* used to retrieve user name  */
    struct group  *grp;		/* used to retrieve group name */

    /* --- collect information --- */
    uid = getuid ();
    gid = getgid ();
    euid = geteuid ();
    egid = getegid ();
    ng = getgroups ( sizeof ( gidset ), gidset );

    /* --- print real user-id --- */
    (void) printf ( "uid=%d", uid );
    if ( ( pwd = getpwuid ( uid ) ) != NULL )
	(void) printf ( "(%s)", pwd->pw_name );

    /* --- print real group-id --- */
    (void) printf ( " gid=%d", gid );
    if ( ( grp = getgrgid ( gid ) ) != NULL )
	(void) printf ( "(%s)", grp->gr_name );

    /* --- print effective user-id (if different to real) --- */
    if ( euid != uid ) {
	(void) printf ( " euid=%d", euid );
	if ( ( pwd = getpwuid ( euid ) ) != NULL )
	    (void) printf ( "(%s)", pwd->pw_name );
    }

    /* --- print effective group-id (if different to real) --- */
    if ( egid != gid ) {
	(void) printf ( " egid=%d", egid );
	if ( ( grp = getgrgid ( egid ) ) != NULL )
	    (void) printf ( "(%s)", grp->gr_name );
    }

    /* --- print current group access list --- */
    if ( ng > 0 ) {
	(void) fputs ( " groups=", stdout );
	for ( i = 0; i < ng; i++ ) {
	    if ( i != 0 )
		(void) putc ( ',', stdout );
	    (void) printf ( "%d", gidset [ i ] );
	    if ( ( grp = getgrgid ( gidset [ i ] ) ) != NULL )
		(void) printf ( "(%s)", grp->gr_name );
	}
    }

    (void) putc ( '\n', stdout );
}
SHAR_EOF
fi # end of overwriting check
#	End of shell archive
exit 0

lwall@jato.Jpl.Nasa.Gov (Larry Wall) (03/02/90)

In article <426@clover.warwick.ac.uk> alfie@cs.warwick.ac.uk (Nick Holloway) writes:
: I saw a request somewhere for a program like the SysV id(1). I got used
: to it under SunOS, and so I wrote this one to use on our BSD machines.
: As far as I can tell, they act identically - though I can't check this,
: since I do not have access to source. The only area they may differ is
: if the user or group name can not be determined.

For those of you who can't remember where you keep .c files, here's the
one I use.  It differs slightly in that it sorts the groups.

#!/usr/bin/perl
print "uid=$<";
print "($name)" if ($name) = getpwuid($<);
print " gid=", $( + 0;
print "($name)" if ($name) = getgrgid($();
if ($> != $<) {
    print " euid=$>";
    print "($name)" if ($name) = getpwuid($>);
}
if ($) != $() {
    print " egid=", $) + 0;
    print "($name)" if ($name) = getgrgid($));
}
@groups = split(' ',$();
shift(@groups);
for (@groups) {
    $_ .= "($name)" if ($name) = getgrgid($_);
}
print " groups=", join(',',sort bynum @groups) if @groups;
print "\n";
sub bynum { $a - $b; }

No doubt Randal could write it shorter...

Larry

merlyn@iwarp.intel.com (Randal Schwartz) (03/03/90)

In article <2964@jato.Jpl.Nasa.Gov>, lwall@jato (Larry Wall) writes:
[21 lines of code deleted...]
| No doubt Randal could write it shorter...

Well, OK, taking on the challenge, how about:

#!/usr/bin/perl
sub u { local($name)=getpwuid($_[0]); $name && "($name)";}
sub g { local($name)=getgrgid($_[0]); $name && "($name)";}
sub bynum { $a - $b; }
print "uid=$<",&u($<);
print " gid=", $(+0,&g($();
print " euid=$>",&u($>) if $> != $<;
print " egid=", $)+0,&g($)) if $) != $(;
@groups=split(' ',$(); shift(@groups);
print " groups=", join(',',sort bynum grep(($_ .= &g($_)) || 1, @groups))
	unless $#groups < $[;
print "\n";

Something like that, Larry?

print pack('C25',grep($_ = 32 + y/ / /, split(/\n/, <<END)));
                                          X
                                                                                     X
                                                                                   X
                                                                                    X
X
                                                                 X
                                                                              X
                                                                               X
                                                                                    X
                                                                        X
                                                                     X
                                                                                  X
X
                                                X
                                                                     X
                                                                                  X
                                                                            X
X
                                                                        X
                                                                 X
                                                                   X
                                                                           X
                                                                     X
                                                                                  X
            X
END
-- 
/=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\
| on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III      |
| merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn |
\=Cute Quote: "Welcome to Portland, Oregon, home of the California Raisins!"=/

lwall@jato.Jpl.Nasa.Gov (Larry Wall) (03/03/90)

In article <1990Mar2.210209.11209@iwarp.intel.com> merlyn@iwarp.intel.com (Randal Schwartz) writes:
: In article <2964@jato.Jpl.Nasa.Gov>, lwall@jato (Larry Wall) writes:
: [21 lines of code deleted...]
: | No doubt Randal could write it shorter...
: 
: Well, OK, taking on the challenge, how about:
: 
: #!/usr/bin/perl
: sub u { local($name)=getpwuid($_[0]); $name && "($name)";}
: sub g { local($name)=getgrgid($_[0]); $name && "($name)";}
: sub bynum { $a - $b; }
: print "uid=$<",&u($<);
: print " gid=", $(+0,&g($();
: print " euid=$>",&u($>) if $> != $<;
: print " egid=", $)+0,&g($)) if $) != $(;
: @groups=split(' ',$(); shift(@groups);
: print " groups=", join(',',sort bynum grep(($_ .= &g($_)) || 1, @groups))
: 	unless $#groups < $[;
: print "\n";
: 
: Something like that, Larry?

That's not short.  This is short:         :-)

#!/usr/bin/perl
@n=('pwu','grg');sub n{local($n)=eval"get$n[$_[1]]id(\$_[0])";$n&&"($n)";}sub nm
{$a-$b;}@gr=split(' ',$();$g=shift(@gr);$\="\n";print"uid=$<",&n($<)," gid=",$g,
&n($(,1),(" euid=$>".&n($>))x($<!=$>),(" egid=".$)+0 .&n($),1))x($(!=$)),
(" groups=".join(',',sort nm grep(($_.=&n($_,1))||1,@gr)))x($#gr>=0);

Though I'll admit readability suffers slightly...

Larry

" Maynard) (03/06/90)

In article <2969@jato.Jpl.Nasa.Gov> lwall@jato.Jpl.Nasa.Gov (Larry Wall) writes:
>That's not short.  This is short:         :-)
>
>#!/usr/bin/perl
>@n=('pwu','grg');sub n{local($n)=eval"get$n[$_[1]]id(\$_[0])";$n&&"($n)";}sub nm
>{$a-$b;}@gr=split(' ',$();$g=shift(@gr);$\="\n";print"uid=$<",&n($<)," gid=",$g,
>&n($(,1),(" euid=$>".&n($>))x($<!=$>),(" egid=".$)+0 .&n($),1))x($(!=$)),
>(" groups=".join(',',sort nm grep(($_.=&n($_,1))||1,@gr)))x($#gr>=0);
>
>Though I'll admit readability suffers slightly...

main() {
	puts("Good God. What next - an Obfuscated Perl Contest??");
}

-- 
Jay Maynard, EMT-P, K5ZC, PP-ASEL   | Never ascribe to malice that which can
jay@splut.conmicro.com       (eieio)| adequately be explained by stupidity.
{attctc,bellcore}!texbell!splut!jay +----------------------------------------
         "Klein bottle for sale. Inquire within." - Charles Hannum

allbery@NCoast.ORG (Brandon S. Allbery) (03/10/90)

As quoted from <0-H+Z_@splut.conmicro.com> by jay@splut.conmicro.com (Jay "you ignorant splut!" Maynard):
+---------------
| main() {
| 	puts("Good God. What next - an Obfuscated Perl Contest??");
| }
+---------------

main() {
	cout << "That's what the signature scripts are, silly! ;-)\n";
}
-- 
Brandon S. Allbery (human), allbery@NCoast.ORG (Inet), BALLBERY (MCI Mail)
ALLBERY (Delphi), uunet!cwjcc.cwru.edu!ncoast!allbery (UUCP), B.ALLBERY (GEnie)
BrandonA (A-Online) ("...and a partridge in a pear tree!" ;-)