473,394 Members | 1,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

On unix.. Know if the user is in a specific system group ?

I'm doing a program that needs to know if the user is in a specific
group in the system.

How do I do it ? :-)

I'm desesperate and I'm starting to think about executing a shell
command to find the answer.

Anyone ever made that ? I can't find anything when searching with
google.

Thanks,
--Ben

Sep 6 '07 #1
9 1445
In article <11**********************@o80g2000hse.googlegroups .com>,
Benoit Lefebvre <be*************@gmail.comwrote:
>I'm doing a program that needs to know if the user is in a specific
group in the system.
>How do I do it ? :-)
>I'm desesperate and I'm starting to think about executing a shell
command to find the answer.
Try comp.unix.programmer (who might perhaps refer you to getgroups())
--
Is there any thing whereof it may be said, See, this is new? It hath
been already of old time, which was before us. -- Ecclesiastes
Sep 6 '07 #2
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote:
In article <11**********************@o80g2000hse.googlegroups .com>,
Benoit Lefebvre <be*************@gmail.comwrote:
I'm doing a program that needs to know if the user is in a specific
group in the system.
How do I do it ? :-)
I'm desesperate and I'm starting to think about executing a shell
command to find the answer.

Try comp.unix.programmer (who might perhaps refer you to getgroups())
getgroups() isn't POSIX standard *I think*; it's a convenience function
provided by a lot of unixes. I think what you want is getgrnam
or getgrgid, depending on if you're specifying group name or ID.
Sep 6 '07 #3
On Sep 6, 12:50 pm, Rob Hoelz <ho...@wisc.eduwrote:
rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote:
In article <1189093439.487319.294...@o80g2000hse.googlegroups .com>,
Benoit Lefebvre <benoit.lefeb...@gmail.comwrote:
>I'm doing a program that needs to know if the user is in a specific
>group in the system.
>How do I do it ? :-)
>I'm desesperate and I'm starting to think about executing a shell
>command to find the answer.
Try comp.unix.programmer (who might perhaps refer you to getgroups())

getgroups() isn't POSIX standard *I think*; it's a convenience function
provided by a lot of unixes.
getgroups() is POSIX, setgroups() is not.

Robert Gamble

Sep 6 '07 #4
On Sep 6, 12:50 pm, Rob Hoelz <ho...@wisc.eduwrote:
rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote:
In article <1189093439.487319.294...@o80g2000hse.googlegroups .com>,
Benoit Lefebvre <benoit.lefeb...@gmail.comwrote:
>I'm doing a program that needs to know if the user is in a specific
>group in the system.
>How do I do it ? :-)
>I'm desesperate and I'm starting to think about executing a shell
>command to find the answer.
Try comp.unix.programmer (who might perhaps refer you to getgroups())

getgroups() isn't POSIX standard *I think*; it's a convenience function
provided by a lot of unixes. I think what you want is getgrnam
or getgrgid, depending on if you're specifying group name or ID.
Thanks, you aligned me in the right direction with getgrnam

I started with these sources :
http://samba.org/ftp/tridge/nsswitch-tests/getgrnam.c

And here is what I came up with:

It's checking if the user that runs the script from sudo is in the
tempsa group or not.
Because the environment variable "SUDO_USER" is generated by sudo just
before running this command, I know I can trust it.
This program will only be run directly from SUDO from some users

--8<----------------------------------------
struct group *gr;
int nolog;

gr = getgrnam("tempsa");

nolog = 1;
if (gr->gr_mem != NULL) {
int i =0;
while(gr->gr_mem[i] != NULL) {
if (!strcmp(gr->gr_mem[i],getenv("SUDO_USER"))) {
nolog = 0;
}
i++;
}
}
--------------------------------------------

thanks,
--Ben

Sep 6 '07 #5
In article <11*********************@y42g2000hsy.googlegroups. com>,
Benoit Lefebvre <be*************@gmail.comwrote:
>And here is what I came up with:
>It's checking if the user that runs the script from sudo is in the
tempsa group or not.
Because the environment variable "SUDO_USER" is generated by sudo just
before running this command, I know I can trust it.
This program will only be run directly from SUDO from some users
And will you enforce that, by ensuring that the binary will
be inaccessible to anyone who hasn't already sudo'd to the proper
group? And if it -is- inaccessible to all but the proper people,
then why bother checking the group??

struct group *gr;
int nolog;

gr = getgrnam("tempsa");

nolog = 1;
if (gr->gr_mem != NULL) {
int i =0;
while(gr->gr_mem[i] != NULL) {
if (!strcmp(gr->gr_mem[i],getenv("SUDO_USER"))) {
nolog = 0;
}
i++;
}
}
Once you have found a nolog condition, what is the point
in continuing in the loop? If you used

while(nolog && gr->gr_mem[i] != NULL)

then you would not bother with the test if you've already passed;
or, better yet, use a break; after the nolog = 0;
--
Is there any thing whereof it may be said, See, this is new? It hath
been already of old time, which was before us. -- Ecclesiastes
Sep 6 '07 #6
Rob Hoelz <ho***@wisc.eduwrites:
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote:
>In article <11**********************@o80g2000hse.googlegroups .com>,
Benoit Lefebvre <be*************@gmail.comwrote:
>I'm doing a program that needs to know if the user is in a specific
group in the system.
>How do I do it ? :-)
>I'm desesperate and I'm starting to think about executing a shell
command to find the answer.

Try comp.unix.programmer (who might perhaps refer you to getgroups())

getgroups() isn't POSIX standard *I think*; it's a convenience function
provided by a lot of unixes. I think what you want is getgrnam
or getgrgid, depending on if you're specifying group name or ID.
This kind of speculation is exactly why comp.unix.programmer is a
better place to discuss this. The experts over there are much more
likely to know the answer -- and if they don't, the other experts will
quickly correct them.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 6 '07 #7
On Thu, 06 Sep 2007 08:43:59 -0700, in comp.lang.c , Benoit Lefebvre
<be*************@gmail.comwrote:
>I'm doing a program that needs to know if the user is in a specific
group in the system.

How do I do it ? :-)
grep /etc/group username
>Anyone ever made that ? I can't find anything when searching with
google.
comp.unix.progammer maybe?
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Sep 6 '07 #8
Mark McIntyre <ma**********@spamcop.netwrites:
On Thu, 06 Sep 2007 08:43:59 -0700, in comp.lang.c , Benoit Lefebvre
<be*************@gmail.comwrote:
>>I'm doing a program that needs to know if the user is in a specific
group in the system.

How do I do it ? :-)

grep /etc/group username
Nope.
>>Anyone ever made that ? I can't find anything when searching with
google.

comp.unix.progammer maybe?
Yup.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 6 '07 #9
On Fri, 07 Sep 2007 00:28:50 +0100, Mark McIntyre
<ma**********@spamcop.netwrote:
>On Thu, 06 Sep 2007 08:43:59 -0700, in comp.lang.c , Benoit Lefebvre
<be*************@gmail.comwrote:
>>I'm doing a program that needs to know if the user is in a specific
group in the system.

How do I do it ? :-)

grep /etc/group username
grep: can't open username

(works better t'other way round.:)
>
>>Anyone ever made that ? I can't find anything when searching with
google.

comp.unix.progammer maybe?
Yes.

--
Al Balmer
Sun City, AZ
Sep 7 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: jrefactors | last post by:
If the C programs have UNIX system calls such as fork(), alarm(), etc.., we should call it UNIX programs, not traditional C programs? We couldn't compile the programs with system calls using VC++...
5
by: jrefactors | last post by:
when people say unix programmer, does it mean they write programs in unix environment,and those programs are run in unix platform? it is not necessary they are using unix function calls? I heard...
4
by: Alan | last post by:
Is anyone know the equivalent term used for Dynamic Link Library (DLL) in unix environment?? Thanks Alan
48
by: Daniel Rudy | last post by:
Hello, On a x86 machine, what is the format of a pointer in C? I know for a fact that the x86 p-mode uses a /selector:offset/ notation where the selector is defined in either the GDT or LDT. ...
9
by: craig.overton | last post by:
All, I am currently developing an FTP class in VB.NET. It's kid tested, mother approved when trying to access an FTP Server on a Windows box meaning I can connect, run commands, upload and...
0
by: Holly | last post by:
I copied this code that works to connect into Unix. I am looking for a way to get it to work with a secure Unix box. Anyone have any insights on how to do this? I am trying to build an sftp...
21
by: Tom Gur | last post by:
Hi, It's seems that csh and tcsh acts a bit different when handling special characters in quotes. i.e: if i'll supply my program with the following arguments: -winpath "c:\\temp\\" tcsh will...
65
by: Hongyu | last post by:
Dear all: I am trying to write to a file with full directory name and file name specified (./outdir/mytestout.txt where . is the current directory) in C programming language and under Unix, but...
0
amitpatel66
by: amitpatel66 | last post by:
There is always a requirement that in Oracle Applications, the Concurrent Program need to be execute programatically based on certain conditions/validations: Concurrent programs can be executed...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.