2 * Copyright (c) 2005,2008,2010-2013 Todd C. Miller <Todd.Miller@courtesan.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 * Trivial replacements for the libc getgr{uid,nam}() routines.
23 #include <sys/types.h>
32 #endif /* STDC_HEADERS */
34 # if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
38 #endif /* HAVE_STRING_H */
41 #endif /* HAVE_STRINGS_H */
50 # define LINE_MAX 2048
57 static const char *grfile = "/etc/group";
58 static int gr_stayopen;
60 void mysetgrfile(const char *);
61 void mysetgrent(void);
62 void myendgrent(void);
63 struct group *mygetgrent(void);
64 struct group *mygetgrnam(const char *);
65 struct group *mygetgrgid(gid_t);
68 mysetgrfile(const char *file)
79 grf = fopen(grfile, "r");
81 fcntl(fileno(grf), F_SETFD, FD_CLOEXEC);
101 static struct group gr;
102 static char grbuf[LINE_MAX], *gr_mem[GRMEM_MAX+1];
107 if ((colon = fgets(grbuf, sizeof(grbuf), grf)) == NULL)
110 memset(&gr, 0, sizeof(gr));
111 if ((colon = strchr(cp = colon, ':')) == NULL)
115 if ((colon = strchr(cp = colon, ':')) == NULL)
119 if ((colon = strchr(cp = colon, ':')) == NULL)
122 gr.gr_gid = atoi(cp);
124 if (len > 0 && colon[len - 1] == '\n')
125 colon[len - 1] = '\0';
126 if (*colon != '\0') {
128 cp = strtok(colon, ",");
129 for (n = 0; cp != NULL && n < GRMEM_MAX; n++) {
131 cp = strtok(NULL, ",");
133 gr.gr_mem[n++] = NULL;
140 mygetgrnam(const char *name)
145 if ((grf = fopen(grfile, "r")) == NULL)
147 fcntl(fileno(grf), F_SETFD, FD_CLOEXEC);
151 while ((gr = mygetgrent()) != NULL) {
152 if (strcmp(gr->gr_name, name) == 0)
163 mygetgrgid(gid_t gid)
168 if ((grf = fopen(grfile, "r")) == NULL)
170 fcntl(fileno(grf), F_SETFD, FD_CLOEXEC);
174 while ((gr = mygetgrent()) != NULL) {
175 if (gr->gr_gid == gid)