I'm trying to find all groups to which user belongs in my UNIX system, and that for each user.Implementation has to be in C. Here is my code:
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <pwd.h>
#include <grp.h>
static void error_fatal(char* msg)
{ perror(msg); exit(EXIT_FAILURE); }
int main(int argc, char** argv) {
struct group* grp;
struct passwd* pwd;
char *name;
int i = 0;
setpwent();
while((pwd = getpwent()) != NULL){
if( ( name = (char*) malloc( (strlen(pwd->pw_name)+1)*sizeof(char))) == NULL ) error_fatal("malloc");
strcpy(name, pwd->pw_name);
printf("%s:\n", name);
setgrent();
while( (grp = getgrent()) != NULL ) {
for( i=0; i < (sizeof(grp->gr_mem)/sizeof(grp->gr_mem[0])); i++ ){
if( /*strlen(&grp->gr_mem[i][0]) == strlen(name) && */ !strcmp(grp->gr_mem[i], name) )
printf("%s\n", name);
} }
endgrent();
free(name);
}
endpwent();
return 0;
}
But I get segmentation fault after "root:" output. I'm pretty sure the problem is in accessing list of members in the fourth field of /etc/group file (see man 5 group for details).
So, basically my problem would be to find out how many members each group has, so my counter(i in program, the last for loop) would have nice upper boundary.
Your problem is here:
struct groupis defined as:You're assuming
gr_memis an array but it is not. It is a pointer pointing to the first element of an array. Sosizeof(grp->gr_mem)/sizeof(grp->gr_mem[0])gives you the size of a pointer, probably 8 on your system. So if a user has less than 8 groups, you'll end up reading past the end of the arraygr_mempoints to the start of.Because the array pointed to by
gr_memis NULL terminated, finding that terminator tells you when the loop is done: