Discussion:
get logged in username
(too old to reply)
Mohsen Alimomeni
2009-09-17 16:14:18 UTC
Permalink
Hi,

How can I get the logged in username under remote command execution? I
am using openssh version 4.0 under fedora core 4.

more explanation:
when I do "ssh ***@host", I can get the username "admin", by the
command "who -m", since there is tty which the username is assigned to
it. But suppose I want to execute a remote command "ssh ***@host
myprog", I want to get the username inside the myprog. The command
"who -m", doesn't work because no tty is created for the user.
How can I get the username in this case?

Regards,
--
__ \ /_\\_-//_ Mohsen Alimomeni
Dennis Taylor
2009-09-18 16:28:01 UTC
Permalink
Do you have whoami available? If not try creating a temp file and then
using ls to examine who owns it. Then delete it when you're done.=20
-----Original Message-----
Sent: Thursday, September 17, 2009 2:45 AM
Subject: get logged in username
=20
Hi,
=20
How can I get the logged in username under remote command execution? I
am using openssh version 4.0 under fedora core 4.
=20
command "who -m", since there is tty which the username is assigned to
myprog", I want to get the username inside the myprog. The command
"who -m", doesn't work because no tty is created for the user.
How can I get the username in this case?
=20
Regards,
--
__ \ /_\\_-//_ Mohsen Alimomeni
=
Aarón Mizrachi
2009-09-18 16:43:59 UTC
Permalink
--nextPart3015526.rTeVldfUWg
Content-Type: Text/Plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Post by Mohsen Alimomeni
Hi,
=20
How can I get the logged in username under remote command execution? I
am using openssh version 4.0 under fedora core 4.
=20
command "who -m", since there is tty which the username is assigned to
myprog", I want to get the username inside the myprog. The command
"who -m", doesn't work because no tty is created for the user.
How can I get the username in this case?
=20
You are right, and this is because common programs doesn't login. However, =
for=20
log about ssh, check on /var/log/secure.... And, for running things, "ps -
edalf" or "ps axu" or "pstree -u".
Post by Mohsen Alimomeni
Regards,
--
__ \ /_\\_-//_ Mohsen Alimomeni
=20
And one more thing out of topic. Fedora 4 is outdated, vulnerable and is no=
t=20
releasing updates anymore, not even critical updates. If you are not using=
=20
this server as penetration testing lab pourporse, i strongly recommend you =
to=20
install the latest version.=20

=2D-=20
Ing. Aaron G. Mizrachi P. =20

http://www.unmanarc.com
Mobil 1: + 58 416-6143543
Mobil 2: + 58 424-2412503
BBPIN: 0x 247066C1

--nextPart3015526.rTeVldfUWg
Content-Type: application/pgp-signature; name=signature.asc
Content-Description: This is a digitally signed message part.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.12 (GNU/Linux)

iEYEABECAAYFAkqyiaAACgkQ2ixydRu83wBO0QCg1iMR3EL6DiMqC1NGxbNoip4C
TbIAoN2fdUl+yfsmqNOdlJTLsDmswhw/
=XI8Q
-----END PGP SIGNATURE-----

--nextPart3015526.rTeVldfUWg--
Greg Wooledge
2009-09-18 16:46:41 UTC
Permalink
Post by Mohsen Alimomeni
command "who -m", since there is tty which the username is assigned to
it.
You also know it's "admin" because you typed "admin" in the ssh command.
Don't be too quick to discount client-side knowledge... though clearly
it's up to you to determine whether the client can be trusted.
Post by Mohsen Alimomeni
myprog", I want to get the username inside the myprog. The command
"who -m", doesn't work because no tty is created for the user.
You could create a pseudoterminal by running "ssh -t ***@host myprog"
but I suspect this is a red herring. I think what you're really asking
is "How does a program determine the name of {a,the} user that maps to
the program's {effective,real} UID, apart from running some shell command
like 'whoami'?".
Post by Mohsen Alimomeni
How can I get the username in this case?
If myprog has access to libc, and host is a Unix-like system, then I
believe the standard approach is:

1) Call geteuid() to get the effective UID, or getuid() to get the "real"
UID -- whichever you actually want.

2) Call getpwuid() to map the UID to a human-readable name.

This really has nothing to do with ssh per se. It's just standard
Unix/libc programming.
H. Kurth Bemis
2009-09-18 16:48:55 UTC
Permalink
You'll need to get the owner of the running process which should be your
'admin' in this case.

If you're using a bash script, you could use "id", which will return
information about the user, like groups and uid. id -u returns my
userid, id -un returns my username. (GNU Coreutils 6.10)

If you're using another script, Python, PHP and PERL have built in
functions to obtain the current user.

If you are working in C, this might help you.
(Disclaimer: I'm not a C programmer)
http://www.gnu.org/software/libc/manual/html_node/Users-and-Groups.html#Users-and-Groups

Good Luck
~k
Post by Mohsen Alimomeni
Hi,
How can I get the logged in username under remote command execution? I
am using openssh version 4.0 under fedora core 4.
command "who -m", since there is tty which the username is assigned to
myprog", I want to get the username inside the myprog. The command
"who -m", doesn't work because no tty is created for the user.
How can I get the username in this case?
Regards,
--
__ \ /_\\_-//_ Mohsen Alimomeni
Dan Nelson
2009-09-18 17:17:44 UTC
Permalink
How can I get the logged in username under remote command execution? I am
using openssh version 4.0 under fedora core 4.
command "who -m", since there is tty which the username is assigned to
myprog", I want to get the username inside the myprog. The command
"who -m", doesn't work because no tty is created for the user.
How can I get the username in this case?
Try "whoami", or "id -un". Your system may also store the username for you
in the USER or LOGNAME environment variables.
--
Dan Nelson
***@allantgroup.com
Kevin Brott
2009-09-21 16:51:21 UTC
Permalink
Post by Mohsen Alimomeni
Hi,
How can I get the logged in username under remote command execution? I
am using openssh version 4.0 under fedora core 4.
command "who -m", since there is tty which the username is assigned to
myprog", I want to get the username inside the myprog. The command
"who -m", doesn't work because no tty is created for the user.
How can I get the username in this case?
Regards,
--
__ \ /_\\_-//_ Mohsen Alimomeni
If all you need is a tty to get 'who -m' to work, why not use 'ssh -tt
***@host' to allocate a tty to the session?

- --
# include <stddisclaimer.h>
/* Kevin Brott <***@gmail.com> */
Mohsen Alimomeni
2009-09-21 17:04:21 UTC
Permalink
Hi, Thanks for reply,

This is the exact scenario:
When I use the command "ssh ***@host", the user is authenticated by
a custom Pam module, and it's given the UID, GID and shell from a
custom nss module. The shell is also a custom CLI, which needs the
username - not the UID - to operate well. I can't get the username
from UID since there is no one-to-one correspondence between them.
Also the commands: who, id and .. doesn't give me the username, since
there is no tty for the session.

Regards,
Mohsen

On Thu, Sep 17, 2009 at 9:28 PM, Schaff Mark-C00070
What are trying to do. Need =A0information .
Thank you
Mark Schaff
Sent from my mobile
-----Original Message-----
Sent: Thursday, September 17, 2009 12:18 PM
Subject: get logged in username
Hi,
How can I get the logged in username under remote command execution? I
am using openssh version 4.0 under fedora core 4.
command "who -m", since there is tty which the username is assigned to
myprog", I want to get the username inside the myprog. The command
"who -m", doesn't work because no tty is created for the user.
How can I get the username in this case?
Regards,
--
__ \ /_\\_-//_ Mohsen Alimomeni
--=20
__ \ /_\\_-//_ Mohsen Alimomeni
Derek Martin
2009-09-22 23:44:17 UTC
Permalink
--/9DWx/yDrRhgMJTb
Content-Type: text/plain; charset=iso-8859-1
Content-Disposition: inline
Post by Mohsen Alimomeni
a custom Pam module, and it's given the UID, GID and shell from a
custom nss module. The shell is also a custom CLI, which needs the
username - not the UID - to operate well.
This is a fine example of why usernames and UIDs should always have a
1-to-1 correspondence. As far as the OS is concerned, the UID is what
identifies a user uniquely, not its username. Also, you've reduced
the accountability of your system: for example, if user "foo" and
user "bar" both have UID 1234, then when bar creates a file, it will
appear to have been created by foo (assuming foo appears first in
/etc/passwd, or is returned first in whatever mechanism your system
uses to look up UIDs and usernames). Likewise, when user bar does
something that normally gets logged, it will be logged under user foo
(given the same conditions).

This is, in general, bad. You likely may encounter other things which
break subtlely, or not so subtlely. I don't know what problem you're
trying to solve by doing this, but there's probably a better way.
--
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D


--/9DWx/yDrRhgMJTb
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)

iD8DBQFKuTdIdjdlQoHP510RAlxZAJ98n4ape8/VuqlA4IlSjT7M1mxNKQCfQYkj
TzFhuUzdL75bKU/WIDsC4+o=
=HNUK
-----END PGP SIGNATURE-----

--/9DWx/yDrRhgMJTb--
Mohsen Alimomeni
2009-09-23 15:38:31 UTC
Permalink
Thanks for all replies, I could solve the problem.

About the last comment, you are right. In my config, usernames and
UIDs have a 1-to-1 correspondence in /etc/passwd. But I use a custom
pam, nss module which have several users with the same UID, equal to a
user in passwd. These users are not allowed to create files or things
making trouble. A custom shell is assigned to them, which doesn't
allow most of these things.

Thanks everybody.
Mohsen
Post by Derek Martin
Post by Mohsen Alimomeni
a custom Pam module, and it's given the UID, GID and shell from a
custom nss module. The shell is also a custom CLI, which needs the
username - not the UID - to operate well.
This is a fine example of why usernames and UIDs should always have a
1-to-1 correspondence. =A0As far as the OS is concerned, the UID is what
identifies a user uniquely, not its username. =A0Also, you've reduced
the accountability of your system: for example, if user "foo" and
user "bar" both have UID 1234, then when bar creates a file, it will
appear to have been created by foo (assuming foo appears first in
/etc/passwd, or is returned first in whatever mechanism your system
uses to look up UIDs and usernames). =A0Likewise, when user bar does
something that normally gets logged, it will be logged under user foo
(given the same conditions).
This is, in general, bad. =A0You likely may encounter other things which
break subtlely, or not so subtlely. =A0I don't know what problem you're
trying to solve by doing this, but there's probably a better way.
--
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D
--=20
__ \ /_\\_-//_ Mohsen Alimomeni

Loading...