Solved

Retrieving group info on Unix

Posted on 2000-02-29
8
260 Views
Last Modified: 2012-05-04
Hi,

Could anyone help me to retrieve a users login group as well as all the other groups he is a member of. The problem is that a call like "getgroups" only returns the calling users groups and I have to find the groups of any arbitrary user. I am running cc on SCO Unix.

Thanks
Johan
0
Comment
Question by:johand
8 Comments
 
LVL 2

Expert Comment

by:obg
ID: 2569008
I don't think that is possible unless you have root privs. Is that a problem?
0
 

Author Comment

by:johand
ID: 2569435
Running the program with root priv. isnt a problem :)
0
 
LVL 86

Expert Comment

by:jkr
ID: 2569736
>>The problem is that a call like "getgroups" only returns
>>the calling users groups and I have to find the groups of
>>any arbitrary user

what about using 'setuid()'?
0
 
LVL 3

Expert Comment

by:ufolk123
ID: 2571394
May be one good option left is read the /etc/groups and read group information for each group.

The entry in this file is like this
Bdiosa::101:mvpevt,mvpss,mvpsv

where Bdiosa is a group with identifer(101) and have users mvpevt,mvpss,mvpsv
..
So you can get all information you want.Only thing is info is organised in form of group.You may to write a small code to search a user name for each of these entries and collect data.
Please get back for any clarifications.


0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:johand
ID: 2572503
To ufolk123 : I am going to reject your answer :) purely because I see that as the absolute last option. If there is any way that I can retrieve the groupinfo without having to parse the /etc/group file manually I would prefer that method. If no-one else can offer me such a solution I will ask you to resubmit your solution so that I can accept it.

To jkr : Could you clarify how to use setuid to do this ?
0
 
LVL 3

Expert Comment

by:Alisher_N
ID: 2573530
ufolk123's suggestion is quite easy if you use
grep user1 /etc/group
you immediately have a list of groups where user1 is a member, just cut first word till ':'  (semicolon), not too hard I think...
0
 
LVL 86

Accepted Solution

by:
jkr earned 200 total points
ID: 2574048
Well, I thought of sth. like

(pseudocode)

#include <unistd.h>
#include <sys/types.h>

/* ...*/

#define MAX_SIZE 256

uid_t myuid;
uid_t uid;
gid_t gids [ MAX_SIZE];

int ngroups = 0;

uid = <id of the user of interest>;

myuid = getuid();

setuid ( uid);

ngroups = getgroups ( MAX_SIZE, gids);

setuid ( myuid);


0
 

Author Comment

by:johand
ID: 2579225
Duh !! <slaps forehead repeatedly> of course ! Thanks a lot  :)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now