[alt.sources] euser - display the effective user name, SysV-ish

tneff@bfmny0.UUCP (Tom Neff) (07/08/89)

If you need a FAST way to generate the name of your current effective
user (including the effects of su(1) etc), this works.  It's not fancy.
Obviously it has to read /etc/passwd.  Feel free to hack and pass
around.

#! /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:
#	euser.c
# This archive created: Wed Jul  5 19:58:33 1989
export PATH; PATH=/bin:/usr/bin:$PATH
if test -f 'euser.c'
then
	echo shar: "will not over-write existing file 'euser.c'"
else
sed 's/^X//' << \SHAR_EOF > 'euser.c'
X/*
X	euser.c		rev: 05-Jul-89		Tom Neff
X
X	Display the name of our effective user at the moment.
X
X	System V-ish.  Have a blast.
X
X	Uses /etc/passwd.
X */
X
X#include <stdio.h>
X#include <string.h>
X
Xunsigned short geteuid(), euid;
X
XFILE *pass;
Xchar line[256], *name;
Xint uid = 0;
X
Xmain()
X{
X	euid = geteuid();
X	if ((pass = fopen("/etc/passwd","r")) == NULL)
X		exit(2);
X	while (fgets(line, sizeof(line), pass) != NULL)
X	{
X		name = strtok(line, ":");
X		strtok(NULL, ":");
X		uid = atoi(strtok(NULL, ":"));
X		if (euid == uid)
X		{
X			puts(name);
X			exit(0);
X		}
X	}
X	exit(1);
X}
SHAR_EOF
fi
exit 0
#	End of shell archive
-- 
"My God, Thiokol, when do you     \\	Tom Neff
want me to launch -- next April?"  \\	uunet!bfmny0!tneff

jfh@rpp386.Dallas.TX.US (John F. Haugh II) (07/08/89)

In article <14448@bfmny0.UUCP> tneff@bfmny0.UUCP (Tom Neff) writes:
>If you need a FAST way to generate the name of your current effective
>user (including the effects of su(1) etc), this works.  It's not fancy.
>Obviously it has to read /etc/passwd.  Feel free to hack and pass
>around.

>X	while (fgets(line, sizeof(line), pass) != NULL)

This is not a very good idea.  You make assumptions about the
password file which probably won't be holding true for too many
more releases of UNIX.

getpwuid() is going to be your best bet.  It should be hoped
that your local software vendor took the time to implement
getpwuid() in an efficient manner; such as using YP or ndbm or
whatever services are available.  In a distributed environment
the password file may not even reside on your machine completely.
-- 
John F. Haugh II                        +-Quote of the month club: ------------
VoiceNet: (512) 832-8832   Data: -8835  | "Computer security is to information
InterNet: jfh@rpp386.cactus.org         |  control as a chastity belt is to
UucpNet : <backbone>!bigtex!rpp386!jfh  +- birth control"    -- Doug Steves  --

jmb@patton.sgi.com (Jim Barton) (07/08/89)

Why waste time with this?  The system V 'id' command does the
same thing!  And, it's standard!

-- Jim Barton
Silicon Graphics Computer Systems    "UNIX: Live Free Or Die!"
jmb@sgi.sgi.com, sgi!jmb@decwrl.dec.com, ...{decwrl,sun}!sgi!jmb

  "I used to be disgusted, now I'm just amused."
			- Elvis Costello, 'Red Shoes'
--

wht@tridom.uucp (Warren Tucker) (07/09/89)

In article <14448@bfmny0.UUCP>, tneff@bfmny0.UUCP (Tom Neff) writes:
> If you need a FAST way to generate the name of your current effective
> user (including the effects of su(1) etc), this works.  It's not fancy.
> Obviously it has to read /etc/passwd.  Feel free to hack and pass
> around.

Try this:

#!/bin/sh
# shar:	Shell Archiver  (v1.22)
#
#	Run the following text with /bin/sh to create:
#	  whoami.c
#
sed 's/^X//' << 'SHAR_EOF' > whoami.c &&
X/*+-------------------------------------------------------------------------
X	whoami.c - System V 'whoami' - prints effective user name (euid)
X	...!gatech!emory!tridom!wht
X
XSystem V ain't got one of these goodies
X--------------------------------------------------------------------------*/
X/*+:EDITS:*/
X/*:01-27-1989-17:16-wht-creation */
X
X#include <stdio.h>
X#include <pwd.h>
X
X/*+-------------------------------------------------------------------------
X	who_am_i() - return first username in /etc/passwd that matches euid
X--------------------------------------------------------------------------*/
Xchar *
Xwho_am_i()
X{
Xstruct passwd *passwd;
Xstruct passwd *getpwuid();
X
X	passwd = getpwuid(geteuid());    /* gotta succeed unless MUCH is wrong */
X	endpwent();
X	if(passwd == (struct passwd *)0) /* much wrong has been known to happen */
X		return("???");
X	return(passwd->pw_name);
X
X}	/* end of who_am_i */
X
X/*+-------------------------------------------------------------------------
X	main(argc,argv,envp)
X--------------------------------------------------------------------------*/
Xmain(argc,argv,envp)
Xint argc;
Xchar **argv;
Xchar **envp;
X{
X	printf("%s\n",who_am_i());
X	exit(0);
X}	/* end of main */
X
X/* vi: set tabstop=4 shiftwidth=4: */
X/* end of whoami.c */
SHAR_EOF
chmod 0644 whoami.c || echo "restore of whoami.c fails"
exit 0
-- 
-------------------------------------------------------------------
Warren Tucker, Tridom Corporation       ...!gatech!emory!tridom!wht 
Sforzando (It., sfohr-tsahn'-doh).  A direction to perform the tone
or chord with special stress, or marked and sudden emphasis.

spl@mcnc.org (Steve Lamont) (07/09/89)

In article <37612@sgi.SGI.COM> jmb@patton.sgi.com (Jim Barton) writes:
<
<Why waste time with this?  The system V 'id' command does the
<same thing!  And, it's standard!
<
Not on BSD...  I just tried it and it ain't here...

-- 
							spl
Steve Lamont, sciViGuy			EMail:	spl@ncsc.org
North Carolina Supercomputing Center	Phone: (919) 248-1120
Box 12732/RTP, NC 27709

tneff@bfmny0.UUCP (Tom Neff) (07/09/89)

In article <37612@sgi.SGI.COM> jmb@patton.sgi.com (Jim Barton) writes:
>Why waste time with this?  The system V 'id' command does the
>same thing!  And, it's standard!

I wrote my little hack for situations where you need your effective
logname FAST.  It's not a complicated program.  Sys V's ID(1) is nice
but requires sed on the back end to strip out just the name, and that
slows things down.
-- 
"My God, Thiokol, when do you     \\	Tom Neff
want me to launch -- next April?"  \\	uunet!bfmny0!tneff

jfh@rpp386.Dallas.TX.US (John F. Haugh II) (07/10/89)

In article <37612@sgi.SGI.COM> jmb@patton.sgi.com (Jim Barton) writes:
>Why waste time with this?  The system V 'id' command does the
>same thing!  And, it's standard!

It isn't standard under BSD, and the version on AIX doesn't support
concurrent groups.

This version [ the one attached below ] should resolve all of those
problems.  And its public domain, so you can rip it off and send it
out with your BSD port.

Enjoy.
--
/*
 * id - print current process identification information
 *
 * This program was written by John F. Haugh II (jfh@rpp386.UUCP)
 * on 7/9/89 and hereby placed into the public domain.
 *
 * Syntax:
 *	id
 * Synopsis:
 *	Print the current process identifiers.  This includes the
 *	UID, GID, effective-UID and effective-GID.  In environment
 *	where concurrent groupsets exist, the list of groups in
 *	the concurrent groupset is printed as well.
 *
 * Notes:
 *	Straightforward implementation.  Get the IDs and print
 *	them out.  We key on the #define NGROUPS to decide if
 *	concurrent groups exist.  This may have to be changed to
 *	something more intelligent.
 */

#include <sys/types.h>
#include <stdio.h>
#include <grp.h>
#include <pwd.h>

main (argc, argv)
int	argc;
char	**argv;
{
#ifdef	NGROUPS
	int	groups[NGROUPS];
	int	ngroups;
#endif
	int	id;
	int	i;
	struct	passwd	*pw,
			*getpwuid();
	struct	group	*gr,
			*getgrgid();

	if (pw = getpwuid (id = getuid ()))
		printf ("uid=%d(%s)", id, pw->pw_name);
	else
		printf ("uid=%d", id);

	if (gr = getgrgid (id = getgid ()))
		printf (" gid=%d(%s)", id, gr->gr_name);
	else
		printf (" gid=%d", id);

	if (getuid () != geteuid ()) {
		if (pw = getpwuid (id = geteuid ()))
			printf (" euid=%d(%s)", id, pw->pw_name);
		else
			printf (" euid=%d", id);
	}
	if (getgid () != getegid ()) {
		if (gr = getgrgid (id = getegid ()))
			printf (" egid=%d(%s)", id, gr->gr_name);
		else
			printf (" egid=%d", id);
	}
#ifdef	NGROUPS
	if ((ngroups = getgroups (NGROUPS, groups)) != -1) {
		printf (" groupset=(");
		for (i = 0;i < ngroups;i++) {
			if (i)
				putchar (' ');

			if (gr = getgrgid (groups[i]))
				printf ("%s", groups[i], gr->gr_name);
			else
				printf ("%d", groups[i]);
		}
		putchar (')');
	}
#endif
	putchar ('\n');
}
-- 
John F. Haugh II                        +-Quote of the month club: ------------
VoiceNet: (512) 832-8832   Data: -8835  | "Computer security is to information
InterNet: jfh@rpp386.cactus.org         |  control as a chastity belt is to
UucpNet : <backbone>!bigtex!rpp386!jfh  +- birth control"    -- Doug Steves  --

logan@vsedev.VSE.COM (James Logan III) (07/10/89)

In article <14452@bfmny0.UUCP> tneff@bfmny0.UUCP (Tom Neff) writes:
# Sys V's ID(1) is nice
# but requires sed on the back end to strip out just the name, and that
# slows things down.

Sed is not required.  Try using expr(1)!

			-Jim
-- 
James Logan                       UUCP: uunet!vsedev!cpe6!logan
Data General Telecommunications   Inet: logan%cpe6@vsedev.vse.com
(301) 590-3069