Solved

Retrieving group info on Unix

Posted on 2000-02-29
8
304 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

688 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