2 * Copyright (c) 2005,2008,2010-2011 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>
24 #include <sys/param.h>
33 #endif /* STDC_HEADERS */
35 # if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
39 #endif /* HAVE_STRING_H */
42 #endif /* HAVE_STRINGS_H */
51 # define LINE_MAX 2048
58 static const char *grfile = "/etc/group";
59 static int gr_stayopen;
61 void mysetgrfile(const char *);
62 void mysetgrent(void);
63 void myendgrent(void);
64 struct group *mygetgrent(void);
65 struct group *mygetgrnam(const char *);
66 struct group *mygetgrgid(gid_t);
69 mysetgrfile(const char *file)
80 grf = fopen(grfile, "r");
82 fcntl(fileno(grf), F_SETFD, FD_CLOEXEC);
102 static struct group gr;
103 static char grbuf[LINE_MAX], *gr_mem[GRMEM_MAX+1];
108 if ((colon = fgets(grbuf, sizeof(grbuf), grf)) == NULL)
111 memset(&gr, 0, sizeof(gr));
112 if ((colon = strchr(cp = colon, ':')) == NULL)
116 if ((colon = strchr(cp = colon, ':')) == NULL)
120 if ((colon = strchr(cp = colon, ':')) == NULL)
123 gr.gr_gid = atoi(cp);
125 if (len > 0 && colon[len - 1] == '\n')
126 colon[len - 1] = '\0';
127 if (*colon != '\0') {
129 cp = strtok(colon, ",");
130 for (n = 0; cp != NULL && n < GRMEM_MAX; n++) {
132 cp = strtok(NULL, ",");
134 gr.gr_mem[n++] = NULL;
141 mygetgrnam(const char *name)
146 if ((grf = fopen(grfile, "r")) == NULL)
148 fcntl(fileno(grf), F_SETFD, FD_CLOEXEC);
152 while ((gr = mygetgrent()) != NULL) {
153 if (strcmp(gr->gr_name, name) == 0)
164 mygetgrgid(gid_t gid)
169 if ((grf = fopen(grfile, "r")) == NULL)
171 fcntl(fileno(grf), F_SETFD, FD_CLOEXEC);
175 while ((gr = mygetgrent()) != NULL) {
176 if (gr->gr_gid == gid)