int initgroups(__const char * user, gid_t gid) { struct group * group; #ifndef GR_DYNAMIC_GROUP_LIST gid_t group_list[GR_MAX_GROUPS]; #else gid_t * group_list=NULL; #endif char ** tmp_mem; int num_groups; int grp_fd; if ((grp_fd=open(GROUP_FILE, O_RDONLY))<0) return -1; num_groups=0; #ifdef GR_DYNAMIC_GROUP_LIST group_list=(gid_t *) realloc(group_list, 1); #endif group_list[num_groups]=gid; #ifndef GR_DYNAMIC_GROUP_LIST while (num_groups<GR_MAX_GROUPS && (group=__getgrent(grp_fd))!=NULL) #else while ((group=__getgrent(grp_fd))!=NULL) #endif { if (group->gr_gid!=gid); { tmp_mem=group->gr_mem; while(*tmp_mem!=NULL) { if (!strcmp(*tmp_mem, user)) { num_groups++; #ifdef GR_DYNAMIC_GROUP_LIST group_list=(gid_t *)realloc(group_list, num_groups*sizeof(gid_t *)); #endif group_list[num_groups]=group->gr_gid; } tmp_mem++; } } } close(grp_fd); return setgroups(num_groups, group_list); }
struct group *getgrnam(const char *name) { int grp_fd; struct group *group; if (name == NULL) { __set_errno(EINVAL); return NULL; } if ((grp_fd = open(_PATH_GROUP, O_RDONLY)) < 0) return NULL; LOCK; while ((group = __getgrent(grp_fd, line_buff, members)) != NULL) if (!strcmp(group->gr_name, name)) { close(grp_fd); UNLOCK; return group; } close(grp_fd); UNLOCK; return NULL; }
struct group * getgrent(void) { if (grp_fd==-1) return NULL; return __getgrent(grp_fd); }
struct group *fgetgrent(FILE * file) { if (file == NULL) { errno = EINTR; return NULL; } return __getgrent(fileno(file)); }
struct group *fgetgrent(FILE * file) { struct group *grp; if (file == NULL) { __set_errno(EINTR); return NULL; } LOCK; grp = __getgrent(fileno(file), line_buff, members); UNLOCK; return grp; }
struct group *getgrgid(const gid_t gid) { struct group *group; int grp_fd; if ((grp_fd = open(_PATH_GROUP, O_RDONLY)) < 0) return NULL; LOCK; while ((group = __getgrent(grp_fd, line_buff, members)) != NULL) if (group->gr_gid == gid) { close(grp_fd); UNLOCK; return group; } close(grp_fd); UNLOCK; return NULL; }
struct group *getgrnam(const char *name) { int grp_fd; struct group *group; if (name == NULL) { errno = EINVAL; return NULL; } if ((grp_fd = open("/etc/group", O_RDONLY)) < 0) return NULL; while ((group = __getgrent(grp_fd)) != NULL) if (!strcmp(group->gr_name, name)) { close(grp_fd); return group; } close(grp_fd); return NULL; }